1 /* src/server/init.c */
3 #define _POSIX_C_SOURCE 2 /* getopt(), optarg */
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()
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(),
26 #include "run.h" /* obey_msg(), io_loop() */
27 #include "world.h" /* global world */
31 extern void obey_argv(int argc, char * argv[])
34 while (-1 != (opt = getopt(argc, argv, "vs::")))
45 world.replay = atoi(optarg);
57 extern void setup_server_io()
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);
81 extern void remake_world(uint32_t seed)
83 char * f_name = "remake_world()";
85 world.log = NULL; /* thing_actions.c's update_log() checks for this. */
87 world.thing_count = 0;
88 free(world.map.cells);
89 free_things(world.things);
90 world.last_update_turn = 0;
92 struct ThingType * tt;
93 for (tt = world.thing_types; NULL != tt; tt = tt->next)
95 if (world.player_type == tt->id)
97 add_things(tt->id, tt->start_n);
101 for (tt = world.thing_types; NULL != tt; tt = tt->next)
103 if (world.player_type != tt->id)
105 add_things(tt->id, tt->start_n);
108 set_cleanup_flag(CLEANUP_THINGS);
110 for (t = world.things; NULL != t; t = t->next)
112 t->fov_map = t->lifepoints ? build_fov_map(t) : NULL;
116 exit_trouble(unlink(world.path_record), f_name, "unlink()");
123 extern void run_game()
125 char * f_name = "run_game()";
126 if (!access(world.path_record, F_OK))
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))
138 try_fclose(file, f_name);
147 end = (NULL == try_fgets(line, linemax + 1, file, f_name));
154 try_fclose(file, f_name);
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));