Improve map functions

This commit is contained in:
Anton Vakhrushev 2019-09-16 15:14:44 +03:00
parent e95adec0e6
commit f75b413f8f
3 changed files with 43 additions and 35 deletions

View File

@ -25,6 +25,10 @@ abstract class Tile
@point
end
def cap
@cap
end
def cur
@cur
end
@ -81,10 +85,12 @@ class ForesterHouseTile < Tile
end
class Map
SIZE = 4
def initialize
@data = {} of String => Tile
(0...4).each do |x|
(0...4).each do |y|
(0...SIZE).each do |x|
(0...SIZE).each do |y|
self.set(GrassTile.new(Point.new(x, y)))
end
end
@ -105,45 +111,46 @@ class Map
@data[key(point)] = tile
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
d = 99999
(0...4).each do |x|
(0...4).each do |y|
tile = self.get(Point.new(x, y))
if tile.letter == 'f' && tile.cur > 0
td = Point.new(x, y).distance(point)
if td < d
d = td
p = Point.new(x, y)
end
d = Int32::MAX
tiles do |tile_point, tile|
if (yield tile)
tile_dist = tile_point.distance(point)
if tile_dist < d
d = tile_dist
p = tile_point
end
end
end
p
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
p = nil
d = 99999
(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
nearest_point point do |tile|
tile.letter == 'f' && tile.cur < tile.cap
end
p
end
def print
(0...4).each do |x|
(0...4).each do |y|
(0...SIZE).each do |x|
(0...SIZE).each do |y|
printf "%c", @data[key(Point.new(x, y))].letter
end
printf "\n"

View File

@ -3,12 +3,12 @@ class App::CommandQueue
@data = [] of NamedTuple(ts: Int32, cmd: Command)
end
# Plan finishing of *command* at time *ts*
def push(ts : Int32, cmd : Command)
@data.push({ts: ts, cmd: cmd})
@data.sort! do |a, b|
b[:ts] <=> a[:ts]
end
# puts @data
end
def pop(ts : Int32)

View File

@ -1,3 +1,4 @@
require "colorize"
require "./queue"
require "./map"
@ -161,10 +162,10 @@ class World
if item.nil?
break
end
cmd_ts, cmd = item[:ts], item[:cmd]
@ts = cmd_ts
printf "world : %d : finish `%s`\n", @ts, typeof(cmd)
cmd.finish(self)
command_ts, command = item[:ts], item[:cmd]
@ts = command_ts
command.finish(self)
printf "world : %d : finish `%s`\n", @ts, typeof(command)
end
end
end
@ -173,5 +174,5 @@ w = World.new
w.map.print
w.push(BuildWoodMillCommand.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