/** * @param $locId * @return LocationHelper * @throws \Exception */ public function getLocation($locId) { if ($this->locations->offsetExists($locId)) { return $this->locations->offsetGet($locId); } else { return $this->locHelper->factory($locId); } }
/** * * @return mixed */ public function ai() { $npc = $this->getNpcHelper()->getNpc(); $this->_addListeners($npc); $state = isset($npc["state"]) ? $npc["state"] : self::STATE_STAND; //обработка в зависимости от текущего состояния switch ($state) { //обычное состояние case self::STATE_AGRESSY: $target = $this->getNpcHelper()->getTarget(); //если агрессивный и нет таргета, то находим самую слабую цель if (false === $target) { $this->getLocHelper()->eachObjects("loc", function ($objId, $obj) { if ($objId != $this->getNpcHelper()->getNpcId()) { $players = []; if (substr($objId, 0, 7) == "player_") { //считаем сумму параметров $calcService = new CalculateHelper($this->getNpcHelper()->getNpc()); $statsSumm = $calcService->getStatSumm(); $playerHelper = Game::init()->getService('player.helper')->factory($objId); $playerCalculateService = new CalculateHelper($playerHelper->getPlayer()); $playerStatsSumm = $playerCalculateService->getStatSumm(); $pr = $statsSumm / $playerStatsSumm * 100; //$players[$objId] = ; if ($pr > 5) { //если статы не меньше 1/2 то нападаем $this->getNpcHelper()->setTarget($objId); $battleEvent = new BattleEvent(); $battleEvent->setLocHelper($this->getLocHelper()); $battleEvent->setAttackType(BattleEvent::AUTO_ATTAK); $battleEvent->setToObjectId($objId); $battleEvent->setSelfObjectId($this->getNpcHelper()->getNpcId()); Game::init()->getDispatcher()->dispatch("battle.attack", $battleEvent); } } } }); } if ($this->getLocHelper()->objectExists($target)) { //атакуем } else { $this->supervision->eachLocations(function ($locHelper) use($target) { if ($locHelper->objectExists($target)) { //нашли объект, идем $this->locHelper->moveLocObject($this->getNpcHelper()->getNpcId(), $this->getNpcHelper()->getNpcId()); } }); } break; //обычное состояние //обычное состояние default: break; } }
public function loadLoc() { $this->locHelper->setCollection($this->db->locations); $loc = $this->locHelper->getCollection()->findOne(["lid" => $this->locId]); if ($loc) { $this->locHelper = new LocationHelper($loc); $this->locHelper->setCollection($this->getDb()->locations); } else { throw new \Exception("Err load loc"); } }
/** * @param $locId * @return LocationHelper * @throws \Exception */ public function factory($locId) { $loc = $this->getCollection()->findOne(["lid" => $locId]); if ($loc) { $helper = new LocationHelper($loc); $helper->setCollection($this->getCollection()); return $helper; } else { throw new \Exception("Loc " . $locId . " not found"); } }
/** * @param $obj * @param LocationHelper $locHelper */ private function _moveAi($obj, $locHelper) { $move = $obj["ai"]["move"]; if (!isset($move["status"])) { $move["status"][0] = "stand"; //если нет статуса, то стоит } $nextMove = isset($move["next_move"]) ? $move["next_move"] : time(); $moveList = isset($move["list"]) ? $move["list"] : []; //какие локации посетил $countLocs = isset($move["num"]) ? rand($move["num"][0], $move["num"][1]) : 1; //сколько локаций ходит if (time() >= $nextMove) { //подошло время идти if ($move["status"][0] == "stand") { $move["status"][0] = "in_move"; $move["status"][1] = $countLocs; } //если стояли - то идем } if ($move["status"][0] == "in_move" and count($move["list"]) < $move["status"][1] and $move["status"][1] > 0) { $blackTerr = isset($move["black_terr"]) ? $move["black_terr"] : []; $door = $locHelper->getRandomDoorId($moveList, $blackTerr); $move = $this->_startMove($move, $door); } else { $move = $this->_stopMove($move); } $obj["ai"]["move"] = $move; return $obj; }