Skip to content

Commit

Permalink
Add player:override_day_night_ratio() for arbitrarily controlling sun…
Browse files Browse the repository at this point in the history
…light brightness
  • Loading branch information
celeron55 authored and sapier committed Feb 1, 2014
1 parent 86a6cca commit 6a3fa9d
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 2 deletions.
3 changes: 3 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -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:
Expand Down
14 changes: 14 additions & 0 deletions src/client.cpp
Expand Up @@ -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 "
Expand Down
5 changes: 5 additions & 0 deletions src/client.h
Expand Up @@ -135,6 +135,7 @@ enum ClientEventType
CE_HUDRM,
CE_HUDCHANGE,
CE_SET_SKY,
CE_OVERRIDE_DAY_NIGHT_RATIO,
};

struct ClientEvent
Expand Down Expand Up @@ -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;
};
};

Expand Down
7 changes: 7 additions & 0 deletions src/clientserver.h
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/environment.cpp
Expand Up @@ -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)
{
}

Expand Down Expand Up @@ -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);
}
Expand Down
11 changes: 10 additions & 1 deletion src/environment.h
Expand Up @@ -78,7 +78,7 @@ class Environment
std::list<Player*> getPlayers(bool ignore_disconnected);

u32 getDayNightRatio();

// 0-23999
virtual void setTimeOfDay(u32 time)
{
Expand All @@ -100,6 +100,12 @@ class Environment
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;
Expand All @@ -110,6 +116,9 @@ class Environment
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;
};

/*
Expand Down
6 changes: 6 additions & 0 deletions src/game.cpp
Expand Up @@ -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);
}
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/script/lua_api/l_object.cpp
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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}
};
3 changes: 3 additions & 0 deletions src/script/lua_api/l_object.h
Expand Up @@ -228,6 +228,9 @@ class ObjectRef : public ModApiBase {
// 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);

Expand Down
27 changes: 27 additions & 0 deletions src/server.cpp
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/server.h
Expand Up @@ -322,6 +322,9 @@ class Server : public con::PeerHandler, public MapEventReceiver,

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);
Expand Down Expand Up @@ -360,6 +363,7 @@ class Server : public con::PeerHandler, public MapEventReceiver,
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.
Expand Down

0 comments on commit 6a3fa9d

Please sign in to comment.