holy fuck it works
This commit is contained in:
parent
970c3c9d36
commit
64cba49392
5 changed files with 3749 additions and 28 deletions
3605
mc/LibDeflate.lua
Normal file
3605
mc/LibDeflate.lua
Normal file
File diff suppressed because it is too large
Load diff
|
@ -137,6 +137,12 @@ function bufferStuff.Reader:readByte()
|
|||
return value
|
||||
end
|
||||
|
||||
function bufferStuff.Reader:readBool()
|
||||
local value = string.unpack(">b", string.sub(self.bufferData, self.offset, self.offset + 1))
|
||||
self.offset = self.offset + 1
|
||||
return value >= 1
|
||||
end
|
||||
|
||||
function bufferStuff.Reader:readUByte()
|
||||
local value = string.unpack(">B", string.sub(self.bufferData, self.offset, self.offset + 1))
|
||||
self.offset = self.offset + 1
|
||||
|
|
24
mc/chunk.lua
24
mc/chunk.lua
|
@ -1,14 +1,22 @@
|
|||
local util = require("util")
|
||||
local nibbleArray = require("nibbleArray")
|
||||
local bit32 = require("bit32")
|
||||
|
||||
local MAX_HEIGHT = 128
|
||||
|
||||
local chunk = {}
|
||||
|
||||
local defaultsTable = {
|
||||
blocks = {}
|
||||
blocks = {},
|
||||
metadata = {},
|
||||
blockLight = {},
|
||||
skyLight = {}
|
||||
}
|
||||
|
||||
function chunk:CreateCoordPair(x, z)
|
||||
return bit32.bor((x >= 0 and 0 or 2147483648), bit32.bor(bit32.rshift(bit32.band(x, 0x7fff), -16), bit32.bor((z >= 0 and 0 or 0x8000), bit32.band(z, 0x7fff))))
|
||||
end
|
||||
|
||||
function chunk:new()
|
||||
local newTable = util.deepCopyTable(defaultsTable) or {}
|
||||
|
||||
|
@ -16,7 +24,11 @@ function chunk:new()
|
|||
for i = 1, blockBufferSize do
|
||||
table.insert(newTable.blocks, 0)
|
||||
end
|
||||
print(#newTable.blocks)
|
||||
for i = 1, blockBufferSize / 2 do
|
||||
table.insert(newTable.metadata, 0)
|
||||
table.insert(newTable.blockLight, 255)
|
||||
table.insert(newTable.skyLight, 255)
|
||||
end
|
||||
|
||||
-- Set this table as this """class"""'s metatable
|
||||
setmetatable(newTable, self)
|
||||
|
@ -25,4 +37,12 @@ function chunk:new()
|
|||
return newTable
|
||||
end
|
||||
|
||||
function chunk:setBlock(blockId, x, y, z)
|
||||
self.blocks[bit32.bor(bit32.rshift(x, -11), bit32.bor(bit32.rshift(z, -7), y)) + 1] = blockId
|
||||
end
|
||||
|
||||
function chunk:getBlockId(x, y, z)
|
||||
return self.blocks[bit32.bor(bit32.rshift(x, -11), bit32.bor(bit32.rshift(z, -7), y)) + 1]
|
||||
end
|
||||
|
||||
return chunk
|
139
mc/server.lua
139
mc/server.lua
|
@ -1,63 +1,152 @@
|
|||
-- local bufferStuff = require("bufferstuff")
|
||||
-- local writer = bufferStuff.Writer:new()
|
||||
-- writer:writeUByte(10)
|
||||
-- writer:writeUByte(67)
|
||||
-- writer:writeString16("Hello World!")
|
||||
|
||||
-- local reader = bufferStuff.Reader:new(writer:toBuffer())
|
||||
-- print(reader:readUByte())
|
||||
-- print(reader:readUByte())
|
||||
-- print(reader:readString16())
|
||||
|
||||
local chunk = require("chunk")
|
||||
--local chunk = require("chunk")
|
||||
local packet = require("packet")
|
||||
local bufferStuff = require("bufferstuff")
|
||||
--local libDeflate = require("LibDeflate")
|
||||
|
||||
local tempChunk = chunk:new()
|
||||
-- local tempChunk = chunk:new()
|
||||
-- for x = 0, 15 do
|
||||
-- for z = 0, 15 do
|
||||
-- tempChunk:setBlock(2, x, 62, z)
|
||||
-- end
|
||||
-- end
|
||||
|
||||
local ws = nil
|
||||
|
||||
local playerX = 0
|
||||
local playerY = 0
|
||||
local playerZ = 0
|
||||
local playerYaw = 0
|
||||
local playerPitch = 0
|
||||
local onGround = false
|
||||
|
||||
local function doWebsocket()
|
||||
local ws = http.websocket("ws://localhost:25567")
|
||||
ws = http.websocket("ws://localhost:25567")
|
||||
|
||||
while true do
|
||||
local b, bv, data, bx = os.pullEvent("websocket_message")
|
||||
|
||||
local reader = bufferStuff.Reader:new(data)
|
||||
local packetId = reader:readByte()
|
||||
if packetId == packet.LoginRequest then
|
||||
if packetId == packet.KeepAlive then
|
||||
ws.send(data, true)
|
||||
elseif packetId == packet.LoginRequest then
|
||||
local protocolVersion = reader:readInt()
|
||||
local username = reader:readString16()
|
||||
local mapSeed = reader:readLong()
|
||||
local dimension = reader:readByte()
|
||||
print(username .. " is requesting login")
|
||||
|
||||
local writer = bufferStuff.Writer:new()
|
||||
-- Login packet
|
||||
writer:writeByte(packet.LoginRequest) -- Packet ID
|
||||
writer:writeInt(1) -- Entity ID
|
||||
writer:writeString16("") -- Login Token
|
||||
writer:writeLong(0) -- Seed
|
||||
writer.writeByte(0) -- Dimension
|
||||
ws.send(writer:toBuffer())
|
||||
print("Login granted")
|
||||
writer:writeByte(0) -- Dimension
|
||||
ws.send(writer:toBuffer(), true)
|
||||
|
||||
-- teleport
|
||||
-- local teleportWriter = bufferStuff.Writer:new()
|
||||
-- teleportWriter:writeByte(packet.PlayerPositionLook)
|
||||
-- teleportWriter:writeDouble(8)
|
||||
-- teleportWriter:writeDouble(75)
|
||||
-- teleportWriter:writeDouble(75.62)
|
||||
-- teleportWriter:writeDouble(8)
|
||||
-- teleportWriter:writeFloat(0)
|
||||
-- teleportWriter:writeFloat(0)
|
||||
-- teleportWriter:writeBool(true)
|
||||
-- ws.send(teleportWriter:toBuffer(), true)
|
||||
|
||||
-- pre chunk
|
||||
local preChunkWriter = bufferStuff.Writer:new()
|
||||
preChunkWriter:writeByte(packet.PreChunk)
|
||||
preChunkWriter:writeInt(0)
|
||||
preChunkWriter:writeInt(0)
|
||||
preChunkWriter:writeBool(true)
|
||||
ws.send(preChunkWriter:toBuffer(), true)
|
||||
|
||||
local blockWriter = bufferStuff.Writer:new()
|
||||
for x = 0, 15 do
|
||||
for z = 0, 15 do
|
||||
blockWriter:writeByte(packet.BlockChange)
|
||||
blockWriter:writeInt(x)
|
||||
blockWriter:writeByte(62)
|
||||
blockWriter:writeInt(z)
|
||||
blockWriter:writeByte(2)
|
||||
blockWriter:writeByte(0)
|
||||
end
|
||||
end
|
||||
ws.send(blockWriter:toBuffer(), true)
|
||||
|
||||
-- chunk
|
||||
-- local chunkWriter = bufferStuff.Writer:new()
|
||||
-- local chunkData = bufferStuff.Writer:new()
|
||||
-- for _, id in ipairs(tempChunk.blocks) do
|
||||
-- chunkData:writeUByte(id)
|
||||
-- end
|
||||
-- for _, id in ipairs(tempChunk.metadata) do
|
||||
-- chunkData:writeUByte(id)
|
||||
-- end
|
||||
-- for _, id in ipairs(tempChunk.blockLight) do
|
||||
-- chunkData:writeUByte(id)
|
||||
-- end
|
||||
-- for _, id in ipairs(tempChunk.skyLight) do
|
||||
-- chunkData:writeUByte(id)
|
||||
-- end
|
||||
-- chunkWriter:writeByte(packet.MapChunk)
|
||||
-- chunkWriter:writeInt(0)
|
||||
-- chunkWriter:writeShort(0)
|
||||
-- chunkWriter:writeInt(0)
|
||||
-- chunkWriter:writeByte(15)
|
||||
-- chunkWriter:writeByte(127)
|
||||
-- chunkWriter:writeByte(15)
|
||||
|
||||
-- local compressedData = libDeflate:CompressDeflate(chunkData:toBuffer())
|
||||
|
||||
-- chunkWriter:writeInt(#compressedData)
|
||||
-- local chunkPacket = chunkWriter:toBuffer()
|
||||
-- ws.send(chunkPacket .. compressedData, true)
|
||||
|
||||
elseif packetId == packet.Handshake then
|
||||
local username = reader:readString16()
|
||||
print(username .. " is probing the server")
|
||||
|
||||
local writer = bufferStuff.Writer:new()
|
||||
writer:writeByte(packet.Handshake)
|
||||
writer:writeString16("-")
|
||||
ws.send(writer:toBuffer())
|
||||
print("Handshake ACK sent")
|
||||
ws.send(writer:toBuffer(), true)
|
||||
elseif packetId == packet.Player then
|
||||
onGround = reader:readBool()
|
||||
elseif packetId == packet.PlayerPosition then
|
||||
playerX = reader:readDouble()
|
||||
playerY = reader:readDouble()
|
||||
reader:readDouble()
|
||||
playerZ = reader:readDouble()
|
||||
onGround = reader:readBool()
|
||||
elseif packetId == packet.PlayerPositionLook then
|
||||
playerX = reader:readDouble()
|
||||
playerY = reader:readDouble()
|
||||
reader:readDouble()
|
||||
playerZ = reader:readDouble()
|
||||
playerYaw = reader:readFloat()
|
||||
playerPitch = reader:readFloat()
|
||||
onGround = reader:readBool()
|
||||
elseif packet == packet.PlayerLook then
|
||||
playerYaw = reader:readFloat()
|
||||
playerPitch = reader:readFloat()
|
||||
onGround = reader:readBool()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local keepAliveWriter = bufferStuff.Writer:new()
|
||||
keepAliveWriter:writeByte(packet.KeepAlive)
|
||||
local keepAlivePacket = keepAliveWriter:toBuffer()
|
||||
|
||||
local tickCount = 0
|
||||
local function serverTickLoop()
|
||||
while true do
|
||||
|
||||
|
||||
os.sleep(0.05)
|
||||
--print(playerX, playerY, playerZ, playerYaw, playerPitch, onGround)
|
||||
|
||||
os.sleep(0.1)
|
||||
tickCount = tickCount + 1
|
||||
end
|
||||
end
|
||||
|
|
|
@ -36,7 +36,8 @@ server.on("connection", (socket) => {
|
|||
writeClient(data as Buffer);
|
||||
});
|
||||
|
||||
socket.on("error", () => {
|
||||
socket.on("error", (err) => {
|
||||
console.log(err);
|
||||
clearInterval(interval);
|
||||
});
|
||||
socket.on("close", () => {
|
||||
|
|
Loading…
Reference in a new issue