builtin/voxelworld, games/digger: WIP

This commit is contained in:
Perttu Ahola 2014-10-12 10:11:36 +03:00
parent 640ed1cf05
commit e87c89c703
3 changed files with 108 additions and 10 deletions

View File

@ -2,10 +2,61 @@
-- http://www.apache.org/licenses/LICENSE-2.0
-- Copyright 2014 Perttu Ahola <celeron55@gmail.com>
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

View File

@ -11,6 +11,7 @@
#include "interface/voxel.h"
#include "interface/block.h"
#include <PolyVoxCore/SimpleVolume.h>
#include <cereal/archives/portable_binary.hpp>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
#include <Node.h>
@ -25,12 +26,29 @@ namespace pv = PolyVox;
using namespace Urho3D;
namespace std {
template<> struct hash<pv::Vector<2u, uint16_t>>{
std::size_t operator()(const pv::Vector<2u, uint16_t> &v) const {
return ((std::hash<uint16_t>() (v.getX()) << 0) ^
(std::hash<uint16_t>() (v.getY()) << 1));
template<> struct hash<pv::Vector<2u, int16_t>>{
std::size_t operator()(const pv::Vector<2u, int16_t> &v) const {
return ((std::hash<int16_t>() (v.getX()) << 0) ^
(std::hash<int16_t>() (v.getY()) << 1));
}
};
}
namespace cereal {
template<class Archive>
void save(Archive &archive, const pv::Vector3DInt16 &v){
archive((int32_t)v.getX(), (int32_t)v.getY(), (int32_t)v.getZ());
}
template<class Archive>
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<int32_t> 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_<pv::Vector<2, int16_t>, sm_<int16_t, Section>> m_sections;
sm_<pv::Vector<2, int16_t>, sm_<int16_t, Section>> m_sections;
// Cache of last used sections (add to end, remove from beginning)
//std::deque<Section*> 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:

View File

@ -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)