rail logic ported from 1.5.2, disabled 1 action in mobai

This commit is contained in:
GameHerobrine 2023-05-14 20:11:50 +03:00
parent 8bb5667a55
commit 218a000c41
6 changed files with 278 additions and 164 deletions

View file

@ -10,6 +10,14 @@ define("VIEW", 3);
define("VIEWER", 3);
define("SPECTATOR", 3);
//Block Sides
define("SIDE_DOWN", 0);
define("SIDE_UP", 1);
define("SIDE_ZNEG", 2);
define("SIDE_ZPOS", 3);
define("SIDE_XNEG", 4);
define("SIDE_XPOS", 5);
//Players
define("MAX_CHUNK_RATE", 20 / arg("max-chunks-per-second", 4)); //Default rate ~256 kB/s
define("PLAYER_MAX_QUEUE", 1024);

View file

@ -17,7 +17,7 @@ class Sheep extends Animal{
}elseif($this->isBaby() and Entity::$updateOnTick){
$this->server->schedule(mt_rand(400, 1000), [$this, "eatGrass"], null, true); //unknown time
}*/
$this->ai->addTask(new TaskEatTileGoal());
//$this->ai->addTask(new TaskEatTileGoal()); TODO enable after grass spread fix
}
public function createSaveData(){
$data = parent::createSaveData();

View file

@ -1,6 +1,6 @@
<?php
class PoweredRailBlock extends FlowableBlock{
require_once("RailBaseBlock.php");
class PoweredRailBlock extends RailBaseBlock{
public function __construct($meta = 0){
parent::__construct(POWERED_RAIL, 0, "PoweredRailBlock");
$this->hardness = 0.7;
@ -8,18 +8,9 @@ class PoweredRailBlock extends FlowableBlock{
$this->isSolid = true;
}
public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){
$down = $this->getSide(0);
if($down->getID() !== AIR and $down instanceof SolidBlock){
$this->level->setBlock($block, $this, true, false, true);
return true;
}
return false;
}
public function onUpdate($type){
if($type === BLOCK_UPDATE_NORMAL){
if($this->getSide(0)->getID() === AIR){ //Replace with common break method
if($this->getSide(0)->getID() === AIR){ //Replace with common break method TODO change meta on break
ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem($this->id, $this->meta, 1));
$this->level->setBlock($this, new AirBlock(), true, false, true);
return BLOCK_UPDATE_NORMAL;

View file

@ -0,0 +1,24 @@
<?php
abstract class RailBaseBlock extends FlowableBlock //TODO move some methods here
{
public static function isRailBlock(Level $l, $x, $y, $z){
$id = $l->level->getBlockID($x, $y, $z);
return $id === POWERED_RAIL || $id === RAIL;
}
public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){
$down = $this->getSide(0);
if($down->getID() !== AIR and $down instanceof SolidBlock){
$this->level->setBlock($block, $this, true, false, true);
if(RailBlock::$shouldconnectrails){
$logic = new RailLogic($this);
$logic->place(false, true);
}
return true;
}
return false;
}
}

View file

@ -1,6 +1,6 @@
<?php
class RailBlock extends FlowableBlock{
require_once("RailBaseBlock.php");
class RailBlock extends RailBaseBlock{
public function __construct($meta = 0){
parent::__construct(RAIL, $meta, "Rail");
$this->hardness = 0.7;
@ -10,155 +10,6 @@ class RailBlock extends FlowableBlock{
public static $shouldconnectrails = false;
private function isRailBlock($id){
return $id === RAIL || $id === POWERED_RAIL;
}
private function isValidRailMeta($meta){
return !($meta < 0 || $meta > 10);
}
private function canConnectRail($block) {
if(!($block instanceof RailBlock)) return null;
if($this->distanceSquared($block) > 2) return null;
$result = $this->checkRail($this);
if(count($result) === 2) return null;
return $result;
}
private function checkRail($rail){//fixed
if(!($rail instanceof RailBlock)) return null;
$damage = $rail->meta;
if ($damage < 0 || $damage > 10) return null;
$delta = array( //TODO make static
array(array(0, 1), array(0, -1)),
array(array(1, 0), array(-1, 0)),
array(array(1, 0), array(-1, 0)),
array(array(1, 0), array(-1, 0)),
array(array(0, 1), array(0, -1)),
array(array(0, 1), array(0, -1)),
array(array(1, 0), array(0, 1)),
array(array(0, 1), array(-1, 0)),
array(array(-1, 0), array(0, -1)),
array(array(0, -1), array(1, 0))
);
$deltaY = array(0, 1, -1);
$blocks = $delta[$damage];
$connected = array();
foreach($deltaY as $y){
$v3 = new Vector3(
$rail->getFloorX() + $blocks[0][0],
$rail->getFloorY() + $y,
$rail->getFloorZ() + $blocks[0][1]
);
$railBlock = $rail->level->getBlock($v3);
$idToConnect = $railBlock->getID();
$metaToConnect = $railBlock->getMetadata();
if (!$this->isRailBlock($idToConnect) || !$this->isValidRailMeta($metaToConnect)) continue;
$xDiff = $this->getFloorX() - $v3->getFloorX();
$zDiff = $this->getFloorZ() - $v3->getFloorZ();
foreach($blocks as $xz) {
if($xz[0] !== $xDiff || $xz[1] !== $zDiff) continue;
$connected[] = $v3;
}
}
foreach($deltaY as $y){
$v3 = new Vector3(
$rail->getFloorX() + $blocks[1][0],
$rail->getFloorY() + $y,
$rail->getFloorZ() + $blocks[1][1]
);
$railBlock = $rail->level->getBlock($v3);
$idToConnect = $railBlock->getID();
$metaToConnect = $railBlock->getMetadata();
if(!$this->isRailBlock($idToConnect) || !$this->isValidRailMeta($metaToConnect)) continue;
$xDiff = $this->getFloorX() - $v3->getFloorX();
$zDiff = $this->getFloorZ() - $v3->getFloorZ();
foreach($blocks as $xz){
if ($xz[0] !== $xDiff || $xz[1] !== $zDiff) continue;
$connected[] = $v3;
}
}
return $connected;
}
private function connectRail($rail){
$connected = $this->canConnectRail($rail);
if ($connected === null || count($connected) === 0) return;
if(count($connected) === 1){
$v3 = $connected[0]->subtract($this);
$this->meta = ($v3->y !== 1) ? ($v3->x === 0 ? 0 : 1) : (int) ($v3->z === 0 ? ($v3->x / -2) + 2.5 : ($v3->z / 2) + 4.5);
}elseif(count(connected) === 2){
$subtract = array(new Vector3(0, 0, 0),new Vector3(0, 0, 0));
for ($i = 0; $i < count($connected); $i++) {
$subtract[$i] = $connected[$i]->subtract($this);
}
if (abs($subtract[0]->x) === abs($subtract[1]->z) && abs($subtract[1]->x) === abs($subtract[0]->z)){
$v3 = $connected[0]->subtract($this)->add($connected[1]->subtract($this));
$this->meta = $v3->x === 1 ? ($v3->z === 1 ? 6 : 9) : ($v3->z === 1 ? 7 : 8);
}elseif ($subtract[0]->y === 1 || $subtract[1]->y === 1){
$v3 = $subtract[0]->y === 1 ? $subtract[0] : $subtract[1];
$this->meta = $v3->x === 0 ? ($v3->z === -1 ? 4 : 5) : ($v3->x === 1 ? 2 : 3);
}else{
$this->meta = $subtract[0]->x === 0 ? 0 : 1;
}
}
$this->level->setBlock($rail, new RailBlock($this->meta), true, true);
}
public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){
$down = $this->getSide(0);
if($down->getID() !== AIR and $down instanceof SolidBlock){
//Rail connection(ported from nukkit)
if(RailBlock::$shouldconnectrails){
$arrayXZ = array(array(1, 0),array(0, 1),array(-1, 0),array(0, -1));
$arrayY = array(0, 1, -1);
$connected = array();
foreach($arrayXZ as $xz) {
$x = $xz[0];
$z = $xz[1];
foreach($arrayY as $y){
$v3c = new Vector3($x, $y, $z);
$v3 = $v3c->add($this);
$v3block = $this->level->getBlock($v3);
if ($v3block === null) continue;
if (!$this->isRailBlock($v3block->id) || !$this->isValidRailMeta($v3block->meta)) continue;
if (!($v3block instanceof RailBlock)) continue;
$this->connectRail($v3block);
array_push($connected, $v3block);
}
if(count($connected) >= 2) break;
}
if(count($connected) === 1) {
$v3 = $connected[0]->subtract($this);
$this->meta = ($v3->y !== 1) ? ($v3->x === 0 ? 0 : 1) : (int) ($v3->z === 0 ? ($v3->x / -2) + 2.5 : ($v3->z / 2) + 4.5);
}elseif(count($connected) === 2){
$subtract = array(new Vector3(0, 0, 0),new Vector3(0, 0, 0));
for($i = 0; $i < count($connected); $i++){
$subtract[$i] = $connected[$i]->subtract($this);
}
if(abs($subtract[0]->x) === abs($subtract[1]->z) && abs($subtract[1]->x) === abs($subtract[0]->z)) {
$v3 = $connected[0]->subtract($this)->add($connected[1]->subtract($this));
$this->meta = $v3->x === 1 ? ($v3->z === 1 ? 6 : 9) : ($v3->z === 1 ? 7 : 8);
}elseif($subtract[0]->y === 1 || $subtract[1]->y === 1) {
$v3 = $subtract[0]->y == 1 ? $subtract[0] : $subtract[1];
$this->meta = $v3->x === 0 ? ($v3->z === -1 ? 4 : 5) : ($v3->x === 1 ? 2 : 3);
}else{
$this->meta = $subtract[0]->x === 0 ? 0 : 1;
}
}
}
//End of Logic
$this->level->setBlock($block, $this, true, false, true);
return true;
}
return false;
}
public function onUpdate($type){
if($type === BLOCK_UPDATE_NORMAL){
if($this->getSide(0)->getID() === AIR){//Replace with common break method

240
src/utils/RailLogic.php Normal file
View file

@ -0,0 +1,240 @@
<?php
class RailLogic
{
public $x, $y, $z;
public $level;
/**
* @var Vector3[]
*/
public $railPositions = [];
public $isStraight;
public function __construct(RailBaseBlock $rail){
$id = $rail->getID();
$meta = $rail->getMetadata();
$this->level = $rail->level;
$this->x = $rail->x;
$this->y = $rail->y;
$this->z = $rail->z;
$this->isStraight = $id === POWERED_RAIL;
$this->updateConnections($meta);
}
public function getRail($v){
if(($b = $this->level->getBlock($v)) instanceof RailBaseBlock){
return new RailLogic($b);
}else if(($b = $this->level->getBlockWithoutVector($v->x, $v->y + 1, $v->z)) instanceof RailBaseBlock){
return new RailLogic($b);
}else if(($b = $this->level->getBlockWithoutVector($v->x, $v->y - 1, $v->z)) instanceof RailBaseBlock){
return new RailLogic($b);
}
return null;
}
public function removeSoftConnections(){
for($ind = 0; $ind < count($this->railPositions); ++$ind){
$logic = $this->getRail($this->railPositions[$ind]);
if($logic != null && $this->connectsTo($logic)){
$this->railPositions[$ind] = new Vector3($logic->x, $logic->y, $logic->z);
}else{
unset($this->railPositions[$ind--]); //TODO check?
$this->railPositions = array_values($this->railPositions); //reindexing
}
}
}
public function connectsTo(RailLogic $logic){
foreach($this->railPositions as $rpos){
if(($rpos->x === $logic->x) && ($rpos->z === $logic->z)){
return true;
}
}
return false;
}
public function canConnectTo(RailLogic $rail){
//return this.isRailChunkPositionCorrect(par1BlockBaseRailLogic) ? true : (this.railChunkPosition.size() == 2 ? false : (this.railChunkPosition.isEmpty() ? true : true));
return $this->connectsTo($rail) ? true : (count($this->railPositions) == 2 ? false : (empty($this->railPositions) ? true : true));
}
public function hasNeighborRail($x, $y, $z){
$logic = $this->getRail(new Vector3($x, $y, $z));
if($logic === null){
return false;
}else{
$logic->removeSoftConnections(); //i place $this here and was searching why isnt it working correctly for 2 hours
return $logic->canConnectTo($this);
}
}
public function place($b, $b1){
$hasZneg = $this->hasNeighborRail($this->x, $this->y, $this->z - 1);
$hasZpos = $this->hasNeighborRail($this->x, $this->y, $this->z + 1);
$hasXneg = $this->hasNeighborRail($this->x - 1, $this->y, $this->z);
$hasXpos = $this->hasNeighborRail($this->x + 1, $this->y, $this->z);
$state = -1;
if(($hasZneg || $hasZpos) && !($hasXneg && $hasXpos)){
$state = 0;
}
if(($hasXneg || $hasXpos) && !($hasZneg && $hasZpos)){
$state = 1;
}
if(!$this->isStraight){
if($hasZpos && $hasXpos && !($hasZneg && $hasXneg)) $state = 6;
if($hasZpos && $hasXneg && !($hasZneg && $hasXpos)) $state = 7;
if($hasZneg && $hasXneg && !($hasZpos && $hasXpos)) $state = 8;
if($hasZneg && $hasXpos && !($hasZpos && $hasXneg)) $state = 9;
}
if($state === -1){
if($hasZneg || $hasZpos) $state = 0;
if($hasXneg || $hasXpos) $state = 1;
if(!$this->isStraight){
if($b){
if($hasZpos && $hasXpos) $state = 6;
if($hasXneg && $hasZpos) $state = 7;
if($hasXpos && $hasZneg) $state = 9;
if($hasZneg && $hasXneg) $state = 8;
}else{
if($hasZneg && $hasXneg) $state = 8;
if($hasXpos && $hasZneg) $state = 9;
if($hasXneg && $hasZpos) $state = 7;
if($hasZpos && $hasXpos) $state = 6;
}
}
}
if($state === 0){
if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z - 1)) $state = 4;
if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z + 1)) $state = 5;
}else if($state === 1){
if(RailBaseBlock::isRailBlock($this->level, $this->x + 1, $this->y + 1, $this->z)) $state = 2;
if(RailBaseBlock::isRailBlock($this->level, $this->x - 1, $this->y + 1, $this->z)) $state = 3;
}else if($state < 0) $state = 0;
$this->updateConnections($state);
$meta = $state;
if($this->isStraight){
$meta = $this->level->level->getBlockDamage($this->x, $this->x, $this->z) & 8 | $state;
}
if($b1 || $this->level->level->getBlockDamage($this->x, $this->y, $this->z)){
//this.logicWorld.setBlockMetadataWithNotify(this.railX, this.railY, this.railZ, var8, 3);
$bl = $this->level->getBlockWithoutVector($this->x, $this->y, $this->z);
$bl->setMetadata($meta);
$this->level->setBlock(new Vector3($this->x, $this->y, $this->z), $bl, true, false, true);
foreach($this->railPositions as $rpos){
$logic = $this->getRail($rpos);
if($logic !== null){
$logic->removeSoftConnections();
if($logic->canConnectTo($this)){
$logic->connectTo($this);
}
}
}
}
}
public function hasConnection($x, $y, $z){
foreach($this->railPositions as $rpos){
if($rpos->x === $x && $rpos->z === $z) return true;
}
return false;
}
public function connectTo(RailLogic $logic){
$this->railPositions[] = new Vector3($logic->x, $logic->y, $logic->z);
$hasZneg = $this->hasConnection($this->x, $this->y, $this->z - 1);
$hasZpos = $this->hasConnection($this->x, $this->y, $this->z + 1);
$hasXneg = $this->hasConnection($this->x - 1, $this->y, $this->z);
$hasXpos = $this->hasConnection($this->x + 1, $this->y, $this->z);
$state = -1;
if($hasZneg || $hasZpos) $state = 0;
if($hasXneg || $hasXpos) $state = 1;
if(!$this->isStraight){
if($hasZpos && $hasXpos && !($hasZneg && $hasXneg)) $state = 6;
if($hasZpos && $hasXneg && !($hasZneg && $hasXpos)) $state = 7;
if($hasZneg && $hasXneg && !($hasZpos && $hasXpos)) $state = 8;
if($hasZneg && $hasXpos && !($hasZpos && $hasXneg)) $state = 9;
}
if($state == 0){
if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z - 1)) $state = 4;
if(RailBaseBlock::isRailBlock($this->level, $this->x, $this->y + 1, $this->z + 1)) $state = 5;
}else if($state == 1){
if(RailBaseBlock::isRailBlock($this->level, $this->x + 1, $this->y + 1, $this->z)) $state = 2;
if(RailBaseBlock::isRailBlock($this->level, $this->x - 1, $this->y + 1, $this->z)) $state = 3;
}else if($state < 0) $state = 0;
$meta = $state;
if($this->isStraight){
$meta = $this->level->level->getBlockDamage($this->x, $this->x, $this->z) & 8 | $state;
}
$b = $this->level->getBlockWithoutVector($this->x, $this->y, $this->z);
$b->setMetadata($meta);
$this->level->setBlock(new Vector3($this->x, $this->y, $this->z), $b, true, false, true);
}
public function updateConnections($meta){
$this->railPositions = [];
switch($meta){
case 0:
$this->railPositions[] = new Vector3($this->x, $this->y, $this->z - 1);
$this->railPositions[] = new Vector3($this->x, $this->y, $this->z + 1);
break;
case 1:
$this->railPositions[] = new Vector3($this->x - 1, $this->y, $this->z);
$this->railPositions[] = new Vector3($this->x + 1, $this->y, $this->z);
break;
case 2:
$this->railPositions[] = new Vector3($this->x - 1, $this->y, $this->z);
$this->railPositions[] = new Vector3($this->x + 1, $this->y + 1, $this->z);
break;
case 3:
$this->railPositions[] = new Vector3($this->x - 1, $this->y + 1, $this->z);
$this->railPositions[] = new Vector3($this->x + 1, $this->y, $this->z);
break;
case 4:
$this->railPositions[] = new Vector3($this->x, $this->y + 1, $this->z - 1);
$this->railPositions[] = new Vector3($this->x, $this->y, $this->z + 1);
break;
case 5:
$this->railPositions[] = new Vector3($this->x, $this->y, $this->z - 1);
$this->railPositions[] = new Vector3($this->x, $this->y + 1, $this->z + 1);
break;
case 6:
$this->railPositions[] = new Vector3($this->x + 1, $this->y, $this->z);
$this->railPositions[] = new Vector3($this->x, $this->y, $this->z + 1);
break;
case 7:
$this->railPositions[] = new Vector3($this->x - 1, $this->y, $this->z);
$this->railPositions[] = new Vector3($this->x, $this->y, $this->z + 1);
break;
case 8:
$this->railPositions[] = new Vector3($this->x - 1, $this->y, $this->z);
$this->railPositions[] = new Vector3($this->x, $this->y, $this->z - 1);
break;
case 9:
$this->railPositions[] = new Vector3($this->x + 1, $this->y, $this->z);
$this->railPositions[] = new Vector3($this->x, $this->y, $this->z - 1);
break;
}
}
}