[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 |
---|
| 7 | * domain software, no warranty is made or implied by Crack dot Com or |
---|
| 8 | * Jonathan Clark. |
---|
| 9 | */ |
---|
| 10 | |
---|
| 11 | #include "config.h" |
---|
| 12 | |
---|
[481] | 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" |
---|
[2] | 22 | |
---|
[39] | 23 | extern char const *get_username(); |
---|
[2] | 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 | |
---|
[494] | 52 | for (int i=1; i<argc; i++) |
---|
[2] | 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 | { |
---|
[494] | 67 | for (client_descriptor *p=client_list; p; ) |
---|
[2] | 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; |
---|
[494] | 81 | for (client_descriptor *p=client_list; p; ) |
---|
[124] | 82 | { |
---|
[2] | 83 | int delete_me=0; |
---|
| 84 | if (p->connection) |
---|
| 85 | { |
---|
| 86 | if (p->player) // clients with players are required to send input |
---|
| 87 | { |
---|
[124] | 88 | int error=!get_pkt(p->connection,pk); |
---|
| 89 | if (!error) |
---|
[2] | 90 | pk.insert_into(next_out); |
---|
[124] | 91 | else // on error delete the client |
---|
| 92 | delete_me=1; |
---|
[2] | 93 | } else |
---|
| 94 | { |
---|
[124] | 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 | } |
---|
[2] | 118 | |
---|
| 119 | } |
---|
| 120 | } |
---|
[124] | 121 | } |
---|
| 122 | } |
---|
[2] | 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; |
---|
[494] | 131 | delete del_me; |
---|
[2] | 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 | { |
---|
[17] | 144 | next_out.write_uint8(SCMD_SYNC); |
---|
| 145 | next_out.write_uint32(make_sync_uint32()); |
---|
[2] | 146 | } |
---|
[124] | 147 | |
---|
[17] | 148 | next_out.write_uint8(SCMD_END_OF_PACKET); // so clients knows when to stop reading |
---|
[2] | 149 | |
---|
[494] | 150 | for (client_descriptor *p=client_list; p; ) |
---|
[2] | 151 | { |
---|
| 152 | if (p->connection && p->player) |
---|
| 153 | { |
---|
| 154 | int error=!p->connection->send(next_out); |
---|
| 155 | if (error) |
---|
| 156 | { |
---|
[124] | 157 | client_descriptor *del_me=p; |
---|
| 158 | p=p->next; |
---|
| 159 | if (last) |
---|
| 160 | last->next=p; |
---|
| 161 | else client_list=p; |
---|
[494] | 162 | delete del_me; |
---|
[2] | 163 | } else { last=p; p=p->next; } |
---|
[124] | 164 | } else |
---|
[2] | 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; |
---|
[494] | 183 | for (; p; p=p->next) |
---|
[2] | 184 | if (p->requested_join) |
---|
| 185 | { |
---|
| 186 | |
---|
| 187 | view *f=player_list; |
---|
[494] | 188 | for (; f && f->next; f=f->next); // find last player, add one for pn |
---|
[2] | 189 | int i,st=0; |
---|
[494] | 190 | for (i=0; i<total_objects; i++) |
---|
[2] | 191 | if (!strcmp(object_names[i],"START")) |
---|
[124] | 192 | st=i; |
---|
[2] | 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 | |
---|
[124] | 199 | f->next=new view(o,NULL,f->player_number+1); |
---|
[2] | 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 | |
---|
[124] | 207 | view *v=f->next; |
---|
[2] | 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; |
---|
[124] | 215 | |
---|
[2] | 216 | wait=1; |
---|
| 217 | p->requested_join=0; |
---|
| 218 | } |
---|
| 219 | |
---|
[124] | 220 | |
---|
[2] | 221 | if (wait) // wait for acknowedgement from everone then delete net file |
---|
| 222 | { |
---|
| 223 | packet pk; |
---|
| 224 | current_level->save("netstart.spe",1); |
---|
[17] | 225 | printf("%d sync for save\n",make_sync_uint32()); |
---|
[2] | 226 | |
---|
| 227 | client_descriptor *last=NULL; |
---|
[494] | 228 | for (p=client_list; p; p=p->next) |
---|
[2] | 229 | { |
---|
| 230 | if (p->player) |
---|
| 231 | { |
---|
[124] | 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()) |
---|
[2] | 237 | service_net_request(); |
---|
[124] | 238 | } |
---|
[2] | 239 | |
---|
[124] | 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; |
---|
[2] | 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 |
---|
[124] | 265 | if join request add SCMD_JOIN_GAME to out packet |
---|
| 266 | send inputs |
---|
[2] | 267 | |
---|
| 268 | Client - read commands from server |
---|
| 269 | process commands |
---|
[124] | 270 | tick_game |
---|
| 271 | draw |
---|
[2] | 272 | |
---|
| 273 | |
---|
| 274 | Server (if join request) : |
---|
| 275 | create new_player |
---|
| 276 | save level to netstart.spe |
---|
[124] | 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. |
---|
[2] | 280 | |
---|
| 281 | |
---|
| 282 | |
---|
| 283 | |
---|
| 284 | |
---|
| 285 | |
---|
| 286 | */ |
---|