home · contact · privacy
624d800f6db17a0492922fc1aba7fe867fb66f57
[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[linemax + 1];
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     try_fclose(file, f_name);
65 }
66
67
68
69 extern void obey_argv(int argc, char * argv[])
70 {
71     int opt;
72     while (-1 != (opt = getopt(argc, argv, "vs::")))
73     {
74         if      ('v' == opt)
75         {
76             world.is_verbose = 1;
77         }
78         else if ('s' == opt)
79         {
80             world.replay = 1;
81             if (optarg)
82             {
83                 world.replay = atoi(optarg);
84             }
85         }
86         else
87         {
88             exit(EXIT_FAILURE);
89         }
90     }
91 }
92
93
94
95 extern void setup_server_io()
96 {
97     char * f_name = "setup_server_io()";
98     int test = mkdir("server", 0700);
99     exit_trouble(test && EEXIST != errno, f_name, "mkdir()");
100     world.file_out = try_fopen(s[PATH_OUT], "w", f_name);
101     world.server_test = try_malloc(10 + 1 + 10 + 1 + 1, f_name);
102     sprintf(world.server_test, "%d %d\n", getpid(), (int) time(0));
103     try_fwrite(world.server_test, strlen(world.server_test), 1,
104                world.file_out, f_name);
105     fflush(world.file_out);
106     set_cleanup_flag(CLEANUP_OUT);
107     char * path_in = s[PATH_IN];
108     if (!access(path_in, F_OK))        /* This keeps out input from old input */
109     {                                  /* file streams of clients             */
110         unlink(path_in)   ;            /* communicating with server processes */
111     }                                  /* superseded by this current one.     */
112     world.file_in = try_fopen(path_in, "w", f_name);
113     try_fclose(world.file_in, f_name);
114     world.file_in = try_fopen(path_in, "r", f_name);
115     set_cleanup_flag(CLEANUP_IN);
116 }
117
118
119
120 extern void remake_world()
121 {
122     char * f_name = "remake_world()";
123     free(world.log);
124     world.log = NULL;      /* thing_actions.c's update_log() checks for this. */
125     world.seed_map = world.seed;
126     free_things(world.things);
127     world.last_update_turn = 0;
128     remake_map();
129     struct ThingType * tt;
130     for (tt = world.thing_types; NULL != tt; tt = tt->next)
131     {
132         if (world.player_type == tt->id)
133         {
134             add_things(tt->id, tt->start_n);
135             break;
136         }
137     }
138     for (tt = world.thing_types; NULL != tt; tt = tt->next)
139     {
140         if (world.player_type != tt->id)
141         {
142             add_things(tt->id, tt->start_n);
143         }
144     }
145     set_cleanup_flag(CLEANUP_THINGS);
146     struct Thing * t;
147     for (t = world.things; NULL != t; t = t->next)
148     {
149         t->fov_map = t->lifepoints ? build_fov_map(t) : NULL;
150     }
151     if (!world.replay && !access(s[PATH_RECORD], F_OK))
152     {
153         exit_trouble(unlink(s[PATH_RECORD]), f_name, "unlink()");
154     }
155     world.turn = 1;
156 }
157
158
159
160 extern void run_game()
161 {
162     char * f_name = "run_game()";
163     if (world.replay)
164     {
165         replay_game();
166         return;
167     }
168     char * path_savefile = s[PATH_SAVE];
169     if (!access(path_savefile, F_OK))
170     {
171         FILE * file = try_fopen(path_savefile, "r", f_name);
172         uint32_t linemax = textfile_width(file);
173         char line[linemax + 1];
174         while (NULL != try_fgets(line, linemax + 1, file, f_name))
175         {
176             if (strlen(line) && strcmp("\n", line))
177             {
178                 obey_msg(line, 0);
179             }
180         }
181         try_fclose(file, f_name);
182     }
183     else
184     {
185         char * command = s[CMD_MAKE_WORLD];
186         char msg[strlen(command) + 1 + 11 + 1];
187         sprintf(msg, "%s %d", command, (int) time(NULL));
188         obey_msg(msg, 1);
189     }
190     io_loop();
191 }