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