8 #include "keybindings.h"
10 #include "map_objects.h"
11 #include "map_object_actions.h"
15 int main (int argc, char *argv[]) {
18 // Read in startup options (i.e. replay option and replay start turn).
21 world.interactive = 1;
22 while ((opt = getopt(argc, argv, "s::")) != -1) {
25 world.interactive = 0;
28 start_turn = atoi(optarg);
31 exit(EXIT_FAILURE); } }
33 // Initialize log, player, monster/item definitions and monsters/items.
34 world.log = calloc(1, sizeof(char));
35 update_log (&world, " ");
38 world.player = &player;
41 init_map_object_defs(&world, "defs");
43 // For interactive mode, try to load world state from savefile.
45 if (1 == world.interactive && 0 == access("savefile", F_OK)) {
46 file = fopen("savefile", "r");
47 world.seed = read_uint32_bigendian(file);
48 world.turn = read_uint32_bigendian(file);
49 player.pos.y = read_uint16_bigendian(file) - 1;
50 player.pos.x = read_uint16_bigendian(file) - 1;
51 player.hitpoints = fgetc(file);
52 read_map_objects (&world.monster, file, sizeof(struct Monster), read_map_objects_monsterdata);
53 read_map_objects (&world.item, file, sizeof(struct Item), NULL);
56 // For non-interactive mode, try to load world state from record file.
59 if (0 == world.interactive) {
60 file = fopen("record", "r");
61 world.seed = read_uint32_bigendian(file); }
63 // For interactive-mode in newly started world, generate a start seed from the current time.
65 file = fopen("record", "w");
66 world.seed = time(NULL);
67 write_uint32_bigendian(world.seed, file);
70 // Generate map from seed and, if newly generated world, start positions of actors.
72 struct Map map = init_map();
74 if (1 == world.turn) {
75 player.pos = find_passable_pos(&map);
76 void * foo = build_map_objects (&world, &world.monster, 0, 1 + rrand(0,0) % 27, sizeof(struct Monster),
77 build_map_objects_monsterdata);
78 foo = build_map_objects (&world, foo, 1, 1 + rrand(0,0) % 9, sizeof(struct Monster),
79 build_map_objects_monsterdata);
80 build_map_objects (&world, foo, 2, 1 + rrand(0,0) % 3, sizeof(struct Monster),
81 build_map_objects_monsterdata);
82 foo = build_map_objects (&world, &world.item, 3, 1 + rrand(0,0) % 3, sizeof(struct Item),
83 build_map_objects_itemdata);
84 build_map_objects (&world, foo, 4, 1 + rrand(0,0) % 3, sizeof(struct Item), build_map_objects_itemdata); }
86 // Initialize window system and windows.
87 WINDOW * screen = initscr();
92 init_keybindings(&world);
93 struct WinMeta win_meta = init_win_meta(screen);
94 struct Win win_keys = init_win(&win_meta, "Keys", &world, draw_keys_win);
95 struct Win win_map = init_win(&win_meta, "Map", &world, draw_map_win);
96 struct Win win_info = init_win(&win_meta, "Info", &world, draw_info_win);
97 struct Win win_log = init_win(&win_meta, "Log", &world, draw_log_win);
98 win_keys.frame.size.x = 29;
99 win_map.frame.size.x = win_meta.pad.size.x - win_keys.frame.size.x - win_log.frame.size.x - 2;
100 win_info.frame.size.y = 2;
101 win_log.frame.size.y = win_meta.pad.size.y - (2 + win_info.frame.size.y);
102 toggle_window(&win_meta, &win_keys);
103 toggle_window(&win_meta, &win_map);
104 toggle_window(&win_meta, &win_info);
105 toggle_window(&win_meta, &win_log);
109 unsigned char quit_called = 0;
110 unsigned char await_actions = 1;
111 if (0 == world.interactive) {
114 if (start_turn == world.turn)
116 if (0 == start_turn) {
117 draw_all_wins (&win_meta);
119 if (1 == await_actions &&
120 (world.turn < start_turn || key == get_action_key(world.keybindings, "wait / next turn")) ) {
125 else if (0 == action)
126 player_wait (&world);
127 else if (NORTH == action)
128 move_player(&world, NORTH);
129 else if (EAST == action)
130 move_player(&world, EAST);
131 else if (SOUTH == action)
132 move_player(&world, SOUTH);
133 else if (WEST == action)
134 move_player(&world, WEST); }
136 quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
137 if (1 == quit_called)
138 exit_game(&world, &map); } }
142 uint32_t last_turn = 0;
144 if (last_turn != world.turn) {
146 last_turn = world.turn; }
147 if (1 == await_actions && 0 == player.hitpoints)
149 draw_all_wins (&win_meta);
151 if (1 == await_actions && key == get_action_key(world.keybindings, "player up"))
152 move_player(&world, NORTH);
153 else if (1 == await_actions && key == get_action_key(world.keybindings, "player right"))
154 move_player(&world, EAST);
155 else if (1 == await_actions && key == get_action_key(world.keybindings, "player down"))
156 move_player(&world, SOUTH);
157 else if (1 == await_actions && key == get_action_key(world.keybindings, "player left"))
158 move_player(&world, WEST);
159 else if (1 == await_actions && key == get_action_key(world.keybindings, "wait / next turn"))
160 player_wait (&world);
162 quit_called = meta_keys(key, &world, &win_meta, &win_keys, &win_map, &win_info, &win_log);
163 if (1 == quit_called)
164 exit_game(&world, &map); } } }