Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- settings.lua
- ...
- {
- type = "string-setting",
- name = "caves-surface-resource-names", -- caves-surface-resource-names=List of resource names to place on surface.
- setting_type = "startup",
- default_value = "crude-oil"
- }
- control.lua
- ...
- local function lookupResourceNames(resourceNamesString)
- -- split resource name string into finders
- local resourceFinders = {}
- for resourceName in string.gmatch(resourceNamesString, "[^, ]+") do
- resourceFinders[resourceName] = { name = resourceName }
- end
- -- try to load and cache all existing resource names
- if not global.rawResources then
- if remote.interfaces["data-raw"] then
- -- data-raw-prototypes mod is installed, we can use partial resource name matching by getting all the resource names ahead of time
- global.rawResources = remote.call("data-raw", "prototypes_of_type", "resource")
- else
- -- data-raw-prototypes mod is not installed, require exactly matching resource names
- global.rawResources = resourceFinders
- end
- end
- -- make a map of resource names matching the finders
- local resourceNames = {}
- for _, resource in pairs(global.rawResources) do
- for name, finder in pairs(resourceFinders) do
- escaped, _ = string.gsub(name, "[%-]", "%%-") -- only escape "-" for now "().%+-*?[^$"
- if string.match(resource.name:lower(), escaped:lower()) then
- resourceNames[resource.name] = true;
- break
- end
- end
- end
- return resourceNames
- end
- script.on_event(defines.events.on_chunk_generated, function(e)
- local surfaceResourceNames = {}
- if settings.startup["caves-surface-resource-names"] then
- surfaceResourceNames = lookupResourceNames(settings.startup["caves-surface-resource-names"].value)
- end
- if e.surface == game.surfaces["nauvis"] then
- local overworldResources = game.surfaces["nauvis"].find_entities_filtered{e.area, type= "resource"}
- for key, entity in pairs(overworldResources) do
- if not surfaceResourceNames[entity.name] then
- entity.destroy()
- end
- end
- global.mineSurface.request_to_generate_chunks(e.area.left_top)
- end
- ...
- if e.surface == global.mineSurface then
- ...
- table.insert(global.chunksToCreate, e.area)
- -- remove surface only resources from mine after generation
- local underworldResources = global.mineSurface.find_entities_filtered{e.area, type= "resource"}
- for key, entity in pairs(underworldResources) do
- if surfaceResourceNames[entity.name] then
- entity.destroy()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement