14 Day Roguelike – Day III

Today I finally overcame my collision detection troubles. I also added mobs that randomly move around. They have a Damage Point (dp) and Armor Point (ap) rating, as well as a Health Point (hp) rating. My attack_mob(int y, int x) function works perfectly except that it apparently doesn’t store the new HP value in mobs[i].hp. Eventually I’ll figure out how to make that work…eventually.

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
#include "curses.h"
#include "signal.h"
#include "stdlib.h"
#include "time.h"
 
//Define the map height and width
#define MAP_HEIGHT 40
#define MAP_WIDTH 30
 
//Create the 2D arrays for the floor and walls
bool floor[MAP_HEIGHT][MAP_WIDTH];
bool walls[MAP_HEIGHT][MAP_WIDTH];
 
//NCurses player yx
int player_y = 0;
int player_x = 0;
//Map player yx
int player_map_y = MAP_HEIGHT / 2;
int player_map_x = MAP_WIDTH / 2;
 
//Function prototypes
static void finish(int sig);
static bool proc_in(int c);
static void draw_map();
static void draw_player();
static void init_player();
static void init_mobs();
static void draw_mobs();
static void proc_mobs();
static bool check_collision(int y, int x);
static void attack_mob(int y, int x);
 
typedef struct mob_
{
        //NCurses Coordinates
        int nc_y;
        int nc_x;
        //Map Coordinates
        int mp_y;
        int mp_x;
        //Combat stuff
        int hp;
        int dp;
        int ap;
} mob;
 
 
typedef struct player_
{
        int nc_y;
        int nc_x;
        int mp_y;
        int mp_x;
        int hp;
        int dp;
        int ap;
} player;
 
int test = MAP_HEIGHT / 10;
 
mob mobs[MAP_HEIGHT / 10];
 
player p;
 
//MAIN
int main(int argc, char * argv[])
{
        //endwin() on SIGINT
        (void)signal(SIGINT, finish);
        //NCurses init stuff
        (void)initscr();
        (void)keypad(stdscr, TRUE);
        (void)cbreak();
        (void)noecho();
        (void)nonl();
 
        //Seed rand using time
        srand(time(NULL));
        //Generate a basic map
        int y = 0;
        int x = 0;
        for(y = 0; y < MAP_HEIGHT; y++)
        {
                for(x = 0; x < MAP_WIDTH; x++)
                {
                        floor[y][x] = TRUE;
                }
        }
        for(y = 0; y < MAP_HEIGHT; y++)
        {
                        walls[y][0] = TRUE;
                        walls[y][MAP_WIDTH - 1] = TRUE;
        }
        for(x = 0; x < MAP_WIDTH; x++)
        {
                walls[0][x] = TRUE;
                walls[MAP_HEIGHT - 1][x] = TRUE;
        }
        //Init the player
        init_player();
        //Init the mobs
        init_mobs();
        //Draw the map
        draw_map();
        //Draw the mobs
        draw_mobs();
        //Draw the player
        draw_player();
        //Refresh the screen
        refresh();
 
        //Loop forever
        while(TRUE)
        {
                //Only refresh the screen if something happens
                if(proc_in(getch()) == TRUE)
                {
                        //Process the mobs
                        proc_mobs();
                        //Draw the map;
                        draw_map();
                        //Draw the mobs
                        draw_mobs();
                        //Draw the player
                        draw_player();
                        //Refresh the screen
                        refresh();
                }
        }
        return 0;
}
 
