Anonymous 3D Imageboard http://cyberia.digital/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

362 lines
11KB

  1. <?php
  2. class Level {
  3. public $name;
  4. public $key;
  5. public $theme = "medieval";
  6. public $x;
  7. public $y;
  8. public $width;
  9. public $height;
  10. public $data;
  11. public $door_key_n;
  12. public $door_key_e;
  13. public $door_key_s;
  14. public $door_key_w;
  15. public $is_safe = false;
  16. public $is_chan = false;
  17. public $is_secret = false;
  18. public $level_data;
  19. /*
  20. public $TILE_GROUND = 0;
  21. public $TILE_WALL = 1;
  22. public $TILE_WALL_DRAWABLE = 2;
  23. public $TILE_DOOR = 3;*/
  24. function __construct( $name, $width, $height, $is_chan, $is_secret) {
  25. $this-> name = $name;
  26. $this-> width = $width;
  27. $this-> height = $height;
  28. $this-> is_chan = $is_chan;
  29. $this-> is_secret = $is_secret;
  30. $this-> key = $name;
  31. }
  32. public function setKeyForDir($key_,$dir_){
  33. switch($dir_){
  34. case 0:
  35. $this -> door_key_n = $key_;
  36. break;
  37. case 1:
  38. $this -> door_key_e = $key_;
  39. break;
  40. case 2:
  41. $this -> door_key_s = $key_;
  42. break;
  43. case 3:
  44. $this -> door_key_w = $key_;
  45. break;
  46. }
  47. }
  48. public function getNumDoors(){
  49. $num = 0;
  50. if(isset($this -> door_key_n)) $num ++;
  51. if(isset($this -> door_key_e)) $num ++;
  52. if(isset($this -> door_key_s)) $num ++;
  53. if(isset($this -> door_key_w)) $num ++;
  54. return $num;
  55. }
  56. public function getDoorCoordKey($key_){
  57. $x = 0;
  58. $y = 0;
  59. //test N
  60. if($key_ == $this -> door_key_n){
  61. for($x = 0 ; $x < $this->width ; $x++ ){
  62. $tile = $this -> pickTile($this->width, $this->height,new Coord($x , $y));
  63. if($tile == Tile::getValueForName("gate")) return new Coord($x,$y);
  64. }
  65. } else if($key_ == $this -> door_key_e){
  66. $x = $this->width - 1;
  67. for($y = 0 ; $y < $this->height ; $y++ ){
  68. $tile = $this -> pickTile( $this->width, $this->height, new Coord($x , $y));
  69. if($tile == Tile::getValueForName("gate")) return new Coord($x,$y);
  70. }
  71. } else if($key_ == $this -> door_key_s){
  72. $y = $this->height - 1;
  73. for($x = 0 ; $x < $this->width ; $x++ ){
  74. $tile = $this -> pickTile($this->width, $this->height,new Coord($x , $y));
  75. if($tile == Tile::getValueForName("gate")) return new Coord($x,$y);
  76. }
  77. }else if($key_ == $this -> door_key_w){
  78. for($y = 0 ; $y < $this->height ; $y++ ){
  79. $tile = $this -> pickTile($this->width, $this->height,new Coord($x , $y));
  80. if($tile == Tile::getValueForName("gate")) return new Coord($x,$y);
  81. }
  82. }
  83. return null;
  84. }
  85. public function placePlayerForKey($key_){
  86. $coord = $this -> getDoorCoordKey($key_);
  87. if($coord->y == 0) {
  88. return new Coord($coord->x , $coord->y+1);
  89. } else if($coord->x == $this->width-1) {
  90. return new Coord($coord->x-1 , $coord->y);
  91. }else if($coord->y == $this->height-1) {
  92. return new Coord($coord->x , $coord->y-1);
  93. }else if($coord->x == 0) {
  94. return new Coord($coord->x+1 , $coord->y);
  95. }
  96. return new Coord(1 , 1);
  97. }
  98. public function initLevelData(){
  99. //New version: all the level are the same
  100. $this->level_data =
  101. [
  102. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  103. 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
  104. 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
  105. 1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,
  106. 1,0,0,1,0,0,0,0,0,1,0,4,0,0,1,0,0,1,
  107. 1,0,0,1,0,0,13,0,0,1,0,1,0,0,1,0,0,1,
  108. 1,0,0,1,0,0,0,0,0,1,0,1,0,0,1,0,0,1,
  109. 1,0,0,1,0,0,0,0,0,1,3,1,0,0,1,0,0,1,
  110. 1,0,0,1,1,1,4,1,1,1,1,1,0,0,1,0,0,1,
  111. 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,
  112. 1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,1,
  113. 1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,
  114. 14,0,0,0,0,0,0,0,0,0,0,5,0,0,0,0,0,1,
  115. 1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,
  116. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
  117. ];
  118. $this-> width = 18;
  119. $this-> height = 15;
  120. /*
  121. //Create a middle room
  122. //Middle room looks cool everywhere
  123. $this->fillgrid(Tile::getValueForName("ground"), $this->width, $this->height);
  124. //Place wall
  125. $this->makeWallForLevel();
  126. //place gates
  127. $coord = new Coord(0,0);
  128. if(isset( $this -> door_key_n)){
  129. $coord ->y = 0;
  130. $coord ->x = rand(1, $this->width-2);
  131. $this->placeToLevel(Tile::getValueForName("gate"), $this->width, $this->height, $coord);
  132. }
  133. if(isset( $this -> door_key_e)){
  134. $coord ->y = rand(1, $this->height-2);
  135. $coord ->x = $this->width-1;
  136. $this->placeToLevel(Tile::getValueForName("gate"), $this->width, $this->height, $coord);
  137. }
  138. if(isset( $this -> door_key_s)){
  139. $coord ->y = $this->height-1;
  140. $coord ->x = rand(1, $this->width-2);
  141. $this->placeToLevel(Tile::getValueForName("gate"), $this->width, $this->height, $coord);
  142. }
  143. if(isset( $this -> door_key_w)){
  144. $coord ->y = rand(1, $this->height-2);
  145. $coord ->x = 0;
  146. $this->placeToLevel(Tile::getValueForName("gate"), $this->width, $this->height, $coord);
  147. }*/
  148. //Display the level
  149. $this -> displayLevelData();
  150. }
  151. function makeWallForLevel(){
  152. //North wall
  153. $y = 0;
  154. for ($x = 0 ; $x <= $this-> width ; $x++){
  155. $coord = new Coord($x,$y);
  156. $this->placeToLevel(Tile::getValueForName("wall"), $this->width, $this->height, $coord);
  157. }
  158. //South wall
  159. $y = $this->height-1;
  160. for ($x = 0 ; $x < $this-> width ; $x++){
  161. $coord = new Coord($x,$y);
  162. $this->placeToLevel(Tile::getValueForName("wall"), $this->width, $this->height, $coord);
  163. }
  164. //West
  165. $x = 0;
  166. for ($y = 0 ; $y < $this-> height ; $y++){
  167. $coord = new Coord($x,$y);
  168. $this->placeToLevel(Tile::getValueForName("wall"), $this->width, $this->height, $coord);
  169. }
  170. //Est
  171. $x = $this->width-1;
  172. for ($y = 0 ; $y < $this-> height ; $y++){
  173. $coord = new Coord($x,$y);
  174. $this->placeToLevel(Tile::getValueForName("wall"), $this->width, $this->height, $coord);
  175. }
  176. }
  177. public function displayLevelData(){
  178. echo "# MAP ".$this->name." num tiles:".count($this->level_data)."<br/>";
  179. $x = 0;
  180. $y = 0;
  181. foreach($this->level_data as $data){
  182. //echo "x:".$x."y:".$y."<br/>";
  183. /*
  184. if(!isset( $data ) || $data == Tile::getValueForName("wall") ){
  185. echo "#";
  186. } else if($data == Tile::getValueForName("ground")) {
  187. echo ".";
  188. } else if($data == Tile::getValueForName("door")) {
  189. //echo "x:".$x."y:".$y."<br/>";
  190. echo "D";
  191. }*/
  192. echo Tile::getDisplayForVal($data);
  193. $x ++;
  194. if($x >= $this -> width){
  195. $x = 0;
  196. $y ++;
  197. echo "<br/>";
  198. }
  199. }
  200. }
  201. //Level Data management
  202. function fillgrid($defaultTile_, $width_, $height_){
  203. $this->level_data = array();
  204. for ($i = 0; $i < $height_*$width_; $i++) {
  205. array_push( $this->level_data ,$defaultTile_);
  206. }
  207. }
  208. function placeToLevel($data, $width_, $height_, $coord){
  209. $this->level_data [ ( $coord->y *$width_) + $coord->x] = $data;
  210. }
  211. function pickTile($width_, $height_, $coord){
  212. return $this->level_data [ ( $coord->y *$width_)+ $coord->x];
  213. }
  214. //Fill a data base row
  215. public function fillRow($row_){
  216. echo "Write level: ".$this-> name."; for key".$this-> key." with ".count($this-> level_data)." data";
  217. $row_ -> key = $this-> key;
  218. $row_ -> name = $this-> name;
  219. $row_ -> theme = $this-> theme;
  220. /*
  221. $row_ -> x = $this-> x;
  222. $row_ -> y = $this-> y;
  223. */
  224. $row_ -> width = $this-> width;
  225. $row_ -> height = $this-> height;
  226. if(isset( $this-> door_key_n))$row_ -> door_key_n = $this-> door_key_n;
  227. if(isset( $this-> door_key_e)) $row_ -> door_key_e = $this-> door_key_e;
  228. if(isset( $this-> door_key_s)) $row_ -> door_key_s = $this-> door_key_s;
  229. if(isset( $this-> door_key_w)) $row_ -> door_key_w = $this-> door_key_w;
  230. $dataString = "";
  231. foreach( $this-> level_data as $data ){
  232. $dataString = $dataString.$data.",";
  233. }
  234. $dataString = $dataString .substr(0, strlen($dataString) - 1);
  235. $row_ -> level_data = $dataString;
  236. $row_ -> is_safe = $this-> is_safe;
  237. $row_ -> is_chan = $this-> is_chan;
  238. $row_ -> is_secret = $this-> is_secret;
  239. //var_dump( $row_);
  240. return $row_;
  241. }
  242. //Get Ground tiles
  243. public function getEmptyCoord(){
  244. $coords = array();
  245. $x = 0;
  246. $y = 0;
  247. foreach( $this-> level_data as $data ){
  248. if((int) $data == Tile::getValueForName("ground")){
  249. $coord = new Coord($x,$y);
  250. array_push($coords,$coord);
  251. }
  252. $x ++;
  253. if($x >= $this-> width){
  254. $y ++;
  255. $x = 0;
  256. }
  257. }
  258. return $coords[rand( 0, count( $coords )-1)];
  259. }
  260. //Fill a data base row
  261. public static function parseRow($row_){
  262. $level = new Level("",0,0,false, false);
  263. $level -> key = $row_ -> key;
  264. $level -> name = $row_ -> name;
  265. $level -> theme = $row_ -> theme;
  266. $level -> is_secret = $row_ -> is_secret;
  267. $level -> x = $row_ -> x;
  268. $level -> y = $row_ -> y;
  269. $level -> width = $row_ -> width;
  270. $level -> height = $row_ -> height;
  271. if(isset( $row_-> door_key_n)) $level -> door_key_n = $row_-> door_key_n;
  272. if(isset( $row_-> door_key_e)) $level -> door_key_e = $row_-> door_key_e;
  273. if(isset( $row_-> door_key_s)) $level -> door_key_s = $row_-> door_key_s;
  274. if(isset( $row_-> door_key_w)) $level -> door_key_w = $row_-> door_key_w;
  275. //parse the data
  276. $level -> level_data = explode(",",$row_ -> level_data);
  277. $level -> is_safe = $row_-> is_safe;
  278. $level -> is_chan = $row_-> is_chan;
  279. return $level ;
  280. }
  281. //Fill a data base row
  282. public static function getDim($num){
  283. $minTile = 4;
  284. if($num < $minTile ) $num = $minTile;
  285. $width = rand ( $minTile , $num - $minTile );
  286. $height = $num - $width;
  287. return new Coord( $width, $height);
  288. }
  289. }