Commit 71f863197bf4b88fed3d15a1e6df93b896adfe4d
1 parent
c1ff6e0d9f
Exists in
master
* Switched to 3-space indentation (4 is better but gnaroengaeognag) in the
srv_player.c so that it matches the rest of the files. * Fixed bug with linked list in the server, and added more debug prints. * Moved some linked-list specific functions from server.c to srv_player.c Still need to fix the linked list on the client, and make the server quit or reset when there is only one player left, and perhaps get rid of the flood of debugging prints. Should send PLAYER_WIN to clients if they reach the exit, probably. And add another exit in the opposite side of the randomly generated maze to prevent the predator from blocking it.
Showing 4 changed files with 140 additions and 101 deletions Side-by-side Diff
client/main.c
View file @
71f8631
... | ... | @@ -180,7 +180,7 @@ |
180 | 180 | * Poll the network in each frame. Because. |
181 | 181 | */ |
182 | 182 | |
183 | - int numready = SDLNet_CheckSockets(srv_sset, 0); | |
183 | + int result, numready = SDLNet_CheckSockets(srv_sset, 0); | |
184 | 184 | |
185 | 185 | if (numready == -1) |
186 | 186 | { |
... | ... | @@ -194,7 +194,7 @@ |
194 | 194 | |
195 | 195 | printf("srv socket is ready!!\n"); |
196 | 196 | |
197 | - if (SDLNet_TCP_Recv(srv_sock, &packet, 2) == 2) | |
197 | + if ((result = SDLNet_TCP_Recv(srv_sock, &packet, 2)) == 2) | |
198 | 198 | { |
199 | 199 | switch (SDLNet_Read16(&packet)) |
200 | 200 | { |
... | ... | @@ -234,6 +234,12 @@ |
234 | 234 | break; |
235 | 235 | |
236 | 236 | } |
237 | + } | |
238 | + else if (result <= 0) | |
239 | + { | |
240 | + fprintf(stderr, "SDLNet_TCP_Recv: %s\n", SDLNet_GetError()); | |
241 | + fprintf(stderr, "Lost connection to the server?\n"); | |
242 | + break; | |
237 | 243 | } |
238 | 244 | } |
239 | 245 |
server/server.c
View file @
71f8631
... | ... | @@ -38,6 +38,7 @@ |
38 | 38 | if (n == -1) |
39 | 39 | { |
40 | 40 | perror("sendall"); |
41 | + fprintf(stderr, "sendall: Tried to send to socket %d\n", s); | |
41 | 42 | return total; |
42 | 43 | } |
43 | 44 | |
... | ... | @@ -289,6 +290,8 @@ |
289 | 290 | } |
290 | 291 | else |
291 | 292 | { |
293 | + Player *justMoved; | |
294 | + int movPnum; | |
292 | 295 | /* Handle data from a client. (ONLY A SHORT/magic no) */ |
293 | 296 | |
294 | 297 | if ((nbytes = recv(i, &magic, sizeof magic, 0 )) <= 0) |
... | ... | @@ -311,8 +314,17 @@ |
311 | 314 | case PLAYER_MOV: |
312 | 315 | x = getshort(i); |
313 | 316 | y = getshort(i); |
314 | - printf("player with socket %d moved to %d, %d\n", | |
315 | - i, x, y); | |
317 | + | |
318 | + // A bit of a lack of error checking... | |
319 | + justMoved = player_byfd(pset, i); | |
320 | + movPnum = justMoved->playerno; | |
321 | + | |
322 | + justMoved->x = x; | |
323 | + justMoved->y = y; | |
324 | + | |
325 | + printf("PLAYER [%s](fd %d)\t - Moved to (%d, %d)\n", | |
326 | + justMoved->name, i, x, y); | |
327 | + | |
316 | 328 | break; |
317 | 329 | } |
318 | 330 | |
319 | 331 | |
320 | 332 | |
... | ... | @@ -320,13 +332,8 @@ |
320 | 332 | { |
321 | 333 | if (FD_ISSET(j, &master)) |
322 | 334 | { |
323 | - Player *justMoved = player_byfd(pset, i); | |
324 | 335 | Player *coll; |
325 | - int movPnum = justMoved->playerno; | |
326 | 336 | |
327 | - justMoved->x = x; | |
328 | - justMoved->y = y; | |
329 | - | |
330 | 337 | /* |
331 | 338 | * If the player that just moved is a hunter, and it |
332 | 339 | * just stepped on a regular player, kill the (regular) |
333 | 340 | |
... | ... | @@ -337,10 +344,14 @@ |
337 | 344 | { |
338 | 345 | int dead = coll->fd; |
339 | 346 | |
347 | + print_pset(pset); | |
348 | + | |
340 | 349 | printf("%s died, socket %d dropped.\n", coll->name, dead); |
341 | 350 | broadcast_disconnect(pset, dead, 1); |
342 | 351 | close(dead); |
343 | 352 | FD_CLR(dead, &master); |
353 | + | |
354 | + print_pset(pset); | |
344 | 355 | } |
345 | 356 | |
346 | 357 | /* |
... | ... | @@ -438,24 +449,6 @@ |
438 | 449 | pset_map(pset, &send_dc, remove_pno, death ? PLAYER_DIE : PLAYER_DC); |
439 | 450 | } |
440 | 451 | |
441 | -/* | |
442 | - * Check if some player in `pset' has the same x, y as `node'. Return the | |
443 | - * first player that is colliding, or NULL if there are no collisions. | |
444 | - */ | |
445 | -Player * | |
446 | -check_collision(Player_set *pset, Player *node) | |
447 | -{ | |
448 | - Player *temp; | |
449 | - | |
450 | - for (temp = pset->first; temp != NULL; temp = temp->next) | |
451 | - { | |
452 | - if (temp != node && temp->x == node->x && temp->y == node->y) | |
453 | - return temp; | |
454 | - } | |
455 | - return NULL; | |
456 | -} | |
457 | - | |
458 | - | |
459 | 452 | void |
460 | 453 | set_positions(Player_set *pset) |
461 | 454 | { |
462 | 455 | |
... | ... | @@ -464,12 +457,11 @@ |
464 | 457 | { |
465 | 458 | do |
466 | 459 | { |
467 | - temp->x = mrand(0,19) * 2; | |
468 | - temp->y = mrand(0,19) * 2; | |
460 | + temp->x = mrand(0, MAZE.width - 1) * 2; | |
461 | + temp->y = mrand(0, MAZE.width - 1) * 2; | |
469 | 462 | } while (check_collision(pset, temp) != NULL); |
470 | 463 | } |
471 | 464 | } |
472 | - | |
473 | 465 | |
474 | 466 | short int |
475 | 467 | choose_hunter(Player_set *pset) |
server/server.h
View file @
71f8631
... | ... | @@ -50,15 +50,15 @@ |
50 | 50 | void begin_game(Player_set *pset); |
51 | 51 | int sendMov(int psock, short int movepno, int x, int y); |
52 | 52 | void broadcast_disconnect(Player_set *pset, int fd, int death); |
53 | -Player *check_collision(Player_set *pset, Player *node); | |
54 | 53 | |
55 | - | |
56 | 54 | Player_set *init_pset(); |
57 | 55 | void free_pset(Player_set *p); |
58 | 56 | void add_player(Player_set *set); |
59 | 57 | void rm_player(Player_set *set, Player *p); |
60 | 58 | Player *player_byfd(Player_set *s, int fd); |
61 | 59 | Player *player_byindex(Player_set *s, int fd); |
60 | +void print_pset(Player_set *set); | |
61 | +Player *check_collision(Player_set *pset, Player *node); | |
62 | 62 | void pset_map(Player_set *s, |
63 | 63 | void (*func)(Player *p, int a, unsigned short sig), int fd, |
64 | 64 | unsigned short sig); |
server/srv_player.c
View file @
71f8631
... | ... | @@ -10,22 +10,22 @@ |
10 | 10 | Player_set * |
11 | 11 | init_pset() |
12 | 12 | { |
13 | - return calloc(1, sizeof(Player_set)); | |
13 | + return calloc(1, sizeof(Player_set)); | |
14 | 14 | } |
15 | 15 | |
16 | 16 | void |
17 | 17 | free_pset(Player_set *p) |
18 | 18 | { |
19 | - /* | |
20 | - * Scan through the entire linked list and free each element of each | |
21 | - * struct, then free p. | |
22 | - */ | |
23 | - while (p->first != NULL) | |
24 | - { | |
25 | - rm_player(p, p->first); | |
26 | - } | |
19 | + /* | |
20 | + * Scan through the entire linked list and free each element of each | |
21 | + * struct, then free p. | |
22 | + */ | |
23 | + while (p->first != NULL) | |
24 | + { | |
25 | + rm_player(p, p->first); | |
26 | + } | |
27 | 27 | |
28 | - free(p); | |
28 | + free(p); | |
29 | 29 | } |
30 | 30 | |
31 | 31 | /* |
32 | 32 | |
33 | 33 | |
34 | 34 | |
35 | 35 | |
36 | 36 | |
37 | 37 | |
38 | 38 | |
39 | 39 | |
... | ... | @@ -35,64 +35,86 @@ |
35 | 35 | void |
36 | 36 | add_player(Player_set *set) |
37 | 37 | { |
38 | - if (set->first == NULL) | |
39 | - { | |
40 | - /* Player elements should be initialized to all zeroes. */ | |
41 | - set->first = calloc(1, sizeof(Player)); | |
42 | - set->cur = set->last = set->first; | |
43 | - } | |
44 | - else | |
45 | - { | |
46 | - set->last->next = calloc(1, sizeof(Player)); | |
47 | - set->cur = set->last = set->last->next; | |
48 | - } | |
49 | - set->last->playerno = ++(set->last_pno); | |
38 | + if (set->first == NULL) | |
39 | + { | |
40 | + /* Player elements should be initialized to all zeroes. */ | |
41 | + set->first = calloc(1, sizeof(Player)); | |
42 | + set->cur = set->last = set->first; | |
43 | + } | |
44 | + else | |
45 | + { | |
46 | + set->last->next = calloc(1, sizeof(Player)); | |
47 | + | |
48 | + /* | |
49 | + * Make the prev pointer in the new Player point to the previous | |
50 | + * player. | |
51 | + */ | |
52 | + set->last->next->prev = set->last; | |
53 | + | |
54 | + set->cur = set->last = set->last->next; | |
55 | + } | |
56 | + set->last->playerno = ++(set->last_pno); | |
50 | 57 | } |
51 | 58 | |
52 | 59 | void |
53 | 60 | rm_player(Player_set *set, Player *p) |
54 | 61 | { |
55 | - if (p->prev != NULL) | |
56 | - { | |
57 | - p->prev->next = p->next; | |
58 | - } | |
62 | + printf(" XX - Removing player %p\n", p); | |
63 | + if (p->prev != NULL) | |
64 | + { | |
65 | + p->prev->next = p->next; | |
66 | + } | |
59 | 67 | |
60 | - if (p->next != NULL) | |
61 | - { | |
62 | - p->next->prev = p->prev; | |
63 | - } | |
68 | + if (p->next != NULL) | |
69 | + { | |
70 | + p->next->prev = p->prev; | |
71 | + } | |
64 | 72 | |
65 | - if (set->first == p) | |
66 | - { | |
67 | - set->first = p->next; | |
68 | - } | |
73 | + if (set->first == p) | |
74 | + { | |
75 | + set->first = p->next; | |
76 | + } | |
69 | 77 | |
70 | - if (set->last == p) | |
71 | - { | |
72 | - set->last = p->prev; | |
73 | - } | |
78 | + if (set->last == p) | |
79 | + { | |
80 | + set->last = p->prev; | |
81 | + } | |
74 | 82 | |
75 | - free(p->name); | |
76 | - free(p); | |
83 | + free(p->name); | |
84 | + free(p); | |
77 | 85 | } |
78 | 86 | |
87 | +void | |
88 | +print_pset(Player_set *set) | |
89 | +{ | |
90 | + Player *temp; | |
91 | + | |
92 | + for (temp = set->first; temp != NULL; temp = temp->next) | |
93 | + { | |
94 | + printf(" * PLAYER %s with pnum %d, fd %d (%p)\n", | |
95 | + temp->name, temp->playerno, temp->fd, temp); | |
96 | + printf(" - next: %p\n", temp->next); | |
97 | + printf(" - prev: %p\n", temp->prev); | |
98 | + } | |
99 | +} | |
100 | + | |
79 | 101 | /* |
80 | 102 | * Glorious optimized enterprise-level search algorithm. |
81 | 103 | */ |
82 | 104 | Player * |
83 | 105 | player_byfd(Player_set *s, int fd) |
84 | 106 | { |
85 | - Player *this = s->first; | |
107 | + Player *this = s->first; | |
86 | 108 | |
87 | - while (this != NULL) | |
88 | - { | |
89 | - if (this->fd == fd) | |
90 | - { | |
91 | - return this; | |
92 | - } | |
93 | - this = this->next; | |
94 | - } | |
95 | - return NULL; | |
109 | + while (this != NULL) | |
110 | + { | |
111 | + if (this->fd == fd) | |
112 | + { | |
113 | + return this; | |
114 | + } | |
115 | + this = this->next; | |
116 | + } | |
117 | + return NULL; | |
96 | 118 | } |
97 | 119 | |
98 | 120 | /* |
99 | 121 | |
100 | 122 | |
... | ... | @@ -101,22 +123,41 @@ |
101 | 123 | Player * |
102 | 124 | player_byindex(Player_set *s, int index) |
103 | 125 | { |
104 | - Player *this = s->first; | |
105 | - int counter = 0; | |
106 | - while (this != NULL) | |
107 | - { | |
108 | - if (counter++ == index) | |
109 | - { | |
110 | - return this; | |
111 | - } | |
112 | - this = this->next; | |
113 | - } | |
114 | - return NULL; | |
126 | + Player *this = s->first; | |
127 | + int counter = 0; | |
128 | + while (this != NULL) | |
129 | + { | |
130 | + if (counter++ == index) | |
131 | + { | |
132 | + return this; | |
133 | + } | |
134 | + this = this->next; | |
135 | + } | |
136 | + return NULL; | |
115 | 137 | } |
116 | 138 | |
139 | +/* | |
140 | + * Check if some player in `pset' has the same x, y as `node'. Return the | |
141 | + * first player that is colliding, or NULL if there are no collisions. | |
142 | + */ | |
143 | +Player * | |
144 | +check_collision(Player_set *pset, Player *node) | |
145 | +{ | |
146 | + Player *temp; | |
147 | + | |
148 | + for (temp = pset->first; temp != NULL; temp = temp->next) | |
149 | + { | |
150 | + if (temp != node && temp->x == node->x && temp->y == node->y) | |
151 | + return temp; | |
152 | + } | |
153 | + return NULL; | |
154 | +} | |
155 | + | |
117 | 156 | void |
118 | -pset_map(Player_set *s, void (*func)(Player *p, int a, unsigned short sig), | |
119 | - int fd, unsigned short sig) | |
157 | +pset_map(Player_set *s, | |
158 | + void (*func)(Player *p, int a, unsigned short sig), | |
159 | + int fd, | |
160 | + unsigned short sig) | |
120 | 161 | { |
121 | 162 | Player *this = s->first; |
122 | 163 |