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