value NOT $this->$value; //Our psuedo enums. define("TileType", 0); define("TileValue", 1); //Tile Types define("TileWall", 'w'); //User-placed-wall. define("TileEmpty", 'o'); define("TileStart", 's'); define("TileFinish", 'f'); define("TileCheckpoint", 'c'); define("TileRock", 'r'); define("TileTeleportIn", 't'); define("TileTeleportOut", 'u'); define("TileUnbuildable", 'q'); define("TileSinglePath", 'x'); class map { public $ID; public $tiles; public $teleports; public $checkpoints; //These are headers actually used in the mapCode public $width; public $height; public $walls; public $name; public $flags; public $dateCreated; public $dateExpires; public $isBlind; public $isMultiPath; public $code; public function __construct($code = NULL, $mapID = NULL) { //TODO: Use new code by default. if ($mapID !== NULL) $this->ID = $mapID; if ($code !== NULL) { $dimensions = explode('.', explode(':', $code)[0])[0]; if (strpos($dimensions, 'x') == true) { $this->applyMapFromOldCode($code); } else { $this->applyMapFromCode($code); } } } public function applyMapFromOldCode($code) { //echo "decoding mapcode: [$code]"; $this->code = $code; unset($this->tiles); $tmp = explode( ":", $code); $headers = explode( '.', $tmp[0]); $splitCode = explode( '.', $tmp[1]); $dimensions = explode( 'x', $headers[0]); $this->width = $dimensions[0]; $this->height = $dimensions[1]; $this->walls = substr($headers[3], 1); $this->name = $headers[5]; //Map Name //Make assumptions: $this->isMultiPath = false; $this->isBlind = false; $this->teleports = 0; $this->checkpoints = 0; $t = -1; $index = 0; for ($y = 0; $y < $this->height; $y++) { //Number of Rows for ($x = 0; $x < $this->width; $x++) { //Number of Columns $t++; $next = substr($splitCode[$index], 0, strlen($splitCode[$index]) - 1); //Are we at the next target, if there is one. if ($next == $t AND $next != '') { //Update tile. $type = substr($splitCode[$index], -1, 1); $value = ''; // Count checkpoints and etc. switch ($type) { case 'S': $this->isMultiPath = true; $value = 2; $type = TileStart; break; case 'w': $value = ''; $type = Tilewall; break; case 'r': $value = ''; $type = TileRock; break; case 'R': $value = 2; $type = TileRock; break; case 'q': $value = 3; $type = TileRock; break; case 'x': $value = ''; $type = TileSinglePath; break; case 'X': $value = 2; $type = TileSinglePath; break; //Probably a more intelligent way to do this; but it works: case 'a': $value = ''; $type = TileCheckpoint; break; case 'b': $value = 2; $type = TileCheckpoint; break; case 'c': $value = 3; $type = TileCheckpoint; break; case 'd': $value = 4; $type = TileCheckpoint; break; case 'e': $value = 5; $type = TileCheckpoint; break; case 't': $value = ''; $type = TileTeleportIn; break; case 'm': $value = 2; $type = TileTeleportIn; break; case 'g': $value = 3; $type = TileTeleportIn; break; case 'i': $value = 4; $type = TileTeleportIn; break; case 'k': $value = 5; $type = TileTeleportIn; break; case 'u': $value = ''; $type = TileTeleportOut; break; case 'n': $value = 2; $type = TileTeleportOut; break; case 'h': $value = 3; $type = TileTeleportOut; break; case 'j': $value = 4; $type = TileTeleportOut; break; case 'l': $value = 5; $type = TileTeleportOut; break; } $this->tiles[$y][$x][TileType] = $type; $this->tiles[$y][$x][TileValue] = $value; //Checkpoint values if ($value == '') $value = 1; if ($type == TileCheckpoint) { if ($this->checkpoints < $value) { $this->checkpoints = $value; } } if ($type == TileTeleportIn) { if ($this->teleports < $value) { $this->teleports = $value; } } $index++; //Start from 0 again. $t = -1; } else { $this->tiles[$y][$x][TileType] = TileEmpty; } } } } //From the newer mapcode. public function applyMapFromCode($code) { $this->code = $code; unset($this->tiles); $tmp = explode( ":", $code); $headers = explode( '.', $tmp[0]); $splitCode = explode( '.', $tmp[1]); $this->width = $headers[0]; $this->height = $headers[1]; $this->walls = $headers[2]; $this->name = $headers[3]; $this->flags = $headers[4]; $this->dateCreated = $headers[5]; $this->dateExpires = $headers[6]; //switch FLAGS. $this->isBlind = false; //Make assumptions: $this->isMultiPath = false; $this->teleports = 0; $this->checkpoints = 0; $distance = -1; //First Element $index = 0; $codePart = explode(',', $splitCode[$index]); $next = $codePart[0]; for ($y = 0; $y < $this->height; $y++) { //Number of Rows for ($x = 0; $x < $this->width; $x++) { //Number of Columns $distance++; if ($distance == $next) { $type = $codePart[1][0]; $value = substr($codePart[1], 1); if ($value <= 1) $value = ''; $this->tiles[$y][$x][TileType] = $type; $this->tiles[$y][$x][TileValue] = $value; if ($value == '') $value = 1; if ($type == TileCheckpoint) { if ($this->checkpoints < $value) { $this->checkpoints = $value; } } if ($type == TileTeleportIn) { if ($this->teleports < $value) { $this->teleports = $value; } } //Next Element $index++; $codePart = explode(',', $splitCode[$index]); $next = $codePart[0]; if ($next == '') $next = 0; //Start from 0 again. $distance = -1; } else { $this->tiles[$y][$x][TileType] = TileEmpty; } } } } //Outputs the new code. public function getCode() { //If we already have the code on hand... //if ($this->code) return $this->code; //Otherwise we need to build the code: $headers[0] = $this->width; $headers[1] = $this->height; $headers[3] = $this->walls; $headers[4] = $this->name; $headers[5] = $this->flags; $headers[6] = $this->dateCreated; $headers[7] = $this->dateExpires; $code = implode(".", $headers).':'; for ($y = 0; $y < $this->height; $y++) { //Number of Rows for ($x = 0; $x < $this->width; $x++) { //Number of Columns if ($this->tiles[$y][$x][TileType] !== TileEmpty) { $positionOutput = $position; if ($position == 0) $positionOutput = ''; $code .= $positionOutput.','.$this->tiles[$y][$x][TileType].$this->tiles[$y][$x][TileValue].'.'; $position = -1; } $position++; } } $this->code = $code; return $code; } function getOldCode() { $code = ""; //The string to return to the database. $index = 0; //The current number of tiles from the last tile saved. $code = $this->width.'x'.$this->height; $code .= '.c'.$this->checkpoints; $code .= '.r0'; //rocks... yeah. $code .= '.w'.$this->walls; $code .= '.t'.$this->teleports; $code .= '.'.$this->name; $code .= ".:"; $oldNames['o'] = 'o'; $oldNames['s'] = 's'; $oldNames['s2'] = 'S'; $oldNames['f'] = 'f'; $oldNames['c'] = 'a'; $oldNames['c2'] = 'b'; $oldNames['c3'] = 'c'; $oldNames['c4'] = 'd'; $oldNames['c5'] = 'e'; $oldNames['t'] = 't'; $oldNames['t2'] = 'm'; $oldNames['t3'] = 'g'; $oldNames['t4'] = 'i'; $oldNames['t5'] = 'k'; $oldNames['u'] = 'u'; $oldNames['u2'] = 'n'; $oldNames['u3'] = 'h'; $oldNames['u4'] = 'j'; $oldNames['u5'] = 'l'; $oldNames['x'] = 'x'; $oldNames['x2'] = 'X'; $oldNames['r'] = 'r'; $oldNames['r2'] = 'R'; $oldNames['r3'] = 'q'; $oldNames['p'] = 'p'; for( $i = 0; $i < $this->height; $i++) { for( $j = 0; $j < $this->width; $j++) { if($this->tiles[$i][$j][0] != 'o') { //As long as the tile is NOT open, embed it in the code. $type = $this->tiles[$i][$j][0]; $value = $this->tiles[$i][$j][1]; if ($value <= 1) $value = ''; $code .= $index.$oldNames[$type.$value].'.'; //$code .= $index.$mapMatrix[$i][$j].'.'; //Start from 0 again. $index = -1; } $index += 1; } } return $code; } //Other "Magical" functions. public function __toString() { return $this->name; } } ?>