src/Network/Server.cc
author Tero Marttila <terom@fixme.fi>
Thu, 22 Jan 2009 02:56:50 +0200
branchnew_graphics
changeset 420 278020dcd9b7
parent 417 c503e0c6a740
child 428 712b943195a6
permissions -rw-r--r--
clean up Server/Client log output a bit
187
f41f894213ca restructure network code a bit
terom
parents: 186
diff changeset
     1
186
0738f2949a2b move src/Network% to src/Network/%
terom
parents: 185
diff changeset
     2
#include "Server.hh"
187
f41f894213ca restructure network code a bit
terom
parents: 186
diff changeset
     3
#include "Protocol.hh"
417
c503e0c6a740 support for building without Network/Graphics, although the disable-graphics case is kind of hackish still
Tero Marttila <terom@fixme.fi>
parents: 408
diff changeset
     4
#include "../Config.hh"
186
0738f2949a2b move src/Network% to src/Network/%
terom
parents: 185
diff changeset
     5
#include "../Engine.hh"
0738f2949a2b move src/Network% to src/Network/%
terom
parents: 185
diff changeset
     6
#include "../Logger.hh"
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     7
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     8
#include <cassert>
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
     9
381
9b35bc329d23 separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents: 355
diff changeset
    10
NetworkServer::NetworkServer (GameState &state, const NetworkEndpoint &listen_addr) : 
276
87434abc1ba1 ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents: 275
diff changeset
    11
    state(state), netsession(NETWORK_MAGIC_ID), controller(netsession, NETCHAN_CORE) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    12
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    13
    // connect slots
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    14
    slots.connect(netsession.sig_node_connected(), this, &NetworkServer::on_node_connected);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    15
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    16
    // and then we listen
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    17
    netsession.listen(listen_addr);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    18
420
278020dcd9b7 clean up Server/Client log output a bit
Tero Marttila <terom@fixme.fi>
parents: 417
diff changeset
    19
    Engine::log(INFO, "net.server") << "Listening on interface: " << listen_addr;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    20
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    21
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    22
void NetworkServer::on_node_connected (NetworkNode *node) {
420
278020dcd9b7 clean up Server/Client log output a bit
Tero Marttila <terom@fixme.fi>
parents: 417
diff changeset
    23
    Engine::log(INFO, "net.server") << "Client connected, sending terrain data: " << node->getRemoteAddress();
278020dcd9b7 clean up Server/Client log output a bit
Tero Marttila <terom@fixme.fi>
parents: 417
diff changeset
    24
408
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    25
    // send the terrain data
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    26
    send_terrain_data(node);
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    27
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    28
    // create the player object (it logs it)
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    29
    NetworkServerPlayer *player = new NetworkServerPlayer(*this, node);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    30
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    31
    // add to players
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    32
    players.push_back(player);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    33
}
408
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    34
 
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    35
void NetworkServer::send_terrain_data (NetworkNode *node) {
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    36
    Terrain &terrain = state.world.terrain;
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    37
    
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    38
    // dimensions?
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    39
    PixelCoordinate map = terrain.getDimensions();
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    40
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    41
    // translate to a byte array
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    42
    size_t terrain_size = map.x * map.y;
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    43
    
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    44
    // get terrain buffer
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    45
    const uint8_t *terrain_buf = terrain.getTerrainBuffer();
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    46
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    47
    // allocate our packet...
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    48
    BigNetworkPacket pkt (
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    49
        // NetworkChannel header
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    50
        NETWORK_SESSION_HEADER_SIZE     
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    51
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    52
        // our own header
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    53
        + 2 * sizeof(uint32_t)          
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    54
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    55
        // compressed terrain buffer
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    56
        + NetworkPacketOutput::write_compressed_size(terrain_size)
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    57
    );
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    58
    
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    59
    // write netsession header
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    60
    node->write_packet_header(pkt, NETCHAN_TERRAIN_ARRAY);
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    61
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    62
    // write terrain dimensions
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    63
    pkt.write_uint32(map.x);
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    64
    pkt.write_uint32(map.y);
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    65
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    66
    // write compressed terrain data
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    67
    pkt.write_compressed(terrain_buf, terrain_size);
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    68
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    69
    // send
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    70
    node->send_raw(pkt, true);
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    71
}
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    72
e6cfc44266af reorganize Terrain/PhysicsWorld/GameState/Engine to use NetworkClientConnect, and hence handle the connection process asynchronously, and finally properly implement receiving the terrain data from the server
Tero Marttila <terom@fixme.fi>
parents: 406
diff changeset
    73
       
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    74
void NetworkServer::handle_disconnect (NetworkServerPlayer *player) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    75
    // remove from list
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    76
    players.remove(player);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    77
}
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
    78
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
    79
