From 6a3fa9df12d5982f84e83e1c61ff757edc1043f4 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Fri, 18 Oct 2013 11:53:19 +0300 Subject: [PATCH] Add player:override_day_night_ratio() for arbitrarily controlling sunlight brightness --- doc/lua_api.txt | 3 +++ src/client.cpp | 14 ++++++++++++++ src/client.h | 5 +++++ src/clientserver.h | 7 +++++++ src/environment.cpp | 6 +++++- src/environment.h | 11 ++++++++++- src/game.cpp | 6 ++++++ src/script/lua_api/l_object.cpp | 23 +++++++++++++++++++++++ src/script/lua_api/l_object.h | 3 +++ src/server.cpp | 27 +++++++++++++++++++++++++++ src/server.h | 4 ++++ 11 files changed, 107 insertions(+), 2 deletions(-) diff --git a/doc/lua_api.txt b/doc/lua_api.txt index beec5408..f42cec76 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -1820,6 +1820,9 @@ Player-only: (no-op for other objects) - "plain": Uses 0 textures, bgcolor used ^ Note: currently does not work directly in on_joinplayer; use minetest.after(0) in there. +- override_day_night_ratio(ratio or nil) + ^ 0...1: Overrides day-night ratio, controlling sunlight to a specific amount + ^ nil: Disables override, defaulting to sunlight based on day-night cycle InvRef: Reference to an inventory methods: diff --git a/src/client.cpp b/src/client.cpp index 48b00377..ca6c80c4 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -2045,6 +2045,20 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) event.set_sky.params = params; m_client_event_queue.push_back(event); } + else if(command == TOCLIENT_OVERRIDE_DAY_NIGHT_RATIO) + { + std::string datastring((char *)&data[2], datasize - 2); + std::istringstream is(datastring, std::ios_base::binary); + + bool do_override = readU8(is); + float day_night_ratio_f = (float)readU16(is) / 65536; + + ClientEvent event; + event.type = CE_OVERRIDE_DAY_NIGHT_RATIO; + event.override_day_night_ratio.do_override = do_override; + event.override_day_night_ratio.ratio_f = day_night_ratio_f; + m_client_event_queue.push_back(event); + } else { infostream<<"Client: Ignoring unknown command " diff --git a/src/client.h b/src/client.h index 33d806f9..f693bb14 100644 --- a/src/client.h +++ b/src/client.h @@ -135,6 +135,7 @@ enum ClientEventType CE_HUDRM, CE_HUDCHANGE, CE_SET_SKY, + CE_OVERRIDE_DAY_NIGHT_RATIO, }; struct ClientEvent @@ -223,6 +224,10 @@ struct ClientEvent std::string *type; std::vector *params; } set_sky; + struct{ + bool do_override; + float ratio_f; + } override_day_night_ratio; }; }; diff --git a/src/clientserver.h b/src/clientserver.h index ecdc644d..29d01e5a 100644 --- a/src/clientserver.h +++ b/src/clientserver.h @@ -519,6 +519,13 @@ enum ToClientCommand u8 len u8[len] param */ + + TOCLIENT_OVERRIDE_DAY_NIGHT_RATIO = 0x50, + /* + u16 command + u8 do_override (boolean) + u16 day-night ratio 0...65535 + */ }; enum ToServerCommand diff --git a/src/environment.cpp b/src/environment.cpp index 5249dd9e..aea3afea 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -49,7 +49,9 @@ Environment::Environment(): m_time_of_day(9000), m_time_of_day_f(9000./24000), m_time_of_day_speed(0), - m_time_counter(0) + m_time_counter(0), + m_enable_day_night_ratio_override(false), + m_day_night_ratio_override(0.0f) { } @@ -190,6 +192,8 @@ std::list Environment::getPlayers(bool ignore_disconnected) u32 Environment::getDayNightRatio() { + if(m_enable_day_night_ratio_override) + return m_day_night_ratio_override; bool smooth = g_settings->getBool("enable_shaders"); return time_to_daynight_ratio(m_time_of_day_f*24000, smooth); } diff --git a/src/environment.h b/src/environment.h index e439b2ef..ffd4a155 100644 --- a/src/environment.h +++ b/src/environment.h @@ -78,7 +78,7 @@ public: std::list getPlayers(bool ignore_disconnected); u32 getDayNightRatio(); - + // 0-23999 virtual void setTimeOfDay(u32 time) { @@ -100,6 +100,12 @@ public: float getTimeOfDaySpeed() { return m_time_of_day_speed; } + void setDayNightRatioOverride(bool enable, u32 value) + { + m_enable_day_night_ratio_override = enable; + m_day_night_ratio_override = value; + } + protected: // peer_ids in here should be unique, except that there may be many 0s std::list m_players; @@ -110,6 +116,9 @@ protected: float m_time_of_day_speed; // Used to buffer dtime for adding to m_time_of_day float m_time_counter; + // Overriding the day-night ratio is useful for custom sky visuals + bool m_enable_day_night_ratio_override; + u32 m_day_night_ratio_override; }; /* diff --git a/src/game.cpp b/src/game.cpp index ef694b6a..56519d30 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -2498,6 +2498,12 @@ void the_game( delete event.set_sky.type; delete event.set_sky.params; } + else if (event.type == CE_OVERRIDE_DAY_NIGHT_RATIO) + { + bool enable = event.override_day_night_ratio.do_override; + u32 value = event.override_day_night_ratio.ratio_f * 1000; + client.getEnv().setDayNightRatioOverride(enable, value); + } } } diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index 86e5a71c..059496c3 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -1129,6 +1129,28 @@ int ObjectRef::l_set_sky(lua_State *L) return 1; } +// override_day_night_ratio(self, brightness=0...1) +int ObjectRef::l_override_day_night_ratio(lua_State *L) +{ + ObjectRef *ref = checkobject(L, 1); + Player *player = getplayer(ref); + if (player == NULL) + return 0; + + bool do_override = false; + float ratio = 0.0f; + if (!lua_isnil(L, 2)){ + do_override = true; + ratio = luaL_checknumber(L, 2); + } + + if (!getServer(L)->overrideDayNightRatio(player, do_override, ratio)) + return 0; + + lua_pushboolean(L, true); + return 1; +} + ObjectRef::ObjectRef(ServerActiveObject *object): m_object(object) { @@ -1247,5 +1269,6 @@ const luaL_reg ObjectRef::methods[] = { luamethod(ObjectRef, hud_set_hotbar_image), luamethod(ObjectRef, hud_set_hotbar_selected_image), luamethod(ObjectRef, set_sky), + luamethod(ObjectRef, override_day_night_ratio), {0,0} }; diff --git a/src/script/lua_api/l_object.h b/src/script/lua_api/l_object.h index b41938ca..2c53d1a6 100644 --- a/src/script/lua_api/l_object.h +++ b/src/script/lua_api/l_object.h @@ -228,6 +228,9 @@ private: // set_sky(self, type, list) static int l_set_sky(lua_State *L); + // override_day_night_ratio(self, type, list) + static int l_override_day_night_ratio(lua_State *L); + public: ObjectRef(ServerActiveObject *object); diff --git a/src/server.cpp b/src/server.cpp index 644aec1f..5b799eb2 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -3291,6 +3291,23 @@ void Server::SendSetSky(u16 peer_id, const video::SColor &bgcolor, m_clients.send(peer_id, 0, data, true); } +void Server::SendOverrideDayNightRatio(u16 peer_id, bool do_override, + float ratio) +{ + std::ostringstream os(std::ios_base::binary); + + // Write command + writeU16(os, TOCLIENT_OVERRIDE_DAY_NIGHT_RATIO); + writeU8(os, do_override); + writeU16(os, ratio*65535); + + // Make data buffer + std::string s = os.str(); + SharedBuffer data((u8 *)s.c_str(), s.size()); + // Send as reliable + m_clients.send(peer_id, 0, data, true); +} + void Server::SendTimeOfDay(u16 peer_id, u16 time, f32 time_speed) { DSTACK(__FUNCTION_NAME); @@ -4465,6 +4482,16 @@ bool Server::setSky(Player *player, const video::SColor &bgcolor, return true; } +bool Server::overrideDayNightRatio(Player *player, bool do_override, + float ratio) +{ + if (!player) + return false; + + SendOverrideDayNightRatio(player->peer_id, do_override, ratio); + return true; +} + void Server::notifyPlayers(const std::wstring msg) { SendChatMessage(PEER_ID_INEXISTENT,msg); diff --git a/src/server.h b/src/server.h index e493c5ea..21e9c811 100644 --- a/src/server.h +++ b/src/server.h @@ -322,6 +322,9 @@ public: bool setSky(Player *player, const video::SColor &bgcolor, const std::string &type, const std::vector ¶ms); + + bool overrideDayNightRatio(Player *player, bool do_override, + float brightness); /* con::PeerHandler implementation. */ void peerAdded(con::Peer *peer); @@ -360,6 +363,7 @@ private: void SendHUDSetParam(u16 peer_id, u16 param, const std::string &value); void SendSetSky(u16 peer_id, const video::SColor &bgcolor, const std::string &type, const std::vector ¶ms); + void SendOverrideDayNightRatio(u16 peer_id, bool do_override, float ratio); /* Send a node removal/addition event to all clients except ignore_id. -- 2.30.2