sc: calculate dps from damage and cooldown
[sheet.git] / sc.plp
1 <(common.inc.plp)><:
2
3 my $datafile = 'sc-units.inc.pl';
4 if ($ENV{PATH_INFO} and $ENV{PATH_INFO} eq '/2') {
5         $datafile = 'sc2-units.inc.pl';
6 }
7
8 Html({
9         title => 'starcraft unit cheat sheet',
10         version => 'v1.0',
11         description => [
12                 'Reference of StarCraft unit properties,'
13                 . ' comparing various statistics of all the units in Brood War'
14                 . ' including costs, damage, defense, speed, ranges, and abilities.',
15         ],
16         keywords => [qw'
17                 starcraft game unit statistics stats comparison table sheet cheat
18                 reference software attributes properties
19         '],
20         stylesheet => [qw'light'],
21         data => [$datafile],
22 });
23
24 :>
25 <h1>StarCraft units</h1>
26
27 <p>
28 Unit properties as seen or measured in Brood War
29 <span title="no known changes as of v1.16.1">version≥1.08</span>.
30 </p>
31
32 <style type="text/css">
33         .units {
34                 border-collapse: separate;
35                 border-spacing: 0;
36                 margin: 0 auto;
37                 white-space: nowrap;
38         }
39         .units th, .units td {
40                 border: 0;
41                 padding: 0 0.2em;
42                 background: transparent;
43                 text-align: left;
44         }
45         tr.alt td {
46                 font-size: 70%;
47                 border-top-style: dashed;
48         }
49         .units tr th:first-child {
50                 padding-left: 0;
51         }
52         .units tr:hover:not(.race) {
53                 background: #EEE;
54         }
55
56         table h2 {
57                 padding: 1ex 0;
58                 margin: 0;
59                 text-align: center;
60         }
61         .units thead th, .units tfoot th {
62                 font-size: 70%;
63                 font-weight: normal;
64         }
65
66         .unit-o {color: #C08} /* organic */
67         .unit-u {color: #44C} /* mechanic */
68         img.unit-o, img.unit-u {
69                 margin-left: 0.2em;
70                 vertical-align: middle;
71         }
72         .unit-composed {
73                 color: #C88;
74                 font-size: 70%;
75         }
76         .unit {
77                 text-align: center;
78                 white-space: nowrap;
79         }
80         .unit.unit-s {color: #770}
81         .unit.unit-m {color: #C70}
82         .unit.unit-l {color: #D22}
83         .hurt.unit-s::before {
84                 content: '~';
85                 color: #773;
86         }
87         .hurt.unit-l::before {
88                 content: '*';
89                 color: #C66;
90         }
91         .hurt .unit-splash {
92                 position: absolute;
93         }
94         .hurtrel, .units .hurtrel {
95                 padding-left: 1em;
96                 font-size: 70%;
97                 color: #778;
98         }
99         .unit-splash {
100                 color: #4A0;
101         }
102         .unit-detect::before {
103                 content: '!';
104                 color: #0A8;
105                 font-size: 70%;
106                 vertical-align: super;
107         }
108         .unit-magic {
109                 padding-left: 0.5em;
110         }
111         .magic-perma {
112                 text-decoration: underline;
113         }
114
115         .units .val {
116                 text-align: right;
117         }
118
119         .legend dt {
120                 float: none;
121                 text-align: left;
122                 margin-top: 1ex;
123                 font-weight: bold;
124         }
125         .legend dd:before {
126                 content: '• ';
127         }
128         .legend dd {
129                 text-indent: -1em;
130                 margin-left: 1em;
131                 break-before: avoid;
132                 -webkit-column-break-before: avoid;
133         }
134
135         .units, .legend {
136                 display: table-cell;
137         }
138         .legend {
139                 min-width: 20em;
140                 text-align: left;
141         }
142         .units {
143                 float: left;
144                 padding-bottom: 1em;
145                 margin-right: 2em;
146         }
147         .footer {
148                 clear: left;
149         }
150 </style>
151
152 <:
153 sub coltoggle {
154         my ($name, $id) = @_;
155         return sprintf(
156                 (defined $get{order} ? $get{order} eq $id : !$id) ? '%2$s ▼'
157                         : '<a href="?%s">%s</a>',
158                 $id && "order=$id", $name
159         );
160 }
161 :><table class="units">
162 <thead><tr>
163         <th></th>
164         <th><:= coltoggle('name', '') :></th>
165         <th class="val min"><img src="/minerals.png" alt="min"></th>
166         <th class="val gas"><img src="/gas.png" alt="gas"></th>
167         <th class="val time"><:= coltoggle(qw'build cost') :></th>
168         <th class="unit" colspan="2"><:= coltoggle(qw'size size') :></th>
169         <th class="val unit-hp">HP</th>
170         <th class="val unit-shield">shield</th>
171         <th class="val unit-armor">armor</th>
172         <th class="val hurt" colspan="2">ground</th>
173         <th class="val hurt" colspan="2">air</th>
174         <th class="val unit-range">range</th>
175         <th class="val unit-sight">sight</th>
176         <th class="val unit-speed">speed</th>
177         <th class="unit-magic">specials</th>
178 </tr></thead>
179 <:
180 sub showrange {
181         my ($row, @elements) = @_;
182         my ($min, $max);
183
184         my $value = $row;
185         $value = ref $value eq 'HASH' && $value->{$_} or last for @elements;
186         if (ref $value eq 'ARRAY') {
187                 $min = $value->[0];
188                 $max = $value->[-1];
189         }
190         else {
191                 $min = $max = $value;
192         }
193         defined $min or return '';
194
195         if ($row->{upgrade}) {
196                 for (@{ $row->{upgrade} }) {
197                         my $increase = $_ or next;
198                         $increase = ref $increase eq 'HASH' && $increase->{$_} or last for @elements;
199                         $increase = $increase->[-1] if ref $increase eq 'ARRAY';
200                         $max += $increase if $increase;
201                 }
202         }
203
204         if ($elements[0] eq 'attack' and $elements[1] ne 'range' and $elements[2] eq 'dps') {{
205                 my $attack = $row->{ $elements[0] }->{ $elements[1] };
206                 ref $attack or $attack = $row->{ $elements[0] }->{$attack}; # follow
207                 my $type = $attack->{type} or next;
208                 if ($type eq 'explosive') {
209                         $min /= 2;
210                 }
211                 elsif ($type eq 'implosive') {
212                         $min /= 4;
213                 }
214         }}
215         $_ = int($_ + .5) for $min, $max;  # round halves up
216
217         return $min == $max ? $min : "$min-$max";
218 }
219
220         sub showattack {
221                 my ($row, $area) = @_;
222                 my $attack = $row->{attack}->{$area};
223                 if (not ref $attack) {
224                         # reference to another area
225                         $area = $attack;
226                         $attack = $row->{attack}->{$area};
227                 }
228
229                 return '<td colspan="2" class="hurt">' unless $attack;
230
231                 my $tagbase = '<td class="val hurt';
232                 if (ref $attack and $attack->{type}) {
233                         if ($attack->{type} eq 'explosive') {
234                                 $tagbase .= ' unit-l';
235                         }
236                         elsif ($attack->{type} eq 'implosive') {
237                                 $tagbase .= ' unit-s';
238                         }
239                 }
240                 $tagbase .= '">';
241
242                 my $out = showrange($row, 'attack', $area, 'damage');
243                 $out .= '<span class="unit-splash">+</span>' if $attack->{splash};
244                 $attack->{dps} = $attack->{cooldown} && [
245                         map { 24 * $_ / $attack->{cooldown} * ($attack->{count} // 1) }
246                         map { ref $_ ? @{$_} : $_ }
247                         $attack->{damage}
248                 ];
249                 $out .= '<td class="val hurt hurtrel">' . showrange($row, 'attack', $area, 'dps');
250                 return $tagbase . $out;
251         }
252
253         sub showmagic {
254                 my ($row) = @_;
255                 my $specials = $row->{special} or return '';
256                 return join ' ', map {
257                         sprintf '<span%s title="%s">%s</span>',
258                                 $_->{duration} < 0 && ' class="magic-perma"',
259                                 join('',
260                                         $_->{name},
261                                         $_->{desc} ? ": $_->{desc}" : '',
262                                         $_->{range} || $_->{cost} ? sprintf(' (%s)', join ', ',
263                                                 $_->{range} ? "range $_->{range}" : (),
264                                                 $_->{cost} ? sprintf('cost %.0f%%',
265                                                         100 * $_->{cost} / $row->{energy}
266                                                 ) : (),
267                                         ) : '',
268                                 ),
269                                 sprintf($_->{build} ? '(%s)' : '%s', $_->{abbr}),
270                 } grep { defined $_->{abbr} } @{$specials};
271         }
272
273         sub showunitcols {
274                 my ($row) = @_;
275                 local $_ = $row;
276                 $_->{hp} += $_->{shield} if $_->{shield};
277                 my $suitchar = '';
278                 if ($_->{suit}) {
279                         $suitchar = [qw/? s m l/]->[$_->{suit}];
280                 }
281
282                 return (
283                         '<td class="val min">' . ($_->{min} // ''),
284                         '<td class="val gas">' . ($_->{gas} || ''),
285                         !defined $_->{build} ? '<td>' : sprintf('<td class="val time">%s%.0f',
286                                 !!$_->{base} && '<span class="unit-composed">+</span>',
287                                 $_->{build} || '0',
288                         ),
289                         !$suitchar ? '<td>' : sprintf('<td class="unit unit-%s">%s', $suitchar, ucfirst $suitchar),
290                         '<td class="val unit">' . join('',
291                                 defined $_->{unit} && $_->{unit} == .5 ? '½' : $_->{unit},
292                                 defined $_->{organic} && sprintf(
293                                         '<span class="unit-%s" title="%s">%s</span>',
294                                         $_->{organic} ? 'o' : 'u',
295                                         $_->{organic} ? 'organic' : 'mechanic',
296                                         $_->{organic} ? 'o' : 'm',
297                                 ),
298                         ),
299                         '<td class="val unit-hp">' . $_->{hp} // '',
300                         '<td class="val unit-shield">' . (
301                                 $_->{shield} ? sprintf('%.0f%%', 100 * $_->{shield} / $_->{hp}) : '&nbsp;'
302                         ),
303                         '<td class="val unit-armor">' . showrange($_, 'armor'),
304                         showattack($_, 'ground'),
305                         showattack($_, 'air'),
306                         '<td class="val unit-range">' . showrange($_, 'attack', 'range'),
307                         '<td class="val unit-sight">' . sprintf(
308                                 $_->{detect} ? '<strong class="unit-detect">%s</strong>' : '%s',
309                                 showrange($_, 'sight')
310                         ),
311                         '<td class="val unit-speed">' . showrange($_, 'speed'),
312                         '<td class="unit-magic">' . showmagic($_),
313                         "\n"
314                 );
315         }
316
317         my $units = do $datafile;
318         die "Cannot open unit data: $_\n" for $@ || $! || ();
319         my $grouped = !exists $get{order};
320         if (exists $get{order}) {
321                 $get{order} ||= '';
322                 if ($get{order} eq 'size') {
323                         $_->{order} = $_->{unit}*8 + $_->{suit} + $_->{hp}/512 + $_->{min}/8192 for @$units;
324                 }
325                 elsif ($get{order} eq 'cost') {
326                         $_->{order} = $_->{gas}*1.5 + $_->{min} + $_->{unit}/8 + $_->{build}/256/8 for @$units;
327                 }
328                 else {
329                         $units->[$_]->{order} = $_ for 0 .. $#$units;
330                 }
331         }
332         my @rows = $grouped ? @$units : sort {$a->{order} <=> $b->{order}} @$units;
333
334         my ($race, $cat) = ('', '');
335         for (@rows) {
336                 $race = $_->{race},
337                 printf '<tbody id="%s"><tr class="race"><th colspan="18"><h2>%s</h2>'."\n", $race, ucfirst $race
338                         if $grouped and $race ne $_->{race};
339                 $_->{cat} = $_->{race} if not $grouped;
340                 print(
341                         '<tr>',
342                         sprintf('<t%s class="cat">%s', $cat ne $_->{cat} ? ('h', $cat = $_->{cat}) : ('d', '&nbsp;')),
343                         '<td>' . $_->{name},
344                         showunitcols($_),
345                 );
346
347                 for my $alt (grep { $_->{alt} } @{ $_->{special} }) {
348                         print(
349                                 '<tr class="alt"><td class="cat"><td>' . $alt->{alt},
350                                 showunitcols($alt),
351                         );
352                 }
353         }
354 :>
355 </table>
356
357 <div class="legend">
358 <h2>Legend</h2>
359
360 <dl>
361 <dt>cost
362         <dd>minerals+gas required to create one unit
363         <dd>includes total expenses if based on existing units
364 <dt>build
365         <dd>relative time needed to create at least one unit
366         <dd>excludes construction of dependencies such as buildings
367                 and <span class="unit-composed">+</span>parent units
368 <dt>size
369         <dd>affected by <span class="unit unit-s">S</span>mall,
370                 <span class="unit unit-m">M</span>edium,
371                 or <span class="unit unit-l">L</span>arge unit damage
372         <dd>number of command points taken per unit
373         <dd><span class="unit unit-o">o</span>rganic/<span class="unit unit-u">m</span>echanic unit
374 <dt>HP<dd>
375         total number of hitpoints (including shields)
376 <dt>shield
377         <dd>percentage of HP in shields
378         <dd>shields always take full damage, irrelevant of unit size
379         <dd>does not take armor bonuses, but upgrades can decrease damage to any shield hit by upto 3
380 <dt>armor
381         <dd>base unit armor
382         <dd>can be increased by upto 3 at various facilities
383         <dd>each point decreases damage per hit by one, upto a minimum of ½
384         <dd>reduction applies to initial damage, before size penalties <small>(so a plasma hit of 12 to 4 armor large deals 2 damage, not ½)</small>
385 <dt>ground/air
386         <dd>damage done per single attack against ground/air units
387         <dd>2nd column indicates relative amount of damage done in
388                 1 second of fastest game time
389         <dd>splash damage<span class="unit-splash">+</span> hits nearby objects as well
390         <dd><span class="hurt unit-l">explosive</span> damage does only
391                 50% damage to small units, 75% to medium, 100% to large
392         <dd><span class="hurt unit-s">concussive/plasma</span> damage does
393                 25% to large, 50% medium, 100% to small units
394 <dt>sight
395         <dd>range in which the unit detects other units
396         <dd><strong class="unit-detect">emphasis</strong> indicates ability to detect cloaked units
397 <dt>range
398         <dd>maximum range of weapon (note siege tank also has a minimum range)
399 <dt>speed
400         <dd>relative speed of movement (when in full motion, startup speed ignored)
401 <dt>specials
402         <dd>special abilities are usually casted manually, but some are <span class="magic-perma">always active</span>
403         <dd>parentheses () indicate that it needs to be researched first
404         <dd>hover for description
405         <dd>range is maximum range required to activate
406         <dd>cost is percentage of total energy lost
407 </dl>
408
409 <p>
410 When two values are given (1-2), second value indicates attribute after all
411 possible upgrades.
412 </p>
413
414 </div>
415