103 lines
3.1 KiB
Lua
Raw Normal View History

-- Buildat: builtin/voxelworld/client_lua/module.lua
-- http://www.apache.org/licenses/LICENSE-2.0
-- Copyright 2014 Perttu Ahola <celeron55@gmail.com>
local log = buildat.Logger("voxelworld")
2014-10-12 10:11:36 +03:00
local magic = require("buildat/extension/urho3d")
local replicate = require("buildat/extension/replicate")
local cereal = require("buildat/extension/cereal")
local dump = buildat.dump
local M = {}
2014-10-12 10:11:36 +03:00
local camera_node = nil
2014-10-12 11:26:33 +03:00
local update_counter = -1
M.chunk_size_voxels = nil
M.section_size_chunks = nil
M.section_size_voxels = nil
2014-10-12 10:11:36 +03:00
function M.init()
log:info("voxelworld.init()")
2014-10-12 10:11:36 +03:00
buildat.sub_packet("voxelworld:init", function(data)
local values = cereal.binary_input(data, {"object",
{"chunk_size_voxels", {"object",
{"x", "int32_t"},
{"y", "int32_t"},
{"z", "int32_t"},
}},
{"section_size_chunks", {"object",
{"x", "int32_t"},
{"y", "int32_t"},
{"z", "int32_t"},
}},
})
log:info(dump(values))
2014-10-12 11:26:33 +03:00
M.chunk_size_voxels = buildat.IntVector3(values.chunk_size_voxels)
M.section_size_chunks = buildat.IntVector3(values.section_size_chunks)
M.section_size_voxels =
M.chunk_size_voxels:mul_components(M.section_size_chunks)
2014-10-12 10:11:36 +03:00
end)
magic.SubscribeToEvent("Update", function(event_type, event_data)
2014-10-12 13:28:24 +03:00
if camera_node and M.section_size_voxels then
2014-10-12 10:11:36 +03:00
local p = camera_node.position
2014-10-12 11:26:33 +03:00
update_counter = update_counter + 1
2014-10-12 10:11:36 +03:00
if update_counter % 60 == 0 then
2014-10-12 11:26:33 +03:00
local section_p = buildat.IntVector3(p):div_components(
M.section_size_voxels):floor()
2014-10-12 13:28:24 +03:00
--log:info("p: "..p.x..", "..p.y..", "..p.z.." -> section_p: "..
-- section_p.x..", "..section_p.y..", "..section_p.z)
2014-10-12 11:26:33 +03:00
--[[send_get_section(section_p + buildat.IntVector3( 0, 0, 0))
send_get_section(section_p + buildat.IntVector3(-1, 0, 0))
send_get_section(section_p + buildat.IntVector3( 1, 0, 0))
send_get_section(section_p + buildat.IntVector3( 0, 1, 0))
send_get_section(section_p + buildat.IntVector3( 0,-1, 0))
send_get_section(section_p + buildat.IntVector3( 0, 0, 1))
send_get_section(section_p + buildat.IntVector3( 0, 0,-1))]]
2014-10-12 10:11:36 +03:00
end
end
end)
2014-10-12 11:26:33 +03:00
local function setup_buildat_voxel_data(node)
2014-10-12 13:28:24 +03:00
local data = node:GetVar("buildat_voxel_data"):GetBuffer()
2014-10-12 11:26:33 +03:00
local w = node:GetVar("buildat_voxel_w"):GetInt()
local h = node:GetVar("buildat_voxel_h"):GetInt()
local d = node:GetVar("buildat_voxel_d"):GetInt()
2014-10-12 13:28:24 +03:00
log:info(dump(node:GetName()).." voxel data size: "..data:GetSize())
2014-10-12 11:26:33 +03:00
buildat.set_8bit_voxel_geometry(node, w, h, d, data)
node:SetScale(magic.Vector3(1, 1, 1))
end
replicate.sub_sync_node_added({}, function(node)
if not node:GetVar("buildat_voxel_data"):IsEmpty() then
setup_buildat_voxel_data(node)
end
local name = node:GetName()
end)
2014-10-12 10:11:36 +03:00
end
function M.set_camera(new_camera_node)
camera_node = new_camera_node
end
2014-10-12 11:26:33 +03:00
function send_get_section(p)
local data = cereal.binary_output({
p = {
x = p.x,
y = p.y,
z = p.z,
},
}, {"object",
{"p", {"object",
{"x", "int32_t"},
{"y", "int32_t"},
{"z", "int32_t"},
}},
})
--log:info(dump(buildat.bytes(data)))
buildat.send_packet("voxelworld:get_section", data)
end
return M
-- vim: set noet ts=4 sw=4: