get wood command
This commit is contained in:
parent
fb139b2334
commit
7e2f841099
@ -16,62 +16,74 @@ class Resources
|
|||||||
end
|
end
|
||||||
|
|
||||||
abstract class Command
|
abstract class Command
|
||||||
abstract def supports?(world : World) : Bool
|
abstract def start(world : World)
|
||||||
abstract def run(ts : Int32, world : World)
|
abstract def finish(world : World)
|
||||||
end
|
end
|
||||||
|
|
||||||
class BuildWoodMillCommand < Command
|
class BuildWoodMillCommand < Command
|
||||||
def initialize(@point : Point)
|
def initialize(@point : Point)
|
||||||
end
|
end
|
||||||
|
|
||||||
def supports?(world : World) : Bool
|
def start(world : World) : Int32
|
||||||
return true
|
printf "start build mill at [%d, (%d:%d)]\n", world.ts, @point.x, @point.y
|
||||||
|
return 30
|
||||||
end
|
end
|
||||||
|
|
||||||
def run(ts : Int32, world : World)
|
def finish(world : World)
|
||||||
printf "build mill at [%d,%d]\n", @point.x, @point.y
|
printf "finish build mill at [%d,%d]\n", @point.x, @point.y
|
||||||
mill = WoodMillTile.new(@point)
|
mill = WoodMillTile.new(@point)
|
||||||
world.map.set(mill)
|
world.map.set(mill)
|
||||||
wood_point = world.map.nearest_wood(@point)
|
world.push(GetWoodCommand.new(@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
|
||||||
|
|
||||||
class GetWoodCommand < Command
|
class GetWoodCommand < Command
|
||||||
|
BASE_TIME = 5
|
||||||
|
BASE_WOOD = 20
|
||||||
|
|
||||||
def initialize(@point : Point)
|
def initialize(@point : Point)
|
||||||
|
@wood = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
def supports?(world : World) : Bool
|
def start(world : World)
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
def run(ts : Int32, world : World)
|
|
||||||
res = world.resources
|
|
||||||
res.add_wood(10)
|
|
||||||
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
|
# printf "cut down wood at [%d,%d]\n", wood_point.x, wood_point.y
|
||||||
dist = @point.distance(wood_point)
|
# dist = @point.distance(wood_point)
|
||||||
world.push(ts + dist + 5, GetWoodCommand.new(@point))
|
# world.push(GetWoodCommand.new(@point))
|
||||||
else
|
# else
|
||||||
printf "no wood tile\n"
|
# printf "no wood tile\n"
|
||||||
end
|
# end
|
||||||
|
return BASE_TIME
|
||||||
|
end
|
||||||
|
|
||||||
|
def finish(world : World)
|
||||||
|
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
|
||||||
|
|
||||||
class World
|
class World
|
||||||
def initialize
|
def initialize
|
||||||
|
@ts = 0
|
||||||
@resources = Resources.new
|
@resources = Resources.new
|
||||||
@map = Map.new
|
@map = Map.new
|
||||||
@queue = App::CommandQueue.new
|
@queue = App::CommandQueue.new
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def ts
|
||||||
|
@ts
|
||||||
|
end
|
||||||
|
|
||||||
def resources
|
def resources
|
||||||
@resources
|
@resources
|
||||||
end
|
end
|
||||||
@ -80,13 +92,11 @@ class World
|
|||||||
@map
|
@map
|
||||||
end
|
end
|
||||||
|
|
||||||
def push(ts : Int32, command : Command) : Bool
|
def push(command : Command)
|
||||||
if !command.supports?(self)
|
dur = command.start(self)
|
||||||
return false
|
done_at = @ts + dur
|
||||||
end
|
printf "world:plan `%s` at %d\n", typeof(command), done_at
|
||||||
printf "push command %s, %d\n", typeof(command), ts
|
@queue.push(done_at, command)
|
||||||
@queue.push(ts, command)
|
|
||||||
true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def run(ts : Int32)
|
def run(ts : Int32)
|
||||||
@ -95,15 +105,15 @@ class World
|
|||||||
if item.nil?
|
if item.nil?
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
printf "pop command %d\n", item[:ts]
|
cmd_ts, cmd = item[:ts], item[:cmd]
|
||||||
item[:cmd].run(item[:ts], self)
|
@ts = cmd_ts
|
||||||
|
printf "world:finish `%s` at %d\n", typeof(cmd), cmd_ts
|
||||||
|
cmd.finish(self)
|
||||||
end
|
end
|
||||||
printf "Wood: %d\n", @resources.wood
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
w = World.new
|
w = World.new
|
||||||
w.map.print
|
w.map.print
|
||||||
w.push(0, BuildWoodMillCommand.new(Point.new(0, 0)))
|
w.push(BuildWoodMillCommand.new(Point.new(0, 0)))
|
||||||
w.run(20)
|
w.run(45)
|
||||||
w.map.print
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user