home · contact · privacy
Server: Make config file set number of objects' start appearances.
[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         add_map_objects(mod->id, mod->start_n);
93     }
94     set_cleanup_flag(CLEANUP_MAP_OBJECTS);
95     if (world.turn)
96     {
97         exit_trouble(unlink(world.path_record), f_name, "unlink()");
98     }
99     world.turn = 1;
100 }
101
102
103
104 extern void run_game()
105 {
106     char * f_name = "run_game()";
107     if (!access(world.path_record, F_OK))
108     {
109         FILE * file = try_fopen(world.path_record, "r", f_name);
110         uint32_t linemax = textfile_width(file);
111         char line[linemax + 1];
112         while (   (!world.replay || (world.turn < world.replay))
113                && NULL != try_fgets(line, linemax + 1, file, f_name))
114         {
115             obey_msg(line, 0);
116         }
117         if (!world.replay)
118         {
119             try_fclose(file, f_name);
120             io_loop();
121             return;
122         }
123         uint8_t end = 0;
124         while (!io_loop())
125         {
126             if (!end)
127             {
128                 end = (NULL == try_fgets(line, linemax + 1, file, f_name));
129                 if (!end)
130                 {
131                     obey_msg(line, 0);
132                 }
133             }
134         }
135         try_fclose(file, f_name);
136         return;
137     }
138     exit_err(world.replay, "No record file found to replay.");
139     char * command = "seed";
140     char msg[strlen(command) + 1 + 11 + 1];
141     sprintf(msg, "%s %d", command, (int) time(NULL));
142     obey_msg(msg, 1);
143     io_loop();
144 }