1 | /* |
1 | /* |
2 | CrossFire, A Multiplayer game for X-windows |
2 | CrossFire, A Multiplayer game for X-windows |
3 | |
3 | |
|
|
4 | Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
4 | Copyright (C) 2002-2003 Mark Wedel & The Crossfire Development Team |
5 | Copyright (C) 2002-2003 Mark Wedel & The Crossfire Development Team |
5 | Copyright (C) 1992 Frank Tore Johansen |
6 | Copyright (C) 1992 Frank Tore Johansen |
6 | |
7 | |
7 | This program is free software; you can redistribute it and/or modify |
8 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by |
9 | it under the terms of the GNU General Public License as published by |
… | |
… | |
46 | #include <arpa/inet.h> |
47 | #include <arpa/inet.h> |
47 | |
48 | |
48 | #include <loader.h> |
49 | #include <loader.h> |
49 | |
50 | |
50 | #define MAX_QUEUE_DEPTH 500 //TODO |
51 | #define MAX_QUEUE_DEPTH 500 //TODO |
51 | #define MAX_QUEUE_BACKLOG 5. //TODO |
52 | #define MAX_QUEUE_BACKLOG 3. //TODO |
52 | |
53 | |
53 | void |
54 | void |
54 | client::reset_state () |
55 | client::reset_state () |
55 | { |
56 | { |
56 | if (!pl) |
57 | if (!pl) |
… | |
… | |
86 | client::handle_command () |
87 | client::handle_command () |
87 | { |
88 | { |
88 | bool skipping = false; |
89 | bool skipping = false; |
89 | |
90 | |
90 | while (!cmd_queue.empty () |
91 | while (!cmd_queue.empty () |
91 | && !(pl && pl->state == ST_PLAYING && pl->ob && pl->ob->speed_left < 0)) |
92 | && !(state == ST_PLAYING && pl->ob && pl->ob->speed_left <= 0)) |
92 | { |
93 | { |
93 | command &cmd = cmd_queue.front (); |
94 | command &cmd = cmd_queue.front (); |
94 | |
95 | |
95 | if (cmd.stamp + MAX_QUEUE_BACKLOG < now ()) |
96 | if (cmd.stamp + MAX_QUEUE_BACKLOG < now ()) |
96 | { |
97 | { |
… | |
… | |
116 | |
117 | |
117 | void |
118 | void |
118 | flush_sockets (void) |
119 | flush_sockets (void) |
119 | { |
120 | { |
120 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
121 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
121 | if ((*i)->status != Ns_Dead) |
|
|
122 | (*i)->flush (); |
122 | (*i)->flush (); |
123 | } |
123 | } |
124 | |
124 | |
125 | /** |
125 | /** |
126 | * This checks the sockets for input, does the right thing. |
126 | * This checks the sockets for input, does the right thing. |
127 | * |
127 | * |
… | |
… | |
135 | #ifdef CS_LOGSTATS |
135 | #ifdef CS_LOGSTATS |
136 | if ((time (NULL) - cst_lst.time_start) >= CS_LOGTIME) |
136 | if ((time (NULL) - cst_lst.time_start) >= CS_LOGTIME) |
137 | write_cs_stats (); |
137 | write_cs_stats (); |
138 | #endif |
138 | #endif |
139 | |
139 | |
140 | /* Go through the players. Let the loop set the next pl value, |
140 | //TODO: should not be done here, either |
141 | * since we may remove some |
141 | for (int i = 0; i < clients.size (); ++i) |
142 | */ |
|
|
143 | //TODO: must be handled cleanly elsewhere |
|
|
144 | for (player *pl = first_player; pl; ) |
|
|
145 | { |
142 | { |
146 | player *npl = pl->next; |
|
|
147 | |
|
|
148 | //TODO: must be handled cleanly elsewhere |
|
|
149 | if (!pl->socket || pl->socket->status == Ns_Dead) |
|
|
150 | { |
|
|
151 | save_player (pl->ob, 0); |
|
|
152 | |
|
|
153 | if (!QUERY_FLAG (pl->ob, FLAG_REMOVED)) |
|
|
154 | { |
|
|
155 | terminate_all_pets (pl->ob); |
|
|
156 | pl->ob->remove (); |
|
|
157 | } |
|
|
158 | |
|
|
159 | leave (pl, 1); |
|
|
160 | final_free_player (pl); |
|
|
161 | } |
|
|
162 | |
|
|
163 | pl = npl; |
|
|
164 | } |
|
|
165 | |
|
|
166 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ) |
|
|
167 | { |
|
|
168 | client *s = *i; |
143 | client *s = clients [i]; |
169 | |
|
|
170 | if (s->status == Ns_Dead) |
|
|
171 | { |
|
|
172 | clients.erase (i); |
|
|
173 | delete s; |
|
|
174 | } |
|
|
175 | else |
|
|
176 | ++i; |
|
|
177 | } |
|
|
178 | |
|
|
179 | /* We need to do some of the processing below regardless */ |
|
|
180 | |
|
|
181 | /* Check for any input on the sockets */ |
|
|
182 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
|
|
183 | { |
|
|
184 | client *s = *i; |
|
|
185 | player *pl = s->pl; |
144 | player *pl = s->pl; |
186 | |
145 | |
187 | //TODO: should not be done here, either |
146 | if (pl && pl->ns && !pl->ns->destroyed ()) |
188 | if (s->status != Ns_Dead && pl) |
|
|
189 | { |
147 | { |
|
|
148 | client *ns = pl->ns; |
|
|
149 | |
190 | /* Update the players stats once per tick. More efficient than |
150 | /* Update the players stats once per tick. More efficient than |
191 | * sending them whenever they change, and probably just as useful |
151 | * sending them whenever they change, and probably just as useful |
192 | */ |
152 | */ |
193 | esrv_update_stats (pl); |
153 | esrv_update_stats (pl); |
194 | |
154 | |
195 | if (pl->last_weight != -1 && pl->last_weight != WEIGHT (pl->ob)) |
155 | if (ns->last_weight != -1 && ns->last_weight != WEIGHT (pl->ob)) |
196 | { |
156 | { |
197 | esrv_update_item (UPD_WEIGHT, pl->ob, pl->ob); |
157 | esrv_update_item (UPD_WEIGHT, pl->ob, pl->ob); |
198 | if (pl->last_weight != WEIGHT (pl->ob)) |
158 | if (ns->last_weight != WEIGHT (pl->ob)) |
199 | LOG (llevError, "esrv_update_item(UPD_WEIGHT) did not set player weight: is %lu, should be %lu\n", |
159 | LOG (llevError, "esrv_update_item(UPD_WEIGHT) did not set player weight: is %lu, should be %lu\n", |
200 | (unsigned long) pl->last_weight, WEIGHT (pl->ob)); |
160 | (unsigned long) ns->last_weight, WEIGHT (pl->ob)); |
201 | } |
161 | } |
202 | |
162 | |
203 | draw_client_map (pl->ob); |
163 | draw_client_map (pl->ob); |
204 | |
164 | |
205 | if (s->update_look) |
165 | if (s->update_look) |
206 | esrv_draw_look (pl->ob); |
166 | esrv_draw_look (pl->ob); |
207 | } |
167 | } |
|
|
168 | |
|
|
169 | s->refcnt_chk (); |
208 | } |
170 | } |
209 | } |
171 | } |
210 | |
172 | |