home · contact · privacy
Add command to let the AI decide player's next move.
[plomrogue] / src / server / run.c
1 /* src/server/run.c */
2
3 #define _POSIX_C_SOURCE 200809L
4 #include "run.h"
5 #include <stddef.h> /* NULL */
6 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
7 #include <stdio.h> /* FILE, printf(), fflush() */
8 #include <stdlib.h> /* free() */
9 #include <string.h> /* strlen(), strcmp(), strncmp(), strdup() */
10 #include <time.h> /* time_t, time() */
11 #include <unistd.h> /* access() */
12 #include "../common/parse_file.h" /* set_err_line_options(), token_from_line(),
13                                    * err_line(), err_line_inc(), parse_val()
14                                    */
15 #include "../common/readwrite.h" /* try_fopen(), try_fcose(), try_fwrite(),
16                                   * try_fgets(), textfile_width(), try_fputc(),
17                                   * atomic_write_finish(), build_temp_path()
18                                   */
19 #include "../common/rexit.h" /* exit_trouble(), exit_err() */
20 #include "../common/try_malloc.h" /* try_malloc() */
21 #include "ai.h" /* ai() */
22 #include "cleanup.h" /* unset_cleanup_flag() */
23 #include "god_commands.h" /* parse_god_command_1arg(),parse_god_command_2arg()*/
24 #include "hardcoded_strings.h" /* s */
25 #include "io.h" /* io_round(), save_world() */
26 #include "things.h" /* Thing, get_thing_action_id_by_name(), get_player() */
27 #include "world.h" /* world */
28
29
30
31 /* If "string" and "comparand" match in string, set "c_to_set" to value."  */
32 static uint8_t set_char_by_string_comparison(char * string, char * comparand,
33                                              char * c_to_set, char value);
34
35 /* Return 1 on world.exists, else 0 and err_line() appropriate error message. */
36 static uint8_t player_commands_allowed();
37
38 /* Parse player command "tok0" with no argument to player action. */
39 static uint8_t parse_player_command_0arg(char * tok0);
40
41 /* Parse player command "tok0" with one argument "tok1" to player action. */
42 static uint8_t parse_player_command_1arg(char * tok0, char * tok1);
43
44 /* Parse/apply command "tok0" (read further tokens as necessary). */
45 static uint8_t parse_command(char * tok0);
46
47 /* Compares first line of server out file to world.server_test, aborts if they
48  * don't match, but not before unsetting the flags deleting files in the server
49  * directory, for in that case those must be assumed to belong to another server
50  * process.
51  */
52 static void server_test();
53
54 /* Run the game world and its inhabitants (and their actions) until the player
55  * avatar is free to receive new commands (or is dead).
56  */
57 static void turn_over();
58
59
60
61 static uint8_t set_char_by_string_comparison(char * string, char * comparand,
62                                              char * c_to_set, char value)
63 {
64     if (!strcmp(string, comparand))
65     {
66         * c_to_set = value;
67         return 1;
68     }
69     return 0;
70 }
71
72
73
74 static uint8_t player_commands_allowed()
75 {
76     if (!world.exists)
77     {
78         err_line(1, "No world exists in which to run player commands.");
79         return 0;
80     }
81     return 1;
82 }
83
84
85
86 static uint8_t parse_player_command_0arg(char * tok0)
87 {
88     struct Thing * player = get_player();
89     if (   !strcmp(tok0, s[S_CMD_WAIT]) || !strcmp(tok0, s[S_CMD_PICKUP])
90         || !strcmp(tok0, s[S_CMD_AI]))
91     {
92         if (player_commands_allowed())
93         {
94             if (!strcmp(tok0, s[S_CMD_AI]))
95             {
96                 ai(player);
97             }
98             else
99             {
100                 player->command = get_thing_action_id_by_name(tok0);
101                 player->arg = 0;
102             }
103             turn_over();
104         }
105         return 1;
106     }
107     return 0;
108 }
109
110
111
112 static uint8_t parse_player_command_1arg(char * tok0, char * tok1)
113 {
114     struct Thing * player = get_player();
115     if (   (   parse_val(tok0, tok1, s[S_CMD_DROP], '8', (char *) &player->arg)
116             || parse_val(tok0, tok1, s[S_CMD_USE], '8', (char *) &player->arg))
117         && player_commands_allowed())
118     {
119         player->command = get_thing_action_id_by_name(tok0);
120         turn_over();
121     }
122     else if (!strcmp(tok0, s[S_CMD_MOVE]) && player_commands_allowed())
123     {
124         char dir = '\0';
125         if (!(   set_char_by_string_comparison(tok1, "east",       &dir, 'd')
126               || set_char_by_string_comparison(tok1, "south-east", &dir, 'c')
127               || set_char_by_string_comparison(tok1, "south-west", &dir, 'x')
128               || set_char_by_string_comparison(tok1, "west",       &dir, 's')
129               || set_char_by_string_comparison(tok1, "north-west", &dir, 'w')
130               || set_char_by_string_comparison(tok1, "north-east", &dir, 'e')))
131         {
132             return 0;
133         }
134         player->arg = dir;
135         player->command = get_thing_action_id_by_name(tok0);
136         turn_over();
137     }
138     else
139     {
140         return 0;
141     }
142     return 1;
143 }
144
145
146
147 static uint8_t parse_command(char * tok0)
148 {
149     if (parse_player_command_0arg(tok0))
150     {
151         return 1;
152     }
153     else
154     {
155         char * tok1 = token_from_line(NULL);
156         if (tok1 && (   parse_player_command_1arg(tok0, tok1)
157                      || parse_god_command_1arg(tok0, tok1)))
158         {
159             return 1;
160         }
161         else
162         {
163             char * tok2 = token_from_line(NULL);
164             if (tok2 && parse_god_command_2arg(tok0, tok1, tok2))
165             {
166                 return 1;
167             }
168         }
169     }
170     return 0;
171 }
172
173
174
175 static void server_test()
176 {
177     char test[10 + 1 + 10 + 1 + 1];
178     FILE * file = try_fopen(s[S_PATH_OUT], "r", __func__);
179     try_fgets(test, 10 + 10 + 1 + 1, file, __func__);
180     try_fclose(file, __func__);
181     if (strcmp(test, world.server_test))
182     {
183         unset_cleanup_flag(CLEANUP_WORLDSTATE);
184         unset_cleanup_flag(CLEANUP_OUT);
185         unset_cleanup_flag(CLEANUP_IN);
186         char * msg = "Server test string in server output file does not match. "
187                      "This indicates that the current server process has been "
188                      "superseded by another one.";
189         exit_err(1, msg);
190     }
191 }
192
193
194
195 static void turn_over()
196 {
197     struct Thing * player = get_player();
198     struct Thing * thing = player;
199     uint16_t start_turn = world.turn;
200     while (    0 < player->lifepoints
201            || (0 == player->lifepoints && start_turn == world.turn))
202     {
203         if (NULL == thing)
204         {
205             world.turn++;
206             thing = world.things;
207         }
208         if (0 < thing->lifepoints)
209         {
210             if (0 == thing->command)
211             {
212                 if (thing == player)
213                 {
214                     break;
215                 }
216                 ai(thing);
217             }
218             thing->progress++;
219             struct ThingAction * ta = get_thing_action(thing->command);
220             if (thing->progress == ta->effort)
221             {
222                 ta->func(thing);
223                 thing->command = 0;
224                 thing->progress = 0;
225             }
226         }
227         thing = thing->next;
228     }
229 }
230
231
232
233 extern void record(char * msg, uint8_t force)
234 {
235     static FILE * file_tmp = NULL;
236     static time_t save_wait = 0;
237     static char * path_tmp;
238     if (!file_tmp)
239     {
240         path_tmp = build_temp_path(s[S_PATH_RECORD]);
241         file_tmp = try_fopen(path_tmp, "w", __func__);
242         if (!access(s[S_PATH_RECORD], F_OK))
243         {
244             FILE * file_read = try_fopen(s[S_PATH_RECORD], "r", __func__);
245             uint32_t linemax = textfile_width(file_read);
246             char * line = try_malloc(linemax + 1, __func__);
247             while (try_fgets(line, linemax + 1, file_read, __func__))
248             {
249                 try_fwrite(line, strlen(line), 1, file_tmp, __func__);
250             }
251             free(line);
252             try_fclose(file_read, __func__);
253         }
254     }
255     if (msg)
256     {
257         try_fwrite(msg, strlen(msg), 1, file_tmp, __func__);
258         try_fputc('\n', file_tmp, __func__);
259     }
260     if (force || time(NULL) > save_wait + 15)
261     {
262         save_wait = time(NULL);
263         save_world();
264         atomic_write_finish(file_tmp, s[S_PATH_RECORD], path_tmp);
265         file_tmp = NULL;
266     }
267 }
268
269
270
271 extern void obey_msg(char * msg, uint8_t do_record, uint8_t do_verbose)
272 {
273     if (world.is_verbose && do_verbose)
274     {
275         exit_trouble(-1 == printf("Input: %s\n", msg), __func__, "printf");
276     }
277     set_err_line_options("Trouble with message: ", msg, 0);
278     char * msg_copy = strdup(msg);
279     char * tok0 = token_from_line(msg_copy);
280     if (NULL != tok0)
281     {
282
283         if (parse_command(tok0))
284         {
285             if (world.exists)
286             {
287                 world.do_update = 1;
288             }
289             if (do_record)
290             {
291                 record(msg, 0);
292             }
293             char * tokplus = token_from_line(NULL);
294             err_line(NULL != tokplus, "Too many arguments, ignoring overflow.");
295             free(msg_copy);
296             return;
297         }
298     }
299     err_line(1, "Unknown command/argument or bad number of tokens.");
300     free(msg_copy);
301 }
302
303
304
305 extern uint8_t io_loop()
306 {
307     while (1)
308     {
309         char * msg = io_round();
310         server_test();
311         if (NULL == msg)
312         {
313             continue;
314         }
315         if (world.is_verbose)
316         {
317             exit_trouble(-1 == printf("Input: %s\n", msg), __func__, "printf");
318         }
319         if (!strcmp("QUIT", msg))
320         {
321             free(msg);
322             return 1;
323         }
324         if (!strcmp("PING", msg))
325         {
326             free(msg);
327             char * pong = "PONG\n";
328             try_fwrite(pong, strlen(pong), 1, world.file_out, __func__);
329             fflush(world.file_out);
330             continue;
331         }
332         if (world.replay)
333         {
334             free(msg);
335             return 0;
336         }
337         obey_msg(msg, 1, 0);
338         free(msg);
339     }
340 }