core: fix building
This commit is contained in:
@@ -6,6 +6,19 @@ local PlacementManager = {}
|
||||
local ChunkManager = require("./ChunkManager")
|
||||
local Util = require("./Util")
|
||||
|
||||
local DEBUG_PLACEMENT = true
|
||||
local function debugPlacementLog(...: any)
|
||||
if DEBUG_PLACEMENT then
|
||||
Util.StudioLog(...)
|
||||
end
|
||||
end
|
||||
|
||||
local function debugPlacementWarn(...: any)
|
||||
if DEBUG_PLACEMENT then
|
||||
Util.StudioWarn(...)
|
||||
end
|
||||
end
|
||||
|
||||
PlacementManager.ChunkFolder = ChunkManager.ChunkFolder
|
||||
|
||||
local raycastParams = RaycastParams.new()
|
||||
@@ -13,12 +26,15 @@ raycastParams.FilterDescendantsInstances = {PlacementManager.ChunkFolder}
|
||||
raycastParams.FilterType = Enum.RaycastFilterType.Include
|
||||
raycastParams.IgnoreWater = true
|
||||
|
||||
if _G.SB then return nil end
|
||||
_G.SB = true
|
||||
if _G.__BLOCKSCRAFT_PLACEMENT_MANAGER then
|
||||
return _G.__BLOCKSCRAFT_PLACEMENT_MANAGER
|
||||
end
|
||||
_G.__BLOCKSCRAFT_PLACEMENT_MANAGER = PlacementManager
|
||||
|
||||
PlacementManager.SelectionBox = script.SelectionBox:Clone()
|
||||
PlacementManager.SelectionBox.Name = "$SelectionBox"..(game:GetService("RunService"):IsServer() and "_SERVER" or "")
|
||||
PlacementManager.SelectionBox.Parent = game:GetService("Workspace"):FindFirstChildOfClass("Terrain")
|
||||
PlacementManager.SelectionBox.Adornee = nil
|
||||
|
||||
-- Trash method TODO: Fix this
|
||||
local function findChunkFolderFromDescendant(inst: Instance): Instance?
|
||||
@@ -27,15 +43,83 @@ local function findChunkFolderFromDescendant(inst: Instance): Instance?
|
||||
if current.Parent == PlacementManager.ChunkFolder then
|
||||
return current
|
||||
end
|
||||
-- Fallback: match by name in case the ChunkFolder reference differs (e.g. recreated/parented later)
|
||||
if current.Parent:IsA("Folder") and current.Parent.Name == (PlacementManager.ChunkFolder and PlacementManager.ChunkFolder.Name) then
|
||||
return current
|
||||
end
|
||||
current = current.Parent
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
local function findBlockRoot(inst: Instance, chunkFolder: Instance): Instance?
|
||||
local current = inst
|
||||
while current and current ~= chunkFolder do
|
||||
if current:IsA("BasePart") then
|
||||
return current
|
||||
end
|
||||
current = current.Parent
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
local function resolveBlockInstance(chunkFolder: Instance, chunkName: string, blockName: string): Instance?
|
||||
local chunkInst = chunkFolder:FindFirstChild(chunkName)
|
||||
if not chunkInst then
|
||||
return nil
|
||||
end
|
||||
return chunkInst:FindFirstChild(blockName)
|
||||
end
|
||||
|
||||
local function clearSelection(reason: string?)
|
||||
PlacementManager.SelectionBox.Adornee = nil
|
||||
PlacementManager.SelectionBox.Parent = nil
|
||||
lastNormalId = nil
|
||||
if reason then
|
||||
lastRaycastFailure = reason
|
||||
end
|
||||
end
|
||||
|
||||
local function setSelection(target: Instance, parent: Instance)
|
||||
PlacementManager.SelectionBox.Parent = parent
|
||||
PlacementManager.SelectionBox.Adornee = target
|
||||
end
|
||||
|
||||
local function findChunkAndBlock(inst: Instance): (string?, string?)
|
||||
local root = PlacementManager.ChunkFolder
|
||||
if not root then
|
||||
return nil, nil
|
||||
end
|
||||
local current = inst
|
||||
while current and current.Parent do
|
||||
-- case: current parent is the chunk folder root; then current is the chunk itself (no block name yet)
|
||||
if current.Parent == root then
|
||||
return current.Name, inst.Name
|
||||
end
|
||||
-- case: grandparent is chunk folder root; parent is chunk, current is block/model
|
||||
if current.Parent.Parent == root then
|
||||
return current.Parent.Name, current.Name
|
||||
end
|
||||
current = current.Parent
|
||||
end
|
||||
return nil, nil
|
||||
end
|
||||
|
||||
local Mouse: Mouse = nil
|
||||
local lastNormalId: Enum.NormalId? = nil
|
||||
local BREAK_ROLLBACK_TIMEOUT = 0.6
|
||||
local pendingBreaks = {}
|
||||
local lastRaycastFailure: string? = nil
|
||||
local function vectorToNormalId(normal: Vector3): Enum.NormalId
|
||||
local ax, ay, az = math.abs(normal.X), math.abs(normal.Y), math.abs(normal.Z)
|
||||
if ax >= ay and ax >= az then
|
||||
return normal.X >= 0 and Enum.NormalId.Right or Enum.NormalId.Left
|
||||
elseif ay >= ax and ay >= az then
|
||||
return normal.Y >= 0 and Enum.NormalId.Top or Enum.NormalId.Bottom
|
||||
else
|
||||
return normal.Z >= 0 and Enum.NormalId.Back or Enum.NormalId.Front
|
||||
end
|
||||
end
|
||||
|
||||
local function makeChunkKey(cx: number, cy: number, cz: number): string
|
||||
return `{cx},{cy},{cz}`
|
||||
@@ -150,38 +234,127 @@ local function isWithinReach(cx: number, cy: number, cz: number, x: number, y: n
|
||||
return true
|
||||
end
|
||||
|
||||
local function ensureChunkFolder(): Instance?
|
||||
if PlacementManager.ChunkFolder and PlacementManager.ChunkFolder.Parent then
|
||||
return PlacementManager.ChunkFolder
|
||||
end
|
||||
local found = workspace:FindFirstChild("$blockscraft_client")
|
||||
if found then
|
||||
PlacementManager.ChunkFolder = found
|
||||
return found
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Gets the block and normalid of the block (and surface) the player is looking at
|
||||
function PlacementManager:Raycast()
|
||||
if not Mouse then
|
||||
Mouse = game:GetService("Players").LocalPlayer:GetMouse()
|
||||
end
|
||||
local objLookingAt = Mouse.Target
|
||||
local dir = Mouse.TargetSurface or Enum.NormalId.Top
|
||||
if not objLookingAt then
|
||||
PlacementManager.SelectionBox.Adornee = nil
|
||||
script.RaycastResult.Value = nil
|
||||
lastNormalId = nil
|
||||
return
|
||||
end
|
||||
|
||||
--if not objLookingAt:IsDescendantOf(ChunkManager.ChunkFolder) then return end
|
||||
local chunkFolder = findChunkFolderFromDescendant(objLookingAt)
|
||||
local chunkFolder = ensureChunkFolder()
|
||||
if not chunkFolder then
|
||||
PlacementManager.SelectionBox.Adornee = nil
|
||||
clearSelection("chunk folder missing")
|
||||
script.RaycastResult.Value = nil
|
||||
lastNormalId = nil
|
||||
return
|
||||
end
|
||||
if chunkFolder:GetAttribute("ns") == true then
|
||||
PlacementManager.SelectionBox.Adornee = nil
|
||||
script.RaycastResult.Value = nil
|
||||
lastNormalId = nil
|
||||
|
||||
raycastParams.FilterDescendantsInstances = {chunkFolder}
|
||||
local cam = workspace.CurrentCamera
|
||||
if not cam then
|
||||
lastRaycastFailure = "no camera"
|
||||
return
|
||||
end
|
||||
PlacementManager.SelectionBox.Adornee = objLookingAt
|
||||
local ray = Mouse.UnitRay
|
||||
local result = workspace:Raycast(ray.Origin, ray.Direction * MAX_REACH, raycastParams)
|
||||
if not result then
|
||||
clearSelection("raycast miss")
|
||||
script.RaycastResult.Value = nil
|
||||
debugPlacementLog("[PLACE][CLIENT][RAYCAST]", "miss")
|
||||
return
|
||||
end
|
||||
|
||||
local objLookingAt = result.Instance
|
||||
if not objLookingAt then
|
||||
clearSelection("raycast nil instance")
|
||||
script.RaycastResult.Value = nil
|
||||
debugPlacementWarn("[PLACE][CLIENT][RAYCAST]", "nil instance in result")
|
||||
return
|
||||
end
|
||||
|
||||
local hitChunkFolder = findChunkFolderFromDescendant(objLookingAt)
|
||||
if not hitChunkFolder then
|
||||
debugPlacementWarn(
|
||||
"[PLACE][CLIENT][REJECT]",
|
||||
"target not in chunk folder",
|
||||
objLookingAt:GetFullName(),
|
||||
"parent",
|
||||
objLookingAt.Parent and objLookingAt.Parent:GetFullName() or "nil"
|
||||
)
|
||||
clearSelection("target not in chunk folder")
|
||||
script.RaycastResult.Value = nil
|
||||
return
|
||||
end
|
||||
if hitChunkFolder:GetAttribute("ns") == true then
|
||||
debugPlacementWarn(
|
||||
"[PLACE][CLIENT][REJECT]",
|
||||
"chunk flagged ns",
|
||||
hitChunkFolder:GetFullName()
|
||||
)
|
||||
clearSelection("target chunk marked ns")
|
||||
script.RaycastResult.Value = nil
|
||||
return
|
||||
end
|
||||
PlacementManager.ChunkFolder = chunkFolder
|
||||
|
||||
local blockRoot = findBlockRoot(objLookingAt, chunkFolder) or objLookingAt
|
||||
local chunkName, blockName = findChunkAndBlock(blockRoot)
|
||||
if not chunkName or not blockName then
|
||||
clearSelection("failed to resolve chunk/block")
|
||||
script.RaycastResult.Value = nil
|
||||
return
|
||||
end
|
||||
local okChunk, chunkCoords = pcall(function()
|
||||
return Util.BlockPosStringToCoords(chunkName)
|
||||
end)
|
||||
local okBlock, blockCoords = pcall(function()
|
||||
return Util.BlockPosStringToCoords(blockName)
|
||||
end)
|
||||
if not okChunk or not okBlock then
|
||||
clearSelection("failed to parse chunk/block names")
|
||||
script.RaycastResult.Value = nil
|
||||
return
|
||||
end
|
||||
|
||||
-- hide selection if block no longer exists (air/removed)
|
||||
local chunk = ChunkManager:GetChunk(chunkCoords.X, chunkCoords.Y, chunkCoords.Z)
|
||||
local blockData = chunk and chunk:GetBlockAt(blockCoords.X, blockCoords.Y, blockCoords.Z)
|
||||
if not blockData or blockData == 0 or blockData.id == 0 then
|
||||
clearSelection("block missing/air")
|
||||
script.RaycastResult.Value = nil
|
||||
return
|
||||
end
|
||||
local blockInstance = resolveBlockInstance(chunkFolder, chunkName, blockName) or blockRoot
|
||||
if not blockInstance then
|
||||
clearSelection("missing block instance")
|
||||
script.RaycastResult.Value = nil
|
||||
return
|
||||
end
|
||||
|
||||
lastRaycastFailure = nil
|
||||
setSelection(blockInstance, PlacementManager.ChunkFolder)
|
||||
script.RaycastResult.Value = objLookingAt
|
||||
lastNormalId = dir
|
||||
return objLookingAt, dir
|
||||
lastNormalId = vectorToNormalId(result.Normal)
|
||||
debugPlacementLog(
|
||||
"[PLACE][CLIENT][RAYCAST][HIT]",
|
||||
blockInstance:GetFullName(),
|
||||
"chunkFolder",
|
||||
hitChunkFolder:GetFullName(),
|
||||
"blockName",
|
||||
blockInstance.Name,
|
||||
"normal",
|
||||
lastNormalId.Name
|
||||
)
|
||||
return objLookingAt, lastNormalId
|
||||
end
|
||||
|
||||
function PlacementManager:RaycastGetResult()
|
||||
@@ -195,18 +368,70 @@ local tickRemote = game:GetService("ReplicatedStorage").Tick
|
||||
|
||||
-- FIRES REMOTE
|
||||
function PlacementManager:PlaceBlock(cx, cy, cz, x, y, z, blockId: string)
|
||||
debugPlacementLog("[PLACE][CLIENT][PLACE_CALL]", "chunk", cx, cy, cz, "block", x, y, z, "blockId", blockId)
|
||||
if typeof(cx) ~= "number" or typeof(cy) ~= "number" or typeof(cz) ~= "number" then
|
||||
debugPlacementWarn("[PLACE][CLIENT][REJECT]", "chunk type", cx, cy, cz, x, y, z, blockId)
|
||||
return
|
||||
end
|
||||
if typeof(x) ~= "number" or typeof(y) ~= "number" or typeof(z) ~= "number" then
|
||||
debugPlacementWarn("[PLACE][CLIENT][REJECT]", "block type", cx, cy, cz, x, y, z, blockId)
|
||||
return
|
||||
end
|
||||
if x < 1 or x > 8 or y < 1 or y > 8 or z < 1 or z > 8 then
|
||||
debugPlacementWarn("[PLACE][CLIENT][REJECT]", "block bounds", cx, cy, cz, x, y, z, blockId)
|
||||
return
|
||||
end
|
||||
if not isWithinReach(cx, cy, cz, x, y, z) then
|
||||
debugPlacementWarn("[PLACE][CLIENT][REJECT]", "reach", cx, cy, cz, x, y, z, blockId)
|
||||
return
|
||||
end
|
||||
|
||||
-- ensure chunk is present/rendered client-side
|
||||
local chunk = ChunkManager:GetChunk(cx, cy, cz)
|
||||
if chunk and not chunk.loaded then
|
||||
ChunkManager:LoadChunk(cx, cy, cz)
|
||||
end
|
||||
if not chunk then
|
||||
debugPlacementWarn("[PLACE][CLIENT][REJECT]", "missing chunk", cx, cy, cz, x, y, z, blockId)
|
||||
return
|
||||
end
|
||||
|
||||
-- if the client already thinks this block is the same id, skip sending
|
||||
if chunk then
|
||||
local existing = chunk:GetBlockAt(x, y, z)
|
||||
local existingId = existing and existing.id
|
||||
if existingId and tostring(existingId) == tostring(blockId) then
|
||||
debugPlacementLog(
|
||||
"[PLACE][CLIENT][SKIP]",
|
||||
"duplicate id",
|
||||
"chunk",
|
||||
cx,
|
||||
cy,
|
||||
cz,
|
||||
"block",
|
||||
x,
|
||||
y,
|
||||
z,
|
||||
"existingId",
|
||||
existingId,
|
||||
"blockId",
|
||||
blockId
|
||||
)
|
||||
return
|
||||
else
|
||||
debugPlacementLog(
|
||||
"[PLACE][CLIENT][EXISTING]",
|
||||
"chunk",
|
||||
cx,
|
||||
cy,
|
||||
cz,
|
||||
"block",
|
||||
x,
|
||||
y,
|
||||
z,
|
||||
"existingId",
|
||||
existingId
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -218,6 +443,7 @@ function PlacementManager:PlaceBlock(cx, cy, cz, x, y, z, blockId: string)
|
||||
})
|
||||
end
|
||||
|
||||
debugPlacementLog("[PLACE][CLIENT][SEND]", cx, cy, cz, x, y, z, blockId)
|
||||
placeRemote:FireServer(cx, cy, cz, x, y, z, blockId)
|
||||
end
|
||||
|
||||
@@ -227,12 +453,15 @@ function PlacementManager:BreakBlock(cx, cy, cz, x, y, z)
|
||||
return
|
||||
end
|
||||
if typeof(x) ~= "number" or typeof(y) ~= "number" or typeof(z) ~= "number" then
|
||||
debugPlacementWarn("[BREAK][CLIENT][REJECT]", "block type", cx, cy, cz, x, y, z)
|
||||
return
|
||||
end
|
||||
if x < 1 or x > 8 or y < 1 or y > 8 or z < 1 or z > 8 then
|
||||
debugPlacementWarn("[BREAK][CLIENT][REJECT]", "block bounds", cx, cy, cz, x, y, z)
|
||||
return
|
||||
end
|
||||
if not isWithinReach(cx, cy, cz, x, y, z) then
|
||||
debugPlacementWarn("[BREAK][CLIENT][REJECT]", "reach", cx, cy, cz, x, y, z)
|
||||
return
|
||||
end
|
||||
|
||||
@@ -241,6 +470,7 @@ function PlacementManager:BreakBlock(cx, cy, cz, x, y, z)
|
||||
local chunkKey = makeChunkKey(cx, cy, cz)
|
||||
local blockKey = makeBlockKey(x, y, z)
|
||||
if getPendingBreak(chunkKey, blockKey) then
|
||||
debugPlacementLog("[BREAK][CLIENT][SKIP]", "pending rollback", cx, cy, cz, x, y, z)
|
||||
return
|
||||
end
|
||||
pendingBreaks[chunkKey] = pendingBreaks[chunkKey] or {}
|
||||
@@ -252,6 +482,7 @@ function PlacementManager:BreakBlock(cx, cy, cz, x, y, z)
|
||||
chunk:RemoveBlock(x, y, z)
|
||||
end
|
||||
scheduleBreakRollback(cx, cy, cz, x, y, z)
|
||||
debugPlacementLog("[BREAK][CLIENT][SEND]", cx, cy, cz, x, y, z)
|
||||
breakRemote:FireServer(cx, cy, cz, x, y, z)
|
||||
end
|
||||
|
||||
@@ -283,28 +514,54 @@ local function applyBreakBlockLocal(cx, cy, cz, x, y, z)
|
||||
end
|
||||
|
||||
function PlacementManager:GetBlockAtMouse(): nil | {chunk:Vector3, block: Vector3}
|
||||
pcall(function()
|
||||
PlacementManager:Raycast()
|
||||
end)
|
||||
local selectedPart = PlacementManager:RaycastGetResult()
|
||||
--print(selectedPart and selectedPart:GetFullName() or nil)
|
||||
if selectedPart == nil then
|
||||
PlacementManager.SelectionBox.Adornee = nil
|
||||
script.RaycastResult.Value = nil
|
||||
lastNormalId = nil
|
||||
debugPlacementLog("[PLACE][CLIENT][TARGET]", "no selectedPart after raycast", lastRaycastFailure)
|
||||
return nil
|
||||
end
|
||||
if not selectedPart.Parent then
|
||||
PlacementManager.SelectionBox.Adornee = nil
|
||||
script.RaycastResult.Value = nil
|
||||
lastNormalId = nil
|
||||
local chunkName, blockName = findChunkAndBlock(selectedPart)
|
||||
if not chunkName or not blockName then
|
||||
debugPlacementWarn(
|
||||
"[PLACE][CLIENT][TARGET]",
|
||||
"failed to find chunk/block from selection",
|
||||
selectedPart:GetFullName()
|
||||
)
|
||||
return nil
|
||||
end
|
||||
if not selectedPart.Parent then
|
||||
PlacementManager.SelectionBox.Adornee = nil
|
||||
script.RaycastResult.Value = nil
|
||||
lastNormalId = nil
|
||||
|
||||
local okChunk, chunkCoords = pcall(function()
|
||||
return Util.BlockPosStringToCoords(chunkName :: string)
|
||||
end)
|
||||
local okBlock, blockCoords = pcall(function()
|
||||
return Util.BlockPosStringToCoords(blockName :: string)
|
||||
end)
|
||||
if not okChunk or not okBlock then
|
||||
debugPlacementWarn(
|
||||
"[PLACE][CLIENT][TARGET]",
|
||||
"failed to parse names",
|
||||
"chunkName",
|
||||
chunkName,
|
||||
"blockName",
|
||||
blockName
|
||||
)
|
||||
return nil
|
||||
end
|
||||
local chunkCoords = Util.BlockPosStringToCoords(selectedPart.Parent.Name)
|
||||
local blockCoords = Util.BlockPosStringToCoords(selectedPart.Name)
|
||||
debugPlacementLog(
|
||||
"[PLACE][CLIENT][TARGET]",
|
||||
"chunk",
|
||||
chunkName,
|
||||
"block",
|
||||
blockName,
|
||||
"normal",
|
||||
(lastNormalId and lastNormalId.Name) or "nil"
|
||||
)
|
||||
|
||||
return {
|
||||
chunk = chunkCoords,
|
||||
@@ -334,12 +591,33 @@ function PlacementManager:GetPlacementAtMouse(): nil | {chunk:Vector3, block: Ve
|
||||
end
|
||||
local offset = normalIdToOffset(hit.normal)
|
||||
local placeChunk, placeBlock = offsetChunkBlock(hit.chunk, hit.block, offset)
|
||||
debugPlacementLog(
|
||||
"[PLACE][CLIENT][PLACE_TARGET]",
|
||||
"target chunk",
|
||||
hit.chunk,
|
||||
"target block",
|
||||
hit.block,
|
||||
"normal",
|
||||
hit.normal.Name,
|
||||
"place chunk",
|
||||
placeChunk,
|
||||
"place block",
|
||||
placeBlock
|
||||
)
|
||||
return {
|
||||
chunk = placeChunk,
|
||||
block = placeBlock
|
||||
}
|
||||
end
|
||||
|
||||
function PlacementManager:DebugGetPlacementOrWarn()
|
||||
local placement = PlacementManager:GetPlacementAtMouse()
|
||||
if not placement then
|
||||
debugPlacementWarn("[PLACE][CLIENT][REJECT]", "no placement target under mouse", lastRaycastFailure)
|
||||
end
|
||||
return placement
|
||||
end
|
||||
|
||||
function PlacementManager:Init()
|
||||
game:GetService("RunService").RenderStepped:Connect(function()
|
||||
local a,b = pcall(function()
|
||||
|
||||
Reference in New Issue
Block a user