# HG changeset patch # User terom # Date 1227048629 0 # Node ID 1415a2d45686cc4f28d01183625a933336100307 # Parent be91c125f741fff7f145f2f777b8a2ce3e954c09 working simple network-physics code diff -r be91c125f741 -r 1415a2d45686 src/proto2/GameState.cc --- a/src/proto2/GameState.cc Tue Nov 18 22:07:36 2008 +0000 +++ b/src/proto2/GameState.cc Tue Nov 18 22:50:29 2008 +0000 @@ -17,6 +17,8 @@ if (input & INPUT_MOVE_RIGHT) fx += PLAYER_MOVE_FORCE; - // apply force - applyForce(Vector(fx, fy), INPUT_INTERVAL_MS); + if (fx || fy) { + // apply force + applyForce(Vector(fx, fy), INPUT_INTERVAL_MS); + } } diff -r be91c125f741 -r 1415a2d45686 src/proto2/Network.hh --- a/src/proto2/Network.hh Tue Nov 18 22:07:36 2008 +0000 +++ b/src/proto2/Network.hh Tue Nov 18 22:50:29 2008 +0000 @@ -6,6 +6,8 @@ #include +const int32_t COORDINATE_MAX = 1 << 30; + class NetworkCore { protected: GameState &state; @@ -18,24 +20,28 @@ // constructor NetworkCore (GameState &state) : state(state), netsession(NETWORK_APP_NAME), netobjs(&netsession, NETWORK_NETOBJ_CHAN) { } + + }; +// XXX: util methods +void writeVector (CL_NetPacket &pkt, const Vector &vec); +Vector readVector (CL_NetPacket &pkt); + enum NetworkMessage { NETMSG_PACKET_INVALID = 0x00, /* * You have joined the game: * - * uint32_t x - * uint32_t y + * Vector initial_position */ NETMSG_SERVER_HELLO = 0x0100, /* * New client has connected to server: - * - * uint32_t x - * uint32_t y + * + * Vector initial_position */ NETMSG_PLAYER_JOIN = 0x0101, @@ -48,24 +54,23 @@ /* * Client has moved * - * int32_t dx - * int32_t dy + * Vector impulse_force + * uint16_t impulse_ms */ NETMSG_CLIENT_MOVE = 0x0201, /* * Initial player info * - * uint32_t x - * uint32_t y + * Vector initial_position */ NETMSG_PLAYER_INFO = 0x0300, /* * Player position update * - * uint32_t x - * uint32_t y + * Vector position + * Vector velocity */ NETMSG_PLAYER_POSITION = 0x0301, }; diff -r be91c125f741 -r 1415a2d45686 src/proto2/NetworkClient.cc --- a/src/proto2/NetworkClient.cc Tue Nov 18 22:07:36 2008 +0000 +++ b/src/proto2/NetworkClient.cc Tue Nov 18 22:50:29 2008 +0000 @@ -38,15 +38,12 @@ void NetworkClient::on_server_hello (CL_NetObject_Client &obj, CL_NetPacket &pkt) { // read the packet - uint32_t x = pkt.input.read_uint32(); - uint32_t y = pkt.input.read_uint32(); - - Vector initial_position(x, y); + Vector position = readVector(pkt); - Engine::log(INFO, "client.on_server_hello") << "obj=" << obj << ", pos=" << initial_position; + Engine::log(INFO, "client.on_server_hello") << "obj=" << obj << ", pos=" << position; // create the LocalPlayer object - NetworkClientLocalPlayer *player = new NetworkClientLocalPlayer(*this, obj, initial_position); + NetworkClientLocalPlayer *player = new NetworkClientLocalPlayer(*this, obj, position); // inform state state.newLocalPlayer(player); @@ -54,15 +51,12 @@ void NetworkClient::on_player_info (CL_NetObject_Client &obj, CL_NetPacket &pkt) { // read the packet - uint32_t x = pkt.input.read_uint32(); - uint32_t y = pkt.input.read_uint32(); + Vector position = readVector(pkt); - Vector initial_position(x, y); - - Engine::log(INFO, "client.on_player_info") << "obj=" << obj << ", pos=" << initial_position; + Engine::log(INFO, "client.on_player_info") << "obj=" << obj << ", pos=" << position; // create the LocalPlayer object - NetworkClientRemotePlayer *player = new NetworkClientRemotePlayer(*this, obj, initial_position); + NetworkClientRemotePlayer *player = new NetworkClientRemotePlayer(*this, obj, position); // inform state state.newRemotePlayer(player); @@ -71,15 +65,12 @@ void NetworkClient::on_player_join (CL_NetObject_Client &obj, CL_NetPacket &pkt) { // read the packet - uint32_t x = pkt.input.read_uint32(); - uint32_t y = pkt.input.read_uint32(); + Vector position = readVector(pkt); - Vector initial_position(x, y); - - Engine::log(INFO, "client.on_player_join") << "obj=" << obj << ", pos=" << initial_position; + Engine::log(INFO, "client.on_player_join") << "obj=" << obj << ", pos=" << position; // create the RemotePlayer object - NetworkClientRemotePlayer *player = new NetworkClientRemotePlayer(*this, obj, initial_position); + NetworkClientRemotePlayer *player = new NetworkClientRemotePlayer(*this, obj, position); // inform state state.newRemotePlayer(player); @@ -94,38 +85,36 @@ // delete player; } -NetworkClientLocalPlayer::NetworkClientLocalPlayer (NetworkClient &client, CL_NetObject_Client &obj, Vector initial_position) : - LocalPlayer(client.state, initial_position, true), client(client), obj(obj) { +NetworkClientLocalPlayer::NetworkClientLocalPlayer (NetworkClient &client, CL_NetObject_Client &obj, Vector position) : + LocalPlayer(client.state, position, true), client(client), obj(obj) { // receive messages slots.connect(obj.sig_received_message(NETMSG_PLAYER_POSITION), this, &NetworkClientLocalPlayer::on_position); } -void NetworkClientLocalPlayer::handleMove (PlayerInput_Move move) { +void NetworkClientLocalPlayer::applyForce (Vector force, uint16_t dt) { // always send move, in all cases CL_NetPacket pkt; - pkt.output.write_uint16(move); + writeVector(pkt, force); + pkt.output.write_uint16(dt); obj.send(NETMSG_CLIENT_MOVE, pkt, false); - // handle locally - LocalPlayer::handleMove(move); + // do not handle locally } void NetworkClientLocalPlayer::on_position (CL_NetPacket &pkt) { - uint32_t x = pkt.input.read_uint32(); - uint32_t y = pkt.input.read_uint32(); + Vector position = readVector(pkt); + Vector velocity = readVector(pkt); - Vector pos (x, y); - - Engine::log(INFO, "client_player.on_position") << "obj=" << obj << ", pos=" << pos; + Engine::log(INFO, "client_player.on_position") << "obj=" << obj << ", position=" << position << ", velocity=" << velocity; - // XXX: transmit velocity/force - updatePhysics(pos, Vector(0, 0)); + // just update... + updatePhysics(position, velocity); } -NetworkClientRemotePlayer::NetworkClientRemotePlayer (NetworkClient &client, CL_NetObject_Client &obj, Vector initial_position) : - RemotePlayer(client.state, initial_position, true), client(client), obj(obj) { +NetworkClientRemotePlayer::NetworkClientRemotePlayer (NetworkClient &client, CL_NetObject_Client &obj, Vector position) : + RemotePlayer(client.state, position, true), client(client), obj(obj) { // receive messages slots.connect(obj.sig_received_message(NETMSG_PLAYER_POSITION), this, &NetworkClientRemotePlayer::on_position); @@ -133,15 +122,13 @@ } void NetworkClientRemotePlayer::on_position (CL_NetPacket &pkt) { - uint32_t x = pkt.input.read_uint32(); - uint32_t y = pkt.input.read_uint32(); + Vector position = readVector(pkt); + Vector velocity = readVector(pkt); + + Engine::log(INFO, "client_player.on_position") << "obj=" << obj << ", position=" << position << ", velocity=" << velocity; - Vector pos (x, y); - - Engine::log(INFO, "client_player.on_position") << "obj=" << obj << ", pos=" << pos; - - // XXX: transmit velocity/force - updatePhysics(pos, Vector(0, 0)); + // just update... + updatePhysics(position, velocity); } void NetworkClientRemotePlayer::on_quit (CL_NetPacket &pkt) { diff -r be91c125f741 -r 1415a2d45686 src/proto2/NetworkClient.hh --- a/src/proto2/NetworkClient.hh Tue Nov 18 22:07:36 2008 +0000 +++ b/src/proto2/NetworkClient.hh Tue Nov 18 22:50:29 2008 +0000 @@ -40,7 +40,7 @@ public: NetworkClientLocalPlayer (NetworkClient &client, CL_NetObject_Client &obj, Vector initial_position); - virtual void handleMove (PlayerInput_Move input); + virtual void applyForce (Vector force, uint16_t dt); private: void on_position (CL_NetPacket &pkt); diff -r be91c125f741 -r 1415a2d45686 src/proto2/NetworkServer.cc --- a/src/proto2/NetworkServer.cc Tue Nov 18 22:07:36 2008 +0000 +++ b/src/proto2/NetworkServer.cc Tue Nov 18 22:50:29 2008 +0000 @@ -48,7 +48,7 @@ } NetworkServerPlayer::NetworkServerPlayer (NetworkServer &server, CL_NetComputer &computer, uint16_t pid) : - RemotePlayer(server.state, Vector(100, 100), true), server(server), computer(computer), obj(&server.netobjs), pid(pid) { + RemotePlayer(server.state, Vector(PLAYER_INITIAL_X, PLAYER_INITIAL_Y), true), server(server), computer(computer), obj(&server.netobjs), pid(pid) { // log Engine::log(INFO, "server_player.connected") << "computer=" << computer << ", obj=" << obj; @@ -58,8 +58,7 @@ // the initial NETMSG_PLAYER_HELLO CL_NetPacket hello_pkt; - hello_pkt.output.write_uint32(position.x); - hello_pkt.output.write_uint32(position.y); + writeVector(hello_pkt, position); obj.send(computer, NETMSG_SERVER_HELLO, hello_pkt, true); @@ -71,8 +70,7 @@ // player is not in players list yet assert(player != this); - player_pkt.output.write_uint32(player->position.x); - player_pkt.output.write_uint32(player->position.y); + writeVector(player_pkt, player->position); player->obj.send(computer, NETMSG_PLAYER_INFO, player_pkt, true); } @@ -95,17 +93,13 @@ if (!(from == computer)) return; - // read packet - int32_t dx = pkt.input.read_int32(); - int32_t dy = pkt.input.read_int32(); + Vector impulse_force = readVector(pkt); + uint16_t impulse_ms = pkt.input.read_uint16(); - // movement delta - Vector delta(dx, dy); - - Engine::log(INFO, "server_player.on_move") << "obj=" << obj << ", old_pos=" << position << ", delta=" << delta; - - // apply movement - position += delta; + Engine::log(INFO, "server_player.on_move") << "obj=" << obj << ", old_pos=" << position << ", impulse=" << impulse_force << "@" << impulse_ms << "ms"; + + // apply force + applyForce(impulse_force, impulse_ms); // send position update send_position_update(); @@ -113,10 +107,10 @@ void NetworkServerPlayer::send_position_update (void) { CL_NetPacket pkt; - pkt.output.write_uint32(position.x); - pkt.output.write_uint32(position.y); + writeVector(pkt, position); + writeVector(pkt, velocity); - Engine::log(INFO, "server_player.send_position_update") << "obj=" << obj << " -> " << position; + Engine::log(INFO, "server_player.send_position_update") << "obj=" << obj << " -> " << position << "+" << velocity; obj.send(server.netsession.get_all(), NETMSG_PLAYER_POSITION, pkt, false); } diff -r be91c125f741 -r 1415a2d45686 src/proto2/Physics.hh --- a/src/proto2/Physics.hh Tue Nov 18 22:07:36 2008 +0000 +++ b/src/proto2/Physics.hh Tue Nov 18 22:50:29 2008 +0000 @@ -44,7 +44,7 @@ void updatePosition (void); protected: - void applyForce (Vector force, uint16_t dt); + virtual void applyForce (Vector force, uint16_t dt); void updatePhysics (Vector position, Vector velocity); public: diff -r be91c125f741 -r 1415a2d45686 src/proto2/Vector.hh --- a/src/proto2/Vector.hh Tue Nov 18 22:07:36 2008 +0000 +++ b/src/proto2/Vector.hh Tue Nov 18 22:50:29 2008 +0000 @@ -67,7 +67,7 @@ } template -std::ostream& operator<<(std::ostream &s, _Vector &v) { +std::ostream& operator<<(std::ostream &s, const _Vector &v) { return s<<"("<