/** * Renvoie les Rivieres liés à la collection * @return RiviereCollection */ public function getRivieres() { if (is_null($this->cacheRivieres)) { $this->cacheRivieres = RiviereBusiness::getFromParties($this); $this->cacheRivieres->store(); } return $this->cacheRivieres; }
/** * Renvoie les hexagones dans l'ordre d'un balayage * @return HexaCollection */ public function buildHexas() { $this->hexas = new HexaCollection(); $debutLigne = $this->coinSupGauche(); $rivieres = array(); for ($y = 1; $y <= $this->hauteur; $y++) { $current = $debutLigne; for ($x = 0; $x < $this->largeur; $x++) { $this->hexas->ajout($current); // Rivères foreach ($current->getRivieres() as $riviere) { /** @var Riviere $riviere */ if (!isset($rivieres[$riviere->getId()])) { $this->rivieres->ajout($riviere); $rivieres[$riviere->getId()] = true; } } // Visibles $this->ajoutVisiblesHexa($current); $current = $current->getVoisin(0); } if (fmod($debutLigne->getY(), 2) == 1) { // Y impair $debutLigne = $debutLigne->getVoisin(5); } else { // Y pair $debutLigne = $debutLigne->getVoisin(4); } } }
/** * Renvoie les ponts sur les rivières de la collection * @param RiviereCollection $rivieres * @return InfrastructureCollection */ public static function getPontsDeRivieres(RiviereCollection $rivieres) { if ($rivieres->count() == 0) { return new InfrastructureCollection(); } $req = "SELECT * FROM infrastructure WHERE "; $prem = true; $req .= ' ( '; foreach ($rivieres as $riviere) { /** @var Riviere $riviere */ if (!$prem) { $req .= ' OR '; } $req .= " ((idHexa = " . $riviere->getIdHexa1() . " AND idHexa2 = " . $riviere->getIdHexa2() . ") OR (idHexa = " . $riviere->getIdHexa2() . " AND idHexa2 = " . $riviere->getIdHexa1() . ")) "; $prem = false; } $req .= ") AND idType=" . Infrastructures::PONT . ";"; return DbHandler::collFromQuery($req, 'Infrastructure', 'InfrastructureCollection'); }
/** * 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(); }
/** * Renvoie les Joueurs liés aux Rivieres de la collection fournie * @var RiviereCollection $rivieres * @return JoueurCollection */ public static function getFromRivieres(RiviereCollection $rivieres) { $req = "SELECT * FROM joueur WHERE idJoueur IN (SELECT hexa.idTerritoire FROM hexa WHERE hexa.idHexa2 IN (" . $rivieres->getIdHexa2s() . "))"; return DbHandler::collFromQuery($req, 'Joueur', 'JoueurCollection'); }
/** * Renvoie les Hexas liés à une collection de Rivieres * @param RiviereCollection $rivieres * @return HexaCollection */ public static function getFromRivieres(RiviereCollection $rivieres) { $ids = $rivieres->getIdHexas(); if ($ids) { return new HexaCollection(); } $req = "SELECT * FROM hexa WHERE idHexa IN (" . $ids . ");"; return DbHandler::collFromQuery($req, 'Hexa', 'HexaCollection'); }
/** * Renvoie les Parties liés aux Rivieres de la collection fournie * @var RiviereCollection $rivieres * @return PartieCollection */ public static function getFromRivieres(RiviereCollection $rivieres) { $req = "SELECT * FROM partie WHERE idPartie IN (SELECT hexa.idPartie FROM hexa WHERE hexa.idHexa2 IN (" . $rivieres->getIdHexa2s() . "))"; return DbHandler::collFromQuery($req, 'Partie', 'PartieCollection'); }
/** * Ajoute une rivière au cache sans passer par une requête * @param Riviere $riviere */ public function addRiviereToCache(Riviere $riviere) { if (is_null($this->cacheRivieres)) { $this->cacheRivieres = new RiviereCollection(); } $this->cacheRivieres->ajout($riviere); }