mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-20 11:39:43 +00:00
entity ai classes, pigs should look around
This commit is contained in:
parent
6f603fcbe9
commit
8d454e5ec9
12 changed files with 206 additions and 38 deletions
|
|
@ -85,7 +85,6 @@ require_once(FILE_PATH . "/src/entity/Attachable.php");
|
|||
require_once(FILE_PATH . "/src/entity/Damageable.php");
|
||||
require_once(FILE_PATH . "/src/entity/Breedable.php");
|
||||
require_once(FILE_PATH . "/src/entity/Damageable.php");
|
||||
require_once(FILE_PATH . "/src/entity/Pathfindable.php");
|
||||
require_once(FILE_PATH . "/src/entity/Entity.php");
|
||||
require_once(FILE_PATH . "/src/entity/Living.php");
|
||||
require_once(FILE_PATH . "/src/entity/Creature.php");
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@ move methods
|
|||
*/
|
||||
abstract class Animal extends Creature implements Ageable, Breedable{
|
||||
|
||||
public $parent;
|
||||
|
||||
public function isBaby(){
|
||||
if(!isset($this->data["IsBaby"])){
|
||||
$this->data["IsBaby"] = false;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ class Entity extends Position{
|
|||
public static $updateOnTick;
|
||||
public $isCollidable;
|
||||
public $canBeAttacked;
|
||||
public $moveTime;
|
||||
public $moveTime, $lookTime, $idleTime;
|
||||
public $needsUpdate;
|
||||
/**
|
||||
* @var AxisAlignedBB
|
||||
|
|
@ -93,6 +93,7 @@ class Entity extends Position{
|
|||
$this->position = array("level" => $this->level, "x" => &$this->x, "y" => &$this->y, "z" => &$this->z, "yaw" => &$this->yaw, "pitch" => &$this->pitch);
|
||||
$this->height = 0.98;
|
||||
$this->moveTime = 0;
|
||||
$this->lookTime = 0;
|
||||
switch($this->class){
|
||||
case ENTITY_PLAYER:
|
||||
$this->player = $this->data["player"];
|
||||
|
|
@ -150,8 +151,9 @@ class Entity extends Position{
|
|||
$this->speedY += $vY;
|
||||
$this->speedZ += $vZ;
|
||||
}
|
||||
|
||||
public function isMoving(){
|
||||
return ($this->speedX > 0.01 || $this->speedX < -0.01) || ($this->speedY > 0.01 || $this->speedY < -0.01) || ($this->speedZ > 0.01 || $this->speedZ < -0.01);
|
||||
return $this->moveTime > 0 || ($this->speedX > 0.01 || $this->speedX < -0.01) || ($this->speedY > 0.01 || $this->speedY < -0.01) || ($this->speedZ > 0.01 || $this->speedZ < -0.01);
|
||||
}
|
||||
public function setVelocity($vX, $vY = 0, $vZ = 0){
|
||||
if($vX instanceof Vector3){
|
||||
|
|
@ -520,6 +522,13 @@ class Entity extends Position{
|
|||
if($this->moveTime > 0){
|
||||
--$this->moveTime;
|
||||
}
|
||||
if($this->lookTime > 0){
|
||||
--$this->lookTime;
|
||||
}
|
||||
if($this->idleTime > 0){
|
||||
--$this->idleTime;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -627,7 +636,6 @@ class Entity extends Position{
|
|||
}
|
||||
}
|
||||
}else{
|
||||
|
||||
$this->updatePosition($this->x, $this->y, $this->z, $this->yaw, $this->pitch);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,22 @@
|
|||
<?php
|
||||
|
||||
abstract class Living extends Entity implements Damageable, Pathfindable{
|
||||
|
||||
abstract class Living extends Entity implements Damageable{
|
||||
public $pathFinder, $pathFound, $path, $pathNavigator, $target, $ai;
|
||||
public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
|
||||
$this->pathFinder = new AStar($this);
|
||||
$this->pathFound = false;
|
||||
$this->target = false;
|
||||
$this->path = null;
|
||||
$this->ai = new EntityAI($this);
|
||||
$this->pathNavigator = new PathNavigator($this);
|
||||
parent::__construct($level, $eid, $class, $type, $data);
|
||||
$this->canBeAttacked = true;
|
||||
}
|
||||
|
||||
public function getPathfinder(){ //Placeholder
|
||||
return $this->pathFinder;
|
||||
}
|
||||
public function update(){
|
||||
|
||||
$this->ai->updateTasks();
|
||||
parent::update();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
<?php
|
||||
/*
|
||||
Illegal features
|
||||
*/
|
||||
interface Pathfindable{
|
||||
function getPathFinder();
|
||||
}
|
||||
46
src/entity/ai/EntityAI.php
Normal file
46
src/entity/ai/EntityAI.php
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
class EntityAI
|
||||
{
|
||||
/**
|
||||
* @var Living
|
||||
*/
|
||||
public $entity;
|
||||
|
||||
/**
|
||||
* @var TaskBase[]
|
||||
*/
|
||||
protected $tasks;
|
||||
|
||||
public $lastTask;
|
||||
|
||||
public function __construct($entity){
|
||||
$this->entity = $entity;
|
||||
$this->tasks = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a task for entity
|
||||
* @param TaskBase $task
|
||||
*/
|
||||
public function addTask(TaskBase $task){
|
||||
console("[DEBUG] Adding new task...",true, true, 2);
|
||||
$this->tasks[] = $task;
|
||||
}
|
||||
|
||||
public function updateTasks(){
|
||||
foreach($this->tasks as $t){
|
||||
if($t->wasExecuted($this)){
|
||||
$t->onUpdate($this);
|
||||
continue;
|
||||
}
|
||||
if($t->shouldBeExecuted($this)){
|
||||
$t->onStart($this);
|
||||
$t->onUpdate($this);
|
||||
$this->lastTask = $this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
27
src/entity/ai/tasks/TaskBase.php
Normal file
27
src/entity/ai/tasks/TaskBase.php
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
abstract class TaskBase
|
||||
{
|
||||
/**
|
||||
* @param EntityAI $ai
|
||||
* @return bool
|
||||
*/
|
||||
abstract function shouldBeExecuted(EntityAI $ai);
|
||||
|
||||
/**
|
||||
* @param EntityAI $ai
|
||||
* @return bool
|
||||
*/
|
||||
abstract function wasExecuted(EntityAI $ai);
|
||||
|
||||
/**
|
||||
* @param EntityAI $ai
|
||||
*/
|
||||
abstract function onStart(EntityAI $ai);
|
||||
|
||||
/**
|
||||
* @param EntityAI $ai
|
||||
*/
|
||||
abstract function onUpdate(EntityAI $ai);
|
||||
}
|
||||
|
||||
26
src/entity/ai/tasks/TaskFindPath.php
Normal file
26
src/entity/ai/tasks/TaskFindPath.php
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
class TaskFindPath extends TaskBase
|
||||
{
|
||||
public function shouldBeExecuted(EntityAI $ai)
|
||||
{
|
||||
return !$ai->entity->pathFound && $ai->entity->target instanceof Entity && $ai->entity->moveTime <= 0;
|
||||
}
|
||||
|
||||
public function onUpdate(EntityAI $ai)
|
||||
{
|
||||
console("find path!");
|
||||
}
|
||||
public function wasExecuted(EntityAI $ai)
|
||||
{
|
||||
return $ai->entity->pathFound;
|
||||
}
|
||||
public function onStart(EntityAI $ai)
|
||||
{
|
||||
//todo?
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
47
src/entity/ai/tasks/TaskLookAround.php
Normal file
47
src/entity/ai/tasks/TaskLookAround.php
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
class TaskLookAround extends TaskBase
|
||||
{
|
||||
private $finYaw = 0;
|
||||
public function shouldBeExecuted(EntityAI $ai)
|
||||
{
|
||||
return !$ai->entity->isMoving() && $ai->entity->lookTime <= 0 && mt_rand(0,30) == 1 && $ai->entity->idleTime <= 0;
|
||||
}
|
||||
|
||||
public function wasExecuted(EntityAI $ai){
|
||||
|
||||
return $ai->entity->lookTime > 0;
|
||||
}
|
||||
|
||||
public function onStart(EntityAI $ai){
|
||||
$ai->entity->lookTime = 120;
|
||||
$this->finYaw = $ai->entity->yaw;
|
||||
}
|
||||
|
||||
public function onUpdate(EntityAI $ai)
|
||||
{
|
||||
//90 - 75 = -45deg rot
|
||||
//60 - 30 = 90deg rot
|
||||
//30 - 15 = -45deg rot
|
||||
if($ai->entity->lookTime === 1){
|
||||
$ai->entity->idleTime = mt_rand(40, 200);
|
||||
}
|
||||
if(Utils::in_range($ai->entity->lookTime, 105, 120) || Utils::in_range($ai->entity->lookTime, 15, 30)){
|
||||
$ai->entity->yaw -= 3;
|
||||
}
|
||||
if(Utils::in_range($ai->entity->lookTime, 50, 80)){
|
||||
$ai->entity->yaw += 3;
|
||||
}
|
||||
|
||||
$pk = new RotateHeadPacket();
|
||||
$pk->eid = $ai->entity->eid;
|
||||
$pk->yaw = $ai->entity->yaw;
|
||||
$ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk);
|
||||
}
|
||||
public function testLook($pos){
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
26
src/entity/ai/tasks/TaskRandomWalk.php
Normal file
26
src/entity/ai/tasks/TaskRandomWalk.php
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
class TaskRandomWalk extends TaskBase
|
||||
{
|
||||
public function shouldBeExecuted(EntityAI $ai)
|
||||
{
|
||||
return !$ai->entity->pathFound && $ai->entity->moveTime <= 0 && $ai->entity->lookTime <= 0 && !($ai->entity->target instanceof Entity) && mt_rand(0,3) == 1;
|
||||
}
|
||||
|
||||
public function onUpdate(EntityAI $ai)
|
||||
{
|
||||
console("random walk");
|
||||
}
|
||||
public function wasExecuted(EntityAI $ai)
|
||||
{
|
||||
return $ai->entity->moveTime > 0;
|
||||
}
|
||||
public function onStart(EntityAI $ai)
|
||||
{
|
||||
$ai->entity->moveTime = 100;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -1,16 +1,14 @@
|
|||
<?php
|
||||
class Pig extends Animal{
|
||||
const TYPE = MOB_PIG;
|
||||
public $pathFinder, $pathFound, $path, $server, $pathNavigator;
|
||||
public $server;
|
||||
public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
|
||||
$this->pathFinder = new AStar($this); //make it before everything else as entity class constructor sends update
|
||||
$this->pathFound = false;
|
||||
parent::__construct($level, $eid, $class, $type, $data);
|
||||
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:10, "generic");
|
||||
$this->server = ServerAPI::request();
|
||||
$this->pathNavigator = new PathNavigator($this);
|
||||
$this->setSize($this->isBaby() ? 0.45 : 0.9, $this->isBaby() ? 0.45 : 0.9);
|
||||
$this->setName('Pig');
|
||||
$this->ai->addTask(new TaskLookAround());
|
||||
$this->update();
|
||||
}
|
||||
public function isFood($id){
|
||||
|
|
@ -22,22 +20,4 @@ class Pig extends Animal{
|
|||
array(($this->fire > 0 ? COOKED_PORKCHOP:RAW_PORKCHOP), 0, mt_rand(0,2)),
|
||||
);
|
||||
}
|
||||
|
||||
public function update(){
|
||||
/*if($this->pathFound && $this->path !== false && count($this->path) > 0){
|
||||
$this->pathFound = !$this->pathNavigator->followPath($this->path);
|
||||
}else{
|
||||
$start = new MinecraftNode(ceil($this->x), ceil($this->y), ceil($this->z));
|
||||
$stop = $start->add(mt_rand(-10, 10), 0, mt_rand(-10, 10));
|
||||
$this->path = $this->pathFinder->findPath($start, $stop); //allows diagonals, path should be ~5 blocks
|
||||
//var_dump($this->path);
|
||||
if($this->path !== false && count($this->path) > 0){
|
||||
$this->pathFound = true;
|
||||
}else{
|
||||
$this->moveTime = 30;
|
||||
}
|
||||
}*/ //uncomment to enable test moving
|
||||
|
||||
parent::update();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -48,6 +48,11 @@ class Utils{
|
|||
return sha1(strtolower($variable));
|
||||
}
|
||||
}
|
||||
|
||||
public static function in_range($num, $min, $max){
|
||||
return $num >= $min && $num <= $max;
|
||||
}
|
||||
|
||||
public function xrange($start, $limit, $step = 1) {
|
||||
if ($start <= $limit) {
|
||||
if ($step <= 0) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue