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