home · contact · privacy
a91bd2aaac3c5a4d7898d1f2f4b8d25699e255db
[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 "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[PATH_RECORD], F_OK), "No record found to replay.");
44     FILE * file = try_fopen(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[PATH_OUT], "w", f_name);
102     world.server_test = try_malloc(10 + 1 + 10 + 1 + 1, f_name);
103     sprintf(world.server_test, "%d %d\n", getpid(), (int) time(0));
104     try_fwrite(world.server_test, strlen(world.server_test), 1,
105                world.file_out, f_name);
106     fflush(world.file_out);
107     set_cleanup_flag(CLEANUP_OUT);
108     char * path_in = s[PATH_IN];
109     if (!access(path_in, F_OK))        /* This keeps out input from old input */
110     {                                  /* file streams of clients             */
111         unlink(path_in)   ;            /* communicating with server processes */
112     }                                  /* superseded by this current one.     */
113     world.file_in = try_fopen(path_in, "w", f_name);
114     try_fclose(world.file_in, f_name);
115     world.file_in = try_fopen(path_in, "r", f_name);
116     set_cleanup_flag(CLEANUP_IN);
117 }
118
119
120
121 extern void remake_world()
122 {
123     char * f_name = "remake_world()";
124     free(world.log);
125     world.log = NULL;      /* thing_actions.c's update_log() checks for this. */
126     world.seed_map = world.seed;
127     free_things(world.things);
128     world.last_update_turn = 0;
129     remake_map();
130     struct ThingType * tt;
131     for (tt = world.thing_types; NULL != tt; tt = tt->next)
132     {
133         if (world.player_type == tt->id)
134         {
135             add_things(tt->id, tt->start_n);
136             break;
137         }
138     }
139     for (tt = world.thing_types; NULL != tt; tt = tt->next)
140     {
141         if (world.player_type != tt->id)
142         {
143             add_things(tt->id, tt->start_n);
144         }
145     }
146     set_cleanup_flag(CLEANUP_THINGS);
147     struct Thing * t;
148     for (t = world.things; NULL != t; t = t->next)
149     {
150         t->fov_map = t->lifepoints ? build_fov_map(t) : NULL;
151     }
152     if (!world.replay && !access(s[PATH_RECORD], F_OK))
153     {
154         exit_trouble(unlink(s[PATH_RECORD]), f_name, "unlink()");
155     }
156     world.turn = 1;
157 }
158
159
160
161 extern void run_game()
162 {
163     char * f_name = "run_game()";
164     if (world.replay)
165     {
166         replay_game();
167         return;
168     }
169     char * path_savefile = s[PATH_SAVE];
170     if (!access(path_savefile, F_OK))
171     {
172         FILE * file = try_fopen(path_savefile, "r", f_name);
173         uint32_t linemax = textfile_width(file);
174         char * line = try_malloc(linemax + 1, f_name);
175         while (NULL != try_fgets(line, linemax + 1, file, f_name))
176         {
177             if (strlen(line) && strcmp("\n", line))
178             {
179                 obey_msg(line, 0);
180             }
181         }
182         free(line);
183         try_fclose(file, f_name);
184     }
185     else
186     {
187         char * command = s[CMD_MAKE_WORLD];
188         char * msg = try_malloc(strlen(command) + 1 + 11 + 1, f_name);
189         sprintf(msg, "%s %d", command, (int) time(NULL));
190         obey_msg(msg, 1);
191         free(msg);
192     }
193     io_loop();
194 }