...
Add a special case.
if (unit.relations.birth_time < 0) then result = -1
else result = (unit.relations.birth_time % 10)
or
result = (unit.relations.birth_time < 0) ? -1 : (unit.relations.birth_time % 10)
I have an update for my ruby version using a change to the if statement as suggested by 0x517A5D to make the code easier to understand by removing the ambiguity over the debatable interpretations over how modulus should function with negative numbers.
I gave it a usage help string and configurable repeat time (default of once a month). It notifies when it makes a fix. I wrote the help string with the intention of saving the file in hack/scripts/fix/growthbug.rb
# growthbug enable [#d|w|m] |disable|status|now|help
DAY = 1200
USAGE = <<ENDUSAGE
Usage:
fix/growthbug enable|start [#d|#w|#m]
start auto fix process with #d (number of days),
or #w (number of weeks), or #m (number of months),
or the default of one month if omitted.
fix/growthbug disable|stop|end stop auto fix process.
fix/growthbug now run fix process once.
fix/growthbug [help|?] show this help.
fix/growthbug status
show auto fix status (running? not running?).
status is also shown with help, and when
starting or stopping auto fix.
(all options are case insensitive.)
ENDUSAGE
class FixGrowth
def initialize() @running = false end
def status() @running ? 'FixGrowth: Running' : 'FixGrowth: Stopped' end
def process
count = 0
df.world.units.active.each do |unit|
r = unit.relations.birth_time % 10
if unit.relations.birth_time > 0 and r > 0 then
unit.relations.birth_time -= r
count += 1
end
end
puts "FixGrowth: Fixed #{count} units so that their size will grow/thicken." unless count == 0
end
def start( time=DAY*28 )
self.stop if @running
@onupdate = df.onupdate_register('fixgrowth', time) { process }
@running = true
end
def stop
df.onupdate_unregister(@onupdate)
@running = false
end
end
$FixGrowth = FixGrowth.new unless $FixGrowth
run = :help
time = DAY * 28
$script_args.each do |arg|
case arg.downcase
when 'now'
run = :now
when 'enable', 'start'
run = :enable
when 'disable', 'end', 'stop'
run = :disable
when 'status'
run = :status
when 'help', '?'
run = :help
else
if arg =~ /^([1-9]\d*)([dD|wW|mM])$/
time = $~[1].to_i
case $~[2]
when 'd', 'D'
time *= DAY
when 'w', 'W'
time *= DAY * 7
when 'm', 'M'
time *= DAY * 28
end
end
end
end
case run
when :now
$FixGrowth.process
when :enable
$FixGrowth.start(time)
puts $FixGrowth.status
when :disable
$FixGrowth.stop
puts $FixGrowth.status
when :help
puts $FixGrowth.status
puts USAGE
end
If we have a better name for it, I am open to that. I thought it would be best to put the script in the fix directory. I would consider using the suggested name fixgrowthbug except I don't want to conflict with the lua version.