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