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 "misc.h" /* reset_windows() */
22 #include "windows.h" /* draw_all_wins() */
23 #include "world.h" /* world global */
27 /* Read next lines of "file" up to (and excluding) a line "%\n" into the
28 * world.player_inventory string.
30 static void read_inventory(char * read_buf, uint32_t linemax, FILE * file);
32 /* Read the next characters in "file" into world.map.cells. In detail: Read
33 * world.map.size.y times world.map.size.x characters, followed by one ignored
34 * character (that we assume is a newline).
36 static void read_map_cells(FILE * file);
38 /* Repeatedly use try_fgets() with given arguments to read the remaining lines
39 * of "file" into the world.log string.
41 static void read_log(char * read_buf, uint32_t linemax, FILE * file);
43 /* Return value seen by atoi() in next line of "file" when passed to try_fgets()
44 * with the given arguments.
46 static uint16_t read_value_from_line(char * read_buf, uint32_t linemax,
49 /* If the server's out file has changed since the last read_world(), return a
50 * pointer to its file descriptor; else, return NULL.
52 * Two tests are performed to check for a file change. The file's last data
53 * modification time in seconds via stat() is compared against world.last_update
54 * (and if it is changed, world.last_update is re-set to it). If this does not
55 * verify a change, the first bytes of the file are read to compare the game
56 * turn number described therein to the last read turn number in world.turn.
58 * The stat() check is mostly useless, for it only detects file updates once a
59 * second. But the turn check fails if a new world is generated from turn 1 on:
60 * the new world also starts in turn 1, not signifying any world change to the
61 * turn check. The stat() check detects this change with at most 1 second delay.
63 static FILE * changed_server_out_file(char * path);
65 /* Attempt to read the server's out file as representation of the game world in
66 * a hard-coded serialization format. Returns 1 on success and 0 if the out file
67 * wasn't read for supposedly not having changed since a last read_world() call.
69 * Note that the first successful read_world() triggers map_center(), so that on
70 * start the client focuses the map window on the player.
72 static uint8_t read_world();
76 static void read_inventory(char * read_buf, uint32_t linemax, FILE * file)
78 char * f_name = "read_inventory()";
79 char * delimiter = "%\n";
80 free(world.player_inventory);
81 world.player_inventory = NULL;
84 try_fgets(read_buf, linemax + 1, file, f_name);
85 if (!(strcmp(read_buf, delimiter)))
90 if (NULL != world.player_inventory)
92 old_size = strlen(world.player_inventory);
94 int new_size = strlen(read_buf);
95 char * new_inventory = try_malloc(old_size + new_size + 1, f_name);
96 memcpy(new_inventory, world.player_inventory, old_size);
97 sprintf(new_inventory + old_size, "%s", read_buf);
98 free(world.player_inventory);
99 world.player_inventory = new_inventory;
101 world.player_inventory[strlen(world.player_inventory) - 1] = '\0';
102 world.player_inventory_select = 0;
107 static void read_map_cells(FILE * file)
109 char * f_name = "read_map_cells()";
110 free(world.map.cells);
111 world.map.cells = try_malloc(world.map.size.y * world.map.size.x, f_name);
113 for (y = 0; y < world.map.size.y; y++)
115 for (x = 0; x < world.map.size.x; x++)
117 char c = try_fgetc(file, f_name);
118 world.map.cells[(y * world.map.size.x) + x] = c;
120 try_fgetc(file, f_name);
126 static void read_log(char * read_buf, uint32_t linemax, FILE * file)
128 char * f_name = "read_log()";
131 while (try_fgets(read_buf, linemax + 1, file, f_name))
134 if (NULL != world.log)
136 old_size = strlen(world.log);
138 int new_size = strlen(read_buf);
139 char * new_log = try_malloc(old_size + new_size + 1, f_name);
140 memcpy(new_log, world.log, old_size);
141 sprintf(new_log + old_size, "%s", read_buf);
149 static uint16_t read_value_from_line(char * read_buf, uint32_t linemax,
152 char * f_name = "read_value_from_line()";
153 try_fgets(read_buf, linemax + 1, file, f_name);
154 return atoi(read_buf);
159 static FILE * changed_server_out_file(char * path)
161 char * f_name = "changed_server_out_file()";
162 struct stat stat_buf;
163 exit_trouble(stat(path, &stat_buf), f_name, "stat()");
164 if (stat_buf.st_mtime != world.last_update)
166 world.last_update = stat_buf.st_mtime;
167 return try_fopen(path, "r", f_name);
169 FILE * file = try_fopen(path, "r", f_name);
171 try_fgets(turn_string, 6, file, f_name);
172 if (world.turn == atoi(turn_string))
174 try_fclose(file, f_name);
177 exit_trouble(fseek(file, 0, SEEK_SET), f_name, "fseek()");
183 static uint8_t read_world()
185 char * f_name = "read_world()";
186 char * path = "server/out";
187 char * quit_msg = "No server out file found to read. Server may be down.";
188 static uint8_t first_read = 1;
189 exit_err(access(path, F_OK), quit_msg);
190 FILE * file = changed_server_out_file(path);
195 uint32_t linemax = textfile_sizes(file, NULL);
196 char * read_buf = try_malloc(linemax + 1, f_name);
197 world.turn = read_value_from_line(read_buf, linemax, file);
198 world.score = read_value_from_line(read_buf, linemax, file);
199 world.player_lifepoints = read_value_from_line(read_buf, linemax, file);
200 read_inventory(read_buf, linemax, file);
201 world.player_pos.y = read_value_from_line(read_buf, linemax, file);
202 world.player_pos.x = read_value_from_line(read_buf, linemax, file);
208 world.map.size.y = read_value_from_line(read_buf, linemax, file);
209 world.map.size.x = read_value_from_line(read_buf, linemax, file);
210 read_map_cells(file);
211 read_log(read_buf, linemax, file);
213 try_fclose(file, f_name);
219 extern void try_send(char * msg)
221 char * f_name = "try_send()";
222 uint32_t msg_size = strlen(msg) + 1;
223 char * err = "try_send() tries to send message larger than PIPE_BUF bytes.";
224 exit_err(msg_size > PIPE_BUF, err);
229 fd_out = open(world.path_server_in, O_WRONLY | O_NONBLOCK);
234 exit_err(-1 == fd_out && ENXIO != errno, "Server fifo not found.");
237 exit_err(0 == j, "Failed to open server fifo for writing.");
241 int test = write(fd_out, msg, msg_size);
248 exit_err(0 == j, "Failed to write to server fifo.");
249 exit_trouble(-1 == close(fd_out), f_name, "close()");
254 extern char * io_loop()
256 world.halfdelay = 1; /* Ensures read_world() is only called 10 */
257 halfdelay(world.halfdelay); /* times a second during user inactivity. */
258 uint8_t change_in_client = 0;
267 if (read_world() || change_in_client)
271 change_in_client = 0;
275 change_in_client = try_key((uint16_t) key);
276 if (2 == change_in_client)
283 return "Sent QUIT to server.";