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