-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
153 lines (121 loc) · 3.32 KB
/
main.cpp
File metadata and controls
153 lines (121 loc) · 3.32 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <stack> // std::stack
#include <stdlib.h>
#include <time.h> // time
#include <ncurses.h>
#include <csignal>
#include <getopt.h>
#include <iostream>
#include <locale.h>
#include "gamestate.h" // GameState
#include "travelstate.h" // TravelState
#include "combatstate.h" // CombatState
#include "entity.h" // Player
#include "display.h" // Display
#include "logger.h" // Logger
#include "world.h"
#include "roadlocation.h"
void interrupt();
void init(int argc, char** argv);
int main(int argc, char** argv){
// This needs to happen first
init(argc, argv);
int input;
std::stack<GameState*> stateStack;
int combatRoll;
CombatState* cs;
Player pc(20,5,10,5,"Human","Jimbo");
World world;
GameState* currentState = nullptr;
TravelState* t = new TravelState(world.getLocationAt(0,0),Util::North);
stateStack.push(t);
//pc.printInventory();
while(true){
pc.printInventory();
world.drawMap(pc.getPos());
currentState = stateStack.top();
if (currentState->combatProbability > 0){
combatRoll = rand() % 100 + 1;
if (combatRoll < currentState->combatProbability){
wprintw(Display::text_win, "You get attacked!\n");
cs = new CombatState();
stateStack.push(cs);
currentState = cs;
}
}
currentState->printOptions();
wrefresh(Display::text_win);
input = wgetch(Display::text_win);
wclear(Display::text_win);
currentState->handleInput(input, stateStack, pc, world);
}
endwin();
Logger::close();
return 0;
}
void interrupt(int i){
Logger::debug("Closing...\n");
Logger::close();
endwin();
exit(i);
};
void init(int argc, char** argv){
// For utf-8 glyphs
setlocale(LC_ALL, "");
// register the handler to clean up ncurses on close
signal(SIGINT, interrupt);
int c;
bool has_log_option = false;
while (1) {
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"log-level", required_argument, 0, 'l'},
{0, 0, 0, 0}
};
/* getopt_long stores the option index here. */
int option_index = 0;
c = getopt_long (argc, argv, "?:l:", //"abc:d:f:",
long_options, &option_index);
/* Detect the end of the options. */
if (c == -1){
break;
}
switch (c) {
case '?':
printf("TextAdventure!\n");
printf("Executable usage:\n");
printf("\t./main [options]\n\n");
printf("Where [options] is one of the following:\n");
printf("\t--log-level=[level], -l [level]\n");
printf("\t\t[level] can be:\n");
printf("\t\t\tdebug\n");
printf("\t\t\tinfo\n");
printf("\t\t\terror (default)\n");
printf("\t--help, -?\n");
printf("\t\tPrints this message.\n");
exit(0);
break;
case 'l':
if (strcmp(optarg,"debug") == 0){
Logger::init(Logger::DEBUG);
} else if (strcmp(optarg,"info") == 0){
Logger::init(Logger::INFO);
} else {
printf("Invalid log level: %s\n", optarg);
exit(1);
}
has_log_option = true;
break;
default:
abort ();
}
}
// TODO: make this default to ERROR
if (!has_log_option){
Logger::init(Logger::DEBUG);
}
Logger::debug("Loading...\n");
// initialize UI
Display::init();
// Seed the rng
srand(time(0));
}