Project

General

Profile

Statistics
| Revision:

root / proj / src / ent.c @ 328

History | View | Annotate | Download (14.9 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 323 up20180642
static double x_origin = 0;
17
static double y_origin = 0;
18 193 up20180642
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 321 up20180642
    uint16_t type;
34 302 up20180642
    int team;
35 193 up20180642
};
36 321 up20180642
gunner_t* (gunner_ctor)(basic_sprite_t *dude, basic_sprite_t *weapon, uint16_t 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 323 up20180642
int16_t (gunner_get_x_screen)       (const gunner_t *p){ return (int16_t)((p->x-x_origin)*scale); }
95
int16_t (gunner_get_y_screen)       (const gunner_t *p){ return (int16_t)((p->y-y_origin)*scale); }
96 321 up20180642
uint16_t (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 323 up20180642
    const uint16_t w = sprite_get_w(p->dude);
111
    const uint16_t h = sprite_get_h(p->dude);
112
    int16_t x = gunner_get_x_screen(p) - w/2;
113
    int16_t 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 323 up20180642
    rectangle_set_pos(p->green_bar, x, y); rectangle_set_size(p->green_bar, (uint16_t)(w*perc), 10);
118
    rectangle_set_pos(p->red_bar, x+(int16_t)(w*perc), y); rectangle_set_size(p->red_bar, (uint16_t)(w*(1.0-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 320 up20180655
159 203 up20180642
void (bullet_dtor)(bullet_t *p){
160
    if(p == NULL) return;
161
    sprite_dtor(p->b);
162
    free(p);
163
}
164
double  (bullet_get_x)       (const bullet_t *p){ return p->x; }
165
double  (bullet_get_y)       (const bullet_t *p){ return p->y; }
166 320 up20180655
double  (bullet_get_vx)      (const bullet_t *p){ return p->vx; }
167
double  (bullet_get_vy)      (const bullet_t *p){ return p->vy; }
168 323 up20180642
int16_t (bullet_get_x_screen)(const bullet_t *p){ return (int16_t)((p->x-x_origin)*scale); }
169
int16_t (bullet_get_y_screen)(const bullet_t *p){ return (int16_t)((p->y-y_origin)*scale); }
170 267 up20180655
double  (bullet_get_damage)  (const bullet_t *p){ return p->damage; }
171
void    (bullet_set_damage)  (bullet_t *p, double damage) {
172 230 up20180655
    if (damage < 0) damage = 0;
173
    p->damage = damage;
174
}
175 302 up20180642
const gunner_t* (bullet_get_shooter)(const bullet_t *p){ return p->shooter; }
176 226 up20180642
void (bullet_update_movement)(bullet_t *p){
177
    p->x += p->vx;
178
    p->y += p->vy;
179
}
180 231 up20180655
181
void (bullet_update_movement_list)(list_t *bullet_list){
182
    if (list_size(bullet_list) == 0) return;
183
184
    list_node_t *it = list_begin(bullet_list);
185
    while (it != list_end(bullet_list)) {
186
        bullet_update_movement(*(bullet_t**)list_node_val(it));
187
        it = list_node_next(it);
188
    }
189
}
190
191 203 up20180642
void (bullet_draw)(bullet_t *p){
192
    const int16_t x_screen = bullet_get_x_screen(p);
193
    const int16_t y_screen = bullet_get_y_screen(p);
194
    sprite_set_pos  (p->b, x_screen, y_screen);
195
    sprite_set_scale(p->b, scale);
196
    sprite_draw     (p->b);
197
}
198 216 up20180642
199 248 up20180655
void (gunner_draw_list)(list_t *shooter_list) {
200
    if (list_size(shooter_list) == 0) return;
201
202
    list_node_t *it = list_begin(shooter_list);
203
    while (it != list_end(shooter_list)) {
204
        gunner_draw(*(gunner_t**)list_node_val(it));
205
        it = list_node_next(it);
206
    }
207
}
208
209 231 up20180655
void (bullet_draw_list)(list_t *bullet_list) {
210
    if (list_size(bullet_list) == 0) return;
211
212
    list_node_t *it = list_begin(bullet_list);
213
    while (it != list_end(bullet_list)) {
214
        bullet_draw(*(bullet_t**)list_node_val(it));
215
        it = list_node_next(it);
216
    }
217
}
218
219 216 up20180642
struct map{
220
    basic_sprite_t *bsp_background;
221
    sprite_t *background;
222
    uint8_t *collide;
223 309 up20180642
    uint8_t *collide_gunner;
224
    int32_t *prev;
225
    uint8_t *visited;
226 216 up20180642
};
227 309 up20180642
static int (map_collides_gunner_pos)(const map_t *p, double shooter_x, double shooter_y, double radius) {
228
    for (double x = -radius; x <= radius; x += 1) {
229
        double y1 = sqrt(radius*radius - x*x);
230
        double y2 = -y1;
231
        if (map_collides_point(p, shooter_x + x, shooter_y + y1) || map_collides_point(p, shooter_x + x, shooter_y + y2)) return 1;
232
    }
233
    return 0;
234
}
235 321 up20180642
map_t* (map_ctor)(const char *const *background, const char *const *collide){
236 216 up20180642
    map_t *ret = malloc(sizeof(map_t));
237
    if(ret == NULL) return NULL;
238
239
    ret->bsp_background = NULL;
240
    ret->background     = NULL;
241
    ret->collide        = NULL;
242 309 up20180642
    ret->prev           = NULL;
243
    ret->visited        = NULL;
244 216 up20180642
245
    ret->bsp_background = basic_sprite_ctor(background, 0, 0);
246
    ret->background     = sprite_ctor(ret->bsp_background);
247
    if(ret->bsp_background == NULL ||
248 307 up20180642
       ret->background     == NULL){ map_dtor(ret); return NULL; }
249 216 up20180642
250
    basic_sprite_t *bsp_collide = basic_sprite_ctor(collide, 0, 0);
251
    if(bsp_collide == NULL){ map_dtor(ret); return NULL; }
252
    const uint16_t W = basic_sprite_get_w(bsp_collide);
253
    const uint16_t H = basic_sprite_get_h(bsp_collide);
254
    ret->collide = malloc(W*H*sizeof(uint8_t));
255
    if(ret->collide == NULL){ map_dtor(ret); return NULL; }
256
    const uint8_t *m = basic_sprite_get_map(bsp_collide);
257
    for(unsigned i = 0; i < W*H; ++i){
258
        ret->collide[i] = (m[4*i+3] < ALPHA_THRESHOLD ? 1 : 0);
259
    }
260
    basic_sprite_dtor(bsp_collide);
261
262 309 up20180642
    ret->collide_gunner = malloc(W*H*sizeof(uint8_t));
263
    if(ret->collide_gunner == NULL){ map_dtor(ret); return NULL; }
264
    for(size_t i = 0; i < W*H; ++i){
265 323 up20180642
        uint16_t x = i%W, y = (uint16_t)(i/W);
266 309 up20180642
        ret->collide_gunner[i] = (map_collides_gunner_pos(ret, x, y, 36) ? 1 : 0);
267
    }
268 307 up20180642
269 309 up20180642
    ret->prev = malloc(W*H*sizeof(int32_t));
270
    ret->visited = malloc(W*H*sizeof(uint8_t));
271
272
    if(ret->prev == NULL || ret->visited == NULL){
273 307 up20180642
        map_dtor(ret);
274
        return NULL;
275
    }
276 216 up20180642
    return ret;
277
}
278
void (map_dtor)(map_t *p){
279
    if(p == NULL) return;
280
    sprite_dtor(p->background);
281
    basic_sprite_dtor(p->bsp_background);
282
    free(p->collide);
283 309 up20180642
    free(p->prev);
284
    free(p->visited);
285 216 up20180642
    free(p);
286
}
287 328 up20180642
static int16_t (map_get_x_screen)(const map_t *p){ (void)p; return (int16_t)((-x_origin)*scale); }
288
static int16_t (map_get_y_screen)(const map_t *p){ (void)p; return (int16_t)((-y_origin)*scale); }
289 323 up20180642
uint16_t (map_get_width)   (const map_t *p){ return sprite_get_w(p->background); }
290
uint16_t (map_get_height)  (const map_t *p){ return sprite_get_h(p->background); }
291 220 up20180655
int (map_collides_point)(const map_t *p, double x, double y){
292 216 up20180642
    const uint16_t w = sprite_get_w(p->background), h = sprite_get_h(p->background);
293 323 up20180642
    int16_t x_ = (int16_t)x, y_ = (int16_t)y;
294 216 up20180642
    if(x_ < 0 || w <= x_ || y_ < 0 || h <= y_) return 0;
295 323 up20180642
    int32_t pos = x_ + y_*w;
296
    if(0 <= pos && pos < w*h) return p->collide[pos];
297
    else return false;
298 216 up20180642
}
299 309 up20180642
int (map_collides_gunner)(const map_t *p, const gunner_t *shooter) {
300 323 up20180642
    double radius = dmax(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0;
301 309 up20180642
    return map_collides_gunner_pos(p, gunner_get_x(shooter), gunner_get_y(shooter), radius);
302
}
303 321 up20180642
int (map_make_dijkstra)(map_t *p, double x_, double y_){
304 324 up20180642
    int16_t x = (int16_t)x_, y = (int16_t)y_;
305 309 up20180642
306 307 up20180642
    const uint16_t W = basic_sprite_get_w(p->bsp_background),
307
                   H = basic_sprite_get_h(p->bsp_background);
308 309 up20180642
309
    static uint8_t first_time = true;
310
    if(first_time){
311
        for(size_t i = 0; i < W*H; ++i) p->prev[i] = -1;
312
        first_time = false;
313
    }
314 307 up20180642
    /// ACTUAL DIJKSTRA
315
    queue_t *q = queue_ctor();
316 309 up20180642
317
    memset(p->visited, false, W*H*sizeof(uint8_t));
318
319 307 up20180642
    int *ptr;
320 309 up20180642
    int32_t c, pos;
321
    c = y*W+x; p->prev[c] = c; ptr = malloc(sizeof(int)); *ptr = c; queue_push(q, ptr);
322
323 307 up20180642
    while(!queue_empty(q)){
324 309 up20180642
        c = *(int*)queue_top(q); free(queue_top(q)); queue_pop(q);
325 324 up20180642
        x = (int16_t)(c%W), y = (int16_t)(c/W);
326 309 up20180642
        if(p->visited[c]) continue;
327
        p->visited[c] = true;
328
        if(p->collide_gunner[c]) continue;
329
        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); }}
330
        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); }}
331
        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); }}
