ported rail logic from 0.8

This commit is contained in:
GameHerobrine 2023-11-30 07:38:03 +03:00
parent e9a3ba165d
commit 648784b4ff
7 changed files with 346 additions and 192 deletions

View file

@ -22,4 +22,12 @@ define("BLOCK_UPDATE_NORMAL", 1);
define("BLOCK_UPDATE_RANDOM", 2);
define("BLOCK_UPDATE_SCHEDULED", 3);
define("BLOCK_UPDATE_WEAK", 4);
define("BLOCK_UPDATE_TOUCH", 5);
define("BLOCK_UPDATE_TOUCH", 5);
//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);

View file

@ -1,31 +1,11 @@
<?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;
$this->isFullBlock = false;
$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
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;
}
}
return false;
$this->isFullBlock = false;
$this->isSolid = false;
}
}

View file

@ -0,0 +1,56 @@
<?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);
$logic = new RailLogic($this);
$logic->place(false, true);
return true;
}
return false;
}
public function updateState(){}
public function onUpdate($type){
if($type === BLOCK_UPDATE_NORMAL){
$meta = $this->getMetadata();
if($this->id === POWERED_RAIL){
$meta &= 7;
}
$x = $this->x;
$y = $this->y;
$z = $this->z;
if(
($this->level->level->getBlockID($x, $y - 1, $z) == 0) ||
(($meta == 2) && $this->level->level->getBlockID($x + 1, $y, $z) == 0) ||
(($meta == 3) && $this->level->level->getBlockID($x - 1, $y, $z) == 0) ||
(($meta == 4) && $this->level->level->getBlockID($x, $y, $z - 1) == 0) ||
(($meta == 5) && $this->level->level->getBlockID($x, $y, $z + 1) == 0)
){
$this->level->setBlock($this, new AirBlock(), true, false, true);
ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem($this->id, $this->meta, 1));
}else{
$this->updateState();
}
//if($this->getSide(0)->getID() === AIR){//Replace with common break method
// ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem($this->id, $this->meta, 1));
// $this->level->setBlock($this, new AirBlock(), true, false, true);
//}
}
}
}

View file

@ -1,173 +1,21 @@
<?php
class RailBlock extends FlowableBlock{
require_once("RailBaseBlock.php");
class RailBlock extends RailBaseBlock{
public static $shouldconnectrails = true;
public function __construct($meta = 0){
parent::__construct(RAIL, $meta, "Rail");
$this->hardness = 0.7;
$this->isFullBlock = false;
$this->isFullBlock = false;
$this->isSolid = false;
}
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
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;
}
}
return false;
public function updateState(){
//$logic = (new RailLogic($this));
//if($logic->countPotentialConnections() == 3){
// $logic->place(false, false);
//}
}
}

View file

@ -12,10 +12,10 @@ class MinecartItem extends Item{
}
$server = ServerAPI::request();
$data = [
"x" => $target->getX(),
"y" => $target->getY() + 0.8,
"z" => $target->getZ(),
];
"x" => $target->getX() + 0.5,
"y" => $target->getY() + 0.8,
"z" => $target->getZ() + 0.5,
];
$e = $server->api->entity->add($level, ENTITY_OBJECT, OBJECT_MINECART, $data);
$server->api->entity->spawnToAll($e);
if(($player->gamemode & 0x01) === 0x00){

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

@ -0,0 +1,248 @@
<?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);
}elseif(($b = $this->level->getBlockWithoutVector($v->x, $v->y + 1, $v->z)) instanceof RailBaseBlock){
return new RailLogic($b);
}elseif(($b = $this->level->getBlockWithoutVector($v->x, $v->y - 1, $v->z)) instanceof RailBaseBlock){
return new RailLogic($b);
}
return null;
}
public function hasRail($x, $y, $z){
return RailBaseBlock::isRailBlock($this->level, $x, $y, $z) || RailBaseBlock::isRailBlock($this->level, $x, $y + 1, $z) || RailBaseBlock::isRailBlock($this->level, $x, $y - 1, $z);
}
public function countPotentialConnections(){
return $this->hasRail($this->x, $this->y, $this->z - 1) + $this->hasRail($this->x, $this->y, $this->z + 1) + $this->hasRail($this->x - 1, $this->y, $this->z) + $this->hasRail($this->x + 1, $this->y, $this->z);
}
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;
}elseif($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;
}elseif($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;
}elseif($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;
}elseif($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);
$this->level->fastSetBlockUpdateMeta($this->x, $this->y, $this->z, $meta, 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;
}
}
}

View file

@ -122,6 +122,20 @@ class Level{
}
}
public function fastSetBlockUpdateMeta($x, $y, $z, $meta, $updateBlock = false){
$this->level->setBlockDamage($x, $y, $z, $meta);
$pk = new UpdateBlockPacket;
$pk->x = $x;
$pk->y = $y;
$pk->z = $z;
$pk->block = $this->level->getBlockID($x, $y, $z);
$pk->meta = $meta;
$this->server->api->player->broadcastPacket($this->players, $pk);
if($updateBlock){
$this->server->api->block->blockUpdateAround(new Position($x, $y, $z, $this), BLOCK_UPDATE_NORMAL, 1);
}
}
public function getOrderedFullChunk($X, $Z){
$X = (int)$X;
$Z = (int)$Z;