Skip to content

Commit d959692

Browse files
authored
Option to make sky stars deterministic (#16529)
The star constellations are now based on a seed passed in StarParams.
1 parent b4aa73d commit d959692

File tree

8 files changed

+22
-3
lines changed

8 files changed

+22
-3
lines changed

doc/lua_api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9039,6 +9039,7 @@ child will follow movement and rotation of that bone.
90399039
alpha channel is used to set overall star brightness.
90409040
(default: `#ebebff69`)
90419041
* `scale`: Float controlling the overall size of the stars (default: `1`)
9042+
* `star_seed`: Integer number which decides how to generate the sky stars. If set to zero, client picks a random number. (default: `0`)
90429043
* `get_stars()`: returns a table with the current stars parameters as in
90439044
`set_stars`.
90449045
* `set_clouds(cloud_parameters)`: set cloud parameters

src/client/game.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2488,7 +2488,7 @@ void Game::handleClientEvent_SetSky(ClientEvent *event, CameraOrientation *cam)
24882488
);
24892489
} else if (event->set_sky->type == "skybox" &&
24902490
event->set_sky->textures.size() == 6) {
2491-
// Disable the dyanmic mesh skybox:
2491+
// Disable the dynamic mesh skybox:
24922492
sky->setVisible(false);
24932493
// Set fog colors:
24942494
sky->setFallbackBgColor(event->set_sky->bgcolor);
@@ -2564,6 +2564,7 @@ void Game::handleClientEvent_SetStars(ClientEvent *event, CameraOrientation *cam
25642564
sky->setStarColor(event->star_params->starcolor);
25652565
sky->setStarScale(event->star_params->scale);
25662566
sky->setStarDayOpacity(event->star_params->day_opacity);
2567+
sky->setStarSeed(event->star_params->star_seed);
25672568
delete event->star_params;
25682569
}
25692570

src/client/sky.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,15 @@ void Sky::setStarCount(u16 star_count)
807807
}
808808
}
809809

810+
void Sky::setStarSeed(u64 star_seed)
811+
{
812+
// Allow force updating star seed at game init.
813+
if (m_star_params.star_seed != star_seed || m_first_update) {
814+
m_star_params.star_seed = star_seed;
815+
updateStars();
816+
}
817+
}
818+
810819
void Sky::updateStars()
811820
{
812821
m_stars.reset(new scene::SMeshBuffer());
@@ -823,7 +832,8 @@ void Sky::updateStars()
823832
vertices.reserve(4 * m_star_params.count);
824833
indices.reserve(6 * m_star_params.count);
825834

826-
PcgRandom rgen(m_seed);
835+
u64 star_seed = m_star_params.star_seed == 0 ? m_seed : m_star_params.star_seed;
836+
PcgRandom rgen(star_seed);
827837
float d = (0.006 / 2) * m_star_params.scale;
828838
for (u16 i = 0; i < m_star_params.count; i++) {
829839
v3f r = v3f(

src/client/sky.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class Sky : public scene::ISceneNode
7777
void setStarColor(video::SColor star_color) { m_star_params.starcolor = star_color; }
7878
void setStarScale(f32 star_scale) { m_star_params.scale = star_scale; updateStars(); }
7979
void setStarDayOpacity(f32 day_opacity) { m_star_params.day_opacity = day_opacity; }
80+
void setStarSeed(u64 star_seed);
8081

8182
bool getCloudsVisible() const { return m_clouds_visible && m_clouds_enabled; }
8283
const video::SColorf &getCloudColor() const { return m_cloudcolor_f; }

src/network/clientpackethandler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,7 @@ void Client::handleCommand_HudSetStars(NetworkPacket *pkt)
14451445
>> stars.starcolor >> stars.scale;
14461446
try {
14471447
*pkt >> stars.day_opacity;
1448+
*pkt >> stars.star_seed;
14481449
} catch (PacketError &e) {};
14491450

14501451
ClientEvent *event = new ClientEvent();

src/script/lua_api/l_object.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2452,6 +2452,7 @@ int ObjectRef::l_set_stars(lua_State *L)
24522452
"scale", star_params.scale);
24532453
star_params.day_opacity = getfloatfield_default(L, 2,
24542454
"day_opacity", star_params.day_opacity);
2455+
star_params.star_seed = getintfield_default(L, 2, "star_seed", star_params.star_seed);
24552456
}
24562457

24572458
getServer(L)->setStars(player, star_params);
@@ -2480,6 +2481,8 @@ int ObjectRef::l_get_stars(lua_State *L)
24802481
lua_setfield(L, -2, "scale");
24812482
lua_pushnumber(L, star_params.day_opacity);
24822483
lua_setfield(L, -2, "day_opacity");
2484+
lua_pushnumber(L, star_params.star_seed);
2485+
lua_setfield(L, -2, "star_seed");
24832486
return 1;
24842487
}
24852488

src/server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1959,7 +1959,7 @@ void Server::SendSetStars(session_t peer_id, const StarParams &params)
19591959

19601960
pkt << params.visible << params.count
19611961
<< params.starcolor << params.scale
1962-
<< params.day_opacity;
1962+
<< params.day_opacity << params.star_seed;
19631963

19641964
Send(&pkt);
19651965
}

src/skyparams.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct StarParams
6464
video::SColor starcolor;
6565
f32 scale;
6666
f32 day_opacity;
67+
u64 star_seed;
6768
};
6869

6970
struct CloudParams
@@ -142,6 +143,7 @@ class SkyboxDefaults
142143
stars.starcolor = video::SColor(105, 235, 235, 255);
143144
stars.scale = 1;
144145
stars.day_opacity = 0;
146+
stars.star_seed = 0;
145147
return stars;
146148
}
147149

0 commit comments

Comments
 (0)