Commit 2aac6ef41be29c5f1b38177412dbd5471b7ce6d8

Authored by Pedro L Coutin
Exists in master

OCD merging

Showing 3 changed files Side-by-side Diff

client/entities/player.c View file @ 2aac6ef
... ... @@ -134,15 +134,18 @@
134 134 void
135 135 drawPlayer(PLAYER *player)
136 136 {
  137 + printf("inside drawplayer()!!\n");
137 138 if (player->dead)
138 139 {
139 140 return;
140 141 }
141 142 player->sprite->rect.x = MAZE.X + player->x * TILE_WIDTH + 10;
142 143 player->sprite->rect.y = MAZE.Y + player->y * TILE_HEIGHT + 10;
  144 + printf("changed player sprite!!\n");
143 145 SDL_RenderCopy(renderer,
144 146 player->sprite->texture,
145   - NULL, &player->sprite->rect);
  147 + NULL, &(player->sprite->rect));
  148 + printf("exiting drawplayer()!!\n");
146 149 }
147 150  
148 151 /*
client/main.c View file @ 2aac6ef
... ... @@ -15,9 +15,29 @@
15 15 #include "net.h"
16 16  
17 17  
  18 +PLAYER*
  19 +choose_player(PLAYER* node, unsigned char pnum)
  20 +{
  21 + PLAYER *temp;
  22 + for(temp = node->next; temp != NULL; temp = temp->next)
  23 + {
  24 + printf("temp->playerno = %d, pnum = %d\n",temp->playerno,pnum);
  25 + if(temp->playerno == pnum)
  26 + {
  27 + break;
  28 + }
  29 + }
  30 + return temp;
  31 +}
  32 +
18 33 void
19 34 removep(PLAYER *temp)
20 35 {
  36 + if(temp == NULL)
  37 + {
  38 + printf("Can't remove NULL player!!\n");
  39 + return;
  40 + }
21 41 if (temp->prev == NULL)
22 42 {
23 43 if (temp->next != NULL)
24 44  
25 45  
26 46  
... ... @@ -37,20 +57,22 @@
37 57 temp->next->prev = temp->prev;
38 58 }
39 59 }
  60 + clearPlayer(temp);
40 61 free(temp);
41 62 }
42 63  
43 64 void
44   -choose_hunter(PLAYER *node, unsigned char hpno, PICTURE *hsprite)
  65 +choose_hunter(PLAYER *node, unsigned char hpno)
45 66 {
46 67 PLAYER *temp;
  68 +
47 69 for (temp = node; temp != NULL; temp = temp->next)
48 70 {
49 71 if (temp->playerno == hpno)
50 72 {
51 73 temp->type = 1;
52   - temp->sprite = hsprite;
53   - break;
  74 + temp->sprite = &hsprite;
  75 + return;
54 76 }
55 77 }
56 78 }
57 79  
58 80  
59 81  
60 82  
... ... @@ -61,29 +83,34 @@
61 83 {
62 84 PLAYER *temp;
63 85 for (temp = node; temp->next != NULL; temp = temp->next);
  86 + {
  87 + printf("cycle: temp->next->playerno: %d\n",temp->next->playerno);
  88 + }
  89 + for (temp = node; temp->next != NULL; temp = temp->next);
  90 +
64 91 temp->next = newp;
65 92 newp->prev = temp;
66 93 newp->next = NULL;
67 94 }
68 95  
69 96 unsigned char
70   -addp(PLAYER* node,TCPsocket srv_sock)
  97 +addp(PLAYER *node,TCPsocket srv_sock)
71 98 {
72 99 Uint16 magic;
  100 + PLAYER *cur_player = NULL;
  101 +
73 102 do
74 103 {
75   - PLAYER cur_player;
76   - init_player(srv_sock, &cur_player);
77   - add_player(node,&cur_player);
78   - printf("Player %s (%d) connected, at (%d, %d)\n", cur_player.name,
79   - cur_player.playerno, cur_player.x, cur_player.y);
  104 + cur_player = calloc(1,sizeof(PLAYER));
  105 + init_player(srv_sock,cur_player);
  106 + add_player(node,cur_player);
  107 + printf("Player %s (%d) connected, at (%d, %d)\n", cur_player->name,
  108 + cur_player->playerno, cur_player->x, cur_player->y);
  109 +
80 110 } while ((magic = getshort(srv_sock)) == ADD_PLAYER);
81 111  
82 112 printf("players added\n");
83 113  
84   - /*
85   - * Get the hunter.
86   - */
87 114 if (magic == HUNTER)
88 115 {
89 116 unsigned char hunter;
90 117  
91 118  
92 119  
93 120  
94 121  
95 122  
96 123  
97 124  
... ... @@ -229,65 +256,31 @@
229 256 */
230 257  
231 258 SDLNet_TCP_Recv(srv_sock, &myno, 1);
  259 + player = calloc(1, sizeof(PLAYER));
  260 + draw_maze(MAZE.X, MAZE.Y);
232 261  
233   - player = calloc(MAX_PLAYERNUM + 1, sizeof(PLAYER));
234 262  
235   - printf("adding players!!\n");
236   -
237   - while ((magic = getshort(srv_sock)) == ADD_PLAYER)
  263 + if(!((magic = getshort(srv_sock)) == ADD_PLAYER))
238 264 {
239   - PLAYER cur_player;
240   - init_player(srv_sock, &cur_player);
241   - *(player + cur_player.playerno) = cur_player;
242   - printf("Player %s (%d) connected, at (%d, %d)\n", cur_player.name,
243   - cur_player.playerno, cur_player.x, cur_player.y);
244   - *(player+cur_player.playerno) = cur_player;
245   - }
246   -
247   - printf("players added\n");
248   - me = player + myno;
249   -
250   - /*
251   - * Get the hunter.
252   - */
253   - if (magic == HUNTER)
254   - {
255   - unsigned char hunter;
256   -
257   - SDLNet_TCP_Recv(srv_sock, &hunter, 1);
258   -
259   - (player + hunter)->type = 1;
260   - (player + hunter)->sprite = &hsprite;
261   - }
262   - else
263   - {
264   - fprintf(stderr, "Bad magic number %X from server\n", magic);
  265 + printf("server not sending players\n!");
265 266 exit(EXIT_FAILURE);
266 267 }
267   -
268   - printf("HUNTER DECIDED\n");
269   - /*
270   - * Draw things.
271   - */
272   - SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255);
273   -
274   - /* Draw the maze in the middle of the screen! And draw the players */
  268 + unsigned char hunter = addp(player,srv_sock);
  269 + choose_hunter(player,hunter);
  270 + me = choose_player(player,myno);
