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