Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- WATER AS A RESOURCE MAIN LUA CONTROL PROGRAM --
- -- CREATED BY TREEFROGGREAKEN 2019 --
- -- REQUIRED ADDITIONAL LUA FILES --
- require("waterbodies") -- Loads waterbodies.lua
- require("fluidentities")
- require("fluidlist")
- -- GLOBAL VARIABLES
- function Globals()
- if not global.WaterGlobalArea then global.WaterGlobalArea = {} end
- if not global.OPLocate then global.OPLocate = {} end
- if not global.ODLocate then global.ODLocate = {} end
- if not global.LandFill then global.LandFill = {} end
- if not global.FluidProducers then global.FluidProducers = {} end
- if not global.Type then global.Type = 0 end
- if not global.WaterFlow then global.WaterFlow = 0 end
- if not global.CrudeFlow then global.CrudeFlow = 0 end
- if not global.LastWaterFlow then global.LastWaterFlow = 0 end
- if not global.LastCrudeFlow then global.LastCrudeFlow = 0 end
- if not global.WaterBodyType then global.WaterBodyType = 0 end
- if not global.ActiveOPs then global.ActiveOPs = 0 end
- if not global.ActiveODs then global.ActiveODs = 0 end
- if not global.PercentChange then global.PercentChange = 0 end
- if not global.InstallTick then global.InstallTick = 0 end
- if not global.Added then global.Added = true end
- if not global.NewInstall then global.NewInstall = true end
- if not global.ITMessage then global.ITMessage = true end
- end
- function LinkGlobals()
- -------------------------------- DEXY EDIT ------------------------------
- if remote.interfaces["warptorio2"] then
- log("Registred Warptorio2 event")
- local warp_event = remote.call("warptorio2", "postwarpevent")
- script.on_event(warp_event, function(event)
- log("Warped, all lakes removed")
- RemoveAllDrainedArea()
- RestoreWater()
- end)
- end
- -------------------------------------------------------------------------
- WaterFlow = global.WaterFlow
- CrudeFlow = global.CrudeFlow
- LastWaterFlow = global.LastWaterFlow
- LastCrudeFlow = global.LastCrudeFlow
- WaterBodyType = global.WaterBodyType
- ActiveOPs = global.ActiveOPs
- ActiveODs = global.ActiveODs
- PercentChange = global.PercentChange
- end
- -- EVENT DEPENDANT FUNCTIONS
- function SearchPattern (SearchPosition, WASearchQueue) -- Function to Setup SearchPattern
- local north = {x = SearchPosition.x, y = SearchPosition.y+1} -- Create Local North Table make it equal to SearchPostion -1 y
- local northeast = {x = SearchPosition.x+1, y = SearchPosition.y+1}
- local east = {x = SearchPosition.x+1, y = SearchPosition.y} -- Create Local East Table make it equal to SearchPostion +1 x
- local southeast = {x = SearchPosition.x+1, y = SearchPosition.y-1}
- local south = {x = SearchPosition.x, y = SearchPosition.y-1} -- Create Local South Table make it equal to SearchPostion +1 y
- local southwest = {x = SearchPosition.x-1, y = SearchPosition.y-1}
- local west = {x = SearchPosition.x-1, y = SearchPosition.y} -- Create Local West Table make it equal to SearchPostion -1 x
- local northwest = {x = SearchPosition.x-1, y = SearchPosition.y+1}
- WASearchQueue[#WASearchQueue+1] = north
- WASearchQueue[#WASearchQueue+1] = northeast
- WASearchQueue[#WASearchQueue+1] = east
- WASearchQueue[#WASearchQueue+1] = southeast
- WASearchQueue[#WASearchQueue+1] = south
- WASearchQueue[#WASearchQueue+1] = southwest
- WASearchQueue[#WASearchQueue+1] = west
- WASearchQueue[#WASearchQueue+1] = northwest
- return WASearchQueue -- Return WASearchQueue Table as Result
- end
- function GridRef (SearchPosition)
- return string.format ("%.1f , %.1f", SearchPosition.x , SearchPosition.y) -- Print SearchPosition X, Y CoOrds as String
- end
- function IsWater(SearchPosition, surface)
- local tile = game.surfaces[surface].get_tile(SearchPosition.x,SearchPosition.y) -- Get Name of Tile by looking on the surface by using SearchPosition Variable
- if tile.valid == true then
- fluidname = tile.name
- if fluidlist.CheckNames(fluidname) == true then -- IF Water or Deepwater THEN
- return true -- Return TRUE
- else -- Otherwise
- return false -- Return False
- end -- NOT Water or Deepwater
- game.print("NOT A WATER TILE") -- Print Not A Water Tile
- else
- return false
- end
- end
- function GlobalWaterArea()
- if #global.WaterGlobalArea == 0 then
- if global.Type == 1 then
- CreateWaterArea() -- Create WaterArea
- global.OPLocate[1]["WA"] = 1 -- Assign Offshore Pump 1 to WaterArea 1
- global.WaterGlobalArea[1]["OPs"] = 1 -- Assign WaterArea 1 to Offshore Pump 1
- global.WaterGlobalArea[1]["Surface"] = global.OPLocate[1]["entity"].surface
- AssignFluid()
- if global.WaterGlobalArea[1]["ToSearch"] == nil then
- game.print(string.format("%s created, with %sL of %s.", global.WaterGlobalArea[1]["WtrName"], global.WaterGlobalArea[1]["AmountWtr"], global.WaterGlobalArea[1]["FluidType"]))
- else
- game.print("FluidArea being scanned, please wait for assignment.")
- end
- elseif global.Type == 2 then
- local x = global.ODLocate[1]["position"]["x"]
- local y = global.ODLocate[1]["position"]["y"]
- local ODP = global.ODLocate[1]["position"]
- local ODS = global.ODLocate[1]["entity"].surface
- local WaterCheck = global.ODLocate[1]["surface"].get_tile(x,y).name
- if WaterCheck == "water" or WaterCheck == "deepwater" then
- CreateWaterArea()
- global.ODLocate[1]["WA"] = 1
- global.WaterGlobalArea[1]["ODs"] = 1
- global.WaterGlobalArea[1]["Surface"] = global.ODLocate[1]["entity"].surface
- AssignFluid()
- if global.WaterGlobalArea[1]["ToSearch"] == nil then
- game.print(string.format("%s created, with %sL of %s.", global.WaterGlobalArea[1]["WtrName"], global.WaterGlobalArea[1]["AmountWtr"], "water"))
- else
- game.print("FluidArea being scanned, please wait for assignment.")
- end
- else
- game.print("Area not suitable for Offshore Drain.")
- global.ODLocate[1]["entity"].destroy()
- table.remove(global.ODLocate)
- game.surfaces[ODS.name].spill_item_stack(ODP, {name="offshore-drain",count=1})
- end
- end
- else
- CompareAssign()
- end
- end
- function CompareAssign()
- OffShoreCompareArea()
- if FoundExt == true then -- IF No Offshores Found then New Area
- if global.Type == 1 then
- CreateWaterArea()
- global.OPLocate[#global.OPLocate]["WA"] = #global.WaterGlobalArea
- global.WaterGlobalArea[#global.WaterGlobalArea]["OPs"] = 1
- global.WaterGlobalArea[#global.WaterGlobalArea]["Surface"] = global.OPLocate[#global.OPLocate]["entity"].surface
- AssignFluid()
- if global.WaterGlobalArea[#global.WaterGlobalArea]["ToSearch"] == nil then
- 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"]))
- else
- game.print("FluidArea being scanned, please wait for assignment.")
- end
- FoundExt = false
- elseif global.Type == 2 then
- local x = global.ODLocate[#global.ODLocate]["position"]["x"]
- local y = global.ODLocate[#global.ODLocate]["position"]["y"]
- local ODP = global.ODLocate[#global.ODLocate]["position"]
- local ODS = global.ODLocate[#global.ODLocate]["entity"].surface
- local WaterCheck = global.ODLocate[#global.ODLocate]["surface"].get_tile(x,y).name
- if WaterCheck == "water" or WaterCheck == "deepwater" then
- CreateWaterArea()
- global.ODLocate[#global.ODLocate]["WA"] = #global.WaterGlobalArea
- global.WaterGlobalArea[#global.WaterGlobalArea]["ODs"] = 1
- global.WaterGlobalArea[#global.WaterGlobalArea]["Surface"] = global.ODLocate[#global.ODLocate]["entity"].surface
- AssignFluid()
- if global.WaterGlobalArea[#global.WaterGlobalArea]["ToSearch"] == nil then
- game.print(string.format("%s created, with %sL of %s.", global.WaterGlobalArea[#global.WaterGlobalArea]["WtrName"], global.WaterGlobalArea[#global.WaterGlobalArea]["AmountWtr"], "water"))
- else
- game.print("FluidArea being scanned, please wait for assignment.")
- end
- else
- game.print("Area not suitable for Offshore Drain.")
- global.ODLocate[#global.ODLocate]["entity"].destroy()
- table.remove(global.ODLocate,#global.ODLocate)
- game.surfaces[ODS.name].spill_item_stack(ODP, {name="offshore-drain",count=1})
- end
- FoundExt = false
- end
- end
- end
- function CreateWaterArea()
- 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}
- a = #global.WaterGlobalArea
- GetWaterArea(a)
- CalculatedWaterTotal(a)
- waterbodies.WtrName(a)
- a = 0
- end
- function AssignFluid() -- Assign Fluid On New WaterArea Creation
- for a = 1, #global.WaterGlobalArea, 1 do
- if global.WaterGlobalArea[a]["FluidType"] == nil then
- if global.Type == 1 then
- for b = 1, #global.OPLocate, 1 do
- if global.OPLocate[b]["WA"] == a then
- global.WaterGlobalArea[a]["FluidType"] = global.OPLocate[b]["tile"]
- end
- end
- elseif global.Type == 2 then
- for b = 1, #global.ODLocate, 1 do
- if global.ODLocate[b]["WA"] == a then
- global.WaterGlobalArea[a]["FluidType"] = global.ODLocate[b]["tile"]
- end
- end
- end
- end
- end
- end
- function OffShoreCompareArea()
- if global.Type == 1 then
- NuOPs = #global.OPLocate
- OPS = global.OPLocate
- NewOPPosX = OPS[NuOPs]["position"]["x"]
- NewOPPosY = OPS[NuOPs]["position"]["y"]
- elseif global.Type == 2 then
- NuOPs = #global.ODLocate
- OPS = global.ODLocate
- NewOPPosX = OPS[NuOPs]["position"]["x"]
- NewOPPosY = OPS[NuOPs]["position"]["y"]
- end
- local NuWAs = #global.WaterGlobalArea
- local WA = global.WaterGlobalArea
- FoundInt = false
- FoundExt = true
- for a = NuWAs, 1, -1 do -- FOR Each WATERAREA
- for b = 1, #WA[a]["WaterRepArea"], 1 do -- FOR EACH TILE in WATERAREA
- if WA[a]["WaterRepArea"][b] ~= nil then
- local WATilePosX = WA[a]["WaterRepArea"][b]["position"]["x"]
- local WATilePosY = WA[a]["WaterRepArea"][b]["position"]["y"]
- if NewOPPosX == WATilePosX then -- IF NEW OFFSHORE IS IN WATER AREAS X POSITION
- if NewOPPosY == WATilePosY then -- IF NEW OFFSHORE IS IN WATER AREAS Y POSITION
- FoundExt = false
- FoundInt = true
- end
- end
- end
- end
- if FoundInt == true then -- FOUND IN THIS WATERAREA
- if global.Type == 1 then
- OPS[NuOPs]["WA"] = a
- WA[a]["OPs"] = WA[a]["OPs"] + 1
- if WA[a]["Percent"] < 0 then
- 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"]))
- else
- 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"]))
- end
- FoundInt = false
- elseif global.Type == 2 then
- OPS[NuOPs]["WA"] = a
- WA[a]["ODs"] = WA[a]["ODs"] + 1
- if WA[a]["Percent"] >= 0 and WA[a]["Percent"] < 100 then
- 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"]))
- else
- if WA[a]["Percent"] >= 100 then
- game.print(string.format("%s has %s Offshore Drains, with %.0f %% depleted.", WA[a]["WtrName"], WA[a]["ODs"], WA[a]["Percent"], WA[a]["FluidType"]))
- else
- game.print(string.format("%s has %s Offshore Drains, with %.0f %% depleted.", WA[a]["WtrName"], WA[a]["ODs"], 0, WA[a]["FluidType"]))
- end
- end
- FoundInt = false
- end
- end
- end
- return FoundExt
- end
- function GetWaterArea(a) -- Get Water Area Function
- if global.Type == 1 then
- position = global.OPLocate[#global.OPLocate]["position"]
- surface = global.OPLocate[#global.OPLocate]["surface"].name
- WaterRepArea = global.WaterGlobalArea[#global.WaterGlobalArea]["WaterRepArea"]
- WASearchQueue = {position}
- WASearched = {}
- SearchAmount = settings.global["FluidArea-Start-Area"].value
- FA = global.WaterGlobalArea[a]
- elseif global.Type == 2 then
- position = global.ODLocate[#global.ODLocate]["position"]
- surface = global.ODLocate[#global.ODLocate]["surface"].name
- WaterRepArea = global.WaterGlobalArea[#global.WaterGlobalArea]["WaterRepArea"]
- WASearchQueue = {position}
- WASearched = {}
- SearchAmount = settings.global["FluidArea-Start-Area"].value
- FA = global.WaterGlobalArea[a]
- elseif global.WaterGlobalArea[a]["ToSearch"] ~= nil then
- WaterRepArea = global.WaterGlobalArea[a]["WaterRepArea"]
- WASearchQueue = global.WaterGlobalArea[a]["ToSearch"]
- WASearched = global.WaterGlobalArea[a]["HasSearched"]
- surface = global.WaterGlobalArea[a]["Surface"].name
- SearchAmount = settings.global["FluidArea-Additional-Tiles-Per-Second"].value / 60
- FA = global.WaterGlobalArea[a]
- if game.active_mods["SeaBlock"] then
- TotalArea = global.WaterGlobalArea[a]["ShallowWater"] + global.WaterGlobalArea[a]["DeepWater"]
- if TotalArea > 60000 then
- WASearchQueue = {}
- end
- end
- end
- local Shallow = 0
- local Deep = 0
- while #WASearchQueue > 0 do -- WHILE there is a WASerachQueue
- if Shallow + Deep <= SearchAmount then
- local SearchPosition = WASearchQueue[1] -- Convert Offshore Pump Position into SearchPosition
- if WASearched[GridRef(SearchPosition)] ~= true then -- IF GridRef has not been searched
- if IsWater(SearchPosition, surface) == false then -- IF IsWater is FALSE then
- WASearched[GridRef(SearchPosition)] = true -- GridRef has been Searched
- else -- ELSE IsWater is TRUE then
- if fluidname == "water" or fluidname == "water-green" or fluidname == "water-shallow" or fluidname == "water-mud" then -- IF Water Type is "water"
- WaterRepArea[#WaterRepArea+1] = {["name"] = "sand-3" , ["position"] = {["x"] = SearchPosition.x, ["y"] = SearchPosition.y},["OriginalName"] = fluidname}
- FA["ShallowWater"] = FA["ShallowWater"] + 1
- Shallow = Shallow + 1
- elseif fluidname == "deepwater" or fluidname == "deepwater-green" then
- WaterRepArea[#WaterRepArea+1] = {["name"] = "dry-dirt" , ["position"] = {["x"] = SearchPosition.x, ["y"] = SearchPosition.y},["OriginalName"] = fluidname}
- FA["DeepWater"] = FA["DeepWater"] + 1
- Deep = Deep + 1
- end
- WASearched[GridRef(SearchPosition)] = true -- Make SearchPosition TRUE in WASerached
- SearchPattern(SearchPosition, WASearchQueue) -- Run SearchPattern Function Passing SearchPosition and WASearchQueue
- table.remove (WASearchQueue,1) -- Remove from WASearchQueue
- end -- IF IsWater isn't TRUE or False
- else
- table.remove (WASearchQueue,1) -- Remove from WASearchQueue
- end
- else
- FA["ToSearch"] = WASearchQueue
- FA["HasSearched"] = WASearched
- goto TooBigArea
- end
- end
- ::TooBigArea::
- if #WASearchQueue == 0 then
- global.WaterGlobalArea[a]["ToSearch"] = nil
- global.WaterGlobalArea[a]["HasSearched"] = nil
- end
- end
- function FluidAreaContinue(a)
- -- Dexy Edit --
- if global.WaterGlobalArea["TotalLoop"] == nil then
- global.WaterGlobalArea["TotalLoop"] = 0
- else
- global.WaterGlobalArea["TotalLoop"] = global.WaterGlobalArea["TotalLoop"] + 1
- end
- ---------------
- GetWaterArea(a)
- CalculatedWaterTotal(a)
- global.WaterGlobalArea[a]["LoopCount"] = global.WaterGlobalArea[a]["LoopCount"] + 1
- local AlarmEnabled = settings.global["Alarms-Continuing-Search"].value
- if global.WaterGlobalArea[a]["LoopCount"] == 1000 and AlarmEnabled == true then
- global.WaterGlobalArea[a]["LoopCount"] = 0
- -- Dexy Edit --
- --game.print(string.format("Still Scanning FluidArea %s", a))
- game.print(string.format("Still Scanning FluidArea %s loop: %s", a, global.WaterGlobalArea["TotalLoop"]))
- ---------------
- end
- -- Dexy Edit --
- --if global.WaterGlobalArea[a]["ToSearch"] == nil then
- if global.WaterGlobalArea[a]["ToSearch"] == nil or global.WaterGlobalArea["TotalLoop"] > 10000 then
- if global.WaterGlobalArea["TotalLoop"] > 10000 then
- game.print(string.format("Water area %s is too big", a))
- global.WaterGlobalArea[a]["ToSearch"] = nil
- end
- global.WaterGlobalArea["TotalLoop"] = 0
- ---------------
- global.WaterGlobalArea[a]["LoopCount"] = 0
- waterbodies.WtrName(a)
- game.print(string.format("%s created, with %sL of %s.", global.WaterGlobalArea[a]["WtrName"], global.WaterGlobalArea[a]["AmountWtr"], global.WaterGlobalArea[a]["FluidType"]))
- end
- end
- function CalculatedWaterTotal(a)
- local Shallow = global.WaterGlobalArea[a]["ShallowWater"]
- local Deep = global.WaterGlobalArea[a]["DeepWater"]
- local TotalArea = Shallow + Deep
- global.WaterGlobalArea[a]["BTF"] = TotalArea
- local WATER = settings.global["TileFluidAmount-Shallow"].value
- local DEEPWATER = settings.global["TileFluidAmount-Deep"].value
- local PBonus = 0.01
- local WBonus = 0.5
- local PDBonus = 1
- local LBonus = 1.5
- local GLBonus = 2
- local SBonus = 2.5
- local OBonus = 3
- local WaterTotal = (Shallow * WATER) + (Deep * DEEPWATER)
- local WaterBodyType = 0
- if TotalArea < 4 then
- --game.print("PUDDLE")
- global.WaterGlobalArea[a]["AmountWtr"] = WaterTotal * PBonus
- global.WaterGlobalArea[a]["AmountBonusValue"] = PBonus
- global.WaterGlobalArea[a]["WtrName"] = "Puddle"
- global.WaterGlobalArea[a]["WaterBodyType"] = 0
- elseif TotalArea == 4 then
- --game.print("WELL")
- global.WaterGlobalArea[a]["AmountWtr"] = WaterTotal * WBonus
- global.WaterGlobalArea[a]["AmountBonusValue"] = WBonus
- global.WaterGlobalArea[a]["WtrName"] = "Well"
- global.WaterGlobalArea[a]["WaterBodyType"] = 1
- elseif TotalArea > 4 and TotalArea <= 200 then
- --game.print("POND")
- global.WaterGlobalArea[a]["AmountWtr"] = WaterTotal * PDBonus
- global.WaterGlobalArea[a]["AmountBonusValue"] = PDBonus
- global.WaterGlobalArea[a]["WtrName"] = "Pond"
- global.WaterGlobalArea[a]["WaterBodyType"] = 2
- elseif TotalArea > 200 and TotalArea <= 6000 then
- --game.print("LAKE")
- global.WaterGlobalArea[a]["AmountWtr"] = WaterTotal * LBonus
- global.WaterGlobalArea[a]["AmountBonusValue"] = LBonus
- global.WaterGlobalArea[a]["WaterBodyType"] = 3
- elseif TotalArea > 6000 and TotalArea <= 60000 then
- --game.print("GREAT LAKE")
- global.WaterGlobalArea[a]["AmountWtr"] = WaterTotal * GLBonus
- global.WaterGlobalArea[a]["AmountBonusValue"] = GLBonus
- global.WaterGlobalArea[a]["WaterBodyType"] = 4
- elseif TotalArea > 60000 and TotalArea <= 600000 then
- --game.print("SEA")
- global.WaterGlobalArea[a]["AmountWtr"] = WaterTotal * SBonus
- global.WaterGlobalArea[a]["AmountBonusValue"] = SBonus
- global.WaterGlobalArea[a]["WaterBodyType"] = 5
- else
- --game.print("OCEAN")
- global.WaterGlobalArea[a]["AmountWtr"] = WaterTotal * OBonus
- global.WaterGlobalArea[a]["AmountBonusValue"] = OBonus
- global.WaterGlobalArea[a]["WaterBodyType"] = 6
- end
- local RegenOff = settings.global["Disable-FluidArea-RegenRate"].value
- if RegenOff == false then
- local RegenRate = settings.global["FluidArea-RegenRate"].value / 10000
- RegenAmount = (RegenRate * TotalArea)
- else
- RegenAmount = 0
- end
- global.WaterGlobalArea[a]["RegenAmount"] = RegenAmount
- end
- function CorrectPump()
- if global.OPLocate[#global.OPLocate]["entity"].valid then
- if global.OPLocate[#global.OPLocate]["entity"]["name"] == "offshore-pump" then
- if global.OPLocate[#global.OPLocate]["tile"] == "crude-oil" then
- local OPE = global.OPLocate[#global.OPLocate]["entity"]
- local OPD = OPE.direction
- local OPP = OPE.position
- local OPS = OPE.surface
- OPE.destroy()
- local OPN = game.surfaces[OPS.name].create_entity{name="offshore-crude-oil-pump",position = OPP,direction = OPD,force = "player"}
- global.OPLocate[#global.OPLocate]["entity"] = OPN
- end
- end
- else
- game.print("Invalid Pump")
- end
- end
- function CorrectedFluid(tile)
- if tile == "crude-oil-deep" or tile == "crude-oil" then
- correctedtile = "crude-oil"
- else
- correctedtile = "water"
- end
- return correctedtile
- end
- -- ON TICK DEPENDANT FUNCTIONS --
- function RegenWater(a)
- local DisableRegen = settings.global["Disable-FluidArea-RegenRate"].value
- local WGA = global.WaterGlobalArea
- if DisableRegen == false and WGA[a]["FluidType"] == "water" and WGA[a]["Percent"] > 0 and WGA[a]["Percent"] <= 99 and WGA[a]["Depleted"] ~= 1 then
- local WGA = global.WaterGlobalArea
- if WGA[a]["WtrUsed"] - WGA[a]["RegenAmount"] <= 0 then
- WGA[a]["WtrUsed"] = 0
- else
- WGA[a]["WtrUsed"] = WGA[a]["WtrUsed"] - WGA[a]["RegenAmount"]
- end
- WGA[a]["PercentPrev"] = WGA[a]["Percent"]
- WGA[a]["Percent"] = (WGA[a]["WtrUsed"] / WGA[a]["AmountWtr"]) * 100
- PercentChange = WGA[a]["PercentPrev"] - WGA[a]["Percent"]
- end
- end
- function EverySec()
- for a = 1, #global.WaterGlobalArea, 1 do
- if #global.LandFill > 0 then
- LandFill(a)
- end
- RegenWater(a)
- end
- end
- function EveryTick()
- CheckWater()
- for a = 1, #global.WaterGlobalArea, 1 do
- if global.WaterGlobalArea[a]["ToSearch"] ~= nil then
- FluidAreaContinue(a)
- end
- end
- end
- function EmptyDrainPipes()
- local ODL = global.ODLocate
- for a = 1, #ODL, 1 do
- local WGA = global.WaterGlobalArea
- 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
- if ODL[a]["entity"].neighbours[1][1]["name"] == "storage-tank" then
- fluid = ODL[a]["entity"].neighbours[1][1].fluidbox[1]
- if fluid.amount >= 20 then
- fluid.amount = 20
- elseif fluid.amount < 20 and fluid.amount > 0 then
- fluid.amount = fluid.amount
- elseif fluid.amount == 0 then
- fluid.amount = 0
- end
- ODL[a]["entity"].neighbours[1][1].remove_fluid{name=fluid.name,amount=fluid.amount}
- else
- fluid = ODL[a]["entity"].neighbours[1][1].fluidbox[1]
- if fluid.amount >= 20 then
- fluid.amount = 20
- elseif fluid.amount < 20 and fluid.amount > 0 then
- fluid.amount = fluid.amount
- elseif fluid.amount == 0 then
- fluid.amount = 0
- end
- ODL[a]["entity"].neighbours[1][1].remove_fluid{name=fluid.name,amount=fluid.amount}
- end
- end
- WGA[ODL[a]["WA"]]["WtrAdd"] = 0
- end
- end
- function AssignTiles(a)
- local WGA = global.WaterGlobalArea
- local WGAA = WGA[a]["ShallowWater"] + WGA[a]["DeepWater"]
- local WGAT = WGA[a]["WaterRepArea"]
- local WGAFT = WGA[a]["FluidType"]
- if PercentChange < 0 and WGA[a]["TilesSet"] ~= "P" then -- IF WATERAREA IS BEING DEPLETED (PUMPS IN CHARGE)
- for b = 1, WGAA, 1 do
- if WGAT[b] ~= nil then
- 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
- WGA[a]["WaterRepArea"][b]["name"] = "sand-3"
- elseif WGAT[b]["OriginalName"] == "deepwater" or WGAT[b]["OriginalName"] == "crude-oil-deep" or WGAT[b]["OriginalName"] == "deepwater-green" then
- WGA[a]["WaterRepArea"][b]["name"] = "dry-dirt"
- end
- end
- end
- WGA[a]["TilesSet"] = "P"
- elseif PercentChange > 0 and WGA[a]["TilesSet"] ~= "D" then -- ELSE WATERAREA IS BEING FILLED (DRAINS IN CHARGE)
- for b = 1, WGAA, 1 do
- if WGAT[b] ~= nil then
- if WGAT[b]["name"] == "sand-3" then
- if WGAFT == "water" then
- if WGAT[b]["OriginalName"] == "water" then
- WGA[a]["WaterRepArea"][b]["name"] = "water"
- elseif WGAT[b]["OriginalName"] == "water-green" then
- WGA[a]["WaterRepArea"][b]["name"] = "water-green"
- elseif WGAT[b]["OriginalName"] == "water-shallow" then
- WGA[a]["WaterRepArea"][b]["name"] = "water-shallow"
- elseif WGAT[b]["OriginalName"] == "water-mud" then
- WGA[a]["WaterRepArea"][b]["name"] = "water-mud"
- end
- else
- WGA[a]["WaterRepArea"][b]["name"] = "crude-oil"
- end
- elseif WGAT[b]["name"] == "dry-dirt" then
- if WGAFT == "water" then
- if WGAT[b]["OriginalName"] == "deepwater" then
- WGA[a]["WaterRepArea"][b]["name"] = "deepwater"
- elseif WGAT[b]["OriginalName"] == "deepwater-green" then
- WGA[a]["WaterRepArea"][b]["name"] = "deepwater-green"
- end
- else
- WGA[a]["WaterRepArea"][b]["name"] = "crude-oil-deep"
- end
- end
- end
- end
- WGA[a]["TilesSet"] = "D"
- end
- end
- function WaterRandom(a)
- local WGA = global.WaterGlobalArea
- local WaterTiles = (WGA[a]["ShallowWater"] + WGA[a]["DeepWater"])
- local Surface = WGA[a]["Surface"]
- local finalper = 20
- local TilePerPercent = math.floor((WaterTiles / finalper) * 0.8)
- if TilePerPercent < 1 then
- TilePerPercent = 1
- end
- local randnotiles = math.random(1,TilePerPercent)
- for z = 1, randnotiles, 1 do
- local rand = math.random(1,WaterTiles)
- game.surfaces[Surface.name].set_tiles({WGA[a]["WaterRepArea"][rand]})
- end
- end
- function BackToFront(a)
- local WGA = global.WaterGlobalArea
- local WaterTiles = (WGA[a]["ShallowWater"] + WGA[a]["DeepWater"])
- local Surface = WGA[a]["Surface"]
- local TileCount = WGA[a]["BTF"]
- local WAArea = WGA[a]["WaterRepArea"]
- local finalper = 20
- local TilePerPercent = math.floor(WaterTiles / finalper)
- if TilePerPercent < 1 then
- TilePerPercent = 1
- end
- if PercentChange < 0 and TileCount ~= 0 then -- PUMPS
- if TilePerPercent > TileCount then
- TilePerPercent = TileCount - 1
- end
- for b=TilePerPercent, 0, -1 do
- if WAArea[TileCount] ~= nil then
- --if WAArea[TileCount]["name"] == "sand-3" or WAArea[TileCount]["name"] == "dry-dirt" then
- game.surfaces[Surface.name].set_tiles({WAArea[TileCount]})
- WGA[a]["BTF"] = WGA[a]["BTF"] - 1
- TileCount = WGA[a]["BTF"]
- --end
- else
- WGA[a]["BTF"] = WGA[a]["BTF"] - 1
- TileCount = WGA[a]["BTF"]
- end
- end
- elseif PercentChange > 0 and TileCount <= WaterTiles then -- DRAINS
- if TileCount == 0 then
- TileCount = 1
- end
- for b=TilePerPercent, 0, -1 do
- if WAArea[TileCount] ~= nil then
- --if WAArea[TileCount]["name"] == "water" or WAArea[TileCount]["name"] == "deepwater" or WAArea[TileCount]["name"] == "crude-oil" or WAArea[TileCount]["name"] == "crude-oil-deep" then
- game.surfaces[Surface.name].set_tiles({WAArea[TileCount]})
- WGA[a]["BTF"] = WGA[a]["BTF"] + 1
- TileCount = WGA[a]["BTF"]
- --end
- if TileCount > WaterTiles then
- TileCount = WaterTiles
- end
- else
- WGA[a]["BTF"] = WGA[a]["BTF"] + 1
- TileCount = WGA[a]["BTF"]
- end
- end
- end
- end
- function AddedWaterArea(a)
- local WGA = global.WaterGlobalArea
- if WGA[a]["AmountWtr"] ~= WGA[a]["WtrUsed"] and WGA[a]["Percent"] > 0 then
- local Percent = WGA[a]["Percent"]
- local WA = WGA[a]
- local LowAlarmEnabled = settings.global["Alarms-Low-Level (50/75/90%)"].value
- local HighAlarmEnabled = settings.global["Alarms-High-Level (95/97/98/99%)"].value
- if Percent < 100 and Percent > 80 then
- WGA[a]["Depleted"] = 0
- local RP = WA["RandPercent"]
- if (RP - Percent) >= 0.1 then
- local Method = settings.global["FluidArea-Replace-Method"].value
- if Method == "Random" then
- WaterRandom(a)
- elseif Method == "From/To Pump" then
- BackToFront(a)
- end
- WA["RandPercent"] = Percent
- end
- if Percent < 99 and WA["Fired99"] == true and HighAlarmEnabled == true then
- WA["Fired99"] = false
- elseif Percent < 98 and WA["Fired98"] == true and HighAlarmEnabled == true then
- WA["Fired98"] = false
- elseif Percent < 97 and WA["Fired97"] == true and HighAlarmEnabled == true then
- WA["Fired97"] = false
- elseif Percent < 95 and WA["Fired95"] == true and HighAlarmEnabled == true then
- WA["Fired95"] = false
- elseif Percent < 90 and WA["Fired90"] == true and HighAlarmEnabled == true then
- WA["Fired90"] = false
- end
- elseif Percent <= 80 and Percent > 0 then
- WA["RandPercent"] = 79
- if Percent < 75 and WA["Fired75"] == true and LowAlarmEnabled == true then
- WA["Fired75"] = false
- elseif Percent < 50 and WA["Fired50"] == true and LowAlarmEnabled == true then
- WA["Fired50"] = false
- end
- if WA["Below80"] == 0 then
- if Method == "Random" then
- game.surfaces[WA["Surface"]].set_tiles(WA["WaterRepArea"])
- elseif Method == "From/To Pump" then
- -- Should Be Filled
- end
- WA["Below80"] = 1
- WGA[a]["BTF"] = WGA[a]["ShallowWater"] + WGA[a]["DeepWater"]
- end
- elseif Percent <= 0 then
- -- DO NOTHING
- end
- end
- end
- function DepleatedWaterArea(a)
- local WGA = global.WaterGlobalArea
- if WGA[a]["Depleted"] ~= 1 then
- local Percent = WGA[a]["Percent"]
- local LowAlarmEnabled = settings.global["Alarms-Low-Level (50/75/90%)"].value
- local HighAlarmEnabled = settings.global["Alarms-High-Level (95/97/98/99%)"].value
- local WA = WGA[a]
- if Percent <= 49 then
- --game.print("IN NORMAL BOUNDS")
- elseif Percent >= 50 and WA["Fired50"] == false and LowAlarmEnabled == true then
- game.print(string.format("%s has used %.0f %% of available %s.",WA["WtrName"], WA["Percent"], WA["FluidType"]))
- WA["Fired50"] = true
- elseif Percent >= 75 and WA["Fired75"] == false and LowAlarmEnabled == true then
- game.print(string.format("%s has used %.0f %% of available %s.",WA["WtrName"], WA["Percent"], WA["FluidType"]))
- WA["Fired75"] = true
- elseif Percent >= 80 and Percent < 100 then
- WA["Below80"] = 0
- local RP = WA["RandPercent"]
- if (Percent - RP) >= 1 then
- Method = settings.global["FluidArea-Replace-Method"].value
- if Method == "Random" then
- WaterRandom(a)
- elseif Method == "From/To Pump" then
- BackToFront(a)
- end
- WA["RandPercent"] = Percent
- end
- if Percent >= 90 and WA["Fired90"] == false and LowAlarmEnabled == true then
- game.print(string.format("%s has used %.0f %% of available %s.",WA["WtrName"], WA["Percent"], WA["FluidType"]))
- WA["Fired90"] = true
- elseif Percent >= 95 and WA["Fired95"] == false and HighAlarmEnabled == true then
- game.print(string.format("%s has used %.0f %% of available %s.",WA["WtrName"], WA["Percent"], WA["FluidType"]))
- WA["Fired95"] = true
- elseif Percent >= 97 and WA["Fired97"] == false and HighAlarmEnabled == true then
- game.print(string.format("%s has used %.0f %% of available %s.",WA["WtrName"], WA["Percent"], WA["FluidType"]))
- WA["Fired97"] = true
- elseif Percent >= 98 and WA["Fired98"] == false and HighAlarmEnabled == true then
- game.print(string.format("%s has used %.0f %% of available %s.",WA["WtrName"], WA["Percent"], WA["FluidType"]))
- WA["Fired98"] = true
- elseif Percent >= 99 and WA["Fired99"] == false and HighAlarmEnabled == true then
- game.print(string.format("%s has used %.0f %% of available %s.",WA["WtrName"], WA["Percent"], WA["FluidType"]))
- WA["Fired99"] = true
- end
- elseif Percent >= 100 then
- if global.NewInstall == false then
- for z = 1, #global.OPLocate, 1 do
- local OP = global.OPLocate[z]
- if OP["WA"] == a and OP["name"] ~= "offshore-pump-nofluid" then
- local OPD = OP["direction"]
- local OPE = OP["entity"]
- local OPP = OP["position"]
- local OPS = OP["surface"]
- if OP["Active"] == 1 then
- OP["Active"] = 0
- ActiveOPs = ActiveOPs - 1
- WA["OPsA"] = WA["OPsA"] - 1
- end
- if game.active_mods["aai-industry"] then -- MOD Compat with AAI, remove the additional sprites
- local x = OPP.x
- local y = OPP.y
- if OPD == 0 then
- AS = game.surfaces[OPS.name].find_entities_filtered{area={{x-1, 0},{x+1,y+1}}, name = "offshore-pump-output"}
- elseif OPD == 4 then
- AS = game.surfaces[OPS.name].find_entities_filtered{area={{x-1, y-1},{x+1,0}}, name = "offshore-pump-output"}
- elseif OPD == 2 then
- AS = game.surfaces[OPS.name].find_entities_filtered{area={{0, y-1},{x+1,y+1}}, name = "offshore-pump-output"}
- elseif OPD == 6 then
- AS = game.surfaces[OPS.name].find_entities_filtered{area={{0, y-1},{x+1,y+1}}, name = "offshore-pump-output"}
- end
- local Entity = AS[1]
- if Entity ~= nil then
- Entity.destroy()
- end
- end
- OPE.destroy()
- local OPNF = game.surfaces[OPS.name].create_entity{name="offshore-pump-nofluid",position = OPP,direction = OPD,force = "player"}
- global.OPLocate[z]["entity"] = OPNF
- end
- end
- WGA[a]["Depleted"] = 1
- WGA[a]["Percent"] = 100
- WGA[a]["PercentPrev"] = 100
- WGA[a]["RandPercent"] = 100
- game.surfaces[WA["Surface"].name].set_tiles(WGA[a]["WaterRepArea"]) -- Set Tiles to those in WaterArea Table
- WGA[a]["WtrUsed"] = WGA[a]["AmountWtr"]
- game.print(string.format("%s has been depleted of %s.",WA["WtrName"], WA["FluidType"]))
- WGA[a]["FluidType"] = "None"
- WGA[a]["BTF"] = 0
- else
- local CurrentTick = game.tick
- if CurrentTick < (global.InstallTick + 18000) and global.ITMessage == true then
- game.print(string.format("Fluid Area Depletion Stopped on New/Mid Game Install. 5 Minutes from install till depletion."))
- global.ITMessage = false
- end
- if CurrentTick >= (global.InstallTick + 18000) then
- game.print(string.format("Fluid Area 5 Mins Over. Depleting Fluid Area."))
- WGA[a]["Percent"] = 99
- global.NewInstall = false
- global.ITMessage = false
- end
- end
- end
- else
- -- AREA DEPLETED
- end
- end
- function RemoveAllDrainedArea()
- for a = #global.WaterGlobalArea, 1, -1 do -- FOR Each WATERAREA
- if global.WaterGlobalArea[a]["Depleted"] == 1 then
- -- hack only remove the list of tiles
- global.WaterGlobalArea[a]["WaterRepArea"] = {}
- end
- end
- end
- function CalcWaterUse(a)
- local WGA = global.WaterGlobalArea
- local WaterAreaUsed = WGA[a]["WtrUsed"]
- local WaterAreaDrained = WGA[a]["WtrAdd"]
- local WaterAreaAmount = WGA[a]["AmountWtr"]
- local WaterAreaPercent = WGA[a]["Percent"]
- local WaterFlowRate = WaterFlow - LastWaterFlow
- local CrudeFlowRate = CrudeFlow - LastCrudeFlow
- local WaterAreaActivePumps = WGA[a]["OPsA"]
- local WaterAreaActiveDrains = WGA[a]["ODsA"]
- local WAAD = WGA[a]["ODsA"]
- if ActiveOPs == 0 then
- ActiveOPs = 1
- end
- local PumpRatio = WaterAreaActivePumps / ActiveOPs
- if WGA[a]["FluidType"] == "water" then
- FluidFlowRate = WaterFlowRate
- elseif WGA[a]["FluidType"] == "crude-oil" then
- FluidFlowRate = CrudeFlowRate - CrudeAdjust
- elseif WGA[a]["FluidType"] == nil then
- FluidFlowRate = 0
- end
- if FluidFlowRate < 0 then
- FluidFlowRate = 0
- end
- local check = WaterAreaActivePumps * 20
- if FluidFlowRate > check then
- FluidFlowRate = WaterAreaActivePumps * 20
- end
- local FluidRatePerPump = FluidFlowRate * PumpRatio
- if WaterAreaUsed == 0 then
- --game.print("START UP")
- WGA[a]["WtrUsed"] = WaterAreaUsed + FluidRatePerPump
- WGA[a]["Depleted"] = 0
- elseif WaterAreaUsed < 0 then
- --game.print("WA < 0")
- WGA[a]["WtrUsed"] = 0
- WGA[a]["Depleted"] = 0
- elseif WaterAreaUsed == WaterAreaAmount then
- --game.print("WA = AMOUNT")
- WGA[a]["WtrUsed"] = (WaterAreaUsed - WaterAreaDrained)
- elseif WaterAreaDrained == 0 and WAAD > 0 and WaterAreaPercent < 0.3 then
- --game.print("WD = 0")
- WGA[a]["WtrUsed"] = 0
- elseif WaterAreaUsed - WaterAreaDrained > 0 then
- --game.print("WU - WD > 0")
- if WaterAreaActivePumps > 0 and WaterAreaActiveDrains > 0 then
- --game.print("WU AND WA ACTIVE")
- WGA[a]["WtrUsed"] = WaterAreaUsed + (FluidRatePerPump - WaterAreaDrained)
- elseif WaterAreaActivePumps == 0 and WaterAreaActiveDrains > 0 then
- --game.print("WD ACTIVE")
- WGA[a]["WtrUsed"] = WaterAreaUsed - WaterAreaDrained
- elseif WaterAreaActivePumps > 0 and WaterAreaActiveDrains == 0 then
- --game.print("WA ACTIVE")
- WGA[a]["WtrUsed"] = WaterAreaUsed + FluidRatePerPump
- end
- elseif WaterAreaUsed - WaterAreaDrained < 0 then
- --game.print("WU - WD < 0")
- WGA[a]["WtrUsed"] = 0
- WGA[a]["Depleted"] = 0
- else
- --game.print("WU = WU")
- WGA[a]["WtrUsed"] = WaterAreaUsed
- end
- WGA[a]["PercentPrev"] = WGA[a]["Percent"]
- WGA[a]["Percent"] = (WGA[a]["WtrUsed"] / WGA[a]["AmountWtr"]) * 100
- PercentChange = WGA[a]["PercentPrev"] - WGA[a]["Percent"]
- end
- function CheckDrainAssignedFluidUse(a) -- Assign Fluid to Empty WaterArea
- for c = 1, #global.ODLocate,1 do
- local ODL = global.ODLocate
- local WGA = global.WaterGlobalArea
- 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
- if ODL[c]["entity"].neighbours[1][1].get_fluid_count() > 0.005 then
- fluid = ODL[c]["entity"].neighbours[1][1].fluidbox[1]
- if ODL[c]["entity"].neighbours[1][1]["name"] == "storage-tank" then
- if fluid.amount >= 20 then
- WGA[a]["WtrAdd"] = WGA[a]["WtrAdd"] + 20
- elseif fluid.amount < 20 and fluid.amount > 0 then
- WGA[a]["WtrAdd"] = WGA[a]["WtrAdd"] + 1
- elseif fluid.amount == 0 then
- WGA[a]["WtrAdd"] = WGA[a]["WtrAdd"]
- end
- else
- if fluid.amount >= 20 then
- WGA[a]["WtrAdd"] = WGA[a]["WtrAdd"] + 20
- elseif fluid.amount < 20 and fluid.amount > 0 then
- WGA[a]["WtrAdd"] = WGA[a]["WtrAdd"] + fluid.amount
- elseif fluid.amount == 0 then
- WGA[a]["WtrAdd"] = WGA[a]["WtrAdd"]
- end
- end
- else
- WGA[a]["WtrAdd"] = WGA[a]["WtrAdd"]
- end
- end
- end
- end
- function CheckDrainAssignedFluid(a)
- if ActiveODs ~= 0 then
- local WGA = global.WaterGlobalArea
- local ODL = global.ODLocate
- for c = 1, #WGA, 1 do
- if WGA[c]["FluidType"] == "None" then
- if ODL[a]["WA"] == c and ODL[a]["entity"].neighbours[1][1] and ODL[a]["entity"].neighbours[1][1].fluidbox[1] ~= nil then
- WGA[c]["FluidType"] = ODL[a]["PipeFluid"]
- end
- end
- end
- end
- end
- function CheckOPActive(a)
- local GOPL = global.OPLocate[a]
- local WGA = global.WaterGlobalArea
- if GOPL["Active"] == 0 and WGA[GOPL["WA"]]["Depleted"] ~= 1 then
- if GOPL["entity"].valid and GOPL["entity"].neighbours[1][1] then
- GOPL["Active"] = 1
- if WGA[GOPL["WA"]]["OPsA"] <= 0 then
- WGA[GOPL["WA"]]["OPsA"] = 1
- else
- WGA[GOPL["WA"]]["OPsA"] = WGA[GOPL["WA"]]["OPsA"] + 1
- end
- ActiveOPs = ActiveOPs + 1
- end
- elseif GOPL["Active"] == 1 and WGA[GOPL["WA"]]["Depleted"] ~= 1 then
- if GOPL["entity"].valid and GOPL["entity"].neighbours[1][1] then
- GOPL["Active"] = 1
- ActiveOPs = ActiveOPs + 1
- elseif GOPL["entity"].valid and not GOPL["entity"].neighbours[1][1] then
- GOPL["Active"] = 0
- if WGA[GOPL["WA"]]["OPsA"] <= 0 then
- WGA[GOPL["WA"]]["OPsA"] = 0
- else
- WGA[GOPL["WA"]]["OPsA"] = WGA[GOPL["WA"]]["OPsA"] - 1
- end
- ActiveOPs = ActiveOPs - 1
- end
- end
- end
- function CheckODActive(a)
- local GOPL = global.ODLocate[a]
- local WGA = global.WaterGlobalArea
- local ODL = global.ODLocate
- if GOPL["Active"] == 0 then
- if GOPL["entity"].neighbours[1][1] then
- if GOPL["entity"].valid and GOPL["entity"].neighbours[1][1].fluidbox[1] ~= nil and WGA[ODL[a]["WA"]]["WtrUsed"] > 0.005 then
- GOPL["Active"] = 1
- if WGA[GOPL["WA"]]["ODsA"] <= 0 then
- WGA[GOPL["WA"]]["ODsA"] = 1
- else
- WGA[GOPL["WA"]]["ODsA"] = WGA[GOPL["WA"]]["ODsA"] + 1
- end
- ActiveODs = ActiveODs + 1
- GOPL["PipeFluid"] = GOPL["entity"].neighbours[1][1].fluidbox[1].name
- end
- else
- GOPL["entity"].clear_fluid_inside()
- end
- elseif GOPL["Active"] == 1 then
- 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
- GOPL["Active"] = 1
- ActiveODs = ActiveODs + 1
- GOPL["PipeFluid"] = GOPL["entity"].neighbours[1][1].fluidbox[1].name
- 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
- GOPL["Active"] = 0
- if WGA[GOPL["WA"]]["ODsA"] <= 0 then
- WGA[GOPL["WA"]]["ODsA"] = 0
- else
- WGA[GOPL["WA"]]["ODsA"] = WGA[GOPL["WA"]]["ODsA"] - 1
- end
- ActiveODs = ActiveODs - 1
- GOPL["PipeFluid"] = "None"
- end
- end
- end
- function LandFill(a)
- local WGA = global.WaterGlobalArea
- WASize = #WGA[a]["WaterRepArea"]
- WAra = WGA[a]["WaterRepArea"]
- for b = #global.LandFill, 1, -1 do
- LFPosX = global.LandFill[b]["position"]["x"] + 0.5
- LFPosY = global.LandFill[b]["position"]["y"] + 0.5
- for c = 1, WASize, 1 do
- if WAra[c]["name"] ~= "landfill" then
- WAPosX = WAra[c]["position"]["x"]
- WAPosY = WAra[c]["position"]["y"]
- if LFPosX == WAPosX then -- IF LandFill position x is equal to FluidArea position x
- if LFPosY == WAPosY then -- IF LandFill position y is equal to FluidArea position y
- global.WaterGlobalArea[a]["WaterRepArea"][c]["name"] = "landfill"
- local FluidName = global.LandFill[b]["name"]
- if FluidName == "water" or FluidName == "crude-oil" or FluidName == "water-green" or FluidName == "water-shallow" or FluidName == "water-mud" then
- local ShallowAmount = settings.global["TileFluidAmount-Shallow"].value
- global.WaterGlobalArea[a]["AmountWtr"] = WGA[a]["AmountWtr"] - (ShallowAmount * WGA[a]["AmountBonusValue"])
- elseif FluidName == "deepwater" or FluidName == "crude-oil-deep" or FluidName == "deepwater-green" then
- local DeepAmount = settings.global["TileFluidAmount-Deep"].value
- global.WaterGlobalArea[a]["AmountWtr"] = WGA[a]["AmountWtr"] - (DeepAmount * WGA[a]["AmountBonusValue"])
- end
- table.remove(global.LandFill,#global.LandFill)
- goto EscapeLFSearch
- end
- end
- end
- if a == #global.WaterGlobalArea and c == WASize then
- table.remove(global.LandFill,#global.LandFill)
- goto EscapeLFSearch
- end
- end
- ::EscapeLFSearch::
- end
- if #global.LandFill == 0 then
- local LandfillEnabled = settings.global["Alarms-Landfill Message"].value
- game.print(string.format("Landfill has reduced FluidArea %s, to %sL of %s.", WGA[a]["WtrName"], WGA[a]["AmountWtr"], WGA[a]["FluidType"]))
- end
- ::EscapeLFSearchNF::
- end
- function CheckActive()
- ActiveOPs = 0
- ActiveODs = 0
- if #global.OPLocate ~= 0 then
- for a = 1, #global.OPLocate, 1 do
- CheckOPActive(a)
- end
- end
- if #global.ODLocate ~= 0 then
- for a = 1, #global.ODLocate, 1 do
- CheckODActive(a)
- CheckDrainAssignedFluid(a)
- end
- end
- end
- function ScanOffshores()
- for a = #game.surfaces, 1, -1 do
- local Jacks = game.surfaces[a].find_entities_filtered{name= "pumpjack"}
- if #Jacks > 0 then
- for b = 1, #Jacks, 1 do
- Jackentity = Jacks[b]
- Jackposition = Jackentity.position
- Jacksurface = Jackentity.surface
- global.FluidProducers[#global.FluidProducers+1] = {["entity"] = Jackentity, ["position"] = {["x"] = Jackposition.x , ["y"] = Jackposition.y}, ["surface"] = Jacksurface, ["FluidType"] = nil, ["LastAmount"] = 0}
- end
- end
- local Pumps = game.surfaces[a].find_entities_filtered{name= "offshore-pump"}
- if #Pumps > 0 then
- for b = 1, #Pumps, 1 do
- Pumpentity = Pumps[b]
- Pumpposition = Pumpentity.position -- Variable for e.Position
- Pumpsurface = Pumpentity.surface -- Variable for e.Surface
- Pumpdirection = Pumpentity.direction -- Variable for OPentity.direction
- global.OPLocate[#global.OPLocate+1] = {["entity"] = Pumpentity,["position"] = {["x"] = Pumpposition.x, ["y"] = Pumpposition.y},["surface"] = Pumpsurface,["direction"] = Pumpdirection,["tile"] = "water", ["Active"] = 0, ["WA"] = 0}
- global.Type = 1
- CorrectPump()
- GlobalWaterArea()
- global.Type = 0
- end
- end
- end
- global.Added = false
- end
- function PumpRereplace(a)
- local PRPV = settings.global["FluidArea-RereplacePumps"].value
- local PRP = 100 - PRPV
- local WGA = global.WaterGlobalArea
- if PRPV == 0 then
- --Do Nothing as Off
- else
- local FAP = WGA[a]["Percent"]
- if PRP >= FAP then
- for b = #global.OPLocate, 1, -1 do
- ----------------------------------------- Dexy Edit ------------------------------------
- if global.OPLocate[b]["entity"].valid == false then
- return
- end
- ----------------------------------------------------------------------------------------
- if global.OPLocate[b]["WA"] == a and global.OPLocate[b]["entity"].name == "offshore-pump-nofluid"then
- local OP = global.OPLocate[b]
- local OPD = OP["direction"]
- local OPE = OP["entity"]
- local OPP = OP["position"]
- local OPS = OP["surface"]
- OPE.destroy()
- if WGA[a]["FluidType"] == "water" then
- OPN = game.surfaces[OPS.name].create_entity{name="offshore-pump",position = OPP,direction = OPD,force = "player"}
- elseif WGA[a]["FluidType"] == "crude-oil" then
- OPN = game.surfaces[OPS.name].create_entity{name="offshore-crude-oil-pump",position = OPP,direction = OPD,force = "player"}
- end
- global.OPLocate[b]["entity"] = OPN
- if global.OPLocate[b]["entity"] == nil then
- if global.WaterGlobalArea[global.OPLocate[b]["WA"]]["OPs"] <= 0 then
- global.WaterGlobalArea[global.OPLocate[b]["WA"]]["OPs"] = 0
- else
- global.WaterGlobalArea[global.OPLocate[b]["WA"]]["OPs"] = global.WaterGlobalArea[global.OPLocate[b]["WA"]]["OPs"] - 1
- end
- 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"]))
- table.remove(global.OPLocate,b)
- game.surfaces[OPS.name].spill_item_stack(OPP, {name="offshore-pump",count=1})
- end
- end
- end
- end
- end
- end
- function CheckWater ()
- local WGA = global.WaterGlobalArea
- fluidentities.CheckFluidProducers()
- if global.Added == true then
- ScanOffshores()
- end
- if #WGA ~= nil and global.Added ~= true then
- WaterFlow = math.ceil(game.forces["player"].fluid_production_statistics.get_input_count("water"))
- CrudeFlow = math.ceil(game.forces["player"].fluid_production_statistics.get_input_count("crude-oil"))
- CheckActive()
- for a = 1, #WGA, 1 do
- if WGA[a]["OPsA"] > 0 or WGA[a]["ODsA"] > 0 then
- CheckDrainAssignedFluidUse(a)
- CalcWaterUse(a)
- AssignTiles(a)
- end
- if PercentChange <= 0 then
- DepleatedWaterArea(a)
- elseif PercentChange > 0 then
- AddedWaterArea(a)
- PumpRereplace(a)
- end
- if WGA[a]["ODsA"] > 0 then
- EmptyDrainPipes()
- end
- end
- LastWaterFlow = WaterFlow
- LastCrudeFlow = CrudeFlow
- end
- end
- -- SCRIPT EVENT FUNCTIONS --
- function LandFillCheck(event)
- if event.item.valid == true then
- if event.item.name == "landfill" then
- local tiles = event.tiles
- local surface = event.surface_index
- for a = 1, #tiles, 1 do
- global.LandFill[#global.LandFill+1] = {["name"] = tiles[a].old_tile.name, ["position"] = {["x"] = tiles[a]["position"].x, ["y"] = tiles[a]["position"].y},["surface"] = surface}
- end
- end
- end
- end
- function BuiltOffShore(event) -- Script Event On Built
- if event.created_entity.valid == true then
- if event.created_entity.name =="offshore-pump" then -- If Offshore Pump Built
- OPentity = event.created_entity -- Variable for Event.created_entity
- OPposition = OPentity.position -- Variable for e.Position
- OPsurface = OPentity.surface -- Variable for e.Surface
- OPdirection = OPentity.direction -- Variable for OPentity.direction
- tile = OPsurface.get_tile(OPposition.x,OPposition.y).name
- CorrectedFluid(tile)
- global.OPLocate[#global.OPLocate+1] = {["entity"] = OPentity,["position"] = {["x"] = OPposition.x, ["y"] = OPposition.y},["surface"] = OPsurface,["direction"] = OPdirection,["tile"] = correctedtile, ["Active"] = 0, ["WA"] = 0}
- global.Type = 1
- CorrectPump()
- GlobalWaterArea()
- global.Type = 0
- elseif event.created_entity.name =="offshore-drain" then
- ODentity = event.created_entity
- ODposition = ODentity.position
- ODsurface = ODentity.surface
- ODdirection = ODentity.direction
- tile = ODsurface.get_tile(ODposition.x,ODposition.y).name
- CorrectedFluid(tile)
- 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}
- global.Type = 2
- GlobalWaterArea()
- global.Type = 0
- elseif event.created_entity.name =="pumpjack" then
- FPentity = event.created_entity
- FPposition = FPentity.position
- FPsurface = FPentity.surface
- global.FluidProducers[#global.FluidProducers+1] = {["entity"] = FPentity, ["position"] = {["x"] = FPposition.x , ["y"] = FPposition.y}, ["surface"] = FPsurface, ["FluidType"] = nil, ["LastAmount"] = 0}
- else
- --game.print("Not Built A Offshore Pump") -- If Not Offshore Pump then print Bad Times
- end
- global.Type = 0
- else
- game.print("Entity Creation Was Invalid - Not Added")
- end
- end
- function DestroyedOffShore(event) -- Script Event On Player Mined
- 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
- --game.print("PICKED UP MY OFFSHORE PUMP") -- Print My Offshore Pump
- DOentity = event.entity
- DOposition = DOentity.position
- global.Type = 1
- DestroyOffshore()
- global.Type = 0
- elseif event.entity.name == "offshore-drain" then
- DOentity = event.entity
- if DOentity.neighbours[1][1] and DOentity.neighbours[1][1].get_fluid_count() ~= 0 then
- DOentity.neighbours[1][1].fluidbox[1] = nil
- end
- DOposition = DOentity.position
- global.Type = 2
- DestroyOffshore()
- global.Type = 0
- elseif event.entity.name == "pumpjack" then
- DOentity = event.entity
- DOposition = DOentity.position
- global.Type = 3
- DestroyOffshore()
- global.Type = 0
- end
- end
- function DestroyOffshore()
- if global.Type == 1 then
- PTYPE = global.OPLocate
- PTYPEL = #global.OPLocate
- elseif global.Type == 2 then
- PTYPE = global.ODLocate
- PTYPEL = #global.ODLocate
- elseif global.Type == 3 then
- PTYPE = global.FluidProducers
- PTYPEL = #global.FluidProducers
- end
- local DOPosX = DOposition.x
- local DOPosY = DOposition.y
- for a = PTYPEL, 1, -1 do
- local OPosX = PTYPE[a]["position"]["x"]
- local OPosY = PTYPE[a]["position"]["y"]
- if DOPosX == OPosX then
- if DOPosY == OPosY then
- if global.Type == 1 then
- local WGA = PTYPE[a]["WA"]
- if #global.WaterGlobalArea ~= 0 and WGA ~= 0 then
- global.WaterGlobalArea[WGA]["OPs"] = global.WaterGlobalArea[WGA]["OPs"] - 1
- end
- if PTYPE[a]["Active"] == 1 and WGA ~= 0 then
- global.WaterGlobalArea[WGA]["OPsA"] = global.WaterGlobalArea[WGA]["OPsA"] - 1
- ActiveOPs = ActiveOPs - 1
- end
- elseif global.Type == 2 then
- local WGA = PTYPE[a]["WA"]
- if #global.WaterGlobalArea ~= 0 and WGA ~= 0 then
- global.WaterGlobalArea[WGA]["ODs"] = global.WaterGlobalArea[WGA]["ODs"] - 1
- end
- if PTYPE[a]["Active"] == 1 and WGA ~= 0 then
- global.WaterGlobalArea[WGA]["ODsA"] = global.WaterGlobalArea[WGA]["ODsA"] - 1
- ActiveODs = ActiveODs - 1
- end
- end
- table.remove(PTYPE,a)
- end
- end
- end
- end
- function UpdateMod(data)
- if data.mod_changes.WaterAsAResource then
- oldVer = data.mod_changes.WaterAsAResource.old_version
- if oldVer == nil then
- global.LastWaterFlow = math.ceil(game.forces["player"].fluid_production_statistics.get_input_count("water"))
- global.LastCrudeFlow = math.ceil(game.forces["player"].fluid_production_statistics.get_input_count("crude-oil"))
- global.InstallTick = game.tick
- else
- --game.print("Water As A Resource: Updating Mod Internals")
- Globals()
- LinkGlobals()
- if #global.WaterGlobalArea > 0 then
- for a = 1, #global.WaterGlobalArea, 1 do
- local Count = global.WaterGlobalArea[a]["ShallowWater"] + global.WaterGlobalArea[a]["DeepWater"]
- if global.WaterGlobalArea[a]["WtrName"] == nil or global.WaterGlobalArea[a]["WtrName"] == "None" then
- global.WaterGlobalArea[a]["WtrName"] = a
- end
- if global.WaterGlobalArea[a]["AmountBonusValue"] == nil or global.WaterGlobalArea[a]["AmountBonusValue"] == 0 then
- if Count < 4 then
- global.WaterGlobalArea[a]["AmountBonusValue"] = 0.01
- elseif Count == 4 then
- global.WaterGlobalArea[a]["AmountBonusValue"] = 0.5
- elseif Count >4 and Count <= 200 then
- global.WaterGlobalArea[a]["AmountBonusValue"] = 1
- elseif Count > 200 and Count <= 6000 then
- global.WaterGlobalArea[a]["AmountBonusValue"] = 1.5
- elseif Count > 6000 and Count <= 60000 then
- global.WaterGlobalArea[a]["AmountBonusValue"] = 2
- elseif Count > 60000 and Count <= 600000 then
- global.WaterGlobalArea[a]["AmountBonusValue"] = 2.5
- else
- global.WaterGlobalArea[a]["AmountBonusValue"] = 3
- end
- end
- if global.WaterGlobalArea[a]["WtrUsed"] == nil then
- global.WaterGlobalArea[a]["WtrUsed"] = (global.WaterGlobalArea[a]["AmountWtr"] * global.WaterGlobalArea[a]["Percent"]) / 100
- end
- if global.WaterGlobalArea[a]["OPsA"] == nil or global.WaterGlobalArea[a]["OPsA"] < 0 then
- global.WaterGlobalArea[a]["OPsA"] = 0
- end
- if global.WaterGlobalArea[a]["WtrAdd"] == nil then
- global.WaterGlobalArea[a]["WtrAdd"] = 0
- end
- if global.WaterGlobalArea[a]["ODsA"] == nil or global.WaterGlobalArea[a]["ODsA"] < 0 then
- global.WaterGlobalArea[a]["ODsA"] = 0
- end
- if global.WaterGlobalArea[a]["ODs"] == nil then
- global.WaterGlobalArea[a]["ODs"] = 0
- end
- if global.WaterGlobalArea[a]["PercentPrev"] == nil then
- global.WaterGlobalArea[a]["PercentPrev"] = global.WaterGlobalArea[a]["Percent"]
- end
- if global.WaterGlobalArea[a]["Depleted"] == 1 then
- global.WaterGlobalArea[a]["FluidType"] = "None"
- global.WaterGlobalArea[a]["Percent"] = 100
- global.WaterGlobalArea[a]["PercentPrev"] = 100
- global.WaterGlobalArea[a]["WtrUsed"] = global.WaterGlobalArea[a]["AmountWtr"]
- else
- global.WaterGlobalArea[a]["FluidType"] = "water"
- global.WaterGlobalArea[a]["Percent"] = 0
- global.WaterGlobalArea[a]["PercentPrev"] = 0
- global.WaterGlobalArea[a]["WtrUsed"] = 0
- end
- if global.WaterGlobalArea[a]["ToSearch"] == nil or global.WaterGlobalArea[a]["ToSearch"] == 0 then
- global.WaterGlobalArea[a]["ToSearch"] = nil
- end
- if global.WaterGlobalArea[a]["HasSearched"] == nil or global.WaterGlobalArea[a]["HasSearched"] == 0 then
- global.WaterGlobalArea[a]["HasSearched"] = nil
- end
- if global.WaterGlobalArea[a]["TilesSet"] == nil then
- global.WaterGlobalArea[a]["TilesSet"] = "N"
- end
- if global.WaterGlobalArea[a]["Percent"] < 50 then
- global.WaterGlobalArea[a]["Fired50"] = false
- global.WaterGlobalArea[a]["Fired75"] = false
- global.WaterGlobalArea[a]["Fired90"] = false
- global.WaterGlobalArea[a]["Fired95"] = false
- global.WaterGlobalArea[a]["Fired97"] = false
- global.WaterGlobalArea[a]["Fired98"] = false
- global.WaterGlobalArea[a]["Fired99"] = false
- end
- if global.WaterGlobalArea[a]["Percent"] >= 50 then
- global.WaterGlobalArea[a]["Fired50"] = true
- end
- if global.WaterGlobalArea[a]["Percent"] >= 75 then
- global.WaterGlobalArea[a]["Fired75"] = false
- end
- if global.WaterGlobalArea[a]["Percent"] >= 90 then
- global.WaterGlobalArea[a]["Fired90"] = true
- end
- if global.WaterGlobalArea[a]["Percent"] >= 95 then
- global.WaterGlobalArea[a]["Fired95"] = true
- end
- if global.WaterGlobalArea[a]["Percent"] >= 97 then
- global.WaterGlobalArea[a]["Fired97"] = true
- end
- if global.WaterGlobalArea[a]["Percent"] >= 98 then
- global.WaterGlobalArea[a]["Fired98"] = true
- end
- if global.WaterGlobalArea[a]["Percent"] >= 99 then
- global.WaterGlobalArea[a]["Fired99"] = true
- end
- global.WaterGlobalArea[a]["BTF"] = Count
- for b = 1, Count, 1 do
- local tile = global.WaterGlobalArea[a]["WaterRepArea"][b]["name"]
- if tile == "water" or tile == "deepwater" then
- global.WaterGlobalArea[a]["FluidType"] = "water"
- elseif tile == "crude-oil" or tile == "crude-oil-deep" then
- global.WaterGlobalArea[a]["FluidType"] = "crude-oil"
- elseif global.WaterGlobalArea[a]["FluidType"] == nil then
- global.WaterGlobalArea[a]["FluidType"] = "None"
- end
- end
- if global.WaterGlobalArea[a]["LoopCount"] == nil then
- global.WaterGlobalArea[a]["LoopCount"] = 0
- end
- if global.WaterGlobalArea[a]["RegenAmount"] == nil then
- global.WaterGlobalArea[a]["RegenAmount"] = 0
- end
- if global.WaterGlobalArea[a]["Percent"] >= 80 and global.WaterGlobalArea[a]["Percent"] < 100 then
- local TPP = math.floor((global.WaterGlobalArea[a]["ShallowWater"] + global.WaterGlobalArea[a]["DeepWater"]) / 20)
- local PF = math.floor(global.WaterGlobalArea[a]["Percent"] - 80) / 0.1
- local Tiles = TPP * (PF/100)
- global.WaterGlobalArea[a]["BTF"] = global.WaterGlobalArea[a]["BTF"] - Tiles
- else
- global.WaterGlobalArea[a]["BTF"] = Count
- end
- for b = 1, #game.surfaces, 1 do
- pos = global.WaterGlobalArea[a]["WaterRepArea"][1]["position"]
- local tile = game.surfaces[b].get_tile(pos)
- if tile.valid == true then
- local tilename = tile.name
- if tilename == "water" or tilename =="deepwater" or tilename=="crude-oil" or tilename=="crude-oil-deep" or tilename=="sand-3" or tilename=="dry-dirt" then
- global.WaterGlobalArea[a]["Surface"] = game.surfaces[b]
- end
- end
- for c = Count, 1 , -1 do
- posc = global.WaterGlobalArea[a]["WaterRepArea"][c]["position"]
- local tile = game.surfaces[b].get_tile(posc)
- if tile.valid == true then
- local tilename = tile.name
- if tilename == "water" or tilename =="deepwater" or tilename=="crude-oil" or tilename=="crude-oil-deep" then
- global.WaterGlobalArea[a]["WaterRepArea"][c]["OriginalName"] = tilename
- end
- end
- end
- end
- end
- end
- if #global.OPLocate > 0 then
- for a = #global.OPLocate, 1, -1 do
- if global.OPLocate[a]["WA"] == nil or global.OPLocate[a]["WA"] == 0 then
- if global.OPLocate[a]["OPposition"] ~= nil then
- OPTilePosX = global.OPLocate[a]["OPposition"]["x"]
- OPTilePosY = global.OPLocate[a]["OPposition"]["y"]
- else
- OPTilePosX = global.OPLocate[a]["position"]["x"]
- OPTilePosY = global.OPLocate[a]["position"]["y"]
- end
- for b = 1, #global.WaterGlobalArea, 1 do
- CompTiles = global.WaterGlobalArea[b]["ShallowWater"] + global.WaterGlobalArea[b]["DeepWater"]
- for c = CompTiles, 1, -1 do -- FOR EACH TILE in WATERAREA
- WATilePosX = global.WaterGlobalArea[b]["WaterRepArea"][c]["position"]["x"]
- WATilePosY = global.WaterGlobalArea[b]["WaterRepArea"][c]["position"]["y"]
- if WATilePosX == OPTilePosX then
- if WATilePosY == OPTilePosY then
- global.OPLocate[a]["WA"] = b
- end
- end
- end
- end
- end
- if global.OPLocate[a]["Active"] == nil then
- global.OPLocate[a]["Active"] = 0
- end
- if global.OPLocate[a]["entity"] == nil then
- global.OPLocate[a]["entity"] = global.OPLocate[a]["OPentity"]
- end
- if global.OPLocate[a]["position"] == nil then
- global.OPLocate[a]["position"] = global.OPLocate[a]["OPposition"]
- end
- if global.OPLocate[a]["direction"] == nil then
- global.OPLocate[a]["direction"] = global.OPLocate[a]["OPdirection"]
- end
- for b = 1, #global.WaterGlobalArea, 1 do
- if global.OPLocate[a]["WA"] == b and global.WaterGlobalArea[b]["Depleted"] ~= 1 then
- global.OPLocate[a]["tile"] = global.WaterGlobalArea[b]["FluidType"]
- elseif global.OPLocate[a]["WA"] == b and global.WaterGlobalArea[b]["Depleted"] == 1 then
- global.OPLocate[a]["tile"] = "None"
- end
- end
- for b = 1, #game.surfaces, 1 do
- pos = global.OPLocate[a]["position"]
- local tile = game.surfaces[b].get_tile(pos)
- if tile.valid == true then
- local tilename = tile.name
- if tilename == "water" or tilename =="deepwater" or tilename=="crude-oil" or tilename=="crude-oil-deep" or tilename=="sand-3" or tilename=="dry-dirt" then
- global.OPLocate[a]["surface"] = game.surfaces[b]
- end
- end
- end
- if global.OPLocate[a]["tile"] == "water" or global.OPLocate[a]["tile"] == "deepwater" then
- local OPE = global.OPLocate[a]["entity"]
- local OPD = global.OPLocate[a]["direction"]
- local OPP = global.OPLocate[a]["position"]
- local OPS = global.OPLocate[a]["surface"]
- OPE.destroy()
- local OPN = game.surfaces[OPS.name].create_entity{name="offshore-pump",position = OPP,direction = OPD,force = "player"}
- global.OPLocate[a]["entity"] = OPN
- if global.OPLocate[a]["entity"] == nil then
- if global.WaterGlobalArea[global.OPLocate[a]["WA"]]["OPs"] <= 0 then
- global.WaterGlobalArea[global.OPLocate[a]["WA"]]["OPs"] = 0
- else
- global.WaterGlobalArea[global.OPLocate[a]["WA"]]["OPs"] = global.WaterGlobalArea[global.OPLocate[a]["WA"]]["OPs"] - 1
- end
- table.remove(global.OPLocate,a)
- game.surfaces[OPS.name].spill_item_stack(OPP, {name="offshore-pump",count=1})
- 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"]))
- end
- elseif global.OPLocate[a]["tile"] == "crude-oil" or global.OPLocate[a]["tile"] == "crude-oil-deep" then
- local OPE2 = global.OPLocate[a]["entity"]
- local OPD2 = global.OPLocate[a]["direction"]
- local OPP2 = global.OPLocate[a]["position"]
- local OPS2 = global.OPLocate[a]["surface"]
- OPE2.destroy()
- local OPN2 = game.surfaces[OPS2.name].create_entity{name="offshore-crude-oil-pump",position = OPP2,direction = OPD2,force = "player"}
- global.OPLocate[a]["entity"] = OPN2
- if global.OPLocate[a]["entity"] == nil then
- if global.WaterGlobalArea[global.OPLocate[a]["WA"]]["OPs"] <= 0 then
- global.WaterGlobalArea[global.OPLocate[a]["WA"]]["OPs"] = 0
- else
- global.WaterGlobalArea[global.OPLocate[a]["WA"]]["OPs"] = global.WaterGlobalArea[global.OPLocate[a]["WA"]]["OPs"] - 1
- end
- table.remove(global.OPLocate,a)
- game.surfaces[OPS2.name].spill_item_stack(OPP2, {name="offshore-pump",count=1})
- 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"]))
- end
- else
- local OPE3 = global.OPLocate[a]["entity"]
- local OPD3 = global.OPLocate[a]["direction"]
- local OPP3 = global.OPLocate[a]["position"]
- local OPS3 = global.OPLocate[a]["surface"]
- OPE3.destroy()
- local OPN3 = game.surfaces[OPS3.name].create_entity{name="offshore-pump-nofluid",position = OPP3,direction = OPD3,force = "player"}
- global.OPLocate[a]["entity"] = OPN3
- end
- end
- end
- if #global.ODLocate > 0 then
- for a = 1, #global.ODLocate, 1 do
- for b = 1, #game.surfaces, 1 do
- pos = global.ODLocate[a]["position"]
- local tile = game.surfaces[b].get_tile(pos)
- if tile.valid == true then
- local tilename = tile.name
- if tilename == "water" or tilename =="deepwater" or tilename=="crude-oil" or tilename=="crude-oil-deep" or tilename=="sand-3" or tilename=="dry-dirt" then
- global.ODLocate[a]["surface"] = game.surfaces[b]
- end
- end
- end
- local OPE4 = global.ODLocate[a]["entity"]
- local OPD4 = global.ODLocate[a]["direction"]
- local OPP4 = global.ODLocate[a]["position"]
- local OPS4 = global.ODLocate[a]["surface"]
- OPE4.destroy()
- local OPN4 = game.surfaces[OPS4.name].create_entity{name='offshore-drain',position = OPP4, direction = OPD4, force = "player"}
- global.ODLocate[a]["entity"] = OPN4
- for b = 1, #global.WaterGlobalArea, 1 do
- if global.ODLocate[a]["WA"] == b and global.WaterGlobalArea[b]["Depleted"] ~= 1 then
- global.ODLocate[a]["tile"] = global.WaterGlobalArea[b]["FluidType"]
- global.ODLocate[a]["PipeFluid"] = global.WaterGlobalArea[b]["FluidType"]
- elseif global.ODLocate[a]["WA"] == b and global.WaterGlobalArea[b]["Depleted"] == 1 then
- global.ODLocate[a]["tile"] = "None"
- global.ODLocate[a]["PipeFluid"] = "None"
- end
- end
- end
- end
- if #global.FluidProducers > 0 then
- for b = 1, #game.surfaces, 1 do
- local Pumps = game.surfaces[b].find_entities_filtered{name= "pumpjack"}
- for a = 1, #Pumps, 1 do
- global.FluidProducers[a]["entity"] = Pumps[a]
- global.FluidProducers[a]["position"]["x"] = Pumps[a]["position"].x
- global.FluidProducers[a]["position"]["y"] = Pumps[a]["position"].y
- global.FluidProducers[a]["surface"] = Pumps[a].surface
- global.FluidProducers[a]["FluidType"] = nil
- global.FluidProducers[a]["LastAmount"] = 0
- end
- end
- end
- end
- global.Added = false
- global.NewInstall = false
- end
- end
- -- COMMAND FUNCTIONS --
- function RestoreWater()
- local RWDisabled = settings.global["Disable-RestoreWater-Command"].value
- if RWDisabled == true then
- game.print("RestoreWater Command has been disabled in map settings.")
- else
- if global.WaterGlobalArea then
- for a = 1, #global.WaterGlobalArea, 1 do
- if global.WaterGlobalArea[a]["Surface"].valid then -- Dexy Edit
- local NoTiles = global.WaterGlobalArea[a]["ShallowWater"] + global.WaterGlobalArea[a]["DeepWater"]
- if NoTiles ~= nil then -- Dexy Edit
- for b = 1, NoTiles, 1 do
- if global.WaterGlobalArea[a]["WaterRepArea"][b] ~= nil then -- Dexy Edit
- local TileName = global.WaterGlobalArea[a]["WaterRepArea"][b]["name"]
- if TileName == "sand-3" or TileName == "crude-oil" then
- global.WaterGlobalArea[a]["WaterRepArea"][b]["name"] = "water"
- elseif TileName == "dry-dirt" or TileName == "crude-oil-deep" then
- global.WaterGlobalArea[a]["WaterRepArea"][b]["name"] = "deepwater"
- else
- game.print("Not A Replaceable Tile")
- end
- end
- end
- end
- game.surfaces[global.WaterGlobalArea[a]["Surface"].name].set_tiles(global.WaterGlobalArea[a]["WaterRepArea"])
- end
- end
- global.WaterGlobalArea = {}
- end
- if global.OPLocate then
- for a = 1, #global.OPLocate, 1 do
- RepOP = global.OPLocate[a]["position"]
- RepOPD = global.OPLocate[a]["direction"]
- RepOPE = global.OPLocate[a]["entity"]
- RepOPS = global.OPLocate[a]["surface"]
- RepOPE.destroy()
- if RepOPS.valid and game.surfaces[RepOPS.name] ~= nil and game.surfaces[RepOPS.name].valid then -- Dexy edit
- game.surfaces[RepOPS.name].create_entity{name="offshore-pump",position = RepOP,direction = RepOPD,force = "player"}
- end
- end
- global.OPLocate = {}
- end
- if global.ODLocate then
- for a = 1, #global.ODLocate, 1 do
- RepODE = global.ODLocate[a]["entity"]
- RepODE.destroy()
- end
- global.ODLocate = {}
- end
- global.FluidProducers = {}
- global.PercentChange = 0
- global.WaterFlow = 0
- global.CrudeFlow = 0
- global.LastWaterFlow = 0
- global.LastCrudeFlow = 0
- game.print("FluidAreas Restored!")
- end
- end
- function Offshores()
- game.print("Offshore Pump(s) Location Info")
- if #global.OPLocate == 0 then
- game.print("No Offshore Pumps to show.")
- else
- for a = 1, #global.OPLocate, 1 do
- local FluidName = string.sub(global.OPLocate[a]["tile"],1,1):upper()..string.sub(global.OPLocate[a]["tile"],2)
- local SurfaceName = string.sub(global.OPLocate[a]["surface"].name,1,1):upper()..string.sub(global.OPLocate[a]["surface"].name,2)
- 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"]))
- end
- end
- end
- function OffshoreDrains()
- game.print("Offshore Drain(s) Location Info")
- if #global.ODLocate == 0 then
- game.print("No Offshore Drains to show.")
- else
- for a = 1, #global.ODLocate, 1 do
- if global.ODLocate[a]["PipeFluid"] == nil then
- FluidName = "None"
- else
- FluidName = string.sub(global.ODLocate[a]["PipeFluid"],1,1):upper()..string.sub(global.ODLocate[a]["PipeFluid"],2)
- end
- local SurfaceName = string.sub(global.ODLocate[a]["surface"].name,1,1):upper()..string.sub(global.ODLocate[a]["surface"].name,2)
- 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"]))
- end
- end
- end
- function WaterAreas()
- game.print("FluidArea(s) Info")
- if #global.WaterGlobalArea == 0 then
- game.print("No FluidAreas to show.")
- else
- for a = 1, #global.WaterGlobalArea, 1 do
- if #global.WaterGlobalArea[a]["WaterRepArea"] > 0 then -- == 0 When removed
- local FluidName = string.sub(global.WaterGlobalArea[a]["FluidType"],1,1):upper()..string.sub(global.WaterGlobalArea[a]["FluidType"],2)
- local FluidName2 = FluidName
- if FluidName2 == "None" then
- FluidName2 = "Fluid"
- end
- local SurfaceName = string.sub(global.WaterGlobalArea[a]["Surface"].name,1,1):upper()..string.sub(global.WaterGlobalArea[a]["Surface"].name,2)
- 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"]))
- end
- end
- end
- end
- function PumpJacks()
- game.print("Pumpjack(s) Location Info")
- if #global.FluidProducers == 0 then
- game.print("No Pumpjacks to show.")
- else
- for a = 1, #global.FluidProducers, 1 do
- local SurfaceName = string.sub(global.FluidProducers[a]["surface"].name,1,1):upper()..string.sub(global.FluidProducers[a]["surface"].name,2)
- game.print(string.format("Pumpjack Position: {%s, %s}, Pumpjack Surface: %s",global.FluidProducers[a]["position"]["x"],global.FluidProducers[a]["position"]["y"],SurfaceName))
- end
- end
- end
- function ScenFunc()
- global.LandFill = {}
- global.InstallTick = 0
- end
- function WaaRSetup()
- Globals()
- LinkGlobals()
- end
- -- SCRIPT EVENTS --
- script.on_init(WaaRSetup)
- script.on_load(LinkGlobals)
- script.on_nth_tick(60,EverySec)
- script.on_event(defines.events.on_tick, EveryTick)
- script.on_event({defines.events.on_built_entity,defines.events.on_robot_built_entity}, BuiltOffShore)
- script.on_event({defines.events.on_player_mined_entity,defines.events.on_robot_mined_entity}, DestroyedOffShore)
- script.on_event({defines.events.on_player_built_tile,defines.events.on_robot_built_tile}, LandFillCheck)
- script.on_event({defines.events.on_game_created_from_scenario},ScenFunc)
- script.on_configuration_changed(UpdateMod)
- commands.add_command("RestoreWater", "Restores Water Areas & Clears GlobalTable",RestoreWater)
- commands.add_command("Offshores", "Displays All The Offshore Pumps Built", Offshores)
- commands.add_command("OffshoreDrains", "Displays All The Offshore Drains Built", OffshoreDrains)
- commands.add_command("WaterAreas", "Displays All The WaterAreas Built", WaterAreas)
- commands.add_command("Pumpjacks", "Displays All The Pumpjacks Built", PumpJacks)
- commands.add_command("RemoveAllDrainedArea", "Remove all drained area", RemoveAllDrainedArea)
Add Comment
Please, Sign In to add comment