1 /* src/server/main.c */
3 #include <errno.h> /* global errno */
4 #include <stdio.h> /* printf() */
5 #include <stdlib.h> /* exit() */
6 #include <sys/stat.h> /* mkfifo(), mkdir() */
7 #include <unistd.h> /* access() */
8 #include "../common/err_try_fgets.h" /* set_err_try_fgets_delim() */
9 #include "../common/rexit.h" /* exit_err, exit_trouble(), set_cleanup_func() */
10 #include "cleanup.h" /* set_cleanup_flag(), cleanup() */
11 #include "init.h" /* run_game(), obey_argv() */
12 #include "map_object_actions.h" /* init_map_object_actions() */
13 #include "map_objects.h" /* init_map_object_defs() */
14 #include "run.h" /* obey_argv(), run_game() */
15 #include "world.h" /* struct World */
23 int main(int argc, char ** argv)
25 char * f_name = "main()";
27 /* So error exits also go through the server's cleanup() function. */
28 set_cleanup_func(cleanup);
30 /* Init settings from command line / hard-coded values. Print start info. */
31 obey_argv(argc, argv);
34 char * printf_err = "Trouble in main() with printf().";
35 int test = printf("Starting plomrogue-server.\n");
36 exit_err(-1 == test, printf_err);
39 test = printf("Replay mode. Auto-replaying up to turn %d.\n",
41 exit_err(-1 == test, printf_err);
44 world.path_map_obj_defs = "confserver/defs";
45 world.path_map_obj_acts = "confserver/map_object_actions";
46 world.path_in = "server/in";
47 world.path_out = "server/out";
48 world.path_record = "record";
49 world.tmp_suffix = "_tmp";
50 set_err_try_fgets_delim("%%\n");
52 /* Set map geometry. */
53 world.map.size.x = 64;
54 world.map.size.y = 64;
55 world.map.dist_orthogonal = 5;
56 world.map.dist_diagonal = 7;
58 /* Check existence of config files. */
59 char * err_mod = "No map object definitions file.";
60 char * err_moa = "No map object actions file.";
61 exit_err(access(world.path_map_obj_defs, F_OK), err_mod);
62 exit_err(access(world.path_map_obj_acts, F_OK), err_moa);
64 /* Treat world.path_in file as server process lock file. */
65 char * err = "Found pre-existing input fifo file. This indicates another "
66 "roguelike-server may be running. It should be killed first.";
67 exit_err(!access(world.path_in, F_OK), err);
68 int test = mkdir("server", 0700);
69 exit_trouble(test && EEXIST != errno, f_name, "mkdir()");
70 exit_trouble(mkfifo(world.path_in, 0600), f_name, "mkfifo()");
71 set_cleanup_flag(CLEANUP_FIFO);
73 /* Init from config files map object (action) definitions. */
74 init_map_object_defs();
75 init_map_object_actions();
77 /* Enter play or replay mode loops, then leave properly. */