diff --git a/lib/entities/client/client_entity.lua b/lib/entities/client/client_entity.lua index f714dca..c6e5de1 100644 --- a/lib/entities/client/client_entity.lua +++ b/lib/entities/client/client_entity.lua @@ -62,29 +62,35 @@ end --- Registers an entity received from the server and sets up proximity spawning. -- @param entityData table Data received from the server via 'community_bridge:client:CreateEntity' -function ClientEntity.Register(entityData) - if Entities[entityData.id] then return end -- Already registered - +function ClientEntity.Create(entityData) + entityData.id = entityData.id or Ids.CreateUniqueId(Entities) + if Entities[entityData.id] then return Entities[entityData.id] end -- Already registered Entities[entityData.id] = entityData - -- print(string.format("[ClientEntity] Registering %s entity %s", entityData.entityType, entityData.id)) - - -- Use Point system for proximity checks - -- print(string.format("[ClientEntity] Registering entity %s at %s", entityData.id, json.encode(entityData))) - return Point.Register(entityData.id, entityData.coords, entityData.spawnDistance or 50.0, entityData, SpawnEntity, RemoveEntity, function() end) + return Point.Register(entityData.id, entityData.coords, entityData.spawnDistance or 50.0, entityData, SpawnEntity, RemoveEntity, function() end) end -function ClientEntity.RegisterBulk(entities) +--Depricated use ClientEntity.Create instead +--- Registers an entity and spawns it in the world if not already spawned. +ClientEntity.Register = ClientEntity.Create + + +function ClientEntity.CreateBulk(entities) local registeredEntities = {} for _, entityData in pairs(entities) do - ClientEntity.Register(entityData) - registeredEntities[entityData.id] = entityData + local entity = ClientEntity.Create(entityData) + registeredEntities[entity.id] = entity end return registeredEntities end +-- Depricated use ClientEntity.CreateBulk instead +ClientEntity.RegisterBulk = ClientEntity.CreateBulk + + + --- Unregisters an entity and removes it from the world if spawned. -- @param id string|number The ID of the entity to unregister. -function ClientEntity.Unregister(id) +function ClientEntity.Destroy(id) local entityData = Entities[id] if not entityData then return end @@ -92,6 +98,8 @@ function ClientEntity.Unregister(id) RemoveEntity(entityData) Entities[id] = nil end +ClientEntity.Unregister = ClientEntity.Destroy + --- Updates the data for a registered entity. -- @param id string|number The ID of the entity to update. @@ -104,7 +112,6 @@ function ClientEntity.Update(id, data) local needsPointUpdate = false for key, value in pairs(data) do if key == 'coords' and #(entityData.coords - value) > 0.1 then - -- print(string.format("[ClientEntity] Updating coords for entity %s", id)) needsPointUpdate = true end if key == 'spawnDistance' and entityData.spawnDistance ~= value then @@ -162,13 +169,21 @@ function ClientEntity.RegisterAction(name, func) ClientEntityActions.RegisterAction(name, func) end +function ClientEntity.OnCreate(_type, func) + ClientEntity.OnCreates = ClientEntity.OnCreates or {} + if not ClientEntity.OnCreates[_type] then + ClientEntity.OnCreates[_type] = {} + end + table.insert(ClientEntity.OnCreates[_type], func) +end + -- Network Event Handlers RegisterNetEvent("community_bridge:client:CreateEntity", function(entityData) - ClientEntity.Register(entityData) + ClientEntity.Create(entityData) end) RegisterNetEvent("community_bridge:client:CreateEntities", function(entities) - ClientEntity.RegisterBulk(entities) + ClientEntity.CreateBulk(entities) end) RegisterNetEvent("community_bridge:client:DeleteEntity", function(id) diff --git a/lib/placers/client/placeable_object.lua b/lib/placers/client/placeable_object.lua index 8851828..06bb92d 100644 --- a/lib/placers/client/placeable_object.lua +++ b/lib/placers/client/placeable_object.lua @@ -125,68 +125,68 @@ local function getMouseWorldPos(depth) return playerPos + normal * depth end -local function isInBoundary(pos, boundary) - if not boundary then return true end +-- local function isInBoundary(pos, boundary) +-- if not boundary then return true end - local x, y, z = table.unpack(pos) +-- local x, y, z = table.unpack(pos) - -- Handle legacy min/max boundary format for backwards compatibility - if boundary.min and boundary.max then - 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 +-- -- Handle legacy min/max boundary format for backwards compatibility +-- if boundary.min and boundary.max then +-- 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 - -- Handle list of points (polygon boundary) - if boundary.points and #boundary.points > 0 then - local points = boundary.points - local minZ = boundary.minZ or -math.huge - local maxZ = boundary.maxZ or math.huge +-- -- Handle list of points (polygon boundary) +-- if boundary.points and #boundary.points > 0 then +-- local points = boundary.points +-- local minZ = boundary.minZ or -math.huge +-- local maxZ = boundary.maxZ or math.huge - -- Check Z bounds first - if z < minZ or z > maxZ then - return false - end +-- -- Check Z bounds first +-- if z < minZ or z > maxZ then +-- return false +-- end - -- Point-in-polygon test using ray casting algorithm (improved version) - local inside = false - local n = #points +-- -- Point-in-polygon test using ray casting algorithm (improved version) +-- local inside = false +-- local n = #points - for i = 1, n do - local j = i == n and 1 or i + 1 -- Next point (wrap around) +-- for i = 1, n do +-- local j = i == n and 1 or i + 1 -- Next point (wrap around) - local xi, yi = points[i].x or points[i][1], points[i].y or points[i][2] - local xj, yj = points[j].x or points[j][1], points[j].y or points[j][2] +-- local xi, yi = points[i].x or points[i][1], points[i].y or points[i][2] +-- local xj, yj = points[j].x or points[j][1], points[j].y or points[j][2] - -- Ensure xi, yi, xj, yj are numbers - if not (xi and yi and xj and yj) then - goto continue - end +-- -- Ensure xi, yi, xj, yj are numbers +-- if not (xi and yi and xj and yj) then +-- goto continue +-- end - -- Ray casting test - if ((yi > y) ~= (yj > y)) then - -- Calculate intersection point - local intersect = (xj - xi) * (y - yi) / (yj - yi) + xi - if x < intersect then - inside = not inside - end - end +-- -- Ray casting test +-- if ((yi > y) ~= (yj > y)) then +-- -- Calculate intersection point +-- local intersect = (xj - xi) * (y - yi) / (yj - yi) + xi +-- if x < intersect then +-- inside = not inside +-- end +-- end - ::continue:: - end +-- ::continue:: +-- end - return inside - end +-- return inside +-- end - -- Fallback to true if boundary format is not recognized - return true -end +-- -- Fallback to true if boundary format is not recognized +-- return true +-- end local function checkMaterialAndBoundary() if not state.currentEntity then return true end local pos = GetEntityCoords(state.currentEntity) - local inBounds = isInBoundary(pos, state.settings.boundary) + local inBounds = Bridge.Math.InBoundary(pos, state.settings.boundary) -- Check built-in boundary first if state.settings.boundary and not inBounds then return false end @@ -217,7 +217,7 @@ local function checkMaterialAndBoundaryDetailed() if not state.currentEntity then return true, true, true end local pos = GetEntityCoords(state.currentEntity) - local inBounds = isInBoundary(pos, state.settings.boundary) + local inBounds = Bridge.Math.InBoundary(pos, state.settings.boundary) local customCheckPassed = true local materialCheckPassed = true @@ -440,7 +440,7 @@ local function handleNormalMode() end if state.currentEntity then - SetEntityCoords(state.currentEntity, pos.x, pos.y, pos.z, false, false, false, true) + SetEntityCoords(state.currentEntity, pos.x, pos.y, pos.z, false, false, false, false) SetEntityHeading(state.currentEntity, state.heading) if state.snapToGround then local slerp = PlaceObjectOnGroundProperly(state.currentEntity) @@ -554,14 +554,21 @@ local function placementLoop() state.keys.placeObject = false -- Reset the key state local canPlace = checkMaterialAndBoundary() if canPlace then + Wait(100) local coords = GetEntityCoords(state.currentEntity) - local heading = GetEntityHeading(state.currentEntity) + if not state.settings.allowVertical or state.snapToGround then + local groundZ, _z = GetGroundZFor_3dCoord(coords.x, coords.y, coords.z + 50, false) + if groundZ then + coords = vector3(coords.x, coords.y, _z) + end + end + local rotation = GetEntityRotation(state.currentEntity) if state.promise then state.promise:resolve({ entity = state.currentEntity, coords = coords, - heading = heading, + rotation = rotation, placed = true }) end @@ -692,7 +699,7 @@ function PlaceableObject.Create(model, settings) local p = promise.new() state.promise = p - local point = Bridge.ClientEntity.Register({ + local point = Bridge.ClientEntity.Create({ id = 'placeable_object', entityType = 'object', model = model, diff --git a/lib/utility/shared/math.lua b/lib/utility/shared/math.lua index 09e750b..30717ce 100644 --- a/lib/utility/shared/math.lua +++ b/lib/utility/shared/math.lua @@ -84,6 +84,63 @@ function Math.GetOffsetFromMatrix(position, rotation, offset) return position + rotated end +function Math.InBoundary(pos, boundary) + if not boundary then return true end + + local x, y, z = table.unpack(pos) + + -- Handle legacy min/max boundary format for backwards compatibility + if boundary.min and boundary.max then + 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 + + -- Handle list of points (polygon boundary) + if boundary.points and #boundary.points > 0 then + local points = boundary.points + local minZ = boundary.minZ or -math.huge + local maxZ = boundary.maxZ or math.huge + + -- Check Z bounds first + if z < minZ or z > maxZ then + return false + end + + -- Point-in-polygon test using ray casting algorithm (improved version) + local inside = false + local n = #points + + for i = 1, n do + local j = i == n and 1 or i + 1 -- Next point (wrap around) + + local xi, yi = points[i].x or points[i][1], points[i].y or points[i][2] + local xj, yj = points[j].x or points[j][1], points[j].y or points[j][2] + + -- Ensure xi, yi, xj, yj are numbers + if not (xi and yi and xj and yj) then + goto continue + end + + -- Ray casting test + if ((yi > y) ~= (yj > y)) then + -- Calculate intersection point + local intersect = (xj - xi) * (y - yi) / (yj - yi) + xi + if x < intersect then + inside = not inside + end + end + + ::continue:: + end + + return inside + end + + -- Fallback to true if boundary format is not recognized + return true +end + exports('Math', Math) return Math \ No newline at end of file