Project

General

Profile

Statistics
| Revision:

root / proj / src / ent.c @ 335

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