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