1 /* src/server/init.c */
3 #define _POSIX_C_SOURCE 200809L /* getopt(), optarg, strdup() */
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 "configfile.h" /* read_config_file() */
22 #include "map.h" /* init_map() */
23 #include "map_objects.h" /* free_map_objects(), add_map_objects() */
24 #include "rrand.h" /* rrand() */
25 #include "run.h" /* obey_msg(), io_loop() */
26 #include "world.h" /* global world */
30 extern void obey_argv(int argc, char * argv[])
33 while (-1 != (opt = getopt(argc, argv, "vs::")))
44 world.replay = atoi(optarg);
56 extern void init_map_and_map_objects_configs()
58 world.map.size.x = 64;
59 world.map.size.y = 64;
60 world.map.dist_orthogonal = 5;
61 world.map.dist_diagonal = 7;
67 extern void setup_server_io()
69 char * f_name = "setup_server_io()";
70 int test = mkdir("server", 0700);
71 exit_trouble(test && EEXIST != errno, f_name, "mkdir()");
72 world.file_out = try_fopen(world.path_out, "w", f_name);
73 world.server_test = try_malloc(10 + 1 + 10 + 1 + 1, f_name);
74 sprintf(world.server_test, "%d %d\n", getpid(), (int) time(0));
75 try_fwrite(world.server_test, strlen(world.server_test), 1,
76 world.file_out, f_name);
77 fflush(world.file_out);
78 set_cleanup_flag(CLEANUP_OUT);
79 if (!access(world.path_in, F_OK)) /* This keeps out input from old input */
80 { /* file streams of clients */
81 unlink(world.path_in); /* communicating with server processes */
82 } /* superseded by this current one. */
83 world.file_in = try_fopen(world.path_in, "w", f_name);
84 try_fclose(world.file_in, f_name);
85 world.file_in = try_fopen(world.path_in, "r", f_name);
86 set_cleanup_flag(CLEANUP_IN);
91 extern void remake_world(uint32_t seed)
93 char * f_name = "remake_world()";
95 world.log = NULL; /* map_object_action.c's update_log() checks for this. */
97 world.map_obj_count = 0;
98 free(world.map.cells);
99 free_map_objects(world.map_objs);
100 world.last_update_turn = 0;
102 add_map_objects(0, 1);
103 add_map_objects(1, 1 + rrand() % 27);
104 add_map_objects(2, 1 + rrand() % 9);
105 add_map_objects(3, 1 + rrand() % 3);
106 add_map_objects(4, 1 + rrand() % 3);
107 add_map_objects(5, 1 + rrand() % 3);
108 set_cleanup_flag(CLEANUP_MAP_OBJECTS);
111 exit_trouble(unlink(world.path_record), f_name, "unlink()");
118 extern void run_game()
120 char * f_name = "run_game()";
121 if (!access(world.path_record, F_OK))
123 FILE * file = try_fopen(world.path_record, "r", f_name);
124 uint32_t linemax = textfile_width(file);
125 char line[linemax + 1];
126 while ( (!world.replay || (world.turn < world.replay))
127 && NULL != try_fgets(line, linemax + 1, file, f_name))
133 try_fclose(file, f_name);
142 end = (NULL == try_fgets(line, linemax + 1, file, f_name));
149 try_fclose(file, f_name);
152 exit_err(world.replay, "No record file found to replay.");
153 char * command = "seed";
154 char msg[strlen(command) + 1 + 11 + 1];
155 sprintf(msg, "%s %d", command, (int) time(NULL));