1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-01-30 04:30:58 +02:00

fixed negative map values and some minor changes

This commit is contained in:
Maik Wild 2018-09-20 13:15:12 +02:00
parent 97c5ea74ee
commit 962fb8c4c3
3 changed files with 52 additions and 51 deletions

View File

@ -31,6 +31,7 @@ local message_count = 0
@param message string @param message string
]] ]]
function Debug.print(message) function Debug.print(message)
if type(message) ~= 'string' and type(message) ~= 'number' then message = type(message) end
message_count = message_count + 1 message_count = message_count + 1
if (debug) then if (debug) then
game.print('[' .. message_count .. '] ' .. message) game.print('[' .. message_count .. '] ' .. message)
@ -44,10 +45,12 @@ end
@param y number @param y number
@param message string @param message string
]] ]]
function Debug.printPosition(x, y, message) function Debug.printPosition(position, message)
message = message or ''
if type(message) ~= 'string' and type(message) ~= 'number' then message = type(message) end
message_count = message_count + 1 message_count = message_count + 1
if (debug) then if (debug) then
game.print('[' .. message_count .. '] {x=' .. x .. ', y=' .. y .. '} ' .. message) game.print('[' .. message_count .. '] {x=' .. position.x .. ', y=' .. position.y .. '} ' .. message)
end end
end end

View File

