sc: rename data files to append game release abbreviation
[sheet.git] / sc.plp
1 <(common.inc.plp)><:
2
3 my $scver = 'bw';
4 if ($ENV{PATH_INFO} and $ENV{PATH_INFO} eq '/2') {
5         my $scver = 'hots';
6 }
7 my $datafile = "sc-units-$scver.inc.pl";
8
9 Html({
10         title => 'starcraft unit cheat sheet',
11         version => 'v1.0',
12         description => [
13                 'Reference of StarCraft unit properties,'
14                 . ' comparing various statistics of all the units in Brood War'
15                 . ' including costs, damage, defense, speed, ranges, and abilities.',
16         ],
17         keywords => [qw'
18                 starcraft game unit statistics stats comparison table sheet cheat
19                 reference software attributes properties
20         '],
21         stylesheet => [qw'light'],
22         raw => '<link rel="stylesheet" type="text/css" media="all" href="/sc.css?1.1" title="light">',
23         data => [$datafile],
24 });
25
26 :>
27 <h1>StarCraft units</h1>
28
29 <p>
30 Unit properties as seen or measured in Brood War
31 <span title="no known changes as of v1.16.1">version≥1.08</span>.
32 </p>
33
34 <:
35 sub coltoggle {
36         my ($name, $id) = @_;
37         return sprintf(
38                 (defined $get{order} ? $get{order} eq $id : !$id) ? '%2$s ▼'
39                         : '<a href="?%s">%s</a>',
40                 $id && "order=$id", $name
41         );
42 }
43 :><table class="units">
44 <thead><tr>
45         <th></th>
46         <th><:= coltoggle('name', '') :></th>
47         <th class="val min"><img src="/minerals.png" alt="min"></th>
48         <th class="val gas"><img src="/gas.png" alt="gas"></th>
49         <th class="val time"><:= coltoggle(qw'build cost') :></th>
50         <th class="unit" colspan="2"><:= coltoggle(qw'size size') :></th>
51         <th class="val unit-hp">HP</th>
52         <th class="val unit-shield">shield</th>
53         <th class="val unit-armor">armor</th>
54         <th class="val hurt" colspan="2">ground</th>
55         <th class="val hurt" colspan="2">air</th>
56         <th class="val unit-range">range</th>
57         <th class="val unit-sight">sight</th>
58         <th class="val unit-speed">speed</th>
59         <th class="unit-magic">specials</th>
60 </tr></thead>
61 <:
62 sub showrange {
63         my ($min, $max) = @_;
64         $_ = int($_ + .5) for $min, $max;  # round halves up
65         return $min == $max ? $min : "$min-$max";
66 }
67
68 sub showval {
69         my ($row, @elements) = @_;
70         my ($min, $max);
71
72         my $value = $row;
73         $value = ref $value eq 'HASH' && $value->{$_} or last for @elements;
74         if (ref $value eq 'ARRAY') {
75                 $min = $value->[0];
76                 $max = $value->[-1];
77         }
78         else {
79                 $min = $max = $value;
80         }
81         defined $min or return '';
82
83         if ($row->{upgrade}) {
84                 for (@{ $row->{upgrade} }) {
85                         my $increase = $_ or next;
86                         $increase = ref $increase eq 'HASH' && $increase->{$_} or last for @elements;
87                         $increase = $increase->[-1] if ref $increase eq 'ARRAY';
88                         $max += $increase if $increase;
89                 }
90         }
91
92         if ($elements[0] eq 'attack' and $elements[1] ne 'range' and $elements[2] eq 'dps') {{
93                 my $attack = $row->{ $elements[0] }->{ $elements[1] };
94                 ref $attack or $attack = $row->{ $elements[0] }->{$attack}; # follow
95                 my $type = $attack->{type} or next;
96                 if ($type eq 'explosive') {
97                         $min /= 2;
98                 }
99                 elsif ($type eq 'implosive') {
100                         $min /= 4;
101                 }
102         }}
103         return showrange($min, $max);
104 }
105
106         sub showattack {
107                 my ($row, $area) = @_;
108                 my $attack = $row->{attack}->{$area};
109                 if (not ref $attack) {
110                         # reference to another area
111                         $area = $attack;
112                         $attack = $row->{attack}->{$area};
113                 }
114
115                 return '<td colspan="2" class="hurt">' unless $attack;
116
117                 my $tagbase = '<td class="val hurt';
118                 if (ref $attack and $attack->{type}) {
119                         if ($attack->{type} eq 'explosive') {
120                                 $tagbase .= ' unit-l';
121                         }
122                         elsif ($attack->{type} eq 'implosive') {
123                                 $tagbase .= ' unit-s';
124                         }
125                 }
126                 $tagbase .= '">';
127
128                 my $out = showval($row, 'attack', $area, 'damage');
129                 $out .= '<span class="unit-splash">+</span>' if $attack->{splash};
130                 $attack->{dps} = $attack->{cooldown} && [
131                         map { 24 * $_ / $attack->{cooldown} * ($attack->{count} // 1) }
132                         map { ref $_ ? @{$_} : $_ }
133                         $attack->{damage}
134                         #TODO: upgrade (zergling)
135                 ];
136                 $out .= '<td class="val hurt hurtrel">' . showval($row, 'attack', $area, 'dps');
137                 return $tagbase . $out;
138         }
139
140         sub showmagic {
141                 my ($row) = @_;
142                 my $specials = $row->{special} or return '';
143                 return join ' ', map {
144                         sprintf '<span%s title="%s">%s</span>',
145                                 $_->{duration} < 0 && ' class="magic-perma"',
146                                 join('',
147                                         $_->{name},
148                                         $_->{desc} ? ": $_->{desc}" : '',
149                                         $_->{range} || $_->{cost} ? sprintf(' (%s)', join ', ',
150                                                 $_->{range} ? "range $_->{range}" : (),
151                                                 $_->{cost} ? sprintf('cost %.0f%%',
152                                                         100 * $_->{cost} / $row->{energy}
153                                                 ) : (),
154                                         ) : '',
155                                 ),
156                                 sprintf($_->{build} ? '(%s)' : '%s', $_->{abbr}),
157                 } grep { defined $_->{abbr} } @{$specials};
158         }
159
160         sub showunitcols {
161                 my ($row) = @_;
162                 local $_ = $row;
163                 $_->{hp} += $_->{shield} if $_->{shield};
164                 my $suitchar = '';
165                 if ($_->{suit}) {
166                         $suitchar = [qw/? s m l/]->[$_->{suit}];
167                 }
168
169                 return (
170                         '<td class="val min">' . ($_->{min} // ''),
171                         '<td class="val gas">' . ($_->{gas} || ''),
172                         !defined $_->{build} ? '<td>' : sprintf('<td class="val time">%s%.0f',
173                                 !!$_->{base} && '<span class="unit-composed">+</span>',
174                                 $_->{build} || '0',
175                         ),
176                         !$suitchar ? '<td>' : sprintf('<td class="unit unit-%s">%s', $suitchar, ucfirst $suitchar),
177                         '<td class="val unit">' . join('',
178                                 defined $_->{unit} && $_->{unit} == .5 ? '½' : $_->{unit},
179                                 defined $_->{organic} && sprintf(
180                                         '<span class="unit-%s" title="%s">%s</span>',
181                                         $_->{organic} ? 'o' : 'u',
182                                         $_->{organic} ? 'organic' : 'mechanic',
183                                         $_->{organic} ? 'o' : 'm',
184                                 ),
185                         ),
186                         '<td class="val unit-hp">' . $_->{hp} // '',
187                         '<td class="val unit-shield">' . (
188                                 $_->{shield} ? sprintf('%.0f%%', 100 * $_->{shield} / $_->{hp}) : '&nbsp;'
189                         ),
190                         '<td class="val unit-armor">' . showval($_, 'armor'),
191                         showattack($_, 'ground'),
192                         showattack($_, 'air'),
193                         '<td class="val unit-range">' . showval($_, 'attack', 'range'),
194                         '<td class="val unit-sight">' . sprintf(
195                                 $_->{detect} ? '<strong class="unit-detect">%s</strong>' : '%s',
196                                 showval($_, 'sight')
197                         ),
198                         '<td class="val unit-speed">' . showval($_, 'speed'),
199                         '<td class="unit-magic">' . showmagic($_),
200                         "\n"
201                 );
202         }
203
204         my $units = do $datafile;
205         die "Cannot open unit data: $_\n" for $@ || $! || ();
206         my $grouped = 1;  # race headers
207         if (exists $get{order}) {
208                 $grouped = 0;
209                 $get{order} ||= '';
210                 if ($get{order} eq 'size') {
211                         $_->{order} = $_->{unit}*8 + $_->{suit} + $_->{hp}/512 + $_->{min}/8192 for @$units;
212                 }
213                 elsif ($get{order} eq 'cost') {
214                         $_->{order} = $_->{gas}*1.5 + $_->{min} + $_->{unit}/8 + $_->{build}/256/8 for @$units;
215                 }
216                 else {
217                         $units->[$_]->{order} = $_ for 0 .. $#$units;
218                 }
219         }
220         my @rows = @{$units};
221         @rows = sort {$a->{order} <=> $b->{order}} @rows unless $grouped;
222
223         my ($race, $cat) = ('', '');
224         for (@rows) {
225                 if ($grouped) {
226                         printf '<tbody id="%s"><tr class="race"><th colspan="18"><h2>%s</h2>'."\n",
227                                 $race = $_->{race}, ucfirst $race
228                                         unless $race eq $_->{race};
229                 }
230                 else {
231                         $_->{cat} = $_->{race};
232                 }
233
234                 print(
235                         '<tr>',
236                         '<th class="cat">', $cat ne $_->{cat} && ($cat = $_->{cat}),
237                         '<td>', $_->{name},
238                         showunitcols($_),
239                 );
240
241                 for my $subrow (@{ $_->{special} }) {
242                         $subrow->{alt} or next;
243                         print(
244                                 '<tr class="alt"><td class="cat"><td>', $subrow->{alt},
245                                 showunitcols($subrow),
246                         );
247                 }
248         }
249 :>
250 </table>
251
252 <div class="legend">
253 <h2>Legend</h2>
254
255 <dl>
256 <dt>cost
257         <dd>minerals+gas required to create one unit
258         <dd>includes total expenses if based on existing units
259 <dt>build
260         <dd>relative time needed to create at least one unit
261         <dd>excludes construction of dependencies such as buildings
262                 and <span class="unit-composed">+</span>parent units
263 <dt>size
264         <dd>affected by <span class="unit unit-s">S</span>mall,
265                 <span class="unit unit-m">M</span>edium,
266                 or <span class="unit unit-l">L</span>arge unit damage
267         <dd>number of command points taken per unit
268         <dd><span class="unit unit-o">o</span>rganic/<span class="unit unit-u">m</span>echanic unit
269 <dt>HP<dd>
270         total number of hitpoints (including shields)
271 <dt>shield
272         <dd>percentage of HP in shields
273         <dd>shields always take full damage, irrelevant of unit size
274         <dd>does not take armor bonuses, but upgrades can decrease damage to any shield hit by upto 3
275 <dt>armor
276         <dd>base unit armor
277         <dd>can be increased by upto 3 at various facilities
278         <dd>each point decreases damage per hit by one, upto a minimum of ½
279         <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>
280 <dt>ground/air
281         <dd>damage done per single attack against ground/air units
282         <dd>2nd column indicates relative amount of damage done in
283                 1 second of fastest game time
284         <dd>splash damage<span class="unit-splash">+</span> hits nearby objects as well
285         <dd><span class="hurt unit-l">explosive</span> damage does only
286                 50% damage to small units, 75% to medium, 100% to large
287         <dd><span class="hurt unit-s">concussive/plasma</span> damage does
288                 25% to large, 50% medium, 100% to small units
289 <dt>sight
290         <dd>range in which the unit detects other units
291         <dd><strong class="unit-detect">emphasis</strong> indicates ability to detect cloaked units
292 <dt>range
293         <dd>maximum range of weapon (note siege tank also has a minimum range)
294 <dt>speed
295         <dd>relative speed of movement (when in full motion, startup speed ignored)
296 <dt>specials
297         <dd>special abilities are usually casted manually, but some are <span class="magic-perma">always active</span>
298         <dd>parentheses () indicate that it needs to be researched first
299         <dd>hover for description
300         <dd>range is maximum range required to activate
301         <dd>cost is percentage of total energy lost
302 </dl>
303
304 <p>
305 When two values are given (1-2), second value indicates attribute after all
306 possible upgrades.
307 </p>
308
309 </div>
310