PageRenderTime 61ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/braldahim/library/Bral/Competences/Creuser.php

https://github.com/Canop/braldahim
PHP | 381 lines | 296 code | 67 blank | 18 comment | 84 complexity | 764f50e3e87c8e8f1aba7c51dd27bc04 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of Braldahim, under Gnu Public Licence v3.
  4. * See licence.txt or http://www.gnu.org/licenses/gpl-3.0.html
  5. * Copyright: see http://www.braldahim.com/sources
  6. */
  7. class Bral_Competences_Creuser extends Bral_Competences_Competence {
  8. function prepareCommun() {
  9. Zend_Loader::loadClass('Tunnel');
  10. Zend_Loader::loadClass("Bral_Util_Metier");
  11. $this->view->creuserOk = false;
  12. $this->prepareTableau(false);
  13. }
  14. private function prepareTableau($supprimeMinerai) {
  15. $tabNiveauxValides[] = -10;
  16. $tabNiveauxValides[] = -11;
  17. $tabNiveauxValides[] = -12;
  18. $tabNiveauxValides[] = -13;
  19. if (!in_array($this->view->user->z_braldun, $tabNiveauxValides)) {
  20. $this->view->niveauValide = false;
  21. return;
  22. } else {
  23. $this->view->niveauValide = true;
  24. }
  25. Zend_Loader::loadClass("Bral_Util_Dijkstra");
  26. $dijkstra = new Bral_Util_Dijkstra();
  27. $dijkstra->calcul(1, $this->view->user->x_braldun, $this->view->user->y_braldun, $this->view->user->z_braldun, null, false);
  28. $this->distance = 1;
  29. $x_min = $this->view->user->x_braldun - $this->distance;
  30. $x_max = $this->view->user->x_braldun + $this->distance;
  31. $y_min = $this->view->user->y_braldun - $this->distance;
  32. $y_max = $this->view->user->y_braldun + $this->distance;
  33. Zend_Loader::loadClass("Zone");
  34. $zoneTable = new Zone();
  35. $zones = $zoneTable->selectVue($x_min, $y_min, $x_max, $y_max, $this->view->user->z_braldun);
  36. $this->distance = 2;
  37. $x_min = $this->view->user->x_braldun - $this->distance;
  38. $x_max = $this->view->user->x_braldun + $this->distance;
  39. $y_min = $this->view->user->y_braldun - $this->distance;
  40. $y_max = $this->view->user->y_braldun + $this->distance;
  41. Zend_Loader::loadClass("Filon");
  42. $filonTable = new Filon();
  43. $tunnelTable = new Tunnel();
  44. $tunnels = $tunnelTable->selectVue($x_min, $y_min, $x_max, $y_max, $this->view->user->z_braldun);
  45. $defautChecked = false;
  46. $tabTunnels = null;
  47. $tabZones = null;
  48. for ($j = $this->distance; $j >= -$this->distance; $j--) {
  49. for ($i = -$this->distance; $i <= $this->distance; $i++) {
  50. $x = $this->view->user->x_braldun + $i;
  51. $y = $this->view->user->y_braldun + $j;
  52. $tabTunnels[$x][$y] = false;
  53. $tabTunnelsPossibles[$x][$y] = false;
  54. if (count($zones) > 1) { // s'il y a plus de 2 zones, on fait un contrôle
  55. $tabZones[$x][$y] = false;
  56. } else {
  57. $tabZones[$x][$y] = true;
  58. }
  59. }
  60. }
  61. foreach ($tunnels as $t) {
  62. $tabTunnels[$t["x_tunnel"]][$t["y_tunnel"]] = true;
  63. }
  64. if (count($zones) > 1) { // s'il y a plus de 2 zones, on fait un contrôle
  65. $this->calculTabZones($zones, $tabZones);
  66. }
  67. $this->distance = 1;
  68. $filonsSupprimes = 0;
  69. $numero = -1;
  70. for ($j = $this->distance; $j >= -$this->distance; $j--) {
  71. for ($i = -$this->distance; $i <= $this->distance; $i++) {
  72. $x = $this->view->user->x_braldun + $i;
  73. $y = $this->view->user->y_braldun + $j;
  74. $numero++;
  75. $nDecouverte = 0;
  76. if ($tabTunnels[$x - 1][$y - 1] === true) $nDecouverte++;
  77. if ($tabTunnels[$x - 1][$y] === true) $nDecouverte++;
  78. if ($tabTunnels[$x - 1][$y + 1] === true) $nDecouverte++;
  79. if ($tabTunnels[$x][$y + 1] === true) $nDecouverte++;
  80. if ($tabTunnels[$x][$y - 1] === true) $nDecouverte++;
  81. if ($tabTunnels[$x + 1][$y + 1] === true) $nDecouverte++;
  82. if ($tabTunnels[$x + 1][$y] === true) $nDecouverte++;
  83. if ($tabTunnels[$x + 1][$y - 1] === true) $nDecouverte++;
  84. //La case à creuser doit avoir au minimum 4 cases adjacentes non évidées
  85. if ($nDecouverte < 3) {
  86. // on regarde la distance dijkstra
  87. if ($dijkstra->getDistance($numero) == 1) {
  88. $tabTunnelsPossibles[$x][$y] = true;
  89. }
  90. }
  91. }
  92. }
  93. $this->distance = 1;
  94. for ($j = $this->distance; $j >= -$this->distance; $j--) {
  95. $change_level = true;
  96. for ($i = -$this->distance; $i <= $this->distance; $i++) {
  97. $x = $this->view->user->x_braldun + $i;
  98. $y = $this->view->user->y_braldun + $j;
  99. $caseCourante = false;
  100. $display = $x;
  101. $display .= " ; ";
  102. $display .= $y;
  103. $tunnelTrouve = false;
  104. $valid = false;
  105. if ($tabZones[$x][$y] === true && $tabTunnelsPossibles[$x][$y] === true && $tabTunnels[$x][$y] === false) {
  106. $valid = true;
  107. }
  108. if (($j == 0 && $i == 0) == true) { // on n'affiche pas de boutons dans la case du milieu
  109. $valid = false;
  110. $caseCourante = true;
  111. }
  112. if ($x < $this->view->config->game->x_min || $x > $this->view->config->game->x_max
  113. || $y < $this->view->config->game->y_min || $y > $this->view->config->game->y_max
  114. ) { // on n'affiche pas de boutons dans la case du milieu
  115. $valid = false;
  116. }
  117. // si l'on a effectué l'action, on supprime les filons
  118. // où les cases sont non valides, où la construction de tunnel n'est pas possible
  119. // et où il n'y a pas déjà un tunnel
  120. if ($supprimeMinerai && !$valid && $tabTunnelsPossibles[$x][$y] == false && $tabTunnels[$x][$y] === false && !$caseCourante) {
  121. $nb = $filonTable->delete("x_filon = " . $x . " and y_filon = " . $y . " and z_filon=" . $this->view->user->z_braldun);
  122. $filonsSupprimes = $filonsSupprimes + $nb;
  123. }
  124. if ($valid === true && $defautChecked == false) {
  125. $default = "checked";
  126. $defautChecked = true;
  127. $this->view->creuserOk = true;
  128. } else {
  129. $default = "";
  130. }
  131. $tab[] = array("x_offset" => $i,
  132. "y_offset" => $j,
  133. "default" => $default,
  134. "display" => $display,
  135. "change_level" => $change_level, // nouvelle ligne dans le tableau
  136. "valid" => $valid
  137. );
  138. $tabValidation[$i][$j] = $valid;
  139. if ($change_level) {
  140. $change_level = false;
  141. }
  142. }
  143. }
  144. $this->view->filonsSupprimes = $filonsSupprimes;
  145. $this->view->tableau = $tab;
  146. $this->tableauValidation = $tabValidation;
  147. }
  148. function prepareFormulaire() {
  149. if ($this->view->assezDePa == false) {
  150. return;
  151. }
  152. }
  153. function prepareResultat() {
  154. // Verification des Pa
  155. if ($this->view->assezDePa == false) {
  156. throw new Zend_Exception(get_class($this) . " Pas assez de PA : " . $this->view->user->pa_braldun);
  157. }
  158. if ($this->view->niveauValide == false) {
  159. throw new Zend_Exception(get_class($this) . " Niveau invalide : " . $this->view->user->z_braldun);
  160. }
  161. if ($this->view->creuserOk == false) {
  162. throw new Zend_Exception(get_class($this) . " Monter Palissade interdit");
  163. }
  164. // on verifie que l'on peut monter une tunnel sur la case
  165. $x_y = $this->request->get("valeur_1");
  166. list ($offset_x, $offset_y) = preg_split("/h/", $x_y);
  167. if ($offset_x < -$this->distance || $offset_x > $this->distance) {
  168. throw new Zend_Exception(get_class($this) . " Creuser X impossible : " . $offset_x);
  169. }
  170. if ($offset_y < -$this->distance || $offset_y > $this->distance) {
  171. throw new Zend_Exception(get_class($this) . " Creuser Y impossible : " . $offset_y);
  172. }
  173. if ($this->tableauValidation[$offset_x][$offset_y] !== true) {
  174. throw new Zend_Exception(get_class($this) . " Creuser XY impossible : " . $offset_x . $offset_y);
  175. }
  176. // calcul des jets
  177. $this->calculJets();
  178. if ($this->view->okJet1 === true) {
  179. $this->calculCreuser($this->view->user->x_braldun + $offset_x, $this->view->user->y_braldun + $offset_y);
  180. }
  181. $this->calculPx();
  182. $this->calculPoids();
  183. $this->calculBalanceFaim();
  184. $this->majBraldun();
  185. }
  186. private function calculCreuser($x, $y) {
  187. if (Bral_Util_De::get_1d10() > 5) { // 4 chances sur 10
  188. $nidDecouvert = $this->calculNid($x, $y, $this->view->user->z_braldun);
  189. }
  190. $this->view->user->x_braldun = $x;
  191. $this->view->user->y_braldun = $y;
  192. $data = array(
  193. "x_tunnel" => $x,
  194. "y_tunnel" => $y,
  195. "z_tunnel" => $this->view->user->z_braldun,
  196. "date_tunnel" => date("Y-m-d H:00:00"),
  197. "est_eboulable_tunnel" => 'oui',
  198. );
  199. $tunnelTable = new Tunnel();
  200. $tunnelTable->insert($data);
  201. unset($tunnelTable);
  202. // Pour la suppression des filons invalides
  203. $this->prepareTableau(true);
  204. Zend_Loader::loadClass("StatsFabricants");
  205. $statsFabricants = new StatsFabricants();
  206. $moisEnCours = mktime(0, 0, 0, date("m"), 2, date("Y"));
  207. $dataFabricants["niveau_braldun_stats_fabricants"] = $this->view->user->niveau_braldun;
  208. $dataFabricants["id_fk_braldun_stats_fabricants"] = $this->view->user->id_braldun;
  209. $dataFabricants["mois_stats_fabricants"] = date("Y-m-d", $moisEnCours);
  210. $dataFabricants["nb_piece_stats_fabricants"] = 1;
  211. $dataFabricants["id_fk_metier_stats_fabricants"] = Bral_Util_Metier::METIER_MINEUR_ID;
  212. $statsFabricants->insertOrUpdate($dataFabricants);
  213. $this->view->tunnel = $data;
  214. }
  215. private function calculNid($x, $y, $z) {
  216. // on récupère l'entrée de mine la plus proche au niveau 0;
  217. Zend_Loader::loadClass("TypeLieu");
  218. Zend_Loader::loadClass("Lieu");
  219. $lieuTable = new Lieu();
  220. $lieuRowset = $lieuTable->findByTypeAndPosition(TypeLieu::ID_TYPE_MINE, $this->view->user->x_braldun, $this->view->user->y_braldun, "non");
  221. if ($lieuRowset == null || count($lieuRowset) < 1) {
  222. throw new Zend_Exception("Erreur nb mine x:" . $this->view->user->x_braldun . " y:" . $this->view->user->y_braldun);
  223. }
  224. $lieu = $lieuRowset[0];
  225. $distance = $lieu["distance"];
  226. //if ($distance < 11) { // 11: distance = 5 + niveauMin x 3, avec niveauMin=2, distanceMin : 11
  227. if ($distance < 11) {
  228. return false; // pas de monstre à poper pour une distance < 11
  229. }
  230. Zend_Loader::loadClass("Bral_Batchs_Batch");
  231. Zend_Loader::loadClass("Bral_Batchs_CreationNids");
  232. Zend_Loader::loadClass("TypeMonstre");
  233. Zend_Loader::loadClass("ZoneNid");
  234. Zend_Loader::loadClass("CreationNids");
  235. Zend_Loader::loadClass("Nid");
  236. $nbMonstres = Bral_Util_De::get_de_specifique(Bral_Batchs_CreationNids::NB_MONSTRES_PAR_NID_MIN, Bral_Batchs_CreationNids::NB_MONSTRES_PAR_NID_MAX);
  237. $nbJours = Bral_Util_De::get_de_specifique(0, 4);
  238. // Recuperation de la zone de nid
  239. $zoneNidTable = new ZoneNid();
  240. $zones = $zoneNidTable->findByCase($x, $y, $z);
  241. if ($zones == null || count($zones) > 1) {
  242. throw new Zend_Exception("Creuser: Erreur Parametrage zone nid: x:" . $x . " y:" . $y . " z:" . $z . " n:" . count($zones));
  243. }
  244. $idZoneNid = $zones[0]["id_zone_nid"];
  245. $typeMonstreTable = new TypeMonstre();
  246. $niveauMin = ($distance - 5) / 3;
  247. $niveauMax = ($distance + 15 - 5) / 3;
  248. if ($niveauMin > 18) {
  249. $niveauMin = 18;
  250. }
  251. if ($niveauMax > 39) {
  252. $niveauMax = 1000;
  253. }
  254. // Récupération des types de monstres associés à la zone de nid
  255. $creationNidsTable = new CreationNids();
  256. $typesMonstres = $creationNidsTable->findIdTypeMonstreNiveauMinMaxByIdZone($idZoneNid, $niveauMin, $niveauMax);
  257. if ($typesMonstres == null || count($typesMonstres) < 1) {
  258. throw new Zend_Exception("Creuser: Erreur Parametrage 2 zone nid: " . $idZoneNid . " min:" . $niveauMin . " max:" . $niveauMax);
  259. }
  260. $idKey = Bral_Util_De::get_de_specifique(0, count($typesMonstres) - 1);
  261. $typeMonstre = $typesMonstres[$idKey];
  262. $idTypeMonstre = $typeMonstre["id_type_monstre"];
  263. if ($idTypeMonstre == TypeMonstre::ID_TYPE_BALROG) {
  264. Zend_Loader::loadClass("Monstre");
  265. $monstreTable = new Monstre();
  266. if ($monstreTable->countAllByType($idTypeMonstre) > 0) {
  267. $idTypeMonstre = $idTypeMonstre - 1;
  268. } else {
  269. $nbMonstres = 1;
  270. $nbJours = 0;
  271. Zend_Loader::loadClass("Bral_Util_Tracemail");
  272. Bral_Util_Tracemail::traite("Creation du Balrog en $x, $y, $z", $this->view, "Balrog !");
  273. }
  274. } else {
  275. Zend_Loader::loadClass("Bral_Util_Tracemail");
  276. Bral_Util_Tracemail::traite("Apparition monstre en mine en $x, $y, $z", $this->view, "Monstre en mine !");
  277. }
  278. $data = array(
  279. 'x_nid' => $x,
  280. 'y_nid' => $y,
  281. 'z_nid' => $z,
  282. 'nb_monstres_total_nid' => $nbMonstres,
  283. 'nb_monstres_restants_nid' => $nbMonstres,
  284. 'id_fk_zone_nid' => $idZoneNid,
  285. 'id_fk_type_monstre_nid' => $idTypeMonstre,
  286. 'date_creation_nid' => date("Y-m-d H:i:s"),
  287. 'date_generation_nid' => Bral_Util_ConvertDate::get_date_add_day_to_date(date("Y-m-d H:i:s"), $nbJours),
  288. );
  289. $nidTable = new Nid();
  290. $nidTable->insert($data);
  291. return true;
  292. }
  293. function getListBoxRefresh() {
  294. return $this->constructListBoxRefresh(array("box_competences", "box_vue"));
  295. }
  296. private function calculTabZones($zones, &$tabZones) {
  297. $distance = 1;
  298. foreach ($zones as $z) {
  299. if ($z["est_mine_zone"] == "oui") {
  300. for ($j = $distance; $j >= -$distance; $j--) {
  301. for ($i = -$distance; $i <= $distance; $i++) {
  302. $x = $this->view->user->x_braldun + $i;
  303. $y = $this->view->user->y_braldun + $j;
  304. if ($x >= $z["x_min_zone"] && $x <= $z["x_max_zone"] &&
  305. $y >= $z["y_min_zone"] && $y <= $z["y_max_zone"]
  306. ) {
  307. $tabZones[$x][$y] = true;
  308. }
  309. }
  310. }
  311. }
  312. }
  313. }
  314. }