sc: inline damage type indicators in html
[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 addupgrade {
52         my ($ref, $increase) = @_;
53         if (ref $increase eq 'HASH') {
54                 addupgrade(\${$ref}->{$_}, $increase->{$_}) for keys %{$increase};
55         }
56         elsif (ref $increase eq 'ARRAY') {
57                 addupgrade(\${$ref}->[$_], $increase->[$_]) for 0 .. $#{$increase};
58         }
59         ${$ref} += $increase if $increase =~ /^-?[0-9.]+/;
60 }
61
62 for my $unit (@{$units}) {
63         for my $upgrade (@{ $unit->{upgrade} }) {
64                 while (my ($col, $increase) = each %{$upgrade}) {
65                         defined $unit->{$col} or next;
66                         addupgrade(\( $unit->{upgraded}->{$col} //= $unit->{$col} ), $increase);
67                 }
68         }
69 }
70
71 sub coltoggle {
72         my ($name, $id) = @_;
73         return sprintf(
74                 (defined $get{order} ? $get{order} eq $id : !$id) ? '%2$s ▼'
75                         : '<a href="?%s">%s</a>',
76                 $id && "order=$id", $name
77         );
78 }
79 :><table class="units">
80 <thead><tr>
81         <th></th>
82         <th><:= coltoggle('name', '') :></th>
83         <th class="val min"><img src="/minerals.png" alt="min"></th>
84         <th class="val gas"><img src="/gas.png" alt="gas"></th>
85         <th class="val time"><:= coltoggle(qw'build cost') :></th>
86         <th class="unit" colspan="4"><:= coltoggle(qw'size size') :></th>
87         <th class="val unit-hp">HP</th>
88         <th class="val unit-shield">shield</th>
89         <th class="val unit-armor" title="armor">⛨</th>
90         <th class="val hurt" colspan=3>attack</th>
91         <th class="hurt hurtrel">dps</th>
92         <th class="val unit-range">range</th>
93         <th class="val unit-sight">sight</th>
94         <th class="val unit-speed">speed</th>
95         <th class="unit-magic">specials</th>
96 </tr></thead>
97 <:
98 sub showrange {
99         my ($min, $max) = @_;
100         return '' if not defined $min;
101         $_ &&= int($_ + .5) for $min, $max;  # round halves up
102         return $min if not defined $max or $min == $max;
103         return "$min-$max";
104 }
105
106         sub showattack {
107                 my ($row, $area) = @_;
108                 my $attack = $row->{attack}->[$area]
109                         or return '<td colspan=4 class="hurt">';
110
111                 my $out = '';
112                 $out .= '<td class="unit hurt-g">' . '▽' x !!($attack->{anti} & 1);
113                 $out .= '<td class="unit hurt-a">' . '△' x !!($attack->{anti} & 2);
114                 $out .= '<td class="val hurt">';
115
116                 my $upattack = $row->{upgraded}->{attack}->[$area];
117                 my $damage = $attack->{damage};
118                 my $maxdamage = $upattack->{damage} // $damage;
119                 $damage = $damage->[0] if ref $damage;
120                 $maxdamage = $maxdamage->[-1] if ref $maxdamage;
121
122                 $out .= "<small>$attack->{count}× </small>" if $attack->{count} > 1;
123                 $out .= '<span class="unit-l" title="explosive">*</span>'
124                         if $attack->{type} eq 'explosive';
125                 $out .= '<span class="unit-s" title="implosive">~</span>'
126                         if $attack->{type} eq 'implosive';
127                 $out .= showrange($damage, $maxdamage);
128                 $out .= sprintf('<span class="unit-splash" title="%s">%s</span>',
129                         $attack->{splash} eq 'line' ? ('linear', '×') : ('splash', '+')
130                 ) if $attack->{splash};
131
132                 $out .= '<td class="val hurt hurtrel">';
133                 if ($attack->{cooldown}) {
134                         if (my $type = $attack->{type}) {
135                                 if ($type eq 'explosive') {
136                                         $damage /= 2;
137                                 }
138                                 elsif ($type eq 'implosive') {
139                                         $damage /= 4;
140                                 }
141                         }
142                         $damage *= ($attack->{count} // 1) / $attack->{cooldown};
143                         $maxdamage *= ($upattack->{count} // $attack->{count} // 1)
144                                     / ($upattack->{cooldown} // $attack->{cooldown});
145                         $out .= showrange($damage, $maxdamage);
146                 }
147
148                 return $out;
149         }
150
151         sub showmagic {
152                 my ($row) = @_;
153                 my $specials = $row->{special} or return '';
154                 return join ' ', map {
155                         sprintf '<span%s title="%s">%s</span>',
156                                 $_->{duration} < 0 && ' class="magic-perma"',
157                                 join('',
158                                         $_->{name},
159                                         $_->{desc} ? ": $_->{desc}" : '',
160                                         $_->{range} || $_->{cost} ? sprintf(' (%s)', join ', ',
161                                                 $_->{range} ? "range $_->{range}" : (),
162                                                 $_->{cost} ? sprintf('cost %.0f%%%s',
163                                                         100 * $_->{cost} / $row->{energy},
164                                                         defined $_->{maint} && sprintf('+%.1f%%/s',
165                                                                 100 * $_->{maint} / $row->{energy},
166                                                         ),
167                                                 ) : (),
168                                         ) : '',
169                                 ),
170                                 sprintf($_->{build} ? '(%s)' : '%s', $_->{abbr}),
171                 } grep { defined $_->{abbr} } @{$specials};
172         }
173
174         sub showunitcols {
175                 my ($row) = @_;
176                 local $_ = $row;
177                 $_->{hp} += $_->{shield} if $_->{shield};
178                 my $suitchar = '';
179                 if ($_->{attr}->{structure}) {
180                         $suitchar = 'b';
181                 }
182                 elsif ($_->{suit}) {
183                         $suitchar = [qw/? s m l/]->[$_->{suit}];
184                 }
185                 elsif ($_->{cargo} > 0) {
186                         $suitchar = [qw/? s m l l h h h h/]->[abs $_->{cargo}];
187                 }
188                 elsif ($_->{size}) {
189                         $suitchar = [qw/s m l h h h/]->[$_->{size}];
190                 }
191                 elsif ($_->{attr} and $_->{attr}->{light}) {
192                         $suitchar = 's';
193                 }
194                 elsif ($_->{attr} and $_->{attr}->{armored}) {
195                         $suitchar = 'l';
196                 }
197
198                 return (
199                         '<td class="val min">' . ($_->{min} // ''),
200                         '<td class="val gas">' . ($_->{gas} || ''),
201                         !defined $_->{build} ? '<td>' : sprintf('<td class="val time">%s%.0f',
202                                 !!$_->{base} && '<span class="unit-composed">+</span>',
203                                 $_->{build} || '0',
204                         ),
205                         !$suitchar ? '<td>' : sprintf('<td class="unit unit-%s">%s%s',
206                                 $suitchar, ucfirst $suitchar,
207                                 $_->{attr}->{massive}
208                                         && '<span class="unit-massive" title="massive">⚓</span>',
209                         ),
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">' .
232                                 showrange($_->{armor}, $_->{upgraded}->{armor}),
233                         showattack($_, 0),
234                         '<td class="val unit-range">' .
235                                 showrange(map { $_->{attack}->[0]->{range} } $_, $_->{upgraded}),
236                         '<td class="val unit-sight">' . sprintf(
237                                 $_->{detect} ? '<strong class="unit-detect">%s</strong>' : '%s',
238                                 showrange($_->{sight}, $_->{upgraded}->{sight})
239                         ),
240                         '<td class="val unit-speed">' .
241                                 showrange($_->{speed}, $_->{upgraded}->{speed}),
242                         $_->{attr}->{jump}
243                                 && qq'<span class="unit unit-jump" title="$_->{attr}->{jump}">↕</span>',
244                         $_->{attr}->{flying}
245                                 && qq'<span class="unit unit-jump" title="flying">↑</span>',
246                         '<td class="unit-magic">' . showmagic($_),
247                         !$_->{attack}->[1] ? () : (
248                                 '<tr><td colspan=12>', showattack($_, 1), '<td colspan=4>'
249                         ),
250                         "\n"
251                 );
252         }
253
254         my $grouped = 1;  # race headers
255         if (exists $get{order}) {
256                 $grouped = 0;
257                 $get{order} ||= '';
258                 if ($get{order} eq 'size') {
259                         $_->{order} = $_->{unit}*8 + $_->{suit} + $_->{hp}/512 + $_->{min}/8192 for @$units;
260                 }
261                 elsif ($get{order} eq 'cost') {
262                         $_->{order} = $_->{gas}*1.5 + $_->{min} + $_->{unit}/8 + $_->{build}/256/8 for @$units;
263                 }
264                 else {
265                         $units->[$_]->{order} = $_ for 0 .. $#$units;
266                 }
267         }
268         my @rows = @{$units};
269         @rows = sort {$a->{order} <=> $b->{order}} @rows unless $grouped;
270
271         my ($race, $cat) = ('', '');
272         for (@rows) {
273                 if ($grouped) {
274                         printf '<tbody id="%s"><tr class="race"><th colspan="20"><h2>%s</h2>'."\n",
275                                 $race = $_->{race}, ucfirst $race
276                                         unless $race eq $_->{race};
277                 }
278                 else {
279                         $_->{cat} = $_->{race};
280                 }
281
282                 print(
283                         '<tr>',
284                         '<th class="cat">', $cat ne $_->{cat} && ($cat = $_->{cat}),
285                         '<td>', $_->{name},
286                         showunitcols($_),
287                 );
288
289                 for my $subrow (@{ $_->{special} }) {
290                         $subrow->{alt} or next;
291                         print(
292                                 '<tr class="alt"><th class="cat"><td>', $subrow->{alt},
293                                 showunitcols($subrow),
294                         );
295                 }
296         }
297 :>
298 </table>
299
300 <div class="legend">
301 <h2>Legend</h2>
302
303 <dl>
304 <dt>cost
305         <dd>minerals+gas required to create one unit
306         <dd>includes total expenses if based on existing units
307 <dt>build
308         <dd>relative time needed to create at least one unit
309         <dd>excludes construction of dependencies such as buildings
310                 and <span class="unit-composed">+</span>parent units
311 <dt>size
312         <dd><:
313 if ($scver{major} > 1) {
314                 :>transports can fit 8 <span class="unit unit-s">S</span>mall,
315                 4 <span class="unit unit-m">M</span>edium,
316                 2 <span class="unit unit-l">L</span>arge,
317                 or a single <span class="unit unit-h">H</span>uge unit
318         <dd>massive <span class="unit-massive">⚓</span> units
319                 cannot be lifted or slowed and can break force fields<:
320 } else {
321                 :>affected by <span class="unit unit-s">S</span>mall,
322                 <span class="unit unit-m">M</span>edium, or
323                 <span class="unit unit-l">L</span>arge unit damage<:
324 } :>
325         <dd>number of command points taken per unit
326         <dd><:
327 if ($scver{major} > 1) {
328                 :>received damage depends on
329                 <span class="unit unit-o">o</span>rganic,
330                 <span class="unit unit-u">m</span>echanic,
331                 <span class="unit unit-p">ψ</span>(ps)ionic,
332                 <span class="unit unit-s">L</span>ight, and
333                 <span class="unit unit-l">A</span>rmored
334                 attributes<:
335 } else {
336                 :><span class="unit unit-o">o</span>rganic/<span class="unit unit-u">m</span>echanic unit<:
337 } :>
338 <dt>HP<dd>
339         total number of hitpoints (including shields)
340 <dt>shield
341         <dd>percentage of HP in shields
342         <dd>shields always take full damage, irrelevant of unit size
343         <dd>does not take armor bonuses, but upgrades can decrease damage to any shield hit by upto 3
344 <dt>armor
345         <dd>base unit armor
346         <dd>can be increased by upto 3 at various facilities
347         <dd>each point decreases damage per hit by one, upto a minimum of ½
348         <dd>reduction applies to initial damage, before size penalties
349                 <small>(so a plasma hit of 12 to 4 armor large deals 2 damage, not ½)</small>
350 <dt>attack
351         <dd>targets <span class="hurt-g">▽</span>&nbsp;ground
352                 and/or  <span class="hurt-a">△</span>&nbsp;air
353         <dd>damage given per single hit
354         <dd><em>dps</em> indicates relative amount of damage done in
355                 1 second of in-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="unit-l">*</span>explosive damage does only
359                 50% damage to small units, 75% to medium, 100% to large
360         <dd><span class="unit-s">~</span>concussive/plasma 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