Skip to content

Commit e403115

Browse files
raymooZeno-
authored andcommittedNov 12, 2016
Add control information to player interacts (#4685)
1 parent 67ec2fa commit e403115

File tree

4 files changed

+76
-49
lines changed

4 files changed

+76
-49
lines changed
 

‎src/client.cpp

+33-19
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,30 @@ void Client::Send(NetworkPacket* pkt)
929929
serverCommandFactoryTable[pkt->getCommand()].reliable);
930930
}
931931

932+
// Will fill up 12 + 12 + 4 + 4 + 4 bytes
933+
void writePlayerPos(LocalPlayer *myplayer, NetworkPacket *pkt)
934+
{
935+
v3f pf = myplayer->getPosition() * 100;
936+
v3f sf = myplayer->getSpeed() * 100;
937+
s32 pitch = myplayer->getPitch() * 100;
938+
s32 yaw = myplayer->getYaw() * 100;
939+
u32 keyPressed = myplayer->keyPressed;
940+
941+
v3s32 position(pf.X, pf.Y, pf.Z);
942+
v3s32 speed(sf.X, sf.Y, sf.Z);
943+
944+
/*
945+
Format:
946+
[0] v3s32 position*100
947+
[12] v3s32 speed*100
948+
[12+12] s32 pitch*100
949+
[12+12+4] s32 yaw*100
950+
[12+12+4+4] u32 keyPressed
951+
*/
952+
953+
*pkt << position << speed << pitch << yaw << keyPressed;
954+
}
955+
932956
void Client::interact(u8 action, const PointedThing& pointed)
933957
{
934958
if(m_state != LC_Ready) {
@@ -938,12 +962,17 @@ void Client::interact(u8 action, const PointedThing& pointed)
938962
return;
939963
}
940964

965+
LocalPlayer *myplayer = m_env.getLocalPlayer();
966+
if (myplayer == NULL)
967+
return;
968+
941969
/*
942970
[0] u16 command
943971
[2] u8 action
944972
[3] u16 item
945-
[5] u32 length of the next item
973+
[5] u32 length of the next item (plen)
946974
[9] serialized PointedThing
975+
[9 + plen] player position information
947976
actions:
948977
0: start digging (from undersurface) or use
949978
1: stop digging (all parameters ignored)
@@ -963,6 +992,8 @@ void Client::interact(u8 action, const PointedThing& pointed)
963992

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

995+
writePlayerPos(myplayer, &pkt);
996+
966997
Send(&pkt);
967998
}
968999

@@ -1291,26 +1322,9 @@ void Client::sendPlayerPos()
12911322

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

1294-
v3f pf = myplayer->getPosition();
1295-
v3f sf = myplayer->getSpeed();
1296-
s32 pitch = myplayer->getPitch() * 100;
1297-
s32 yaw = myplayer->getYaw() * 100;
1298-
u32 keyPressed = myplayer->keyPressed;
1299-
1300-
v3s32 position(pf.X*100, pf.Y*100, pf.Z*100);
1301-
v3s32 speed(sf.X*100, sf.Y*100, sf.Z*100);
1302-
/*
1303-
Format:
1304-
[0] v3s32 position*100
1305-
[12] v3s32 speed*100
1306-
[12+12] s32 pitch*100
1307-
[12+12+4] s32 yaw*100
1308-
[12+12+4+4] u32 keyPressed
1309-
*/
1310-
13111325
NetworkPacket pkt(TOSERVER_PLAYERPOS, 12 + 12 + 4 + 4 + 4);
13121326

1313-
pkt << position << speed << pitch << yaw << keyPressed;
1327+
writePlayerPos(myplayer, &pkt);
13141328

13151329
Send(&pkt);
13161330
}

‎src/network/networkpacket.h

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class NetworkPacket
4040
u32 getSize() { return m_datasize; }
4141
u16 getPeerId() { return m_peer_id; }
4242
u16 getCommand() { return m_command; }
43+
const u32 getRemainingBytes() const { return m_datasize - m_read_offset; }
4344

4445
// Returns a c-string without copying.
4546
// A better name for this would be getRawString()

‎src/network/serverpackethandler.cpp

+38-30
Original file line numberDiff line numberDiff line change
@@ -774,9 +774,10 @@ void Server::handleCommand_GotBlocks(NetworkPacket* pkt)
774774
}
775775
}
776776

