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