From ee70db72f679fd7b3b8fcf995ff2d56bc2e9c813 Mon Sep 17 00:00:00 2001 From: Valansch Date: Mon, 4 Jun 2018 23:26:52 +0200 Subject: [PATCH] Implemented /undo and /antigrief_surface --- antigrief.lua | 58 ++++++++++++++++++++++++++++++++------------- control.lua | 1 - custom_commands.lua | 26 ++++++++++++++++++++ 3 files changed, 67 insertions(+), 18 deletions(-) diff --git a/antigrief.lua b/antigrief.lua index fab1f48e..9ab76e16 100644 --- a/antigrief.lua +++ b/antigrief.lua @@ -2,27 +2,23 @@ local Event = require "utils.event" Event.on_init(function() - global.ag_surface = game.create_surface("antigrief", { autoplace_controls = { coal = { frequency = "normal", richness = "normal", size = "none" }, ["copper-ore"] = { frequency = "normal", richness = "normal", size = "none" }, ["crude-oil"] = { frequency = "normal", richness = "normal", size = "none" }, desert = { frequency = "normal", richness = "normal", size = "none" }, dirt = { frequency = "normal", richness = "normal", size = "none" }, ["enemy-base"] = { frequency = "normal", richness = "normal", size = "none" }, grass = { frequency = "normal", richness = "normal", size = "very-high" }, ["iron-ore"] = { frequency = "normal", richness = "normal", size = "none" }, sand = { frequency = "normal", richness = "normal", size = "none" }, stone = { frequency = "normal", richness = "normal", size = "none" }, trees = { frequency = "normal", richness = "normal", size = "none" }, ["uranium-ore"] = { frequency = "normal", richness = "normal", size = "none" } }, cliff_settings = { cliff_elevation_0 = 1024, cliff_elevation_interval = 10, name = "cliff" }, height = 2000000, peaceful_mode = false, seed = 3461559752, starting_area = "very-low", starting_points = { { x = 0, y = 0 } }, terrain_segmentation = "normal", water = "none", width = 2000000}) + global.ag_surface=game.create_surface("antigrief",{autoplace_controls={coal={frequency="normal",richness="normal",size="none"},["copper-ore"]={frequency="normal",richness="normal",size="none"},["crude-oil"]={frequency="normal",richness="normal",size="none"},desert={frequency="normal",richness="normal",size="none"},dirt={frequency="normal",richness="normal",size="none"},["enemy-base"]={frequency="normal",richness="normal",size="none"},grass={frequency="normal",richness="normal",size="very-high"},["iron-ore"]={frequency="normal",richness="normal",size="none"},sand={frequency="normal",richness="normal",size="none"},stone={frequency="normal",richness="normal",size="none"},trees={frequency="normal",richness="normal",size="none"},["uranium-ore"]={frequency="normal",richness="normal",size="none"}},cliff_settings={cliff_elevation_0=1024,cliff_elevation_interval=10,name="cliff"},height=2000000,peaceful_mode=false,seed=3461559752,starting_area="very-low",starting_points={{x=0,y=0}},terrain_segmentation="normal",water="none",width=2000000}) global.ag_surface.always_day = true end) + local function place_entity_on_surface(entity, surface, replace, player) local new_entity = nil - if replace then - for _,e in pairs(surface.find_entities_filtered{position = entity.position}) do + for _,e in pairs(surface.find_entities_filtered{position = entity.position}) do + if replace or e.type == "entity-ghost" then e.destroy() end + end + if (replace or surface.count_entities_filtered{position = entity.position} == 0) then new_entity = surface.create_entity{name = entity.name, position = entity.position, force = entity.force, direction = entity.direction} - if player then - new_entity.last_user = player - end - else - if surface.count_entities_filtered{position = entity.position} == 0 then - new_entity = surface.create_entity{name = entity.name, position = entity.position, force = entity.force, direction = entity.direction} - if player then - new_entity.last_user = player - end + if player and new_entity then + new_entity.last_user = player end end return new_entity @@ -73,15 +69,43 @@ end) local Module = {} Module.undo = function(player) - if type(player) == "nil" or type(player) == "string" then return end --No support for strings! + local player = player + if type(player) == "nil" or type(player) == "string" then return --No support for strings! + elseif type(player) == "number" then player = game.players[player] end for _,e in pairs(global.ag_surface.find_entities_filtered{}) do - if e.last_user == player or e.last_user.index == player then - place_entity_on_surface(e, game.surfaces.nauvis, false) - for _,f in pairs(global.ag_surface.find_entities_filtered{position = e.position}) do - f.destroy() + if e.last_user == player then + --Place removed entity IF no collision is detected + local new_entity = place_entity_on_surface(e, game.surfaces.nauvis, false) + --Transfere items + if new_entity and e.type == "container" then + local items = e.get_inventory(defines.inventory.chest).get_contents() + if items then + for item, n in pairs(items) do + new_entity.insert{name = item, count = n} + end + end + end + end + end + + --Remove all items from all surfaces that player placed an entity + for _,surface in pairs(game.surfaces) do + for _,e in pairs(global.ag_surface.find_entities_filtered{force = player.force}) do + if e.last_user == player then + e.destroy() end end end end +Module.antigrief_surface_tp = function() + if game.player then + if game.player.surface == global.ag_surface then + game.player.teleport(game.player.position, game.surfaces.nauvis) + else + game.player.teleport(game.player.position, global.ag_surface) + end + end +end + return Module diff --git a/control.lua b/control.lua index 4361dc5c..326a947d 100644 --- a/control.lua +++ b/control.lua @@ -14,7 +14,6 @@ require 'fish_market' require 'reactor_meltdown' require 'map_layout' require 'bot' -require 'antigrief' -- GUIs the order determines the order they appear at the top. require 'info' require 'player_list' diff --git a/custom_commands.lua b/custom_commands.lua index cb43fa70..20739d5d 100644 --- a/custom_commands.lua +++ b/custom_commands.lua @@ -3,6 +3,7 @@ local Event = require 'utils.event' local Token = require 'utils.global_token' local UserGroups = require 'user_groups' local Utils = require 'utils.utils' +local Antigrief = require 'antigrief' function player_print(str) if game.player then @@ -472,6 +473,28 @@ local function pool() end end +local function undo(cmd) + if (not game.player) or not game.player.admin then + cant_run(cmd.name) + return + end + --warning + if cmd.parameter and game.players[cmd.parameter] then + Antigrief.undo(game.players[cmd.parameter]) + game.print(string.format("Undoing everything %s did...", cmd.parameter)) + else + player_print("Usage: /undo ") + end +end + +local function antigrief_surface_tp() + if (not game.player) or not game.player.admin then + cant_run(cmd.name) + return + end + Antigrief.antigrief_surface_tp() +end + if not _DEBUG then local old_add_command = commands.add_command commands.add_command = @@ -483,6 +506,7 @@ if not _DEBUG then local success, error = pcall(func, cmd) if not success then log(error) + player_print(error) end end ) @@ -507,3 +531,5 @@ commands.add_command('zoom', ' Sets your zoom.', zoom) commands.add_command('all-tech', 'researches all technologies', function() if game.player and game.player.admin then game.player.force.research_all_technologies() end end) commands.add_command('hax', 'Toggles your hax', function() if game.player and game.player.admin then game.player.cheat_mode = not game.player.cheat_mode end end) commands.add_command('pool', 'Spawns a pool', pool) +commands.add_command('undo', ' undoes everything a player has done (Admins only)', undo) +commands.add_command('antigrief_surface', 'move you to the antigrief surface or back', antigrief_surface_tp)