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