From fb139b2334437ba2182e7c189ed016d84f6ea828 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sun, 15 Sep 2019 14:17:38 +0300 Subject: [PATCH] Improve wood mill command --- src/map.cr | 24 +++++++++++++++++++++++- src/village.cr | 50 +++++++++++++++++++------------------------------- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/src/map.cr b/src/map.cr index 141c551..e170128 100644 --- a/src/map.cr +++ b/src/map.cr @@ -9,6 +9,10 @@ struct Point def y @y end + + def distance(p : Point) : Int32 + return (p.x - @x).abs + (p.y - @y).abs + end end abstract class Tile @@ -79,7 +83,7 @@ class Map end def get(point : Point) : Tile - @data[key(Point.new(x, y))] + @data[key(point)] end def set(tile : Tile) @@ -90,6 +94,24 @@ class Map @data[key(point)] = tile end + def nearest_wood(point : Point) : Point | Nil + p = nil + d = 99999 + (0...4).each do |x| + (0...4).each do |y| + tile = self.get(Point.new(x, y)) + if tile.letter == 'f' + td = Point.new(x, y).distance(point) + if td < d + d = td + p = Point.new(x, y) + end + end + end + end + p + end + def print (0...4).each do |x| (0...4).each do |y| diff --git a/src/village.cr b/src/village.cr index 13d8831..02cd0a0 100644 --- a/src/village.cr +++ b/src/village.cr @@ -30,12 +30,20 @@ class BuildWoodMillCommand < Command def run(ts : Int32, world : World) printf "build mill at [%d,%d]\n", @point.x, @point.y - world.map.set(WoodMillTile.new(@point)) - world.push(ts + 5, GetWoodCommand.new) + mill = WoodMillTile.new(@point) + world.map.set(mill) + 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 -class BuildForesterHouseCommand < Command +class GetWoodCommand < Command def initialize(@point : Point) end @@ -43,36 +51,17 @@ class BuildForesterHouseCommand < Command return true end - def run(ts : Int32, world : World) - printf "build forester house at [%d,%d]\n", @point.x, @point.y - world.map.set(ForesterHouseTile.new(@point)) - world.push(ts + 10, GrowWoodCommand.new) - end -end - -class GetWoodCommand < Command - def supports?(world : World) : Bool - return true - end - def run(ts : Int32, world : World) res = world.resources res.add_wood(10) - puts "get wood" - c = GetWoodCommand.new - world.push(ts + 5, c) - end -end - -class GrowWoodCommand < Command - def supports?(world : World) : Bool - return true - end - - def run(ts : Int32, world : World) - res = world.resources - puts "grow wood" - world.push(ts + 5, GetWoodCommand.new) + 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 @@ -116,6 +105,5 @@ end w = World.new w.map.print w.push(0, BuildWoodMillCommand.new(Point.new(0, 0))) -w.push(0, BuildForesterHouseCommand.new(Point.new(2, 0))) w.run(20) w.map.print