author | terom |
Mon, 22 Dec 2008 02:37:43 +0000 | |
changeset 387 | 294ce7ae8140 |
parent 381 | 9b35bc329d23 |
child 391 | 59c2499fe7bb |
permissions | -rw-r--r-- |
187 | 1 |
|
186 | 2 |
#include "Server.hh" |
187 | 3 |
#include "Protocol.hh" |
4 |
#include "Config.hh" |
|
186 | 5 |
#include "../Engine.hh" |
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> |
387
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
9 |
#include <zlib.h> |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
10 |
|
381
9b35bc329d23
separate sockaddr stuff out of NetworkAddress... now called NetworkEndpoint
terom
parents:
355
diff
changeset
|
11 |
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
|
12 |
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
|
13 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
14 |
// connect slots |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
15 |
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
|
16 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
17 |
// and then we listen |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
18 |
netsession.listen(listen_addr); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
19 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
20 |
Engine::log(INFO, "server") << "running, listen_addr=" << listen_addr; |
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 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
23 |
void NetworkServer::on_node_connected (NetworkNode *node) { |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
24 |
// create the player object (it logs it) |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
25 |
NetworkServerPlayer *player = new NetworkServerPlayer(*this, node); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
26 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
27 |
// add to players |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
28 |
players.push_back(player); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
29 |
} |
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 |
void NetworkServer::handle_disconnect (NetworkServerPlayer *player) { |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
32 |
// remove from list |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
33 |
players.remove(player); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
34 |
} |
274
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
271
diff
changeset
|
35 |
|
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
271
diff
changeset
|
36 |
/* |
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
271
diff
changeset
|
37 |
* NetworkServerObject |
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
271
diff
changeset
|
38 |
*/ |
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
271
diff
changeset
|
39 |
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
|
40 |
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
|
41 |
{ |
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
271
diff
changeset
|
42 |
|
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
271
diff
changeset
|
43 |
} |
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
271
diff
changeset
|
44 |
|
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
271
diff
changeset
|
45 |
/* |
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
271
diff
changeset
|
46 |
* NetworkServerPlayer |
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
271
diff
changeset
|
47 |
*/ |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
48 |
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
|
49 |
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
|
50 |
{ |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
51 |
// log |
274
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
271
diff
changeset
|
52 |
Engine::log(INFO, "server_player.connected") << this << ": node=" << node; |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
53 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
54 |
// messages |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
55 |
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
|
56 |
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
|
57 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
58 |
// the initial NETMSG_PLAYER_HELLO |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
59 |
NetworkPacket hello_pkt; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
60 |
hello_pkt.write_vector(position); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
61 |
|
274
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
271
diff
changeset
|
62 |
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
|
63 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
64 |
// send other player objects |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
65 |
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
|
66 |
NetworkPacket player_pkt; |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
67 |
NetworkServerPlayer *player = *it; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
68 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
69 |
// player is not in players list yet |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
70 |
assert(player != this); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
71 |
|
355
0fafdf0029c0
misc. touching up of doc and NetworkServer comments...
terom
parents:
334
diff
changeset
|
72 |
// write packet |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
73 |
player_pkt.write_vector(player->position); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
74 |
|
274
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
271
diff
changeset
|
75 |
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
|
76 |
|
0fafdf0029c0
misc. touching up of doc and NetworkServer comments...
terom
parents:
334
diff
changeset
|
77 |
// XXX: send rope info... |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
78 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
79 |
|
355
0fafdf0029c0
misc. touching up of doc and NetworkServer comments...
terom
parents:
334
diff
changeset
|
80 |
// 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
|
81 |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
82 |
// 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
|
83 |
this->send_all_except(NETMSG_PLAYER_JOIN, hello_pkt, node, true); |
203
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
200
diff
changeset
|
84 |
|
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
200
diff
changeset
|
85 |
// send terrain data... |
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
200
diff
changeset
|
86 |
send_terrain_data(); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
87 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
88 |
|
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
187
diff
changeset
|
89 |
void NetworkServerPlayer::handleDig (Vector position, float radius) { |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
187
diff
changeset
|
90 |
NetworkPacket pkt; |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
187
diff
changeset
|
91 |
|
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
187
diff
changeset
|
92 |
pkt.write_vector(position); |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
187
diff
changeset
|
93 |
pkt.write_float32(radius); |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
187
diff
changeset
|
94 |
|
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
187
diff
changeset
|
95 |
Engine::log(INFO, "server_player.handle_dig") << "position=" << position << ", radius=" << radius; |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
187
diff
changeset
|
96 |
|
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
187
diff
changeset
|
97 |
// 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
|
98 |
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
|
99 |
|
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
187
diff
changeset
|
100 |
// 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
|
101 |
Player::handleDig(position, radius); |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
187
diff
changeset
|
102 |
} |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
187
diff
changeset
|
103 |
|
276
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
104 |
void NetworkServerPlayer::handleFireWeapon (Weapon *weapon, Vector position, Vector velocity) { |
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
105 |
Engine::log(INFO, "server_player.fire_weapon") << "weapon='" << weapon->getName() << "', position=" << position << ", velocity=" << velocity; |
223 | 106 |
|
107 |
// 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
|
108 |
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
|
109 |
|
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
110 |
// 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
|
111 |
weaponFired(weapon); |
239
550397d9d479
implement network weapon changes and fix weapon firing
terom
parents:
224
diff
changeset
|
112 |
} |
550397d9d479
implement network weapon changes and fix weapon firing
terom
parents:
224
diff
changeset
|
113 |
|
550397d9d479
implement network weapon changes and fix weapon firing
terom
parents:
224
diff
changeset
|
114 |
void NetworkServerPlayer::handleChangeWeapon (unsigned int weaponIndex) { |
550397d9d479
implement network weapon changes and fix weapon firing
terom
parents:
224
diff
changeset
|
115 |
NetworkPacket pkt; |
550397d9d479
implement network weapon changes and fix weapon firing
terom
parents:
224
diff
changeset
|
116 |
|
550397d9d479
implement network weapon changes and fix weapon firing
terom
parents:
224
diff
changeset
|
117 |
Engine::log(INFO, "server_player.change_weapon") << "weaponIndex=" << weaponIndex; |
550397d9d479
implement network weapon changes and fix weapon firing
terom
parents:
224
diff
changeset
|
118 |
|
550397d9d479
implement network weapon changes and fix weapon firing
terom
parents:
224
diff
changeset
|
119 |
// write packet |
550397d9d479
implement network weapon changes and fix weapon firing
terom
parents:
224
diff
changeset
|
120 |
pkt.write_uint8(weaponIndex); |
550397d9d479
implement network weapon changes and fix weapon firing
terom
parents:
224
diff
changeset
|
121 |
|
550397d9d479
implement network weapon changes and fix weapon firing
terom
parents:
224
diff
changeset
|
122 |
// 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
|
123 |
send_all(NETMSG_PLAYER_WEAPON_CHANGE, pkt, true); |
239
550397d9d479
implement network weapon changes and fix weapon firing
terom
parents:
224
diff
changeset
|
124 |
|
550397d9d479
implement network weapon changes and fix weapon firing
terom
parents:
224
diff
changeset
|
125 |
// pass through |
550397d9d479
implement network weapon changes and fix weapon firing
terom
parents:
224
diff
changeset
|
126 |
Player::handleChangeWeapon(weaponIndex); |
223 | 127 |
} |
241 | 128 |
|
129 |
void NetworkServerPlayer::handleRopeState (RopeState state) { |
|
130 |
NetworkPacket pkt; |
|
131 |
||
328 | 132 |
Engine::log(INFO, "server_player.rope_state") << "state=" << rope.getState() << ", position=" << rope.getPosition() << ", velocity=" << rope.getVelocity() << ", length=" << rope.getLength() << ", pivotPlayer=" << rope.getPivotPlayer(); |
241 | 133 |
|
134 |
switch (state) { |
|
135 |
case ROPE_FLYING: |
|
136 |
pkt.write_vector(rope.getPosition()); |
|
137 |
pkt.write_vector(rope.getVelocity()); |
|
138 |
pkt.write_float32(rope.getLength()); |
|
139 |
||
274
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
271
diff
changeset
|
140 |
send_all(NETMSG_PLAYER_ROPE_THROW, pkt, true); |
241 | 141 |
|
142 |
break; |
|
143 |
||
328 | 144 |
case ROPE_FIXED: { |
145 |
Player *player_base = rope.getPivotPlayer(); |
|
146 |
NetworkServerPlayer *player = NULL; |
|
147 |
||
148 |
if (player_base != NULL && (player = dynamic_cast<NetworkServerPlayer*>(player_base)) == NULL) |
|
149 |
throw Error("NetworkServerPlayer::handleRopeState: rope's pivotPlayer is not a NetworkServerPlayer"); |
|
150 |
||
241 | 151 |
pkt.write_vector(rope.getPosition()); |
152 |
pkt.write_float32(rope.getLength()); |
|
328 | 153 |
controller.write_object(pkt, player); // may be NULL |
241 | 154 |
|
274
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
271
diff
changeset
|
155 |
send_all(NETMSG_PLAYER_ROPE_FIXED, pkt, true); |
241 | 156 |
|
328 | 157 |
} break; |
241 | 158 |
|
159 |
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
|
160 |
send_all(NETMSG_PLAYER_ROPE_RELEASED, pkt, true); |
241 | 161 |
|
162 |
break; |
|
163 |
} |
|
164 |
} |
|
165 |
||
166 |
void NetworkServerPlayer::handleRopeLength (float length) { |
|
167 |
NetworkPacket pkt; |
|
168 |
||
169 |
pkt.write_float32(length); |
|
170 |
||
274
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
271
diff
changeset
|
171 |
send_all(NETMSG_PLAYER_ROPE_LENGTH, pkt, true); |
241 | 172 |
} |
223 | 173 |
|
302 | 174 |
void NetworkServerPlayer::spawn (Vector position) { |
175 |
NetworkPacket pkt; |
|
176 |
||
177 |
// write packet |
|
178 |
pkt.write_vector(position); |
|
179 |
||
180 |
Engine::log(INFO, "server_player.spawn") << this << ": position=" << position; |
|
181 |
||
182 |
// send |
|
183 |
send_all(NETMSG_PLAYER_SPAWN, pkt, true); |
|
184 |
||
185 |
// super |
|
186 |
Player::spawn(position); |
|
187 |
} |
|
188 |
||
189 |
void NetworkServerPlayer::die (bool start_timer) { |
|
190 |
NetworkPacket pkt; |
|
191 |
||
192 |
Engine::log(INFO, "server_player.die") << this; |
|
193 |
||
194 |
// send |
|
195 |
send_all(NETMSG_PLAYER_DIE, pkt, true); |
|
196 |
||
197 |
// super |
|
198 |
Player::die(start_timer); |
|
199 |
} |
|
200 |
||
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
201 |
void NetworkServerPlayer::on_disconnected (void) { |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
202 |
NetworkPacket pkt; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
203 |
|
274
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
271
diff
changeset
|
204 |
Engine::log(INFO, "server_player.disconnected") << this << ": node=" << node; |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
205 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
206 |
// remove from server |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
207 |
server.handle_disconnect(this); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
208 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
209 |
// 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
|
210 |
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
|
211 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
212 |
// free |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
213 |
// delete this; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
214 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
215 |
|
223 | 216 |
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
|
217 |
// sanity-check, other players shouldn't move |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
218 |
if (src != node) { |
223 | 219 |
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
|
220 |
return; |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
221 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
222 |
|
239
550397d9d479
implement network weapon changes and fix weapon firing
terom
parents:
224
diff
changeset
|
223 |
// read packet |
221 | 224 |
PlayerInput input = pkt.read_uint16(); |
334 | 225 |
TimeMS dt = pkt.read_uint32(); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
226 |
|
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
241
diff
changeset
|
227 |
// 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
|
228 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
229 |
// apply input |
334 | 230 |
handleInput(input, dt); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
231 |
|
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
232 |
// send position update |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
233 |
send_position_update(); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
234 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
235 |
|
203
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
200
diff
changeset
|
236 |
void NetworkServerPlayer::send_terrain_data (void) { |
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
200
diff
changeset
|
237 |
Terrain &terrain = server.state.world; |
282
e0e4dfc3e528
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents:
276
diff
changeset
|
238 |
|
387
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
239 |
// dimensions? |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
240 |
PixelCoordinate map = terrain.getDimensions(); |
203
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
200
diff
changeset
|
241 |
|
387
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
242 |
// translate to a byte array |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
243 |
size_t terrain_size = map.x * map.y; |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
244 |
uint8_t terrain_buf[map.x][map.y]; |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
245 |
|
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
246 |
// copy over from terrain vector |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
247 |
for (PixelDimension x = 0; x < map.x; x++) { |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
248 |
for (PixelDimension y = 0; y < map.y; y++) { |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
249 |
terrain_buf[x][y] = (uint8_t) terrain.terrain[x][y]; |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
250 |
} |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
251 |
} |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
252 |
|
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
253 |
// compress the terrain buffer |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
254 |
unsigned long deflate_size = compressBound(terrain_size); |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
255 |
uint8_t deflate_buf[deflate_size]; |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
256 |
|
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
257 |
// and compress |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
258 |
if (compress(deflate_buf, &deflate_size, (const uint8_t *) terrain_buf, terrain_size) != Z_OK) |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
259 |
throw Error("compress failed"); |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
260 |
|
203
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
200
diff
changeset
|
261 |
// allocate our packet... |
387
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
262 |
BigNetworkPacket pkt ( |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
263 |
NETWORK_SESSION_HEADER_SIZE // NetworkChannel header |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
264 |
+ 3 * sizeof(uint32_t) // our own header |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
265 |
+ deflate_size // compressed terrain buffer |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
266 |
); |
203
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
200
diff
changeset
|
267 |
|
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
200
diff
changeset
|
268 |
// write netsession header |
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
200
diff
changeset
|
269 |
node->write_packet_header(pkt, NETCHAN_TERRAIN_ARRAY); |
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
200
diff
changeset
|
270 |
|
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
200
diff
changeset
|
271 |
// write terrain dimensions |
387
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
272 |
pkt.write_uint32(map.x); |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
273 |
pkt.write_uint32(map.y); |
203
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
200
diff
changeset
|
274 |
|
387
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
275 |
// write compressed data |
294ce7ae8140
dirty implementation of compressed terrain array data
terom
parents:
381
diff
changeset
|
276 |
pkt.write(deflate_buf, deflate_size); |
203
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
200
diff
changeset
|
277 |
|
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
200
diff
changeset
|
278 |
// send |
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
200
diff
changeset
|
279 |
node->send_raw(pkt, true); |
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
200
diff
changeset
|
280 |
} |
3ec7ab40755f
send initial terrain data to clients using a new NETCHAN_TERRAIN_ARRAY channel using raw BigNetworkPacket
terom
parents:
200
diff
changeset
|
281 |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
282 |
void NetworkServerPlayer::send_position_update (void) { |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
283 |
NetworkPacket pkt; |
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
187
diff
changeset
|
284 |
|
264 | 285 |
int flags = |
286 |
(inAir ? NETWORK_PHYSICS_INAIR : 0) | |
|
287 |
(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
|
288 |
|
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
289 |
pkt.write_vector(position); |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
290 |
pkt.write_vector(velocity); |
200
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
187
diff
changeset
|
291 |
pkt.write_uint8(flags); |
2dbf40661580
better NetworkBuffer/Packet stuff + some additional Physics+Network stuff + random fixes
terom
parents:
187
diff
changeset
|
292 |
pkt.write_float32(aim); |
185
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
293 |
|
255
99431fdb0dc8
add PixelDimension/PixelCoordinate types, convert Terrain to use them, and convert/clean up drawing code
terom
parents:
241
diff
changeset
|
294 |
// 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
|
295 |
|
274
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
271
diff
changeset
|
296 |
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
|
297 |
} |
25becd2cb026
that's not a prototype anymore... at least it shouldn't be
terom
parents:
diff
changeset
|
298 |
|
223 | 299 |
/* |
300 |
* NetworkServerProjectile |
|
301 |
*/ |
|
274
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
271
diff
changeset
|
302 |
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
|
303 |
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
|
304 |
Projectile(player, position, velocity, weapon, true), NetworkServerObject(server) |
223 | 305 |
{ |
306 |
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
|
307 |
|
87434abc1ba1
ability to send NetworkObjectID's via packets, modify Network Projectiles to use this and fix recoil/reloading
terom
parents:
275
diff
changeset
|
308 |
server.controller.write_object(pkt, player); |
223 | 309 |
pkt.write_vector(position); |
310 |
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
|
311 |
pkt.write_uint8(weapon->getID()); |
223 | 312 |
|
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 |
send_all(NETMSG_PROJECTILE_PLAYER_FIRED, pkt, true); |
223 | 314 |
} |
315 |
||
224
e6faefba2ec1
fixed logger, and network projectiles should work reasonably well now
terom
parents:
223
diff
changeset
|
316 |
void NetworkServerProjectile::onDestroy (Vector position, bool removeGround) { |
e6faefba2ec1
fixed logger, and network projectiles should work reasonably well now
terom
parents:
223
diff
changeset
|
317 |
NetworkPacket pkt; |
e6faefba2ec1
fixed logger, and network projectiles should work reasonably well now
terom
parents:
223
diff
changeset
|
318 |
|
296
4d3ebaa29430
add separate Types.hh, and fix projectile-worm collisions on network
terom
parents:
290
diff
changeset
|
319 |
Engine::log(INFO, "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
|
320 |
|
224
e6faefba2ec1
fixed logger, and network projectiles should work reasonably well now
terom
parents:
223
diff
changeset
|
321 |
pkt.write_vector(position); |
e6faefba2ec1
fixed logger, and network projectiles should work reasonably well now
terom
parents:
223
diff
changeset
|
322 |
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
|
323 |
|
274
c35307e8645c
add comments, move addPlayer/removePlayer to Player.cc, and refactor Network code to inherit from NetworkObject
terom
parents:
271
diff
changeset
|
324 |
send_all(NETMSG_PROJECTILE_DESTROY, pkt, true); |
224
e6faefba2ec1
fixed logger, and network projectiles should work reasonably well now
terom
parents:
223
diff
changeset
|
325 |
|
e6faefba2ec1
fixed logger, and network projectiles should work reasonably well now
terom
parents:
223
diff
changeset
|
326 |
// XXX: leak obj, not yet implemented: obj.destory(); |
e6faefba2ec1
fixed logger, and network projectiles should work reasonably well now
terom
parents:
223
diff
changeset
|
327 |
|
e6faefba2ec1
fixed logger, and network projectiles should work reasonably well now
terom
parents:
223
diff
changeset
|
328 |
// pass on to super |
e6faefba2ec1
fixed logger, and network projectiles should work reasonably well now
terom
parents:
223
diff
changeset
|
329 |
Projectile::onDestroy(position, removeGround); |
e6faefba2ec1
fixed logger, and network projectiles should work reasonably well now
terom
parents:
223
diff
changeset
|
330 |
} |
e6faefba2ec1
fixed logger, and network projectiles should work reasonably well now
terom
parents:
223
diff
changeset
|
331 |
|
330 | 332 |
void NetworkServerProjectile::onHitPlayer (Player *player_ptr) { |
296
4d3ebaa29430
add separate Types.hh, and fix projectile-worm collisions on network
terom
parents:
290
diff
changeset
|
333 |
NetworkPacket pkt; |
4d3ebaa29430
add separate Types.hh, and fix projectile-worm collisions on network
terom
parents:
290
diff
changeset
|
334 |
NetworkServerPlayer *player = dynamic_cast<NetworkServerPlayer*>(player_ptr); |
4d3ebaa29430
add separate Types.hh, and fix projectile-worm collisions on network
terom
parents:
290
diff
changeset
|
335 |
|
4d3ebaa29430
add separate Types.hh, and fix projectile-worm collisions on network
terom
parents:
290
diff
changeset
|
336 |
if (player == NULL) |
4d3ebaa29430
add separate Types.hh, and fix projectile-worm collisions on network
terom
parents:
290
diff
changeset
|
337 |
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
|
338 |
|
330 | 339 |
Engine::log(INFO, "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
|
340 |
|
4d3ebaa29430
add separate Types.hh, and fix projectile-worm collisions on network
terom
parents:
290
diff
changeset
|
341 |
// write packet |
4d3ebaa29430
add separate Types.hh, and fix projectile-worm collisions on network
terom
parents:
290
diff
changeset
|
342 |
controller.write_object(pkt, player); |
4d3ebaa29430
add separate Types.hh, and fix projectile-worm collisions on network
terom
parents:
290
diff
changeset
|
343 |
|
4d3ebaa29430
add separate Types.hh, and fix projectile-worm collisions on network
terom
parents:
290
diff
changeset
|
344 |
// send |
4d3ebaa29430
add separate Types.hh, and fix projectile-worm collisions on network
terom
parents:
290
diff
changeset
|
345 |
send_all(NETMSG_PROJECTILE_HIT_PLAYER, pkt, true); |
304
d0f60a97a85e
fix NetworkServerProjectile::onHitPlayer to call super
terom
parents:
302
diff
changeset
|
346 |
|
d0f60a97a85e
fix NetworkServerProjectile::onHitPlayer to call super
terom
parents:
302
diff
changeset
|
347 |
// super |
308
60f4b55d5713
Calculates kills and deaths. You get frags by killing yourself \o/
saiam
parents:
304
diff
changeset
|
348 |
Projectile::onHitPlayer(player_ptr); |
296
4d3ebaa29430
add separate Types.hh, and fix projectile-worm collisions on network
terom
parents:
290
diff
changeset
|
349 |
} |
4d3ebaa29430
add separate Types.hh, and fix projectile-worm collisions on network
terom
parents:
290
diff
changeset
|
350 |