/** * @param int $clanid * @return string */ public function TakeAction($clanid) { $clan = new Clan($clanid); $clan->setDb($this->db); $clan->load(); $depot = new Depot($clan->getDepot()); $depot->setDb($this->db); $depot->load(); $action = []; // Consume food or starve $this->ConsumeFood($clan); // Clans periodically process their food-type tokens for personal consumption if (rand(1, 10) == 10) { $yield = $clan->getFood() + $depot->fillLarder(); $clan->setFood($yield); $clan->update(); } // Clans periodically reconsider and revise their current activity // according exigencies, leader ptype, etc. if (rand(1, 20) == 20) { $activity = $this->Consider($clan); return 'Clan' . $clan->getId() . ' reconsidered and ' . $activity; } // ... finally, we have the behavior state-machine itself where the current // clan->activity is processed once per ActionRound switch ($clan->getActivity()) { case 'wandering': $action['Action'] = 'travel'; $action['Issuer'] = $clan; $mz = $this->map->GetARandomMove($clan); $action['Args'] = $mz->getX() . ',' . $mz->getY(); $result = $this->rules->submit($action); if ($result['Type'] == 'Success') { if (rand(1, 3) == 3) { $result['Description'] .= ' and began exploring'; $clan->setActivity('exploring'); $clan->update(); } } return $result['Description']; case 'exploring': // Check my local zone and see if there are any trade // tokens to produce $mz = $this->map->getMapzoneFromAbstract($clan->getX(), $clan->getY()); $producing = $this->trade->exploreForTrade($mz); if (is_null($producing)) { $clan->setActivity('wandering'); } else { $clan->setProducing($producing); $clan->setActivity('working'); } $clan->update(); return 'Clan' . $clan->getId() . ' explored ' . $clan->getX() . ', ' . $clan->getY(); case 'working': // If I am producing anything, produce one and deposit it // into my depot. If I am full up, change activity to // 'trading' $tgp = new TradegoodPlatonic($clan->getProducing()); $tgp->setDb($this->db); $tgp->load(); if (rand(1, 15) < 15) { $depot->Produce(strtolower($tgp->getNamed())); $result = 'Clan' . $clan->getId() . ' produced ' . $tgp->getNamed() . ' in ' . $clan->getX() . ', ' . $clan->getY(); } else { $result = 'Clan' . $clan->getId() . ' labored without producing sufficient ' . $tgp->getNamed(); } if ($depot->check(strtolower($tgp->getNamed())) >= 10) { $clan->setProducing(null); $clan->setActivity('trading'); $clan->update(); $result = 'Clan' . $clan->getId() . ' completed work and is seeking to trade'; } return $result; case 'trading': // If I'm in a city now, do buy and sell behavior. If I'm in teleport // range of a city, teleport there. If I'm truly in the wild, revert // behavior to wandering. $city = $this->map->findNearestCity($clan->getX(), $clan->getY()); if (isset($city)) { if ($city->getX() == $clan->getX() && $city->getY() == $clan->getY()) { $result = ''; $this->clans->setClanCurrentCity($clan, $city); $result .= $this->clanSellBehavior($clan, $depot, $city); if ($clan->getCoin() > 65) { $result .= $this->clanBuyBehavior($clan, $city); } return $result; } else { $this->map->teleportCity($clan, $city); return 'Clan' . $clan->getId() . ' traveled to ' . $city->getNamed(); } } else { $clan->setActivity('wandering'); $clan->update(); return 'Clan ' . $clan->getId() . ' wanted to trade but was not near a market.'; } case 'holiday': // If we can afford to, let's party if ($clan->getFood() >= 150) { $clan->setFood($clan->getFood() - 100); if ($clan->getPopulation() >= 200 && rand(0, $clan->getPopulation()) > 265) { $clan->setPopulation($clan->getPopulation() - 100); $this->tribes->createClan($clan->getTribeId()); $this->news->createSomeNews('A family of clan' . $clan->Id() . ' went to seek new pastures', $clan->getY(), $clan->getY()); } else { $clan->setPopulation($clan->getPopulation() + (25 + rand(1, 7))); } $clan->update(); return 'Clan' . $clan->getId() . ' celebrated a holy day'; } else { return 'Clan' . $clan->getId() . ' reconsidered and ' . $this->Consider($clan); } default: return 'Clan' . $clan->getId() . ' did inscrutable things on a hill'; } }
/** * Get a representation of one of the platonic tradegood categories. Must be spelled correctly. * Use this if you need the text description, images or current tradevalue/foodvalue properties. * * @param $fieldname * @return TradegoodPlatonic */ public function GetPlatonic($fieldname) { $query = "SELECT id FROM game.tradegoodplatonic WHERE named='" . strtolower($fieldname) . "';"; $this->db->setQuery($query); $this->db->query(); $loadObj = $this->db->loadObject(); $tgp = new TradegoodPlatonic($loadObj->id); $tgp->setDb($this->db); $tgp->load(); return $tgp; }
/** * @param IDepotHaver $issuer * @param City $city * @param $good * @param $amt * @return array */ public function buyGoods(IDepotHaver $issuer, $city, $good, $amt) { $depot = new Depot($issuer->getDepot()); $depot->setDb($this->db); $depot->load(); $tgp = new TradegoodPlatonic($good); $tgp->setDb($this->db); $tgp->load(); $city = new City($city); $city->setDb($this->db); $city->load(); if (empty($depot) | empty($tgp)) { return $this->getResult('Event failure', 'Null depot or tradegood'); } // Calculate costs, taxes, etc. $purse = $issuer->getCoin(); $cost = $amt * $tgp->tradevalue; // If we have sufficient coin if ($purse >= $cost) { $this->SetCoins($issuer, $purse - $cost); $city->taxTrade($cost); $currentStores = $depot->GetValueByString($tgp->named); $depot->setValueByString($tgp->named, $currentStores + $amt); return $this->getResult('Success', 'bought ' . $amt . ' ' . $tgp->named . ' for ' . $cost . ' coin'); } else { return $this->getResult('Illegal move', 'attempted to purchase ' . $amt . ' ' . $tgp->named . ' but had insufficient coin'); } }
/** * @param Mapzone $mz * @param TradegoodPlatonic $tg */ public function insertANewTradegoodToken(Mapzone $mz, TradegoodPlatonic $tg) { $query = 'INSERT INTO tradegoodtoken(mapzone, tg, named, tradevalue, foodvalue) VALUES(' . $mz->getId() . ', ' . $tg->getId() . ', "' . $tg->getNamed() . '", ' . $tg->getTradevalue() . ', ' . $tg->getFoodvalue() . ');'; $this->db->setQuery($query); $this->db->query(); }