-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
283 lines (239 loc) · 8.57 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
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
#define _POSIX_C_SOURCE 200201L
#define _GNU_SOURCE //for asprintf function when creating new save file name
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
//#include <time.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <locale.h>
//#define _POSIX_C_SOURCE 200201L
int main(int argc, char *argv[]){
setlocale(LC_ALL, "UTF-8");
srand(time(NULL));
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
// enable mouse clicking
curs_set(FALSE);
nodelay(stdscr, TRUE);
start_color();
//default color
init_pair(1, COLOR_WHITE, COLOR_BLACK);
//error color
init_pair(2, COLOR_RED, COLOR_BLACK);
//window background color
init_pair(3, COLOR_BLACK, COLOR_WHITE);
//tutorial 'chapters' color
init_pair(4, COLOR_GREEN, COLOR_BLACK);
//yellow looks a little bit like brown so its used for lumberjack symbol
init_pair(5, COLOR_YELLOW, COLOR_BLACK);
int gold_count = 2500; //track gold resource count
int wood_count = 1000; //track wood resource count
int army_count = 0; //track army resource count
int day_count = 0;
struct Building bank, lumberjack, barracks, hospital;
initialize_building("Bank", 1, 2500, 1000, 0, &bank);
initialize_building("Lumberjack", 1, 1000, 500, 200, &lumberjack);
initialize_building("Barracks", 0, 1000, 500, 0, &barracks);
initialize_building("Hospital", 0, 3000, 2500, 1000, &hospital);
struct Accessories trees, bank_statues, army_statues, guard_towers;
initialize_accessory(0, 10, 0, 150, 250, 50, &trees, "Tree");
initialize_accessory(0, 3, 0, 1500, 300, 200, &bank_statues, "Bank statue");
initialize_accessory(0, 3, 0, 1500, 500, 400, &army_statues, "Army statue");
initialize_accessory(0, 4, 0, 1500, 1000, 500, &guard_towers, "Guard towers");
struct Buff buff_bank, buff_lumberjack, buff_barracks;
initialize_buff("bank", 0, 1500, 200, 100, &buff_bank);
initialize_buff("lumberjack", 0, 500, 1000, 200, &buff_lumberjack);
initialize_buff("barracks", 0, 800, 500, 1000, &buff_barracks);
int default_texture = 1;
bool loaded_game = false;
char village_name[25];
char save_file_name[15];
WINDOW *main_win = newwin(0, 0, 0, 0);
if(argc == 2){
if(strcmp(argv[1], "-s") == 0 || strcmp(argv[1], "--save") == 0){
endwin();
printf("You need to enter number of save file!\nSee tutorial/help if you need help.\n");
printf("To open tutorial/help window, start game with '-h / --help / -t / --tutorial' as paramaters\n");
exit(1);
}
else if(strcmp(argv[1], "-h") == 0 || strcmp(argv[1],"--help") == 0 || strcmp(argv[1],"-t") == 0 || strcmp(argv[1],"--tutorial") == 0){
print_tutorial_page_one();
int input = getch();
int page = 1;
while(input != '\n'){
input = getch();
switch(input){
case KEY_RIGHT:
if(page == 1){
page = 2;
clear();
print_tutorial_page_two();
refresh();
}
break;
case KEY_LEFT:
if(page == 2){
page = 1;
clear();
print_tutorial_page_one();
refresh();
}
break;
}
}
endwin();
return EXIT_SUCCESS;
}
else if(strcmp(argv[1], "-g") == 0 || strcmp(argv[1], "--graphics") == 0){
endwin();
printf("You mus enter number of texture pack\nSee tutorial/help if you need help\n");
printf("To open tutorial window just start the game with -h/--help/-t/--tutorial as parameters");
exit(0);
}
else{
endwin();
printf("[ERROR] Not enough parameters passed\n");
exit(1);
}
}
if(argc >= 3 && (strcmp(argv[1], "-s") == 0|| strcmp(argv[1], "--save") == 0 || strcmp(argv[1], "-g") == 0 || strcmp(argv[1], "--graphics") == 0 )){
if(strcmp(argv[1], "-s") == 0 || strcmp(argv[1], "--save") == 0){
//load save file
FILE *save_file;
char file_name[] = "save";
strcat(file_name, argv[2]);
strcat(file_name, ".txt");
if((save_file = fopen(file_name, "r")) == NULL){
endwin();
printf("Couldnt load save file with name %s\nPlease check if you entered right number, and try again\n", file_name);
exit(1);
}
loaded_game = true;
fscanf(save_file ,"Name: %s\n" , village_name);
fscanf(save_file ,"Gold: %d\n", &gold_count);
fscanf(save_file ,"Wood: %d\n", &wood_count);
fscanf(save_file, "Army: %d\n", &army_count);
fscanf(save_file, "Day: %d\n", &day_count);
fscanf(save_file, "Bank_level: %d\n", &bank.level);
fscanf(save_file, "Lumberjack_level: %d\n", &lumberjack.level);
fscanf(save_file, "Barracks_level: %d\n", &barracks.level);
fscanf(save_file, "Trees_count: %d\n", &trees.count);
fscanf(save_file, "Army_statues_count: %d\n", &army_statues.count);
fscanf(save_file, "Bank_statues_count: %d\n", &bank_statues.count);
fscanf(save_file, "Has_hospital: %d\n", &hospital.level);
fscanf(save_file, "Guard_towers_count: %d\n", &guard_towers.count);
fscanf(save_file, "Bank_buff_days_left: %d\n", &buff_bank.days_left);
fscanf(save_file, "Lumberjack_buff_days_left: %d\n", &buff_lumberjack.days_left);
fscanf(save_file, "Barracks_buff_days_left: %d\n", &buff_barracks.days_left);
//change buildings upgrade prices and accessroy next prices acording to count / level
if(bank.level == 2){
bank.upgrade_gold_price = 7500;
bank.upgrade_wood_price = 2500;
}
if(lumberjack.level == 2){
lumberjack.upgrade_gold_price = 3000;
lumberjack.upgrade_wood_price = 1500;
lumberjack.upgrade_army_price = 600;
}
if(barracks.level == 1){
barracks.upgrade_gold_price = 2500;
barracks.upgrade_wood_price = 1000;
}
else if(barracks.level == 2){
barracks.upgrade_gold_price = 5000;
barracks.upgrade_wood_price = 2500;
}
if(trees.count >= 1){
trees.buff = trees.count * 10;
trees.next_gold_price = (trees.count + 1) * 150;
trees.next_wood_price = 250 +((trees.count + 1) * 100);
trees.next_army_price = 50 + ((trees.count + 1) * 25);
}
if(bank_statues.count >= 1){
bank_statues.buff = bank_statues.count * 10;
bank_statues.next_gold_price = 1500 + ((bank_statues.count + 1) * 2000);
bank_statues.next_wood_price = 300 + ((bank_statues.count + 1) * 500);
bank_statues.next_army_price = 200 + ((bank_statues.count + 1) * 300);
}
if(army_statues.count >= 1){
army_statues.buff = army_statues.count * 10;
army_statues.next_gold_price = 1500 + ((army_statues.count + 1) * 1500);
army_statues.next_wood_price = 500 + ((army_statues.count + 1) * 700);
army_statues.next_army_price = 400 + ((army_statues.count + 1) * 500);
}
if(guard_towers.count >= 1){
guard_towers.buff = guard_towers.count * 3;
guard_towers.next_gold_price = 1500 * (guard_towers.count + 1);
guard_towers.next_wood_price = 1000 * (guard_towers.count + 1);
guard_towers.next_army_price = 500 * (guard_towers.count + 1);
}
strcpy(save_file_name, file_name);
fclose(save_file);
if(argc >= 4){
if(strcmp(argv[3], "-g") == 0 || strcmp(argv[3], "--graphics") == 0){
if(argc == 5){
if(strcmp(argv[4], "2") == 0){
default_texture = 2;
}
}
else{
endwin();
printf("\n[ERROR] You must enter number of texture pack after -g/--graphics parameter.\n[INFO] To show tutorial start the game with -h / --h / -t / --tutorial parameter\n\n");
exit(1);
}
}
}
// exit(1);
}
else{
if(strcmp(argv[2], "2") == 0)
default_texture = 2;
}
}
if(!loaded_game){
FILE *new_save_file;
char new_save_file_name[] = "save";
int save_file_num = 0;
// char *save_file_num_str;
// char buffer[100];
char fin_buffer[100];
int success = 1;
do{
char *save_file_num_str;
char buffer[100];
save_file_num++;
if(asprintf(&save_file_num_str, "%d", save_file_num) == -1){
endwin();
printf("Error while generating new save file name\n");
exit(1);
}
else{
strcat(strcpy(buffer, new_save_file_name), save_file_num_str);
strcat(buffer, ".txt");
if((new_save_file = fopen(buffer, "r")) == NULL){
success = 1;
strcpy(fin_buffer, buffer);
}
else{
fclose(new_save_file);
success = 0;
}
free(save_file_num_str);
}
}while(success == 0);
strcpy(save_file_name, fin_buffer);
set_village_name(LINES, COLS, village_name);
}
/* else{
if(strcmp(argv[2], "2") == 0)
default_texture = 2;
}
}*/
main_game(LINES, COLS, village_name, wood_count, gold_count, army_count, day_count, &bank, &lumberjack, &barracks, &hospital, &trees, &bank_statues, &army_statues, &guard_towers, &buff_bank, &buff_lumberjack, &buff_barracks, main_win, save_file_name, default_texture);
endwin();
return EXIT_SUCCESS;
}