4 #include <stddef.h> /* NULL */
5 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
6 #include <stdio.h> /* FILE, sprintf(), fflush() */
7 #include <stdlib.h> /* free() */
8 #include <string.h> /* strlen(), strcmp() strncmp(), atoi() */
9 #include <unistd.h> /* access() */
10 #include "../common/readwrite.h" /* try_fopen(), try_fcose(), try_fwrite(),
11 * try_fgets(), try_fclose_unlink_rename(),
12 * textfile_width(), try_fputc()
14 #include "../common/rexit.h" /* exit_trouble(), exit_err() */
15 #include "ai.h" /* ai() */
16 #include "cleanup.h" /* unset_cleanup_flag() */
17 #include "init.h" /* remake_world() */
18 #include "io.h" /* io_round() */
19 #include "map_object_actions.h" /* get_moa_id_by_name() */
20 #include "map_objects.h" /* struct MapObj, get_player() */
21 #include "world.h" /* global world */
25 /* Run the game world and its inhabitants (and their actions) until the player
26 * avatar is free to receive new commands (or is dead).
28 static void turn_over();
30 /* Helper to turn_over() to determine whether a map object's action effort has
31 * reached its end. The simplicity of just comparing map_object->progress to
32 * moa->effort is suspended for actor movement, for in this case the effort
33 * depends on the diagonal movement penalty expressed in the ratio of
34 * world.map.dist_diagonal / world.map.dist_orthogonal. (Movement being diagonal
35 * or orthogonal is determined by the ->arg char encoding an even or un-even
38 static uint8_t is_effort_finished(struct MapObjAct * moa,
39 struct MapObj * map_object);
41 /* If "msg"'s first part matches "command_name", set player's MapObj's .command
42 * to the command's id and its .arg to a numerical value following in the latter
43 * part of "msg" (if no digits are found, use 0); then finish player's turn and
44 * turn game over to the NPCs via turn_over(); then return 1. Else, return 0.
46 static uint8_t apply_player_command(char * msg, char * command_name);
48 /* Compares first line of file at world.path_out to world.server_test, aborts if
49 * they don't match, but not before unsetting the flags deleting files in the
50 * server directory, for in that case those must be assumed to belong to another
53 static void server_test();
57 static void turn_over()
59 struct MapObj * player = get_player();
60 struct MapObj * map_object = player;
61 uint16_t start_turn = world.turn;
62 while ( 0 < player->lifepoints
63 || (0 == player->lifepoints && start_turn == world.turn))
65 if (NULL == map_object)
68 map_object = world.map_objs;
70 if (0 < map_object->lifepoints)
72 if (0 == map_object->command)
74 if (map_object == player)
80 map_object->progress++;
81 struct MapObjAct * moa = world.map_obj_acts;
82 while (moa->id != map_object->command)
86 if (is_effort_finished(moa, map_object))
88 moa->func(map_object);
89 map_object->command = 0;
90 map_object->progress = 0;
93 map_object = map_object->next;
99 static uint8_t is_effort_finished(struct MapObjAct * moa,
100 struct MapObj * map_object)
102 if (moa->func != actor_move)
104 if (map_object->progress == moa->effort)
109 else if (strchr("8624", map_object->arg))
111 if (map_object->progress == moa->effort)
116 else if (strchr("1379", map_object->arg))
118 uint16_t diagonal_effort = (moa->effort * world.map.dist_diagonal)
119 / world.map.dist_orthogonal;
120 if (map_object->progress == diagonal_effort)
130 static uint8_t apply_player_command(char * msg, char * command_name)
132 if (!strncmp(msg, command_name, strlen(command_name)))
134 struct MapObj * player = get_player();
135 player->arg = atoi(&(msg[strlen(command_name)]));
136 player->command = get_moa_id_by_name(command_name);
145 static void server_test()
147 char * f_name = "server_test()";
148 char test[10 + 1 + 10 + 1 + 1];
149 FILE * file = try_fopen(world.path_out, "r", f_name);
150 try_fgets(test, 10 + 10 + 1 + 1, file, f_name);
151 try_fclose(file, f_name);
152 if (strcmp(test, world.server_test))
154 unset_cleanup_flag(CLEANUP_WORLDSTATE);
155 unset_cleanup_flag(CLEANUP_OUT);
156 unset_cleanup_flag(CLEANUP_IN);
157 char * msg = "Server test string in server output file does not match. "
158 "This indicates that the current server process has been "
159 "superseded by another one.";
166 extern void obey_msg(char * msg, uint8_t do_record)
168 char * f_name = "obey_msg()";
169 if ( apply_player_command(msg, "wait") /* TODO: Check for non-error */
170 || apply_player_command(msg, "move") /* return value of a modified */
171 || apply_player_command(msg, "pick_up")/* get_moa_id_by_name(); if id */
172 || apply_player_command(msg, "drop") /* found, execute on it what's */
173 || apply_player_command(msg, "use")); /* in apply_player_command(). */
176 char * seed_command = "seed";
177 if (!strncmp(msg, seed_command, strlen(seed_command)))
179 remake_world(atoi(&(msg[strlen(seed_command)])));
184 char path_tmp[strlen(world.path_record) + strlen(world.tmp_suffix) + 1];
185 sprintf(path_tmp, "%s%s", world.path_record, world.tmp_suffix);
186 FILE * file_tmp = try_fopen(path_tmp, "w", f_name);
187 if (!access(world.path_record, F_OK))
189 FILE * file_read = try_fopen(world.path_record, "r", f_name);
190 uint32_t linemax = textfile_width(file_read);
191 char line[linemax + 1];
192 while (try_fgets(line, linemax + 1, file_read, f_name))
194 try_fwrite(line, strlen(line), 1, file_tmp, f_name);
196 try_fclose(file_read, f_name);
198 try_fwrite(msg, strlen(msg), 1, file_tmp, f_name);
199 try_fputc('\n', file_tmp, f_name);
200 try_fclose_unlink_rename(file_tmp, path_tmp, world.path_record, f_name);
206 extern uint8_t io_loop()
208 char * f_name = "io_loop()";
212 char * msg = io_round();
217 if (world.is_verbose)
219 exit_trouble(-1 == printf("Input: %s\n", msg), f_name, "printf()");
221 if (!strcmp("QUIT", msg))
226 if (!strcmp("PING", msg))
229 char * pong = "PONG\n";
230 try_fwrite(pong, strlen(pong), 1, world.file_out, f_name);
231 fflush(world.file_out);