#include "core/log.h" #include "client_file/api.h" #include "network/api.h" #include "replicate/api.h" #include "voxelworld/api.h" #include "interface/module.h" #include "interface/server.h" #include "interface/event.h" #include "interface/mesh.h" #include "interface/voxel.h" #include "interface/noise.h" #include #include #include #include #include #include #include #include #include #include #include #include #include namespace digger { namespace magic = Urho3D; namespace pv = PolyVox; using interface::Event; using interface::VoxelInstance; using namespace Urho3D; struct Module: public interface::Module { interface::Server *m_server; uint m_slow_count = 0; sp_ m_atlas_reg; sp_ m_voxel_reg; Module(interface::Server *server): interface::Module("digger"), m_server(server) { } ~Module() {} void init() { m_server->sub_event(this, Event::t("core:start")); m_server->sub_event(this, Event::t("core:continue")); 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("voxelworld:generation_request")); } void event(const Event::Type &type, const Event::Private *p) { EVENT_VOIDN("core:start", on_start) EVENT_VOIDN("core:continue", on_continue) EVENT_TYPEN("core:tick", on_tick, interface::TickEvent) EVENT_TYPEN("client_file:files_transmitted", on_files_transmitted, client_file::FilesTransmitted) EVENT_TYPEN("voxelworld:generation_request", on_generation_request, voxelworld::GenerationRequest) } void on_start() { m_server->access_scene([&](Scene *scene) { Context *context = scene->GetContext(); ResourceCache *cache = context->GetSubsystem(); { Node *node = scene->CreateChild("DirectionalLight"); node->SetDirection(Vector3(-0.6f, -1.0f, 0.8f)); Light *light = node->CreateComponent(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetCastShadows(true); light->SetBrightness(0.8); light->SetColor(Color(1.0, 1.0, 0.95)); } { Node *node = scene->CreateChild("DirectionalLight"); node->SetDirection(Vector3(0.3f, -1.0f, -0.4f)); Light *light = node->CreateComponent(); light->SetLightType(LIGHT_DIRECTIONAL); light->SetCastShadows(true); light->SetBrightness(0.2); light->SetColor(Color(0.7, 0.7, 1.0)); } }); } void on_continue() { } void update_scene() { } void on_tick(const interface::TickEvent &event) { } void on_files_transmitted(const client_file::FilesTransmitted &event) { network::access(m_server, [&](network::Interface *inetwork){ inetwork->send(event.recipient, "core:run_script", "buildat.run_script_file(\"main/init.lua\")"); }); } void on_generation_request(const voxelworld::GenerationRequest &event) { log_v(MODULE, "on_generation_request(): section_p: (%i, %i, %i)", event.section_p.getX(), event.section_p.getY(), event.section_p.getZ()); voxelworld::access(m_server, [&](voxelworld::Interface *ivoxelworld) { const pv::Vector3DInt16 §ion_p = event.section_p; pv::Region region = ivoxelworld->get_section_region(section_p); pv::Vector3DInt32 p0 = region.getLowerCorner(); pv::Vector3DInt32 p1 = region.getUpperCorner(); log_t(MODULE, "on_generation_request(): p0: (%i, %i, %i)", p0.getX(), p0.getY(), p0.getZ()); log_t(MODULE, "on_generation_request(): p1: (%i, %i, %i)", p1.getX(), p1.getY(), p1.getZ()); interface::NoiseParams np(0, 20, interface::v3f(40,40,40), 0, 4, 0.5); auto lc = region.getLowerCorner(); auto uc = region.getUpperCorner(); for(int z = lc.getZ(); z <= uc.getZ(); z++){ for(int y = lc.getY(); y <= uc.getY(); y++){ for(int x = lc.getX(); x <= uc.getX(); x++){ pv::Vector3DInt32 p(x, y, z); if(z > 37 && z < 50 && y > 20){ ivoxelworld->set_voxel(p, VoxelInstance(1)); continue; } if(x > 27 && x < 40 && y > 20){ ivoxelworld->set_voxel(p, VoxelInstance(1)); continue; } double a = interface::NoisePerlin2D(&np, x, z, 0); if(y < a+5){ ivoxelworld->set_voxel(p, VoxelInstance(2)); } else if(y < a+10){ ivoxelworld->set_voxel(p, VoxelInstance(3)); } else if(y < a+11){ ivoxelworld->set_voxel(p, VoxelInstance(4)); } else { ivoxelworld->set_voxel(p, VoxelInstance(1)); } } } } }); } }; extern "C" { BUILDAT_EXPORT void* createModule_main(interface::Server *server){ return (void*)(new Module(server)); } } } // vim: set noet ts=4 sw=4: