Commit 989be69d260632e1884ad955f554861eda3a3405

Authored by Pedro L Coutin
Exists in master

merging rohan into main 1

Showing 7 changed files Side-by-side Diff

client/Makefile View file @ 989be69
1 1 CC= clang
2   -CFLAGS= -Wall -O2 -c -I/usr/local/include
  2 +CFLAGS= -Wall -O0 -g -c -I/usr/local/include
3 3 LDFLAGS= -L/usr/local/lib -lGL -lSDL2 -lSDL2_image -lSDL2_net
4 4  
5 5 OBJFILES= main.o cfg.o maze.o picture.o player.o ini.o net.o
client/entities/entities.h View file @ 989be69
... ... @@ -32,10 +32,46 @@
32 32  
33 33 void local_player_update(TCPsocket sock, PLAYER *, PLAYER *, const Uint8 *);
34 34  
  35 +/*
  36 + * Move the player `p' TO a specified x, y location (assigns, doesn't
  37 + * translate).
  38 + */
35 39 void movePlayer(PLAYER *p, short newx, short newy);
36 40  
  41 +/*
  42 + * Reset the maze cell that the player is on...
  43 + */
37 44 void clearPlayer(PLAYER *);
38 45  
  46 +/*
  47 + * Return the player with pnode `pnum' from the player linked list
  48 + * starting with `node'.
  49 + */
  50 +PLAYER *choose_player(PLAYER *node, unsigned char pnum);
  51 +
  52 +/*
  53 + * Free the player `temp' after linking the two players next to `temp'
  54 + * together. Also erases it from the screen.
  55 + */
  56 +void removep(PLAYER *temp);
  57 +
  58 +/*
  59 + * On the linked list starting with `node', find the player with pnode
  60 + * `hpno', and set it to be of hunter type.
  61 + */
  62 +void choose_hunter(PLAYER *node, unsigned char hpno);
  63 +
  64 +/*
  65 + * Add an initialized player "object" to the player linked list containing
  66 + * `node'.
  67 + */
  68 +void add_player(PLAYER *node, PLAYER *newp);
  69 +
  70 +/*
  71 + * Keep receiving players from the server until it's done and it chooses a
  72 + * hunter, adding them to the player linked list containing `node'.
  73 + */
  74 +unsigned char addp(PLAYER *node, TCPsocket srv_sock);
39 75  
40 76 /*
41 77 * GLOBALS
client/entities/player.c View file @ 989be69
... ... @@ -121,6 +121,10 @@
121 121 drawPlayer(me);
122 122 }
123 123  
  124 +/*
  125 + * Move the player `p' TO a specified x, y location (assigns, doesn't
  126 + * translate).
  127 + */
