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