Project

General

Profile

Statistics
| Revision:

root / proj / src / proj_func.c @ 349

History | View | Annotate | Download (14.6 KB)

1 167 up20180655
#include <lcom/lcf.h>
2
3 156 up20180655
#include "proj_func.h"
4 167 up20180655
5 156 up20180655
#include "interrupts_func.h"
6 166 up20180642
#include "graph.h"
7 167 up20180655
#include "keyboard.h"
8 156 up20180655
#include "errors.h"
9 167 up20180655
#include "proj_macros.h"
10
#include "utils.h"
11 200 up20180655
#include "ent.h"
12 231 up20180655
#include "fast_math.h"
13 251 up20180655
#include "rectangle.h"
14
#include "font.h"
15 156 up20180655
16 167 up20180655
#include <math.h>
17
18 156 up20180655
int cleanup(void) {
19
    int r = SUCCESS;
20
    if ((r = unsubscribe_all()))
21
        printf("%s: failed to unsubscribe drivers.\n", __func__);
22 166 up20180642
    if ((r = graph_cleanup()))
23
        printf("%s: graph cleanup failed\n", __func__);
24 156 up20180655
25
    return r;
26
}
27 167 up20180655
28 200 up20180655
static keys_t key_presses;
29 184 up20180655
void update_key_presses(void) {
30 321 up20180642
    if (keyboard_get_size() == 1) {
31
        switch(keyboard_get_scancode()[0]) {
32 200 up20180655
        case W_MAKE_CODE        : key_presses.w_pressed     = 1;        break;
33
        case W_BREAK_CODE       : key_presses.w_pressed     = 0;        break;
34
        case A_MAKE_CODE        : key_presses.a_pressed     = 1;        break;
35
        case A_BREAK_CODE       : key_presses.a_pressed     = 0;        break;
36
        case S_MAKE_CODE        : key_presses.s_pressed     = 1;        break;
37
        case S_BREAK_CODE       : key_presses.s_pressed     = 0;        break;
38
        case D_MAKE_CODE        : key_presses.d_pressed     = 1;        break;
39
        case D_BREAK_CODE       : key_presses.d_pressed     = 0;        break;
40
        case CTRL_MAKE_CODE     : key_presses.ctrl_pressed  = 1;        break;
41
        case CTRL_BREAK_CODE    : key_presses.ctrl_pressed  = 0;        break;
42 250 up20180655
        case PLUS_MAKE_CODE     : key_presses.plus_pressed  = 1;    update_scale();        break;
43
        case PLUS_BREAK_CODE    : key_presses.plus_pressed  = 0;    update_scale();        break;
44
        case MINUS_MAKE_CODE    : key_presses.minus_pressed = 1;    update_scale();        break;
45
        case MINUS_BREAK_CODE   : key_presses.minus_pressed = 0;    update_scale();        break;
46 167 up20180655
        }
47
    }
48
}
49 345 up20180642
keys_t* (get_key_presses)(void) {
50
    return &key_presses;
51
}
52 240 up20180655
53 345 up20180642
static int16_t mouse_x = 0, mouse_y = 0;
54
void (update_mouse)(struct packet *p) {
55
    mouse_x = max_16(0, mouse_x + p->delta_x);
56
    mouse_x = min_16(mouse_x, (int16_t)graph_get_XRes() - 1);
57
58
    mouse_y = max_16(0, mouse_y - p->delta_y);
59
    mouse_y = min_16(mouse_y, (int16_t)graph_get_YRes() - 1);
60
61
    key_presses.lb_pressed = p->lb;
62
}
63
int16_t* get_mouse_X(void) { return &mouse_x; }
64
int16_t* get_mouse_Y(void) { return &mouse_y; }
65
double get_mouse_angle(gunner_t *p) {
66
    return atan2(gunner_get_y_screen(p) - mouse_y, mouse_x - gunner_get_x_screen(p));
67
}
68
69 307 up20180642
void update_movement(map_t *map, gunner_t *p, keys_t *keys, list_t *shooter_list) {
70 324 up20180642
    /** Player movement */{
71
        int ver_mov = keys->s_pressed - keys->w_pressed;
72
        int hor_mov = keys->d_pressed - keys->a_pressed;
73
        double x = gunner_get_x(p);
74
        double y = gunner_get_y(p);
75 246 up20180655
76 324 up20180642
        gunner_set_pos(p, x + SHOOTER_SPEED * hor_mov, y);
77
        if (map_collides_gunner(map, p)) gunner_set_pos(p, x, y);
78
        else {
79
            list_node_t *it = list_begin(shooter_list);
80
            while (it != list_end(shooter_list)) {
81
                gunner_t *p2 = *(gunner_t**)list_node_val(it);
82
                if (p != p2 && gunner_collides_gunner(p, p2)) {
83
                    gunner_set_pos(p, x, y);
84
                    break;
85
                }
86
                it = list_node_next(it);
87 246 up20180655
            }
88
        }
89 324 up20180642
        x = gunner_get_x(p);
90
        gunner_set_pos(p, x, y + SHOOTER_SPEED * ver_mov);
91
        if (map_collides_gunner(map, p)) gunner_set_pos(p, x, y);
92
        else {
93
            list_node_t *it = list_begin(shooter_list);
94
            while (it != list_end(shooter_list)) {
95
                gunner_t *p2 = *(gunner_t**)list_node_val(it);
96
                if (p != p2 && gunner_collides_gunner(p, p2)) {
97
                    gunner_set_pos(p, x, y);
98
                    break;
99
                }
100
                it = list_node_next(it);
101 246 up20180655
            }
102
        }
103 222 up20180655
    }
104 324 up20180642
    /** Followers movement */ {
105
        list_node_t *it = list_begin(shooter_list);
106
        while(it != list_end(shooter_list)){
107
            gunner_t *g = *(gunner_t**)list_node_val(it);
108
            if(gunner_get_type(g) & GUNNER_FOLLOW){
109
                double theta = 0.0;
110
                map_where_to_follow(map, gunner_get_x(g), gunner_get_y(g), &theta);
111
                double c = fm_cos(theta), s = fm_sin(theta);
112
                double dx = ZOMBIE_SPEED*c, dy = -ZOMBIE_SPEED*s;
113
                double x = gunner_get_x(g);
114
                double y = gunner_get_y(g);
115
                gunner_set_pos(g, x+dx, y+dy);
116
                if (map_collides_gunner(map, g)){
117
                    gunner_set_pos(g, x, y);
118
                } else {
119
                    list_node_t *it2 = list_begin(shooter_list);
120
                    while (it2 != list_end(shooter_list)) {
121
                        gunner_t *p2 = *(gunner_t**)list_node_val(it2);
122
                        if (g != p2 && gunner_collides_gunner(g, p2)) {
123
                            gunner_set_pos(g, x, y);
124
                            break;
125
                        }
126
                        it2 = list_node_next(it2);
127 309 up20180642
                    }
128
                }
129 324 up20180642
                gunner_set_angle(g, theta-M_PI_2);
130 309 up20180642
            }
131 324 up20180642
            it = list_node_next(it);
132 307 up20180642
        }
133
    }
134 184 up20180655
}
135 240 up20180655
136 345 up20180642
void (get_random_spawn)(const map_t *map, gunner_t *p, list_t *l) {
137
    uint16_t w = map_get_width(map), h = map_get_height(map);
138
    double x, y;
139
140
    while(true){
141
        x = rand() % w;
142
        y = rand() % h;
143
        gunner_set_pos(p, x, y);
144
        if(map_collides_gunner(map, p)) continue;
145
        int collides = false;
146
        list_node_t *it = list_begin(l);
147
        while(it != list_end(l)){
148
            if(gunner_collides_gunner(p, *list_node_val(it))){
149
                collides = true;
150
                break;
151
            }
152
            it = list_node_next(it);
153
        }
154
        if(!collides) return;
155
    }
156
}
157
158
void update_scale(void) {
159
    static uint8_t last_plus = 0, last_minus = 0;
160
    if (key_presses.ctrl_pressed) {
161
        if (key_presses.plus_pressed && !last_plus) {
162
            double scale = ent_get_scale();
163
            scale *= 1.1;
164
            if (scale <= MAX_SCALE) ent_set_scale(scale);
165
        }
166
        else if (key_presses.minus_pressed && !last_minus) {
167
            double scale = ent_get_scale();
168
            scale /= 1.1;
169
            if (scale >= MIN_SCALE) ent_set_scale(scale);
170
        }
171
172
        //printf("SCALE: %d\n", (int)(ent_get_scale()*1000));
173
    }
174
175
    last_plus = key_presses.plus_pressed;
176
    last_minus = key_presses.minus_pressed;
177
}
178
179 237 up20180642
void (shoot_bullet)(const gunner_t *shooter, list_t *bullet_list, const basic_sprite_t *bsp_bullet) {
180 231 up20180655
    double angle = gunner_get_angle(shooter);
181
    double vx = -BULLET_SPEED * fm_sin(angle);
182
    double vy = -BULLET_SPEED * fm_cos(angle);
183 237 up20180642
    bullet_t *bullet = bullet_ctor(shooter, bsp_bullet, gunner_get_x(shooter), gunner_get_y(shooter), vx, vy);
184 231 up20180655
    list_insert(bullet_list, list_end(bullet_list), bullet);
185
}
186
187 236 up20180642
void (update_game_state)(const map_t *map, list_t *shooter_list, list_t *bullet_list) {
188 302 up20180642
    /// BULLETS
189 231 up20180655
    bullet_update_movement_list(bullet_list);
190 236 up20180642
    list_node_t *bullet_it = list_begin(bullet_list);
191
    while (bullet_it != list_end(bullet_list)) {
192
        /// Collision with walls
193
        bullet_t *bullet = *(bullet_t**)list_node_val(bullet_it);
194 231 up20180655
        if (map_collides_bullet(map, bullet)) {
195 236 up20180642
            list_node_t *aux = list_node_next(bullet_it);
196
            /// Delete bullet
197
            bullet_dtor(list_erase(bullet_list, bullet_it));
198
            /// Advance iterator
199
            bullet_it = aux;
200 231 up20180655
            continue;
201
        }
202 236 up20180642
        /// Collision with shooters
203
        list_node_t *shooter_it = list_begin(shooter_list);
204
        int deleted_bullet = false;
205
        while(shooter_it != list_end(shooter_list)){
206
            gunner_t *shooter = *(gunner_t**)list_node_val(shooter_it);
207 302 up20180642
            if(gunner_collides_bullet(shooter, bullet) &&
208
               gunner_get_team(shooter) != gunner_get_team(bullet_get_shooter(bullet))) {
209 236 up20180642
                list_node_t *aux = list_node_next(bullet_it);
210
                /// Change health
211
                gunner_set_curr_health(shooter, gunner_get_curr_health(shooter) - bullet_get_damage(bullet));
212
                if (gunner_get_curr_health(shooter) <= 0) {
213 301 up20180642
                    gunner_dtor(list_erase(shooter_list, shooter_it));
214 236 up20180642
                }
215
                /// Delete bullet
216
                bullet_dtor(list_erase(bullet_list, bullet_it)); deleted_bullet = true;
217
                /// Advance iterator
218
                bullet_it = aux;
219
                break;
220
            } else shooter_it = list_node_next(shooter_it);
221 231 up20180655
        }
222 236 up20180642
        if(deleted_bullet) continue;
223
        /// Advance iterator if necessary
224
        bullet_it = list_node_next(bullet_it);
225 231 up20180655
    }
226 305 up20180642
    /// MELEE
227 302 up20180642
    list_node_t *it1 = list_begin(shooter_list);
228
    while(it1 != list_end(shooter_list)){
229
        gunner_t *s1 = *list_node_val(it1);
230 321 up20180642
        if(!(gunner_get_type(s1) & GUNNER_MELEE)){ it1 = list_node_next(it1); continue; }
231 302 up20180642
        list_node_t *it2 = list_begin(shooter_list);
232
        while(it2 != list_end(shooter_list)){
233
            gunner_t *s2 = *list_node_val(it2);
234 307 up20180642
            if(gunner_get_team(s1) != gunner_get_team(s2) &&
235
               gunner_distance(s1, s2) < MELEE_RANGE)
236 305 up20180642
                gunner_set_curr_health(s2, gunner_get_curr_health(s2) - MELEE_DAMAGE);
237 302 up20180642
            if(gunner_get_curr_health(s2) <= 0){
238
                list_node_t *aux = list_node_next(it2);
239
                gunner_dtor(list_erase(shooter_list, it2));
240
                it2 = aux;
241
            }else it2 = list_node_next(it2);
242
        }
243
        it1 = list_node_next(it1);
244
    }
245 231 up20180655
}
246
247 345 up20180642
text_timer_t* (text_timer_ctor)(const font_t *fnt){
248
    if(fnt == NULL) return NULL;
249
    text_timer_t *ret = malloc(sizeof(timer_t));
250
    if (ret == NULL) return NULL;
251
    ret->time = 0;
252
    ret->text = text_ctor(fnt, "000s");
253
    text_set_color(ret->text, TEXT_COLOR);
254
    return ret;
255
}
256
void (text_timer_update)(text_timer_t *p){
257
    if (p->time >= 999) return;
258
    p->time++;
259
    char buffer[100];
260
    sprintf(buffer, "%03ds", p->time);
261
    text_set_string(p->text, buffer);
262
}
263
void (text_timer_reset)(text_timer_t *p){
264
    text_set_string(p->text, "000s");
265
}
266
void (text_timer_dtor)(text_timer_t *p){
267
    if (p == NULL) return;
268
    text_dtor(p->text);
269
    free(p);
270
}
271 200 up20180655
272 345 up20180642
host_info_t* host_info_ctor(gunner_t *host, gunner_t *remote) {
273
    host_info_t *ret = (host_info_t*)malloc(sizeof(host_info_t));
274
    if (ret == NULL) return ret;
275 200 up20180655
276 345 up20180642
    ret->host_x               = (int16_t)gunner_get_x          (host);
277
    ret->host_y               = (int16_t)gunner_get_y          (host);
278
    ret->host_angle           = (int16_t)gunner_get_angle      (host);
279
    ret->host_health          = (int16_t)gunner_get_health     (host);
280
    ret->host_current_health  = (int16_t)gunner_get_curr_health(host);
281 200 up20180655
282 345 up20180642
    // remote
283
    ret->remote_x               = (int16_t)gunner_get_x          (remote);
284
    ret->remote_y               = (int16_t)gunner_get_y          (remote);
285
    ret->remote_angle           = (int16_t)gunner_get_angle      (remote);
286
    ret->remote_health          = (int16_t)gunner_get_health     (remote);
287
    ret->remote_current_health  = (int16_t)gunner_get_curr_health(remote);
288 341 up20180642
289 345 up20180642
    //ret->no_bullets = 0;
290
291
    return ret;
292 341 up20180642
}
293 345 up20180642
void host_info_dtor(host_info_t *p) {
294
    if (p==NULL) return;
295
    /*
296
    if ((p->bullets_x) != NULL){ free(p->bullets_x); p->bullets_x = NULL; }
297 341 up20180642

298 345 up20180642
    if ((p->bullets_y) != NULL){ free(p->bullets_y); p->bullets_y = NULL; }
299 171 up20180655

300 345 up20180642
    if ((p->bullets_vx) != NULL){ free(p->bullets_vx); p->bullets_vx = NULL; }
301 171 up20180655

302 345 up20180642
    if ((p->bullets_vy) != NULL){ free(p->bullets_vy); p->bullets_vy = NULL; }
303 231 up20180655

304 345 up20180642
    if ((p->bullets_shooter) != NULL){ free(p->bullets_shooter); p->bullets_shooter = NULL; }
305
    */
306
    free(p);
307 171 up20180655
}
308
309 345 up20180642
remote_info_t* remote_info_ctor(void) {
310
    remote_info_t *ret = (remote_info_t*)malloc(sizeof(remote_info_t));
311
    if (ret == NULL) return ret;
312
313
    memset(&(ret->remote_keys_pressed), 0, sizeof(keys_t));
314
315
    ret->remote_angle = 0;
316
317
    return ret;
318 231 up20180655
}
319 345 up20180642
void remote_info_dtor(remote_info_t *p) {
320
    if (p==NULL) return;
321
    free(p);
322
}
323 231 up20180655
324 345 up20180642
bullet_info_t* bullet_info_ctor(void) {
325
    bullet_info_t *ret = (bullet_info_t*)malloc(sizeof(bullet_info_t));
326
    if (ret == NULL) return ret;
327 171 up20180655
328 345 up20180642
    ret->new_bullet = false;
329 171 up20180655
330 345 up20180642
    return ret;
331 167 up20180655
}
332 345 up20180642
void bullet_info_dtor(bullet_info_t *p) {
333
    if (p == NULL) return;
334
    free(p);
335
}
336 250 up20180655
337 342 up20180655
void build_host_structure(host_info_t *p, gunner_t *host, gunner_t *remote) {
338 320 up20180655
    // host
339 329 up20180655
    p->host_x               = (int16_t)gunner_get_x          (host);
340
    p->host_y               = (int16_t)gunner_get_y          (host);
341
    p->host_angle           = (int16_t)gunner_get_angle      (host);
342
    p->host_health          = (int16_t)gunner_get_health     (host);
343
    p->host_current_health  = (int16_t)gunner_get_curr_health(host);
344 320 up20180655
345
    // remote
346 329 up20180655
    p->remote_x               = (int16_t)gunner_get_x          (remote);
347
    p->remote_y               = (int16_t)gunner_get_y          (remote);
348
    p->remote_angle           = (int16_t)gunner_get_angle      (remote);
349
    p->remote_health          = (int16_t)gunner_get_health     (remote);
350
    p->remote_current_health  = (int16_t)gunner_get_curr_health(remote);
351 342 up20180655
    /*
352 320 up20180655
    // bullets
353
    size_t sz = list_size(bullet_list);
354
    p->no_bullets = sz;
355 329 up20180655
    p->bullets_x        = (int16_t*)realloc(p->bullets_x         , sz * sizeof(int16_t));
356
    p->bullets_y        = (int16_t*)realloc(p->bullets_y         , sz * sizeof(int16_t));
357
    p->bullets_vx       = (int16_t*)realloc(p->bullets_vx        , sz * sizeof(int16_t));
358
    p->bullets_vy       = (int16_t*)realloc(p->bullets_vy        , sz * sizeof(int16_t));
359
    p->bullets_shooter  = (bool*)   realloc(p->bullets_shooter   , sz * sizeof(bool ));
360 320 up20180655

361
    list_node_t *it = list_begin(bullet_list);
362
    size_t i = 0;
363
    while (it != list_end(bullet_list)) {
364
        bullet_t *bullet = *list_node_val(it);
365 329 up20180655
        p->bullets_x        [i] = (int16_t)bullet_get_x                      (bullet);
366
        p->bullets_y        [i] = (int16_t)bullet_get_y                      (bullet);
367
        p->bullets_vx       [i] = (int16_t)bullet_get_vx                     (bullet);
368
        p->bullets_vy       [i] = (int16_t)bullet_get_vy                     (bullet);
369 320 up20180655
        p->bullets_shooter  [i] = gunner_get_team(bullet_get_shooter(bullet)) != gunner_get_team(host);
370
        it = list_node_next(it);
371
        i++;
372
    }
373 342 up20180655
    */
374 320 up20180655
}
375
void build_remote_structure(remote_info_t *p, keys_t *keys, double angle) {
376
    memcpy(&(p->remote_keys_pressed), keys, sizeof(keys_t));
377
    p->remote_angle = angle;
378
}