Skip to content

Commit

Permalink
Server: Add second planet
Browse files Browse the repository at this point in the history
  • Loading branch information
SirLynix committed Dec 30, 2024
1 parent 7ebe2ff commit 19c5911
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 13 deletions.
2 changes: 1 addition & 1 deletion include/ServerLib/ServerPlanetEnvironment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace tsom
class TSOM_SERVERLIB_API ServerPlanetEnvironment final : public ServerEnvironment
{
public:
ServerPlanetEnvironment(ServerInstance& serverInstance, std::filesystem::path savePath, Nz::UInt32 seed, const Nz::Vector3ui& chunkCount, float cellSize, float cornerRadius = 16.f);
ServerPlanetEnvironment(ServerInstance& serverInstance, std::string_view generatorName, std::filesystem::path savePath, Nz::UInt32 seed, const Nz::Vector3ui& chunkCount, float cellSize, float cornerRadius = 16.f);
ServerPlanetEnvironment(const ServerPlanetEnvironment&) = delete;
ServerPlanetEnvironment(ServerPlanetEnvironment&&) = delete;
~ServerPlanetEnvironment();
Expand Down
99 changes: 91 additions & 8 deletions scripts/planets/bob.lua
Original file line number Diff line number Diff line change
@@ -1,33 +1,116 @@
-- Do not touch to this 2 variables
local perlin = PerlinNoise()
local chunksize = 32
local scale = 0.2

local minGrenerationFreeHeight = 0 -- Generation height limit used to make generation faster if we want empty chunks to allow players to build tall things
local baseFreeHeight = 30 -- Should be greater than minFreeHeight, difference between both will define max generation height from baseFreeHeight

return function (chunk, seed)
perlin:reseed(seed)

local blockLibrary = chunk:GetBlockLibrary()
local blockCount = chunk:GetBlockCount()

local empty = blockLibrary:GetBlockIndex("empty")
local dirt = blockLibrary:GetBlockIndex("dirt")
local grass = blockLibrary:GetBlockIndex("grass")
local emptyBlock = blockLibrary:GetBlockIndex("empty")
local debugBlock = blockLibrary:GetBlockIndex("debug")
local dirtBlock = blockLibrary:GetBlockIndex("dirt")
local grassBlock = blockLibrary:GetBlockIndex("grass")
local hullBlock = blockLibrary:GetBlockIndex("hull")
local hull2Block = blockLibrary:GetBlockIndex("hull2")
local snowBlock = blockLibrary:GetBlockIndex("snow")
local stoneBlock = blockLibrary:GetBlockIndex("stone")
local stoneMossyBlock = blockLibrary:GetBlockIndex("stone_mossy")
local forcefieldBlock = blockLibrary:GetBlockIndex("forcefield")
local planksBlock = blockLibrary:GetBlockIndex("planks")
local stoneBricksBlock = blockLibrary:GetBlockIndex("stone_bricks")
local copperBlock = blockLibrary:GetBlockIndex("copper_block")
local glassBlock = blockLibrary:GetBlockIndex("glass")

local planet = chunk:GetContainer()
local chunkIndices = chunk:GetIndices()


local maxHeight = (chunksize * planet:GetChunkCount()^(1/3))/2;
local maxGenerationHeight = maxHeight - minGrenerationFreeHeight
local baseHeight = maxHeight - baseFreeHeight -- Only works for planets with the same number of chunks in all the directions

local terrainVariation1Scale = 0.06 * baseHeight
local terrainVariation2Scale = 0.16 * baseHeight
local moutainScale = 0.03 * baseHeight
local spikeScale = 0.2 * baseHeight
local caveScale = 0.06 -- Other scale unit

local content = {}

for z = 0, chunksize - 1 do
for y = 0, chunksize - 1 do
for x = 0, chunksize - 1 do
local blockPos = planet:GetBlockIndices(chunkIndices, Vec3ui(x, y, z))
local blockPosNorm, distToCenter = Vec3f(blockPos.x * 1.0, blockPos.y * 1.0, blockPos.z * 1.0):GetNormal()
distToCenter = math.max(math.abs(blockPos.x + 0.5), math.abs(blockPos.y + 0.5), math.abs(blockPos.z + 0.5))

local height = perlin:normalizedOctave3D_01(blockPosNorm.x * scale, blockPosNorm.y * scale, blockPosNorm.z * scale, 4, 2.0)
height = height * 100
table.insert(content, distToCenter > height and empty or grass)
if distToCenter > maxGenerationHeight then
table.insert(content, emptyBlock)
goto continue
end

local blockPresence = perlin:normalizedOctave3D_01(blockPos.x * caveScale, blockPos.y * caveScale, blockPos.z * caveScale, 4, 0.1)

if distToCenter <= baseHeight then
if blockPresence >= 0.3 and blockPresence <= 0.7 then
if distToCenter <= baseHeight-5 then
table.insert(content, stoneBlock)
elseif distToCenter <= baseHeight then
table.insert(content, dirtBlock)
end
else
table.insert(content, emptyBlock)
end
else
local baseMountainous = perlin:normalizedOctave3D_01((blockPosNorm.x * moutainScale)+10, blockPosNorm.y * moutainScale, blockPosNorm.z * moutainScale, 4, 0.1)
if baseMountainous < 0.6 then
mountainous = 0
elseif baseMountainous < 0.8 then
mountainous = 5*baseMountainous-3
else
mountainous = 1
end

local heightVariation1 = 10 * perlin:normalizedOctave3D_01(blockPosNorm * terrainVariation1Scale, blockPosNorm.y * terrainVariation1Scale, blockPosNorm.z * terrainVariation1Scale, 4, 0.1)
local heightVariation2 = 40 * mountainous * perlin:normalizedOctave3D_01((blockPosNorm.x * terrainVariation2Scale)+20, blockPosNorm.y * terrainVariation2Scale, blockPosNorm.z * terrainVariation2Scale, 4, 0.1)

local baseSpikeHeight = perlin:normalizedOctave3D_01((blockPosNorm.x * spikeScale)+30, blockPosNorm.y * spikeScale, blockPosNorm.z * spikeScale, 4, 0.1)
if baseSpikeHeight < 0.7 then
spikeHeight = 0
elseif baseSpikeHeight < 0.9 then
spikeHeight = 5*baseSpikeHeight-3.5
else
spikeHeight = 1
end
spikeHeight = (1-mountainous) * spikeHeight * 20

local height = baseHeight + heightVariation1 + heightVariation2 + spikeHeight

if distToCenter <= height then
if distToCenter >= height - spikeHeight then
table.insert(content, stoneMossyBlock)
elseif mountainous > 0.5 and heightVariation2 > 0.5 then
table.insert(content, snowBlock)
elseif mountainous > 0.1 then
table.insert(content, stoneBlock)
elseif baseMountainous < 0.4 then
table.insert(content, grassBlock)
else
table.insert(content, dirtBlock)
end
else
table.insert(content, emptyBlock)
end
end

::continue::
end
end
end

chunk:Reset(content)
end
1 change: 0 additions & 1 deletion src/CommonLib/Protocol/Packets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <CommonLib/Version.hpp>
#include <CommonLib/Utility/BinaryCompressor.hpp>
#include <NazaraUtils/TypeTraits.hpp>
#include <lz4.h>
#include <fmt/format.h>

namespace tsom
Expand Down
30 changes: 29 additions & 1 deletion src/Server/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <ServerLib/PlayerTokenAppComponent.hpp>
#include <ServerLib/ServerInstanceAppComponent.hpp>
#include <ServerLib/ServerPlanetEnvironment.hpp>
#include <ServerLib/Components/EnvironmentProxyComponent.hpp>
#include <ServerLib/Components/NetworkedComponent.hpp>
#include <ServerLib/Session/InitialSessionHandler.hpp>
#include <Nazara/Core/Application.hpp>
#include <Nazara/Core/Core.hpp>
Expand All @@ -21,6 +23,9 @@
#include <Main/Main.hpp>
#include <fmt/color.h>

#include <ServerLib/Components/EnvironmentEnterTriggerComponent.hpp>
#include <Nazara/Core/Components/NodeComponent.hpp>

int ServerMain(int argc, char* argv[])
{
Nz::Application<Nz::Core, Nz::Physics3D, Nz::Network> app(argc, argv);
Expand Down Expand Up @@ -56,9 +61,32 @@ int ServerMain(int argc, char* argv[])
auto& sessionManager = instance.AddSessionManager(serverPort);
sessionManager.SetDefaultHandler<tsom::InitialSessionHandler>(std::ref(instance));

tsom::ServerPlanetEnvironment planet(instance, saveDirectory, 42, Nz::Vector3ui(5), 1.f);
tsom::ServerPlanetEnvironment planet(instance, "alice", saveDirectory / Nz::Utf8Path("alice"), 42, Nz::Vector3ui(5), 1.f);
instance.SetDefaultSpawnpoint(&planet, Nz::Vector3f::Up() * 100.f + Nz::Vector3f::Backward() * 5.f, Nz::Quaternionf::Identity());

tsom::ServerPlanetEnvironment planet2(instance, "bob", saveDirectory / Nz::Utf8Path("bob"), 41, Nz::Vector3ui(5), 1.f, 40.f);

entt::handle switchToPlanet2Entity = planet.CreateEntity();
auto& enterPlanet2Trigger = switchToPlanet2Entity.emplace<tsom::EnvironmentEnterTriggerComponent>();
enterPlanet2Trigger.aabb = Nz::Boxf(-300.f, -300.f, -300.f, 600.f, 600.f, 600.f);
enterPlanet2Trigger.targetEnvironment = &planet2;
enterPlanet2Trigger.updateRoot = true;

switchToPlanet2Entity.emplace<Nz::NodeComponent>(Nz::Vector3f(-10000.f, 0.f, 0.f));
switchToPlanet2Entity.emplace<tsom::EnvironmentProxyComponent>().targetEnvironment = &planet2;
switchToPlanet2Entity.emplace<tsom::NetworkedComponent>();

entt::handle switchToPlanet1Entity = planet2.CreateEntity();
auto& enterPlanet1Trigger = switchToPlanet1Entity.emplace<tsom::EnvironmentEnterTriggerComponent>();
enterPlanet1Trigger.aabb = Nz::Boxf(-300.f, -300.f, -300.f, 600.f, 600.f, 600.f);
switchToPlanet1Entity.emplace<Nz::NodeComponent>(Nz::Vector3f(10000.f, 0.f, 0.f));
switchToPlanet1Entity.emplace<tsom::EnvironmentProxyComponent>().targetEnvironment = &planet;
switchToPlanet1Entity.emplace<tsom::NetworkedComponent>();
enterPlanet1Trigger.targetEnvironment = &planet;
enterPlanet1Trigger.updateRoot = true;

instance.SetDefaultSpawnpoint(&planet2, Nz::Vector3f::Up() * 120.f + Nz::Vector3f::Backward() * 5.f, Nz::Quaternionf::Identity());

fmt::print(fg(fmt::color::lime_green), "server ready.\n");

if (Nz::UInt32 maxStuckTime = config.GetIntegerValue<Nz::UInt32>("Server.MaxStuckSeconds"))
Expand Down
4 changes: 2 additions & 2 deletions src/ServerLib/ServerPlanetEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace tsom
{
constexpr unsigned int chunkSaveVersion = 1;

ServerPlanetEnvironment::ServerPlanetEnvironment(ServerInstance& serverInstance, std::filesystem::path savePath, Nz::UInt32 seed, const Nz::Vector3ui& chunkCount, float cellSize, float cornerRadius) :
ServerPlanetEnvironment::ServerPlanetEnvironment(ServerInstance& serverInstance, std::string_view generatorName, std::filesystem::path savePath, Nz::UInt32 seed, const Nz::Vector3ui& chunkCount, float cellSize, float cornerRadius) :
ServerEnvironment(serverInstance, ServerEnvironmentType::Planet, true),
m_savePath(std::move(savePath))
{
Expand Down Expand Up @@ -61,7 +61,7 @@ namespace tsom
if (!m_savePath.empty())
LoadFromDirectory();

planetComponent.planet->GenerateChunks(blockLibrary, taskScheduler, seed, chunkCount, "alice");
planetComponent.planet->GenerateChunks(blockLibrary, taskScheduler, seed, chunkCount, generatorName);
taskScheduler.WaitForTasks();

planetComponent.planet->GeneratePlatform(blockLibrary, tsom::Direction::Right, { 65, -18, -39 });
Expand Down

0 comments on commit 19c5911

Please sign in to comment.