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" /* pretty_dumb_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 /* If "msg"'s first part matches "command_name", set player's MapObj's .command
30 * to the command's id and its .arg to a numerical value following in the latter
31 * part of "msg" (if no digits are found, use 0); then finish player's turn and
32 * turn game over to the NPCs via turn_over(); then return 1. Else, return 0.
34 static uint8_t apply_player_command(char * msg, char * command_name);
38 static void turn_over()
40 struct MapObj * player = get_player();
41 struct MapObj * map_object = player;
42 uint16_t start_turn = world.turn;
43 uint8_t first_round = 1;
44 while ( 0 < player->lifepoints
45 || (0 == player->lifepoints && start_turn == world.turn))
47 if (NULL == map_object)
50 map_object = world.map_objs;
52 if (0 < map_object->lifepoints)
54 if (0 == first_round && 0 == map_object->progress)
56 if (map_object == player)
60 pretty_dumb_ai(map_object);
63 map_object->progress++;
64 struct MapObjAct * moa = world.map_obj_acts;
65 while (moa->id != map_object->command)
69 if (map_object->progress == moa->effort)
71 moa->func(map_object);
72 map_object->progress = 0;
75 map_object = map_object->next;
81 static uint8_t apply_player_command(char * msg, char * command_name)
83 if (!strncmp(msg, command_name, strlen(command_name)))
85 struct MapObj * player = get_player();
86 player->arg = atoi(&(msg[strlen(command_name)]));
87 player->command = get_moa_id_by_name(command_name);
96 extern void obey_msg(char * msg, uint8_t do_record)
98 char * f_name = "obey_msg()";
99 if ( apply_player_command(msg, "wait") /* TODO: Check for non-error */
100 || apply_player_command(msg, "move") /* return value of a modified */
101 || apply_player_command(msg, "pick_up")/* get_moa_id_by_name(); if id */
102 || apply_player_command(msg, "drop") /* found, execute on it what's */
103 || apply_player_command(msg, "use")); /* in apply_player_command(). */
106 char * seed_command = "seed";
107 if (!strncmp(msg, seed_command, strlen(seed_command)))
109 remake_world(atoi(&(msg[strlen(seed_command)])));
114 char path_tmp[strlen(world.path_record) + strlen(world.tmp_suffix) + 1];
115 sprintf(path_tmp, "%s%s", world.path_record, world.tmp_suffix);
116 FILE * file_tmp = try_fopen(path_tmp, "w", f_name);
117 if (!access(world.path_record, F_OK))
119 FILE * file_read = try_fopen(world.path_record, "r", f_name);
120 uint32_t linemax = textfile_sizes(file_read, NULL);
121 char line[linemax + 1];
122 while (try_fgets(line, linemax + 1, file_read, f_name))
124 try_fwrite(line, strlen(line), 1, file_tmp, f_name);
126 try_fclose(file_read, f_name);
128 try_fwrite(msg, strlen(msg), 1, file_tmp, f_name);
129 try_fputc('\n', file_tmp, f_name);
130 try_fclose_unlink_rename(file_tmp, path_tmp, world.path_record, f_name);
136 extern uint8_t io_loop()
138 char * f_name = "io_loop()";
141 char * msg = io_round();
146 if (world.is_verbose)
148 exit_trouble(-1 == printf("Input: %s\n", msg), f_name, "printf()");
150 if (!strcmp("QUIT", msg))