maps.c 10.5 KB
Newer Older
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
1
#include "maps.h"
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
2

Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
3
char input[WIDTH];
4
5
6
char mapfile[8][100]  = {"./sprites/map1.txt", "./sprites/map2.txt", "./sprites/map3.txt", 
                        "./sprites/map4.txt", "./sprites/map5.txt", "./sprites/map6.txt", 
                        "./sprites/map7.txt", "./sprites/congrats.txt"};
7

8
rockford player;
9
firefly *firstff, *lastff;
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
10
11

int colors[8][3];
12
int alter;
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
13
int dead;
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
14
15
int offy, offx;
int row, col;
16
int leveltime, waittime, falltime;
17
int mapnumber;
18
19
20
int done, lightning;
int hold;
int extx, exty;
21
22
int pushdelay;
int highscore;
23
int blink1, blink2, blink3;
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
24
int nff;
25
26
FILE *lastscorefile;
FILE *highscorefile;
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
27

28
void readFileMap(map *m, int mapn, int pscore) {
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
29
    int i, j;
30
    done = lightning = false;
31
32
33
    blink1 = 4;
    blink2 = 5;
    blink3 = 6;
34
    hold = 0;
35
    nff = 0;
36
    pushdelay = 0;
37
    mapnumber = mapn;
38
    waittime = 0;
39
    falltime = 0;
40
    m->sprite = fopen(mapfile[mapnumber], "r");
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
41

Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
42
43
44
45
    firstff = (firefly*) malloc(sizeof(firefly));
    lastff = firstff;
    lastff->next = NULL;

46
47
    fscanf(m->sprite, "%d %d %d", &m->diamonds, &m->score, &leveltime);
    for (i = 0; i < 8; i++)
48
49
50
        for (j = 0; j < 3; j++)
            fscanf(m->sprite, "%d", &colors[i][j]);

51
52
    for (i = 0; i < HEIGHT; i++) {
        fscanf(m->sprite, "%s", input);
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
53
        for (j = 0; j < WIDTH; j++) {
54
55
56
            if (input[j] == '5') {
                player.x = i;
                player.y = j;
57
                player.dir = 5;
58
                player.diamonds = 0;
59
                player.score = pscore;
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
60
                dead = false;
61
62
63
            } else if (input[j] == '4') {
                extx = i;
                exty = j;
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
64
            } else if (input[j] == 'M') {
65
66
67
68
69
                lastff->next = (firefly*) malloc(sizeof(firefly));
                lastff = lastff->next;
                lastff->x = j;
                lastff->y = i;
                lastff->dir = 8;
70
                lastff->dead = false;
71
                lastff->next = NULL;
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
72
                nff++;
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
73
            }
74
            m->grid[j][i] = input[j];
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
75
        }
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
76
    }
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
77
    fclose(m->sprite);
78
    setColors();
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
79

Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
80
    getmaxyx(stdscr, row, col);
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
81
82
83
84
85
    offy = (row / 2) - (12 * 3);
    offx = (col / 2) - (20 * 6);
}

void setColors() {
86
87
88
89
90
91
92
    init_color(COLOR_WHITE,   colors[0][0], colors[0][1], colors[0][2]);
    init_color(COLOR_BLACK,   colors[1][0], colors[1][1], colors[1][2]);
    init_color(COLOR_RED,     colors[2][0], colors[2][1], colors[2][2]);
    init_color(COLOR_YELLOW,  colors[3][0], colors[3][1], colors[3][2]);
    init_color(COLOR_GREEN,   colors[4][0], colors[4][1], colors[4][2]);
    init_color(COLOR_BLUE,    colors[5][0], colors[5][1], colors[5][2]);
    init_color(COLOR_MAGENTA, colors[6][0], colors[6][1], colors[6][2]);
93
    init_color(COLOR_CYAN,    colors[7][0], colors[7][1], colors[7][2]);
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
94
95
96

    // dirt color
    init_pair(1, COLOR_BLACK, COLOR_RED);
97

98
99
100
    // dirt color 2
    init_pair(13, COLOR_GREEN, COLOR_RED);

101
102
    // rock gray color 3
    init_pair(10, COLOR_BLACK, COLOR_BLUE);
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
103

Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
104
    // rock yellow color 2
105
    init_pair(8, COLOR_BLACK, COLOR_WHITE);
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
106
    
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
107
    // rock gray color 2
108
109
    init_pair(3, COLOR_BLACK, COLOR_GREEN);
    
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
110
111
    // wall color
    init_pair(4, COLOR_BLACK, COLOR_WHITE);
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
112
    
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
113
114
    // steel color
    init_pair(5, COLOR_WHITE, COLOR_BLUE);
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
115
    
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
116
    // rockford color
117
    init_pair(6, COLOR_BLUE, COLOR_BLACK);
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
118
    
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
119
    // diamond color
120
    init_pair(7, COLOR_MAGENTA, COLOR_BLACK);
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
121

Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
122
123
124
    //explosion color
    init_pair(9, COLOR_WHITE, COLOR_BLACK);

125
126
127
    //number color
    init_pair(2, COLOR_YELLOW, COLOR_BLACK);

128
129
130
131
132
    //white number color
    init_pair(11, COLOR_CYAN, COLOR_BLACK);

    //rockford legs
    init_pair(12, COLOR_RED, COLOR_BLACK);
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
133
134
}

