Fix wood growing

This commit is contained in:
Anton Vakhrushev 2019-09-16 16:11:18 +03:00
parent 533f05c88f
commit bd1e38e4c2
3 changed files with 19 additions and 14 deletions

View File

@ -11,7 +11,7 @@ class BuildWoodMillCommand < Command
def start(world : World) : Int32 def start(world : World) : Int32
printf " << start build mill at [%d:%d]\n", @point.x, @point.y printf " << start build mill at [%d:%d]\n", @point.x, @point.y
return BASE_TIME BASE_TIME
end end
def finish(world : World) def finish(world : World)
@ -37,7 +37,7 @@ class GetWoodCommand < Command
else else
printf " << no wood tile\n" printf " << no wood tile\n"
@wood = 0 @wood = 0
return BASE_TIME BASE_TIME
end end
end end
@ -49,7 +49,7 @@ class GetWoodCommand < Command
@point.x, @point.y, @point.x, @point.y,
dist, @wood, dist, @wood,
wood_point.x, wood_point.y wood_point.x, wood_point.y
return BASE_TIME + 2 * dist BASE_TIME + 2 * dist
end end
def finish(world : World) def finish(world : World)
@ -73,7 +73,7 @@ class BuildForesterHouseCommand < Command
def start(world : World) : Int32 def start(world : World) : Int32
printf " >> start build forester house at [%d:%d]\n", @point.x, @point.y printf " >> start build forester house at [%d:%d]\n", @point.x, @point.y
return BASE_TIME BASE_TIME
end end
def finish(world : World) def finish(world : World)
@ -99,9 +99,9 @@ class GrowWoodCommand < Command
if !@wood_tile.nil? if !@wood_tile.nil?
calc_time(@wood_tile.as(Tile)) calc_time(@wood_tile.as(Tile))
else else
printf "no wood tile\n" printf " >> no wood tile\n"
@wood = 0 @wood = 0
return BASE_TIME BASE_TIME
end end
end end
@ -112,12 +112,13 @@ class GrowWoodCommand < Command
@point.x, @point.y, @point.x, @point.y,
dist, dist,
wood_point.x, wood_point.y wood_point.x, wood_point.y
return BASE_TIME + 2 * dist BASE_TIME + 2 * dist
end end
def finish(world : World) def finish(world : World)
printf " >> finish grow wood at [%d,%d]\n", @point.x, @point.y printf " >> finish grow wood at [%d,%d]\n", @point.x, @point.y
if !@wood_tile.nil? if !@wood_tile.nil?
printf " >> finish grow wood for %d\n", BASE_WOOD
@wood_tile.as(Tile).charge(BASE_WOOD) @wood_tile.as(Tile).charge(BASE_WOOD)
end end
world.push(GrowWoodCommand.new(@point)) world.push(GrowWoodCommand.new(@point))
@ -125,7 +126,7 @@ class GrowWoodCommand < Command
private def nearest_wood(world : World) private def nearest_wood(world : World)
world.map.nearest_tile @point do |tile| world.map.nearest_tile @point do |tile|
tile.letter == 'f' && tile.cur < tile.cur tile.letter == 'f' && tile.cur < tile.cap
end end
end end
end end

View File

@ -37,15 +37,15 @@ abstract class Tile
if value >= @cur if value >= @cur
wd = @cur wd = @cur
@cur = 0 @cur = 0
return wd wd
else else
@cur -= value @cur -= value
return value value
end end
end end
def charge(value) def charge(value)
if value + @cur > @cap if (value + @cur) > @cap
@cur = @cap @cur = @cap
else else
@cur += value @cur += value

View File

@ -60,7 +60,11 @@ end
w = World.new w = World.new
w.map.print w.map.print
w.push(BuildWoodMillCommand.new(Point.new(0, 0))) w.push(BuildWoodMillCommand.new(Point.new(2, 1)))
w.push(BuildForesterHouseCommand.new(Point.new(0, 0))) w.push(BuildWoodMillCommand.new(Point.new(2, 3)))
w.run(60) w.push(BuildForesterHouseCommand.new(Point.new(2, 0)))
w.push(BuildForesterHouseCommand.new(Point.new(1, 2)))
w.push(BuildForesterHouseCommand.new(Point.new(3, 2)))
w.run(120)
w.map.print
printf "Wood: %d\n", w.resources.wood printf "Wood: %d\n", w.resources.wood