/*
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
    80
 * NetworkServerObject
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
    81
 */
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
    82
NetworkServerObject::NetworkServerObject (NetworkServer &server) :
276
87434abc1ba1 ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents: 275
diff changeset
    83
    NetworkObject_Server(server.controller), server(server)
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
    84
{
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
    85
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
    86
}
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
    87
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
    88
/*
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
    89
 * NetworkServerPlayer
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
    90
 */
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    91
NetworkServerPlayer::NetworkServerPlayer (NetworkServer &server, NetworkNode *node) : 
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
    92
    Player(server.state, Vector(PLAYER_INITIAL_X, PLAYER_INITIAL_Y), true), NetworkServerObject(server), node(node) 
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
    93
{
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    94
    // messages
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    95
    slots.connect(node->sig_disconnected(), this, &NetworkServerPlayer::on_disconnected);
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
    96
    slots.connect(this->sig_message(NETMSG_CLIENT_INPUT), this, &NetworkServerPlayer::on_input);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    97
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    98
    // the initial NETMSG_PLAYER_HELLO
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
    99
    NetworkPacket hello_pkt;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   100
    hello_pkt.write_vector(position);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   101
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
   102
    this->send_to(node, NETMSG_SERVER_HELLO, hello_pkt, true);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   103
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   104
    // send other player objects
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   105
    for (std::list<NetworkServerPlayer*>::iterator it = server.players.begin(); it != server.players.end(); it++) {
355
0fafdf0029c0 misc. touching up of doc and NetworkServer comments...
terom
parents: 334
diff changeset
   106
        NetworkPacket player_pkt;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   107
        NetworkServerPlayer *player = *it;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   108
        
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   109
        // player is not in players list yet
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   110
        assert(player != this);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   111
        
355
0fafdf0029c0 misc. touching up of doc and NetworkServer comments...
terom
parents: 334
diff changeset
   112
        // write packet
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   113
        player_pkt.write_vector(player->position);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   114
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
   115
        player->send_to(node, NETMSG_PLAYER_INFO, player_pkt, true);
355
0fafdf0029c0 misc. touching up of doc and NetworkServer comments...
terom
parents: 334
diff changeset
   116
0fafdf0029c0 misc. touching up of doc and NetworkServer comments...
terom
parents: 334
diff changeset
   117
        // XXX: send rope info...
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   118
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   119
355
0fafdf0029c0 misc. touching up of doc and NetworkServer comments...
terom
parents: 334
diff changeset
   120
    // XXX: send projectiles? Or let the client handle the events that the unknown projectiles generate?
0fafdf0029c0 misc. touching up of doc and NetworkServer comments...
terom
parents: 334
diff changeset
   121
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   122
    // broadcast NETMSG_PLAYER_JOIN to all clients except current
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
   123
    this->send_all_except(NETMSG_PLAYER_JOIN, hello_pkt, node, true);
420
278020dcd9b7 clean up Server/Client log output a bit
Tero Marttila <terom@fixme.fi>
parents: 417
diff changeset
   124
278020dcd9b7 clean up Server/Client log output a bit
Tero Marttila <terom@fixme.fi>
parents: 417
diff changeset
   125
    Engine::log(INFO, "net.server") << "Player joined: " << this << " from " << node->getRemoteAddress();
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   126
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   127
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 187
diff changeset
   128
void NetworkServerPlayer::handleDig (Vector position, float radius) {
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 187
diff changeset
   129
    NetworkPacket pkt;
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 187
diff changeset
   130
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 187
diff changeset
   131
    pkt.write_vector(position);
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 187
diff changeset
   132
    pkt.write_float32(radius);
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 187
diff changeset
   133
420
278020dcd9b7 clean up Server/Client log output a bit
Tero Marttila <terom@fixme.fi>
parents: 417
diff changeset
   134
    Engine::log(DEBUG, "server_player.handle_dig") << "position=" << position << ", radius=" << radius;
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 187
diff changeset
   135
    
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 187
diff changeset
   136
    // tell everyone... make this reliable... 
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
   137
    this->send_all(NETMSG_PLAYER_DIG, pkt, true);
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 187
diff changeset
   138
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 187
diff changeset
   139
    // and carry out the actual dig on the server as well
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 187
diff changeset
   140
    Player::handleDig(position, radius);
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 187
diff changeset
   141
}
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 187
diff changeset
   142
276
87434abc1ba1 ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents: 275
diff changeset
   143
void NetworkServerPlayer::handleFireWeapon (Weapon *weapon, Vector position, Vector velocity) {
420
278020dcd9b7 clean up Server/Client log output a bit
Tero Marttila <terom@fixme.fi>
parents: 417
diff changeset
   144
    Engine::log(DEBUG, "server_player.fire_weapon") << "weapon='" << weapon->getName() << "', position=" << position << ", velocity=" << velocity;
223
2fcaf54ed37b basic network-projectiles
terom
parents: 221
diff changeset
   145
2fcaf54ed37b basic network-projectiles
terom
parents: 221
diff changeset
   146
    // create new NetworkServerProjectile object
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
   147
    new NetworkServerProjectile(server, this, position, velocity, weapon);
276
87434abc1ba1 ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents: 275
diff changeset
   148
87434abc1ba1 ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents: 275
diff changeset
   149
    // as handleFireWeapon does
87434abc1ba1 ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents: 275
diff changeset
   150
    weaponFired(weapon);
239
550397d9d479 implement network weapon changes and fix weapon firing
terom
parents: 224
diff changeset
   151
}
550397d9d479 implement network weapon changes and fix weapon firing
terom
parents: 224
diff changeset
   152
550397d9d479 implement network weapon changes and fix weapon firing
terom
parents: 224
diff changeset
   153
void NetworkServerPlayer::handleChangeWeapon (unsigned int weaponIndex) {
550397d9d479 implement network weapon changes and fix weapon firing
terom
parents: 224
diff changeset
   154
    NetworkPacket pkt;
550397d9d479 implement network weapon changes and fix weapon firing
terom
parents: 224
diff changeset
   155
420
278020dcd9b7 clean up Server/Client log output a bit
Tero Marttila <terom@fixme.fi>
parents: 417
diff changeset
   156
    Engine::log(DEBUG, "server_player.change_weapon") << "weaponIndex=" << weaponIndex;
239
550397d9d479 implement network weapon changes and fix weapon firing
terom
parents: 224
diff changeset
   157
    
550397d9d479 implement network weapon changes and fix weapon firing
terom
parents: 224
diff changeset
   158
    // write packet
550397d9d479 implement network weapon changes and fix weapon firing
terom
parents: 224
diff changeset
   159
    pkt.write_uint8(weaponIndex);
550397d9d479 implement network weapon changes and fix weapon firing
terom
parents: 224
diff changeset
   160
    
550397d9d479 implement network weapon changes and fix weapon firing
terom
parents: 224
diff changeset
   161
    // XXX: only tell the client itself?
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
   162
    send_all(NETMSG_PLAYER_WEAPON_CHANGE, pkt, true);
239
550397d9d479 implement network weapon changes and fix weapon firing
terom
parents: 224
diff changeset
   163
    
550397d9d479 implement network weapon changes and fix weapon firing
terom
parents: 224
diff changeset
   164
    // pass through
550397d9d479 implement network weapon changes and fix weapon firing
terom
parents: 224
diff changeset
   165
    Player::handleChangeWeapon(weaponIndex);
223
2fcaf54ed37b basic network-projectiles
terom
parents: 221
diff changeset
   166
}
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   167
        
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   168
void NetworkServerPlayer::handleRopeState (RopeState state) {
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   169
    NetworkPacket pkt; 
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   170
420
278020dcd9b7 clean up Server/Client log output a bit
Tero Marttila <terom@fixme.fi>
parents: 417
diff changeset
   171
    Engine::log(DEBUG, "server_player.rope_state") << "state=" << rope.getState() << ", position=" << rope.getPosition() << ", velocity=" << rope.getVelocity() << ", length=" << rope.getLength() << ", pivotPlayer=" << rope.getPivotPlayer();
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   172
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   173
    switch (state) {
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   174
    case ROPE_FLYING:
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   175
        pkt.write_vector(rope.getPosition());
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   176
        pkt.write_vector(rope.getVelocity());
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   177
        pkt.write_float32(rope.getLength());
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   178
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
   179
        send_all(NETMSG_PLAYER_ROPE_THROW, pkt, true);
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   180
        
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   181
        break;
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   182
328
51d644c8d5a2 fix player-pivoted rope
terom
parents: 308
diff changeset
   183
    case ROPE_FIXED: {
51d644c8d5a2 fix player-pivoted rope
terom
parents: 308
diff changeset
   184
        Player *player_base = rope.getPivotPlayer();
51d644c8d5a2 fix player-pivoted rope
terom
parents: 308
diff changeset
   185
        NetworkServerPlayer *player = NULL;
51d644c8d5a2 fix player-pivoted rope
terom
parents: 308
diff changeset
   186
51d644c8d5a2 fix player-pivoted rope
terom
parents: 308
diff changeset
   187
        if (player_base != NULL && (player = dynamic_cast<NetworkServerPlayer*>(player_base)) == NULL)
51d644c8d5a2 fix player-pivoted rope
terom
parents: 308
diff changeset
   188
            throw Error("NetworkServerPlayer::handleRopeState: rope's pivotPlayer is not a NetworkServerPlayer");
51d644c8d5a2 fix player-pivoted rope
terom
parents: 308
diff changeset
   189
        
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   190
        pkt.write_vector(rope.getPosition());
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   191
        pkt.write_float32(rope.getLength());
328
51d644c8d5a2 fix player-pivoted rope
terom
parents: 308
diff changeset
   192
        controller.write_object(pkt, player);    // may be NULL
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   193
        
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
   194
        send_all(NETMSG_PLAYER_ROPE_FIXED, pkt, true);
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   195
328
51d644c8d5a2 fix player-pivoted rope
terom
parents: 308
diff changeset
   196
    } break;
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   197
    
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   198
    case ROPE_FOLDED:
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
   199
        send_all(NETMSG_PLAYER_ROPE_RELEASED, pkt, true);
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   200
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   201
        break;
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   202
    }
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   203
}
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   204
        
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   205
void NetworkServerPlayer::handleRopeLength (float length) {
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   206
    NetworkPacket pkt;
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   207
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   208
    pkt.write_float32(length);
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   209
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
   210
    send_all(NETMSG_PLAYER_ROPE_LENGTH, pkt, true);
241
e95b1602d836 implement the ROT (Rope Over TCP) protocol
terom
parents: 239
diff changeset
   211
}
223
2fcaf54ed37b basic network-projectiles
terom
parents: 221
diff changeset
   212
302
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   213
void NetworkServerPlayer::spawn (Vector position) {
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   214
    NetworkPacket pkt;
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   215
    
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   216
    // write packet
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   217
    pkt.write_vector(position);
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   218
420
278020dcd9b7 clean up Server/Client log output a bit
Tero Marttila <terom@fixme.fi>
parents: 417
diff changeset
   219
    Engine::log(DEBUG, "server_player.spawn") << this << ": position=" << position;
302
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   220
    
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   221
    // send
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   222
    send_all(NETMSG_PLAYER_SPAWN, pkt, true);
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   223
    
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   224
    // super
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   225
    Player::spawn(position);
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   226
}
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   227
        
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   228
void NetworkServerPlayer::die (bool start_timer) {
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   229
    NetworkPacket pkt;
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   230
420
278020dcd9b7 clean up Server/Client log output a bit
Tero Marttila <terom@fixme.fi>
parents: 417
diff changeset
   231
    Engine::log(DEBUG, "server_player.die") << this;
302
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   232
    
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   233
    // send
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   234
    send_all(NETMSG_PLAYER_DIE, pkt, true);
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   235
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   236
    // super
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   237
    Player::die(start_timer);
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   238
}
e734d8e9bbb5 make spawn/die work over the network
terom
parents: 296
diff changeset
   239
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   240
void NetworkServerPlayer::on_disconnected (void) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   241
    NetworkPacket pkt;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   242
    
420
278020dcd9b7 clean up Server/Client log output a bit
Tero Marttila <terom@fixme.fi>
parents: 417
diff changeset
   243
    Engine::log(INFO, "net.server") << "Player disconnected: " << this;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   244
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   245
    // remove from server
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   246
    server.handle_disconnect(this);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   247
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   248
    // tell other clients
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
   249
    send_all(NETMSG_PLAYER_QUIT, pkt, true);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   250
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   251
    // free
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   252
//    delete this;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   253
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   254
223
2fcaf54ed37b basic network-projectiles
terom
parents: 221
diff changeset
   255
void NetworkServerPlayer::on_input (NetworkNode *src, NetworkPacketInput &pkt) {
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   256
    // sanity-check, other players shouldn't move
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   257
    if (src != node) {
223
2fcaf54ed37b basic network-projectiles
terom
parents: 221
diff changeset
   258
        Engine::log(WARN, "server_player.on_input") << "packet from wrong src=" << src << ", node=" << node;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   259
        return;
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   260
    }
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   261
    
239
550397d9d479 implement network weapon changes and fix weapon firing
terom
parents: 224
diff changeset
   262
    // read packet 
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents: 209
diff changeset
   263
    PlayerInput input = pkt.read_uint16();
334
0cf3f2be51eb transmit handleInput dt
terom
parents: 330
diff changeset
   264
    TimeMS dt = pkt.read_uint32();
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   265
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 241
diff changeset
   266
//    Engine::log(INFO, "server_player.on_input") << "player=" << obj << ", old_pos=" << position << ", input=" << input;
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   267
    
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   268
    // apply input
334
0cf3f2be51eb transmit handleInput dt
terom
parents: 330
diff changeset
   269
    handleInput(input, dt);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   270
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   271
    // send position update
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   272
    send_position_update();
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   273
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   274
        
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   275
void NetworkServerPlayer::send_position_update (void) {
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   276
    NetworkPacket pkt;
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 187
diff changeset
   277
    
264
215de3d4de60 change facingRight from a bool to an
terom
parents: 263
diff changeset
   278
    int flags = 
215de3d4de60 change facingRight from a bool to an
terom
parents: 263
diff changeset
   279
        (inAir ? NETWORK_PHYSICS_INAIR : 0) | 
215de3d4de60 change facingRight from a bool to an
terom
parents: 263
diff changeset
   280
        (facing == FACING_RIGHT ? NETWORK_PHYSICS_FACE_RIGHT : 0);
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 187
diff changeset
   281
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   282
    pkt.write_vector(position);
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   283
    pkt.write_vector(velocity);
200
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 187
diff changeset
   284
    pkt.write_uint8(flags);
2dbf40661580 better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents: 187
diff changeset
   285
    pkt.write_float32(aim);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   286
255
99431fdb0dc8 add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents: 241
diff changeset
   287
//    Engine::log(INFO, "server_player.send_position_update") << "obj=" << obj << " -> " << position << "+" << velocity << " [" << flags << "]";
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   288
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
   289
    send_all(NETMSG_PLAYER_POSITION, pkt, false);
185
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   290
}
25becd2cb026 that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff changeset
   291
223
2fcaf54ed37b basic network-projectiles
terom
parents: 221
diff changeset
   292
/* 
2fcaf54ed37b basic network-projectiles
terom
parents: 221
diff changeset
   293
 * NetworkServerProjectile
2fcaf54ed37b basic network-projectiles
terom
parents: 221
diff changeset
   294
 */
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
   295
NetworkServerProjectile::NetworkServerProjectile (NetworkServer &server, NetworkServerPlayer *player, Vector position,
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
   296
        Vector velocity, Weapon *weapon) :
276
87434abc1ba1 ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents: 275
diff changeset
   297
    Projectile(player, position, velocity, weapon, true), NetworkServerObject(server)
223
2fcaf54ed37b basic network-projectiles
terom
parents: 221
diff changeset
   298
{
2fcaf54ed37b basic network-projectiles
terom
parents: 221
diff changeset
   299
    NetworkPacket pkt;
276
87434abc1ba1 ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents: 275
diff changeset
   300
    
87434abc1ba1 ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents: 275
diff changeset
   301
    server.controller.write_object(pkt, player);
223
2fcaf54ed37b basic network-projectiles
terom
parents: 221
diff changeset
   302
    pkt.write_vector(position);
2fcaf54ed37b basic network-projectiles
terom
parents: 221
diff changeset
   303
    pkt.write_vector(velocity);
276
87434abc1ba1 ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents: 275
diff changeset
   304
    pkt.write_uint8(weapon->getID());
223
2fcaf54ed37b basic network-projectiles
terom
parents: 221
diff changeset
   305
276
87434abc1ba1 ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents: 275
diff changeset
   306
    send_all(NETMSG_PROJECTILE_PLAYER_FIRED, pkt, true);
223
2fcaf54ed37b basic network-projectiles
terom
parents: 221
diff changeset
   307
}
2fcaf54ed37b basic network-projectiles
terom
parents: 221
diff changeset
   308
224
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 223
diff changeset
   309
void NetworkServerProjectile::onDestroy (Vector position, bool removeGround) {
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 223
diff changeset
   310
    NetworkPacket pkt;
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 223
diff changeset
   311
420
278020dcd9b7 clean up Server/Client log output a bit
Tero Marttila <terom@fixme.fi>
parents: 417
diff changeset
   312
    Engine::log(DEBUG, "server_projectile.destroy") << this << "position=" << position << ", removeGround=" << removeGround;
276
87434abc1ba1 ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents: 275
diff changeset
   313
    
224
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 223
diff changeset
   314
    pkt.write_vector(position);
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 223
diff changeset
   315
    pkt.write_uint8(removeGround ? NETWORK_PROJECTILE_REMOVE_GROUND : 0);
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 223
diff changeset
   316
274
c35307e8645c add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents: 271
diff changeset
   317
    send_all(NETMSG_PROJECTILE_DESTROY, pkt, true);
224
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 223
diff changeset
   318
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 223
diff changeset
   319
    // XXX: leak obj, not yet implemented:  obj.destory();
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 223
diff changeset
   320
    
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 223
diff changeset
   321
    // pass on to super
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 223
diff changeset
   322
    Projectile::onDestroy(position, removeGround);
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 223
diff changeset
   323
}
e6faefba2ec1 fixed logger, and network projectiles should work reasonably well now
terom
parents: 223
diff changeset
   324
330
dcc47278e5ab fix Network Projectile::onHitPlayer
terom
parents: 328
diff changeset
   325
void NetworkServerProjectile::onHitPlayer (Player *player_ptr) {
296
4d3ebaa29430 add separate Types.hh, and fix projectile-worm collisions on network
terom
parents: 290
diff changeset
   326
    NetworkPacket pkt;
4d3ebaa29430 add separate Types.hh, and fix projectile-worm collisions on network
terom
parents: 290
diff changeset
   327
    NetworkServerPlayer *player = dynamic_cast<NetworkServerPlayer*>(player_ptr);
4d3ebaa29430 add separate Types.hh, and fix projectile-worm collisions on network
terom
parents: 290
diff changeset
   328
4d3ebaa29430 add separate Types.hh, and fix projectile-worm collisions on network
terom
parents: 290
diff changeset
   329
    if (player == NULL) 
4d3ebaa29430 add separate Types.hh, and fix projectile-worm collisions on network
terom
parents: 290
diff changeset
   330
        throw Error("NetworkServerProjectile::onHitPlayer called with non-NetworkServerPlayer player");
4d3ebaa29430 add separate Types.hh, and fix projectile-worm collisions on network
terom
parents: 290
diff changeset
   331
    
420
278020dcd9b7 clean up Server/Client log output a bit
Tero Marttila <terom@fixme.fi>
parents: 417
diff changeset
   332
    Engine::log(DEBUG, "server_projectile.hit_player") << this << ": player=" << player;
296
4d3ebaa29430 add separate Types.hh, and fix projectile-worm collisions on network
terom
parents: 290
diff changeset
   333
    
4d3ebaa29430 add separate Types.hh, and fix projectile-worm collisions on network
terom
parents: 290
diff changeset
   334
    // write packet
4d3ebaa29430 add separate Types.hh, and fix projectile-worm collisions on network
terom
parents: 290
diff changeset
   335
    controller.write_object(pkt, player);
4d3ebaa29430 add separate Types.hh, and fix projectile-worm collisions on network
terom
parents: 290
diff changeset
   336
4d3ebaa29430 add separate Types.hh, and fix projectile-worm collisions on network
terom
parents: 290
diff changeset
   337
    // send
4d3ebaa29430 add separate Types.hh, and fix projectile-worm collisions on network
terom
parents: 290
diff changeset
   338
    send_all(NETMSG_PROJECTILE_HIT_PLAYER, pkt, true);
304
d0f60a97a85e fix NetworkServerProjectile::onHitPlayer to call super
terom
parents: 302
diff changeset
   339
d0f60a97a85e fix NetworkServerProjectile::onHitPlayer to call super
terom
parents: 302
diff changeset
   340
    // super
308
60f4b55d5713 Calculates kills and deaths. You get frags by killing yourself \o/
saiam
parents: 304
diff changeset
   341
    Projectile::onHitPlayer(player_ptr);
296
4d3ebaa29430 add separate Types.hh, and fix projectile-worm collisions on network
terom
parents: 290
diff changeset
   342
}
4d3ebaa29430 add separate Types.hh, and fix projectile-worm collisions on network
terom
parents: 290
diff changeset
   343