-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.lua
More file actions
391 lines (316 loc) · 13.6 KB
/
Copy pathserver.lua
File metadata and controls
391 lines (316 loc) · 13.6 KB
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
local QBCore = exports['qb-core']:GetCoreObject()
local BUSCKET_START_NUMBER = 5
-- Objects definitions
local LobbyTemplate = {
id = nil,
playerData = nil,
participants = {},
participantsCount = 0,
deaths = {},
deathsCount = 0,
aliveCount = 0,
planeModel = nil,
planeEntity = nil, -- Plane
planeNetId = nil, -- Plane network id
pilotPed = nil, -- Ped
pilotNetId = nil, -- Pilot ped
jumpWarned = false, -- the player has been notified that he can now jump,
winnerId = nil
}
local Participant = {
id = nil,
isNpc = false,
hasLeft = false,
ped = nil,
position = nil,
loot = nil,
drops = nil,
health = nil,
armor = nil,
status = nil,
hasJumped = nil,
isDead = false,
lastUpdate = nil,
deadAt = nil,
killerId = nil,
kills = {},
killsCount = 0
}
local playersToLobby = {}
local teams
-- require 'inc/Team.lua'
-- require 'inc/helpers.lua'
--- For Later
local defaultPlayerLobbyData = Config.defaultPlayerLobbyData
local lobbies = {}
-- Configuration
local ZONE_START_RADIUS = 1000.0
local ZONE_END_RADIUS = 5.0
local ZONE_TOTAL_TIME = 300 -- seconds (5 minutes)
local PLANE_SPAWN_DELAY = 4 -- seconds to wait for plane init
local PLANE_FLIGHT_DURATION = 30 -- seconds for plane to traverse path (tweakable)
local PLANE_HEIGHT = 800.0 -- altitude the plane will fly at
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
function deepCopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for k, v in next, orig, nil do
copy[deepCopy(k)] = deepCopy(v)
end
setmetatable(copy, deepCopy(getmetatable(orig)))
else
copy = orig
end
return copy
end
-- Make serveral random coords later
local function randomCoordInBounds()
local x = 247.77
local y = -967.77
local z = 29.31
return x, y, z
end
function GetHeadingFromVector_2d(dx, dy)
local heading = math.deg(math.atan2(dy, dx))
heading = heading + 270
if heading < 0 then
heading = heading + 360.0
end
return heading
end
local function nearestFreeLobbyId()
for i=BUSCKET_START_NUMBER, (tablelength(lobbies)+BUSCKET_START_NUMBER+1) do
if lobbies[i] == nil then
return i
end
end
return i+1
end
local function leaseLobby()
local i = nearestFreeLobbyId()
lobbies[i] = deepCopy(LobbyTemplate)
lobbies[i].id = i
lobbies[i].playerData = deepCopy(Config.defaultPlayerLobbyData)
lobbies[i].playerData.id = i
print("[battleroyal] Id choossen:"..i)
return i
end
local function releaseLobby(i)
lobbies[i] = nil
end
QBCore.Commands.Add("battleexit", "(playerid)", {}, false, function(source, args)
if(args[1]==nil) then args[1] = source end
TriggerClientEvent("battleroyal:playerSync:exit", args[1], true)
end, "admin")
QBCore.Commands.Add("resetzone", "Start zone + plane drop (admin)", {}, false, function(source, args)
local playerLobbyData = deepCopy(defaultPlayerLobbyData)
TriggerClientEvent("battleroyal:playerSync:lobby", -1, playerLobbyData)
end, "admin")
QBCore.Commands.Add("refusezone", "", {}, false, function(source, args)
TriggerClientEvent("battleroyal:playerSync:nozone", source, true)
end, "admin")
QBCore.Commands.Add("acceptzone", "", {}, false, function(source, args)
TriggerClientEvent("battleroyal:playerSync:nozone", source, false)
end, "admin")
QBCore.Commands.Add("battleroyal", "Start Battleroyal", {}, false, function(source, args)
local cx, cy, cz = randomCoordInBounds()
local startRadius = ZONE_START_RADIUS
local endRadius = ZONE_END_RADIUS
local heading = math.rad(math.random(0, 359))
local dirX = math.cos(heading)
local dirY = math.sin(heading)
local pathLen = math.max( (startRadius * 4), 2000 )
local startX = cx - dirX * pathLen/2
local startY = cy - dirY * pathLen/2
local startZ = PLANE_HEIGHT
local endX = cx + dirX * pathLen/2
local endY = cy + dirY * pathLen/2
local endZ = PLANE_HEIGHT
local src = source
local lobbyId = leaseLobby()
lobbies[lobbyId].playerData.id = lobbyId
lobbies[lobbyId].playerData.zoneData = {
x = cx, y = cy, z = cz,
radius = startRadius,
endRadius = endRadius,
totalTime = ZONE_TOTAL_TIME,
}
lobbies[lobbyId].playerData.flightData = {
start = { x = startX, y = startY, z = startZ },
finish = { x = endX, y = endY, z = endZ },
spawnDelay = PLANE_SPAWN_DELAY,
flightDuration = PLANE_FLIGHT_DURATION,
planeModel = "cargoplane",
pilotModel = "s_m_m_pilot_01"
}
lobbies[lobbyId].playerData.startedAt = os.time()
TriggerClientEvent("battleroyal:play", -1, lobbies[lobbyId].playerData)
-- Create plane and pilot
local modelPlane = lobbies[lobbyId].playerData.flightData.planeModel
local modelPilot = lobbies[lobbyId].playerData.flightData.pilotModel
local planeHash = GetHashKey(modelPlane)
local pilotHash = GetHashKey(modelPilot)
lobbies[lobbyId].playerData.planeEntity = CreateVehicle(planeHash, lobbies[lobbyId].playerData.flightData.start.x, lobbies[lobbyId].playerData.flightData.start.y, lobbies[lobbyId].playerData.flightData.start.z, 0.0, true, false)
--lobbies[lobbyId].playerData.pilotPed = CreatePedInsideVehicle(lobbies[lobbyId].playerData.planeEntity, 4, pilotHash, 0, true, false) -- Not properly working
lobbies[lobbyId].playerData.pilotPed = CreatePed(4, pilotHash, lobbies[lobbyId].playerData.flightData.start.x, lobbies[lobbyId].playerData.flightData.start.y, lobbies[lobbyId].playerData.flightData.start.z, 0.0 ,true, false)
SetPedIntoVehicle(lobbies[lobbyId].playerData.pilotPed, lobbies[lobbyId].playerData.planeEntity, -1)
Citizen.Wait(5000)
SetEntityDistanceCullingRadius(lobbies[lobbyId].playerData.planeEntity, 1000.0)
local planeNetId = NetworkGetNetworkIdFromEntity(lobbies[lobbyId].playerData.planeEntity)
local pilotNetId = NetworkGetNetworkIdFromEntity(lobbies[lobbyId].playerData.pilotPed)
for playerId,playerData in pairs(lobbies[lobbyId].participants) do
TriggerClientEvent("battleroyal:planeSync:setNetId", playerId, {lobbyId=lobbies[lobbyId].id,planeNetId=planeNetId, pilotNetId=pilotNetId});
end
local start = lobbies[lobbyId].playerData.flightData.start
local finish = lobbies[lobbyId].playerData.flightData.finish
local steps = math.max(5, lobbies[lobbyId].playerData.flightData.flightDuration or 120) * 30
local step = 0
local sx, sy, sz = start.x, start.y, start.z
local ex, ey, ez = finish.x, finish.y, finish.z
local heading = GetHeadingFromVector_2d(ex - sx, ey - sy)
SetEntityHeading(lobbies[lobbyId].playerData.planeEntity, heading)
CreateThread(function()
while step <= steps and DoesEntityExist(lobbies[lobbyId].playerData.planeEntity) do
local t = step / steps
local nx = sx + (ex - sx) * t
local ny = sy + (ey - sy) * t
local nz = sz + (ez - sz) * t
SetEntityCoords(lobbies[lobbyId].playerData.planeEntity, nx, ny, nz, false, false, false, true);
step = step + 1
if lobbies[lobbyId].playerData.jumpWarned == false then
local dist = #(vector3(nx, ny, nz) - vector3(lobbies[lobbyId].playerData.zoneData.x, lobbies[lobbyId].playerData.zoneData.y, lobbies[lobbyId].playerData.zoneData.z))
if dist <= lobbies[lobbyId].playerData.zoneData.radius and lobbies[lobbyId].playerData.jumpWarned == false then
lobbies[lobbyId].playerData.canJumpFromPlane = true
lobbies[lobbyId].playerData.jumpWarned = true
for playerId,playerData in pairs(lobbies[lobbyId].participants) do
if lobbies[lobbyId].participants[playerId].isNpc == false then
TriggerClientEvent("battleroyal:playerSync:lobby", playerId, lobbies[lobbyId].playerData)
end
end
--TriggerClientEvent("battleroyal:playerSync:lobby", -1 , lobbies[lobbyId].playerData);
end
end
Wait(33)
end
DeleteEntity(lobbies[lobbyId].playerData.planeEntity)
lobbies[lobbyId].playerData.planeEntity = nil
DeleteEntity(lobbies[lobbyId].playerData.pilotPed)
lobbies[lobbyId].playerData.pilotPed = nil
while lobbies[lobbyId].aliveCount > 1 do
end
endLobby(lobbyId)
end)
print("[battleroyal] Zone + plane event started at:", cx, cy)
end, "admin")
RegisterNetEvent("battleroyal:play")
AddEventHandler("battleroyal:play", function(lobby)
local src = source
if lobbies[lobby.id] == nil then
TriggerClientEvent("battleroyal:playerSync:exit",src, true)
print("[battleroyal] Invalid lobby : "..lobbies[lobby.id].id)
else
lobbies[lobby.id].participants[src] = deepCopy(Participant)
lobbies[lobby.id].participants[src].id = src
lobbies[lobby.id].participants[src].name = GetPlayerName(src)
lobbies[lobby.id].participants[src].isDead = false
lobbies[lobby.id].participants[src].hasJumped = false
lobbies[lobby.id].participants[src].isNpc = false
lobbies[lobby.id].participants[src].hasLeft = false
lobbies[lobby.id].participants[src].lastUpdate = os.time()
lobbies[lobby.id].participants[src].loot = {}
lobbies[lobby.id].participants[src].drops = {}
lobbies[lobby.id].participants[src].health = 100
lobbies[lobby.id].participants[src].armor = 0
lobbies[lobby.id].participants[src].status = "ingame"
lobbies[lobby.id].participantsCount=lobbies[lobby.id].participantsCount+1
lobbies[lobby.id].aliveCount=lobbies[lobby.id].participantsCount
playersToLobby[src] = lobby.id
print("[battleroyal] New participant in lobby "..lobby.id)
end
end)
RegisterNetEvent("battleroyal:planeSync:shareNetId")
AddEventHandler("battleroyal:planeSync:shareNetId", function(netId)
TriggerClientEvent("battleroyal:planeSync:setNetId", -1, netId)
end)
RegisterNetEvent("battleroyal:exit")
AddEventHandler("battleroyal:exit", function()
local playerLobbyData = deepCopy(defaultPlayerLobbyData)
playersToLobby[src] = nil
TriggerClientEvent("battleroyal:playerSync:lobby", -1, playerLobbyData)
end)
local function endLobby(lobbyId)
-- lobbies[lobbyId].deathsCount
-- lobbies[lobby.id].participantsCount
for playerId,playerData in pairs(lobbies[lobbyId].participants) do
if lobbies[lobbyId].participants[playerId].isNpc == false then
TriggerClientEvent("battleroyal:playerSync:exit", playerId, true)
end
end
TriggerClientEvent("battleroyal:winner", -1, {
lobbyId: lobbyId,
playerId: source,
winnerId: lobbies[lobbyId].winnerId
})
releaseLobby(lobbyId)
end
RegisterNetEvent("battleroyal:playerSync:death")
AddEventHandler("battleroyal:playerSync:death", function(bool, killerId)
local src = source
if bool then
local lobbyId = playersToLobby[src]
if lobbies[lobbyId].participants[src].isDead == false then
lobbies[lobbyId].participants[src].isDead = true
lobbies[lobbyId].participants[src].killerId = killerId
lobbies[lobbyId].participants[src].deadAt = os.time()
lobbies[lobbyId].deaths[src] = true
lobbies[lobbyId].deathsCount =lobbies[lobbyId].deathsCount+ 1
if (killerId == nil) or lobbies[lobbyId].participants[killerId] == nil then
print("[battleroyal] Killer is not a participant ??? Kick or Ban ??"..killerId)
else
lobbies[lobbyId].participants[killerId].kills[src] = true
lobbies[lobbyId].participants[killerId].killsCount = lobbies[lobbyId].participants[killerId].killsCount +1
if(lobbies[lobbyId].deathsCount == lobbies[lobbyId].participantsCount -1) then
local winner = killerId;
lobbies[lobbyId].winnerId = killerId;
end
end
lobbies[lobby.id].participants[src].aliveCount=lobbies[lobby.id].participants[src].aliveCount-1
end
end
end)
RegisterNetEvent("battleroyal:playerSync:lobby")
AddEventHandler("battleroyal:playerSync:lobby", function(lobby)
local src = source
if lobbies[lobby.id] == nil then
print("[battleroyal] Invalid lobby : "..lobby.id)
TriggerClientEvent("battleroyal:playerSync:lobby",src, defaultPlayerLobbyData)
else
if playersToLobby[src] == lobby.id then
lobby.stats.alive = lobbies[lobby.id].aliveCount
lobby.stats.deaths = lobbies[lobby.id].deathsCount
lobby.stats.players = lobbies[lobby.id].participantsCount
lobby.stats.kills = lobbies[lobby.id].participants[src].killsCount
lobby.player.isDead = lobbies[lobby.id].participants[src].isDead
TriggerClientEvent("battleroyal:playerSync:lobby",src, lobby)
end
end
end)
AddEventHandler('playerDropped', function (reason, resourceName, clientDropReason)
local lobbyId = playersToLobby[source]
if lobbyId == nil or lobbies[lobbyId] == nil then
-- Player was not in lobby
else
-- Player was in lobby
lobbies[lobbyId].participants[source].isDead = true
lobbies[lobbyId].participants[source].killerId = nil
lobbies[lobbyId].participants[source].hasLeft=true
lobbies[lobbyId].participants[source].aliveCount=lobbies[lobbyId].participants[source].aliveCount-1
lobbies[lobbyId].participants[source].lastUpdate = os.time()
end
end)