Project

General

Profile

Statistics
| Revision:

root / proj / src / ent.c @ 319

History | View | Annotate | Download (14.5 KB)

1 193 up20180642
#include <lcom/lcf.h>
2
3
#include "ent.h"
4
5
#include "graph.h"
6 226 up20180642
#include "utils.h"
7 229 up20180655
#include "rectangle.h"
8 307 up20180642
#include "font.h"
9
#include "errors.h"
10
#include "queue.h"
11 220 up20180655
#include <math.h>
12
13 307 up20180642
#define GREEN_HEALTH_COLOR      0x009900
14
15 193 up20180642
static double scale = 1.0;
16
static int16_t x_origin = 0;
17
static int16_t y_origin = 0;
18
19
void (ent_set_scale) (double n){ scale = n; }
20
void (ent_set_origin)(double x, double y){ x_origin = x; y_origin = y; }
21 194 up20180642
double (ent_get_scale)  (void){ return scale; }
22 193 up20180642
double (ent_get_XLength)(void){ return graph_get_XRes()/scale; }
23
double (ent_get_YLength)(void){ return graph_get_YRes()/scale; }
24
25 201 up20180642
struct gunner{
26 203 up20180642
    double x, y; //real position
27 232 up20180655
    double spawn_x, spawn_y;
28 193 up20180642
    sprite_t *dude;
29
    sprite_t *weapon;
30 267 up20180655
    double health, current_health;
31 314 up20180642
    rectangle_t *green_bar, *red_bar;
32 307 up20180642
    text_t *txt;
33 301 up20180642
    gunner_type type;
34 302 up20180642
    int team;
35 193 up20180642
};
36 302 up20180642
gunner_t* (gunner_ctor)(basic_sprite_t *dude, basic_sprite_t *weapon, gunner_type type, int team){
37 201 up20180642
    gunner_t *ret = malloc(sizeof(gunner_t));
38 193 up20180642
    if(ret == NULL) return NULL;
39 232 up20180655
    ret->spawn_x = 0.0;
40
    ret->spawn_y = 0.0;
41 193 up20180642
    ret->x = 0.0;
42
    ret->y = 0.0;
43 229 up20180655
    ret->health = 100;
44 230 up20180655
    ret->current_health = ret->health;
45 314 up20180642
    ret->green_bar = rectangle_ctor(0,0,0,0);
46
    ret->red_bar   = rectangle_ctor(0,0,0,0);
47
    ret->txt = text_ctor(font_get_default(), "");
48 302 up20180642
    ret->type = type;
49
    ret->team = team;
50 193 up20180642
    ret->dude   = sprite_ctor(dude  );
51
    ret->weapon = sprite_ctor(weapon);
52 314 up20180642
    if(ret->txt == NULL || ret->dude == NULL || ret->weapon == NULL || ret->green_bar == NULL || ret->red_bar == NULL){
53 201 up20180642
        gunner_dtor(ret);
54 193 up20180642
        return NULL;
55 307 up20180642
    }
56 314 up20180642
    rectangle_set_fill_color(ret->green_bar, GREEN_HEALTH_COLOR);
57
    rectangle_set_fill_color(ret->red_bar  , GRAPH_RED         );
58 307 up20180642
    text_set_size(ret->txt, 15);
59
    text_set_valign(ret->txt, text_valign_center);
60
    text_set_halign(ret->txt, text_halign_center);
61
    text_set_color(ret->txt, GRAPH_WHITE);
62
    return ret;
63 193 up20180642
}
64 201 up20180642
void (gunner_dtor)(gunner_t *p){
65 193 up20180642
    if(p == NULL) return;
66
    sprite_dtor(p->dude);
67
    sprite_dtor(p->weapon);
68 314 up20180642
    rectangle_dtor(p->green_bar);
69
    rectangle_dtor(p->red_bar);
70 307 up20180642
    text_dtor(p->txt);
71 193 up20180642
    free(p);
72
}
73 216 up20180642
void (gunner_set_pos)  (gunner_t *p, double x, double y){ p->x = x; p->y = y; }
74 232 up20180655
void (gunner_set_spawn)  (gunner_t *p, double x, double y){ p->spawn_x = x; p->spawn_y = y; }
75 216 up20180642
void (gunner_set_angle)(gunner_t *p, double angle      ){
76 193 up20180642
    sprite_set_angle(p->dude  , angle);
77
    sprite_set_angle(p->weapon, angle);
78
}
79 267 up20180655
void (gunner_set_health)        (gunner_t *p, double health) {
80 230 up20180655
    if (health < 0) health = 0;
81
    p->health = health;
82
}
83 267 up20180655
void (gunner_set_curr_health)   (gunner_t *p, double health) {
84 230 up20180655
    if (health < 0) health = 0;
85
    p->current_health = health;
86
}
87 229 up20180655
double  (gunner_get_x)              (const gunner_t *p){ return p->x; }
88
double  (gunner_get_y)              (const gunner_t *p){ return p->y; }
89 232 up20180655
double  (gunner_get_spawn_x)        (const gunner_t *p){ return p->spawn_x; }
90
double  (gunner_get_spawn_y)        (const gunner_t *p){ return p->spawn_y; }
91 231 up20180655
double  (gunner_get_angle)          (const gunner_t *p){ return sprite_get_angle(p->dude); }
92 267 up20180655
double  (gunner_get_health)         (const gunner_t *p){ return p->health; }
93
double  (gunner_get_curr_health)    (const gunner_t *p){ return p->current_health; }
94 229 up20180655
int16_t (gunner_get_x_screen)       (const gunner_t *p){ return (p->x-x_origin)*scale; }
95
int16_t (gunner_get_y_screen)       (const gunner_t *p){ return (p->y-y_origin)*scale; }
96 301 up20180642
gunner_type (gunner_get_type)       (const gunner_t *p){ return p->type; }
97 302 up20180642
int     (gunner_get_team)           (const gunner_t *p){ return p->team; }
98 201 up20180642
void (gunner_draw)(gunner_t *p){
99
    const int16_t x_screen = gunner_get_x_screen(p);
100
    const int16_t y_screen = gunner_get_y_screen(p);
101 193 up20180642
    sprite_set_pos  (p->dude  , x_screen, y_screen);
102
    sprite_set_pos  (p->weapon, x_screen, y_screen);
103
    sprite_set_scale(p->dude  , scale);
104
    sprite_set_scale(p->weapon, scale);
105
    sprite_draw     (p->weapon);
106
    sprite_draw     (p->dude  );
107 229 up20180655
    gunner_draw_health(p);
108 193 up20180642
}
109 229 up20180655
void (gunner_draw_health)(const gunner_t *p) {
110
    int16_t w = sprite_get_w(p->dude);
111
    int16_t h = sprite_get_h(p->dude);
112 230 up20180655
    double x = gunner_get_x_screen(p) - w/2;
113
    double y = gunner_get_y_screen(p) - h/2 - 10;
114 267 up20180655
    double curr_health = gunner_get_curr_health(p);
115
    double health = gunner_get_health(p);
116
    double perc = curr_health/health;
117 314 up20180642
    rectangle_set_pos(p->green_bar, x, y); rectangle_set_size(p->green_bar, (int16_t)(w*perc), 10);
118
    rectangle_set_pos(p->red_bar, x+(int16_t)(w*perc), y); rectangle_set_size(p->red_bar, (int16_t)(w*(1-perc)), 10);
119 307 up20180642
    char buf[20]; sprintf(buf, "%d/%d", (int)p->current_health, (int)p->health);
120 314 up20180642
    text_set_string(p->txt, buf);
121 307 up20180642
    text_set_pos(p->txt, x+w/2, y+10/2);
122 314 up20180642
    rectangle_draw(p->green_bar);
123
    rectangle_draw(p->red_bar);
124 307 up20180642
    text_draw(p->txt);
125 229 up20180655
}
126
127 302 up20180642
double (gunner_distance)(const gunner_t *p1, const gunner_t *p2){
128
    double dx = gunner_get_x(p1) - gunner_get_x(p2);
129
    double dy = gunner_get_y(p1) - gunner_get_y(p2);
130
    return sqrt(dx*dx+dy*dy);
131
}
132
133 203 up20180642
struct bullet{
134 236 up20180642
    const gunner_t *shooter;
135 203 up20180642
    double x, y; //real position
136 226 up20180642
    double vx, vy;
137 203 up20180642
    sprite_t *b;
138 267 up20180655
    double damage;
139 203 up20180642
};
140 237 up20180642
bullet_t* (bullet_ctor)(const gunner_t *shooter, const basic_sprite_t *b, double x, double y, double vx, double vy){
141 203 up20180642
    bullet_t *ret = malloc(sizeof(bullet_t));
142
    if(ret == NULL) return NULL;
143 236 up20180642
    ret->shooter = shooter;
144 227 up20180642
    ret-> x =  x;
145
    ret-> y =  y;
146
    ret->vx = vx;
147
    ret->vy = vy;
148 232 up20180655
    ret->damage = 10;
149 203 up20180642
    ret->b = sprite_ctor(b);
150
    if(ret->b == NULL){
151
        bullet_dtor(ret);
152
        return NULL;
153 227 up20180642
    }
154
    double angle = atan2(-ret->vy, ret->vx);
155
    sprite_set_angle(ret->b, angle-M_PI_2);
156
    return ret;
157 203 up20180642
}
158
void (bullet_dtor)(bullet_t *p){
159
    if(p == NULL) return;
160
    sprite_dtor(p->b);
161
    free(p);
162
}
163
double  (bullet_get_x)       (const bullet_t *p){ return p->x; }
164
double  (bullet_get_y)       (const bullet_t *p){ return p->y; }
165
int16_t (bullet_get_x_screen)(const bullet_t *p){ return (p->x-x_origin)*scale; }
166
int16_t (bullet_get_y_screen)(const bullet_t *p){ return (p->y-y_origin)*scale; }
167 267 up20180655
double  (bullet_get_damage)  (const bullet_t *p){ return p->damage; }
168
void    (bullet_set_damage)  (bullet_t *p, double damage) {
169 230 up20180655
    if (damage < 0) damage = 0;
170
    p->damage = damage;
171
}
172 302 up20180642
const gunner_t* (bullet_get_shooter)(const bullet_t *p){ return p->shooter; }
173 226 up20180642
void (bullet_update_movement)(bullet_t *p){
174
    p->x += p->vx;
175
    p->y += p->vy;
176
}
177 231 up20180655
178
void (bullet_update_movement_list)(list_t *bullet_list){
179
    if (list_size(bullet_list) == 0) return;
180
181
    list_node_t *it = list_begin(bullet_list);
182
    while (it != list_end(bullet_list)) {
183
        bullet_update_movement(*(bullet_t**)list_node_val(it));
184
        it = list_node_next(it);
185
    }
186
}
187
188 203 up20180642
void (bullet_draw)(bullet_t *p){
189
    const int16_t x_screen = bullet_get_x_screen(p);
190
    const int16_t y_screen = bullet_get_y_screen(p);
191
    sprite_set_pos  (p->b, x_screen, y_screen);
192
    sprite_set_scale(p->b, scale);
193
    sprite_draw     (p->b);
194
}
195 216 up20180642
196 248 up20180655
void (gunner_draw_list)(list_t *shooter_list) {
197
    if (list_size(shooter_list) == 0) return;
198
199
    list_node_t *it = list_begin(shooter_list);
200
    while (it != list_end(shooter_list)) {
201
        gunner_draw(*(gunner_t**)list_node_val(it));
202
        it = list_node_next(it);
203
    }
204
}
205
206 231 up20180655
void (bullet_draw_list)(list_t *bullet_list) {
207
    if (list_size(bullet_list) == 0) return;
208
209
    list_node_t *it = list_begin(bullet_list);
210
    while (it != list_end(bullet_list)) {
211
        bullet_draw(*(bullet_t**)list_node_val(it));
212
        it = list_node_next(it);
213
    }
214
}
215
216 216 up20180642
struct map{
217
    basic_sprite_t *bsp_background;
218
    sprite_t *background;
219
    uint8_t *collide;
220 309 up20180642
    uint8_t *collide_gunner;
221
    int32_t *prev;
222
    uint8_t *visited;
223 216 up20180642
};
224 309 up20180642
static int (map_collides_gunner_pos)(const map_t *p, double shooter_x, double shooter_y, double radius) {
225
    for (double x = -radius; x <= radius; x += 1) {
226
        double y1 = sqrt(radius*radius - x*x);
227
        double y2 = -y1;
228
        if (map_collides_point(p, shooter_x + x, shooter_y + y1) || map_collides_point(p, shooter_x + x, shooter_y + y2)) return 1;
229
    }
230
    return 0;
231
}
232 216 up20180642
map_t* (map_ctor)(const char **background, const char **collide){
233
    map_t *ret = malloc(sizeof(map_t));
234
    if(ret == NULL) return NULL;
235
236
    ret->bsp_background = NULL;
237
    ret->background     = NULL;
238
    ret->collide        = NULL;
239 309 up20180642
    ret->prev           = NULL;
240
    ret->visited        = NULL;
241 216 up20180642
242
    ret->bsp_background = basic_sprite_ctor(background, 0, 0);
243
    ret->background     = sprite_ctor(ret->bsp_background);
244
    if(ret->bsp_background == NULL ||
245 307 up20180642
       ret->background     == NULL){ map_dtor(ret); return NULL; }
246 216 up20180642
247
    basic_sprite_t *bsp_collide = basic_sprite_ctor(collide, 0, 0);
248
    if(bsp_collide == NULL){ map_dtor(ret); return NULL; }
249
    const uint16_t W = basic_sprite_get_w(bsp_collide);
250
    const uint16_t H = basic_sprite_get_h(bsp_collide);
251
    ret->collide = malloc(W*H*sizeof(uint8_t));
252
    if(ret->collide == NULL){ map_dtor(ret); return NULL; }
253
    const uint8_t *m = basic_sprite_get_map(bsp_collide);
254
    for(unsigned i = 0; i < W*H; ++i){
255
        ret->collide[i] = (m[4*i+3] < ALPHA_THRESHOLD ? 1 : 0);
256
    }
257
    basic_sprite_dtor(bsp_collide);
258
259 309 up20180642
    ret->collide_gunner = malloc(W*H*sizeof(uint8_t));
260
    if(ret->collide_gunner == NULL){ map_dtor(ret); return NULL; }
261
    for(size_t i = 0; i < W*H; ++i){
262
        int16_t x = i%W, y = i/W;
263
        ret->collide_gunner[i] = (map_collides_gunner_pos(ret, x, y, 36) ? 1 : 0);
264
    }
265 307 up20180642
266 309 up20180642
    ret->prev = malloc(W*H*sizeof(int32_t));
267
    ret->visited = malloc(W*H*sizeof(uint8_t));
268
269
    if(ret->prev == NULL || ret->visited == NULL){
270 307 up20180642
        map_dtor(ret);
271
        return NULL;
272
    }
273 216 up20180642
    return ret;
274
}
275
void (map_dtor)(map_t *p){
276
    if(p == NULL) return;
277
    sprite_dtor(p->background);
278
    basic_sprite_dtor(p->bsp_background);
279
    free(p->collide);
280 309 up20180642
    free(p->prev);
281
    free(p->visited);
282 216 up20180642
    free(p);
283
}
284
int16_t (map_get_x_screen)(const map_t *p){ return (-x_origin)*scale; }
285
int16_t (map_get_y_screen)(const map_t *p){ return (-y_origin)*scale; }
286 246 up20180655
int16_t (map_get_width)   (const map_t *p){ return sprite_get_w(p->background); }
287
int16_t (map_get_height)  (const map_t *p){ return sprite_get_h(p->background); }
288 220 up20180655
int (map_collides_point)(const map_t *p, double x, double y){
289 216 up20180642
    const uint16_t w = sprite_get_w(p->background), h = sprite_get_h(p->background);
290
    int16_t x_ = x, y_ = y;
291
    if(x_ < 0 || w <= x_ || y_ < 0 || h <= y_) return 0;
292
    uint32_t pos = x_ + y_*w;
293
    return p->collide[pos];
294
}
295 309 up20180642
int (map_collides_gunner)(const map_t *p, const gunner_t *shooter) {
296
    double radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0;
297
    return map_collides_gunner_pos(p, gunner_get_x(shooter), gunner_get_y(shooter), radius);
298
}
299
int (map_make_dijkstra)(map_t *p, int16_t x, int16_t y){
300
301 307 up20180642
    const uint16_t W = basic_sprite_get_w(p->bsp_background),
302
                   H = basic_sprite_get_h(p->bsp_background);
303 309 up20180642
304
    static uint8_t first_time = true;
305
    if(first_time){
306
        for(size_t i = 0; i < W*H; ++i) p->prev[i] = -1;
307
        first_time = false;
308
    }
309 307 up20180642
    /// ACTUAL DIJKSTRA
310
    queue_t *q = queue_ctor();
311 309 up20180642
312
    memset(p->visited, false, W*H*sizeof(uint8_t));
313
314 307 up20180642
    int *ptr;
315 309 up20180642
    int32_t c, pos;
316
    c = y*W+x; p->prev[c] = c; ptr = malloc(sizeof(int)); *ptr = c; queue_push(q, ptr);
317
318 307 up20180642
    while(!queue_empty(q)){
319 309 up20180642
        c = *(int*)queue_top(q); free(queue_top(q)); queue_pop(q);
320
        x = c%W, y = c/W;
321
        if(p->visited[c]) continue;
322
        p->visited[c] = true;
323
        if(p->collide_gunner[c]) continue;
324
        if(0   <= x-1){ pos = y*W+(x-1); if(!p->visited[pos] && p->prev[pos] != c){ p->prev[pos] = c; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }}
325
        if(x+1 <  W  ){ pos = y*W+(x+1); if(!p->visited[pos] && p->prev[pos] != c){ p->prev[pos] = c; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }}
326
        if(0   <= y-1){ pos = (y-1)*W+x; if(!p->visited[pos] && p->prev[pos] != c){ p->prev[pos] = c; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }}
327
        if(y+1 <  H  ){ pos = (y+1)*W+x; if(!p->visited[pos] && p->prev[pos] != c){ p->prev[pos] = c; ptr = malloc(sizeof(int)); *ptr = pos; queue_push(q, ptr); }}
328 307 up20180642
    }
329 220 up20180655
330 309 up20180642
    queue_dtor(q);
331 307 up20180642
332 309 up20180642
    return SUCCESS;
333 220 up20180655
}
334 309 up20180642
int (map_where_to_follow)(const map_t *p, float x, float y, float *theta){
335
    const uint16_t W = basic_sprite_get_w(p->bsp_background);
336
    int x_ = x, y_ = y;
337
    int pos = y_*W+x_;
338
    //printf("Is in %d,%d\n", x_, y_);
339
    int newx = p->prev[pos]%W, newy = p->prev[pos]/W;
340
    //printf("from %d,%d to %d,%d\n", x_, y_, newx, newy);
341
    *theta = atan2(-(newy-y_), newx-x_);
342
    return SUCCESS;
343
}
344 220 up20180655
345 226 up20180642
int (map_collides_bullet)(const map_t *p, const bullet_t *bull){
346
    double radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0;
347
    double bullet_x = bullet_get_x(bull);
348
    double bullet_y = bullet_get_y(bull);
349
    for (double x = -radius; x < radius; x += 1){
350
        double y1 = sqrt(radius*radius - x*x);
351
        double y2 = -y1;
352
        if (map_collides_point(p, bullet_x + x, bullet_y + y1) || map_collides_point(p, bullet_x + x, bullet_y + y2)) return 1;
353
    }
354 220 up20180655
    return 0;
355
}
356
357 228 up20180655
int (gunner_collides_bullet)(const gunner_t *shooter, const bullet_t *bull){
358 236 up20180642
    if(bull->shooter == shooter) return false;
359
360 228 up20180655
    double shooter_radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0;
361
    double shooter_x = gunner_get_x(shooter);
362
    double shooter_y = gunner_get_y(shooter);
363
364
    double bullet_radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0;
365
    double bullet_x = bullet_get_x(bull);
366
    double bullet_y = bullet_get_y(bull);
367
368
    double dx = shooter_x - bullet_x;
369
    double dy = shooter_y - bullet_y;
370
    double distance = sqrt(dx*dx + dy*dy);
371
    return distance <= shooter_radius+bullet_radius;
372
}
373
374 308 up20180655
double (distance_gunners)(const gunner_t *shooter1, const gunner_t *shooter2) {
375 233 up20180655
    double shooter1_x = gunner_get_x(shooter1);
376
    double shooter1_y = gunner_get_y(shooter1);
377
378
    double shooter2_x = gunner_get_x(shooter2);
379
    double shooter2_y = gunner_get_y(shooter2);
380
381
    double dx = shooter1_x - shooter2_x;
382
    double dy = shooter1_y - shooter2_y;
383 308 up20180655
    return sqrt(dx*dx + dy*dy);
384
}
385
386
int (gunner_collides_gunner)(const gunner_t *shooter1, const gunner_t *shooter2) {
387
    double shooter1_radius = max(sprite_get_w(shooter1->dude), sprite_get_h(shooter1->dude))/2.0;
388
    double shooter2_radius = max(sprite_get_w(shooter2->dude), sprite_get_h(shooter2->dude))/2.0;
389
    double distance = distance_gunners(shooter1, shooter2);
390 233 up20180655
    return distance <= shooter1_radius+shooter2_radius;
391
}
392
393 216 up20180642
void   (map_draw)(map_t *p){
394
    const int16_t x_screen = map_get_x_screen(p);
395
    const int16_t y_screen = map_get_y_screen(p);
396
    sprite_set_pos  (p->background, x_screen, y_screen);
397
    sprite_set_scale(p->background, scale);
398
    sprite_draw     (p->background);
399
}