Project

General

Profile

Revision 307

working on dijkstra for zombies

View differences:

proj/include/ent.h
71 71
int    (map_collides_bullet)(const map_t *p, const bullet_t *bullet);
72 72
int16_t (map_get_width)   (const map_t *p);
73 73
int16_t (map_get_height)  (const map_t *p);
74
int (map_make_dijkstra)(map_t *p, uint16_t x, uint16_t y);
74 75
int (gunner_collides_bullet)(const gunner_t *shooter, const bullet_t *bull);
75 76
int (gunner_collides_gunner)(const gunner_t *shooter1, const gunner_t *shooter2);
76 77
void   (map_draw)(map_t *p);
proj/include/proj_func.h
29 29
/**
30 30
 * @brief Updates movement variables.
31 31
 */
32
void update_movement(const map_t *map, gunner_t *p, keys_t *keys, list_t *shooter_list);
32
void update_movement(map_t *map, gunner_t *p, keys_t *keys, list_t *shooter_list);
33 33

  
34 34
void update_mouse(struct packet *p);
35 35

  
......
39 39

  
40 40
void (update_game_state)(const map_t *map, list_t *shooter_list, list_t *bullet_list);
41 41

  
42
void (get_random_spawn)(const map_t *map, gunner_t *p);
42
void (get_random_spawn)(const map_t *map, gunner_t *p, list_t *l);
43 43

  
44 44
void update_scale(void);
45 45

  
proj/include/proj_macros.h
26 26
#define HIGHLIGHT_COLOR         0x404040
27 27
#define DELIMITER_COLOR         0x404040
28 28

  
29
#define MELEE_RANGE    150
29
#define MELEE_RANGE    80
30 30
#define MELEE_DAMAGE   1
31 31

  
32 32
#endif /* end of include guard: PROJ_MACROS_H_INCLUDED */
proj/libs/graph/include/font.h
18 18
font_t* (font_ctor)(const char *s);
19 19
void    (font_dtor)(font_t *p);
20 20

  
21
font_t *consolas;
22

  
21 23
struct text;
22 24
typedef struct text text_t;
23 25
text_t* (text_ctor)(const font_t *fnt, const char *txt);
proj/libs/graph/include/graph.h
11 11
#define LINEAR_FRAME_BUFFER_MD  BIT(14)
12 12

  
13 13
// Colors in RBG (8 bit)
14
#define GRAPH_BLACK               0x000000
15
#define GRAPH_WHITE               0xFFFFFF
16
#define GRAPH_TRANSPARENT         0x00
14
#define GRAPH_BLACK             0x000000
15
#define GRAPH_WHITE             0xFFFFFF
16
#define GRAPH_TRANSPARENT       0x00
17
#define GRAPH_RED               0xFF0000
17 18

  
18 19
// Alpha
19 20
#define ALPHA_THRESHOLD     0x7F
proj/libs/graph/src/font.c
197 197
        }
198 198
    }
199 199
    // Draw text
200
    for(int16_t newy = 0; newy < newH; ++newy){
200
    for(int16_t x, y, newy = 0; newy < newH; ++newy){
201
        y = inity+newy;
201 202
        for(int16_t newx = 0; newx < newW; ++newx){
203
            x = initx+newx;
204
            if(!(0 <= x && x < graph_get_XRes() &&
205
                 0 <= y && y < graph_get_YRes())) continue;
202 206
            uint8_t a = *(alp_new_buf+newx+newy*newW);
203
            if(a < 0x7F) graph_set_pixel(initx+newx,inity+newy,p->color);
207
            if(a < ALPHA_THRESHOLD) graph_set_pixel(x,y,p->color);
204 208
        }
205 209
    }
206 210
    free(alp_new_buf);
proj/src/ent.c
5 5
#include "graph.h"
6 6
#include "utils.h"
7 7
#include "rectangle.h"
8
#include "font.h"
9
#include "errors.h"
10
#include "queue.h"
8 11
#include <math.h>
9 12

  
13
#define GREEN_HEALTH_COLOR      0x009900
14

  
10 15
static double scale = 1.0;
11 16
static int16_t x_origin = 0;
12 17
static int16_t y_origin = 0;
......
23 28
    sprite_t *dude;
24 29
    sprite_t *weapon;
25 30
    double health, current_health;
31
    text_t *txt;
26 32
    gunner_type type;
27 33
    int team;
28 34
};
......
35 41
    ret->y = 0.0;
