1 /* src/server/init.c */
3 #define _POSIX_C_SOURCE 2 /* getopt(), optarg */
5 #include <errno.h> /* global errno, EEXIST */
6 #include <stddef.h> /* NULL */
7 #include <stdint.h> /* uint32_t */
8 #include <stdio.h> /* FILE, sprintf(), fflush() */
9 #include <stdlib.h> /* exit(), free(), atoi() */
10 #include <string.h> /* strlen() */
11 #include <sys/stat.h> /* mkdir() */
12 #include <sys/types.h> /* defines pid_t, time_t */
13 #include <time.h> /* time() */
14 #include <unistd.h> /* optarg, getopt(), access(), getpid() */
15 #include "../common/parse_file.h" /* err_line_zero(), err_line_inc() */
16 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), textfile_width(),
17 * try_fgets(), try_fwrite(),
18 * detect_atomic_leftover()
20 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
21 #include "../common/try_malloc.h" /* try_malloc() */
22 #include "cleanup.h" /* set_cleanup_flag() */
23 #include "field_of_view.h" /* build_fov_map() */
24 #include "hardcoded_strings.h" /* s */
25 #include "map.h" /* remake_map() */
26 #include "things.h" /* Thing, ThingType, free_things(), add_things(),
27 * get_thing_id_action_id_by_name()
29 #include "run.h" /* obey_msg(), io_loop() */
30 #include "world.h" /* global world */
35 /* Pass to obey_msg() lines from file at "path", on "record" write to same. Do
36 * not pass lines that consist only of a newline character. Transform newline
37 * in the line passed to \0.
39 static void obey_lines_from_file(char * path, uint8_t record);
41 /* Replay game from record file up to the turn named in world.replay, then turn
42 * over to manual replay via io_loop().
44 static void replay_game();
46 /* Return 1 if the type defined by world.player_type has a .start_n of 0.
47 * Return 2 if no thing action with .name of s[S_CMD_WAIT] is defined.
50 static uint8_t world_cannot_be_made();
53 static void obey_lines_from_file(char * path, uint8_t record)
55 FILE * file = try_fopen(path, "r", __func__);
56 uint32_t linemax = textfile_width(file);
57 char * line = try_malloc(linemax + 1, __func__);
58 while (NULL != try_fgets(line, linemax + 1, file, __func__))
62 if (strcmp("\n", line))
64 char * nl = strchr(line, '\n');
69 obey_msg(line, record, 1);
75 try_fclose(file, __func__);
80 static void replay_game()
82 exit_err(access(s[S_PATH_RECORD], F_OK), "No record found to replay.");
83 FILE * file = try_fopen(s[S_PATH_RECORD], "r", __func__);
84 uint32_t linemax = textfile_width(file);
85 char * line = try_malloc(linemax + 1, __func__);
86 while ( world.turn < world.replay
87 && NULL != try_fgets(line, linemax + 1, file, __func__))
97 end = (NULL == try_fgets(line, linemax + 1, file, __func__));
100 obey_msg(line, 0, 1);
106 try_fclose(file, __func__);
111 static uint8_t world_cannot_be_made()
113 uint8_t player_will_be_generated = 0;
114 struct ThingType * tt;
115 for (tt = world.thing_types; NULL != tt; tt = tt->next)
117 if (world.player_type == tt->id)
119 player_will_be_generated = 0 < tt->start_n;
123 if (!player_will_be_generated)
127 if (!get_thing_action_id_by_name(s[S_CMD_WAIT]))
136 extern void obey_argv(int argc, char * argv[])
139 while (-1 != (opt = getopt(argc, argv, "vs::")))
143 world.is_verbose = 1;
150 world.replay = atoi(optarg);
162 extern void setup_server_io()
164 int test = mkdir("server", 0700);
165 exit_trouble(test && EEXIST != errno, __func__, "mkdir");
166 world.file_out = try_fopen(s[S_PATH_OUT], "w", __func__);
167 world.server_test = try_malloc(10 + 1 + 10 + 1 + 1, __func__);
168 test = sprintf(world.server_test, "%d %d\n", getpid(), (int) time(0));
169 exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);
170 try_fwrite(world.server_test, strlen(world.server_test), 1,
171 world.file_out, __func__);
172 fflush(world.file_out);
173 set_cleanup_flag(CLEANUP_OUT);
174 char * path_in = s[S_PATH_IN];
175 if (!access(path_in, F_OK)) /* This keeps out input from old input */
176 { /* file streams of clients */
177 unlink(path_in) ; /* communicating with server processes */
178 } /* superseded by this current one. */
179 world.file_in = try_fopen(path_in, "w", __func__);
180 try_fclose(world.file_in, __func__);
181 world.file_in = try_fopen(path_in, "r", __func__);
182 set_cleanup_flag(CLEANUP_IN);
187 extern uint8_t remake_world()
189 uint8_t test = world_cannot_be_made();
195 world.log = NULL; /* thing_actions.c's update_log() checks for this. */
196 world.seed_map = world.seed;
197 free_things(world.things);
199 struct ThingType * tt;
200 for (tt = world.thing_types; NULL != tt; tt = tt->next)
202 if (world.player_type == tt->id)
204 add_things(tt->id, tt->start_n);
208 for (tt = world.thing_types; NULL != tt; tt = tt->next)
210 if (world.player_type != tt->id)
212 add_things(tt->id, tt->start_n);
216 for (t = world.things; NULL != t; t = t->next)
218 t->fov_map = t->lifepoints ? build_fov_map(t) : NULL;
228 extern void run_game()
230 detect_atomic_leftover(s[S_PATH_SAVE]);
231 detect_atomic_leftover(s[S_PATH_RECORD]);
238 if (!access(s[S_PATH_SAVE], F_OK))
240 obey_lines_from_file(s[S_PATH_SAVE], 0);
244 char * err = "No world config file from which to start a new world.";
245 exit_err(access(s[S_PATH_CONFIG], F_OK), err);
246 obey_lines_from_file(s[S_PATH_CONFIG], 1);
248 char * command = s[S_CMD_MAKE_WORLD];
249 char * msg = try_malloc(strlen(command) + 1 + 11 + 1, __func__);
250 int test = sprintf(msg, "%s %d", command, (int) time(NULL));
251 exit_trouble(test < 0, __func__, s[S_FCN_SPRINTF]);