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