4 #include <stddef.h> /* NULL */
5 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
6 #include <stdio.h> /* FILE, sprintf() */
7 #include <stdlib.h> /* free() */
8 #include <string.h> /* strlen(), 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_sizes(), try_fputc()
14 #include "../common/rexit.h" /* exit_trouble() */
15 #include "ai.h" /* ai() */
16 #include "init.h" /* remake_world() */
17 #include "io.h" /* io_round() */
18 #include "map_object_actions.h" /* get_moa_id_by_name() */
19 #include "map_objects.h" /* struct MapObj, get_player() */
20 #include "world.h" /* global world */
24 /* Run the game world and its inhabitants (and their actions) until the player
25 * avatar is free to receive new commands (or is dead).
27 static void turn_over();
29 /* Helper to turn_over() to determine whether a map object's action effort has
30 * reached its end. The simplicity of just comparing map_object->progress to
31 * moa->effort is suspended for actor movement, for in this case the effort
32 * depends on the diagonal movement penalty expressed in the ratio of
33 * world.map.dist_diagonal / world.map.dist_orthogonal. (Movement being diagonal
34 * or orthogonal is determined by the ->arg char encoding an even or un-even
37 static uint8_t is_effort_finished(struct MapObjAct * moa,
38 struct MapObj * map_object);
40 /* If "msg"'s first part matches "command_name", set player's MapObj's .command
41 * to the command's id and its .arg to a numerical value following in the latter
42 * part of "msg" (if no digits are found, use 0); then finish player's turn and
43 * turn game over to the NPCs via turn_over(); then return 1. Else, return 0.
45 static uint8_t apply_player_command(char * msg, char * command_name);
49 static void turn_over()
51 struct MapObj * player = get_player();
52 struct MapObj * map_object = player;
53 uint16_t start_turn = world.turn;
54 while ( 0 < player->lifepoints
55 || (0 == player->lifepoints && start_turn == world.turn))
57 if (NULL == map_object)
60 map_object = world.map_objs;
62 if (0 < map_object->lifepoints)
64 if (0 == map_object->command)
66 if (map_object == player)
72 map_object->progress++;
73 struct MapObjAct * moa = world.map_obj_acts;
74 while (moa->id != map_object->command)
78 if (is_effort_finished(moa, map_object))
80 moa->func(map_object);
81 map_object->command = 0;
82 map_object->progress = 0;
85 map_object = map_object->next;
91 static uint8_t is_effort_finished(struct MapObjAct * moa,
92 struct MapObj * map_object)
94 if (moa->func != actor_move)
96 if (map_object->progress == moa->effort)
101 else if (strchr("8624", map_object->arg))
103 if (map_object->progress == moa->effort)
108 else if (strchr("1379", map_object->arg))
110 uint16_t diagonal_effort = (moa->effort * world.map.dist_diagonal)
111 / world.map.dist_orthogonal;
112 if (map_object->progress == diagonal_effort)
122 static uint8_t apply_player_command(char * msg, char * command_name)
124 if (!strncmp(msg, command_name, strlen(command_name)))
126 struct MapObj * player = get_player();
127 player->arg = atoi(&(msg[strlen(command_name)]));
128 player->command = get_moa_id_by_name(command_name);
137 extern void obey_msg(char * msg, uint8_t do_record)
139 char * f_name = "obey_msg()";
140 if ( apply_player_command(msg, "wait") /* TODO: Check for non-error */
141 || apply_player_command(msg, "move") /* return value of a modified */
142 || apply_player_command(msg, "pick_up")/* get_moa_id_by_name(); if id */
143 || apply_player_command(msg, "drop") /* found, execute on it what's */
144 || apply_player_command(msg, "use")); /* in apply_player_command(). */
147 char * seed_command = "seed";
148 if (!strncmp(msg, seed_command, strlen(seed_command)))
150 remake_world(atoi(&(msg[strlen(seed_command)])));
155 char path_tmp[strlen(world.path_record) + strlen(world.tmp_suffix) + 1];
156 sprintf(path_tmp, "%s%s", world.path_record, world.tmp_suffix);
157 FILE * file_tmp = try_fopen(path_tmp, "w", f_name);
158 if (!access(world.path_record, F_OK))
160 FILE * file_read = try_fopen(world.path_record, "r", f_name);
161 uint32_t linemax = textfile_sizes(file_read, NULL);
162 char line[linemax + 1];
163 while (try_fgets(line, linemax + 1, file_read, f_name))
165 try_fwrite(line, strlen(line), 1, file_tmp, f_name);
167 try_fclose(file_read, f_name);
169 try_fwrite(msg, strlen(msg), 1, file_tmp, f_name);
170 try_fputc('\n', file_tmp, f_name);
171 try_fclose_unlink_rename(file_tmp, path_tmp, world.path_record, f_name);
177 extern uint8_t io_loop()
179 char * f_name = "io_loop()";
182 char * msg = io_round();
187 if (world.is_verbose)
189 exit_trouble(-1 == printf("Input: %s\n", msg), f_name, "printf()");
191 if (!strcmp("QUIT", msg))