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