135
136
137
138
139
140
141
142
143
144
145
void saveScore() {
    lastscorefile = fopen("./data/lastscore.txt", "w+");
    highscorefile = fopen("./data/highscore.txt", "r");

    fprintf(lastscorefile, "%d", player.score);
    fscanf(highscorefile, "%d", &highscore);
    fclose(highscorefile);
    if (highscore < player.score) {
        highscorefile = fopen("./data/highscore.txt", "w+");
        fprintf(highscorefile, "%d", player.score);
        fclose(highscorefile);
146
    }
147
148
    fclose(lastscorefile);
}
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
149

150
151
void updateMap(map *m, int action, int *ismenu, int *loadMenu) {
    int k, l;
152
153
154
    getmaxyx(stdscr, row, col);
    offy = (row / 2) - (12 * 3);
    offx = (col / 2) - (20 * 6);
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
155

156
157
158
    if (action == 9) {
        saveScore();
        *ismenu = true;
159
        *loadMenu = true;
160
161
162
163
164
165
166
167
168
169
170
171
    } else {
        player.dir = action;
        pushdelay = (pushdelay + 1) % 30;
        if (player.diamonds >= m->diamonds) {
            m->score = 15;
            done = true;
            lightning = hold <= 8;
        }
        if (!dead) {
            if (done && player.x == extx && player.y == exty) {
                if (leveltime == 0) {
                    saveScore();
172
                    readFileMap(m, mapnumber + 1, player.score);
173
174
175
176
177
178
                }
            } else {
                updateRockford(&player, m, action, done, pushdelay);
                if (leveltime == 0)
                    dead = true;
            }
179
        } else {
180
            waittime++;
181
182
183
184
            if (waittime < 21) {
		        for(k = -1; k <= 1; k++)
			        for(l = -1; l <= 1; l++)
				        if(m->grid[player.y+k][player.x+l] != '1')
185
                		    m->grid[player.y+k][player.x+l] = 'x';
186
187
188
189
            } else if (waittime <= 21) {
		        for(k = -1; k <= 1; k++)
			        for(l = -1; l <= 1; l++)
				        if(m->grid[player.y+k][player.x+l] != '1')
190
                		    m->grid[player.y+k][player.x+l] = '0';
191
            } else if (waittime > 100)
192
                readFileMap(m, mapnumber, player.score);
193
        }
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
194
    }
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
195
196
}

