Skip to content

Commit 7a0b640

Browse files
committed
Decouple vehicles from AMX instances
1 parent 29cf87b commit 7a0b640

File tree

5 files changed

+59
-66
lines changed

5 files changed

+59
-66
lines changed

amx/client/client.lua

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,19 @@ local BOATS = {
2222
[454] = true
2323
}
2424

25+
local defaultEmptyTableMt = {
26+
__index = function(t, k)
27+
local info = {}
28+
t[k] = info
29+
return t[k]
30+
end
31+
}
32+
2533
g_AMXs = {}
2634

35+
g_Vehicles = {}
36+
setmetatable(g_Vehicles, defaultEmptyTableMt)
37+
2738
local screenWidth, screenHeight = guiGetScreenSize()
2839

2940
addEventHandler('onClientResourceStart', resourceRoot,
@@ -49,16 +60,6 @@ end
4960
function addAMX(name, type)
5061
g_AMXs[name] = { name = name, type = type, vehicles = {}, playerobjects = {}, textdraws = {}, textlabels = {}, menus = {}, blips = {} }
5162
-- textdraws = { id = { text = text, color = color, align = 1|2|3, x = x, y = y, boxsize = {width, height}, parts={{x=x,y=y,color=color,text=text},...} }, ... }
52-
setmetatable(
53-
g_AMXs[name].vehicles,
54-
{
55-
__index = function(t, k)
56-
local vehInfo = {}
57-
t[k] = vehInfo
58-
return t[k]
59-
end
60-
}
61-
)
6263
if type == 'gamemode' then
6364
setTime(12, 0)
6465
end
@@ -589,11 +590,11 @@ function SetPlayerPosFindZ(amxName, x, y, z)
589590
end
590591

591592
function SetVehicleParamsForPlayer(vehicle, isObjective, doorsLocked)
592-
local amx, vehID = getElemAMX(vehicle), getElemID(vehicle)
593-
if not amx or not vehID then
593+
local vehID = getElemID(vehicle)
594+
if not vehID then
594595
return
595596
end
596-
local vehInfo = amx.vehicles[vehID]
597+
local vehInfo = g_Vehicles[vehID]
597598
if isObjective then
598599
if vehInfo.blip then
599600
destroyElement(vehInfo.blip)
@@ -672,8 +673,8 @@ addEventHandler('onClientElementStreamIn', root,
672673
vehicleDrops[source] = { timer = timer, tries = 0 }
673674
end
674675

675-
local amx, vehID = getElemAMX(source), getElemID(source)
676-
local vehInfo = amx and vehID and amx.vehicles[vehID]
676+
local vehID = getElemID(source)
677+
local vehInfo = vehID and g_Vehicles[vehID]
677678
if vehInfo and not vehInfo.blip then
678679
vehInfo.blip = createBlipAttachedTo(source, 0, 1, 136, 136, 136, 150, 0, 500)
679680
setElementParent(vehInfo.blip, source)
@@ -690,8 +691,8 @@ addEventHandler('onClientElementStreamIn', root,
690691
addEventHandler('onClientElementStreamOut', root,
691692
function()
692693
if getElementType(source) ~= 'vehicle' then
693-
local amx, vehID = getElemAMX(source), getElemID(source)
694-
local vehInfo = amx and vehID and amx.vehicles[vehID]
694+
local vehID = getElemID(source)
695+
local vehInfo = vehID and g_Vehicles[vehID]
695696
if vehInfo and vehInfo.blip and not vehInfo.blippersistent then
696697
if isElement(vehInfo.blip) then
697698
destroyElement(vehInfo.blip)
@@ -717,7 +718,7 @@ addEventHandler('onClientVehicleStartEnter', root,
717718
)
718719

719720
function DestroyVehicle(amxName, vehID)
720-
g_AMXs[amxName].vehicles[vehID] = nil
721+
g_Vehicles[vehID] = nil
721722
end
722723

723724
-----------------------------

amx/server/amx.lua

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ function loadAMX(fileName, res)
121121
g_LoadedAMXs[amx.name] = amx
122122

123123
amx.pickups = {}
124-
amx.vehicles = {}
125124
amx.objects = {}
126125
amx.playerobjects = {}
127126
amx.timers = {}
@@ -162,6 +161,16 @@ function loadAMX(fileName, res)
162161
end
163162
addEvent('onAMXStart')
164163

164+
function destroyGlobalElements()
165+
for i,vehinfo in pairs(g_Vehicles) do
166+
if vehinfo.respawntimer then
167+
killTimer(vehinfo.respawntimer)
168+
vehinfo.respawntimer = nil
169+
removeElem(g_Vehicles, vehinfo.elem)
170+
end
171+
end
172+
end
173+
165174
function unloadAMX(amx, notifyClient)
166175
outputDebugString('Unloading ' .. amx.name .. '.amx')
167176

@@ -175,20 +184,13 @@ function unloadAMX(amx, notifyClient)
175184

176185
amxUnload(amx.cptr)
177186

178-
for i,elemtype in ipairs({'pickups', 'vehicles', 'objects', 'gangzones','bots','markers','textlabels','textdraws'}) do
187+
for i,elemtype in ipairs({'pickups', 'objects', 'gangzones','bots','markers','textlabels','textdraws'}) do
179188
for id,data in pairs(amx[elemtype]) do
180189
removeElem(amx, elemtype, data.elem)
181190
destroyElement(data.elem)
182191
end
183192
end
184193

185-
for i,vehinfo in pairs(amx.vehicles) do
186-
if vehinfo.respawntimer then
187-
killTimer(vehinfo.respawntimer)
188-
vehinfo.respawntimer = nil
189-
end
190-
end
191-
192194
table.each(amx.timers, killTimer)
193195
table.each(amx.files, fileClose)
194196

amx/server/events.lua

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -385,26 +385,22 @@ function respawnStaticVehicle(vehicle)
385385
if not isElement(vehicle) then
386386
return
387387
end
388-
local amx, vehID = getElemAMX(vehicle), getElemID(vehicle)
389-
if not amx or not amx.vehicles[vehID] then
388+
local vehID = getElemID(vehicle)
389+
if not g_Vehicles[vehID] then
390390
return
391391
end
392-
if isTimer(amx.vehicles[vehID].respawntimer) then
393-
killTimer(amx.vehicles[vehID].respawntimer)
392+
if isTimer(g_Vehicles[vehID].respawntimer) then
393+
killTimer(g_Vehicles[vehID].respawntimer)
394394
end
395-
amx.vehicles[vehID].respawntimer = nil
396-
local spawninfo = amx.vehicles[vehID].spawninfo
395+
g_Vehicles[vehID].respawntimer = nil
396+
local spawninfo = g_Vehicles[vehID].spawninfo
397397
spawnVehicle(vehicle, spawninfo.x, spawninfo.y, spawninfo.z, 0, 0, spawninfo.angle)
398398
procCallInternal(amx, 'OnVehicleSpawn', vehID)
399399
end
400400

401401
addEventHandler('onVehicleEnter', root,
402402
function(player, seat, jacked)
403403
local vehID = getElemID(source)
404-
local amx = getElemAMX(source)
405-
if not amx then
406-
return
407-
end
408404
if isPed(player) then
409405
local pedID = getElemID(player)
410406
g_Bots[pedID].vehicle = source
@@ -415,14 +411,14 @@ addEventHandler('onVehicleEnter', root,
415411
g_Players[playerID].vehicle = source
416412
setPlayerState(player, seat == 0 and PLAYER_STATE_DRIVER or PLAYER_STATE_PASSENGER)
417413

418-
if amx.vehicles[vehID] and amx.vehicles[vehID].respawntimer then
419-
killTimer(amx.vehicles[vehID].respawntimer)
420-
amx.vehicles[vehID].respawntimer = nil
414+
if g_Vehicles[vehID] and g_Vehicles[vehID].respawntimer then
415+
killTimer(g_Vehicles[vehID].respawntimer)
416+
g_Vehicles[vehID].respawntimer = nil
421417
end
422418

423419
if ManualVehEngineAndLights then
424420
if (getVehicleType(source) ~= "Plane" and getVehicleType(source) ~= "Helicopter") then
425-
setVehicleEngineState(source, amx.vehicles[vehID].engineState)
421+
setVehicleEngineState(source, g_Vehicles[vehID].engineState)
426422
end
427423
end
428424
end
@@ -447,11 +443,7 @@ addEventHandler('onVehicleStartEnter', root,
447443

448444
addEventHandler('onVehicleExit', root,
449445
function(player, seat, jacker)
450-
local amx = getElemAMX(source)
451446
local vehID = getElemID(source)
452-
if not amx then
453-
return
454-
end
455447

456448
if isPed(player) then
457449
local pedID = getElemID(player)
@@ -469,11 +461,11 @@ addEventHandler('onVehicleExit', root,
469461
return
470462
end
471463
end
472-
if amx.vehicles[vehID] and amx.vehicles[vehID].respawntimer then
473-
killTimer(amx.vehicles[vehID].respawntimer)
474-
amx.vehicles[vehID].respawntimer = nil
464+
if g_Vehicles[vehID] and g_Vehicles[vehID].respawntimer then
465+
killTimer(g_Vehicles[vehID].respawntimer)
466+
g_Vehicles[vehID].respawntimer = nil
475467
end
476-
amx.vehicles[vehID].respawntimer = setTimer(respawnStaticVehicle, amx.vehicles[vehID].respawndelay, 1, source)
468+
g_Vehicles[vehID].respawntimer = setTimer(respawnStaticVehicle, g_Vehicles[vehID].respawndelay, 1, source)
477469
end
478470
)
479471

@@ -503,19 +495,15 @@ addEventHandler('onVehicleStartExit', root,
503495

504496
addEventHandler('onVehicleExplode', root,
505497
function()
506-
local amx = getElemAMX(source)
507498
local vehID = getElemID(source)
508-
if not amx then
509-
return
510-
end
511499

512500
procCallOnAll('OnVehicleDeath', vehID, 0) -- NOES, MY VEHICLE DIED
513501

514-
if amx.vehicles[vehID].respawntimer then
515-
killTimer(amx.vehicles[vehID].respawntimer)
516-
amx.vehicles[vehID].respawntimer = nil
502+
if g_Vehicles[vehID].respawntimer then
503+
killTimer(g_Vehicles[vehID].respawntimer)
504+
g_Vehicles[vehID].respawntimer = nil
517505
end
518-
amx.vehicles[vehID].respawntimer = setTimer(respawnStaticVehicle, amx.vehicles[vehID].respawndelay, 1, source)
506+
g_Vehicles[vehID].respawntimer = setTimer(respawnStaticVehicle, g_Vehicles[vehID].respawndelay, 1, source)
519507
end
520508
)
521509

amx/server/syscalls.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,19 +253,19 @@ function AddStaticVehicleEx(amx, model, x, y, z, angle, color1, color2, respawnD
253253

254254
setVehicleColor(vehicle, color1, color2, 0, 0)
255255
end
256-
local vehID = addElem(amx, 'vehicles', vehicle)
256+
local vehID = addElem(g_Vehicles, vehicle)
257257
if respawnDelay < 0 then
258258
respawnDelay = 120
259259
end
260-
amx.vehicles[vehID].respawndelay = respawnDelay*1000
261-
amx.vehicles[vehID].spawninfo = { x = x, y = y, z = z, angle = angle }
260+
g_Vehicles[vehID].respawndelay = respawnDelay*1000
261+
g_Vehicles[vehID].spawninfo = { x = x, y = y, z = z, angle = angle }
262262
if ManualVehEngineAndLights then
263263
if (getVehicleType(vehicle) ~= "Plane" and getVehicleType(vehicle) ~= "Helicopter") then
264264
setVehicleEngineState(vehicle, false)
265265
for i=0, 4 do
266266
setVehicleLightState(vehicle, i, 0)
267267
end
268-
amx.vehicles[vehID].engineState = false
268+
g_Vehicles[vehID].engineState = false
269269
end
270270
end
271271
return vehID
@@ -433,7 +433,7 @@ end
433433

434434
function DestroyVehicle(amx, vehicle)
435435
clientCall(root, 'DestroyVehicle', amx.name, getElemID(vehicle))
436-
removeElem(amx, 'vehicles', vehicle)
436+
removeElem(g_Vehicles, vehicle)
437437
local vehicleID = getElemID(vehicle)
438438
for i,playerdata in pairs(g_Players) do
439439
playerdata.streamedVehicles[vehicleID] = nil

amx/server/util.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,6 @@ function addElem(amx, listname, elem)
348348
globList = _G['g_' .. listname:sub(1, 1):upper() .. listname:sub(2)]
349349
if globList then
350350
id = 0
351-
-- vehicles in sa-mp start at ID 1
352-
if listname == 'vehicles' then
353-
id = 1
354-
end
355351
while globList[id] do
356352
id = id + 1
357353
end
@@ -361,6 +357,12 @@ function addElem(amx, listname, elem)
361357

362358
if not id then
363359
id = 0
360+
361+
-- vehicles in sa-mp start at ID 1
362+
if list == g_Vehicles then
363+
id = 1
364+
end
365+
364366
while list[id] do
365367
id = id + 1
366368
end

0 commit comments

Comments
 (0)