Minecart is in separated class

This commit is contained in:
GameHerobrine 2022-08-02 17:20:54 +03:00
parent 5596f58a0a
commit a75e286337
11 changed files with 515 additions and 32 deletions

View file

@ -59,7 +59,8 @@ class EntityAPI{
$this->server->api->console->register("spawnmob", "<mob>", [$this, "commandHandler"]);
$this->server->api->console->register("despawn", "", [$this, "CommandHandler"]);
}
public function commandHandler($cmd, $args, $issuer, $alias){
$output = "";
switch($cmd){

View file

@ -62,7 +62,7 @@ class LevelAPI{
$e = $this->server->api->entity->add($this->levels[$name], ENTITY_FALLING, $entity["id"], $entity);
$e->setPosition(new Vector3($entity["Pos"][0], $entity["Pos"][1], $entity["Pos"][2]), $entity["Rotation"][0], $entity["Rotation"][1]);
$e->setHealth($entity["Health"]);
}elseif($entity["id"] === OBJECT_PAINTING or $entity["id"] === OBJECT_ARROW){ //Painting
}elseif(Utils::getEntityTypeByID($entity["id"]) === ENTITY_OBJECT){ //Object
$e = $this->server->api->entity->add($this->levels[$name], ENTITY_OBJECT, $entity["id"], $entity);
$e->setPosition(new Vector3($entity["Pos"][0], $entity["Pos"][1], $entity["Pos"][2]), $entity["Rotation"][0], $entity["Rotation"][1]);
$e->setHealth(1);

View file

@ -108,8 +108,6 @@ class Entity extends Position{
if($this->type === OBJECT_SNOWBALL){
$this->server->schedule(1210, array($this, "update")); //Despawn
$this->update();
}elseif($this->type == OBJECT_MINECART){
$this->update();
}
break;
}
@ -120,6 +118,22 @@ class Entity extends Position{
}
}
public function lookOn($entity){
$horizontal = sqrt(pow(($target->entity->x - $entity->x), 2) + pow(($target->entity->z - $entity->z) , 2));
$vertical = $target->entity->y - ($entity->y + -0.5); /*0.5 = $entity->getEyeHeight()*/
$pitch = -atan2($vertical, $horizontal) / M_PI * 180; //negative is up, positive is down
$xDist = $target->entity->x - $entity->x;
$zDist = $target->entity->z - $entity->z;
$yaw = atan2($zDist, $xDist) / M_PI * 180 - 90;
if($yaw < 0){
$yaw += 360.0;
}
$this->yaw = $yaw;
$this->pitch = $pitch;
}
public function getDrops(){
if($this->class === ENTITY_PLAYER and $this->player instanceof Player and ($this->player->gamemode & 0x01) === 0){
$inv = array();
@ -138,13 +152,6 @@ class Entity extends Position{
}
}
return $inv;
}elseif($this->class === ENTITY_OBJECT){
switch($this->type){
case OBJECT_MINECART:
return array(
array(MINECART, 0, 1),
);
}
}
return array();
}
@ -646,26 +653,6 @@ class Entity extends Position{
$pk->speedZ = $this->speedZ;
$player->dataPacket($pk);
break;
case ENTITY_OBJECT:
if($this->type === OBJECT_MINECART){
$pk = new AddEntityPacket;
$pk->eid = $this->eid;
$pk->type = $this->type;
$pk->x = $this->x;
$pk->y = $this->y;
$pk->z = $this->z;
$pk->yaw = $this->yaw;
$pk->pitch = $this->pitch;
$player->dataPacket($pk);
$pk = new SetEntityMotionPacket;
$pk->eid = $this->eid;
$pk->speedX = $this->speedX;
$pk->speedY = $this->speedY;
$pk->speedZ = $this->speedZ;
$player->dataPacket($pk);
}
break;
case ENTITY_FALLING:
$pk = new AddEntityPacket;
$pk->eid = $this->eid;

View file

@ -36,6 +36,7 @@ class EntityRegistry{
public static $entityList;
/*Register all entities*/
public static function registerEntities(){
console("[INFO] Registering entities...");
self::$entityList = new EntityList;
self::registerEntity(Pig::class);
self::registerEntity(Chicken::class);
@ -49,6 +50,7 @@ class EntityRegistry{
self::registerEntity(Arrow::class);
self::registerEntity(PrimedTNT::class);
self::registerEntity(Painting::class);
self::registerEntity(Minecart::class);
}
/*Register an Entity*/
@ -58,7 +60,7 @@ class EntityRegistry{
//self::$entityList[$className::TYPE] = $className;
self::$entityList->addEntity(new PropertyEntity($className::CLASS_TYPE, $className::TYPE, $className));
//self::$shortNames[$className] = $class->getShortName(); what is this even supposed to do?
console("[INFO] Registered a ".$className);
//console("[INFO] Registered a ".$className);
return true;
}

5
src/entity/Rideable.php Normal file
View file

@ -0,0 +1,5 @@
<?php
interface Rideable{
}

View file

@ -0,0 +1,5 @@
<?php
abstract class Vehicle extends Entity implements Rideable{
const CLASS_TYPE = ENTITY_OBJECT;
}

View file

@ -0,0 +1,41 @@
<?php
class Minecart extends Vehicle{
const TYPE = OBJECT_MINECART;
function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
parent::__construct($level, $eid, $class, $type, $data);
$this->x = isset($this->data["TileX"]) ? $this->data["TileX"]:$this->x;
$this->y = isset($this->data["TileY"]) ? $this->data["TileY"]:$this->y;
$this->z = isset($this->data["TileZ"]) ? $this->data["TileZ"]:$this->z;
$this->setHealth(1, "generic");
//$this->setName((isset($objects[$this->type]) ? $objects[$this->type]:$this->type));
$this->size = 1;
$this->update();
}
public function getDrops(){
return array(
array(MINECART, 0, 1),
);
}
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->yaw = $this->yaw;
$pk->pitch = $this->pitch;
$player->dataPacket($pk);
$pk = new SetEntityMotionPacket;
$pk->eid = $this->eid;
$pk->speedX = $this->speedX;
$pk->speedY = $this->speedY;
$pk->speedZ = $this->speedZ;
$player->dataPacket($pk);
}
}

297
src/math/AxisAlignedBB.php Normal file
View file

@ -0,0 +1,297 @@
<?php
class AxisAlignedBB{
public $minX;
public $minY;
public $minZ;
public $maxX;
public $maxY;
public $maxZ;
public function __construct($minX, $minY, $minZ, $maxX, $maxY, $maxZ){
$this->minX = $minX;
$this->minY = $minY;
$this->minZ = $minZ;
$this->maxX = $maxX;
$this->maxY = $maxY;
$this->maxZ = $maxZ;
}
public function setBounds($minX, $minY, $minZ, $maxX, $maxY, $maxZ){
$this->minX = $minX;
$this->minY = $minY;
$this->minZ = $minZ;
$this->maxX = $maxX;
$this->maxY = $maxY;
$this->maxZ = $maxZ;
return $this;
}
public function addCoord($x, $y, $z){
$minX = $this->minX;
$minY = $this->minY;
$minZ = $this->minZ;
$maxX = $this->maxX;
$maxY = $this->maxY;
$maxZ = $this->maxZ;
if($x < 0){
$minX += $x;
}elseif($x > 0){
$maxX += $x;
}
if($y < 0){
$minY += $y;
}elseif($y > 0){
$maxY += $y;
}
if($z < 0){
$minZ += $z;
}elseif($z > 0){
$maxZ += $z;
}
return new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ);
}
public function grow($x, $y, $z){
return new AxisAlignedBB($this->minX - $x, $this->minY - $y, $this->minZ - $z, $this->maxX + $x, $this->maxY + $y, $this->maxZ + $z);
}
public function expand($x, $y, $z){
$this->minX -= $x;
$this->minY -= $y;
$this->minZ -= $z;
$this->maxX += $x;
$this->maxY += $y;
$this->maxZ += $z;
return $this;
}
public function offset($x, $y, $z){
$this->minX += $x;
$this->minY += $y;
$this->minZ += $z;
$this->maxX += $x;
$this->maxY += $y;
$this->maxZ += $z;
return $this;
}
public function shrink($x, $y, $z){
return new AxisAlignedBB($this->minX + $x, $this->minY + $y, $this->minZ + $z, $this->maxX - $x, $this->maxY - $y, $this->maxZ - $z);
}
public function contract($x, $y, $z){
$this->minX += $x;
$this->minY += $y;
$this->minZ += $z;
$this->maxX -= $x;
$this->maxY -= $y;
$this->maxZ -= $z;
return $this;
}
public function setBB(AxisAlignedBB $bb){
$this->minX = $bb->minX;
$this->minY = $bb->minY;
$this->minZ = $bb->minZ;
$this->maxX = $bb->maxX;
$this->maxY = $bb->maxY;
$this->maxZ = $bb->maxZ;
return $this;
}
public function getOffsetBoundingBox($x, $y, $z){
return new AxisAlignedBB($this->minX + $x, $this->minY + $y, $this->minZ + $z, $this->maxX + $x, $this->maxY + $y, $this->maxZ + $z);
}
public function calculateXOffset(AxisAlignedBB $bb, $x){
if($bb->maxY <= $this->minY or $bb->minY >= $this->maxY){
return $x;
}
if($bb->maxZ <= $this->minZ or $bb->minZ >= $this->maxZ){
return $x;
}
if($x > 0 and $bb->maxX <= $this->minX){
$x1 = $this->minX - $bb->maxX;
if($x1 < $x){
$x = $x1;
}
}
if($x < 0 and $bb->minX >= $this->maxX){
$x2 = $this->maxX - $bb->minX;
if($x2 > $x){
$x = $x2;
}
}
return $x;
}
public function calculateYOffset(AxisAlignedBB $bb, $y){
if($bb->maxX <= $this->minX or $bb->minX >= $this->maxX){
return $y;
}
if($bb->maxZ <= $this->minZ or $bb->minZ >= $this->maxZ){
return $y;
}
if($y > 0 and $bb->maxY <= $this->minY){
$y1 = $this->minY - $bb->maxY;
if($y1 < $y){
$y = $y1;
}
}
if($y < 0 and $bb->minY >= $this->maxY){
$y2 = $this->maxY - $bb->minY;
if($y2 > $y){
$y = $y2;
}
}
return $y;
}
public function calculateZOffset(AxisAlignedBB $bb, $z){
if($bb->maxX <= $this->minX or $bb->minX >= $this->maxX){
return $z;
}
if($bb->maxY <= $this->minY or $bb->minY >= $this->maxY){
return $z;
}
if($z > 0 and $bb->maxZ <= $this->minZ){
$z1 = $this->minZ - $bb->maxZ;
if($z1 < $z){
$z = $z1;
}
}
if($z < 0 and $bb->minZ >= $this->maxZ){
$z2 = $this->maxZ - $bb->minZ;
if($z2 > $z){
$z = $z2;
}
}
return $z;
}
public function intersectsWith(AxisAlignedBB $bb){
if($bb->maxX > $this->minX and $bb->minX < $this->maxX){
if($bb->maxY > $this->minY and $bb->minY < $this->maxY){
return $bb->maxZ > $this->minZ and $bb->minZ < $this->maxZ;
}
}
return false;
}
public function isVectorInside(Vector3 $vector){
if($vector->x <= $this->minX or $vector->x >= $this->maxX){
return false;
}
if($vector->y <= $this->minY or $vector->y >= $this->maxY){
return false;
}
return $vector->z > $this->minZ and $vector->z < $this->maxZ;
}
public function getAverageEdgeLength(){
return ($this->maxX - $this->minX + $this->maxY - $this->minY + $this->maxZ - $this->minZ) / 3;
}
public function isVectorInYZ(Vector3 $vector){
return $vector->y >= $this->minY and $vector->y <= $this->maxY and $vector->z >= $this->minZ and $vector->z <= $this->maxZ;
}
public function isVectorInXZ(Vector3 $vector){
return $vector->x >= $this->minX and $vector->x <= $this->maxX and $vector->z >= $this->minZ and $vector->z <= $this->maxZ;
}
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;
}
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);
if($v1 !== null and !$this->isVectorInYZ($v1)){
$v1 = null;
}
if($v2 !== null and !$this->isVectorInYZ($v2)){
$v2 = null;
}
if($v3 !== null and !$this->isVectorInXZ($v3)){
$v3 = null;
}
if($v4 !== null and !$this->isVectorInXZ($v4)){
$v4 = null;
}
if($v5 !== null and !$this->isVectorInXY($v5)){
$v5 = null;
}
if($v6 !== null and !$this->isVectorInXY($v6)){
$v6 = null;
}
$vector = $v1;
if($v2 !== null and ($vector === null or $pos1->distanceSquared($v2) < $pos1->distanceSquared($vector))){
$vector = $v2;
}
if($v3 !== null and ($vector === null or $pos1->distanceSquared($v3) < $pos1->distanceSquared($vector))){
$vector = $v3;
}
if($v4 !== null and ($vector === null or $pos1->distanceSquared($v4) < $pos1->distanceSquared($vector))){
$vector = $v4;
}
if($v5 !== null and ($vector === null or $pos1->distanceSquared($v5) < $pos1->distanceSquared($vector))){
$vector = $v5;
}
if($v6 !== null and ($vector === null or $pos1->distanceSquared($v6) < $pos1->distanceSquared($vector))){
$vector = $v6;
}
if($vector === null){
return null;
}
$f = -1;
if($vector === $v1){
$f = 4;
}elseif($vector === $v2){
$f = 5;
}elseif($vector === $v3){
$f = 0;
}elseif($vector === $v4){
$f = 1;
}elseif($vector === $v5){
$f = 2;
}elseif($vector === $v6){
$f = 3;
}
return MovingObjectPosition::fromBlock(0, 0, 0, $f, $vector);
}
}

