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" /* meta_control(), player_control(), wingeom_control(),
22 #include "map_window.h" /* for map_center() */
23 #include "wincontrol.h" /* WinConf struct, get_winconf_by_win() */
24 #include "windows.h" /* draw_all_wins() */
25 #include "world.h" /* world global */
29 /* Read next lines of "file" up to (and excluding) a line "%\n" into the
30 * world.player_inventory string.
32 static void read_inventory(char * read_buf, uint32_t linemax, FILE * file);
34 /* Read the next characters in "file" into world.map.cells. In detail: Read
35 * world.map.size.y times world.map.size.x characters, followed by one ignored
36 * character (that we assume is a newline).
38 static void read_map_cells(FILE * file);
40 /* Repeatedly use try_fgets() with given arguments to read the remaining lines
41 * of "file" into the world.log string.
43 static void read_log(char * read_buf, uint32_t linemax, FILE * file);
45 /* Return value seen by atoi() in next line of "file" when passed to try_fgets()
46 * with the given arguments.
48 static uint16_t read_value_from_line(char * read_buf, uint32_t linemax,
51 /* If the server's out file has changed since the last read_world(), return a
52 * pointer to its file descriptor; else, return NULL.
54 * Two tests are performed to check for a file change. The file's last data
55 * modification time in seconds via stat() is compared against world.last_update
56 * (and if it is changed, world.last_update is re-set to it). If this does not
57 * verify a change, the first bytes of the file are read to compare the game
58 * turn number described therein to the last read turn number in world.turn.
60 * The stat() check is mostly useless, for it only detects file updates once a
61 * second. But the turn check fails if a new world is generated from turn 1 on:
62 * the new world also starts in turn 1, not signifying any world change to the
63 * turn check. The stat() check detects this change with at most 1 second delay.
65 static FILE * changed_server_out_file(char * path);
67 /* Attempt to read the server's out file as representation of the game world in
68 * a hard-coded serialization format. Returns 1 on success and 0 if the out file
69 * wasn't read for supposedly not having changed since a last read_world() call.
71 * Note that the first successful read_world() triggers map_center(), so that on
72 * start the client focuses the map window on the player.
74 static uint8_t read_world();
78 static void read_inventory(char * read_buf, uint32_t linemax, FILE * file)
80 char * f_name = "read_inventory()";
81 char * delimiter = "%\n";
82 free(world.player_inventory);
83 world.player_inventory = NULL;
86 try_fgets(read_buf, linemax + 1, file, f_name);
87 if (!(strcmp(read_buf, delimiter)))
92 if (NULL != world.player_inventory)
94 old_size = strlen(world.player_inventory);
96 int new_size = strlen(read_buf);
97 char * new_inventory = try_malloc(old_size + new_size + 1, f_name);
98 memcpy(new_inventory, world.player_inventory, old_size);
99 sprintf(new_inventory + old_size, "%s", read_buf);
100 free(world.player_inventory);
101 world.player_inventory = new_inventory;
103 world.player_inventory[strlen(world.player_inventory) - 1] = '\0';
104 world.player_inventory_select = 0;
109 static void read_map_cells(FILE * file)
111 char * f_name = "read_map_cells()";
112 free(world.map.cells);
113 world.map.cells = try_malloc(world.map.size.y * world.map.size.x, f_name);
115 for (y = 0; y < world.map.size.y; y++)
117 for (x = 0; x < world.map.size.x; x++)
119 char c = try_fgetc(file, f_name);
120 world.map.cells[(y * world.map.size.x) + x] = c;
122 try_fgetc(file, f_name);
128 static void read_log(char * read_buf, uint32_t linemax, FILE * file)
130 char * f_name = "read_log()";
133 while (try_fgets(read_buf, linemax + 1, file, f_name))
136 if (NULL != world.log)
138 old_size = strlen(world.log);
140 int new_size = strlen(read_buf);
141 char * new_log = try_malloc(old_size + new_size + 1, f_name);
142 memcpy(new_log, world.log, old_size);
143 sprintf(new_log + old_size, "%s", read_buf);
151 static uint16_t read_value_from_line(char * read_buf, uint32_t linemax,
154 char * f_name = "read_value_from_line()";
155 try_fgets(read_buf, linemax + 1, file, f_name);
156 return atoi(read_buf);
161 static FILE * changed_server_out_file(char * path)
163 char * f_name = "changed_server_out_file()";
164 struct stat stat_buf;
165 exit_trouble(stat(path, &stat_buf), f_name, "stat()");
166 if (stat_buf.st_mtime != world.last_update)
168 world.last_update = stat_buf.st_mtime;
169 return try_fopen(path, "r", f_name);
171 FILE * file = try_fopen(path, "r", f_name);
173 try_fgets(turn_string, 6, file, f_name);
174 if (world.turn == atoi(turn_string))
176 try_fclose(file, f_name);
179 exit_trouble(fseek(file, 0, SEEK_SET), f_name, "fseek()");
185 static uint8_t read_world()
187 char * f_name = "read_world()";
188 char * path = "server/out";
189 char * quit_msg = "No server out file found to read. Server may be down.";
190 static uint8_t first_read = 1;
191 exit_err(access(path, F_OK), quit_msg);
192 FILE * file = changed_server_out_file(path);
197 uint32_t linemax = textfile_sizes(file, NULL);
198 char * read_buf = try_malloc(linemax + 1, f_name);
199 world.turn = read_value_from_line(read_buf, linemax, file);
200 world.score = read_value_from_line(read_buf, linemax, file);
201 world.player_lifepoints = read_value_from_line(read_buf, linemax, file);
202 read_inventory(read_buf, linemax, file);
203 world.player_pos.y = read_value_from_line(read_buf, linemax, file);
204 world.player_pos.x = read_value_from_line(read_buf, linemax, file);
210 world.map.size.y = read_value_from_line(read_buf, linemax, file);
211 world.map.size.x = read_value_from_line(read_buf, linemax, file);
212 read_map_cells(file);
213 read_log(read_buf, linemax, file);
215 try_fclose(file, f_name);
221 extern void try_send(char * msg)
223 char * f_name = "try_send()";
224 uint32_t msg_size = strlen(msg) + 1;
225 char * err = "try_send() tries to send message larger than PIPE_BUF bytes.";
226 exit_err(msg_size > PIPE_BUF, err);
231 fd_out = open(world.path_server_in, O_WRONLY | O_NONBLOCK);
236 exit_err(-1 == fd_out && ENXIO != errno, "Server fifo not found.");
239 exit_err(0 == j, "Failed to open server fifo for writing.");
243 int test = write(fd_out, msg, msg_size);
250 exit_err(0 == j, "Failed to write to server fifo.");
251 exit_trouble(-1 == close(fd_out), f_name, "close()");
256 extern char * io_loop()
258 world.halfdelay = 1; /* Ensures read_world() is only called 10 */
259 halfdelay(world.halfdelay); /* times a second during user inactivity. */
260 uint8_t change_in_client = 0;
263 if (read_world() || change_in_client)
267 change_in_client = 0;
271 change_in_client = meta_control(key);
272 if (2 == change_in_client)
276 if (!change_in_client)
278 change_in_client = player_control(key);
280 if (!change_in_client)
282 struct WinConf * wc = get_winconf_by_win(world.wmeta.active);
283 change_in_client = (1 == wc->view && wingeom_control(key))
284 || (2 == wc->view && winkeyb_control(key));
289 return "Sent QUIT to server.";