4 #include <errno.h> /* global errno */
5 #include <fcntl.h> /* open() */
6 #include <limits.h> /* PIPE_BUF */
7 #include <ncurses.h> /* halfdelay(), getch() */
8 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
9 #include <stdio.h> /* FILE, sprintf(), fseek() */
10 #include <string.h> /* strcmp(), strlen(), memcpy() */
11 #include <stdlib.h> /* free(), atoi() */
12 #include <sys/stat.h> /* stat() */
13 #include <unistd.h> /* access(), write() */
14 #include "../common/try_malloc.h" /* try_malloc() */
15 #include "../common/rexit.h" /* exit_trouble(), exit_err() */
16 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), try_fgets(),
19 #include "control.h" /* try_key() */
20 #include "map_window.h" /* for map_center() */
21 #include "windows.h" /* draw_all_wins() */
22 #include "world.h" /* world global */
26 /* Read next lines of "file" up to (and excluding) a line "%\n" into the
27 * world.player_inventory string.
29 static void read_inventory(char * read_buf, uint32_t linemax, FILE * file);
31 /* Read the next characters in "file" into world.map.cells. In detail: Read
32 * world.map.size.y times world.map.size.x characters, followed by one ignored
33 * character (that we assume is a newline).
35 static void read_map_cells(FILE * file);
37 /* Repeatedly use try_fgets() with given arguments to read the remaining lines
38 * of "file" into the world.log string.
40 static void read_log(char * read_buf, uint32_t linemax, FILE * file);
42 /* Return value seen by atoi() in next line of "file" when passed to try_fgets()
43 * with the given arguments.
45 static uint16_t read_value_from_line(char * read_buf, uint32_t linemax,
48 /* If the server's out file has changed since the last read_world(), return a
49 * pointer to its file descriptor; else, return NULL.
51 * Two tests are performed to check for a file change. The file's last data
52 * modification time in seconds via stat() is compared against world.last_update
53 * (and if it is changed, world.last_update is re-set to it). If this does not
54 * verify a change, the first bytes of the file are read to compare the game
55 * turn number described therein to the last read turn number in world.turn.
57 * The stat() check is mostly useless, for it only detects file updates once a
58 * second. But the turn check fails if a new world is generated from turn 1 on:
59 * the new world also starts in turn 1, not signifying any world change to the
60 * turn check. The stat() check detects this change with at most 1 second delay.
62 static FILE * changed_server_out_file(char * path);
64 /* Attempt to read the server's out file as representation of the game world in
65 * a hard-coded serialization format. Returns 1 on success and 0 if the out file
66 * wasn't read for supposedly not having changed since a last read_world() call.
68 * Note that the first successful read_world() triggers map_center(), so that on
69 * start the client focuses the map window on the player.
71 static uint8_t read_world();
75 static void read_inventory(char * read_buf, uint32_t linemax, FILE * file)
77 char * f_name = "read_inventory()";
78 char * delimiter = "%\n";
79 free(world.player_inventory);
80 world.player_inventory = NULL;
83 try_fgets(read_buf, linemax + 1, file, f_name);
84 if (!(strcmp(read_buf, delimiter)))
89 if (NULL != world.player_inventory)
91 old_size = strlen(world.player_inventory);
93 int new_size = strlen(read_buf);
94 char * new_inventory = try_malloc(old_size + new_size + 1, f_name);
95 memcpy(new_inventory, world.player_inventory, old_size);
96 sprintf(new_inventory + old_size, "%s", read_buf);
97 free(world.player_inventory);
98 world.player_inventory = new_inventory;
100 world.player_inventory[strlen(world.player_inventory) - 1] = '\0';
101 world.player_inventory_select = 0;
106 static void read_map_cells(FILE * file)
108 char * f_name = "read_map_cells()";
109 free(world.map.cells);
110 world.map.cells = try_malloc(world.map.size.y * world.map.size.x, f_name);
112 for (y = 0; y < world.map.size.y; y++)
114 for (x = 0; x < world.map.size.x; x++)
116 char c = try_fgetc(file, f_name);
117 world.map.cells[(y * world.map.size.x) + x] = c;
119 try_fgetc(file, f_name);
125 static void read_log(char * read_buf, uint32_t linemax, FILE * file)
127 char * f_name = "read_log()";
130 while (try_fgets(read_buf, linemax + 1, file, f_name))
133 if (NULL != world.log)
135 old_size = strlen(world.log);
137 int new_size = strlen(read_buf);
138 char * new_log = try_malloc(old_size + new_size + 1, f_name);
139 memcpy(new_log, world.log, old_size);
140 sprintf(new_log + old_size, "%s", read_buf);
148 static uint16_t read_value_from_line(char * read_buf, uint32_t linemax,
151 char * f_name = "read_value_from_line()";
152 try_fgets(read_buf, linemax + 1, file, f_name);
153 return atoi(read_buf);
158 static FILE * changed_server_out_file(char * path)
160 char * f_name = "changed_server_out_file()";
161 struct stat stat_buf;
162 exit_trouble(stat(path, &stat_buf), f_name, "stat()");
163 if (stat_buf.st_mtime != world.last_update)
165 world.last_update = stat_buf.st_mtime;
166 return try_fopen(path, "r", f_name);
168 FILE * file = try_fopen(path, "r", f_name);
170 try_fgets(turn_string, 6, file, f_name);
171 if (world.turn == atoi(turn_string))
173 try_fclose(file, f_name);
176 exit_trouble(fseek(file, 0, SEEK_SET), f_name, "fseek()");
182 static uint8_t read_world()
184 char * f_name = "read_world()";
185 char * path = "server/out";
186 char * quit_msg = "No server out file found to read. Server may be down.";
187 static uint8_t first_read = 1;
188 exit_err(access(path, F_OK), quit_msg);
189 FILE * file = changed_server_out_file(path);
194 uint32_t linemax = textfile_sizes(file, NULL);
195 char * read_buf = try_malloc(linemax + 1, f_name);
196 world.turn = read_value_from_line(read_buf, linemax, file);
197 world.score = read_value_from_line(read_buf, linemax, file);
198 world.player_lifepoints = read_value_from_line(read_buf, linemax, file);
199 read_inventory(read_buf, linemax, file);
200 world.player_pos.y = read_value_from_line(read_buf, linemax, file);
201 world.player_pos.x = read_value_from_line(read_buf, linemax, file);
207 world.map.size.y = read_value_from_line(read_buf, linemax, file);
208 world.map.size.x = read_value_from_line(read_buf, linemax, file);
209 read_map_cells(file);
210 read_log(read_buf, linemax, file);
212 try_fclose(file, f_name);
218 extern void try_send(char * msg)
220 char * f_name = "try_send()";
221 uint32_t msg_size = strlen(msg) + 1;
222 char * err = "try_send() tries to send message larger than PIPE_BUF bytes.";
223 exit_err(msg_size > PIPE_BUF, err);
228 fd_out = open(world.path_server_in, O_WRONLY | O_NONBLOCK);
233 exit_err(-1 == fd_out && ENXIO != errno, "Server fifo not found.");
236 exit_err(0 == j, "Failed to open server fifo for writing.");
240 int test = write(fd_out, msg, msg_size);
247 exit_err(0 == j, "Failed to write to server fifo.");
248 exit_trouble(-1 == close(fd_out), f_name, "close()");
253 extern char * io_loop()
255 world.halfdelay = 1; /* Ensures read_world() is only called 10 */
256 halfdelay(world.halfdelay); /* times a second during user inactivity. */
257 uint8_t change_in_client = 0;
260 if (read_world() || change_in_client)
264 change_in_client = 0;
268 change_in_client = try_key((uint16_t) key);
269 if (2 == change_in_client)
276 return "Sent QUIT to server.";