Skip to content

Instantly share code, notes, and snippets.

@miio
Created April 6, 2013 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miio/5326229 to your computer and use it in GitHub Desktop.
Save miio/5326229 to your computer and use it in GitHub Desktop.
MAP_MIN = 0
MAP_MAX = 3000
BLOCK_SIZE = 10
DEBUG = false
# [level1_rate, level2_rate, ...]
RATE = [
25,
20,
15,
11,
9,
6,
5,
4,
3,
2
]
# Check total rate is 100
raise "Error total rate. Should 100. But,current #{RATE.reduce(&:+) }" unless 100 == RATE.reduce(&:+)
@csv_str = ""
@current_id = 1
def convert_map
debug_map = Array.new(BLOCK_SIZE).map{ Array.new(BLOCK_SIZE, 0) }
RATE.each_with_index do |level_limit, level|
used_level_num = 0
loop do
BLOCK_SIZE.times.sort_by{rand}.each do |block_x|
BLOCK_SIZE.times.sort_by{rand}.each do |block_y|
x = block_x
y = block_y
if (level_limit / 2) <= rand(level_limit) and 0 == debug_map[x][y]
@csv_str += "#{@current_id}, #{level + 1}, #{x}, #{y}\n"
used_level_num += 1
debug_map[x][y] = {current_id: @current_id, level: level + 1, x: x, y: y}
@current_id += 1
break if used_level_num >= level_limit
end
end
break if used_level_num >= level_limit
end
break if used_level_num >= level_limit
end
end
debug_map
end
current_position_x = MAP_MIN
current_position_y = MAP_MIN
debug_map = Array.new(MAP_MAX).map{ Array.new(MAP_MAX, 0) }
((MAP_MAX/BLOCK_SIZE)).times.each do |x|
((MAP_MAX/BLOCK_SIZE)).times.each do |y|
convert_map.each_with_index do |rows, row_x|
current_x = row_x + current_position_x
rows.each_with_index do |row, row_y|
current_y = row_y + current_position_y
debug_map[current_x][current_y] = {
current_id: row[:current_id],
level: row[:level],
x: current_x,
y: current_y
}
end
end
current_position_y += BLOCK_SIZE
end
current_position_x += BLOCK_SIZE
current_position_y = MAP_MIN
end
if DEBUG
line = ""
debug_map.each do |x|
x.each do |obj|
line += "|#{obj[:level]}|"
end
p line
line = ""
end
else
debug_map.each do |x|
x.each do |obj|
p "#{obj[:current_id]},#{obj[:level]},#{obj[:x]},#{obj[:y]}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment