-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.lua
249 lines (218 loc) · 8.43 KB
/
event.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
239
240
241
242
243
244
245
246
247
248
249
sea.event = {}
-- Hook argument type casts
local hooks = {
always = {},
assist = {"player", "player", "player"},
attack = {"player"},
attack2 = {"player", true},
bombdefuse = {"player"},
bombexplode = {"player", true, true},
bombplant = {"player", true, true},
["break"] = {true, true, "player"},
build = {"player", "objectType", true, true, true, "object"},
buildattempt = {"player", "objectType", true, true, true},
buy = {"player", "itemType"},
clientdata = {"player", true, true, true},
clientsetting = {"player", true, true, true},
collect = {"player", "item", "itemType", true, true, true},
connect = {"player"},
die = {"player", "player", "itemType", true, true, "object"},
dominate = {"player", true, true},
drop = {"player", "item", "itemType", true, true, true, true, true},
endround = {true},
flagcapture = {"player", true, true},
flagtake = {"player", true, true},
flashlight = {"player", true},
hit = {"player", "player", "itemType", true, true, true, "object"},
hitzone = {"image", "player", "object", "itemType", true, true, true},
hostagerescue = {"player", true, true},
httpdata = {true, true, true},
itemfadeout = {"item", "itemType", true, true},
join = {"player"},
key = {"player", true, true},
kill = {"player", "player", "itemType", true, true, "object", "player"},
leave = {"player", true},
log = {true},
mapchange = {true},
menu = {"player", true, true},
minute = {},
move = {"player", true, true, true},
movetile = {"player", true, true},
ms100 = {},
name = {"player", true, true, true},
objectdamage = {"object", true, "player"},
objectkill = {"object", "player"},
objectupgrade = {"object", "player", true, true},
parse = {true},
projectile = {"player", "itemType", true, true},
projectile_impact = {"player", "itemType", true, true, true, true},
radio = {"player", true},
rcon = {true, "player", true, true},
reload = {"player", true},
say = {"player", true},
sayteam = {"player", true},
second = {},
select = {"player", "itemType", true},
serveraction = {"player", true},
shieldhit = {"player", "player", "itemType", true, true, true},
shutdown = {},
spawn = {"player"},
specswitch = {"player", "player"},
spray = {"player"},
startround = {true},
startround_prespawn = {true},
suicide = {"player"},
team = {"player", true, true},
trigger = {true, true},
triggerentity = {true, true},
turretscan = {"object", true, true, true},
use = {"player", true, true, true, true},
usebutton = {"player", true, true},
vipescape = {"player", true, true},
vote = {"player", true, true},
walkover = {"player", "item", "itemType", true, true, true}
}
local createName = sea.createEventName
function sea.addEvent(name, func, priority)
if not sea.event[name] then
sea.error("Attempted to add event with the invalid name \""..name.."\"")
return false
end
table.insert(sea.event[name], {
func = func,
priority = priority or 0
})
sea.info("Tied function to the event \""..name.."\"")
return true
end
function sea.callEvent(name, ...)
if not sea.event[name] then
sea.error("Attempted to call event with the invalid name \""..name.."\"")
return
end
local returnValue
for _, v in spairs(sea.event[name], function(tbl, a, b) return tbl[b].priority < tbl[a].priority end) do
returnValue = v.func(...)
end
return returnValue or false
end
-- Adding player control events
for name in pairs(sea.config.player.control) do
sea.createControlEventName(name)
end
sea.hook = {}
for name, params in pairs(hooks) do
sea.event[createName("hook", name)] = {}
local function defaultFunc(...)
local args = {...}
if name == "join" then
sea.Player.create(args[1]):loadData()
elseif name == "objectkill" then
sea.Object.remove(args[1])
elseif name == "build" then
sea.Object.create(args[6])
elseif name == "collect" then
sea.Item.remove(args[2])
elseif name == "itemfadeout" then
sea.Item.remove(args[1])
elseif name == "startround_prespawn" then
-- Clearing image table
for id in pairs(sea.image) do
sea.Image.remove(id)
end
-- Clearing object and item tables and regenerating them as they are removed at each round start
for id in pairs(sea.object) do
sea.Object.remove(id)
end
sea.Object.generate()
for id in pairs(sea.item) do
sea.Item.remove(id)
end
sea.Item.generate()
elseif name == "startround" then
-- Recreating the UI images (because they are removed at round start)
for _, player in pairs(sea.Player.get()) do
for _, element in pairs(player.ui.element) do
if element.image then
element.image = sea.Image.create(element.imagePath, element.x, element.y, 2, player)
element:update()
end
end
end
elseif name == "second" then
for _, player in pairs(sea.Player.get()) do
player.stat["Time Played"] = player.stat["Time Played"] + 1
end
elseif name == "minute" then
sea.game:saveData()
for _, player in pairs(sea.Player.get()) do
player:saveData()
end
elseif name == "shutdown" then
sea.game:saveData()
end
for i = 1, #args do
if type(params[i]) == "string" and args[i] > 0 then
args[i] = sea[params[i]][args[i]] or args[i]
end
end
if name == "join" then
if sea.config.game.setting.welcomeMessage then
args[1]:alert("Welcome to "..sea.game.name..", "..args[1].name.."!", sea.Color.green)
end
elseif name == "leave" then
if type(args[1]) == "table" then
args[1]:saveData()
args[1]:destroy()
end
elseif name == "clientsetting" then
if args[2] == 1 then
local playerUI = args[1].ui
playerUI.width, playerUI.height = args[3], args[4]
playerUI:update()
end
elseif name == "key" then
if type(args[1]) == "table" then -- Checks if the sea.player with the specific ID exists, this has to be here becuase key hook event is triggered when player is leaving the server (between the time where they pressed disconnect and until they navigate to the main menu)
for k, v in pairs(args[1].control) do
if v == args[2] then
return sea.callEvent(createName(args[3] == 1 and "press" or "release", k:toPascalCase(" ")), args[1])
end
end
end
elseif name == "serveraction" then
if args[2] == 1 then
args[1]:displayMenu(sea.mainMenu)
end
elseif name == "menu" then
if args[1].currentMenu[1] then
args[1].currentMenu[1]:interact(args[1], args[3])
end
elseif name == "die" then
args[1].stat["Deaths"] = args[1].stat["Deaths"] + 1
elseif name == "kill" then
args[1].stat["Kills"] = args[1].stat["Kills"] + 1
elseif name == "assist" then
args[1].stat["Assists"] = args[1].stat["Assists"] + 1
end
return sea.callEvent(createName("hook", name), unpack(args))
end
local func
if name == "drop" then
func = function(playerID, itemID, type, loadedAmmo, spareAmmo, mode, x, y)
if sea.config.game.setting.itemDrop then
if type ~= 50 then
sea.Item.create(itemID)
end
return defaultFunc(playerID, itemID, type, loadedAmmo, spareAmmo, mode, x, y)
else
defaultFunc(playerID, itemID, type, loadedAmmo, spareAmmo, mode, x, y)
return 1
end
end
end
sea.hook[name] = func or defaultFunc
addhook(name, "sea.hook."..name)
end
function addhook()
sea.error("addhook() is invalid, use sea.addEvent() instead.")
end