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