View file

@ -13,6 +13,32 @@ class Utils{
return ((@fsockopen("8.8.8.8", 80, $e = null, $n = null, 2) !== false or @fsockopen("www.linux.org", 80, $e = null, $n = null, 2) !== false or @fsockopen("www.php.net", 80, $e = null, $n = null, 2) !== false) ? true:false);
}*/
public static function getEntityTypeByID($id){
switch($id){
case 10:
case 11:
case 12:
case 13:
case 32:
case 33:
case 34:
case 35:
case 36:
return ENTITY_MOB;
case 62:
case 65:
case 80:
case 81:
case 82:
case 83:
case 84:
return ENTITY_OBJECT;
case 66:
return FALLING_SAND;
}
return $id;
}
public static function getCallableIdentifier(callable $variable){
if(is_array($variable)){
return sha1(strtolower(get_class($variable[0])) . "::" . strtolower($variable[1]));

View file

@ -0,0 +1,58 @@
<?php
class MovingObjectPosition{
/** 0 = block, 1 = entity */
public $typeOfHit;
public $blockX;
public $blockY;
public $blockZ;
/**
* Which side was hit. If its -1 then it went the full length of the ray trace.
* Bottom = 0, Top = 1, East = 2, West = 3, North = 4, South = 5.
*/
public $sideHit;
/** @var Vector3 */
public $hitVector;
/** @var Entity */
public $entityHit = null;
protected function __construct(){
}
/**
* @param int $x
* @param int $y
* @param int $z
* @param int $side
* @param Vector3 $hitVector
*
* @return MovingObjectPosition
*/
public static function fromBlock($x, $y, $z, $side, Vector3 $hitVector){
$ob = new MovingObjectPosition;
$ob->typeOfHit = 0;
$ob->blockX = $x;
$ob->blockY = $y;
$ob->blockZ = $z;
$ob->hitVector = new Vector3($hitVector->x, $hitVector->y, $hitVector->z);
return $ob;
}
/**
* @param Entity $entity
*
* @return MovingObjectPosition
*/
public static function fromEntity(Entity $entity){
$ob = new MovingObjectPosition;
$ob->typeOfHit = 1;
$ob->entityHit = $entity;
$ob->hitVector = new Vector3($entity->x, $entity->y, $entity->z);
return $ob;
}
}

View file

@ -0,0 +1,61 @@
<?php
/**
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
/***REM_START***/
require_once("LevelGenerator.php");
/***REM_END***/
class VoidGenerator implements LevelGenerator{
private $level, $random, $structure, $chunks, $options, $floorLevel, $populators = array();
public function __construct(array $options = array()){
$this->options = $options;
/*if(isset($this->options["mineshaft"])){
$this->populators[] = new MineshaftPopulator(isset($this->options["mineshaft"]["chance"]) ? floatval($this->options["mineshaft"]["chance"]) : 0.01);
}*/
}
public function init(Level $level, Random $random){
$this->level = $level;
$this->random = $random;
}
public function generateChunk($chunkX, $chunkZ){
for($Y = 0; $Y < 8; ++$Y){
$this->level->setMiniChunk($chunkX, $chunkZ, $Y, $this->chunks[$Y]);
}
}
public function populateChunk($chunkX, $chunkZ){
foreach($this->populators as $populator){
$this->random->setSeed((int) ($chunkX * 0xdead + $chunkZ * 0xbeef) ^ $this->level->getSeed());
$populator->populate($this->level, $chunkX, $chunkZ, $this->random);
}
}
public function populateLevel(){
$this->random->setSeed($this->level->getSeed());
}
public function getSpawn(){
return new Vector3(128, $this->floorLevel, 128);
}
}