Guest User

control.lua

a guest
Sep 20th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 71.90 KB | None | 0 0
  1. -- WATER AS A RESOURCE MAIN LUA CONTROL PROGRAM --
  2. --       CREATED BY TREEFROGGREAKEN 2019        --
  3.  
  4. -- REQUIRED ADDITIONAL LUA FILES --
  5.  
  6. require("waterbodies") -- Loads waterbodies.lua
  7. require("fluidentities")
  8. require("fluidlist")
  9.  
  10.  
  11.  
  12. -- GLOBAL VARIABLES
  13.  
  14. function Globals()
  15.     if not global.WaterGlobalArea then global.WaterGlobalArea = {} end
  16.     if not global.OPLocate then global.OPLocate = {} end
  17.     if not global.ODLocate then global.ODLocate = {} end
  18.     if not global.LandFill then global.LandFill = {} end
  19.     if not global.FluidProducers then global.FluidProducers = {} end
  20.    
  21.     if not global.Type then global.Type = 0 end
  22.     if not global.WaterFlow then global.WaterFlow = 0 end
  23.     if not global.CrudeFlow then global.CrudeFlow = 0 end
  24.     if not global.LastWaterFlow then global.LastWaterFlow = 0 end
  25.     if not global.LastCrudeFlow then global.LastCrudeFlow = 0 end
  26.     if not global.WaterBodyType then global.WaterBodyType = 0 end
  27.     if not global.ActiveOPs then global.ActiveOPs = 0 end
  28.     if not global.ActiveODs then global.ActiveODs = 0 end
  29.     if not global.PercentChange then global.PercentChange = 0 end
  30.     if not global.InstallTick then global.InstallTick = 0 end
  31.    
  32.     if not global.Added then global.Added = true end
  33.     if not global.NewInstall then global.NewInstall = true end
  34.     if not global.ITMessage then global.ITMessage = true end
  35. end
  36.  
  37. function LinkGlobals()
  38.  
  39. -------------------------------- DEXY EDIT ------------------------------
  40.     if remote.interfaces["warptorio2"] then
  41.         log("Registred Warptorio2 event")
  42.         local warp_event = remote.call("warptorio2", "postwarpevent")
  43.         script.on_event(warp_event, function(event)
  44.             log("Warped, all lakes removed")
  45.             RemoveAllDrainedArea()
  46.             RestoreWater()
  47.         end)   
  48.     end
  49. -------------------------------------------------------------------------
  50.  
  51.     WaterFlow = global.WaterFlow
  52.     CrudeFlow = global.CrudeFlow
  53.     LastWaterFlow = global.LastWaterFlow
  54.     LastCrudeFlow = global.LastCrudeFlow
  55.     WaterBodyType = global.WaterBodyType
  56.     ActiveOPs = global.ActiveOPs
  57.     ActiveODs = global.ActiveODs
  58.     PercentChange = global.PercentChange
  59. end
  60.  
  61. -- EVENT DEPENDANT FUNCTIONS
  62.  
  63. function SearchPattern (SearchPosition, WASearchQueue)                          -- Function to Setup SearchPattern
  64.     local north = {x = SearchPosition.x, y = SearchPosition.y+1}                -- Create Local North Table make it equal to SearchPostion -1 y
  65.     local northeast = {x = SearchPosition.x+1, y = SearchPosition.y+1}
  66.     local east  = {x = SearchPosition.x+1, y = SearchPosition.y}                -- Create Local East Table make it equal to SearchPostion +1 x
  67.     local southeast = {x = SearchPosition.x+1, y = SearchPosition.y-1}
  68.     local south = {x = SearchPosition.x, y = SearchPosition.y-1}                -- Create Local South Table make it equal to SearchPostion +1 y
  69.     local southwest = {x = SearchPosition.x-1, y = SearchPosition.y-1}
  70.     local west  = {x = SearchPosition.x-1, y = SearchPosition.y}                -- Create Local West Table make it equal to SearchPostion -1 x
  71.     local northwest = {x = SearchPosition.x-1, y = SearchPosition.y+1}
  72.     WASearchQueue[#WASearchQueue+1] = north
  73.     WASearchQueue[#WASearchQueue+1] = northeast
  74.     WASearchQueue[#WASearchQueue+1] = east
  75.     WASearchQueue[#WASearchQueue+1] = southeast
  76.     WASearchQueue[#WASearchQueue+1] = south
  77.     WASearchQueue[#WASearchQueue+1] = southwest
  78.     WASearchQueue[#WASearchQueue+1] = west
  79.     WASearchQueue[#WASearchQueue+1] = northwest
  80.     return WASearchQueue                                                        -- Return WASearchQueue Table as Result
  81. end
  82.  
  83. function GridRef (SearchPosition)
  84.     return string.format ("%.1f , %.1f", SearchPosition.x , SearchPosition.y)   -- Print SearchPosition X, Y CoOrds as String
  85. end
  86.  
  87. function IsWater(SearchPosition, surface)
  88.     local tile = game.surfaces[surface].get_tile(SearchPosition.x,SearchPosition.y)         -- Get Name of Tile by looking on the surface by using SearchPosition Variable
  89.     if tile.valid == true then
  90.         fluidname = tile.name
  91.         if fluidlist.CheckNames(fluidname) == true then                                 -- IF Water or Deepwater THEN
  92.             return true                                                             -- Return TRUE
  93.         else                                                                        -- Otherwise
  94.             return false                                                            -- Return False
  95.         end                                                                         -- NOT Water or Deepwater
  96.         game.print("NOT A WATER TILE")      -- Print Not A Water Tile
  97.     else
  98.         return false
  99.     end
  100. end
  101.  
  102. function GlobalWaterArea()
  103.     if #global.WaterGlobalArea == 0 then
  104.         if global.Type == 1 then
  105.             CreateWaterArea()   -- Create WaterArea
  106.             global.OPLocate[1]["WA"] = 1            -- Assign Offshore Pump 1 to WaterArea 1
  107.             global.WaterGlobalArea[1]["OPs"] = 1    -- Assign WaterArea 1 to Offshore Pump 1
  108.             global.WaterGlobalArea[1]["Surface"] = global.OPLocate[1]["entity"].surface
  109.             AssignFluid()
  110.             if global.WaterGlobalArea[1]["ToSearch"] == nil then
  111.                 game.print(string.format("%s created, with %sL of %s.", global.WaterGlobalArea[1]["WtrName"], global.WaterGlobalArea[1]["AmountWtr"], global.WaterGlobalArea[1]["FluidType"]))
  112.             else
  113.                 game.print("FluidArea being scanned, please wait for assignment.")
  114.             end
  115.         elseif global.Type == 2 then
  116.             local x = global.ODLocate[1]["position"]["x"]
  117.             local y = global.ODLocate[1]["position"]["y"]
  118.             local ODP = global.ODLocate[1]["position"]
  119.             local ODS = global.ODLocate[1]["entity"].surface
  120.             local WaterCheck = global.ODLocate[1]["surface"].get_tile(x,y).name
  121.             if WaterCheck == "water" or WaterCheck == "deepwater" then
  122.                 CreateWaterArea()
  123.                 global.ODLocate[1]["WA"] = 1
  124.                 global.WaterGlobalArea[1]["ODs"] = 1
  125.                 global.WaterGlobalArea[1]["Surface"] = global.ODLocate[1]["entity"].surface
  126.                 AssignFluid()
  127.                 if global.WaterGlobalArea[1]["ToSearch"] == nil then
  128.                     game.print(string.format("%s created, with %sL of %s.", global.WaterGlobalArea[1]["WtrName"], global.WaterGlobalArea[1]["AmountWtr"], "water"))
  129.                 else
  130.                     game.print("FluidArea being scanned, please wait for assignment.")
  131.                 end
  132.             else
  133.                 game.print("Area not suitable for Offshore Drain.")
  134.                 global.ODLocate[1]["entity"].destroy()
  135.                 table.remove(global.ODLocate)
  136.                 game.surfaces[ODS.name].spill_item_stack(ODP, {name="offshore-drain",count=1})
  137.             end
  138.         end
  139.     else
  140.         CompareAssign()
  141.     end
  142. end
  143.  
  144. function CompareAssign()
  145.     OffShoreCompareArea()
  146.     if FoundExt == true then                        -- IF No Offshores Found then New Area
  147.         if global.Type == 1 then
  148.             CreateWaterArea()
  149.             global.OPLocate[#global.OPLocate]["WA"] = #global.WaterGlobalArea
  150.             global.WaterGlobalArea[#global.WaterGlobalArea]["OPs"] = 1
  151.             global.WaterGlobalArea[#global.WaterGlobalArea]["Surface"] = global.OPLocate[#global.OPLocate]["entity"].surface
  152.             AssignFluid()
  153.             if global.WaterGlobalArea[#global.WaterGlobalArea]["ToSearch"] == nil then
  154.                 game.print(string.format("%s created, with %sL of %s.", global.WaterGlobalArea[#global.WaterGlobalArea]["WtrName"], global.WaterGlobalArea[#global.WaterGlobalArea]["AmountWtr"], global.WaterGlobalArea[#global.WaterGlobalArea]["FluidType"]))
  155.             else
  156.                 game.print("FluidArea being scanned, please wait for assignment.")
  157.             end
  158.             FoundExt = false
  159.         elseif global.Type == 2 then
  160.             local x = global.ODLocate[#global.ODLocate]["position"]["x"]
  161.             local y = global.ODLocate[#global.ODLocate]["position"]["y"]
  162.             local ODP = global.ODLocate[#global.ODLocate]["position"]
  163.             local ODS = global.ODLocate[#global.ODLocate]["entity"].surface
  164.             local WaterCheck = global.ODLocate[#global.ODLocate]["surface"].get_tile(x,y).name
  165.             if WaterCheck == "water" or WaterCheck == "deepwater" then
  166.                 CreateWaterArea()
  167.                 global.ODLocate[#global.ODLocate]["WA"] = #global.WaterGlobalArea
  168.                 global.WaterGlobalArea[#global.WaterGlobalArea]["ODs"] = 1
  169.                 global.WaterGlobalArea[#global.WaterGlobalArea]["Surface"] = global.ODLocate[#global.ODLocate]["entity"].surface
  170.                 AssignFluid()
  171.                 if global.WaterGlobalArea[#global.WaterGlobalArea]["ToSearch"] == nil then
  172.                     game.print(string.format("%s created, with %sL of %s.", global.WaterGlobalArea[#global.WaterGlobalArea]["WtrName"], global.WaterGlobalArea[#global.WaterGlobalArea]["AmountWtr"], "water"))
  173.                 else
  174.                     game.print("FluidArea being scanned, please wait for assignment.")
  175.                 end
  176.             else
  177.                 game.print("Area not suitable for Offshore Drain.")
  178.                 global.ODLocate[#global.ODLocate]["entity"].destroy()
  179.                 table.remove(global.ODLocate,#global.ODLocate)
  180.                 game.surfaces[ODS.name].spill_item_stack(ODP, {name="offshore-drain",count=1})
  181.             end
  182.             FoundExt = false
  183.         end
  184.     end
  185. end
  186.  
  187. function CreateWaterArea()
  188.     global.WaterGlobalArea[#global.WaterGlobalArea+1] = {["WtrName"] = "None",["Surface"] = nil, ["ToSearch"] = nil, ["HasSearched"] = nil,["LoopCount"] = 0, ["AmountWtr"] = 0,["AmountBonusValue"] = 0,["RegenAmount"] = 0,["Depleted"] = 0,["ShallowWater"] = 0, ["DeepWater"] = 0,["Percent"] = 0,["PercentPrev"] = 0,["Fired50"] = false, ["Fired75"] = false, ["Fired90"] = false, ["Fired95"] = false, ["Fired97"] = false,["Fired98"] = false,["Fired99"] = false,["RandPercent"] = 79, ["BTF"] = 0,["Below80"] = 0, ["WaterRepArea"] = {},["TilesSet"] = "N",["OPs"] = 0,["OPsA"] = 0,["ODs"] = 0, ["ODsA"] = 0, ["WtrUsed"] = 0,["WtrAdd"] = 0, ["WaterBodyType"] = 0, ["FluidType"] = nil}
  189.     a = #global.WaterGlobalArea
  190.     GetWaterArea(a)
  191.     CalculatedWaterTotal(a)
  192.     waterbodies.WtrName(a)
  193.     a = 0
  194. end
  195.  
  196. function AssignFluid()  -- Assign Fluid On New WaterArea Creation
  197.     for a = 1, #global.WaterGlobalArea, 1 do
  198.         if global.WaterGlobalArea[a]["FluidType"] == nil then
  199.             if global.Type == 1 then
  200.                 for b = 1, #global.OPLocate, 1 do
  201.                     if global.OPLocate[b]["WA"] == a then
  202.                         global.WaterGlobalArea[a]["FluidType"] = global.OPLocate[b]["tile"]
  203.                     end
  204.                 end
  205.             elseif global.Type == 2 then
  206.                 for b = 1, #global.ODLocate, 1 do
  207.                     if global.ODLocate[b]["WA"] == a then
  208.                         global.WaterGlobalArea[a]["FluidType"] = global.ODLocate[b]["tile"]
  209.                     end
  210.                 end
  211.             end
  212.         end
  213.     end
  214. end
  215.  
  216. function OffShoreCompareArea()
  217.     if global.Type == 1 then
  218.         NuOPs = #global.OPLocate
  219.         OPS = global.OPLocate
  220.         NewOPPosX = OPS[NuOPs]["position"]["x"]
  221.         NewOPPosY = OPS[NuOPs]["position"]["y"]
  222.     elseif global.Type == 2 then
  223.         NuOPs = #global.ODLocate
  224.         OPS = global.ODLocate
  225.         NewOPPosX = OPS[NuOPs]["position"]["x"]
  226.         NewOPPosY = OPS[NuOPs]["position"]["y"]
  227.     end
  228.     local NuWAs = #global.WaterGlobalArea
  229.     local WA = global.WaterGlobalArea
  230.     FoundInt = false
  231.     FoundExt = true
  232.     for a = NuWAs, 1, -1 do     -- FOR Each WATERAREA
  233.         for b = 1, #WA[a]["WaterRepArea"], 1 do -- FOR EACH TILE in WATERAREA
  234.             if WA[a]["WaterRepArea"][b] ~= nil then
  235.                 local WATilePosX = WA[a]["WaterRepArea"][b]["position"]["x"]
  236.                 local WATilePosY = WA[a]["WaterRepArea"][b]["position"]["y"]
  237.                 if NewOPPosX == WATilePosX then     -- IF NEW OFFSHORE IS IN WATER AREAS X POSITION
  238.                     if NewOPPosY == WATilePosY then -- IF NEW OFFSHORE IS IN WATER AREAS Y POSITION
  239.                         FoundExt = false
  240.                         FoundInt = true
  241.                     end
  242.                 end
  243.             end
  244.         end
  245.         if FoundInt == true then -- FOUND IN THIS WATERAREA
  246.             if global.Type == 1 then
  247.                 OPS[NuOPs]["WA"] = a
  248.                 WA[a]["OPs"] = WA[a]["OPs"] + 1
  249.                 if WA[a]["Percent"] < 0 then
  250.                     game.print(string.format("%s has %s Offshore Pumps, with %.0f %% of %s volume depleted.", WA[a]["WtrName"], WA[a]["OPs"], 0, WA[a]["FluidType"]))
  251.                 else
  252.                     game.print(string.format("%s has %s Offshore Pumps, with %.0f %% of %s volume depleted.", WA[a]["WtrName"], WA[a]["OPs"], WA[a]["Percent"], WA[a]["FluidType"]))
  253.                 end
  254.                 FoundInt = false
  255.             elseif global.Type == 2 then
  256.                 OPS[NuOPs]["WA"] = a
  257.                 WA[a]["ODs"] = WA[a]["ODs"] + 1
  258.                 if WA[a]["Percent"] >= 0 and WA[a]["Percent"] < 100 then
  259.                     game.print(string.format("%s has %s Offshore Drains, with %.0f %% of %s volume depleted.", WA[a]["WtrName"], WA[a]["ODs"], WA[a]["Percent"], WA[a]["FluidType"]))
  260.                 else
  261.                     if WA[a]["Percent"] >= 100 then
  262.                         game.print(string.format("%s has %s Offshore Drains, with %.0f %% depleted.", WA[a]["WtrName"], WA[a]["ODs"], WA[a]["Percent"], WA[a]["FluidType"]))
  263.                     else
  264.                         game.print(string.format("%s has %s Offshore Drains, with %.0f %% depleted.", WA[a]["WtrName"], WA[a]["ODs"], 0, WA[a]["FluidType"]))
  265.                     end
  266.                 end
  267.                 FoundInt = false
  268.             end
  269.         end
  270.     end
  271.     return FoundExt
  272. end
  273.  
  274. function GetWaterArea(a)                                        -- Get Water Area Function
  275.     if global.Type == 1 then
  276.         position = global.OPLocate[#global.OPLocate]["position"]
  277.         surface = global.OPLocate[#global.OPLocate]["surface"].name
  278.         WaterRepArea = global.WaterGlobalArea[#global.WaterGlobalArea]["WaterRepArea"]
  279.         WASearchQueue = {position}
  280.         WASearched = {}
  281.         SearchAmount = settings.global["FluidArea-Start-Area"].value
  282.         FA = global.WaterGlobalArea[a]
  283.     elseif global.Type == 2 then
  284.         position = global.ODLocate[#global.ODLocate]["position"]
  285.         surface = global.ODLocate[#global.ODLocate]["surface"].name
  286.         WaterRepArea = global.WaterGlobalArea[#global.WaterGlobalArea]["WaterRepArea"]
  287.         WASearchQueue = {position}
  288.         WASearched = {}
  289.         SearchAmount = settings.global["FluidArea-Start-Area"].value
  290.         FA = global.WaterGlobalArea[a]
  291.     elseif global.WaterGlobalArea[a]["ToSearch"] ~= nil then
  292.         WaterRepArea = global.WaterGlobalArea[a]["WaterRepArea"]
  293.         WASearchQueue = global.WaterGlobalArea[a]["ToSearch"]
  294.         WASearched = global.WaterGlobalArea[a]["HasSearched"]
  295.         surface = global.WaterGlobalArea[a]["Surface"].name
  296.         SearchAmount = settings.global["FluidArea-Additional-Tiles-Per-Second"].value / 60
  297.         FA = global.WaterGlobalArea[a]
  298.         if game.active_mods["SeaBlock"] then
  299.             TotalArea = global.WaterGlobalArea[a]["ShallowWater"] + global.WaterGlobalArea[a]["DeepWater"]
  300.             if TotalArea > 60000 then
  301.                 WASearchQueue = {}
  302.             end
  303.         end
  304.     end
  305.     local Shallow = 0
  306.     local Deep = 0
  307.     while #WASearchQueue > 0 do                                                     -- WHILE there is a WASerachQueue
  308.         if Shallow + Deep <= SearchAmount then
  309.             local SearchPosition = WASearchQueue[1]                                     -- Convert Offshore Pump Position into SearchPosition
  310.             if WASearched[GridRef(SearchPosition)] ~= true then                         -- IF GridRef has not been searched
  311.                 if IsWater(SearchPosition, surface) == false then                       -- IF IsWater is FALSE then
  312.                     WASearched[GridRef(SearchPosition)] = true                          -- GridRef has been Searched
  313.                 else                                                                    -- ELSE IsWater is TRUE then
  314.                     if fluidname == "water" or fluidname == "water-green" or fluidname == "water-shallow" or fluidname == "water-mud" then                  -- IF Water Type is "water"
  315.                         WaterRepArea[#WaterRepArea+1] = {["name"] = "sand-3" , ["position"] = {["x"] = SearchPosition.x, ["y"] = SearchPosition.y},["OriginalName"] = fluidname}
  316.                         FA["ShallowWater"] = FA["ShallowWater"] + 1
  317.                         Shallow = Shallow + 1
  318.                     elseif fluidname == "deepwater" or fluidname == "deepwater-green" then
  319.                         WaterRepArea[#WaterRepArea+1] = {["name"] = "dry-dirt" , ["position"] = {["x"] = SearchPosition.x, ["y"] = SearchPosition.y},["OriginalName"] = fluidname}
  320.                         FA["DeepWater"] = FA["DeepWater"] + 1
  321.                         Deep = Deep + 1
  322.                     end
  323.                     WASearched[GridRef(SearchPosition)] = true                                  -- Make SearchPosition TRUE in WASerached
  324.                     SearchPattern(SearchPosition, WASearchQueue)                                -- Run SearchPattern Function Passing SearchPosition and WASearchQueue
  325.                     table.remove (WASearchQueue,1)                                              -- Remove from WASearchQueue
  326.                 end                                                                             -- IF IsWater isn't TRUE or False
  327.             else
  328.                 table.remove (WASearchQueue,1)              -- Remove from WASearchQueue
  329.             end
  330.         else
  331.             FA["ToSearch"] = WASearchQueue
  332.             FA["HasSearched"] = WASearched
  333.             goto TooBigArea
  334.         end
  335.     end
  336.     ::TooBigArea::
  337.     if #WASearchQueue == 0 then
  338.         global.WaterGlobalArea[a]["ToSearch"] = nil
  339.         global.WaterGlobalArea[a]["HasSearched"] = nil
  340.     end
  341. end
  342.  
  343. function FluidAreaContinue(a)
  344.  
  345.     -- Dexy Edit --
  346.     if global.WaterGlobalArea["TotalLoop"] == nil then
  347.         global.WaterGlobalArea["TotalLoop"] = 0
  348.     else
  349.         global.WaterGlobalArea["TotalLoop"] = global.WaterGlobalArea["TotalLoop"] + 1
  350.     end
  351.     ---------------
  352.  
  353.     GetWaterArea(a)
  354.     CalculatedWaterTotal(a)
  355.     global.WaterGlobalArea[a]["LoopCount"] = global.WaterGlobalArea[a]["LoopCount"] + 1
  356.     local AlarmEnabled = settings.global["Alarms-Continuing-Search"].value
  357.     if global.WaterGlobalArea[a]["LoopCount"] == 1000 and AlarmEnabled == true then
  358.         global.WaterGlobalArea[a]["LoopCount"] = 0
  359.        
  360.         -- Dexy Edit --
  361.         --game.print(string.format("Still Scanning FluidArea %s", a))
  362.         game.print(string.format("Still Scanning FluidArea %s loop: %s", a, global.WaterGlobalArea["TotalLoop"]))
  363.         ---------------
  364.        
  365.     end
  366.     -- Dexy Edit --
  367.     --if global.WaterGlobalArea[a]["ToSearch"] == nil then
  368.     if global.WaterGlobalArea[a]["ToSearch"] == nil or global.WaterGlobalArea["TotalLoop"] > 10000 then
  369.         if global.WaterGlobalArea["TotalLoop"] > 10000 then
  370.             game.print(string.format("Water area %s is too big", a))
  371.             global.WaterGlobalArea[a]["ToSearch"] = nil
  372.         end
  373.    
  374.         global.WaterGlobalArea["TotalLoop"] = 0
  375.     ---------------
  376.         global.WaterGlobalArea[a]["LoopCount"] = 0
  377.         waterbodies.WtrName(a)
  378.         game.print(string.format("%s created, with %sL of %s.", global.WaterGlobalArea[a]["WtrName"], global.WaterGlobalArea[a]["AmountWtr"], global.WaterGlobalArea[a]["FluidType"]))
  379.     end
  380. end
  381.  
  382. function CalculatedWaterTotal(a)
  383.     local Shallow = global.WaterGlobalArea[a]["ShallowWater"]
  384.     local Deep = global.WaterGlobalArea[a]["DeepWater"]
  385.     local TotalArea = Shallow + Deep
  386.     global.WaterGlobalArea[a]["BTF"] = TotalArea
  387.     local WATER = settings.global["TileFluidAmount-Shallow"].value
  388.     local DEEPWATER = settings.global["TileFluidAmount-Deep"].value
  389.     local PBonus = 0.01
  390.     local WBonus = 0.5
  391.     local PDBonus = 1
  392.     local LBonus = 1.5
  393.     local GLBonus = 2
  394.     local SBonus = 2.5
  395.     local OBonus = 3
  396.     local WaterTotal = (Shallow * WATER) + (Deep * DEEPWATER)
  397.     local WaterBodyType = 0
  398.     if TotalArea < 4 then
  399.         --game.print("PUDDLE")
  400.         global.WaterGlobalArea[a]["AmountWtr"] = WaterTotal * PBonus
  401.         global.WaterGlobalArea[a]["AmountBonusValue"] = PBonus
  402.         global.WaterGlobalArea[a]["WtrName"] = "Puddle"
  403.         global.WaterGlobalArea[a]["WaterBodyType"] = 0
  404.     elseif TotalArea == 4 then
  405.         --game.print("WELL")
  406.         global.WaterGlobalArea[a]["AmountWtr"] = WaterTotal * WBonus
  407.         global.WaterGlobalArea[a]["AmountBonusValue"] = WBonus
  408.         global.WaterGlobalArea[a]["WtrName"] = "Well"
  409.         global.WaterGlobalArea[a]["WaterBodyType"] = 1
  410.     elseif TotalArea > 4 and TotalArea <= 200 then
  411.         --game.print("POND")
  412.         global.WaterGlobalArea[a]["AmountWtr"] = WaterTotal * PDBonus
  413.         global.WaterGlobalArea[a]["AmountBonusValue"] = PDBonus
  414.         global.WaterGlobalArea[a]["WtrName"] = "Pond"
  415.         global.WaterGlobalArea[a]["WaterBodyType"] = 2
  416.     elseif TotalArea > 200 and TotalArea <= 6000 then
  417.         --game.print("LAKE")
  418.         global.WaterGlobalArea[a]["AmountWtr"] = WaterTotal * LBonus
  419.         global.WaterGlobalArea[a]["AmountBonusValue"] = LBonus
  420.         global.WaterGlobalArea[a]["WaterBodyType"] = 3
  421.     elseif TotalArea > 6000 and TotalArea <= 60000 then
  422.         --game.print("GREAT LAKE")
  423.         global.WaterGlobalArea[a]["AmountWtr"] = WaterTotal * GLBonus
  424.         global.WaterGlobalArea[a]["AmountBonusValue"] = GLBonus
  425.         global.WaterGlobalArea[a]["WaterBodyType"] = 4
  426.     elseif TotalArea > 60000 and TotalArea <= 600000 then
  427.         --game.print("SEA")
  428.         global.WaterGlobalArea[a]["AmountWtr"] = WaterTotal * SBonus
  429.         global.WaterGlobalArea[a]["AmountBonusValue"] = SBonus
  430.         global.WaterGlobalArea[a]["WaterBodyType"] = 5
  431.     else
  432.         --game.print("OCEAN")
  433.         global.WaterGlobalArea[a]["AmountWtr"] = WaterTotal * OBonus
  434.         global.WaterGlobalArea[a]["AmountBonusValue"] = OBonus
  435.         global.WaterGlobalArea[a]["WaterBodyType"] = 6
  436.     end
  437.     local RegenOff = settings.global["Disable-FluidArea-RegenRate"].value
  438.     if RegenOff == false then
  439.         local RegenRate = settings.global["FluidArea-RegenRate"].value / 10000
  440.         RegenAmount = (RegenRate * TotalArea)
  441.     else
  442.         RegenAmount = 0
  443.     end
  444.     global.WaterGlobalArea[a]["RegenAmount"] = RegenAmount
  445. end
  446.  
  447. function CorrectPump()
  448.     if global.OPLocate[#global.OPLocate]["entity"].valid then
  449.         if global.OPLocate[#global.OPLocate]["entity"]["name"] == "offshore-pump" then
  450.             if global.OPLocate[#global.OPLocate]["tile"] == "crude-oil" then
  451.                 local OPE = global.OPLocate[#global.OPLocate]["entity"]
  452.                 local OPD = OPE.direction
  453.                 local OPP = OPE.position
  454.                 local OPS = OPE.surface
  455.                 OPE.destroy()
  456.                 local OPN = game.surfaces[OPS.name].create_entity{name="offshore-crude-oil-pump",position = OPP,direction = OPD,force = "player"}
  457.                 global.OPLocate[#global.OPLocate]["entity"] = OPN
  458.             end
  459.         end
  460.     else
  461.         game.print("Invalid Pump")
  462.     end
  463. end
  464.  
  465. function CorrectedFluid(tile)
  466.     if tile == "crude-oil-deep" or tile == "crude-oil" then
  467.         correctedtile = "crude-oil"
  468.     else
  469.         correctedtile = "water"
  470.     end
  471.     return correctedtile
  472. end
  473.  
  474. -- ON TICK DEPENDANT FUNCTIONS --
  475.  
  476. function RegenWater(a)
  477.     local DisableRegen = settings.global["Disable-FluidArea-RegenRate"].value
  478.     local WGA = global.WaterGlobalArea
  479.     if DisableRegen == false and WGA[a]["FluidType"] == "water" and WGA[a]["Percent"] > 0 and WGA[a]["Percent"] <= 99 and WGA[a]["Depleted"] ~= 1 then
  480.         local WGA = global.WaterGlobalArea
  481.         if WGA[a]["WtrUsed"] - WGA[a]["RegenAmount"] <= 0 then
  482.             WGA[a]["WtrUsed"] = 0
  483.         else
  484.             WGA[a]["WtrUsed"] = WGA[a]["WtrUsed"] - WGA[a]["RegenAmount"]
  485.         end
  486.         WGA[a]["PercentPrev"] = WGA[a]["Percent"]
  487.         WGA[a]["Percent"] = (WGA[a]["WtrUsed"] / WGA[a]["AmountWtr"]) * 100
  488.         PercentChange = WGA[a]["PercentPrev"] - WGA[a]["Percent"]
  489.     end
  490. end
  491.  
  492. function EverySec()
  493.     for a = 1, #global.WaterGlobalArea, 1 do
  494.         if #global.LandFill > 0 then
  495.             LandFill(a)
  496.         end
  497.         RegenWater(a)
  498.     end
  499. end
  500.  
  501. function EveryTick()
  502.     CheckWater()
  503.     for a = 1, #global.WaterGlobalArea, 1 do
  504.         if global.WaterGlobalArea[a]["ToSearch"] ~= nil then
  505.             FluidAreaContinue(a)
  506.         end
  507.     end
  508. end
  509.  
  510.  
  511. function EmptyDrainPipes()
  512.     local ODL = global.ODLocate
  513.     for a = 1, #ODL, 1 do
  514.         local WGA = global.WaterGlobalArea
  515.         if WGA[ODL[a]["WA"]]["Depleted"] ~= 1 and ODL[a]["Active"] == 1 and ODL[a]["entity"].neighbours[1][1] and ODL[a]["entity"].neighbours[1][1].fluidbox[1] ~= nil and WGA[ODL[a]["WA"]]["FluidType"] == ODL[a]["PipeFluid"] and ODL[a]["entity"].neighbours[1][1].get_fluid_count() > 0.005 then
  516.             if ODL[a]["entity"].neighbours[1][1]["name"] == "storage-tank" then
  517.                 fluid = ODL[a]["entity"].neighbours[1][1].fluidbox[1]
  518.                 if fluid.amount >= 20 then
  519.                     fluid.amount = 20
  520.                 elseif fluid.amount < 20 and fluid.amount > 0 then
  521.                     fluid.amount = fluid.amount
  522.                 elseif fluid.amount == 0 then
  523.                     fluid.amount = 0
  524.                 end
  525.                 ODL[a]["entity"].neighbours[1][1].remove_fluid{name=fluid.name,amount=fluid.amount}
  526.             else
  527.                 fluid = ODL[a]["entity"].neighbours[1][1].fluidbox[1]
  528.                 if fluid.amount >= 20 then
  529.                     fluid.amount = 20
  530.                 elseif fluid.amount < 20 and fluid.amount > 0 then
  531.                     fluid.amount = fluid.amount
  532.                 elseif fluid.amount == 0 then
  533.                     fluid.amount = 0
  534.                 end
  535.                 ODL[a]["entity"].neighbours[1][1].remove_fluid{name=fluid.name,amount=fluid.amount}
  536.             end
  537.         end
  538.         WGA[ODL[a]["WA"]]["WtrAdd"] = 0
  539.     end
  540.  end
  541.  
  542. function AssignTiles(a)
  543.     local WGA = global.WaterGlobalArea
  544.     local WGAA = WGA[a]["ShallowWater"] + WGA[a]["DeepWater"]
  545.     local WGAT = WGA[a]["WaterRepArea"]
  546.     local WGAFT = WGA[a]["FluidType"]
  547.     if PercentChange < 0 and WGA[a]["TilesSet"] ~= "P" then -- IF WATERAREA IS BEING DEPLETED (PUMPS IN CHARGE)
  548.         for b = 1, WGAA, 1 do
  549.             if WGAT[b] ~= nil then
  550.                 if WGAT[b]["OriginalName"] == "water" or WGAT[b]["OriginalName"] == "crude-oil" or WGAT[b]["OriginalName"] == "water-green" or WGAT[b]["OriginalName"] == "water-shallow" or WGAT[b]["OriginalName"] == "water-mud" then
  551.                     WGA[a]["WaterRepArea"][b]["name"] = "sand-3"
  552.                 elseif WGAT[b]["OriginalName"] == "deepwater" or WGAT[b]["OriginalName"] == "crude-oil-deep" or WGAT[b]["OriginalName"] == "deepwater-green" then
  553.                     WGA[a]["WaterRepArea"][b]["name"] = "dry-dirt"
  554.                 end
  555.             end
  556.         end
  557.         WGA[a]["TilesSet"] = "P"
  558.     elseif PercentChange > 0 and WGA[a]["TilesSet"] ~= "D" then -- ELSE WATERAREA IS BEING FILLED (DRAINS IN CHARGE)
  559.         for b = 1, WGAA, 1 do
  560.             if WGAT[b] ~= nil then
  561.                 if WGAT[b]["name"] == "sand-3" then
  562.                     if WGAFT == "water" then
  563.                         if WGAT[b]["OriginalName"] == "water" then
  564.                             WGA[a]["WaterRepArea"][b]["name"] = "water"
  565.                         elseif WGAT[b]["OriginalName"] == "water-green" then
  566.                             WGA[a]["WaterRepArea"][b]["name"] = "water-green"
  567.                         elseif WGAT[b]["OriginalName"] == "water-shallow" then
  568.                             WGA[a]["WaterRepArea"][b]["name"] = "water-shallow"
  569.                         elseif WGAT[b]["OriginalName"] == "water-mud" then
  570.                             WGA[a]["WaterRepArea"][b]["name"] = "water-mud"
  571.                         end
  572.                     else
  573.                         WGA[a]["WaterRepArea"][b]["name"] = "crude-oil"
  574.                     end
  575.                 elseif WGAT[b]["name"] == "dry-dirt" then
  576.                     if WGAFT == "water" then
  577.                         if WGAT[b]["OriginalName"] == "deepwater" then
  578.                             WGA[a]["WaterRepArea"][b]["name"] = "deepwater"
  579.                         elseif WGAT[b]["OriginalName"] == "deepwater-green" then
  580.                             WGA[a]["WaterRepArea"][b]["name"] = "deepwater-green"
  581.                         end
  582.                     else
  583.                         WGA[a]["WaterRepArea"][b]["name"] = "crude-oil-deep"
  584.                     end
  585.                 end
  586.             end
  587.         end
  588.         WGA[a]["TilesSet"] = "D"
  589.     end
  590. end
  591.  
  592. function WaterRandom(a)
  593.     local WGA = global.WaterGlobalArea
  594.     local WaterTiles = (WGA[a]["ShallowWater"] + WGA[a]["DeepWater"])
  595.     local Surface = WGA[a]["Surface"]
  596.     local finalper = 20
  597.     local TilePerPercent = math.floor((WaterTiles / finalper) * 0.8)
  598.     if TilePerPercent < 1 then
  599.         TilePerPercent = 1
  600.     end
  601.     local randnotiles = math.random(1,TilePerPercent)
  602.     for z = 1, randnotiles, 1 do
  603.         local rand = math.random(1,WaterTiles)
  604.         game.surfaces[Surface.name].set_tiles({WGA[a]["WaterRepArea"][rand]})
  605.     end
  606. end
  607.  
  608. function BackToFront(a)
  609.     local WGA = global.WaterGlobalArea
  610.     local WaterTiles = (WGA[a]["ShallowWater"] + WGA[a]["DeepWater"])
  611.     local Surface = WGA[a]["Surface"]
  612.     local TileCount = WGA[a]["BTF"]
  613.     local WAArea = WGA[a]["WaterRepArea"]
  614.     local finalper = 20
  615.     local TilePerPercent = math.floor(WaterTiles / finalper)
  616.     if TilePerPercent < 1 then
  617.         TilePerPercent = 1
  618.     end
  619.     if PercentChange < 0 and TileCount ~= 0 then    -- PUMPS
  620.         if TilePerPercent > TileCount then
  621.             TilePerPercent = TileCount - 1
  622.         end
  623.         for b=TilePerPercent, 0, -1 do
  624.             if WAArea[TileCount] ~= nil then
  625.                 --if WAArea[TileCount]["name"] == "sand-3" or WAArea[TileCount]["name"] == "dry-dirt" then
  626.                     game.surfaces[Surface.name].set_tiles({WAArea[TileCount]})
  627.                     WGA[a]["BTF"] = WGA[a]["BTF"] - 1
  628.                     TileCount = WGA[a]["BTF"]
  629.                 --end
  630.             else
  631.                 WGA[a]["BTF"] = WGA[a]["BTF"] - 1
  632.                 TileCount = WGA[a]["BTF"]
  633.             end
  634.         end
  635.     elseif PercentChange > 0 and TileCount <= WaterTiles then -- DRAINS
  636.         if TileCount == 0 then
  637.             TileCount = 1
  638.         end
  639.         for b=TilePerPercent, 0, -1 do
  640.             if WAArea[TileCount] ~= nil then
  641.                 --if WAArea[TileCount]["name"] == "water" or WAArea[TileCount]["name"] == "deepwater" or WAArea[TileCount]["name"] == "crude-oil" or WAArea[TileCount]["name"] == "crude-oil-deep" then
  642.                     game.surfaces[Surface.name].set_tiles({WAArea[TileCount]})
  643.                     WGA[a]["BTF"] = WGA[a]["BTF"] + 1
  644.                     TileCount = WGA[a]["BTF"]
  645.                 --end
  646.                 if TileCount > WaterTiles then
  647.                     TileCount = WaterTiles
  648.                 end
  649.             else
  650.                 WGA[a]["BTF"] = WGA[a]["BTF"] + 1
  651.                 TileCount = WGA[a]["BTF"]
  652.             end
  653.         end
  654.     end
  655. end
  656.  
  657. function AddedWaterArea(a)
  658.     local WGA = global.WaterGlobalArea
  659.     if WGA[a]["AmountWtr"] ~= WGA[a]["WtrUsed"] and WGA[a]["Percent"] > 0 then
  660.         local Percent = WGA[a]["Percent"]
  661.         local WA = WGA[a]
  662.         local LowAlarmEnabled = settings.global["Alarms-Low-Level (50/75/90%)"].value
  663.         local HighAlarmEnabled = settings.global["Alarms-High-Level (95/97/98/99%)"].value
  664.         if Percent < 100 and Percent > 80 then
  665.             WGA[a]["Depleted"] = 0
  666.             local RP = WA["RandPercent"]
  667.             if (RP - Percent) >= 0.1 then
  668.                 local Method = settings.global["FluidArea-Replace-Method"].value
  669.                 if Method == "Random" then
  670.                     WaterRandom(a)
  671.                 elseif Method == "From/To Pump" then
  672.                     BackToFront(a)
  673.                 end
  674.                 WA["RandPercent"] = Percent
  675.             end
  676.             if Percent < 99 and WA["Fired99"] == true and HighAlarmEnabled == true then
  677.                 WA["Fired99"] = false
  678.             elseif Percent < 98 and WA["Fired98"] == true and HighAlarmEnabled == true then
  679.                 WA["Fired98"] = false
  680.             elseif Percent < 97 and WA["Fired97"] == true and HighAlarmEnabled == true then
  681.                 WA["Fired97"] = false
  682.             elseif Percent < 95 and WA["Fired95"] == true and HighAlarmEnabled == true then
  683.                 WA["Fired95"] = false
  684.             elseif Percent < 90 and WA["Fired90"] == true and HighAlarmEnabled == true then
  685.                 WA["Fired90"] = false
  686.             end
  687.         elseif Percent <= 80 and Percent > 0 then
  688.             WA["RandPercent"] = 79
  689.             if Percent < 75 and WA["Fired75"] == true and LowAlarmEnabled == true then
  690.                 WA["Fired75"] = false
  691.             elseif Percent < 50 and WA["Fired50"] == true and LowAlarmEnabled == true then
  692.                 WA["Fired50"] = false
  693.             end
  694.             if WA["Below80"] == 0 then
  695.                 if Method == "Random" then
  696.                     game.surfaces[WA["Surface"]].set_tiles(WA["WaterRepArea"])
  697.                 elseif Method == "From/To Pump" then
  698.                     -- Should Be Filled
  699.                 end
  700.                 WA["Below80"] = 1
  701.                 WGA[a]["BTF"] = WGA[a]["ShallowWater"] + WGA[a]["DeepWater"]
  702.             end
  703.         elseif Percent <= 0 then
  704.             -- DO NOTHING
  705.         end
  706.     end
  707. end
  708.  
  709. function DepleatedWaterArea(a)
  710.     local WGA = global.WaterGlobalArea
  711.     if WGA[a]["Depleted"] ~= 1 then
  712.         local Percent = WGA[a]["Percent"]
  713.         local LowAlarmEnabled = settings.global["Alarms-Low-Level (50/75/90%)"].value
  714.         local HighAlarmEnabled = settings.global["Alarms-High-Level (95/97/98/99%)"].value
  715.         local WA = WGA[a]
  716.         if Percent <= 49 then
  717.             --game.print("IN NORMAL BOUNDS")
  718.         elseif Percent >= 50 and WA["Fired50"] == false and LowAlarmEnabled == true then
  719.             game.print(string.format("%s has used %.0f %% of available %s.",WA["WtrName"], WA["Percent"], WA["FluidType"]))
  720.             WA["Fired50"] = true
  721.         elseif Percent >= 75 and WA["Fired75"] == false and LowAlarmEnabled == true then
  722.             game.print(string.format("%s has used %.0f %% of available %s.",WA["WtrName"], WA["Percent"], WA["FluidType"]))
  723.             WA["Fired75"] = true
  724.         elseif Percent >= 80 and Percent < 100 then
  725.             WA["Below80"] = 0
  726.             local RP = WA["RandPercent"]
  727.             if (Percent - RP) >= 1 then
  728.                 Method = settings.global["FluidArea-Replace-Method"].value
  729.                 if Method == "Random" then
  730.                     WaterRandom(a)
  731.                 elseif Method == "From/To Pump" then
  732.                     BackToFront(a)
  733.                 end
  734.                 WA["RandPercent"] = Percent
  735.             end
  736.             if Percent >= 90 and WA["Fired90"] == false and LowAlarmEnabled == true then
  737.                 game.print(string.format("%s has used %.0f %% of available %s.",WA["WtrName"], WA["Percent"], WA["FluidType"]))
  738.                 WA["Fired90"] = true
  739.             elseif Percent >= 95 and WA["Fired95"] == false and HighAlarmEnabled == true then
  740.                 game.print(string.format("%s has used %.0f %% of available %s.",WA["WtrName"], WA["Percent"], WA["FluidType"]))
  741.                 WA["Fired95"] = true
  742.             elseif Percent >= 97 and WA["Fired97"] == false and HighAlarmEnabled == true then
  743.                 game.print(string.format("%s has used %.0f %% of available %s.",WA["WtrName"], WA["Percent"], WA["FluidType"]))
  744.                 WA["Fired97"] = true
  745.             elseif Percent >= 98 and WA["Fired98"] == false and HighAlarmEnabled == true then
  746.                 game.print(string.format("%s has used %.0f %% of available %s.",WA["WtrName"], WA["Percent"], WA["FluidType"]))
  747.                 WA["Fired98"] = true
  748.             elseif Percent >= 99 and WA["Fired99"] == false and HighAlarmEnabled == true then
  749.                 game.print(string.format("%s has used %.0f %% of available %s.",WA["WtrName"], WA["Percent"], WA["FluidType"]))
  750.                 WA["Fired99"] = true
  751.             end    
  752.         elseif Percent >= 100 then
  753.             if global.NewInstall == false then
  754.                 for z = 1, #global.OPLocate, 1 do
  755.                     local OP = global.OPLocate[z]
  756.                     if OP["WA"] == a and OP["name"] ~= "offshore-pump-nofluid" then
  757.                         local OPD = OP["direction"]
  758.                         local OPE = OP["entity"]
  759.                         local OPP = OP["position"]
  760.                         local OPS = OP["surface"]
  761.                         if OP["Active"] == 1 then
  762.                             OP["Active"] = 0
  763.                             ActiveOPs = ActiveOPs - 1
  764.                             WA["OPsA"] = WA["OPsA"] - 1
  765.                         end
  766.                         if game.active_mods["aai-industry"] then -- MOD Compat with AAI, remove the additional sprites
  767.                             local x = OPP.x
  768.                             local y = OPP.y
  769.                             if OPD == 0 then
  770.                                 AS = game.surfaces[OPS.name].find_entities_filtered{area={{x-1, 0},{x+1,y+1}}, name = "offshore-pump-output"}
  771.                             elseif OPD == 4 then
  772.                                 AS = game.surfaces[OPS.name].find_entities_filtered{area={{x-1, y-1},{x+1,0}}, name = "offshore-pump-output"}
  773.                             elseif OPD == 2 then
  774.                                 AS = game.surfaces[OPS.name].find_entities_filtered{area={{0, y-1},{x+1,y+1}}, name = "offshore-pump-output"}
  775.                             elseif OPD == 6 then
  776.                                 AS = game.surfaces[OPS.name].find_entities_filtered{area={{0, y-1},{x+1,y+1}}, name = "offshore-pump-output"}
  777.                             end
  778.                             local Entity = AS[1]
  779.                             if Entity ~= nil then
  780.                                 Entity.destroy()
  781.                             end
  782.                         end
  783.                         OPE.destroy()
  784.                         local OPNF = game.surfaces[OPS.name].create_entity{name="offshore-pump-nofluid",position = OPP,direction = OPD,force = "player"}
  785.                         global.OPLocate[z]["entity"] = OPNF
  786.                     end
  787.                 end
  788.                 WGA[a]["Depleted"] = 1
  789.                 WGA[a]["Percent"] = 100
  790.                 WGA[a]["PercentPrev"] = 100
  791.                 WGA[a]["RandPercent"] = 100
  792.                 game.surfaces[WA["Surface"].name].set_tiles(WGA[a]["WaterRepArea"])                                                 -- Set Tiles to those in WaterArea Table
  793.                 WGA[a]["WtrUsed"] = WGA[a]["AmountWtr"]
  794.                 game.print(string.format("%s has been depleted of %s.",WA["WtrName"], WA["FluidType"]))
  795.                 WGA[a]["FluidType"] = "None"
  796.                 WGA[a]["BTF"] = 0
  797.             else
  798.                 local CurrentTick = game.tick
  799.                 if CurrentTick < (global.InstallTick + 18000) and global.ITMessage == true then
  800.                     game.print(string.format("Fluid Area Depletion Stopped on New/Mid Game Install. 5 Minutes from install till depletion."))
  801.                     global.ITMessage = false
  802.                 end
  803.                 if CurrentTick >= (global.InstallTick + 18000) then
  804.                     game.print(string.format("Fluid Area 5 Mins Over. Depleting Fluid Area."))
  805.                     WGA[a]["Percent"] = 99
  806.                     global.NewInstall = false
  807.                     global.ITMessage = false
  808.                 end
  809.             end
  810.         end
  811.     else
  812.         -- AREA DEPLETED
  813.     end
  814. end
  815.  
  816. function RemoveAllDrainedArea()
  817.     for a = #global.WaterGlobalArea, 1, -1 do   -- FOR Each WATERAREA
  818.         if global.WaterGlobalArea[a]["Depleted"] == 1 then
  819.             -- hack only remove the list of tiles
  820.             global.WaterGlobalArea[a]["WaterRepArea"] = {}
  821.         end
  822.     end
  823. end
  824.  
  825. function CalcWaterUse(a)
  826.     local WGA = global.WaterGlobalArea
  827.     local WaterAreaUsed = WGA[a]["WtrUsed"]
  828.     local WaterAreaDrained = WGA[a]["WtrAdd"]
  829.     local WaterAreaAmount = WGA[a]["AmountWtr"]
  830.     local WaterAreaPercent = WGA[a]["Percent"]
  831.     local WaterFlowRate = WaterFlow - LastWaterFlow
  832.     local CrudeFlowRate = CrudeFlow - LastCrudeFlow
  833.     local WaterAreaActivePumps = WGA[a]["OPsA"]
  834.     local WaterAreaActiveDrains = WGA[a]["ODsA"]
  835.     local WAAD = WGA[a]["ODsA"]
  836.     if ActiveOPs == 0 then
  837.         ActiveOPs = 1
  838.     end
  839.     local PumpRatio = WaterAreaActivePumps / ActiveOPs
  840.     if WGA[a]["FluidType"] == "water" then
  841.         FluidFlowRate = WaterFlowRate
  842.     elseif WGA[a]["FluidType"] == "crude-oil" then
  843.         FluidFlowRate = CrudeFlowRate - CrudeAdjust
  844.     elseif WGA[a]["FluidType"] == nil then
  845.         FluidFlowRate = 0
  846.     end
  847.     if FluidFlowRate < 0 then
  848.         FluidFlowRate = 0
  849.     end
  850.     local check = WaterAreaActivePumps * 20
  851.     if FluidFlowRate > check then
  852.         FluidFlowRate = WaterAreaActivePumps * 20
  853.     end
  854.     local FluidRatePerPump = FluidFlowRate * PumpRatio
  855.     if WaterAreaUsed == 0 then
  856.         --game.print("START UP")
  857.         WGA[a]["WtrUsed"] = WaterAreaUsed + FluidRatePerPump
  858.         WGA[a]["Depleted"] = 0
  859.     elseif WaterAreaUsed < 0 then
  860.         --game.print("WA < 0")
  861.         WGA[a]["WtrUsed"] = 0
  862.         WGA[a]["Depleted"] = 0
  863.     elseif WaterAreaUsed == WaterAreaAmount then
  864.         --game.print("WA = AMOUNT")
  865.         WGA[a]["WtrUsed"] = (WaterAreaUsed - WaterAreaDrained)
  866.     elseif WaterAreaDrained == 0 and WAAD > 0 and WaterAreaPercent < 0.3 then
  867.         --game.print("WD = 0")
  868.         WGA[a]["WtrUsed"] = 0
  869.     elseif WaterAreaUsed - WaterAreaDrained > 0 then
  870.         --game.print("WU - WD > 0")
  871.         if WaterAreaActivePumps > 0 and WaterAreaActiveDrains > 0 then
  872.             --game.print("WU AND WA ACTIVE")
  873.             WGA[a]["WtrUsed"] = WaterAreaUsed + (FluidRatePerPump - WaterAreaDrained)
  874.         elseif WaterAreaActivePumps == 0 and WaterAreaActiveDrains > 0 then
  875.             --game.print("WD ACTIVE")
  876.             WGA[a]["WtrUsed"] = WaterAreaUsed - WaterAreaDrained
  877.         elseif WaterAreaActivePumps > 0 and WaterAreaActiveDrains == 0 then
  878.             --game.print("WA ACTIVE")
  879.             WGA[a]["WtrUsed"] = WaterAreaUsed + FluidRatePerPump
  880.         end
  881.     elseif WaterAreaUsed - WaterAreaDrained < 0 then
  882.         --game.print("WU - WD < 0")
  883.         WGA[a]["WtrUsed"] = 0
  884.         WGA[a]["Depleted"] = 0
  885.     else
  886.         --game.print("WU = WU")
  887.         WGA[a]["WtrUsed"] = WaterAreaUsed
  888.     end
  889.     WGA[a]["PercentPrev"] = WGA[a]["Percent"]
  890.     WGA[a]["Percent"] = (WGA[a]["WtrUsed"] / WGA[a]["AmountWtr"]) * 100
  891.     PercentChange = WGA[a]["PercentPrev"] - WGA[a]["Percent"]
  892. end
  893.  
  894. function CheckDrainAssignedFluidUse(a) -- Assign Fluid to Empty WaterArea
  895.     for c = 1, #global.ODLocate,1 do
  896.         local ODL = global.ODLocate
  897.         local WGA = global.WaterGlobalArea
  898.         if ODL[c]["WA"] == a and ODL[c]["entity"].neighbours[1][1] and ODL[c]["entity"].neighbours[1][1].fluidbox[1] ~= nil and ODL[c]["PipeFluid"] == WGA[a]["FluidType"] then
  899.             if ODL[c]["entity"].neighbours[1][1].get_fluid_count() > 0.005 then
  900.                 fluid = ODL[c]["entity"].neighbours[1][1].fluidbox[1]
  901.                 if ODL[c]["entity"].neighbours[1][1]["name"] == "storage-tank" then
  902.                     if fluid.amount >= 20 then
  903.                         WGA[a]["WtrAdd"] = WGA[a]["WtrAdd"] + 20
  904.                     elseif fluid.amount < 20 and fluid.amount > 0 then
  905.                         WGA[a]["WtrAdd"] = WGA[a]["WtrAdd"] + 1
  906.                     elseif fluid.amount == 0 then
  907.                         WGA[a]["WtrAdd"] = WGA[a]["WtrAdd"]
  908.                     end
  909.                 else
  910.                     if fluid.amount >= 20 then
  911.                         WGA[a]["WtrAdd"] = WGA[a]["WtrAdd"] + 20
  912.                     elseif fluid.amount < 20 and fluid.amount > 0 then
  913.                         WGA[a]["WtrAdd"] = WGA[a]["WtrAdd"] + fluid.amount
  914.                     elseif fluid.amount == 0 then
  915.                         WGA[a]["WtrAdd"] = WGA[a]["WtrAdd"]
  916.                     end
  917.                 end
  918.             else
  919.                 WGA[a]["WtrAdd"] = WGA[a]["WtrAdd"]
  920.             end
  921.         end
  922.     end
  923. end
  924.  
  925. function CheckDrainAssignedFluid(a)
  926.     if ActiveODs ~= 0 then
  927.     local WGA = global.WaterGlobalArea
  928.     local ODL = global.ODLocate
  929.         for c = 1, #WGA, 1 do
  930.             if WGA[c]["FluidType"] == "None" then
  931.                 if ODL[a]["WA"] == c and ODL[a]["entity"].neighbours[1][1] and ODL[a]["entity"].neighbours[1][1].fluidbox[1] ~= nil then
  932.                     WGA[c]["FluidType"] = ODL[a]["PipeFluid"]
  933.                 end
  934.             end
  935.         end
  936.     end
  937. end
  938.  
  939. function CheckOPActive(a)
  940.     local GOPL = global.OPLocate[a]
  941.     local WGA = global.WaterGlobalArea
  942.     if GOPL["Active"] == 0 and WGA[GOPL["WA"]]["Depleted"] ~= 1 then
  943.         if GOPL["entity"].valid and GOPL["entity"].neighbours[1][1] then
  944.             GOPL["Active"] = 1
  945.             if WGA[GOPL["WA"]]["OPsA"] <= 0 then
  946.                 WGA[GOPL["WA"]]["OPsA"] = 1
  947.             else
  948.                 WGA[GOPL["WA"]]["OPsA"] = WGA[GOPL["WA"]]["OPsA"] + 1
  949.             end
  950.             ActiveOPs = ActiveOPs + 1
  951.         end
  952.     elseif GOPL["Active"] == 1 and WGA[GOPL["WA"]]["Depleted"] ~= 1 then
  953.         if GOPL["entity"].valid and GOPL["entity"].neighbours[1][1] then
  954.             GOPL["Active"] = 1
  955.             ActiveOPs = ActiveOPs + 1
  956.         elseif GOPL["entity"].valid and not GOPL["entity"].neighbours[1][1] then
  957.             GOPL["Active"] = 0
  958.             if WGA[GOPL["WA"]]["OPsA"] <= 0 then
  959.                 WGA[GOPL["WA"]]["OPsA"] = 0
  960.             else
  961.                 WGA[GOPL["WA"]]["OPsA"] = WGA[GOPL["WA"]]["OPsA"] - 1
  962.             end
  963.             ActiveOPs = ActiveOPs - 1
  964.         end
  965.     end
  966. end
  967.  
  968. function CheckODActive(a)
  969.     local GOPL = global.ODLocate[a]
  970.     local WGA = global.WaterGlobalArea
  971.     local ODL = global.ODLocate
  972.     if GOPL["Active"] == 0 then
  973.         if GOPL["entity"].neighbours[1][1] then
  974.             if GOPL["entity"].valid and GOPL["entity"].neighbours[1][1].fluidbox[1] ~= nil and WGA[ODL[a]["WA"]]["WtrUsed"] > 0.005 then
  975.                 GOPL["Active"] = 1
  976.                 if WGA[GOPL["WA"]]["ODsA"] <= 0 then
  977.                     WGA[GOPL["WA"]]["ODsA"] = 1
  978.                 else
  979.                     WGA[GOPL["WA"]]["ODsA"] = WGA[GOPL["WA"]]["ODsA"] + 1
  980.                 end
  981.                 ActiveODs = ActiveODs + 1
  982.                 GOPL["PipeFluid"] = GOPL["entity"].neighbours[1][1].fluidbox[1].name
  983.             end
  984.         else
  985.             GOPL["entity"].clear_fluid_inside()
  986.         end
  987.     elseif GOPL["Active"] == 1 then
  988.         if GOPL["entity"].valid and GOPL["entity"].neighbours[1][1] and GOPL["entity"].neighbours[1][1].fluidbox[1] ~= nil and WGA[ODL[a]["WA"]]["WtrUsed"] > 0.005 then
  989.             GOPL["Active"] = 1
  990.             ActiveODs = ActiveODs + 1
  991.             GOPL["PipeFluid"] = GOPL["entity"].neighbours[1][1].fluidbox[1].name
  992.         elseif GOPL["entity"].valid or not GOPL["entity"].neighbours[1][1] or GOPL["entity"].neighbours[1][1].fluidbox[1] == nil or WGA[ODL[a]["WA"]]["Percent"] <= 0 then
  993.             GOPL["Active"] = 0
  994.             if WGA[GOPL["WA"]]["ODsA"] <= 0 then
  995.                 WGA[GOPL["WA"]]["ODsA"] = 0
  996.             else
  997.                 WGA[GOPL["WA"]]["ODsA"] = WGA[GOPL["WA"]]["ODsA"] - 1
  998.             end
  999.             ActiveODs = ActiveODs - 1
  1000.             GOPL["PipeFluid"] = "None"
  1001.         end
  1002.     end
  1003. end
  1004.  
  1005. function LandFill(a)
  1006.     local WGA = global.WaterGlobalArea
  1007.     WASize = #WGA[a]["WaterRepArea"]
  1008.     WAra = WGA[a]["WaterRepArea"]
  1009.     for b = #global.LandFill, 1, -1 do
  1010.         LFPosX = global.LandFill[b]["position"]["x"] + 0.5
  1011.         LFPosY = global.LandFill[b]["position"]["y"] + 0.5
  1012.         for c = 1, WASize, 1 do
  1013.             if WAra[c]["name"] ~= "landfill" then
  1014.                 WAPosX = WAra[c]["position"]["x"]
  1015.                 WAPosY = WAra[c]["position"]["y"]
  1016.                 if LFPosX == WAPosX then -- IF LandFill position x is equal to FluidArea position x
  1017.                     if LFPosY == WAPosY then -- IF LandFill position y is equal to FluidArea position y
  1018.                     global.WaterGlobalArea[a]["WaterRepArea"][c]["name"] = "landfill"
  1019.                     local FluidName = global.LandFill[b]["name"]
  1020.                     if FluidName == "water" or FluidName == "crude-oil" or FluidName == "water-green" or FluidName == "water-shallow" or FluidName == "water-mud" then
  1021.                         local ShallowAmount = settings.global["TileFluidAmount-Shallow"].value
  1022.                         global.WaterGlobalArea[a]["AmountWtr"] = WGA[a]["AmountWtr"] - (ShallowAmount * WGA[a]["AmountBonusValue"])
  1023.                     elseif FluidName == "deepwater" or FluidName == "crude-oil-deep" or FluidName == "deepwater-green" then
  1024.                         local DeepAmount = settings.global["TileFluidAmount-Deep"].value
  1025.                         global.WaterGlobalArea[a]["AmountWtr"] = WGA[a]["AmountWtr"] - (DeepAmount * WGA[a]["AmountBonusValue"])
  1026.                     end
  1027.                     table.remove(global.LandFill,#global.LandFill)
  1028.                     goto EscapeLFSearch
  1029.                     end
  1030.                 end
  1031.             end
  1032.             if a == #global.WaterGlobalArea and c == WASize then
  1033.                 table.remove(global.LandFill,#global.LandFill)
  1034.                 goto EscapeLFSearch
  1035.             end
  1036.         end
  1037.     ::EscapeLFSearch::
  1038.     end
  1039.     if #global.LandFill == 0 then
  1040.         local LandfillEnabled = settings.global["Alarms-Landfill Message"].value
  1041.         game.print(string.format("Landfill has reduced FluidArea %s, to %sL of %s.", WGA[a]["WtrName"], WGA[a]["AmountWtr"], WGA[a]["FluidType"]))
  1042.     end
  1043.     ::EscapeLFSearchNF::
  1044. end
  1045.  
  1046. function CheckActive()
  1047.     ActiveOPs = 0
  1048.     ActiveODs = 0
  1049.     if #global.OPLocate ~= 0 then
  1050.         for a = 1, #global.OPLocate, 1 do
  1051.             CheckOPActive(a)
  1052.         end
  1053.     end
  1054.     if #global.ODLocate ~= 0 then
  1055.         for a = 1, #global.ODLocate, 1 do
  1056.             CheckODActive(a)
  1057.             CheckDrainAssignedFluid(a)
  1058.         end
  1059.     end
  1060. end
  1061.  
  1062. function ScanOffshores()
  1063.     for a = #game.surfaces, 1, -1 do
  1064.         local Jacks = game.surfaces[a].find_entities_filtered{name= "pumpjack"}
  1065.         if #Jacks > 0 then
  1066.             for b = 1, #Jacks, 1 do
  1067.                 Jackentity = Jacks[b]
  1068.                 Jackposition = Jackentity.position 
  1069.                 Jacksurface = Jackentity.surface
  1070.                 global.FluidProducers[#global.FluidProducers+1] = {["entity"] = Jackentity, ["position"] = {["x"] = Jackposition.x , ["y"] = Jackposition.y}, ["surface"] = Jacksurface, ["FluidType"] = nil, ["LastAmount"] = 0}
  1071.             end
  1072.         end
  1073.         local Pumps = game.surfaces[a].find_entities_filtered{name= "offshore-pump"}
  1074.         if #Pumps > 0 then
  1075.             for b = 1, #Pumps, 1 do
  1076.                 Pumpentity = Pumps[b]
  1077.                 Pumpposition = Pumpentity.position                                                  -- Variable for e.Position
  1078.                 Pumpsurface = Pumpentity.surface                                                    -- Variable for e.Surface
  1079.                 Pumpdirection = Pumpentity.direction                                                -- Variable for OPentity.direction
  1080.                 global.OPLocate[#global.OPLocate+1] = {["entity"] = Pumpentity,["position"] = {["x"] = Pumpposition.x, ["y"] = Pumpposition.y},["surface"] = Pumpsurface,["direction"] = Pumpdirection,["tile"] = "water", ["Active"] = 0, ["WA"] = 0}
  1081.                 global.Type = 1
  1082.                 CorrectPump()
  1083.                 GlobalWaterArea()
  1084.                 global.Type = 0
  1085.             end
  1086.         end
  1087.     end
  1088.     global.Added = false
  1089. end
  1090.  
  1091. function PumpRereplace(a)
  1092.     local PRPV = settings.global["FluidArea-RereplacePumps"].value
  1093.     local PRP = 100 - PRPV
  1094.     local WGA = global.WaterGlobalArea
  1095.     if PRPV == 0 then
  1096.         --Do Nothing as Off
  1097.     else
  1098.         local FAP = WGA[a]["Percent"]
  1099.         if PRP >= FAP then
  1100.             for b = #global.OPLocate, 1, -1 do
  1101.            
  1102.                 ----------------------------------------- Dexy Edit ------------------------------------
  1103.                 if global.OPLocate[b]["entity"].valid == false then
  1104.                     return
  1105.                 end
  1106.                 ----------------------------------------------------------------------------------------
  1107.            
  1108.                 if global.OPLocate[b]["WA"] == a and global.OPLocate[b]["entity"].name == "offshore-pump-nofluid"then
  1109.                     local OP = global.OPLocate[b]
  1110.                     local OPD = OP["direction"]
  1111.                     local OPE = OP["entity"]
  1112.                     local OPP = OP["position"]
  1113.                     local OPS = OP["surface"]
  1114.                     OPE.destroy()
  1115.                     if WGA[a]["FluidType"] == "water" then
  1116.                         OPN = game.surfaces[OPS.name].create_entity{name="offshore-pump",position = OPP,direction = OPD,force = "player"}
  1117.                     elseif WGA[a]["FluidType"] == "crude-oil" then
  1118.                         OPN = game.surfaces[OPS.name].create_entity{name="offshore-crude-oil-pump",position = OPP,direction = OPD,force = "player"}
  1119.                     end
  1120.                     global.OPLocate[b]["entity"] = OPN
  1121.                     if global.OPLocate[b]["entity"] == nil then
  1122.                         if global.WaterGlobalArea[global.OPLocate[b]["WA"]]["OPs"] <= 0 then
  1123.                             global.WaterGlobalArea[global.OPLocate[b]["WA"]]["OPs"] = 0
  1124.                         else
  1125.                             global.WaterGlobalArea[global.OPLocate[b]["WA"]]["OPs"] = global.WaterGlobalArea[global.OPLocate[b]["WA"]]["OPs"] - 1
  1126.                         end
  1127.                         game.print(string.format("Water As A Resource: Offshore Pump Removed from %s, as Pipes have another fluid type.", global.WaterGlobalArea[global.OPLocate[b]["WA"]]["WtrName"]))
  1128.                         table.remove(global.OPLocate,b)
  1129.                         game.surfaces[OPS.name].spill_item_stack(OPP, {name="offshore-pump",count=1})
  1130.                     end
  1131.                 end
  1132.             end
  1133.         end
  1134.     end
  1135. end
  1136.  
  1137. function CheckWater ()
  1138.     local WGA = global.WaterGlobalArea
  1139.     fluidentities.CheckFluidProducers()
  1140.     if global.Added == true then
  1141.         ScanOffshores()
  1142.     end
  1143.     if #WGA ~= nil and global.Added ~= true then
  1144.         WaterFlow = math.ceil(game.forces["player"].fluid_production_statistics.get_input_count("water"))
  1145.         CrudeFlow = math.ceil(game.forces["player"].fluid_production_statistics.get_input_count("crude-oil"))
  1146.         CheckActive()
  1147.         for a = 1, #WGA, 1 do
  1148.             if WGA[a]["OPsA"] > 0 or WGA[a]["ODsA"] > 0 then
  1149.                 CheckDrainAssignedFluidUse(a)
  1150.                 CalcWaterUse(a)
  1151.                 AssignTiles(a)
  1152.             end
  1153.             if PercentChange <= 0 then
  1154.                 DepleatedWaterArea(a)
  1155.             elseif PercentChange > 0 then
  1156.                 AddedWaterArea(a)
  1157.                 PumpRereplace(a)
  1158.             end
  1159.             if WGA[a]["ODsA"] > 0 then
  1160.                 EmptyDrainPipes()
  1161.             end
  1162.         end
  1163.         LastWaterFlow = WaterFlow
  1164.         LastCrudeFlow = CrudeFlow
  1165.     end
  1166. end
  1167.  
  1168. -- SCRIPT EVENT FUNCTIONS --
  1169.  
  1170. function LandFillCheck(event)
  1171.     if event.item.valid == true then
  1172.         if event.item.name == "landfill" then
  1173.             local tiles = event.tiles
  1174.             local surface = event.surface_index
  1175.             for a = 1, #tiles, 1 do
  1176.                 global.LandFill[#global.LandFill+1] = {["name"] = tiles[a].old_tile.name, ["position"] = {["x"] = tiles[a]["position"].x, ["y"] = tiles[a]["position"].y},["surface"] = surface}
  1177.             end
  1178.         end
  1179.     end
  1180. end
  1181.  
  1182. function BuiltOffShore(event)                   -- Script Event On Built
  1183.     if event.created_entity.valid == true then
  1184.         if event.created_entity.name =="offshore-pump" then                                 -- If Offshore Pump Built
  1185.             OPentity = event.created_entity                                                 -- Variable for Event.created_entity
  1186.             OPposition = OPentity.position                                                  -- Variable for e.Position
  1187.             OPsurface = OPentity.surface                                                    -- Variable for e.Surface
  1188.             OPdirection = OPentity.direction                                                -- Variable for OPentity.direction
  1189.             tile = OPsurface.get_tile(OPposition.x,OPposition.y).name
  1190.             CorrectedFluid(tile)
  1191.             global.OPLocate[#global.OPLocate+1] = {["entity"] = OPentity,["position"] = {["x"] = OPposition.x, ["y"] = OPposition.y},["surface"] = OPsurface,["direction"] = OPdirection,["tile"] = correctedtile, ["Active"] = 0, ["WA"] = 0}
  1192.             global.Type = 1
  1193.             CorrectPump()
  1194.             GlobalWaterArea()
  1195.             global.Type = 0
  1196.         elseif event.created_entity.name =="offshore-drain" then
  1197.             ODentity = event.created_entity
  1198.             ODposition = ODentity.position
  1199.             ODsurface = ODentity.surface
  1200.             ODdirection = ODentity.direction
  1201.             tile = ODsurface.get_tile(ODposition.x,ODposition.y).name
  1202.             CorrectedFluid(tile)
  1203.             global.ODLocate[#global.ODLocate+1] = {["entity"] = ODentity,["position"] = {["x"] = ODposition.x, ["y"] = ODposition.y},["surface"] = ODsurface,["direction"] = ODdirection, ["tile"] = correctedtile, ["PipeFluid"] = nil, ["Active"] = 0, ["WA"] = 0}
  1204.             global.Type = 2
  1205.             GlobalWaterArea()
  1206.             global.Type = 0
  1207.         elseif event.created_entity.name =="pumpjack" then
  1208.             FPentity = event.created_entity
  1209.             FPposition = FPentity.position 
  1210.             FPsurface = FPentity.surface
  1211.             global.FluidProducers[#global.FluidProducers+1] = {["entity"] = FPentity, ["position"] = {["x"] = FPposition.x , ["y"] = FPposition.y}, ["surface"] = FPsurface, ["FluidType"] = nil, ["LastAmount"] = 0}
  1212.         else
  1213.             --game.print("Not Built A Offshore Pump")                                   -- If Not Offshore Pump then print Bad Times
  1214.         end
  1215.         global.Type = 0
  1216.     else
  1217.         game.print("Entity Creation Was Invalid - Not Added")
  1218.     end
  1219. end
  1220.  
  1221. function DestroyedOffShore(event)           -- Script Event On Player Mined
  1222.     if event.entity.name == "offshore-pump" or event.entity.name == "offshore-pump-nofluid" or event.entity.name == "offshore-crude-oil-pump" then                              -- If Offshore Pump No Fluid Mined
  1223.         --game.print("PICKED UP MY OFFSHORE PUMP")                                  -- Print My Offshore Pump
  1224.         DOentity = event.entity
  1225.         DOposition = DOentity.position
  1226.         global.Type = 1
  1227.         DestroyOffshore()
  1228.         global.Type = 0
  1229.     elseif event.entity.name == "offshore-drain" then
  1230.         DOentity = event.entity
  1231.         if DOentity.neighbours[1][1] and DOentity.neighbours[1][1].get_fluid_count() ~= 0 then
  1232.             DOentity.neighbours[1][1].fluidbox[1] = nil
  1233.         end
  1234.         DOposition = DOentity.position
  1235.         global.Type = 2
  1236.         DestroyOffshore()
  1237.         global.Type = 0
  1238.     elseif event.entity.name == "pumpjack" then
  1239.         DOentity = event.entity
  1240.         DOposition = DOentity.position
  1241.         global.Type = 3
  1242.         DestroyOffshore()
  1243.         global.Type = 0
  1244.     end
  1245. end
  1246.  
  1247. function DestroyOffshore()
  1248.     if global.Type == 1 then
  1249.         PTYPE = global.OPLocate
  1250.         PTYPEL = #global.OPLocate
  1251.     elseif global.Type == 2 then
  1252.         PTYPE = global.ODLocate
  1253.         PTYPEL = #global.ODLocate
  1254.     elseif global.Type == 3 then
  1255.         PTYPE = global.FluidProducers
  1256.         PTYPEL = #global.FluidProducers
  1257.     end
  1258.     local DOPosX = DOposition.x
  1259.     local DOPosY = DOposition.y
  1260.     for a = PTYPEL, 1, -1 do
  1261.         local OPosX = PTYPE[a]["position"]["x"]
  1262.         local OPosY = PTYPE[a]["position"]["y"]
  1263.         if DOPosX == OPosX then
  1264.             if DOPosY == OPosY then
  1265.                 if global.Type == 1 then
  1266.                     local WGA = PTYPE[a]["WA"]
  1267.                     if #global.WaterGlobalArea ~= 0 and WGA ~= 0 then
  1268.                         global.WaterGlobalArea[WGA]["OPs"] = global.WaterGlobalArea[WGA]["OPs"] - 1
  1269.                     end                
  1270.                     if PTYPE[a]["Active"] == 1 and WGA ~= 0 then
  1271.                         global.WaterGlobalArea[WGA]["OPsA"] = global.WaterGlobalArea[WGA]["OPsA"] - 1
  1272.                         ActiveOPs = ActiveOPs - 1
  1273.                     end
  1274.                 elseif global.Type == 2 then
  1275.                     local WGA = PTYPE[a]["WA"]
  1276.                     if #global.WaterGlobalArea ~= 0 and WGA ~= 0 then
  1277.                     global.WaterGlobalArea[WGA]["ODs"] = global.WaterGlobalArea[WGA]["ODs"] - 1
  1278.                     end                
  1279.                     if PTYPE[a]["Active"] == 1 and WGA ~= 0 then
  1280.                     global.WaterGlobalArea[WGA]["ODsA"] = global.WaterGlobalArea[WGA]["ODsA"] - 1
  1281.                     ActiveODs = ActiveODs - 1
  1282.                     end
  1283.                 end
  1284.                 table.remove(PTYPE,a)
  1285.             end
  1286.         end
  1287.     end
  1288. end
  1289.  
  1290. function UpdateMod(data)
  1291.     if data.mod_changes.WaterAsAResource then
  1292.         oldVer = data.mod_changes.WaterAsAResource.old_version
  1293.         if oldVer == nil then
  1294.             global.LastWaterFlow = math.ceil(game.forces["player"].fluid_production_statistics.get_input_count("water"))
  1295.             global.LastCrudeFlow = math.ceil(game.forces["player"].fluid_production_statistics.get_input_count("crude-oil"))
  1296.             global.InstallTick = game.tick
  1297.         else
  1298.             --game.print("Water As A Resource: Updating Mod Internals")
  1299.             Globals()
  1300.             LinkGlobals()
  1301.             if #global.WaterGlobalArea > 0 then
  1302.                 for a = 1, #global.WaterGlobalArea, 1 do
  1303.                     local Count = global.WaterGlobalArea[a]["ShallowWater"] + global.WaterGlobalArea[a]["DeepWater"]
  1304.                     if global.WaterGlobalArea[a]["WtrName"] == nil or global.WaterGlobalArea[a]["WtrName"] == "None" then
  1305.                         global.WaterGlobalArea[a]["WtrName"] = a
  1306.                     end
  1307.                     if global.WaterGlobalArea[a]["AmountBonusValue"] == nil or global.WaterGlobalArea[a]["AmountBonusValue"] == 0 then
  1308.                         if Count < 4 then
  1309.                             global.WaterGlobalArea[a]["AmountBonusValue"] = 0.01
  1310.                         elseif Count == 4 then
  1311.                             global.WaterGlobalArea[a]["AmountBonusValue"] = 0.5
  1312.                         elseif Count >4 and Count <= 200 then
  1313.                             global.WaterGlobalArea[a]["AmountBonusValue"] = 1
  1314.                         elseif Count > 200 and Count <= 6000 then
  1315.                             global.WaterGlobalArea[a]["AmountBonusValue"] = 1.5
  1316.                         elseif Count > 6000 and Count <= 60000 then
  1317.                             global.WaterGlobalArea[a]["AmountBonusValue"] = 2
  1318.                         elseif Count > 60000 and Count <= 600000 then
  1319.                             global.WaterGlobalArea[a]["AmountBonusValue"] = 2.5
  1320.                         else
  1321.                             global.WaterGlobalArea[a]["AmountBonusValue"] = 3
  1322.                         end
  1323.                     end
  1324.                     if global.WaterGlobalArea[a]["WtrUsed"] == nil then
  1325.                         global.WaterGlobalArea[a]["WtrUsed"] = (global.WaterGlobalArea[a]["AmountWtr"] * global.WaterGlobalArea[a]["Percent"]) / 100
  1326.                     end
  1327.                     if global.WaterGlobalArea[a]["OPsA"] == nil or global.WaterGlobalArea[a]["OPsA"] < 0 then
  1328.                         global.WaterGlobalArea[a]["OPsA"] = 0
  1329.                     end
  1330.                     if global.WaterGlobalArea[a]["WtrAdd"] == nil then
  1331.                         global.WaterGlobalArea[a]["WtrAdd"] = 0
  1332.                     end
  1333.                     if global.WaterGlobalArea[a]["ODsA"] == nil or global.WaterGlobalArea[a]["ODsA"] < 0 then
  1334.                         global.WaterGlobalArea[a]["ODsA"] = 0
  1335.                     end
  1336.                     if global.WaterGlobalArea[a]["ODs"] == nil then
  1337.                         global.WaterGlobalArea[a]["ODs"] = 0
  1338.                     end
  1339.                     if global.WaterGlobalArea[a]["PercentPrev"] == nil then
  1340.                         global.WaterGlobalArea[a]["PercentPrev"] = global.WaterGlobalArea[a]["Percent"]
  1341.                     end
  1342.                     if global.WaterGlobalArea[a]["Depleted"] == 1 then
  1343.                         global.WaterGlobalArea[a]["FluidType"] = "None"
  1344.                         global.WaterGlobalArea[a]["Percent"] = 100
  1345.                         global.WaterGlobalArea[a]["PercentPrev"] = 100
  1346.                         global.WaterGlobalArea[a]["WtrUsed"] = global.WaterGlobalArea[a]["AmountWtr"]
  1347.                     else
  1348.                         global.WaterGlobalArea[a]["FluidType"] = "water"
  1349.                         global.WaterGlobalArea[a]["Percent"] = 0
  1350.                         global.WaterGlobalArea[a]["PercentPrev"] = 0
  1351.                         global.WaterGlobalArea[a]["WtrUsed"] = 0
  1352.                     end
  1353.                     if global.WaterGlobalArea[a]["ToSearch"] == nil or global.WaterGlobalArea[a]["ToSearch"] == 0 then
  1354.                         global.WaterGlobalArea[a]["ToSearch"] = nil
  1355.                     end
  1356.                     if global.WaterGlobalArea[a]["HasSearched"] == nil or global.WaterGlobalArea[a]["HasSearched"] == 0 then
  1357.                         global.WaterGlobalArea[a]["HasSearched"] = nil
  1358.                     end
  1359.                     if global.WaterGlobalArea[a]["TilesSet"] == nil then
  1360.                         global.WaterGlobalArea[a]["TilesSet"] = "N"
  1361.                     end
  1362.                     if global.WaterGlobalArea[a]["Percent"] < 50 then
  1363.                         global.WaterGlobalArea[a]["Fired50"] = false
  1364.                         global.WaterGlobalArea[a]["Fired75"] = false
  1365.                         global.WaterGlobalArea[a]["Fired90"] = false
  1366.                         global.WaterGlobalArea[a]["Fired95"] = false
  1367.                         global.WaterGlobalArea[a]["Fired97"] = false
  1368.                         global.WaterGlobalArea[a]["Fired98"] = false
  1369.                         global.WaterGlobalArea[a]["Fired99"] = false
  1370.                     end
  1371.                     if global.WaterGlobalArea[a]["Percent"] >= 50 then
  1372.                         global.WaterGlobalArea[a]["Fired50"] = true
  1373.                     end
  1374.                     if global.WaterGlobalArea[a]["Percent"] >= 75 then
  1375.                         global.WaterGlobalArea[a]["Fired75"] = false
  1376.                     end
  1377.                     if global.WaterGlobalArea[a]["Percent"] >= 90 then
  1378.                         global.WaterGlobalArea[a]["Fired90"] = true
  1379.                     end
  1380.                     if global.WaterGlobalArea[a]["Percent"] >= 95 then
  1381.                         global.WaterGlobalArea[a]["Fired95"] = true
  1382.                     end
  1383.                     if global.WaterGlobalArea[a]["Percent"] >= 97 then
  1384.                         global.WaterGlobalArea[a]["Fired97"] = true
  1385.                     end
  1386.                     if global.WaterGlobalArea[a]["Percent"] >= 98 then
  1387.                         global.WaterGlobalArea[a]["Fired98"] = true
  1388.                     end
  1389.                     if global.WaterGlobalArea[a]["Percent"] >= 99 then
  1390.                         global.WaterGlobalArea[a]["Fired99"] = true
  1391.                     end
  1392.                     global.WaterGlobalArea[a]["BTF"] = Count
  1393.                     for b = 1, Count, 1 do
  1394.                         local tile = global.WaterGlobalArea[a]["WaterRepArea"][b]["name"]
  1395.                         if tile == "water" or tile == "deepwater" then
  1396.                             global.WaterGlobalArea[a]["FluidType"] = "water"
  1397.                         elseif tile == "crude-oil" or tile == "crude-oil-deep" then
  1398.                             global.WaterGlobalArea[a]["FluidType"] = "crude-oil"
  1399.                         elseif global.WaterGlobalArea[a]["FluidType"] == nil then
  1400.                             global.WaterGlobalArea[a]["FluidType"] = "None"
  1401.                         end
  1402.                        
  1403.                     end
  1404.                     if global.WaterGlobalArea[a]["LoopCount"] == nil then
  1405.                         global.WaterGlobalArea[a]["LoopCount"] = 0
  1406.                     end
  1407.                     if global.WaterGlobalArea[a]["RegenAmount"] == nil then
  1408.                         global.WaterGlobalArea[a]["RegenAmount"] = 0
  1409.                     end
  1410.                     if global.WaterGlobalArea[a]["Percent"] >= 80 and global.WaterGlobalArea[a]["Percent"] < 100 then
  1411.                         local TPP = math.floor((global.WaterGlobalArea[a]["ShallowWater"] + global.WaterGlobalArea[a]["DeepWater"]) / 20)
  1412.                         local PF = math.floor(global.WaterGlobalArea[a]["Percent"] - 80) / 0.1
  1413.                         local Tiles = TPP * (PF/100)
  1414.                         global.WaterGlobalArea[a]["BTF"] = global.WaterGlobalArea[a]["BTF"] - Tiles
  1415.                     else
  1416.                         global.WaterGlobalArea[a]["BTF"] = Count
  1417.                     end
  1418.                     for b = 1, #game.surfaces, 1 do
  1419.                         pos = global.WaterGlobalArea[a]["WaterRepArea"][1]["position"]
  1420.                         local tile = game.surfaces[b].get_tile(pos)
  1421.                         if tile.valid == true then
  1422.                             local tilename = tile.name
  1423.                             if tilename == "water" or tilename =="deepwater" or tilename=="crude-oil" or tilename=="crude-oil-deep" or tilename=="sand-3" or tilename=="dry-dirt" then
  1424.                                 global.WaterGlobalArea[a]["Surface"] = game.surfaces[b]
  1425.                             end
  1426.                         end
  1427.                         for c = Count, 1 , -1 do
  1428.                             posc = global.WaterGlobalArea[a]["WaterRepArea"][c]["position"]
  1429.                             local tile = game.surfaces[b].get_tile(posc)
  1430.                             if tile.valid == true then
  1431.                                 local tilename = tile.name
  1432.                                 if tilename == "water" or tilename =="deepwater" or tilename=="crude-oil" or tilename=="crude-oil-deep" then
  1433.                                     global.WaterGlobalArea[a]["WaterRepArea"][c]["OriginalName"] = tilename
  1434.                                 end
  1435.                             end
  1436.                         end
  1437.                     end
  1438.                 end
  1439.             end
  1440.             if #global.OPLocate > 0 then
  1441.                 for a = #global.OPLocate, 1, -1 do
  1442.                     if global.OPLocate[a]["WA"] == nil or global.OPLocate[a]["WA"] == 0 then
  1443.                         if global.OPLocate[a]["OPposition"] ~= nil then
  1444.                             OPTilePosX = global.OPLocate[a]["OPposition"]["x"]
  1445.                             OPTilePosY = global.OPLocate[a]["OPposition"]["y"]
  1446.                         else
  1447.                             OPTilePosX = global.OPLocate[a]["position"]["x"]
  1448.                             OPTilePosY = global.OPLocate[a]["position"]["y"]
  1449.                         end
  1450.                     for b = 1, #global.WaterGlobalArea, 1 do
  1451.                         CompTiles = global.WaterGlobalArea[b]["ShallowWater"] + global.WaterGlobalArea[b]["DeepWater"]
  1452.                         for c = CompTiles, 1, -1 do -- FOR EACH TILE in WATERAREA
  1453.                             WATilePosX = global.WaterGlobalArea[b]["WaterRepArea"][c]["position"]["x"]
  1454.                             WATilePosY = global.WaterGlobalArea[b]["WaterRepArea"][c]["position"]["y"]
  1455.                                 if WATilePosX == OPTilePosX then
  1456.                                     if WATilePosY == OPTilePosY then
  1457.                                         global.OPLocate[a]["WA"] = b
  1458.                                     end
  1459.                                 end
  1460.                             end
  1461.                         end
  1462.                     end
  1463.                     if global.OPLocate[a]["Active"] == nil then
  1464.                         global.OPLocate[a]["Active"] = 0
  1465.                     end
  1466.                     if global.OPLocate[a]["entity"] == nil then
  1467.                         global.OPLocate[a]["entity"] = global.OPLocate[a]["OPentity"]
  1468.                     end
  1469.                     if global.OPLocate[a]["position"] == nil then
  1470.                         global.OPLocate[a]["position"] = global.OPLocate[a]["OPposition"]
  1471.                     end
  1472.                     if global.OPLocate[a]["direction"] == nil then
  1473.                         global.OPLocate[a]["direction"] = global.OPLocate[a]["OPdirection"]
  1474.                     end
  1475.                     for b = 1, #global.WaterGlobalArea, 1 do
  1476.                         if global.OPLocate[a]["WA"] == b and global.WaterGlobalArea[b]["Depleted"] ~= 1 then
  1477.                             global.OPLocate[a]["tile"] = global.WaterGlobalArea[b]["FluidType"]
  1478.                         elseif global.OPLocate[a]["WA"] == b and global.WaterGlobalArea[b]["Depleted"] == 1 then
  1479.                             global.OPLocate[a]["tile"] = "None"
  1480.                         end
  1481.                     end
  1482.                     for b = 1, #game.surfaces, 1 do
  1483.                         pos = global.OPLocate[a]["position"]
  1484.                         local tile = game.surfaces[b].get_tile(pos)
  1485.                         if tile.valid == true then
  1486.                             local tilename = tile.name
  1487.                             if tilename == "water" or tilename =="deepwater" or tilename=="crude-oil" or tilename=="crude-oil-deep" or tilename=="sand-3" or tilename=="dry-dirt" then
  1488.                                 global.OPLocate[a]["surface"] = game.surfaces[b]
  1489.                             end
  1490.                         end
  1491.                     end
  1492.                     if global.OPLocate[a]["tile"] == "water" or global.OPLocate[a]["tile"] == "deepwater" then
  1493.                         local OPE = global.OPLocate[a]["entity"]
  1494.                         local OPD = global.OPLocate[a]["direction"]
  1495.                         local OPP = global.OPLocate[a]["position"]
  1496.                         local OPS = global.OPLocate[a]["surface"]
  1497.                         OPE.destroy()
  1498.                         local OPN = game.surfaces[OPS.name].create_entity{name="offshore-pump",position = OPP,direction = OPD,force = "player"}
  1499.                         global.OPLocate[a]["entity"] = OPN
  1500.                         if global.OPLocate[a]["entity"] == nil then
  1501.                             if global.WaterGlobalArea[global.OPLocate[a]["WA"]]["OPs"] <= 0 then
  1502.                                 global.WaterGlobalArea[global.OPLocate[a]["WA"]]["OPs"] = 0
  1503.                             else
  1504.                                 global.WaterGlobalArea[global.OPLocate[a]["WA"]]["OPs"] = global.WaterGlobalArea[global.OPLocate[a]["WA"]]["OPs"] - 1
  1505.                             end
  1506.                             table.remove(global.OPLocate,a)
  1507.                             game.surfaces[OPS.name].spill_item_stack(OPP, {name="offshore-pump",count=1})
  1508.                             game.print(string.format("Water As A Resource: Offshore Pump Removed from %s, as Fluid Mixing Not Allowed", global.WaterGlobalArea[global.OPLocate[a]["WA"]]["WtrName"]))
  1509.                         end
  1510.                     elseif global.OPLocate[a]["tile"] == "crude-oil" or global.OPLocate[a]["tile"] == "crude-oil-deep" then
  1511.                         local OPE2 = global.OPLocate[a]["entity"]
  1512.                         local OPD2 = global.OPLocate[a]["direction"]
  1513.                         local OPP2 = global.OPLocate[a]["position"]
  1514.                         local OPS2 = global.OPLocate[a]["surface"]
  1515.                         OPE2.destroy()
  1516.                         local OPN2 = game.surfaces[OPS2.name].create_entity{name="offshore-crude-oil-pump",position = OPP2,direction = OPD2,force = "player"}
  1517.                         global.OPLocate[a]["entity"] = OPN2
  1518.                         if global.OPLocate[a]["entity"] == nil then
  1519.                             if global.WaterGlobalArea[global.OPLocate[a]["WA"]]["OPs"] <= 0 then
  1520.                                 global.WaterGlobalArea[global.OPLocate[a]["WA"]]["OPs"] = 0
  1521.                             else
  1522.                                 global.WaterGlobalArea[global.OPLocate[a]["WA"]]["OPs"] = global.WaterGlobalArea[global.OPLocate[a]["WA"]]["OPs"] - 1
  1523.                             end
  1524.                             table.remove(global.OPLocate,a)
  1525.                             game.surfaces[OPS2.name].spill_item_stack(OPP2, {name="offshore-pump",count=1})
  1526.                             game.print(string.format("Water As A Resource: Offshore Crude Oil Pump Removed from %s, as Fluid Mixing Not Allowed", global.WaterGlobalArea[global.OPLocate[a]["WA"]]["WtrName"]))
  1527.                         end
  1528.                     else
  1529.                         local OPE3 = global.OPLocate[a]["entity"]
  1530.                         local OPD3 = global.OPLocate[a]["direction"]
  1531.                         local OPP3 = global.OPLocate[a]["position"]
  1532.                         local OPS3 = global.OPLocate[a]["surface"]
  1533.                         OPE3.destroy()
  1534.                         local OPN3 = game.surfaces[OPS3.name].create_entity{name="offshore-pump-nofluid",position = OPP3,direction = OPD3,force = "player"}
  1535.                         global.OPLocate[a]["entity"] = OPN3
  1536.                     end
  1537.                 end
  1538.             end
  1539.             if #global.ODLocate > 0 then
  1540.                 for a = 1, #global.ODLocate, 1 do
  1541.                     for b = 1, #game.surfaces, 1 do
  1542.                         pos = global.ODLocate[a]["position"]
  1543.                         local tile = game.surfaces[b].get_tile(pos)
  1544.                         if tile.valid == true then
  1545.                             local tilename = tile.name
  1546.                             if tilename == "water" or tilename =="deepwater" or tilename=="crude-oil" or tilename=="crude-oil-deep" or tilename=="sand-3" or tilename=="dry-dirt" then
  1547.                                 global.ODLocate[a]["surface"] = game.surfaces[b]
  1548.                             end
  1549.                         end
  1550.                     end
  1551.                     local OPE4 = global.ODLocate[a]["entity"]
  1552.                     local OPD4 = global.ODLocate[a]["direction"]
  1553.                     local OPP4 = global.ODLocate[a]["position"]
  1554.                     local OPS4 = global.ODLocate[a]["surface"]
  1555.                     OPE4.destroy()
  1556.                     local OPN4 = game.surfaces[OPS4.name].create_entity{name='offshore-drain',position = OPP4, direction = OPD4, force = "player"}
  1557.                     global.ODLocate[a]["entity"] = OPN4
  1558.                     for b = 1, #global.WaterGlobalArea, 1 do
  1559.                         if global.ODLocate[a]["WA"] == b and global.WaterGlobalArea[b]["Depleted"] ~= 1 then
  1560.                             global.ODLocate[a]["tile"] = global.WaterGlobalArea[b]["FluidType"]
  1561.                             global.ODLocate[a]["PipeFluid"] = global.WaterGlobalArea[b]["FluidType"]
  1562.                         elseif global.ODLocate[a]["WA"] == b and global.WaterGlobalArea[b]["Depleted"] == 1 then
  1563.                             global.ODLocate[a]["tile"] = "None"
  1564.                             global.ODLocate[a]["PipeFluid"] = "None"
  1565.                         end
  1566.                     end
  1567.                 end
  1568.             end
  1569.             if #global.FluidProducers > 0 then
  1570.                 for b = 1, #game.surfaces, 1 do
  1571.                     local Pumps = game.surfaces[b].find_entities_filtered{name= "pumpjack"}
  1572.                     for a = 1, #Pumps, 1 do
  1573.                         global.FluidProducers[a]["entity"] = Pumps[a]
  1574.                         global.FluidProducers[a]["position"]["x"] = Pumps[a]["position"].x
  1575.                         global.FluidProducers[a]["position"]["y"] = Pumps[a]["position"].y
  1576.                         global.FluidProducers[a]["surface"] = Pumps[a].surface
  1577.                         global.FluidProducers[a]["FluidType"] = nil
  1578.                         global.FluidProducers[a]["LastAmount"] = 0
  1579.                     end
  1580.                 end
  1581.             end
  1582.         end
  1583.         global.Added = false
  1584.         global.NewInstall = false
  1585.     end
  1586. end
  1587.  
  1588.  
  1589. -- COMMAND FUNCTIONS --
  1590.  
  1591. function RestoreWater()
  1592.     local RWDisabled = settings.global["Disable-RestoreWater-Command"].value
  1593.     if RWDisabled == true then
  1594.         game.print("RestoreWater Command has been disabled in map settings.")
  1595.     else
  1596.         if global.WaterGlobalArea then
  1597.             for a = 1, #global.WaterGlobalArea, 1 do
  1598.                 if global.WaterGlobalArea[a]["Surface"].valid then -- Dexy Edit
  1599.                     local NoTiles = global.WaterGlobalArea[a]["ShallowWater"] + global.WaterGlobalArea[a]["DeepWater"]
  1600.                     if NoTiles ~= nil then -- Dexy Edit
  1601.                         for b = 1, NoTiles, 1 do
  1602.                             if global.WaterGlobalArea[a]["WaterRepArea"][b] ~= nil then -- Dexy Edit
  1603.                                 local TileName = global.WaterGlobalArea[a]["WaterRepArea"][b]["name"]
  1604.                                 if TileName == "sand-3" or TileName == "crude-oil" then
  1605.                                     global.WaterGlobalArea[a]["WaterRepArea"][b]["name"] = "water"
  1606.                                 elseif TileName == "dry-dirt" or TileName == "crude-oil-deep" then
  1607.                                     global.WaterGlobalArea[a]["WaterRepArea"][b]["name"] = "deepwater"
  1608.                                 else
  1609.                                     game.print("Not A Replaceable Tile")
  1610.                                 end
  1611.                             end
  1612.                         end
  1613.                     end
  1614.                 game.surfaces[global.WaterGlobalArea[a]["Surface"].name].set_tiles(global.WaterGlobalArea[a]["WaterRepArea"])
  1615.                 end
  1616.             end
  1617.         global.WaterGlobalArea = {}
  1618.         end
  1619.         if global.OPLocate then
  1620.             for a = 1, #global.OPLocate, 1 do
  1621.                 RepOP = global.OPLocate[a]["position"]
  1622.                 RepOPD = global.OPLocate[a]["direction"]
  1623.                 RepOPE = global.OPLocate[a]["entity"]
  1624.                 RepOPS = global.OPLocate[a]["surface"]
  1625.                 RepOPE.destroy()
  1626.                 if RepOPS.valid and game.surfaces[RepOPS.name] ~= nil and game.surfaces[RepOPS.name].valid then  -- Dexy edit
  1627.                     game.surfaces[RepOPS.name].create_entity{name="offshore-pump",position = RepOP,direction = RepOPD,force = "player"}
  1628.                 end
  1629.             end
  1630.         global.OPLocate = {}
  1631.         end
  1632.         if global.ODLocate then
  1633.             for a = 1, #global.ODLocate, 1 do
  1634.                 RepODE = global.ODLocate[a]["entity"]
  1635.                 RepODE.destroy()
  1636.             end
  1637.         global.ODLocate = {}
  1638.         end
  1639.         global.FluidProducers = {}
  1640.         global.PercentChange = 0
  1641.         global.WaterFlow = 0
  1642.         global.CrudeFlow = 0
  1643.         global.LastWaterFlow = 0
  1644.         global.LastCrudeFlow = 0
  1645.         game.print("FluidAreas Restored!")
  1646.     end
  1647. end
  1648.  
  1649. function Offshores()
  1650.     game.print("Offshore Pump(s) Location Info")
  1651.     if #global.OPLocate == 0 then
  1652.         game.print("No Offshore Pumps to show.")
  1653.     else
  1654.         for a = 1, #global.OPLocate, 1 do
  1655.         local FluidName = string.sub(global.OPLocate[a]["tile"],1,1):upper()..string.sub(global.OPLocate[a]["tile"],2)
  1656.         local SurfaceName = string.sub(global.OPLocate[a]["surface"].name,1,1):upper()..string.sub(global.OPLocate[a]["surface"].name,2)
  1657.         game.print(string.format("Offshore Pump Position: {%s, %s}, Pump Surface: %s, Fluid Type: %s, FluidArea: %s, Active: %s",global.OPLocate[a]["position"]["x"],global.OPLocate[a]["position"]["y"],SurfaceName, FluidName,global.OPLocate[a]["WA"], global.OPLocate[a]["Active"]))
  1658.         end
  1659.     end
  1660. end
  1661.  
  1662. function OffshoreDrains()
  1663.     game.print("Offshore Drain(s) Location Info")
  1664.     if #global.ODLocate == 0 then
  1665.         game.print("No Offshore Drains to show.")
  1666.     else
  1667.         for a = 1, #global.ODLocate, 1 do
  1668.             if global.ODLocate[a]["PipeFluid"] == nil then
  1669.                 FluidName = "None"
  1670.             else
  1671.                 FluidName = string.sub(global.ODLocate[a]["PipeFluid"],1,1):upper()..string.sub(global.ODLocate[a]["PipeFluid"],2)
  1672.             end
  1673.             local SurfaceName = string.sub(global.ODLocate[a]["surface"].name,1,1):upper()..string.sub(global.ODLocate[a]["surface"].name,2)
  1674.             game.print(string.format("Offshore Drain Position: {%s, %s}, Pipe Surface: %s , Pipe Fluid: %s, Fluid Area: %s, Active: %s",global.ODLocate[a]["position"]["x"],global.ODLocate[a]["position"]["y"],SurfaceName, FluidName, global.ODLocate[a]["WA"], global.ODLocate[a]["Active"]))
  1675.         end
  1676.     end
  1677. end
  1678.  
  1679. function WaterAreas()
  1680.     game.print("FluidArea(s) Info")
  1681.     if #global.WaterGlobalArea == 0 then
  1682.         game.print("No FluidAreas to show.")
  1683.     else
  1684.         for a = 1, #global.WaterGlobalArea, 1 do
  1685.             if #global.WaterGlobalArea[a]["WaterRepArea"] > 0 then -- == 0 When removed
  1686.                 local FluidName = string.sub(global.WaterGlobalArea[a]["FluidType"],1,1):upper()..string.sub(global.WaterGlobalArea[a]["FluidType"],2)
  1687.                 local FluidName2 = FluidName
  1688.                 if FluidName2 == "None" then
  1689.                     FluidName2 = "Fluid"
  1690.                 end
  1691.                 local SurfaceName = string.sub(global.WaterGlobalArea[a]["Surface"].name,1,1):upper()..string.sub(global.WaterGlobalArea[a]["Surface"].name,2)
  1692.                 game.print(string.format("%s, FluidArea Surface: %s, Fluid Type: %s, %s Amount: %.f, Regen %.2f, Tiles : Shallow: %.f / Deep: %.f, Percent Depleted: %.4f %%, Active: Pumps: %.f / Drains: %.f", global.WaterGlobalArea[a]["WtrName"],SurfaceName,FluidName,FluidName2, global.WaterGlobalArea[a]["AmountWtr"],global.WaterGlobalArea[a]["RegenAmount"],global.WaterGlobalArea[a]["ShallowWater"],global.WaterGlobalArea[a]["DeepWater"], global.WaterGlobalArea[a]["Percent"], global.WaterGlobalArea[a]["OPsA"], global.WaterGlobalArea[a]["ODsA"]))
  1693.             end
  1694.         end
  1695.     end
  1696. end
  1697.  
  1698. function PumpJacks()
  1699.     game.print("Pumpjack(s) Location Info")
  1700.     if #global.FluidProducers == 0 then
  1701.         game.print("No Pumpjacks to show.")
  1702.     else
  1703.         for a = 1, #global.FluidProducers, 1 do
  1704.         local SurfaceName = string.sub(global.FluidProducers[a]["surface"].name,1,1):upper()..string.sub(global.FluidProducers[a]["surface"].name,2)
  1705.         game.print(string.format("Pumpjack Position: {%s, %s}, Pumpjack Surface: %s",global.FluidProducers[a]["position"]["x"],global.FluidProducers[a]["position"]["y"],SurfaceName))
  1706.         end
  1707.     end
  1708. end
  1709.  
  1710. function ScenFunc()
  1711.     global.LandFill = {}
  1712.     global.InstallTick = 0
  1713. end
  1714.  
  1715. function WaaRSetup()
  1716.     Globals()
  1717.     LinkGlobals()
  1718. end
  1719.  
  1720. -- SCRIPT EVENTS --
  1721.  
  1722. script.on_init(WaaRSetup)
  1723. script.on_load(LinkGlobals)
  1724. script.on_nth_tick(60,EverySec)
  1725. script.on_event(defines.events.on_tick, EveryTick)
  1726. script.on_event({defines.events.on_built_entity,defines.events.on_robot_built_entity}, BuiltOffShore)
  1727. script.on_event({defines.events.on_player_mined_entity,defines.events.on_robot_mined_entity}, DestroyedOffShore)
  1728. script.on_event({defines.events.on_player_built_tile,defines.events.on_robot_built_tile}, LandFillCheck)
  1729. script.on_event({defines.events.on_game_created_from_scenario},ScenFunc)
  1730. script.on_configuration_changed(UpdateMod)
  1731. commands.add_command("RestoreWater", "Restores Water Areas & Clears GlobalTable",RestoreWater)
  1732. commands.add_command("Offshores", "Displays All The Offshore Pumps Built", Offshores)
  1733. commands.add_command("OffshoreDrains", "Displays All The Offshore Drains Built", OffshoreDrains)
  1734. commands.add_command("WaterAreas", "Displays All The WaterAreas Built", WaterAreas)
  1735. commands.add_command("Pumpjacks", "Displays All The Pumpjacks Built", PumpJacks)
  1736. commands.add_command("RemoveAllDrainedArea", "Remove all drained area", RemoveAllDrainedArea)
Add Comment
Please, Sign In to add comment