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