Project

General

Profile

Statistics
| Revision:

root / proj / src / ent.c @ 309

History | View | Annotate | Download (14.2 KB)

1
#include <lcom/lcf.h>
2

    
3
#include "ent.h"
4

    
5
#include "graph.h"
6
#include "utils.h"
7
#include "rectangle.h"
8
#include "font.h"
9
#include "errors.h"
10
#include "queue.h"
11
#include <math.h>
12

    
13
#define GREEN_HEALTH_COLOR      0x009900
14

    
15
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
double (ent_get_scale)  (void){ return scale; }
22
double (ent_get_XLength)(void){ return graph_get_XRes()/scale; }
23
double (ent_get_YLength)(void){ return graph_get_YRes()/scale; }
24

    
25
struct gunner{
26
    double x, y; //real position
27
    double spawn_x, spawn_y;
28
    sprite_t *dude;
29
    sprite_t *weapon;
30
    double health, current_health;
31
    text_t *txt;
32
    gunner_type type;
33
    int team;
34
};
35
gunner_t* (gunner_ctor)(basic_sprite_t *dude, basic_sprite_t *weapon, gunner_type type, int team){
36
    gunner_t *ret = malloc(sizeof(gunner_t));
37
    if(ret == NULL) return NULL;
38
    ret->spawn_x = 0.0;
39
    ret->spawn_y = 0.0;
40
    ret->x = 0.0;
41
    ret->y = 0.0;
42
    ret->health = 100;
43
    ret->current_health = ret->health;
44
    ret->txt = text_ctor(consolas, "");
45
    ret->type = type;
46
    ret->team = team;
47
    ret->dude   = sprite_ctor(dude  );
48
    ret->weapon = sprite_ctor(weapon);
49
    if(ret->dude == NULL || ret->weapon == NULL){
50
        gunner_dtor(ret);
51
        return NULL;
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;
58
}
59
void (gunner_dtor)(gunner_t *p){
60
    if(p == NULL) return;
61
    sprite_dtor(p->dude);
62
    sprite_dtor(p->weapon);
63
    text_dtor(p->txt);
64
    free(p);
65
}
66
void (gunner_set_pos)  (gunner_t *p, double x, double y){ p->x = x; p->y = y; }
67
void (gunner_set_spawn)  (gunner_t *p, double x, double y){ p->spawn_x = x; p->spawn_y = y; }
68
void (gunner_set_angle)(gunner_t *p, double angle      ){
69
    sprite_set_angle(p->dude  , angle);
70
    sprite_set_angle(p->weapon, angle);
71
}
72
void (gunner_set_health)        (gunner_t *p, double health) {
73
    if (health < 0) health = 0;
74
    p->health = health;
75
}
76
void (gunner_set_curr_health)   (gunner_t *p, double health) {
77
    if (health < 0) health = 0;
78
    p->current_health = health;
79
}
80
double  (gunner_get_x)              (const gunner_t *p){ return p->x; }
81
double  (gunner_get_y)              (const gunner_t *p){ return p->y; }
82
double  (gunner_get_spawn_x)        (const gunner_t *p){ return p->spawn_x; }
83
double  (gunner_get_spawn_y)        (const gunner_t *p){ return p->spawn_y; }
84
double  (gunner_get_angle)          (const gunner_t *p){ return sprite_get_angle(p->dude); }
85
double  (gunner_get_health)         (const gunner_t *p){ return p->health; }
86
double  (gunner_get_curr_health)    (const gunner_t *p){ return p->current_health; }
87
int16_t (gunner_get_x_screen)       (const gunner_t *p){ return (p->x-x_origin)*scale; }
88
int16_t (gunner_get_y_screen)       (const gunner_t *p){ return (p->y-y_origin)*scale; }
89
gunner_type (gunner_get_type)       (const gunner_t *p){ return p->type; }
90
int     (gunner_get_team)           (const gunner_t *p){ return p->team; }
91
void (gunner_draw)(gunner_t *p){
92
    const int16_t x_screen = gunner_get_x_screen(p);
93
    const int16_t y_screen = gunner_get_y_screen(p);
94
    sprite_set_pos  (p->dude  , x_screen, y_screen);
95
    sprite_set_pos  (p->weapon, x_screen, y_screen);
96
    sprite_set_scale(p->dude  , scale);
97
    sprite_set_scale(p->weapon, scale);
98
    sprite_draw     (p->weapon);
99
    sprite_draw     (p->dude  );
100
    gunner_draw_health(p);
101
}
102

    
103
void (gunner_draw_health)(const gunner_t *p) {
104
    int16_t w = sprite_get_w(p->dude);
105
    int16_t h = sprite_get_h(p->dude);
106
    double x = gunner_get_x_screen(p) - w/2;
107
    double y = gunner_get_y_screen(p) - h/2 - 10;
108
    double curr_health = gunner_get_curr_health(p);
109
    double health = gunner_get_health(p);
110
    double perc = curr_health/health;
111
    rectangle_t *green_bar = rectangle_ctor(x, y, (int16_t)(w*perc), 10);
112
    rectangle_set_fill_color(green_bar, GREEN_HEALTH_COLOR);
113
    rectangle_t *red_bar = rectangle_ctor(x+(int16_t)(w*perc), y, (int16_t)(w*(1-perc)), 10);
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);
118
    rectangle_draw(green_bar);
119
    rectangle_draw(red_bar);
120
    text_draw(p->txt);
121
    rectangle_dtor(green_bar);
122
    rectangle_dtor(red_bar);
123

    
124
}
125

    
126
double (gunner_distance)(const gunner_t *p1, const gunner_t *p2){
127
    double dx = gunner_get_x(p1) - gunner_get_x(p2);
128
    double dy = gunner_get_y(p1) - gunner_get_y(p2);
129
    return sqrt(dx*dx+dy*dy);
130
}
131

    
132
struct bullet{
133
    const gunner_t *shooter;
134
    double x, y; //real position
135
    double vx, vy;
136
    sprite_t *b;
137
    double damage;
138
};
139
bullet_t* (bullet_ctor)(const gunner_t *shooter, const basic_sprite_t *b, double x, double y, double vx, double vy){
140
    bullet_t *ret = malloc(sizeof(bullet_t));
141
    if(ret == NULL) return NULL;
142
    ret->shooter = shooter;
143
    ret-> x =  x;
144
    ret-> y =  y;
145
    ret->vx = vx;
146
    ret->vy = vy;
147
    ret->damage = 10;
148
    ret->b = sprite_ctor(b);
149
    if(ret->b == NULL){
150
        bullet_dtor(ret);
151
        return NULL;
152
    }
153
    double angle = atan2(-ret->vy, ret->vx);
154
    sprite_set_angle(ret->b, angle-M_PI_2);
155
    return ret;
156
}
157
void (bullet_dtor)(bullet_t *p){
158
    if(p == NULL) return;
159
    sprite_dtor(p->b);
160
    free(p);
161
}
162
double  (bullet_get_x)       (const bullet_t *p){ return p->x; }
163
double  (bullet_get_y)       (const bullet_t *p){ return p->y; }
164
int16_t (bullet_get_x_screen)(const bullet_t *p){ return (p->x-x_origin)*scale; }
165
int16_t (bullet_get_y_screen)(const bullet_t *p){ return (p->y-y_origin)*scale; }
166
double  (bullet_get_damage)  (const bullet_t *p){ return p->damage; }
167
void    (bullet_set_damage)  (bullet_t *p, double damage) {
168
    if (damage < 0) damage = 0;
169
    p->damage = damage;
170
}
171
const gunner_t* (bullet_get_shooter)(const bullet_t *p){ return p->shooter; }
172
void (bullet_update_movement)(bullet_t *p){
173
    p->x += p->vx;
174
    p->y += p->vy;
175
}
176

    
177
void (bullet_update_movement_list)(list_t *bullet_list){
178
    if (list_size(bullet_list) == 0) return;
179

    
180
    list_node_t *it = list_begin(bullet_list);
181
    while (it != list_end(bullet_list)) {
182
        bullet_update_movement(*(bullet_t**)list_node_val(it));
183
        it = list_node_next(it);
184
    }
185
}
186

    
187
void (bullet_draw)(bullet_t *p){
188
    const int16_t x_screen = bullet_get_x_screen(p);
189
    const int16_t y_screen = bullet_get_y_screen(p);
190
    sprite_set_pos  (p->b, x_screen, y_screen);
191
    sprite_set_scale(p->b, scale);
192
    sprite_draw     (p->b);
193
}
194

    
195
void (gunner_draw_list)(list_t *shooter_list) {
196
    if (list_size(shooter_list) == 0) return;
197

    
198
    list_node_t *it = list_begin(shooter_list);
199
    while (it != list_end(shooter_list)) {
200
        gunner_draw(*(gunner_t**)list_node_val(it));
201
        it = list_node_next(it);
202
    }
203
}
204

    
205
void (bullet_draw_list)(list_t *bullet_list) {
206
    if (list_size(bullet_list) == 0) return;
207

    
208
    list_node_t *it = list_begin(bullet_list);
209
    while (it != list_end(bullet_list)) {
210
        bullet_draw(*(bullet_t**)list_node_val(it));
211
        it = list_node_next(it);
212
    }
213
}
214

    
215
struct map{
216
    basic_sprite_t *bsp_background;
217
    sprite_t *background;
218
    uint8_t *collide;
219
    uint8_t *collide_gunner;
220
    int32_t *prev;
221
    uint8_t *visited;
222
};
223
static int (map_collides_gunner_pos)(const map_t *p, double shooter_x, double shooter_y, double radius) {
224
    for (double x = -radius; x <= radius; x += 1) {
225
        double y1 = sqrt(radius*radius - x*x);
226
        double y2 = -y1;
227
        if (map_collides_point(p, shooter_x + x, shooter_y + y1) || map_collides_point(p, shooter_x + x, shooter_y + y2)) return 1;
228
    }
229
    return 0;
230
}
231
map_t* (map_ctor)(const char **background, const char **collide){
232
    map_t *ret = malloc(sizeof(map_t));
233
    if(ret == NULL) return NULL;
234

    
235
    ret->bsp_background = NULL;
236
    ret->background     = NULL;
237
    ret->collide        = NULL;
238
    ret->prev           = NULL;
239
    ret->visited        = NULL;
240

    
241
    ret->bsp_background = basic_sprite_ctor(background, 0, 0);
242
    ret->background     = sprite_ctor(ret->bsp_background);
243
    if(ret->bsp_background == NULL ||
244
       ret->background     == NULL){ map_dtor(ret); return NULL; }
245

    
246
    basic_sprite_t *bsp_collide = basic_sprite_ctor(collide, 0, 0);
247
    if(bsp_collide == NULL){ map_dtor(ret); return NULL; }
248
    const uint16_t W = basic_sprite_get_w(bsp_collide);
249
    const uint16_t H = basic_sprite_get_h(bsp_collide);
250
    ret->collide = malloc(W*H*sizeof(uint8_t));
251
    if(ret->collide == NULL){ map_dtor(ret); return NULL; }
252
    const uint8_t *m = basic_sprite_get_map(bsp_collide);
253
    for(unsigned i = 0; i < W*H; ++i){
254
        ret->collide[i] = (m[4*i+3] < ALPHA_THRESHOLD ? 1 : 0);
255
    }
256
    basic_sprite_dtor(bsp_collide);
257

    
258
    ret->collide_gunner = malloc(W*H*sizeof(uint8_t));
259
    if(ret->collide_gunner == NULL){ map_dtor(ret); return NULL; }
260
    for(size_t i = 0; i < W*H; ++i){
261
        int16_t x = i%W, y = i/W;
262
        ret->collide_gunner[i] = (map_collides_gunner_pos(ret, x, y, 36) ? 1 : 0);
263
    }
264

    
265
    ret->prev = malloc(W*H*sizeof(int32_t));
266
    ret->visited = malloc(W*H*sizeof(uint8_t));
267

    
268
    if(ret->prev == NULL || ret->visited == NULL){
269
        map_dtor(ret);
270
        return NULL;
271
    }
272
    return ret;
273
}
274
void (map_dtor)(map_t *p){
275
    if(p == NULL) return;
276
    sprite_dtor(p->background);
277
    basic_sprite_dtor(p->bsp_background);
278
    free(p->collide);
279
    free(p->prev);
280
    free(p->visited);
281
    free(p);
282
}
283
int16_t (map_get_x_screen)(const map_t *p){ return (-x_origin)*scale; }
284
int16_t (map_get_y_screen)(const map_t *p){ return (-y_origin)*scale; }
285
int16_t (map_get_width)   (const map_t *p){ return sprite_get_w(p->background); }
286
int16_t (map_get_height)  (const map_t *p){ return sprite_get_h(p->background); }
287
int (map_collides_point)(const map_t *p, double x, double y){
288
    const uint16_t w = sprite_get_w(p->background), h = sprite_get_h(p->background);
289
    int16_t x_ = x, y_ = y;
290
    if(x_ < 0 || w <= x_ || y_ < 0 || h <= y_) return 0;
291
    uint32_t pos = x_ + y_*w;
292
    return p->collide[pos];
293
}
294
int (map_collides_gunner)(const map_t *p, const gunner_t *shooter) {
295
    double radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0;
296
    return map_collides_gunner_pos(p, gunner_get_x(shooter), gunner_get_y(shooter), radius);
297
}
298
int (map_make_dijkstra)(map_t *p, int16_t x, int16_t y){
299

    
300
    const uint16_t W = basic_sprite_get_w(p->bsp_background),
301
                   H = basic_sprite_get_h(p->bsp_background);
302

    
303
    static uint8_t first_time = true;
304
    if(first_time){
305
        for(size_t i = 0; i < W*H; ++i) p->prev[i] = -1;
306
        first_time = false;
307
    }
308
    /// ACTUAL DIJKSTRA
309
    queue_t *q = queue_ctor();
310

    
311
    memset(p->visited, false, W*H*sizeof(uint8_t));
312

    
313
    int *ptr;
314
    int32_t c, pos;
315
    c = y*W+x; p->prev[c] = c; ptr = malloc(sizeof(int)); *ptr = c; queue_push(q, ptr);
316

    
317
    while(!queue_empty(q)){
318
        c = *(int*)queue_top(q); free(queue_top(q)); queue_pop(q);
319
        x = c%W, y = c/W;
320
        if(p->visited[c]) continue;
321
        p->visited[c] = true;
322
        if(p->collide_gunner[c]) continue;
323
        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); }}