124 128 void
125 129 movePlayer(PLAYER *p, short newx, short newy)
126 130 {
... ... @@ -159,5 +163,139 @@
159 163 SDL_RenderCopy(renderer,
160 164 black.texture,
161 165 NULL, &player->sprite->rect);
  166 +}
  167 +
  168 +/*
  169 + * Return the player with pnode `pnum' from the player linked list
  170 + * starting with `node'.
  171 + */
  172 +PLAYER *
  173 +choose_player(PLAYER *node, unsigned char pnum)
  174 +{
  175 + PLAYER *temp;
  176 + for (temp = node->next; temp != NULL; temp = temp->next)
  177 + {
  178 + printf("temp->playerno = %d, pnum = %d\n", temp->playerno, pnum);
  179 + if (temp->playerno == pnum)
  180 + {
  181 + break;
  182 + }
  183 + }
  184 + return temp;
  185 +}
  186 +
  187 +/*
  188 + * Free the player `temp' after linking the two players next to `temp'
  189 + * together. Also erases it from the screen.
  190 + */
  191 +//potential bug: hunter eats someone, then someone disconnects. they both
  192 +//have the same location, so they both disappear?
  193 +void
  194 +removep(PLAYER *temp)
  195 +{
  196 + if (temp == NULL)
  197 + {
  198 + printf("Can't remove NULL player!!\n");
  199 + return;
  200 + }
  201 + if (temp->prev == NULL)
  202 + {
  203 + if (temp->next != NULL)
  204 + {
  205 + temp->next->prev = NULL;
  206 + }
  207 + }
  208 + else
  209 + {
  210 + if (temp->next == NULL)
  211 + {
  212 + temp->prev->next = NULL;
  213 + }
  214 + else
  215 + {
  216 + temp->prev->next = temp->next;
  217 + temp->next->prev = temp->prev;
  218 + }
  219 + }
  220 + clearPlayer(temp);
  221 + free(temp);
  222 +}
  223 +
  224 +/*
  225 + * Blindly iterate through the player linked list starting from `node' to
  226 + * find the player with pnode `hpno', and set it to be of hunter type.
  227 + */
  228 +void
  229 +choose_hunter(PLAYER *node, unsigned char hpno)
  230 +{
  231 + PLAYER *temp;
  232 +
  233 + for (temp = node; temp != NULL; temp = temp->next)
  234 + {
  235 + if (temp->playerno == hpno)
  236 + {
  237 + temp->type = 1;
  238 + temp->sprite = &hsprite;
  239 + return;
  240 + }
  241 + }
  242 +}
  243 +
  244 +/*
  245 + * Add an initialized player "object" to the player linked list containing
  246 + * `node'.
  247 + */
  248 +void
  249 +add_player(PLAYER *node, PLAYER *newp)
  250 +{
  251 + PLAYER *temp;
  252 +
  253 + for (temp = node; temp->next != NULL; temp = temp->next)
  254 + {
  255 + printf("cycle: temp->next->playerno: %d\n",temp->next->playerno);
  256 + }
  257 +
  258 + for (temp = node; temp->next != NULL; temp = temp->next);
  259 +
  260 + temp->next = newp;
  261 + newp->prev = temp;
  262 + newp->next = NULL;
  263 +}
  264 +
  265 +/*
  266 + * Keep receiving players from the server until it's done and it chooses a
  267 + * hunter, adding them to the player linked list containing `node'.
  268 + */
  269 +unsigned char
  270 +addp(PLAYER *node, TCPsocket srv_sock)
  271 +{
  272 + Uint16 magic;
  273 + PLAYER *cur_player = NULL;
  274 +
  275 + do
  276 + {
  277 + cur_player = calloc(1, sizeof(PLAYER));
  278 + init_player(srv_sock, cur_player);
  279 + add_player(node, cur_player);
  280 +
  281 + printf("Player %s (%d) connected, at (%d, %d)\n", cur_player->name,
  282 + cur_player->playerno, cur_player->x, cur_player->y);
  283 +
  284 + } while ((magic = getshort(srv_sock)) == ADD_PLAYER);
  285 +
  286 + printf("players added\n");
  287 +
  288 + if (magic == HUNTER)
  289 + {
  290 + unsigned char hunter;
  291 +
  292 + SDLNet_TCP_Recv(srv_sock, &hunter, 1);
  293 + return hunter;
  294 + }
  295 + else
  296 + {
  297 + fprintf(stderr, "Bad magic number %X from server\n", magic);
  298 + exit(EXIT_FAILURE);
  299 + }
162 300 }
client/main.c View file @ 989be69
... ... @@ -14,119 +14,6 @@
14 14 #include "mot.h"
15 15 #include "net.h"
16 16  
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   -
33   -void
34   -removep(PLAYER *temp)
35   -{
36   - if(temp == NULL)
37   - {
38   - printf("Can't remove NULL player!!\n");
39   - return;
40   - }
41   - if (temp->prev == NULL)
42   - {
43   - if (temp->next != NULL)
44   - {
45   - temp->next->prev = NULL;
46   - }
47   - }
48   - else
49   - {
50   - if (temp->next == NULL)
51   - {
52   - temp->prev->next = NULL;
53   - }
54   - else
55   - {
56   - temp->prev->next = temp->next;
57   - temp->next->prev = temp->prev;
58   - }
59   - }
60   - clearPlayer(temp);
61   - free(temp);
62   -}
63   -
64   -void
65   -choose_hunter(PLAYER *node, unsigned char hpno)
66   -{
67   - PLAYER *temp;
68   -
69   - for (temp = node; temp != NULL; temp = temp->next)
70   - {
71   - if (temp->playerno == hpno)
72   - {
73   - temp->type = 1;
74   - temp->sprite = &hsprite;
75   - return;
76   - }
77   - }
78   -}
79   -
80   -
81   -void
82   -add_player(PLAYER *node, PLAYER *newp)
83   -{
84   - PLAYER *temp;
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   -
91   - temp->next = newp;
92   - newp->prev = temp;
93   - newp->next = NULL;
94   -}
95   -
96   -unsigned char
97   -addp(PLAYER *node,TCPsocket srv_sock)
98   -{
99   - Uint16 magic;
100   - PLAYER *cur_player = NULL;
101   -
102   - do
103   - {
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   -
110   - } while ((magic = getshort(srv_sock)) == ADD_PLAYER);
111   -
112   - printf("players added\n");
113   -
114   - if (magic == HUNTER)
115   - {
116   - unsigned char hunter;
117   -
118   - SDLNet_TCP_Recv(srv_sock, &hunter, 1);
119   - return hunter;
120   - }
121   - else
122   - {
123   - fprintf(stderr, "Bad magic number %X from server\n", magic);
124   - exit(EXIT_FAILURE);
125   - }
126   -
127   -}
128   -
129   -
130 17 int
131 18 main(int argc, char *argv[])
132 19 {
133 20  
134 21  
135 22  
136 23  
137 24  
138 25  
... ... @@ -257,19 +144,23 @@
257 144  
258 145 SDLNet_TCP_Recv(srv_sock, &myno, 1);
259 146 player = calloc(1, sizeof(PLAYER));
260   - draw_maze(MAZE.X, MAZE.Y);
261 147  
262   -
263   - if(!((magic = getshort(srv_sock)) == ADD_PLAYER))
  148 + if (!((magic = getshort(srv_sock)) == ADD_PLAYER))
264 149 {
265 150 printf("server not sending players\n!");
266 151 exit(EXIT_FAILURE);
267 152 }
  153 +
268 154 unsigned char hunter = addp(player,srv_sock);
  155 +
269 156 choose_hunter(player,hunter);
270 157 me = choose_player(player,myno);
  158 +
  159 + SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255);
