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