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" /* MapObjDef, free_map_objects(), add_map_objects() */
23 #include "run.h" /* obey_msg(), io_loop() */
24 #include "world.h" /* global world */
28 extern void obey_argv(int argc, char * argv[])
31 while (-1 != (opt = getopt(argc, argv, "vs::")))
42 world.replay = atoi(optarg);
54 extern void setup_server_io()
56 char * f_name = "setup_server_io()";
57 int test = mkdir("server", 0700);
58 exit_trouble(test && EEXIST != errno, f_name, "mkdir()");
59 world.file_out = try_fopen(world.path_out, "w", f_name);
60 world.server_test = try_malloc(10 + 1 + 10 + 1 + 1, f_name);
61 sprintf(world.server_test, "%d %d\n", getpid(), (int) time(0));
62 try_fwrite(world.server_test, strlen(world.server_test), 1,
63 world.file_out, f_name);
64 fflush(world.file_out);
65 set_cleanup_flag(CLEANUP_OUT);
66 if (!access(world.path_in, F_OK)) /* This keeps out input from old input */
67 { /* file streams of clients */
68 unlink(world.path_in); /* communicating with server processes */
69 } /* superseded by this current one. */
70 world.file_in = try_fopen(world.path_in, "w", f_name);
71 try_fclose(world.file_in, f_name);
72 world.file_in = try_fopen(world.path_in, "r", f_name);
73 set_cleanup_flag(CLEANUP_IN);
78 extern void remake_world(uint32_t seed)
80 char * f_name = "remake_world()";
82 world.log = NULL; /* map_object_action.c's update_log() checks for this. */
84 world.map_obj_count = 0;
85 free(world.map.cells);
86 free_map_objects(world.map_objs);
87 world.last_update_turn = 0;
89 struct MapObjDef * mod;
90 for (mod = world.map_obj_defs; NULL != mod; mod = mod->next)
92 if (world.player_type == mod->id)
94 add_map_objects(mod->id, mod->start_n);
98 for (mod = world.map_obj_defs; NULL != mod; mod = mod->next)
100 if (world.player_type != mod->id)
102 add_map_objects(mod->id, mod->start_n);
105 set_cleanup_flag(CLEANUP_MAP_OBJECTS);
108 exit_trouble(unlink(world.path_record), f_name, "unlink()");
115 extern void run_game()
117 char * f_name = "run_game()";
118 if (!access(world.path_record, F_OK))
120 FILE * file = try_fopen(world.path_record, "r", f_name);
121 uint32_t linemax = textfile_width(file);
122 char line[linemax + 1];
123 while ( (!world.replay || (world.turn < world.replay))
124 && NULL != try_fgets(line, linemax + 1, file, f_name))
130 try_fclose(file, f_name);
139 end = (NULL == try_fgets(line, linemax + 1, file, f_name));
146 try_fclose(file, f_name);
149 exit_err(world.replay, "No record file found to replay.");
150 char * command = "seed";
151 char msg[strlen(command) + 1 + 11 + 1];
152 sprintf(msg, "%s %d", command, (int) time(NULL));