Skip to content

Commit

Permalink
Add control information to player interacts (#4685)
Browse files Browse the repository at this point in the history
  • Loading branch information
raymoo authored and Zeno- committed Nov 12, 2016
1 parent 67ec2fa commit e403115
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 49 deletions.
52 changes: 33 additions & 19 deletions src/client.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -929,6 +929,30 @@ void Client::Send(NetworkPacket* pkt)
serverCommandFactoryTable[pkt->getCommand()].reliable); serverCommandFactoryTable[pkt->getCommand()].reliable);
} }


// Will fill up 12 + 12 + 4 + 4 + 4 bytes
void writePlayerPos(LocalPlayer *myplayer, NetworkPacket *pkt)
{
v3f pf = myplayer->getPosition() * 100;
v3f sf = myplayer->getSpeed() * 100;
s32 pitch = myplayer->getPitch() * 100;
s32 yaw = myplayer->getYaw() * 100;
u32 keyPressed = myplayer->keyPressed;

v3s32 position(pf.X, pf.Y, pf.Z);
v3s32 speed(sf.X, sf.Y, sf.Z);

/*
Format:
[0] v3s32 position*100
[12] v3s32 speed*100
[12+12] s32 pitch*100
[12+12+4] s32 yaw*100
[12+12+4+4] u32 keyPressed
*/

*pkt << position << speed << pitch << yaw << keyPressed;
}

void Client::interact(u8 action, const PointedThing& pointed) void Client::interact(u8 action, const PointedThing& pointed)
{ {
if(m_state != LC_Ready) { if(m_state != LC_Ready) {
Expand All @@ -938,12 +962,17 @@ void Client::interact(u8 action, const PointedThing& pointed)
return; return;
} }


LocalPlayer *myplayer = m_env.getLocalPlayer();
if (myplayer == NULL)
return;

/* /*
[0] u16 command [0] u16 command
[2] u8 action [2] u8 action
[3] u16 item [3] u16 item
[5] u32 length of the next item [5] u32 length of the next item (plen)
[9] serialized PointedThing [9] serialized PointedThing
[9 + plen] player position information
actions: actions:
0: start digging (from undersurface) or use 0: start digging (from undersurface) or use
1: stop digging (all parameters ignored) 1: stop digging (all parameters ignored)
Expand All @@ -963,6 +992,8 @@ void Client::interact(u8 action, const PointedThing& pointed)


pkt.putLongString(tmp_os.str()); pkt.putLongString(tmp_os.str());


writePlayerPos(myplayer, &pkt);

Send(&pkt); Send(&pkt);
} }


Expand Down Expand Up @@ -1291,26 +1322,9 @@ void Client::sendPlayerPos()


assert(myplayer->peer_id == our_peer_id); assert(myplayer->peer_id == our_peer_id);


v3f pf = myplayer->getPosition();
v3f sf = myplayer->getSpeed();
s32 pitch = myplayer->getPitch() * 100;
s32 yaw = myplayer->getYaw() * 100;
u32 keyPressed = myplayer->keyPressed;

v3s32 position(pf.X*100, pf.Y*100, pf.Z*100);
v3s32 speed(sf.X*100, sf.Y*100, sf.Z*100);
/*
Format:
[0] v3s32 position*100
[12] v3s32 speed*100
[12+12] s32 pitch*100
[12+12+4] s32 yaw*100
[12+12+4+4] u32 keyPressed
*/

NetworkPacket pkt(TOSERVER_PLAYERPOS, 12 + 12 + 4 + 4 + 4); NetworkPacket pkt(TOSERVER_PLAYERPOS, 12 + 12 + 4 + 4 + 4);


pkt << position << speed << pitch << yaw << keyPressed; writePlayerPos(myplayer, &pkt);


Send(&pkt); Send(&pkt);
} }
Expand Down
1 change: 1 addition & 0 deletions src/network/networkpacket.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class NetworkPacket
u32 getSize() { return m_datasize; } u32 getSize() { return m_datasize; }
u16 getPeerId() { return m_peer_id; } u16 getPeerId() { return m_peer_id; }
u16 getCommand() { return m_command; } u16 getCommand() { return m_command; }
const u32 getRemainingBytes() const { return m_datasize - m_read_offset; }


// Returns a c-string without copying. // Returns a c-string without copying.
// A better name for this would be getRawString() // A better name for this would be getRawString()
Expand Down
68 changes: 38 additions & 30 deletions src/network/serverpackethandler.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -774,9 +774,10 @@ void Server::handleCommand_GotBlocks(NetworkPacket* pkt)
} }
} }


