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