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