Improve map functions
This commit is contained in:
parent
e95adec0e6
commit
f75b413f8f
65
src/map.cr
65
src/map.cr
@ -25,6 +25,10 @@ abstract class Tile
|
|||||||
@point
|
@point
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def cap
|
||||||
|
@cap
|
||||||
|
end
|
||||||
|
|
||||||
def cur
|
def cur
|
||||||
@cur
|
@cur
|
||||||
end
|
end
|
||||||
@ -81,10 +85,12 @@ class ForesterHouseTile < Tile
|
|||||||
end
|
end
|
||||||
|
|
||||||
class Map
|
class Map
|
||||||
|
SIZE = 4
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@data = {} of String => Tile
|
@data = {} of String => Tile
|
||||||
(0...4).each do |x|
|
(0...SIZE).each do |x|
|
||||||
(0...4).each do |y|
|
(0...SIZE).each do |y|
|
||||||
self.set(GrassTile.new(Point.new(x, y)))
|
self.set(GrassTile.new(Point.new(x, y)))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -105,45 +111,46 @@ class Map
|
|||||||
@data[key(point)] = tile
|
@data[key(point)] = tile
|
||||||
end
|
end
|
||||||
|
|
||||||
def nearest_wood(point : Point) : Point | Nil
|
def tiles
|
||||||
|
(0...SIZE).each do |x|
|
||||||
|
(0...SIZE).each do |y|
|
||||||
|
point = Point.new(x, y)
|
||||||
|
tile = self.get(point)
|
||||||
|
yield point, tile
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def nearest_point(point : Point, &block) : Point | Nil
|
||||||
p = nil
|
p = nil
|
||||||
d = 99999
|
d = Int32::MAX
|
||||||
(0...4).each do |x|
|
tiles do |tile_point, tile|
|
||||||
(0...4).each do |y|
|
if (yield tile)
|
||||||
tile = self.get(Point.new(x, y))
|
tile_dist = tile_point.distance(point)
|
||||||
if tile.letter == 'f' && tile.cur > 0
|
if tile_dist < d
|
||||||
td = Point.new(x, y).distance(point)
|
d = tile_dist
|
||||||
if td < d
|
p = tile_point
|
||||||
d = td
|
|
||||||
p = Point.new(x, y)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
p
|
p
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def nearest_wood(point : Point) : Point | Nil
|
||||||
|
nearest_point point do |tile|
|
||||||
|
tile.letter == 'f' && tile.cur > 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def nearest_any_wood(point : Point) : Point | Nil
|
def nearest_any_wood(point : Point) : Point | Nil
|
||||||
p = nil
|
nearest_point point do |tile|
|
||||||
d = 99999
|
tile.letter == 'f' && tile.cur < tile.cap
|
||||||
(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
|
end
|
||||||
p
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def print
|
def print
|
||||||
(0...4).each do |x|
|
(0...SIZE).each do |x|
|
||||||
(0...4).each do |y|
|
(0...SIZE).each do |y|
|
||||||
printf "%c", @data[key(Point.new(x, y))].letter
|
printf "%c", @data[key(Point.new(x, y))].letter
|
||||||
end
|
end
|
||||||
printf "\n"
|
printf "\n"
|
||||||
|
@ -3,12 +3,12 @@ class App::CommandQueue
|
|||||||
@data = [] of NamedTuple(ts: Int32, cmd: Command)
|
@data = [] of NamedTuple(ts: Int32, cmd: Command)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Plan finishing of *command* at time *ts*
|
||||||
def push(ts : Int32, cmd : Command)
|
def push(ts : Int32, cmd : Command)
|
||||||
@data.push({ts: ts, cmd: cmd})
|
@data.push({ts: ts, cmd: cmd})
|
||||||
@data.sort! do |a, b|
|
@data.sort! do |a, b|
|
||||||
b[:ts] <=> a[:ts]
|
b[:ts] <=> a[:ts]
|
||||||
end
|
end
|
||||||
# puts @data
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def pop(ts : Int32)
|
def pop(ts : Int32)
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
require "colorize"
|
||||||
require "./queue"
|
require "./queue"
|
||||||
require "./map"
|
require "./map"
|
||||||
|
|
||||||
@ -161,10 +162,10 @@ class World
|
|||||||
if item.nil?
|
if item.nil?
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
cmd_ts, cmd = item[:ts], item[:cmd]
|
command_ts, command = item[:ts], item[:cmd]
|
||||||
@ts = cmd_ts
|
@ts = command_ts
|
||||||
printf "world : %d : finish `%s`\n", @ts, typeof(cmd)
|
command.finish(self)
|
||||||
cmd.finish(self)
|
printf "world : %d : finish `%s`\n", @ts, typeof(command)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -173,5 +174,5 @@ w = World.new
|
|||||||
w.map.print
|
w.map.print
|
||||||
w.push(BuildWoodMillCommand.new(Point.new(0, 0)))
|
w.push(BuildWoodMillCommand.new(Point.new(0, 0)))
|
||||||
w.push(BuildForesterHouseCommand.new(Point.new(0, 0)))
|
w.push(BuildForesterHouseCommand.new(Point.new(0, 0)))
|
||||||
w.run(120)
|
w.run(60)
|
||||||
printf "Wood: %d\n", w.resources.wood
|
printf "Wood: %d\n", w.resources.wood
|
||||||
|
Loading…
Reference in New Issue
Block a user