Improve wood getting

This commit is contained in:
Anton Vakhrushev 2019-09-16 11:20:16 +03:00
parent 7e2f841099
commit b30c0c4611
2 changed files with 48 additions and 38 deletions

View File

@ -17,12 +17,37 @@ end
abstract class Tile
def initialize(@point : Point)
@cap = 0
@cur = 0
end
def point
@point
end
def cur
@cur
end
def withdraw(value)
if value >= @cur
wd = @cur
@cur = 0
return wd
else
@cur -= value
return value
end
end
def charge(value)
if value + @cur > @cap
@cur = @cap
else
@cur += value
end
end
abstract def letter : Char
end
@ -38,20 +63,6 @@ class WoodTile < Tile
@cur = cap
end
def cap
@cap
end
def inc(v : Int32)
@cur += v
if @cur < 0
@cur = 0
end
if @cur > @cap
@cur = @cap
end
end
def letter : Char
'f'
end
@ -100,7 +111,7 @@ class Map
(0...4).each do |x|
(0...4).each do |y|
tile = self.get(Point.new(x, y))
if tile.letter == 'f'
if tile.letter == 'f' && tile.cur > 0
td = Point.new(x, y).distance(point)
if td < d
d = td

View File

@ -25,7 +25,7 @@ class BuildWoodMillCommand < Command
end
def start(world : World) : Int32
printf "start build mill at [%d, (%d:%d)]\n", world.ts, @point.x, @point.y
printf "start build mill at [%d:%d]\n", @point.x, @point.y
return 30
end
@ -39,36 +39,34 @@ end
class GetWoodCommand < Command
BASE_TIME = 5
BASE_WOOD = 20
BASE_WOOD = 80
def initialize(@point : Point)
@wood = 0
end
def start(world : World)
def start(world : World) : Int32
wood_point = world.map.nearest_wood(@point)
# if !wood_point.nil?
# printf "cut down wood at [%d,%d]\n", wood_point.x, wood_point.y
# dist = @point.distance(wood_point)
# world.push(GetWoodCommand.new(@point))
# else
# printf "no wood tile\n"
# end
return BASE_TIME
if !wood_point.nil?
dist = @point.distance(wood_point)
tile = world.map.get(wood_point)
@wood = tile.withdraw(BASE_WOOD)
printf "start cut down wood at [%d,%d] -> %d -> %d -> [%d,%d]\n",
@point.x, @point.y,
dist, @wood,
wood_point.x, wood_point.y
return BASE_TIME + 2 * dist
else
printf "no wood tile\n"
@wood = 0
return BASE_TIME
end
end
def finish(world : World)
printf "finish cut down wood at [%d,%d]\n", @point.x, @point.y
world.resources.add_wood(@wood)
world.push(GetWoodCommand.new(@point))
# res = world.resources
# res.add_wood(10)
# wood_point = world.map.nearest_wood(@point)
# if !wood_point.nil?
# printf "cut down wood at [%d,%d]\n", wood_point.x, wood_point.y
# dist = @point.distance(wood_point)
# world.push(ts + dist + 5, GetWoodCommand.new(@point))
# else
# printf "no wood tile\n"
# end
end
end
@ -80,7 +78,7 @@ class World
@queue = App::CommandQueue.new
end
def ts
private def ts
@ts
end
@ -116,4 +114,5 @@ end
w = World.new
w.map.print
w.push(BuildWoodMillCommand.new(Point.new(0, 0)))
w.run(45)
w.run(120)
printf "Wood: %d\n", w.resources.wood