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(), unlink(), getpid() */
15 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), textfile_width(),
16 * try_fgets(), try_fwrite()
18 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
19 #include "../common/try_malloc.h" /* try_malloc() */
20 #include "cleanup.h" /* set_cleanup_flag() */
21 #include "field_of_view.h" /* build_fov_map() */
22 #include "hardcoded_strings.h" /* s */
23 #include "map.h" /* remake_map() */
24 #include "things.h" /* Thing, ThingType, free_things(), add_things(),
27 #include "run.h" /* obey_msg(), io_loop() */
28 #include "world.h" /* global world */
33 /* Replay game from record file up to the turn named in world.replay, then turn
34 * over to manual replay via io_loop().
36 static void replay_game();
40 static void replay_game()
42 char * f_name = "replay_game()";
43 exit_err(access(s[S_PATH_RECORD], F_OK), "No record found to replay.");
44 FILE * file = try_fopen(s[S_PATH_RECORD], "r", f_name);
45 uint32_t linemax = textfile_width(file);
46 char * line = try_malloc(linemax + 1, f_name);
47 while ( world.turn < world.replay
48 && NULL != try_fgets(line, linemax + 1, file, f_name))
57 end = (NULL == try_fgets(line, linemax + 1, file, f_name));
65 try_fclose(file, f_name);
70 extern void obey_argv(int argc, char * argv[])
73 while (-1 != (opt = getopt(argc, argv, "vs::")))
84 world.replay = atoi(optarg);
96 extern void setup_server_io()
98 char * f_name = "setup_server_io()";
99 int test = mkdir("server", 0700);
100 exit_trouble(test && EEXIST != errno, f_name, "mkdir()");
101 world.file_out = try_fopen(s[S_PATH_OUT], "w", f_name);
102 world.server_test = try_malloc(10 + 1 + 10 + 1 + 1, f_name);
103 test = sprintf(world.server_test, "%d %d\n", getpid(), (int) time(0));
104 exit_trouble(test < 0, f_name, "sprintf()");
105 try_fwrite(world.server_test, strlen(world.server_test), 1,
106 world.file_out, f_name);
107 fflush(world.file_out);
108 set_cleanup_flag(CLEANUP_OUT);
109 char * path_in = s[S_PATH_IN];
110 if (!access(path_in, F_OK)) /* This keeps out input from old input */
111 { /* file streams of clients */
112 unlink(path_in) ; /* communicating with server processes */
113 } /* superseded by this current one. */
114 world.file_in = try_fopen(path_in, "w", f_name);
115 try_fclose(world.file_in, f_name);
116 world.file_in = try_fopen(path_in, "r", f_name);
117 set_cleanup_flag(CLEANUP_IN);
122 extern void remake_world()
124 char * f_name = "remake_world()";
126 world.log = NULL; /* thing_actions.c's update_log() checks for this. */
127 world.seed_map = world.seed;
128 free_things(world.things);
129 world.last_update_turn = 0;
131 struct ThingType * tt;
132 for (tt = world.thing_types; NULL != tt; tt = tt->next)
134 if (world.player_type == tt->id)
136 add_things(tt->id, tt->start_n);
140 for (tt = world.thing_types; NULL != tt; tt = tt->next)
142 if (world.player_type != tt->id)
144 add_things(tt->id, tt->start_n);
147 set_cleanup_flag(CLEANUP_THINGS);
149 for (t = world.things; NULL != t; t = t->next)
151 t->fov_map = t->lifepoints ? build_fov_map(t) : NULL;
153 if (!world.replay && !access(s[S_PATH_RECORD], F_OK))
155 exit_trouble(unlink(s[S_PATH_RECORD]), f_name, "unlink()");
162 extern void run_game()
164 char * f_name = "run_game()";
170 char * path_savefile = s[S_PATH_SAVE];
171 if (!access(path_savefile, F_OK))
173 FILE * file = try_fopen(path_savefile, "r", f_name);
174 uint32_t linemax = textfile_width(file);
175 char * line = try_malloc(linemax + 1, f_name);
176 while (NULL != try_fgets(line, linemax + 1, file, f_name))
178 if (strlen(line) && strcmp("\n", line))
184 try_fclose(file, f_name);
188 char * command = s[S_CMD_MAKE_WORLD];
189 char * msg = try_malloc(strlen(command) + 1 + 11 + 1, f_name);
190 int test = sprintf(msg, "%s %d", command, (int) time(NULL));
191 exit_trouble(test < 0, f_name, "sprintf()");