197
void renderMap(map *m, int time) {
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
198
    int i, j;
199

200
201
202
203
204
205
206
207
208
209
210
    if (mapnumber == 7) {
        m->grid[21][20] = ((player.score / 100000) % 10) + 'A';
        m->grid[22][20] = ((player.score / 10000) % 10) + 'A';
        m->grid[23][20] = ((player.score / 1000) % 10) + 'A';
        m->grid[24][20] = ((player.score / 100) % 10) + 'A';
        m->grid[25][20] = ((player.score / 10) % 10) + 'A';
        m->grid[26][20] = (player.score % 10) + 'A';
    } else {
        m->grid[3][0]  = ((m->diamonds / 10) % 10) + 'A';
        m->grid[4][0]  = (m->diamonds % 10) + 'A';
        m->grid[5][0]  = 'K';
211

212
213
214
215
216
        m->grid[6][0]  = ((m->score / 10) % 10) + 'A';
        m->grid[7][0]  = (m->score % 10) + 'A';
        
        m->grid[12][0] = ((player.diamonds / 10) % 10) + 'A';
        m->grid[13][0] = (player.diamonds % 10) + 'A';
217

218
219
220
        m->grid[25][0] = (leveltime / 100) + 'A';
        m->grid[26][0] = ((leveltime / 10) % 10) + 'A';
        m->grid[27][0] = (leveltime % 10) + 'A';
221

222
223
224
225
226
227
228
        m->grid[31][0] = ((player.score / 100000) % 10) + 'A';
        m->grid[32][0] = ((player.score / 10000) % 10) + 'A';
        m->grid[33][0] = ((player.score / 1000) % 10) + 'A';
        m->grid[34][0] = ((player.score / 100) % 10) + 'A';
        m->grid[35][0] = ((player.score / 10) % 10) + 'A';
        m->grid[36][0] = (player.score % 10) + 'A';
    }
229

230
    if (done && player.x == extx && player.y == exty) {
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
231
        if (leveltime != 0) {
232
233
234
235
236
237
238
            leveltime--;
            player.score++;
        }
    } else {
        if (time == 29 && leveltime != 0)
            leveltime--;
    } 
239

Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
240
241
    if (lightning)
        hold++;
242
    if (!(time % 2)) {
243
244
245
246
        blink1 = (blink1 % 3) + 4;
        blink2 = (blink2 % 3) + 4;
        blink3 = (blink3 % 3) + 4;
    }
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
247

248
    if (nff && !(time % 4))
Bruno Freitas Tissei's avatar
Bug fix    
Bruno Freitas Tissei committed
249
        updateFirefly(m, firstff, &dead, done);
250

Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
251
252
    for (i = 0; i < WIDTH; i++) {
        for (j = 0; j < HEIGHT; j++) {
253
254
            updatePhysics(m, m->grid[i][j], i, j, offx, offy, time, falltime, waittime, &dead);
            falltime = (falltime + 1) % 7;
255

Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
256
257
            if (mapnumber == 7 && ((m->grid[i][j] >= 'a' && m->grid[i][j] <= 'z') || 
                m->grid[i][j] == '@' || m->grid[i][j] == '#' || m->grid[i][j] == '!'))
258
                renderLetter(m->grid[i][j], i, j, offx, offy);
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
259
260
            if (m->grid[i][j] >= 'A' && m->grid[i][j] <= 'K')
                renderHUB(m->grid[i][j], i, j, offx, offy);
261

262
            switch (m->grid[i][j]) {
263
                case('0'):
264
                    renderChar(empty, i, j, offx, offy, lightning ? 4 : 6);
265
                    break;
266
                case('1'):
267
                    renderChar((j == 1 || j == 22) ? steel : steel2, i, j, offx, offy, 5);
268
269
                    break;
                case('2'):
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
270
                    renderDirt(i, j, offx, offy);
271
                    break;
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
272
                case('z'):
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
273
                    renderChar(wall, i, j, offx, offy, 4);
274
                    break;
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
275
                case('w'):
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
276
277
                    renderChar(wall2, i, j, offx, offy, 4);
                    break;
278
                case('4'):
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
279
280
281
                    renderChar(ext, i, j, offx, offy, 5);
                    if (done && time >= 15)
                        renderChar(empty, i, j, offx, offy, 4);
282
283
                    break;
                case('5'):
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
284
                    renderRockford(&player, i, j, offx, offy);
285
                    break;
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
286
287
288
                case('M'):
                    renderFirefly(i, j, offx, offy, blink1, blink2, blink3);
                    break;
289
                case('6'):
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
290
291
292
                    if ((m->grid[i][j+1] == '6' || m->grid[i][j+1] == 'w' || 
                         m->grid[i][j+1] == 'z' || m->grid[i][j+1] == '7') && 
                         m->grid[i+1][j] == '0' && m->grid[i+1][j+1] == '0')
293
                        m->grid[i][j] = 'r';
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
294
295
296
                    else if ((m->grid[i][j+1] == '6' || m->grid[i][j+1] == 'w' || 
                              m->grid[i][j+1] == 'z' || m->grid[i][j+1] == '7') && 
                              m->grid[i-1][j] == '0' && m->grid[i-1][j+1] == '0')
297
298
                        m->grid[i][j] = 'l';

Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
299
300
301
                    if (m->grid[i][j+1] == '0')
                        m->grid[i][j] = 'f';

Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
302
                    renderBould(i, j, offx, offy);
303
304
                    break;
                case('7'):
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
305
306
307
                    if ((m->grid[i][j+1] == '6' || m->grid[i][j+1] == 'w' || 
                         m->grid[i][j+1] == 'z' || m->grid[i][j+1] == '7') && 
                         m->grid[i-1][j] == '0' && m->grid[i-1][j+1] == '0')
Ivo Júnior's avatar
Ivo Júnior committed
308
                        m->grid[i][j] = 'L';
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
309
310
311
                    else if ((m->grid[i][j+1] == '6' || m->grid[i][j+1] == 'w' || 
                              m->grid[i][j+1] == 'z' || m->grid[i][j+1] == '7') && 
                              m->grid[i+1][j] == '0' && m->grid[i+1][j+1] == '0')
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
312
313
                        m->grid[i][j] = 'R';

Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
314
315
                    if (m->grid[i][j+1] == '0')
                        m->grid[i][j] = 'h';
316

Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
317
                    renderDiamonds(i, j, offx, offy, time);
318
                    break;
319
            }
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
320
321
322

        }
    }
Bruno Freitas Tissei's avatar
Bruno Freitas Tissei committed
323
}