Advertisement
Guest User

Untitled

a guest
Jul 18th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.49 KB | None | 0 0
  1.  
  2. settings.lua
  3. ...
  4.  
  5.   {
  6.     type = "string-setting",
  7.     name = "caves-surface-resource-names", -- caves-surface-resource-names=List of resource names to place on surface.
  8.     setting_type = "startup",
  9.     default_value = "crude-oil"
  10.   }
  11.  
  12. control.lua
  13. ...
  14.  
  15. local function lookupResourceNames(resourceNamesString)
  16.     -- split resource name string into finders
  17.     local resourceFinders = {}
  18.     for resourceName in string.gmatch(resourceNamesString, "[^, ]+") do
  19.         resourceFinders[resourceName] = { name = resourceName  }
  20.     end
  21.  
  22.     -- try to load and cache all existing resource names
  23.     if not global.rawResources then
  24.         if remote.interfaces["data-raw"] then
  25.             -- data-raw-prototypes mod is installed, we can use partial resource name matching by getting all the resource names ahead of time
  26.             global.rawResources = remote.call("data-raw", "prototypes_of_type", "resource")
  27.         else
  28.             -- data-raw-prototypes mod is not installed, require exactly matching resource names
  29.             global.rawResources = resourceFinders
  30.         end
  31.     end
  32.  
  33.     -- make a map of resource names matching the finders
  34.     local resourceNames = {}
  35.     for _, resource in pairs(global.rawResources) do
  36.         for name, finder in pairs(resourceFinders) do
  37.             escaped, _ = string.gsub(name, "[%-]", "%%-") -- only escape "-" for now "().%+-*?[^$"
  38.             if string.match(resource.name:lower(), escaped:lower()) then
  39.                 resourceNames[resource.name] = true;
  40.                 break
  41.             end
  42.         end
  43.     end
  44.  
  45.     return resourceNames
  46. end
  47.  
  48. script.on_event(defines.events.on_chunk_generated, function(e)
  49.  
  50.     local surfaceResourceNames = {}
  51.     if settings.startup["caves-surface-resource-names"] then
  52.         surfaceResourceNames = lookupResourceNames(settings.startup["caves-surface-resource-names"].value)
  53.     end
  54.     if e.surface == game.surfaces["nauvis"] then
  55.  
  56.         local overworldResources = game.surfaces["nauvis"].find_entities_filtered{e.area, type= "resource"}
  57.         for key, entity in pairs(overworldResources) do
  58.             if not surfaceResourceNames[entity.name] then
  59.                 entity.destroy()
  60.             end
  61.         end
  62.         global.mineSurface.request_to_generate_chunks(e.area.left_top)
  63.  
  64.     end
  65.  
  66. ...
  67.  
  68.     if e.surface == global.mineSurface then
  69.  
  70. ...
  71.  
  72.         table.insert(global.chunksToCreate, e.area)
  73.  
  74.                 -- remove surface only resources from mine after generation
  75.         local underworldResources = global.mineSurface.find_entities_filtered{e.area, type= "resource"}
  76.         for key, entity in pairs(underworldResources) do
  77.             if surfaceResourceNames[entity.name] then
  78.                 entity.destroy()
  79.             end
  80.         end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement