Project

General

Profile

Statistics
| Revision:

root / proj / src / proj_func.c @ 352

History | View | Annotate | Download (14.4 KB)

1
#include <lcom/lcf.h>
2

    
3
#include "proj_func.h"
4

    
5
#include "interrupts_func.h"
6
#include "libs.h"
7
#include "proj_macros.h"
8
#include "ent.h"
9

    
10
#include <math.h>
11

    
12
int cleanup(void) {
13
    int r = SUCCESS;
14
    if ((r = unsubscribe_all()))
15
        printf("%s: failed to unsubscribe drivers.\n", __func__);
16
    if ((r = graph_cleanup()))
17
        printf("%s: graph cleanup failed\n", __func__);
18

    
19
    return r;
20
}
21

    
22
static keys_t key_presses;
23
void update_key_presses(void) {
24
    if (keyboard_get_size() == 1) {
25
        switch(keyboard_get_scancode()[0]) {
26
        case W_MAKE_CODE        : key_presses.w_pressed     = 1;        break;
27
        case W_BREAK_CODE       : key_presses.w_pressed     = 0;        break;
28
        case A_MAKE_CODE        : key_presses.a_pressed     = 1;        break;
29
        case A_BREAK_CODE       : key_presses.a_pressed     = 0;        break;
30
        case S_MAKE_CODE        : key_presses.s_pressed     = 1;        break;
31
        case S_BREAK_CODE       : key_presses.s_pressed     = 0;        break;
32
        case D_MAKE_CODE        : key_presses.d_pressed     = 1;        break;
33
        case D_BREAK_CODE       : key_presses.d_pressed     = 0;        break;
34
        case CTRL_MAKE_CODE     : key_presses.ctrl_pressed  = 1;        break;
35
        case CTRL_BREAK_CODE    : key_presses.ctrl_pressed  = 0;        break;
36
        case PLUS_MAKE_CODE     : key_presses.plus_pressed  = 1;    update_scale();        break;
37
        case PLUS_BREAK_CODE    : key_presses.plus_pressed  = 0;    update_scale();        break;
38
        case MINUS_MAKE_CODE    : key_presses.minus_pressed = 1;    update_scale();        break;
39
        case MINUS_BREAK_CODE   : key_presses.minus_pressed = 0;    update_scale();        break;
40
        }
41
    }
42
}
43
keys_t* (get_key_presses)(void) {
44
    return &key_presses;
45
}
46

    
47
static int16_t mouse_x = 0, mouse_y = 0;
48
void (update_mouse)(struct packet *p) {
49
    mouse_x = max_16(0, mouse_x + p->delta_x);
50
    mouse_x = min_16(mouse_x, (int16_t)graph_get_XRes() - 1);
51

    
52
    mouse_y = max_16(0, mouse_y - p->delta_y);
53
    mouse_y = min_16(mouse_y, (int16_t)graph_get_YRes() - 1);
54

    
55
    key_presses.lb_pressed = p->lb;
56
}
57
int16_t* get_mouse_X(void) { return &mouse_x; }
58
int16_t* get_mouse_Y(void) { return &mouse_y; }
59
double get_mouse_angle(gunner_t *p) {
60
    return atan2(gunner_get_y_screen(p) - mouse_y, mouse_x - gunner_get_x_screen(p));
61
}
62

    
63
void update_movement(map_t *map, gunner_t *p, keys_t *keys, list_t *shooter_list) {
64
    /** Player movement */{
65
        int ver_mov = keys->s_pressed - keys->w_pressed;
66
        int hor_mov = keys->d_pressed - keys->a_pressed;
67
        double x = gunner_get_x(p);
68
        double y = gunner_get_y(p);
69

    
70
        gunner_set_pos(p, x + SHOOTER_SPEED * hor_mov, y);
71
        if (map_collides_gunner(map, p)) gunner_set_pos(p, x, y);
72
        else {
73
            list_node_t *it = list_begin(shooter_list);
74
            while (it != list_end(shooter_list)) {
75
                gunner_t *p2 = *(gunner_t**)list_node_val(it);
76
                if (p != p2 && gunner_collides_gunner(p, p2)) {
77
                    gunner_set_pos(p, x, y);
78
                    break;
79
                }
80
                it = list_node_next(it);
81
            }
82
        }
83
        x = gunner_get_x(p);
84
        gunner_set_pos(p, x, y + SHOOTER_SPEED * ver_mov);
85
        if (map_collides_gunner(map, p)) gunner_set_pos(p, x, y);
86
        else {
87
            list_node_t *it = list_begin(shooter_list);
88
            while (it != list_end(shooter_list)) {
89
                gunner_t *p2 = *(gunner_t**)list_node_val(it);
90
                if (p != p2 && gunner_collides_gunner(p, p2)) {
91
                    gunner_set_pos(p, x, y);
92
                    break;
93
                }
94
                it = list_node_next(it);
95
            }
96
        }
97
    }
98
    /** Followers movement */ {
99
        list_node_t *it = list_begin(shooter_list);
100
        while(it != list_end(shooter_list)){
101
            gunner_t *g = *(gunner_t**)list_node_val(it);
102
            if(gunner_get_type(g) & GUNNER_FOLLOW){
103
                double theta = 0.0;
104
                map_where_to_follow(map, gunner_get_x(g), gunner_get_y(g), &theta);
105
                double c = fm_cos(theta), s = fm_sin(theta);
106
                double dx = ZOMBIE_SPEED*c, dy = -ZOMBIE_SPEED*s;
107
                double x = gunner_get_x(g);
108
                double y = gunner_get_y(g);
109
                gunner_set_pos(g, x+dx, y+dy);
110
                if (map_collides_gunner(map, g)){
111
                    gunner_set_pos(g, x, y);
112
                } else {
113
                    list_node_t *it2 = list_begin(shooter_list);
114
                    while (it2 != list_end(shooter_list)) {
115
                        gunner_t *p2 = *(gunner_t**)list_node_val(it2);
116
                        if (g != p2 && gunner_collides_gunner(g, p2)) {
117
                            gunner_set_pos(g, x, y);
118
                            break;
119
                        }
120
                        it2 = list_node_next(it2);
121
                    }
122
                }
123
                gunner_set_angle(g, theta-M_PI_2);
124
            }
125
            it = list_node_next(it);
126
        }
127
    }
128
}
129

    
130
void (get_random_spawn)(const map_t *map, gunner_t *p, list_t *l) {
131
    uint16_t w = map_get_width(map), h = map_get_height(map);
132
    double x, y;
133

    
134
    while(true){
135
        x = rand() % w;
136
        y = rand() % h;
137
        gunner_set_pos(p, x, y);
138
        if(map_collides_gunner(map, p)) continue;
139
        int collides = false;
140
        list_node_t *it = list_begin(l);
141
        while(it != list_end(l)){
142
            if(gunner_collides_gunner(p, *list_node_val(it))){
143
                collides = true;
144
                break;
145
            }
146
            it = list_node_next(it);
147
        }
148
        if(!collides) return;
149
    }
150
}
151

    
152
void update_scale(void) {
153
    static uint8_t last_plus = 0, last_minus = 0;
154
    if (key_presses.ctrl_pressed) {
155
        if (key_presses.plus_pressed && !last_plus) {
156
            double scale = ent_get_scale();
157
            scale *= 1.1;
158
            if (scale <= MAX_SCALE) ent_set_scale(scale);
159
        }
160
        else if (key_presses.minus_pressed && !last_minus) {
161
            double scale = ent_get_scale();
162
            scale /= 1.1;
163
            if (scale >= MIN_SCALE) ent_set_scale(scale);
164
        }
165

    
166
        //printf("SCALE: %d\n", (int)(ent_get_scale()*1000));
167
    }
168

    
169
    last_plus = key_presses.plus_pressed;
170
    last_minus = key_presses.minus_pressed;
171
}
172

    
173
void (shoot_bullet)(const gunner_t *shooter, list_t *bullet_list, const basic_sprite_t *bsp_bullet) {
174
    double angle = gunner_get_angle(shooter);
175
    double vx = -BULLET_SPEED * fm_sin(angle);
176
    double vy = -BULLET_SPEED * fm_cos(angle);
177
    bullet_t *bullet = bullet_ctor(shooter, bsp_bullet, gunner_get_x(shooter), gunner_get_y(shooter), vx, vy);
178
    list_insert(bullet_list, list_end(bullet_list), bullet);
179
}
180

    
181
void (update_game_state)(const map_t *map, list_t *shooter_list, list_t *bullet_list) {
182
    /// BULLETS
183
    bullet_update_movement_list(bullet_list);
184
    list_node_t *bullet_it = list_begin(bullet_list);
185
    while (bullet_it != list_end(bullet_list)) {
186
        /// Collision with walls
187
        bullet_t *bullet = *(bullet_t**)list_node_val(bullet_it);
188
        if (map_collides_bullet(map, bullet)) {
189
            list_node_t *aux = list_node_next(bullet_it);
190
            /// Delete bullet
191
            bullet_dtor(list_erase(bullet_list, bullet_it));
192
            /// Advance iterator
193
            bullet_it = aux;
194
            continue;
195
        }
196
        /// Collision with shooters
197
        list_node_t *shooter_it = list_begin(shooter_list);
198
        int deleted_bullet = false;
199
        while(shooter_it != list_end(shooter_list)){
200
            gunner_t *shooter = *(gunner_t**)list_node_val(shooter_it);
201
            if(gunner_collides_bullet(shooter, bullet) &&
202
               gunner_get_team(shooter) != gunner_get_team(bullet_get_shooter(bullet))) {
203
                list_node_t *aux = list_node_next(bullet_it);
204
                /// Change health
205
                gunner_set_curr_health(shooter, gunner_get_curr_health(shooter) - bullet_get_damage(bullet));
206
                if (gunner_get_curr_health(shooter) <= 0) {
207
                    gunner_dtor(list_erase(shooter_list, shooter_it));
208
                }
209
                /// Delete bullet
210
                bullet_dtor(list_erase(bullet_list, bullet_it)); deleted_bullet = true;
211
                /// Advance iterator
212
                bullet_it = aux;
213
                break;
214
            } else shooter_it = list_node_next(shooter_it);
215
        }
216
        if(deleted_bullet) continue;
217
        /// Advance iterator if necessary
218
        bullet_it = list_node_next(bullet_it);
219
    }
220
    /// MELEE
221
    list_node_t *it1 = list_begin(shooter_list);
222
    while(it1 != list_end(shooter_list)){
223
        gunner_t *s1 = *list_node_val(it1);
224
        if(!(gunner_get_type(s1) & GUNNER_MELEE)){ it1 = list_node_next(it1); continue; }
225
        list_node_t *it2 = list_begin(shooter_list);
226
        while(it2 != list_end(shooter_list)){
227
            gunner_t *s2 = *list_node_val(it2);
228
            if(gunner_get_team(s1) != gunner_get_team(s2) &&
229
               gunner_distance(s1, s2) < MELEE_RANGE)
230
                gunner_set_curr_health(s2, gunner_get_curr_health(s2) - MELEE_DAMAGE);
231
            if(gunner_get_curr_health(s2) <= 0){
232
                list_node_t *aux = list_node_next(it2);
233
                gunner_dtor(list_erase(shooter_list, it2));
234
                it2 = aux;
235
            }else it2 = list_node_next(it2);
236
        }
237
        it1 = list_node_next(it1);
238
    }
239
}
240

    
241
text_timer_t* (text_timer_ctor)(const font_t *fnt){
242
    if(fnt == NULL) return NULL;
243
    text_timer_t *ret = malloc(sizeof(timer_t));
244
    if (ret == NULL) return NULL;
245
    ret->time = 0;
246
    ret->text = text_ctor(fnt, "000s");
247
    text_set_color(ret->text, TEXT_COLOR);
248
    return ret;
249
}
250
void (text_timer_update)(text_timer_t *p){
251
    if (p->time >= 999) return;
252
    p->time++;
253
    char buffer[100];
254
    sprintf(buffer, "%03ds", p->time);
255
    text_set_string(p->text, buffer);
256
}
257
void (text_timer_reset)(text_timer_t *p){
258
    text_set_string(p->text, "000s");
259
}
260
void (text_timer_dtor)(text_timer_t *p){
261
    if (p == NULL) return;
262
    text_dtor(p->text);
263
    free(p);
264
}
265

    
266
host_info_t* host_info_ctor(gunner_t *host, gunner_t *remote) {
267
    host_info_t *ret = (host_info_t*)malloc(sizeof(host_info_t));
268
    if (ret == NULL) return ret;
269

    
270
    ret->host_x               = (int16_t)gunner_get_x          (host);
271
    ret->host_y               = (int16_t)gunner_get_y          (host);
272
    ret->host_angle           = (float)  gunner_get_angle      (host);
273
    ret->host_health          = (int16_t)gunner_get_health     (host);
274
    ret->host_current_health  = (int16_t)gunner_get_curr_health(host);
275

    
276
    // remote
277
    ret->remote_x               = (int16_t)gunner_get_x          (remote);
278
    ret->remote_y               = (int16_t)gunner_get_y          (remote);
279
    ret->remote_angle           = (float)  gunner_get_angle      (remote);
280
    ret->remote_health          = (int16_t)gunner_get_health     (remote);
281
    ret->remote_current_health  = (int16_t)gunner_get_curr_health(remote);
282

    
283
    //ret->no_bullets = 0;
284

    
285
    return ret;
286
}
287
void host_info_dtor(host_info_t *p) {
288
    if (p==NULL) return;
289
    /*
290
    if ((p->bullets_x) != NULL){ free(p->bullets_x); p->bullets_x = NULL; }
291

292
    if ((p->bullets_y) != NULL){ free(p->bullets_y); p->bullets_y = NULL; }
293

294
    if ((p->bullets_vx) != NULL){ free(p->bullets_vx); p->bullets_vx = NULL; }
295

296
    if ((p->bullets_vy) != NULL){ free(p->bullets_vy); p->bullets_vy = NULL; }
297

298
    if ((p->bullets_shooter) != NULL){ free(p->bullets_shooter); p->bullets_shooter = NULL; }
299
    */
300
    free(p);
301
}
302

    
303
remote_info_t* remote_info_ctor(void) {
304
    remote_info_t *ret = (remote_info_t*)malloc(sizeof(remote_info_t));
305
    if (ret == NULL) return ret;
306

    
307
    memset(&(ret->remote_keys_pressed), 0, sizeof(keys_t));
308

    
309
    ret->remote_angle = 0;
310

    
311
    return ret;
312
}
313
void remote_info_dtor(remote_info_t *p) {
314
    if (p==NULL) return;
315
    free(p);
316
}
317

    
318
bullet_info_t* bullet_info_ctor(void) {
319
    bullet_info_t *ret = (bullet_info_t*)malloc(sizeof(bullet_info_t));
320
    if (ret == NULL) return ret;
321

    
322
    ret->new_bullet = false;
323

    
324
    return ret;
325
}
326
void bullet_info_dtor(bullet_info_t *p) {
327
    if (p == NULL) return;
328
    free(p);
329
}
330

    
331
void build_host_structure(host_info_t *p, gunner_t *host, gunner_t *remote) {
332
    // host
333
    p->host_x               = (int16_t)gunner_get_x          (host);
334
    p->host_y               = (int16_t)gunner_get_y          (host);
335
    p->host_angle           = (float)  gunner_get_angle      (host);
336
    p->host_health          = (int16_t)gunner_get_health     (host);
337
    p->host_current_health  = (int16_t)gunner_get_curr_health(host);
338

    
339
    // remote
340
    p->remote_x               = (int16_t)gunner_get_x          (remote);
341
    p->remote_y               = (int16_t)gunner_get_y          (remote);
342
    p->remote_angle           = (float)  gunner_get_angle      (remote);
343
    p->remote_health          = (int16_t)gunner_get_health     (remote);
344
    p->remote_current_health  = (int16_t)gunner_get_curr_health(remote);
345
    /*
346
    // bullets
347
    size_t sz = list_size(bullet_list);
348
    p->no_bullets = sz;
349
    p->bullets_x        = (int16_t*)realloc(p->bullets_x         , sz * sizeof(int16_t));
350
    p->bullets_y        = (int16_t*)realloc(p->bullets_y         , sz * sizeof(int16_t));
351
    p->bullets_vx       = (int16_t*)realloc(p->bullets_vx        , sz * sizeof(int16_t));
352
    p->bullets_vy       = (int16_t*)realloc(p->bullets_vy        , sz * sizeof(int16_t));
353
    p->bullets_shooter  = (bool*)   realloc(p->bullets_shooter   , sz * sizeof(bool ));
354

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