refactor(resource/zoneCreator): general cleanup

- improved spacing
- remove redundant thread
- fix chat suggestion registration and change to export
This commit is contained in:
DokaDoka
2023-01-07 17:35:42 +11:00
parent ccf29592fb
commit 4794af7ae0

View File

@@ -4,7 +4,7 @@ local zoneType, step, xCoord, yCoord, zCoord, heading, height, width, length
local steps = {{0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 25, 50, 100}, {0.25, 0.5, 1, 2.5, 5, 15, 30, 45, 60, 90, 180}} local steps = {{0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 25, 50, 100}, {0.25, 0.5, 1, 2.5, 5, 15, 30, 45, 60, 90, 180}}
local points = {} local points = {}
local format = 'array' local format = 'array'
local displayModes = {'basic', 'walls', 'axes', 'both'} local displayModes = {'basic', 'walls', 'axes', 'full'}
local displayMode = 1 local displayMode = 1
local minCheck = steps[1][1] / 2 local minCheck = steps[1][1] / 2
local lastZone = {} local lastZone = {}
@@ -12,7 +12,7 @@ local alignMovementWithCamera = false
local useLastZoneFalsyInputs = {['0'] = true, [''] = true, ['false'] = true, ['nil'] = true} local useLastZoneFalsyInputs = {['0'] = true, [''] = true, ['false'] = true, ['nil'] = true}
local function firstToUpper(str) local function firstToUpper(str)
return (str:gsub("^%l", string.upper)) return (str:gsub('^%l', string.upper))
end end
local function updateText() local function updateText()
@@ -158,7 +158,7 @@ local function getRelativePos(origin, point, theta)
local x = math.floor(((pX * cosTheta - pY * sinTheta) + origin.x) * 100 + 0.0) / 100 local x = math.floor(((pX * cosTheta - pY * sinTheta) + origin.x) * 100 + 0.0) / 100
local y = math.floor(((pX * sinTheta + pY * cosTheta) + origin.y) * 100 + 0.0) / 100 local y = math.floor(((pX * sinTheta + pY * cosTheta) + origin.y) * 100 + 0.0) / 100
return x, y return x, y
end end
local isFivem = cache.game == 'fivem' local isFivem = cache.game == 'fivem'
local controls = { local controls = {
@@ -189,10 +189,8 @@ local function startCreator(arg, useLast)
Wait(0) Wait(0)
if IsDisabledControlJustReleased(0, 73) then -- x if IsDisabledControlJustReleased(0, 73) then -- x
if creatorActive then
controlsActive = not controlsActive controlsActive = not controlsActive
end end
end
if IsDisabledControlJustReleased(0, 301) then -- m if IsDisabledControlJustReleased(0, 301) then -- m
if creatorActive then if creatorActive then
@@ -231,6 +229,7 @@ local function startCreator(arg, useLast)
EnableControlAction(0, controls['INPUT_LOOK_LR'], true) EnableControlAction(0, controls['INPUT_LOOK_LR'], true)
EnableControlAction(0, controls['INPUT_LOOK_UD'], true) EnableControlAction(0, controls['INPUT_LOOK_UD'], true)
EnableControlAction(0, controls['INPUT_MP_TEXT_CHAT_ALL'], true) EnableControlAction(0, controls['INPUT_MP_TEXT_CHAT_ALL'], true)
local change = false local change = false
local lStep = steps[1][step] local lStep = steps[1][step]
local rStep = steps[2][step] local rStep = steps[2][step]
@@ -252,6 +251,7 @@ local function startCreator(arg, useLast)
elseif IsDisabledControlJustReleased(0, 16) then -- scroll down elseif IsDisabledControlJustReleased(0, 16) then -- scroll down
if IsDisabledControlPressed(0, 21) then -- shift held down if IsDisabledControlPressed(0, 21) then -- shift held down
change = true change = true
if height - lStep > lStep then if height - lStep > lStep then
height -= lStep height -= lStep
elseif height - lStep > 0 then elseif height - lStep > 0 then
@@ -259,6 +259,7 @@ local function startCreator(arg, useLast)
end end
elseif IsDisabledControlPressed(0, 36) then -- ctrl held down elseif IsDisabledControlPressed(0, 36) then -- ctrl held down
change = true change = true
if width - lStep > lStep then if width - lStep > lStep then
width -= lStep width -= lStep
elseif width - lStep > 0 then elseif width - lStep > 0 then
@@ -266,6 +267,7 @@ local function startCreator(arg, useLast)
end end
elseif IsDisabledControlPressed(0, 19) then -- alt held down elseif IsDisabledControlPressed(0, 19) then -- alt held down
change = true change = true
if length - lStep > lStep then if length - lStep > lStep then
length -= lStep length -= lStep
elseif length - lStep > 0 then elseif length - lStep > 0 then
@@ -277,108 +279,139 @@ local function startCreator(arg, useLast)
end end
elseif IsDisabledControlJustReleased(0, 32) then -- w elseif IsDisabledControlJustReleased(0, 32) then -- w
change = true change = true
if alignMovementWithCamera then if alignMovementWithCamera then
local newX, newY = getRelativePos(vec2(xCoord, yCoord), vec2(xCoord, yCoord + lStep), GetGameplayCamRot(2).z) local newX, newY = getRelativePos(vec2(xCoord, yCoord), vec2(xCoord, yCoord + lStep), GetGameplayCamRot(2).z)
if math.abs(newX) < minCheck then if math.abs(newX) < minCheck then
newX = 0.0 newX = 0.0
end end
if math.abs(newY) < minCheck then if math.abs(newY) < minCheck then
newY = 0.0 newY = 0.0
end end
xCoord = newX xCoord = newX
yCoord = newY yCoord = newY
else else
local newValue = yCoord + lStep local newValue = yCoord + lStep
if math.abs(newValue) < minCheck then if math.abs(newValue) < minCheck then
newValue = 0.0 newValue = 0.0
end end
yCoord = newValue yCoord = newValue
end end
elseif IsDisabledControlJustReleased(0, 33) then -- s elseif IsDisabledControlJustReleased(0, 33) then -- s
change = true change = true
if alignMovementWithCamera then if alignMovementWithCamera then
local newX, newY = getRelativePos(vec2(xCoord, yCoord), vec2(xCoord, yCoord - lStep), GetGameplayCamRot(2).z) local newX, newY = getRelativePos(vec2(xCoord, yCoord), vec2(xCoord, yCoord - lStep), GetGameplayCamRot(2).z)
if math.abs(newX) < minCheck then if math.abs(newX) < minCheck then
newX = 0.0 newX = 0.0
end end
if math.abs(newY) < minCheck then if math.abs(newY) < minCheck then
newY = 0.0 newY = 0.0
end end
xCoord = newX xCoord = newX
yCoord = newY yCoord = newY
else else
local newValue = yCoord - lStep local newValue = yCoord - lStep
if math.abs(newValue) < minCheck then if math.abs(newValue) < minCheck then
newValue = 0.0 newValue = 0.0
end end
yCoord = newValue yCoord = newValue
end end
elseif IsDisabledControlJustReleased(0, 35) then -- d elseif IsDisabledControlJustReleased(0, 35) then -- d
change = true change = true
if alignMovementWithCamera then if alignMovementWithCamera then
local newX, newY = getRelativePos(vec2(xCoord, yCoord), vec2(xCoord + lStep, yCoord), GetGameplayCamRot(2).z) local newX, newY = getRelativePos(vec2(xCoord, yCoord), vec2(xCoord + lStep, yCoord), GetGameplayCamRot(2).z)
if math.abs(newX) < minCheck then if math.abs(newX) < minCheck then
newX = 0.0 newX = 0.0
end end
if math.abs(newY) < minCheck then if math.abs(newY) < minCheck then
newY = 0.0 newY = 0.0
end end
xCoord = newX xCoord = newX
yCoord = newY yCoord = newY
else else
local newValue = xCoord + lStep local newValue = xCoord + lStep
if math.abs(newValue) < minCheck then if math.abs(newValue) < minCheck then
newValue = 0.0 newValue = 0.0
end end
xCoord = newValue xCoord = newValue
end end
elseif IsDisabledControlJustReleased(0, 34) then -- a elseif IsDisabledControlJustReleased(0, 34) then -- a
change = true change = true
if alignMovementWithCamera then if alignMovementWithCamera then
local newX, newY = getRelativePos(vec2(xCoord, yCoord), vec2(xCoord - lStep, yCoord), GetGameplayCamRot(2).z) local newX, newY = getRelativePos(vec2(xCoord, yCoord), vec2(xCoord - lStep, yCoord), GetGameplayCamRot(2).z)
if math.abs(newX) < minCheck then if math.abs(newX) < minCheck then
newX = 0.0 newX = 0.0
end end
if math.abs(newY) < minCheck then if math.abs(newY) < minCheck then
newY = 0.0 newY = 0.0
end end
xCoord = newX xCoord = newX
yCoord = newY yCoord = newY
else else
local newValue = xCoord - lStep local newValue = xCoord - lStep
if math.abs(newValue) < minCheck then if math.abs(newValue) < minCheck then
newValue = 0.0 newValue = 0.0
end end
xCoord = newValue xCoord = newValue
end end
elseif IsDisabledControlJustReleased(0, 45) then -- r elseif IsDisabledControlJustReleased(0, 45) then -- r
change = true change = true
local newValue = zCoord + lStep local newValue = zCoord + lStep
if math.abs(newValue) < minCheck then if math.abs(newValue) < minCheck then
newValue = 0.0 newValue = 0.0
end end
zCoord = newValue zCoord = newValue
elseif IsDisabledControlJustReleased(0, 23) then -- f elseif IsDisabledControlJustReleased(0, 23) then -- f
change = true change = true
local newValue = zCoord - lStep local newValue = zCoord - lStep
if math.abs(newValue) < minCheck then if math.abs(newValue) < minCheck then
newValue = 0.0 newValue = 0.0
end end
zCoord = newValue zCoord = newValue
elseif IsDisabledControlJustReleased(0, 38) then -- e elseif IsDisabledControlJustReleased(0, 38) then -- e
change = true change = true
heading -= rStep heading -= rStep
if heading < 0 then if heading < 0 then
heading += 360 heading += 360
end end
elseif IsDisabledControlJustReleased(0, 44) then -- q elseif IsDisabledControlJustReleased(0, 44) then -- q
change = true change = true
heading += rStep heading += rStep
if heading >= 360 then if heading >= 360 then
heading -= 360 heading -= 360
end end
elseif IsDisabledControlJustReleased(0, 47) then -- g elseif IsDisabledControlJustReleased(0, 47) then -- g
change = true change = true
if displayMode == #displayModes then if displayMode == #displayModes then
displayMode = 1 displayMode = 1
else else
@@ -386,6 +419,7 @@ local function startCreator(arg, useLast)
end end
elseif IsDisabledControlJustReleased(0, 22) then -- space elseif IsDisabledControlJustReleased(0, 22) then -- space
change = true change = true
if zoneType == 'poly' then if zoneType == 'poly' then
points[#points + 1] = vec2(xCoord, yCoord) points[#points + 1] = vec2(xCoord, yCoord)
end end
@@ -412,11 +446,14 @@ RegisterCommand('zone', function(source, args, rawCommand)
lib.notify({title = 'Invalid zone type', type = 'error'}) lib.notify({title = 'Invalid zone type', type = 'error'})
return return
end end
if creatorActive then if creatorActive then
lib.notify({title = 'Already creating a zone', type = 'error'}) lib.notify({title = 'Already creating a zone', type = 'error'})
return return
end end
local useLast = args[2] and not useLastZoneFalsyInputs[args[2]] local useLast = args[2] and not useLastZoneFalsyInputs[args[2]]
if useLast then if useLast then
if not lastZone[args[1]] then if not lastZone[args[1]] then
lib.notify({title = 'No last zone to duplicate', type = 'error'}) lib.notify({title = 'No last zone to duplicate', type = 'error'})
@@ -427,11 +464,14 @@ RegisterCommand('zone', function(source, args, rawCommand)
return return
end end
end end
CreateThread(function()
startCreator(args[1], useLast) startCreator(args[1], useLast)
end)
end, true) end, true)
TriggerEvent('chat:addSuggestion', '/zone', 'Starts creation of the specified zone', { CreateThread(function()
{ name = 'zoneType', help = 'poly, box, sphere' }, { name = 'useLast', help = 'duplicates the last created zone of the specified type (box and sphere only, optional)'} Wait(1000)
}) exports.chat:addSuggestion('/zone', 'Starts creation of the specified zone', {
{ name = 'zoneType', help = 'poly, box, sphere' },
{ name = 'useLast', help = 'duplicates the last created zone of the specified type (box and sphere only, optional)' }
})
end)