-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
114 lines (93 loc) · 2.2 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <curses.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
#include "utils.h"
#include "level.h"
#include "mob.h"
#include "item.h"
#include "player.h"
#include "list.h"
/** Whether to quit the game or not. */
bool quit = false;
/**
* Catch a sigint and exit gracefully
*/
void catch_sigint(int dummy) {
(void) dummy;
quit = true;
}
/** Entry point. */
int main() {
/* Initialise curses */
WINDOW * mainwin = initscr();
start_color();
cbreak();
noecho();
nonl();
intrflush(stdscr, false);
keypad(stdscr, true);
start_color();
curs_set(0);
init_color(COLOR_BLUE, 250, 250, 250);
/* Attach the signal handler */
signal(SIGINT, catch_sigint);
srand(time(NULL));
Mob * player = create_player();
Level * level_head = xalloc(Level);
level_head->mobs = &player->moblist;
level_head->player = player;
player->level = level_head;
build_level(level_head);
player->xpos = level_head->startx;
player->ypos = level_head->starty;
level_head->cells[player->xpos][player->ypos]->occupant = player;
#ifndef AUTOPLAY
/* Intro text */
mvaddprintf( 5, 60, "Press '?' for help");
mvaddprintf(10, 10, "You enter a cave.");
mvaddprintf(11, 10, "It's beneath the surface.");
mvaddprintf(19, 44, "A game for Ludum Dare 29 by HackSoc.");
if (getch() == '?') {
show_help();
}
#endif // AUTOPLAY
clear();
/* Game loop */
while(!quit) {
/* Update mobs */
run_turn(player->level);
clear();
}
/* Free the things */
while (level_head != NULL) {
Level * level = level_head;
level_head = (level_head->levels.next == NULL) ? NULL : fromlist(Level, levels, level_head->levels.next);
Mob * mob = fromlist(Mob, moblist, level->mobs);
while (mob != NULL) {
mob = kill_mob(mob);
}
for (int x = 0; x < LEVELWIDTH; x++) {
for (int y = 0; y < LEVELHEIGHT; y++) {
List * inventory = level->cells[x][y]->items;
while (inventory != NULL) {
Item * tmp = fromlist(Item, inventory, inventory);
inventory = inventory->next;
xfree(tmp);
}
xfree(level->cells[x][y]);
}
xfree(level->cells[x]);
}
xfree(level->cells);
xfree(level);
}
/* Deinitialise curses */
curs_set(1);
nl();
echo();
nocbreak();
delwin(mainwin);
endwin();
}