-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3_glua_ext.lua
454 lines (380 loc) · 10.7 KB
/
3_glua_ext.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
if glua_ext_loaded then return end
local R = debug.getregistry()
local ENTITY = R.Entity
local WEAPON = R.Weapon
local NPC = R.NPC
local PLAYER = R.Player
local PHYS = R.PhysObj
do
local THREAD = debug.getmetatable(coroutine.create(function() end))
local FUNC = debug.getmetatable(function() end)
do
local typeMeta = {
[debug.getmetatable("")] = 'string',
[debug.getmetatable(0)] = 'number',
[debug.getmetatable(false)] = 'boolean',
[FUNC] = 'function',
[THREAD] = "thread",
[R.Vector] = 'Vector',
[R.Angle] = 'Angle',
[R.VMatrix] = 'VMatrix',
[PLAYER] = 'Player',
[ENTITY] = 'Entity',
[WEAPON] = 'Weapon',
[NPC] = 'NPC',
[R.Color] = 'table',
[PHYS] = 'PhysObj',
[R.Vehicle] = 'Vehicle',
[R.IMaterial] = 'IMaterial',
[R.CTakeDamageInfo] = 'CTakeDamageInfo',
[R.CEffectData] = 'CEffectData',
[R.CMoveData] = 'CMoveData',
[R.CUserCmd] = 'CUserCmd',
[R.ConVar] = 'ConVar'
}
if CLIENT then
typeMeta[R.Panel] = 'Panel'
typeMeta[R.CSEnt] = 'CSEnt'
typeMeta[R.IMesh] = 'IMesh'
end
oldType = oldType or type
local getmetatable = getmetatable
local __type = oldType
function type(object)
if object == nil then return 'nil' end
return typeMeta[getmetatable(object)] or __type(object)
end
end
if jit.status() == true then
local TYPE_NIL = TYPE_NIL
local typeIDs = {
[debug.getmetatable("")] = TYPE_STRING,
[debug.getmetatable(0)] = TYPE_NUMBER,
[debug.getmetatable(false)] = TYPE_BOOL,
[FUNC] = TYPE_FUNCTION,
[THREAD] = TYPE_THREAD,
[R.Vector] = TYPE_VECTOR,
[R.Angle] = TYPE_ANGLE,
[R.VMatrix] = TYPE_MATRIX,
[PLAYER] = TYPE_ENTITY,
[ENTITY] = TYPE_ENTITY,
[WEAPON] = TYPE_ENTITY,
[NPC] = TYPE_ENTITY,
[R.Color] = TYPE_COLOR,
[PHYS] = TYPE_PHYSOBJ,
[R.Vehicle] = TYPE_ENTITY,
[R.IMaterial] = TYPE_MATERIAL,
[R.CTakeDamageInfo] = TYPE_DAMAGEINFO,
[R.CEffectData] = TYPE_EFFECTDATA,
[R.CMoveData] = TYPE_MOVEDATA,
[R.CUserCmd] = TYPE_USERCMD,
[R.ConVar] = TYPE_CONVAR
}
if CLIENT then
typeIDs[R.Panel] = TYPE_PANEL
typeIDs[R.CSEnt] = TYPE_ENTITY
typeIDs[R.IMesh] = TYPE_IMESH
end
oldTypeID = oldTypeID or TypeID
local __oldType = oldTypeID
local getmetatable = getmetatable
function TypeID(object)
if object == nil then return TYPE_NIL end
return typeIDs[getmetatable(object)] or __oldType(object)
end
else
local TYPE_NIL = TYPE_NIL
oldTypeID = oldTypeID or TypeID
local __oldType = oldTypeID
function TypeID(object)
if object == nil then return TYPE_NIL end
return __oldType(object)
end
end
end
---- 1 000 000
-- table.remove:
-- sum = 12.332
-- avg = 0.12332
-- median = 0.010999999999999
-- table.Remove:
-- sum = 1.041
-- avg = 0.01041
-- median = 0.009999999999998
function table.Remove(tbl, index)
local c = #tbl
-- if index > c then
-- index = c
-- end
local lastValue = tbl[index]
if index >= c or c == 1 then
tbl[index] = nil
else
tbl[index] = tbl[c]
tbl[c] = nil
end
return lastValue
end
do
local strSub = string.sub
-- string.GetPathFromFilename = function(path)
-- local i = #path
-- ::iter::
-- local c = strSub( path, i, i )
-- if ( c == "/" or c == "\\" ) then return strSub( path, 1, i ) end
-- i = i - 1
-- if i > 0 then goto iter end
-- return path
-- end
-- string.Explode:
-- sum = 2.867
-- avg = 0.02867
-- median = 0.027500000000003
-- string.SplitString:
-- sum = 0.69100000000003
-- avg = 0.0069100000000003
-- median = 0.0059999999999718
local strLen = string.len
string.SplitString = function(separator, str)
local results = {}
local index, lastPos = 1, 1
local i, iMax = 1, strLen(str)
local lastArg
::iter::
if strSub(str, i, i) == separator then
lastArg = strSub(str, lastPos, i - 1)
-- #lastArg > 0 then
results[index] = lastArg
index = index + 1
-- end
lastPos = i + 1
end
i = i + 1
if i <= iMax then goto iter end
lastArg = strSub(str, lastPos)
if lastArg ~= "" then results[index] = lastArg end
return results
end
end
do
---- 1 000 000
-- BlastDamageSqr:
-- sum = 0.13600000000002
-- avg = 0.13600000000002
-- median = 0.13600000000002
-- BlastDamage:
-- sum = 0.858
-- avg = 0.858
-- median = 0.858
local trace = {}
local traceData = {output = trace, filter = {}}
function util.BlastDamageSqr(inflictor, attacker, damageOrigin, damageRadius, damage)
if damage == 0 then return end
local _, players = player.Iterator()
local ply = NULL
local dmg = 0
local info = DamageInfo()
info:SetAttacker(attacker)
info:SetInflictor(inflictor)
info:SetDamageType(DMG_BLAST)
info:SetDamageForce(vector_up)
info:SetDamagePosition(damageOrigin)
traceData.start = damageOrigin
traceData.filter[1] = inflictor
local sqr = damageRadius * damageRadius
local dist = 0
for i = 1, #players do
ply = players[i]
if ply then
dist = ply:GetPos():DistToSqr(damageOrigin)
if dist == 0 or dist <= sqr then
dmg = dist == 0 and damage or 0
if dmg == 0 then
traceData.filter[2] = ply
traceData.endpos = ply:NearestPoint(damageOrigin)
util.TraceLine(traceData)
if not trace.Hit or trace.Entity == ply then
dmg = ((damageRadius - traceData.endpos:Distance(damageOrigin)) / damageRadius) * damage
end
end
if dmg > 0 then
info:SetDamage(dmg)
ply:TakeDamageInfo(info)
end
end
end
end
end
end
--- type
local returnFalse = function() return false end
local returnTrue = function() return true end
ENTITY.IsPlayer = returnFalse
ENTITY.IsWeapon = returnFalse
ENTITY.IsNPC = returnFalse
ENTITY.IsNextbot = returnFalse
WEAPON.IsPlayer = returnFalse
WEAPON.IsWeapon = returnTrue
WEAPON.IsNPC = returnFalse
WEAPON.IsNextbot = returnFalse
NPC.IsPlayer = returnFalse
NPC.IsWeapon = returnFalse
NPC.IsNPC = returnTrue
NPC.IsNextbot = returnFalse
PLAYER.IsWeapon = returnFalse
PLAYER.IsNPC = returnFalse
PLAYER.IsNextbot = returnFalse
PLAYER.IsPlayer = returnTrue
PHYS.IsWeapon = returnFalse
PHYS.IsNPC = returnFalse
PHYS.IsNextbot = returnFalse
PHYS.IsPlayer = returnFalse
if (SERVER) then
local NEXTBOT = R.NextBot
NEXTBOT.IsPlayer = returnFalse
NEXTBOT.IsWeapon = returnFalse
NEXTBOT.IsNPC = returnFalse
NEXTBOT.IsNextbot = returnTrue
end
do
local inext = ipairs({})
local EntityCache, EntityLen = nil, 0
local PlayerCache, PlayerLen = nil, 0
-- alias player.GetCount but faster
function player.Count()
if PlayerCache == nil then
PlayerCache = player.GetAll()
PlayerLen = #PlayerCache
end
return PlayerLen
end
-- alias ents.GetCount but faster
function ents.Count()
if EntityCache == nil then
EntityCache = ents.GetAll()
EntityLen = #EntityCache
end
return EntityLen
end
function player.Iterator()
if PlayerCache == nil then
PlayerCache = player.GetAll()
PlayerLen = #PlayerCache
end
return inext, PlayerCache, 0
end
function ents.Iterator()
if EntityCache == nil then
EntityCache = ents.GetAll()
EntityLen = #EntityCache
end
return inext, EntityCache, 0
end
function player.All()
if PlayerCache == nil then
PlayerCache = player.GetAll()
PlayerLen = #PlayerCache
end
return PlayerLen, PlayerCache
end
function ents.All()
if EntityCache == nil then
EntityCache = ents.GetAll()
EntityLen = #EntityCache
end
return EntityLen, EntityCache
end
local function InvalidateEntityCache(ent)
if ent:IsPlayer() then PlayerCache, PlayerLen = nil, 0 end
EntityCache, EntityLen = nil, 0
end
hook.Remove( "OnEntityCreated", "player.Iterator" )
hook.Remove( "EntityRemoved", "player.Iterator" )
hook.Add( "OnEntityCreated", "ents.Iterator", InvalidateEntityCache )
hook.Add( "EntityRemoved", "ents.Iterator", InvalidateEntityCache )
-- example ::
-- for k, v in player.Iterator() do print(k, v) end
-- local playerLen, players = player.All()
-- for i = 1, playerLen do print(i, players[i]) end
end
do
local FrameNumber = FrameNumber
local TraceLine = util.TraceLine
local VECTOR = R.Vector
local V_Add, V_Mul, V_Set = VECTOR.Add, VECTOR.Mul, VECTOR.Set
do
local LastPlayerTrace
local output = {}
-- Thanks @GoodOldBrick
local START_VECTOR, END_VECTOR, DIR_VECTOR = Vector(), Vector(), Vector()
local trace = { output = output, start = START_VECTOR, endpos = END_VECTOR, filter = NULL }
local GetAimVector, EyePos = PLAYER.GetAimVector, ENTITY.EyePos
function PLAYER:GetEyeTrace(distance)
if (CLIENT) then
local framenum = FrameNumber()
if (LastPlayerTrace == framenum) then return output end
LastPlayerTrace = framenum
end
V_Set(START_VECTOR, EyePos(self))
V_Set(END_VECTOR, START_VECTOR)
V_Set(DIR_VECTOR, GetAimVector(self))
V_Mul(DIR_VECTOR, distance or 32768)
V_Add(END_VECTOR, DIR_VECTOR)
trace.filter = self
TraceLine(trace)
return output
end
end
do
local LastPlayerAimTrace
local output = {}
local START_VECTOR, END_VECTOR, DIR_VECTOR = Vector(), Vector(), Vector()
local trace = { output = output, start = START_VECTOR, endpos = END_VECTOR, filter = NULL }
local EyeAngles, Forward, EyePos = ENTITY.EyeAngles, R.Angle.Forward, ENTITY.EyePos
function PLAYER:GetEyeTraceNoCursor(distance)
if (CLIENT) then
local framenum = FrameNumber()
if (LastPlayerAimTrace == framenum) then return output end
LastPlayerAimTrace = framenum
end
V_Set(START_VECTOR, EyePos(self))
V_Set(END_VECTOR, START_VECTOR)
V_Set(DIR_VECTOR, Forward(EyeAngles(self)))
V_Mul(DIR_VECTOR, distance or 32768)
V_Add(END_VECTOR, DIR_VECTOR)
trace.filter = self
TraceLine(trace)
return output
end
end
end
MAX_PLAYER_BITS = math.ceil( math.log( 1 + game.MaxPlayers() ) / math.log( 2 ) )
do
local cachergb = {}
local cachehex = {}
for i = 0, 255 do
local c = string.format("%02x", i)
cachergb[c] = i
cachehex[i] = c
end
util.RGBToHex = function(color)
return ( cachehex[color.r] or "ff" ) .. (cachehex[color.g] or "ff" ) .. ( cachehex[color.b] or "ff" ) .. "ff"
end
util.RGBAToHex = function(color)
return ( cachehex[color.r] or "ff" ) .. (cachehex[color.g] or "ff" ) .. ( cachehex[color.b] or "ff" ) .. ( cachehex[color.a] or "ff" )
end
local sub = string.sub
util.HexToRGB = function(hex)
return cachergb[sub(hex,1,2)], cachergb[sub(hex,3,4)], cachergb[sub(hex,5,6)]
end
util.HexToRGBA = function(hex)
return cachergb[sub(hex,1,2)], cachergb[sub(hex,3,4)], cachergb[sub(hex,5,6)], cachergb[sub(hex,7,8)]
end
-- :: Example ::
-- TOOL.ClientConVar[ "hex" ] = "ffffffff"
-- ply:ConCommand( "remover_hex " .. util.RGBAToHex( color_white ) )
-- local hex = LocalPlayer():GetInfo("remover_hex")
-- if not hex then hex = "ffffffff" end
-- local r,g,b,a = util.HexToRGBA(hex)
end
glua_ext_loaded = true