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 "map.h" /* init_map() */
22 #include "map_objects.h" /* free_map_objects(), add_map_objects() */
23 #include "rrand.h" /* rrand() */
24 #include "run.h" /* obey_msg(), io_loop() */
25 #include "world.h" /* global world */
29 extern void obey_argv(int argc, char * argv[])
32 while (-1 != (opt = getopt(argc, argv, "vs::")))
43 world.replay = atoi(optarg);
55 extern void setup_server_io()
57 char * f_name = "setup_server_io()";
58 int test = mkdir("server", 0700);
59 exit_trouble(test && EEXIST != errno, f_name, "mkdir()");
60 world.file_out = try_fopen(world.path_out, "w", f_name);
61 world.server_test = try_malloc(10 + 1 + 10 + 1 + 1, f_name);
62 sprintf(world.server_test, "%d %d\n", getpid(), (int) time(0));
63 try_fwrite(world.server_test, strlen(world.server_test), 1,
64 world.file_out, f_name);
65 fflush(world.file_out);
66 set_cleanup_flag(CLEANUP_OUT);
67 if (!access(world.path_in, F_OK)) /* This keeps out input from old input */
68 { /* file streams of clients */
69 unlink(world.path_in); /* communicating with server processes */
70 } /* superseded by this current one. */
71 world.file_in = try_fopen(world.path_in, "w", f_name);
72 try_fclose(world.file_in, f_name);
73 world.file_in = try_fopen(world.path_in, "r", f_name);
74 set_cleanup_flag(CLEANUP_IN);
79 extern void remake_world(uint32_t seed)
81 char * f_name = "remake_world()";
83 world.log = NULL; /* map_object_action.c's update_log() checks for this. */
85 world.map_obj_count = 0;
86 free(world.map.cells);
87 free_map_objects(world.map_objs);
88 world.last_update_turn = 0;
90 add_map_objects(0, 1);
91 add_map_objects(1, 1 + rrand() % 27);
92 add_map_objects(2, 1 + rrand() % 9);
93 add_map_objects(3, 1 + rrand() % 3);
94 add_map_objects(4, 1 + rrand() % 3);
95 add_map_objects(5, 1 + rrand() % 3);
96 set_cleanup_flag(CLEANUP_MAP_OBJECTS);
99 exit_trouble(unlink(world.path_record), f_name, "unlink()");
106 extern void run_game()
108 char * f_name = "run_game()";
109 if (!access(world.path_record, F_OK))
111 FILE * file = try_fopen(world.path_record, "r", f_name);
112 uint32_t linemax = textfile_width(file);
113 char line[linemax + 1];
114 while ( (!world.replay || (world.turn < world.replay))
115 && NULL != try_fgets(line, linemax + 1, file, f_name))
121 try_fclose(file, f_name);
130 end = (NULL == try_fgets(line, linemax + 1, file, f_name));
137 try_fclose(file, f_name);
140 exit_err(world.replay, "No record file found to replay.");
141 char * command = "seed";
142 char msg[strlen(command) + 1 + 11 + 1];
143 sprintf(msg, "%s %d", command, (int) time(NULL));