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