home · contact · privacy
Send log messages through server out file. Includes major refactoring.
[plomrogue] / src / server / run.c
1 /* src/server/run.c
2  *
3  * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4  * or any later version. For details on its copyright, license, and warranties,
5  * see the file NOTICE in the root directory of the PlomRogue source package.
6  */
7
8 #define _POSIX_C_SOURCE 200809L /* strdup() */
9 #include "run.h"
10 #include <stddef.h> /* NULL */
11 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, int16_t */
12 #include <stdio.h> /* FILE, printf(), fflush() */
13 #include <stdlib.h> /* free() */
14 #include <string.h> /* strlen(), strcmp(), strncmp(), strdup() */
15 #include <time.h> /* time_t, time() */
16 #include <unistd.h> /* access() */
17 #include "../common/parse_file.h" /* set_err_line_options(), token_from_line(),
18                                    * err_line(), err_line_inc(), parse_val()
19                                    */
20 #include "../common/readwrite.h" /* try_fopen(), try_fcose(), try_fwrite(),
21                                   * try_fgets(), textfile_width(), try_fputc(),
22                                   * atomic_write_finish(), build_temp_path()
23                                   */
24 #include "../common/rexit.h" /* exit_trouble(), exit_err() */
25 #include "../common/try_malloc.h" /* try_malloc() */
26 #include "ai.h" /* ai() */
27 #include "cleanup.h" /* unset_cleanup_flag() */
28 #include "god_commands.h" /* parse_god_command_(1|2|3)arg() */
29 #include "hardcoded_strings.h" /* s */
30 #include "io.h" /* io_round(), save_world() */
31 #include "things.h" /* Thing, ThingType, get_thing_action_id_by_name(),
32                      * get_player(), try_thing_proliferation()
33                      */
34 #include "world.h" /* world */
35
36
37
38 /* If "string" and "comparand" match in string, set "c_to_set" to value."  */
39 static uint8_t set_char_by_string_comparison(char * string, char * comparand,
40                                              char * c_to_set, char value);
41
42 /* Return 1 on world.exists, else 0 and err_line() appropriate error message. */
43 static uint8_t player_commands_allowed();
44
45 /* Parse player command "tok0" with no argument to player action. */
46 static uint8_t parse_player_command_0arg(char * tok0);
47
48 /* Parse player command "tok0" with one argument "tok1" to player action. */
49 static uint8_t parse_player_command_1arg(char * tok0, char * tok1);
50
51 /* Parse/apply command "tok0" (read further tokens as necessary). */
52 static uint8_t parse_command(char * tok0);
53
54 /* Compares first line of server out file to world.server_test, aborts if they
55  * don't match, but not before unsetting the flags deleting files in the server
56  * directory, for in that case those must be assumed to belong to another server
57  * process.
58  */
59 static void server_test();
60
61 /* Return array of IDs of non-owned things in game world, ended by non-ID -1. */
62 static int16_t * build_whitelist();
63
64 /* Return 1 if value of "id" appears in "whitelist", else 0. */
65 static uint8_t thing_in_whitelist(uint8_t id, int16_t * whitelist);
66
67 /* Run the game world and its inhabitants (and their actions) until the player
68  * avatar is free to receive new commands (or is dead). Only actions and
69  * proliferations for non-owned things are performed that exist at the start of
70  * the turn jumped into, or started anew by the cycle.
71  */
72 static void turn_over();
73
74 /* Try to read "msg" as meta command, act accordingly; on success, free it. */
75 static uint8_t meta_commands(char * msg);
76
77
78
79 static uint8_t set_char_by_string_comparison(char * string, char * comparand,
80                                              char * c_to_set, char value)
81 {
82     if (!strcmp(string, comparand))
83     {
84         * c_to_set = value;
85         return 1;
86     }
87     return 0;
88 }
89
90
91
92 static uint8_t player_commands_allowed()
93 {
94     if (!world.exists)
95     {
96         return !err_line(1, "No world exists in which to run player commands.");
97     }
98     return 1;
99 }
100
101
102
103 static uint8_t parse_player_command_0arg(char * tok0)
104 {
105     struct Thing * player = get_player();
106     if (   !strcmp(tok0, s[S_CMD_WAIT]) || !strcmp(tok0, s[S_CMD_PICKUP])
107         || !strcmp(tok0, s[S_CMD_AI]))
108     {
109         if (player_commands_allowed())
110         {
111             if (!strcmp(tok0, s[S_CMD_AI]))
112             {
113                 ai(player);
114             }
115             else
116             {
117                 player->command = get_thing_action_id_by_name(tok0);
118                 player->arg = 0;
119             }
120             turn_over();
121         }
122         return 1;
123     }
124     return 0;
125 }
126
127
128
129 static uint8_t parse_player_command_1arg(char * tok0, char * tok1)
130 {
131     struct Thing * player = get_player();
132     if (   (   parse_val(tok0, tok1, s[S_CMD_DROP], '8', (char *) &player->arg)
133             || parse_val(tok0, tok1, s[S_CMD_USE], '8', (char *) &player->arg))
134         && player_commands_allowed())
135     {
136         player->command = get_thing_action_id_by_name(tok0);
137         turn_over();
138     }
139     else if (!strcmp(tok0, s[S_CMD_MOVE]) && player_commands_allowed())
140     {
141         char dir = '\0';
142         if (!(   set_char_by_string_comparison(tok1, "east",       &dir, 'd')
143               || set_char_by_string_comparison(tok1, "south-east", &dir, 'c')
144               || set_char_by_string_comparison(tok1, "south-west", &dir, 'x')
145               || set_char_by_string_comparison(tok1, "west",       &dir, 's')
146               || set_char_by_string_comparison(tok1, "north-west", &dir, 'w')
147               || set_char_by_string_comparison(tok1, "north-east", &dir, 'e')))
148         {
149             return 0;
150         }
151         player->arg = dir;
152         player->command = get_thing_action_id_by_name(tok0);
153         turn_over();
154     }
155     else
156     {
157         return 0;
158     }
159     return 1;
160 }
161
162
163
164 static uint8_t parse_command(char * tok0)
165 {
166     if (parse_player_command_0arg(tok0))
167     {
168         return 1;
169     }
170     else
171     {
172         char * tok1 = token_from_line(NULL);
173         if (tok1 && (   parse_player_command_1arg(tok0, tok1)
174                      || parse_god_command_1arg(tok0, tok1)))
175         {
176             return 1;
177         }
178         else
179         {
180             char * tok2 = token_from_line(NULL);
181             if (tok2 && parse_god_command_2arg(tok0, tok1, tok2))
182             {
183                 return 1;
184             }
185             else
186             {
187                 char * tok3 = token_from_line(NULL);
188                 if (tok2 && parse_god_command_3arg(tok0, tok1, tok2, tok3))
189                 {
190                     return 1;
191                 }
192             }
193         }
194     }
195     return 0;
196 }
197
198
199
200 static void server_test()
201 {
202     char test[10 + 1 + 10 + 1 + 1];
203     FILE * file = try_fopen(s[S_PATH_OUT], "r", __func__);
204     try_fgets(test, 10 + 10 + 1 + 1, file, __func__);
205     try_fclose(file, __func__);
206     if (strcmp(test, world.server_test))
207     {
208         unset_cleanup_flag(CLEANUP_WORLDSTATE);
209         unset_cleanup_flag(CLEANUP_OUT);
210         unset_cleanup_flag(CLEANUP_IN);
211         char * msg = "Server test string in server output file does not match. "
212                      "This indicates that the current server process has been "
213                      "superseded by another one.";
214         exit_err(1, msg);
215     }
216 }
217
218
219
220 static int16_t * build_whitelist()
221 {
222     uint16_t i_things = NULL != world.things;
223     struct Thing * t = world.things;
224     for (; t; t = t->next, i_things++);
225     int16_t * whitelist = try_malloc(i_things * sizeof(int16_t), __func__);
226     for (i_things = 0, t = world.things; t;
227          whitelist[i_things] = t->id, t = t->next, i_things++);
228     whitelist[i_things] = -1;
229     return whitelist;
230 }
231
232
233
234 static uint8_t thing_in_whitelist(uint8_t id, int16_t * whitelist)
235 {
236     uint16_t i;
237     for (i = 0; -1 < whitelist[i]; i++)
238     {
239         if ((int16_t) id == whitelist[i])
240         {
241             return 1;
242         }
243     }
244     return 0;
245 }
246
247
248
249 static void turn_over()
250 {
251     struct Thing * player = get_player();
252     struct Thing * thing = player;
253     int16_t * whitelist = build_whitelist();
254     while (0 < player->lifepoints)
255     {
256         if (!thing)
257         {
258             world.turn++;
259             thing = world.things;
260             free(whitelist);
261             whitelist = build_whitelist();/* The whitelist excludes things    */
262         }                                 /* that appear only during the turn.*/
263         if (thing_in_whitelist(thing->id, whitelist))
264         {
265             if (0 < thing->lifepoints)
266             {
267                 if (0 == thing->command)
268                 {
269                     if (thing == player)
270                     {
271                         break;
272                     }
273                     ai(thing);
274                 }
275                 thing->progress++;
276                 struct ThingAction * ta = get_thing_action(thing->command);
277                 if (thing->progress == ta->effort)
278                 {
279                     ta->func(thing);
280                     thing->command = 0;
281                     thing->progress = 0;
282                 }
283             }
284             try_thing_proliferation(thing);
285         }
286         thing = thing->next;
287     }
288     free(whitelist);
289 }
290
291
292
293 static uint8_t meta_commands(char * msg)
294 {
295     if (!strcmp("QUIT", msg))
296     {
297         free(msg);
298         return 2;
299     }
300     if (!strcmp("PING", msg))
301     {
302         free(msg);
303         send_to_outfile("PONG\n");
304         return 1;
305     }
306     if (!strcmp("STACK", msg))
307     {
308         free(msg);
309         send_to_outfile("THINGS_BELOW_PLAYER START\n");
310         struct Thing * player = get_player();
311         struct Thing * t;
312         for (t = world.things; t; t = t->next)
313         {
314             if (   t->pos.y == player->pos.y && t->pos.x == player->pos.x
315                 && t != player)
316             {
317                 struct ThingType * tt = get_thing_type(t->type);
318                 send_to_outfile(tt->name);
319                 send_to_outfile("\n");
320             }
321         }
322         send_to_outfile("THINGS_BELOW_PLAYER END\n");
323         return 1;
324     }
325     return 0;
326 }
327
328
329
330 extern void send_to_outfile(char * answer)
331 {
332     try_fwrite(answer, strlen(answer), 1, world.file_out, __func__);
333     fflush(world.file_out);
334 }
335
336
337
338 extern void record(char * msg, uint8_t force)
339 {
340     static FILE * file_tmp = NULL;
341     static time_t save_wait = 0;
342     static char * path_tmp;
343     if (!file_tmp)
344     {
345         path_tmp = build_temp_path(s[S_PATH_RECORD]);
346         file_tmp = try_fopen(path_tmp, "w", __func__);
347         if (!access(s[S_PATH_RECORD], F_OK))
348         {
349             FILE * file_read = try_fopen(s[S_PATH_RECORD], "r", __func__);
350             uint32_t linemax = textfile_width(file_read);
351             char * line = try_malloc(linemax + 1, __func__);
352             while (try_fgets(line, linemax + 1, file_read, __func__))
353             {
354                 try_fwrite(line, strlen(line), 1, file_tmp, __func__);
355             }
356             free(line);
357             try_fclose(file_read, __func__);
358         }
359     }
360     if (msg)
361     {
362         try_fwrite(msg, strlen(msg), 1, file_tmp, __func__);
363         try_fputc('\n', file_tmp, __func__);
364     }
365     if (force || time(NULL) > save_wait + 15)
366     {
367         save_wait = time(NULL);
368         save_world();
369         atomic_write_finish(file_tmp, s[S_PATH_RECORD], path_tmp);
370         file_tmp = NULL;
371     }
372 }
373
374
375
376 extern void obey_msg(char * msg, uint8_t do_record, uint8_t do_verbose)
377 {
378     if (world.is_verbose && do_verbose)
379     {
380         exit_trouble(-1 == printf("Input: %s\n", msg), __func__, "printf");
381     }
382     set_err_line_options("Trouble with message: ", msg, 0);
383     char * msg_copy = strdup(msg);
384     char * tok0 = token_from_line(msg_copy);
385     if (tok0)
386     {
387
388         if (parse_command(tok0))
389         {
390             if (world.exists)
391             {
392                 world.do_update = 1;
393             }
394             if (do_record)
395             {
396                 record(msg, 0);
397             }
398             char * tokplus = token_from_line(NULL);
399             err_line(!(!tokplus), "Too many arguments, ignoring overflow.");
400             free(msg_copy);
401             return;
402         }
403     }
404     err_line(1, "Unknown command/argument or bad number of tokens.");
405     free(msg_copy);
406 }
407
408
409
410 extern uint8_t io_loop()
411 {
412     while (1)
413     {
414         char * msg = io_round();
415         server_test();
416         if (!msg)
417         {
418             continue;
419         }
420         if (world.is_verbose)
421         {
422             exit_trouble(-1 == printf("Input: %s\n", msg), __func__, "printf");
423         }
424         uint8_t test = meta_commands(msg);
425         if (test)
426         {
427             if (2 == test)
428             {
429                 return 1;
430             }
431             continue;
432         }
433         if (world.replay)
434         {
435             free(msg);
436             return 0;
437         }
438         obey_msg(msg, 1, 0);
439         free(msg);
440     }
441 }