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