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(),
12 #include "draw_wins.h" /* for draw_keys_win(), draw_map_win(), draw_info_win(),
15 #include "keybindings.h" /* for initkeybindings(), get_action_key() */
16 #include "readwrite.h" /* for read_uint16_bigendian, read_uint32_bigendian,
17 * write_uint32_bigendian
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()
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()
29 #include "yx_uint16.h" /* for dir enum */
30 #include "rrand.h" /* for rrand(), rrand_seed() */
32 int main(int argc, char *argv[])
36 /* Read in startup options (i.e. replay option and replay start turn). */
39 world.interactive = 1;
40 while ((opt = getopt(argc, argv, "s::")) != -1)
45 world.interactive = 0;
48 start_turn = atoi(optarg);
55 /* Initialize log, player, monster/item definitions and monsters/items. */
56 world.log = calloc(1, sizeof(char));
57 update_log (&world, " ");
60 world.player = &player;
63 init_map_object_defs(&world, "defs");
65 /* For interactive mode, try to load world state from savefile. */
67 if (1 == world.interactive && 0 == access("savefile", F_OK))
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);
81 /* For non-interactive mode, try to load world state from record file. */
85 if (0 == world.interactive)
87 file = fopen("record", "r");
88 world.seed = read_uint32_bigendian(file);
91 /* For interactive-mode in newly started world, generate a start seed
92 * from the current time.
96 file = fopen("record", "w");
97 world.seed = time(NULL);
98 write_uint32_bigendian(world.seed, file);
103 /* Generate map from seed and, if newly generated world, start positions of
106 rrand_seed(world.seed);
107 struct Map map = init_map();
111 player.pos = find_passable_pos(&map);
112 void * foo = build_map_objects(&world, &world.monster,
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,
124 build_map_objects_itemdata);
125 build_map_objects(&world, foo, 4, 1 + rrand() % 3,
126 sizeof(struct Item), build_map_objects_itemdata);
129 /* Initialize window system and windows. */
130 WINDOW * screen = initscr();
133 keypad(screen, TRUE);
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);
153 unsigned char quit_called = 0;
154 unsigned char await_actions = 1;
155 if (0 == world.interactive)
160 if (start_turn == world.turn)
166 draw_all_wins (&win_meta);
169 if (1 == await_actions
170 && (world.turn < start_turn
171 || key == get_action_key(world.keybindings,
172 "wait / next turn")) )
180 else if (0 == action)
182 player_wait (&world);
184 else if (NORTH == action)
186 move_player(&world, NORTH);
188 else if (EAST == action)
190 move_player(&world, EAST);
192 else if (SOUTH == action)
194 move_player(&world, SOUTH);
196 else if (WEST == action)
198 move_player(&world, WEST);
203 quit_called = meta_keys(key, &world, &win_meta, &win_keys,
204 &win_map, &win_info, &win_log);
205 if (1 == quit_called)
207 exit_game(&world, &map);
213 /* Interactive mode. */
216 uint32_t last_turn = 0;
219 if (last_turn != world.turn)
222 last_turn = world.turn;
224 if (1 == await_actions && 0 == player.hitpoints)
228 draw_all_wins (&win_meta);
230 if (1 == await_actions
231 && key == get_action_key(world.keybindings,
234 move_player(&world, NORTH);
236 else if (1 == await_actions
237 && key == get_action_key(world.keybindings,
240 move_player(&world, EAST);
242 else if (1 == await_actions
243 && key == get_action_key(world.keybindings,
246 move_player(&world, SOUTH);
248 else if (1 == await_actions
249 && key == get_action_key(world.keybindings,
252 move_player(&world, WEST);
254 else if (1 == await_actions
255 && key == get_action_key(world.keybindings,
258 player_wait (&world);
262 quit_called = meta_keys(key, &world, &win_meta, &win_keys,
263 &win_map, &win_info, &win_log);
264 if (1 == quit_called)
266 exit_game(&world, &map);