Commit 9f59b128f3090e170726b50e5251d2eaca44cdb7

Authored by Pedro L Coutin
1 parent 989be69d26
Exists in master

Lots of things changed, but a bug with the maze width...

Showing 5 changed files with 105 additions and 56 deletions Side-by-side Diff

client/MOT_spec View file @ 9f59b12
... ... @@ -13,7 +13,7 @@
13 13  
14 14 MAX_PLAYERNUM 32
15 15  
16   -all the magic numbers will be unsigned short...
  16 +all the magic numbers will be uint16_t...
17 17  
18 18 TYPES
19 19 unsigned short magical instruction things
... ... @@ -67,7 +67,8 @@
67 67 * send unsigned char pnum
68 68  
69 69 Player disconnects/loses/get hunted
70   -* send PLAYER_DC
  70 +* send PLAYER_DC for disconnect
  71 +* send PLAYER_DIE for lose
71 72 * send unsigned char pnum
72 73 > clear the player from the screen
73 74  
client/main.c View file @ 9f59b12
... ... @@ -151,10 +151,10 @@
151 151 exit(EXIT_FAILURE);
152 152 }
153 153  
154   - unsigned char hunter = addp(player,srv_sock);
  154 + unsigned char hunter = addp(player, srv_sock);
155 155  
156   - choose_hunter(player,hunter);
157   - me = choose_player(player,myno);
  156 + choose_hunter(player, hunter);
  157 + me = choose_player(player, myno);
158 158  
159 159 SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255);
160 160 draw_maze(MAZE.X, MAZE.Y);
161 161  
... ... @@ -191,7 +191,9 @@
191 191 {
192 192 unsigned char packet, hunter;
193 193 unsigned char pnum, movx, movy;
  194 +
194 195 printf("srv socket is ready!!\n");
  196 +
195 197 if (SDLNet_TCP_Recv(srv_sock, &packet, 2) == 2)
196 198 {
197 199 switch (SDLNet_Read16(&packet))
198 200  
199 201  
... ... @@ -206,15 +208,31 @@
206 208 pnum, movx, movy);
207 209 movePlayer(choose_player(player,pnum), movx, movy);
208 210 break;
  211 +
209 212 case PLAYER_WIN:
210 213 puts("PLAYER_WIN");
211 214 break;
  215 +
212 216 case PLAYER_DC:
213 217 puts("PLAYER_DC");
214 218 pnum = getshort(srv_sock);
215 219 printf("Player %d disconnected!!\n", pnum);
216 220 removep(choose_player(player,pnum));
217 221 break;
  222 +
  223 + case PLAYER_DIE:
  224 + puts("PLAYER_DIE");
  225 + pnum = getshort(srv_sock);
  226 +
  227 + if (pnum == myno)
  228 + {
  229 + puts("YOU ARE DEAD\nGAME OVER");
  230 + goto exit;
  231 + }
  232 + printf("Player %d deaded!!!!!\n", pnum);
  233 + removep(choose_player(player,pnum));
  234 + break;
  235 +
218 236 case ADD_PLAYER:
219 237 printf("ADD_PLAYER\n");
220 238 hunter = addp(player,srv_sock);
... ... @@ -265,6 +283,7 @@
265 283 }
266 284 }
267 285  
  286 +exit:
268 287 SDL_DestroyTexture(psprite.texture);
269 288 SDL_DestroyTexture(hsprite.texture);
270 289 SDL_DestroyTexture(black.texture);
server/server.c View file @ 9f59b12
... ... @@ -31,7 +31,7 @@
31 31 int bytesleft = len; // how many we have left to send
32 32 int n;
33 33  
34   - while(total < len)
  34 + while (total < len)
35 35 {
36 36 n = send(s, buf+total, bytesleft, 0);
37 37  
38 38  
39 39  
... ... @@ -78,18 +78,22 @@
78 78 return ntohs(ret);
79 79 }
80 80  
  81 +/*
  82 + * Return: 0 on success
  83 + * 1 on failure
  84 + */
