<commit_before><?php

/**
 *  OPBE
 *  Copyright (C) 2013  Jstar
 *
 * This file is part of OPBE.
 * 
 * OPBE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * OPBE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with OPBE.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package OPBE
 * @author Jstar <<EMAIL>>
 * @copyright 2013 Jstar <<EMAIL>>
 * @license http://www.gnu.org/licenses/ GNU AGPLv3 License
 * @version beta(25-02-2015)
 * @link https://github.com/jstar88/opbe
 */

class PhysicShot2
{
    private $shipType;
    private $damage;
    private $count;

    private $assorbedDamage = 0;
    private $bouncedDamage = 0;
    private $hullDamage = 0;
    private $cellDestroyed = 0;
    
    
    /**
     * PhysicShot::__construct()
     * 
     * @param ShipType $shipType
     * @param int $damage
     * @param int $count
     * @return
     */
    public function __construct(ShipType $shipType, $damage, $count)
    {
        echo "damage=$damage<br>count=$count<br>";
        if ($damage < 0)
            throw new Exception('negative damage');
        if ($count < 0)
            throw new Exception('negative amount of shots');
        $this->fighters = $shipType->cloneMe();
        $this->damage = $damage;
        $this->count = $count;
    }
    
    
    /**
     * PhysicShot::getAssorbedDamage()
     * 
     * @return float
     */
    public function getAssorbedDamage($cell = false)
    {
        if ($cell)
        {
            return $this->cellDestroyed;
        }
        return $this->assorbedDamage;

    }
    
    
    /**
     * PhysicShot::getBouncedDamage()
     * 
     * @return float
     */
    public function getBouncedDamage()
    {
        return $this->bouncedDamage;
    }
    
    
    /**
     * PhysicShot::getHullDamage()
     * 
     * @return float
     */
    public function getHullDamage()
    {
        return $this->hullDamage;
    }
    
    
    /**
     * PhysicShot::getPureDamage()
     * Return the total amount of damage from enemy
     * @return int
     */
    public function getPureDamage()
    {
        return $this->damage * $this->count;
    }
    
    
    /**
     * PhysicShot::getHitShips()
     * Return the number of hitten ships.
     * @return
     */
    public function getHitShips()
    {
        return min($this->count, $this->fighters->getCount());
    }
    
    
    /**
     * PhysicShot::start()
     * Start the system
     * @return
     */
    public function start()
    {
        $n = $this->count;
        $d = $this->damage;
        $currentCellsCount = $this->fighters->getCurrentShield();
        if (USE_HITSHIP_LIMITATION)
        {
            $currentCellsCount = round($currentCellsCount * $this->getHitShips() / ( $this->fighters->getCount()));
        }
        $s_100 = $this->fighters->getShieldCellValue();
        $s = $currentCellsCount * $s_100;    
        $x = $this->clamp($d, $s_100);
        
        $this->bouncedDamage = ($d - $x)* $n;
        $this->assorbedDamage = min($x * $n, $s);
        
        $hullDamage = $this->getPureDamage() - $this->assorbedDamage - $this->bouncedDamage;
        $hullDamage = min($hullDamage, $this->fighters->getCurrentLife() * $this->getHitShips() / $this->fighters->getCount());
        $this->hullDamage = max(0, $hullDamage);
        
        // da sistemare, la suddivisione in celle dello scudo  inutile. 
        $this->cellDestroyed = min(round($this->assorbedDamage / $s_100),$currentCellsCount);
    }
    
    private function clamp($a,$b)
    {
        if ($a > $b)
        {
            return $a;
        }
        return 0;
    }
    
}<commit_msg>fixed division by zero in case of zero shield ships<commit_after><?php

/**
 *  OPBE
 *  Copyright (C) 2013  Jstar
 *
 * This file is part of OPBE.
 * 
 * OPBE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * OPBE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with OPBE.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package OPBE
 * @author Jstar <<EMAIL>>
 * @copyright 2013 Jstar <<EMAIL>>
 * @license http://www.gnu.org/licenses/ GNU AGPLv3 License
 * @version beta(25-02-2015)
 * @link https://github.com/jstar88/opbe
 */

class PhysicShot2
{
    private $shipType;
    private $damage;
    private $count;

    private $assorbedDamage = 0;
    private $bouncedDamage = 0;
    private $hullDamage = 0;
    private $cellDestroyed = 0;
    
    
    /**
     * PhysicShot::__construct()
     * 
     * @param ShipType $shipType
     * @param int $damage
     * @param int $count
     * @return
     */
    public function __construct(ShipType $shipType, $damage, $count)
    {
        echo "damage=$damage<br>count=$count<br>";
        if ($damage < 0)
            throw new Exception('negative damage');
        if ($count < 0)
            throw new Exception('negative amount of shots');
        $this->fighters = $shipType->cloneMe();
        $this->damage = $damage;
        $this->count = $count;
    }
    
    
    /**
     * PhysicShot::getAssorbedDamage()
     * 
     * @return float
     */
    public function getAssorbedDamage($cell = false)
    {
        if ($cell)
        {
            return $this->cellDestroyed;
        }
        return $this->assorbedDamage;

    }
    
    
    /**
     * PhysicShot::getBouncedDamage()
     * 
     * @return float
     */
    public function getBouncedDamage()
    {
        return $this->bouncedDamage;
    }
    
    
    /**
     * PhysicShot::getHullDamage()
     * 
     * @return float
     */
    public function getHullDamage()
    {
        return $this->hullDamage;
    }
    
    
    /**
     * PhysicShot::getPureDamage()
     * Return the total amount of damage from enemy
     * @return int
     */
    public function getPureDamage()
    {
        return $this->damage * $this->count;
    }
    
    
    /**
     * PhysicShot::getHitShips()
     * Return the number of hitten ships.
     * @return
     */
    public function getHitShips()
    {
        return min($this->count, $this->fighters->getCount());
    }
    
    
    /**
     * PhysicShot::start()
     * Start the system
     * @return
     */
    public function start()
    {
        $n = $this->count;
        $d = $this->damage;
        $currentCellsCount = $this->fighters->getCurrentShield();
        if (USE_HITSHIP_LIMITATION)
        {
            $currentCellsCount = round($currentCellsCount * $this->getHitShips() / ( $this->fighters->getCount()));
        }
        $s_100 = $this->fighters->getShieldCellValue();
        $s = $currentCellsCount * $s_100;    
        $x = $this->clamp($d, $s_100);
        
        $this->bouncedDamage = ($d - $x)* $n;
        $this->assorbedDamage = min($x * $n, $s);
        
        $hullDamage = $this->getPureDamage() - $this->assorbedDamage - $this->bouncedDamage;
        $hullDamage = min($hullDamage, $this->fighters->getCurrentLife() * $this->getHitShips() / $this->fighters->getCount());
        $this->hullDamage = max(0, $hullDamage);
        
        // da sistemare, la suddivisione in celle dello scudo  inutile. 
        $this->cellDestroyed = ($s_100 == 0)?$currentCellsCount : min(round($this->assorbedDamage / $s_100),$currentCellsCount);
    }
    
    private function clamp($a,$b)
    {
        if ($a > $b)
        {
            return $a;
        }
        return 0;
    }
    
}
{'fraction_non_alphanumeric': 0.12900108708781255, 'fraction_numerical': 0.010387727986471796, 'mean_word_length': 2.112781954887218, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 91, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4910667', 'n_tokens_mistral': 2746, 'n_tokens_neox': 2650, 'n_words': 651}