4 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
5 #include <stdio.h> /* FILE, sprintf() */
6 #include <stdlib.h> /* free() */
7 #include <string.h> /* strlen(), strncmp(), atoi() */
8 #include <unistd.h> /* access() */
9 #include "../common/readwrite.h" /* try_fopen(), try_fcose(), try_fwrite(),
10 * try_fgets(), try_fclose_unlink_rename(),
11 * textfile_sizes(), try_fputc()
13 #include "../common/rexit.h" /* exit_trouble() */
14 #include "ai.h" /* pretty_dumb_ai() */
15 #include "init.h" /* remake_world() */
16 #include "io.h" /* io_round() */
17 #include "map_object_actions.h" /* get_moa_id_by_name() */
18 #include "map_objects.h" /* struct MapObj, get_player() */
19 #include "world.h" /* global world */
23 /* Run the game world and its inhabitants (and their actions) until the player
24 * avatar is free to receive new commands (or is dead).
26 static void turn_over();
28 /* If "msg"'s first part matches "command_name", set player's MapObj's .command
29 * to the command's id and its .arg to a numerical value following in the latter
30 * part of "msg" (if no digits are found, use 0); then finish player's turn and
31 * turn game over to the NPCs via turn_over(); then return 1. Else, return 0.
33 static uint8_t apply_player_command(char * msg, char * command_name);
37 static void turn_over()
39 struct MapObj * player = get_player();
40 struct MapObj * map_object = player;
41 uint16_t start_turn = world.turn;
42 uint8_t first_round = 1;
43 while ( 0 < player->lifepoints
44 || (0 == player->lifepoints && start_turn == world.turn))
46 if (NULL == map_object)
49 map_object = world.map_objs;
51 if (0 < map_object->lifepoints)
53 if (0 == first_round && 0 == map_object->progress)
55 if (map_object == player)
59 pretty_dumb_ai(map_object);
62 map_object->progress++;
63 struct MapObjAct * moa = world.map_obj_acts;
64 while (moa->id != map_object->command)
68 if (map_object->progress == moa->effort)
70 moa->func(map_object);
71 map_object->progress = 0;
74 map_object = map_object->next;
80 static uint8_t apply_player_command(char * msg, char * command_name)
82 if (!strncmp(msg, command_name, strlen(command_name)))
84 struct MapObj * player = get_player();
85 player->arg = atoi(&(msg[strlen(command_name)]));
86 player->command = get_moa_id_by_name(command_name);
95 extern void obey_msg(char * msg, uint8_t do_record)
97 char * f_name = "obey_msg()";
98 if ( apply_player_command(msg, "wait") /* TODO: Check for non-error */
99 || apply_player_command(msg, "move") /* return value of a modified */
100 || apply_player_command(msg, "pick_up")/* get_moa_id_by_name(); if id */
101 || apply_player_command(msg, "drop") /* found, execute on it what's */
102 || apply_player_command(msg, "use")); /* in apply_player_command(). */
105 char * seed_command = "seed";
106 if (!strncmp(msg, seed_command, strlen(seed_command)))
108 remake_world(atoi(&(msg[strlen(seed_command)])));
113 char path_tmp[strlen(world.path_record) + strlen(world.tmp_suffix) + 1];
114 sprintf(path_tmp, "%s%s", world.path_record, world.tmp_suffix);
115 FILE * file_tmp = try_fopen(path_tmp, "w", f_name);
116 if (!access(world.path_record, F_OK))
118 FILE * file_read = try_fopen(world.path_record, "r", f_name);
119 uint32_t linemax = textfile_sizes(file_read, NULL);
120 char line[linemax + 1];
121 while (try_fgets(line, linemax + 1, file_read, f_name))
123 try_fwrite(line, strlen(line), 1, file_tmp, f_name);
125 try_fclose(file_read, f_name);
127 try_fwrite(msg, strlen(msg), 1, file_tmp, f_name);
128 try_fputc('\n', file_tmp, f_name);
129 try_fclose_unlink_rename(file_tmp, path_tmp, world.path_record, f_name);
135 extern uint8_t io_loop()
137 char * f_name = "io_loop()";
140 char * msg = io_round();
145 if (world.is_verbose)
147 exit_trouble(-1 == printf("Input: %s\n", msg), f_name, "printf()");
149 if (!strcmp("QUIT", msg))