From bf97096dd77beabd747de89a50bc2390fa1ce773 Mon Sep 17 00:00:00 2001 From: Cooldude2606 Date: Thu, 28 Mar 2019 22:47:51 +0000 Subject: [PATCH] Added Paths --- config/file_loader.lua | 1 + config/worn_paths.lua | 112 +++++++++++++ expcore/common.lua | 150 ++++++++++++++++++ modules/addons/worn-paths.lua | 125 +++++++++++++++ old/modules/{ => DONE}/WornPaths/control.lua | 0 old/modules/{ => DONE}/WornPaths/softmod.json | 0 .../{ => DONE}/WornPaths/src/entites.lua | 0 .../{ => DONE}/WornPaths/src/paths.lua | 0 .../{ => DONE}/WornPaths/src/placed.lua | 0 9 files changed, 388 insertions(+) create mode 100644 config/worn_paths.lua create mode 100644 modules/addons/worn-paths.lua rename old/modules/{ => DONE}/WornPaths/control.lua (100%) rename old/modules/{ => DONE}/WornPaths/softmod.json (100%) rename old/modules/{ => DONE}/WornPaths/src/entites.lua (100%) rename old/modules/{ => DONE}/WornPaths/src/paths.lua (100%) rename old/modules/{ => DONE}/WornPaths/src/placed.lua (100%) diff --git a/config/file_loader.lua b/config/file_loader.lua index 048cbd72..3a85a462 100644 --- a/config/file_loader.lua +++ b/config/file_loader.lua @@ -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 diff --git a/config/worn_paths.lua b/config/worn_paths.lua new file mode 100644 index 00000000..f498721d --- /dev/null +++ b/config/worn_paths.lua @@ -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 + } +} \ No newline at end of file diff --git a/expcore/common.lua b/expcore/common.lua index aa70892a..4b5aee81 100644 --- a/expcore/common.lua +++ b/expcore/common.lua @@ -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 \ No newline at end of file diff --git a/modules/addons/worn-paths.lua b/modules/addons/worn-paths.lua new file mode 100644 index 00000000..fef49a2d --- /dev/null +++ b/modules/addons/worn-paths.lua @@ -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 \ No newline at end of file diff --git a/old/modules/WornPaths/control.lua b/old/modules/DONE/WornPaths/control.lua similarity index 100% rename from old/modules/WornPaths/control.lua rename to old/modules/DONE/WornPaths/control.lua diff --git a/old/modules/WornPaths/softmod.json b/old/modules/DONE/WornPaths/softmod.json similarity index 100% rename from old/modules/WornPaths/softmod.json rename to old/modules/DONE/WornPaths/softmod.json diff --git a/old/modules/WornPaths/src/entites.lua b/old/modules/DONE/WornPaths/src/entites.lua similarity index 100% rename from old/modules/WornPaths/src/entites.lua rename to old/modules/DONE/WornPaths/src/entites.lua diff --git a/old/modules/WornPaths/src/paths.lua b/old/modules/DONE/WornPaths/src/paths.lua similarity index 100% rename from old/modules/WornPaths/src/paths.lua rename to old/modules/DONE/WornPaths/src/paths.lua diff --git a/old/modules/WornPaths/src/placed.lua b/old/modules/DONE/WornPaths/src/placed.lua similarity index 100% rename from old/modules/WornPaths/src/placed.lua rename to old/modules/DONE/WornPaths/src/placed.lua