mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-21 12:09:43 +00:00
ported rail logic from 0.8
This commit is contained in:
parent
e9a3ba165d
commit
648784b4ff
7 changed files with 346 additions and 192 deletions
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
56
src/material/block/nonfull/RailBaseBlock.php
Normal file
56
src/material/block/nonfull/RailBaseBlock.php
Normal 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);
|
||||
//}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
//}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue