-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathJAM_Utilities.lua
More file actions
354 lines (321 loc) · 13.5 KB
/
Copy pathJAM_Utilities.lua
File metadata and controls
354 lines (321 loc) · 13.5 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
JAM.Utilities = {}
JUtils = JAM.Utilities
JUtils.Keys = {
["ESC"] = 322, ["F1"] = 288, ["F2"] = 289, ["F3"] = 170, ["F5"] = 166, ["F6"] = 167, ["F7"] = 168, ["F8"] = 169, ["F9"] = 56, ["F10"] = 57,
["~"] = 243, ["1"] = 157, ["2"] = 158, ["3"] = 160, ["4"] = 164, ["5"] = 165, ["6"] = 159, ["7"] = 161, ["8"] = 162, ["9"] = 163, ["-"] = 84, ["="] = 83, ["BACKSPACE"] = 177,
["TAB"] = 37, ["Q"] = 44, ["W"] = 32, ["E"] = 38, ["R"] = 45, ["T"] = 245, ["Y"] = 246, ["U"] = 303, ["P"] = 199, ["["] = 116, ["]"] = 40, ["ENTER"] = 18,
["CAPS"] = 137, ["A"] = 34, ["S"] = 8, ["D"] = 9, ["F"] = 23, ["G"] = 47, ["H"] = 74, ["K"] = 311, ["L"] = 182,
["LEFTSHIFT"] = 21, ["Z"] = 20, ["X"] = 73, ["C"] = 26, ["V"] = 0, ["B"] = 29, ["N"] = 249, ["M"] = 244, [","] = 82, ["."] = 81,
["LEFTCTRL"] = 36, ["LEFTALT"] = 19, ["SPACE"] = 22, ["RIGHTCTRL"] = 70,
["HOME"] = 213, ["PAGEUP"] = 10, ["PAGEDOWN"] = 11, ["DELETE"] = 178,
["LEFT"] = 174, ["RIGHT"] = 175, ["UP"] = 27, ["DOWN"] = 173,
["NENTER"] = 201, ["N4"] = 108, ["N5"] = 60, ["N6"] = 107, ["N+"] = 96, ["N-"] = 97, ["N7"] = 117, ["N8"] = 61, ["N9"] = 118
}
JUtils.Weapons = {
Melee = {
'WEAPON_KNIFE', 'WEAPON_KNUCKLE', 'WEAPON_NIGHTSTICK', 'WEAPON_HAMMER', 'WEAPON_BAT', 'WEAPON_GOLFCLUB', 'WEAPON_CROWBAR', 'WEAPON_BOTTLE', 'WEAPON_DAGGER',
'WEAPON_HATCHET', 'WEAPON_MACHETE', 'WEAPON_SWITCHBLADE', 'WEAPON_POOLCUE',
},
Pistol = {
'WEAPON_REVOLVER', 'WEAPON_PISTOL', 'WEAPON_PISTOL_MK2', 'WEAPON_COMBATPISTOL', 'WEAPON_APPISTOL', 'WEAPON_PISTOL50', 'WEAPON_SNSPISTOL',
'WEAPON_HEAVYPISTOL','WEAPON_VINTAGEPISTOL', 'WEAPON_DOUBLEACTION', 'WEAPON_REVOLVER_MK2', 'WEAPON_SNSPISTOL_MK2',
},
SMG = {
'WEAPON_MICROSMG','WEAPON_MINISMG','WEAPON_SMG','WEAPON_SMG_MK2','WEAPON_ASSAULTSMG', 'WEAPON_MACHINEPISTOL',
},
MG = {
'WEAPON_MG','WEAPON_COMBATMG','WEAPON_COMBATMG_MK2',
},
Assault = {
'WEAPON_ASSAULTRIFLE', 'WEAPON_ASSAULTRIFLE_MK2', 'WEAPON_CARBINERIFLE', 'WEAPON_CARBINERIFLE_MK2', 'WEAPON_ADVANCEDRIFLE', 'WEAPON_SPECIALCARBINE',
'WEAPON_BULLPUPRIFLE', 'WEAPON_COMPACTRIFLE', 'WEAPON_SPECIALCARBINE_MK2', 'WEAPON_BULLPUPRIFLE_MK2',
},
Shotgun = {
'WEAPON_PUMPSHOTGUN','WEAPON_SAWNOFFSHOTGUN','WEAPON_BULLPUPSHOTGUN','WEAPON_ASSAULTSHOTGUN','WEAPON_HEAVYSHOTGUN','WEAPON_DBSHOTGUN',
'WEAPON_PUMPSHOTGUN_MK2',
},
}
function JUtils:DrawTextTemplate(text,x,y,font,scale1,scale2,colour1,colour2,colour3,colour4,wrap1,wrap2,centre,outline,dropshadow1,dropshadow2,dropshadow3,dropshadow4,dropshadow5,edge1,edge2,edge3,edge4,edge5)
return {
text = "",
x = -1,
y = -1,
font = font or 6,
scale1 = scale1 or 0.5,
scale2 = scale2 or 0.5,
colour1 = colour1 or 255,
colour2 = colour2 or 255,
colour3 = colour3 or 255,
colour4 = colour4 or 255,
wrap1 = wrap1 or 0.0,
wrap2 = wrap2 or 1.0,
centre = ( type(centre) ~= "boolean" and true or centre ),
outline = outline or 1,
dropshadow1 = dropshadow1 or 2,
dropshadow2 = dropshadow2 or 0,
dropshadow3 = dropshadow3 or 0,
dropshadow4 = dropshadow4 or 0,
dropshadow5 = dropshadow5 or 0,
edge1 = edge1 or 255,
edge2 = edge2 or 255,
edge3 = edge3 or 255,
edge4 = edge4 or 255,
edge5 = edge5 or 255,
}
end
function JUtils:DrawText( t )
if not t or not t.text or t.text == "" or t.x == -1 or t.y == -1
then return false
end
-- Setup Text
SetTextFont (t.font)
SetTextScale (t.scale1, t.scale2)
SetTextColour (t.colour1,t.colour2,t.colour3,t.colour4)
SetTextWrap (t.wrap1,t.wrap2)
SetTextCentre (t.centre)
SetTextOutline (t.outline)
SetTextDropshadow (t.dropshadow1,t.dropshadow2,t.dropshadow3,t.dropshadow4,t.dropshadow5)
SetTextEdge (t.edge1,t.edge2,t.edge3,t.edge4,t.edge5)
SetTextEntry ("STRING")
-- Draw Text
AddTextComponentSubstringPlayerName (t.text)
DrawText (t.x,t.y)
return true
end
function JUtils.PointOnSphere(alt,azu,radius,orgX,orgY,orgZ)
local toradians = 0.017453292384744
alt,azu,radius,orgX,orgY,orgZ = ( tonumber(alt or 0) or 0 ) * toradians, ( tonumber(azu or 0) or 0 ) * toradians, tonumber(radius or 0) or 0, tonumber(orgX or 0) or 0, tonumber(orgY or 0) or 0, tonumber(orgZ or 0) or 0
if vector3
then
return
vector3(
orgX + radius * math.sin( azu ) * math.cos( alt ),
orgY + radius * math.cos( azu ) * math.cos( alt ),
orgZ + radius * math.sin( alt )
)
end
end
function JUtils.ClampCircle(x,y,radius)
x = ( tonumber(x or 0) or 0 )
y = ( tonumber(y or 0) or 0 )
radius = ( tonumber(radius or 0) or 0 )
local d = math.sqrt(x*x+y*y)
d = radius / d
if d < 1 then x = x * (d/radius)*radius; y = y * (d/radius)*radius; end
return x,y
end
function JUtils.GetHashKey(strToHash)
if type(strToHash) == "number" then return strToHash; end;
return GetHashKeyPrev(tostring(strToHash or "") or "")%0x100000000;
end
GetHashKeyPrev = GetHashKeyPrev or GetHashKey
GetHashKey = JUtils.GetHashKey
function JUtils:FindNearestZone(position, table)
if type(table) ~= 'table' or type(position) ~= 'vector3' then return 999999; end
local closestZone,closestAction,closestDist,closestCoords
for _,zone in pairs(table) do
if zone.Positions then
for act,pos in pairs(zone.Positions) do
if type(pos) == 'vector3' then
local curDist = JUtils:GetVecDist(position, pos)
if not closestDist or curDist < closestDist then
closestZone,closestAction,closestDist,closestCoords = zone,act,curDist,pos
end
end
end
end
end
return closestZone,closestAction,closestDist,closestCoords
end
function string.tohex(s,chunkSize)
s = ( type(s) == "string" and s or type(s) == "nil" and "" or tostring(s) )
chunkSize = chunkSize or 2048
local rt = {}
string.tohex_sformat = ( string.tohex_sformat and string.tohex_chunkSize and string.tohex_chunkSize == chunkSize and string.tohex_sformat ) or string.rep("%02X",math.min(#s,chunkSize))
string.tohex_chunkSize = ( string.tohex_chunkSize and string.tohex_chunkSize == chunkSize and string.tohex_chunkSize or chunkSize )
for i = 1,#s,chunkSize do
rt[#rt+1] = string.format(string.tohex_sformat:sub(1,(math.min(#s-i+1,chunkSize)*4)),s:byte(i,i+chunkSize-1))
end
if #rt == 1 then return rt[1]
else return table.concat(rt,"")
end
end
function JUtils.GetXYDist(x1,y1,z1,x2,y2,z2)
return math.sqrt( ( (x1 or 0) - (x2 or 0) )*( (x1 or 0) - (x2 or 0) )+( (y1 or 0) - (y2 or 0) )*( (y1 or 0) - (y2 or 0) )+( (z1 or 0) - (z2 or 0) )*( (z1 or 0) - (z2 or 0) ) )
end
function JUtils:GetV2Dist(v1, v2)
if not v1 or not v2 or not v1.x or not v2.x or not v1.y or not v2.y then return 0; end
return math.sqrt( ( (v1.x or 0) - (v2.x or 0) )*( (v1.x or 0) - (v2.x or 0) )+( (v1.y or 0) - (v2.y or 0) )*( (v1.y or 0) - (v2.y or 0) ) )
end
function JUtils:GetVecDist(v1,v2)
if not v1 or not v2 or not v1.x or not v2.x then return 0; end
return math.sqrt( ( (v1.x or 0) - (v2.x or 0) )*( (v1.x or 0) - (v2.x or 0) )+( (v1.y or 0) - (v2.y or 0) )*( (v1.y or 0) - (v2.y or 0) )+( (v1.z or 0) - (v2.z or 0) )*( (v1.z or 0) - (v2.z or 0) ) )
end
function JUtils.GetCoordsInFrontOfCam(...)
local function Distance(v1,v2) if not v1 or not v1.x then return 0; end; v2 = v2 or vector3(0,0,0); return math.sqrt( ( (v1.x or 0) - (v2.x or 0) )*( (v1.x or 0) - (v2.x or 0) )+( (v1.y or 0) - (v2.y or 0) )*( (v1.y or 0) - (v2.y or 0) )+( (v1.z or 0) - (v2.z or 0) )*( (v1.z or 0) - (v2.z or 0) ) );end;
local coords = GetGameplayCamCoord()
local rot = GetGameplayCamRot(2)
local direction = vector3(( math.sin(rot.z*(3.141593/180))*-1)*math.abs(math.cos(rot.x)), math.cos(rot.z*(3.141593/180))*math.abs(math.cos(rot.x)), math.sin(rot.x*(3.141593/180)))
local distanceMod = Distance((coords-GetEntityCoords(PlayerPedId(),false) or vector3(0,0,0)))
local retTable = {}
if ( select("#",...) == 0 ) then return vector3( coords.x + ( 1*direction.x ), coords.y + ( 1*direction.y ), coords.z + ( 1*direction.z ) ) ; end
for k = 1,select("#",...) do
local distance = ( select(k,...) ) + distanceMod
if ( type(distance) == "number" )
then
if ( distance == 0 )
then retTable[k] = coords
else retTable[k] = vector3(coords.x+(distance*direction.x),coords.y+(distance*direction.y),coords.z+(distance*direction.z))
end
end
end
return unpack(retTable)
end
function JUtils.RotationToDirection(rot)
return vector3(( math.sin(rot.z*(3.141593/180))*-1)*math.abs(math.cos(rot.x)), math.cos(rot.z*(3.141593/180))*math.abs(math.cos(rot.x)), math.sin(rot.x*(3.141593/180)))
end
function JUtils.LoadModel(model, wait)
local hk = JUtils.GetHashKey(model)
if wait then
while not HasModelLoaded(hk) do
Citizen.Wait(0)
RequestModel(hk)
end
else
RequestModel(hk)
end
return true
end
function JUtils.ReleaseModel(model)
local hk = JUtils.GetHashKey(model)
if HasModelLoaded(hk) then
SetModelAsNoLongerNeeded(hk)
end
return true
end
function JUtils:LoadModelTable(table)
if type(table) ~= 'table' then return false; end
for k,v in pairs(table) do
if type(v) == 'string' then
local hk = JUtils.GetHashKey(v)
while not HasModelLoaded(hk) do
RequestModel(hk)
Citizen.Wait(0)
end
end
end
return true
end
function JUtils:ReleaseModelTable(table)
if type(table) ~= 'table' then return false; end
for k,v in pairs(table) do
if type(v) == 'string' then
local hk = JUtils.GetHashKey(v)
if HasModelLoaded(hk) then
SetModelAsNoLongerNeeded(hk)
end
end
end
return true
end
function JUtils.LoadWeaponTable(table)
if type(table) ~= 'table' then return false; end
for k,v in pairs(table) do
if type(v) == 'string' then
local hk = JUtils.GetHashKey(v)
while not HasWeaponAssetLoaded(hk) do
RequestWeaponAsset(hk)
Citizen.Wait(0)
end
end
end
return true
end
function JUtils.ReleaseWeaponTable(table)
if type(table) ~= 'table' then return false; end
for k,v in pairs(table) do
if type(v) == 'string' then
local hk = JUtils.GetHashKey(v)
if HasWeaponAssetLoaded(hk) then
RemoveWeaponAsset(hk)
end
end
end
return true
end
function JUtils.LoadAnimTable(table)
if type(table) ~= 'table' then return false; end
for k,v in pairs(table) do
if type(v) == 'string' then
while not HasAnimDictLoaded(v) do
RequestAnimDict(v)
Citizen.Wait(0)
end
end
end
return true
end
function JUtils.ReleaseAnimTable(table)
if type(table) ~= 'table' then return false; end
for k,v in pairs(table) do
if type(v) == 'string' then
if HasAnimDictLoaded(v) then
RemoveAnimDict(v)
end
end
end
return true
end
function JUtils:LoadAnimDict(dict)
if type(dict) ~= 'string' then return false; end
while not HasAnimDictLoaded(dict) do
RequestAnimDict(dict)
Citizen.Wait(0)
end
return true
end
function JUtils:ReleaseAnimDict(dict)
if type(dict) ~= 'string' then return false; end
if HasAnimDictLoaded(dict) then
RemoveAnimDict(dict)
end
return true
end
function JUtils.NetworkControlEntity(ent)
if type(ent) ~= 'number' then return false; end
while not NetworkHasControlOfEntity(ent) do
NetworkRequestControlOfEntity(ent)
Citizen.Wait(0)
end
return true
end
function JUtils.NetworkControlDoor(obj)
if type(obj) ~= 'number' then return false; end
while not NetworkHasControlOfDoor(obj) do
NetworkRequestControlOfDoor(obj)
Citizen.Wait(0)
end
return true
end
function JUtils.InRange(val, target, range)
if target + range > val and target - range < val then return true;
else return false; end
end
math.pow = math.pow or function(n,p) return (n or 1)^(p or 1) ; end
function JUtils:Round(val, scale)
val,scale = val or 0, scale or 0
return (
val < 0 and math.floor((math.abs(val*math.pow(10,scale))+0.5))*math.pow(10,((scale)*-1))*(-1)
or math.floor((math.abs(val*math.pow(10,scale))+0.5))*math.pow(10,((scale)*-1))
)
end
function JUtils:GetKeyPressed(key)
if not key then return false; end
if (IsDisabledControlJustPressed(0, self.Keys[key]) or IsControlJustPressed(0, self.Keys[key])) then return true
else return false; end
end
function JUtils:GetKeyHeld(key)
if not key then return false; end
if (IsDisabledControlPressed(0, self.Keys[key]) or IsControlPressed(0, self.Keys[key])) then return true
else return false; end
end