Merge pull request #37 from kotyaralih/arrowstest

projectiles
This commit is contained in:
GameHerobrine 2024-03-24 06:32:54 +03:00 • committed by GitHub
commit 35842c3a0d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 921 additions and 201 deletions

View file

@ -633,7 +633,6 @@ class Player{
foreach($this->hotbar as $slot){
$hotbar[] = $slot <= -1 ? -1 : $slot + 9;
}
$pk = new ContainerSetContentPacket;
$pk->windowid = 0;
$pk->slots = $this->inventory;
@ -1753,7 +1752,7 @@ class Player{
}
$blockVector = new Vector3($packet->x, $packet->y, $packet->z);
if(($this->spawned === false or $this->blocked === true) and $packet->face >= 0 and $packet->face <= 5){
$target = $this->level->getBlock($blockVector);
$block = $target->getSide($packet->face);
@ -1828,11 +1827,52 @@ class Player{
$pk->meta = $block->getMetadata();
$this->dataPacket($pk);
break;
}elseif($packet->face === 0xff and $this->server->handle("player.action", $data) !== false){
$this->entity->inAction = true;
$this->entity->inActionCounter = 0;
$this->startAction = microtime(true);
$this->entity->updateMetadata();
}elseif($packet->face === 0xff){
$slotItem = $this->getHeldItem();
if($slotItem->getID() == SNOWBALL || $slotItem->getID() == EGG){ //TODO better way
$x = $packet->x * 0.000030518;
$y = $packet->y * 0.000030518;
$z = $packet->z * 0.000030518;
$d = sqrt($x*$x + $y*$y + $z*$z);
if($d >= 0.0001){
$shootX = $x / $d;
$shootY = $y / $d;
$shootZ = $z / $d;
$data = [
"x" => $this->entity->x,
"y" => $this->entity->y + $this->entity->getEyeHeight(),
"z" => $this->entity->z,
"yaw" => $this->entity->yaw,
"pitch" => $this->entity->pitch,
"shooter" => $this->entity->eid,
"shootX" => $shootX,
"shootY" => $shootY,
"shootZ" => $shootZ
];
if($slotItem->getID() == EGG){
$e = $this->server->api->entity->add($this->entity->level, ENTITY_OBJECT, OBJECT_EGG, $data);
}else{
$e = $this->server->api->entity->add($this->level, ENTITY_OBJECT, OBJECT_SNOWBALL, $data);
}
$this->removeItem($slotItem->getID(), $slotItem->meta, 1);
$this->server->api->entity->spawnToAll($e);
}
}else{
if($this->server->handle("player.action", $data) !== false){
$this->entity->inAction = true;
$this->entity->inActionCounter = 0;
$this->startAction = microtime(true);
$this->entity->updateMetadata();
}
}
}
break;
case ProtocolInfo::PLAYER_ACTION_PACKET:
@ -1845,44 +1885,57 @@ class Player{
switch($packet->action){
case 5: //Shot arrow
if($this->entity->inAction === true){
if($this->getSlot($this->slot)->getID() === BOW){
if($this->entity->inAction){
$arrowSlot = $this->hasItem(ARROW);
if($this->getSlot($this->slot)->getID() === BOW && (($this->gamemode & 0x01) == 0x1 || $arrowSlot !== false)){ //TODO check arrow count
if($this->startAction !== false){
$time = microtime(true) - $this->startAction;
$d = [
"x" => $this->entity->x,
"y" => $this->entity->y + 1.6,
"z" => $this->entity->z,
"yaw" => $this->entity->yaw,
"pitch" => $this->entity->pitch
];
$e = $this->server->api->entity->add($this->level, ENTITY_OBJECT, OBJECT_ARROW, $d);
$e->speedX = -sin(($e->yaw / 180) * M_PI) * cos(($e->pitch / 180) * M_PI);
$e->speedZ = cos(($e->yaw / 180) * M_PI) * cos(($e->pitch / 180) * M_PI);
$e->speedY = -sin(($e->pitch / 180) * M_PI);
$e->shooterEID = $this->entity->eid;
$e->shotByEntity = true;
/**
* Max usage: 72000ticks
* initalPower = 72000 - (72000 - usedCtr)
* power = initialPower / 20'
* power = (power*power+power*2)/3
* powerMax is 1, powerMin is 0.1
* args: xvel, yvel, zvel, (power+power)*1.5, 1.0
*/
$initalPower = $this->entity->inActionCounter;
$power = $initalPower / 20;
$power = ($power * $power + $power * 2) / 3;
if($power > 1.0) $power = 1;
elseif($power < 0.1){
//CANCEL but i am too lazy
$power = 0.1;
if($power >= 0.1){
if($power > 1) $power = 1;
$this->server->dhandle("player.shoot", [
"player" => $this,
"power" => &$power,
]);
$d = [
"x" => $this->entity->x,
"y" => $this->entity->y + 1.6,
"z" => $this->entity->z,
"yaw" => $this->entity->yaw,
"pitch" => $this->entity->pitch,
"shooter" => $this->entity->eid,
];
$e = $this->server->api->entity->add($this->level, ENTITY_OBJECT, OBJECT_ARROW, $d);
$e->speedX = -sin(($e->yaw / 180) * M_PI) * cos(($e->pitch / 180) * M_PI);
$e->speedZ = cos(($e->yaw / 180) * M_PI) * cos(($e->pitch / 180) * M_PI);
$e->speedY = -sin(($e->pitch / 180) * M_PI);
$e->shooterEID = $this->entity->eid;
$e->shotByEntity = true;
/**
* Max usage: 72000ticks
* initalPower = 72000 - (72000 - usedCtr)
* power = initialPower / 20'
* power = (power*power+power*2)/3
* powerMax is 1, powerMin is 0.1
* args: xvel, yvel, zvel, (power+power)*1.5, 1.0
*/
$e->critical = ($power == 1);
$e->shoot($e->speedX, $e->speedY, $e->speedZ, ($power+$power) * 1.5, 1.0);
$this->server->api->entity->spawnToAll($e);
if(($this->gamemode & 0x01) == 0x0) {
$bow = $this->getSlot($this->slot);
if(++$bow->meta >= $bow->getMaxDurability()){
$this->inventory[$this->slot] = BlockAPI::getItem(AIR, 0, 0);
}
$this->removeItem(ARROW, 0, 1, false);
$this->sendInventory();
}
}
$e->critical = ($power === 1);
$e->shoot($e->speedX, $e->speedY, $e->speedZ, ($power+$power) * 1.5, 1.0);
$this->server->api->entity->spawnToAll($e);
}
}else{ //inv desynced, resend
$this->sendInventory();
}
}
$this->startAction = false;

View file

@ -10,7 +10,6 @@ class Entity extends Position
public $counter = 0;
public $fallDistance = 0;
public static $updateOnTick, $allowedAI;
public $isCollidable;
public $canBeAttacked;
public $moveTime, $lookTime, $idleTime, $knockbackTime = 0;
public $needsUpdate = true;
@ -102,7 +101,7 @@ class Entity extends Position
public $inWeb;
public $inLava;
function __construct(Level $level, $eid, $class, $type = 0, $data = array())
function __construct(Level $level, $eid, $class, $type = 0, $data = [])
{
$this->random = new Random();
$this->last = [&$this->lastX, &$this->lastY, &$this->lastZ, &$this->lastYaw, &$this->lastPitch, &$this->lastTime]; //pointers to variables
@ -167,6 +166,7 @@ class Entity extends Position
$this->hasGravity = true;
$this->canBeAttacked = true;
$this->fireResistance = 20;
break;
case ENTITY_OBJECT:
$this->x = isset($this->data["TileX"]) ? $this->data["TileX"] : $this->x;
@ -174,15 +174,7 @@ class Entity extends Position
$this->z = isset($this->data["TileZ"]) ? $this->data["TileZ"] : $this->z;
$this->setHealth(1, "generic");
$this->stepHeight = false;
$this->width = 1;
$this->height = 1;
if($this->type === OBJECT_SNOWBALL){
$this->server->schedule(1210, array(
$this,
"update"
));
}
$this->setSize(1, 1);
break;
}
$this->radius = $this->width / 2;
@ -367,6 +359,18 @@ class Entity extends Position
}
}
public function canSee(Entity $e){
return $this->level->rayTraceBlocks(new Vector3($this->x, $this->y + $this->getEyeHeight(), $this->z), new Vector3($e->x, $e->y + $e->getEyeHeight(), $e->z)) == null;
}
/**
* Can entity be collided with or not
* @return boolean
*/
public function isPickable(){
return $this->isPlayer() ? !$this->dead : false;
}
public function handleLavaMovement(){ //TODO maybe try merging with water?
$this->inLava = $this->level->isLavaInBB($this->boundingBox->expand(-0.1, -0.4, -0.1));
return $this->inLava;
@ -392,8 +396,8 @@ class Entity extends Position
if($this->isPlayer() && $this->player->spawned === true && $this->player->blocked !== true && !$this->dead){
$myBB = $this->boundingBox->grow(1, 0.5, 1);
foreach($this->server->api->entity->getRadius($this, 2, ENTITY_ITEM) as $item){
if(!$item->closed && $item->spawntime > 0 && $item->delayBeforePickup <= 0){
foreach($this->server->api->entity->getRadius($this, 2, false) as $item){
if($item->class === ENTITY_ITEM && !$item->closed && $item->spawntime > 0 && $item->delayBeforePickup <= 0){
if($item->boundingBox->intersectsWith($myBB)){
if((($this->player->gamemode & 0x01) === 1 || $this->player->hasSpace($item->type, $item->meta, $item->stack) === true) && $this->server->api->dhandle("player.pickup", array(
"eid" => $this->player->eid,
@ -406,7 +410,11 @@ class Entity extends Position
$item->close();
}
}
}else if($item->class == ENTITY_OBJECT && !$item->closed && $item->type == OBJECT_ARROW && $item->shotByPlayer && $item->inGround && $item->shake <= 0 && $item->boundingBox->intersectsWith($myBB)){
if(($this->player->gamemode & 0x01) == 1 || $this->player->hasSpace(ARROW, 0, 1)){
$this->player->addItem(ARROW, 0, 1);
$item->close();
}
}
}
unset($myBB);
@ -1258,6 +1266,9 @@ class Entity extends Position
0 => $this->yaw,
1 => $this->pitch,
],
"speedX" => $this->speedX,
"speedY" => $this->speedY,
"speedZ" => $this->speedZ
];
if($this->class === ENTITY_OBJECT){
@ -1270,7 +1281,7 @@ class Entity extends Position
}
if($this->class === ENTITY_ITEM){
$data["Item"] = [
"id" => $this->type,
"id" => $this->itemID,
"Damage" => $this->meta,
"Count" => $this->stack,
];
@ -1306,7 +1317,11 @@ class Entity extends Position
}
$dmg = $this->applyArmor($dmg, $cause);
$ret = $this->setHealth(max(- 128, $this->getHealth() - ((int) $dmg)), $cause, $force);
if($this->isPlayer() && $this->player->gamemode & 0x01 == 0x01){
return false;
}
if ($ret != false && $this->hasKnockback && is_numeric($cause) && ($entity = $this->server->api->entity->get($cause)) != false) {
$d = $entity->x - $this->x;
@ -1405,19 +1420,15 @@ class Entity extends Position
public function setHealth($health, $cause = "generic", $force = false)
{
$health = (int) $health;
$harm = false;
if($health < $this->health){
$harm = true;
$dmg = $this->health - $health;
if(($this->class !== ENTITY_PLAYER or (($this->player instanceof Player) and (($this->player->gamemode & 0x01) === 0x00 or $force === true))) and ($this->dmgcounter[0] < microtime(true) or $this->dmgcounter[1] < $dmg) and !$this->dead){
$this->dmgcounter[0] = microtime(true) + 0.5;
$this->dmgcounter[1] = $dmg;
} else{
return false; // Entity inmunity
}
} elseif($health === $this->health and ! $this->dead){
return false;
$harm = true;
$dmg = $this->health - $health;
if(($this->class !== ENTITY_PLAYER or (($this->player instanceof Player) and (($this->player->gamemode & 0x01) === 0x00 or $force === true))) and ($this->dmgcounter[0] < microtime(true) or $this->dmgcounter[1] < $dmg) and !$this->dead){
$this->dmgcounter[0] = microtime(true) + 0.5;
$this->dmgcounter[1] = $dmg;
} else{
return false; // Entity inmunity
}
if($this->server->api->dhandle("entity.health.change", array(
"entity" => $this,
"eid" => $this->eid,

View file

@ -30,6 +30,10 @@ abstract class Living extends Entity implements Pathfindable{
//if(self::$despawnMobs) $this->server->schedule(self::$despawnTimer, [$this, "close"]); //900*20
}
public function isPickable(){
return !$this->dead;
}
public function fall(){
$dmg = ceil($this->fallDistance - 3);
if($dmg > 0){

View file

@ -1,5 +1,216 @@
<?php
abstract class Projectile extends Entity{
const CLASS_TYPE = ENTITY_OBJECT;
public $shooterEID;
public $shake = 0;
public $inGround = false;
public $ticksInAir = 0, $ticksInGround = 0;
public $xTile = 0, $yTile = 0, $zTile = 0;
public $inTile = false;
public function __construct(Level $level, $eid, $class, $type = 0, $data = [])
{
parent::__construct($level, $eid, $class, $type, $data);
$this->gravity = 0.03;
$this->setSize(0.25, 0.25);
$this->x -= cos($this->yaw / 180 * M_PI) * 0.16;
$this->y -= 0.1;
$this->z -= cos($this->yaw / 180 * M_PI) * 0.16;
$this->boundingBox->setBounds($this->x - $this->radius, $this->y - $this->yOffset /*+ $this->ySize*/, $this->z - $this->radius, $this->x + $this->radius, $this->y - $this->yOffset + $this->height /*+ $this->ySize*/, $this->z + $this->radius);
$shooter = $data["shooter"] ?? false;
if($shooter !== false) $shooter = $this->level->entityList[$shooter] ?? false;
$this->inTile = $data["inTile"] ?? $this->inTile;
$this->inGround = $data["inGround"] ?? $this->inGround;
$this->xTile = $data["xTile"] ?? $this->xTile;
$this->yTile = $data["yTile"] ?? $this->yTile;
$this->zTile = $data["zTile"] ?? $this->zTile;
if($shooter instanceof Entity){
$this->shooterEID = $shooter->eid;
if($shooter->isPlayer()){
$shootX = $data["shootX"] ?? 0;
$shootY = $data["shootY"] ?? 0;
$shootZ = $data["shootZ"] ?? 0;
$throwPower = 1.5;
$this->shoot($shootX, $shootY, $shootZ, $throwPower, 1.0);
}else{
/*
* TODO shoot if not player
* $pitch = $this->pitch;
$v18 = cos($pitch/180 * 3.1416);
$v19 = cos($pitch/180 * 3.1416); //this one uses $pitch+getThrowUpAngleOffset, but last is 0
$throwPower = 1.5;
$this->shoot(, , , $throwPower, 1.0);*/
}
}
}
public function shoot($d, $d1, $d2, $f, $f1){ //unchecked, may be not same as arrow
$f2 = sqrt($d * $d + $d1 * $d1 + $d2 * $d2);
$d /= $f2;
$d1 /= $f2;
$d2 /= $f2;
$d += $this->random->nextGaussian() * 0.0075 * $f1;
$d1 += $this->random->nextGaussian() * 0.0075 * $f1;
$d2 += $this->random->nextGaussian() * 0.0075 * $f1;
$d *= $f;
$d1 *= $f;
$d2 *= $f;
$this->speedX = $d;
$this->speedY = $d1;
$this->speedZ = $d2;
$f3 = sqrt($d * $d + $d2 * $d2);
$this->yaw = (atan2($d, $d2) * 180) / M_PI;
$this->pitch = (atan2($d1, $f3) * 180) / M_PI;
$this->updatePosition();
}
public function update($now){
$this->lastX = $this->x;
$this->lastY = $this->y;
$this->lastZ = $this->z;
$this->lastPitch = $this->pitch;
$this->lastYaw = $this->yaw;
$this->lastSpeedX = $this->speedX;
$this->lastSpeedY = $this->speedY;
$this->lastSpeedZ = $this->speedZ;
$this->needsUpdate = true;
$this->handleWaterMovement(); //TODO: maybe just call parent::update(); ?
if($this->fire > 0){
if(!$this->isImmuneToFire){
if(($this->fire % 20) == 0){
$this->harm(1, "burning");
}
--$this->fire;
}else{
$this->fire -= 4;
if($this->fire <= 0) $this->fire = 0;
}
if($this->fire <= 0){
$this->updateMetadata();
}
}
if($this->handleLavaMovement()){
if(!$this->isImmuneToFire){
$this->harm(4, "fire");
$oldOnFire = $this->fire;
if($oldOnFire < 20*30) $this->fire = 20*30; //30 seconds
}
}
if($this->isInVoid()){
$this->outOfWorld();
}
//Entity::update end
if($this->shake > 0) --$this->shake;
if(!$this->inGround){
++$this->ticksInAir;
goto CHECK_COLLISIONS; //TODO dont use gotos
}
$blockID = $this->level->level->getBlockID($this->xTile, $this->yTile, $this->zTile);
if($this->inTile != $blockID){
$this->speedX += (lcg_value() * 0.2);
$this->speedY += (lcg_value() * 0.2);
$this->speedZ += (lcg_value() * 0.2);
$this->inGround = false;
$this->ticksInAir = $this->ticksInGround = 0;
CHECK_COLLISIONS:
$start = new Vector3($this->x, $this->y, $this->z);
$end = new Vector3($this->x + $this->speedX, $this->y + $this->speedY, $this->z + $this->speedZ);
/**
* @var MovingObjectPosition $v4
*/
$v4 = $this->level->rayTraceBlocks($start, $end);
$start = new Vector3($this->x, $this->y, $this->z); //TODO remove?
if($v4 != null){
$end = new Vector3($v4->hitVector->x, $v4->hitVector->y, $v4->hitVector->z);
}else{
$end = new Vector3($this->x + $this->speedX, $this->y + $this->speedY, $this->z + $this->speedZ);
}
$entities = $this->level->getEntitiesInAABB($this->boundingBox->addCoord($this->speedX, $this->speedY, $this->speedZ)->expand(1, 1, 1));
$bestDist = 0;
$bestEnt = null;
foreach($entities as $eid => $ent){
if($eid != $this->eid && $ent->isPickable() && ($eid != $this->shooterEID || $this->ticksInAir >= 5)){
$v12 = $ent->boundingBox->expand(0.3, 0.3, 0.3);
$v13 = $v12->calculateIntercept($start, $end);
if($v13 != null){
$dist = $start->distance($v13->hitVector);
if($dist < $bestDist || $bestDist == 0){
$bestEnt = $ent;
}
}
}
}
if($bestEnt != null){
$v4 = MovingObjectPosition::fromEntity($bestEnt);
}
if($v4 != null){
$this->onHit($v4);
}
$this->x += $this->speedX;
$this->y += $this->speedY;
$this->z += $this->speedZ;
$v21 = sqrt($this->speedX*$this->speedX + $this->speedZ*$this->speedZ);
$this->yaw = atan2($this->speedX, $this->speedZ) * 180 / M_PI;
$this->pitch = atan2($this->speedY, $v21) * 180 / M_PI;
$this->pitch = $this->lastPitch + ($this->pitch - $this->lastPitch) * 0.2;
$this->yaw = $this->lastYaw + ($this->yaw - $this->lastYaw) * 0.2;
$v24 = $this->inWater ? 0.8 : 0.99;
$this->speedX *= $v24;
$this->speedY *= $v24;
$this->speedZ *= $v24;
$this->speedY -= $this->gravity;
$v7 = $this->width / 2;
$v8 = $this->height;
$this->boundingBox->setBounds($this->x - $v7, $this->y - $this->yOffset /*+ $this->ySize*/, $this->z - $v7, $this->x + $v7, $this->y - $this->yOffset + $v8 /*+ $this->ySize*/, $this->z + $v7);
}
}
public function onHit(MovingObjectPosition $hitResult){
}
public function createSaveData(){
$data = parent::createSaveData();
$data["inTile"] = $this->inTile;
$data["inGround"] = $this->inGround;
$data["xTile"] = $this->xTile;
$data["yTile"] = $this->yTile;
$data["zTile"] = $this->zTile;
return $data;
}
public function spawn($player)
{
$pk = new AddEntityPacket();
$pk->eid = $this->eid;
$pk->type = $this->type;
$pk->x = $this->x;
$pk->y = $this->y;
$pk->z = $this->z;
$pk->speedX = $this->speedX;
$pk->speedY = $this->speedY;
$pk->speedZ = $this->speedZ;
$pk->did = 0;
$player->dataPacket($pk);
}
}

View file

@ -24,6 +24,12 @@ class EntityAI
$this->mobController = new MobController($entity);
}
public function canSee(Entity $entity){
//TODO caches
return $this->entity->canSee($entity);
}
/**
* Add a task for entity
* @param TaskBase $task

View file

@ -15,7 +15,12 @@ class TaskEatTileGoal extends TaskBase
public function onUpdate(EntityAI $ai)
{
if (--$this->selfCounter === 4)
if($ai->isStarted("TaskEatTileGoal")){
$this->reset();
return false;
}
if (--$this->selfCounter == 4)
{
$id = $ai->entity->level->level->getBlockID($ai->entity->x, $ai->entity->y, $ai->entity->z);
$idb = $ai->entity->level->level->getBlockID($ai->entity->x, $ai->entity->y - 1, $ai->entity->z);
@ -32,7 +37,7 @@ class TaskEatTileGoal extends TaskBase
public function canBeExecuted(EntityAI $ai)
{
if($ai->isStarted("TaskRandomWalk")) return false;
if($ai->isStarted("TaskRandomWalk") || $ai->isStarted("TaskEatTileGoal")) return false;
if(mt_rand(0, ($ai->entity instanceof Ageable && $ai->entity->isBaby()) ? 50 : 1000) == 0){
$idm = $ai->entity->level->level->getBlock($ai->entity->x, $ai->entity->y, $ai->entity->z);
$idb = $ai->entity->level->level->getBlockID($ai->entity->x, $ai->entity->y - 1, $ai->entity->z);

View file

@ -19,7 +19,7 @@ class TaskLookAround extends TaskBase
public function canBeExecuted(EntityAI $ai)
{
return !$ai->entity->inPanic && !$ai->entity->isMoving() && lcg_value() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/
return !$ai->entity->inPanic && !$ai->entity->isMoving() && lcg_value() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskAttackPlayer") && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/
}
public function onUpdate(EntityAI $ai)

View file

@ -26,7 +26,7 @@ class TaskRandomWalk extends TaskBase
public function onUpdate(EntityAI $ai)
{
if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->isStarted("TaskTempt") || $ai->isStarted("TaskFollowParent") ) {
if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->isStarted("TaskTempt") || $ai->isStarted("TaskFollowParent") || $ai->isStarted("TaskRangedAttack")) {
$this->reset();
return false; //TODO Better way: block movement
}
@ -37,7 +37,7 @@ class TaskRandomWalk extends TaskBase
public function canBeExecuted(EntityAI $ai)
{
if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->entity->hasPath() || $ai->isStarted("TaskTempt") ||$ai->isStarted("TaskAttackPlayer")) {
if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->entity->hasPath() || $ai->isStarted("TaskTempt") || $ai->isStarted("TaskAttackPlayer") || $ai->isStarted("TaskRangedAttack")) {
return false;
} // i really need mutexBits
return !$ai->entity->inPanic && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskEatTileGoal") && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskFollowParent") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0;

View file

@ -0,0 +1,147 @@
<?php
use EntityAI;
class TaskRangedAttack extends \TaskBase
{
public $server;
public $attackCounter = 0;
public $ticksNoSeen = 0;
public function __construct($speed, $range){
$this->speedMultiplier = $speed;
$this->rangeSquared = $range*$range;
$this->server = ServerAPI::request();
}
public function onStart(EntityAI $ai)
{
$this->selfCounter = 1;
$this->ticksNoSeen = 0;
}
public function onEnd(EntityAI $ai)
{
$ai->entity->target = false;
}
public function onUpdate(EntityAI $ai)
{
if(!$this->isTargetValid($ai)){
$this->reset();
$this->onEnd($ai);
return false;
}
--$this->attackCounter;
//if(!$ai->canSee($ai->entity->target)){ useless without pathfinder =<
// ++$this->ticksNoSeen;
//}else{
// $this->ticksNoSeen = 0;
//}
$dist = $ai->entity->distanceSquared($ai->entity->target);
if($dist > 100){
$ai->mobController->setMovingTarget($ai->entity->target->x, $ai->entity->target->y, $ai->entity->target->z, $this->speedMultiplier);
}
$ai->mobController->setLookPosition($ai->entity->target->x, $ai->entity->target->y + 0.12, $ai->entity->target->z, 10, $ai->entity->getVerticalFaceSpeed());
if($this->attackCounter <= 0){
$this->rangedAttack($ai->entity, $ai->entity->target);
$this->attackCounter = 60;
}
}
public function rangedAttack($selfEntity, $target){
$d = [
"x" => $selfEntity->x,
"y" => $selfEntity->y + 1.6,
"z" => $selfEntity->z,
"yaw" => $selfEntity->yaw,
"pitch" => $selfEntity->pitch
];
/**
* @var Arrow $arrow
*/
$arrow = $this->server->api->entity->add($selfEntity->level, ENTITY_OBJECT, OBJECT_ARROW, $d);
$arrow->shotByEntity = true;
$arrow->shooterEID = $selfEntity->eid;
$posY = ($target->y + $target->getEyeHeight() - 0.1);
$diffX = $target->x - $selfEntity->x;
$diffY = ($target->boundingBox->minY + ($target->height / 3)) - $posY;
$diffZ = $target->z - $selfEntity->z;
$v12 = sqrt($diffX * $diffX + $diffZ * $diffZ);
//if($v12 >= 1.0e-7){
$yaw = ((atan2($diffZ, $diffX) * 180) / M_PI) - 90;
$pitch = -((atan2($diffY, $v12) * 180) / M_PI);
$v16 = $diffX / $v12;
$v18 = $diffZ / $v12;
$arrow->x = $selfEntity->x + $v16;
$arrow->y = $posY;
$arrow->z = $selfEntity->z + $v18;
$arrow->yaw = $yaw;
$arrow->pitch = $pitch;
$v20 = $v12 * 0.2;
$arrow->shoot($diffX, $diffY + $v20, $diffZ, 1.6, 12);
$this->server->api->entity->spawnToAll($arrow);
//}
}
public function isTargetValid(EntityAI $ai){
$e = $ai->entity;
if($e->target instanceof Entity && !$e->target->closed){
$t = $e->target;
$xDiff = ($t->x - $e->x);
$yDiff = ($t->y - $e->y);
$zDiff = ($t->z - $e->z);
return ($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff) <= $this->rangeSquared;
}
return false;
}
public function tryTargeting(EntityAI $ai){
$e = $ai->entity;
if($e->target instanceof Entity){
$t = $e->target;
$xDiff = ($t->x - $e->x);
$yDiff = ($t->y - $e->y);
$zDiff = ($t->z - $e->z);
if(($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff) <= $this->rangeSquared){
return true;
}
}
$bestTargetDistance = INF;
$closestTarget = null;
foreach($e->level->players as $p){
if($p->spawned){
$pt = $p->entity;
$xDiff = $pt->x - $e->x;
$yDiff = $pt->y - $e->y;
$zDiff = $pt->z - $e->z;
$d = ($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff);
if($d <= $this->rangeSquared){
if($bestTargetDistance >= $d){
$closestTarget = $pt;
$bestTargetDistance = $d;
}
}
}
}
if($closestTarget != null){
$e->target = $closestTarget; //TODO dont save entity object ?
return true;
}
return false;
}
public function canBeExecuted(EntityAI $ai)
{
return $this->tryTargeting($ai);
}
}

View file

@ -8,11 +8,11 @@ class Skeleton extends Monster{
$this->setName("Skeleton");
$this->ai->removeTask("TaskAttackPlayer");
$this->setSpeed(0.25);
//$this->ai->addTask(new TaskDestroyServerPerformance());
$this->ai->addTask(new TaskRandomWalk(1.0));
$this->ai->addTask(new TaskLookAround());
$this->ai->addTask(new TaskSwimming());
$this->ai->addTask(new TaskRangedAttack(1.0, 16));
}
public function getAttackDamage(){

View file

@ -1,25 +1,52 @@
<?php
class Arrow extends Projectile{
class Arrow extends Entity{
const TYPE = OBJECT_ARROW;
const CLASS_TYPE = ENTITY_OBJECT;
public $criticial = false;
public $shooterEID = 0;
public $shotByEntity;
public $xTile, $yTile, $zTile;
public $inTile, $inData;
public $shake;
public $inGround;
public $airTicks = 0;
public $inWall = false;
public $criticial = false;
public $groundTicks = 0;
function __construct(Level $level, $eid, $class, $type = 0, $data = [], $shooter = false){
public $shotByPlayer = false;
function __construct(Level $level, $eid, $class, $type = 0, $data = []){
parent::__construct($level, $eid, $class, $type, $data);
$this->gravity = 0.05;
$this->setSize(0.5, 0.5);
$this->setName("Arrow");
$this->shooterEID = $shooter;
$this->shotByEntity = $shooter instanceof Entity;
$shooter = $data["shooter"] ?? false;
if($shooter !== false){
$this->shooterEID = $shooter;
$this->shotByPlayer = $level->entityList[$shooter]->isPlayer();
}
$this->airTicks = $this->groundTicks = 0;
$this->inTile = $data["inTile"] ?? $this->inTile;
$this->inData = $data["inData"] ?? $this->inData;
$this->inGround = $data["inGround"] ?? $this->inGround;
$this->xTile = $data["xTile"] ?? $this->xTile;
$this->yTile = $data["yTile"] ?? $this->yTile;
$this->zTile = $data["zTile"] ?? $this->zTile;
$this->shotByPlayer = $data["shotByPlayer"] ?? $this->shotByPlayer;
//$this->server->schedule(1210, array($this, "update")); //Despawn
}
public function createSaveData(){
$data = parent::createSaveData();
$data["inTile"] = $this->inTile;
$data["inData"] = $this->inData;
$data["inGround"] = $this->inGround;
$data["xTile"] = $this->xTile;
$data["yTile"] = $this->yTile;
$data["zTile"] = $this->zTile;
$data["shotByPlayer"] = $this->shotByPlayer;
return $data;
}
public function handleUpdate(){
$pk = new MoveEntityPacket_PosRot;
$pk->eid = $this->eid;
@ -52,82 +79,187 @@ class Arrow extends Projectile{
//$this->update();
//TODO i guess? $ticksInGround = 0;
}
public function sendMoveUpdate()
{
$pk = new MoveEntityPacket_PosRot();
$pk->eid = $this->eid;
$pk->x = $this->x;
$pk->y = $this->y;
$pk->z = $this->z;
$pk->yaw = $this->yaw;
$pk->pitch = $this->pitch;
foreach($this->level->players as $p){ //sending packets directly makes movement less laggy
$p->directDataPacket(clone $pk);
}
}
public function update($now){
$this->needsUpdate = false; //TODO reenable
return;
//parent::update();
if($this->closed || ($this->x > 255 || $this->x < 0 || $this->y < 0 || $this->z < 0 || $this->z > 255) || $this->groundTicks > 200) { //remove after 10 seconds in wall, idc about vanilla
$this->server->api->entity->remove($this->eid);
return;
}
$this->lastX = $this->x;
$this->lastY = $this->y;
$this->lastZ = $this->z;
$this->lastPitch = $this->pitch;
$this->lastYaw = $this->yaw;
$this->lastSpeedX = $this->speedX;
$this->lastSpeedY = $this->speedY;
$this->lastSpeedZ = $this->speedZ;
$this->needsUpdate = true;
if($this->inWall) {
++$this->groundTicks;
return; //yeah whatever
$this->handleWaterMovement(); //TODO: maybe just call parent::update(); ?
if($this->fire > 0){
if(!$this->isImmuneToFire){
if(($this->fire % 20) == 0){
$this->harm(1, "burning");
}
--$this->fire;
}else{
$this->fire -= 4;
if($this->fire <= 0) $this->fire = 0;
}
if($this->fire <= 0){
$this->updateMetadata();
}
}
if($this->speedX != 0 or $this->speedY != 0 or $this->speedZ != 0){
$f = sqrt(($this->speedX * $this->speedX) + ($this->speedZ * $this->speedZ));
$this->yaw = (atan2($this->speedX, $this->speedZ) * 180 / M_PI);
$this->pitch = (atan2($this->speedY, $f) * 180 / M_PI);
if($this->handleLavaMovement()){
if(!$this->isImmuneToFire){
$this->harm(4, "fire");
$oldOnFire = $this->fire;
if($oldOnFire < 20*30) $this->fire = 20*30; //30 seconds
}
}
$rt = $this->boundingBox->shrink(0.4, 0.4, 0.4)->addCoord($this->speedX, $this->speedY, $this->speedZ);
for($x = floor($rt->minX); $x < ceil($rt->maxX); ++$x){
for($z = floor($rt->minZ); $z < ceil($rt->maxZ); ++$z){
for($y = ceil($rt->minY); $y < ceil($rt->maxY); ++$y){
$b = $this->level->level->getBlockID($x, $y, $z);
if(StaticBlock::getIsSolid($b)){
$bb = StaticBlock::getBoundingBoxForBlockCoords($b, $x, $y, $z);
$this->speedY = $bb->calculateYOffset($this->boundingBox, $this->speedY);
$this->speedX = $bb->calculateXOffset($this->boundingBox, $this->speedX);
$this->speedZ = $bb->calculateZOffset($this->boundingBox, $this->speedZ);
$this->inWall = true;
break;
if($this->isInVoid()){
$this->outOfWorld();
}
//Entity::update end
if($this->lastPitch == $this->lastYaw && $this->lastYaw == 0){
$v1 = sqrt($this->speedX*$this->speedX + $this->speedZ*$this->speedZ);
$this->lastYaw = $this->yaw = atan2($this->speedX, $this->speedZ) * 180 / M_PI;
$this->lastPitch = $this->pitch = atan2($this->speedY, $v1) * 180 / M_PI;
}
if($this->shake > 0){
--$this->shake;
}
if($this->inGround){
[$blockID, $blockMeta] = $this->level->level->getBlock($this->xTile, $this->yTile, $this->zTile);
$this->speedZ = $this->speedX = $this->speedY = 0;
if($blockID == $this->inTile && $blockMeta == $this->inData){
++$this->groundTicks;
if($this->groundTicks >= 1200){ //TODO customizeable?
$this->close();
return;
}
}else{
$this->inGround = false;
$this->speedX *= lcg_value() * 0.2;
$this->speedY *= lcg_value() * 0.2;
$this->speedZ *= lcg_value() * 0.2;
$this->groundTicks = $this->airTicks = 0;
}
}else{
++$this->airTicks;
$start = new Vector3($this->x, $this->y, $this->z);
$end = new Vector3($this->x + $this->speedX, $this->y + $this->speedY, $this->z + $this->speedZ);
/**
* @var MovingObjectPosition $v4
*/
$v4 = $this->level->rayTraceBlocks($start, $end);
$start = new Vector3($this->x, $this->y, $this->z); //TODO remove?
if($v4 != null){
$end = new Vector3($v4->hitVector->x, $v4->hitVector->y, $v4->hitVector->z);
}else{
$end = new Vector3($this->x + $this->speedX, $this->y + $this->speedY, $this->z + $this->speedZ);
}
$entities = $this->level->getEntitiesInAABB($this->boundingBox->addCoord($this->speedX, $this->speedY, $this->speedZ)->expand(1, 1, 1));
$bestDist = 0;
$bestEnt = null;
foreach($entities as $eid => $ent){
if($eid != $this->eid && $ent->isPickable() && ($eid != $this->shooterEID || $this->airTicks >= 5)){
$v12 = $ent->boundingBox->expand(0.3, 0.3, 0.3);
$v13 = $v12->calculateIntercept($start, $end);
if($v13 != null){
$dist = $start->distance($v13->hitVector);
if($dist < $bestDist || $bestDist == 0){
$bestEnt = $ent;
}
}
}
}
}
if(!$this->inWall){
$bbexp = $this->boundingBox->addCoord($this->speedX, $this->speedY, $this->speedZ)->expand(0, 0.2, 0);
foreach($this->level->entityList as $e){
if($e instanceof Entity && $e->eid !== $this->eid && !$e->closed && $e->canBeShot() && $e->boundingBox->intersectsWith($bbexp)){
if($this->shotByEntity && $this->shooterEID === $e->eid && $this->airTicks < 5) continue;
$dmg = ceil(sqrt($this->speedX * $this->speedX + $this->speedY * $this->speedY + $this->speedZ * $this->speedZ) * 2);
if($bestEnt != null){
$v4 = MovingObjectPosition::fromEntity($bestEnt);
}
//TODO entity collisions
if($v4 != null){
if($v4->entityHit != null){
//TODO entity hit
$v49 = sqrt($this->speedY*$this->speedY + $this->speedX*$this->speedX + $this->speedZ*$this->speedZ);
$damage = ceil($v49+$v49);
if($this->criticial){
$dmg += mt_rand(0, (int)($dmg/2+1));
$damage += mt_rand(0, $damage / 2 + 1);
}
$e->harm($dmg, $this->eid);
$this->closed = true;
break;
if($v4->entityHit->harm($damage, $this->eid)){
//vanilla seems to increase arrow count if $v4->entity is mob
$this->close();
}else{
$this->speedX *= -0.1;
$this->speedY *= -0.1;
$this->speedZ *= -0.1;
$this->yaw += 180;
$this->lastYaw += 180;
$this->airTicks = 0;
}
}else{
$this->xTile = $v4->blockX;
$this->yTile = $v4->blockY;
$this->zTile = $v4->blockZ;
[$this->inTile, $this->inData] = $this->level->level->getBlock($this->xTile, $this->yTile, $this->zTile);
$this->speedX = $v4->hitVector->x - $this->x;
$this->speedY = $v4->hitVector->y - $this->y;
$this->speedZ = $v4->hitVector->z - $this->z;
$v21 = sqrt($this->speedX*$this->speedX + $this->speedY*$this->speedY + $this->speedZ*$this->speedZ);
if($v21 != 0){
$this->x -= $this->speedX / $v21 * 0.05;
$this->y -= $this->speedY / $v21 * 0.05;
$this->z -= $this->speedZ / $v21 * 0.05;
}
$this->inGround = true;
$this->shake = 7;
$this->criticial = false;
}
}
$this->x += $this->speedX;
$this->y += $this->speedY;
$this->z += $this->speedZ;
$v21 = sqrt($this->speedX*$this->speedX + $this->speedZ*$this->speedZ);
$this->yaw = atan2($this->speedX, $this->speedZ) * 180 / M_PI;
$this->pitch = atan2($this->speedY, $v21) * 180 / M_PI;
$this->pitch = $this->lastPitch + ($this->pitch - $this->lastPitch) * 0.2;
$this->yaw = $this->lastYaw + ($this->yaw - $this->lastYaw) * 0.2;
$v24 = $this->inWater ? 0.8 : 0.99;
$this->speedX *= $v24;
$this->speedY *= $v24;
$this->speedZ *= $v24;
$this->speedY -= 0.05;
//$this->setP
$v7 = $this->width / 2;
$v8 = $this->height;
$this->boundingBox->setBounds($this->x - $v7, $this->y - $this->yOffset /*+ $this->ySize*/, $this->z - $v7, $this->x + $v7, $this->y - $this->yOffset + $v8 /*+ $this->ySize*/, $this->z + $v7);
$this->doBlocksCollision();
}
$this->x += $this->speedX;
$this->y += $this->speedY;
$this->z += $this->speedZ;
++$this->airTicks; //TODO onGround state
$this->speedX *= 0.99;
$this->speedY *= 0.99;
$this->speedZ *= 0.99;
$this->speedY -= $this->gravity;
$this->updatePosition();
}

View file

@ -16,6 +16,10 @@ class FallingSand extends Entity{
$this->gravity = 0.04;
}
public function isPickable(){
return !$this->dead;
}
public function update($now){
if($this->closed) return;

View file

@ -68,6 +68,10 @@ class Minecart extends Vehicle{
$this->yOffset = $this->height / 2;
}
public function isPickable(){
return !$this->dead;
}
public function getDrops(){
return [
[MINECART, 0, 1]

View file

@ -20,6 +20,10 @@ class Painting extends Entity{
];
}
public function isPickable(){
return true;
}
public function createSaveData(){
$data = parent::createSaveData();

View file

@ -16,6 +16,10 @@ class PrimedTNT extends Entity{
//$this->server->schedule(5, [$this, "updateFuse"], [], true);
}
public function isPickable(){
return !$this->dead;
}
public function getMetadata(){
$d = parent::getMetadata();
$d[16]["value"] = (int) $this->data["fuse"];

View file

@ -0,0 +1,32 @@
<?php
class ThrownEgg extends Projectile{
const TYPE = OBJECT_EGG;
public function onHit(MovingObjectPosition $hitResult){
if($hitResult->typeOfHit == 1){
$hitResult->entityHit->harm(0, $this->eid);
}
$rand = mt_rand(0, 7);
if($rand == 0){
$count = mt_rand(0, 31) == 0 ? 4 : 1;
$data = [
"x" => $this->x,
"y" => $this->y,
"z" => $this->z,
"yaw" => $this->yaw,
"pitch" => 0,
"IsBaby" => true
];
for($i = 0; $i < $count; ++$i){
$chicken = $this->server->api->entity->add($this->level, ENTITY_MOB, MOB_CHICKEN, $data);
$this->server->api->entity->spawnToAll($chicken);
}
}
$this->close();
}
}

View file

@ -0,0 +1,11 @@
<?php
class ThrownSnowball extends Projectile{
const TYPE = OBJECT_SNOWBALL;
public function onHit(MovingObjectPosition $hitResult){
if($hitResult->typeOfHit == 1){
$hitResult->entityHit->harm(0, $this->eid);
}
$this->close();
}
}

View file

@ -8,6 +8,10 @@ class TripodCamera extends Entity{
$this->setHealth(PHP_INT_MAX, "generic");
}
public function isPickable(){
return !$this->dead;
}
public function interactWith(Entity $e, $action){
if($e->isPlayer() and $action === InteractPacket::ACTION_HOLD){
//todo

View file

@ -5,8 +5,8 @@ class EntityRegistry{
/*Register all entities*/
public static function registerEntities(){
console("[INFO] Registering entities...");
self::$entityList = new EntityList;
ConsoleAPI::info("Registering entities...");
self::$entityList = new EntityList();
self::registerEntity(Pig::class);
self::registerEntity(Chicken::class);
self::registerEntity(Sheep::class);
@ -22,6 +22,8 @@ class EntityRegistry{
self::registerEntity(Minecart::class);
self::registerEntity(FallingSand::class);
self::registerEntity(ItemEntity::class);
self::registerEntity(ThrownEgg::class);
self::registerEntity(ThrownSnowball::class);
}
/*Register an Entity*/

View file

@ -155,19 +155,63 @@ abstract class Block extends Position{
public $z = 0;
public $slipperiness;
public static $minX = 0, $minY = 0, $minZ = 0, $maxX = 1, $maxY = 1, $maxZ = 1;
public static function setBlockBounds($minX, $minY, $minZ, $maxX, $maxY, $maxZ){ //TODO call for some specific blocks
static::$minX = $minX;
static::$minY = $minY;
static::$minZ = $minZ;
static::$maxX = $maxX;
static::$maxY = $maxY;
static::$maxZ = $maxZ;
}
public static function getAABB(Level $level, $x, $y, $z){
return new AxisAlignedBB(static::$minX + $x, static::$minY + $y, static::$minZ + $z, static::$maxX + $x, static::$maxY + $y, static::$maxZ + $z);
return StaticBlock::getAABB($level, $x, $y, $z);
}
public static function containsX($id, $v){
return $v == null ? false : $v->y >= StaticBlock::$minYs[$id] && $v->y <= StaticBlock::$maxYs[$id] && $v->z >= StaticBlock::$minZs[$id] && $v->z <= StaticBlock::$maxZs[$id];
}
public static function containsY($id, $v){
return $v == null ? false : $v->x >= StaticBlock::$minXs[$id] && $v->x <= StaticBlock::$maxXs[$id] && $v->z >= StaticBlock::$minZs[$id] && $v->z <= StaticBlock::$maxZs[$id];
}
public static function containsZ($id, $v){
return $v == null ? false : $v->x >= StaticBlock::$minXs[$id] && $v->x <= StaticBlock::$maxXs[$id] && $v->y >= StaticBlock::$minYs[$id] && $v->y <= StaticBlock::$maxYs[$id];
}
public static function clip(Level $level, $x, $y, $z, Vector3 $start, Vector3 $end){
//self::updateShape($level, $x, $y, $z); TODO update shape
$id = $level->level->getBlockID($x, $y, $z);
$start = $start->subtract($x, $y, $z);
$end = $end->subtract($x, $y, $z);
$v7 = $start->clipX($end, StaticBlock::$minXs[$id]);
$v8 = $start->clipX($end, StaticBlock::$maxXs[$id]);
$v9 = $start->clipY($end, StaticBlock::$minYs[$id]);
$v10 = $start->clipY($end, StaticBlock::$maxYs[$id]);
$v11 = $start->clipZ($end, StaticBlock::$minZs[$id]);
$v12 = $start->clipZ($end, StaticBlock::$maxZs[$id]);
if(!self::containsX($id, $v7)) $v7 = null;
if(!self::containsX($id, $v8)) $v8 = null;
if(!self::containsY($id, $v9)) $v9 = null;
if(!self::containsY($id, $v10)) $v10 = null;
if(!self::containsZ($id, $v11)) $v11 = null;
if(!self::containsZ($id, $v12)) $v12 = null;
$v13 = null;
if($v7 != null && ($v13 == null || $start->distanceSquared($v7) < $start->distanceSquared($v13))) $v13 = $v7;
if($v8 != null && ($v13 == null || $start->distanceSquared($v8) < $start->distanceSquared($v13))) $v13 = $v8;
if($v9 != null && ($v13 == null || $start->distanceSquared($v9) < $start->distanceSquared($v13))) $v13 = $v9;
if($v10 != null && ($v13 == null || $start->distanceSquared($v10) < $start->distanceSquared($v13))) $v13 = $v10;
if($v11 != null && ($v13 == null || $start->distanceSquared($v11) < $start->distanceSquared($v13))) $v13 = $v11;
if($v12 != null && ($v13 == null || $start->distanceSquared($v12) < $start->distanceSquared($v13))) $v13 = $v12;
if($v13 == null) return null;
$v14 = -1;
if($v13 == $v7) $v14 = 4;
if($v13 == $v8) $v14 = 5;
if($v13 == $v9) $v14 = 0;
if($v13 == $v10) $v14 = 1;
if($v13 == $v11) $v14 = 2;
if($v13 == $v12) $v14 = 3;
return MovingObjectPosition::fromBlock($x, $y, $z, $v14, $v13->add($x, $y, $z));
}
public static function addVelocityToEntity(Level $level, $x, $y, $z, Entity $entity, Vector3 $velocityVector){}
public static function onRandomTick(Level $level, $x, $y, $z){}

View file

@ -6,6 +6,7 @@ class StaticBlock
{
const DEFAULT_SLIPPERINESS = 0.6;
const DEFAULT_HARDNESS = 0;
private static $NULL_BOUNDS;
public static $isSolid = [];
public static $isTransparent = [];
@ -19,6 +20,8 @@ class StaticBlock
public static $hardness = [];
public static $slipperiness = [];
public static $boundingBoxes = [];
public static $minXs = [], $minYs = [], $minZs = [], $maxXs = [], $maxYs = [], $maxZs = [];
public static function init(){
self::$NULL_BOUNDS = new AxisAlignedBB(0, 0, 0, 0, 0, 0);
foreach(Block::$class as $nonstaticname){
@ -39,32 +42,35 @@ class StaticBlock
FireBlock::setFlammabilityAndCatchingChance($b->getID(), 0, 0);
self::setBlockBounds($b->getID(), 0, 0, 0, 1, 1, 1);
}
Block::$class[BED_BLOCK]::setBlockBounds(0, 0, 0, 1, 0.5625, 1);
self::setBlockBounds(BED_BLOCK, 0, 0, 0, 1, 0.5625, 1);
//Cake: has bounds based on world state
//Chest: has bounds based on world state
Block::$class[WHEAT_BLOCK]::setBlockBounds(0, 0, 0, 1, 0.25, 1);
Block::$class[CARROT_BLOCK]::setBlockBounds(0, 0, 0, 1, 0.25, 1);
Block::$class[POTATO_BLOCK]::setBlockBounds(0, 0, 0, 1, 0.25, 1);
Block::$class[BEETROOT_BLOCK]::setBlockBounds(0, 0, 0, 1, 0.25, 1); //i assume it extends class Crops
Block::$class[WHEAT_BLOCK]::setBlockBounds(0, 0, 0, 1, 0.25, 1);
Block::$class[DEAD_BUSH]::setBlockBounds(0.1, 0, 0.1, 0.9, 0.8, 0.9);
Block::$class[DOOR_BLOCK]::setBlockBounds(0, 0, 0, 1, 1, 1); //has bounds based on rotation
Block::$class[FARMLAND]::setBlockBounds(0, 0, 0, 1, 0.9375, 1);
self::setBlockBounds(WHEAT_BLOCK, 0, 0, 0, 1, 0.25, 1);
self::setBlockBounds(CARROT_BLOCK, 0, 0, 0, 1, 0.25, 1);
self::setBlockBounds(POTATO_BLOCK,0, 0, 0, 1, 0.25, 1);
self::setBlockBounds(BEETROOT_BLOCK, 0, 0, 0, 1, 0.25, 1); //i assume it extends class Crops
self::setBlockBounds(WHEAT_BLOCK, 0, 0, 0, 1, 0.25, 1);
self::setBlockBounds(DEAD_BUSH, 0.1, 0, 0.1, 0.9, 0.8, 0.9);
self::setBlockBounds(DOOR_BLOCK, 0, 0, 0, 1, 1, 1); //has bounds based on rotation
self::setBlockBounds(FARMLAND, 0, 0, 0, 1, 0.9375, 1);
//Fence: bounds on state
//Fence Gate: bounds on state
Block::$class[TALL_GRASS]::setBlockBounds(0.1, 0, 0.1, 0.9, 0.8, 0.9);
Block::$class[MELON_STEM]::setBlockBounds(0.5 - 0.125, 0, 0.5 - 0.125, 0.5 + 0.125, 0.25, 0.5 + 0.125);
Block::$class[PUMPKIN_STEM]::setBlockBounds(0.5 - 0.125, 0, 0.5 - 0.125, 0.5 + 0.125, 0.25, 0.5 + 0.125);
Block::$class[SAPLING]::setBlockBounds(0.1, 0, 0.1, 0.9, 0.8, 0.9);
Block::$class[BROWN_MUSHROOM]::setBlockBounds(0.3, 0, 0.3, 0.8, 0.4, 0.8);
Block::$class[SLAB]::setBlockBounds(0, 0, 0, 1, 0.5, 1);
self::setBlockBounds(TALL_GRASS, 0.1, 0, 0.1, 0.9, 0.8, 0.9);
self::setBlockBounds(MELON_STEM, 0.5 - 0.125, 0, 0.5 - 0.125, 0.5 + 0.125, 0.25, 0.5 + 0.125);
self::setBlockBounds(PUMPKIN_STEM, 0.5 - 0.125, 0, 0.5 - 0.125, 0.5 + 0.125, 0.25, 0.5 + 0.125);
self::setBlockBounds(SAPLING, 0.1, 0, 0.1, 0.9, 0.8, 0.9);
self::setBlockBounds(BROWN_MUSHROOM, 0.3, 0, 0.3, 0.8, 0.4, 0.8);
self::setBlockBounds(SLAB, 0, 0, 0, 1, 0.5, 1);
//Ladder: based on rotation
//Glass Pane: based on state
RailBaseBlock::setBlockBounds(0, 0, 0, 1, 0.125, 1);
Block::$class[SUGARCANE_BLOCK]::setBlockBounds(0.5 - 0.375, 0, 0.5 - 0.375, 0.5 + 0.375, 1, 0.5 + 0.375);
Block::$class[SNOW_LAYER]::setBlockBounds(0, 0, 0, 1, 0.125, 1);
self::setBlockBounds(RAIL, 0, 0, 0, 1, 0.125, 1);
self::setBlockBounds(POWERED_RAIL, 0, 0, 0, 1, 0.125, 1);
self::setBlockBounds(SUGARCANE_BLOCK, 0.5 - 0.375, 0, 0.5 - 0.375, 0.5 + 0.375, 1, 0.5 + 0.375);
self::setBlockBounds(SNOW_LAYER, 0, 0, 0, 1, 0.125, 1);
//Stairs: based on different factors
//Stone wall: based on state
@ -92,6 +98,21 @@ class StaticBlock
FireBlock::setFlammabilityAndCatchingChance(SPONGE, 30, 60);
}
public static function setBlockBounds($blockID, $minX, $minY, $minZ, $maxX, $maxY, $maxZ){
self::$maxXs[$blockID] = $maxX;
self::$maxYs[$blockID] = $maxY;
self::$maxZs[$blockID] = $maxZ;
self::$minXs[$blockID] = $minX;
self::$minYs[$blockID] = $minY;
self::$minZs[$blockID] = $minZ;
}
public static function getAABB(Level $level, $x, $y, $z){
$id = $level->level->getBlockID($x, $y, $z);
return new AxisAlignedBB(self::$minXs[$id] + $x, self::$minYs[$id] + $y, self::$minZs[$id] + $z, self::$maxXs[$id] + $x, self::$maxYs[$id] + $y, self::$maxZs[$id] + $z); //TODO get bb from self::$boundingBoxes ?
}
public static function getBlock($id){
return Block::$class[$id] ?? Block::$class[0];
}

View file

@ -2,7 +2,7 @@
class BowItem extends Item{
public function __construct($meta = 0, $count = 1){
parent::__construct(BOW, 0, $count, "Bow");
parent::__construct(BOW, $meta, $count, "Bow");
}
public function isTool(){ //it is tool too
return true;

View file

@ -220,14 +220,19 @@ class AxisAlignedBB{
public function isVectorInXY(Vector3 $vector){
return $vector->x >= $this->minX and $vector->x <= $this->maxX and $vector->y >= $this->minY and $vector->y <= $this->maxY;
}
/**
* @param Vector3 $pos1
* @param Vector3 $pos2
* @return NULL|MovingObjectPosition
*/
public function calculateIntercept(Vector3 $pos1, Vector3 $pos2){
$v1 = $pos1->getIntermediateWithXValue($pos2, $this->minX);
$v2 = $pos1->getIntermediateWithXValue($pos2, $this->maxX);
$v3 = $pos1->getIntermediateWithYValue($pos2, $this->minY);
$v4 = $pos1->getIntermediateWithYValue($pos2, $this->maxY);
$v5 = $pos1->getIntermediateWithZValue($pos2, $this->minZ);
$v6 = $pos1->getIntermediateWithZValue($pos2, $this->maxZ);
$v1 = $pos1->clipX($pos2, $this->minX);
$v2 = $pos1->clipX($pos2, $this->maxX);
$v3 = $pos1->clipY($pos2, $this->minY);
$v4 = $pos1->clipY($pos2, $this->maxY);
$v5 = $pos1->clipZ($pos2, $this->minZ);
$v6 = $pos1->clipZ($pos2, $this->maxZ);
if($v1 !== null and !$this->isVectorInYZ($v1)){
$v1 = null;

View file

@ -197,7 +197,7 @@ class Vector3{
*
* @return Vector3
*/
public function getIntermediateWithXValue(Vector3 $v, $x){
public function clipX(Vector3 $v, $x){
$xDiff = $v->x - $this->x;
$yDiff = $v->y - $this->y;
$zDiff = $v->z - $this->z;
@ -214,6 +214,9 @@ class Vector3{
return new Vector3($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f);
}
}
public function getIntermediateWithXValue(Vector3 $v, $y){
return $this->clipX($v, $y);
}
/**
* Returns a new vector with y value equal to the second parameter, along the line between this vector and the
@ -224,7 +227,7 @@ class Vector3{
*
* @return Vector3
*/
public function getIntermediateWithYValue(Vector3 $v, $y){
public function clipY(Vector3 $v, $y){
$xDiff = $v->x - $this->x;
$yDiff = $v->y - $this->y;
$zDiff = $v->z - $this->z;
@ -242,6 +245,9 @@ class Vector3{
}
}
public function getIntermediateWithYValue(Vector3 $v, $y){
return $this->clipY($v, $y);
}
/**
* Returns a new vector with z value equal to the second parameter, along the line between this vector and the
* passed in vector, or null if not possible.
@ -251,7 +257,7 @@ class Vector3{
*
* @return Vector3
*/
public function getIntermediateWithZValue(Vector3 $v, $z){
public function clipZ(Vector3 $v, $z){
$xDiff = $v->x - $this->x;
$yDiff = $v->y - $this->y;
$zDiff = $v->z - $this->z;
@ -268,4 +274,8 @@ class Vector3{
return new Vector3($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f);
}
}
public function getIntermediateWithZValue(Vector3 $v, $y){
return $this->clipZ($v, $y);
}
}

View file

@ -82,7 +82,7 @@ class Level{
return true;
}
/* TODO neccessary for projectiles
// TODO neccessary for projectiles
public function rayTraceBlocks(Vector3 $start, Vector3 $end){
$par3 = false; //TODO move to params?
@ -97,6 +97,7 @@ class Level{
[$startID, $startMeta] = $this->level->getBlock($xStart, $yStart, $zStart);
$block = StaticBlock::getBlock($startID);
//$block::updateShape($this, $xStart, $yStart, $zStart); //TODO better way to do it
$aabb = $block::getAABB($this, $xStart, $yStart, $zStart);
if($startID > 0 && $aabb != null){ //TODO also block::canColideCheck
@ -106,8 +107,7 @@ class Level{
for($i = 0; $i <= 200; ++$i){
if($xStart == $xEnd && $yStart == $yEnd && $zStart == $zEnd){ //also add checks for nan?
console("$start $end");
if(is_nan($start->x) || is_nan($start->y) || is_nan($start->z) || ($xStart == $xEnd && $yStart == $yEnd && $zStart == $zEnd)){ //also add checks for nan?
return null;
}
@ -131,11 +131,10 @@ class Level{
$v29 = $end->y - $start->y;
$v31 = $end->z - $start->z;
if($v39) $v21 = ($v15 - $start->x) / $v27;
if($v40) $v23 = ($v17 - $start->y) / $v29;
if($v41) $v25 = ($v19 - $start->z) / $v31;
if($v39) $v21 = $v27 == 0 ? ($v15 - $start->x)*INF : ($v15 - $start->x) / $v27;
if($v40) $v23 = $v29 == 0 ? ($v17 - $start->y)*INF : ($v17 - $start->y) / $v29;
if($v41) $v25 = $v31 == 0 ? ($v19 - $start->z)*INF : ($v19 - $start->z) / $v31;
$v33 = false;
if($v21 < $v23 && $v21 < $v25){
$v42 = $xEnd > $xStart ? 4 : 5;
@ -151,8 +150,8 @@ class Level{
}else{
$v42 = $zEnd > $zStart ? 2 : 3;
$start->x += $v27 * $v23;
$start->y += $v29 * $v21;
$start->x += $v27 * $v25;
$start->y += $v29 * $v25;
$start->z = $v19;
}
@ -167,9 +166,9 @@ class Level{
[$blockID, $blockMeta] = $this->level->getBlock($xStart, $yStart, $zStart);
$block = StaticBlock::getBlock($blockID);
$aabb = $block::getAABB($this, $xStart, $yStart, $zStart);
if($startID > 0 && $aabb != null){ //TODO also block::canColideCheck
if($blockID > 0 && $aabb != null){ //TODO also block::canColideCheck
$v38 = $block::clip($this, $xStart, $yStart, $zStart, $start, $end);
if($v38 != null) return $v38;
}
@ -177,8 +176,7 @@ class Level{
return null;
}
*/
//TODO mayPlace
public function isLavaInBB($aabb){
$minX = floor($aabb->minX);
$maxX = floor($aabb->maxX + 1);
@ -632,7 +630,10 @@ class Level{
}
return $ents;
}
/**
* @param AxisAlignedBB $bb
* @return Entity[]
*/
public function getEntitiesInAABB(AxisAlignedBB $bb){
$minChunkX = ((int)($bb->minX)) >> 4;
$minChunkZ = ((int)($bb->minZ)) >> 4;

View file

@ -1,8 +1,8 @@
<?php
class MovingObjectPosition{
/** 0 = block, 1 = entity */
public $typeOfHit;
/** 0 = block, 1 = entity, 2 = none*/
public $typeOfHit = 2;
public $blockX;
public $blockY;
@ -23,7 +23,11 @@ class MovingObjectPosition{
protected function __construct(){
}
public function __toString(){
return "MovingObjectPosition: {blockXYZ: {$this->blockX} {$this->blockY} {$this->blockZ}, side: {$this->sideHit} hitVec: {$this->hitVector} entityHit: {$this->entityHit}}";
}
/**
* @param int $x
* @param int $y
@ -39,6 +43,7 @@ class MovingObjectPosition{
$ob->blockX = $x;
$ob->blockY = $y;
$ob->blockZ = $z;
$ob->sideHit = $side;
$ob->hitVector = new Vector3($hitVector->x, $hitVector->y, $hitVector->z);
return $ob;
}