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