1 /* src/server/main.c */
3 #include <errno.h> /* global errno */
4 #include <stdio.h> /* printf(), fflush() */
5 #include <stdlib.h> /* exit() */
6 #include <string.h> /* strlen() */
7 #include <sys/stat.h> /* mkfifo(), mkdir() */
8 #include <sys/types.h> /* defines pid_t, time_t */
9 #include <time.h> /* #time() */
10 #include <unistd.h> /* access(), getpid(), unlink() */
11 #include "../common/err_try_fgets.h" /* set_err_try_fgets_delim() */
12 #include "../common/readwrite.h" /* try_fopen(), try_fwrite(), try_fclose() */
13 #include "../common/rexit.h" /* exit_err, set_cleanup_func() */
14 #include "../common/try_malloc.h" /* try_malloc() */
15 #include "cleanup.h" /* set_cleanup_flag(), cleanup() */
16 #include "init.h" /* run_game(), obey_argv() */
17 #include "map_object_actions.h" /* init_map_object_actions() */
18 #include "map_objects.h" /* init_map_object_defs() */
19 #include "run.h" /* obey_argv(), run_game() */
20 #include "world.h" /* struct World */
28 int main(int argc, char ** argv)
30 char * f_name = "main()";
32 /* So error exits also go through the server's cleanup() function. */
33 set_cleanup_func(cleanup);
35 /* Init settings from command line / hard-coded values. Print start info. */
36 obey_argv(argc, argv);
39 char * printf_err = "Trouble in main() with printf().";
40 int test = printf("Starting plomrogue-server.\n");
41 exit_err(-1 == test, printf_err);
44 test = printf("Replay mode. Auto-replaying up to turn %d.\n",
46 exit_err(-1 == test, printf_err);
49 world.path_map_obj_defs = "confserver/defs";
50 world.path_map_obj_acts = "confserver/map_object_actions";
51 world.path_worldstate = "server/worldstate";
52 world.path_out = "server/out";
53 world.path_in = "server/in";
54 world.path_record = "record";
55 world.tmp_suffix = "_tmp";
56 set_err_try_fgets_delim("%%\n");
58 /* Set map geometry. */
59 world.map.size.x = 64;
60 world.map.size.y = 64;
61 world.map.dist_orthogonal = 5;
62 world.map.dist_diagonal = 7;
64 /* Check existence of config files. */
65 char * err_mod = "No map object definitions file.";
66 char * err_moa = "No map object actions file.";
67 exit_err(access(world.path_map_obj_defs, F_OK), err_mod);
68 exit_err(access(world.path_map_obj_acts, F_OK), err_moa);
70 /* Init from config files map object (action) definitions. */
71 init_map_object_defs();
72 init_map_object_actions();
74 /* Create server directory if it does not exist yet. */
75 int test = mkdir("server", 0700);
76 exit_trouble(test && EEXIST != errno, f_name, "mkdir()");
78 /* Create server out file and start it with server process test string. */
79 world.file_out = try_fopen(world.path_out, "w", f_name);
80 world.server_test = try_malloc(10 + 1 + 10 + 1 + 1, f_name);
81 sprintf(world.server_test, "%d %d\n", getpid(), (int) time(0));
82 try_fwrite(world.server_test, strlen(world.server_test), 1,
83 world.file_out, f_name);
84 fflush(world.file_out);
85 set_cleanup_flag(CLEANUP_OUT);
87 /* Create server in file, switch to reading it. */
88 if (!access(world.path_in, F_OK)) /* This keeps out input from old input */
89 { /* file streams of clients */
90 unlink(world.path_in); /* communicating with server processes */
91 } /* superseded by this current one. */
92 world.file_in = try_fopen(world.path_in, "w", f_name);
93 try_fclose(world.file_in, f_name);
94 world.file_in = try_fopen(world.path_in, "r", f_name);
95 set_cleanup_flag(CLEANUP_IN);
97 /* Enter play or replay mode loops, then leave properly. */