-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
98 lines (88 loc) · 2.93 KB
/
Copy pathclient.c
File metadata and controls
98 lines (88 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "client.h"
#include "server.h"
#include "network.h"
#include "transmit.h"
/* Set up a multiplayer game. */
void setup_multiplayer(game_t* game, int players, int player, int sockfd) {
int i;
game->multiplayer = 1;
game->multiplayer_data.mode = MODE_TEAM;
game->multiplayer_data.players = players;
game->multiplayer_data.player = player;
game->multiplayer_data.sockfd = sockfd;
game->players = realloc(game->players, sizeof(player_t) * players);
for (i = 1; i < players; i++)
spawn_player(game, &game->players[i], PLAYER_LIVES);
}
/* Check whether a multiplayer game should end. */
int check_multiplayer_game_over(game_t* game) {
int slot;
for (slot = 0; slot < NUMBER_OF_PLAYERS; slot++) {
if (!game->players[slot].nospawn)
return 0;
}
return 1;
}
/* Load multiplayer data from the server; non-blocking. */
void load_server_data(game_t* game) {
char* buffer;
int sockfd = game->multiplayer_data.sockfd, command;
while (1) {
if (receive(sockfd, &command, &buffer) < 0) {
if (errno != EWOULDBLOCK && errno != EAGAIN)
game->running = 0;
break;
}
switch (command) {
case CMD_GAME_UPDATE:
unserialize_game_data(buffer, game);
break;
case CMD_GAME_OVER:
case CMD_QUIT:
// TODO: CMD_QUIT should show the server quit message in buffer
game->running = 0;
break;
}
free(buffer);
}
}
/* Handle user keyboard input during a multiplayer game. */
void handle_input_multi(game_t* game) {
int key;
char tmpbuf[8];
while ((key = getkey()) != KEY_NOTHING) {
switch (key) {
case 'q':
transmit(game->multiplayer_data.sockfd, CMD_PLAYER_PART, NULL);
game->running = 0;
break;
case KEY_UP:
case 'w':
snprintf(tmpbuf, 8, "%d", INPUT_UP);
transmit(game->multiplayer_data.sockfd, CMD_INPUT, tmpbuf);
break;
case KEY_DOWN:
case 's':
snprintf(tmpbuf, 8, "%d", INPUT_DOWN);
transmit(game->multiplayer_data.sockfd, CMD_INPUT, tmpbuf);
break;
case KEY_LEFT:
case 'a':
snprintf(tmpbuf, 8, "%d", INPUT_LEFT);
transmit(game->multiplayer_data.sockfd, CMD_INPUT, tmpbuf);
break;
case KEY_RIGHT:
case 'd':
snprintf(tmpbuf, 8, "%d", INPUT_RIGHT);
transmit(game->multiplayer_data.sockfd, CMD_INPUT, tmpbuf);
break;
case ' ':
snprintf(tmpbuf, 8, "%d", INPUT_SHOOT);
transmit(game->multiplayer_data.sockfd, CMD_INPUT, tmpbuf);
break;
}
}
}