1 /* src/server/init.c */
3 //#define _POSIX_C_SOURCE 2 /* getopt(), optarg */
4 #define _POSIX_C_SOURCE 200809L /* getopt(), optarg, strdup() */
6 #include <errno.h> /* global errno, EEXIST */
7 #include <stddef.h> /* NULL */
8 #include <stdint.h> /* uint32_t */
9 #include <stdio.h> /* FILE, sprintf(), fflush() */
10 #include <stdlib.h> /* exit(), free(), atoi() */
11 #include <string.h> /* strlen() */
12 #include <sys/stat.h> /* mkdir() */
13 #include <sys/types.h> /* defines pid_t, time_t */
14 #include <time.h> /* time() */
15 #include <unistd.h> /* optarg, getopt(), access(), unlink(), getpid() */
16 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), textfile_width(),
17 * try_fgets(), try_fwrite()
19 #include "../common/rexit.h" /* exit_err() */
20 #include "../common/try_malloc.h" /* try_malloc() */
21 #include "cleanup.h" /* set_cleanup_flag() */
22 #include "configfile.h" /* read_config_file() */
23 #include "map.h" /* init_map() */
24 #include "map_objects.h" /* free_map_objects(), add_map_objects() */
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;
68 extern void setup_server_io()
70 char * f_name = "setup_server_io()";
71 int test = mkdir("server", 0700);
72 exit_trouble(test && EEXIST != errno, f_name, "mkdir()");
73 world.file_out = try_fopen(world.path_out, "w", f_name);
74 world.server_test = try_malloc(10 + 1 + 10 + 1 + 1, f_name);
75 sprintf(world.server_test, "%d %d\n", getpid(), (int) time(0));
76 try_fwrite(world.server_test, strlen(world.server_test), 1,
77 world.file_out, f_name);
78 fflush(world.file_out);
79 set_cleanup_flag(CLEANUP_OUT);
80 if (!access(world.path_in, F_OK)) /* This keeps out input from old input */
81 { /* file streams of clients */
82 unlink(world.path_in); /* communicating with server processes */
83 } /* superseded by this current one. */
84 world.file_in = try_fopen(world.path_in, "w", f_name);
85 try_fclose(world.file_in, f_name);
86 world.file_in = try_fopen(world.path_in, "r", f_name);
87 set_cleanup_flag(CLEANUP_IN);
92 extern void remake_world(uint32_t seed)
94 char * f_name = "remake_world()";
96 world.log = NULL; /* map_object_action.c's update_log() checks for this. */
98 world.map_obj_count = 0;
99 free(world.map.cells);
100 free_map_objects(world.map_objs);
101 world.last_update_turn = 0;
103 add_map_objects(0, 1);
104 add_map_objects(1, 1 + rrand() % 27);
105 add_map_objects(2, 1 + rrand() % 9);
106 add_map_objects(3, 1 + rrand() % 3);
107 add_map_objects(4, 1 + rrand() % 3);
108 add_map_objects(5, 1 + rrand() % 3);
109 set_cleanup_flag(CLEANUP_MAP_OBJECTS);
112 exit_trouble(unlink(world.path_record), f_name, "unlink()");
119 extern void run_game()
121 char * f_name = "run_game()";
122 if (!access(world.path_record, F_OK))
124 FILE * file = try_fopen(world.path_record, "r", f_name);
125 uint32_t linemax = textfile_width(file);
126 char line[linemax + 1];
127 while ( (!world.replay || (world.turn < world.replay))
128 && NULL != try_fgets(line, linemax + 1, file, f_name))
134 try_fclose(file, f_name);
143 end = (NULL == try_fgets(line, linemax + 1, file, f_name));
150 try_fclose(file, f_name);
153 exit_err(world.replay, "No record file found to replay.");
154 char * command = "seed";
155 char msg[strlen(command) + 1 + 11 + 1];
156 sprintf(msg, "%s %d", command, (int) time(NULL));