2018-11-20 12:46:19 +02:00
local Event = require ' utils.event '
2018-11-26 03:07:03 +02:00
local Utils = require ' utils.core '
2018-11-20 12:46:19 +02:00
local Module = { }
2018-04-06 21:58:50 +02:00
2017-08-25 11:18:28 +02:00
global.player_spawns = { } -- player_index to spawn_name
2017-08-28 08:08:23 +02:00
global.spawns = { } -- spawn_name to x, y, player_online_count
2018-11-20 12:46:19 +02:00
Module.add_spawn = function ( name , x , y )
if type ( name ) ~= ' string ' then
game.print ( ' name must be a string ' )
2017-08-28 08:08:23 +02:00
return
end
2018-11-20 12:46:19 +02:00
if type ( x ) ~= ' number ' then
game.print ( ' x must be a number ' )
2017-08-28 08:08:23 +02:00
return
end
2018-11-20 12:46:19 +02:00
if type ( y ) ~= ' number ' then
game.print ( ' y must be a number ' )
2017-08-28 08:08:23 +02:00
return
end
2018-11-06 13:55:52 +02:00
2018-11-20 12:46:19 +02:00
global.spawns [ name ] = { x = x , y = y , count = 0 }
2017-08-28 08:08:23 +02:00
end
2017-08-25 11:18:28 +02:00
local function get_min_count_spawn_name ( )
2017-08-25 23:32:55 +02:00
local min = 1000000
2017-08-25 11:18:28 +02:00
local min_spawn = nil
for name , t in pairs ( global.spawns ) do
local count = t.count
2017-08-25 23:32:55 +02:00
if min > count then
2017-08-25 11:18:28 +02:00
min = count
2018-11-06 13:55:52 +02:00
min_spawn = name
2017-08-25 11:18:28 +02:00
end
end
return min_spawn
end
local function player_joined_game ( event )
local index = event.player_index
2018-11-06 13:55:52 +02:00
local spawn_name = global.player_spawns [ index ]
2017-08-25 11:18:28 +02:00
-- 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
2018-11-06 13:55:52 +02:00
spawn_name = get_min_count_spawn_name ( )
2018-11-20 12:46:19 +02:00
if not spawn_name then
return
end
2017-08-25 11:18:28 +02:00
local spawn = global.spawns [ spawn_name ]
global.player_spawns [ index ] = spawn_name
2019-05-16 12:10:56 +02:00
game.get_player ( index ) . teleport ( spawn )
2018-11-06 13:55:52 +02:00
2017-08-25 11:18:28 +02:00
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 ]
2018-11-20 12:46:19 +02:00
if not spawn then
return
end
2017-08-25 11:18:28 +02:00
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 ]
2018-11-06 13:55:52 +02:00
local spawn = global.spawns [ spawn_name ]
2017-08-25 11:18:28 +02:00
2018-11-20 12:46:19 +02:00
if not spawn then
return
end
2018-11-06 13:55:52 +02:00
2019-05-16 12:10:56 +02:00
game.get_player ( index ) . teleport ( spawn )
2017-08-25 11:18:28 +02:00
end
local function tp_spawn ( player_name , spawn_name )
2019-05-16 12:10:56 +02:00
local player = game.get_player ( player_name )
2017-08-25 11:18:28 +02:00
if not player then
2018-11-20 12:46:19 +02:00
player_name = player_name or ' '
game.player . print ( ' player ' .. player_name .. ' does not exist. ' )
2017-08-25 11:18:28 +02:00
return
end
local spawn = global.spawns [ spawn_name ]
if not spawn then
2018-11-20 12:46:19 +02:00
spawn_name = spawn_name or ' '
game.player . print ( ' spawn ' .. spawn_name .. ' does not exist. ' )
2017-08-25 11:18:28 +02:00
return
2018-11-06 13:55:52 +02:00
end
2017-08-25 11:18:28 +02:00
player.teleport ( spawn )
end
local function change_spawn ( player_name , spawn_name )
local new_spawn = global.spawns [ spawn_name ]
if not new_spawn then
2018-11-20 12:46:19 +02:00
spawn_name = spawn_name or ' '
game.player . print ( ' spawn ' .. spawn_name .. ' does not exist. ' )
2017-08-25 11:18:28 +02:00
return
end
2019-05-16 12:10:56 +02:00
local player = game.get_player ( player_name )
2017-08-25 11:18:28 +02:00
if not player then
2018-11-20 12:46:19 +02:00
player_name = player_name or ' '
game.player . print ( ' player ' .. player_name .. ' does not exist. ' )
2017-08-25 11:18:28 +02:00
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
2018-11-20 12:46:19 +02:00
game.player . print ( player_name .. ' spawn moved to ' .. spawn_name )
2017-08-25 11:18:28 +02:00
end
local function print_spawns ( )
2018-06-05 11:25:00 +02:00
for name , spawn in pairs ( global.spawns ) do
2018-11-20 12:46:19 +02:00
game.player . print ( string.format ( ' %s: (%d, %d), player count = %d ' , name , spawn.x , spawn.y , spawn.count ) )
2018-06-05 11:25:00 +02:00
end
2017-08-25 11:18:28 +02:00
end
local function print_players_for_spawn ( target_spawn_name )
if not global.spawns [ target_spawn_name ] then
2018-11-20 12:46:19 +02:00
target_spawn_name = target_spawn_name or ' '
game.player . print ( ' spawn ' .. target_spawn_name .. ' does not exist. ' )
2017-08-25 11:18:28 +02:00
return
end
2018-11-20 12:46:19 +02:00
local str = ' '
2017-08-25 11:18:28 +02:00
for index , spawn_name in pairs ( global.player_spawns ) do
if target_spawn_name == spawn_name then
2019-05-16 12:10:56 +02:00
local player = game.get_player ( index )
2017-08-25 11:18:28 +02:00
if player.connected then
2018-11-20 12:46:19 +02:00
str = str .. player.name .. ' , '
2017-08-25 11:18:28 +02:00
end
end
end
2018-11-20 12:46:19 +02:00
if str == ' ' then
str = ' no players '
end
2017-08-25 11:18:28 +02:00
game.player . print ( str )
end
local function tp_spawn_command ( cmd )
if not game.player . admin then
2018-11-20 12:46:19 +02:00
Utils.cant_run ( cmd.name )
return
2017-08-25 11:18:28 +02:00
end
local params = cmd.parameter
2018-11-20 12:46:19 +02:00
if type ( params ) ~= ' string ' then
game.player . print ( ' Command failed. Usage: /tpspawn <player>, <spawn_name> ' )
2017-08-25 11:18:28 +02:00
return
end
2018-11-20 12:46:19 +02:00
local ps = { }
for p in params : gmatch ( ' %S+ ' ) do
table.insert ( ps , p )
2017-08-25 11:18:28 +02:00
end
2018-11-20 12:46:19 +02:00
if # ps == 1 then
tp_spawn ( game.player . name , ps [ 1 ] )
else
tp_spawn ( ps [ 1 ] , ps [ 2 ] )
end
2017-08-25 11:18:28 +02:00
end
2018-11-20 12:46:19 +02:00
local function change_spawn_command ( cmd )
2017-08-25 11:18:28 +02:00
if not game.player . admin then
2018-11-20 12:46:19 +02:00
Utils.cant_run ( cmd.name )
return
2017-08-25 11:18:28 +02:00
end
local params = cmd.parameter
2018-11-20 12:46:19 +02:00
if type ( params ) ~= ' string ' then
game.player . print ( ' Command failed. Usage: /changespawn <player>, <spawn_name> ' )
2017-08-25 11:18:28 +02:00
return
end
2018-11-20 12:46:19 +02:00
local ps = { }
for p in params : gmatch ( ' %S+ ' ) do
table.insert ( ps , p )
end
2017-08-25 11:18:28 +02:00
2018-11-06 13:55:52 +02:00
change_spawn ( ps [ 1 ] , ps [ 2 ] )
2017-08-25 11:18:28 +02:00
end
local function print_spawns_command ( cmd )
if not game.player . admin then
2018-11-20 12:46:19 +02:00
Utils.cant_run ( cmd.name )
return
2017-08-25 11:18:28 +02:00
end
print_spawns ( )
end
local function print_players_for_spawn_command ( cmd )
if not game.player . admin then
2018-11-20 12:46:19 +02:00
Utils.cant_run ( cmd.name )
return
2017-08-25 11:18:28 +02:00
end
local params = cmd.parameter
2018-11-20 12:46:19 +02:00
if type ( params ) ~= ' string ' then
game.player . print ( ' Command failed. Usage: /playersforspawn <spawn_name> ' )
2017-08-25 11:18:28 +02:00
return
end
2018-11-20 12:46:19 +02:00
local ps = { }
for p in params : gmatch ( ' %S+ ' ) do
table.insert ( ps , p )
end
2017-08-25 11:18:28 +02:00
print_players_for_spawn ( ps [ 1 ] )
end
2018-04-06 21:58:50 +02:00
Event.add ( defines.events . on_player_joined_game , player_joined_game )
Event.add ( defines.events . on_player_left_game , player_left_game )
Event.add ( defines.events . on_player_respawned , player_respawned )
2017-08-25 11:18:28 +02:00
2019-03-04 13:54:55 +02:00
commands.add_command ( ' tpspawn ' , ' <player> <spawn_name> teleports a player to the spawn point (Admins only) ' , tp_spawn_command ) -- luacheck: ignore
commands.add_command ( ' changespawn ' , ' <player> <spawn_name> changes the spawn point for a player (Admins only) ' , change_spawn_command ) -- luacheck: ignore
commands.add_command ( ' printspawns ' , ' prints info on all spawn points (Admins only) ' , print_spawns_command ) -- luacheck: ignore
commands.add_command ( ' printplayersforspawn ' , ' <spawn_name> prints all the connected players for a spawn (Admins only) ' , print_players_for_spawn_command ) -- luacheck: ignore
2018-11-20 12:46:19 +02:00
return Module