13248fbb256afea92f25b287db48f48b14e01bd5
[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="2"><:= 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">armor</th>
70         <th class="val hurt" colspan="2">ground</th>
71         <th class="val hurt" colspan="2">air</th>
72         <th class="val unit-range">range</th>
73         <th class="val unit-sight">sight</th>
74         <th class="val unit-speed">speed</th>
75         <th class="unit-magic">specials</th>
76 </tr></thead>
77 <:
78 sub showrange {
79         my ($min, $max) = @_;
80         $_ = int($_ + .5) for $min, $max;  # round halves up
81         return $min == $max ? $min : "$min-$max";
82 }
83
84 sub showval {
85         my ($row, @elements) = @_;
86         my ($min, $max);
87
88         my $value = $row;
89         $value = ref $value eq 'HASH' && $value->{$_} or last for @elements;
90         if (ref $value eq 'ARRAY') {
91                 $min = $value->[0];
92                 $max = $value->[-1];
93         }
94         else {
95                 $min = $max = $value;
96         }
97         defined $min or return '';
98
99         if ($row->{upgrade}) {
100                 for (@{ $row->{upgrade} }) {
101                         my $increase = $_ or next;
102                         $increase = ref $increase eq 'HASH' && $increase->{$_} or last for @elements;
103                         $increase = $increase->[-1] if ref $increase eq 'ARRAY';
104                         $max += $increase if $increase;
105                 }
106         }
107
108         if ($elements[0] eq 'attack' and $elements[1] ne 'range' and $elements[2] eq 'dps') {{
109                 my $attack = $row->{ $elements[0] }->{ $elements[1] };
110                 ref $attack or $attack = $row->{ $elements[0] }->{$attack}; # follow
111                 my $type = $attack->{type} or next;
112                 if ($type eq 'explosive') {
113                         $min /= 2;
114                 }
115                 elsif ($type eq 'implosive') {
116                         $min /= 4;
117                 }
118         }}
119         return showrange($min, $max);
120 }
121
122         sub showattack {
123                 my ($row, $area) = @_;
124                 my $attack = $row->{attack}->{$area};
125                 if (not ref $attack) {
126                         # reference to another area
127                         $area = $attack;
128                         $attack = $row->{attack}->{$area};
129                 }
130
131                 return '<td colspan="2" class="hurt">' unless $attack;
132
133                 my $tagbase = '<td class="val hurt';
134                 if (ref $attack and $attack->{type}) {
135                         if ($attack->{type} eq 'explosive') {
136                                 $tagbase .= ' unit-l';
137                         }
138                         elsif ($attack->{type} eq 'implosive') {
139                                 $tagbase .= ' unit-s';
140                         }
141                 }
142                 $tagbase .= '">';
143
144                 my $out = showval($row, 'attack', $area, 'damage');
145                 $out .= '<span class="unit-splash">+</span>' if $attack->{splash};
146                 $attack->{dps} = $attack->{cooldown} && [
147                         map { 24 * $_ / $attack->{cooldown} * ($attack->{count} // 1) }
148                         map { ref $_ ? @{$_} : $_ }
149                         $attack->{damage}
150                         #TODO: upgrade (zergling)
151                 ];
152                 $out .= '<td class="val hurt hurtrel">' . showval($row, 'attack', $area, 'dps');
153                 return $tagbase . $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%%',
168                                                         100 * $_->{cost} / $row->{energy}
169                                                 ) : (),
170                                         ) : '',
171                                 ),
172                                 sprintf($_->{build} ? '(%s)' : '%s', $_->{abbr}),
173                 } grep { defined $_->{abbr} } @{$specials};
174         }
175
176         sub showunitcols {
177                 my ($row) = @_;
178                 local $_ = $row;
179                 $_->{hp} += $_->{shield} if $_->{shield};
180                 my $suitchar = '';
181                 if ($_->{suit}) {
182                         $suitchar = [qw/? s m l/]->[$_->{suit}];
183                 }
184
185                 return (
186                         '<td class="val min">' . ($_->{min} // ''),
187                         '<td class="val gas">' . ($_->{gas} || ''),
188                         !defined $_->{build} ? '<td>' : sprintf('<td class="val time">%s%.0f',
189                                 !!$_->{base} && '<span class="unit-composed">+</span>',
190                                 $_->{build} || '0',
191                         ),
192                         !$suitchar ? '<td>' : sprintf('<td class="unit unit-%s">%s', $suitchar, ucfirst $suitchar),
193                         '<td class="val unit">' . join('',
194                                 defined $_->{unit} && $_->{unit} == .5 ? '½' : $_->{unit},
195                                 defined $_->{organic} && sprintf(
196                                         '<span class="unit-%s" title="%s">%s</span>',
197                                         $_->{organic} ? 'o' : 'u',
198                                         $_->{organic} ? 'organic' : 'mechanic',
199                                         $_->{organic} ? 'o' : 'm',
200                                 ),
201                         ),
202                         '<td class="val unit-hp">' . $_->{hp} // '',
203                         '<td class="val unit-shield">' . (
204                                 $_->{shield} ? sprintf('%.0f%%', 100 * $_->{shield} / $_->{hp}) : '&nbsp;'
205                         ),
206                         '<td class="val unit-armor">' . showval($_, 'armor'),
207                         showattack($_, 'ground'),
208                         showattack($_, 'air'),
209                         '<td class="val unit-range">' . showval($_, 'attack', 'range'),
210                         '<td class="val unit-sight">' . sprintf(
211                                 $_->{detect} ? '<strong class="unit-detect">%s</strong>' : '%s',
212                                 showval($_, 'sight')
213                         ),
214                         '<td class="val unit-speed">' . showval($_, 'speed'),
215                         '<td class="unit-magic">' . showmagic($_),
216                         "\n"
217                 );
218         }
219
220         my $grouped = 1;  # race headers
221         if (exists $get{order}) {
222                 $grouped = 0;
223                 $get{order} ||= '';
224                 if ($get{order} eq 'size') {
225                         $_->{order} = $_->{unit}*8 + $_->{suit} + $_->{hp}/512 + $_->{min}/8192 for @$units;
226                 }
227                 elsif ($get{order} eq 'cost') {
228                         $_->{order} = $_->{gas}*1.5 + $_->{min} + $_->{unit}/8 + $_->{build}/256/8 for @$units;
229                 }
230                 else {
231                         $units->[$_]->{order} = $_ for 0 .. $#$units;
232                 }
233         }
234         my @rows = @{$units};
235         @rows = sort {$a->{order} <=> $b->{order}} @rows unless $grouped;
236
237         my ($race, $cat) = ('', '');
238         for (@rows) {
239                 if ($grouped) {
240                         printf '<tbody id="%s"><tr class="race"><th colspan="18"><h2>%s</h2>'."\n",
241                                 $race = $_->{race}, ucfirst $race
242                                         unless $race eq $_->{race};
243                 }
244                 else {
245                         $_->{cat} = $_->{race};
246                 }
247
248                 print(
249                         '<tr>',
250                         '<th class="cat">', $cat ne $_->{cat} && ($cat = $_->{cat}),
251                         '<td>', $_->{name},
252                         showunitcols($_),
253                 );
254
255                 for my $subrow (@{ $_->{special} }) {
256                         $subrow->{alt} or next;
257                         print(
258                                 '<tr class="alt"><td class="cat"><td>', $subrow->{alt},
259                                 showunitcols($subrow),
260                         );
261                 }
262         }
263 :>
264 </table>
265
266 <div class="legend">
267 <h2>Legend</h2>
268
269 <dl>
270 <dt>cost
271         <dd>minerals+gas required to create one unit
272         <dd>includes total expenses if based on existing units
273 <dt>build
274         <dd>relative time needed to create at least one unit
275         <dd>excludes construction of dependencies such as buildings
276                 and <span class="unit-composed">+</span>parent units
277 <dt>size
278         <dd>affected by <span class="unit unit-s">S</span>mall,
279                 <span class="unit unit-m">M</span>edium,
280                 or <span class="unit unit-l">L</span>arge unit damage
281         <dd>number of command points taken per unit
282         <dd><span class="unit unit-o">o</span>rganic/<span class="unit unit-u">m</span>echanic unit
283 <dt>HP<dd>
284         total number of hitpoints (including shields)
285 <dt>shield
286         <dd>percentage of HP in shields
287         <dd>shields always take full damage, irrelevant of unit size
288         <dd>does not take armor bonuses, but upgrades can decrease damage to any shield hit by upto 3
289 <dt>armor
290         <dd>base unit armor
291         <dd>can be increased by upto 3 at various facilities
292         <dd>each point decreases damage per hit by one, upto a minimum of ½
293         <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>
294 <dt>ground/air
295         <dd>damage done per single attack against ground/air units
296         <dd>2nd column indicates relative amount of damage done in
297                 1 second of fastest game time
298         <dd>splash damage<span class="unit-splash">+</span> hits nearby objects as well
299         <dd><span class="hurt unit-l">explosive</span> damage does only
300                 50% damage to small units, 75% to medium, 100% to large
301         <dd><span class="hurt unit-s">concussive/plasma</span> damage does
302                 25% to large, 50% medium, 100% to small units
303 <dt>sight
304         <dd>range in which the unit detects other units
305         <dd><strong class="unit-detect">emphasis</strong> indicates ability to detect cloaked units
306 <dt>range
307         <dd>maximum range of weapon (note siege tank also has a minimum range)
308 <dt>speed
309         <dd>relative speed of movement (when in full motion, startup speed ignored)
310 <dt>specials
311         <dd>special abilities are usually casted manually, but some are <span class="magic-perma">always active</span>
312         <dd>parentheses () indicate that it needs to be researched first
313         <dd>hover for description
314         <dd>range is maximum range required to activate
315         <dd>cost is percentage of total energy lost
316 </dl>
317
318 <p>
319 When two values are given (1-2), second value indicates attribute after all
320 possible upgrades.
321 </p>
322
323 </div>
324