home · contact · privacy
MAJOR re-write. Split plomrogue into a server and a client. Re-wrote large parts
[plomrogue] / src / server / main.c
1 /* src/server/main.c */
2
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/rexit.h" /* exit_err, exit_trouble(), set_cleanup_func() */
9 #include "cleanup.h" /* set_cleanup_flag(), cleanup() */
10 #include "init.h" /* run_game(), obey_argv() */
11 #include "map_object_actions.h" /* init_map_object_actions() */
12 #include "map_objects.h" /* init_map_object_defs() */
13 #include "run.h" /* obey_argv(), run_game() */
14 #include "world.h" /* struct World */
15
16
17
18 struct World world;
19
20
21
22 int main(int argc, char ** argv)
23 {
24     char * f_name = "main()";
25
26     /* So error exits also go through the server's cleanup() function. */
27     set_cleanup_func(cleanup);
28
29     /* Init settings from command line / hard-coded values. Print start info. */
30     obey_argv(argc, argv);
31     if (world.is_verbose)
32     {
33         char * printf_err = "Trouble in main() with printf().";
34         int test = printf("Starting plomrogue-server.\n");
35         exit_err(-1 == test, printf_err);
36         if (world.replay)
37         {
38             test = printf("Replay mode. Auto-replaying up to turn %d.\n",
39                          world.replay);
40             exit_err(-1 == test, printf_err);
41         }
42     }
43     world.path_in     = "server/in";
44     world.path_out    = "server/out";
45     world.path_record = "record";
46     world.tmp_suffix  = "_tmp";
47
48     /* Treat world.path_in file as server process lock file. */
49     char * err = "Found pre-existing input fifo file. This indicates another "
50                  "roguelike-server may be running. It should be killed first.";
51     exit_err(!access(world.path_in, F_OK), err);
52     int test = mkdir("server", 0700);
53     exit_trouble(test && EEXIST != errno, f_name, "mkdir()");
54     exit_trouble(mkfifo(world.path_in, 0600), f_name, "mkfifo()");
55     set_cleanup_flag(CLEANUP_FIFO);
56
57     /* Init from config files map object (action) definitions. */
58     init_map_object_defs("confserver/defs");
59     init_map_object_actions("confserver/map_object_actions");
60
61     /* Enter play or replay mode loops, then leave properly. */
62     run_game();
63     cleanup();
64     exit(EXIT_SUCCESS);
65 }