static bool proc_in(int c)
{
        //Process the key that was pressed
        switch(c)
        {
                case '4': //If 4 then move left
                        if(check_collision(p.mp_y, p.mp_x - 1))
                        {
                                p.nc_x--;
                                p.mp_x--;
                        }
                        return TRUE;
                        break;
                case '6': //If 6 then move right
                        if(check_collision(p.mp_y, p.mp_x + 1))//But only if there is no wall
                        {
                                p.nc_x++;
                                p.mp_x++;
                        }
                        return TRUE;
                        break;
                case '8': //If 8 then move up
                        if(check_collision(p.mp_y - 1, p.mp_x))//But only if there is no wall
                        {
                                p.nc_y--;
                                p.mp_y--;
                        }
                        return TRUE;
                        break;
                case '2': //If 2 then move down
                        if(check_collision(p.mp_y + 1, p.mp_x))//But only if there is no wall
                        {
                                p.nc_y++;
                                p.mp_y++;
                        }
                        return TRUE;
                        break;
                case '1': //If 1 then move down and left
                        if(check_collision(p.mp_y + 1, p.mp_x - 1))//But only if there is no wall
                        {
                                p.nc_y++;
                                p.mp_y++;
                                p.nc_x--;
                                p.mp_x--;
                        }
                        return TRUE;
                        break;
                case '3': //If 3 then move down and right
                        if(check_collision(p.mp_y + 1, p.mp_x + 1))//But only if there is no wall
                        {
                                p.nc_y++;
                                p.mp_y++;
                                p.nc_x++;
                                p.mp_x++;
                        }
                        return TRUE;
                        break;
                case '7': //If 7 then move up and left
                        if(check_collision(p.mp_y - 1, p.mp_x - 1)) //But only if there is no wall
                        {
                                p.nc_y--;
                                p.mp_y--;
                                p.nc_x--;
                                p.mp_x--;
                        }
                        return TRUE;
                        break;
                case '9': //If 8 then move up and right
                        if(check_collision(p.mp_y - 1, p.mp_x + 1)) //But only if there is no wall
                        {
                                p.nc_y--;
                                p.mp_y--;
                                p.nc_x++;
                                p.mp_x++;
                        }
                        return TRUE;
                        break;
                case '.':
                        return TRUE;
                        break;
        }
        return FALSE;
}
 
 
static void draw_map()
{
        //Draw the map
        int y = 0;
        int x = 0;
        for(y = 0; y < MAP_HEIGHT; y++)
        {
                for(x = 0; x < MAP_WIDTH; x++)
                {
                        //If there is a floor on cell yx
                        if(floor[y][x] == TRUE)
                        {
                                //Draw it
                                mvaddch((LINES / 2 - MAP_HEIGHT / 2) + y,(COLS / 2 - MAP_WIDTH / 2) +  x, '.');
                        }
                }
        }
        move(0, 0);
        for(y = 0; y < MAP_HEIGHT; y++)
        {
                for(x = 0; x < MAP_WIDTH; x++)
                {
                        //If there is a wall on cell yx
                        if(walls[y][x] == TRUE)
                        {
                                //Draw it
                                mvaddch((LINES / 2 - MAP_HEIGHT / 2) + y,(COLS / 2 - MAP_WIDTH / 2) +  x, 'W');
                        }
                }
        }
        move(0, 0);
}
 
static void draw_player()
{
        mvaddch(p.nc_y, p.nc_x, '@');
        move(0,0);
}
 
static void finish(int sig)
{
        endwin();
        exit(0);
}
 
static void init_player()
{
        p.mp_y = MAP_HEIGHT / 2;
        p.mp_x = MAP_WIDTH / 2;
        p.nc_y = (LINES / 2 - MAP_HEIGHT / 2) + p.mp_y;
        p.nc_x = (COLS / 2 - MAP_WIDTH / 2) + p.mp_x;
        p.hp = 100;
        p.dp = 10;
        p.ap = 25;
}
 
static void init_mobs()
{
        int i = 0;
        for(i = 0; i < sizeof(mobs) / sizeof(mob); i++)
        {
                mobs[i].mp_y = rand()%(MAP_HEIGHT - 5) + 1;
                mobs[i].mp_x = rand()%(MAP_WIDTH - 5) + 1;
                mobs[i].nc_y = (LINES / 2 - MAP_HEIGHT / 2) + mobs[i].mp_y;
                mobs[i].nc_x = (COLS / 2 - MAP_WIDTH / 2) + mobs[i].mp_x;
                mobs[i].hp = rand()%250 + 1;
                mobs[i].dp = rand()%25 + 1;
                mobs[i].ap = rand()%50 + 1;
        }
}
 
