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