Make auto world run

This commit is contained in:
Anton Vakhrushev 2019-10-04 17:51:41 +03:00
parent de5a36d6d6
commit 123a025ce5
3 changed files with 19 additions and 18 deletions

View File

@ -5,13 +5,14 @@ require "./game/resources"
require "./game/world" require "./game/world"
require "./cli/command_router" require "./cli/command_router"
world = Game::World.new ts = Time.local.to_unix
world = Game::World.new(ts)
router = CLI::CommandRouter.new router = CLI::CommandRouter.new
router.add "st" do router.add "st" do
printf "Stat:\n\tTime: %d\n\tCrystals: %d\n\tTarraform: %d\n", printf "Stat:\n\tTime: %s\n\tCrystals: %d\n\tTarraform: %d\n",
world.ts, Time.unix(world.ts).to_local.to_s,
world.resources[Game::ResourceType::Crystal], world.resources[Game::ResourceType::Crystal],
world.resources[Game::ResourceType::Terraformation] world.resources[Game::ResourceType::Terraformation]
end end
@ -20,29 +21,26 @@ router.add "m" do
world.map.print world.map.print
end end
router.add "run {ts}" do |p|
ts = p["ts"].to_i32
world.run ts
printf "Run to %d\n", ts
end
router.add "harv {x} {y}" do |p| router.add "harv {x} {y}" do |p|
x = p["x"].to_i32 x = p["x"].to_i32
y = p["y"].to_i32 y = p["y"].to_i32
world.push(Game::BuildCrystalHarvesterCommand.new(Game::Point.new(x, y))) point = Game::Point.new(x, y)
world.push(Game::BuildCrystalHarvesterCommand.new(point))
printf "Build harvester at %d %d\n", x, y printf "Build harvester at %d %d\n", x, y
end end
router.add "rest {x} {y}" do |p| router.add "rest {x} {y}" do |p|
x = p["x"].to_i32 x = p["x"].to_i32
y = p["y"].to_i32 y = p["y"].to_i32
world.push(Game::BuildCrystalRestorerCommand.new(Game::Point.new(x, y))) point = Game::Point.new(x, y)
world.push(Game::BuildCrystalRestorerCommand.new(point))
end end
router.add "terr {x} {y}" do |p| router.add "terr {x} {y}" do |p|
x = p["x"].to_i32 x = p["x"].to_i32
y = p["y"].to_i32 y = p["y"].to_i32
world.push(Game::BuildTerraformerCommand.new(Game::Point.new(x, y))) point = Game::Point.new(x, y)
world.push(Game::BuildTerraformerCommand.new(point))
end end
def normalize_command(cmd) def normalize_command(cmd)
@ -56,6 +54,8 @@ loop do
if norm == "exit" if norm == "exit"
break break
end end
current_time = Time.local.to_unix
world.run current_time
router.handle cmd router.handle cmd
printf "\n" printf "\n"
end end

View File

@ -1,6 +1,6 @@
class Game::Queue class Game::Queue
struct Item struct Item
def initialize(@ts : Int32, @command : Game::Command) def initialize(@ts : Int64, @command : Game::Command)
end end
getter ts getter ts
@ -11,7 +11,7 @@ class Game::Queue
@data = [] of Item @data = [] of Item
end end
def push(ts : Int32, value : Game::Command) def push(ts : Int64, value : Game::Command)
# very unoptimal algo # very unoptimal algo
@data.push(Item.new(ts, value)) @data.push(Item.new(ts, value))
@data.sort! do |a, b| @data.sort! do |a, b|
@ -19,7 +19,7 @@ class Game::Queue
end end
end end
def pop(ts : Int32) : Item | Nil def pop(ts : Int64) : Item | Nil
if @data.size == 0 if @data.size == 0
return nil return nil
end end

View File

@ -1,7 +1,7 @@
require "./resources" require "./resources"
class Game::World class Game::World
property ts : Int32 property ts : Int64
def initialize(@ts = 0) def initialize(@ts = 0)
@map = Map.new @map = Map.new
@ -15,11 +15,11 @@ class Game::World
def push(command : Command) def push(command : Command)
dur = command.start(self) dur = command.start(self)
done_at = @ts + dur done_at = @ts + dur.to_i64
@tasks.push(done_at, command) @tasks.push(done_at, command)
end end
def run(ts : Int32) def run(ts : Int64)
loop do loop do
item = @tasks.pop(ts) item = @tasks.pop(ts)
if item.nil? if item.nil?
@ -29,5 +29,6 @@ class Game::World
@ts = item.ts @ts = item.ts
command.finish(self) command.finish(self)
end end
@ts = ts
end end
end end