From e87c89c70382928cd93f34275ccf85630e8cd39a Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Sun, 12 Oct 2014 10:11:36 +0300 Subject: [PATCH] builtin/voxelworld, games/digger: WIP --- builtin/voxelworld/client_lua/module.lua | 51 +++++++++++++++++++ builtin/voxelworld/voxelworld.cpp | 62 ++++++++++++++++++++---- games/digger/main/client_lua/init.lua | 5 +- 3 files changed, 108 insertions(+), 10 deletions(-) diff --git a/builtin/voxelworld/client_lua/module.lua b/builtin/voxelworld/client_lua/module.lua index 2ee34d4..14d8455 100644 --- a/builtin/voxelworld/client_lua/module.lua +++ b/builtin/voxelworld/client_lua/module.lua @@ -2,10 +2,61 @@ -- http://www.apache.org/licenses/LICENSE-2.0 -- Copyright 2014 Perttu Ahola local log = buildat.Logger("voxelworld") +local magic = require("buildat/extension/urho3d") +local replicate = require("buildat/extension/replicate") +local cereal = require("buildat/extension/cereal") +local dump = buildat.dump local M = {} +local camera_node = nil +local update_counter = 0 + function M.init() log:info("voxelworld.init()") + + 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)) + end) + + magic.SubscribeToEvent("Update", function(event_type, event_data) + if camera_node then + local p = camera_node.position + if update_counter % 60 == 0 then + --log:info("p: "..p.x..", "..p.y..", "..p.z) + local data = cereal.binary_output({ + p = { + x = p.x, + y = p.y, + z = p.z, + }, + }, {"object", + {"p", {"object", + {"x", "double"}, + {"y", "double"}, + {"z", "double"}, + }}, + }) + buildat.send_packet("voxelworld:camera_position", data) + end + update_counter = update_counter + 1 + end + end) +end + +function M.set_camera(new_camera_node) + camera_node = new_camera_node end return M diff --git a/builtin/voxelworld/voxelworld.cpp b/builtin/voxelworld/voxelworld.cpp index b2f9bd7..23d6f59 100644 --- a/builtin/voxelworld/voxelworld.cpp +++ b/builtin/voxelworld/voxelworld.cpp @@ -11,6 +11,7 @@ #include "interface/voxel.h" #include "interface/block.h" #include +#include #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wstrict-aliasing" #include @@ -25,12 +26,29 @@ namespace pv = PolyVox; using namespace Urho3D; namespace std { -template<> struct hash>{ - std::size_t operator()(const pv::Vector<2u, uint16_t> &v) const { - return ((std::hash() (v.getX()) << 0) ^ - (std::hash() (v.getY()) << 1)); + +template<> struct hash>{ + std::size_t operator()(const pv::Vector<2u, int16_t> &v) const { + return ((std::hash() (v.getX()) << 0) ^ + (std::hash() (v.getY()) << 1)); } }; + +} + +namespace cereal { + +template +void save(Archive &archive, const pv::Vector3DInt16 &v){ + archive((int32_t)v.getX(), (int32_t)v.getY(), (int32_t)v.getZ()); +} +template +void load(Archive &archive, pv::Vector3DInt16 &v){ + int16_t x, y, z; + archive((int32_t)x, (int32_t)y, (int32_t)z); + v.setX(x); v.setY(y); v.setZ(z); +} + } namespace voxelworld { @@ -38,8 +56,8 @@ namespace voxelworld { struct Section { pv::Vector3DInt16 chunk_size; - pv::Region contained_chunks; // Position and size in chunks - pv::Vector3DInt16 section_p; // Position in sections + pv::Region contained_chunks;// Position and size in chunks + pv::Vector3DInt16 section_p;// Position in sections // Static voxel nodes (each contains one chunk); Initialized to 0. pv::SimpleVolume node_ids; @@ -70,7 +88,7 @@ struct Module: public interface::Module, public voxelworld::Interface pv::Vector3DInt16 m_section_size_chunks = pv::Vector3DInt16(4, 4, 4); // Sections (first (y,z), then x) - //sm_, sm_> m_sections; + sm_, sm_> m_sections; // Cache of last used sections (add to end, remove from beginning) //std::deque m_last_used_sections; @@ -92,6 +110,8 @@ struct Module: public interface::Module, public voxelworld::Interface m_server->sub_event(this, Event::t("network:client_connected")); m_server->sub_event(this, Event::t("core:tick")); m_server->sub_event(this, Event::t("client_file:files_transmitted")); + m_server->sub_event(this, Event::t( + "network:packet_received/voxelworld:camera_position")); m_server->access_scene([&](Scene *scene) { @@ -189,6 +209,8 @@ struct Module: public interface::Module, public voxelworld::Interface EVENT_TYPEN("core:tick", on_tick, interface::TickEvent) EVENT_TYPEN("client_file:files_transmitted", on_files_transmitted, client_file::FilesTransmitted) + EVENT_TYPEN("network:packet_received/voxelworld:camera_position", + on_camera_position, network::Packet) } void on_start() @@ -217,10 +239,32 @@ struct Module: public interface::Module, public voxelworld::Interface void on_files_transmitted(const client_file::FilesTransmitted &event) { + int peer = event.recipient; network::access(m_server, [&](network::Interface *inetwork){ - inetwork->send(event.recipient, "core:run_script", + inetwork->send(peer, "core:run_script", "require(\"buildat/module/voxelworld\").init()"); }); + std::ostringstream os(std::ios::binary); + { + cereal::PortableBinaryOutputArchive ar(os); + ar(m_chunk_size_voxels); + ar(m_section_size_chunks); + } + network::access(m_server, [&](network::Interface *inetwork){ + inetwork->send(peer, "voxelworld:init", os.str()); + }); + } + + void on_camera_position(const network::Packet &packet) + { + double px, py, pz; + { + std::istringstream is(packet.data, std::ios::binary); + cereal::PortableBinaryInputArchive ar(is); + ar(px, py, pz); + } + log_v(MODULE, "Camera position of C%i: (%f, %f, %f)", + packet.sender, px, py, pz); } // Interface @@ -237,5 +281,5 @@ extern "C" { } } } -// vim: set noet ts=4 sw=4: +// vim: set noet ts=4 sw=4: diff --git a/games/digger/main/client_lua/init.lua b/games/digger/main/client_lua/init.lua index 1c04602..193709e 100644 --- a/games/digger/main/client_lua/init.lua +++ b/games/digger/main/client_lua/init.lua @@ -7,6 +7,7 @@ local dump = buildat.dump local cereal = require("buildat/extension/cereal") local magic = require("buildat/extension/urho3d") local replicate = require("buildat/extension/replicate") +local voxelworld = require("buildat/module/voxelworld") local scene = replicate.main_scene @@ -20,13 +21,15 @@ camera_node:LookAt(magic.Vector3(0, 1, 0)) local viewport = magic.Viewport:new(scene, camera_node:GetComponent("Camera")) magic.renderer:SetViewport(0, viewport) +voxelworld.set_camera(camera_node) + -- Add some text local title_text = magic.ui.root:CreateChild("Text") title_text:SetText("digger/init.lua") title_text:SetFont(magic.cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15) title_text.horizontalAlignment = magic.HA_CENTER title_text.verticalAlignment = magic.VA_CENTER -title_text:SetPosition(0, magic.ui.root.height*(-0.33)) +title_text:SetPosition(0, -magic.ui.root.height/2 + 20) magic.ui:SetFocusElement(nil)