1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-12 10:04:40 +02:00

Merge branch 'develop' of https://github.com/Valansch/RedMew into develop

This commit is contained in:
grilledham 2018-06-07 12:20:21 +01:00
commit 033ada95b3
9 changed files with 272 additions and 73 deletions

183
antigrief.lua Normal file
View File

@ -0,0 +1,183 @@
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="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)
local function is_mocked(entity)
return rawget(entity, 'mock')
end
local function place_entity_on_surface(entity, surface, replace, player)
local new_entity = nil
for _,e in ipairs(surface.find_entities_filtered{position = entity.position}) do
if replace or e.type == "entity-ghost" then
e.destroy()
end
end
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
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
end
Event.add(defines.events.on_chunk_generated, function(event)
if event.surface.name == "antigrief" then
local tiles = {}
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
event.surface.set_tiles(tiles)
end
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 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
if entity.last_user then
global.original_last_users_by_ent_pos[get_position_str(entity.position)] = entity.last_user.index
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!
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)
--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
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
--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)
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.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
end
end
end
end)
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)] =
Utils.ternary(event.entity.last_user, event.entity.last_user.index)
end)
local Module = {}
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 on
for _,surface in pairs(game.surfaces) do
if surface ~= global.ag_surface then
for _,e in ipairs(surface.find_entities_filtered{force = player.force.name}) do
if e.last_user == player then
e.destroy()
end
end
end
end
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)]
local new_entity = place_entity_on_surface(e, game.surfaces.nauvis, false, last_user)
--Transfere items
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
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
Module.count_removed_entities = function(player)
return #Utils.find_entities_by_last_user(player, global.ag_surface)
end
return Module

View File

@ -15,7 +15,6 @@ require 'fish_market'
require 'reactor_meltdown'
require 'map_layout'
require 'bot'
-- GUIs the order determines the order they appear at the top.
require 'info'
require 'player_list'

View File

@ -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,39 @@ 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
if cmd.parameter and game.players[cmd.parameter] then
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),
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))
global.undo_warned_players[game.player.index] = nil
else
player_print("Usage: /undo <player>")
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 +517,7 @@ if not _DEBUG then
local success, error = pcall(func, cmd)
if not success then
log(error)
player_print(error)
end
end
)
@ -507,3 +542,5 @@ commands.add_command('zoom', '<number> 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', '<player> undoes everything a player has done (Admins only)', undo)
commands.add_command('antigrief_surface', 'moves you to the antigrief surface or back (Admins only)', antigrief_surface_tp)

View File

@ -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

View File

@ -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

View File

@ -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
@ -143,10 +137,8 @@ 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"
game.player.print(string.format("%s: (%d, %d), player count = %d", name, spawn.x, spawn.y, spawn.count))
end
game.player.print(str)
end
local function print_players_for_spawn(target_spawn_name)
@ -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", "<player> <spawn_name> teleports a player to the spawn point (Admins only)", tp_spawn_command)
commands.add_command("changespawn", "<player> <spawn_name> 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", "<spawn_name> prints all the connected players for a spawn (Admins only)", print_players_for_spawn_command)
commands.add_command("printplayersforspawn", "<spawn_name> prints all the connected players for a spawn (Admins only)", print_players_for_spawn_command)

View File

@ -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)

View File

@ -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

View File

@ -37,4 +37,31 @@ 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
Module.ternary = function(c, t, f)
if c then return t else return f end
end
return Module