324
        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); }}
325
        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); }}
326
        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); }}
327
    }
328

    
329
    queue_dtor(q);
330

    
331
    return SUCCESS;
332
}
333
int (map_where_to_follow)(const map_t *p, float x, float y, float *theta){
334
    const uint16_t W = basic_sprite_get_w(p->bsp_background);
335
    int x_ = x, y_ = y;
336
    int pos = y_*W+x_;
337
    //printf("Is in %d,%d\n", x_, y_);
338
    int newx = p->prev[pos]%W, newy = p->prev[pos]/W;
339
    //printf("from %d,%d to %d,%d\n", x_, y_, newx, newy);
340
    *theta = atan2(-(newy-y_), newx-x_);
341
    return SUCCESS;
342
}
343

    
344
int (map_collides_bullet)(const map_t *p, const bullet_t *bull){
345
    double radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0;
346
    double bullet_x = bullet_get_x(bull);
347
    double bullet_y = bullet_get_y(bull);
348
    for (double x = -radius; x < radius; x += 1){
349
        double y1 = sqrt(radius*radius - x*x);
350
        double y2 = -y1;
351
        if (map_collides_point(p, bullet_x + x, bullet_y + y1) || map_collides_point(p, bullet_x + x, bullet_y + y2)) return 1;
352
    }
353
    return 0;
354
}
355

    
356
int (gunner_collides_bullet)(const gunner_t *shooter, const bullet_t *bull){
357
    if(bull->shooter == shooter) return false;
358

    
359
    double shooter_radius = max(sprite_get_w(shooter->dude), sprite_get_h(shooter->dude))/2.0;
360
    double shooter_x = gunner_get_x(shooter);
361
    double shooter_y = gunner_get_y(shooter);
362

    
363
    double bullet_radius = max(sprite_get_w(bull->b), sprite_get_h(bull->b))/2.0;
364
    double bullet_x = bullet_get_x(bull);
365
    double bullet_y = bullet_get_y(bull);
366

    
367
    double dx = shooter_x - bullet_x;
368
    double dy = shooter_y - bullet_y;
369
    double distance = sqrt(dx*dx + dy*dy);
370
    return distance <= shooter_radius+bullet_radius;
371
}
372

    
373
double (distance_gunners)(const gunner_t *shooter1, const gunner_t *shooter2) {
374
    double shooter1_x = gunner_get_x(shooter1);
375
    double shooter1_y = gunner_get_y(shooter1);
376

    
377
    double shooter2_x = gunner_get_x(shooter2);
378
    double shooter2_y = gunner_get_y(shooter2);
379

    
380
    double dx = shooter1_x - shooter2_x;
381
    double dy = shooter1_y - shooter2_y;
382
    return sqrt(dx*dx + dy*dy);
383
}
384

    
385
int (gunner_collides_gunner)(const gunner_t *shooter1, const gunner_t *shooter2) {
386
    double shooter1_radius = max(sprite_get_w(shooter1->dude), sprite_get_h(shooter1->dude))/2.0;
387
    double shooter2_radius = max(sprite_get_w(shooter2->dude), sprite_get_h(shooter2->dude))/2.0;
388
    double distance = distance_gunners(shooter1, shooter2);
389
    return distance <= shooter1_radius+shooter2_radius;
390
}
391

    
392
void   (map_draw)(map_t *p){
393
    const int16_t x_screen = map_get_x_screen(p);
394
    const int16_t y_screen = map_get_y_screen(p);
395
    sprite_set_pos  (p->background, x_screen, y_screen);
396
    sprite_set_scale(p->background, scale);
397
    sprite_draw     (p->background);
398
}