-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
457 lines (409 loc) · 10.8 KB
/
main.cpp
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
#include <ncurses.h>
#include <vector>
#include <fstream>
//#include "object.h"
//#include "world.h"
#include "split.h"
//color definitions
#define MEADOW_PAIR 1
#define SWAMP_PAIR 2
#define WATER_PAIR 3
#define WALL_PAIR 4
#define DIAMOND_PAIR 5
#define HERO_PAIR 6
void print_lose();
void print_win();
void movecheck();
using namespace std;
void showGrov(int y, int x, grovnik * show) {
if (x < 0 || y < 0 || x > 127 || y > 127) return;
char atpoint = ' ';
if (show -> poi) {
switch (show -> poi -> get_type()) {
case 1: //treasure
atpoint = '$';
break;
case 2: //food
atpoint = 'F';
break;
case 3: //tool
atpoint = 'T';
break;
case 4: //clues
atpoint = '?';
break;
case 5: //ship
atpoint = 'S';
break;
case 6: //binoculars
atpoint = 'B';
break;
case 7: //obstacle
atpoint = '!';
break;
}
}
if (show -> terrain == DIAMOND_PAIR) {
atpoint = '$';
}
attron(COLOR_PAIR(show -> terrain));
mvaddch(y, x, atpoint);
attroff(COLOR_PAIR(show -> terrain));
}
// Move player to y1/x1 depending on terrain and use POI @ y1/x1
// Sorry you have to pass all that by reference but it's out of scope
void move_player(int* playery, int* playerx, int y1, int x1, World* map, int* energy, int* whiffles, bool* binoculars, bool* boat, vector<tool*>* inventory, bool* diamond) {
grovnik* check = map->getAt(y1, x1);
switch (check->terrain) {
case 1: //meadow
*playery = y1;
*playerx = x1;
(*energy)--;
break;
case 2: //swamp
*playery = y1;
*playerx = x1;
*energy -= 2;
break;
case 3: //water
if (*boat) {
*playery = y1;
*playerx = x1;
break;
}
if(*boat)
(*energy)--; //don't want energy to go down if cant move
break;
case 4: //wall
//(*energy)--; //dont want energy to go down if cant move
break;
case 5: //diamond
*playery = y1;
*playerx = x1;
*diamond = true;
break;
}
if (!check->poi) {
return;
}
int objType = check->poi->get_type();
if (objType == 1) { //treasure
*whiffles += check->poi->get_cost();
map->clearPOI(y1, x1);
} else if (objType == 2) { //food
food* foundFood = check->poi->getFood();
if (*whiffles >= foundFood->get_cost()) {
*whiffles -= foundFood->get_cost();
*energy += foundFood->get_value();
map->clearPOI(y1, x1);
}
} else if (objType == 3) { //tools
tool* foundTool = check->poi->getTool();
if (*whiffles >= foundTool->get_cost()) {
*whiffles -= foundTool->get_cost();
map->clearPOI(y1, x1);
inventory->push_back(foundTool);
}
} else if (objType == 4) { //clues
char * hint;
clue * get_clue = check->poi->getClue();
mvprintw(15, 119, " "); // clears text for next use
mvprintw(15, 119, "You found a clue!");
hint = new char[strlen(get_clue->getHint()) + 1];
strcpy(hint, get_clue->getHint());
// displaying the clue close to where the player is
move(*playery + 5, *playerx + 5);
printw("The clue is: %s", hint);
refresh();
//deleting text
mvprintw(16, 119, "Press any key to continue!");
getch();
mvprintw(16, 119, " ");
refresh();
for(unsigned int x = 0; x < strlen("The clue is: ") + strlen(hint); x++) {
if(map->getfog(*playery + 5, *playerx + 5 + x)) {
showGrov(*playery + 5, *playerx + 5 + x, map->getAt(*playery + 5, *playerx + 5 + x));
} else {
mvaddch(*playery + 5, *playerx + 5 + x, ' ');
}
}
delete [] hint;
*energy += 1; // fixed where it would take an energy when you pressed button to continue
map->clearPOI(y1, x1);
} else if (objType == 5) { //ship
char answer;
mvprintw(15, 119, " "); // clears text for next use
mvprintw(15, 119, "You have found a boat!");
mvprintw(16, 119, "Would you like to purchase it?");
mvprintw(17, 119, "Cost: 200 Whiffles");
mvprintw(18, 119, "Press y or n: ");
refresh();
answer = getchar();
// clearing text
mvprintw(15, 119, " ");
mvprintw(16, 119, " ");
mvprintw(17, 119, " ");
mvprintw(18, 119, " ");
if(answer == 'y' || answer == 'Y')
{
if (*whiffles >= check->poi->get_cost()) {
mvprintw(15, 119, "You purchased the boat!");
*whiffles -= check->poi->get_cost();
map->clearPOI(y1, x1);
*boat = true;
}
else
mvprintw(15, 119, "Error! Could not purchase!");
}
else
mvprintw(15, 119, "You did not purchase the boat!");
} else if (objType == 6) { //binoculars
char answer;
mvprintw(15, 119, " "); // clears text for next use
mvprintw(15, 119, "You have found some binoculars!");
mvprintw(16, 119, "Would you like to purchase them?");
mvprintw(17, 119, "Cost: 50 Whiffles");
mvprintw(18, 119, "Press y or n: ");
refresh();
answer = getchar();
// clearing text
mvprintw(15, 119, " ");
mvprintw(16, 119, " ");
mvprintw(17, 119, " ");
mvprintw(18, 119, " ");
if(answer == 'y' || answer == 'Y')
{
if (*whiffles >= check->poi->get_cost()) {
mvprintw(15, 119, "You purchased the binoculars!");
*whiffles -= check->poi->get_cost();
map->clearPOI(y1, x1);
*binoculars = true;
}
else
mvprintw(15, 119, "Error! Could not purchase!");
}
else
mvprintw(15, 119, "You did not purchase the binoculars!");
} else { //obsticles
// TODO
}
}
int main() {
initscr();
keypad(stdscr, true);
noecho();
clear();
//initialize color stuff
start_color();
init_pair(MEADOW_PAIR, COLOR_BLACK, COLOR_GREEN);
init_pair(SWAMP_PAIR, COLOR_BLACK, COLOR_MAGENTA);
init_pair(WATER_PAIR, COLOR_BLACK, COLOR_BLUE);
init_pair(WALL_PAIR, COLOR_BLACK, COLOR_WHITE);
init_pair(DIAMOND_PAIR, COLOR_WHITE, COLOR_CYAN);
init_pair(HERO_PAIR, COLOR_YELLOW, COLOR_RED);
// initialize variables
World map;
int * herox = new int;
int * heroy = new int;
*heroy = 2;
*herox = 2;
char ** obstypes = map.fileRead(heroy, herox);
bool running = true; // program running conditional
// player variables
int energy = 100;
int whiffles = 1000;
bool binoculars = false;
bool boat = false;
vector<tool*> inventory;
bool diamond = false;
// cursor position
int cx = 0;
int cy = 0;
int playerx = *herox;
int playery = *heroy;
delete herox;
delete heroy;
// set values, due to possibility of changing viewport
int Cols = COLS;
int Rows = LINES;
int splitPos = min(128, Cols * 3 / 4);
map.clearfog_rad(playery, playerx, 1);
// Clear map for debugging file reader
//map.clearfog_rad(0, 0, 128);
// Initial world draw
for (int y = 0; y < 128; y++) {
for (int x = 0; x < Cols * 3/4; x++) {
if(map.getfog(y, x)) {
showGrov(y, x, map.getAt(y, x));
} else {
mvaddch(y, x, ' ');
}
}
}
// Initial hero display
attron(COLOR_PAIR(6));
mvaddch(playery, playerx, '@');
attroff(COLOR_PAIR(6));
//Draw the splitscreen split
mvvline(0, splitPos, '|', Rows);
drawValues(splitPos, whiffles, energy, binoculars, boat);
if (map.getfog(cy, cx)) {
drawTerr(splitPos, map.getAt(cy, cx), obstypes);
} else {
drawTerr(splitPos, NULL, obstypes);
}
//drawsplit(whiffles, energy, binoculars, boat, map.getAt(cy, cx), inventory);
while (running) {
int ch = getch();
// User input
switch(ch) {
case 'q': //press q to quit
running = false;
break;
//Player movement
case 'w': //move player up
if (playery) {
move_player(&playery, &playerx, playery - 1, playerx, &map, &energy, &whiffles, &binoculars, &boat, &inventory, &diamond);
}
break;
case 's': //move player down
if (playery < 128) {
move_player(&playery, &playerx, playery + 1, playerx, &map, &energy, &whiffles, &binoculars, &boat, &inventory, &diamond);
}
break;
case 'a': //move player left
if (playerx) {
move_player(&playery, &playerx, playery, playerx - 1, &map, &energy, &whiffles, &binoculars, &boat, &inventory, &diamond);
}
break;
case 'd': //move player right
if (playerx < 128) {
move_player(&playery, &playerx, playery, playerx + 1, &map, &energy, &whiffles, &binoculars, &boat, &inventory, &diamond);
}
break;
case 'c':
map.clearfog_rad(64, 64, 65);
for (int y = 0; y < 128; y++) {
for (int x = 0; x < Cols * 3/4; x++) {
showGrov(y, x, map.getAt(y, x));
}
}
break;
// Cursor movement
case KEY_UP: //move up
if(cy) {
--cy;
}
break;
case KEY_DOWN: //move down
if(cy < Rows - 1) {
++cy;
}
break;
case KEY_LEFT: //move left
if(cx) {
--cx;
}
break;
case KEY_RIGHT: //move right
if(cx < Cols - 1) {
++cx;
}
break;
default:
break;
}
//Player movement post-draw
if (ch == 'w' || ch == 'a' || ch == 's' || ch == 'd') {
showGrov(playery, playerx, map.getAt(playery, playerx));
int radius;
if(binoculars) {
map.clearfog_rad(playery, playerx, 2);
radius = 2;
} else {
map.clearfog_rad(playery, playerx, 1);
radius = 1;
}
for(int y = playery - radius; y <= playery + radius; y++) {
for(int x = playerx - radius; x <= playerx + radius; x++) {
showGrov(y, x, map.getAt(y, x));
}
}
// hero display post-move
attron(COLOR_PAIR(6));
mvaddch(playery, playerx, '@');
attroff(COLOR_PAIR(6));
}
// If the user types something, draw over it
if (ch != -1) {
if (cx < splitPos){ // Redraw map info
if(map.getfog(cy, cx)) {
showGrov(cy, cx, map.getAt(cy, cx));
} else {
mvaddch(cy, cx, ' ');
}
// hero display. In case you typed over it
attron(COLOR_PAIR(6));
mvaddch(playery, playerx, '@');
attroff(COLOR_PAIR(6));
}
if (cx == splitPos) { //Redraw the screen split
mvaddch(cy, splitPos, '|');
}
if (cx > splitPos){ // Redraw splitscreen blank background
mvaddch(cy, cx, ' ');
}
}
drawValues(splitPos, whiffles, energy, binoculars, boat);
if (map.getfog(cy, cx)) {
drawTerr(splitPos, map.getAt(cy, cx), obstypes);
} else {
drawTerr(splitPos, NULL, obstypes);
}
//After the mvaddch, because they move the cursor as well
move(cy, cx);
if(energy <= 0)
running = false;
if(diamond)
running = false;
refresh();
}
if(energy <= 0)
print_lose();
if(diamond)
print_win();
// obstypes de-allocation
for (int i=0; i < 10; ++i) {
delete[] obstypes[i];
}
delete[] obstypes;
return endwin();
}
void print_lose()
{
beep();
flash();
wrefresh(stdscr);
initscr();
noecho();
mvprintw((LINES/2), (COLS/2)-8, "YOU LOSE");
move(0,0);
refresh();
getch();
endwin();
refresh();
}
void print_win()
{
beep();
flash();
wrefresh(stdscr);
initscr();
noecho();
mvprintw((LINES/2), (COLS/2)-8, "YOU WIN");
move(0,0);
refresh();
getch();
endwin();
refresh();
}