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