-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cpp
More file actions
350 lines (306 loc) · 9.86 KB
/
Copy pathProgram.cpp
File metadata and controls
350 lines (306 loc) · 9.86 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
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
#include <iostream>
#include <ncurses.h>
#include <fstream>
#include <ctime>
#include <cstdlib>
#include <unistd.h>
#include "Block.h"
#include "Logger.h"
//Prototypes
void UpdateTime(int );
void GenerateMap();
void PrintMap();
void PrintCommands();
void ClearStatusLine();
void DeleteMap();
void SetupColors();
//Constants
#define ROW_OFFSET 6
#define COLUMN_OFFSET 35
//Game settings
int rows = 0;
int columns = 0;
int randomTickSpeed = 3;
int speed = 1;
//Global
Block*** map;
int main() {
int seed = time(0);
srand(seed);
//Start ncurses
initscr();
raw();
noecho();
curs_set(0);
keypad(stdscr, TRUE);
mousemask(ALL_MOUSE_EVENTS, NULL);
MEVENT event;
SetupColors();
//Clear log file
std::ofstream file("logs.txt", std::ios::trunc);
//Get terminal size and set r and c respectively
//Also make room for other text
getmaxyx(stdscr, rows, columns);
if (rows < 21 || columns < 80) {
endwin();
std::cout << "\033[2J\033[H";
std::cout << "Please set your terminal size to be bigger than 21x80." << std::endl;
return 0;
}
rows -= ROW_OFFSET;
columns -= COLUMN_OFFSET;
//Allocate the new map size
map = new Block**[rows];
for (int i = 0; i < rows; i++) {
map[i] = new Block*[columns];
}
GenerateMap();
PrintMap();
PrintCommands();
refresh();
//Set up loop
bool running = true;
int command;
bool waitingForInput = false;
Log("Program started with seed: %d", seed);
int ticks = 0;
while (running) {
if (!waitingForInput) {
//Minecraft chooses 3 random blocks (randomTickSpeed) in the chunk and ticks them.
//A tick happens every .05 seconds.
for (int i = 0; i < randomTickSpeed; i++) {
Block* randomBlock = map[rand() % rows][rand() % columns];
randomBlock->Tick(map, rows, columns);
UpdateTime(++ticks/3);
}
PrintMap();
refresh();
//.05 seconds
usleep(50000);
}
//Player commands
timeout(0);
command = getch();
if (command != ERR) {
ClearStatusLine();
// Place Grass Block
if (command == 'G') {
waitingForInput = true;
int x, y;
//Must turn blocking off in order to read the click area
timeout(-1);
command = getch();
if (command == KEY_MOUSE && getmouse(&event) == OK) {
x = event.y;
y = event.x - 4;
}
timeout(0);
if (x > -1 && y > -1) {
refresh();
//Check out of bounds
if (x >= 0 && x < rows && y >= 0 && y < columns) {
delete map[x][y];
map[x][y] = new Grass(x, y);
ClearStatusLine();
mvprintw(rows + 4, 0, "Placed Grass at %d, %d", x, y);
Log("Placed Grass at %d, %d", x, y);
}
}
waitingForInput = false;
}
// Place Mycelium Block
else if (command == 'M') {
waitingForInput = true;
int x, y;
//Must turn blocking off
timeout(-1);
command = getch();
if (command == KEY_MOUSE && getmouse(&event) == OK) {
x = event.y;
y = event.x - 4;
}
timeout(0);
if (x > -1 && y > -1) {
refresh();
//Check out of bounds
if (x >= 0 && x < rows && y >= 0 && y < columns) {
delete map[x][y];
map[x][y] = new Mycelium(x, y);
ClearStatusLine();
mvprintw(rows + 4, 0, "Placed Mycelium at %d, %d", x, y);
Log("Placed Mycelium at %d, %d", x, y);
}
}
waitingForInput = false;
}
//Place Wheat Field
else if (command == 'W') {
waitingForInput = true;
int x1, y1;
int x2, y2;
timeout(-1);
//Get both corners
command = getch();
if (command == KEY_MOUSE && getmouse(&event) == OK) {
x1 = event.y;
y1 = event.x - 4;
}
command = getch();
if (command == KEY_MOUSE && getmouse(&event) == OK) {
x2 = event.y;
y2 = event.x - 4;
}
timeout(0);
if ((x1 > -1 && y1 > -1) && (x2 > -1 && y2 > -1)) {
refresh();
//Check out of bounds
if ((x1 >= 0 && x1 < rows && y1 >= 0 && y1 < columns) && (x2 >= 0 && x2 < rows && y2 >= 0 && y2 < columns)) {
for (int i = x1; i <= x2; i++) {
for (int j = y1; j <= y2; j++) {
delete map[i][j];
map[i][j] = new Wheat(i, j);
}
}
}
}
waitingForInput = false;
}
else if (command == KEY_UP) {
randomTickSpeed *= 5;
Log("Speed increased to %d", randomTickSpeed);
mvprintw(rows + 4, 0, "Speed set to level %d", ++speed);
}
else if (command == KEY_DOWN) {
if (randomTickSpeed != 3) {
randomTickSpeed /= 5;
mvprintw(rows + 4, 0, "Speed set to level %d", --speed);
Log("Speed decreased to %d", randomTickSpeed);
}
}
//Quit
else if (command == 'Q') {
running = false;
Log("Quitting...");
}
}
}
endwin();
DeleteMap();
return 0;
}
void UpdateTime(int ticks) {
//A day is 24,000 ticks.
//1 hour in minecraft is 1000 ticks.
int totalHours = ticks/1000;
int hours = totalHours % 24;
int totalDays = totalHours / 24;
//Daylight cycle function
//Find if Sun or Moon and its position
int sunPos = hours % 24;
int moonPos = (sunPos + 12) % 24;
//Print the sun/moon
for (int i = 0; i <= 24; i++) {
//This was my way of expanding the print of the cycle to be wider
//It works I just feel like there is definitely a better way to do this...
if (i % 2 == 1) continue;
int i2 = i/2;
int pos;
if (i2 > 6) {
pos = 6 - (i2 - 6);
} else {
pos = i2;
}
if (i2 == sunPos) {
attron(COLOR_PAIR(COLOR_YELLOW));
mvprintw(20 - pos, (columns + 7) + i, " ");
attroff(COLOR_PAIR(COLOR_YELLOW));
continue;
}
if (i2 == moonPos) {
attron(COLOR_PAIR(COLOR_BLUE));
mvprintw(20 - pos, (columns + 7) + i, " ");
attroff(COLOR_PAIR(COLOR_BLUE));
continue;
}
mvprintw(20 - pos, (columns + 7) + i, ".");
}
mvprintw(22, columns + 7, "Day: %d", totalDays);
}
void GenerateMap() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
map[i][j] = new Dirt(i, j);
}
}
}
void PrintMap() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
attron(COLOR_PAIR(map[i][j]->color));
mvprintw(i, j + 4, "%c", map[i][j]->icon);
attroff(COLOR_PAIR(map[i][j]->color));
}
}
}
void PrintCommands() {
//Print border
mvaddch(2, columns + 6, ACS_ULCORNER);
//Horizontal is 1/2 a cell so double it
for (int i = 1; i <= 20; i++) {
mvaddch(2, columns + 6 + i, ACS_HLINE);
}
//Vertical
for (int i = 1; i <= 10; i++) {
mvaddch(2 + i, columns + 6, ACS_VLINE);
}
//Print text
mvprintw(1, columns + 7, "COMMANDS");
mvprintw(3, columns + 7, "G - Place Grass");
mvprintw(4, columns + 7, "M - Place Mycelium");
mvprintw(5, columns + 7, "W - Place Wheat");
mvprintw(6, columns + 7, "Up/Down - Change speed ");
mvprintw(12, columns + 7, "Q - Quit");
}
void ClearStatusLine() {
//rows + 4, 0
for (int i = 0; i < columns + COLUMN_OFFSET; i++) {
mvprintw(rows + 4, i, " ");
}
}
void DeleteMap() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
delete map[i][j];
}
}
}
//Actual minecraft colors
enum CustomColors {
COLOR_DIRT = 100,
COLOR_GRASS,
COLOR_MYCELIUM,
COLOR_GREEN_WHEAT,
COLOR_YELLOW_WHEAT,
COLOR_SOIL,
};
//RGB * 1000 / 255
void SetupColors() {
start_color();
init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_YELLOW);
init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLUE);
init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
//Custom Colors
init_color(COLOR_DIRT, 573, 424, 302);
init_pair(COLOR_DIRT, COLOR_DIRT, COLOR_DIRT);
init_color(COLOR_GRASS, 388, 498, 251);
init_pair(COLOR_GRASS, COLOR_GRASS, COLOR_GRASS);
init_color(COLOR_MYCELIUM, 412, 380, 439);
init_pair(COLOR_MYCELIUM, COLOR_MYCELIUM, COLOR_MYCELIUM);
init_color(COLOR_SOIL, 329, 176, 110);
init_pair(COLOR_GREEN_WHEAT, COLOR_GREEN, COLOR_SOIL);
init_pair(COLOR_YELLOW_WHEAT, COLOR_YELLOW, COLOR_SOIL);
}