Ejemplo n.º 1
0
 private function getChooseSpecialUnits($target, $squads, $error = null)
 {
     $page = new Neuron_Core_Template();
     $page->setTextSection('specialUnits', 'battle');
     $page->set('error', $error);
     $page->set('target', Neuron_Core_Tools::output_varchar($target->getName()));
     $page->set('targetId', $target->getId());
     $distance = Dolumar_Map_Map::getDistanceBetweenVillages($this->village, $target, false);
     $page->set('distance', Neuron_Core_Tools::output_distance($distance));
     foreach ($this->village->getAttackSlots($target) as $k => $v) {
         if (isset($squads[$k])) {
             $unitId = $squads[$k]->getSquad()->getId() . '_' . $squads[$k]->getUnitId();
             $page->addListValue('slots', array('id' => $k, 'unit' => $unitId));
         }
     }
     $duration = $this->village->battle->getMoveDuration($squads, $distance);
     if ($duration > 60 * 60 * 24) {
         $page->set('duration', $duration);
     }
     $honour = Dolumar_Battle_Battle::getHonourPenalty($this->village, $target);
     if ($honour > 0) {
         //$bigger = round ( ($this->village->getScore () / $target->getScore ()) * 100) - 100;
         $bigger = round(Dolumar_Battle_Battle::getSizeDifference($this->village, $target) * 100 - 100);
         $page->set('honour', $honour);
         $page->set('size', $bigger);
     }
     // Fetch thze special units
     $units = $this->village->getSpecialUnits();
     foreach ($units as $v) {
         $actions = $v->getEffects();
         // Prepare the actions
         $aActions = array();
         foreach ($actions as $action) {
             if ($action instanceof Dolumar_Effects_Battle) {
                 $aActions[] = array('name' => $action->getName(), 'id' => $action->getId(), 'cost' => Neuron_Core_Tools::resourceToText($action->getCost($v, $target), false, false, false, 'rune', false));
             }
         }
         if (count($aActions) > 0) {
             asort($aActions);
             // Add the special unit to the list
             $page->addListValue('specialunits', array('id' => $v->getId(), 'name' => Neuron_Core_Tools::output_varchar($v->getName(false, true)), 'actions' => $aActions));
         }
     }
     return $page->parse('battle/specialUnits.phpt');
 }
Ejemplo n.º 2
0
 public function attackVillage($oTarget, $oUnits, $specialUnits = array())
 {
     if (count($oUnits) > 0) {
         $db = Neuron_Core_Database::__getInstance();
         // First: check if special units are valid
         $aSpecialUnits = array();
         foreach ($specialUnits as $v) {
             $action = $v[0]->getEffect($v[1]);
             if ($action && $action instanceof Dolumar_Effects_Battle) {
                 $aSpecialUnits[] = array($v[0], $action);
             }
         }
         // Second: calculate the sum of all resource cost
         $aResources = array();
         foreach ($aSpecialUnits as $v) {
             foreach ($v[1]->getCost($v, $oTarget) as $res => $am) {
                 if (isset($aResources[$res])) {
                     $aResources[$res] += $am;
                 } else {
                     $aResources[$res] = $am;
                 }
             }
         }
         // Thirdly: withdraw the required amount
         if (!$this->objProfile->resources->takeResourcesAndRunes($aResources)) {
             $this->error = 'no_resources';
         } else {
             $afstand = Dolumar_Map_Map::getDistanceBetweenVillages($this->objProfile, $oTarget);
             $naarVillage = $this->getMoveDuration($oUnits, $afstand, true);
             $naarHuis = $this->getMoveDuration($oUnits, $afstand, false);
             $fightDate = NOW + $naarVillage;
             // Honour!!!
             $honour = Dolumar_Battle_Battle::getHonourPenalty($this->objProfile, $oTarget);
             $slotsamount = count($this->getAttackSlots($oTarget));
             // Insert battle
             $battleId = $db->insert('battle', array('vid' => $this->objProfile->getId(), 'targetId' => $oTarget->getId(), 'startDate' => NOW, 'arriveDate' => NOW + $naarVillage, 'fightDate' => $fightDate, 'endDate' => $fightDate + $naarHuis + 1, 'goHomeDuration' => $naarHuis, 'iHonourLose' => $honour, 'iBattleSlots' => $slotsamount));
             // Add troops
             foreach ($oUnits as $slot => $unit) {
                 $db->insert('battle_squads', array('bs_bid' => $battleId, 'bs_squadId' => $unit->getSquad()->getId(), 'bs_unitId' => $unit->getUnitId(), 'bs_vid' => $unit->getVillage()->getId(), 'bs_slot' => $slot));
             }
             // Add special troops
             foreach ($aSpecialUnits as $v) {
                 $db->insert('battle_specialunits', array('bsu_bid' => $battleId, 'bsu_vsu_id' => $v[0]->getId(), 'bsu_ba_id' => $v[1]->getId(), 'bsu_vid' => $this->objProfile->getId()));
             }
             // Notify players
             $pl_attacker = $this->objProfile->getOwner();
             $pl_defender = $oTarget->getOwner();
             $notExtra = array('attacker' => $this->objProfile, 'defender' => $oTarget, 'pl_attacker' => $this->objProfile->getOwner(), 'pl_defender' => $oTarget->getOwner());
             $pl_attacker->sendNotification('attacking', 'battle', array('defender' => $oTarget, 'pl_defender' => $oTarget->getOwner(), 'village' => $this->objProfile, 'player' => $this->objProfile->getOwner()), $pl_attacker, true);
             $pl_defender->sendNotification('defending', 'battle', array('attacker' => $this->objProfile, 'pl_attacker' => $this->objProfile->getOwner(), 'village' => $oTarget, 'player' => $oTarget), $pl_attacker, false);
             // Done
             return true;
         }
     } else {
         $this->error = 'no_troops';
         return false;
     }
 }