root / proj / src / proj.c @ 313
History | View | Annotate | Download (29.5 KB)
1 |
#include <lcom/lcf.h> |
---|---|
2 |
#include <lcom/proj.h> |
3 |
#include <lcom/liblm.h> |
4 |
#include <math.h> |
5 |
|
6 |
#include "proj_macros.h" |
7 |
#include "proj_func.h" |
8 |
|
9 |
#include "kbc.h" |
10 |
#include "timer.h" |
11 |
#include "keyboard.h" |
12 |
#include "mouse.h" |
13 |
#include "graph.h" |
14 |
#include "menu.h" |
15 |
#include "rtc.h" |
16 |
#include "hltp.h" |
17 |
#include "interrupts_func.h" |
18 |
#include "makecode_map.h" |
19 |
|
20 |
#include "graph.h" |
21 |
#include "rectangle.h" |
22 |
#include "font.h" |
23 |
#include "ent.h" |
24 |
|
25 |
#include "crosshair.h" |
26 |
#include "shooter.h" |
27 |
#include "zombie.h" |
28 |
#include "pistol.h" |
29 |
#include "nothing.h" |
30 |
#include "bullet.h" |
31 |
#include "map1.h" |
32 |
|
33 |
#include "errors.h" |
34 |
|
35 |
#include "list.h" |
36 |
|
37 |
int main(int argc, char* argv[]) { |
38 |
|
39 |
lcf_set_language("EN-US");
|
40 |
|
41 |
//lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
|
42 |
|
43 |
//lcf_log_output("/home/lcom/labs/proj/output.txt");
|
44 |
|
45 |
if (lcf_start(argc, argv)) return 1; |
46 |
|
47 |
lcf_cleanup(); |
48 |
|
49 |
return 0; |
50 |
} |
51 |
|
52 |
basic_sprite_t *bsp_crosshair = NULL;
|
53 |
basic_sprite_t *bsp_shooter = NULL;
|
54 |
basic_sprite_t *bsp_zombie = NULL;
|
55 |
basic_sprite_t *bsp_pistol = NULL;
|
56 |
basic_sprite_t *bsp_nothing = NULL;
|
57 |
basic_sprite_t *bsp_bullet = NULL;
|
58 |
map_t *map1 = NULL;
|
59 |
sprite_t *sp_crosshair = NULL;
|
60 |
|
61 |
static int (singleplayer)(void); |
62 |
static int (multiplayer)(void); |
63 |
static int (chat)(void); |
64 |
int(proj_main_loop)(int argc, char *argv[]) { |
65 |
|
66 |
int r;
|
67 |
|
68 |
if(font_init()){ printf("Failed to initialize fonts\n"); return 1; } |
69 |
|
70 |
/// subscribe interrupts
|
71 |
if (subscribe_all()) { return 1; } |
72 |
|
73 |
/// initialize graphics
|
74 |
if(graph_init(GRAPH_MODE)){
|
75 |
printf("%s: failed to initalize graphics.\n", __func__);
|
76 |
if (cleanup()) printf("%s: failed to cleanup.\n", __func__); |
77 |
return 1; |
78 |
} |
79 |
|
80 |
/// Load stuff
|
81 |
{ |
82 |
graph_clear_screen(); |
83 |
text_t *txt = text_ctor(default_font, "Loading...");
|
84 |
text_set_pos(txt, graph_get_XRes()/2, graph_get_YRes()/2); |
85 |
text_set_valign(txt, text_valign_center); |
86 |
text_set_halign(txt, text_halign_center); |
87 |
text_set_color(txt, TEXT_COLOR); |
88 |
text_draw(txt); |
89 |
text_dtor(txt); |
90 |
graph_draw(); |
91 |
|
92 |
bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n"); |
93 |
bsp_shooter = get_shooter (); if(bsp_shooter == NULL) printf("Failed to get shooter\n"); |
94 |
bsp_zombie = get_zombie (); if(bsp_zombie == NULL) printf("Failed to get zombie\n"); |
95 |
bsp_pistol = get_pistol (); if(bsp_pistol == NULL) printf("Failed to get pistol\n"); |
96 |
bsp_nothing = get_nothing (); if(bsp_nothing == NULL) printf("Failed to get nothing\n"); |
97 |
bsp_bullet = get_bullet (); if(bsp_bullet == NULL) printf("Failed to get bullet\n"); |
98 |
map1 = get_map1 (); if(map1 == NULL) printf("Failed to get map1\n"); |
99 |
|
100 |
sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n"); |
101 |
} |
102 |
|
103 |
menu_t *main_menu = menu_ctor(default_font); |
104 |
menu_add_item(main_menu, "Single player");
|
105 |
menu_add_item(main_menu, "Multiplayer");
|
106 |
menu_add_item(main_menu, "Chat");
|
107 |
menu_add_item(main_menu, "Exit");
|
108 |
|
109 |
//uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
110 |
uint8_t last_lb = 0;
|
111 |
struct packet pp;
|
112 |
keys_t *keys = get_key_presses(); |
113 |
|
114 |
/// loop stuff
|
115 |
int click = 0; |
116 |
uint32_t int_vector = 0;
|
117 |
int good = true; |
118 |
while (good) {
|
119 |
/* Get a request message. */
|
120 |
if((r = get_interrupts_vector(&int_vector))) return r; |
121 |
for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) { |
122 |
if (int_vector & n) {
|
123 |
interrupt_handler(i); |
124 |
switch (i) {
|
125 |
case TIMER0_IRQ:
|
126 |
|
127 |
graph_clear_screen(); |
128 |
switch(menu_update_state(main_menu, click)){
|
129 |
case -1: break; |
130 |
case 0: singleplayer(); break; //campaign(); break; |
131 |
case 1: multiplayer() ; break; |
132 |
case 2: chat(); break; |
133 |
case 3: good = false; break; |
134 |
} |
135 |
menu_draw(main_menu); |
136 |
|
137 |
click = 0;
|
138 |
|
139 |
sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
140 |
sprite_draw(sp_crosshair); |
141 |
graph_draw(); |
142 |
|
143 |
break;
|
144 |
case KBC_IRQ:
|
145 |
if ((scancode[0]) == ESC_BREAK_CODE) good = false; |
146 |
case MOUSE_IRQ:
|
147 |
if (counter_mouse_ih >= 3) { |
148 |
mouse_parse_packet(packet_mouse_ih, &pp); |
149 |
update_mouse(&pp); |
150 |
if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
|
151 |
last_lb = keys->lb_pressed; |
152 |
counter_mouse_ih = 0;
|
153 |
} |
154 |
break;
|
155 |
case COM1_IRQ: nctp_ih(); break; |
156 |
} |
157 |
} |
158 |
} |
159 |
} |
160 |
|
161 |
basic_sprite_dtor (bsp_crosshair); bsp_crosshair = NULL;
|
162 |
basic_sprite_dtor (bsp_shooter ); bsp_shooter = NULL;
|
163 |
basic_sprite_dtor (bsp_zombie ); bsp_zombie = NULL;
|
164 |
sprite_dtor (sp_crosshair ); sp_crosshair = NULL;
|
165 |
basic_sprite_dtor (bsp_pistol ); bsp_pistol = NULL;
|
166 |
basic_sprite_dtor (bsp_nothing ); bsp_nothing = NULL;
|
167 |
map_dtor (map1 ); map1 = NULL;
|
168 |
if(font_free()){ printf("Failed to free fonts\n"); return 1; } |
169 |
|
170 |
// Unsubscribe interrupts
|
171 |
if (unsubscribe_all()) {
|
172 |
if (cleanup())
|
173 |
printf("%s: failed to cleanup.\n", __func__);
|
174 |
return 1; |
175 |
} |
176 |
|
177 |
if (cleanup()) {
|
178 |
printf("%s: failed to cleanup.\n", __func__);
|
179 |
return 1; |
180 |
} |
181 |
|
182 |
return 0; |
183 |
} |
184 |
|
185 |
host_info_t *host = NULL;
|
186 |
remote_info_t *remote = NULL;
|
187 |
|
188 |
static void multiplayer_process(const uint8_t *p, const size_t sz) { |
189 |
void *dest = NULL; |
190 |
hltp_type tp = hltp_interpret(p, sz, &dest); |
191 |
switch(tp){
|
192 |
case hltp_type_host:
|
193 |
host_info_dtor(host); |
194 |
host = (host_info_t*)dest; |
195 |
break;
|
196 |
case hltp_type_remote:
|
197 |
remote_info_dtor(remote); |
198 |
remote = (remote_info_t*)dest; |
199 |
break;
|
200 |
default: break; |
201 |
} |
202 |
} |
203 |
static int (multiplayer_host)(void); |
204 |
static int (multiplayer_remote)(void); |
205 |
static int (multiplayer)(void) { |
206 |
int r;
|
207 |
|
208 |
menu_t *main_menu = menu_ctor(default_font); |
209 |
menu_add_item(main_menu, "Create");
|
210 |
menu_add_item(main_menu, "Connect");
|
211 |
menu_add_item(main_menu, "Back");
|
212 |
|
213 |
//uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
214 |
uint8_t last_lb = 0;
|
215 |
struct packet pp;
|
216 |
keys_t *keys = get_key_presses(); |
217 |
|
218 |
/// loop stuff
|
219 |
int click = 0; |
220 |
uint32_t int_vector = 0;
|
221 |
int good = true; |
222 |
while (good) {
|
223 |
/* Get a request message. */
|
224 |
if((r = get_interrupts_vector(&int_vector))) return r; |
225 |
for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) { |
226 |
if (int_vector & n) {
|
227 |
interrupt_handler(i); |
228 |
switch (i) {
|
229 |
case TIMER0_IRQ:
|
230 |
|
231 |
graph_clear_screen(); |
232 |
switch(menu_update_state(main_menu, click)){
|
233 |
case -1: break; |
234 |
case 0: multiplayer_host(); break; |
235 |
case 1: multiplayer_remote(); break; |
236 |
case 2: good = false; break; |
237 |
} |
238 |
menu_draw(main_menu); |
239 |
|
240 |
click = 0;
|
241 |
|
242 |
sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
243 |
sprite_draw(sp_crosshair); |
244 |
graph_draw(); |
245 |
|
246 |
break;
|
247 |
case KBC_IRQ:
|
248 |
if ((scancode[0]) == ESC_BREAK_CODE) good = false; |
249 |
case MOUSE_IRQ:
|
250 |
if (counter_mouse_ih >= 3) { |
251 |
mouse_parse_packet(packet_mouse_ih, &pp); |
252 |
update_mouse(&pp); |
253 |
if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
|
254 |
last_lb = keys->lb_pressed; |
255 |
counter_mouse_ih = 0;
|
256 |
} |
257 |
break;
|
258 |
case COM1_IRQ: nctp_ih(); break; |
259 |
} |
260 |
} |
261 |
} |
262 |
} |
263 |
return 0; |
264 |
} |
265 |
|
266 |
static int (multiplayer_host)(void) { |
267 |
//int r;
|
268 |
|
269 |
nctp_dump(); |
270 |
nctp_set_processor(multiplayer_process);/*
|
271 |
|
272 |
ent_set_scale(DEFAULT_SCALE);
|
273 |
text_timer_t *in_game_timer = timer_ctor(default_font);
|
274 |
|
275 |
list_t *shooter_list = list_ctor();
|
276 |
|
277 |
gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
|
278 |
gunner_set_spawn(shooter1, 75, 75);
|
279 |
|
280 |
gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter2 == NULL) printf("Failed to get shooter2\n");
|
281 |
gunner_set_spawn(shooter2, 975, 75);
|
282 |
|
283 |
list_insert(shooter_list, list_end(shooter_list), shooter1);
|
284 |
list_insert(shooter_list, list_end(shooter_list), shooter2);
|
285 |
|
286 |
do {
|
287 |
get_random_spawn(map1, shooter1, shooter_list);
|
288 |
get_random_spawn(map1, shooter2, shooter_list);
|
289 |
} while (distance_gunners(shooter1, shooter2) < 700);
|
290 |
|
291 |
list_t *bullet_list = list_ctor();
|
292 |
|
293 |
ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
|
294 |
gunner_get_y(shooter1)-ent_get_YLength()/2.0);
|
295 |
|
296 |
//uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
297 |
uint8_t last_lb = 0;
|
298 |
struct packet pp;
|
299 |
keys_t *keys = get_key_presses();
|
300 |
|
301 |
/// loop stuff
|
302 |
uint32_t int_vector = 0;
|
303 |
int good = true;
|
304 |
int state = 0; // -1 for remote win, 0 for draw, 1 for host win
|
305 |
while (good) {
|
306 |
if ((r = get_interrupts_vector(&int_vector))) return r;
|
307 |
for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
|
308 |
interrupt_handler(i);
|
309 |
switch (i) {
|
310 |
case TIMER0_IRQ:
|
311 |
|
312 |
break;
|
313 |
|
314 |
case KBC_IRQ:
|
315 |
|
316 |
break;
|
317 |
|
318 |
case MOUSE_IRQ:
|
319 |
|
320 |
|
321 |
break;
|
322 |
|
323 |
case COM1_IRQ: nctp_ih(); break;
|
324 |
}
|
325 |
}
|
326 |
}*/
|
327 |
|
328 |
return 0; |
329 |
} |
330 |
static int (multiplayer_remote)(void) {/* |
331 |
int r;
|
332 |
|
333 |
nctp_dump();
|
334 |
nctp_set_processor(multiplayer_process);
|
335 |
|
336 |
ent_set_scale(DEFAULT_SCALE);
|
337 |
text_timer_t *in_game_timer = timer_ctor(default_font);
|
338 |
|
339 |
list_t *shooter_list = list_ctor();
|
340 |
|
341 |
gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
|
342 |
gunner_set_spawn(shooter1, 75, 75);
|
343 |
|
344 |
gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter2 == NULL) printf("Failed to get shooter2\n");
|
345 |
gunner_set_spawn(shooter2, 975, 75);
|
346 |
|
347 |
list_insert(shooter_list, list_end(shooter_list), shooter1);
|
348 |
list_insert(shooter_list, list_end(shooter_list), shooter2);
|
349 |
|
350 |
do {
|
351 |
get_random_spawn(map1, shooter1, shooter_list);
|
352 |
get_random_spawn(map1, shooter2, shooter_list);
|
353 |
} while (distance_gunners(shooter1, shooter2) < 700);
|
354 |
|
355 |
list_t *bullet_list = list_ctor();
|
356 |
|
357 |
ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
|
358 |
gunner_get_y(shooter1)-ent_get_YLength()/2.0);
|
359 |
|
360 |
//uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
361 |
uint8_t last_lb = 0;
|
362 |
struct packet pp;
|
363 |
keys_t *keys = get_key_presses();
|
364 |
|
365 |
/// loop stuff
|
366 |
uint32_t int_vector = 0;
|
367 |
int good = true;
|
368 |
int state = 0; // -1 for remote win, 0 for draw, 1 for host win
|
369 |
while (good) {
|
370 |
if ((r = get_interrupts_vector(&int_vector))) return r;
|
371 |
for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
|
372 |
interrupt_handler(i);
|
373 |
switch (i) {
|
374 |
case TIMER0_IRQ:
|
375 |
|
376 |
break;
|
377 |
|
378 |
case KBC_IRQ:
|
379 |
|
380 |
break;
|
381 |
|
382 |
case MOUSE_IRQ:
|
383 |
|
384 |
|
385 |
break;
|
386 |
|
387 |
case COM1_IRQ: nctp_ih(); break;
|
388 |
}
|
389 |
}
|
390 |
}*/
|
391 |
|
392 |
return 0; |
393 |
} |
394 |
|
395 |
static int (campaign)(void); |
396 |
static int (zombies)(void); |
397 |
static int (singleplayer)(void) { |
398 |
|
399 |
int r;
|
400 |
|
401 |
menu_t *main_menu = menu_ctor(default_font); |
402 |
menu_add_item(main_menu, "Campaign");
|
403 |
menu_add_item(main_menu, "Zombies");
|
404 |
menu_add_item(main_menu, "Back");
|
405 |
|
406 |
//uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
407 |
uint8_t last_lb = 0;
|
408 |
struct packet pp;
|
409 |
keys_t *keys = get_key_presses(); |
410 |
|
411 |
/// loop stuff
|
412 |
int click = 0; |
413 |
uint32_t int_vector = 0;
|
414 |
int good = true; |
415 |
while (good) {
|
416 |
/* Get a request message. */
|
417 |
if((r = get_interrupts_vector(&int_vector))) return r; |
418 |
for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) { |
419 |
if (int_vector & n) {
|
420 |
interrupt_handler(i); |
421 |
switch (i) {
|
422 |
case TIMER0_IRQ:
|
423 |
|
424 |
graph_clear_screen(); |
425 |
switch(menu_update_state(main_menu, click)){
|
426 |
case -1: break; |
427 |
case 0: campaign(); break; |
428 |
case 1: zombies(); break; |
429 |
case 2: good = false; break; |
430 |
} |
431 |
menu_draw(main_menu); |
432 |
|
433 |
click = 0;
|
434 |
|
435 |
sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
436 |
sprite_draw(sp_crosshair); |
437 |
graph_draw(); |
438 |
|
439 |
break;
|
440 |
case KBC_IRQ:
|
441 |
if ((scancode[0]) == ESC_BREAK_CODE) good = false; |
442 |
case MOUSE_IRQ:
|
443 |
if (counter_mouse_ih >= 3) { |
444 |
mouse_parse_packet(packet_mouse_ih, &pp); |
445 |
update_mouse(&pp); |
446 |
if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
|
447 |
last_lb = keys->lb_pressed; |
448 |
counter_mouse_ih = 0;
|
449 |
} |
450 |
break;
|
451 |
case COM1_IRQ: nctp_ih(); break; |
452 |
} |
453 |
} |
454 |
} |
455 |
} |
456 |
|
457 |
return 0; |
458 |
} |
459 |
|
460 |
static int (campaign)(void){ |
461 |
|
462 |
int r;
|
463 |
|
464 |
ent_set_scale(DEFAULT_SCALE); |
465 |
text_timer_t *in_game_timer = timer_ctor(default_font); |
466 |
|
467 |
list_t *shooter_list = list_ctor(); |
468 |
|
469 |
gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n"); |
470 |
gunner_set_spawn(shooter1, 75, 75); |
471 |
gunner_set_pos(shooter1, 75, 75); |
472 |
|
473 |
gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing, gunner_player, 2);
|
474 |
gunner_set_spawn(shooter2, 975, 75); |
475 |
gunner_set_pos(shooter2, 775, 75); |
476 |
|
477 |
list_insert(shooter_list, list_end(shooter_list), shooter1); |
478 |
list_insert(shooter_list, list_end(shooter_list), shooter2); |
479 |
|
480 |
list_t *bullet_list = list_ctor(); |
481 |
|
482 |
ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
483 |
gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
484 |
|
485 |
//uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
486 |
uint8_t last_lb = 0;
|
487 |
struct packet pp;
|
488 |
keys_t *keys = get_key_presses(); |
489 |
|
490 |
/// loop stuff
|
491 |
uint32_t int_vector = 0;
|
492 |
int good = true; |
493 |
while (good) {
|
494 |
/* Get a request message. */
|
495 |
if((r = get_interrupts_vector(&int_vector))) return r; |
496 |
for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) { |
497 |
if(!good) break; |
498 |
if (int_vector & n) {
|
499 |
interrupt_handler(i); |
500 |
switch (i) {
|
501 |
case TIMER0_IRQ:
|
502 |
|
503 |
if (no_interrupts % 60 == 0) timer_update(in_game_timer); |
504 |
update_movement(map1, shooter1, keys, shooter_list); |
505 |
|
506 |
update_game_state(map1, shooter_list, bullet_list); |
507 |
|
508 |
//update_scale();
|
509 |
double angle = get_mouse_angle(shooter1);
|
510 |
gunner_set_angle(shooter1, angle - M_PI_2); |
511 |
|
512 |
ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
513 |
gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
514 |
|
515 |
graph_clear_screen(); |
516 |
map_draw (map1); |
517 |
bullet_draw_list(bullet_list); |
518 |
gunner_draw_list(shooter_list); |
519 |
|
520 |
text_draw(in_game_timer->text); |
521 |
|
522 |
sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
523 |
sprite_draw(sp_crosshair); |
524 |
graph_draw(); |
525 |
|
526 |
break;
|
527 |
case KBC_IRQ:
|
528 |
if ((scancode[0]) == ESC_BREAK_CODE) { |
529 |
good = false;
|
530 |
} |
531 |
break;
|
532 |
case MOUSE_IRQ:
|
533 |
if (counter_mouse_ih >= 3) { |
534 |
mouse_parse_packet(packet_mouse_ih, &pp); |
535 |
update_mouse(&pp); |
536 |
if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
|
537 |
shoot_bullet(shooter1, bullet_list, bsp_bullet); |
538 |
last_lb = keys->lb_pressed; |
539 |
counter_mouse_ih = 0;
|
540 |
|
541 |
} |
542 |
break;
|
543 |
case COM1_IRQ: nctp_ih(); break; |
544 |
} |
545 |
} |
546 |
} |
547 |
} |
548 |
|
549 |
while(list_size(shooter_list) > 0){ |
550 |
gunner_t *p = list_erase(shooter_list, list_begin(shooter_list)); |
551 |
gunner_dtor(p); |
552 |
} |
553 |
if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n"); |
554 |
|
555 |
while(list_size(bullet_list) > 0){ |
556 |
bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list)); |
557 |
bullet_dtor(p); |
558 |
} |
559 |
if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n"); |
560 |
|
561 |
timer_dtor(in_game_timer); in_game_timer = NULL;
|
562 |
|
563 |
return SUCCESS;
|
564 |
} |
565 |
|
566 |
#define ZOMBIES_NUM 5 |
567 |
#define ZOMBIE_HEALTH_FACTOR 1.1 |
568 |
static int (zombies)(void){ |
569 |
|
570 |
int r;
|
571 |
|
572 |
ent_set_scale(DEFAULT_SCALE); |
573 |
text_timer_t *in_game_timer = timer_ctor(default_font); |
574 |
|
575 |
list_t *shooter_list = list_ctor(); |
576 |
|
577 |
gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n"); |
578 |
gunner_set_spawn(shooter1, 980, 790); |
579 |
gunner_set_pos(shooter1, 980, 790); |
580 |
|
581 |
list_insert(shooter_list, list_end(shooter_list), shooter1); |
582 |
|
583 |
list_t *bullet_list = list_ctor(); |
584 |
|
585 |
ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
586 |
gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
587 |
|
588 |
//uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
589 |
uint8_t last_lb = 0;
|
590 |
struct packet pp;
|
591 |
keys_t *keys = get_key_presses(); |
592 |
|
593 |
/// loop stuff
|
594 |
uint32_t int_vector = 0;
|
595 |
int good = true; |
596 |
int dead = false; |
597 |
|
598 |
int health = 50; |
599 |
|
600 |
/** #DEV */ /* { |
601 |
gunner_t *zombie = gunner_ctor(bsp_zombie, bsp_nothing, gunner_melee | gunner_follow, 3);
|
602 |
gunner_set_health(zombie, health);
|
603 |
gunner_set_curr_health(zombie, health);
|
604 |
health *= ZOMBIE_HEALTH_FACTOR;
|
605 |
gunner_set_pos(zombie, 1100, 75);
|
606 |
list_push_back(shooter_list, zombie);
|
607 |
}*/ //\#DEV |
608 |
|
609 |
map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1)); |
610 |
|
611 |
while (good && !dead) {
|
612 |
/* Get a request message. */
|
613 |
if((r = get_interrupts_vector(&int_vector))) return r; |
614 |
for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) { |
615 |
if(!good || dead) break; |
616 |
if (int_vector & n) {
|
617 |
interrupt_handler(i); |
618 |
switch (i) {
|
619 |
case TIMER0_IRQ:
|
620 |
if (no_interrupts % 60 == 0) timer_update(in_game_timer); |
621 |
if (no_interrupts % 6 == 0){ |
622 |
map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1)); |
623 |
} |
624 |
|
625 |
update_movement(map1, shooter1, keys, shooter_list); |
626 |
|
627 |
update_game_state(map1, shooter_list, bullet_list); |
628 |
|
629 |
if(list_find(shooter_list, shooter1) == list_end(shooter_list)){
|
630 |
good = false;
|
631 |
dead = true;
|
632 |
break;
|
633 |
} |
634 |
|
635 |
double angle = get_mouse_angle(shooter1);
|
636 |
gunner_set_angle(shooter1, angle - M_PI_2); |
637 |
|
638 |
ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
639 |
gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
640 |
|
641 |
while(list_size(shooter_list) < ZOMBIES_NUM+1){ |
642 |
gunner_t *zombie = gunner_ctor(bsp_zombie, bsp_nothing, gunner_melee | gunner_follow, 3);
|
643 |
gunner_set_health(zombie, health); |
644 |
gunner_set_curr_health(zombie, health); |
645 |
health *= ZOMBIE_HEALTH_FACTOR; |
646 |
get_random_spawn(map1, zombie, shooter_list); |
647 |
list_push_back(shooter_list, zombie); |
648 |
} |
649 |
|
650 |
graph_clear_screen(); |
651 |
map_draw (map1); |
652 |
bullet_draw_list(bullet_list); |
653 |
gunner_draw_list(shooter_list); |
654 |
|
655 |
text_draw(in_game_timer->text); |
656 |
|
657 |
sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
658 |
sprite_draw(sp_crosshair); |
659 |
graph_draw(); |
660 |
|
661 |
break;
|
662 |
case KBC_IRQ:
|
663 |
if ((scancode[0]) == ESC_BREAK_CODE) { |
664 |
good = false;
|
665 |
} |
666 |
break;
|
667 |
case MOUSE_IRQ:
|
668 |
if (counter_mouse_ih >= 3) { |
669 |
mouse_parse_packet(packet_mouse_ih, &pp); |
670 |
update_mouse(&pp); |
671 |
if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
|
672 |
shoot_bullet(shooter1, bullet_list, bsp_bullet); |
673 |
last_lb = keys->lb_pressed; |
674 |
counter_mouse_ih = 0;
|
675 |
|
676 |
} |
677 |
break;
|
678 |
case COM1_IRQ: nctp_ih(); break; |
679 |
} |
680 |
} |
681 |
} |
682 |
} |
683 |
|
684 |
while(list_size(shooter_list) > 0){ |
685 |
gunner_t *p = list_erase(shooter_list, list_begin(shooter_list)); |
686 |
gunner_dtor(p); |
687 |
} |
688 |
if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n"); |
689 |
|
690 |
while(list_size(bullet_list) > 0){ |
691 |
bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list)); |
692 |
bullet_dtor(p); |
693 |
} |
694 |
if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n"); |
695 |
|
696 |
if(dead){
|
697 |
printf("YOU DIED\n");
|
698 |
} |
699 |
|
700 |
timer_dtor(in_game_timer); in_game_timer = NULL;
|
701 |
|
702 |
return SUCCESS;
|
703 |
} |
704 |
|
705 |
#define CHAT_MAX_SIZE 75 |
706 |
#define CHAT_MAX_NUM 19 |
707 |
text_t *t_text[CHAT_MAX_NUM] = {NULL};
|
708 |
rectangle_t *r_text = NULL;
|
709 |
static void chat_process(const uint8_t *p, const size_t sz){ |
710 |
char buffer2[CHAT_MAX_NUM+3]; |
711 |
void *dest = NULL; |
712 |
hltp_type tp = hltp_interpret(p, sz, &dest); |
713 |
switch(tp){
|
714 |
case hltp_type_string:
|
715 |
strcpy(buffer2, dest); |
716 |
strncat(buffer2, " <", 2); |
717 |
for(size_t i = CHAT_MAX_NUM-1; i; --i) |
718 |
text_set_text(t_text[i], text_get_string(t_text[i-1]));
|
719 |
text_set_text(t_text[0], buffer2);
|
720 |
for(size_t i = 0; i < CHAT_MAX_NUM; ++i){ |
721 |
if(text_get_string(t_text[i])[0] == '>'){ |
722 |
text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
|
723 |
text_set_halign(t_text[i], text_halign_left); |
724 |
}else{
|
725 |
text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
|
726 |
text_set_halign(t_text[i], text_halign_right); |
727 |
} |
728 |
} |
729 |
break;
|
730 |
default: break; |
731 |
} |
732 |
} |
733 |
static int (chat)(void){ |
734 |
int r;
|
735 |
|
736 |
nctp_dump(); |
737 |
nctp_set_processor(chat_process); |
738 |
|
739 |
struct packet pp;
|
740 |
|
741 |
char buffer[CHAT_MAX_SIZE] = ""; |
742 |
rectangle_t *r_buffer = NULL; {
|
743 |
r_buffer = rectangle_ctor(0,0,900,70); |
744 |
rectangle_set_pos(r_buffer, graph_get_XRes()/2 -rectangle_get_w(r_buffer)/2, |
745 |
graph_get_YRes()*0.87-rectangle_get_h(r_buffer)/2); |
746 |
rectangle_set_fill_color (r_buffer, GRAPH_BLACK); |
747 |
rectangle_set_outline_width(r_buffer, 2);
|
748 |
rectangle_set_outline_color(r_buffer, GRAPH_WHITE); |
749 |
rectangle_set_fill_trans(r_buffer, GRAPH_TRANSPARENT); |
750 |
} |
751 |
text_t *t_buffer = NULL; {
|
752 |
t_buffer = text_ctor(default_font, "");
|
753 |
text_set_pos(t_buffer, rectangle_get_x(r_buffer)+50,
|
754 |
rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)/2);
|
755 |
text_set_halign(t_buffer, text_halign_left); |
756 |
text_set_valign(t_buffer, text_valign_center); |
757 |
text_set_color (t_buffer, TEXT_COLOR); |
758 |
} |
759 |
text_t *t_size = NULL; {
|
760 |
t_size = text_ctor(default_font, "");
|
761 |
text_set_pos(t_size, rectangle_get_x(r_buffer)+rectangle_get_w(r_buffer)-5,
|
762 |
rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)-5);
|
763 |
text_set_halign(t_size, text_halign_right); |
764 |
text_set_valign(t_size, text_valign_bottom); |
765 |
text_set_color (t_size, TEXT_COLOR); |
766 |
text_set_size (t_size, 18);
|
767 |
char buffer2[20]; |
768 |
sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
|
769 |
text_set_text(t_size, buffer2); |
770 |
} |
771 |
|
772 |
/** r_text */ {
|
773 |
r_text = rectangle_ctor(0,0,900,550); |
774 |
rectangle_set_pos(r_text, graph_get_XRes()/2 -rectangle_get_w(r_buffer)/2, |
775 |
graph_get_YRes()*0.09); |
776 |
rectangle_set_fill_color (r_text, GRAPH_BLACK); |
777 |
rectangle_set_outline_width(r_text, 2);
|
778 |
rectangle_set_outline_color(r_text, GRAPH_WHITE); |
779 |
rectangle_set_fill_trans(r_text, GRAPH_TRANSPARENT); |
780 |
} |
781 |
/** t_text */ {
|
782 |
for(size_t i = 0; i < CHAT_MAX_NUM; ++i){ |
783 |
t_text[i] = text_ctor(default_font, " ");
|
784 |
text_set_pos(t_text[i], rectangle_get_x(r_text)+50,
|
785 |
rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-25*i); |
786 |
text_set_halign(t_text[i], text_halign_left); |
787 |
text_set_valign(t_text[i], text_valign_bottom); |
788 |
text_set_color (t_text[i], TEXT_COLOR); |
789 |
} |
790 |
} |
791 |
|
792 |
/// loop stuff
|
793 |
uint32_t int_vector = 0;
|
794 |
int good = true; |
795 |
while (good) {
|
796 |
/* Get a request message. */
|
797 |
if((r = get_interrupts_vector(&int_vector))) return r; |
798 |
for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) { |
799 |
if (int_vector & n) {
|
800 |
interrupt_handler(i); |
801 |
switch (i) {
|
802 |
case TIMER0_IRQ:
|
803 |
graph_clear_screen(); |
804 |
sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
805 |
|
806 |
rectangle_draw(r_buffer); |
807 |
text_draw(t_buffer); |
808 |
text_draw(t_size); |
809 |
|
810 |
rectangle_draw(r_text); |
811 |
for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_draw(t_text[i]); |
812 |
|
813 |
sprite_draw(sp_crosshair); |
814 |
graph_draw(); |
815 |
break;
|
816 |
case KBC_IRQ:
|
817 |
if (scancode[0] == ESC_BREAK_CODE) good = false; |
818 |
else if (scancode[0] == ENTER_MAKE_CODE) { |
819 |
hltp_send_string(buffer); |
820 |
char buffer2[CHAT_MAX_SIZE+3] = "> "; |
821 |
strncat(buffer2, buffer, strlen(buffer)); |
822 |
for(size_t i = CHAT_MAX_NUM-1; i; --i) |
823 |
text_set_text(t_text[i], text_get_string(t_text[i-1]));
|
824 |
text_set_text(t_text[0], buffer2);
|
825 |
for(size_t i = 0; i < CHAT_MAX_NUM; ++i){ |
826 |
if(text_get_string(t_text[i])[0] == '>'){ |
827 |
text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
|
828 |
text_set_halign(t_text[i], text_halign_left); |
829 |
}else{
|
830 |
text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
|
831 |
text_set_halign(t_text[i], text_halign_right); |
832 |
} |
833 |
} |
834 |
buffer[0] = '\0'; |
835 |
} else if(scancode[0] == BACKSPACE_MAKE_CODE){ |
836 |
buffer[strlen(buffer)-1] = '\0'; |
837 |
} else {
|
838 |
char c = map_makecode(scancode[0]); |
839 |
if (c == ERROR_CODE) break; |
840 |
if(strlen(buffer) < CHAT_MAX_SIZE) strncat(buffer, &c, 1); |
841 |
else printf("Char limit exceeded\n"); |
842 |
} |
843 |
text_set_text(t_buffer, buffer); |
844 |
char buffer2[20]; |
845 |
sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
|
846 |
text_set_text(t_size, buffer2); |
847 |
case MOUSE_IRQ:
|
848 |
if (counter_mouse_ih >= 3) { |
849 |
mouse_parse_packet(packet_mouse_ih, &pp); |
850 |
update_mouse(&pp); |
851 |
counter_mouse_ih = 0;
|
852 |
} |
853 |
break;
|
854 |
case COM1_IRQ: nctp_ih(); break; |
855 |
} |
856 |
} |
857 |
} |
858 |
} |
859 |
|
860 |
rectangle_dtor(r_buffer); |
861 |
text_dtor (t_buffer); |
862 |
|
863 |
rectangle_dtor(r_text); |
864 |
for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_dtor(t_text[i]); |
865 |
|
866 |
nctp_set_processor(NULL);
|
867 |
|
868 |
return SUCCESS;
|
869 |
} |