From 88af5e2fe557fc5eedc985209061b3c37f9ba3a2 Mon Sep 17 00:00:00 2001 From: Valansch Date: Sun, 3 Jun 2018 15:47:12 +0200 Subject: [PATCH 01/17] wip version of antigrief/undo --- antigrief.lua | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ control.lua | 2 +- 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 antigrief.lua diff --git a/antigrief.lua b/antigrief.lua new file mode 100644 index 00000000..bcfc7ea1 --- /dev/null +++ b/antigrief.lua @@ -0,0 +1,76 @@ +--local Event = require "utils.event" +local surface = {} + +Event.on_init(function() + 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}) + surface.always_day = true +end) + +local function place_entity_on_surface(entity, surface, replace, player) + if entity.surface == surface then return end + if replace then + for _,e in pairs(surface.find_entities_filtered{position = entity.position}) do + e.destroy() + end + local 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 + local 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 + end + end +end + +Event.add(defines.events.on_chunk_generated, function(event) + if event.surface.name == "antigrief" then + for _,e in pairs(event.surface.find_entities_filtered{area = event.area, force = "neutral"}) do + if e.type ~= "player" then + e.destroy() + end + end + local tiles = {} + for x = event.area.left_top.x, event.area.right_bottom.x do + for y = event.area.left_top.y, event.area.right_bottom.y do + table.insert(tiles,{name="lab-dark-2", position = {x,y}}) + end + end + event.surface.set_tiles(tiles) + end +end) + +--on_entity_settings_pasted + + + +Event.add(defines.events.on_robot_pre_mined, function(event) + if event.entity.force.name == "player" and event.entity.last_user then + place_entity_on_surface(event.entity, true, surface, event.entity.last_user) + end +end) + +Event.add(defines.events.on_player_mined_entity, function(event) + place_entity_on_surface(event.entity, surface, true, event.player_index) +end) + +local Module = {} + +Module.undo = function(player) + if type(player) == "nil" or type(player) == "string" then return end --No support for strings! + if type(player) == "number" then + local player = game.players[player] + end + for _,e in pairs(surface.find_entities_filtered{}) do + if e.last_user == player then + place_entity_on_surface(e, game.surfaces.nauvis, false) + end + end +end + +Foo = Module + +return Module diff --git a/control.lua b/control.lua index ab7e2fb0..4361dc5c 100644 --- a/control.lua +++ b/control.lua @@ -14,7 +14,7 @@ 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' From daed9da30b0f40231d16d56339b900553f7eb9c3 Mon Sep 17 00:00:00 2001 From: Valansch Date: Mon, 4 Jun 2018 18:40:06 +0200 Subject: [PATCH 02/17] fixed undo desync --- antigrief.lua | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/antigrief.lua b/antigrief.lua index bcfc7ea1..1f5de653 100644 --- a/antigrief.lua +++ b/antigrief.lua @@ -1,13 +1,13 @@ ---local Event = require "utils.event" -local surface = {} +local Event = require "utils.event" + Event.on_init(function() - 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}) - surface.always_day = true + 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) - if entity.surface == surface then return end if replace then for _,e in pairs(surface.find_entities_filtered{position = entity.position}) do e.destroy() @@ -49,24 +49,24 @@ end) Event.add(defines.events.on_robot_pre_mined, function(event) if event.entity.force.name == "player" and event.entity.last_user then - place_entity_on_surface(event.entity, true, surface, event.entity.last_user) + place_entity_on_surface(event.entity, true, global.ag_surface, event.entity.last_user) end end) Event.add(defines.events.on_player_mined_entity, function(event) - place_entity_on_surface(event.entity, surface, true, event.player_index) + place_entity_on_surface(event.entity, global.ag_surface, true, event.player_index) end) local Module = {} Module.undo = function(player) if type(player) == "nil" or type(player) == "string" then return end --No support for strings! - if type(player) == "number" then - local player = game.players[player] - end - for _,e in pairs(surface.find_entities_filtered{}) do - if e.last_user == player then + 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() + end end end end From ab18b08c64957e0eb0c0e411df0992b2e775fa7d Mon Sep 17 00:00:00 2001 From: Valansch Date: Mon, 4 Jun 2018 22:24:18 +0200 Subject: [PATCH 03/17] implemented antigrief: on_entity_died support --- antigrief.lua | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/antigrief.lua b/antigrief.lua index 1f5de653..fab1f48e 100644 --- a/antigrief.lua +++ b/antigrief.lua @@ -4,26 +4,28 @@ 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.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 e.destroy() end - local new_entity = surface.create_entity{name = entity.name, position = entity.position, force = entity.force, direction = entity.direction} + 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 - local new_entity = surface.create_entity{name = entity.name, position = entity.position, force = entity.force, direction = entity.direction} - if player then + 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 - end + end end + return new_entity end Event.add(defines.events.on_chunk_generated, function(event) @@ -43,16 +45,27 @@ Event.add(defines.events.on_chunk_generated, function(event) end end) ---on_entity_settings_pasted - - - -Event.add(defines.events.on_robot_pre_mined, function(event) +Event.add(defines.events.on_robot_pre_mined, function(event) if event.entity.force.name == "player" and event.entity.last_user then - place_entity_on_surface(event.entity, true, global.ag_surface, event.entity.last_user) + place_entity_on_surface(event.entity, global.ag_surface, true, event.entity.last_user) end end) +Event.add(defines.events.on_entity_died, function(event) + --is a player on the same force as the destroyed object + if event.entity and event.entity.force.name == "player" and event.cause and + event.cause.force == event.entity.force and event.cause.type == "player" then + local new_entity = place_entity_on_surface(event.entity, global.ag_surface, true, event.cause.player) + if new_entity and event.entity.type == "container" then + local items = event.entity.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) Event.add(defines.events.on_player_mined_entity, function(event) place_entity_on_surface(event.entity, global.ag_surface, true, event.player_index) end) @@ -63,7 +76,7 @@ Module.undo = function(player) if type(player) == "nil" or type(player) == "string" then return end --No support for strings! 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) + 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() end @@ -71,6 +84,4 @@ Module.undo = function(player) end end -Foo = Module - return Module From ee70db72f679fd7b3b8fcf995ff2d56bc2e9c813 Mon Sep 17 00:00:00 2001 From: Valansch Date: Mon, 4 Jun 2018 23:26:52 +0200 Subject: [PATCH 04/17] 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) From 99c319a4a271c22c585587c011a26ffa1ecf8c01 Mon Sep 17 00:00:00 2001 From: JCA122204 <34488805+JCA122204@users.noreply.github.com> Date: Mon, 4 Jun 2018 17:58:47 -0400 Subject: [PATCH 05/17] Removed unessesary lines and variables --- train_station_names.lua | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/train_station_names.lua b/train_station_names.lua index 173d5fb6..f354bad5 100644 --- a/train_station_names.lua +++ b/train_station_names.lua @@ -2,15 +2,12 @@ local Event = require 'utils.event' local function player_built_entity(event) local entity = event.created_entity - if not entity or not entity.valid then - return - end + if not entity or not entity.valid then return end if entity.name == 'train-stop' then local y = math.random(1, 3) if y ~= 1 then - local total_players = #game.players - local x = math.random(1, total_players) + local x = math.random(1, #game.players) local player = game.players[x] event.created_entity.backer_name = player.name end From ba5b28657e0a86453737d739fc6c1fda0f5f7a28 Mon Sep 17 00:00:00 2001 From: JCA122204 <34488805+JCA122204@users.noreply.github.com> Date: Mon, 4 Jun 2018 18:35:17 -0400 Subject: [PATCH 06/17] Remove unessecary lines --- tasklist.lua | 48 +++++++++++------------------------------------- 1 file changed, 11 insertions(+), 37 deletions(-) diff --git a/tasklist.lua b/tasklist.lua index c675ed75..45c8ad4b 100644 --- a/tasklist.lua +++ b/tasklist.lua @@ -87,14 +87,11 @@ local function tasklist(player) global.tasklist_items[3] = frame.textfield_task_3.text global.tasklist_items[4] = frame.textfield_task_4.text global.tasklist_items[5] = frame.textfield_task_5.text - if (global.tasklist_items[5] .. global.tasklist_items[4] .. global.tasklist_items[3] .. global.tasklist_items[2] .. global.tasklist_items[1] == "") then - return - end + if (global.tasklist_items[5] .. global.tasklist_items[4] .. global.tasklist_items[3] .. global.tasklist_items[2] .. global.tasklist_items[1] == "") then return end global.tasklist_author = player.name - local msg = player.name - msg = msg .. " has created an updated tasklist!" + local msg = player.name .. " has created an updated tasklist!" local frame = player.gui.left["tasklist-assembler"] frame.destroy() @@ -103,14 +100,9 @@ local function tasklist(player) local frame = player.gui.left["tasklist-panel"] - if (frame) then - frame.destroy() - end - - if (global.autoshow_tasklist_for_player[player.name] == true) then - tasklist_show(player) - end + if (frame) then frame.destroy() end + if (global.autoshow_tasklist_for_player[player.name] == true) then tasklist_show(player) end end game.print(msg) @@ -161,45 +153,27 @@ local function on_gui_click(event) if (name == "tasklist") then local frame = player.gui.left["tasklist-panel"] - if (frame) then - frame.destroy() - else - tasklist_show(player) - end + if (frame) then frame.destroy() else tasklist_show(player) end local frame = player.gui.left["tasklist-assembler"] - if (frame) then - frame.destroy() - end + if (frame) then frame.destroy() end end if (name == "new_tasklist_assembler_button") then local frame = player.gui.left["tasklist-assembler"] - if (frame) then - frame.destroy() - else - tasklist_assembler(player) - end + if (frame) then frame.destroy() else tasklist_assembler(player) end end - if (name == "create_new_tasklist_button") then - tasklist(player) - end + if (name == "create_new_tasklist_button") then tasklist(player) end if (name == "tasklist_hide_button") then local frame = player.gui.left["tasklist-panel"] - if (frame) then - frame.destroy() - end + if (frame) then frame.destroy() end local frame = player.gui.left["tasklist-assembler"] - if (frame) then - frame.destroy() - end + if (frame) then frame.destroy() end end - if (name == "auto_show_tasklist_checkbox") then - global.autoshow_tasklist_for_player[player.name] = event.element.state - end + if (name == "auto_show_tasklist_checkbox") then global.autoshow_tasklist_for_player[player.name] = event.element.state end end Event.add(defines.events.on_gui_click, on_gui_click) From 23fbc50801783671339f28668479ef35718202d1 Mon Sep 17 00:00:00 2001 From: JCA122204 <34488805+JCA122204@users.noreply.github.com> Date: Mon, 4 Jun 2018 18:40:17 -0400 Subject: [PATCH 07/17] Remove unessecary lines --- spawn_control.lua | 36 +++++++++--------------------------- 1 file changed, 9 insertions(+), 27 deletions(-) diff --git a/spawn_control.lua b/spawn_control.lua index b7694c8b..10dcda6f 100644 --- a/spawn_control.lua +++ b/spawn_control.lua @@ -51,9 +51,7 @@ local function player_joined_game(event) spawn_name = get_min_count_spawn_name() - if not spawn_name then - return - end + if not spawn_name then return end local spawn = global.spawns[spawn_name] global.player_spawns[index] = spawn_name @@ -68,9 +66,7 @@ local function player_left_game(event) local spawn_name = global.player_spawns[index] local spawn = global.spawns[spawn_name] - if not spawn then - return - end + if not spawn then return end local count = spawn.count spawn.count = count - 1 @@ -81,9 +77,7 @@ local function player_respawned(event) local spawn_name = global.player_spawns[index] local spawn = global.spawns[spawn_name] - if not spawn then - return - end + if not spawn then return end game.players[index].teleport(spawn) end @@ -142,9 +136,7 @@ end local function print_spawns() local str = "" - for name, spawn in pairs(global.spawns) do - str = str .. name .. ": (" .. spawn.x .. ", " .. spawn.y .. "), player count = " .. spawn.count .. "\n\r" - end + for name, spawn in pairs(global.spawns) do str = str .. name .. ": (" .. spawn.x .. ", " .. spawn.y .. "), player count = " .. spawn.count .. "\n\r" end game.player.print(str) end @@ -166,9 +158,7 @@ local function print_players_for_spawn(target_spawn_name) end end - if str == "" then - str = "no players" - end + if str == "" then str = "no players" end game.player.print(str) end @@ -189,11 +179,7 @@ local function tp_spawn_command(cmd) table.insert( ps,p ) end - if #ps == 1 then - tp_spawn(game.player.name, ps[1]) - else - tp_spawn(ps[1], ps[2]) - end + if #ps == 1 then tp_spawn(game.player.name, ps[1]) else tp_spawn(ps[1], ps[2]) end end function change_spawn_command(cmd) @@ -209,9 +195,7 @@ function change_spawn_command(cmd) end local ps ={} - for p in params:gmatch("%S+") do - table.insert( ps,p ) - end + for p in params:gmatch("%S+") do table.insert( ps,p ) end change_spawn(ps[1], ps[2]) end @@ -238,9 +222,7 @@ local function print_players_for_spawn_command(cmd) end local ps ={} - for p in params:gmatch("%S+") do - table.insert( ps,p ) - end + for p in params:gmatch("%S+") do table.insert( ps,p ) end print_players_for_spawn(ps[1]) end @@ -252,4 +234,4 @@ Event.add(defines.events.on_player_respawned, player_respawned) commands.add_command("tpspawn", " teleports a player to the spawn point (Admins only)", tp_spawn_command) commands.add_command("changespawn", " changes the spawn point for a player (Admins only)", change_spawn_command) commands.add_command("printspawns", "prints info on all spawn points (Admins only)", print_spawns_command) -commands.add_command("printplayersforspawn", " prints all the connected players for a spawn (Admins only)", print_players_for_spawn_command) \ No newline at end of file +commands.add_command("printplayersforspawn", " prints all the connected players for a spawn (Admins only)", print_players_for_spawn_command) From e6a2ef869778e6ceed3be06149806a6f9e2496ee Mon Sep 17 00:00:00 2001 From: Valansch Date: Tue, 5 Jun 2018 01:34:58 +0200 Subject: [PATCH 08/17] implemented Utils.find_entities_by_last_user and undo warning. --- antigrief.lua | 28 +++++++++++++++++----------- custom_commands.lua | 14 ++++++++++++-- utils/utils.lua | 23 +++++++++++++++++++++++ 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/antigrief.lua b/antigrief.lua index 9ab76e16..17943099 100644 --- a/antigrief.lua +++ b/antigrief.lua @@ -1,5 +1,5 @@ local Event = require "utils.event" - +local Utils = require "utils.utils" 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}) @@ -7,7 +7,6 @@ Event.on_init(function() end) - local function place_entity_on_surface(entity, surface, replace, player) local new_entity = nil for _,e in pairs(surface.find_entities_filtered{position = entity.position}) do @@ -72,6 +71,18 @@ Module.undo = function(player) 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 + + --Remove all items from all surfaces that player placed an entity + for _,surface in pairs(game.surfaces) do + if surface ~= global.ag_surface then + for _,e in pairs(surface.find_entities_filtered{force = player.force.name}) do + if e.last_user == player then + e.destroy() + end + end + end + end + for _,e in pairs(global.ag_surface.find_entities_filtered{}) do if e.last_user == player then --Place removed entity IF no collision is detected @@ -87,15 +98,6 @@ Module.undo = function(player) 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() @@ -108,4 +110,8 @@ Module.antigrief_surface_tp = function() end end +Module.count_removed_entities = function(player) + return #Utils.find_entities_by_last_user(player, global.ag_surface) +end + return Module diff --git a/custom_commands.lua b/custom_commands.lua index 20739d5d..c3cb8e47 100644 --- a/custom_commands.lua +++ b/custom_commands.lua @@ -473,13 +473,23 @@ local function pool() end end +global.undo_warned_players = {} 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 + if not global.undo_warned_players[game.player.index] then + global.undo_warned_players[game.player.index] = true + game.player.print( + string.format("Warning! You are about to remove %s entities and restore %s entities.", + #Utils.find_entities_by_last_user(game.players[cmd.parameter], game.surfaces.nauvis), + Antigrief.count_removed_entities(game.players[cmd.parameter])) + ) + game.player.print("To execute the command please run it again.") + return + end Antigrief.undo(game.players[cmd.parameter]) game.print(string.format("Undoing everything %s did...", cmd.parameter)) else @@ -532,4 +542,4 @@ commands.add_command('all-tech', 'researches all technologies', function() if ga 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) +commands.add_command('antigrief_surface', 'moves you to the antigrief surface or back (Admins only)', antigrief_surface_tp) diff --git a/utils/utils.lua b/utils/utils.lua index ce5c7a10..44e681c4 100644 --- a/utils/utils.lua +++ b/utils/utils.lua @@ -37,4 +37,27 @@ Module.cast_bool = function(var) if var then return true else return false end end +Module.find_entities_by_last_user = function(player, surface, filters) + if type(player) == "string" or not player then + error("bad argument #1 to '" .. debug.getinfo(1, "n").name .. "' (number or LuaPlayer expected, got ".. type(player) .. ")", 1) + return + end + if type(surface) ~= "table" and type(surface) ~= "number" then + error("bad argument #2 to '" .. debug.getinfo(1, "n").name .. "' (number or LuaSurface expected, got ".. type(surface) .. ")", 1) + return + end + local entities = {} + local surface = surface + local player = player + local filters = filters or {} + if type(surface) == "number" then surface = game.surfaces[surface] end + if type(player) == "number" then player = game.players[player] end + filters.force = player.force.name + for _,e in pairs(surface.find_entities_filtered(filters)) do + if e.last_user == player then + table.insert(entities, e) + end + end + return entities + end return Module From 8905388565dde876e205109ea109d46c8630c0f7 Mon Sep 17 00:00:00 2001 From: Valansch Date: Tue, 5 Jun 2018 02:06:26 +0200 Subject: [PATCH 09/17] Fixed potential issue with reactors blowing up on antigrief surface --- reactor_meltdown.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reactor_meltdown.lua b/reactor_meltdown.lua index acc1b3e4..51b84c91 100644 --- a/reactor_meltdown.lua +++ b/reactor_meltdown.lua @@ -145,7 +145,7 @@ local function entity_build(event) if not event.created_entity.valid then return end - if event.created_entity.name == 'nuclear-reactor' then + if event.created_entity.name == 'nuclear-reactor' and event.created_entity.surface.name ~= "antigrief" then table.insert(global.reactors, event.created_entity) end end From 0bd88cc924d8f2fbf29b2c4bb7b08688d8448dc3 Mon Sep 17 00:00:00 2001 From: Valansch Date: Tue, 5 Jun 2018 11:25:00 +0200 Subject: [PATCH 10/17] Fixed bug in print_spawns and minor refactoring --- spawn_control.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spawn_control.lua b/spawn_control.lua index 10dcda6f..96c4e609 100644 --- a/spawn_control.lua +++ b/spawn_control.lua @@ -136,9 +136,9 @@ end local function print_spawns() local str = "" - for name, spawn in pairs(global.spawns) do str = str .. name .. ": (" .. spawn.x .. ", " .. spawn.y .. "), player count = " .. spawn.count .. "\n\r" end - - game.player.print(str) + for name, spawn in pairs(global.spawns) do + game.player.print(string.format("%s: (%d, %d), player count = %d", name, spawn.x, spawn.y, spawn.count)) + end end local function print_players_for_spawn(target_spawn_name) From 2d438a2629bdc2453772a13b60835abc3dda1c55 Mon Sep 17 00:00:00 2001 From: Valansch Date: Tue, 5 Jun 2018 18:00:25 +0200 Subject: [PATCH 11/17] Implemented undo rotation, paste, decon --- antigrief.lua | 75 ++++++++++++++++++++++++++++++++++++++++++------- utils/utils.lua | 6 +++- 2 files changed, 70 insertions(+), 11 deletions(-) diff --git a/antigrief.lua b/antigrief.lua index 17943099..dfcd3035 100644 --- a/antigrief.lua +++ b/antigrief.lua @@ -1,12 +1,18 @@ local Event = require "utils.event" local Utils = require "utils.utils" +global.original_last_users_by_ent_pos = {} + 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.always_day = true end) +local function is_mocked(entity) + return pcall(function() return entity.mock end) +end + local function place_entity_on_surface(entity, surface, replace, player) local new_entity = nil for _,e in pairs(surface.find_entities_filtered{position = entity.position}) do @@ -14,10 +20,16 @@ local function place_entity_on_surface(entity, surface, replace, player) e.destroy() end end - if (replace or surface.count_entities_filtered{position = entity.position} == 0) then + local entities_to_be_replaced = surface.find_entities_filtered{position = entity.position} + if (replace or #entities_to_be_replaced == 0 or entities_to_be_replaced[1].type == entity.type) then new_entity = surface.create_entity{name = entity.name, position = entity.position, force = entity.force, direction = entity.direction} - if player and new_entity then - new_entity.last_user = player + if new_entity then + if not is_mocked(entity) then + new_entity.copy_settings(entity) + end + if player then + new_entity.last_user = player + end end end return new_entity @@ -41,11 +53,46 @@ Event.add(defines.events.on_chunk_generated, function(event) end) Event.add(defines.events.on_robot_pre_mined, function(event) - if event.entity.force.name == "player" and event.entity.last_user then - place_entity_on_surface(event.entity, global.ag_surface, true, event.entity.last_user) - end + --The bot isnt the culprit! The last user is! They marked it for deconstruction! + event.player_index = Utils.ternary(event.last_user, entity.last_user.index) + on_entity_changed(event) end) +local function get_position_str(pos) + return string.format("%d|%d", pos.x, pos.y) +end + +local function on_entity_changed(event) + local entity = event.entity or event.destination + local player = game.players[event.player_index] + if player.admin then x=2 end --Freebees for admins + if true or entity.last_user ~= player and entity.force == player.force then --commented out to be able to debug + place_entity_on_surface(entity, global.ag_surface, true, event.player_index) + end + if entity.last_user then + global.original_last_users_by_ent_pos[get_position_str(entity.position)] = entity.last_user.index + end +end + +local function get_pre_rotate_direction(entity) + --Some entities have 8 rotation steps and some have 4. So a mathmatical reverse is not possible + entity.rotate{reverse=true} + local direction = entity.direction + entity.rotate() + return direction +end + +Event.add(defines.events.on_player_rotated_entity, function(event) + local entity = event.entity + --Mock entity us used because the api doesnt support pre_player_rotated entity. + --The mocked entity has the entity state before rotation + local mock_entity = {name = entity.name, position = entity.position, mock = true, + last_user = entity.last_user, force = entity.force, direction = get_pre_rotate_direction(entity)} + event.entity = mock_entity + on_entity_changed(event) +end) +Event.add(defines.events.on_pre_entity_settings_pasted, on_entity_changed) + Event.add(defines.events.on_entity_died, function(event) --is a player on the same force as the destroyed object if event.entity and event.entity.force.name == "player" and event.cause and @@ -61,10 +108,15 @@ Event.add(defines.events.on_entity_died, function(event) end end end) -Event.add(defines.events.on_player_mined_entity, function(event) - place_entity_on_surface(event.entity, global.ag_surface, true, event.player_index) + +Event.add(defines.events.on_player_mined_entity, on_entity_changed) + +Event.add(defines.events.on_marked_for_deconstruction, function(event) + global.original_last_users_by_ent_pos[get_position_str(event.entity.position)] = + event.entity.last_user.index end) + local Module = {} Module.undo = function(player) @@ -72,7 +124,7 @@ Module.undo = function(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 - --Remove all items from all surfaces that player placed an entity + --Remove all items from all surfaces that player placed an entity on for _,surface in pairs(game.surfaces) do if surface ~= global.ag_surface then for _,e in pairs(surface.find_entities_filtered{force = player.force.name}) do @@ -86,7 +138,8 @@ Module.undo = function(player) for _,e in pairs(global.ag_surface.find_entities_filtered{}) do 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) + local last_user = global.original_last_users_by_ent_pos[get_position_str(e.position)] + local new_entity = place_entity_on_surface(e, game.surfaces.nauvis, false, last_user) --Transfere items if new_entity and e.type == "container" then local items = e.get_inventory(defines.inventory.chest).get_contents() @@ -115,3 +168,5 @@ Module.count_removed_entities = function(player) end return Module + +--TODO: Remove items from antigrief surface, override orientation, cause build event diff --git a/utils/utils.lua b/utils/utils.lua index 44e681c4..635e1e9d 100644 --- a/utils/utils.lua +++ b/utils/utils.lua @@ -59,5 +59,9 @@ Module.find_entities_by_last_user = function(player, surface, filters) end end return entities - end +end + +Module.ternary = function(c, t, f) + if c then return t else return f end +end return Module From 210d354368ec47629ed502f5dd4d3feea32e3e62 Mon Sep 17 00:00:00 2001 From: Valansch Date: Tue, 5 Jun 2018 19:02:01 +0200 Subject: [PATCH 12/17] Fixed undo robot bug and that a griefers username would be on something he rotated once --- antigrief.lua | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/antigrief.lua b/antigrief.lua index dfcd3035..129e2de1 100644 --- a/antigrief.lua +++ b/antigrief.lua @@ -52,12 +52,6 @@ Event.add(defines.events.on_chunk_generated, function(event) end end) -Event.add(defines.events.on_robot_pre_mined, function(event) - --The bot isnt the culprit! The last user is! They marked it for deconstruction! - event.player_index = Utils.ternary(event.last_user, entity.last_user.index) - on_entity_changed(event) -end) - local function get_position_str(pos) return string.format("%d|%d", pos.x, pos.y) end @@ -65,8 +59,8 @@ end local function on_entity_changed(event) local entity = event.entity or event.destination local player = game.players[event.player_index] - if player.admin then x=2 end --Freebees for admins - if true or entity.last_user ~= player and entity.force == player.force then --commented out to be able to debug + if player.admin then return end --Freebees for admins + if entity.last_user ~= player and entity.force == player.force then --commented out to be able to debug place_entity_on_surface(entity, global.ag_surface, true, event.player_index) end if entity.last_user then @@ -74,6 +68,12 @@ local function on_entity_changed(event) end end +Event.add(defines.events.on_robot_pre_mined, function(event) + --The bot isnt the culprit! The last user is! They marked it for deconstruction! + event.player_index = Utils.ternary(event.entity.last_user, event.entity.last_user.index) + on_entity_changed(event) +end) + local function get_pre_rotate_direction(entity) --Some entities have 8 rotation steps and some have 4. So a mathmatical reverse is not possible entity.rotate{reverse=true} @@ -86,8 +86,9 @@ Event.add(defines.events.on_player_rotated_entity, function(event) local entity = event.entity --Mock entity us used because the api doesnt support pre_player_rotated entity. --The mocked entity has the entity state before rotation + --We also dont know who rotated it and dont want the griefers name there so we set it to 1 local mock_entity = {name = entity.name, position = entity.position, mock = true, - last_user = entity.last_user, force = entity.force, direction = get_pre_rotate_direction(entity)} + last_user = game.players[1], force = entity.force, direction = get_pre_rotate_direction(entity)} event.entity = mock_entity on_entity_changed(event) end) @@ -141,13 +142,21 @@ Module.undo = function(player) local last_user = global.original_last_users_by_ent_pos[get_position_str(e.position)] local new_entity = place_entity_on_surface(e, game.surfaces.nauvis, false, last_user) --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} + if new_entity then + + local player = Utils.ternary(new_entity.last_user, new_entity.last_user, game.player) + local event = {created_entity = new_entity, player_index = player.index, stack = {}} + script.raise_event(defines.events.on_built_entity, event) + + if 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 + e.destroy() --destory entity only if a new entity was created end end end @@ -168,5 +177,3 @@ Module.count_removed_entities = function(player) end return Module - ---TODO: Remove items from antigrief surface, override orientation, cause build event From 8c64c02aae5ef9d45e054535a8f4290cb8fa2f1f Mon Sep 17 00:00:00 2001 From: Valansch Date: Tue, 5 Jun 2018 19:18:50 +0200 Subject: [PATCH 13/17] undo: added double rotation support --- antigrief.lua | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/antigrief.lua b/antigrief.lua index 129e2de1..b418e4fc 100644 --- a/antigrief.lua +++ b/antigrief.lua @@ -20,7 +20,7 @@ local function place_entity_on_surface(entity, surface, replace, player) e.destroy() end end - local entities_to_be_replaced = surface.find_entities_filtered{position = entity.position} + local entities_to_be_replaced = surface.find_entities_filtered{position = entity.position} if (replace or #entities_to_be_replaced == 0 or entities_to_be_replaced[1].type == entity.type) then new_entity = surface.create_entity{name = entity.name, position = entity.position, force = entity.force, direction = entity.direction} if new_entity then @@ -84,13 +84,19 @@ end Event.add(defines.events.on_player_rotated_entity, function(event) local entity = event.entity - --Mock entity us used because the api doesnt support pre_player_rotated entity. - --The mocked entity has the entity state before rotation - --We also dont know who rotated it and dont want the griefers name there so we set it to 1 - local mock_entity = {name = entity.name, position = entity.position, mock = true, - last_user = game.players[1], force = entity.force, direction = get_pre_rotate_direction(entity)} - event.entity = mock_entity - on_entity_changed(event) + + local ag_entities = global.ag_surface.find_entities_filtered{position = entity.position} + --If a player has rotated twice we want to preserve the original state. + if #ag_entities == 0 or not ag_entities[1].last_user or ag_entities[1].last_user ~= entity.last_user then + + --Mock entity us used because the api doesnt support pre_player_rotated entity. + --The mocked entity has the entity state before rotation + --We also dont know who rotated it and dont want the griefers name there so we set it to 1 + local mock_entity = {name = entity.name, position = entity.position, mock = true, + last_user = game.players[1], force = entity.force, direction = get_pre_rotate_direction(entity)} + event.entity = mock_entity + on_entity_changed(event) + end end) Event.add(defines.events.on_pre_entity_settings_pasted, on_entity_changed) @@ -146,7 +152,7 @@ Module.undo = function(player) local player = Utils.ternary(new_entity.last_user, new_entity.last_user, game.player) local event = {created_entity = new_entity, player_index = player.index, stack = {}} - script.raise_event(defines.events.on_built_entity, event) + --script.raise_event(defines.events.on_built_entity, event) if e.type == "container" then local items = e.get_inventory(defines.inventory.chest).get_contents() From fc9f56d2926896968121c6e94024f88ba194e115 Mon Sep 17 00:00:00 2001 From: Valansch Date: Tue, 5 Jun 2018 19:22:43 +0200 Subject: [PATCH 14/17] Commented script.raise_event back in --- antigrief.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/antigrief.lua b/antigrief.lua index b418e4fc..901c0c0a 100644 --- a/antigrief.lua +++ b/antigrief.lua @@ -152,7 +152,7 @@ Module.undo = function(player) local player = Utils.ternary(new_entity.last_user, new_entity.last_user, game.player) local event = {created_entity = new_entity, player_index = player.index, stack = {}} - --script.raise_event(defines.events.on_built_entity, event) + script.raise_event(defines.events.on_built_entity, event) if e.type == "container" then local items = e.get_inventory(defines.inventory.chest).get_contents() From 398b3e42f41ca58533a1f67a0b1aab84fde67935 Mon Sep 17 00:00:00 2001 From: Maik Wild Date: Wed, 6 Jun 2018 15:32:20 +0200 Subject: [PATCH 15/17] Fixed grilleds suggestions --- antigrief.lua | 38 ++++++++++++++++----------------- map_gen/combined/dimensions.lua | 4 ++-- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/antigrief.lua b/antigrief.lua index 901c0c0a..a8e2c943 100644 --- a/antigrief.lua +++ b/antigrief.lua @@ -4,7 +4,7 @@ local Utils = require "utils.utils" global.original_last_users_by_ent_pos = {} 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="none"},["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="normal",width=2000000}) global.ag_surface.always_day = true end) @@ -37,14 +37,9 @@ end Event.add(defines.events.on_chunk_generated, function(event) if event.surface.name == "antigrief" then - for _,e in pairs(event.surface.find_entities_filtered{area = event.area, force = "neutral"}) do - if e.type ~= "player" then - e.destroy() - end - end local tiles = {} - for x = event.area.left_top.x, event.area.right_bottom.x do - for y = event.area.left_top.y, event.area.right_bottom.y do + for x = event.area.left_top.x, event.area.right_bottom.x - 1 do + for y = event.area.left_top.y, event.area.right_bottom.y - 1 do table.insert(tiles,{name="lab-dark-2", position = {x,y}}) end end @@ -70,8 +65,10 @@ end Event.add(defines.events.on_robot_pre_mined, function(event) --The bot isnt the culprit! The last user is! They marked it for deconstruction! - event.player_index = Utils.ternary(event.entity.last_user, event.entity.last_user.index) - on_entity_changed(event) + if event.entity.valid and event.entity.last_user then + event.player_index = event.entity.last_user.index + on_entity_changed(event) + end end) local function get_pre_rotate_direction(entity) @@ -85,6 +82,8 @@ end Event.add(defines.events.on_player_rotated_entity, function(event) local entity = event.entity + if not entity.valid then return end + local ag_entities = global.ag_surface.find_entities_filtered{position = entity.position} --If a player has rotated twice we want to preserve the original state. if #ag_entities == 0 or not ag_entities[1].last_user or ag_entities[1].last_user ~= entity.last_user then @@ -102,17 +101,17 @@ Event.add(defines.events.on_pre_entity_settings_pasted, on_entity_changed) Event.add(defines.events.on_entity_died, function(event) --is a player on the same force as the destroyed object - if event.entity and event.entity.force.name == "player" and event.cause and + if event.entity and event.entity.valid and event.entity.force.name == "player" and event.cause and event.cause.force == event.entity.force and event.cause.type == "player" then - local new_entity = place_entity_on_surface(event.entity, global.ag_surface, true, event.cause.player) - if new_entity and event.entity.type == "container" then - local items = event.entity.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 + local new_entity = place_entity_on_surface(event.entity, global.ag_surface, true, event.cause.player) + if new_entity and event.entity.type == "container" then + local items = event.entity.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) @@ -120,14 +119,13 @@ Event.add(defines.events.on_player_mined_entity, on_entity_changed) Event.add(defines.events.on_marked_for_deconstruction, function(event) global.original_last_users_by_ent_pos[get_position_str(event.entity.position)] = - event.entity.last_user.index + Utils.ternary(event.entity.last_user, event.entity.last_user.index) end) local Module = {} Module.undo = function(player) - 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 diff --git a/map_gen/combined/dimensions.lua b/map_gen/combined/dimensions.lua index b2416ebf..5d870276 100644 --- a/map_gen/combined/dimensions.lua +++ b/map_gen/combined/dimensions.lua @@ -54,8 +54,8 @@ local function generate_nihil(event) end end local tiles = {} - for x = event.area.left_top.x, event.area.right_bottom.x do - for y = event.area.left_top.y, event.area.right_bottom.y do + for x = event.area.left_top.x, event.area.right_bottom.x -1 do + for y = event.area.left_top.y, event.area.right_bottom.y - 1 do table.insert(tiles,{name="lab-dark-1", position = {x,y}}) end end From e28920307def1b2e1ce41157adc1db3bf7e88959 Mon Sep 17 00:00:00 2001 From: Valansch Date: Wed, 6 Jun 2018 22:48:22 +0200 Subject: [PATCH 16/17] Minor changes to antigrief.lua --- antigrief.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/antigrief.lua b/antigrief.lua index a8e2c943..b2fbd055 100644 --- a/antigrief.lua +++ b/antigrief.lua @@ -10,12 +10,12 @@ Event.on_init(function() end) local function is_mocked(entity) - return pcall(function() return entity.mock end) + return rawget(entity, 'mock') end local function place_entity_on_surface(entity, surface, replace, player) local new_entity = nil - for _,e in pairs(surface.find_entities_filtered{position = entity.position}) do + for _,e in ipairs(surface.find_entities_filtered{position = entity.position}) do if replace or e.type == "entity-ghost" then e.destroy() end @@ -54,7 +54,7 @@ end local function on_entity_changed(event) local entity = event.entity or event.destination local player = game.players[event.player_index] - if player.admin then return end --Freebees for admins + if player.admin or not entity.valid then return end --Freebees for admins if entity.last_user ~= player and entity.force == player.force then --commented out to be able to debug place_entity_on_surface(entity, global.ag_surface, true, event.player_index) end @@ -132,7 +132,7 @@ Module.undo = function(player) --Remove all items from all surfaces that player placed an entity on for _,surface in pairs(game.surfaces) do if surface ~= global.ag_surface then - for _,e in pairs(surface.find_entities_filtered{force = player.force.name}) do + for _,e in ipairs(surface.find_entities_filtered{force = player.force.name}) do if e.last_user == player then e.destroy() end @@ -140,7 +140,7 @@ Module.undo = function(player) end end - for _,e in pairs(global.ag_surface.find_entities_filtered{}) do + for _,e in ipairs(global.ag_surface.find_entities_filtered{}) do if e.last_user == player then --Place removed entity IF no collision is detected local last_user = global.original_last_users_by_ent_pos[get_position_str(e.position)] From 51ec380330ce3e9dec72eda02f26add822f54ccb Mon Sep 17 00:00:00 2001 From: Valansch Date: Wed, 6 Jun 2018 22:53:30 +0200 Subject: [PATCH 17/17] Made undo warning appear more than once Undo will only not spell a warning if the undo was ran twice by the same user with the same user as target --- custom_commands.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/custom_commands.lua b/custom_commands.lua index c3cb8e47..c91ee43b 100644 --- a/custom_commands.lua +++ b/custom_commands.lua @@ -480,8 +480,8 @@ local function undo(cmd) return end if cmd.parameter and game.players[cmd.parameter] then - if not global.undo_warned_players[game.player.index] then - global.undo_warned_players[game.player.index] = true + if not global.undo_warned_players[game.player.index] or global.undo_warned_players[game.player.index] ~= game.players[cmd.parameter].index then + global.undo_warned_players[game.player.index] = game.players[cmd.parameter].index game.player.print( string.format("Warning! You are about to remove %s entities and restore %s entities.", #Utils.find_entities_by_last_user(game.players[cmd.parameter], game.surfaces.nauvis), @@ -492,6 +492,7 @@ local function undo(cmd) end Antigrief.undo(game.players[cmd.parameter]) game.print(string.format("Undoing everything %s did...", cmd.parameter)) + global.undo_warned_players[game.player.index] = nil else player_print("Usage: /undo ") end