root / proj / src / proj_func.c @ 329
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 | 167 | up20180655 | |
30 | 320 | up20180655 | 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 | 329 | up20180655 | 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 | 320 | up20180655 | |
40 | // remote
|
||
41 | 329 | up20180655 | 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 | 320 | up20180655 | |
47 | ret->no_bullets = 0;
|
||
48 | |||
49 | return ret;
|
||
50 | } |
||
51 | |||
52 | 311 | up20180655 | 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 | 324 | up20180642 | remote_info_t* remote_info_ctor(void) {
|
69 | 320 | up20180655 | 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 | 311 | up20180655 | void remote_info_dtor(remote_info_t *p) {
|
80 | if (p==NULL) return; |
||
81 | 320 | up20180655 | free(p); |
82 | } |
||
83 | 311 | up20180655 | |
84 | 324 | up20180642 | bullet_info_t* bullet_info_ctor(void) {
|
85 | 320 | up20180655 | bullet_info_t *ret = (bullet_info_t*)malloc(sizeof(bullet_info_t));
|
86 | if (ret == NULL) return ret; |
||
87 | 311 | up20180655 | |
88 | 320 | up20180655 | ret->new_bullet = false;
|
89 | 311 | up20180655 | |
90 | 320 | up20180655 | return ret;
|
91 | } |
||
92 | 311 | up20180655 | |
93 | 320 | up20180655 | void bullet_info_dtor(bullet_info_t *p) {
|
94 | if (p == NULL) return; |
||
95 | 311 | up20180655 | free(p); |
96 | } |
||
97 | |||
98 | 184 | up20180655 | void update_key_presses(void) { |
99 | 321 | up20180642 | if (keyboard_get_size() == 1) { |
100 | switch(keyboard_get_scancode()[0]) { |
||
101 | 200 | up20180655 | 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 | 250 | up20180655 | 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 | 167 | up20180655 | } |
116 | } |
||
117 | } |
||
118 | 240 | up20180655 | |
119 | 307 | up20180642 | void update_movement(map_t *map, gunner_t *p, keys_t *keys, list_t *shooter_list) {
|
120 | 324 | up20180642 | /** 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 | 246 | up20180655 | |
126 | 324 | up20180642 | 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 | 246 | up20180655 | } |
138 | } |
||
139 | 324 | up20180642 | 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 | 246 | up20180655 | } |
152 | } |
||
153 | 222 | up20180655 | } |
154 | 324 | up20180642 | /** 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 | 309 | up20180642 | } |
178 | } |
||
179 | 324 | up20180642 | gunner_set_angle(g, theta-M_PI_2); |
180 | 309 | up20180642 | } |
181 | 324 | up20180642 | it = list_node_next(it); |
182 | 307 | up20180642 | } |
183 | } |
||
184 | 184 | up20180655 | } |
185 | 240 | up20180655 | |
186 | 237 | up20180642 | void (shoot_bullet)(const gunner_t *shooter, list_t *bullet_list, const basic_sprite_t *bsp_bullet) { |
187 | 231 | up20180655 | double angle = gunner_get_angle(shooter);
|
188 | double vx = -BULLET_SPEED * fm_sin(angle);
|
||
189 | double vy = -BULLET_SPEED * fm_cos(angle);
|
||
190 | 237 | up20180642 | bullet_t *bullet = bullet_ctor(shooter, bsp_bullet, gunner_get_x(shooter), gunner_get_y(shooter), vx, vy); |
191 | 231 | up20180655 | list_insert(bullet_list, list_end(bullet_list), bullet); |
192 | } |
||
193 | |||
194 | 236 | up20180642 | void (update_game_state)(const map_t *map, list_t *shooter_list, list_t *bullet_list) { |
195 | 302 | up20180642 | /// BULLETS
|
196 | 231 | up20180655 | bullet_update_movement_list(bullet_list); |
197 | 236 | up20180642 | 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 | 231 | up20180655 | if (map_collides_bullet(map, bullet)) {
|
202 | 236 | up20180642 | 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 | 231 | up20180655 | continue;
|
208 | } |
||
209 | 236 | up20180642 | /// 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 | 302 | up20180642 | if(gunner_collides_bullet(shooter, bullet) &&
|
215 | gunner_get_team(shooter) != gunner_get_team(bullet_get_shooter(bullet))) { |
||
216 | 236 | up20180642 | 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 | 301 | up20180642 | gunner_dtor(list_erase(shooter_list, shooter_it)); |
221 | 236 | up20180642 | } |
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 | 231 | up20180655 | } |
229 | 236 | up20180642 | if(deleted_bullet) continue; |
230 | /// Advance iterator if necessary
|
||
231 | bullet_it = list_node_next(bullet_it); |
||
232 | 231 | up20180655 | } |
233 | 305 | up20180642 | /// MELEE
|
234 | 302 | up20180642 | list_node_t *it1 = list_begin(shooter_list); |
235 | while(it1 != list_end(shooter_list)){
|
||
236 | gunner_t *s1 = *list_node_val(it1); |
||
237 | 321 | up20180642 | if(!(gunner_get_type(s1) & GUNNER_MELEE)){ it1 = list_node_next(it1); continue; } |
238 | 302 | up20180642 | list_node_t *it2 = list_begin(shooter_list); |
239 | while(it2 != list_end(shooter_list)){
|
||
240 | gunner_t *s2 = *list_node_val(it2); |
||
241 | 307 | up20180642 | if(gunner_get_team(s1) != gunner_get_team(s2) &&
|
242 | gunner_distance(s1, s2) < MELEE_RANGE) |
||
243 | 305 | up20180642 | gunner_set_curr_health(s2, gunner_get_curr_health(s2) - MELEE_DAMAGE); |
244 | 302 | up20180642 | 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 | 231 | up20180655 | } |
253 | |||
254 | 307 | up20180642 | void (get_random_spawn)(const map_t *map, gunner_t *p, list_t *l) { |
255 | 246 | up20180655 | uint16_t w = map_get_width(map), h = map_get_height(map); |
256 | double x, y;
|
||
257 | |||
258 | 307 | up20180642 | while(true){ |
259 | 246 | up20180655 | x = rand() % w; |
260 | y = rand() % h; |
||
261 | gunner_set_pos(p, x, y); |
||
262 | 307 | up20180642 | 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 | 246 | up20180655 | } |
275 | |||
276 | 200 | up20180655 | 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 | 222 | up20180655 | //printf("SCALE: %d\n", (int)(ent_get_scale()*1000));
|
291 | 200 | up20180655 | } |
292 | |||
293 | last_plus = key_presses.plus_pressed; |
||
294 | last_minus = key_presses.minus_pressed; |
||
295 | } |
||
296 | |||
297 | 321 | up20180642 | static int16_t mouse_x = 0, mouse_y = 0; |
298 | 171 | up20180655 | |
299 | 231 | up20180655 | void (update_mouse)(struct packet *p) { |
300 | 323 | up20180642 | mouse_x = max16(0, mouse_x + p->delta_x);
|
301 | 324 | up20180642 | mouse_x = min16(mouse_x, (int16_t)graph_get_XRes() - 1);
|
302 | 171 | up20180655 | |
303 | 323 | up20180642 | mouse_y = max16(0, mouse_y - p->delta_y);
|
304 | 324 | up20180642 | mouse_y = min16(mouse_y, (int16_t)graph_get_YRes() - 1);
|
305 | 231 | up20180655 | |
306 | key_presses.lb_pressed = p->lb; |
||
307 | 171 | up20180655 | } |
308 | |||
309 | 231 | up20180655 | keys_t* (get_key_presses)(void) {
|
310 | return &key_presses;
|
||
311 | } |
||
312 | |||
313 | 321 | up20180642 | int16_t* get_mouse_X(void) { return &mouse_x; } |
314 | 171 | up20180655 | |
315 | 321 | up20180642 | int16_t* get_mouse_Y(void) { return &mouse_y; } |
316 | 171 | up20180655 | |
317 | 201 | up20180642 | 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 | 167 | up20180655 | } |
320 | 250 | up20180655 | |
321 | 320 | up20180655 | void build_host_structure(host_info_t *p, gunner_t *host, gunner_t *remote, list_t *bullet_list) {
|
322 | // host
|
||
323 | 329 | up20180655 | 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 | 320 | up20180655 | |
329 | // remote
|
||
330 | 329 | up20180655 | 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 | 320 | up20180655 | |
336 | // bullets
|
||
337 | size_t sz = list_size(bullet_list); |
||
338 | p->no_bullets = sz; |
||
339 | 329 | up20180655 | 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 | 320 | up20180655 | |
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 | 329 | up20180655 | 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 | 320 | up20180655 | 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 | 250 | up20180655 | 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 | 251 | up20180655 | text_set_color(ret->text, TEXT_COLOR); |
371 | 250 | up20180655 | return ret;
|
372 | } |
||
373 | |||
374 | void (timer_update)(text_timer_t *p){
|
||
375 | if (p->time >= 999) return; |
||
376 | p->time++; |
||
377 | 319 | up20180642 | char buffer[100]; |
378 | sprintf(buffer, "%03ds", p->time);
|
||
379 | text_set_string(p->text, buffer); |
||
380 | 250 | up20180655 | } |
381 | |||
382 | 251 | up20180655 | void (timer_reset)(text_timer_t *p){
|
383 | 319 | up20180642 | text_set_string(p->text, "000s");
|
384 | 251 | up20180655 | } |
385 | |||
386 | 250 | up20180655 | void (timer_dtor)(text_timer_t *p){
|
387 | if (p == NULL) return; |
||
388 | text_dtor(p->text); |
||
389 | free(p); |
||
390 | } |