36 42
    ret->health = 100;
37 43
    ret->current_health = ret->health;
44
    ret->txt = text_ctor(consolas, "");
38 45
    ret->type = type;
39 46
    ret->team = team;
40 47
    ret->dude   = sprite_ctor(dude  );
......
42 49
    if(ret->dude == NULL || ret->weapon == NULL){
43 50
        gunner_dtor(ret);
44 51
        return NULL;
45
    } else return ret;
52
    }
53
    text_set_size(ret->txt, 15);
54
    text_set_valign(ret->txt, text_valign_center);
55
    text_set_halign(ret->txt, text_halign_center);
56
    text_set_color(ret->txt, GRAPH_WHITE);
57
    return ret;
46 58
}
47 59
void (gunner_dtor)(gunner_t *p){
48 60
    if(p == NULL) return;
49 61
    sprite_dtor(p->dude);
50 62
    sprite_dtor(p->weapon);
63
    text_dtor(p->txt);
51 64
    free(p);
52 65
}
53 66
void (gunner_set_pos)  (gunner_t *p, double x, double y){ p->x = x; p->y = y; }
......
96 109
    double health = gunner_get_health(p);
97 110
    double perc = curr_health/health;
98 111
    rectangle_t *green_bar = rectangle_ctor(x, y, (int16_t)(w*perc), 10);
99
    rectangle_set_fill_color(green_bar, 0x00FF00);
112
    rectangle_set_fill_color(green_bar, GREEN_HEALTH_COLOR);
100 113
    rectangle_t *red_bar = rectangle_ctor(x+(int16_t)(w*perc), y, (int16_t)(w*(1-perc)), 10);
101
    rectangle_set_fill_color(red_bar, 0xFF0000);
114
    rectangle_set_fill_color(red_bar, GRAPH_RED);
115
    char buf[20]; sprintf(buf, "%d/%d", (int)p->current_health, (int)p->health);
116
    text_set_text(p->txt, buf);
117
    text_set_pos(p->txt, x+w/2, y+10/2);
102 118
    rectangle_draw(green_bar);
103 119
    rectangle_draw(red_bar);
120
    text_draw(p->txt);
104 121
    rectangle_dtor(green_bar);
105 122
    rectangle_dtor(red_bar);
123

  
106 124
}
107 125

  
108 126
double (gunner_distance)(const gunner_t *p1, const gunner_t *p2){
......
198 216
    basic_sprite_t *bsp_background;
199 217
    sprite_t *background;
200 218
    uint8_t *collide;
219
    int *x_prev;
220
    int *y_prev;
201 221
};
202 222
map_t* (map_ctor)(const char **background, const char **collide){
203 223
    map_t *ret = malloc(sizeof(map_t));
......
206 226
    ret->bsp_background = NULL;
207 227
    ret->background     = NULL;
208 228
    ret->collide        = NULL;
229
    ret->x_prev         = NULL;
230
    ret->y_prev         = NULL;
209 231

  
210 232
    ret->bsp_background = basic_sprite_ctor(background, 0, 0);
211 233
    ret->background     = sprite_ctor(ret->bsp_background);
212 234
    if(ret->bsp_background == NULL ||
213
        ret->background     == NULL){ map_dtor(ret); return NULL; }
235
       ret->background     == NULL){ map_dtor(ret); return NULL; }
214 236

  
215 237
    basic_sprite_t *bsp_collide = basic_sprite_ctor(collide, 0, 0);
216 238
    if(bsp_collide == NULL){ map_dtor(ret); return NULL; }