static void draw_mobs()
{
        int i = 0;
        for(i = 0; i  0)
                {
                        mvaddch(mobs[i].nc_y, mobs[i].nc_x, 'm');
                }
                else
                {
                        mvaddch(mobs[i].nc_y, mobs[i].nc_x, 'x');
                }
        }
        move(0, 0);
}
 
static void proc_mobs()
{
        int i = 0;
        for(i = 0; i  0)
                {
                        int a = rand()%4;
                        int y = rand()%2;
                        int x = rand()%2;
                        switch(a){
                                case 0:
                                        if(y == 0 && check_collision(mobs[i].mp_y -1, mobs[i].mp_x))
                                        {
                                                mobs[i].nc_y--;
                                                mobs[i].mp_y--;
                                        }
                                        else if(y == 1 && check_collision(mobs[i].mp_y +1, mobs[i].mp_x))
                                        {
                                                mobs[i].nc_y++;
                                                mobs[i].mp_y++;
                                        }
                                        break;
                                case 1:
                                        if(x == 0 && check_collision(mobs[i].mp_y, mobs[i].mp_x -1))
                                        {
                                                mobs[i].nc_x--;
                                                mobs[i].mp_x--;
                                        }
                                        else if(x == 1 && check_collision(mobs[i].mp_y, mobs[i].mp_x +1))
                                        {
                                                mobs[i].nc_x++;
                                                mobs[i].mp_x++;
                                        }
                                        break;
                                case 2:
                                        if(y == 0 && check_collision(mobs[i].mp_y -1, mobs[i].mp_x))
                                        {
                                                mobs[i].nc_y--;
                                                mobs[i].mp_y--;
                                        }
                                        else if(y == 1 && check_collision(mobs[i].mp_y +1, mobs[i].mp_x))
                                        {
                                                mobs[i].nc_y++;
                                                mobs[i].mp_y++;
                                        }
                                        if(x == 0 && check_collision(mobs[i].mp_y, mobs[i].mp_x -1))
                                        {
                                                mobs[i].nc_x--;
                                                mobs[i].mp_x--;
                                        }
                                        else if(x == 1 && check_collision(mobs[i].mp_y, mobs[i].mp_x +1))
                                        {
                                                mobs[i].nc_x++;
                                                mobs[i].mp_x++;
                                        }
                                        break;
                                case 3:
                                        break;
                        }
                }
        }
}
 
static bool check_collision(int y, int x)
{
        int check = 0;
        if(walls[y][x] == FALSE && floor[y][x] == TRUE)
        {
                if(y != p.mp_y || x != p.mp_x)
                {
                        int i = 0;
                        for(i = 0; i  0)
                                {
                                        if(y != mobs[i].mp_y || x != mobs[i].mp_x)
                                        {
                                                check++;
                                        }
                                        else
                                        {
                                                attack_mob(y, x);
                                        }
                                }
                        }
                }
        }
        if(check == sizeof(mobs) / sizeof(mob))
        {
                return TRUE;
        }
        else
        {
                return FALSE;
        }
}
 
static void attack_mob(int y, int x)
{
        int i = 0;
        for(i = 0; i < sizeof(mobs) / sizeof(mob); i++)
        {
                if(mobs[i].mp_y == y && mobs[i].mp_x == x)
                {
                        int damage = 0;
                        int i = 0;
                        for(i = 0; i <= p.dp; i++)
                        {
                                damage = damage + rand()%6 + 1;
                        }
                        for(i = 0; i <= mobs[i].ap; i++)
                        {
                                damage = damage - rand()%4 + 1;
                        }
 
                        mobs[i].hp -= damage;
                        move(0, 0);
                        if(mobs[i].hp <= 0)
                        {
                                printf("You hit monster for %d damage. You killed monster", damage);
                        }
                        else
                        {
                                printf("You hit monster for %d damage.", damage);
                        }
                        move(0, 0);
                }
        }
}

Leave a Reply