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 4 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
3 changes: 3 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -3783,6 +3783,9 @@ 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)
Copy link
Member

Choose a reason for hiding this comment

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

put the example on a separate line

* `get_time_offset()` returns the offset
Copy link
Member

Choose a reason for hiding this comment

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

* `get_time_offset()`: returns the players time of day offset

would be better

* `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;
}

void getTimeOffset(u16 *offset)
Copy link
Member

Choose a reason for hiding this comment

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

is there a reason why this isn't u16 getTimeOffset() const?

Copy link
Contributor

Choose a reason for hiding this comment

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

Why not just return the number instead of using a pointer?

Copy link
Author

Choose a reason for hiding this comment

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

I used getDayNightRatio as an example because I'm not so good in c++.

{
*offset = 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 == NULL)
Copy link
Member

Choose a reason for hiding this comment

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

!player

Copy link
Author

Choose a reason for hiding this comment

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

Every other function in this file is with player == NULL too.

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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
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

@SmallJoker SmallJoker Feb 4, 2018

Choose a reason for hiding this comment

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

}
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)->setTimeOfDay(getEnv(L)->getTimeOfDay());
Copy link
Member

Choose a reason for hiding this comment

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

better just resend the time to that one player


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 == NULL)
Copy link
Member

Choose a reason for hiding this comment

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

!player

Copy link
Author

Choose a reason for hiding this comment

The 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(&offset);
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
15 changes: 12 additions & 3 deletions src/server.cpp
Expand Up @@ -1800,13 +1800,22 @@ 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()) {
Copy link
Member

@SmallJoker SmallJoker Feb 4, 2018

Choose a reason for hiding this comment

The 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.
EDIT: player can be NULL/nullptr, so use continue if that applies.

RemotePlayer *player = m_env->getPlayer(client_name.c_str());
peer_id = player->getPeerId();
NetworkPacket pkt(TOCLIENT_TIME_OF_DAY, 0, peer_id);
Copy link
Member

Choose a reason for hiding this comment

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

please add a blank line above here

u16 time_offset;
player->getTimeOffset(&time_offset);
u16 ntime = (time+time_offset);
pkt << ntime << time_speed;
Send(&pkt);
Copy link
Member

Choose a reason for hiding this comment

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

you could move the actual implementation into the else and have this code only call SendTimeOfDay(peer_id, time, time_speed) for each player

}
}
else {
NetworkPacket pkt(TOCLIENT_TIME_OF_DAY, 0, peer_id);
pkt << time << time_speed;
Copy link
Member

Choose a reason for hiding this comment

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

this code does not account for time offsets

Send(&pkt);
}
}
Expand Down