-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
blips_server.lua
399 lines (304 loc) · 11.5 KB
/
blips_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
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
392
393
394
395
396
397
398
399
--[[
LiveMap - A LiveMap for FiveM servers
Copyright (C) 2017 Jordan Dalton
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program in the file "LICENSE". If not, see <http://www.gnu.org/licenses/>.
]]
--[[
When the client has generated the blips, this is called to save them.
@param blipTable A array that contains all the blips to save
]]
RegisterServerEvent("livemap:blipsGenerated")
--[[
Adds a blip to the blips that the UI can see (will be saved)
@param blip A table that represents a blip.
Only the host of the session can use this event.
A blip follows the format:
{
sprite = Number (required)
pos = Table (required){
x = Float (required)
y = Float (required)
z = Float (required)
}
name = String (optional)
description = String (optional)
}
sprite is the "spriteId" used when creating a blip.
x,y,z represents the position of the blip (rounded to 2dp) and is stored inside "pos"
name is the name of the blip shown on the UI
desscription is a description of what the blip does, shown on UI
]]
RegisterServerEvent("livemap:AddBlip")
--[[
Updates the information that is associated with a blip.
Only the host of the session can use this event.
@param blip A table that represents a blip (see the above format)
Note: You cannot change the "sprite", "x", "y", "z" properties of the blip
]]
RegisterServerEvent("livemap:UpdateBlip")
RegisterServerEvent("livemap:RemoveClosestBlip")
RegisterServerEvent("livemap:RemoveBlip")
-- An array of blips that we have. This is encoded into JSON and sent over HTTP (when requested)
local blips = {}
-- Set this to the player who did the "blips generate" command. Should stop
-- randomers triggering the event :)
local playerWhoGeneratedBlips = nil
--[[
===================================
= Functions =
===================================
]]
--[[
Checks to see if the "blips" array is empty.
@returns boolean True if the array is empty, False otherwise
]]
function blipsIsEmpty()
for _, _ in pairs(blips) do
return false
end
return true
end
--[[
Checks if the "blips" array contains a certain blip.
@param id The sprite ID of the blip
@param x The x coordinate of the blip (rounded to 2dp)
@param y The y coordinate of the blip (rounded to 2dp)
@param z The z coordinate of the blip (rounded to 2dp)
@return int The index that the blip is found at, -1 if not found.
]]
function blipsContainsBlip(id, x, y, z)
if blips[id] == nil then
return -1
end
for k,v in ipairs(blips[id]) do
if v.pos.x == x and v.pos.y == y and v.pos.z == z then
return k
end
end
return -1
end
--[[
Sends the encoded "blips" array to the client requesting it
If no blips, an error is sent instead
@param res The result object that can be sent data
]]
function sendBlips(res)
if not blipsIsEmpty() then
res.send(json.encode(blips))
else
res.send(json.encode({
error = "blip cache is empty"
}))
end
end
--[[
Saves the "blips" array to a file on the server.
The filename is set in the server.cfg file with the convar `blip_file`
]]
function saveBlips()
if blipsIsEmpty() then
print("Blips are empty... No need to save")
return
end
local blipFile = GetConvar("blip_file", "server/blips.json")
local saved = SaveResourceFile(GetCurrentResourceName(), blipFile, json.encode(blips, {indent = true}), -1)
if not saved then
print("LiveMap couldn't save the blips to file.. Maybe the directory isn't writable?")
end
end
--[[
===================================
= Events =
===================================
]]
AddEventHandler("livemap:AddBlip", function(blip)
if not blip["sprite"] or blip["sprite"] == nil then
-- We don't have a sprite id... we can't save it :(
print("LiveMap Error: AddBlip cannot run because no sprite wasn't supplied.")
return
end
if not blip["pos"] or blip["pos"] == nil then
print("LiveMap Error:: AddBlip cannot run because the pos wasn't supplied.")
return
end
local id = tostring(blip["sprite"])
blip["sprite"] = nil
-- Conveert coordinates to 2dp
blip["pos"].x = tonumber(string.format("%.2f", blip["pos"].x))
blip["pos"].y = tonumber(string.format("%.2f", blip["pos"].y))
blip["pos"].z = tonumber(string.format("%.2f", blip["pos"].z))
if blipsContainsBlip(id, blip.pos.x, blip.pos.y, blip.pos.z) == -1 then
if blips[id] == nil then
blips[id] = {}
end
table.insert(blips[id], blip)
TriggerEvent("livemap:internal_AddBlip", id, blip)
print("Sending addBlip for " .. id)
else
print("Blip already exists at " .. blip.pos.x .. "," .. blip.pos.y .. "," .. blip.pos.z)
end
end)
AddEventHandler("livemap:UpdateBlip", function(blip)
if not blip["sprite"] or blip["sprite"] == nil then
-- We don't have a sprite id... we can't save it :(
print("LiveMap Error: UpdateBlip cannot run because no sprite wasn't supplied.")
return
end
if not blip["pos"] or blip["pos"] == nil then
print("LiveMap Error:: UpdateBlip cannot run because the pos wasn't supplied.")
return
end
local id = tostring(blip["sprite"])
blip["sprite"] = nil
blip["pos"].x = tonumber(string.format("%.2f", blip["pos"].x))
blip["pos"].y = tonumber(string.format("%.2f", blip["pos"].y))
blip["pos"].y = tonumber(string.format("%.2f", blip["pos"].y))
local index = blipsContainsBlip(id, blip.pos.x, blip.pos.y, blip.pos.z)
if index ~= -1 then
blips[id][index] = blip
TriggerEvent("livemap:internal_UpdateBlip", id, blip)
else
-- Blip doesn't exist, add it?
if blips[id] == nil then
blips[id] = {}
end
table.insert(blips[id], blip)
TriggerEvent("livemap:internal_AddBlip", id, blip)
end
end)
AddEventHandler("livemap:RemoveBlip", function(blip)
if not blip["sprite"] or blip["sprite"] == nil then
-- We don't have a sprite id... we can't save it :(
print("LiveMap Error: RemoveBlip cannot run because no sprite wasn't supplied.")
return
end
if not blip["pos"] or blip["pos"] == nil then
print("LiveMap Error:: RemoveBlip cannot run because the pos wasn't supplied.")
return
end
local id = tostring(blip["sprite"])
blip["sprite"] = nil
blip["pos"].x = tonumber(string.format("%.2f", blip["pos"].x))
blip["pos"].y = tonumber(string.format("%.2f", blip["pos"].y))
blip["pos"].y = tonumber(string.format("%.2f", blip["pos"].y))
local index = blipsContainsBlip(id, blip.pos.x, blip.pos.y, blip.pos.z)
if index == -1 then
print("LiveMap Error: Cannot remove blip as it isn't in the table")
else
blips[id][index] = nil
TriggerEvent("livemap:RemoveBlip", id, blip)
end
end)
function getDistanceBetween(p1, p2)
-- Apparently math.pow doesn't exist (nil value)
local x = (p2.x - p1.x) * (p2.x - p1.x)
local y = (p2.y - p1.y) * (p2.y - p1.y)
local z = (p2.z - p1.z) * (p2.z - p1.z)
-- sqrt works though!
return math.sqrt(x + y + z)
end
AddEventHandler("livemap:RemoveClosestBlip", function(playerPos)
local currentClosest = nil
local sprite = nil
local currentDistance = 999999
for spriteId, blipArray in pairs(blips) do
for _, blip in pairs(blipArray) do
local distance = getDistanceBetween(playerPos, blip.pos)
if (distance < currentDistance) then
currentDistance = distance
currentClosest = blip
sprite = spriteId
end
end
end
if currentClosest == nil then
-- No blips at all?
return
end
print("Removing closest blip ("..sprite..") located at: " .. currentClosest.pos.x .. ", " .. currentClosest.pos.y .. ", " .. currentClosest.pos.z)
local index = blipsContainsBlip(sprite, currentClosest.pos.x, currentClosest.pos.y, currentClosest.pos.z)
if index == -1 then
print("LiveMap Error: Umm... The closest blip doesn't have an index.. Something seriously wrong here!!!")
else
blips[sprite][index] = nil
TriggerEvent("livemap:internal_RemoveBlip", sprite, currentClosest)
end
end)
AddEventHandler("livemap:blipsGenerated", function(blipTable)
local id = GetPlayerIdentifier(source, 0)
if playerWhoGeneratedBlips == nil or playerWhoGeneratedBlips ~= id then
print("playerWhoGeneratedBlips was incorrect.. Maybe \"" .. id .. "\" triggered the event themselves?\nEither way, I'm not doing it..")
return
end
blips = blipTable
saveBlips()
end)
AddEventHandler("onResourceStop", function(name)
if name == GetCurrentResourceName() then
saveBlips() -- Make sure the blips file is saved
end
end)
AddEventHandler("onResourceStart", function(name)
if name == GetCurrentResourceName() then
local blipFile = GetConvar("blip_file", "server/blips.json")
local blipData = LoadResourceFile(GetCurrentResourceName(), blipFile)
if blipData then
blips = json.decode(blipData)
-- Update all the old positions to use the new one :)
for spriteId, blipArray in pairs(blips) do
for _, blip in pairs(blipArray) do
if not blip["pos"] then
blip.pos = {x=blip.x, y=blip.y, z=blip.z}
blip.x, blip.y, blip.z = nil,nil,nil
end
end
end
print("loaded blip cache from file")
end
end
end)
AddEventHandler('playerDropped', function()
local id = GetPlayerIdentifier(source, 0)
TriggerEvent("livemap:internal_RemovePlayer", id)
end)
--[[
===================================
= Misc =
===================================
]]
--[[
Register a command and can be used in-game by an admin to generate the inital
set of blips.
]]
RegisterCommand("blips", function(source, args, rawCommand)
local playerId = GetPlayerIdentifier(source, 0)
if args[1] == "generate" then
print("Generating blips using the in-game native: Player " .. playerId .. " is generating (I hope you know them)")
playerWhoGeneratedBlips = playerId
TriggerClientEvent("livemap:getBlipsFromClient", source)
return
end
end, true)
--[[
Handle HTTP requests. Mainly used for getting the blips via ajax from the UI
]]
SetHttpHandler(function(req, res)
local path = req.path
-- Restrict the origin if set, otherwise allow everyone
res.writeHead(200, { ["Access-Control-Allow-Origin"] = GetConvar("livemap_access_control", "*")} )
if path == "/blips" or path == "/blips.json" then
return sendBlips(res)
end
return res.send(json.encode({
error = "path \"" .. path .."\" not found"
}))
end)