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