1 /* src/server/init.c */
4 #include <errno.h> /* global errno, EEXIST */
5 #include <stddef.h> /* NULL */
6 #include <stdint.h> /* uint32_t */
7 #include <stdio.h> /* sprintf(), fflush() */
8 #include <stdlib.h> /* exit(), free() */
9 #include <string.h> /* atoi(), strlen() */
10 #include <sys/stat.h> /* mkdir() */
11 #include <sys/types.h> /* defines pid_t, time_t */
12 #include <time.h> /* time() */
13 #include <unistd.h> /* optarg, getopt(), access(), unlink(), getpid() */
14 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), textfile_width(),
15 * try_fgets(), try_fwrite()
17 #include "../common/rexit.h" /* exit_err() */
18 #include "../common/try_malloc.h" /* try_malloc() */
19 #include "cleanup.h" /* set_cleanup_flag() */
20 #include "map_object_actions.h" /* init_map_object_actions() */
21 #include "map_objects.h" /* free_map_objects(), add_map_objects(),
22 * init_map_object_defs()
24 #include "map.h" /* init_map() */
25 #include "rrand.h" /* rrand() */
26 #include "run.h" /* obey_msg(), io_loop() */
27 #include "world.h" /* global world */
31 extern void obey_argv(int argc, char * argv[])
34 while (-1 != (opt = getopt(argc, argv, "vs::")))
45 world.replay = atoi(optarg);
57 extern void init_map_and_map_objects_configs()
59 world.map.size.x = 64;
60 world.map.size.y = 64;
61 world.map.dist_orthogonal = 5;
62 world.map.dist_diagonal = 7;
63 char * err_mod = "No map object definitions file.";
64 char * err_moa = "No map object actions file.";
65 exit_err(access(world.path_map_obj_defs, F_OK), err_mod);
66 exit_err(access(world.path_map_obj_acts, F_OK), err_moa);
67 init_map_object_defs();
68 init_map_object_actions();
73 extern void setup_server_io()
75 char * f_name = "setup_server_io()";
76 int test = mkdir("server", 0700);
77 exit_trouble(test && EEXIST != errno, f_name, "mkdir()");
78 world.file_out = try_fopen(world.path_out, "w", f_name);
79 world.server_test = try_malloc(10 + 1 + 10 + 1 + 1, f_name);
80 sprintf(world.server_test, "%d %d\n", getpid(), (int) time(0));
81 try_fwrite(world.server_test, strlen(world.server_test), 1,
82 world.file_out, f_name);
83 fflush(world.file_out);
84 set_cleanup_flag(CLEANUP_OUT);
85 if (!access(world.path_in, F_OK)) /* This keeps out input from old input */
86 { /* file streams of clients */
87 unlink(world.path_in); /* communicating with server processes */
88 } /* superseded by this current one. */
89 world.file_in = try_fopen(world.path_in, "w", f_name);
90 try_fclose(world.file_in, f_name);
91 world.file_in = try_fopen(world.path_in, "r", f_name);
92 set_cleanup_flag(CLEANUP_IN);
97 extern void remake_world(uint32_t seed)
99 char * f_name = "remake_world()";
101 world.log = NULL; /* map_object_action.c's update_log() checks for this. */
103 world.map_obj_count = 0;
104 free(world.map.cells);
105 free_map_objects(world.map_objs);
106 world.last_update_turn = 0;
108 add_map_objects(0, 1);
109 add_map_objects(1, 1 + rrand() % 27);
110 add_map_objects(2, 1 + rrand() % 9);
111 add_map_objects(3, 1 + rrand() % 3);
112 add_map_objects(4, 1 + rrand() % 3);
113 add_map_objects(5, 1 + rrand() % 3);
114 set_cleanup_flag(CLEANUP_MAP_OBJECTS);
117 exit_trouble(unlink(world.path_record), f_name, "unlink()");
124 extern void run_game()
126 char * f_name = "run_game()";
127 if (!access(world.path_record, F_OK))
129 FILE * file = try_fopen(world.path_record, "r", f_name);
130 uint32_t linemax = textfile_width(file);
131 char line[linemax + 1];
132 while ( (!world.replay || (world.turn < world.replay))
133 && NULL != try_fgets(line, linemax + 1, file, f_name))
139 try_fclose(file, f_name);
148 end = (NULL == try_fgets(line, linemax + 1, file, f_name));
155 try_fclose(file, f_name);
158 exit_err(world.replay, "No record file found to replay.");
159 char * command = "seed";
160 char msg[strlen(command) + 1 + 11 + 1];
161 sprintf(msg, "%s %d", command, (int) time(NULL));