Entity velocity

This commit is contained in:
Shoghi Cervantes Pueyo 2013-01-19 12:41:53 +01:00
parent 0fa3dded89
commit aef32f2012
3 changed files with 22 additions and 26 deletions

View File

@ -85,6 +85,9 @@ class LevelAPI{
} }
public function setBlock($x, $y, $z, $block, $meta = 0, $update = true){ public function setBlock($x, $y, $z, $block, $meta = 0, $update = true){
if($x < 0 or $y < 0 or $z < 0){
return false;
}
$this->map->setBlock($x, $y, $z, $block, $meta); $this->map->setBlock($x, $y, $z, $block, $meta);
$this->heightMap[$z][$x] = $this->map->getFloor($x, $z); $this->heightMap[$z][$x] = $this->map->getFloor($x, $z);
if($this->server->api->dhandle("block.change", array( if($this->server->api->dhandle("block.change", array(
@ -99,6 +102,7 @@ class LevelAPI{
$this->server->api->block->updateBlocksAround($x, $y, $z, BLOCK_UPDATE_NORMAL); $this->server->api->block->updateBlocksAround($x, $y, $z, BLOCK_UPDATE_NORMAL);
} }
} }
return true;
} }
public function getOrderedChunk($X, $Z, $columnsPerPacket = 2){ public function getOrderedChunk($X, $Z, $columnsPerPacket = 2){

View File

@ -30,29 +30,22 @@ the Free Software Foundation, either version 3 of the License, or
class UDPSocket{ class UDPSocket{
private $encrypt; private $encrypt;
var $buffer, $connected, $errors, $sock, $server; var $buffer, $connected, $errors, $sock, $server;
function __construct($server, $port, $listen = false, $socket = false){ function __construct($server, $port, $listen = false){
$this->errors = array_fill(88,(125 - 88) + 1, true); $this->errors = array_fill(88,(125 - 88) + 1, true);
$this->server = $server; $this->server = $server;
$this->port = $port; $this->port = $port;
if($socket !== false){ $this->sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$this->sock = $socket; socket_set_option($this->sock, SOL_SOCKET, SO_BROADCAST, 1); //Allow sending broadcast messages
if($listen !== true){
$this->connected = true; $this->connected = true;
$this->buffer = array(); $this->buffer = array();
$this->unblock(); $this->unblock();
}else{ }else{
$this->sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); if(socket_bind($this->sock, "0.0.0.0", $port) === true){
socket_set_option($this->sock, SOL_SOCKET, SO_BROADCAST, 1);
if($listen !== true){
$this->connected = true;
$this->buffer = array();
$this->unblock(); $this->unblock();
}else{ }else{
if(socket_bind($this->sock, "0.0.0.0", $port) === true){ console("[ERROR] Couldn't bind to 0.0.0.0:".$port, true, true, 0);
$this->unblock(); die();
}else{
console("[ERROR] Couldn't bind to 0.0.0.0:".$port, true, true, 0);
die();
}
} }
} }
} }

View File

@ -33,7 +33,7 @@ define("ENTITY_ITEM", 3);
define("ENTITY_PAINTING", 4); define("ENTITY_PAINTING", 4);
class Entity extends stdClass{ class Entity extends stdClass{
var $invincible = false, $eid, $type, $name, $x, $y, $z, $speedX, $speedY, $speedZ, $last = array(0, 0, 0, 0), $yaw, $pitch, $dead, $data, $class, $attach, $metadata, $closed, $player, $onTick; var $invincible = false, $eid, $type, $name, $x, $y, $z, $speedX, $speedY, $speedZ, $speed, $last = array(0, 0, 0, 0), $yaw, $pitch, $dead, $data, $class, $attach, $metadata, $closed, $player, $onTick;
private $server; private $server;
function __construct($server, $eid, $class, $type = 0, $data = array()){ function __construct($server, $eid, $class, $type = 0, $data = array()){
$this->server = $server; $this->server = $server;
@ -49,7 +49,7 @@ class Entity extends stdClass{
$this->closed = false; $this->closed = false;
$this->name = ""; $this->name = "";
$this->server->query("INSERT OR REPLACE INTO entities (EID, type, class, health) VALUES (".$this->eid.", ".$this->type.", ".$this->class.", ".$this->health.");"); $this->server->query("INSERT OR REPLACE INTO entities (EID, type, class, health) VALUES (".$this->eid.", ".$this->type.", ".$this->class.", ".$this->health.");");
$this->server->schedule(20, array($this, "update"), array(), true); $this->server->schedule(2, array($this, "update"), array(), true);
$this->metadata = array(); $this->metadata = array();
$this->x = isset($this->data["x"]) ? $this->data["x"]:0; $this->x = isset($this->data["x"]) ? $this->data["x"]:0;
$this->y = isset($this->data["y"]) ? $this->data["y"]:0; $this->y = isset($this->data["y"]) ? $this->data["y"]:0;
@ -57,6 +57,7 @@ class Entity extends stdClass{
$this->speedX = isset($this->data["speedX"]) ? $this->data["speedX"]:0; $this->speedX = isset($this->data["speedX"]) ? $this->data["speedX"]:0;
$this->speedY = isset($this->data["speedY"]) ? $this->data["speedY"]:0; $this->speedY = isset($this->data["speedY"]) ? $this->data["speedY"]:0;
$this->speedZ = isset($this->data["speedZ"]) ? $this->data["speedZ"]:0; $this->speedZ = isset($this->data["speedZ"]) ? $this->data["speedZ"]:0;
$this->speed = 0;
$this->yaw = isset($this->data["yaw"]) ? $this->data["yaw"]:0; $this->yaw = isset($this->data["yaw"]) ? $this->data["yaw"]:0;
$this->pitch = isset($this->data["pitch"]) ? $this->data["pitch"]:0; $this->pitch = isset($this->data["pitch"]) ? $this->data["pitch"]:0;
$this->position = array("x" => &$this->x, "y" => &$this->y, "z" => &$this->z, "yaw" => &$this->yaw, "pitch" => &$this->pitch); $this->position = array("x" => &$this->x, "y" => &$this->y, "z" => &$this->z, "yaw" => &$this->yaw, "pitch" => &$this->pitch);
@ -79,6 +80,7 @@ class Entity extends stdClass{
} }
public function update(){ public function update(){
$this->calculateVelocity();
$this->server->api->dhandle("entity.move", $this); $this->server->api->dhandle("entity.move", $this);
if($this->class === ENTITY_ITEM and $this->closed === false){ if($this->class === ENTITY_ITEM and $this->closed === false){
$player = $this->server->query("SELECT EID FROM entities WHERE class == ".ENTITY_PLAYER." AND abs(x - {$this->x}) <= 1.5 AND abs(y - {$this->y}) <= 1.5 AND abs(z - {$this->z}) <= 1.5 LIMIT 1;", true); $player = $this->server->query("SELECT EID FROM entities WHERE class == ".ENTITY_PLAYER." AND abs(x - {$this->x}) <= 1.5 AND abs(y - {$this->y}) <= 1.5 AND abs(z - {$this->z}) <= 1.5 LIMIT 1;", true);
@ -202,8 +204,6 @@ class Entity extends stdClass{
$this->y = $y; $this->y = $y;
$this->z = $z; $this->z = $z;
$this->server->query("UPDATE entities SET x = ".$this->x.", y = ".$this->y.", z = ".$this->z." WHERE EID = ".$this->eid.";"); $this->server->query("UPDATE entities SET x = ".$this->x.", y = ".$this->y.", z = ".$this->z." WHERE EID = ".$this->eid.";");
$this->updateVelocity();
$this->server->api->dhandle("entity.move", $this);
} }
public function move($x, $y, $z, $yaw = 0, $pitch = 0){ public function move($x, $y, $z, $yaw = 0, $pitch = 0){
@ -215,8 +215,6 @@ class Entity extends stdClass{
$this->pitch += $pitch; $this->pitch += $pitch;
$this->pitch %= 90; $this->pitch %= 90;
$this->server->query("UPDATE entities SET x = ".$this->x.", y = ".$this->y.", z = ".$this->z.", pitch = ".$this->pitch.", yaw = ".$this->yaw." WHERE EID = ".$this->eid.";"); $this->server->query("UPDATE entities SET x = ".$this->x.", y = ".$this->y.", z = ".$this->z.", pitch = ".$this->pitch.", yaw = ".$this->yaw." WHERE EID = ".$this->eid.";");
$this->updateVelocity();
$this->server->api->dhandle("entity.move", $this);
} }
public function setPosition($x, $y, $z, $yaw, $pitch){ public function setPosition($x, $y, $z, $yaw, $pitch){
@ -226,8 +224,6 @@ class Entity extends stdClass{
$this->yaw = $yaw; $this->yaw = $yaw;
$this->pitch = $pitch; $this->pitch = $pitch;
$this->server->query("UPDATE entities SET x = ".$this->x.", y = ".$this->y.", z = ".$this->z.", pitch = ".$this->pitch.", yaw = ".$this->yaw." WHERE EID = ".$this->eid.";"); $this->server->query("UPDATE entities SET x = ".$this->x.", y = ".$this->y.", z = ".$this->z.", pitch = ".$this->pitch.", yaw = ".$this->yaw." WHERE EID = ".$this->eid.";");
$this->updateVelocity();
$this->server->api->dhandle("entity.move", $this);
} }
public function inBlock($x, $y, $z){ public function inBlock($x, $y, $z){
@ -239,18 +235,21 @@ class Entity extends stdClass{
return false; return false;
} }
public function updateVelocity(){ public function calculateVelocity(){
$diffTime = microtime(true) - $this->last[3]; $diffTime = microtime(true) - $this->last[3];
$this->last[3] = microtime(true); $this->last[3] = microtime(true);
$speedX = ($this->x - $this->last[0]) / $diffTime; $origin = new Vector3($this->last[0], $this->last[1], $this->last[2]);
$final = new Vector3($this->x, $this->y, $this->z);
$speedX = abs($this->x - $this->last[0]) / $diffTime;
$this->last[0] = $this->x; $this->last[0] = $this->x;
$speedY = ($this->y - $this->last[1]) / $diffTime; $speedY = ($this->y - $this->last[1]) / $diffTime;
$this->last[1] = $this->y; $this->last[1] = $this->y;
$speedZ = ($this->z - $this->last[2]) / $diffTime; $speedZ = abs($this->z - $this->last[2]) / $diffTime;
$this->last[2] = $this->z; $this->last[2] = $this->z;
$this->speedX = $speedX; $this->speedX = $speedX;
$this->speedY = $speedY; $this->speedY = $speedY;
$this->speedZ = $speedZ; $this->speedZ = $speedZ;
$this->speed = $origin->distance($final) / $diffTime;
} }
public function getPosition($round = false){ public function getPosition($round = false){