chore: init
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,38 @@
|
||||
local Node = {}
|
||||
Node.__index = Node
|
||||
|
||||
function Node.new(data)
|
||||
local node = setmetatable({}, Node)
|
||||
node.data = data
|
||||
node.left = nil
|
||||
node.right = nil
|
||||
return node
|
||||
end
|
||||
|
||||
-- return an iterator that traverses the tree in order
|
||||
function Node:inorder()
|
||||
local stack = {}
|
||||
local current = {self, ""}
|
||||
table.insert(stack, current)
|
||||
|
||||
return function()
|
||||
while current[1].left do
|
||||
local parent = current
|
||||
current = {parent[1].left, parent[2] .. "0"}
|
||||
table.insert(stack, current)
|
||||
end
|
||||
|
||||
if #stack > 0 then
|
||||
local node = table.remove(stack)
|
||||
|
||||
if node[1].right then
|
||||
local parent = node
|
||||
current = {parent[1].right, parent[2] .. "1"}
|
||||
table.insert(stack, current)
|
||||
end
|
||||
return node[1], node[2]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return Node
|
||||
@@ -0,0 +1,232 @@
|
||||
--[[
|
||||
|
||||
PriorityQueue - v1.0.1 - public domain Lua priority queue
|
||||
implemented with indirect binary heap
|
||||
no warranty implied; use at your own risk
|
||||
|
||||
based on binaryheap library (github.com/iskolbin/binaryheap)
|
||||
|
||||
author: Ilya Kolbin (iskolbin@gmail.com)
|
||||
url: github.com/iskolbin/priorityqueue
|
||||
|
||||
See documentation in README file.
|
||||
|
||||
COMPATIBILITY
|
||||
|
||||
Lua 5.1, 5.2, 5.3, LuaJIT 1, 2
|
||||
|
||||
LICENSE
|
||||
|
||||
This software is dual-licensed to the public domain and under the following
|
||||
license: you are granted a perpetual, irrevocable license to copy, modify,
|
||||
publish, and distribute this file as you see fit.
|
||||
|
||||
--]]
|
||||
|
||||
local floor, setmetatable = math.floor, setmetatable
|
||||
|
||||
local function siftup( self, from )
|
||||
local items, priorities, indices, higherpriority = self, self._priorities, self._indices, self._higherpriority
|
||||
local index = from
|
||||
local parent = floor( index / 2 )
|
||||
while index > 1 and higherpriority( priorities[index], priorities[parent] ) do
|
||||
priorities[index], priorities[parent] = priorities[parent], priorities[index]
|
||||
items[index], items[parent] = items[parent], items[index]
|
||||
indices[items[index]], indices[items[parent]] = index, parent
|
||||
index = parent
|
||||
parent = floor( index / 2 )
|
||||
end
|
||||
return index
|
||||
end
|
||||
|
||||
local function siftdown( self, limit )
|
||||
local items, priorities, indices, higherpriority, size = self, self._priorities, self._indices, self._higherpriority, self._size
|
||||
for index = limit, 1, -1 do
|
||||
local left = index + index
|
||||
local right = left + 1
|
||||
while left <= size do
|
||||
local smaller = left
|
||||
if right <= size and higherpriority( priorities[right], priorities[left] ) then
|
||||
smaller = right
|
||||
end
|
||||
if higherpriority( priorities[smaller], priorities[index] ) then
|
||||
items[index], items[smaller] = items[smaller], items[index]
|
||||
priorities[index], priorities[smaller] = priorities[smaller], priorities[index]
|
||||
indices[items[index]], indices[items[smaller]] = index, smaller
|
||||
else
|
||||
break
|
||||
end
|
||||
index = smaller
|
||||
left = index + index
|
||||
right = left + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local PriorityQueueMt
|
||||
|
||||
local PriorityQueue = {}
|
||||
|
||||
local function minishigher( a, b )
|
||||
return a < b
|
||||
end
|
||||
|
||||
local function maxishigher( a, b )
|
||||
return a > b
|
||||
end
|
||||
|
||||
function PriorityQueue.new( priority_or_array )
|
||||
local t = type( priority_or_array )
|
||||
local higherpriority = minishigher
|
||||
|
||||
if t == 'table' then
|
||||
higherpriority = priority_or_array.higherpriority or higherpriority
|
||||
elseif t == 'function' or t == 'string' then
|
||||
higherpriority = priority_or_array
|
||||
elseif t ~= 'nil' then
|
||||
local msg = 'Wrong argument type to PriorityQueue.new, it must be table or function or string, has: %q'
|
||||
error( msg:format( t ))
|
||||
end
|
||||
|
||||
if type( higherpriority ) == 'string' then
|
||||
if higherpriority == 'min' then
|
||||
higherpriority = minishigher
|
||||
elseif higherpriority == 'max' then
|
||||
higherpriority = maxishigher
|
||||
else
|
||||
local msg = 'Wrong string argument to PriorityQueue.new, it must be "min" or "max", has: %q'
|
||||
error( msg:format( tostring( higherpriority )))
|
||||
end
|
||||
end
|
||||
|
||||
local self = setmetatable( {
|
||||
_priorities = {},
|
||||
_indices = {},
|
||||
_size = 0,
|
||||
_higherpriority = higherpriority or minishigher
|
||||
}, PriorityQueueMt )
|
||||
|
||||
if t == 'table' then
|
||||
self:batchenq( priority_or_array )
|
||||
end
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
function PriorityQueue:enqueue( item, priority )
|
||||
local items, priorities, indices = self, self._priorities, self._indices
|
||||
if indices[item] ~= nil then
|
||||
error( 'Item ' .. tostring(indices[item]) .. ' is already in the heap' )
|
||||
end
|
||||
local size = self._size + 1
|
||||
self._size = size
|
||||
items[size], priorities[size], indices[item] = item, priority, size
|
||||
siftup( self, size )
|
||||
return self
|
||||
end
|
||||
|
||||
function PriorityQueue:remove( item )
|
||||
local index = self._indices[item]
|
||||
if index ~= nil then
|
||||
local size = self._size
|
||||
local items, priorities, indices = self, self._priorities, self._indices
|
||||
indices[item] = nil
|
||||
if size == index then
|
||||
items[size], priorities[size] = nil, nil
|
||||
self._size = size - 1
|
||||
else
|
||||
local lastitem = items[size]
|
||||
items[index], priorities[index] = items[size], priorities[size]
|
||||
items[size], priorities[size] = nil, nil
|
||||
indices[lastitem] = index
|
||||
size = size - 1
|
||||
self._size = size
|
||||
if size > 1 then
|
||||
siftdown( self, siftup( self, index ))
|
||||
end
|
||||
end
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
function PriorityQueue:contains( item )
|
||||
return self._indices[item] ~= nil
|
||||
end
|
||||
|
||||
function PriorityQueue:update( item, priority )
|
||||
local ok = self:remove( item )
|
||||
if ok then
|
||||
self:enqueue( item, priority )
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
function PriorityQueue:dequeue()
|
||||
local size = self._size
|
||||
|
||||
assert( size > 0, 'Heap is empty' )
|
||||
|
||||
local items, priorities, indices = self, self._priorities, self._indices
|
||||
local item, priority = items[1], priorities[1]
|
||||
indices[item] = nil
|
||||
|
||||
if size > 1 then
|
||||
local newitem = items[size]
|
||||
items[1], priorities[1] = newitem, priorities[size]
|
||||
items[size], priorities[size] = nil, nil
|
||||
indices[newitem] = 1
|
||||
size = size - 1
|
||||
self._size = size
|
||||
siftdown( self, 1 )
|
||||
else
|
||||
items[1], priorities[1] = nil, nil
|
||||
self._size = 0
|
||||
end
|
||||
|
||||
return item, priority
|
||||
end
|
||||
|
||||
function PriorityQueue:peek()
|
||||
return self[1], self._priorities[1]
|
||||
end
|
||||
|
||||
function PriorityQueue:len()
|
||||
return self._size
|
||||
end
|
||||
|
||||
function PriorityQueue:empty()
|
||||
return self._size <= 0
|
||||
end
|
||||
|
||||
function PriorityQueue:batchenq( iparray )
|
||||
local items, priorities, indices = self, self._priorities, self._indices
|
||||
local size = self._size
|
||||
for i = 1, #iparray, 2 do
|
||||
local item, priority = iparray[i], iparray[i+1]
|
||||
if indices[item] ~= nil then
|
||||
error( 'Item ' .. tostring(indices[item]) .. ' is already in the heap' )
|
||||
end
|
||||
size = size + 1
|
||||
items[size], priorities[size] = item, priority
|
||||
indices[item] = size
|
||||
end
|
||||
self._size = size
|
||||
if size > 1 then
|
||||
siftdown( self, floor( size / 2 ))
|
||||
end
|
||||
end
|
||||
|
||||
PriorityQueueMt = {
|
||||
__index = PriorityQueue,
|
||||
__len = PriorityQueue.len,
|
||||
}
|
||||
|
||||
return setmetatable( PriorityQueue, {
|
||||
__call = function( _, ... )
|
||||
return PriorityQueue.new( ... )
|
||||
end
|
||||
} )
|
||||
@@ -0,0 +1,114 @@
|
||||
--Huffman.lua
|
||||
--iiau, Sat May 18 2024
|
||||
--Implementation of huffman coding algorithm for use in Roblox
|
||||
|
||||
local Huffman = {}
|
||||
local PriorityQueue = require(script.PriorityQueue)
|
||||
local Node = require(script.Node)
|
||||
local BitBuffer = require(script.BitBuffer)
|
||||
|
||||
local CHUNK_SIZE = 256
|
||||
|
||||
--thanks to https://stackoverflow.com/a/32220398 for helping me with this
|
||||
local function to_bin(n)
|
||||
local t = {}
|
||||
for _ = 1, 32 do
|
||||
n = bit32.rrotate(n, -1)
|
||||
table.insert(t, bit32.band(n, 1))
|
||||
end
|
||||
return table.concat(t)
|
||||
end
|
||||
|
||||
-- Encode a string to huffman coded string. Limitation is that the data should not be more than 16777215 bytes.
|
||||
-- @param data The string to encode
|
||||
-- @return The encoded string
|
||||
Huffman.encode = function(data: string) : string
|
||||
assert(#data > 0, "Data must not be empty")
|
||||
local buffer = BitBuffer()
|
||||
|
||||
-- get the frequency of each character in the string
|
||||
local freq, dict, size = {}, {}, 0
|
||||
for c in data:gmatch(".") do
|
||||
freq[c] = (freq[c] or 0) + 1
|
||||
end
|
||||
for _ in pairs(freq) do
|
||||
size += 1
|
||||
end
|
||||
|
||||
local q = PriorityQueue.new 'min'
|
||||
for k: string, v: number in pairs(freq) do
|
||||
local leaf = Node.new(string.byte(k))
|
||||
q:enqueue(leaf, v)
|
||||
end
|
||||
|
||||
while q:len() > 1 do
|
||||
local left, freq_l = q:dequeue()
|
||||
local right, freq_r = q:dequeue()
|
||||
local parent = Node.new()
|
||||
parent.left = left
|
||||
parent.right = right
|
||||
|
||||
q:enqueue(parent, freq_l + freq_r)
|
||||
end
|
||||
local tree = q:dequeue()
|
||||
buffer.writeUInt8(size-1)
|
||||
buffer.writeUnsigned(24, #data)
|
||||
for node, bits: string in tree:inorder() do
|
||||
if not node.data then
|
||||
continue
|
||||
end
|
||||
local number = tonumber(bits, 2)
|
||||
local bit_array = string.split(bits, "")
|
||||
for i = 1, #bit_array do
|
||||
bit_array[i] = tonumber(bit_array[i])
|
||||
end
|
||||
|
||||
dict[string.char(node.data)] = bit_array
|
||||
buffer.writeUInt8(node.data) -- char
|
||||
buffer.writeUnsigned(5, #bits) -- number of bits
|
||||
buffer.writeUnsigned(#bits, number) -- bits
|
||||
end
|
||||
for c in data:gmatch(".") do
|
||||
buffer.writeBits(table.unpack(dict[c]))
|
||||
end
|
||||
|
||||
-- to avoid the dreaded too many results to unpack error
|
||||
local chunks = {}
|
||||
for _, chunk in buffer.exportChunk(CHUNK_SIZE) do
|
||||
table.insert(chunks, chunk)
|
||||
end
|
||||
return table.concat(chunks)
|
||||
end
|
||||
|
||||
-- Decode a string from huffman coded string
|
||||
-- @param data The string to decode
|
||||
-- @return The decoded string
|
||||
Huffman.decode = function(data: string) : string
|
||||
assert(#data > 0, "Data must not be empty")
|
||||
local buffer = BitBuffer(data)
|
||||
|
||||
local dict_size = buffer.readUInt8()+1
|
||||
local len_data = buffer.readUnsigned(24)
|
||||
local dict, read = {}, 0
|
||||
|
||||
for i = 1, dict_size do
|
||||
local char = buffer.readUInt8()
|
||||
local digits = buffer.readUnsigned(5)
|
||||
local bits = buffer.readUnsigned(digits)
|
||||
dict[to_bin(bits):sub(-digits)] = char
|
||||
end
|
||||
local decoded = {}
|
||||
local bits = ""
|
||||
while read < len_data do
|
||||
bits ..= buffer.readBits(1)[1]
|
||||
local char = dict[bits]
|
||||
if char then
|
||||
table.insert(decoded, string.char(char))
|
||||
bits = ""
|
||||
read += 1
|
||||
end
|
||||
end
|
||||
return table.concat(decoded)
|
||||
end
|
||||
|
||||
return Huffman
|
||||
@@ -0,0 +1,170 @@
|
||||
-- Module by 1waffle1 and boatbomber, optimized and fixed by iiau
|
||||
-- https://devforum.roblox.com/t/text-compression/163637/37
|
||||
|
||||
local dictionary = {} -- key to len
|
||||
|
||||
do -- populate dictionary
|
||||
local length = 0
|
||||
for i = 32, 127 do
|
||||
if i ~= 34 and i ~= 92 then
|
||||
local c = string.char(i)
|
||||
dictionary[c] = length
|
||||
dictionary[length] = c
|
||||
length = length + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local escapemap_126, escapemap_127 = {}, {}
|
||||
local unescapemap_126, unescapemap_127 = {}, {}
|
||||
|
||||
local blacklisted_126 = { 34, 92 }
|
||||
for i = 126, 180 do
|
||||
table.insert(blacklisted_126, i)
|
||||
end
|
||||
|
||||
do -- Populate escape map
|
||||
-- represents the numbers 0-31, 34, 92, 126, 127 (36 characters)
|
||||
-- and 128-180 (52 characters)
|
||||
-- https://devforum.roblox.com/t/text-compression/163637/5
|
||||
for i = 0, 31 + #blacklisted_126 do
|
||||
local b = blacklisted_126[i - 31]
|
||||
local s = i + 32
|
||||
|
||||
-- Note: 126 and 127 are magic numbers
|
||||
local c = string.char(b or i)
|
||||
local e = string.char(s + (s >= 34 and 1 or 0) + (s >= 91 and 1 or 0))
|
||||
|
||||
escapemap_126[c] = e
|
||||
unescapemap_126[e] = c
|
||||
end
|
||||
|
||||
for i = 1, 255 - 180 do
|
||||
local c = string.char(i + 180)
|
||||
local s = i + 34
|
||||
local e = string.char(s + (s >= 92 and 1 or 0))
|
||||
|
||||
escapemap_127[c] = e
|
||||
unescapemap_127[e] = c
|
||||
end
|
||||
end
|
||||
|
||||
local function escape(s)
|
||||
-- escape the control characters 0-31, double quote 34, backslash 92, Tilde 126, and DEL 127 (36 chars)
|
||||
-- escape characters 128-180 (53 chars)
|
||||
return string.gsub(string.gsub(s, '[%c"\\\126-\180]', function(c)
|
||||
return "\126" .. escapemap_126[c]
|
||||
end), '[\181-\255]', function(c)
|
||||
return "\127" .. escapemap_127[c]
|
||||
end)
|
||||
end
|
||||
local function unescape(s)
|
||||
return string.gsub(string.gsub(s, "\127(.)", function(e)
|
||||
return unescapemap_127[e]
|
||||
end), "\126(.)", function(e)
|
||||
return unescapemap_126[e]
|
||||
end)
|
||||
end
|
||||
|
||||
local b93Cache = {}
|
||||
local function tobase93(n)
|
||||
local value = b93Cache[n]
|
||||
if value then
|
||||
return value
|
||||
end
|
||||
|
||||
local c = n
|
||||
value = ""
|
||||
repeat
|
||||
local remainder = n % 93
|
||||
value = dictionary[remainder] .. value
|
||||
n = (n - remainder) / 93
|
||||
until n == 0
|
||||
|
||||
b93Cache[c] = value
|
||||
return value
|
||||
end
|
||||
|
||||
local b10Cache = {}
|
||||
local function tobase10(value)
|
||||
local n = b10Cache[value]
|
||||
if n then
|
||||
return n
|
||||
end
|
||||
|
||||
n = 0
|
||||
for i = 1, #value do
|
||||
n = n + math.pow(93, i - 1) * dictionary[string.sub(value, -i, -i)]
|
||||
end
|
||||
|
||||
b10Cache[value] = n
|
||||
return n
|
||||
end
|
||||
|
||||
local function compress(text)
|
||||
assert(type(text) == "string", "bad argument #1 to 'compress' (string expected, got " .. typeof(text) .. ")")
|
||||
local dictionaryCopy = table.clone(dictionary)
|
||||
local key, sequence, size = "", {}, #dictionary
|
||||
|
||||
local width, spans, span = 1, {}, 0
|
||||
local function listkey(k)
|
||||
local value = tobase93(dictionaryCopy[k])
|
||||
local valueLength = #value
|
||||
if valueLength > width then
|
||||
width, span, spans[width] = valueLength, 0, span
|
||||
end
|
||||
table.insert(sequence, string.rep(" ", width - valueLength) .. value)
|
||||
span += 1
|
||||
end
|
||||
text = escape(text)
|
||||
for i = 1, #text do
|
||||
local c = string.sub(text, i, i)
|
||||
local new = key .. c
|
||||
if dictionaryCopy[new] then
|
||||
key = new
|
||||
else
|
||||
listkey(key)
|
||||
key = c
|
||||
size += 1
|
||||
dictionaryCopy[new] = size
|
||||
dictionaryCopy[size] = new
|
||||
end
|
||||
end
|
||||
listkey(key)
|
||||
spans[width] = span
|
||||
return table.concat(spans, ",") .. "|" .. table.concat(sequence)
|
||||
end
|
||||
|
||||
local function decompress(text)
|
||||
assert(type(text) == "string", "bad argument #1 to 'decompress' (string expected, got " .. typeof(text) .. ")")
|
||||
local dictionaryCopy = table.clone(dictionary)
|
||||
local sequence, spans, content = {}, string.match(text, "(.-)|(.*)")
|
||||
local groups, start = {}, 1
|
||||
for span in string.gmatch(spans, "%d+") do
|
||||
local width = #groups + 1
|
||||
groups[width] = string.sub(content, start, start + span * width - 1)
|
||||
start = start + span * width
|
||||
end
|
||||
local previous
|
||||
|
||||
for width, group in ipairs(groups) do
|
||||
for value in string.gmatch(group, string.rep(".", width)) do
|
||||
local entry = dictionaryCopy[tobase10(value)]
|
||||
if previous then
|
||||
if entry then
|
||||
table.insert(dictionaryCopy, previous .. string.sub(entry, 1, 1))
|
||||
else
|
||||
entry = previous .. string.sub(previous, 1, 1)
|
||||
table.insert(dictionaryCopy, entry)
|
||||
end
|
||||
table.insert(sequence, entry)
|
||||
else
|
||||
sequence[1] = entry
|
||||
end
|
||||
previous = entry
|
||||
end
|
||||
end
|
||||
return unescape(table.concat(sequence))
|
||||
end
|
||||
|
||||
return { compress = compress, decompress = decompress }
|
||||
@@ -0,0 +1,19 @@
|
||||
--Deflate.lua
|
||||
--iiau, Sat May 18 2024
|
||||
|
||||
local Deflate = {}
|
||||
|
||||
local LZW = require(script.LZW)
|
||||
local Huffman = require(script.Huffman)
|
||||
|
||||
Deflate.encode = function(data: string) : string
|
||||
data = LZW.compress(data)
|
||||
return Huffman.encode(data)
|
||||
end
|
||||
|
||||
Deflate.decode = function(data: string) : string
|
||||
data = Huffman.decode(data)
|
||||
return LZW.decompress(data)
|
||||
end
|
||||
|
||||
return Deflate
|
||||
@@ -0,0 +1,70 @@
|
||||
local TerrainGen = {}
|
||||
|
||||
local deflate = require("./TerrainGen/Deflate")
|
||||
|
||||
local DSS = game:GetService("DataStoreService")
|
||||
local WORLDNAME = "DEFAULT"
|
||||
local WORLDID = "b73bb5a6-297d-4352-b637-daec7e8c8f3e"
|
||||
local Store = DSS:GetDataStore("BlockscraftWorldV1", WORLDID)
|
||||
|
||||
local ChunkManager = require(game:GetService("ReplicatedStorage"):WaitForChild("Shared").ChunkManager)
|
||||
local Chunk = require(game:GetService("ReplicatedStorage"):WaitForChild("Shared").ChunkManager.Chunk)
|
||||
|
||||
TerrainGen.ServerChunkCache = {} :: {[typeof("")]: typeof(Chunk.new(0,0,0))}
|
||||
|
||||
-- Load a chunk from the DataStore or generate it if not found
|
||||
function TerrainGen:GetChunk(x, y, z)
|
||||
local key = `{x},{y},{z}`
|
||||
if TerrainGen.ServerChunkCache[key] then
|
||||
return TerrainGen.ServerChunkCache[key]
|
||||
end
|
||||
|
||||
-- Generate a new chunk if it doesn't exist
|
||||
local chunk = Chunk.new(x, y, z)
|
||||
if y == 1 then
|
||||
for cx = 1, 8 do
|
||||
for cz = 1, 8 do
|
||||
--local perlin = math.noise(((x*8)+cx)/100,((z*8)+cz)/100)
|
||||
chunk:CreateBlock(cx, 1, cz, { id = 1, state = {} })
|
||||
--chunk:CreateBlock(x, 2, z, { id = 1, state = {} })
|
||||
end
|
||||
end
|
||||
end
|
||||
if y == 0 then
|
||||
for cx = 1, 8 do
|
||||
for cy = 1, 8 do
|
||||
for cz = 1, 8 do
|
||||
--local perlin = math.noise(((x*8)+cx)/100,((z*8)+cz)/100)
|
||||
chunk:CreateBlock(cx, cy, cz, { id = 2, state = {} })
|
||||
--chunk:CreateBlock(x, 2, z, { id = 1, state = {} })
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
TerrainGen.ServerChunkCache[key] = chunk
|
||||
return chunk
|
||||
end
|
||||
|
||||
-- Fake Chunk
|
||||
function TerrainGen:GetFakeChunk(x, y, z)
|
||||
|
||||
-- Generate a new chunk if it doesn't exist
|
||||
local chunk = Chunk.new(x, y, z)
|
||||
for cy = 1,8 do
|
||||
for cx = 1, 8 do
|
||||
for cz = 1, 8 do
|
||||
--local perlin = math.noise(((x*8)+cx)/100,((z*8)+cz)/100)
|
||||
chunk:CreateBlock(cx, cy, cz, { id = -2, state = {} })
|
||||
--chunk:CreateBlock(x, 2, z, { id = 1, state = {} })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return chunk
|
||||
end
|
||||
|
||||
|
||||
TerrainGen.CM = ChunkManager
|
||||
|
||||
return TerrainGen
|
||||
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()
|
||||
4
src/ServerScriptService/Actor/init.meta.json
Normal file
4
src/ServerScriptService/Actor/init.meta.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"className": "Actor",
|
||||
"ignoreUnknownInstances": true
|
||||
}
|
||||
Reference in New Issue
Block a user