Anonymous 3D Imageboard http://cyberia.digital/
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

335 lignes
10KB

  1. <?php
  2. /*
  3. __ ___ ______ __ ____
  4. / |/ /___ _____ / ____/___ ____ / /__________ / / /__ _____
  5. / /|_/ / __ `/ __ \ / / / __ \/ __ \/ __/ ___/ __ \/ / / _ \/ ___/
  6. / / / / /_/ / /_/ / / /___/ /_/ / / / / /_/ / / /_/ / / / __/ /
  7. /_/ /_/\__,_/ .___/ \____/\____/_/ /_/\__/_/ \____/_/_/\___/_/
  8. /_/
  9. -create a new map
  10. -change an old map
  11. -get map data
  12. -----------------
  13. -get tile values
  14. */
  15. ini_set('display_errors', 1);
  16. require_once "Utils/Constants.php";
  17. require_once "Imports/LazerImports.php";
  18. require_once 'Utils/Helpers.php';
  19. require_once 'Models/Game/Coord.php';
  20. require_once 'Models/Game/Level.php';
  21. require_once 'Models/Game/Tile.php';
  22. use \ForceUTF8\Encoding;
  23. //Autoload usage https://stackoverflow.com/questions/16175023/php-include-class-in-other-class
  24. use Lazer\Classes\Database as Lazer;
  25. defined('LAZER_DATA_PATH') or define('LAZER_DATA_PATH', realpath(dirname(__FILE__)).'/'.Constants::CONST_DB_FOLDER.'/');
  26. Class MapController {
  27. /***************************
  28. * SAVING AND GETTIGNS DATA
  29. ***************************/
  30. function createLevel($level) {
  31. $row = Lazer::table(Constants::CONST_TABLE_MAP);
  32. $row = $level ->fillRow($row);
  33. $row->save();
  34. }
  35. function placePlayerFromTo($to_,$from_){
  36. $row = Lazer::table(Constants::CONST_TABLE_MAP)->where('key', '=', $to_)->find();
  37. $level = Level::parseRow($row);
  38. //get the future player coordonates
  39. $coord = $level -> placePlayerForKey($from_);
  40. //return Coord
  41. return $coord;
  42. }
  43. function getAllTiles(){
  44. return Tile::getAllTiles();
  45. }
  46. /**************************
  47. * MAP PROCEDURAL GENERATION
  48. *************************/
  49. function generateWorld(){
  50. //importnat VARS
  51. $gridData;
  52. $gridWidth = 2;
  53. $gridHeight = 2;
  54. $levelToPlace;
  55. $levelNumMax;
  56. $levelWidthMax;
  57. $levelHeightMax;
  58. //1. generate the dungeon grid
  59. $gridData = $this->fillgrid(null, $gridWidth, $gridHeight);
  60. $levelToPlace = array();
  61. array_push($levelToPlace, new Level(Constants::CONST_MAP_ALPHA_NAME, 15, 13, true, true));
  62. array_push($levelToPlace, new Level("request", 15, 13, true, false) );
  63. array_push($levelToPlace, new Level("conspi", 15, 13, true, false) );
  64. array_push($levelToPlace, new Level("art", 15, 13, true, false) );
  65. array_push($levelToPlace, new Level("lulz", 15, 13, true, true) );
  66. /*
  67. $size = Level::getDim(26);
  68. array_push($levelToPlace, new Level("tripletrap", $size->x, $size->y, true));
  69. $size = Level::getDim(26);
  70. array_push($levelToPlace, new Level("pol", $size->x, $size->y, true) );
  71. $size = Level::getDim(26);
  72. array_push($levelToPlace, new Level("senpai", $size->x, $size->y, true));
  73. $size = Level::getDim(26);
  74. array_push($levelToPlace, new Level("varg", $size->x, $size->y, true) );
  75. $size = Level::getDim(26);
  76. array_push($levelToPlace, new Level("crypto", $size->x, $size->y, true) );*/
  77. //2. place the main level on this grid
  78. /*
  79. foreach ($levelToPlace as $lvl){
  80. $coord = $this->pickPointFromGrid($gridData , $gridWidth, $gridHeight );
  81. if(isset($coord)){
  82. $gridData = $this->placeToGrid($gridData, $lvl, $gridWidth, $gridHeight, $coord );
  83. $lvl -> x = $coord-> x;
  84. $lvl -> y = $coord-> y;
  85. }
  86. }*/
  87. //Genrate room for missing points
  88. /*
  89. $emptyCoord = $this->getCoordsFor($gridData, NULL, $gridWidth, $gridHeight);
  90. for($l = 0; $l < count($levelToPlace); $l++){
  91. $index = rand(0, count($emptyCoord)-1 );
  92. $coord = $emptyCoord[$index];
  93. $level = new Level("???", rand(2,7), rand(2,7), false );
  94. $gridData = $this->placeToGrid($gridData, $level , $gridWidth, $gridHeight, $coord);
  95. array_splice($emptyCoord, $index, 1);
  96. $level-> x = $coord-> x;
  97. $level-> y = $coord-> y;
  98. if(count($emptyCoord) < 1) break;
  99. }*/
  100. $this->displayGrid($gridData, $gridWidth, $gridHeight, true);
  101. //3. each level need to have at least 1 door
  102. /*
  103. $allLevels = $this->getAllLevels($gridData);
  104. foreach ($allLevels as $lvl){
  105. $dir = 0;
  106. $numDoors = rand(1,2);
  107. for( $dir; $dir < 3; $dir ++ ){
  108. //CAUTION level found are the door on the other direction!!!
  109. $levelFound = null;
  110. $oppDir;
  111. $coord = new Coord($lvl-> x, $lvl-> y);
  112. //South
  113. if($dir == 2 ){
  114. $levelFound = $this->getLevelForDir($gridData, $gridWidth, $gridHeight, $coord,$dir);
  115. if(isset( $levelFound->door_key_N )) $levelFound = null;
  116. $oppDir = 0;
  117. }
  118. //West
  119. if($dir == 3 ){
  120. $levelFound = $this->getLevelForDir($gridData, $gridWidth, $gridHeight, $coord,$dir);
  121. if(isset( $levelFound->door_key_E )) $levelFound = null;
  122. $oppDir = 1;
  123. }
  124. //North
  125. if($dir == 0){
  126. $levelFound = $this->getLevelForDir($gridData, $gridWidth, $gridHeight, $coord,$dir);
  127. if(isset( $levelFound->door_key_S )) $levelFound = null;
  128. $oppDir = 2;
  129. }
  130. //East
  131. if($dir == 1 ){
  132. $levelFound = $this->getLevelForDir($gridData, $gridWidth, $gridHeight, $coord,$dir);
  133. if(isset( $levelFound->door_key_W )) $levelFound = null;
  134. $oppDir = 3;
  135. }
  136. if( isset($levelFound) && $numDoors > 0){
  137. $levelFound -> setKeyForDir($lvl->key, $oppDir);
  138. $lvl-> setKeyForDir($levelFound->key, $dir);
  139. $numDoors --;
  140. }
  141. }
  142. //echo $lvl->name." has ".$lvl->getNumDoors()." doors<br/>";
  143. }*/
  144. //Get the level
  145. foreach ($levelToPlace as $lvl){
  146. $lvl -> initLevelData();
  147. $this->createLevel($lvl);
  148. }
  149. }
  150. /****************************
  151. * UTILS for PROCEDURAL
  152. * Generation
  153. ****************************/
  154. function fillgrid($defaultTile_, $width_, $height_){
  155. $newGrid = array();
  156. for ($i = 0; $i < ($height_*$width_); $i++) {
  157. array_push($newGrid,$defaultTile_);
  158. }
  159. return $newGrid;
  160. }
  161. function pickPointFromGrid($gridData, $width_, $height_ ){
  162. for ($i = 0; $i < 10; $i++) {
  163. $x = rand ( 0 , $width_-1);
  164. $y = rand ( 0 , $height_-1);
  165. //is grid coordonate occupped?
  166. $val = $gridData[ ($y*$height_) + $x];
  167. if( !isset($val)) return new Coord($x, $y);
  168. }
  169. return NULL;
  170. }
  171. function placeToGrid($gridData, $data, $width_, $height_, $coord){
  172. $gridData[ ( $coord->y *$width_) + $coord->x] = $data;
  173. return $gridData;
  174. }
  175. function getLevelForCoord($gridData_, $coord_, $width_, $height_){
  176. return $gridData_[ ( $coord_->y *$width_) + $coord_->x];
  177. }
  178. function getCoordsFor($gridData, $data, $width_, $height_){
  179. $listData = array();
  180. for ($y = 0; $y < $height_; $y++) {
  181. for ($x = 0; $x < $width_; $x++) {
  182. if( $gridData[ ($y*$width_) + $x] == $data) array_push($listData, new Coord($x, $y));
  183. }
  184. }
  185. return $listData;
  186. }
  187. /**
  188. * Get all the levels from the griddata
  189. * Return Level[] array;
  190. */
  191. function getAllLevels($gridData){
  192. $levels = array();
  193. foreach ($gridData as $lvl){
  194. if(isset($lvl)) array_push($levels,$lvl);
  195. }
  196. return $levels;
  197. }
  198. function displayGrid($gridData_, $width_, $height_, $is_null){
  199. for ($y = 0; $y < $height_; $y++) {
  200. for ($x = 0; $x < $width_; $x++) {
  201. if( !isset($gridData_[($y*$width_) + $x]) || !isset($gridData_[ ($y*$width_) + $x] -> name)){
  202. echo ".";
  203. } else {
  204. $name = $gridData_[ ($y*$width_) + $x] -> name;
  205. echo substr($name,0,1);
  206. }
  207. }
  208. echo "<br/>";
  209. }
  210. }
  211. function getLevelForDir($gridData_, $width_, $height_, $coord_,$dir_){
  212. switch($dir_){
  213. case 0:
  214. for ($y = $coord_->y -1; $y >= 0; $y--) {
  215. $coord = new Coord( $coord_->x,$y);
  216. $level = $this->getLevelForCoord($gridData_, $coord, $width_, $height_);
  217. if(isset($level)) return $level;
  218. }
  219. break;
  220. case 1:
  221. for ($x = $coord_->x +1; $x < $width_; $x++) {
  222. $coord = new Coord( $x,$coord_->y);
  223. $level = $this->getLevelForCoord($gridData_, $coord, $width_, $height_);
  224. if(isset($level)){
  225. return $level;
  226. }
  227. }
  228. break;
  229. case 2:
  230. for ($y = $coord_->y +1; $y < $height_; $y++) {
  231. $coord = new Coord( $coord_->x,$y);
  232. $level = $this->getLevelForCoord($gridData_, $coord, $width_, $height_);
  233. if(isset($level)) return $level;
  234. }
  235. break;
  236. case 3:
  237. for ($x = $coord_->x -1; $x >= 0; $x--) {
  238. $coord = new Coord( $x,$coord_->y);
  239. $level = $this->getLevelForCoord($gridData_, $coord, $width_, $height_);
  240. if(isset($level)) return $level;
  241. }
  242. break;
  243. }
  244. return null;
  245. }
  246. }