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() */
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[PATH_RECORD], F_OK), "No record found to replay.");
44 FILE * file = try_fopen(s[PATH_RECORD], "r", f_name);
45 uint32_t linemax = textfile_width(file);
46 char line[linemax + 1];
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));
64 try_fclose(file, f_name);
69 extern void obey_argv(int argc, char * argv[])
72 while (-1 != (opt = getopt(argc, argv, "vs::")))
83 world.replay = atoi(optarg);
95 extern void setup_server_io()
97 char * f_name = "setup_server_io()";
98 int test = mkdir("server", 0700);
99 exit_trouble(test && EEXIST != errno, f_name, "mkdir()");
100 world.file_out = try_fopen(s[PATH_OUT], "w", f_name);
101 world.server_test = try_malloc(10 + 1 + 10 + 1 + 1, f_name);
102 sprintf(world.server_test, "%d %d\n", getpid(), (int) time(0));
103 try_fwrite(world.server_test, strlen(world.server_test), 1,
104 world.file_out, f_name);
105 fflush(world.file_out);
106 set_cleanup_flag(CLEANUP_OUT);
107 char * path_in = s[PATH_IN];
108 if (!access(path_in, F_OK)) /* This keeps out input from old input */
109 { /* file streams of clients */
110 unlink(path_in) ; /* communicating with server processes */
111 } /* superseded by this current one. */
112 world.file_in = try_fopen(path_in, "w", f_name);
113 try_fclose(world.file_in, f_name);
114 world.file_in = try_fopen(path_in, "r", f_name);
115 set_cleanup_flag(CLEANUP_IN);
120 extern void remake_world()
122 char * f_name = "remake_world()";
124 world.log = NULL; /* thing_actions.c's update_log() checks for this. */
125 world.seed_map = world.seed;
126 free_things(world.things);
127 world.last_update_turn = 0;
129 struct ThingType * tt;
130 for (tt = world.thing_types; NULL != tt; tt = tt->next)
132 if (world.player_type == tt->id)
134 add_things(tt->id, tt->start_n);
138 for (tt = world.thing_types; NULL != tt; tt = tt->next)
140 if (world.player_type != tt->id)
142 add_things(tt->id, tt->start_n);
145 set_cleanup_flag(CLEANUP_THINGS);
147 for (t = world.things; NULL != t; t = t->next)
149 t->fov_map = t->lifepoints ? build_fov_map(t) : NULL;
151 if (!world.replay && !access(s[PATH_RECORD], F_OK))
153 exit_trouble(unlink(s[PATH_RECORD]), f_name, "unlink()");
160 extern void run_game()
162 char * f_name = "run_game()";
168 char * path_savefile = s[PATH_SAVE];
169 if (!access(path_savefile, F_OK))
171 FILE * file = try_fopen(path_savefile, "r", f_name);
172 uint32_t linemax = textfile_width(file);
173 char line[linemax + 1];
174 while (NULL != try_fgets(line, linemax + 1, file, f_name))
176 if (strlen(line) && strcmp("\n", line))
181 try_fclose(file, f_name);
185 char * command = s[CMD_MAKE_WORLD];
186 char msg[strlen(command) + 1 + 11 + 1];
187 sprintf(msg, "%s %d", command, (int) time(NULL));