[56] | 1 | /* |
---|
| 2 | * Abuse - dark 2D side-scrolling platform game |
---|
| 3 | * Copyright (c) 1995 Crack dot Com |
---|
[494] | 4 | * Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net> |
---|
[56] | 5 | * |
---|
| 6 | * This software was released into the Public Domain. As with most public |
---|
[555] | 7 | * domain software, no warranty is made or implied by Crack dot Com, by |
---|
| 8 | * Jonathan Clark, or by Sam Hocevar. |
---|
[56] | 9 | */ |
---|
| 10 | |
---|
[555] | 11 | #if defined HAVE_CONFIG_H |
---|
| 12 | # include "config.h" |
---|
| 13 | #endif |
---|
[56] | 14 | |
---|
[481] | 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" |
---|
[2] | 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 | |
---|
[494] | 53 | for (int i=1; i<argc; i++) |
---|
[2] | 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 | { |
---|
[494] | 68 | for (client_descriptor *p=client_list; p; ) |
---|
[2] | 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; |
---|
[494] | 82 | for (client_descriptor *p=client_list; p; ) |
---|
[124] | 83 | { |
---|
[2] | 84 | int delete_me=0; |
---|
| 85 | if (p->connection) |
---|
| 86 | { |
---|
| 87 | if (p->player) // clients with players are required to send input |
---|
| 88 | { |
---|
[124] | 89 | int error=!get_pkt(p->connection,pk); |
---|
| 90 | if (!error) |
---|
[2] | 91 | pk.insert_into(next_out); |
---|
[124] | 92 | else // on error delete the client |
---|
| 93 | delete_me=1; |
---|
[2] | 94 | } else |
---|
| 95 | { |
---|
[124] | 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 | } |
---|
[2] | 119 | |
---|
| 120 | } |
---|
| 121 | } |
---|
[124] | 122 | } |
---|
| 123 | } |
---|
[2] | 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; |
---|
[494] | 132 | delete del_me; |
---|
[2] | 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 | { |
---|
[17] | 145 | next_out.write_uint8(SCMD_SYNC); |
---|
| 146 | next_out.write_uint32(make_sync_uint32()); |
---|
[2] | 147 | } |
---|
[124] | 148 | |
---|
[17] | 149 | next_out.write_uint8(SCMD_END_OF_PACKET); // so clients knows when to stop reading |
---|
[2] | 150 | |
---|
[494] | 151 | for (client_descriptor *p=client_list; p; ) |
---|
[2] | 152 | { |
---|
| 153 | if (p->connection && p->player) |
---|
| 154 | { |
---|
| 155 | int error=!p->connection->send(next_out); |
---|
| 156 | if (error) |
---|
| 157 | { |
---|
[124] | 158 | client_descriptor *del_me=p; |
---|
| 159 | p=p->next; |
---|
| 160 | if (last) |
---|
| 161 | last->next=p; |
---|
| 162 | else client_list=p; |
---|
[494] | 163 | delete del_me; |
---|
[2] | 164 | } else { last=p; p=p->next; } |
---|
[124] | 165 | } else |
---|
[2] | 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; |
---|
[494] | 184 | for (; p; p=p->next) |
---|
[2] | 185 | if (p->requested_join) |
---|
| 186 | { |
---|
| 187 | |
---|
| 188 | view *f=player_list; |
---|
[494] | 189 | for (; f && f->next; f=f->next); // find last player, add one for pn |
---|
[2] | 190 | int i,st=0; |
---|
[494] | 191 | for (i=0; i<total_objects; i++) |
---|
[2] | 192 | if (!strcmp(object_names[i],"START")) |
---|
[124] | 193 | st=i; |
---|
[2] | 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 | |
---|
[124] | 200 | f->next=new view(o,NULL,f->player_number+1); |
---|
[2] | 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 | |
---|
[124] | 208 | view *v=f->next; |
---|
[2] | 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; |
---|
[124] | 216 | |
---|
[2] | 217 | wait=1; |
---|
| 218 | p->requested_join=0; |
---|
| 219 | } |
---|
| 220 | |
---|
[124] | 221 | |
---|
[2] | 222 | if (wait) // wait for acknowedgement from everone then delete net file |
---|
| 223 | { |
---|
| 224 | packet pk; |
---|
| 225 | current_level->save("netstart.spe",1); |
---|
[17] | 226 | printf("%d sync for save\n",make_sync_uint32()); |
---|
[2] | 227 | |
---|
| 228 | client_descriptor *last=NULL; |
---|
[494] | 229 | for (p=client_list; p; p=p->next) |
---|
[2] | 230 | { |
---|
| 231 | if (p->player) |
---|
| 232 | { |
---|
[124] | 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()) |
---|
[2] | 238 | service_net_request(); |
---|
[124] | 239 | } |
---|
[2] | 240 | |
---|
[124] | 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; |
---|
[2] | 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 |
---|
[124] | 266 | if join request add SCMD_JOIN_GAME to out packet |
---|
| 267 | send inputs |
---|
[2] | 268 | |
---|
| 269 | Client - read commands from server |
---|
| 270 | process commands |
---|
[124] | 271 | tick_game |
---|
| 272 | draw |
---|
[2] | 273 | |
---|
| 274 | |
---|
| 275 | Server (if join request) : |
---|
| 276 | create new_player |
---|
| 277 | save level to netstart.spe |
---|
[124] | 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. |
---|
[2] | 281 | |
---|
| 282 | |
---|
| 283 | |
---|
| 284 | |
---|
| 285 | |
---|
| 286 | |
---|
| 287 | */ |
---|