Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add per-player time offset #7002

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
9f7b789
Add setTimeOffset and getTimeOffset
Arcelmi Feb 2, 2018
bbd6d11
Send time of day per-player separately
Arcelmi Feb 2, 2018
8360eca
Add set_time_offset and get_time_offset
Arcelmi Feb 2, 2018
e1f8ebd
Add set_time_offset and get_time_offset
Arcelmi Feb 2, 2018
4e4a91a
Change set_time_offset description
Arcelmi Feb 2, 2018
68b2d61
Change SendTimeOfDay to public
Arcelmi Feb 2, 2018
0ad2f9a
Fix missing code for time offsets
Arcelmi Feb 2, 2018
e232279
Change return output of getTimeOffset
Arcelmi Feb 2, 2018
866b5b9
Send time of day after offset changes only to player
Arcelmi Feb 2, 2018
008075c
Wrong folder
Arcelmi Feb 2, 2018
4e01067
Send timeofday on offset changes only to one client
Arcelmi Feb 2, 2018
4333f28
Add blank line above loop
Arcelmi Feb 4, 2018
34ca122
Fix wrong indents
Arcelmi Feb 5, 2018
8d235fb
Add space after conditions
Arcelmi Feb 5, 2018
ba25359
Change to code style guidelines
Arcelmi Feb 5, 2018
017aeda
Fix speed
Arcelmi Feb 5, 2018
5bd2df9
Fix error
Arcelmi Feb 5, 2018
6fc0153
Change indent to spaces
Arcelmi Feb 24, 2018
a52acb7
Remove return in loop
Arcelmi Feb 24, 2018
9b2afd9
Add spaces arround operator
Arcelmi Feb 24, 2018
c00f425
Change player==NULL to !player
Arcelmi Mar 13, 2018
a12684e
Changed player==null to !player
Arcelmi Mar 13, 2018
b81517d
Add files to clang whitelist
Arcelmi Apr 12, 2018
b7204e3
Fix mistake
Arcelmi Apr 12, 2018
217f765
Change time offset description
Arcelmi Apr 22, 2018
f4840bd
Remove unnecessary braces
Arcelmi Apr 22, 2018
e7cfd67
Change condition to modulo
Arcelmi Apr 22, 2018
45fd9cb
Change unsigned to signed int
Arcelmi Apr 22, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -3783,6 +3783,10 @@ This is basically a reference to a C++ `ServerActiveObject`
* `0`...`1`: Overrides day-night ratio, controlling sunlight to a specific amount
* `nil`: Disables override, defaulting to sunlight based on day-night cycle
* `get_day_night_ratio()`: returns the ratio or nil if it isn't overridden
* `set_time_offset(offset)`
* Adds offset to the time of the player
e.g. TimeOfDay is 12:00 and the offset of the player is 1000 then the time of the player is 13:00
paramat marked this conversation as resolved.
Show resolved Hide resolved
* `get_time_offset()` returns the players time of day offset
* `set_local_animation(stand/idle, walk, dig, walk+dig, frame_speed=frame_speed)`:
set animation for player model in third person view

Expand Down
13 changes: 13 additions & 0 deletions src/remoteplayer.h
Expand Up @@ -69,6 +69,16 @@ class RemotePlayer : public Player
*ratio = m_day_night_ratio;
}

void setTimeOffset(u16 offset)
{
m_time_offset = offset;
}

u16 getTimeOffset()
{
return m_time_offset;
}

void setHotbarImage(const std::string &name) { hud_hotbar_image = name; }

std::string getHotbarImage() const { return hud_hotbar_image; }
Expand Down Expand Up @@ -161,6 +171,9 @@ class RemotePlayer : public Player

bool m_day_night_ratio_do_override = false;
float m_day_night_ratio;

u16 m_time_offset = 0;

std::string hud_hotbar_image = "";
std::string hud_hotbar_selected_image = "";

Expand Down
41 changes: 41 additions & 0 deletions src/script/lua_api/l_object.cpp
Expand Up @@ -1720,6 +1720,45 @@ int ObjectRef::l_get_day_night_ratio(lua_State *L)
return 1;
}

// set_time_offset(self, offset)
int ObjectRef::l_set_time_offset(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
RemotePlayer *player = getplayer(ref);
if (!player)
return 0;

int offset_i = luaL_checknumber(L, 2);
u16 offset = offset_i;
//Negative values
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is the offset not just a signed number (s16)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because timeofday is an unsigned number and when I add a negative offset to timeofday and timeofday<0 then it would be the wrong time.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

either way, an offset of, say, 23999 would effectively work the same, and if anything you could perform a modulus (t % 24000)

if (offset_i < 0) {
offset = 24000 + offset_i;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove brackets or add a space after ) -> 0) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