......
224 246
    }
225 247
    basic_sprite_dtor(bsp_collide);
226 248

  
249
    ret->x_prev = malloc(W*H*sizeof(int));
250
    ret->y_prev = malloc(W*H*sizeof(int));
251

  
252
    if(ret->x_prev == NULL || ret->y_prev == NULL){
253
        map_dtor(ret);
254
        return NULL;
255
    }
227 256
    return ret;
228 257
}
229 258
void (map_dtor)(map_t *p){
......
231 260
    sprite_dtor(p->background);
232 261
    basic_sprite_dtor(p->bsp_background);
233 262
    free(p->collide);
263
    free(p->x_prev);
264
    free(p->y_prev);
234 265
    free(p);
235 266
}
236 267
int16_t (map_get_x_screen)(const map_t *p){ return (-x_origin)*scale; }
......
244 275
    uint32_t pos = x_ + y_*w;
245 276
    return p->collide[pos];
246 277
}
278
int (map_make_dijkstra)(map_t *p, uint16_t x, uint16_t y){
279
    const uint16_t W = basic_sprite_get_w(p->bsp_background),
280
                   H = basic_sprite_get_h(p->bsp_background);
281
    for(size_t i = 0; i < W*H; ++i)
282
        p->x_prev[i] = p->y_prev[i] = -1;
283
    /// ACTUAL DIJKSTRA
284
    queue_t *q = queue_ctor();
285
    int *ptr;
286
    p->x_prev[y*W+x] = x; p->x_prev[y*W+x] = y;
287
    ptr = malloc(sizeof(int)); *ptr = y*W+x; queue_push(q, ptr);
288
    while(!queue_empty(q)){
289
        int c = *(int*)list_node_val(queue_top(q)); free(queue_top(q)); queue_pop(q);
290
        if(p->collide[c]){ p->x_prev[c] = p->y_prev[c] = -1; continue; }
291
        uint16_t x = c%W, y = c/W;
292
        if(0   <= x-1){ int pos = y*W+(x-1); if(p->x_prev[pos] == -1){ p->x_prev[pos] = x; p->y_prev[pos] = y; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }}
293
        if(x+1 <  W  ){ int pos = y*W+(x+1); if(p->x_prev[pos] == -1){ p->x_prev[pos] = x; p->y_prev[pos] = y; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }}
294
        if(0   <= y-1){ int pos = (y-1)*W+x; if(p->x_prev[pos] == -1){ p->x_prev[pos] = x; p->y_prev[pos] = y; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }}
295
        if(y+1 <  H  ){ int pos = (y+1)*W+x; if(p->x_prev[pos] == -1){ p->x_prev[pos] = x; p->y_prev[pos] = y; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }}
296
    }
297
    return SUCCESS;
298
}
247 299

  
300

  
248 301
int (map_collides_gunner)(const map_t *p, const gunner_t *shooter) {
249 302
    double radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0;
250 303
    double shooter_x = gunner_get_x(shooter);
proj/src/proj.c
109 109
    keys_t *keys = get_key_presses();
110 110

  
111 111
    /// loop stuff
112
    int ipc_status;
113
    message msg;
114

  
115 112
    int click = 0;
116

  
113
    uint32_t int_vector = 0;
117 114
    int good = true;
118

  
119 115
    while (good) {
120 116
        /* Get a request message. */
121
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
122
            printf("driver_receive failed with %d", r);
123
            continue;
124
        }
125
        if (is_ipc_notify(ipc_status)) { /* received notification */
126
            switch (_ENDPOINT_P(msg.m_source)) {
127
                case HARDWARE: /* hardware interrupt notification */
128
                for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
129
                    if (msg.m_notify.interrupts & n) {
130
                        interrupt_handler(i);
131
                        switch (i) {
132
                            case TIMER0_IRQ:
117
        if((r = get_interrupts_vector(&int_vector))) return r;
118
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
119
            if (int_vector & n) {
120
                interrupt_handler(i);
121
                switch (i) {
122
                    case TIMER0_IRQ:
133 123

  
134
                            graph_clear_screen();
135
                            switch(menu_update_state(main_menu, click)){
136
                                case -1: break;
137
                                case  0: singleplayer(); break; //campaign(); break;
138
                                case  1: break;
139
                                case  2: chat(); break;
140
                                case  3: good = false; break;
141
                            }
142
                            menu_draw(main_menu);
124
                    graph_clear_screen();
125
                    switch(menu_update_state(main_menu, click)){
126
                        case -1: break;
127
                        case  0: singleplayer(); break; //campaign(); break;
128
                        case  1: break;
129
                        case  2: chat(); break;
130
                        case  3: good = false; break;
131
                    }
132
                    menu_draw(main_menu);
143 133

  
144
                            click = 0;
134
                    click = 0;
145 135

  
146
                            sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
147
                            sprite_draw(sp_crosshair);
148
                            graph_draw();
136
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
137
                    sprite_draw(sp_crosshair);
138
                    graph_draw();
149 139

  
150
                            break;
151
                            case KBC_IRQ:
152
                            if ((scancode[0]) == ESC_BREAK_CODE) good = false;
153
                            case MOUSE_IRQ:
154
                            if (counter_mouse_ih >= 3) {
155
                                mouse_parse_packet(packet_mouse_ih, &pp);
156
                                update_mouse(&pp);
157
                                if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
158
                                last_lb = keys->lb_pressed;
159
                                counter_mouse_ih = 0;
160
                            }
161
                            break;
162
                            case COM1_IRQ: nctp_ih(); break;
163
                        }
140
                    break;
141
                    case KBC_IRQ:
142
                    if ((scancode[0]) == ESC_BREAK_CODE) good = false;
143
                    case MOUSE_IRQ:
144
                    if (counter_mouse_ih >= 3) {
145
                        mouse_parse_packet(packet_mouse_ih, &pp);
146
                        update_mouse(&pp);
147
                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
148
                        last_lb = keys->lb_pressed;
149
                        counter_mouse_ih = 0;
164 150
                    }
151
                    break;
152
                    case COM1_IRQ: nctp_ih(); break;
165 153
                }
166

  
167
                break;
168
                default:
169
                break; /* no other notifications expected: do nothing */
170 154
            }
171
        } else { /* received standart message, not a notification */
172
            /* no standart message expected: do nothing */
173 155
        }
174 156
    }
175 157

  
176 158
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
177 159
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
178
    /*basic_sprite_dtor      (bsp_zombie   );*/ bsp_zombie    = NULL;
160
    basic_sprite_dtor      (bsp_zombie   ); bsp_zombie    = NULL;
179 161
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
180 162
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
181 163
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
......
339 321
                        list_node_t *it = list_begin(shooter_list);
340 322
                        while (it != list_end(shooter_list)) {
341 323
                            gunner_t *p = *(gunner_t**)list_node_val(it);
342
                            get_random_spawn(map1, p);
324
                            get_random_spawn(map1, p, shooter_list);
343 325
                            gunner_set_curr_health(p, gunner_get_health(p));
344 326
                            it = list_node_next(it);
345 327
                        }
......
425 407
                    case TIMER0_IRQ:
426 408
                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
427 409

  
428
                    clock_t t1, t2;
429
                    t1 = clock();
430

  
431 410
                    update_movement(map1, shooter1, keys, shooter_list);
432 411

  
433 412
                    update_game_state(map1, shooter_list, bullet_list);
......
449 428
                        gunner_set_health(zombie, health);
450 429
                        gunner_set_curr_health(zombie, health);
451 430
                        health *= ZOMBIE_HEALTH_FACTOR;
452
                        get_random_spawn(map1, zombie);
431
                        get_random_spawn(map1, zombie, shooter_list);
453 432
                        list_push_back(shooter_list, zombie);
454 433
                    }
455 434

  
......
464 443
                    sprite_draw(sp_crosshair);
465 444
                    graph_draw();
466 445

  
467
                    t2 = clock();
468
                    printf("%d microseconds\n", t2-t1);
469

  
470 446
                    break;
471 447
                    case KBC_IRQ:
472 448
                    if ((scancode[0]) == ESC_BREAK_CODE) {
473 449
                        good = false;
474
                        // reset game
475
                        while(list_size(bullet_list) > 0){
476
                            bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
477
                            bullet_dtor(p);
478
                        }
479
                        list_node_t *it = list_begin(shooter_list);
480
                        while (it != list_end(shooter_list)) {
481
                            gunner_t *p = *(gunner_t**)list_node_val(it);
482
                            get_random_spawn(map1, p);
483
                            gunner_set_curr_health(p, gunner_get_health(p));
484
                            it = list_node_next(it);
485
                        }
486
                        timer_reset(in_game_timer);
487 450
                    }
488 451
                    break;
489 452
                    case MOUSE_IRQ:
proj/src/proj_func.c
48 48
    }
