forked from Refactorio/RedMew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spawn_control.lua
238 lines (186 loc) · 6.39 KB
/
spawn_control.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
local Event = require "utils.event"
local Game = require 'utils.game'
global.player_spawns = {} -- player_index to spawn_name
global.spawns = {} -- spawn_name to x, y, player_online_count
function add_spawn(name, x, y)
if type(name) ~= "string" then
game.print("name must be a string")
return
end
if type(x) ~= "number" then
game.print("x must be a number")
return
end
if type(y) ~= "number" then
game.print("y must be a number")
return
end
global.spawns[name] = { x = x, y = y, count = 0}
end
local function get_min_count_spawn_name()
local min = 1000000
local min_spawn = nil
for name, t in pairs(global.spawns) do
local count = t.count
if 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.get_player_by_index(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.get_player_by_index(index).teleport(spawn)
end
local function tp_spawn(player_name, spawn_name)
local player = Game.get_player_by_index(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.get_player_by_index(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
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)
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.get_player_by_index(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.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)
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)