home · contact · privacy
Server's remake_world() unlinks any pre-existing record file.
[plomrogue] / src / server / init.c
1 /* src/server/init.c */
2
3 #include "init.h"
4 #include <errno.h> /* errno */
5 #include <stddef.h> /* NULL */
6 #include <stdint.h> /* uint32_t */
7 #include <stdlib.h> /* exit(), free() */
8 #include <string.h> /* atoi() */
9 #include <time.h> /* time() */
10 #include <unistd.h> /* optarg, getopt(), access(), unlink() */
11 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), textfile_sizes(),
12                                   * try_fgets()
13                                   */
14 #include "../common/rexit.h" /* exit_err() */
15 #include "cleanup.h" /* set_cleanup_flag() */
16 #include "map_objects.h" /* free_map_objects(), add_map_objects() */
17 #include "map.h" /* init_map() */
18 #include "rrand.h" /* rrand() */
19 #include "run.h" /* obey_msg(), io_loop() */
20 #include "world.h" /* global world */
21
22
23
24 extern void obey_argv(int argc, char * argv[])
25 {
26     int opt;
27     while (-1 != (opt = getopt(argc, argv, "vs::")))
28     {
29         if      ('v' == opt)
30         {
31             world.is_verbose = 1;
32         }
33         else if ('s' == opt)
34         {
35             world.replay = 1;
36             if (optarg)
37             {
38                 world.replay = atoi(optarg);
39             }
40         }
41         else
42         {
43             exit(EXIT_FAILURE);
44         }
45     }
46 }
47
48
49
50 extern void remake_world(uint32_t seed)
51 {
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     world.turn = 1;
61     init_map();
62     add_map_objects(0, 1);
63     add_map_objects(1, 1 + rrand() % 27);
64     add_map_objects(2, 1 + rrand() % 9);
65     add_map_objects(3, 1 + rrand() % 3);
66     add_map_objects(4, 1 + rrand() % 3);
67     add_map_objects(5, 1 + rrand() % 3);
68     set_cleanup_flag(CLEANUP_MAP_OBJECTS);
69     int test = unlink(world.path_record);
70     char * err = "remake_world() fails to unlink() record file.";
71     exit_err(test && errno != ENOENT, err);
72 }
73
74
75
76 extern void run_game()
77 {
78     char * f_name = "run_game()";
79     if (!access(world.path_record, F_OK))
80     {
81         FILE * file = try_fopen(world.path_record, "r", f_name);
82         uint32_t linemax = textfile_sizes(file, NULL);
83         char line[linemax + 1];
84         while (   (!world.replay || (world.turn < world.replay))
85                && NULL != try_fgets(line, linemax + 1, file, f_name))
86         {
87             obey_msg(line, 0);
88         }
89         if (!world.replay)
90         {
91             try_fclose(file, f_name);
92             io_loop();
93             return;
94         }
95         uint8_t end = 0;
96         while (!io_loop())
97         {
98             if (!end)
99             {
100                 end = (NULL == try_fgets(line, linemax + 1, file, f_name));
101                 if (!end)
102                 {
103                     obey_msg(line, 0);
104                 }
105             }
106         }
107         try_fclose(file, f_name);
108         return;
109     }
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));
114     obey_msg(msg, 1);
115     io_loop();
116 }