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