Skip to content

Commit 19c5911

Browse files
committed
Server: Add second planet
1 parent 7ebe2ff commit 19c5911

File tree

5 files changed

+123
-13
lines changed

5 files changed

+123
-13
lines changed

include/ServerLib/ServerPlanetEnvironment.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace tsom
2222
class TSOM_SERVERLIB_API ServerPlanetEnvironment final : public ServerEnvironment
2323
{
2424
public:
25-
ServerPlanetEnvironment(ServerInstance& serverInstance, std::filesystem::path savePath, Nz::UInt32 seed, const Nz::Vector3ui& chunkCount, float cellSize, float cornerRadius = 16.f);
25+
ServerPlanetEnvironment(ServerInstance& serverInstance, std::string_view generatorName, std::filesystem::path savePath, Nz::UInt32 seed, const Nz::Vector3ui& chunkCount, float cellSize, float cornerRadius = 16.f);
2626
ServerPlanetEnvironment(const ServerPlanetEnvironment&) = delete;
2727
ServerPlanetEnvironment(ServerPlanetEnvironment&&) = delete;
2828
~ServerPlanetEnvironment();

scripts/planets/bob.lua

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,116 @@
1+
-- Do not touch to this 2 variables
12
local perlin = PerlinNoise()
23
local chunksize = 32
3-
local scale = 0.2
4+
5+
local minGrenerationFreeHeight = 0 -- Generation height limit used to make generation faster if we want empty chunks to allow players to build tall things
6+
local baseFreeHeight = 30 -- Should be greater than minFreeHeight, difference between both will define max generation height from baseFreeHeight
47

58
return function (chunk, seed)
69
perlin:reseed(seed)
710

811
local blockLibrary = chunk:GetBlockLibrary()
912
local blockCount = chunk:GetBlockCount()
1013

11-
local empty = blockLibrary:GetBlockIndex("empty")
12-
local dirt = blockLibrary:GetBlockIndex("dirt")
13-
local grass = blockLibrary:GetBlockIndex("grass")
14+
local emptyBlock = blockLibrary:GetBlockIndex("empty")
15+
local debugBlock = blockLibrary:GetBlockIndex("debug")
16+
local dirtBlock = blockLibrary:GetBlockIndex("dirt")
17+
local grassBlock = blockLibrary:GetBlockIndex("grass")
18+
local hullBlock = blockLibrary:GetBlockIndex("hull")
19+
local hull2Block = blockLibrary:GetBlockIndex("hull2")
20+
local snowBlock = blockLibrary:GetBlockIndex("snow")
21+
local stoneBlock = blockLibrary:GetBlockIndex("stone")
22+
local stoneMossyBlock = blockLibrary:GetBlockIndex("stone_mossy")
23+
local forcefieldBlock = blockLibrary:GetBlockIndex("forcefield")
24+
local planksBlock = blockLibrary:GetBlockIndex("planks")
25+
local stoneBricksBlock = blockLibrary:GetBlockIndex("stone_bricks")
26+
local copperBlock = blockLibrary:GetBlockIndex("copper_block")
27+
local glassBlock = blockLibrary:GetBlockIndex("glass")
1428

1529
local planet = chunk:GetContainer()
1630
local chunkIndices = chunk:GetIndices()
17-
31+
32+
local maxHeight = (chunksize * planet:GetChunkCount()^(1/3))/2;
33+
local maxGenerationHeight = maxHeight - minGrenerationFreeHeight
34+
local baseHeight = maxHeight - baseFreeHeight -- Only works for planets with the same number of chunks in all the directions
35+
36+
local terrainVariation1Scale = 0.06 * baseHeight
37+
local terrainVariation2Scale = 0.16 * baseHeight
38+
local moutainScale = 0.03 * baseHeight
39+
local spikeScale = 0.2 * baseHeight
40+
local caveScale = 0.06 -- Other scale unit
41+
1842
local content = {}
1943

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

26-
local height = perlin:normalizedOctave3D_01(blockPosNorm.x * scale, blockPosNorm.y * scale, blockPosNorm.z * scale, 4, 2.0)
27-
height = height * 100
28-
table.insert(content, distToCenter > height and empty or grass)
51+
if distToCenter > maxGenerationHeight then
52+
table.insert(content, emptyBlock)
53+
goto continue
54+
end
55+
56+
local blockPresence = perlin:normalizedOctave3D_01(blockPos.x * caveScale, blockPos.y * caveScale, blockPos.z * caveScale, 4, 0.1)
57+
58+
if distToCenter <= baseHeight then
59+
if blockPresence >= 0.3 and blockPresence <= 0.7 then
60+
if distToCenter <= baseHeight-5 then
61+
table.insert(content, stoneBlock)
62+
elseif distToCenter <= baseHeight then
63+
table.insert(content, dirtBlock)
64+
end
65+
else
66+
table.insert(content, emptyBlock)
67+
end
68+
else
69+
local baseMountainous = perlin:normalizedOctave3D_01((blockPosNorm.x * moutainScale)+10, blockPosNorm.y * moutainScale, blockPosNorm.z * moutainScale, 4, 0.1)
70+
if baseMountainous < 0.6 then
71+
mountainous = 0
72+
elseif baseMountainous < 0.8 then
73+
mountainous = 5*baseMountainous-3
74+
else
75+
mountainous = 1
76+
end
77+
78+
local heightVariation1 = 10 * perlin:normalizedOctave3D_01(blockPosNorm * terrainVariation1Scale, blockPosNorm.y * terrainVariation1Scale, blockPosNorm.z * terrainVariation1Scale, 4, 0.1)
79+
local heightVariation2 = 40 * mountainous * perlin:normalizedOctave3D_01((blockPosNorm.x * terrainVariation2Scale)+20, blockPosNorm.y * terrainVariation2Scale, blockPosNorm.z * terrainVariation2Scale, 4, 0.1)
80+
81+
local baseSpikeHeight = perlin:normalizedOctave3D_01((blockPosNorm.x * spikeScale)+30, blockPosNorm.y * spikeScale, blockPosNorm.z * spikeScale, 4, 0.1)
82+
if baseSpikeHeight < 0.7 then
83+
spikeHeight = 0
84+
elseif baseSpikeHeight < 0.9 then
85+
spikeHeight = 5*baseSpikeHeight-3.5
86+
else
87+
spikeHeight = 1
88+
end
89+
spikeHeight = (1-mountainous) * spikeHeight * 20
90+
91+
local height = baseHeight + heightVariation1 + heightVariation2 + spikeHeight
92+
93+
if distToCenter <= height then
94+
if distToCenter >= height - spikeHeight then
95+
table.insert(content, stoneMossyBlock)
96+
elseif mountainous > 0.5 and heightVariation2 > 0.5 then
97+
table.insert(content, snowBlock)
98+
elseif mountainous > 0.1 then
99+
table.insert(content, stoneBlock)
100+
elseif baseMountainous < 0.4 then
101+
table.insert(content, grassBlock)
102+
else
103+
table.insert(content, dirtBlock)
104+
end
105+
else
106+
table.insert(content, emptyBlock)
107+
end
108+
end
109+
110+
::continue::
29111
end
30112
end
31113
end
114+
32115
chunk:Reset(content)
33116
end

src/CommonLib/Protocol/Packets.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <CommonLib/Version.hpp>
77
#include <CommonLib/Utility/BinaryCompressor.hpp>
88
#include <NazaraUtils/TypeTraits.hpp>
9-
#include <lz4.h>
109
#include <fmt/format.h>
1110

1211
namespace tsom

src/Server/main.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <ServerLib/PlayerTokenAppComponent.hpp>
99
#include <ServerLib/ServerInstanceAppComponent.hpp>
1010
#include <ServerLib/ServerPlanetEnvironment.hpp>
11+
#include <ServerLib/Components/EnvironmentProxyComponent.hpp>
12+
#include <ServerLib/Components/NetworkedComponent.hpp>
1113
#include <ServerLib/Session/InitialSessionHandler.hpp>
1214
#include <Nazara/Core/Application.hpp>
1315
#include <Nazara/Core/Core.hpp>
@@ -21,6 +23,9 @@
2123
#include <Main/Main.hpp>
2224
#include <fmt/color.h>
2325

26+
#include <ServerLib/Components/EnvironmentEnterTriggerComponent.hpp>
27+
#include <Nazara/Core/Components/NodeComponent.hpp>
28+
2429
int ServerMain(int argc, char* argv[])
2530
{
2631
Nz::Application<Nz::Core, Nz::Physics3D, Nz::Network> app(argc, argv);
@@ -56,9 +61,32 @@ int ServerMain(int argc, char* argv[])
5661
auto& sessionManager = instance.AddSessionManager(serverPort);
5762
sessionManager.SetDefaultHandler<tsom::InitialSessionHandler>(std::ref(instance));
5863

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

67+
tsom::ServerPlanetEnvironment planet2(instance, "bob", saveDirectory / Nz::Utf8Path("bob"), 41, Nz::Vector3ui(5), 1.f, 40.f);
68+
69+
entt::handle switchToPlanet2Entity = planet.CreateEntity();
70+
auto& enterPlanet2Trigger = switchToPlanet2Entity.emplace<tsom::EnvironmentEnterTriggerComponent>();
71+
enterPlanet2Trigger.aabb = Nz::Boxf(-300.f, -300.f, -300.f, 600.f, 600.f, 600.f);
72+
enterPlanet2Trigger.targetEnvironment = &planet2;
73+
enterPlanet2Trigger.updateRoot = true;
74+
75+
switchToPlanet2Entity.emplace<Nz::NodeComponent>(Nz::Vector3f(-10000.f, 0.f, 0.f));
76+
switchToPlanet2Entity.emplace<tsom::EnvironmentProxyComponent>().targetEnvironment = &planet2;
77+
switchToPlanet2Entity.emplace<tsom::NetworkedComponent>();
78+
79+
entt::handle switchToPlanet1Entity = planet2.CreateEntity();
80+
auto& enterPlanet1Trigger = switchToPlanet1Entity.emplace<tsom::EnvironmentEnterTriggerComponent>();
81+
enterPlanet1Trigger.aabb = Nz::Boxf(-300.f, -300.f, -300.f, 600.f, 600.f, 600.f);
82+
switchToPlanet1Entity.emplace<Nz::NodeComponent>(Nz::Vector3f(10000.f, 0.f, 0.f));
83+
switchToPlanet1Entity.emplace<tsom::EnvironmentProxyComponent>().targetEnvironment = &planet;
84+
switchToPlanet1Entity.emplace<tsom::NetworkedComponent>();
85+
enterPlanet1Trigger.targetEnvironment = &planet;
86+
enterPlanet1Trigger.updateRoot = true;
87+
88+
instance.SetDefaultSpawnpoint(&planet2, Nz::Vector3f::Up() * 120.f + Nz::Vector3f::Backward() * 5.f, Nz::Quaternionf::Identity());
89+
6290
fmt::print(fg(fmt::color::lime_green), "server ready.\n");
6391

6492
if (Nz::UInt32 maxStuckTime = config.GetIntegerValue<Nz::UInt32>("Server.MaxStuckSeconds"))

src/ServerLib/ServerPlanetEnvironment.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace tsom
3030
{
3131
constexpr unsigned int chunkSaveVersion = 1;
3232

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

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

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

0 commit comments

Comments
 (0)