275 271 draw_maze(MAZE.X, MAZE.Y);
276   -
277   - drawPlayer(me);
278   -
279   - for (i = 0; i < MAX_PLAYERNUM; ++i)
  272 + PLAYER *temp;
  273 + for (temp = player->next; temp != NULL; temp = temp->next)
280 274 {
281   - if ((player + i) != NULL && (player + i)->sprite != NULL)
282   - {
283   - printf("drew player %d\n", i);
284   - drawPlayer(player + i);
285   - }
  275 + printf("drew player %d\n", temp->playerno);
  276 + drawPlayer(temp);
286 277 }
287   -
  278 +
  279 + printf("starting game!!\n");
288 280 /*
289 281 * Game loop!
290 282 */
  283 +
291 284 for (;;)
292 285 {
293 286 time = SDL_GetTicks();
... ... @@ -305,9 +298,9 @@
305 298 }
306 299 else if (numready)
307 300 {
308   - unsigned short packet, hunter;
309   - int pnum, movx, movy;
310   -
  301 + unsigned char packet, hunter;
  302 + unsigned char pnum, movx, movy;
  303 + printf("srv socket is ready!!\n");
311 304 if (SDLNet_TCP_Recv(srv_sock, &packet, 2) == 2)
312 305 {
313 306 switch (SDLNet_Read16(&packet))
... ... @@ -320,7 +313,7 @@
320 313  
321 314 printf("player %d moved to (%d,%d)\n",
322 315 pnum, movx, movy);
323   - movePlayer(player + pnum, movx, movy);
  316 + movePlayer(choose_player(player,pnum), movx, movy);
324 317 break;
325 318 case PLAYER_WIN:
326 319 puts("PLAYER_WIN");
327 320  
328 321  
... ... @@ -328,12 +321,24 @@
328 321 case PLAYER_DC:
329 322 puts("PLAYER_DC");
330 323 pnum = getshort(srv_sock);
331   - clearPlayer(player+pnum);
332   - removep(player+pnum);
  324 + printf("Player %d disconnected!!\n", pnum);
  325 + removep(choose_player(player,pnum));
333 326 break;
334 327 case ADD_PLAYER:
  328 + printf("ADD_PLAYER\n");
335 329 hunter = addp(player,srv_sock);
336   - choose_hunter(player,hunter,&hsprite);
  330 + choose_hunter(player,hunter);
  331 + me = choose_player(player,myno);
  332 + draw_maze(MAZE.X, MAZE.Y);
  333 + PLAYER *temp;
  334 + for (temp = player->next; temp != NULL; temp = temp->next)
  335 + {
  336 + if (temp != NULL && temp->sprite != NULL)
  337 + {
  338 + printf("drew player %d\n", temp->playerno);
  339 + drawPlayer(temp);
  340 + }
  341 + }
337 342 break;
338 343 }
339 344 }
server/server.c View file @ 2aac6ef
... ... @@ -428,42 +428,57 @@
428 428 Player *to_remove = player_byfd(pset, fd);
429 429 int remove_pno = to_remove->playerno;
430 430 rm_player(pset, to_remove);
431   -
432 431 pset_map(pset, &send_dc, remove_pno);
433 432 }
434 433  
435 434 int
436   -check_collision(Player_set *pset, short pno)
  435 +check_collision(Player_set *pset, Player* node)
