itroducing placeable object + gizmo mix + raycasting

NOTE: Did not change Placeable. Should not impact current resources

rebound_entities are still WIP
This commit is contained in:
gononono64 committed 2025-02-12 08:58:36 -06:00
1 parent 0eddc76c31
commit 99389dcd2d
9 files changed
+434 -460

No files matched your search

+154
View File
@@ -0,0 +1,154 @@
local dataView = setmetatable({
EndBig = ">",
EndLittle = "<",
Types = {
Int8 = { code = "i1" },
Uint8 = { code = "I1" },
Int16 = { code = "i2" },
Uint16 = { code = "I2" },
Int32 = { code = "i4" },
Uint32 = { code = "I4" },
Int64 = { code = "i8" },
Uint64 = { code = "I8" },
Float32 = { code = "f", size = 4 }, -- a float (native size)
Float64 = { code = "d", size = 8 }, -- a double (native size)
LuaInt = { code = "j" }, -- a lua_Integer
UluaInt = { code = "J" }, -- a lua_Unsigned
LuaNum = { code = "n" }, -- a lua_Number
String = { code = "z", size = -1, }, -- zero terminated string
},
FixedTypes = {
String = { code = "c" }, -- a fixed-sized string with n bytes
Int = { code = "i" }, -- a signed int with n bytes
Uint = { code = "I" }, -- an unsigned int with n bytes
},
}, {
__call = function(_, length)
return dataView.ArrayBuffer(length)
end
})
dataView.__index = dataView
--[[ Create an ArrayBuffer with a size in bytes --]]
function dataView.ArrayBuffer(length)
return setmetatable({
blob = string.blob(length),
length = length,
offset = 1,
cangrow = true,
}, dataView)
end
--[[ Wrap a non-internalized string --]]
function dataView.Wrap(blob)
return setmetatable({
blob = blob,
length = blob:len(),
offset = 1,
cangrow = true,
}, dataView)
end
--[[ Return the underlying bytebuffer --]]
function dataView:Buffer() return self.blob end
function dataView:ByteLength() return self.length end
function dataView:ByteOffset() return self.offset end
function dataView:SubView(offset, length)
return setmetatable({
blob = self.blob,
length = length or self.length,
offset = 1 + offset,
cangrow = false,
}, dataView)
end
--[[ Return the Endianness format character --]]
local function ef(big) return (big and dataView.EndBig) or dataView.EndLittle end
--[[ Helper function for setting fixed datatypes within a buffer --]]
local function packblob(self, offset, value, code)
-- If cangrow is false the dataview represents a subview, i.e., a subset
-- of some other string view. Ensure the references are the same before
-- updating the subview
local packed = self.blob:blob_pack(offset, code, value)
if self.cangrow or packed == self.blob then
self.blob = packed
self.length = packed:len()
return true
else
return false
end
end
--[[
Create the API by using dataView.Types
--]]
for label,datatype in pairs(dataView.Types) do
if not datatype.size then -- cache fixed encoding size
datatype.size = string.packsize(datatype.code)
elseif datatype.size >= 0 and string.packsize(datatype.code) ~= datatype.size then
local msg = "Pack size of %s (%d) does not match cached length: (%d)"
error(msg:format(label, string.packsize(datatype.code), datatype.size))
return nil
end
dataView["Get" .. label] = function(self, offset, endian)
offset = offset or 0
if offset >= 0 then
local o = self.offset + offset
local v,_ = self.blob:blob_unpack(o, ef(endian) .. datatype.code)
return v
end
return nil
end
dataView["Set" .. label] = function(self, offset, value, endian)
if offset >= 0 and value then
local o = self.offset + offset
local v_size = (datatype.size < 0 and value:len()) or datatype.size
if self.cangrow or ((o + (v_size - 1)) <= self.length) then
if not packblob(self, o, value, ef(endian) .. datatype.code) then
error("cannot grow subview")
end
else
error("cannot grow dataview")
end
end
return self
end
end
for label,datatype in pairs(dataView.FixedTypes) do
datatype.size = -1 -- Ensure cached encoding size is invalidated
dataView["GetFixed" .. label] = function(self, offset, typelen, endian)
if offset >= 0 then
local o = self.offset + offset
if (o + (typelen - 1)) <= self.length then
local code = ef(endian) .. "c" .. tostring(typelen)
local v,_ = self.blob:blob_unpack(o, code)
return v
end
end
return nil -- Out of bounds
end
dataView["SetFixed" .. label] = function(self, offset, typelen, value, endian)
if offset >= 0 and value then
local o = self.offset + offset
if self.cangrow or ((o + (typelen - 1)) <= self.length) then
local code = ef(endian) .. "c" .. tostring(typelen)
if not packblob(self, o, value, code) then
error("cannot grow subview")
end
else
error("cannot grow dataview")
end
end
return self
end
end
return dataView
+61 -172
View File
@@ -1,162 +1,8 @@
-- Credit: https://github.com/citizenfx/lua/blob/luaglm-dev/cfx/libs/scripts/examples/dataview.lua
local dataView = setmetatable({
EndBig = ">",
EndLittle = "<",
Types = {
Int8 = { code = "i1" },
Uint8 = { code = "I1" },
Int16 = { code = "i2" },
Uint16 = { code = "I2" },
Int32 = { code = "i4" },
Uint32 = { code = "I4" },
Int64 = { code = "i8" },
Uint64 = { code = "I8" },
Float32 = { code = "f", size = 4 }, -- a float (native size)
Float64 = { code = "d", size = 8 }, -- a double (native size)
LuaInt = { code = "j" }, -- a lua_Integer
UluaInt = { code = "J" }, -- a lua_Unsigned
LuaNum = { code = "n" }, -- a lua_Number
String = { code = "z", size = -1, }, -- zero terminated string
},
FixedTypes = {
String = { code = "c" }, -- a fixed-sized string with n bytes
Int = { code = "i" }, -- a signed int with n bytes
Uint = { code = "I" }, -- an unsigned int with n bytes
},
}, {
__call = function(_, length)
return dataView.ArrayBuffer(length)
end
})
dataView.__index = dataView
--[[ Create an ArrayBuffer with a size in bytes --]]
function dataView.ArrayBuffer(length)
return setmetatable({
blob = string.blob(length),
length = length,
offset = 1,
cangrow = true,
}, dataView)
end
--[[ Wrap a non-internalized string --]]
function dataView.Wrap(blob)
return setmetatable({
blob = blob,
length = blob:len(),
offset = 1,
cangrow = true,
}, dataView)
end
--[[ Return the underlying bytebuffer --]]
function dataView:Buffer() return self.blob end
function dataView:ByteLength() return self.length end
function dataView:ByteOffset() return self.offset end
function dataView:SubView(offset, length)
return setmetatable({
blob = self.blob,
length = length or self.length,
offset = 1 + offset,
cangrow = false,
}, dataView)
end
--[[ Return the Endianness format character --]]
local function ef(big) return (big and dataView.EndBig) or dataView.EndLittle end
--[[ Helper function for setting fixed datatypes within a buffer --]]
local function packblob(self, offset, value, code)
-- If cangrow is false the dataview represents a subview, i.e., a subset
-- of some other string view. Ensure the references are the same before
-- updating the subview
local packed = self.blob:blob_pack(offset, code, value)
if self.cangrow or packed == self.blob then
self.blob = packed
self.length = packed:len()
return true
else
return false
end
end
--[[
Create the API by using dataView.Types
--]]
for label,datatype in pairs(dataView.Types) do
if not datatype.size then -- cache fixed encoding size
datatype.size = string.packsize(datatype.code)
elseif datatype.size >= 0 and string.packsize(datatype.code) ~= datatype.size then
local msg = "Pack size of %s (%d) does not match cached length: (%d)"
error(msg:format(label, string.packsize(datatype.code), datatype.size))
return nil
end
dataView["Get" .. label] = function(self, offset, endian)
offset = offset or 0
if offset >= 0 then
local o = self.offset + offset
local v,_ = self.blob:blob_unpack(o, ef(endian) .. datatype.code)
return v
end
return nil
end
dataView["Set" .. label] = function(self, offset, value, endian)
if offset >= 0 and value then
local o = self.offset + offset
local v_size = (datatype.size < 0 and value:len()) or datatype.size
if self.cangrow or ((o + (v_size - 1)) <= self.length) then
if not packblob(self, o, value, ef(endian) .. datatype.code) then
error("cannot grow subview")
end
else
error("cannot grow dataview")
end
end
return self
end
end
for label,datatype in pairs(dataView.FixedTypes) do
datatype.size = -1 -- Ensure cached encoding size is invalidated
dataView["GetFixed" .. label] = function(self, offset, typelen, endian)
if offset >= 0 then
local o = self.offset + offset
if (o + (typelen - 1)) <= self.length then
local code = ef(endian) .. "c" .. tostring(typelen)
local v,_ = self.blob:blob_unpack(o, code)
return v
end
end
return nil -- Out of bounds
end
dataView["SetFixed" .. label] = function(self, offset, typelen, value, endian)
if offset >= 0 and value then
local o = self.offset + offset
if self.cangrow or ((o + (typelen - 1)) <= self.length) then
local code = ef(endian) .. "c" .. tostring(typelen)
if not packblob(self, o, value, code) then
error("cannot grow subview")
end
else
error("cannot grow dataview")
end
end
return self
end
end
-- CREDITS
-- Andyyy7666: https://github.com/overextended/ox_lib/pull/453
-- AvarianKnight: https://forum.cfx.re/t/allow-drawgizmo-to-be-used-outside-of-fxdk/5091845/8?u=demi-automatic
local dataview = dataView
local dataview = Require("lib/client/dataview.lua")
local enableScale = false -- allow scaling mode. doesnt scale collisions and resets when physics are applied it seems
local gizmoEnabled = false
local currentMode = 'translate'
@@ -165,6 +11,7 @@ local currentEntity
-- FUNCTIONS
local function normalize(x, y, z)
local length = math.sqrt(x * x + y * y + z * z)
if length == 0 then
@@ -173,11 +20,6 @@ local function normalize(x, y, z)
return x / length, y / length, z / length
end
local identityMatrix = {
vector3(1, 0, 0),
vector3(0, 1, 0),
vector3(0, 0, 1)
}
local function makeEntityMatrix(entity)
local f, r, u, a = GetEntityMatrix(entity)
local view = dataview.ArrayBuffer(60)
@@ -222,6 +64,14 @@ local function applyEntityMatrix(entity, view)
)
end
function InBoundary(pos, boundary)
if not boundary then return false end
local x, y, z = table.unpack(pos)
local minX, minY, minZ = table.unpack(boundary.min)
local maxX, maxY, maxZ = table.unpack(boundary.max)
return x >= minX and x <= maxX and y >= minY and y <= maxY and z >= minZ and z <= maxZ
end
-- LOOPS
function RegisterButtonHandler(button, onPressed, onReleased)
@@ -234,14 +84,38 @@ function RegisterButtonHandler(button, onPressed, onReleased)
end
local function gizmoLoop(entity, onConfirm, onUpdate, onCancel)
local function gizmoLoop(entity, onConfirm, onUpdate, onCancel, settings)
assert(entity, 'entity is required')
entity = tonumber(entity)
EnterCursorMode()
settings = settings or {}
local allowedMats = settings.allowedMats
local maxDepth = settings.maxDepth or 10
local outLine = false
if IsEntityAPed(entity) then
SetEntityAlpha(entity, 200)
else
SetEntityDrawOutline(entity, true)
end
if allowedMats or boundary then
CreateThread(function()
while gizmoEnabled do
local coords = GetEntityCoords(entity)
local destination = coords - vector3(0, 0, maxDepth)
local hit, _, _, _, materialHash = Raycast.ToCoords(coords, destination, 1, 4)
if hit and hit ~=1 and (allowedMats?[materialHash] or InBoundary(GetEntityCoords(entity), boundary)) then
if not inBounds then
inBounds = true
SetEntityDrawOutlineColor(0, 255, 0, 255)
SetEntityDrawOutline(entity, true)
end
elseif inBounds then
inBounds = false
SetEntityDrawOutline(entity, false)
end
Wait(500)
end
end)
end
while gizmoEnabled and DoesEntityExist(entity) do
@@ -255,11 +129,11 @@ local function gizmoLoop(entity, onConfirm, onUpdate, onCancel)
SetEntityCollision(entity, false, false)
FreezeEntityPosition(entity, false)
local matrixBuffer = makeEntityMatrix(entity)
local changed = DrawGizmo(matrixBuffer:Buffer(), 'Editor1')
local changed = Citizen.InvokeNative(0xEB2EDCA2, matrixBuffer:Buffer(), 'Editor1', Citizen.ReturnResultAnyway())
if changed then
print(GetEntityCoords(entity))
applyEntityMatrix(entity, matrixBuffer)
SetEntityCoords(entity, GetEntityCoords(entity))
local coords = GetEntityCoords(entity)
SetEntityCoords(entity, coords.x, coords.y, coords.z, false, false, false, false)
end
if onUpdate and type(onUpdate) == 'function' then onUpdate(entity, GetEntityCoords(entity), GetEntityRotation(entity)) end
@@ -291,7 +165,8 @@ local function gizmoLoop(entity, onConfirm, onUpdate, onCancel)
local playerPos = GetEntityCoords(PlayerPedId())
local entityPos = GetFinalRenderedCamCoord()
local depth = #(playerPos - entityPos) -- Vector3 distance
PlaceableObject.Create({spawned = entity}, nil, onConfirm, onUpdate, onCancel, {depth = depth})
settings.depth = depth
PlaceableObject.Create({spawned = entity}, nil, onConfirm, onUpdate, onCancel, settings)
end)
--ctrl
@@ -321,6 +196,20 @@ local function gizmoLoop(entity, onConfirm, onUpdate, onCancel)
return true
end
--===================================================
-- █▀ █ █ ▄▀▀ █▄▀ ▀▄▀ ▄▀▄ █ █ █▄ █ ██▀ █ █ ██▄
-- █▀ ▀▄█ ▀▄▄ █ █ █ ▀▄▀ ▀▄█ █ ▀█ █▄▄ ▀▄▀▄▀ █▄█
--===================================================
-- .--------.
-- | taktak |
-- |/-------'
-- .-. ,-.
-- ;oo oo;
-- / \| |/ \
-- |. `. .' .|
-- `;.;' `;.;'
-- .-^-. .-^-. ko1
-- local function textUILoop()
-- CreateThread(function()
-- while gizmoEnabled do
@@ -343,11 +232,11 @@ end
-- EXPORTS
local function useGizmo(entity, onConfirm, onUpdate, onCancel)
local function useGizmo(entity, onConfirm, onUpdate, onCancel, settings)
gizmoEnabled = true
currentEntity = entity
-- textUILoop()
gizmoLoop(entity, onConfirm, onUpdate, onCancel)
gizmoLoop(entity, onConfirm, onUpdate, onCancel, settings)
return {
handle = entity,
@@ -360,7 +249,7 @@ exports("useGizmo", useGizmo)
Gizmo = {}
local running = false
function Gizmo.UseGizmo(obj, onConfirm, onUpdate, onCancel)
function Gizmo.UseGizmo(obj, onConfirm, onUpdate, onCancel, settings)
if running then return end
running = true
@@ -387,7 +276,7 @@ function Gizmo.UseGizmo(obj, onConfirm, onUpdate, onCancel)
running = false
end)
quit = useGizmo(obj, onConfirm, onUpdate, onCancel)
quit = useGizmo(obj, onConfirm, onUpdate, onCancel, settings)
running = false
end
exports("UseGizmo", Gizmo.UseGizmo)
+1 -1
View File
@@ -20,6 +20,7 @@ local function finishPlacing()
end
Placeable.PlaceObject = function(object, distance, snapToGround, allowedMats, offset)
distance = tonumber(distance or 10.0 )
if activePlacementProp then return end
if not object then Prints.Error('no_prop_defined') end
@@ -75,7 +76,6 @@ Placeable.PlaceObject = function(object, distance, snapToGround, allowedMats, of
if IsControlJustReleased(0, 38) then
if not outLine and (not allowedMats or allowedMats[materialHash]) and distCheck < checkDist then
finishPlacing()
return coords, heading
end
end
@@ -1,8 +1,10 @@
Scaleform = Scaleform or Require("client/scaleform.lua")
Utility = Utility or Require("client/utility.lua")
Gizmo = Gizmo or Require("client/gizmo.lua")
Scaleform = Scaleform or Require("lib/client/scaleform.lua")
Utility = Utility or Require("lib/client/utility.lua")
Gizmo = Gizmo or Require("lib/client/gizmo.lua")
Raycast = Raycast or Require("lib/client/raycast.lua")
PlaceableObject = PlaceableObject or {}
local data = {
entity = nil,
placing = false,
@@ -21,6 +23,13 @@ function PlaceableObject.Eyetrace(depth, disableSphere)
return target
end
function PlaceableObject.InBoundary(pos, boundary)
if not boundary then return false end
local x, y, z = table.unpack(pos)
local minX, minY, minZ = table.unpack(boundary.min)
local maxX, maxY, maxZ = table.unpack(boundary.max)
return x >= minX and x <= maxX and y >= minY and y <= maxY and z >= minZ and z <= maxZ
end
-- Creates a placeable object in the game world.
-- @param model (string) The model of the object to create.
-- @param onConfirm (function) [optional] The callback function to execute when the object is confirmed.
@@ -31,18 +40,21 @@ end
-- - depthMax (number) The maximum depth for the object placement. Default is 10.0.
-- - rotationStep (number) The rotation step for the object. Default is 15.0.
-- - depthStep (number) The depth step for the object. Default is 1.0.
function PlaceableObject.Create(data, model, onConfirm, onUpdate, onCancel, settings, config)
function PlaceableObject.Create(data, model, onConfirm, onUpdate, onCancel, settings)
local settings = settings or {}
local depthMin = settings.depthMin or 2.0
depthMax = settings.depthMax or 10.0
rotationStep = settings.rotationStep or 15.0
depthStep = settings.depthStep or 1.0
disableSphere = settings.useSphere and false or true
local depth = settings.depth or 10.0
print(depth)
data.placing = true
data.paused = false
local initialPos = nil
local placeOnGround = true
local allowedMats = settings.allowedMats
local boundary = settings.boundary
local config = settings.config or {}
--scaleform
local scaleform = Scaleform.SetupInstructionalButtons({
@@ -58,39 +70,62 @@ function PlaceableObject.Create(data, model, onConfirm, onUpdate, onCancel, sett
{type = "DRAW_INSTRUCTIONAL_BUTTONS"},
{type = "SET_BACKGROUND_COLOUR"},
})
local heading = -GetEntityHeading(PlayerPedId())
local pos = PlaceableObject.Eyetrace(depth, disableSphere)
if not data.spawned and model then
--get player heading
local obj = Utility.CreateProp(model, pos, vector3(0, 0, heading), nil)
data.spawned = obj
end
assert(data.spawned, "Failed to create object")
SetEntityCollision(data.spawned, false, false)
SetEntityAlpha(data.spawned, 150, false)
SetPedConfigFlag(PlayerPedId(), 146, true)
SetCanClimbOnEntity(data.spawned, false)
SetEntityCompletelyDisableCollision(data.spawned, true, false)
SetEntityNoCollisionEntity(PlayerPedId(), data.spawned, false)
local inBounds = true
if allowedMats or boundary then
CreateThread(function()
while data.placing do
local hit, _, coords, _, materialHash = Raycast.FromCamera(1, 4)
if hit and hit ~=1 and (allowedMats?[materialHash] or PlaceableObject.InBoundary(GetEntityCoords(data.spawned), boundary)) then
if not inBounds then
inBounds = true
SetEntityDrawOutlineColor(0, 255, 0, 255)
SetEntityDrawOutline(data.spawned, true)
end
elseif inBounds then
inBounds = false
SetEntityDrawOutline(data.spawned, false)
end
Wait(500)
end
end)
end
CreateThread(function()
local heading = -GetEntityHeading(PlayerPedId())
local pos = PlaceableObject.Eyetrace(depth)
if not data.spawned and model then
--get player heading
local obj = Utility.CreateProp(model, pos, vector3(0, 0, heading), nil)
data.spawned = obj
end
assert(data.spawned, "Failed to create object")
SetEntityCollision(data.spawned, false, false)
SetEntityAlpha(data.spawned, 150, false)
SetPedConfigFlag(PlayerPedId(), 146, true)
SetCanClimbOnEntity(data.spawned, false)
SetEntityCompletelyDisableCollision(data.spawned, true, false)
SetEntityNoCollisionEntity(PlayerPedId(), data.spawned, false)
while data.placing do
if data.paused then goto continue end
DrawScaleformMovieFullscreen(scaleform, 255, 255, 255, 255, 0)
DisableControlAction(0, 24, true) -- Attack
DisableControlAction(0, 25, true) -- Aim
DisableControlAction(0, 36, true) -- INPUT_DUCK.
if IsControlJustPressed(0, 223) then -- left click
if onConfirm and type(onConfirm) == 'function' then onConfirm(data.spawned, pos, heading) end
data.placing = false
if IsDisabledControlJustPressed(0, 223) then -- left click
if not inBounds then
Notify.SendNotify("You can't place this object here", "error", 5000)
elseif onConfirm then
onConfirm(data.spawned, pos, heading)
data.placing = false
end
end
if IsControlJustPressed(0, 222) then -- right click
if IsDisabledControlJustPressed(0, 25) then -- right click
data.placing = false
if onCancel and type(onCancel) == 'function' then
if onCancel then
onCancel(data.spawned, pos, heading)
else
DeleteEntity(data.spawned)
@@ -98,13 +133,13 @@ function PlaceableObject.Create(data, model, onConfirm, onUpdate, onCancel, sett
return
end
if IsControlPressed(0,19) then
placeOnGround = not (placeOnGround or false)
if IsControlJustPressed(0,19) then
placeOnGround = not placeOnGround
end
if IsControlJustPressed(0, 38) then -- TAB key to switch to gizmo
data.placing = false
return Gizmo.UseGizmo(data.spawned, onConfirm, onUpdate, onCancel)
return Gizmo.UseGizmo(data.spawned, onConfirm, onUpdate, onCancel, settings)
end
if IsControlPressed(0,224) then -- left ctrl
@@ -136,7 +171,7 @@ function PlaceableObject.Create(data, model, onConfirm, onUpdate, onCancel, sett
end
end
pos = PlaceableObject.Eyetrace(depth)
pos = PlaceableObject.Eyetrace(depth, disableSphere)
if data.spawned then
SetEntityCoords(data.spawned, pos.x, pos.y, pos.z)
SetEntityHeading(data.spawned, heading)
@@ -155,14 +190,6 @@ function PlaceableObject.Create(data, model, onConfirm, onUpdate, onCancel, sett
end)
end
return PlaceableObject
RegisterCommand("testers", function()
PlaceableObject.Create({}, "prop_beachflag_01", function(entity, pos, heading)
end, function(entity, pos, heading)
end, function(entity, pos, heading)
end)
end)
+54
View File
@@ -0,0 +1,54 @@
Raycast = {}
--Rework of oxs Raycast system
function Raycast.GetForwardVector(rotation)
local camRot = rotation or GetFinalRenderedCamRot(2)
-- Convert each component to radians
local rx = math.rad(camRot.x)
local ry = math.rad(camRot.y)
local rz = math.rad(camRot.z)
-- Calculate sin and cos for each axis
local sx = math.sin(rx)
local cx = math.cos(rx)
local sy = math.sin(ry)
local cy = math.cos(ry)
local sz = math.sin(rz)
local cz = math.cos(rz)
-- Create forward vector components
local x = -sz * math.abs(cx)
local y = cz * math.abs(cx)
local z = sx
return vector3(x, y, z)
end
function Raycast.ToCoords(startCoords, endCoords, flag, ignore)
local probe = StartShapeTestLosProbe(startCoords.x, startCoords.y, startCoords.z, endCoords.x, endCoords.y, endCoords.z, flag or 511, PlayerPedId(), ignore or 4)
local retval, entity, finalCoords, normals, material = 1, nil, nil, nil, nil
local timeout = 500
while retval == 1 and timeout > 0 do
retval, entity, finalCoords, normals, material = GetShapeTestResultIncludingMaterial(probe)
timeout = timeout - 1
Wait(0)
end
return retval, entity, finalCoords, normals, material
end
function Raycast.FromCamera(flags, ignore, distance)
local coords = GetFinalRenderedCamCoord()
distance = distance or 10
local destination = coords + Raycast.GetForwardVector() * distance
local retval, entity, finalCoords, normals, material = Raycast.ToCoords(coords, destination, flags, ignore)
if retval ~= 1 then
print("Raycast hit max distance, trying again", coords, dist)
local newDest = destination - vector3(0, 0, 10)
return Raycast.ToCoords(destination, newDest, flags, ignore)
end
return retval, entity, finalCoords, normals, material
end
return Raycast