home · contact · privacy
Fixed bug where record unlinking in remake_world() *always* unlinked *any* previously...
[plomrogue] / src / server / init.c
1 /* src/server/init.c */
2
3 #include "init.h"
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_sizes(),
11                                   * try_fgets()
12                                   */
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 */
20
21
22
23 extern void obey_argv(int argc, char * argv[])
24 {
25     int opt;
26     while (-1 != (opt = getopt(argc, argv, "vs::")))
27     {
28         if      ('v' == opt)
29         {
30             world.is_verbose = 1;
31         }
32         else if ('s' == opt)
33         {
34             world.replay = 1;
35             if (optarg)
36             {
37                 world.replay = atoi(optarg);
38             }
39         }
40         else
41         {
42             exit(EXIT_FAILURE);
43         }
44     }
45 }
46
47
48
49 extern void remake_world(uint32_t seed)
50 {
51     char * f_name = "remake_world()";
52     free(world.log);
53     world.log = NULL;  /* map_object_action.c's update_log() checks for this. */
54     world.seed = seed;
55     world.map_obj_count = 0;
56     world.score = 0;
57     free(world.map.cells);
58     free_map_objects(world.map_objs);
59     world.last_update_turn = 0;
60     init_map();
61     add_map_objects(0, 1);
62     add_map_objects(1, 1 + rrand() % 27);
63     add_map_objects(2, 1 + rrand() % 9);
64     add_map_objects(3, 1 + rrand() % 3);
65     add_map_objects(4, 1 + rrand() % 3);
66     add_map_objects(5, 1 + rrand() % 3);
67     set_cleanup_flag(CLEANUP_MAP_OBJECTS);
68     if (world.turn)
69     {
70         exit_trouble(unlink(world.path_record), f_name, "unlink()");
71     }
72     world.turn = 1;
73 }
74
75
76
77 extern void run_game()
78 {
79     char * f_name = "run_game()";
80     if (!access(world.path_record, F_OK))
81     {
82         FILE * file = try_fopen(world.path_record, "r", f_name);
83         uint32_t linemax = textfile_sizes(file, NULL);
84         char line[linemax + 1];
85         while (   (!world.replay || (world.turn < world.replay))
86                && NULL != try_fgets(line, linemax + 1, file, f_name))
87         {
88             obey_msg(line, 0);
89         }
90         if (!world.replay)
91         {
92             try_fclose(file, f_name);
93             io_loop();
94             return;
95         }
96         uint8_t end = 0;
97         while (!io_loop())
98         {
99             if (!end)
100             {
101                 end = (NULL == try_fgets(line, linemax + 1, file, f_name));
102                 if (!end)
103                 {
104                     obey_msg(line, 0);
105                 }
106             }
107         }
108         try_fclose(file, f_name);
109         return;
110     }
111     exit_err(world.replay, "No record file found to replay.");
112     char * command = "seed";
113     char msg[strlen(command) + 1 + 11 + 1];
114     sprintf(msg, "%s %d", command, (int) time(NULL));
115     obey_msg(msg, 1);
116     io_loop();
117 }