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