-
Notifications
You must be signed in to change notification settings - Fork 2
/
global.lua
137 lines (106 loc) · 3.81 KB
/
global.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
Citizen.CreateThread(function()
while true do
Citizen.Wait(0)
math.randomseed(GetGameTimer())
end
end)
rsCharset = {} do -- [0-9a-zA-Z]
for c = 48, 57 do table.insert(rsCharset, string.char(c)) end
for c = 65, 90 do table.insert(rsCharset, string.char(c)) end
end
function randomString(length)
if not length or length <= 0 then return '' end
return randomString(length - 1) .. rsCharset[math.random(1, #rsCharset)]
end
function lerp(pos1, pos2, alpha)
return (1-alpha)*pos1 + alpha*pos2
end
function IsPlayerLookingForward(pPed)
if pPed == nil then local pPed = GetPlayerPed(-1) end
local camCoords = GetGameplayCamCoord() - GetEntityCoords(pPed)
local forward = GetEntityForwardVector(pPed)
local dot = (forward.x * camCoords.x) + (forward.y * camCoords.y) + (forward.z * camCoords.z)
if dot > 0 then return false else return true end
end
-- Nice enumerator functions. I forgot who wrote this originally but it wasn't me.
local function EnumerateEntities(initFunc, moveFunc, disposeFunc)
return coroutine.wrap(function()
local iter, id = initFunc()
if not id or id == 0 then
disposeFunc(iter)
return
end
local enum = {handle = iter, destructor = disposeFunc}
setmetatable(enum, entityEnumerator)
local next = true
repeat
coroutine.yield(id)
next, id = moveFunc(iter)
until not next
enum.destructor, enum.handle = nil, nil
disposeFunc(iter)
end)
end
function EnumerateObjects()
return EnumerateEntities(FindFirstObject, FindNextObject, EndFindObject)
end
function EnumeratePeds()
return EnumerateEntities(FindFirstPed, FindNextPed, EndFindPed)
end
function EnumerateVehicles()
return EnumerateEntities(FindFirstVehicle, FindNextVehicle, EndFindVehicle)
end
function EnumeratePickups()
return EnumerateEntities(FindFirstPickup, FindNextPickup, EndFindPickup)
end
-- Event Callback Helpers --------------------------------
-- How to Use (Client):
-- Call('factions:foo', {}, function(retVal1)
-- print("Nice! retVal:"..retVal1)
-- end)
-- How to Use (Server):
-- AddEventHandler("factions:foo", function(args, cbId)
-- TriggerClientEvent('factions:cl_onCallback_OneParam', source, cbId, "bar")
-- end)
-- RegisterServerEvent("factions:foo")
CallbackTable = {}
function Call(svEventName, args, callback)
local cbType = type(callback)
if cbType == "function" or cbType == "table" then
local callbackId = randomString(8)
CallbackTable[callbackId] = callback
TriggerServerEvent(svEventName, args, callbackId)
else
TriggerServerEvent(svEventName, args, callback)
end
end
AddEventHandler("factions:cl_onCallback", function(callbackId)
CallbackTable[callbackId]()
CallbackTable[callbackId] = nil
end) RegisterNetEvent("factions:cl_onCallback")
AddEventHandler("factions:cl_onCallback_OneParam", function(callbackId, ret1)
CallbackTable[callbackId](ret1)
CallbackTable[callbackId] = nil
end) RegisterNetEvent("factions:cl_onCallback_OneParam")
AddEventHandler("factions:cl_onCallback_AnyParam", function(callbackId, ret1, ret2, ret3, ret4)
if ret4 ~= nil then
CallbackTable[callbackId](ret1, ret2, ret3, ret4)
elseif ret3 ~= nil then
CallbackTable[callbackId](ret1, ret2, ret3)
elseif ret2 ~= nil then
CallbackTable[callbackId](ret1, ret2)
elseif ret1 ~= nil then
CallbackTable[callbackId](ret1)
else
CallbackTable[callbackId]()
end
CallbackTable[callbackId] = nil
end) RegisterNetEvent("factions:cl_onCallback_AnyParam")
-- For debuging mainly
function ShowNotification(text)
SetNotificationTextEntry("STRING")
AddTextComponentString(text)
DrawNotification(false, false)
end
-- Singleton for Client
Global = {}