From a2eea9f4bf1611283cfdb5bf5d31f6ea437fe07d Mon Sep 17 00:00:00 2001 From: Pedro L Coutin Date: Thu, 2 Jan 2014 22:44:43 -0800 Subject: [PATCH] Refactored some of the code in the client and added a few comments. Also, removed the code to accept players if the player count ever goes below the minimum -- if we ever are to accept additional players after the game has started, it should probably be up to the maximum number of players. Or infinity, since we have linked lists stored in the heap, but checking calloc for errors? :p --- client/entities/entities.h | 36 ++++++++++++ client/entities/player.c | 138 +++++++++++++++++++++++++++++++++++++++++++++ client/main.c | 114 ------------------------------------- server/server.c | 40 +------------ server/server.h | 5 ++ 5 files changed, 182 insertions(+), 151 deletions(-) diff --git a/client/entities/entities.h b/client/entities/entities.h index 118bb0c..bc1590c 100644 --- a/client/entities/entities.h +++ b/client/entities/entities.h @@ -32,10 +32,46 @@ void drawPlayer(PLAYER *); void local_player_update(TCPsocket sock, PLAYER *, PLAYER *, const Uint8 *); +/* + * Move the player `p' TO a specified x, y location (assigns, doesn't + * translate). + */ void movePlayer(PLAYER *p, short newx, short newy); +/* + * Reset the maze cell that the player is on... + */ void clearPlayer(PLAYER *); +/* + * Return the player with pnode `pnum' from the player linked list + * starting with `node'. + */ +PLAYER *choose_player(PLAYER *node, unsigned char pnum); + +/* + * Free the player `temp' after linking the two players next to `temp' + * together. Also erases it from the screen. + */ +void removep(PLAYER *temp); + +/* + * On the linked list starting with `node', find the player with pnode + * `hpno', and set it to be of hunter type. + */ +void choose_hunter(PLAYER *node, unsigned char hpno); + +/* + * Add an initialized player "object" to the player linked list containing + * `node'. + */ +void add_player(PLAYER *node, PLAYER *newp); + +/* + * Keep receiving players from the server until it's done and it chooses a + * hunter, adding them to the player linked list containing `node'. + */ +unsigned char addp(PLAYER *node, TCPsocket srv_sock); /* * GLOBALS diff --git a/client/entities/player.c b/client/entities/player.c index eee6915..2db1a83 100644 --- a/client/entities/player.c +++ b/client/entities/player.c @@ -121,6 +121,10 @@ local_player_update(TCPsocket sock, PLAYER *me, PLAYER *remote, drawPlayer(me); } +/* + * Move the player `p' TO a specified x, y location (assigns, doesn't + * translate). + */ void movePlayer(PLAYER *p, short newx, short newy) { @@ -160,3 +164,137 @@ clearPlayer(PLAYER *player) black.texture, NULL, &player->sprite->rect); } + +/* + * Return the player with pnode `pnum' from the player linked list + * starting with `node'. + */ +PLAYER * +choose_player(PLAYER *node, unsigned char pnum) +{ + PLAYER *temp; + for (temp = node->next; temp != NULL; temp = temp->next) + { + printf("temp->playerno = %d, pnum = %d\n", temp->playerno, pnum); + if (temp->playerno == pnum) + { + break; + } + } + return temp; +} + +/* + * Free the player `temp' after linking the two players next to `temp' + * together. Also erases it from the screen. + */ +//potential bug: hunter eats someone, then someone disconnects. they both +//have the same location, so they both disappear? +void +removep(PLAYER *temp) +{ + if (temp == NULL) + { + printf("Can't remove NULL player!!\n"); + return; + } + if (temp->prev == NULL) + { + if (temp->next != NULL) + { + temp->next->prev = NULL; + } + } + else + { + if (temp->next == NULL) + { + temp->prev->next = NULL; + } + else + { + temp->prev->next = temp->next; + temp->next->prev = temp->prev; + } + } + clearPlayer(temp); + free(temp); +} + +/* + * Blindly iterate through the player linked list starting from `node' to + * find the player with pnode `hpno', and set it to be of hunter type. + */ +void +choose_hunter(PLAYER *node, unsigned char hpno) +{ + PLAYER *temp; + + for (temp = node; temp != NULL; temp = temp->next) + { + if (temp->playerno == hpno) + { + temp->type = 1; + temp->sprite = &hsprite; + return; + } + } +} + +/* + * Add an initialized player "object" to the player linked list containing + * `node'. + */ +void +add_player(PLAYER *node, PLAYER *newp) +{ + PLAYER *temp; + + for (temp = node; temp->next != NULL; temp = temp->next) + { + printf("cycle: temp->next->playerno: %d\n",temp->next->playerno); + } + + for (temp = node; temp->next != NULL; temp = temp->next); + + temp->next = newp; + newp->prev = temp; + newp->next = NULL; +} + +/* + * Keep receiving players from the server until it's done and it chooses a + * hunter, adding them to the player linked list containing `node'. + */ +unsigned char +addp(PLAYER *node, TCPsocket srv_sock) +{ + Uint16 magic; + PLAYER *cur_player = NULL; + + do + { + cur_player = calloc(1, sizeof(PLAYER)); + init_player(srv_sock, cur_player); + add_player(node, cur_player); + + printf("Player %s (%d) connected, at (%d, %d)\n", cur_player->name, + cur_player->playerno, cur_player->x, cur_player->y); + + } while ((magic = getshort(srv_sock)) == ADD_PLAYER); + + printf("players added\n"); + + if (magic == HUNTER) + { + unsigned char hunter; + + SDLNet_TCP_Recv(srv_sock, &hunter, 1); + return hunter; + } + else + { + fprintf(stderr, "Bad magic number %X from server\n", magic); + exit(EXIT_FAILURE); + } +} diff --git a/client/main.c b/client/main.c index 482bda4..6143710 100644 --- a/client/main.c +++ b/client/main.c @@ -14,120 +14,6 @@ #include "mot.h" #include "net.h" - -PLAYER* -choose_player(PLAYER* node, unsigned char pnum) -{ - PLAYER *temp; - for(temp = node->next; temp != NULL; temp = temp->next) - { - printf("temp->playerno = %d, pnum = %d\n",temp->playerno,pnum); - if(temp->playerno == pnum) - { - break; - } - } - return temp; -} - -void -removep(PLAYER* temp) -{ - if(temp == NULL) - { - printf("Can't remove NULL player!!\n"); - return; - } - if (temp->prev == NULL) - { - if (temp->next != NULL) - { - temp->next->prev = NULL; - } - } - else - { - if (temp->next == NULL) - { - temp->prev->next = NULL; - } - else - { - temp->prev->next = temp->next; - temp->next->prev = temp->prev; - } - } - clearPlayer(temp); - free(temp); -} - -void -choose_hunter(PLAYER *node, unsigned char hpno) -{ - PLAYER *temp; - - for (temp = node; temp != NULL; temp = temp->next) - { - if (temp->playerno == hpno) - { - temp->type = 1; - temp->sprite = &hsprite; - return; - } - } -} - - -void -add_player(PLAYER *node, PLAYER *newp) -{ - PLAYER *temp; - for(temp = node; temp->next != NULL; temp = temp->next) - { - printf("cycle: temp->next->playerno: %d\n",temp->next->playerno); - } - for (temp = node; temp->next != NULL; temp = temp->next); - - temp->next = newp; - newp->prev = temp; - newp->next = NULL; -} - -unsigned char -addp(PLAYER *node,TCPsocket srv_sock) -{ - Uint16 magic; - PLAYER *cur_player = NULL; - do - { - cur_player = calloc(1,sizeof(PLAYER)); - init_player(srv_sock,cur_player); - add_player(node,cur_player); - - printf("Player %s (%d) connected, at (%d, %d)\n", cur_player->name, - cur_player->playerno, cur_player->x, cur_player->y); - - } while ((magic = getshort(srv_sock)) == ADD_PLAYER); - - printf("players added\n"); - - if (magic == HUNTER) - { - unsigned char hunter; - - SDLNet_TCP_Recv(srv_sock, &hunter, 1); - return hunter; - } - else - { - fprintf(stderr, "Bad magic number %X from server\n", magic); - exit(EXIT_FAILURE); - } - - -} - - int main(int argc, char *argv[]) { diff --git a/server/server.c b/server/server.c index 7d00722..794e2f4 100644 --- a/server/server.c +++ b/server/server.c @@ -12,13 +12,6 @@ #include "../common/mot_maze.h" #include "server.h" - -void begin_game(Player_set *pset); - -int sendMov(int psock, short int movepno, int x, int y); - -void broadcast_disconnect(Player_set *pset, int fd); - void * get_in_addr(struct sockaddr *sa) { @@ -300,13 +293,6 @@ main(int argc, char *argv[]) { printf("server: socket %d hung up\n", i); broadcast_disconnect(pset, i); - if(--players_connected < min_players) - { - printf("too few players, accepting more players now\n"); - - /* DON'T HALT THE GAME, THOUGH */ - game_started = 0; - } } else { @@ -479,6 +465,8 @@ choose_hunter(Player_set *pset) } return 0; } + + void begin_game(Player_set *pset) { @@ -503,6 +491,7 @@ begin_game(Player_set *pset) sendall(cur->fd, (char *) &magic, sizeof(magic)); sendall(cur->fd, info->name, PNAMELEN); } + // hunter magic = htons(HUNTER); sendall(cur->fd, (char *) &magic, sizeof(magic)); @@ -513,29 +502,6 @@ begin_game(Player_set *pset) } -/* add a bunch - * player no - magic = htons(ADD_PLAYER); - sendall(newfd, (char *) &magic, sizeof(magic)); - sendall(newfd, (char *) &pnum, sizeof(pnum)); - x and y, there can be collisions but who cares?????? - magic = htons(mrand(0, 19) * 2); - sendall(newfd, (char *) &magic, sizeof(magic)); - - magic = htons(mrand(0, 19) * 2); - sendall(newfd, (char *) &magic, sizeof(magic)); - - sendall(newfd, pname, 32); - } - - // random hunter - magic = htons(HUNTER); - sendall(newfd, (char *) &magic, sizeof(magic)); - - pnum = mrand(0, 11); - sendall(newfd, (char *) &pnum, sizeof(pnum)); -*/ - int sendMov(int psock, short int movepno, int x, int y) { diff --git a/server/server.h b/server/server.h index 87c3beb..12a2aa5 100644 --- a/server/server.h +++ b/server/server.h @@ -47,6 +47,11 @@ struct _player_set int last_pno; }; +void begin_game(Player_set *pset); +int sendMov(int psock, short int movepno, int x, int y); +void broadcast_disconnect(Player_set *pset, int fd); + + Player_set *init_pset(); void free_pset(Player_set *p); void add_player(Player_set *set); -- 1.9.1