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