Merge branch 'feature/worn-paths' into dev

This commit is contained in:
Cooldude2606
2019-03-28 22:48:14 +00:00
9 changed files with 388 additions and 0 deletions

View File

@@ -21,6 +21,7 @@ return {
'modules.addons.advanced-starting-items',
'modules.addons.spawn-area',
'modules.addons.compilatron',
'modules.addons.worn-paths',
-- Config Files
'config.command_auth_admin', -- commands tagged with admin_only are blocked for non admins
'config.command_auth_runtime_disable', -- allows commands to be enabled and disabled during runtime

112
config/worn_paths.lua Normal file
View File

@@ -0,0 +1,112 @@
--- This file controls the placement/degrading of tiles as players build and walk
return {
weakness_value=1000, -- lower value will make tiles more likely to degrade
strengths={ -- this decides how "strong" a tile is, bigger number means less likely to degrade
-- note: tiles are effected by the tiles around them, a two wide path of the highest value will not degrade (for weakness 6)
-- note: values are relative to the tile with the highest value, recommended to keep highest tile as a "nice" number
-- note: tiles not in list will never degrade under any conditions (which is why some are omitted such as water)
["refined-concrete"]=100,
["refined-hazard-concrete-left"]=100,
["refined-hazard-concrete-right"]=100,
["concrete"]=90,
["hazard-concrete-left"]=90,
["hazard-concrete-right"]=90,
["stone-path"]=80,
["red-desert-0"]=80,
["dry-dirt"]=50,
-- grass four (main grass tiles)
["grass-1"]=50,
["grass-2"]=40,
["grass-3"]=30,
["grass-4"]=25,
-- red three (main red tiles)
["red-desert-1"]=40,
["red-desert-2"]=30,
["red-desert-3"]=25,
-- sand three (main sand tiles)
["sand-1"]=40,
["sand-2"]=30,
["sand-3"]=25,
-- dirt 3 (main dirt tiles)
["dirt-1"]=40,
["dirt-2"]=30,
["dirt-3"]=25,
-- last three/four (all sets of three merge here)
["dirt-4"]=25,
["dirt-5"]=30,
["dirt-6"]=40,
--["dirt-7"]=0, -- last tile, nothing to degrade to
-- land fill chain
["landfill"]=95,
["water-shallow"]=90,
--["water-mud"]=0, -- last tile, nothing to degrade to
},
degrade_order={ -- when a tile degrades it will turn into the next tile given here
["refined-concrete"]='concrete',
["refined-hazard-concrete-left"]='hazard-concrete-left',
["refined-hazard-concrete-right"]='hazard-concrete-right',
["concrete"]='stone-path',
["hazard-concrete-left"]='stone-path',
["hazard-concrete-right"]='stone-path',
["stone-path"]='dry-dirt',
["red-desert-0"]='dry-dirt',
["dry-dirt"]='dirt-4',
-- grass four (main grass tiles)
["grass-1"]='grass-2',
["grass-2"]='grass-3',
["grass-3"]='grass-4',
["grass-4"]='dirt-4',
-- red three (main red tiles)
["red-desert-1"]='red-desert-2',
["red-desert-2"]='red-desert-3',
["red-desert-3"]='dirt-4',
-- sand three (main sand tiles)
["sand-1"]='sand-2',
["sand-2"]='sand-3',
["sand-3"]='dirt-4',
-- dirt 3 (main dirt tiles)
["dirt-1"]='dirt-2',
["dirt-2"]='dirt-3',
["dirt-3"]='dirt-4',
-- last three/four (all sets of three merge here)
["dirt-4"]='dirt-5',
["dirt-5"]='dirt-6',
["dirt-6"]='dirt-7',
--["dirt-7"]=0, -- last tile, nothing to degrade to
-- land fill chain
["landfill"]='water-shallow',
["water-shallow"]='water-mud',
--["water-mud"]=0, -- last tile, nothing to degrade to
},
entities={ -- entities in this list will degrade the tiles under them when they are placed
['stone-furnace']=true,
['steel-furnace']=true,
['electric-furnace']=true,
['assembling-machine-1']=true,
['assembling-machine-2']=true,
['assembling-machine-3']=true,
['beacon']=true,
['centrifuge']=true,
['chemical-plant']=true,
['oil-refinery']=true,
['storage-tank']=true,
['nuclear-reactor']=true,
['steam-engine']=true,
['steam-turbine']=true,
['boiler']=true,
['heat-exchanger']=true,
['stone-wall']=true,
['gate']=true,
['gun-turret']=true,
['laser-turret']=true,
['flamethrower-turret']=true,
['radar']=true,
['lab']=true,
['big-electric-pole']=true,
['substation']=true,
['rocket-silo']=true,
['pumpjack']=true,
['electric-mining-drill']=true,
['roboport']=true
}
}

View File

@@ -270,4 +270,154 @@ function Public.move_items(items,surface,position,radius,chest_type)
return last_chest
end
--[[-- https://github.com/Refactorio/RedMew/blob/9184b2940f311d8c9c891e83429fc57ec7e0c4a2/map_gen/maps/diggy/debug.lua#L31
Prints a colored value on a location.
@param value between -1 and 1
@param surface LuaSurface
@param position Position {x, y}
@param scale float
@param offset float
@param immutable bool if immutable, only set, never do a surface lookup, values never change
]]
function Public.print_grid_value(value, surface, position, scale, offset, immutable)
local is_string = type(value) == 'string'
local color = Colours.white
local text = value
if type(immutable) ~= 'boolean' then
immutable = false
end
if not is_string then
scale = scale or 1
offset = offset or 0
position = {x = position.x + offset, y = position.y + offset}
local r = math.max(1, value) / scale
local g = 1 - math.abs(value) / scale
local b = math.min(1, value) / scale
if (r > 0) then
r = 0
end
if (b < 0) then
b = 0
end
if (g < 0) then
g = 0
end
r = math.abs(r)
color = { r = r, g = g, b = b}
-- round at precision of 2
text = math.floor(100 * value) * 0.01
if (0 == text) then
text = '0.00'
end
end
if not immutable then
local text_entity = surface.find_entity('flying-text', position)
if text_entity then
text_entity.text = text
text_entity.color = color
return
end
end
surface.create_entity{
name = 'flying-text',
color = color,
text = text,
position = position
}.active = false
end
--[[--
Prints a colored value on a location. When given a color_value and a delta_color,
will change the color of the text from the base to base + value * delta. This will
make the color of the text range from 'base_color' to 'base_color + delta_color'
as the color_value ranges from 0 to 1
@param value of number to be displayed
@param surface LuaSurface
@param position Position {x, y}
@param offset float position offset
@param immutable bool if immutable, only set, never do a surface lookup, values never change
@param color_value float How far along the range of values of colors the value is to be displayed
@param base_color {r,g,b} The color for the text to be if color_value is 0
@param delta_color {r,g,b} The amount to correct the base_color if color_value is 1
@param under_bound {r,g,b} The color to be used if color_value < 0
@param over_bound {r,g,b} The color to be used if color_value > 1
]]
function Public.print_colored_grid_value(value, surface, position, offset, immutable,
color_value, base_color, delta_color, under_bound, over_bound)
local is_string = type(value) == 'string'
-- default values:
local color = base_color or Colours.white
local d_color = delta_color or Colours.black
local u_color = under_bound or color
local o_color = over_bound or color
if (color_value < 0) then
color = u_color
elseif (color_value > 1) then
color = o_color
else
color = {
r = color.r + color_value * d_color.r,
g = color.g + color_value * d_color.g,
b = color.b + color_value * d_color.b
}
end
local text = value
if type(immutable) ~= 'boolean' then
immutable = false
end
if not is_string then
offset = offset or 0
position = {x = position.x + offset, y = position.y + offset}
-- round at precision of 2
text = math.floor(100 * value) * 0.01
if (0 == text) then
text = '0.00'
end
end
if not immutable then
local text_entity = surface.find_entity('flying-text', position)
if text_entity then
text_entity.text = text
text_entity.color = color
return
end
end
surface.create_entity{
name = 'flying-text',
color = color,
text = text,
position = position
}.active = false
end
function Public.clear_flying_text(surface)
local entities = surface.find_entities_filtered{name ='flying-text'}
for _,entity in pairs(entities) do
if entity and entity.valid then
entity.destroy()
end
end
end
return Public

View File

@@ -0,0 +1,125 @@
local Event = require 'utils.event'
local Game = require 'utils.game'
local Global = require 'utils.global'
local print_grid_value, clear_flying_text = ext_require('expcore.common','print_grid_value','clear_flying_text')
local config = require 'config.worn_paths'
local max_strength = 0
for _,strength in pairs(config.strengths) do
if strength > max_strength then
max_strength = strength
end
end
local debug_players = {}
Global.register(debug_players, function(tbl)
debug_players = tbl
end)
local function degrade(surface,position)
local tile = surface.get_tile(position)
local tile_name = tile.name
local degrade_tile_name = config.degrade_order[tile_name]
if not degrade_tile_name then return end
surface.set_tiles{{name=degrade_tile_name,position=position}}
end
local function degrade_entity(entity)
local surface = entity.surface
local position = entity.position
local tiles = {}
if not config.entities[entity.name] then return end
local box = entity.prototype.collision_box
local lt = box.left_top
local rb = box.right_bottom
for x = lt.x, rb.x do -- x loop
local px = position.x+x
for y = lt.y, rb.y do -- y loop
local p = {x=px,y=position.y+y}
local tile = surface.get_tile(p)
local tile_name = tile.name
local degrade_tile_name = config.degrade_order[tile_name]
if not degrade_tile_name then return end
table.insert(tiles,{name=degrade_tile_name,position=p})
end
end
surface.set_tiles(tiles)
end
local function get_probability(strength)
local v1 = strength/max_strength
local dif = 1 - v1
local v2 = dif/2
return (1-v1+v2)/config.weakness_value
end
local function get_tile_strength(surface,position)
local tile = surface.get_tile(position)
local tile_name = tile.name
local strength = config.strengths[tile_name]
if not strength then return end
for x = -1,1 do -- x loop
local px = position.x + x
for y = -1,1 do -- y loop
local check_tile = surface.get_tile{x=px,y=position.y+y}
local check_tile_name = check_tile.name
local check_strength = config.strengths[check_tile_name] or 0
strength = strength + check_strength
end
end
return strength/9
end
local function debug_get_tile_strength(surface,position)
for x = -3,3 do -- x loop
local px = position.x+x
for y = -3,3 do -- y loop
local p = {x=px,y=position.y+y}
local strength = get_tile_strength(surface,p) or 0
local tile = surface.get_tile(p)
print_grid_value(get_probability(strength)*config.weakness_value, surface, tile.position)
end
end
end
Event.add(defines.events.on_player_changed_position, function(event)
local player = Game.get_player_by_index(event.player_index)
local surface = player.surface
local position = player.position
local strength = get_tile_strength(surface,position)
if not strength then return end
if get_probability(strength) > math.random() then
degrade(surface,position)
end
if debug_players[player.name] then
debug_get_tile_strength(surface,position)
end
end)
Event.add(defines.events.on_built_entity, function(event)
local entity = event.created_entity
local surface = entity.surface
local position = entity.position
local strength = get_tile_strength(surface,position)
if not strength then return end
if get_probability(strength)*config.weakness_value > math.random() then
degrade_entity(entity)
end
end)
Event.add(defines.events.on_robot_built_entity, function(event)
local entity = event.created_entity
local surface = entity.surface
local position = entity.position
local strength = get_tile_strength(surface,position)
if not strength then return end
if get_probability(strength)*config.weakness_value > math.random() then
degrade_entity(entity)
end
end)
return function(player_name,state)
local player = Game.get_player_from_any(player_name)
clear_flying_text(player.surface)
debug_players[player_name] = state
end