sc: fix reaver scarab cost (wrong key)
[sheet.git] / sc.plp
1 <(common.inc.plp)><:
2 use List::Util qw(max sum);
3
4 my %scver = (
5         id => 'bw',
6         name => 'Brood War',
7         title => 'starcraft',
8         game => 'StarCraft',
9         major => 1,
10 );
11
12 if ($ENV{PATH_INFO} and $ENV{PATH_INFO} eq '/2') {
13         %scver = (
14                 id => 'hots',
15                 name => 'Heart of the Swarm',
16                 title => 'starcraft2',
17                 game => 'StarCraft II',
18                 major => 2,
19         );
20 }
21 my $datafile = "sc-units-$scver{id}.inc.pl";
22
23 Html({
24         title => "$scver{title} unit cheat sheet",
25         version => 'v1.1',
26         description => [
27                 "Reference of $scver{game} unit properties,"
28                 . " comparing various statistics of all the units in $scver{name}"
29                 . ' including costs, damage, defense, speed, ranges, and abilities.',
30         ],
31         keywords => [
32                 qw'
33                 starcraft game unit statistics stats comparison table sheet cheat
34                 reference software attributes properties
35                 ',
36                 $scver{major} < 2 ? qw' bw broodwar brood war ' : qw' starcraft2 hots ',
37         ],
38         stylesheet => [qw'light'],
39         raw => '<link rel="stylesheet" type="text/css" media="all" href="/sc.css?1.1" title="light">',
40         data => [$datafile],
41 });
42
43 print "<h1>$scver{game} units</h1>\n\n";
44
45 my $units = do $datafile;
46 die "Cannot open unit data: $_\n" for $@ || $! || ();
47 my $patch = shift @{$units}
48         or die "Cannot open unit data: metadata not found\n";
49
50 print "<p>Unit properties as seen or measured in $scver{name}\n$patch.\n";
51 print "Also see the $_ table.\n" for join(', ',
52         ('<a href="/sc/2">StarCraft 2: HotS</a>')    x ($scver{major} < 2),
53         ('<a href="/sc">original SC: Brood War</a>') x ($scver{major} > 1),
54 );
55 print "</p>\n\n";
56
57 sub addupgrade {
58         my ($ref, $increase, $org) = @_;
59         if (ref $increase eq 'HASH') {
60                 addupgrade(\${$ref}->{$_}, $increase->{$_}, $org->{$_}) for keys %{$increase};
61         }
62         elsif (ref $increase eq 'ARRAY') {
63                 addupgrade(\${$ref}->[$_], $increase->[$_], $org->[$_]) for 0 .. $#{$increase};
64         }
65         ${$ref} //= $org;
66         ${$ref} += $increase if $increase =~ /^-?[0-9.]+/;
67 }
68
69 for my $unit (@{$units}) {
70         for my $upgrade (@{ $unit->{upgrade} }) {
71                 while (my ($col, $increase) = each %{$upgrade}) {
72                         defined $unit->{$col} or next;
73                         addupgrade(\$unit->{upgraded}->{$col}, $increase, $unit->{$col});
74                 }
75         }
76         for my $special (@{ $unit->{special} }) {
77                 for my $upgrade (@{ $special->{upgrade} }) {
78                         while (my ($col, $increase) = each %{$upgrade}) {
79                                 defined $special->{$col} or next;
80                                 addupgrade(\$special->{upgraded}->{$col}, $increase, $special->{$col});
81                         }
82                 }
83         }
84 }
85
86 sub coltoggle {
87         my ($name, $id, $nolink) = @_;
88         return sprintf(
89                 (defined $get{order} ? $get{order} eq $id : !$id) ? '%2$s ▼'
90                         : $nolink ? '%2$s' : '<a href="?%s">%s</a>',
91                 $id && "order=$id", $name
92         );
93 }
94 :><table class="units">
95 <thead><tr>
96         <th></th>
97         <th><:= coltoggle('name', '') :></th>
98         <th class="val min" title=minerals>cost</th>
99         <th class="val gas">gas</th>
100         <th class="val time"><:= coltoggle(qw'build cost') :></th>
101         <th class="unit" colspan="2"><:= coltoggle(qw'size size') :></th>
102         <th class="unit" colspan="2">attr</th>
103         <th class="val unit-hp">HP</th>
104         <th class="val unit-shield">shield</th>
105         <th class="val unit-armor" title="armor">⛨</th>
106         <th class="val hurt">attack</th>
107         <th class="hurt hurtrel"><:= coltoggle(qw'dps attack 1') :></th>
108         <th class="val unit-range" colspan=3>range</th>
109         <th class="val unit-sight">sight</th>
110         <th class="val unit-speed">speed</th>
111         <th class="unit-magic">specials</th>
112 </tr></thead>
113 <:
114 sub showrange {
115         my ($min, $max) = @_;
116         return '' if not defined $min;
117         $_ &&= int($_ + .5) for $min, $max;  # round halves up
118         return $min || '-' if not defined $max or $min == $max;
119         return "$min-$max";
120 }
121
122         sub showcost {
123                 my ($row, $unit) = @_;
124                 return join(' ',
125                         sprintf('cost %.0f%%', 100 * $row->{cost} / $unit->{energy}),
126                         !defined $row->{maint} ? () :
127                                 sprintf('+%.1f%%/s', 100 * $row->{maint} / $unit->{energy}),
128                 );
129         }
130
131         sub showattack {
132                 my ($row, $area) = @_;
133                 my $attack = $row->{attack}->[$area]
134                         or return '<td colspan=5 class="hurt">';
135
136                 my $upattack = $row->{upgraded}->{attack}->[$area];
137                 my $damage = $attack->{damage};
138                 my $maxdamage = $upattack->{damage} // $damage;
139                 $maxdamage += ($upattack->{upgrade} // $attack->{upgrade}) * 3;
140
141                 my $out = '<td class="val hurt">';
142                 $out .= sprintf '<span title="%s">¤</span> ', showcost($attack, $row)
143                         if $attack->{cost};
144                 $out .= sprintf('<small>%s× </small>',
145                         showrange($attack->{count}, $upattack->{count}),
146                 ) if $attack->{count} > 1;
147                 $out .= '<span class="unit-l" title="explosive">*</span>'
148                         if $attack->{type} eq 'explosive';
149                 $out .= '<span class="unit-s" title="implosive">~</span>'
150                         if $attack->{type} eq 'implosive';
151         if (my @bonus = sort grep { !/^-/ } keys %{ $attack->{bonus} }) {
152                 $out .= sprintf('<span class="%s" title="%s">&ge;</span>',
153                         (map {
154                                 $_ eq 'light' ? 'unit-s' :
155                                 $_ eq 'armored' ? 'unit-l' :
156                                 $_ eq 'organic' ? 'unit-o' :
157                                 $_ eq 'massive' ? 'unit-h' :
158                                 $_ eq 'shields' ? 'unit-shield' :
159                                 '',
160                         } join '_', @bonus),
161                         join(', ', map {(
162                                 sprintf('+%s vs %s',
163                                         showrange(
164                                                 $attack->{bonus}->{$_},
165                                                 $attack->{bonus}->{$_} + $attack->{bonus}->{"-$_"} * 3,
166                                         ),
167                                         $_,
168                                 ),
169                         )} @bonus),
170                 );
171         }
172                 $out .= '<span class="unit-pdd" title="projectile">•</span>'
173                         if $attack->{type} eq 'projectile';
174
175                 $out .= sprintf '<span title="%s">', $attack->{name} if $attack->{name};
176                 $out .= showrange($damage, $maxdamage);
177                 $out .= '</span>' if $attack->{name};
178                 $out .= sprintf('<span class="unit-splash" title="%s">%s</span>',
179                         $attack->{splash} eq 'line' ? ('linear', '+') : ('splash', '⁜')
180                 ) if $attack->{splash};
181
182                 $out .= '<td class="val hurt hurtrel">';
183                 if ($attack->{dps}) {
184                         # precalculated dps, do not touch
185                         $out .= showrange($attack->{dps}->[0],
186                                 $upattack->{dps}->[-1] // $attack->{dps}->[-1]
187                         );
188                 }
189                 elsif ($attack->{cooldown}) {
190                         if (my $type = $attack->{type}) {
191                                 if ($type eq 'explosive') {
192                                         $damage /= 2;
193                                 }
194                                 elsif ($type eq 'implosive') {
195                                         $damage /= 4;
196                                 }
197                         }
198                         $damage *= ($attack->{count} // 1) / $attack->{cooldown};
199                         if (my $bonus = $upattack->{bonus} // $attack->{bonus}) {
200                                 $maxdamage += $_ for max(
201                                         map { $bonus->{$_} + $bonus->{"-$_"} * 3 }
202                                         grep { !/^-/ } keys %{$bonus}
203                                 );
204                         }
205                         $maxdamage *= ($upattack->{count} // $attack->{count} // 1)
206                                     / ($upattack->{cooldown} // $attack->{cooldown});
207                         $out .= showrange($damage, $maxdamage);
208                 }
209
210                 $out .= '<td class="unit hurt-g">' . '▽' x !!($attack->{anti} & 1);
211                 $out .= '<td class="unit hurt-a">' . '△' x !!($attack->{anti} & 2);
212
213                 $out .= '<td class="val unit-range">' .
214                         showrange($attack->{range}, $upattack->{range});
215
216                 return $out;
217         }
218
219         sub showmagic {
220                 my ($row) = @_;
221                 my $specials = $row->{special} or return '';
222                 return join ' ', map {
223                         sprintf '<span%s title="%s">%s</span>',
224                                 $_->{duration} < 0 && ' class="magic-perma"',
225                                 join('',
226                                         $_->{name},
227                                         $_->{desc} ? ": $_->{desc}" : '',
228                                         (map { $_ && " ($_)" } join ', ',
229                                                 #TODO: apply upgrades
230                                                 $_->{range} ? "range $_->{range}" : (),
231                                                 $_->{cost} ? showcost($_, $row) :
232                                                 $_->{cooldown} ? "cooldown $_->{cooldown}s" : (),
233                                         ),
234                                 ),
235                                 sprintf($_->{build} ? '(%s)' : '%s', $_->{abbr}),
236                 } grep { defined $_->{abbr} } @{$specials};
237         }
238
239         sub showunitcols {
240                 my ($row) = @_;
241                 local $_ = $row;
242                 $_->{hp} += $_->{shield} if $_->{shield};
243
244                 return (
245                         '<td class="val min">' . ($_->{min} // ''),
246                         '<td class="val gas">' . ($_->{gas} || ''),
247                         !defined $_->{build} ? '<td>' : sprintf('<td class="val time">%s%.0f',
248                                 !!$_->{base} && '<span class="unit-composed">+</span>',
249                                 $_->{build} || '0',
250                         ),
251                         sprintf('<td class="unit unit-%s" title="%4$s%3$s">%s',
252                                 $_->            {cargo} < 0 ? ('supply',           T => 'transport') :
253                                 $_->{upgraded}->{cargo} < 0 ? ('supply magic-opt', T => 'optional transport') :
254                                 $_->{attr}->{flying}    ? ('air', F => 'flying') :
255                                 $_->{attr}->{structure} ? ('x',   B => 'building') :
256                                 (
257                                         [qw( x s m l l h h h h )]->[ $_->{cargo} ],
258                                         $_->{cargo} || '-',
259                                         $_->{cargo} ? 'transportable' : 'untransportable',
260                                 ),
261                                 defined $_->{size} && sprintf('⌀%.1f ', $_->{size}),
262                         ),
263                         sprintf('<td class="val unit%s">%s',
264                                 defined $_->{pop} && $_->{pop} < 0 && ' unit-supply',
265                                 defined $_->{pop} && $_->{pop} == .5 ? '½' : $_->{pop},
266                         ),
267                         '<td class="unit unit-type">' . join('', grep { $_ }
268                                 (defined $_->{organic} ? !$_->{organic} : $_->{attr}->{mech})
269                                         && '<span class="unit-u" title="mechanic">m</span>',
270                                 ($_->{organic} || $_->{attr}->{organic})
271                                         && '<span class="unit-o" title="organic">o</span>',
272                                 $_->{attr}->{psionic}
273                                         && '<span class="unit-p" title="psionic">ψ</span>',
274                         ),
275                         '<td class="unit unit-attr">' . join('', grep { $_ }
276                                 $_->{attr}->{armored}
277                                         && '<span class="unit unit-l" title="armored">A</span>',
278                                 $_->{attr}->{light}
279                                         && '<span class="unit unit-s" title="light">L</span>',
280                                 $_->{suit} && sprintf(
281                                         '<span class="unit unit-%s" title="%3$s">%s</span>',
282                                         map { @{$_} } [
283                                                 [qw( x ? unknown )],
284                                                 [qw( s S small )],
285                                                 [qw( m M medium )],
286                                                 [qw( l L large )],
287                                         ]->[ $_->{suit} ],
288                                 ),
289                                 $_->{attr}->{massive}
290                                         && '<span class="unit-massive" title="massive">⚓</span>',
291                         ),
292                         '<td class="val unit-hp">' . $_->{hp} // '',
293                         $_->{shield} ? sprintf('<td class="val unit-shield">%.0f%%<td',
294                                 100 * $_->{shield} / $_->{hp}
295                         ) : '<td colspan=2',
296                         ' class="val unit-armor">' .
297                                 showrange($_->{armor}, $_->{upgraded}->{armor}),
298                         showattack($_, 0),
299                         '<td class="val unit-sight">' . sprintf(
300                                 $_->{detect} ? '<strong class="unit-detect">%s</strong>' : '%s',
301                                 showrange($_->{sight}, $_->{upgraded}->{sight})
302                         ),
303                         '<td class="val unit-speed">' .
304                                 showrange($_->{speed}, $_->{upgraded}->{speed}),
305                         $_->{attr}->{jump}
306                                 && qq'<span class="unit unit-jump" title="$_->{attr}->{jump}">↕</span>',
307                         '<td class="unit-magic">' . showmagic($_),
308                         !$_->{attack}->[1] ? () : (
309                                 '<tr><td colspan=12>', showattack($_, 1), '<td colspan=3>'
310                         ),
311                         !$_->{attack}->[2] ? () : (
312                                 '<tr><td colspan=12>', showattack($_, 2), '<td colspan=3>'
313                         ),
314                         "\n"
315                 );
316         }
317
318         my $grouped = 1;  # race headers
319         if (exists $get{order}) {
320                 $grouped = 0;
321                 $get{order} ||= '';
322                 if ($get{order} eq 'size') {
323                         $_->{order} = (
324                                 $_->{pop}*16 + ($_->{size} // $_->{suit}) + $_->{cargo}/8
325                                 + $_->{hp}/512 + $_->{min}/8192
326                         ) for @$units;
327                 }
328                 elsif ($get{order} eq 'cost') {
329                         $_->{order} = (
330                                 $_->{gas}*1.5 + $_->{min} + $_->{pop}/8 + $_->{build}/256/8
331                         ) for @$units;
332                 }
333                 elsif ($get{order} eq 'attack') {
334                         $_->{order} = $_->{hp} / 1024 + $_->{shield} / 1008 + max(
335                                 map {
336                                         ($_->{damage} + $_->{upgrade} * 3)
337                                         * ($_->{count} // 1) / ($_->{cooldown} // 1)
338                                         * ($_->{splash} ? 1.01 : 1)
339                                         * ($_->{type} eq 'implosive' ? .96 : 1)
340                                         * ($_->{type} eq 'explosive' ? .98 : 1)
341                                 } @{ $_->{attack} }
342                         ) for @$units;
343                 }
344                 else {
345                         $units->[$_]->{order} = $_ for 0 .. $#$units;
346                 }
347         }
348         my @rows = @{$units};
349         @rows = sort {$a->{order} <=> $b->{order}} @rows unless $grouped;
350
351         my ($race, $cat) = ('', '');
352         for (@rows) {
353                 if ($grouped) {
354                         printf '<tbody id="%s"><tr class="race"><th colspan="20"><h2>%s</h2>'."\n",
355                                 $race = $_->{race}, ucfirst $race
356                                         unless $race eq $_->{race};
357                 }
358                 else {
359                         $_->{cat} = $_->{race};
360                 }
361
362                 print(
363                         '<tr>',
364                         '<th class="cat">', $cat ne $_->{cat} && ($cat = $_->{cat}),
365                         '<td>', $_->{name},
366                         showunitcols($_),
367                 );
368
369                 for my $subrow (@{ $_->{special} }) {
370                         $subrow->{alt} or next;
371                         print(
372                                 '<tr class="alt"><th class="cat"><td>', $subrow->{alt},
373                                 showunitcols($subrow),
374                         );
375                 }
376         }
377 :>
378 </table>
379
380 <div class="legend">
381 <h2>Legend</h2>
382
383 <dl>
384 <dt>cost
385         <dd>minerals+gas required to create one unit
386         <dd>includes total expenses if based on existing units
387 <dt>build
388         <dd>relative time needed to create at least one unit
389         <dd>excludes construction of dependencies such as buildings
390                 and <span class="unit-composed">+</span>parent units
391 <dt>size
392         <dd><span class="unit unit-supply">T</span>ransports can fit upto
393                 <span class="unit unit-s">8</span>
394                 non-<span class="unit unit-air">F</span>lying units
395         <dd>number of command points taken per unit
396         <dd><:
397 if ($scver{major} > 1) {
398                 :>received damage depends on
399                 <span class="unit unit-o">o</span>rganic,
400                 <span class="unit unit-u">m</span>echanic,
401                 <span class="unit unit-p">ψ</span>(ps)ionic,
402                 <span class="unit unit-s">L</span>ight, and
403                 <span class="unit unit-l">A</span>rmored
404                 attributes
405         <dd>massive <span class="unit-massive">⚓</span> units
406                 cannot be lifted or slowed and can break force fields<:
407 } else {
408                 :><span class="unit unit-o">o</span>rganic/<span class="unit unit-u">m</span>echanic unit
409         <dd>affected by <span class="unit unit-s">S</span>mall,
410                 <span class="unit unit-m">M</span>edium, or
411                 <span class="unit unit-l">L</span>arge unit damage<:
412 } :>
413 <dt>HP<dd>
414         total number of hitpoints (including shields)
415 <dt>shield
416         <dd>percentage of HP in shields
417         <dd>shields always take full damage, irrelevant of unit size
418         <dd>does not take armor bonuses, but upgrades can decrease damage to any shield hit by upto 3
419 <dt>armor
420         <dd>base unit armor
421         <dd>can be increased by upto 3 at various facilities
422         <dd>each point decreases damage per hit by one, upto a minimum of ½
423         <dd>reduction applies to initial damage, before size penalties
424                 <small>(so a plasma hit of 12 to 4 armor large deals 2 damage, not ½)</small>
425 <dt>attack
426         <dd>damage given per single hit
427         <dd><span class="hurtrel">dps</span> indicates relative amount of damage
428                 done in 1 second of in-game time
429         <dd>splash damage hits all objects nearby <span class="unit-splash">⁜</span>
430                 or in a straight line <span class="unit-splash">+</span>.
431         <dd><:
432 if ($scver{major} > 1) {
433                 :>does not include <span>&ge;</span>bonus damage
434                 dealt to susceptible unit types
435         <dd><span class="unit-pdd">•</span>projectile shots are negated by
436                 Point Defense Drones<:
437 } else {
438                 :><span class="unit-l">*</span>explosive damage does only
439                 50% damage to small units, 75% to medium, 100% to large
440         <dd><span class="unit-s">~</span>concussive/plasma damage does
441                 25% to large, 50% medium, 100% to small units<:
442 } :>
443         <dd>targets <span class="hurt-g">▽</span>&nbsp;ground
444                 and/or  <span class="hurt-a">△</span>&nbsp;air
445 <dt>range
446         <dd>maximum range of weapon (note siege tank also has a minimum range)
447 <dt>sight
448         <dd>range in which the unit detects other units
449         <dd><strong class="unit-detect">emphasis</strong> indicates ability to detect cloaked units
450 <dt>speed
451         <dd>relative speed of movement (when in full motion, startup speed ignored)
452 <dt>specials
453         <dd>parentheses () indicate that it needs to be researched first
454         <dd><span class="magic-perma">passive</span> abilities are always enabled
455         <dd>hover for description
456         <dd>range is maximum range required to activate
457         <dd>cost is percentage of total energy lost
458 </dl>
459
460 <p>
461 When two values are given (1-2), second value indicates attribute after all
462 possible upgrades.
463 </p>
464
465 </div>
466