332
        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); }}
333 307 up20180642
    }
334 220 up20180655
335 309 up20180642
    queue_dtor(q);
336 307 up20180642
337 309 up20180642
    return SUCCESS;
338 220 up20180655
}
339 321 up20180642
int (map_where_to_follow)(const map_t *p, double x, double y, double *theta){
340 309 up20180642
    const uint16_t W = basic_sprite_get_w(p->bsp_background);
341 324 up20180642
    int16_t x_ = (int16_t)x, y_ = (int16_t)y;
342
    int32_t pos = y_*W+x_;
343
    int16_t newx = (int16_t)(p->prev[pos]%W), newy = (int16_t)(p->prev[pos]/W);
344 309 up20180642
    *theta = atan2(-(newy-y_), newx-x_);
345
    return SUCCESS;
346
}
347 220 up20180655
348 226 up20180642
int (map_collides_bullet)(const map_t *p, const bullet_t *bull){
349 323 up20180642
    double radius = dmax(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0;
350 226 up20180642
    double bullet_x = bullet_get_x(bull);
351
    double bullet_y = bullet_get_y(bull);
352
    for (double x = -radius; x < radius; x += 1){
353
        double y1 = sqrt(radius*radius - x*x);
354
        double y2 = -y1;
355
        if (map_collides_point(p, bullet_x + x, bullet_y + y1) || map_collides_point(p, bullet_x + x, bullet_y + y2)) return 1;
356
    }
357 220 up20180655
    return 0;
358
}
359
360 228 up20180655
int (gunner_collides_bullet)(const gunner_t *shooter, const bullet_t *bull){
361 236 up20180642
    if(bull->shooter == shooter) return false;
362
363 323 up20180642
    double shooter_radius = dmax(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0;
364 228 up20180655
    double shooter_x = gunner_get_x(shooter);
365
    double shooter_y = gunner_get_y(shooter);
366
367 323 up20180642
    double bullet_radius = dmax(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0;
368 228 up20180655
    double bullet_x = bullet_get_x(bull);
369
    double bullet_y = bullet_get_y(bull);
370
371
    double dx = shooter_x - bullet_x;
372
    double dy = shooter_y - bullet_y;
373
    double distance = sqrt(dx*dx + dy*dy);
374
    return distance <= shooter_radius+bullet_radius;
375
}
376
377 308 up20180655
double (distance_gunners)(const gunner_t *shooter1, const gunner_t *shooter2) {
378 233 up20180655
    double shooter1_x = gunner_get_x(shooter1);
379
    double shooter1_y = gunner_get_y(shooter1);
380
381
    double shooter2_x = gunner_get_x(shooter2);
382
    double shooter2_y = gunner_get_y(shooter2);
383
384
    double dx = shooter1_x - shooter2_x;
385
    double dy = shooter1_y - shooter2_y;
386 308 up20180655
    return sqrt(dx*dx + dy*dy);
387
}
388
389
int (gunner_collides_gunner)(const gunner_t *shooter1, const gunner_t *shooter2) {
390 320 up20180655
    if (shooter1 == shooter2) return false;
391 323 up20180642
    double shooter1_radius = dmax(sprite_get_w(shooter1->dude), sprite_get_h(shooter1->dude))/2.0;
392
    double shooter2_radius = dmax(sprite_get_w(shooter2->dude), sprite_get_h(shooter2->dude))/2.0;
393 308 up20180655
    double distance = distance_gunners(shooter1, shooter2);
394 233 up20180655
    return distance <= shooter1_radius+shooter2_radius;
395
}
396
397 216 up20180642
void   (map_draw)(map_t *p){
398
    const int16_t x_screen = map_get_x_screen(p);
399
    const int16_t y_screen = map_get_y_screen(p);
400
    sprite_set_pos  (p->background, x_screen, y_screen);
401
    sprite_set_scale(p->background, scale);
402
    sprite_draw     (p->background);
403
}