home · contact · privacy
MAJOR re-write. Split plomrogue into a server and a client. Re-wrote large parts
[plomrogue] / src / server / init.c
1 /* src/server/init.c */
2
3 #include "init.h"
4 #include <stdint.h> /* uint32_t */
5 #include <stdlib.h> /* exit(), free() */
6 #include <string.h> /* atoi() */
7 #include <time.h> /* time() */
8 #include <unistd.h> /* optarg, getopt(), access() */
9 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), textfile_sizes(),
10                                   * try_fgets()
11                                   */
12 #include "../common/rexit.h" /* exit_err() */
13 #include "cleanup.h" /* set_cleanup_flag() */
14 #include "map_objects.h" /* free_map_objects(), add_map_objects() */
15 #include "map.h" /* init_map() */
16 #include "rrand.h" /* rrand() */
17 #include "run.h" /* obey_msg(), io_loop() */
18 #include "world.h" /* global world */
19
20
21
22 extern void obey_argv(int argc, char * argv[])
23 {
24     int opt;
25     while (-1 != (opt = getopt(argc, argv, "vs::")))
26     {
27         if      ('v' == opt)
28         {
29             world.is_verbose = 1;
30         }
31         else if ('s' == opt)
32         {
33             world.replay = 1;
34             if (optarg)
35             {
36                 world.replay = atoi(optarg);
37             }
38         }
39         else
40         {
41             exit(EXIT_FAILURE);
42         }
43     }
44 }
45
46
47
48 extern void remake_world(uint32_t seed)
49 {
50     free(world.log);
51     world.log = NULL;
52     world.seed = seed;
53     world.map_obj_count = 0;
54     world.score = 0;
55     free(world.map.cells);
56     if (world.map_objs)
57     {
58         free_map_objects(world.map_objs);
59     }
60     world.last_update_turn = 0;
61     world.turn = 1;
62     init_map();
63     add_map_objects(0, 1);
64     add_map_objects(1, 1 + rrand() % 27);
65     add_map_objects(2, 1 + rrand() % 9);
66     add_map_objects(3, 1 + rrand() % 3);
67     add_map_objects(4, 1 + rrand() % 3);
68     add_map_objects(5, 1 + rrand() % 3);
69     set_cleanup_flag(CLEANUP_MAP_OBJECTS);
70 }
71
72
73
74 extern void run_game()
75 {
76     char * f_name = "run_game()";
77     if (!access(world.path_record, F_OK))
78     {
79         FILE * file = try_fopen(world.path_record, "r", f_name);
80         uint32_t linemax = textfile_sizes(file, NULL);
81         char line[linemax + 1];
82         while (   (!world.replay || (world.turn < world.replay))
83                && NULL != try_fgets(line, linemax + 1, file, f_name))
84         {
85             obey_msg(line, 0);
86         }
87         if (!world.replay)
88         {
89             try_fclose(file, f_name);
90             io_loop();
91             return;
92         }
93         uint8_t end = 0;
94         while (!io_loop())
95         {
96             if (!end)
97             {
98                 end = (NULL == try_fgets(line, linemax + 1, file, f_name));
99                 if (!end)
100                 {
101                     obey_msg(line, 0);
102                 }
103             }
104         }
105         try_fclose(file, f_name);
106         return;
107     }
108     exit_err(world.replay, "No record file found to replay.");
109     char * command = "seed";
110     char msg[strlen(command) + 1 + 11 + 1];
111     sprintf(msg, "%s %d", command, (int) time(NULL));
112     obey_msg(msg, 1);
113     io_loop();
114 }