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