mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-21 12:09:43 +00:00
project uses tabs instead of spaces(as in CONTRIBUTING.md)
This commit is contained in:
parent
f00318b6ed
commit
62571f26cd
115 changed files with 2291 additions and 2364 deletions
|
|
@ -2,74 +2,74 @@
|
|||
|
||||
class EntityAI
|
||||
{
|
||||
/**
|
||||
* @var MobController
|
||||
*/
|
||||
public $mobController;
|
||||
/**
|
||||
* @var Living
|
||||
*/
|
||||
public $entity;
|
||||
|
||||
/**
|
||||
* @var TaskBase[]
|
||||
*/
|
||||
protected $tasks;
|
||||
|
||||
public $lastTask;
|
||||
|
||||
public $counter = 0;
|
||||
|
||||
public function __construct($entity){
|
||||
$this->entity = $entity;
|
||||
$this->tasks = [];
|
||||
$this->mobController = new MobController($entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a task for entity
|
||||
* @param TaskBase $task
|
||||
*/
|
||||
public function addTask(TaskBase $task){
|
||||
console("[DEBUG] Adding new task...",true, true, 2);
|
||||
$this->tasks[$task->__toString()] = $task;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param mixed $id classname
|
||||
* @return TaskBase | false
|
||||
*/
|
||||
public function getTask($id){
|
||||
return isset($this->tasks[$id]) ? $this->tasks[$id] : false;
|
||||
}
|
||||
|
||||
public function isStarted($id){
|
||||
$task = @$this->getTask($id);
|
||||
return $task instanceof TaskBase && $task->isStarted;
|
||||
}
|
||||
/**
|
||||
* @var MobController
|
||||
*/
|
||||
public $mobController;
|
||||
/**
|
||||
* @var Living
|
||||
*/
|
||||
public $entity;
|
||||
|
||||
/**
|
||||
* @var TaskBase[]
|
||||
*/
|
||||
protected $tasks;
|
||||
|
||||
public $lastTask;
|
||||
|
||||
public $counter = 0;
|
||||
|
||||
public function __construct($entity){
|
||||
$this->entity = $entity;
|
||||
$this->tasks = [];
|
||||
$this->mobController = new MobController($entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a task for entity
|
||||
* @param TaskBase $task
|
||||
*/
|
||||
public function addTask(TaskBase $task){
|
||||
console("[DEBUG] Adding new task...",true, true, 2);
|
||||
$this->tasks[$task->__toString()] = $task;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param mixed $id classname
|
||||
* @return TaskBase | false
|
||||
*/
|
||||
public function getTask($id){
|
||||
return isset($this->tasks[$id]) ? $this->tasks[$id] : false;
|
||||
}
|
||||
|
||||
public function isStarted($id){
|
||||
$task = @$this->getTask($id);
|
||||
return $task instanceof TaskBase && $task->isStarted;
|
||||
}
|
||||
|
||||
public function updateTasks(){
|
||||
if($this->counter % 3 != 0){
|
||||
return;
|
||||
}
|
||||
foreach($this->tasks as $t){
|
||||
if(!$t->isStarted && $t->canBeExecuted($this)){
|
||||
$t->isStarted = true;
|
||||
$t->onStart($this);
|
||||
}
|
||||
if($t->isStarted){
|
||||
$t->onUpdate($this);
|
||||
if($t->selfCounter <= 0){
|
||||
$t->isStarted = false;
|
||||
$t->onEnd($this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct(){
|
||||
unset($this->entity);
|
||||
}
|
||||
|
||||
public function updateTasks(){
|
||||
if($this->counter % 3 != 0){
|
||||
return;
|
||||
}
|
||||
foreach($this->tasks as $t){
|
||||
if(!$t->isStarted && $t->canBeExecuted($this)){
|
||||
$t->isStarted = true;
|
||||
$t->onStart($this);
|
||||
}
|
||||
if($t->isStarted){
|
||||
$t->onUpdate($this);
|
||||
if($t->selfCounter <= 0){
|
||||
$t->isStarted = false;
|
||||
$t->onEnd($this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct(){
|
||||
unset($this->entity);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,70 +2,70 @@
|
|||
|
||||
class MobController
|
||||
{
|
||||
/**
|
||||
* @var Entity
|
||||
*/
|
||||
public $entity;
|
||||
|
||||
public function __construct($e){
|
||||
$this->entity = $e;
|
||||
}
|
||||
|
||||
public function moveNonInstant($x, $y, $z){
|
||||
if($x == 0 && $y == 0 && $z == 0){
|
||||
return false; //failed
|
||||
}
|
||||
|
||||
$ox = ($x > 0 ? 1 : ($x < 0 ? -1 : 0));
|
||||
$oy = ($y > 0 ? 1 : ($y < 0 ? -1 : 0));
|
||||
$oz = ($z > 0 ? 1 : ($z < 0 ? -1 : 0));
|
||||
$this->entity->moveEntityWithOffset($ox, $oy, $oz);
|
||||
$this->faceEntity($this->entity->add($ox, $oy, $oz));
|
||||
$pk = new RotateHeadPacket(); //TODO headYaw auto update
|
||||
$pk->eid = $this->entity->eid;
|
||||
$pk->yaw = $this->entity->yaw;
|
||||
$this->entity->server->api->player->broadcastPacket($this->entity->level->players, $pk);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function moveTo($x, $y, $z){
|
||||
return $this->moveNonInstant($x - floor($this->entity->x), $y - floor($this->entity->y), $z - floor($this->entity->z));
|
||||
}
|
||||
|
||||
public function faceEntity(Vector3 $v){
|
||||
$d = $v->subtract($this->entity)->normalize();
|
||||
$dx = $d->x;
|
||||
$dz = $d->z;
|
||||
|
||||
$tan = $dz == 0 ? 0 : (90 - rad2deg($dx / $dz));
|
||||
$thetaOffset = $dz < 0 ? 90 : 270;
|
||||
$this->entity->yaw = $this->entity->headYaw = ($thetaOffset + $tan);
|
||||
}
|
||||
|
||||
public function lookOffset($x, $y, $z, $pitch = true){
|
||||
$tan = $z == 0 ? 0 : (90 - rad2deg(atan($x / $z))); /*arctan(infinity) = pi/2 = (90deg) - 90 = 0*/
|
||||
$thetaOffset = $z < 0 ? 90 : 270;
|
||||
$calcYaw = $tan + $thetaOffset;
|
||||
$this->entity->yaw = $calcYaw;
|
||||
if($pitch){
|
||||
$diff = sqrt($x * $x + $z * $z);
|
||||
$calcPitch = rad2deg(atan($y / $diff));
|
||||
$this->entity->pitch = $this->entity->type === MOB_CHICKEN ? -$calcPitch : $calcPitch;
|
||||
}
|
||||
|
||||
$this->entity->server->query("UPDATE entities SET pitch = ".$this->entity->pitch.", yaw = ".$this->entity->yaw." WHERE EID = ".$this->entity->eid.";");
|
||||
return true;
|
||||
}
|
||||
|
||||
public function lookOn($x, $y = 0, $z = 0, $pitch = true){
|
||||
if($x instanceof Vector3){
|
||||
return $this->lookOn($x->x, $x->y + $x->getEyeHeight(), $x->z, $pitch);
|
||||
}
|
||||
return $this->lookOffset($x - $this->entity->x, ($this->entity->y + $this->entity->height) - $y, $z - $this->entity->z, $pitch);
|
||||
}
|
||||
|
||||
public function __destruct(){
|
||||
unset($this->entity);
|
||||
}
|
||||
/**
|
||||
* @var Entity
|
||||
*/
|
||||
public $entity;
|
||||
|
||||
public function __construct($e){
|
||||
$this->entity = $e;
|
||||
}
|
||||
|
||||
public function moveNonInstant($x, $y, $z){
|
||||
if($x == 0 && $y == 0 && $z == 0){
|
||||
return false; //failed
|
||||
}
|
||||
|
||||
$ox = ($x > 0 ? 1 : ($x < 0 ? -1 : 0));
|
||||
$oy = ($y > 0 ? 1 : ($y < 0 ? -1 : 0));
|
||||
$oz = ($z > 0 ? 1 : ($z < 0 ? -1 : 0));
|
||||
$this->entity->moveEntityWithOffset($ox, $oy, $oz);
|
||||
$this->faceEntity($this->entity->add($ox, $oy, $oz));
|
||||
$pk = new RotateHeadPacket(); //TODO headYaw auto update
|
||||
$pk->eid = $this->entity->eid;
|
||||
$pk->yaw = $this->entity->yaw;
|
||||
$this->entity->server->api->player->broadcastPacket($this->entity->level->players, $pk);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function moveTo($x, $y, $z){
|
||||
return $this->moveNonInstant($x - floor($this->entity->x), $y - floor($this->entity->y), $z - floor($this->entity->z));
|
||||
}
|
||||
|
||||
public function faceEntity(Vector3 $v){
|
||||
$d = $v->subtract($this->entity)->normalize();
|
||||
$dx = $d->x;
|
||||
$dz = $d->z;
|
||||
|
||||
$tan = $dz == 0 ? 0 : (90 - rad2deg($dx / $dz));
|
||||
$thetaOffset = $dz < 0 ? 90 : 270;
|
||||
$this->entity->yaw = $this->entity->headYaw = ($thetaOffset + $tan);
|
||||
}
|
||||
|
||||
public function lookOffset($x, $y, $z, $pitch = true){
|
||||
$tan = $z == 0 ? 0 : (90 - rad2deg(atan($x / $z))); /*arctan(infinity) = pi/2 = (90deg) - 90 = 0*/
|
||||
$thetaOffset = $z < 0 ? 90 : 270;
|
||||
$calcYaw = $tan + $thetaOffset;
|
||||
$this->entity->yaw = $calcYaw;
|
||||
if($pitch){
|
||||
$diff = sqrt($x * $x + $z * $z);
|
||||
$calcPitch = rad2deg(atan($y / $diff));
|
||||
$this->entity->pitch = $this->entity->type === MOB_CHICKEN ? -$calcPitch : $calcPitch;
|
||||
}
|
||||
|
||||
$this->entity->server->query("UPDATE entities SET pitch = ".$this->entity->pitch.", yaw = ".$this->entity->yaw." WHERE EID = ".$this->entity->eid.";");
|
||||
return true;
|
||||
}
|
||||
|
||||
public function lookOn($x, $y = 0, $z = 0, $pitch = true){
|
||||
if($x instanceof Vector3){
|
||||
return $this->lookOn($x->x, $x->y + $x->getEyeHeight(), $x->z, $pitch);
|
||||
}
|
||||
return $this->lookOffset($x - $this->entity->x, ($this->entity->y + $this->entity->height) - $y, $z - $this->entity->z, $pitch);
|
||||
}
|
||||
|
||||
public function __destruct(){
|
||||
unset($this->entity);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,73 +0,0 @@
|
|||
<?php
|
||||
class PathNavigator
|
||||
{
|
||||
const PATH_COMPLETED = 0x1;
|
||||
const PATH_CONTINUE = 0x2;
|
||||
const PATH_CANT_CONTINUE = 0x3;
|
||||
public $entity;
|
||||
/**
|
||||
* @var Node
|
||||
*/
|
||||
private $getTo;
|
||||
public $currentPointIndex;
|
||||
public function __construct(Creature &$entity){
|
||||
$this->entity = $entity;
|
||||
$this->currentPointIndex = 0;
|
||||
}
|
||||
|
||||
|
||||
public function reset(){
|
||||
$this->currentPointIndex = 0;
|
||||
}
|
||||
/**
|
||||
* @param Node[] $path
|
||||
*/
|
||||
public function followPath($path){ //TODO remove local var
|
||||
if(!$this->canContinue()){
|
||||
console($this->getTo);
|
||||
console($this->entity);
|
||||
$diffX = $this->getTo->x - $this->entity->x;
|
||||
$diffY = 0; //todo ?
|
||||
$diffZ = $this->getTo->z - $this->entity->z;
|
||||
|
||||
$this->entity->moveEntityWithOffset(Utils::getSign($diffX), Utils::getSign($diffY), Utils::getSign($diffZ));
|
||||
return self::PATH_CANT_CONTINUE;
|
||||
}
|
||||
$index = $this->currentPointIndex++;
|
||||
if($index >= count($path)){
|
||||
$this->onPathEnd();
|
||||
console("COMPLETED");
|
||||
return self::PATH_COMPLETED;
|
||||
}
|
||||
$node = $path[$index];
|
||||
|
||||
$this->getTo = $node;
|
||||
|
||||
$diffX = $node->x - $this->entity->x;
|
||||
$diffY = 0; //todo ?
|
||||
$diffZ = $node->z - $this->entity->z;
|
||||
|
||||
$this->entity->moveEntityWithOffset(Utils::getSign($diffX), Utils::getSign($diffY), Utils::getSign($diffZ));
|
||||
|
||||
return self::PATH_CONTINUE;
|
||||
}
|
||||
|
||||
public function canContinue(){
|
||||
return empty($this->getTo) || (floor($this->entity->x) == floor($this->getTo->x) && floor($this->entity->z) == floor($this->getTo->z));
|
||||
}
|
||||
|
||||
public function onPathEnd(){
|
||||
$this->currentPointIndex = 0;
|
||||
unset($this->getTo);
|
||||
}
|
||||
|
||||
public function testLook($pos){
|
||||
$x = $pos->x - $this->entity->x;
|
||||
$y = $pos->y - $this->entity->y;
|
||||
$z = $pos->z - $this->entity->z;
|
||||
$this->entity->yaw = -atan2($this->entity->speedX, $this->entity->speedZ) * 180 / M_PI;
|
||||
$this->entity->pitch = $y == 0 ? 0 : rad2deg(-atan2($y, sqrt(pow($x, 2) + pow($z, 2))));
|
||||
$this->entity->server->query("UPDATE entities SET pitch = ".$this->entity->pitch.", yaw = ".$this->entity->yaw." WHERE EID = ".$this->entity->eid.";");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -2,46 +2,46 @@
|
|||
|
||||
abstract class TaskBase
|
||||
{
|
||||
public $isStarted = false, $selfCounter = 0;
|
||||
|
||||
/**
|
||||
* Executed when entity starts task. It is recommended to set counter here.
|
||||
* @param EntityAI $ai
|
||||
*/
|
||||
abstract function onStart(EntityAI $ai);
|
||||
|
||||
/**
|
||||
* Every tick until counter will be not equal to 0. It is recommended to update value of counter here.
|
||||
* @param EntityAI $ai
|
||||
*/
|
||||
abstract function onUpdate(EntityAI $ai);
|
||||
|
||||
/**
|
||||
* On task end.
|
||||
* @param EntityAI $ai
|
||||
*/
|
||||
abstract function onEnd(EntityAI $ai);
|
||||
|
||||
/**
|
||||
* Can start the task
|
||||
* @param EntityAI $ai
|
||||
*/
|
||||
abstract function canBeExecuted(EntityAI $ai);
|
||||
|
||||
/**
|
||||
* @return number ticks
|
||||
*/
|
||||
function getIdleTimeAfterEnd(){
|
||||
return 0;
|
||||
}
|
||||
|
||||
function reset(){
|
||||
$this->isStarted = false;
|
||||
$this->selfCounter = 0;
|
||||
}
|
||||
|
||||
final function __toString(){
|
||||
return get_class($this);
|
||||
}
|
||||
public $isStarted = false, $selfCounter = 0;
|
||||
|
||||
/**
|
||||
* Executed when entity starts task. It is recommended to set counter here.
|
||||
* @param EntityAI $ai
|
||||
*/
|
||||
abstract function onStart(EntityAI $ai);
|
||||
|
||||
/**
|
||||
* Every tick until counter will be not equal to 0. It is recommended to update value of counter here.
|
||||
* @param EntityAI $ai
|
||||
*/
|
||||
abstract function onUpdate(EntityAI $ai);
|
||||
|
||||
/**
|
||||
* On task end.
|
||||
* @param EntityAI $ai
|
||||
*/
|
||||
abstract function onEnd(EntityAI $ai);
|
||||
|
||||
/**
|
||||
* Can start the task
|
||||
* @param EntityAI $ai
|
||||
*/
|
||||
abstract function canBeExecuted(EntityAI $ai);
|
||||
|
||||
/**
|
||||
* @return number ticks
|
||||
*/
|
||||
function getIdleTimeAfterEnd(){
|
||||
return 0;
|
||||
}
|
||||
|
||||
function reset(){
|
||||
$this->isStarted = false;
|
||||
$this->selfCounter = 0;
|
||||
}
|
||||
|
||||
final function __toString(){
|
||||
return get_class($this);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,35 +2,35 @@
|
|||
|
||||
class TaskLookAround extends TaskBase
|
||||
{
|
||||
private $rotation;
|
||||
public function onStart(EntityAI $ai)
|
||||
{
|
||||
$this->selfCounter = 1;
|
||||
$this->rotation = mt_rand(-120, 120);
|
||||
}
|
||||
private $rotation;
|
||||
public function onStart(EntityAI $ai)
|
||||
{
|
||||
$this->selfCounter = 1;
|
||||
$this->rotation = mt_rand(-120, 120);
|
||||
}
|
||||
|
||||
public function onEnd(EntityAI $ai)
|
||||
{
|
||||
$ai->entity->idleTime = mt_rand(20, 40);
|
||||
}
|
||||
public function onEnd(EntityAI $ai)
|
||||
{
|
||||
$ai->entity->idleTime = mt_rand(20, 40);
|
||||
}
|
||||
|
||||
public function canBeExecuted(EntityAI $ai)
|
||||
{
|
||||
return !$ai->entity->isMoving() && Utils::randomFloat() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskTempt"); /*Vanilla value*/
|
||||
}
|
||||
public function canBeExecuted(EntityAI $ai)
|
||||
{
|
||||
return !$ai->entity->isMoving() && Utils::randomFloat() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskTempt"); /*Vanilla value*/
|
||||
}
|
||||
|
||||
public function onUpdate(EntityAI $ai)
|
||||
{
|
||||
if($this->rotation === 0){
|
||||
$this->selfCounter = 0;
|
||||
}
|
||||
$v = min(Utils::getSign($this->rotation) * 10, $this->rotation);
|
||||
$ai->entity->yaw += $v;
|
||||
$pk = new RotateHeadPacket(); //TODO headYaw auto update
|
||||
$pk->eid = $ai->entity->eid;
|
||||
$pk->yaw = $ai->entity->yaw;
|
||||
$ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk);
|
||||
$this->rotation -= $v;
|
||||
|
||||
}
|
||||
public function onUpdate(EntityAI $ai)
|
||||
{
|
||||
if($this->rotation === 0){
|
||||
$this->selfCounter = 0;
|
||||
}
|
||||
$v = min(Utils::getSign($this->rotation) * 10, $this->rotation);
|
||||
$ai->entity->yaw += $v;
|
||||
$pk = new RotateHeadPacket(); //TODO headYaw auto update
|
||||
$pk->eid = $ai->entity->eid;
|
||||
$pk->yaw = $ai->entity->yaw;
|
||||
$ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk);
|
||||
$this->rotation -= $v;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,39 +2,39 @@
|
|||
|
||||
class TaskRandomWalk extends TaskBase
|
||||
{
|
||||
private $x, $z;
|
||||
public function onStart(EntityAI $ai)
|
||||
{
|
||||
$this->x = mt_rand(-10, 10);
|
||||
$this->z = mt_rand(-10, 10);
|
||||
if($this->x === 0 && $this->z === 0){
|
||||
$this->reset();
|
||||
return false;
|
||||
}
|
||||
$this->selfCounter = floor(5 * Utils::distance($ai->entity, $ai->entity->add($this->x, 0, $this->z)));
|
||||
}
|
||||
private $x, $z;
|
||||
public function onStart(EntityAI $ai)
|
||||
{
|
||||
$this->x = mt_rand(-10, 10);
|
||||
$this->z = mt_rand(-10, 10);
|
||||
if($this->x === 0 && $this->z === 0){
|
||||
$this->reset();
|
||||
return false;
|
||||
}
|
||||
$this->selfCounter = floor(5 * Utils::distance($ai->entity, $ai->entity->add($this->x, 0, $this->z)));
|
||||
}
|
||||
|
||||
public function onEnd(EntityAI $ai)
|
||||
{
|
||||
$this->x = $this->z = 0;
|
||||
}
|
||||
public function onEnd(EntityAI $ai)
|
||||
{
|
||||
$this->x = $this->z = 0;
|
||||
}
|
||||
|
||||
public function onUpdate(EntityAI $ai)
|
||||
{
|
||||
if($ai->entity instanceof Creeper && $ai->entity->isIgnited()) {
|
||||
return false; //TODO Better way: block movement
|
||||
}
|
||||
--$this->selfCounter;
|
||||
$ai->mobController->moveNonInstant($this->x, 0, $this->z);
|
||||
}
|
||||
public function onUpdate(EntityAI $ai)
|
||||
{
|
||||
if($ai->entity instanceof Creeper && $ai->entity->isIgnited()) {
|
||||
return false; //TODO Better way: block movement
|
||||
}
|
||||
--$this->selfCounter;
|
||||
$ai->mobController->moveNonInstant($this->x, 0, $this->z);
|
||||
}
|
||||
|
||||
public function canBeExecuted(EntityAI $ai)
|
||||
{
|
||||
if($ai->entity instanceof Creeper && $ai->entity->isIgnited()) {
|
||||
return false;
|
||||
}
|
||||
return !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0;
|
||||
}
|
||||
public function canBeExecuted(EntityAI $ai)
|
||||
{
|
||||
if($ai->entity instanceof Creeper && $ai->entity->isIgnited()) {
|
||||
return false;
|
||||
}
|
||||
return !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue