1 | /* |
---|
2 | * Abuse - dark 2D side-scrolling platform game |
---|
3 | * Copyright (c) 1995 Crack dot Com |
---|
4 | * Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | * |
---|
6 | * This software was released into the Public Domain. As with most public |
---|
7 | * domain software, no warranty is made or implied by Crack dot Com, by |
---|
8 | * Jonathan Clark, or by Sam Hocevar. |
---|
9 | */ |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include "server2.h" |
---|
16 | #include "nfserver.h" |
---|
17 | #include "nfclient.h" |
---|
18 | #include "dprint.h" |
---|
19 | #include "view.h" |
---|
20 | #include "jrand.h" |
---|
21 | #include "objects.h" |
---|
22 | #include "level.h" |
---|
23 | #include "dev.h" |
---|
24 | |
---|
25 | int start_running=0; |
---|
26 | |
---|
27 | class client_descriptor |
---|
28 | { |
---|
29 | public : |
---|
30 | client_descriptor(int client_number); |
---|
31 | view *player; // has this player been inducted yet? |
---|
32 | client_descriptor *next; |
---|
33 | long requested_join,cx1,cy1,cx2,cy2; |
---|
34 | int cnum; |
---|
35 | } ; |
---|
36 | |
---|
37 | client_descriptor::client_descriptor(int client_number) |
---|
38 | { |
---|
39 | player=NULL; |
---|
40 | next=NULL; |
---|
41 | requested_join=0; |
---|
42 | cnum=client_number; |
---|
43 | } |
---|
44 | |
---|
45 | game_server *local_server=NULL; // created on server machine, NULL on all others |
---|
46 | |
---|
47 | |
---|
48 | game_server::game_server(int argc, char **argv, int port) |
---|
49 | { |
---|
50 | client_list=NULL; |
---|
51 | sync_check=0; // should we send sync packets to client? |
---|
52 | |
---|
53 | for (int i=1; i<argc; i++) |
---|
54 | { |
---|
55 | if (!strcmp(argv[i],"-sync")) |
---|
56 | sync_check=1; |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | |
---|
66 | game_server::~game_server() |
---|
67 | { |
---|
68 | for (client_descriptor *p=client_list; p; ) |
---|
69 | { |
---|
70 | client_descriptor *q=p; |
---|
71 | p=p->next; |
---|
72 | delete q; |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | |
---|
78 | void game_server::receive_inputs() // reads inputs from all non-local clients |
---|
79 | { |
---|
80 | /* client_descriptor *last=NULL; |
---|
81 | packet pk; |
---|
82 | for (client_descriptor *p=client_list; p; ) |
---|
83 | { |
---|
84 | int delete_me=0; |
---|
85 | if (p->connection) |
---|
86 | { |
---|
87 | if (p->player) // clients with players are required to send input |
---|
88 | { |
---|
89 | int error=!get_pkt(p->connection,pk); |
---|
90 | if (!error) |
---|
91 | pk.insert_into(next_out); |
---|
92 | else // on error delete the client |
---|
93 | delete_me=1; |
---|
94 | } else |
---|
95 | { |
---|
96 | if (p->connection->ready_to_read()) |
---|
97 | { |
---|
98 | if (!p->connection->get(pk)) |
---|
99 | delete_me=1; |
---|
100 | else |
---|
101 | { |
---|
102 | uchar cmd; |
---|
103 | long cx1,cy1,cx2,cy2; |
---|
104 | if (!pk.read((uchar *)&cmd,1) || cmd!=SCMD_JOIN_GAME || |
---|
105 | pk.read((uchar *)&cx1,4)!=4 || |
---|
106 | pk.read((uchar *)&cy1,4)!=4 || |
---|
107 | pk.read((uchar *)&cx2,4)!=4 || |
---|
108 | pk.read((uchar *)&cy2,4)!=4) |
---|
109 | delete_me=1; |
---|
110 | else |
---|
111 | { |
---|
112 | p->cx1=lltl(cx1); |
---|
113 | p->cy1=lltl(cy1); |
---|
114 | p->cx2=lltl(cx2); |
---|
115 | p->cy2=lltl(cy2); |
---|
116 | p->requested_join=1; // mark this client as wanting to join |
---|
117 | pk.insert_into(next_out); |
---|
118 | } |
---|
119 | |
---|
120 | } |
---|
121 | } |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | if (delete_me) |
---|
126 | { |
---|
127 | client_descriptor *del_me=p; |
---|
128 | p=p->next; |
---|
129 | if (last) |
---|
130 | last->next=p; |
---|
131 | else client_list=p; |
---|
132 | delete del_me; |
---|
133 | } else { last=p; p=p->next; } |
---|
134 | |
---|
135 | |
---|
136 | }*/ |
---|
137 | |
---|
138 | } |
---|
139 | |
---|
140 | void game_server::send_inputs() // pass collected inputs to all non-local clients |
---|
141 | { |
---|
142 | /* client_descriptor *last=NULL; |
---|
143 | if (sync_check) |
---|
144 | { |
---|
145 | next_out.write_uint8(SCMD_SYNC); |
---|
146 | next_out.write_uint32(make_sync_uint32()); |
---|
147 | } |
---|
148 | |
---|
149 | next_out.write_uint8(SCMD_END_OF_PACKET); // so clients knows when to stop reading |
---|
150 | |
---|
151 | for (client_descriptor *p=client_list; p; ) |
---|
152 | { |
---|
153 | if (p->connection && p->player) |
---|
154 | { |
---|
155 | int error=!p->connection->send(next_out); |
---|
156 | if (error) |
---|
157 | { |
---|
158 | client_descriptor *del_me=p; |
---|
159 | p=p->next; |
---|
160 | if (last) |
---|
161 | last->next=p; |
---|
162 | else client_list=p; |
---|
163 | delete del_me; |
---|
164 | } else { last=p; p=p->next; } |
---|
165 | } else |
---|
166 | { |
---|
167 | last=p; |
---|
168 | p=p->next; |
---|
169 | } |
---|
170 | } */ |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | void game_server::check_for_clients() |
---|
175 | { |
---|
176 | |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | void game_server::join_new_players() |
---|
181 | { |
---|
182 | /* int wait=0; |
---|
183 | client_descriptor *p=client_list; |
---|
184 | for (; p; p=p->next) |
---|
185 | if (p->requested_join) |
---|
186 | { |
---|
187 | |
---|
188 | view *f=player_list; |
---|
189 | for (; f && f->next; f=f->next); // find last player, add one for pn |
---|
190 | int i,st=0; |
---|
191 | for (i=0; i<total_objects; i++) |
---|
192 | if (!strcmp(object_names[i],"START")) |
---|
193 | st=i; |
---|
194 | |
---|
195 | game_object *o=create(current_start_type,0,0); |
---|
196 | game_object *start=current_level->get_random_start(320,NULL); |
---|
197 | if (start) { o->x=start->x; o->y=start->y; } |
---|
198 | else { o->x=100; o->y=100; } |
---|
199 | |
---|
200 | f->next=new view(o,NULL,f->player_number+1); |
---|
201 | o->set_controller(f->next); |
---|
202 | |
---|
203 | if (start) |
---|
204 | current_level->add_object_after(o,start); |
---|
205 | else |
---|
206 | current_level->add_object(o); |
---|
207 | |
---|
208 | view *v=f->next; |
---|
209 | |
---|
210 | v->cx1=p->cx1; |
---|
211 | v->cy1=p->cy1; |
---|
212 | v->cx2=p->cx2; |
---|
213 | v->cy2=p->cy2; |
---|
214 | p->player=v; |
---|
215 | v->Drawable=p->cnum; |
---|
216 | |
---|
217 | wait=1; |
---|
218 | p->requested_join=0; |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | if (wait) // wait for acknowedgement from everone then delete net file |
---|
223 | { |
---|
224 | packet pk; |
---|
225 | current_level->save("netstart.spe",1); |
---|
226 | printf("%d sync for save\n",make_sync_uint32()); |
---|
227 | |
---|
228 | client_descriptor *last=NULL; |
---|
229 | for (p=client_list; p; p=p->next) |
---|
230 | { |
---|
231 | if (p->player) |
---|
232 | { |
---|
233 | pk.write_uint8(SCMD_JOIN_START); |
---|
234 | int error=!p->connection->send(pk); |
---|
235 | if (!error) |
---|
236 | { |
---|
237 | while (!p->connection->ready_to_read()) |
---|
238 | service_net_request(); |
---|
239 | } |
---|
240 | |
---|
241 | if (error || !p->connection->get(pk)) |
---|
242 | { |
---|
243 | if (!last) |
---|
244 | client_list=client_list->next; |
---|
245 | else last->next=p->next; |
---|
246 | delete p; |
---|
247 | } else |
---|
248 | last=p; |
---|
249 | } |
---|
250 | } |
---|
251 | unlink("netstart.spe"); |
---|
252 | }*/ |
---|
253 | } |
---|
254 | |
---|
255 | |
---|
256 | |
---|
257 | /* |
---|
258 | |
---|
259 | server/client interaction |
---|
260 | |
---|
261 | |
---|
262 | Client - get/send commands |
---|
263 | |
---|
264 | Server - receive inputs |
---|
265 | check for join request |
---|
266 | if join request add SCMD_JOIN_GAME to out packet |
---|
267 | send inputs |
---|
268 | |
---|
269 | Client - read commands from server |
---|
270 | process commands |
---|
271 | tick_game |
---|
272 | draw |
---|
273 | |
---|
274 | |
---|
275 | Server (if join request) : |
---|
276 | create new_player |
---|
277 | save level to netstart.spe |
---|
278 | wait for all clients with views to send SCMD_NEW_ACK |
---|
279 | new player should read entire level, while old |
---|
280 | clients seek to "player_info" and read this. |
---|
281 | |
---|
282 | |
---|
283 | |
---|
284 | |
---|
285 | |
---|
286 | |
---|
287 | */ |
---|