/** * Fire::__construct() * * @param ShipType $attackerShipType * @param Fleet $defenderFleet * @param bool $attacking * @return */ public function __construct(ShipType $attackerShipType, Fleet $defenderFleet) { log_comment('calculating fire from attacker ' . $attackerShipType->getId()); $this->attackerShipType = $attackerShipType; $this->defenderFleet = $defenderFleet; $this->calculateTotal(); }
/** * ShipsCleaner::start() * Start the system * @return null */ public function start() { /*** calculating probability to explode ***/ //the mean probably to explode based on damage $prob = 1 - $this->fighters->getCurrentLife() / ($this->fighters->getHull() * $this->fighters->getCount()); if ($prob < 0 && $prob > -EPSILON) { $prob = 0; } if ($prob < 0) { throw new Exception("Negative prob"); } //if most of ships are hitten,then we can apply the more realistic way if (USE_BIEXPLOSION_SYSTEM && $this->lastShipHit >= $this->fighters->getCount() / PROB_TO_REAL_MAGIC) { log_comment('lastShipHit bigger than getCount()/magic'); if ($prob < MIN_PROB_TO_EXPLODE) { $probToExplode = 0; } else { $probToExplode = $prob; } } else { log_comment('lastShipHit smaller than getCount()/magic'); $probToExplode = $prob * (1 - MIN_PROB_TO_EXPLODE); } /*** calculating the amount of exploded ships ***/ $teoricExploded = round($this->fighters->getCount() * $probToExplode); if (USE_EXPLODED_LIMITATION) { $teoricExploded = min($teoricExploded, $this->lastShots); } $this->exploded = $teoricExploded; //bounded by the total shots fired to simulate a real combat :) /*** calculating the life of destroyed ships ***/ //$this->remainLife = $this->exploded * (1 - $prob) * ($this->fighters->getCurrentLife() / $this->fighters->getCount()); $this->remainLife = $this->fighters->getCurrentLife() / $this->fighters->getCount(); log_var('prob', $prob); log_var('probToExplode', $probToExplode); log_var('teoricExploded', $teoricExploded); log_var('exploded', $this->exploded); log_var('remainLife', $this->remainLife); }
public function cleanShips() { $shipsCleaners = array(); foreach ($this->array as $id => $shipType) { log_comment("---- exploding {$id} ----"); $sc = $shipType->cleanShips(); $this->count -= $sc->getExplodedShips(); if ($shipType->isEmpty()) { unset($this->array[$id]); } $shipsCleaners[$shipType->getId()] = $sc; } return $shipsCleaners; }
function log_var($name, $value) { if (is_array($value)) { $value = var_export($value); } log_comment("{$name} = {$value}"); }
/** * ShipType::inflictDamage() * Inflict damage to all ships of this type. * @param int $damage * @param int $shotsToThisShipType * @return void */ public function inflictDamage($damage, $shotsToThisShipType) { if ($shotsToThisShipType == 0) { return; } if ($shotsToThisShipType < 0) { throw new Exception("Negative amount of shotsToThisShipType!"); } log_var('Defender single hull', $this->singleLife); log_var('Defender count', $this->getCount()); log_var('currentShield before', $this->currentShield); log_var('currentLife before', $this->currentLife); $this->lastShots += $shotsToThisShipType; $ps = new PhysicShot($this, $damage, $shotsToThisShipType); $ps->start(); log_var('$ps->getAssorbedDamage()', $ps->getAssorbedDamage()); $this->currentShield -= $ps->getAssorbedDamage(); if ($this->currentShield < 0 && $this->currentShield > -EPSILON) { log_comment('fixing double number currentshield'); $this->currentShield = 0; } $this->currentLife -= $ps->getHullDamage(); if ($this->currentLife < 0 && $this->currentLife > -EPSILON) { log_comment('fixing double number currentlife'); $this->currentLife = 0; } log_var('currentShield after', $this->currentShield); log_var('currentLife after', $this->currentLife); $this->lastShipHit += $ps->getHitShips(); log_var('lastShipHit after', $this->lastShipHit); log_var('lastShots after', $this->lastShots); if ($this->currentLife < 0) { throw new Exception('Negative currentLife!'); } if ($this->currentShield < 0) { throw new Exception('Negative currentShield!'); } if ($this->lastShipHit < 0) { throw new Exception('Negative lastShipHit!'); } return $ps; //for web }