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 abstract class Tile
def initialize(@point : Point) def initialize(@point : Point)
@cap = 0
@cur = 0
end end
def point def point
@point @point
end 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 abstract def letter : Char
end end
@ -38,20 +63,6 @@ class WoodTile < Tile
@cur = cap @cur = cap
end 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 def letter : Char
'f' 'f'
end end
@ -100,7 +111,7 @@ class Map
(0...4).each do |x| (0...4).each do |x|
(0...4).each do |y| (0...4).each do |y|
tile = self.get(Point.new(x, 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) td = Point.new(x, y).distance(point)
if td < d if td < d
d = td d = td

View File

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