49 49
}
50 50

  
51
void update_movement(const map_t *map, gunner_t *p, keys_t *keys, list_t *shooter_list) {
51
void update_movement(map_t *map, gunner_t *p, keys_t *keys, list_t *shooter_list) {
52 52
    int ver_mov = keys->s_pressed - keys->w_pressed;
53 53
    int hor_mov = keys->d_pressed - keys->a_pressed;
54 54
    double x = gunner_get_x(p);
......
81 81
            it = list_node_next(it);
82 82
        }
83 83
    }
84

  
85
    // Update zombie positions
86
    map_make_dijkstra(map, gunner_get_x(p), gunner_get_y(p));
87
    list_node_t *it = list_begin(shooter_list);
88
    while(it != list_end(shooter_list)){
89
        gunner_t *g = *(gunner_t**)list_node_val(it);
90
        if(gunner_get_type(g) & gunner_follow){
91
            //float theta = 0.0;
92
            //map_where_to_follow(map, &theta);
93
            //float c = fm_cos(theta), s = fm_sin(theta);
94

  
95
        }
96
        it = list_node_next(it);
97
    }
84 98
}
85 99

  
86 100
void (shoot_bullet)(const gunner_t *shooter, list_t *bullet_list, const basic_sprite_t *bsp_bullet) {
......
138 152
        list_node_t *it2 = list_begin(shooter_list);
139 153
        while(it2 != list_end(shooter_list)){
140 154
            gunner_t *s2 = *list_node_val(it2);
141
            if(s1 != s2 && gunner_distance(s1, s2) < MELEE_RANGE)
155
            if(gunner_get_team(s1) != gunner_get_team(s2) &&
156
               gunner_distance(s1, s2) < MELEE_RANGE)
142 157
                gunner_set_curr_health(s2, gunner_get_curr_health(s2) - MELEE_DAMAGE);
143 158
            if(gunner_get_curr_health(s2) <= 0){
144 159
                list_node_t *aux = list_node_next(it2);
......
150 165
    }
151 166
}
152 167

  
153
void (get_random_spawn)(const map_t *map, gunner_t *p) {
168
void (get_random_spawn)(const map_t *map, gunner_t *p, list_t *l) {
154 169
    uint16_t w = map_get_width(map), h = map_get_height(map);
155 170
    double x, y;
156 171

  
157
    do {
172
    while(true){
158 173
        x = rand() % w;
159 174
        y = rand() % h;
160 175
        gunner_set_pos(p, x, y);
161
    } while (map_collides_gunner(map, p));
176
        if(map_collides_gunner(map, p)) continue;
177
        int collides = false;
178
        list_node_t *it = list_begin(l);
179
        while(it != list_end(l)){
180
            if(gunner_collides_gunner(p, *list_node_val(it))){
181
                collides = true;
182
                break;
183
            }
184
            it = list_node_next(it);
185
        }
186
        if(!collides) return;
187
    }
162 188
}
163 189

  
164 190
void update_scale(void) {

Also available in: Unified diff