437 436 {
438   - if (pno == 0)
  437 + if (node == pset->first)
439 438 return 1;
440   -
441   - if (player_byindex(pset,pno)->x == player_byindex(pset,pno-1)->x &&
442   - player_byindex(pset,pno)->y == player_byindex(pset,pno-1)->y)
443   - return 1;
444   - else
445   - return check_collision(pset,pno-1);
  439 + Player *temp;
  440 + for(temp = pset->first; temp != NULL && temp != node; temp = temp->next)
  441 + {
  442 + if(temp->x == node->x && temp->y == node->y)
  443 + return 0;
  444 + }
  445 + return 1;
446 446 }
447 447  
448 448  
449 449 void
450 450 set_positions(Player_set *pset)
451 451 {
452   - int j, i;
  452 + Player *temp;
  453 + for(temp = pset->first; temp != NULL; temp = temp->next)
  454 + {
  455 + do
  456 + {
  457 + temp->x = mrand(0,19) * 2;
  458 + temp->y = mrand(0,19) * 2;
  459 + }while(!check_collision(pset,temp));
  460 + }
  461 +}
453 462  
454   - for(i = 0; i < pset->last_pno; ++i)
  463 +
  464 +short int
  465 +choose_hunter(Player_set *pset)
  466 +{
  467 + int check = 0;
  468 + Player *temp;
  469 + while(!check)
455 470 {
456   - int check = 0;
457   - while(!check)
  471 + int hpno = mrand(0,pset->last_pno);
  472 + for(temp = pset->first; temp != NULL; temp = temp->next)
458 473 {
459   - player_byindex(pset,i)->x = mrand(0,19) * 2;
460   - player_byindex(pset,i)->y = mrand(0,19) * 2;
461   - check = check_collision(pset,i);
  474 + if(temp->playerno == hpno)
  475 + {
  476 + return hpno;
  477 + }
462 478 }
463 479 }
  480 + return 0;
464 481 }
465   -
466   -
467 482 void
468 483 begin_game(Player_set *pset)
469 484 {
470 485  
471 486  
472 487  
473 488  
474 489  
475 490  
476 491  
477 492  
478 493  
479 494  
... ... @@ -471,39 +486,29 @@
471 486 int j = 0,i = 0;
472 487 short int hpno = mrand(0,pset->last_pno);
473 488 Player *cur, *info;
474   -
475 489 set_positions(pset);
476   -
  490 + printf("postions set!!\n");
  491 + choose_hunter(pset);
477 492 printf("in begin_game()!!\n");
478   -
479   - for (i = 0; i < pset->last_pno; ++i)
  493 + for (cur = pset->first; cur != NULL; cur = cur->next)
480 494 {
481   - cur = player_byindex(pset,i);
482   -
483   - for (j = 0; j < pset->last_pno; ++j)
  495 + for (info = pset->first; info != NULL; info = info->next)
484 496 {
485   - info = player_byindex(pset,j);
486 497 magic = htons(ADD_PLAYER);
487 498 sendall(cur->fd, (char *) &magic, sizeof(magic));
488 499 sendall(cur->fd, (char *) &info->playerno, sizeof(info->playerno));
489   -
490 500 magic = htons(info->x);
491 501 sendall(cur->fd, (char *) &magic, sizeof(magic));
492   -
493 502 magic = htons(info->y);
494 503 sendall(cur->fd, (char *) &magic, sizeof(magic));
495   -
496 504 sendall(cur->fd, info->name, PNAMELEN);
497 505 }
498   -
499 506 // hunter
500 507 magic = htons(HUNTER);
501 508 sendall(cur->fd, (char *) &magic, sizeof(magic));
502 509 sendall(cur->fd, (char *) &hpno, sizeof(hpno));
503   -
504 510 printf("hunter at %d sent to fd %d!!!\n", hpno, cur->fd);
505 511 }
506   -
507 512 printf("out of begin_game()!!\n");
508 513 }
509 514