sc: rename showrange() to prepare for non-integer reuse
[sheet.git] / sc.plp
diff --git a/sc.plp b/sc.plp
index 5d82a8d0ba657cb66c1d525c1c900f5f854d32cb..5454b637d02d7e0f4f7d59d733df64d58ebdcc75 100644 (file)
--- a/sc.plp
+++ b/sc.plp
@@ -114,11 +114,33 @@ sub coltoggle {
 sub showrange {
        my ($min, $max) = @_;
        return '' if not defined $min;
-       $_ &&= int($_ + .5) for $min, $max;  # round halves up
-       return $min || '-' if not defined $max or $min == $max;
+       return $min || '-' if !$max or $min == $max;
        return "$min-$max";
 }
 
+sub showrangeint {
+       $_ &&= int($_ + .5) for @_;  # round halves up
+       return showrange(@_);
+}
+
+       sub showcost {
+               my ($row, $unit) = @_;
+               return join(' ',
+                       sprintf('cost %s%%', join '-',
+                               map { $_ && sprintf '%.0f', 100 * $row->{cost} / $_ } grep { defined $_ }
+                               $unit->{energy},
+                               $unit->{upgraded}->{energy},
+                               $unit->{capacity},
+                               $unit->{upgraded}->{capacity},
+                       ),
+                       !defined $row->{maint} ? () : sprintf('+%s%%/s', join '-',
+                               map { sprintf '%.1f', 100 * $row->{maint} / $_ } grep $_,
+                               $unit->{capacity},
+                               $unit->{upgraded}->{capacity},
+                       ),
+               );
+       }
+
        sub showattack {
                my ($row, $area) = @_;
                my $attack = $row->{attack}->[$area]
@@ -130,8 +152,10 @@ sub showrange {
                $maxdamage += ($upattack->{upgrade} // $attack->{upgrade}) * 3;
 
                my $out = '<td class="val hurt">';
+               $out .= sprintf '<span title="%s">¤</span> ', showcost($attack, $row)
+                       if $attack->{cost};
                $out .= sprintf('<small>%s× </small>',
-                       showrange($attack->{count}, $upattack->{count}),
+                       showrangeint($attack->{count}, $upattack->{count}),
                ) if $attack->{count} > 1;
                $out .= '<span class="unit-l" title="explosive">*</span>'
                        if $attack->{type} eq 'explosive';
@@ -149,7 +173,7 @@ sub showrange {
                        } join '_', @bonus),
                        join(', ', map {(
                                sprintf('+%s vs %s',
-                                       showrange(
+                                       showrangeint(
                                                $attack->{bonus}->{$_},
                                                $attack->{bonus}->{$_} + $attack->{bonus}->{"-$_"} * 3,
                                        ),
@@ -162,7 +186,7 @@ sub showrange {
                        if $attack->{type} eq 'projectile';
 
                $out .= sprintf '<span title="%s">', $attack->{name} if $attack->{name};
-               $out .= showrange($damage, $maxdamage);
+               $out .= showrangeint($damage, $maxdamage);
                $out .= '</span>' if $attack->{name};
                $out .= sprintf('<span class="unit-splash" title="%s">%s</span>',
                        $attack->{splash} eq 'line' ? ('linear', '+') : ('splash', '⁜')
@@ -171,7 +195,7 @@ sub showrange {
                $out .= '<td class="val hurt hurtrel">';
                if ($attack->{dps}) {
                        # precalculated dps, do not touch
-                       $out .= showrange($attack->{dps}->[0],
+                       $out .= showrangeint($attack->{dps}->[0],
                                $upattack->{dps}->[-1] // $attack->{dps}->[-1]
                        );
                }
@@ -193,14 +217,14 @@ sub showrange {
                        }
                        $maxdamage *= ($upattack->{count} // $attack->{count} // 1)
                                    / ($upattack->{cooldown} // $attack->{cooldown});
-                       $out .= showrange($damage, $maxdamage);
+                       $out .= showrangeint($damage, $maxdamage);
                }
 
                $out .= '<td class="unit hurt-g">' . '▽' x !!($attack->{anti} & 1);
                $out .= '<td class="unit hurt-a">' . '△' x !!($attack->{anti} & 2);
 
                $out .= '<td class="val unit-range">' .
-                       showrange($attack->{range}, $upattack->{range});
+                       showrangeint($attack->{range}, $upattack->{range});
 
                return $out;
        }
@@ -217,12 +241,7 @@ sub showrange {
                                        (map { $_ && " ($_)" } join ', ',
                                                #TODO: apply upgrades
                                                $_->{range} ? "range $_->{range}" : (),
-                                               $_->{cost} ? sprintf('cost %.0f%%%s',
-                                                       100 * $_->{cost} / $row->{energy},
-                                                       defined $_->{maint} && sprintf('+%.1f%%/s',
-                                                               100 * $_->{maint} / $row->{energy},
-                                                       ),
-                                               ) :
+                                               $_->{cost} ? showcost($_, $row) :
                                                $_->{cooldown} ? "cooldown $_->{cooldown}s" : (),
                                        ),
                                ),
@@ -288,14 +307,14 @@ sub showrange {
                                100 * $_->{shield} / $_->{hp}
                        ) : '<td colspan=2',
                        ' class="val unit-armor">' .
-                               showrange($_->{armor}, $_->{upgraded}->{armor}),
+                               showrangeint($_->{armor}, $_->{upgraded}->{armor}),
                        showattack($_, 0),
                        '<td class="val unit-sight">' . sprintf(
                                $_->{detect} ? '<strong class="unit-detect">%s</strong>' : '%s',
-                               showrange($_->{sight}, $_->{upgraded}->{sight})
+                               showrangeint($_->{sight}, $_->{upgraded}->{sight})
                        ),
                        '<td class="val unit-speed">' .
-                               showrange($_->{speed}, $_->{upgraded}->{speed}),
+                               showrangeint($_->{speed}, $_->{upgraded}->{speed}),
                        $_->{attr}->{jump}
                                && qq'<span class="unit unit-jump" title="$_->{attr}->{jump}">↕</span>',
                        '<td class="unit-magic">' . showmagic($_),
@@ -437,7 +456,7 @@ if ($scver{major} > 1) {
        <dd>targets <span class="hurt-g">▽</span>&nbsp;ground
                and/or  <span class="hurt-a">△</span>&nbsp;air
 <dt>range
-       <dd>maximum range of weapon (note siege tank also has a minimum range)
+       <dd>maximum range of weapon (note Sieged Tank also has a minimum range)
 <dt>sight
        <dd>range in which the unit detects other units
        <dd><strong class="unit-detect">emphasis</strong> indicates ability to detect cloaked units
@@ -448,7 +467,7 @@ if ($scver{major} > 1) {
        <dd><span class="magic-perma">passive</span> abilities are always enabled
        <dd>hover for description
        <dd>range is maximum range required to activate
-       <dd>cost is percentage of total energy lost
+       <dd>cost is energy loss percentage on spawn and when fully charged
 </dl>
 
 <p>