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