home · contact · privacy
3ae71757fe6ec0568a3bc6f937608846a129db4e
[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     {
91         if (player_commands_allowed())
92         {
93             player->command = get_thing_action_id_by_name(tok0);
94             player->arg = 0;
95             turn_over();
96         }
97         return 1;
98     }
99     return 0;
100 }
101
102
103
104 static uint8_t parse_player_command_1arg(char * tok0, char * tok1)
105 {
106     struct Thing * player = get_player();
107     if (   (   parse_val(tok0, tok1, s[S_CMD_DROP], '8', (char *) &player->arg)
108             || parse_val(tok0, tok1, s[S_CMD_USE], '8', (char *) &player->arg))
109         && player_commands_allowed())
110     {
111         player->command = get_thing_action_id_by_name(tok0);
112         turn_over();
113     }
114     else if (!strcmp(tok0, s[S_CMD_MOVE]) && player_commands_allowed())
115     {
116         char dir = '\0';
117         if (!(   set_char_by_string_comparison(tok1, "east",       &dir, 'd')
118               || set_char_by_string_comparison(tok1, "south-east", &dir, 'c')
119               || set_char_by_string_comparison(tok1, "south-west", &dir, 'x')
120               || set_char_by_string_comparison(tok1, "west",       &dir, 's')
121               || set_char_by_string_comparison(tok1, "north-west", &dir, 'w')
122               || set_char_by_string_comparison(tok1, "north-east", &dir, 'e')))
123         {
124             return 0;
125         }
126         player->arg = dir;
127         player->command = get_thing_action_id_by_name(tok0);
128         turn_over();
129     }
130     else
131     {
132         return 0;
133     }
134     return 1;
135 }
136
137
138
139 static uint8_t parse_command(char * tok0)
140 {
141     if (parse_player_command_0arg(tok0))
142     {
143         return 1;
144     }
145     else
146     {
147         char * tok1 = token_from_line(NULL);
148         if (tok1 && (   parse_player_command_1arg(tok0, tok1)
149                      || parse_god_command_1arg(tok0, tok1)))
150         {
151             return 1;
152         }
153         else
154         {
155             char * tok2 = token_from_line(NULL);
156             if (tok2 && parse_god_command_2arg(tok0, tok1, tok2))
157             {
158                 return 1;
159             }
160         }
161     }
162     return 0;
163 }
164
165
166
167 static void server_test()
168 {
169     char test[10 + 1 + 10 + 1 + 1];
170     FILE * file = try_fopen(s[S_PATH_OUT], "r", __func__);
171     try_fgets(test, 10 + 10 + 1 + 1, file, __func__);
172     try_fclose(file, __func__);
173     if (strcmp(test, world.server_test))
174     {
175         unset_cleanup_flag(CLEANUP_WORLDSTATE);
176         unset_cleanup_flag(CLEANUP_OUT);
177         unset_cleanup_flag(CLEANUP_IN);
178         char * msg = "Server test string in server output file does not match. "
179                      "This indicates that the current server process has been "
180                      "superseded by another one.";
181         exit_err(1, msg);
182     }
183 }
184
185
186
187 static void turn_over()
188 {
189     struct Thing * player = get_player();
190     struct Thing * thing = player;
191     uint16_t start_turn = world.turn;
192     while (    0 < player->lifepoints
193            || (0 == player->lifepoints && start_turn == world.turn))
194     {
195         if (NULL == thing)
196         {
197             world.turn++;
198             thing = world.things;
199         }
200         if (0 < thing->lifepoints)
201         {
202             if (0 == thing->command)
203             {
204                 if (thing == player)
205                 {
206                     break;
207                 }
208                 ai(thing);
209             }
210             thing->progress++;
211             struct ThingAction * ta = get_thing_action(thing->command);
212             if (thing->progress == ta->effort)
213             {
214                 ta->func(thing);
215                 thing->command = 0;
216                 thing->progress = 0;
217             }
218         }
219         thing = thing->next;
220     }
221 }
222
223
224
225 extern void record(char * msg, uint8_t force)
226 {
227     static FILE * file_tmp = NULL;
228     static time_t save_wait = 0;
229     static char * path_tmp;
230     if (!file_tmp)
231     {
232         path_tmp = build_temp_path(s[S_PATH_RECORD]);
233         file_tmp = try_fopen(path_tmp, "w", __func__);
234         if (!access(s[S_PATH_RECORD], F_OK))
235         {
236             FILE * file_read = try_fopen(s[S_PATH_RECORD], "r", __func__);
237             uint32_t linemax = textfile_width(file_read);
238             char * line = try_malloc(linemax + 1, __func__);
239             while (try_fgets(line, linemax + 1, file_read, __func__))
240             {
241                 try_fwrite(line, strlen(line), 1, file_tmp, __func__);
242             }
243             free(line);
244             try_fclose(file_read, __func__);
245         }
246     }
247     if (msg)
248     {
249         try_fwrite(msg, strlen(msg), 1, file_tmp, __func__);
250         try_fputc('\n', file_tmp, __func__);
251     }
252     if (force || time(NULL) > save_wait + 15)
253     {
254         save_wait = time(NULL);
255         save_world();
256         atomic_write_finish(file_tmp, s[S_PATH_RECORD], path_tmp);
257         file_tmp = NULL;
258     }
259 }
260
261
262
263 extern void obey_msg(char * msg, uint8_t do_record, uint8_t do_verbose)
264 {
265     if (world.is_verbose && do_verbose)
266     {
267         exit_trouble(-1 == printf("Input: %s\n", msg), __func__, "printf");
268     }
269     set_err_line_options("Trouble with message: ", msg, 0);
270     char * msg_copy = strdup(msg);
271     char * tok0 = token_from_line(msg_copy);
272     if (NULL != tok0)
273     {
274
275         if (parse_command(tok0))
276         {
277             if (world.exists)
278             {
279                 world.do_update = 1;
280             }
281             if (do_record)
282             {
283                 record(msg, 0);
284             }
285             char * tokplus = token_from_line(NULL);
286             err_line(NULL != tokplus, "Too many arguments, ignoring overflow.");
287             free(msg_copy);
288             return;
289         }
290     }
291     err_line(1, "Unknown command/argument or bad number of tokens.");
292     free(msg_copy);
293 }
294
295
296
297 extern uint8_t io_loop()
298 {
299     while (1)
300     {
301         char * msg = io_round();
302         server_test();
303         if (NULL == msg)
304         {
305             continue;
306         }
307         if (world.is_verbose)
308         {
309             exit_trouble(-1 == printf("Input: %s\n", msg), __func__, "printf");
310         }
311         if (!strcmp("QUIT", msg))
312         {
313             free(msg);
314             return 1;
315         }
316         if (!strcmp("PING", msg))
317         {
318             free(msg);
319             char * pong = "PONG\n";
320             try_fwrite(pong, strlen(pong), 1, world.file_out, __func__);
321             fflush(world.file_out);
322             continue;
323         }
324         if (world.replay)
325         {
326             free(msg);
327             return 0;
328         }
329         obey_msg(msg, 1, 0);
330         free(msg);
331     }
332 }