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