-
Notifications
You must be signed in to change notification settings - Fork 29
/
Fire.php
283 lines (267 loc) · 9.89 KB
/
Fire.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?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 <frascafresca@gmail.com>
* @copyright 2015 Jstar <frascafresca@gmail.com>
* @license http://www.gnu.org/licenses/ GNU AGPLv3 License
* @version 21-03-2015
* @link https://github.com/jstar88/opbe
*
*
* Fire
*
* This class rappresent the fire shotted by attackers to defenders or viceversa.
* Using probabilistic theory, this class will help you in RF(Rapid Fire) calculation with O(1) time and memory functions.
* Sometime i think that SpeedSim's RF calculation is bugged, so you can choose if return its result or not setting "SPEEDSIM" constant to true/false.
*/
class Fire
{
private $attackerShipType;
private $defenderFleet;
/* old way
const SPEEDSIM = true;
const RAPIDFIRE = true;
private $shots = null;
private $power = null;
*/
private $shots = 0;
private $power = 0;
/**
* 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();
}
public function getPower()
{
return $this->attackerShipType->getPower();
}
public function getId()
{
return $this->attackerShipType->getId();
}
//----------- SENDED FIRE -------------
/**
* Fire::getAttackerTotalFire()
* Return the total fire
* @return int
*/
public function getAttackerTotalFire()
{
return $this->power;
}
/**
* Fire::getAttackerTotalShots()
* Return the total shots
* @return int
*/
public function getAttackerTotalShots()
{
return $this->shots;
}
/**
* Fire::calculateTotal()
* Calculate the total power and shots amount of attacker, including RF and standart fire
* @return void
*/
private function calculateTotal()
{
$this->shots += $this->attackerShipType->getCount();
$this->power += $this->getNormalPower();
if (USE_RF)
{
$this->calculateRf();
}
log_var('$this->shots',$this->shots);
/* old way
$this->shots = 0;
$this->power = 0;
if (self::RAPIDFIRE)
{
$this->calculateRf();
}
if (!self::SPEEDSIM || !self::RAPIDFIRE)
{
$this->shots += $this->attackerShipType->getCount();
$this->power += $this->getNormalPower();
}
*/
}
/**
* Fire::calculateRf()
* This function implement the RF component of above function
* @return void
*/
private function calculateRf()
{
//rapid fire
$tmpshots = round($this->getShotsFromOneAttackerShipOfType($this->attackerShipType) * $this->attackerShipType->getCount());
log_var('$tmpshots',$tmpshots);
$this->power += $tmpshots * $this->attackerShipType->getPower();
$this->shots += $tmpshots;
/* old way
$tmpshots = round($this->getShotsFromOneAttackerShipOfType($this->attackerShipType) * $this->attackerShipType->getCount());
if (self::SPEEDSIM && $tmpshots == 0)
{
$tmpshots = $this->attackerShipType->getCount();
}
$this->power += $tmpshots * $this->attackerShipType->getPower();
$this->shots += $tmpshots;
*/
}
/**
* Fire::getShotsFromOneAttackerShipOfType()
* This function return the number of shots caused by RF from one ShipType to all defenders
* @param ShipType $shipType_A
* @return int
*/
private function getShotsFromOneAttackerShipOfType(ShipType $shipType_A)
{
$p = $this->getProbabilityToShotAgainForAttackerShipOfType($shipType_A);
$meanShots = GeometricDistribution::getMeanFromProbability(1 - $p) - 1;
if (USE_RANDOMIC_RF)
{
$max = $meanShots * (1 + MAX_RF_BUFF);
$min = $meanShots * (1 - MAX_RF_NERF);
log_var('$max', $max);
log_var('$min', $min);
log_var('$mean', $meanShots);
return Gauss::getNextMsBetween($meanShots, GeometricDistribution::getStandardDeviationFromProbability(1 - $p), $min, $max);
}
return $meanShots;
}
/**
* Fire::getProbabilityToShotAgainForAttackerShipOfType()
* This function return the probability of a ShipType to shot thanks RF
* @param ShipType $shipType_A
* @return int
*/
private function getProbabilityToShotAgainForAttackerShipOfType(ShipType $shipType_A)
{
$p = 0;
foreach ($this->defenderFleet->getIterator() as $idFleet => $shipType_D)
{
$RF = $shipType_A->getRfTo($shipType_D);
$probabilityToShotAgain = 1 - GeometricDistribution::getProbabilityFromMean($RF);
$probabilityToHitThisType = $shipType_D->getCount() / $this->defenderFleet->getTotalCount();
$p += $probabilityToShotAgain * $probabilityToHitThisType;
}
return $p;
/* old way
$p = 0;
foreach ($this->defenderFleet->getIterator() as $idFleet => $shipType_D)
{
$RF = $shipType_A->getRfTo($shipType_D);
if (!self::SPEEDSIM)
{
$RF = max(0, $RF - 1);
}
$probabilityToShotAgain = ($RF != 0) ? 1 - GeometricDistribution::getProbabilityFromMean($RF) : 0;
$probabilityToHitThisType = $shipType_D->getCount() / $this->defenderFleet->getTotalCount();
$p += $probabilityToShotAgain * $probabilityToHitThisType;
}
return $p;
*/
}
/**
* Fire::getNormalPower()
* Return the total fire shotted from attacker ShipType to all defenders without RF
* @return int
*/
private function getNormalPower()
{
return $this->attackerShipType->getCount() * $this->attackerShipType->getPower();
}
//------- INCOMING FIRE------------
public function getShotsFiredByAttackerTypeToDefenderType(ShipType $shipType_A, ShipType $shipType_D, $real = false)
{
$first = $this->getShotsFiredByAttackerToOne($shipType_A);
$second = new Number($shipType_D->getCount());
return Math::multiple($first, $second, $real);
}
public function getShotsFiredByAttackerToOne(ShipType $shipType_A, $real = false)
{
$num = $this->getShotsFiredByAttackerToAll($shipType_A);
$denum = new Number($this->defenderFleet->getTotalCount());
return Math::divide($num, $denum, $real);
}
public function getShotsFiredByAllToDefenderType(ShipType $shipType_D, $real = false)
{
$first = $this->getShotsFiredByAllToOne();
$second = new Number($shipType_D->getCount());
return Math::multiple($first, $second, $real);
}
public function getShotsFiredByAttackerToAll(ShipType $shipType_A, $real = false)
{
$num = new Number($this->getAttackerTotalShots() * $shipType_A->getCount());
$denum = new Number($this->attackerShipType->getTotalCount());
return Math::divide($num, $denum, $real);
}
public function getShotsFiredByAllToOne($real = false)
{
$num = new Number($this->getAttackerTotalShots());
$denum = new Number($this->defenderFleet->getTotalCount());
return Math::divide($num, $denum, $real);
}
/**
* Fire::__toString()
* Rappresentation of this object
* @return
*/
public function __toString()
{
#global $resource;
# $shots = $this->getAttackerTotalShots();
# $power = $this->getAttackerTotalFire();
# $iter = $this->attackerShipType->getIterator();
# $page = "<center><table bgcolor='#ADC9F4' border='1' ><body><tr><tr><td colspan='" . count($iter) . "'><center><font color='red'>Attackers</font></center></td></tr>";
# foreach ($iter as $attacher)
# $page .= "<td>" . $resource[$attacher->getId()] . "</td>";
# $page .= "</tr><tr>";
# foreach ($iter as $attacher)
# $page .= "<td><center>" . $attacher->getCount() . "</center></td>";
# $iter = $this->defenderFleet->getIterator();
# $page .= "</tr></body></table><br><table bgcolor='#ADC9F4' border='1'><body><tr><td colspan='" . count($iter) . "'><center><font color='red'>Defenders</font></center></td></tr></tr>";
# foreach ($iter as $defender)
# $page .= "<td>" . $resource[$defender->getId()] . "</td>";
# $page .= "<tr>";
# foreach ($iter as $defender)
# $page .= "<td><center>" . $defender->getCount() . "</center></td>";
# $page .= "</tr></body></table><br>";
# $page .= "The attacking fleet fires a total of $shots times with the power of $power upon the defenders.<br>";
# $page .= "</center>";
# return $page;
return $this->getAttackerTotalFire() . '';
}
public function cloneMe()
{
return new Fire($this->attackerShipType, $this->defenderFleet);
}
}