@ -2,17 +2,17 @@
local Event = require 'utils.event' local Event = require 'utils.event'
-- this -- this
local PressureMap = {} local StressMap = {}
local epsilon = 0.01 local epsilon = 0.01
-- main block -- main block
global.pressure_map_storage = {} global.stress_map_storage = {}
local defaultValue = 0 local defaultValue = 0
local _mt_y = { __index=function(tbl,key) tbl[key] = defaultValue return tbl[key] end} local _mt_y = { __index=function(tbl,key) tbl[key] = defaultValue return tbl[key] end}
local _mt_x = {__index=function(tbl,key) tbl[key] = setmetatable({},_mt_y) return rawget(tbl,key) end} local _mt_x = {__index=function(tbl,key) tbl[key] = setmetatable({},_mt_y) return rawget(tbl,key) end}
local function set_metatables() local function set_metatables()
for _,map in pairs(global.pressure_map_storage) do for _,map in ipairs(global.stress_map_storage) do
for _,quad in pairs(map) do for _,quad in pairs(map) do
setmetatable(quad,_mt_x) setmetatable(quad,_mt_x)
for _,stbl in pairs(quad) do for _,stbl in pairs(quad) do
@ -27,94 +27,92 @@ Event.on_init(set_metatables)
Event.on_load(set_metatables) Event.on_load(set_metatables)
PressureMap.events = { StressMap.events = {
--[[-- --[[--
When pressure at certain position changes When stress at certain position changes
- position LuaPosition - position LuaPosition
- value Number - value Number
- old_value Number - old_value Number
- surface LuaSurface - surface LuaSurface
]] ]]
on_pressure_changed = script.generate_event_name() on_stress_changed = script.generate_event_name()
} }
--[[-- --[[--
Adds a fraction to a given location on the pressure_map. Returns the new Adds a fraction to a given location on the stress_map. Returns the new
fraction value of that position. fraction value of that position.
@param pressure_map Table of {@see get_pressure_map} @param stress_map Table of {@see get_stress_map}
@param position Table with x and y @param position Table with x and y
@param number fraction @param number fraction
@return number sum of old fraction + new fraction @return number sum of old fraction + new fraction
]] ]]
function add_fraction(pressure_map, position, fraction) function add_fraction(stress_map, position, fraction)
local map local x = position.x
if position.x >= 0 then local y = position.y
if position.y >= 0 then local quadrant = 1
map = pressure_map.quadrant1 if x < 0 then
else quadrant = quadrant + 1
map = pressure_map.quadrant4 x = - x
end end
else if y < 0 then
if position.y >= 0 then quadrant = quadrant + 2
map = pressure_map.quadrant2 y = - y
else
map = pressure_map.quadrant3
end
end end
--magic meta tables! --magic meta tables!
local value = map[position.x][position.y] + fraction local value = stress_map[quadrant][x][y] + fraction
map[position.x][position.y] = value stress_map[quadrant][x][y] = value
local surface = game.surfaces[pressure_map.surface_index] local surface = game.surfaces[stress_map.surface_index]
script.raise_event(PressureMap.events.on_pressure_changed, {old_value = value - fraction, value = value, position = position, surface = surface}) script.raise_event(StressMap.events.on_stress_changed, {old_value = value - fraction, value = value, position = position, surface = surface})
return value return value
end end
--[[-- --[[--
Creates a new pressure map if it doesn't exist yet and returns it. Creates a new stress map if it doesn't exist yet and returns it.
@param surface LuaSurface @param surface LuaSurface
@return Table with maxed_values_buffer, quadrant1, quadrant2, quadrant3 and quadrant4 @return Table with maxed_values_buffer, [1,2,3,4] containing the quadrants
]] ]]
local function get_pressure_map(surface) local function get_stress_map(surface)
if not global.pressure_map_storage[surface.index] then if not global.stress_map_storage[surface.index] then
global.pressure_map_storage[surface.index] = {} global.stress_map_storage[surface.index] = {}
local map = global.pressure_map_storage[surface.index] local map = global.stress_map_storage[surface.index]
map.quadrant1 = setmetatable({},_mt_x) map[1] = setmetatable({},_mt_x)
map.quadrant2 = setmetatable({},_mt_x) map[2] = setmetatable({},_mt_x)
map.quadrant3 = setmetatable({},_mt_x) map[3] = setmetatable({},_mt_x)
map.quadrant4 = setmetatable({},_mt_x) map[4] = setmetatable({},_mt_x)
map["surface_index"] = surface.index map["surface_index"] = surface.index
map.maxed_values_buffer = {} map.maxed_values_buffer = {}
end end
return global.pressure_map_storage[surface.index] return global.stress_map_storage[surface.index]
end end
function PressureMap.process_maxed_values_buffer(surface, callback) function StressMap.process_maxed_values_buffer(surface, callback)
if ('table' ~= type(surface) or not surface.name) then if ('table' ~= type(surface) or not surface.name) then
error('PressureMap.process_maxed_values_buffer argument #1 expects a LuaSurface, ' .. type(surface) .. ' given.') error('StressMap.process_maxed_values_buffer argument #1 expects a LuaSurface, ' .. type(surface) .. ' given.')
end end
if ('function' ~= type(callback)) then if ('function' ~= type(callback)) then
error('PressureMap.process_maxed_values_buffer argument #2 expects a callback function, ' .. type(callback) .. ' given.') error('StressMap.process_maxed_values_buffer argument #2 expects a callback function, ' .. type(callback) .. ' given.')
end end
local buffer = {} local buffer = {}
local map = get_pressure_map(surface) local map = get_stress_map(surface)
for _, position in pairs(map.maxed_values_buffer) do for _, position in pairs(map.maxed_values_buffer) do
table.insert(buffer, position) table.insert(buffer, position)
end end
@ -128,24 +126,24 @@ end
--[[-- --[[--
@param surface LuaSurface @param surface LuaSurface
@param position Position with x and y @param position Position with x and y
@param number fraction to add to the given position on the surface increase or decreasing pressure @param number fraction to add to the given position on the surface increase or decreasing stress
]] ]]
function PressureMap.add(surface, position, fraction) function StressMap.add(surface, position, fraction)
if ('table' ~= type(surface) or nil == surface.name) then if ('table' ~= type(surface) or nil == surface.name) then
error('PressureMap.set argument #1 expects a LuaSurface, ' .. type(surface) .. ' given.') error('StressMap.set argument #1 expects a LuaSurface, ' .. type(surface) .. ' given.')
end end
if ('table' ~= type(position) or nil == position.x or nil == position.y) then if ('table' ~= type(position) or nil == position.x or nil == position.y) then
error('PressureMap.set argument #2 expects a position with x and y, ' .. type(position) .. ' given.') error('StressMap.set argument #2 expects a position with x and y, ' .. type(position) .. ' given.')
end end
local pressure_map = get_pressure_map(surface) local stress_map = get_stress_map(surface)
local new = add_fraction(pressure_map, position, fraction) local new = add_fraction(stress_map, position, fraction)
if (new >= 1 - epsilon) then if (new >= 1 - epsilon) then
table.insert(pressure_map.maxed_values_buffer, position) table.insert(stress_map.maxed_values_buffer, position)
end end
end end
return PressureMap return StressMap