mirror of
https://github.com/Refactorio/RedMew.git
synced 2024-12-12 10:04:40 +02:00
spawn control added
This commit is contained in:
parent
608850eda6
commit
9bc2198000
@ -18,8 +18,16 @@ require "nuke_control"
|
||||
require "walk_distance"
|
||||
require "on_tick"
|
||||
require "follow"
|
||||
require "spawn_control"
|
||||
|
||||
--[[
|
||||
local function player_respawned(event)
|
||||
game.print("hello")
|
||||
end
|
||||
|
||||
Event.register(defines.events.on_player_respawned, player_respawned)
|
||||
|
||||
--]]
|
||||
|
||||
function player_joined(event)
|
||||
local player = game.players[event.player_index]
|
||||
|
@ -21,7 +21,7 @@ line2 = translate(line2, 55.5,-23.5)
|
||||
|
||||
local half = compound_or({ line2,line1,circle})
|
||||
|
||||
half = translate(half, -78.625, 0)
|
||||
half = translate(half, -78.71875, 0)
|
||||
local map = compound_or({ half, flip_xy(half) })
|
||||
|
||||
map = scale(map, 16, 16)
|
||||
|
234
spawn_control.lua
Normal file
234
spawn_control.lua
Normal file
@ -0,0 +1,234 @@
|
||||
global.player_spawns = {} -- player_index to spawn_name
|
||||
global.spawns = { left = { x = -2144, y = 0, count = 0 }, right = { x = 2144, y = 0, count = 0 }} -- spawn_name to x, y, player_count
|
||||
|
||||
local function get_min_count_spawn_name()
|
||||
local min = nil
|
||||
local min_spawn = nil
|
||||
|
||||
for name, t in pairs(global.spawns) do
|
||||
local count = t.count
|
||||
if not min or min < count then
|
||||
min = count
|
||||
min_spawn = name
|
||||
end
|
||||
end
|
||||
|
||||
return min_spawn
|
||||
end
|
||||
|
||||
local function player_joined_game(event)
|
||||
local index = event.player_index
|
||||
local spawn_name = global.player_spawns[index]
|
||||
|
||||
-- player already has a spawn.
|
||||
if spawn_name then
|
||||
local spawn = global.spawns[spawn_name]
|
||||
local count = spawn.count
|
||||
spawn.count = count + 1
|
||||
return
|
||||
end
|
||||
|
||||
spawn_name = get_min_count_spawn_name()
|
||||
|
||||
if not spawn_name then
|
||||
return
|
||||
end
|
||||
|
||||
local spawn = global.spawns[spawn_name]
|
||||
global.player_spawns[index] = spawn_name
|
||||
game.players[index].teleport(spawn)
|
||||
|
||||
local count = spawn.count
|
||||
spawn.count = count + 1
|
||||
end
|
||||
|
||||
local function player_left_game(event)
|
||||
local index = event.player_index
|
||||
local spawn_name = global.player_spawns[index]
|
||||
local spawn = global.spawns[spawn_name]
|
||||
|
||||
if not spawn then
|
||||
return
|
||||
end
|
||||
|
||||
local count = spawn.count
|
||||
spawn.count = count - 1
|
||||
end
|
||||
|
||||
local function player_respawned(event)
|
||||
local index = event.player_index
|
||||
local spawn_name = global.player_spawns[index]
|
||||
local spawn = global.spawns[spawn_name]
|
||||
|
||||
if not spawn then
|
||||
return
|
||||
end
|
||||
|
||||
game.players[index].teleport(spawn)
|
||||
end
|
||||
|
||||
local function tp_spawn(player_name, spawn_name)
|
||||
local player = game.players[player_name]
|
||||
if not player then
|
||||
player_name = player_name or ""
|
||||
game.player.print("player " .. player_name .. " does not exist.")
|
||||
return
|
||||
end
|
||||
|
||||
local spawn = global.spawns[spawn_name]
|
||||
if not spawn then
|
||||
spawn_name = spawn_name or ""
|
||||
game.player.print("spawn " .. spawn_name .. " does not exist.")
|
||||
return
|
||||
end
|
||||
|
||||
player.teleport(spawn)
|
||||
end
|
||||
|
||||
local function change_spawn(player_name, spawn_name)
|
||||
local new_spawn = global.spawns[spawn_name]
|
||||
|
||||
if not new_spawn then
|
||||
spawn_name = spawn_name or ""
|
||||
game.player.print("spawn " .. spawn_name .. " does not exist.")
|
||||
return
|
||||
end
|
||||
|
||||
local player = game.players[player_name]
|
||||
|
||||
if not player then
|
||||
player_name = player_name or ""
|
||||
game.player.print("player " .. player_name .. " does not exist.")
|
||||
return
|
||||
end
|
||||
|
||||
local index = player.index
|
||||
local old_spawn_name = global.player_spawns[index]
|
||||
local old_spawn = global.spawns[old_spawn_name]
|
||||
|
||||
if old_spawn then
|
||||
local count = old_spawn.count
|
||||
old_spawn.count = count - 1
|
||||
end
|
||||
|
||||
local count = new_spawn.count
|
||||
new_spawn.count = count + 1
|
||||
|
||||
global.player_spawns[index] = spawn_name
|
||||
|
||||
game.player.print(player_name .. " spawn moved to " .. spawn_name)
|
||||
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)
|
||||
end
|
||||
|
||||
local function print_players_for_spawn(target_spawn_name)
|
||||
if not global.spawns[target_spawn_name] then
|
||||
target_spawn_name = target_spawn_name or ""
|
||||
game.player.print("spawn " .. target_spawn_name .. " does not exist.")
|
||||
return
|
||||
end
|
||||
|
||||
str = ""
|
||||
for index, spawn_name in pairs(global.player_spawns) do
|
||||
if target_spawn_name == spawn_name then
|
||||
local player = game.players[index]
|
||||
if player.connected then
|
||||
str = str .. player.name .. ", "
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if str == "" then
|
||||
str = "no players"
|
||||
end
|
||||
game.player.print(str)
|
||||
end
|
||||
|
||||
local function tp_spawn_command(cmd)
|
||||
if not game.player.admin then
|
||||
cant_run(cmd.name)
|
||||
return
|
||||
end
|
||||
|
||||
local params = cmd.parameter
|
||||
if type(params) ~= "string" then
|
||||
game.player.print("Command failed. Usage: /tpspawn <player>, <spawn_name>")
|
||||
return
|
||||
end
|
||||
|
||||
local ps ={}
|
||||
for p in params:gmatch("%S+") do
|
||||
table.insert( ps,p )
|
||||
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)
|
||||
if not game.player.admin then
|
||||
cant_run(cmd.name)
|
||||
return
|
||||
end
|
||||
|
||||
local params = cmd.parameter
|
||||
if type(params) ~= "string" then
|
||||
game.player.print("Command failed. Usage: /changespawn <player>, <spawn_name>")
|
||||
return
|
||||
end
|
||||
|
||||
local ps ={}
|
||||
for p in params:gmatch("%S+") do
|
||||
table.insert( ps,p )
|
||||
end
|
||||
|
||||
change_spawn(ps[1], ps[2])
|
||||
end
|
||||
|
||||
local function print_spawns_command(cmd)
|
||||
if not game.player.admin then
|
||||
cant_run(cmd.name)
|
||||
return
|
||||
end
|
||||
|
||||
print_spawns()
|
||||
end
|
||||
|
||||
local function print_players_for_spawn_command(cmd)
|
||||
if not game.player.admin then
|
||||
cant_run(cmd.name)
|
||||
return
|
||||
end
|
||||
|
||||
local params = cmd.parameter
|
||||
if type(params) ~= "string" then
|
||||
game.player.print("Command failed. Usage: /playersforspawn <spawn_name>")
|
||||
return
|
||||
end
|
||||
|
||||
local ps ={}
|
||||
for p in params:gmatch("%S+") do
|
||||
table.insert( ps,p )
|
||||
end
|
||||
|
||||
print_players_for_spawn(ps[1])
|
||||
end
|
||||
|
||||
Event.register(defines.events.on_player_joined_game, player_joined_game)
|
||||
Event.register(defines.events.on_player_left_game, player_left_game)
|
||||
Event.register(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)
|
Loading…
Reference in New Issue
Block a user