home · contact · privacy
Server: New command TT_PROLIFERATE sets chance for thing proliferation.
[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, 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, get_thing_action_id_by_name(), get_player(),
32                       * 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
75
76 static uint8_t set_char_by_string_comparison(char * string, char * comparand,
77                                              char * c_to_set, char value)
78 {
79     if (!strcmp(string, comparand))
80     {
81         * c_to_set = value;
82         return 1;
83     }
84     return 0;
85 }
86
87
88
89 static uint8_t player_commands_allowed()
90 {
91     if (!world.exists)
92     {
93         return !err_line(1, "No world exists in which to run player commands.");
94     }
95     return 1;
96 }
97
98
99
100 static uint8_t parse_player_command_0arg(char * tok0)
101 {
102     struct Thing * player = get_player();
103     if (   !strcmp(tok0, s[S_CMD_WAIT]) || !strcmp(tok0, s[S_CMD_PICKUP])
104         || !strcmp(tok0, s[S_CMD_AI]))
105     {
106         if (player_commands_allowed())
107         {
108             if (!strcmp(tok0, s[S_CMD_AI]))
109             {
110                 ai(player);
111             }
112             else
113             {
114                 player->command = get_thing_action_id_by_name(tok0);
115                 player->arg = 0;
116             }
117             turn_over();
118         }
119         return 1;
120     }
121     return 0;
122 }
123
124
125
126 static uint8_t parse_player_command_1arg(char * tok0, char * tok1)
127 {
128     struct Thing * player = get_player();
129     if (   (   parse_val(tok0, tok1, s[S_CMD_DROP], '8', (char *) &player->arg)
130             || parse_val(tok0, tok1, s[S_CMD_USE], '8', (char *) &player->arg))
131         && player_commands_allowed())
132     {
133         player->command = get_thing_action_id_by_name(tok0);
134         turn_over();
135     }
136     else if (!strcmp(tok0, s[S_CMD_MOVE]) && player_commands_allowed())
137     {
138         char dir = '\0';
139         if (!(   set_char_by_string_comparison(tok1, "east",       &dir, 'd')
140               || set_char_by_string_comparison(tok1, "south-east", &dir, 'c')
141               || set_char_by_string_comparison(tok1, "south-west", &dir, 'x')
142               || set_char_by_string_comparison(tok1, "west",       &dir, 's')
143               || set_char_by_string_comparison(tok1, "north-west", &dir, 'w')
144               || set_char_by_string_comparison(tok1, "north-east", &dir, 'e')))
145         {
146             return 0;
147         }
148         player->arg = dir;
149         player->command = get_thing_action_id_by_name(tok0);
150         turn_over();
151     }
152     else
153     {
154         return 0;
155     }
156     return 1;
157 }
158
159
160
161 static uint8_t parse_command(char * tok0)
162 {
163     if (parse_player_command_0arg(tok0))
164     {
165         return 1;
166     }
167     else
168     {
169         char * tok1 = token_from_line(NULL);
170         if (tok1 && (   parse_player_command_1arg(tok0, tok1)
171                      || parse_god_command_1arg(tok0, tok1)))
172         {
173             return 1;
174         }
175         else
176         {
177             char * tok2 = token_from_line(NULL);
178             if (tok2 && parse_god_command_2arg(tok0, tok1, tok2))
179             {
180                 return 1;
181             }
182             else
183             {
184                 char * tok3 = token_from_line(NULL);
185                 if (tok2 && parse_god_command_3arg(tok0, tok1, tok2, tok3))
186                 {
187                     return 1;
188                 }
189             }
190         }
191     }
192     return 0;
193 }
194
195
196
197 static void server_test()
198 {
199     char test[10 + 1 + 10 + 1 + 1];
200     FILE * file = try_fopen(s[S_PATH_OUT], "r", __func__);
201     try_fgets(test, 10 + 10 + 1 + 1, file, __func__);
202     try_fclose(file, __func__);
203     if (strcmp(test, world.server_test))
204     {
205         unset_cleanup_flag(CLEANUP_WORLDSTATE);
206         unset_cleanup_flag(CLEANUP_OUT);
207         unset_cleanup_flag(CLEANUP_IN);
208         char * msg = "Server test string in server output file does not match. "
209                      "This indicates that the current server process has been "
210                      "superseded by another one.";
211         exit_err(1, msg);
212     }
213 }
214
215
216
217 static int16_t * build_whitelist()
218 {
219     uint16_t i_things = NULL != world.things;
220     struct Thing * t = world.things;
221     for (; t; t = t->next, i_things++);
222     int16_t * whitelist = try_malloc(i_things * sizeof(int16_t), __func__);
223     for (i_things = 0, t = world.things; t;
224          whitelist[i_things] = t->id, t = t->next, i_things++)
225     whitelist[i_things] = -1;
226     return whitelist;
227 }
228
229
230
231 static uint8_t thing_in_whitelist(uint8_t id, int16_t * whitelist)
232 {
233     int16_t i;
234     for (i = 0; -1 < whitelist[i]; i++)
235     {
236         if ((int16_t) id == whitelist[i])
237         {
238             return 1;
239         }
240     }
241     return 0;
242 }
243
244
245
246 static void turn_over()
247 {
248     struct Thing * player = get_player();
249     struct Thing * thing = player;
250     uint16_t start_turn = world.turn;
251     int16_t * whitelist = build_whitelist();
252     while (    0 < player->lifepoints
253            || (0 == player->lifepoints && start_turn == world.turn))
254     {             /* TODO: check meaning and refactorability of 2nd condition */
255         if (!thing)
256         {
257             world.turn++;
258             thing = world.things;
259             free(whitelist);
260             whitelist = build_whitelist();
261         }
262         if (thing_in_whitelist(thing->id, whitelist))
263         {
264             if (0 < thing->lifepoints)
265             {
266                 if (0 == thing->command)
267                 {
268                     if (thing == player)
269                     {
270                         break;
271                     }
272                     ai(thing);
273                 }
274                 thing->progress++;
275                 struct ThingAction * ta = get_thing_action(thing->command);
276                 if (thing->progress == ta->effort)
277                 {
278                     ta->func(thing);
279                     thing->command = 0;
280                     thing->progress = 0;
281                 }
282             }
283             try_thing_proliferation(thing);
284         }
285         thing = thing->next;
286     }
287     free(whitelist);
288 }
289
290
291
292 extern void record(char * msg, uint8_t force)
293 {
294     static FILE * file_tmp = NULL;
295     static time_t save_wait = 0;
296     static char * path_tmp;
297     if (!file_tmp)
298     {
299         path_tmp = build_temp_path(s[S_PATH_RECORD]);
300         file_tmp = try_fopen(path_tmp, "w", __func__);
301         if (!access(s[S_PATH_RECORD], F_OK))
302         {
303             FILE * file_read = try_fopen(s[S_PATH_RECORD], "r", __func__);
304             uint32_t linemax = textfile_width(file_read);
305             char * line = try_malloc(linemax + 1, __func__);
306             while (try_fgets(line, linemax + 1, file_read, __func__))
307             {
308                 try_fwrite(line, strlen(line), 1, file_tmp, __func__);
309             }
310             free(line);
311             try_fclose(file_read, __func__);
312         }
313     }
314     if (msg)
315     {
316         try_fwrite(msg, strlen(msg), 1, file_tmp, __func__);
317         try_fputc('\n', file_tmp, __func__);
318     }
319     if (force || time(NULL) > save_wait + 15)
320     {
321         save_wait = time(NULL);
322         save_world();
323         atomic_write_finish(file_tmp, s[S_PATH_RECORD], path_tmp);
324         file_tmp = NULL;
325     }
326 }
327
328
329
330 extern void obey_msg(char * msg, uint8_t do_record, uint8_t do_verbose)
331 {
332     if (world.is_verbose && do_verbose)
333     {
334         exit_trouble(-1 == printf("Input: %s\n", msg), __func__, "printf");
335     }
336     set_err_line_options("Trouble with message: ", msg, 0);
337     char * msg_copy = strdup(msg);
338     char * tok0 = token_from_line(msg_copy);
339     if (tok0)
340     {
341
342         if (parse_command(tok0))
343         {
344             if (world.exists)
345             {
346                 world.do_update = 1;
347             }
348             if (do_record)
349             {
350                 record(msg, 0);
351             }
352             char * tokplus = token_from_line(NULL);
353             err_line(!(!tokplus), "Too many arguments, ignoring overflow.");
354             free(msg_copy);
355             return;
356         }
357     }
358     err_line(1, "Unknown command/argument or bad number of tokens.");
359     free(msg_copy);
360 }
361
362
363
364 extern uint8_t io_loop()
365 {
366     while (1)
367     {
368         char * msg = io_round();
369         server_test();
370         if (!msg)
371         {
372             continue;
373         }
374         if (world.is_verbose)
375         {
376             exit_trouble(-1 == printf("Input: %s\n", msg), __func__, "printf");
377         }
378         if (!strcmp("QUIT", msg))
379         {
380             free(msg);
381             return 1;
382         }
383         if (!strcmp("PING", msg))
384         {
385             free(msg);
386             char * pong = "PONG\n";
387             try_fwrite(pong, strlen(pong), 1, world.file_out, __func__);
388             fflush(world.file_out);
389             continue;
390         }
391         if (world.replay)
392         {
393             free(msg);
394             return 0;
395         }
396         obey_msg(msg, 1, 0);
397         free(msg);
398     }
399 }