brace removal, also the comment should explain why it's unsigned


player->setTimeOffset(offset);
getServer(L)->SendTimeOfDay(player->getPeerId(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this should not be done directly from here
instead of making SendTimeOfDay public, add a method in Serve such as updatePlayerTime() that does this

getEnv(L)->getTimeOfDay(), g_settings->getFloat("time_speed"));

lua_pushboolean(L, true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless return value as the player object must logically be valid and there's no way to fail otherwise.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It returns nil if (player == NULL).

return 1;
}

// get_time_offset(self)
int ObjectRef::l_get_time_offset(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
RemotePlayer *player = getplayer(ref);
if (!player)
return 0;

u16 offset = player->getTimeOffset();
lua_pushnumber(L, offset);

return 1;
}

ObjectRef::ObjectRef(ServerActiveObject *object):
m_object(object)
{
Expand Down Expand Up @@ -1854,6 +1893,8 @@ const luaL_Reg ObjectRef::methods[] = {
luamethod(ObjectRef, get_clouds),
luamethod(ObjectRef, override_day_night_ratio),
luamethod(ObjectRef, get_day_night_ratio),
luamethod(ObjectRef, set_time_offset),
luamethod(ObjectRef, get_time_offset),
luamethod(ObjectRef, set_local_animation),
luamethod(ObjectRef, get_local_animation),
luamethod(ObjectRef, set_eye_offset),
Expand Down
6 changes: 6 additions & 0 deletions src/script/lua_api/l_object.h
Expand Up @@ -316,6 +316,12 @@ class ObjectRef : public ModApiBase {
// get_day_night_ratio(self)
static int l_get_day_night_ratio(lua_State *L);

// set_time_offset(self, offset)
static int l_set_time_offset(lua_State *L);

// get_time_offset(self)
static int l_get_time_offset(lua_State *L);

// set_local_animation(self, {stand/idle}, {walk}, {dig}, {walk+dig}, frame_speed)
static int l_set_local_animation(lua_State *L);

Expand Down
23 changes: 20 additions & 3 deletions src/server.cpp
Expand Up @@ -1800,13 +1800,30 @@ void Server::SendOverrideDayNightRatio(session_t peer_id, bool do_override,

void Server::SendTimeOfDay(session_t peer_id, u16 time, f32 time_speed)
{
NetworkPacket pkt(TOCLIENT_TIME_OF_DAY, 0, peer_id);
pkt << time << time_speed;

if (peer_id == PEER_ID_INEXISTENT) {
m_clients.sendToAll(&pkt);
std::vector<session_t> clients = m_clients.getClientIDs();
for (const session_t peer_id : clients) {
RemotePlayer *player = m_env->getPlayer(peer_id);
if (player) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!player)
    continue;
[...]

NetworkPacket pkt(TOCLIENT_TIME_OF_DAY, 0, peer_id);
u16 time_offset = player->getTimeOffset();
u16 ntime = (time+time_offset);
pkt << ntime << time_speed;
Send(&pkt);
}
}
}
else {
RemotePlayer *player = m_env->getPlayer(peer_id);
// When the player is joining, player is NULL
if (!player) {
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. Either remove brackets or put a space after )

paramat marked this conversation as resolved.
Show resolved Hide resolved
NetworkPacket pkt(TOCLIENT_TIME_OF_DAY, 0, peer_id);
u16 time_offset = player->getTimeOffset();
u16 ntime = (time+time_offset);
pkt << ntime << time_speed;
Send(&pkt);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/server.h
Expand Up @@ -337,6 +337,7 @@ class Server : public con::PeerHandler, public MapEventReceiver,
void SendPlayerBreath(PlayerSAO *sao);
void SendInventory(PlayerSAO* playerSAO);
void SendMovePlayer(session_t peer_id);
void SendTimeOfDay(session_t peer_id, u16 time, f32 time_speed);

virtual bool registerModStorage(ModMetadata *storage);
virtual void unregisterModStorage(const std::string &name);
Expand Down Expand Up @@ -373,7 +374,6 @@ class Server : public con::PeerHandler, public MapEventReceiver,


void SendChatMessage(session_t peer_id, const ChatMessage &message);
void SendTimeOfDay(session_t peer_id, u16 time, f32 time_speed);
void SendPlayerHP(session_t peer_id);

void SendLocalPlayerAnimations(session_t peer_id, v2s32 animation_frames[4],
Expand Down
2 changes: 2 additions & 0 deletions util/travis/clang-format-whitelist.txt
Expand Up @@ -406,3 +406,5 @@ src/voxelalgorithms.h
src/voxel.cpp
src/voxel.h
src/wieldmesh.cpp
src/remoteplayer.h
src/gui/guiConfirmRegistration.cpp
paramat marked this conversation as resolved.
Show resolved Hide resolved