Change mine command with storehouse calc

This commit is contained in:
Anton Vakhrushev 2019-10-13 17:29:20 +03:00
parent d15aeaedc4
commit e8f7453a8f
3 changed files with 22 additions and 6 deletions

View File

@ -13,7 +13,7 @@ module Game::TestCommand
Point.new(0, 0), Point.new(0, 0),
Building.new( Building.new(
Building::Type::StartPoint, Building::Type::StartPoint,
storage: 200 storage: 200, roles: [Building::Role::Storehouse]
) )
) )
map.set DepositTile.new( map.set DepositTile.new(
@ -79,8 +79,10 @@ module Game::TestCommand
it "should complete mining command" do it "should complete mining command" do
world = World.new create_map_with_resource world = World.new create_map_with_resource
command = MineCommand.new Point.new(0, 1), once: true command = MineCommand.new Point.new(0, 1), once: true
world.push command time_point = (20 + 1 * 2 + 1 * 2).to_i64
world.run 20 done_at = world.push command
done_at.should eq time_point
world.run time_point
# Check world resources # Check world resources
world.resources[Resource::Type::Crystals].should eq 40 world.resources[Resource::Type::Crystals].should eq 40
# Check tile deposit # Check tile deposit

View File

@ -63,7 +63,7 @@ module Game
storage : Capacity | Nil = nil storage : Capacity | Nil = nil
) )
@name = name != "" ? name : @type.to_s @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) @construction = construction.nil? ? Construction.immediatly : construction.as(Construction)
@production = production @production = production
@mining = mining @mining = mining
@ -79,5 +79,9 @@ module Game
getter mining getter mining
getter restoration getter restoration
getter storage getter storage
def has_role(role : Role) : Bool
@roles.includes? role
end
end end
end end

View File

@ -59,11 +59,14 @@ module Game
end end
resource = mining.resource resource = mining.resource
deposit_tile = nearest_deposit(world, resource.type) 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) mined_amount = deposit_tile.dep.dec(resource.amount)
@holded = resource.type.to_res mined_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 end
mining.ts
end end
def finish(world : World) def finish(world : World)
@ -82,6 +85,13 @@ module Game
end end
tile.as?(DepositTile) tile.as?(DepositTile)
end 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 end
class RestoreCommand < Command class RestoreCommand < Command