/** * Crée les rivieres de la carte */ public function creerRivieres() { mt_srand($this->getRandRivieres()); // Création d'une grille de frontières avec voisinages de proche en proche $frontieres = $this->getHexas()->getFrontieres(); $frontieres->uasort('fr\\gilman\\nj\\common\\bb\\collection\\FrontiereCollection::triParHauteur'); // Création des sources de rivières $sources = new RiviereCollection(); $rivieres = new RiviereCollection(); $index = 0; foreach ($frontieres as $frontiere) { /** @var Frontiere $frontiere */ if ($frontiere->bordDeMer()) { break; } $proba = 1 / 40 * ($frontiere->getHauteur() / 15); if (mt_rand() / mt_getrandmax() <= $proba) { $riviere = new Riviere(); $riviere->setSource($frontiere->getIndex()); $riviere->setFrontiere($frontiere); $sources->ajout($riviere); $rivieres->ajout($riviere); $index++; } } // Faire couler les rivieres foreach ($sources as $index => $source) { /** @var Riviere $source */ /** @var Frontiere $prochain */ $frontiereCourante = $source->getFrontiere(); $coule = true; $precedent = null; while ($coule) { $prochain = $frontiereCourante->prochainCoulage(); if (!is_null($prochain)) { // La rivière se jette dans un fleuve if (!is_null($prochain->getRiviere()) && $prochain->getRiviere()->getSource() != $frontiereCourante->getRiviere()->getSource()) { break; } $riviere = new Riviere(); $riviere->setSource($source->getFrontiere()->getIndex()); $riviere->setFrontiere($prochain); if (rand(1, 10) == 1) { $riviere->setGue(1); } $rivieres->ajout($riviere); $frontiereCourante = $prochain; // La rivière se jette dans la mer if ($prochain->bordDeMer()) { $coule = false; } } else { $coule = false; } } } $rivieres->save(); }
/** * Donne le numéro de côté de la case sur lequel se trouve la rivière * @param Riviere $riviere * @return int */ public function coteRiviere(Riviere $riviere) { $voisin = null; if ($riviere->getIdHexa1() == $this->getId()) { $voisin = $riviere->getHexa2(); } if ($riviere->getIdHexa2() == $this->getId()) { $voisin = $riviere->getHexa1(); } return $this->quelVoisin($voisin); }