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
Changes from 11 commits
9f7b789
bbd6d11
8360eca
e1f8ebd
4e4a91a
68b2d61
0ad2f9a
e232279
866b5b9
008075c
4e01067
4333f28
34ca122
8d235fb
ba25359
017aeda
5bd2df9
6fc0153
a52acb7
9b2afd9
c00f425
a12684e
b81517d
b7204e3
217f765
f4840bd
e7cfd67
45fd9cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1720,6 +1720,44 @@ 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 == NULL) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Every other function in this file is with player == NULL too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fact that other functions do the same doesn't mean it's good code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That was my first real C++-Code so I was inspired by the other code. |
||
return 0; | ||
|
||
int offset_i = luaL_checknumber(L, 2); | ||
u16 offset = offset_i; | ||
//Negative values | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is the offset not just a signed number ( There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spaces around operator |
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove brackets or add a space after There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(), getEnv(L)->getTimeOfDay(), g_settings->getFloat("time_speed")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line >80 characters |
||
|
||
lua_pushboolean(L, true); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 == NULL) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Every other function in this file is with player == NULL too. |
||
return 0; | ||
|
||
u16 offset = player->getTimeOffset(); | ||
lua_pushnumber(L, offset); | ||
|
||
return 1; | ||
} | ||
|
||
ObjectRef::ObjectRef(ServerActiveObject *object): | ||
m_object(object) | ||
{ | ||
|
@@ -1854,6 +1892,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), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1800,13 +1800,28 @@ 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); | ||
for (auto &client_name : m_clients.getPlayerNames()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. std::vector<session_t> clients = m_clients.getClientIDs();
for (const session_t peer_id : clients) {
const RemotePlayer *player = m_env->getPlayer(peer_id);
...
} is faster. |
||
RemotePlayer *player = m_env->getPlayer(client_name.c_str()); | ||
peer_id = player->getPeerId(); | ||
NetworkPacket pkt(TOCLIENT_TIME_OF_DAY, 0, peer_id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add a blank line above here |
||
u16 time_offset = player->getTimeOffset(); | ||
u16 ntime = (time+time_offset); | ||
pkt << ntime << time_speed; | ||
Send(&pkt); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could move the actual implementation into the |
||
} | ||
} | ||
else { | ||
RemotePlayer *player = m_env->getPlayer(peer_id); | ||
// When the player is joining, player is NULL | ||
if (!player){ | ||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong indents