void Server::handleCommand_PlayerPos(NetworkPacket* pkt) void Server::process_PlayerPos(RemotePlayer *player, PlayerSAO *playersao,
NetworkPacket *pkt)
{ {
if (pkt->getSize() < 12 + 12 + 4 + 4) if (pkt->getRemainingBytes() < 12 + 12 + 4 + 4)
return; return;


v3s32 ps, ss; v3s32 ps, ss;
Expand All @@ -791,7 +792,7 @@ void Server::handleCommand_PlayerPos(NetworkPacket* pkt)
f32 yaw = (f32)f32yaw / 100.0; f32 yaw = (f32)f32yaw / 100.0;
u32 keyPressed = 0; u32 keyPressed = 0;


if (pkt->getSize() >= 12 + 12 + 4 + 4 + 4) if (pkt->getRemainingBytes() >= 4)
*pkt >> keyPressed; *pkt >> keyPressed;


v3f position((f32)ps.X / 100.0, (f32)ps.Y / 100.0, (f32)ps.Z / 100.0); v3f position((f32)ps.X / 100.0, (f32)ps.Y / 100.0, (f32)ps.Z / 100.0);
Expand All @@ -800,6 +801,30 @@ void Server::handleCommand_PlayerPos(NetworkPacket* pkt)
pitch = modulo360f(pitch); pitch = modulo360f(pitch);
yaw = modulo360f(yaw); yaw = modulo360f(yaw);


playersao->setBasePosition(position);
player->setSpeed(speed);
playersao->setPitch(pitch);
playersao->setYaw(yaw);
player->keyPressed = keyPressed;
player->control.up = (keyPressed & 1);
player->control.down = (keyPressed & 2);
player->control.left = (keyPressed & 4);
player->control.right = (keyPressed & 8);
player->control.jump = (keyPressed & 16);
player->control.aux1 = (keyPressed & 32);
player->control.sneak = (keyPressed & 64);
player->control.LMB = (keyPressed & 128);
player->control.RMB = (keyPressed & 256);

if (playersao->checkMovementCheat()) {
// Call callbacks
m_script->on_cheat(playersao, "moved_too_fast");
SendMovePlayer(pkt->getPeerId());
}
}

void Server::handleCommand_PlayerPos(NetworkPacket* pkt)
{
RemotePlayer *player = m_env->getPlayer(pkt->getPeerId()); RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
if (player == NULL) { if (player == NULL) {
errorstream << "Server::ProcessData(): Canceling: " errorstream << "Server::ProcessData(): Canceling: "
Expand All @@ -825,26 +850,7 @@ void Server::handleCommand_PlayerPos(NetworkPacket* pkt)
return; return;
} }


playersao->setBasePosition(position); process_PlayerPos(player, playersao, pkt);
player->setSpeed(speed);
playersao->setPitch(pitch);
playersao->setYaw(yaw);
player->keyPressed = keyPressed;
player->control.up = (keyPressed & 1);
player->control.down = (keyPressed & 2);
player->control.left = (keyPressed & 4);
player->control.right = (keyPressed & 8);
player->control.jump = (keyPressed & 16);
player->control.aux1 = (keyPressed & 32);
player->control.sneak = (keyPressed & 64);
player->control.LMB = (keyPressed & 128);
player->control.RMB = (keyPressed & 256);

if (playersao->checkMovementCheat()) {
// Call callbacks
m_script->on_cheat(playersao, "moved_too_fast");
SendMovePlayer(pkt->getPeerId());
}
} }


void Server::handleCommand_DeletedBlocks(NetworkPacket* pkt) void Server::handleCommand_DeletedBlocks(NetworkPacket* pkt)
Expand Down Expand Up @@ -1281,25 +1287,25 @@ void Server::handleCommand_Respawn(NetworkPacket* pkt)


void Server::handleCommand_Interact(NetworkPacket* pkt) void Server::handleCommand_Interact(NetworkPacket* pkt)
{ {
std::string datastring(pkt->getString(0), pkt->getSize());
std::istringstream is(datastring, std::ios_base::binary);

/* /*
[0] u16 command [0] u16 command
[2] u8 action [2] u8 action
[3] u16 item [3] u16 item
[5] u32 length of the next item [5] u32 length of the next item (plen)
[9] serialized PointedThing [9] serialized PointedThing
[9 + plen] player position information
actions: actions:
0: start digging (from undersurface) or use 0: start digging (from undersurface) or use
1: stop digging (all parameters ignored) 1: stop digging (all parameters ignored)
2: digging completed 2: digging completed
3: place block or item (to abovesurface) 3: place block or item (to abovesurface)
4: use item 4: use item
*/ */
u8 action = readU8(is); u8 action;
u16 item_i = readU16(is); u16 item_i;
std::istringstream tmp_is(deSerializeLongString(is), std::ios::binary); *pkt >> action;
*pkt >> item_i;
std::istringstream tmp_is(pkt->readLongString(), std::ios::binary);
PointedThing pointed; PointedThing pointed;
pointed.deSerialize(tmp_is); pointed.deSerialize(tmp_is);


Expand Down Expand Up @@ -1331,6 +1337,8 @@ void Server::handleCommand_Interact(NetworkPacket* pkt)
return; return;
} }


process_PlayerPos(player, playersao, pkt);

v3f player_pos = playersao->getLastGoodPosition(); v3f player_pos = playersao->getLastGoodPosition();


// Update wielded item // Update wielded item
Expand Down
4 changes: 4 additions & 0 deletions src/server.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ class Server : public con::PeerHandler, public MapEventReceiver,


void Send(NetworkPacket* pkt); void Send(NetworkPacket* pkt);


// Helper for handleCommand_PlayerPos and handleCommand_Interact
void process_PlayerPos(RemotePlayer *player, PlayerSAO *playersao,
NetworkPacket *pkt);

// Both setter and getter need no envlock, // Both setter and getter need no envlock,
// can be called freely from threads // can be called freely from threads
void setTimeOfDay(u32 time); void setTimeOfDay(u32 time);
Expand Down

0 comments on commit e403115

Please sign in to comment.