-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.lua
340 lines (309 loc) · 16.4 KB
/
server.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
-- Created by Dalrae
--[[ CONFIG ]]
local team1 = {
["Name"] = "Red",
["TextColorPrefix"] = "~r~",
["ChatColorPrefix"] = "^8",
["BlipColor"] = 1
}
local team2 = {
["Name"] = "Blue",
["TextColorPrefix"] = "~b~",
["ChatColorPrefix"] = "^4",
["BlipColor"] = 3
}
local flagLocations = { -- Pairs of team flag locations
{ --Just some test points
vector3(1920, 4709, 45),
vector3(1941, 4721, 45)
},
--[[{ -- Paleto Gas Station, Los Santos Docks
vector3(159, 6586, 35),
vector3(-1336, -3044, 17)
},]]
}
local maxPoints = 3 -- How many points to win
local captureWithFlagGone = false -- Should the player be able to capture without their flag being at the base?
local canSwitchToUnbalanced = true -- Can players use /switchteams even if it will make the teams unbalanced?
--[[ END CONFIG ]]
function getDistance(a, b, noZ) -- Gets distance between two coords, third arg being true ignores the Z coord (up-down)
local x, y, z = a.x-b.x, a.y-b.y, a.z-b.z
if noZ then
return math.floor(math.sqrt(x*x+y*y))
else
return math.floor(math.sqrt(x*x+y*y+z*z))
end
end
local gameInfo = {}
gameInfo.Teams = {}
gameInfo.PlayerTeams = {}
gameInfo.Objects = {}
gameInfo.CapWithFlagGone = captureWithFlagGone
function getPlayerTeams(player)
local myTeam, otherTeam
for playerT,playerInfo in pairs(gameInfo.PlayerTeams) do
if tonumber(playerT) == tonumber(player) then
myTeam = playerInfo.Team
break
end
end
for _,team in pairs(gameInfo.Teams) do
if team.Name ~= myTeam.Name then
otherTeam = team
break
end
end
return myTeam, otherTeam
end
function isPlayerValid(player)
for _,playerT in pairs(GetPlayers()) do
if tonumber(playerT) == tonumber(player) then
return true
end
end
return false
end
function createTeamFlag(team)
if gameInfo.Objects[team.Name] then
DeleteEntity(NetworkGetEntityFromNetworkId(gameInfo.Objects[team.Name].NetID))
DeleteEntity(NetworkGetEntityFromNetworkId(gameInfo.Objects[team.Name.."Pole"].NetID))
end
local flag = CreateObjectNoOffset(GetHashKey("apa_prop_flag_script"), team.InitialFlagCoords.xyz, true, true, true)
FreezeEntityPosition(flag, true)
gameInfo.Objects[team.Name] = {
["NetID"] = NetworkGetNetworkIdFromEntity(flag)
}
local flagPole = CreateObjectNoOffset(GetHashKey("prop_flagpole_1a"), vector3(team.InitialFlagCoords.x, team.InitialFlagCoords.y, team.InitialFlagCoords.z-5.8).xyz, true, true, true)
FreezeEntityPosition(flagPole, true)
gameInfo.Objects[team.Name.."Pole"] = {
["NetID"] = NetworkGetNetworkIdFromEntity(flagPole)
}
if team.Name == gameInfo.Teams.Team1.Name then
gameInfo.Teams.Team1.FlagPickup = nil
gameInfo.Teams.Team1.FlagDropped = false
gameInfo.Teams.Team1.CurrentFlagCoords = gameInfo.Teams.Team1.InitialFlagCoords
else
gameInfo.Teams.Team2.FlagPickup = nil
gameInfo.Teams.Team2.FlagDropped = false
gameInfo.Teams.Team2.CurrentFlagCoords = gameInfo.Teams.Team2.InitialFlagCoords
end
end
RegisterServerEvent("DalraeEvent:Ping", function()
if not gameInfo.PlayerTeams[source] then
gameInfo.PlayerTeams[source] = {
["Team"] = math.random(0,1) == 0 and team1 or team2,
["Position"] = GetEntityCoords(GetPlayerPed(source)),
["IsConnected"] = true
}
end
end)
CreateThread(function()
Wait(2000)
gameInfo.Teams.Team1 = team1
gameInfo.Teams.Team2 = team2
local flagLocationPair = flagLocations[math.random(1, #flagLocations)]
gameInfo.Teams.Team1.InitialFlagCoords = flagLocationPair[1]
gameInfo.Teams.Team1.CurrentFlagCoords = flagLocationPair[1]
gameInfo.Teams.Team1.Points = 0
gameInfo.Teams.Team2.InitialFlagCoords = flagLocationPair[2]
gameInfo.Teams.Team2.CurrentFlagCoords = flagLocationPair[2]
gameInfo.Teams.Team2.Points = 0
for teamIndex, team in pairs(gameInfo.Teams) do
createTeamFlag(team)
end
while true do
Wait(1000)
--pcall(function()
for playerT, playerInfo in pairs(gameInfo.PlayerTeams) do
if isPlayerValid(playerT) then
gameInfo.PlayerTeams[playerT].Position = GetEntityCoords(GetPlayerPed(playerT))
gameInfo.PlayerTeams[playerT].PlayerPed = NetworkGetNetworkIdFromEntity(GetPlayerPed(playerT))
gameInfo.PlayerTeams[playerT].PlayerName = GetPlayerName(playerT)
else
gameInfo.PlayerTeams[playerT].IsConnected = false
end
end
for index,team in pairs(gameInfo.Teams) do
if team.FlagPickup then
gameInfo.Teams[index].CurrentFlagCoords = GetEntityCoords(GetPlayerPed(team.FlagPickup.Player))
local flagPickup = team.FlagPickup
if isPlayerValid(team.FlagPickup.Player) then
if GetEntityHealth(GetPlayerPed(team.FlagPickup.Player)) <= 0 then -- If the player died while holding the flag
TriggerClientEvent("chatMessage", -1, ("^*^8EVENT NOTIFICATION: ^2%s^3 dropped the %s^3 team flag!"):format(GetPlayerName(team.FlagPickup.Player), team.ChatColorPrefix..team.Name))
createTeamFlag(team)
gameInfo.Teams[index].CurrentFlagCoords = GetEntityCoords(GetPlayerPed(flagPickup.Player))
SetEntityCoords(NetworkGetEntityFromNetworkId(gameInfo.Objects[team.Name].NetID), gameInfo.Teams[index].CurrentFlagCoords)
gameInfo.Teams[index].FlagDropped = true
end
else -- If the player left while holding the flag
TriggerClientEvent("chatMessage", -1, ("^*^8EVENT NOTIFICATION: ^2%s^3 dropped the %s^3 team flag!"):format(GetPlayerName(team.FlagPickup.Player), team.ChatColorPrefix..team.Name))
createTeamFlag(team)
gameInfo.Teams[index].CurrentFlagCoords = GetEntityCoords(GetPlayerPed(flagPickup.Player))
SetEntityCoords(NetworkGetEntityFromNetworkId(gameInfo.Objects[team.Name].NetID), gameInfo.Teams[index].CurrentFlagCoords)
gameInfo.Teams[index].FlagDropped = true
end
end
end
--end)
TriggerClientEvent("DalraeEvent:UpdateGameInfo", -1, gameInfo)
end
end)
RegisterServerEvent("DalraeEvent:UpdateObjectTable", function(eventObjects)
gameInfo.Objects = eventObjects
end)
RegisterServerEvent("DalraeEvent:PickupFlag", function()
local myTeam, otherTeam = getPlayerTeams(source)
if otherTeam.Name == gameInfo.Teams.Team1.Name then
if not gameInfo.Teams.Team1.FlagPickup then
gameInfo.Teams.Team1.FlagPickup = {["Player"] = source, ["Name"] = GetPlayerName(source)}
gameInfo.Objects[gameInfo.Teams.Team1.Name].AttachmentPed = NetworkGetNetworkIdFromEntity(GetPlayerPed(source))
TriggerClientEvent("DalraeEvent:PSAAnnouncement", -1, ("~h~~r~EVENT NOTIFICATION: %s~s~ picked up the %s~s~ team flag!"):format(myTeam.TextColorPrefix..GetPlayerName(source), otherTeam.TextColorPrefix..otherTeam.Name))
TriggerClientEvent("chatMessage", -1, ("^*^8EVENT NOTIFICATION: %s^3 picked up the %s^3 team flag!"):format(myTeam.ChatColorPrefix..GetPlayerName(source), otherTeam.ChatColorPrefix..otherTeam.Name))
end
else
if not gameInfo.Teams.Team2.FlagPickup then
gameInfo.Teams.Team2.FlagPickup = {["Player"] = source, ["Name"] = GetPlayerName(source)}
gameInfo.Objects[gameInfo.Teams.Team2.Name].AttachmentPed = NetworkGetNetworkIdFromEntity(GetPlayerPed(source))
TriggerClientEvent("DalraeEvent:PSAAnnouncement", -1, ("~h~~r~EVENT NOTIFICATION: %s~s~ picked up the %s~s~ team flag!"):format(myTeam.TextColorPrefix..GetPlayerName(source), otherTeam.TextColorPrefix..otherTeam.Name))
TriggerClientEvent("chatMessage", -1, ("^*^8EVENT NOTIFICATION: %s^3 picked up the %s^3 team flag!"):format(myTeam.ChatColorPrefix..GetPlayerName(source), otherTeam.ChatColorPrefix..otherTeam.Name))
end
end
end)
RegisterServerEvent("DalraeEvent:ReturnFlag", function()
local myTeam, otherTeam = getPlayerTeams(source)
if myTeam.Name == gameInfo.Teams.Team1.Name then
gameInfo.Teams.Team1.FlagDropped = false
createTeamFlag(gameInfo.Teams.Team1)
else
gameInfo.Teams.Team2.FlagDropped = false
createTeamFlag(gameInfo.Teams.Team2)
end
end)
RegisterServerEvent("DalraeEvent:CaptureFlag", function()
local myTeam, otherTeam = getPlayerTeams(source)
if otherTeam.Name == gameInfo.Teams.Team1.Name then
if gameInfo.Teams.Team1.FlagPickup then
gameInfo.Teams.Team1.LastFlagPickup = gameInfo.Teams.Team1.FlagPickup
gameInfo.Teams.Team1.FlagPickup = nil
gameInfo.Teams.Team1.FlagDropped = false
gameInfo.Teams.Team1.CurrentFlagCoords = gameInfo.Teams.Team1.InitialFlagCoords
gameInfo.Objects[gameInfo.Teams.Team1.Name].AttachmentPed = nil
createTeamFlag(gameInfo.Teams.Team1)
gameInfo.Teams.Team2.Points = gameInfo.Teams.Team2.Points+1
TriggerClientEvent("DalraeEvent:PSAAnnouncement", -1, ("~h~~r~EVENT NOTIFICATION: %s~s~ captured the %s~s~ team flag!"):format(myTeam.TextColorPrefix..GetPlayerName(source), otherTeam.TextColorPrefix..otherTeam.Name))
TriggerClientEvent("chatMessage", -1, ("^*^8EVENT NOTIFICATION: %s^3 captured the %s^3 team flag!"):format(myTeam.ChatColorPrefix..GetPlayerName(source), otherTeam.ChatColorPrefix..otherTeam.Name))
end
else
if gameInfo.Teams.Team2.FlagPickup then
gameInfo.Teams.Team2.LastFlagPickup = gameInfo.Teams.Team2.FlagPickup
gameInfo.Teams.Team2.FlagPickup = nil
gameInfo.Teams.Team2.FlagDropped = false
gameInfo.Teams.Team2.CurrentFlagCoords = gameInfo.Teams.Team2.InitialFlagCoords
gameInfo.Objects[gameInfo.Teams.Team2.Name].AttachmentPed = nil
createTeamFlag(gameInfo.Teams.Team2)
gameInfo.Teams.Team1.Points = gameInfo.Teams.Team1.Points+1
TriggerClientEvent("DalraeEvent:PSAAnnouncement", -1, ("~h~~r~EVENT NOTIFICATION: %s~s~ captured the %s~s~ team flag!"):format(myTeam.TextColorPrefix..GetPlayerName(source), otherTeam.TextColorPrefix..otherTeam.Name))
TriggerClientEvent("chatMessage", -1, ("^*^8EVENT NOTIFICATION: %s^3 captured the %s^3 team flag!"):format(myTeam.ChatColorPrefix..GetPlayerName(source), otherTeam.ChatColorPrefix..otherTeam.Name))
end
end
if gameInfo.Teams.Team1.Points >= maxPoints then
gameInfo.gameWon = gameInfo.Teams.Team1
gameInfo.gameLost = gameInfo.Teams.Team2
elseif gameInfo.Teams.Team2.Points >= maxPoints then
gameInfo.gameWon = gameInfo.Teams.Team2
gameInfo.gameLost = gameInfo.Teams.Team1
end
if gameInfo.gameWon and not gameInfo.gameAlreadyWon then
gameInfo.gameAlreadyWon = true
TriggerClientEvent("DalraeEvent:PSAAnnouncement", -1, ("~h~~r~EVENT NOTIFICATION: %s~s~ team won the game with ~g~%s~s~/~g~%s~s~!"):format(gameInfo.gameWon.TextColorPrefix..gameInfo.gameWon.Name, gameInfo.gameWon.Points, gameInfo.gameLost.Points))
TriggerClientEvent("chatMessage", -1, ("^*^8EVENT NOTIFICATION: %s^3 team won the game with ^2%s^3/^2%s^3!"):format(gameInfo.gameWon.ChatColorPrefix..gameInfo.gameWon.Name, gameInfo.gameWon.Points, gameInfo.gameLost.Points))
end
end)
RegisterCommand("setflag", function(source, args) -- Should be restricted to staff
if args and args[1] and (args[1]:lower() == team1.Name:lower() or args[1]:lower() == team2.Name:lower()) then
if args[1]:lower() == team1.Name:lower() then
createTeamFlag(team1)
gameInfo.Teams.Team1.CurrentFlagCoords = GetEntityCoords(GetPlayerPed(source))
SetEntityCoords(NetworkGetEntityFromNetworkId(gameInfo.Objects[team1.Name].NetID), GetEntityCoords(GetPlayerPed(source)))
else
createTeamFlag(team2)
gameInfo.Teams.Team2.CurrentFlagCoords = GetEntityCoords(GetPlayerPed(source))
SetEntityCoords(NetworkGetEntityFromNetworkId(gameInfo.Objects[team2.Name].NetID), GetEntityCoords(GetPlayerPed(source)))
end
else
TriggerClientEvent("chatMessage", source, "^*^8Command usage: /setflag [red/blue]")
end
end)
RegisterCommand("resetflag", function(source, args) -- Should be restricted to staff
if args and args[1] and (args[1]:lower() == team1.Name:lower() or args[1]:lower() == team2.Name:lower()) then
if args[1]:lower() == team1.Name:lower() then
createTeamFlag(team1)
gameInfo.Teams.Team1.CurrentFlagCoords = gameInfo.Teams.Team1.InitialFlagCoords
SetEntityCoords(NetworkGetEntityFromNetworkId(gameInfo.Objects[team1.Name].NetID), gameInfo.Teams.Team1.InitialFlagCoords)
else
createTeamFlag(team2)
gameInfo.Teams.Team2.CurrentFlagCoords = gameInfo.Teams.Team2.InitialFlagCoords
SetEntityCoords(NetworkGetEntityFromNetworkId(gameInfo.Objects[team2.Name].NetID), gameInfo.Teams.Team2.InitialFlagCoords)
end
else
TriggerClientEvent("chatMessage", source, "^*^8Command usage: /resetflag [red/blue]")
end
end)
RegisterCommand("setbase", function(source, args) -- Should be restricted to staff
if args and args[1] and (args[1]:lower() == team1.Name:lower() or args[1]:lower() == team2.Name:lower()) then
if args[1]:lower() == team1.Name:lower() then
gameInfo.Teams.Team1.InitialFlagCoords = GetEntityCoords(GetPlayerPed(source))+vector3(0,0,4)
createTeamFlag(team1)
else
gameInfo.Teams.Team2.InitialFlagCoords = GetEntityCoords(GetPlayerPed(source))+vector3(0,0,4)
createTeamFlag(team2)
end
else
TriggerClientEvent("chatMessage", source, "^*^8Command usage: /setflag [red/blue]")
end
end)
RegisterCommand("bothteams", function(source, args) -- Should be restricted to staff
gameInfo.PlayerTeams[source].BothTeams = not gameInfo.PlayerTeams[source].BothTeams
if gameInfo.PlayerTeams[source].BothTeams then
TriggerClientEvent("chatMessage", source, "^*^8You can now see both team's blips and name tags.")
else
TriggerClientEvent("chatMessage", source, "^*^8You can no longer see both team's blips and name tags.")
end
end)
RegisterCommand("switchteams", function (source, args)
local myTeam, otherTeam = getPlayerTeams(source)
local numTeam1, numTeam2 = 0, 0
for _,playerInfo in pairs(gameInfo.PlayerTeams) do
if playerInfo.Team == team1 then
numTeam1 = numTeam1+1
else
numTeam2 = numTeam2+1
end
end
if (gameInfo.Teams.Team1.FlagPickup and gameInfo.Teams.Team1.FlagPickup.Player == source) or (gameInfo.Teams.Team2.FlagPickup and gameInfo.Teams.Team2.FlagPickup.Player == source) then
TriggerClientEvent("chatMessage", source, "^*^8Unable to switch teams while you're holding a flag.")
return
end
if gameInfo.PlayerTeams[source].LastRespawn and GetGameTimer()-gameInfo.PlayerTeams[source].LastRespawn < 5*60*1000 then
TriggerClientEvent("chatMessage", source, ("^*^8Next team switch available in %s seconds"):format((5*60)-math.floor((GetGameTimer()-gameInfo.PlayerTeams[source].LastRespawn)/1000)))
return
end
if canSwitchToUnbalanced or (numTeam1 > numTeam2+2 or numTeam2 > numTeam1+2) then
gameInfo.PlayerTeams[source].Team = otherTeam
gameInfo.PlayerTeams[source].LastRespawn = GetGameTimer()
TriggerClientEvent("DalraeEvent:RespawnPlayer", source)
TriggerClientEvent("chatMessage", source, ("^*^3You switched to the %s^3 team"):format(otherTeam.ChatColorPrefix..otherTeam.Name))
else
TriggerClientEvent("chatMessage", source, ("^*^8Unable to switch to the %s^8 team because it will make the teams unbalanced."):format(otherTeam.ChatColorPrefix..otherTeam.Name))
end
end)
AddEventHandler("onResourceStop", function(name)
if name == GetCurrentResourceName() then
for _,objectItem in pairs(gameInfo.Objects) do
local object = NetworkGetEntityFromNetworkId(objectItem.NetID)
if DoesEntityExist(object) then
DeleteEntity(object)
end
end
end
end)