home · contact · privacy
Make server config files more readable, their parsing more lenient.
[plomrogue] / src / server / init.c
1 /* src/server/init.c */
2
3 //#define _POSIX_C_SOURCE 2 /* getopt(), optarg */
4 #define _POSIX_C_SOURCE 200809L /* getopt(), optarg, strdup() */
5 #include "init.h"
6 #include <errno.h> /* global errno, EEXIST */
7 #include <stddef.h> /* NULL */
8 #include <stdint.h> /* uint32_t */
9 #include <stdio.h> /* FILE, sprintf(), fflush() */
10 #include <stdlib.h> /* exit(), free(), atoi() */
11 #include <string.h> /* strlen() */
12 #include <sys/stat.h> /* mkdir() */
13 #include <sys/types.h> /* defines pid_t, time_t */
14 #include <time.h> /* time() */
15 #include <unistd.h> /* optarg, getopt(), access(), unlink(), getpid() */
16 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), textfile_width(),
17                                   * try_fgets(), try_fwrite()
18                                   */
19 #include "../common/rexit.h" /* exit_err() */
20 #include "../common/try_malloc.h" /* try_malloc() */
21 #include "cleanup.h" /* set_cleanup_flag() */
22 #include "configfile.h" /* read_config_file() */
23 #include "map.h" /* init_map() */
24 #include "map_objects.h" /* free_map_objects(), add_map_objects() */
25 #include "rrand.h" /* rrand() */
26 #include "run.h" /* obey_msg(), io_loop() */
27 #include "world.h" /* global world */
28
29
30
31 extern void obey_argv(int argc, char * argv[])
32 {
33     int opt;
34     while (-1 != (opt = getopt(argc, argv, "vs::")))
35     {
36         if      ('v' == opt)
37         {
38             world.is_verbose = 1;
39         }
40         else if ('s' == opt)
41         {
42             world.replay = 1;
43             if (optarg)
44             {
45                 world.replay = atoi(optarg);
46             }
47         }
48         else
49         {
50             exit(EXIT_FAILURE);
51         }
52     }
53 }
54
55
56
57 extern void init_map_and_map_objects_configs()
58 {
59     world.map.size.x = 64;
60     world.map.size.y = 64;
61     world.map.dist_orthogonal = 5;
62     world.map.dist_diagonal   = 7;
63     read_config_file();
64 }
65
66
67
68 extern void setup_server_io()
69 {
70     char * f_name = "setup_server_io()";
71     int test = mkdir("server", 0700);
72     exit_trouble(test && EEXIST != errno, f_name, "mkdir()");
73     world.file_out = try_fopen(world.path_out, "w", f_name);
74     world.server_test = try_malloc(10 + 1 + 10 + 1 + 1, f_name);
75     sprintf(world.server_test, "%d %d\n", getpid(), (int) time(0));
76     try_fwrite(world.server_test, strlen(world.server_test), 1,
77                world.file_out, f_name);
78     fflush(world.file_out);
79     set_cleanup_flag(CLEANUP_OUT);
80     if (!access(world.path_in, F_OK))  /* This keeps out input from old input */
81     {                                  /* file streams of clients             */
82         unlink(world.path_in);         /* communicating with server processes */
83     }                                  /* superseded by this current one.     */
84     world.file_in = try_fopen(world.path_in, "w", f_name);
85     try_fclose(world.file_in, f_name);
86     world.file_in = try_fopen(world.path_in, "r", f_name);
87     set_cleanup_flag(CLEANUP_IN);
88 }
89
90
91
92 extern void remake_world(uint32_t seed)
93 {
94     char * f_name = "remake_world()";
95     free(world.log);
96     world.log = NULL;  /* map_object_action.c's update_log() checks for this. */
97     world.seed = seed;
98     world.map_obj_count = 0;
99     free(world.map.cells);
100     free_map_objects(world.map_objs);
101     world.last_update_turn = 0;
102     init_map();
103     add_map_objects(0, 1);
104     add_map_objects(1, 1 + rrand() % 27);
105     add_map_objects(2, 1 + rrand() % 9);
106     add_map_objects(3, 1 + rrand() % 3);
107     add_map_objects(4, 1 + rrand() % 3);
108     add_map_objects(5, 1 + rrand() % 3);
109     set_cleanup_flag(CLEANUP_MAP_OBJECTS);
110     if (world.turn)
111     {
112         exit_trouble(unlink(world.path_record), f_name, "unlink()");
113     }
114     world.turn = 1;
115 }
116
117
118
119 extern void run_game()
120 {
121     char * f_name = "run_game()";
122     if (!access(world.path_record, F_OK))
123     {
124         FILE * file = try_fopen(world.path_record, "r", f_name);
125         uint32_t linemax = textfile_width(file);
126         char line[linemax + 1];
127         while (   (!world.replay || (world.turn < world.replay))
128                && NULL != try_fgets(line, linemax + 1, file, f_name))
129         {
130             obey_msg(line, 0);
131         }
132         if (!world.replay)
133         {
134             try_fclose(file, f_name);
135             io_loop();
136             return;
137         }
138         uint8_t end = 0;
139         while (!io_loop())
140         {
141             if (!end)
142             {
143                 end = (NULL == try_fgets(line, linemax + 1, file, f_name));
144                 if (!end)
145                 {
146                     obey_msg(line, 0);
147                 }
148             }
149         }
150         try_fclose(file, f_name);
151         return;
152     }
153     exit_err(world.replay, "No record file found to replay.");
154     char * command = "seed";
155     char msg[strlen(command) + 1 + 11 + 1];
156     sprintf(msg, "%s %d", command, (int) time(NULL));
157     obey_msg(msg, 1);
158     io_loop();
159 }