Add player:override_day_night_ratio() for arbitrarily controlling sunlight brightness
authorPerttu Ahola <celeron55@gmail.com>
Fri, 18 Oct 2013 08:53:19 +0000 (11:53 +0300)
committersapier <Sapier at GMX dot net>
Sat, 1 Feb 2014 17:38:21 +0000 (18:38 +0100)
doc/lua_api.txt
src/client.cpp
src/client.h
src/clientserver.h
src/environment.cpp
src/environment.h
src/game.cpp
src/script/lua_api/l_object.cpp
src/script/lua_api/l_object.h
src/server.cpp
src/server.h

index beec5408522e9a46e386221ce6ddb8340f6cec87..f42cec76c20e2d6374f020ce52fcaca29c4948be 100644 (file)
@@ -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:
index 48b00377f36138077191cf76399ce92a2ef7c8a3..ca6c80c4d2696376d4a879e0a1fe524f21058796 100644 (file)
@@ -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 "
index 33d806f9f6ffb8eedcd13fd92f63c122d05bcb9c..f693bb14211aa3f16388aacce68e3745d75ef6dd 100644 (file)
@@ -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<std::string> *params;
                } set_sky;
+               struct{
+                       bool do_override;
+                       float ratio_f;
+               } override_day_night_ratio;
        };
 };
 
index ecdc644dcb31675f087a5bce5fc41ced40d887db..29d01e5ae6dad526599b1854886b88f8958c354b 100644 (file)
@@ -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
index 5249dd9ec5757de6b1a8afb981feaa4e34ef83d7..aea3afeaa9b25540617e89952bb337eace73c020 100644 (file)
@@ -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<Player*> 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);
 }
index e439b2ef52b2475e961d7053c5225ff38a258a7b..ffd4a15541e61fa91e647558780ace04a9e55261 100644 (file)
@@ -78,7 +78,7 @@ public:
        std::list<Player*> 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<Player*> 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;
 };
 
 /*
index ef694b6afebfaf9e0a4988f42ec5f121f36f7499..56519d30aa9dbfa40593315c7e028c8fcd239477 100644 (file)
@@ -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);
+                               }
                        }
                }
                
index 86e5a71cd4a8c10c3765c51ff028dddccccaf531..059496c3d55701e53da3953f8f608c29b5cb48d3 100644 (file)
@@ -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}
 };
index b41938ca7c6d151fe0d4cad6e450421c302f9e34..2c53d1a6959b374e8d517a4db041015fa2a4a067 100644 (file)
@@ -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);
 
index 644aec1f5cd242b5e134338dcc129353082a92b4..5b799eb2cd99f8b7c7b98614d241759075f1cc47 100644 (file)
@@ -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<u8> 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);
index e493c5ea985eeedc73047102709f480b491bac77..21e9c811cf0460027301c731cc140b855aca80f5 100644 (file)
@@ -322,6 +322,9 @@ public:
                        
        bool setSky(Player *player, const video::SColor &bgcolor,
                        const std::string &type, const std::vector<std::string> &params);
+       
+       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<std::string> &params);
+       void SendOverrideDayNightRatio(u16 peer_id, bool do_override, float ratio);
        
        /*
                Send a node removal/addition event to all clients except ignore_id.