From e8f7453a8f53ef4cc3c1c9c2e8d8b95f970ab9ae Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Sun, 13 Oct 2019 17:29:20 +0300 Subject: [PATCH] Change mine command with storehouse calc --- spec/command_spec.cr | 8 +++++--- src/game/building.cr | 6 +++++- src/game/command.cr | 14 ++++++++++++-- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/spec/command_spec.cr b/spec/command_spec.cr index 57bb5bc..e9238a3 100644 --- a/spec/command_spec.cr +++ b/spec/command_spec.cr @@ -13,7 +13,7 @@ module Game::TestCommand Point.new(0, 0), Building.new( Building::Type::StartPoint, - storage: 200 + storage: 200, roles: [Building::Role::Storehouse] ) ) map.set DepositTile.new( @@ -79,8 +79,10 @@ module Game::TestCommand it "should complete mining command" do world = World.new create_map_with_resource command = MineCommand.new Point.new(0, 1), once: true - world.push command - world.run 20 + time_point = (20 + 1 * 2 + 1 * 2).to_i64 + done_at = world.push command + done_at.should eq time_point + world.run time_point # Check world resources world.resources[Resource::Type::Crystals].should eq 40 # Check tile deposit diff --git a/src/game/building.cr b/src/game/building.cr index 9600b02..dd57414 100644 --- a/src/game/building.cr +++ b/src/game/building.cr @@ -63,7 +63,7 @@ module Game storage : Capacity | Nil = nil ) @name = name != "" ? name : @type.to_s - @roles = roles.nil? ? Array(Role).new : roles + @roles = roles.nil? ? Array(Role).new : roles.as(Array(Role)) @construction = construction.nil? ? Construction.immediatly : construction.as(Construction) @production = production @mining = mining @@ -79,5 +79,9 @@ module Game getter mining getter restoration getter storage + + def has_role(role : Role) : Bool + @roles.includes? role + end end end diff --git a/src/game/command.cr b/src/game/command.cr index f8dbfc6..2c9cb53 100644 --- a/src/game/command.cr +++ b/src/game/command.cr @@ -59,11 +59,14 @@ module Game end resource = mining.resource deposit_tile = nearest_deposit(world, resource.type) - if deposit_tile + stock_tile = nearest_stock(world) + if deposit_tile && stock_tile mined_amount = deposit_tile.dep.dec(resource.amount) @holded = resource.type.to_res mined_amount + mining.ts + 2 * tile.point.distance(stock_tile.point) + 2 * tile.point.distance(deposit_tile.point) + else + mining.ts end - mining.ts end def finish(world : World) @@ -82,6 +85,13 @@ module Game end tile.as?(DepositTile) end + + private def nearest_stock(world : World) : BuildingTile? + tile = world.map.nearest_tile @point do |tile| + tile.is_a?(BuildingTile) && tile.building.has_role Building::Role::Storehouse + end + tile.as?(BuildingTile) + end end class RestoreCommand < Command