81 85 size_t
82 86 sendshort(int sock, short s)
83 87 {
84 88 s = htons(s);
85   - return send(sock, &s, sizeof(s), 0);
  89 + return sendall(sock, (char *) &s, sizeof(s)) != sizeof(s);
86 90 }
87 91  
88 92 int
89 93 main(int argc, char *argv[])
90 94 {
91 95 int ssockfd, csockfd, err;;
92   - unsigned short magic, x, y;
  96 + unsigned int magic, x, y;
93 97 unsigned int u;
94 98 struct addrinfo hints, *srvinfo, *p;
95 99 struct sockaddr_storage caddr;
96 100  
97 101  
98 102  
99 103  
... ... @@ -289,19 +293,19 @@
289 293  
290 294 if ((nbytes = recv(i, &magic, sizeof magic, 0 )) <= 0)
291 295 {
292   - if (nbytes == 0)
  296 + if (nbytes < 0)
293 297 {
294   - printf("server: socket %d hung up\n", i);
295   - broadcast_disconnect(pset, i);
296   - }
297   - else
298   - {
299 298 perror("recv");
300 299 }
  300 +
  301 + printf("server: socket %d hung up\n", i);
  302 + broadcast_disconnect(pset, i, 0);
301 303 close(i);
302 304 FD_CLR(i, &master);
  305 +
303 306 continue;
304 307 }
  308 +
305 309 switch (htons(magic))
306 310 {
307 311 case PLAYER_MOV:
308 312  
309 313  
310 314  
311 315  
... ... @@ -309,30 +313,40 @@
309 313 y = getshort(i);
310 314 printf("player with socket %d moved to %d, %d\n",
311 315 i, x, y);
312   - /*
313   - * now send it to everyone. non blocking TCP?
314   - * broadcast function?
315   - */
316 316 break;
317 317 }
318 318  
319   - /* we got some data to read,son */
320   -
321 319 for (j = 0; j <= fdmax; j++)
322 320 {
323 321 if (FD_ISSET(j, &master))
324 322 {
325   - /*
326   - * don't send it to server and the client
327   - * who sent the data
328   - */
329   -
330   - Player *justMoved = player_byfd(pset,i);
  323 + Player *justMoved = player_byfd(pset, i);
  324 + Player *coll;
331 325 int movPnum = justMoved->playerno;
332 326  
  327 + /*
  328 + * If the player that just moved is a hunter, and it
  329 + * just stepped on a regular player, kill the (regular)
  330 + * player!
  331 + */
  332 + if (justMoved->type &&
  333 + (coll = check_collision(pset, justMoved)) != NULL)
  334 + {
  335 + int dead = coll->fd;
  336 +
  337 + printf("%s died, socket %d dropped.\n", coll->name, dead);
  338 + broadcast_disconnect(pset, dead, 1);
  339 + close(dead);
  340 + FD_CLR(dead, &master);
  341 + }
  342 +
  343 + /*
  344 + * Avoid sending the data to the current client and
  345 + * server.
  346 + */
333 347 if (j != ssockfd
334 348 && j != i
335   - && sendMov(j,movPnum,x,y) == -1)
  349 + && sendMov(j, movPnum, x, y) == -1)
336 350 {
337 351 perror("send");
338 352 }
... ... @@ -360,7 +374,7 @@
360 374 {
361 375 char *pname;
362 376 unsigned char pnum;
363   - unsigned short magic;
  377 + unsigned int magic;
364 378 unsigned int u;
365 379  
366 380 /*
367 381  
368 382  
369 383  
370 384  
371 385  
372 386  
373 387  
374 388  
375 389  
376 390  
... ... @@ -398,37 +412,44 @@
398 412 pset->last->x = -1;
399 413 pset->last->y = -1;
400 414 pset->last->fd = newfd;
401   - printf("new file descriptor:%d and %d\n", pset->last->fd,newfd);
  415 + printf("new file descriptor: %d (%d)\n", pset->last->fd,newfd);
402 416 }
403 417  
404 418 static void
405   -send_dc(Player *p, int pno_removed)
  419 +send_dc(Player *p, int pno_removed, unsigned int sig)
406 420 {
407   - sendshort(p->fd, PLAYER_DC);
  421 + sendshort(p->fd, sig);
408 422 sendshort(p->fd, pno_removed);
409 423 }
410 424  
  425 +/*
  426 + * Remove player with file descriptor fd from the list, and broadcast its
  427 + * disconnecting.
  428 + */
411 429 void
412   -broadcast_disconnect(Player_set *pset, int fd)
  430 +broadcast_disconnect(Player_set *pset, int fd, int death)
413 431 {
414 432 Player *to_remove = player_byfd(pset, fd);
415 433 int remove_pno = to_remove->playerno;
416 434 rm_player(pset, to_remove);
417   - pset_map(pset, &send_dc, remove_pno);
  435 + pset_map(pset, &send_dc, remove_pno, death ? PLAYER_DIE : PLAYER_DC);
418 436 }
419 437  
420   -int
421   -check_collision(Player_set *pset, Player* node)
  438 +/*
  439 + * Check if some player in `pset' has the same x, y as `node'. Return the
  440 + * first player that is colliding, or NULL if there are no collisions.
  441 + */
  442 +Player *
  443 +check_collision(Player_set *pset, Player *node)
422 444 {
423   - if (node == pset->first)
424   - return 1;
425 445 Player *temp;
426   - for(temp = pset->first; temp != NULL && temp != node; temp = temp->next)
  446 +
  447 + for (temp = pset->first; temp != NULL; temp = temp->next)
427 448 {
428   - if(temp->x == node->x && temp->y == node->y)
429   - return 0;
  449 + if (temp != node && temp->x == node->x && temp->y == node->y)
  450 + return temp;
430 451 }
431   - return 1;
  452 + return NULL;
432 453 }
433 454  
434 455  
435 456  
... ... @@ -436,13 +457,13 @@
436 457 set_positions(Player_set *pset)
437 458 {
438 459 Player *temp;
439   - for(temp = pset->first; temp != NULL; temp = temp->next)
  460 + for (temp = pset->first; temp != NULL; temp = temp->next)
440 461 {
441 462 do
442 463 {
443 464 temp->x = mrand(0,19) * 2;
444 465 temp->y = mrand(0,19) * 2;
445   - }while(!check_collision(pset,temp));
  466 + } while (check_collision(pset, temp) != NULL);
446 467 }
447 468 }
448 469  
449 470  
450 471  
... ... @@ -452,12 +473,13 @@
452 473 {
453 474 int check = 0;
454 475 Player *temp;
455   - while(!check)
  476 + while (!check)
456 477 {
457   - int hpno = mrand(0,pset->last_pno);
458   - for(temp = pset->first; temp != NULL; temp = temp->next)
  478 + int hpno = mrand(1, pset->last_pno);
  479 +
  480 + for (temp = pset->first; temp != NULL; temp = temp->next)
459 481 {
460   - if(temp->playerno == hpno)
  482 + if (temp->playerno == hpno)
461 483 {
462 484 return hpno;
463 485 }
464 486  
465 487  
466 488  
467 489  
... ... @@ -470,14 +492,16 @@
470 492 void
471 493 begin_game(Player_set *pset)
472 494 {
473   - unsigned short magic;
  495 + unsigned int magic;
474 496 int j = 0,i = 0;
475   - short int hpno = mrand(0,pset->last_pno);
  497 + short int hpno = mrand(0, pset->last_pno);
476 498 Player *cur, *info;
  499 +
477 500 set_positions(pset);
478 501 printf("postions set!!\n");
  502 +
479 503 choose_hunter(pset);
480   - printf("in begin_game()!!\n");
  504 +
481 505 for (cur = pset->first; cur != NULL; cur = cur->next)
482 506 {
483 507 for (info = pset->first; info != NULL; info = info->next)
... ... @@ -496,6 +520,7 @@
496 520 magic = htons(HUNTER);
497 521 sendall(cur->fd, (char *) &magic, sizeof(magic));
498 522 sendall(cur->fd, (char *) &hpno, sizeof(hpno));
  523 +
499 524 printf("hunter at %d sent to fd %d!!!\n", hpno, cur->fd);
500 525 }
501 526 printf("out of begin_game()!!\n");
... ... @@ -505,8 +530,10 @@
505 530 int
506 531 sendMov(int psock, short int movepno, int x, int y)
507 532 {
508   - if (sendshort(psock,PLAYER_MOV) == 0 || sendshort(psock,movepno) == 0 ||
509   - sendshort(psock,x) == 0 || sendshort(psock,y) == 0)
  533 + if (sendshort(psock, PLAYER_MOV) ||
  534 + sendshort(psock, movepno) ||
  535 + sendshort(psock, x) ||
  536 + sendshort(psock, y))
510 537 {
511 538 return -1;
512 539 }
server/server.h View file @ 9f59b12
... ... @@ -49,7 +49,8 @@
49 49  
50 50 void begin_game(Player_set *pset);
51 51 int sendMov(int psock, short int movepno, int x, int y);
52   -void broadcast_disconnect(Player_set *pset, int fd);
  52 +void broadcast_disconnect(Player_set *pset, int fd, int death);
  53 +Player *check_collision(Player_set *pset, Player *node);
53 54  
54 55  
55 56 Player_set *init_pset();
... ... @@ -58,7 +59,7 @@
58 59 void rm_player(Player_set *set, Player *p);
59 60 Player *player_byfd(Player_set *s, int fd);
60 61 Player *player_byindex(Player_set *s, int fd);
61   -void pset_map(Player_set *s, void (*func)(Player *, int), int fd);
  62 +void pset_map(Player_set *s, void (*func)(), int fd, unsigned int sig);
62 63  
63 64 int mrand(int floor, int ceil);
64 65 void handle_connecting_player(int newfd, Player_set *pset);
server/srv_player.c View file @ 9f59b12
... ... @@ -115,13 +115,14 @@
115 115 }
116 116  
117 117 void
118   -pset_map(Player_set *s, void (*func)(Player *p, int a), int fd)
  118 +pset_map(Player_set *s, void (*func)(Player *p, int a, unsigned int sig),
  119 + int fd, unsigned int sig)
119 120 {
120 121 Player *this = s->first;
121 122  
122 123 while (this != NULL)
123 124 {
124   - func(this, fd);
  125 + func(this, fd, sig);
125 126 this = this->next;
126 127 }
127 128 }