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