home · contact · privacy
Simplified interface of (build/read/write)_map_objects() by making them decide by...
[plomrogue] / src / main.c
1 /* main.c */
2
3 #include "main.h"
4 #include <stdlib.h> /* for atoi(), exit(), EXIT_FAILURE, calloc() */
5 #include <stdio.h> /* for FILE typedef, F_OK */
6 #include <ncurses.h> /* for initscr(), noecho(), curs_set(), keypad(), raw() */
7 #include <time.h> /* for time() */
8 #include <unistd.h> /* for getopt(), optarg */
9 #include "windows.h" /* for structs WinMeta, Win, init_win(), init_win_meta(),
10                       * draw_all_wins()
11                       */
12 #include "draw_wins.h" /* for draw_keys_win(), draw_map_win(), draw_info_win(),
13                         * draw_log_win()
14                         */
15 #include "keybindings.h" /* for initkeybindings(), get_action_key() */
16 #include "readwrite.h" /* for read_uint16_bigendian, read_uint32_bigendian,
17                         * write_uint32_bigendian
18                         */
19 #include "map_objects.h" /* for structs Monster, Item, Player,
20                           * init_map_object_defs(), read_map_objects(),
21                           * build_map_objects()
22                           */
23 #include "map_object_actions.h" /* for player_wait(), move_player() */
24 #include "map.h" /* for struct Map, init_map() */
25 #include "misc.h" /* for update_log(), toggle_window(), exit_game(),
26                    * find_passable_pos(), meta_keys(), save_game()
27                    */
28 #include "yx_uint16.h" /* for dir enum */
29 #include "rrand.h" /* for rrand(), rrand_seed() */
30
31 int main(int argc, char *argv[])
32 {
33     struct World world;
34
35     /* Read in startup options (i.e. replay option and replay start turn). */
36     int opt;
37     uint32_t start_turn;
38     world.interactive = 1;
39     while ((opt = getopt(argc, argv, "s::")) != -1)
40     {
41         switch (opt)
42         {
43             case 's':
44                 world.interactive = 0;
45                 start_turn = 0;
46                 if (optarg)
47                     start_turn = atoi(optarg);
48                  break;
49             default:
50                 exit(EXIT_FAILURE);
51         }
52     }
53
54     /* Initialize log, player, monster/item definitions and monsters/items. */
55     world.log = calloc(1, sizeof(char));
56     update_log (&world, " ");
57     struct Player player;
58     player.hitpoints = 5;
59     world.player = &player;
60     world.monster = 0;
61     world.item = 0;
62     init_map_object_defs(&world, "defs");
63
64     /* For interactive mode, try to load world state from savefile. */
65     FILE * file;
66     if (1 == world.interactive && 0 == access("savefile", F_OK))
67     {
68         file = fopen("savefile", "r");
69         world.seed = read_uint32_bigendian(file);
70         world.turn = read_uint32_bigendian(file);
71         player.pos.y = read_uint16_bigendian(file) - 1;
72         player.pos.x = read_uint16_bigendian(file) - 1;
73         player.hitpoints = fgetc(file);
74         read_map_objects(&world, &world.monster, file);
75         read_map_objects(&world, &world.item,    file);
76         fclose(file);
77     }
78
79     /* For non-interactive mode, try to load world state from record file. */
80     else
81     {
82         world.turn = 1;
83         if (0 == world.interactive)
84         {
85             file = fopen("record", "r");
86             world.seed = read_uint32_bigendian(file);
87         }
88
89       /* For interactive-mode in newly started world, generate a start seed
90        * from the current time.
91        */
92       else
93       {
94           file = fopen("record", "w");
95           world.seed = time(NULL);
96           write_uint32_bigendian(world.seed, file);
97           fclose(file);
98       }
99   }
100
101     /* Generate map from seed and, if newly generated world, start positions of
102      * actors.
103      */
104     rrand_seed(world.seed);
105     struct Map map = init_map();
106     world.map = &map;
107     if (1 == world.turn)
108     {
109         player.pos = find_passable_pos(&map);
110         void * foo;
111         foo = build_map_objects(&world, &world.monster, 1, 1 + rrand() % 27);
112         foo = build_map_objects(&world, foo, 2, 1 + rrand() % 9);
113         build_map_objects(&world, foo, 3, 1 + rrand() % 3);
114         foo = build_map_objects(&world, &world.item, 4, 1 + rrand() % 3);
115         build_map_objects(&world, foo, 5, 1 + rrand() % 3);
116     }
117
118     /* Initialize window system and windows. */
119     WINDOW * screen = initscr();
120     noecho();
121     curs_set(0);
122     keypad(screen, TRUE);
123     raw();
124     init_keybindings(&world);
125     struct WinMeta win_meta = init_win_meta(screen);
126     struct Win win_keys = init_win(&win_meta, "Keys", 0, 29, &world, draw_keys_win);
127     struct Win win_info = init_win(&win_meta, "Info", 2, 20, &world, draw_info_win);
128     uint16_t height_logwin  = win_meta.padframe.size.y - (2 + win_info.frame.size.y);
129     struct Win win_log = init_win(&win_meta, "Log",
130                                   height_logwin, 20, &world, draw_log_win);
131     uint16_t width_mapwin = win_meta.padframe.size.x - win_keys.frame.size.x
132                              - win_log.frame.size.x - 2;
133     struct Win win_map = init_win(&win_meta, "Map",
134                                   0, width_mapwin, &world, draw_map_win);
135     toggle_window(&win_meta, &win_keys);
136     toggle_window(&win_meta, &win_map);
137     toggle_window(&win_meta, &win_info);
138     toggle_window(&win_meta, &win_log);
139
140     /* Replay mode. */
141     int key;
142     unsigned char quit_called = 0;
143     unsigned char await_actions = 1;
144     if (0 == world.interactive)
145     {
146         int action;
147         while (1)
148         {
149             if (start_turn == world.turn)
150             {
151                 start_turn = 0;
152             }
153             if (0 == start_turn)
154             {
155                 draw_all_wins (&win_meta);
156                key = getch();
157             }
158             if (1 == await_actions
159                 && (world.turn < start_turn
160                     || key == get_action_key(world.keybindings,
161                                              "wait / next turn")) )
162             {
163                 action = getc(file);
164                 if (EOF == action)
165                 {
166                     start_turn = 0;
167                     await_actions = 0;
168                 }
169                 else if (0 == action)
170                 {
171                     player_wait (&world);
172                 }
173                 else if (NORTH == action)
174                 {
175                     move_player(&world, NORTH);
176                 }
177                 else if (EAST  == action)
178                 {
179                     move_player(&world, EAST);
180                 }
181                 else if (SOUTH == action)
182                 {
183                     move_player(&world, SOUTH);
184                 }
185                 else if (WEST == action)
186                 {
187                     move_player(&world, WEST);
188                 }
189             }
190             else
191             {
192                 quit_called = meta_keys(key, &world, &win_meta, &win_keys,
193                                              &win_map, &win_info, &win_log);
194                 if (1 == quit_called)
195                 {
196                     exit_game(&world, &map);
197                 }
198             }
199         }
200     }
201
202     /* Interactive mode. */
203     else
204     {
205         uint32_t last_turn = 0;
206         while (1)
207         {
208             if (last_turn != world.turn)
209             {
210                 save_game(&world);
211                 last_turn = world.turn;
212             }
213             if (1 == await_actions && 0 == player.hitpoints)
214             {
215                 await_actions = 0;
216             }
217             draw_all_wins (&win_meta);
218             key = getch();
219             if      (1 == await_actions
220                      && key == get_action_key(world.keybindings,
221                                               "player up"))
222             {
223                 move_player(&world, NORTH);
224             }
225             else if (1 == await_actions
226                      && key == get_action_key(world.keybindings,
227                                               "player right"))
228             {
229                 move_player(&world, EAST);
230             }
231             else if (1 == await_actions
232                      && key == get_action_key(world.keybindings,
233                                               "player down"))
234             {
235                 move_player(&world, SOUTH);
236             }
237             else if (1 == await_actions
238                      && key == get_action_key(world.keybindings,
239                                               "player left"))
240             {
241                 move_player(&world, WEST);
242             }
243             else if (1 == await_actions
244                      && key == get_action_key(world.keybindings,
245                                               "wait / next turn"))
246             {
247                 player_wait (&world);
248             }
249             else
250             {
251                 quit_called = meta_keys(key, &world, &win_meta, &win_keys,
252                                              &win_map, &win_info, &win_log);
253                 if (1 == quit_called)
254                 {
255                     exit_game(&world, &map);
256                 }
257             }
258         }
259     }
260 }