chore: init
This commit is contained in:
189
src/ServerScriptService/Actor/ServerChunkManager/init.server.lua
Normal file
189
src/ServerScriptService/Actor/ServerChunkManager/init.server.lua
Normal file
@@ -0,0 +1,189 @@
|
||||
print("Hello world!")
|
||||
|
||||
task.synchronize()
|
||||
|
||||
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||
|
||||
|
||||
local Shared = ReplicatedStorage:WaitForChild("Shared")
|
||||
local ModsFolder = ReplicatedStorage:WaitForChild("Mods")
|
||||
|
||||
local Util = require(Shared.Util)
|
||||
local TG = require("./ServerChunkManager/TerrainGen")
|
||||
|
||||
do
|
||||
local workspaceModFolder = game:GetService("Workspace"):WaitForChild("mods")
|
||||
|
||||
for _,v in pairs(workspaceModFolder:GetChildren()) do
|
||||
v.Parent = ModsFolder
|
||||
end
|
||||
workspaceModFolder:Destroy()
|
||||
end
|
||||
|
||||
local ML = require(Shared.ModLoader)
|
||||
ML.loadModsS()
|
||||
|
||||
do
|
||||
local bv = Instance.new("BoolValue")
|
||||
bv.Name = "MLLoaded"
|
||||
bv.Value = true
|
||||
bv.Parent = ReplicatedStorage:WaitForChild("Objects")
|
||||
end
|
||||
|
||||
local MAX_CHUNK_DIST = 200
|
||||
|
||||
local FakeChunk = TG:GetFakeChunk(-5,-5,-5)
|
||||
|
||||
task.synchronize()
|
||||
|
||||
ReplicatedStorage.Tick.OnServerEvent:Connect(function(player: Player, v: string)
|
||||
if TG.ServerChunkCache[v] then
|
||||
pcall(function()
|
||||
TG.ServerChunkCache[v].inhabitedTime = tick()
|
||||
end)
|
||||
end
|
||||
end)
|
||||
|
||||
ReplicatedStorage.RecieveChunkPacket.OnServerInvoke = function(plr: Player, x: number, y: number, z: number)
|
||||
-- validate xyz type and limit
|
||||
if typeof(x) ~= "number" or typeof(y) ~= "number" or typeof(z) ~= "number" then
|
||||
return {}
|
||||
end
|
||||
|
||||
if math.abs(x) > MAX_CHUNK_DIST or math.abs(y) > MAX_CHUNK_DIST or math.abs(z) > MAX_CHUNK_DIST then
|
||||
return FakeChunk.data
|
||||
end
|
||||
|
||||
task.desynchronize()
|
||||
local chunk = TG:GetChunk(x, y, z)
|
||||
local chunkdata = chunk.data
|
||||
task.synchronize()
|
||||
|
||||
return chunkdata
|
||||
|
||||
end
|
||||
|
||||
local tickRemote = ReplicatedStorage.Tick
|
||||
local remotes = ReplicatedStorage:WaitForChild("Remotes")
|
||||
local placeRemote = remotes:WaitForChild("PlaceBlock")
|
||||
local breakRemote = remotes:WaitForChild("BreakBlock")
|
||||
local blocksFolder = ReplicatedStorage:WaitForChild("Blocks")
|
||||
local function propogate(a, cx, cy, cz, x, y, z, bd)
|
||||
task.synchronize()
|
||||
tickRemote:FireAllClients(a, cx, cy, cz, x, y, z, bd)
|
||||
task.desynchronize()
|
||||
end
|
||||
|
||||
local MAX_REACH = 24
|
||||
local blockIdMap = {}
|
||||
|
||||
local function rebuildBlockIdMap()
|
||||
table.clear(blockIdMap)
|
||||
for _, block in ipairs(blocksFolder:GetChildren()) do
|
||||
local id = block:GetAttribute("n")
|
||||
if id ~= nil then
|
||||
blockIdMap[id] = id
|
||||
blockIdMap[tostring(id)] = id
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
rebuildBlockIdMap()
|
||||
blocksFolder.ChildAdded:Connect(rebuildBlockIdMap)
|
||||
blocksFolder.ChildRemoved:Connect(rebuildBlockIdMap)
|
||||
|
||||
local function getPlayerPosition(player: Player): Vector3?
|
||||
local character = player.Character
|
||||
if not character then
|
||||
return nil
|
||||
end
|
||||
local root = character:FindFirstChild("HumanoidRootPart")
|
||||
if not root then
|
||||
return nil
|
||||
end
|
||||
return root.Position
|
||||
end
|
||||
|
||||
local function isWithinReach(player: Player, cx: number, cy: number, cz: number, x: number, y: number, z: number): boolean
|
||||
local playerPos = getPlayerPosition(player)
|
||||
if not playerPos then
|
||||
return false
|
||||
end
|
||||
local blockPos = Util.ChunkPosToCFrame(Vector3.new(cx, cy, cz), Vector3.new(x, y, z)).Position
|
||||
return (blockPos - playerPos).Magnitude <= MAX_REACH
|
||||
end
|
||||
|
||||
local function resolveBlockId(blockId: any): string | number | nil
|
||||
return blockIdMap[blockId]
|
||||
end
|
||||
|
||||
local function getServerChunk(cx: number, cy: number, cz: number)
|
||||
task.desynchronize()
|
||||
local chunk = TG:GetChunk(cx, cy, cz)
|
||||
task.synchronize()
|
||||
return chunk
|
||||
end
|
||||
|
||||
placeRemote.OnServerEvent:Connect(function(player, cx, cy, cz, x, y, z, blockId)
|
||||
--print("place",player, cx, cy, cz, x, y, z, blockData)
|
||||
|
||||
if typeof(cx) ~= "number" or typeof(cy) ~= "number" or typeof(cz) ~= "number" then
|
||||
return
|
||||
end
|
||||
if typeof(x) ~= "number" or typeof(y) ~= "number" or typeof(z) ~= "number" then
|
||||
return
|
||||
end
|
||||
if x < 1 or x > 8 or y < 1 or y > 8 or z < 1 or z > 8 then
|
||||
return
|
||||
end
|
||||
if math.abs(cx) > MAX_CHUNK_DIST or math.abs(cy) > MAX_CHUNK_DIST or math.abs(cz) > MAX_CHUNK_DIST then
|
||||
--return
|
||||
end
|
||||
if not isWithinReach(player, cx, cy, cz, x, y, z) then
|
||||
return
|
||||
end
|
||||
local resolvedId = resolveBlockId(blockId)
|
||||
if not resolvedId then
|
||||
return
|
||||
end
|
||||
|
||||
local chunk = getServerChunk(cx, cy, cz)
|
||||
if chunk:GetBlockAt(x, y, z) then
|
||||
return
|
||||
end
|
||||
local data = {
|
||||
id = resolvedId,
|
||||
state = {}
|
||||
}
|
||||
chunk:CreateBlock(x, y, z, data)
|
||||
propogate("B_C", cx, cy, cz, x, y, z, data)
|
||||
end)
|
||||
|
||||
breakRemote.OnServerEvent:Connect(function(player, cx, cy, cz, x, y, z)
|
||||
--print("del",player, cx, cy, cz, x, y, z)
|
||||
|
||||
if typeof(cx) ~= "number" or typeof(cy) ~= "number" or typeof(cz) ~= "number" then
|
||||
return
|
||||
end
|
||||
if typeof(x) ~= "number" or typeof(y) ~= "number" or typeof(z) ~= "number" then
|
||||
return
|
||||
end
|
||||
if x < 1 or x > 8 or y < 1 or y > 8 or z < 1 or z > 8 then
|
||||
return
|
||||
end
|
||||
if math.abs(cx) > MAX_CHUNK_DIST or math.abs(cy) > MAX_CHUNK_DIST or math.abs(cz) > MAX_CHUNK_DIST then
|
||||
return
|
||||
end
|
||||
if not isWithinReach(player, cx, cy, cz, x, y, z) then
|
||||
return
|
||||
end
|
||||
|
||||
local chunk = getServerChunk(cx, cy, cz)
|
||||
if not chunk:GetBlockAt(x, y, z) then
|
||||
return
|
||||
end
|
||||
chunk:RemoveBlock(x, y, z)
|
||||
propogate("B_D", cx, cy, cz, x, y, z, 0)
|
||||
end)
|
||||
|
||||
task.desynchronize()
|
||||
Reference in New Issue
Block a user