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