1 /* src/server/init.c */
4 #include <stddef.h> /* NULL */
5 #include <stdint.h> /* uint32_t */
6 #include <stdlib.h> /* exit(), free() */
7 #include <string.h> /* atoi() */
8 #include <time.h> /* time() */
9 #include <unistd.h> /* optarg, getopt(), access(), unlink() */
10 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), textfile_width(),
13 #include "../common/rexit.h" /* exit_err() */
14 #include "cleanup.h" /* set_cleanup_flag() */
15 #include "map_objects.h" /* free_map_objects(), add_map_objects() */
16 #include "map.h" /* init_map() */
17 #include "rrand.h" /* rrand() */
18 #include "run.h" /* obey_msg(), io_loop() */
19 #include "world.h" /* global world */
23 extern void obey_argv(int argc, char * argv[])
26 while (-1 != (opt = getopt(argc, argv, "vs::")))
37 world.replay = atoi(optarg);
49 extern void remake_world(uint32_t seed)
51 char * f_name = "remake_world()";
53 world.log = NULL; /* map_object_action.c's update_log() checks for this. */
55 world.map_obj_count = 0;
56 free(world.map.cells);
57 free_map_objects(world.map_objs);
58 world.last_update_turn = 0;
60 add_map_objects(0, 1);
61 add_map_objects(1, 1 + rrand() % 27);
62 add_map_objects(2, 1 + rrand() % 9);
63 add_map_objects(3, 1 + rrand() % 3);
64 add_map_objects(4, 1 + rrand() % 3);
65 add_map_objects(5, 1 + rrand() % 3);
66 set_cleanup_flag(CLEANUP_MAP_OBJECTS);
69 exit_trouble(unlink(world.path_record), f_name, "unlink()");
76 extern void run_game()
78 char * f_name = "run_game()";
79 if (!access(world.path_record, F_OK))
81 FILE * file = try_fopen(world.path_record, "r", f_name);
82 uint32_t linemax = textfile_width(file);
83 char line[linemax + 1];
84 while ( (!world.replay || (world.turn < world.replay))
85 && NULL != try_fgets(line, linemax + 1, file, f_name))
91 try_fclose(file, f_name);
100 end = (NULL == try_fgets(line, linemax + 1, file, f_name));
107 try_fclose(file, f_name);
110 exit_err(world.replay, "No record file found to replay.");
111 char * command = "seed";
112 char msg[strlen(command) + 1 + 11 + 1];
113 sprintf(msg, "%s %d", command, (int) time(NULL));