-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.c
370 lines (296 loc) · 10.2 KB
/
init.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
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
/* Author: ETA Team */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <termios.h>
#include <string.h>
#include <math.h>
#include <curses.h>
#include "init.h"
#include "command.h"
#include "combat.h"
#include "autogen.h"
/* Defines player stats */
#define BASE_HP 20
#define BASE_ATTACK 7
#define BASE_DEF 3
#define BASE_NEXT_LEVEL 10
/* Defines bag size */
#define TAM_BAG 5
/* Initializes everything and executes the game */
void startGame(){
char comand;
Nivel* nivel;
Player player;
Enemy *enemies = NULL;
Bag *bag;
Item *items, read;
FILE *database;
/* Initialize colors support from curses.h */
initscr();
start_color();
/* Define color pairs */
init_pair(1,COLOR_WHITE,COLOR_BLACK);
init_pair(2,COLOR_RED,COLOR_BLACK);
init_pair(3,COLOR_GREEN,COLOR_BLACK);
init_pair(4,COLOR_BLUE,COLOR_BLACK);
bkgd(COLOR_PAIR(1));
refresh();
/* Allocs memory and initializes arrays reading the databases */
bag = malloc(TAM_BAG * sizeof(Bag));
database = fopen("items.bin", "rb");
if(database == NULL){
printf("Erro ao ler database!!\n");
return;
}
fread(&QUANT_ITEMS, sizeof(int), 1, database);
items = malloc(QUANT_ITEMS * sizeof(Item));
fread(items, sizeof(Item), QUANT_ITEMS, database);
fclose(database);
/* Loads existent game or starts a new one */
if(gameLoad(&player, nivel, &enemies, bag) == 0){
/* Initialize player and bag */
playerInit(&player, items);
nivel = genNivel(0, &player, &enemies);
bagInit(bag);
comandList();
mvprintw(11,0,"Voce pode rever os comandos disponiveis acessando o MENU.\n\n\nDigite alguma letra para comecar: ");
comand = getch();
clear();
}
/* Give some pots away */
bag[0].item = items[0];
bag[0].quantidade = 3;
bag[0].used = 1;
/* Starts game */
do{
print((*nivel), player, enemies);
comand = getch();
clear();
}while(executeComand(comand, &player, nivel, enemies, bag, items));
free(enemies);
free(bag);
free(items);
endwin();
}
/* Initialize player */
void playerInit(Player *player, Item *items){
/* Inicializa os stas do jogador com os stats base */
(*player).hp = BASE_HP;
(*player).level = 1;
(*player).XP = 0;
(*player).chave = 0;
(*player).NextLevel = BASE_NEXT_LEVEL;
(*player).MaxHP = BASE_HP;
(*player).weapon = items[2];
(*player).attack = BASE_ATTACK + (*player).weapon.valor;
(*player).gear = items[3];
(*player).atual = 0;
(*player).defense = BASE_DEF + (*player).gear.valor;
(*player).con = 0;
(*player).dext = 0;
(*player).str = 0;
(*player).pontos = 0;
}
void bagInit(Bag *bag){
int i;
for(i = 0; i < TAM_BAG; i++)
bag[i].used = 0;
}
void enemyPositions(Nivel* nivel, Enemy *enemies){
int i, j;
for(i = 0; i < nivel->tamI; i++)
for(j = 0; j < nivel->tamJ; j++)
if(nivel->mapa[i][j].enemyIndice > 0){
enemies[nivel->mapa[i][j].enemyIndice].y = i;
enemies[nivel->mapa[i][j].enemyIndice].x = j;
}
}
void print(Nivel nivel, Player controller, Enemy *enemies){
int radius, porcentHP, porcentXP;
int beginMapLine = 10;
int beginStatsCollun = nivel.tamJ * 2 + 5;
move(beginMapLine, beginStatsCollun);
attron(COLOR_PAIR(4));
printw("Status do jogador:");
attron(COLOR_PAIR(1));
porcentXP = ceil(((double) controller.XP / controller.NextLevel) * 100);
porcentHP = ceil(((double) controller.hp / controller.MaxHP) * 100);
move(beginMapLine + 2, beginStatsCollun);
printw("Level: %6d", controller.level);
move(beginMapLine + 3, beginStatsCollun);
printw("HP: %6d/%6d (", controller.hp, controller.MaxHP);
for(int i = 1; i < 21; i++)
if(porcentHP >= 5 * i)
printw("=");
else
printw(".");
printw(") %d%\n", porcentHP);
move(beginMapLine + 4, beginStatsCollun);
printw("Experiencia: %6d/%6d (", controller.XP, controller.NextLevel);
for(int i = 1; i < 21; i++)
if(porcentXP >= 5 * i)
printw("=");
else
printw(".");
printw(") %d%\n", porcentXP);
move(beginMapLine + 5, beginStatsCollun);
printw("Attack: %6d", controller.attack);
move(beginMapLine + 6, beginStatsCollun);
printw("Defense: %6d\n", controller.defense);
for(int i = 0; (i < nivel.tamI); i++)
for(int j = 0; (j < nivel.tamJ); j++)
nivel.mapa[i][j].shown = 0;
nivel.mapa[controller.y][controller.x].shown = 1;
/* Verifies the player vision area */
for(radius = 1; radius < 6; radius++){
for(int i = controller.y - radius; (i < controller.y + radius); i++){
for(int j = controller.x - radius; (j < controller.x + radius); j++){
if(((i >= 0) && (i < nivel.tamI)) && ((j >= 0) && (j < nivel.tamJ))){
if(nivel.mapa[i][j].shown == radius){
if(i - 1 >= 0){
if(nivel.mapa[i - 1][j].wall == 0)
nivel.mapa[i - 1][j].shown = radius + 1;
else
nivel.mapa[i - 1][j].shown = 1;
}
if(i + 1 < nivel.tamI){
if(nivel.mapa[i + 1][j].wall == 0)
nivel.mapa[i + 1][j].shown = radius + 1;
else
nivel.mapa[i + 1][j].shown = 1;
}
if(j - 1 >= 0){
if(nivel.mapa[i][j - 1].wall == 0)
nivel.mapa[i][j - 1].shown = radius + 1;
else
nivel.mapa[i][j - 1].shown = 1;
}
if(j + 1 < nivel.tamJ){
if(nivel.mapa[i][j + 1].wall == 0)
nivel.mapa[i][j + 1].shown = radius + 1;
else
nivel.mapa[i][j + 1].shown = 1;
}
}
if(nivel.mapa[i][j].enemyIndice >= 0)
enemies[nivel.mapa[i][j].enemyIndice].seen = 1;
}
}
}
}
move(beginMapLine, 0);
attron(COLOR_PAIR(2));
printw("Labirinto nivel: %d", nivel.nivel + 1);
attron(COLOR_PAIR(1));
move(beginMapLine + 2, 0);
/* Prints current game situation */
for(int i = 0; (i < nivel.tamI); i++){
for(int j = 0; (j < nivel.tamJ * 2); j += 2){
if(nivel.mapa[i][j/2].shown){
if(nivel.mapa[i][j/2].player){
attron(COLOR_PAIR(4));
printw("P ");
attron(COLOR_PAIR(1));
}
else if(nivel.mapa[i][j/2].wall)
printw("X ");
else if(nivel.mapa[i][j/2].enemyIndice >= 0){
attron(COLOR_PAIR(2));
if(nivel.mapa[i][j/2].enemyIndice > 0)
printw("E ");
else
printw("B ");
attron(COLOR_PAIR(1));
}
else if(nivel.mapa[i][j/2].itemIndice >= 0){
attron(COLOR_PAIR(4));
printw("I ");
attron(COLOR_PAIR(1));
}
else if(nivel.mapa[i][j/2].stairs < 0){
attron(COLOR_PAIR(3));
printw("< ");
attron(COLOR_PAIR(1));
}
else if(nivel.mapa[i][j/2].stairs > 0){
attron(COLOR_PAIR(3));
printw("> ");
attron(COLOR_PAIR(1));
}
else
printw(" ");
}
else
printw(". ");
}
move(beginMapLine + i + 3, 0);
}
/* Describe symbols in map */
move(beginMapLine + nivel.tamI + 4, 0);
attron(COLOR_PAIR(1));
printw("Simbolos no mapa:");
move(beginMapLine + nivel.tamI + 6, 0);
printw("X - Parede");
move(beginMapLine + nivel.tamI + 7, 0);
attron(COLOR_PAIR(4));
printw("P ");
attron(COLOR_PAIR(1));
printw("- Player");
move(beginMapLine + nivel.tamI + 8, 0);
attron(COLOR_PAIR(4));
printw("I ");
attron(COLOR_PAIR(1));
printw("- Item");
move(beginMapLine + nivel.tamI + 9, 0);
attron(COLOR_PAIR(3));
printw("< ");
attron(COLOR_PAIR(1));
printw("- Escada por onde voce desceu para esse nivel");
move(beginMapLine + nivel.tamI + 10, 0);
attron(COLOR_PAIR(3));
printw("> ");
attron(COLOR_PAIR(1));
printw("- Escada para o proximo nivel");
move(beginMapLine + nivel.tamI + 11, 0);
attron(COLOR_PAIR(2));
printw("B ");
attron(COLOR_PAIR(1));
printw("- Boss do nivel");
move(beginMapLine + nivel.tamI + 12, 0);
attron(COLOR_PAIR(2));
printw("E ");
attron(COLOR_PAIR(1));
printw("- Inimigo comum");
move(beginMapLine + nivel.tamI + 13, 0);
attron(COLOR_PAIR(1));
printw("Pressione - para abrir o menu e ver todos os comandos");
move(beginMapLine + nivel.tamI + 14, 0);
}
/* Checks if there is a saved game */
int gameLoad(Player* player, Nivel* nivel, Enemy** enemies, Bag *bag){
char read;
FILE *arq;
arq = fopen("savedGame.bin", "rb");
if(arq != NULL){
attron(COLOR_PAIR(3));
printf("Voce deseja continuar o jogo salvo? (y/n)\n");
scanf(" %c", &read);
if(read == 'y'){
fread(nivel, sizeof(Nivel), 1, arq);
fread(player, sizeof(Player), 1, arq);
(*enemies) = malloc(nivel->inimigos * sizeof(Enemy));
fread((*enemies), nivel->inimigos * sizeof(Enemy), 1, arq);
fread(bag, sizeof(Bag), TAM_BAG, arq);
fclose(arq);
remove("savedGame.bin");
system("clear");
attron(COLOR_PAIR(1));
return 1;
}
attron(COLOR_PAIR(1));
clear();
fclose(arq);
}
return 0;
}