home · contact · privacy
ea13bc105afd39ef1ff9dc08b4ca0db77d58ee8b
[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(), exit_trouble() */
19 #include "../common/try_malloc.h" /* try_malloc() */
20 #include "cleanup.h" /* set_cleanup_flag() */
21 #include "field_of_view.h" /* build_fov_map() */
22 #include "hardcoded_strings.h" /* s */
23 #include "map.h" /* remake_map() */
24 #include "things.h" /* Thing, ThingType, free_things(), add_things(),
25                      * get_player()
26                      */
27 #include "run.h" /* obey_msg(), io_loop() */
28 #include "world.h" /* global world */
29
30
31
32
33 /* Replay game from record file up to the turn named in world.replay, then turn
34  * over to manual replay via io_loop().
35  */
36 static void replay_game();
37
38
39
40 static void replay_game()
41 {
42     char * f_name = "replay_game()";
43     exit_err(access(s[S_PATH_RECORD], F_OK), "No record found to replay.");
44     FILE * file = try_fopen(s[S_PATH_RECORD], "r", f_name);
45     uint32_t linemax = textfile_width(file);
46     char * line = try_malloc(linemax + 1, f_name);
47     while (   world.turn < world.replay
48            && NULL != try_fgets(line, linemax + 1, file, f_name))
49     {
50         obey_msg(line, 0);
51     }
52     uint8_t end = 0;
53     while (!io_loop())
54     {
55         if (!end)
56         {
57             end = (NULL == try_fgets(line, linemax + 1, file, f_name));
58             if (!end)
59             {
60                 obey_msg(line, 0);
61             }
62         }
63     }
64     free(line);
65     try_fclose(file, f_name);
66 }
67
68
69
70 extern void obey_argv(int argc, char * argv[])
71 {
72     int opt;
73     while (-1 != (opt = getopt(argc, argv, "vs::")))
74     {
75         if      ('v' == opt)
76         {
77             world.is_verbose = 1;
78         }
79         else if ('s' == opt)
80         {
81             world.replay = 1;
82             if (optarg)
83             {
84                 world.replay = atoi(optarg);
85             }
86         }
87         else
88         {
89             exit(EXIT_FAILURE);
90         }
91     }
92 }
93
94
95
96 extern void setup_server_io()
97 {
98     char * f_name = "setup_server_io()";
99     int test = mkdir("server", 0700);
100     exit_trouble(test && EEXIST != errno, f_name, "mkdir()");
101     world.file_out = try_fopen(s[S_PATH_OUT], "w", f_name);
102     world.server_test = try_malloc(10 + 1 + 10 + 1 + 1, f_name);
103     test = sprintf(world.server_test, "%d %d\n", getpid(), (int) time(0));
104     exit_trouble(test < 0, f_name, s[S_FCN_SPRINTF]);
105     try_fwrite(world.server_test, strlen(world.server_test), 1,
106                world.file_out, f_name);
107     fflush(world.file_out);
108     set_cleanup_flag(CLEANUP_OUT);
109     char * path_in = s[S_PATH_IN];
110     if (!access(path_in, F_OK))        /* This keeps out input from old input */
111     {                                  /* file streams of clients             */
112         unlink(path_in)   ;            /* communicating with server processes */
113     }                                  /* superseded by this current one.     */
114     world.file_in = try_fopen(path_in, "w", f_name);
115     try_fclose(world.file_in, f_name);
116     world.file_in = try_fopen(path_in, "r", f_name);
117     set_cleanup_flag(CLEANUP_IN);
118 }
119
120
121
122 extern void remake_world()
123 {
124     char * f_name = "remake_world()";
125     free(world.log);
126     world.log = NULL;      /* thing_actions.c's update_log() checks for this. */
127     world.seed_map = world.seed;
128     free_things(world.things);
129     world.last_update_turn = 0;
130     remake_map();
131     struct ThingType * tt;
132     for (tt = world.thing_types; NULL != tt; tt = tt->next)
133     {
134         if (world.player_type == tt->id)
135         {
136             add_things(tt->id, tt->start_n);
137             break;
138         }
139     }
140     for (tt = world.thing_types; NULL != tt; tt = tt->next)
141     {
142         if (world.player_type != tt->id)
143         {
144             add_things(tt->id, tt->start_n);
145         }
146     }
147     set_cleanup_flag(CLEANUP_THINGS);
148     struct Thing * t;
149     for (t = world.things; NULL != t; t = t->next)
150     {
151         t->fov_map = t->lifepoints ? build_fov_map(t) : NULL;
152     }
153     if (!world.replay && !access(s[S_PATH_RECORD], F_OK))
154     {
155         exit_trouble(unlink(s[S_PATH_RECORD]), f_name, "unlink()");
156     }
157     world.turn = 1;
158 }
159
160
161
162 extern void run_game()
163 {
164     char * f_name = "run_game()";
165     if (world.replay)
166     {
167         replay_game();
168         return;
169     }
170     char * path_savefile = s[S_PATH_SAVE];
171     if (!access(path_savefile, F_OK))
172     {
173         FILE * file = try_fopen(path_savefile, "r", f_name);
174         uint32_t linemax = textfile_width(file);
175         char * line = try_malloc(linemax + 1, f_name);
176         while (NULL != try_fgets(line, linemax + 1, file, f_name))
177         {
178             if (strlen(line) && strcmp("\n", line))
179             {
180                 obey_msg(line, 0);
181             }
182         }
183         free(line);
184         try_fclose(file, f_name);
185     }
186     else
187     {
188         char * command = s[S_CMD_MAKE_WORLD];
189         char * msg = try_malloc(strlen(command) + 1 + 11 + 1, f_name);
190         int test = sprintf(msg, "%s %d", command, (int) time(NULL));
191         exit_trouble(test < 0, f_name, s[S_FCN_SPRINTF]);
192         obey_msg(msg, 1);
193         free(msg);
194     }
195     io_loop();
196 }