From 123a025ce518e936dec56d9cf212ab2b3efcfd32 Mon Sep 17 00:00:00 2001 From: Anton Vakhrushev Date: Fri, 4 Oct 2019 17:51:41 +0300 Subject: [PATCH] Make auto world run --- src/expansion.cr | 24 ++++++++++++------------ src/game/queue.cr | 6 +++--- src/game/world.cr | 7 ++++--- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/expansion.cr b/src/expansion.cr index 336cce4..bdd4cd2 100644 --- a/src/expansion.cr +++ b/src/expansion.cr @@ -5,13 +5,14 @@ require "./game/resources" require "./game/world" require "./cli/command_router" -world = Game::World.new +ts = Time.local.to_unix +world = Game::World.new(ts) router = CLI::CommandRouter.new router.add "st" do - printf "Stat:\n\tTime: %d\n\tCrystals: %d\n\tTarraform: %d\n", - world.ts, + printf "Stat:\n\tTime: %s\n\tCrystals: %d\n\tTarraform: %d\n", + Time.unix(world.ts).to_local.to_s, world.resources[Game::ResourceType::Crystal], world.resources[Game::ResourceType::Terraformation] end @@ -20,29 +21,26 @@ router.add "m" do world.map.print 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| x = p["x"].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 end router.add "rest {x} {y}" do |p| x = p["x"].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 router.add "terr {x} {y}" do |p| x = p["x"].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 def normalize_command(cmd) @@ -56,6 +54,8 @@ loop do if norm == "exit" break end + current_time = Time.local.to_unix + world.run current_time router.handle cmd printf "\n" end diff --git a/src/game/queue.cr b/src/game/queue.cr index be5c175..6227f38 100644 --- a/src/game/queue.cr +++ b/src/game/queue.cr @@ -1,6 +1,6 @@ class Game::Queue struct Item - def initialize(@ts : Int32, @command : Game::Command) + def initialize(@ts : Int64, @command : Game::Command) end getter ts @@ -11,7 +11,7 @@ class Game::Queue @data = [] of Item end - def push(ts : Int32, value : Game::Command) + def push(ts : Int64, value : Game::Command) # very unoptimal algo @data.push(Item.new(ts, value)) @data.sort! do |a, b| @@ -19,7 +19,7 @@ class Game::Queue end end - def pop(ts : Int32) : Item | Nil + def pop(ts : Int64) : Item | Nil if @data.size == 0 return nil end diff --git a/src/game/world.cr b/src/game/world.cr index af6c38b..f3d112f 100644 --- a/src/game/world.cr +++ b/src/game/world.cr @@ -1,7 +1,7 @@ require "./resources" class Game::World - property ts : Int32 + property ts : Int64 def initialize(@ts = 0) @map = Map.new @@ -15,11 +15,11 @@ class Game::World def push(command : Command) dur = command.start(self) - done_at = @ts + dur + done_at = @ts + dur.to_i64 @tasks.push(done_at, command) end - def run(ts : Int32) + def run(ts : Int64) loop do item = @tasks.pop(ts) if item.nil? @@ -29,5 +29,6 @@ class Game::World @ts = item.ts command.finish(self) end + @ts = ts end end