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