271 160 draw_maze(MAZE.X, MAZE.Y);
  161 +
272 162 PLAYER *temp;
  163 +
273 164 for (temp = player->next; temp != NULL; temp = temp->next)
274 165 {
275 166 printf("drew player %d\n", temp->playerno);
... ... @@ -330,6 +221,7 @@
330 221 choose_hunter(player,hunter);
331 222 me = choose_player(player,myno);
332 223 draw_maze(MAZE.X, MAZE.Y);
  224 +
333 225 PLAYER *temp;
334 226 for (temp = player->next; temp != NULL; temp = temp->next)
335 227 {
... ... @@ -363,10 +255,7 @@
363 255  
364 256 local_player_update(srv_sock, me, player, SDL_GetKeyboardState(NULL));
365 257 }
366   - SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255);
367   - /*
368   - * Stop drawing things.
369   - */
  258 +
370 259 SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255);
371 260 SDL_RenderPresent(renderer);
372 261  
client/maze.dat View file @ 989be69

No preview for this file type

server/server.c View file @ 989be69
... ... @@ -12,13 +12,6 @@
12 12 #include "../common/mot_maze.h"
13 13 #include "server.h"
14 14  
15   -
16   -void begin_game(Player_set *pset);
17   -
18   -int sendMov(int psock, short int movepno, int x, int y);
19   -
20   -void broadcast_disconnect(Player_set *pset, int fd);
21   -
22 15 void *
23 16 get_in_addr(struct sockaddr *sa)
24 17 {
... ... @@ -300,13 +293,6 @@
300 293 {
301 294 printf("server: socket %d hung up\n", i);
302 295 broadcast_disconnect(pset, i);
303   - if(--players_connected < min_players)
304   - {
305   - printf("too few players, accepting more players now\n");
306   -
307   - /* DON'T HALT THE GAME, THOUGH */
308   - game_started = 0;
309   - }
310 296 }
311 297 else
312 298 {
... ... @@ -479,6 +465,8 @@
479 465 }
480 466 return 0;
481 467 }
  468 +
  469 +
482 470 void
483 471 begin_game(Player_set *pset)
484 472 {
... ... @@ -503,6 +491,7 @@
503 491 sendall(cur->fd, (char *) &magic, sizeof(magic));
504 492 sendall(cur->fd, info->name, PNAMELEN);
505 493 }
  494 +
506 495 // hunter
507 496 magic = htons(HUNTER);
508 497 sendall(cur->fd, (char *) &magic, sizeof(magic));
... ... @@ -512,29 +501,6 @@
512 501 printf("out of begin_game()!!\n");
513 502 }
514 503  
515   -
516   -/* add a bunch
517   - * player no
518   - magic = htons(ADD_PLAYER);
519   - sendall(newfd, (char *) &magic, sizeof(magic));
520   - sendall(newfd, (char *) &pnum, sizeof(pnum));
521   - x and y, there can be collisions but who cares??????
522   - magic = htons(mrand(0, 19) * 2);
523   - sendall(newfd, (char *) &magic, sizeof(magic));
524   -
525   - magic = htons(mrand(0, 19) * 2);
526   - sendall(newfd, (char *) &magic, sizeof(magic));
527   -
528   - sendall(newfd, pname, 32);
529   - }
530   -
531   - // random hunter
532   - magic = htons(HUNTER);
533   - sendall(newfd, (char *) &magic, sizeof(magic));
534   -
535   - pnum = mrand(0, 11);
536   - sendall(newfd, (char *) &pnum, sizeof(pnum));
537   -*/
538 504  
539 505 int
540 506 sendMov(int psock, short int movepno, int x, int y)
server/server.h View file @ 989be69
... ... @@ -47,6 +47,11 @@
47 47 int last_pno;
48 48 };
49 49  
  50 +void begin_game(Player_set *pset);
  51 +int sendMov(int psock, short int movepno, int x, int y);
  52 +void broadcast_disconnect(Player_set *pset, int fd);
  53 +
  54 +
50 55 Player_set *init_pset();
51 56 void free_pset(Player_set *p);
52 57 void add_player(Player_set *set);