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