777-
void Server::handleCommand_PlayerPos(NetworkPacket* pkt)
777+
void Server::process_PlayerPos(RemotePlayer *player, PlayerSAO *playersao,
778+
NetworkPacket *pkt)
778779
{
779-
if (pkt->getSize() < 12 + 12 + 4 + 4)
780+
if (pkt->getRemainingBytes() < 12 + 12 + 4 + 4)
780781
return;
781782

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

794-
if (pkt->getSize() >= 12 + 12 + 4 + 4 + 4)
795+
if (pkt->getRemainingBytes() >= 4)
795796
*pkt >> keyPressed;
796797

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

804+
playersao->setBasePosition(position);
805+
player->setSpeed(speed);
806+
playersao->setPitch(pitch);
807+
playersao->setYaw(yaw);
808+
player->keyPressed = keyPressed;
809+
player->control.up = (keyPressed & 1);
810+
player->control.down = (keyPressed & 2);
811+
player->control.left = (keyPressed & 4);
812+
player->control.right = (keyPressed & 8);
813+
player->control.jump = (keyPressed & 16);
814+
player->control.aux1 = (keyPressed & 32);
815+
player->control.sneak = (keyPressed & 64);
816+
player->control.LMB = (keyPressed & 128);
817+
player->control.RMB = (keyPressed & 256);
818+
819+
if (playersao->checkMovementCheat()) {
820+
// Call callbacks
821+
m_script->on_cheat(playersao, "moved_too_fast");
822+
SendMovePlayer(pkt->getPeerId());
823+
}
824+
}
825+
826+
void Server::handleCommand_PlayerPos(NetworkPacket* pkt)
827+
{
803828
RemotePlayer *player = m_env->getPlayer(pkt->getPeerId());
804829
if (player == NULL) {
805830
errorstream << "Server::ProcessData(): Canceling: "
@@ -825,26 +850,7 @@ void Server::handleCommand_PlayerPos(NetworkPacket* pkt)
825850
return;
826851
}
827852

828-
playersao->setBasePosition(position);
829-
player->setSpeed(speed);
830-
playersao->setPitch(pitch);
831-
playersao->setYaw(yaw);
832-
player->keyPressed = keyPressed;
833-
player->control.up = (keyPressed & 1);
834-
player->control.down = (keyPressed & 2);
835-
player->control.left = (keyPressed & 4);
836-
player->control.right = (keyPressed & 8);
837-
player->control.jump = (keyPressed & 16);
838-
player->control.aux1 = (keyPressed & 32);
839-
player->control.sneak = (keyPressed & 64);
840-
player->control.LMB = (keyPressed & 128);
841-
player->control.RMB = (keyPressed & 256);
842-
843-
if (playersao->checkMovementCheat()) {
844-
// Call callbacks
845-
m_script->on_cheat(playersao, "moved_too_fast");
846-
SendMovePlayer(pkt->getPeerId());
847-
}
853+
process_PlayerPos(player, playersao, pkt);
848854
}
849855

850856
void Server::handleCommand_DeletedBlocks(NetworkPacket* pkt)
@@ -1281,25 +1287,25 @@ void Server::handleCommand_Respawn(NetworkPacket* pkt)
12811287

12821288
void Server::handleCommand_Interact(NetworkPacket* pkt)
12831289
{
1284-
std::string datastring(pkt->getString(0), pkt->getSize());
1285-
std::istringstream is(datastring, std::ios_base::binary);
1286-
12871290
/*
12881291
[0] u16 command
12891292
[2] u8 action
12901293
[3] u16 item
1291-
[5] u32 length of the next item
1294+
[5] u32 length of the next item (plen)
12921295
[9] serialized PointedThing
1296+
[9 + plen] player position information
12931297
actions:
12941298
0: start digging (from undersurface) or use
12951299
1: stop digging (all parameters ignored)
12961300
2: digging completed
12971301
3: place block or item (to abovesurface)
12981302
4: use item
12991303
*/
1300-
u8 action = readU8(is);
1301-
u16 item_i = readU16(is);
1302-
std::istringstream tmp_is(deSerializeLongString(is), std::ios::binary);
1304+
u8 action;
1305+
u16 item_i;
1306+
*pkt >> action;
1307+
*pkt >> item_i;
1308+
std::istringstream tmp_is(pkt->readLongString(), std::ios::binary);
13031309
PointedThing pointed;
13041310
pointed.deSerialize(tmp_is);
13051311

@@ -1331,6 +1337,8 @@ void Server::handleCommand_Interact(NetworkPacket* pkt)
13311337
return;
13321338
}
13331339

1340+
process_PlayerPos(player, playersao, pkt);
1341+
13341342
v3f player_pos = playersao->getLastGoodPosition();
13351343

13361344
// Update wielded item

‎src/server.h

+4
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ class Server : public con::PeerHandler, public MapEventReceiver,
197197

198198
void Send(NetworkPacket* pkt);
199199

200+
// Helper for handleCommand_PlayerPos and handleCommand_Interact
201+
void process_PlayerPos(RemotePlayer *player, PlayerSAO *playersao,
202+
NetworkPacket *pkt);
203+
200204
// Both setter and getter need no envlock,
201205
// can be called freely from threads
202206
void setTimeOfDay(u32 time);

0 commit comments

Comments
 (0)
Please sign in to comment.