root / proj / src / proj.c @ 323
History | View | Annotate | Download (37.2 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 |
static basic_sprite_t *bsp_crosshair = NULL; |
53 |
static basic_sprite_t *bsp_shooter = NULL; |
54 |
static basic_sprite_t *bsp_zombie = NULL; |
55 |
static basic_sprite_t *bsp_pistol = NULL; |
56 |
static basic_sprite_t *bsp_nothing = NULL; |
57 |
static basic_sprite_t *bsp_bullet = NULL; |
58 |
static map_t *map1 = NULL; |
59 |
static 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(font_get_default(), "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(font_get_default()); |
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 |
uint64_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 |
uint32_t n = 1;
|
122 |
for (uint8_t i = 0; i < 32; i++, n <<= 1) { |
123 |
if (int_vector & n) {
|
124 |
interrupt_handler(i); |
125 |
switch (i) {
|
126 |
case TIMER0_IRQ:
|
127 |
|
128 |
graph_clear_screen(); |
129 |
switch(menu_update_state(main_menu, click)){
|
130 |
case -1: break; |
131 |
case 0: singleplayer(); break; //campaign(); break; |
132 |
case 1: multiplayer() ; break; |
133 |
case 2: chat(); break; |
134 |
case 3: good = false; break; |
135 |
} |
136 |
menu_draw(main_menu); |
137 |
|
138 |
click = 0;
|
139 |
|
140 |
sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
141 |
sprite_draw(sp_crosshair); |
142 |
graph_draw(); |
143 |
|
144 |
break;
|
145 |
case KBC_IRQ:
|
146 |
if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) good = false; |
147 |
case MOUSE_IRQ:
|
148 |
if (mouse_get_counter_mouse_ih() >= 3) { |
149 |
mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp); |
150 |
update_mouse(&pp); |
151 |
if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
|
152 |
last_lb = keys->lb_pressed; |
153 |
mouse_set_counter_mouse_ih(0);
|
154 |
} |
155 |
break;
|
156 |
case COM1_IRQ: nctp_ih(); break; |
157 |
} |
158 |
} |
159 |
} |
160 |
} |
161 |
|
162 |
basic_sprite_dtor (bsp_crosshair); bsp_crosshair = NULL;
|
163 |
basic_sprite_dtor (bsp_shooter ); bsp_shooter = NULL;
|
164 |
basic_sprite_dtor (bsp_zombie ); bsp_zombie = NULL;
|
165 |
sprite_dtor (sp_crosshair ); sp_crosshair = NULL;
|
166 |
basic_sprite_dtor (bsp_pistol ); bsp_pistol = NULL;
|
167 |
basic_sprite_dtor (bsp_nothing ); bsp_nothing = NULL;
|
168 |
map_dtor (map1 ); map1 = NULL;
|
169 |
font_free(); |
170 |
|
171 |
// Unsubscribe interrupts
|
172 |
if (unsubscribe_all()) {
|
173 |
if (cleanup())
|
174 |
printf("%s: failed to cleanup.\n", __func__);
|
175 |
return 1; |
176 |
} |
177 |
|
178 |
if (cleanup()) {
|
179 |
printf("%s: failed to cleanup.\n", __func__);
|
180 |
return 1; |
181 |
} |
182 |
|
183 |
return 0; |
184 |
} |
185 |
|
186 |
static host_info_t *host_info = NULL; |
187 |
static remote_info_t *remote_info = NULL; |
188 |
static bullet_info_t *bullet_info = NULL; |
189 |
|
190 |
static void multiplayer_process(const uint8_t *p, const size_t sz) { |
191 |
void *dest = NULL; |
192 |
hltp_type tp = hltp_interpret(p, sz, &dest); |
193 |
switch(tp){
|
194 |
case hltp_type_host:
|
195 |
host_info_dtor(host_info); |
196 |
host_info = (host_info_t*)dest; |
197 |
break;
|
198 |
case hltp_type_remote:
|
199 |
remote_info_dtor(remote_info); |
200 |
remote_info = (remote_info_t*)dest; |
201 |
break;
|
202 |
case hltp_type_bullet:
|
203 |
bullet_info_dtor(bullet_info); |
204 |
bullet_info = (bullet_info_t*)dest; |
205 |
break;
|
206 |
case hltp_type_invalid: break; |
207 |
case hltp_type_string : break; |
208 |
} |
209 |
} |
210 |
static int (multiplayer_host)(void); |
211 |
static int (multiplayer_remote)(void); |
212 |
static int (multiplayer)(void) { |
213 |
int r;
|
214 |
|
215 |
menu_t *main_menu = menu_ctor(font_get_default()); |
216 |
menu_add_item(main_menu, "Create");
|
217 |
menu_add_item(main_menu, "Connect");
|
218 |
menu_add_item(main_menu, "Back");
|
219 |
|
220 |
//uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
221 |
uint8_t last_lb = 0;
|
222 |
struct packet pp;
|
223 |
keys_t *keys = get_key_presses(); |
224 |
|
225 |
/// loop stuff
|
226 |
int click = 0; |
227 |
uint64_t int_vector = 0;
|
228 |
int good = true; |
229 |
while (good) {
|
230 |
/* Get a request message. */
|
231 |
if((r = get_interrupts_vector(&int_vector))) return r; |
232 |
uint32_t n = 1;
|
233 |
for (uint8_t i = 0; i < 32; i++, n <<= 1) { |
234 |
if (int_vector & n) {
|
235 |
interrupt_handler(i); |
236 |
switch (i) {
|
237 |
case TIMER0_IRQ:
|
238 |
|
239 |
graph_clear_screen(); |
240 |
switch(menu_update_state(main_menu, click)){
|
241 |
case -1: break; |
242 |
case 0: multiplayer_host(); break; |
243 |
case 1: multiplayer_remote(); break; |
244 |
case 2: good = false; break; |
245 |
} |
246 |
menu_draw(main_menu); |
247 |
|
248 |
click = 0;
|
249 |
|
250 |
sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
251 |
sprite_draw(sp_crosshair); |
252 |
graph_draw(); |
253 |
|
254 |
break;
|
255 |
case KBC_IRQ:
|
256 |
if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) good = false; |
257 |
case MOUSE_IRQ:
|
258 |
if (mouse_get_counter_mouse_ih() >= 3) { |
259 |
mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp); |
260 |
update_mouse(&pp); |
261 |
if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
|
262 |
last_lb = keys->lb_pressed; |
263 |
mouse_set_counter_mouse_ih(0);
|
264 |
} |
265 |
break;
|
266 |
case COM1_IRQ: nctp_ih(); break; |
267 |
} |
268 |
} |
269 |
} |
270 |
} |
271 |
return 0; |
272 |
} |
273 |
|
274 |
static int (multiplayer_host)(void) { |
275 |
int r;
|
276 |
|
277 |
nctp_dump(); |
278 |
nctp_set_processor(multiplayer_process); |
279 |
|
280 |
ent_set_scale(DEFAULT_SCALE); |
281 |
text_timer_t *in_game_timer = timer_ctor(font_get_default()); |
282 |
|
283 |
list_t *shooter_list = list_ctor(); |
284 |
|
285 |
gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n"); |
286 |
gunner_set_spawn(shooter1, 75, 75); |
287 |
|
288 |
gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 2); if(shooter2 == NULL) printf("Failed to get shooter2\n"); |
289 |
gunner_set_spawn(shooter2, 975, 75); |
290 |
|
291 |
list_insert(shooter_list, list_end(shooter_list), shooter1); |
292 |
list_insert(shooter_list, list_end(shooter_list), shooter2); |
293 |
|
294 |
do {
|
295 |
get_random_spawn(map1, shooter1, shooter_list); |
296 |
get_random_spawn(map1, shooter2, shooter_list); |
297 |
} while (distance_gunners(shooter1, shooter2) < 500); |
298 |
|
299 |
host_info = host_info_ctor(shooter1, shooter2); |
300 |
remote_info = remote_info_ctor(); |
301 |
bullet_info = bullet_info_ctor(); |
302 |
|
303 |
list_t *bullet_list = list_ctor(); |
304 |
|
305 |
ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
306 |
gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
307 |
|
308 |
//uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
309 |
uint8_t last_lb = 0;
|
310 |
struct packet pp;
|
311 |
keys_t *keys = get_key_presses(); |
312 |
/// loop stuff
|
313 |
uint64_t int_vector = 0;
|
314 |
int good = true; |
315 |
int state = 0; // -1 for remote win, 0 for draw, 1 for host win |
316 |
list_node_t *p1, *p2; // player states
|
317 |
int state_1, state_2;
|
318 |
while (good) {
|
319 |
if ((r = get_interrupts_vector(&int_vector))) return r; |
320 |
uint32_t n = 1;
|
321 |
for (uint8_t i = 0; i < 32; i++, n <<= 1) { |
322 |
if (int_vector & n) {
|
323 |
interrupt_handler(i); |
324 |
switch (i) {
|
325 |
case TIMER0_IRQ:
|
326 |
if (timer_get_no_interrupts() % 60 == 0) timer_update(in_game_timer); |
327 |
|
328 |
update_movement(map1, shooter1, keys, shooter_list); |
329 |
update_movement(map1, shooter2, &(remote_info->remote_keys_pressed), shooter_list); |
330 |
|
331 |
update_game_state(map1, shooter_list, bullet_list); |
332 |
|
333 |
p1 = list_find(shooter_list, shooter1); |
334 |
p2 = list_find(shooter_list, shooter2); |
335 |
|
336 |
if ((state_1 = (p1 == list_end(shooter_list))) || (state_2 = (p2 == list_end(shooter_list)))) {
|
337 |
state = state_1 - state_2; |
338 |
good = false;
|
339 |
break;
|
340 |
} |
341 |
|
342 |
double angle = get_mouse_angle(shooter1);
|
343 |
gunner_set_angle(shooter1, angle - M_PI_2); |
344 |
|
345 |
ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
346 |
gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
347 |
|
348 |
gunner_set_angle(shooter2, remote_info->remote_angle); |
349 |
|
350 |
build_host_structure(host_info, shooter1, shooter2, bullet_list); |
351 |
|
352 |
hltp_send_host_info(host_info); |
353 |
|
354 |
graph_clear_screen(); |
355 |
map_draw (map1); |
356 |
bullet_draw_list(bullet_list); |
357 |
gunner_draw_list(shooter_list); |
358 |
|
359 |
text_draw(in_game_timer->text); |
360 |
|
361 |
sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
362 |
sprite_draw(sp_crosshair); |
363 |
graph_draw(); |
364 |
|
365 |
break;
|
366 |
case KBC_IRQ:
|
367 |
if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) { |
368 |
good = false;
|
369 |
} |
370 |
break;
|
371 |
case MOUSE_IRQ:
|
372 |
if (mouse_get_counter_mouse_ih() >= 3) { |
373 |
mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp); |
374 |
update_mouse(&pp); |
375 |
if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
|
376 |
shoot_bullet(shooter1, bullet_list, bsp_bullet); |
377 |
last_lb = keys->lb_pressed; |
378 |
mouse_set_counter_mouse_ih(0);
|
379 |
} |
380 |
break;
|
381 |
|
382 |
case COM1_IRQ:
|
383 |
nctp_ih(); |
384 |
if (bullet_info->new_bullet) {
|
385 |
shoot_bullet(shooter2, bullet_list, bsp_bullet); |
386 |
bullet_info->new_bullet = false;
|
387 |
} |
388 |
break;
|
389 |
} |
390 |
} |
391 |
} |
392 |
} |
393 |
|
394 |
while(list_size(shooter_list) > 0){ |
395 |
gunner_t *p = list_erase(shooter_list, list_begin(shooter_list)); |
396 |
gunner_dtor(p); |
397 |
} |
398 |
if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n"); |
399 |
|
400 |
while(list_size(bullet_list) > 0){ |
401 |
bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list)); |
402 |
bullet_dtor(p); |
403 |
} |
404 |
if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n"); |
405 |
|
406 |
host_info_dtor(host_info); |
407 |
remote_info_dtor(remote_info); |
408 |
bullet_info_dtor(bullet_info); |
409 |
|
410 |
timer_dtor(in_game_timer); in_game_timer = NULL;
|
411 |
|
412 |
return 0; |
413 |
} |
414 |
static int (multiplayer_remote)(void) { |
415 |
int r;
|
416 |
|
417 |
nctp_dump(); |
418 |
nctp_set_processor(multiplayer_process); |
419 |
|
420 |
ent_set_scale(DEFAULT_SCALE); |
421 |
text_timer_t *in_game_timer = timer_ctor(font_get_default()); |
422 |
|
423 |
list_t *shooter_list = list_ctor(); |
424 |
|
425 |
gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 2); if(shooter1 == NULL) printf("Failed to get shooter1\n"); |
426 |
gunner_set_spawn(shooter1, 75, 75); |
427 |
|
428 |
gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 1); if(shooter2 == NULL) printf("Failed to get shooter2\n"); |
429 |
gunner_set_spawn(shooter2, 975, 75); |
430 |
|
431 |
list_insert(shooter_list, list_end(shooter_list), shooter1); |
432 |
list_insert(shooter_list, list_end(shooter_list), shooter2); |
433 |
|
434 |
host_info = host_info_ctor(shooter2, shooter1); |
435 |
remote_info = remote_info_ctor(); |
436 |
bullet_info = bullet_info_ctor(); |
437 |
|
438 |
list_t *bullet_list = list_ctor(); |
439 |
|
440 |
ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
441 |
gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
442 |
|
443 |
//uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
444 |
uint8_t last_lb = 0;
|
445 |
struct packet pp;
|
446 |
keys_t *keys = get_key_presses(); |
447 |
|
448 |
/// loop stuff
|
449 |
uint64_t int_vector = 0;
|
450 |
int good = true; |
451 |
while (good) {
|
452 |
if ((r = get_interrupts_vector(&int_vector))) return r; |
453 |
uint32_t n = 1;
|
454 |
for (uint8_t i = 0; i < 32; i++, n <<= 1) { |
455 |
if (int_vector & n) {
|
456 |
interrupt_handler(i); |
457 |
switch (i) {
|
458 |
case TIMER0_IRQ:
|
459 |
if (timer_get_no_interrupts() % 60 == 0) timer_update(in_game_timer); |
460 |
|
461 |
double angle = get_mouse_angle(shooter1);
|
462 |
|
463 |
build_remote_structure(remote_info, keys, angle); |
464 |
|
465 |
hltp_send_remote_info(remote_info); |
466 |
|
467 |
gunner_set_pos(shooter1, host_info->remote_x, host_info->remote_y); |
468 |
gunner_set_angle(shooter1, host_info->remote_angle); |
469 |
gunner_set_health(shooter1, host_info->remote_health); |
470 |
gunner_set_curr_health(shooter1, host_info->remote_current_health); |
471 |
|
472 |
gunner_set_pos(shooter2, host_info->host_x, host_info->host_y); |
473 |
gunner_set_angle(shooter2, host_info->host_angle); |
474 |
gunner_set_health(shooter2, host_info->host_health); |
475 |
gunner_set_curr_health(shooter2, host_info->host_current_health); |
476 |
|
477 |
ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
478 |
gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
479 |
|
480 |
for (size_t j = 0; j < host_info->no_bullets; j++) { |
481 |
if (host_info->bullets_shooter[j]) { // remote |
482 |
bullet_t *bullet = bullet_ctor(shooter1, bsp_bullet, host_info->bullets_x[j], host_info->bullets_y[j], host_info->bullets_vx[j], host_info->bullets_vy[j]); |
483 |
list_insert(bullet_list, list_end(bullet_list), bullet); |
484 |
} else { // host |
485 |
bullet_t *bullet = bullet_ctor(shooter2, bsp_bullet, host_info->bullets_x[j], host_info->bullets_y[j], host_info->bullets_vx[j], host_info->bullets_vy[j]); |
486 |
list_insert(bullet_list, list_end(bullet_list), bullet); |
487 |
} |
488 |
} |
489 |
|
490 |
graph_clear_screen(); |
491 |
map_draw (map1); |
492 |
bullet_draw_list(bullet_list); |
493 |
gunner_draw_list(shooter_list); |
494 |
|
495 |
text_draw(in_game_timer->text); |
496 |
|
497 |
sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
498 |
sprite_draw(sp_crosshair); |
499 |
graph_draw(); |
500 |
|
501 |
while(list_size(bullet_list) > 0){ |
502 |
bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list)); |
503 |
bullet_dtor(p); |
504 |
} |
505 |
|
506 |
break;
|
507 |
case KBC_IRQ:
|
508 |
if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) { |
509 |
good = false;
|
510 |
} |
511 |
break;
|
512 |
case MOUSE_IRQ:
|
513 |
if (mouse_get_counter_mouse_ih() >= 3) { |
514 |
mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp); |
515 |
update_mouse(&pp); |
516 |
if (last_lb ^ keys->lb_pressed && keys->lb_pressed) {
|
517 |
bullet_info->new_bullet = true;
|
518 |
hltp_send_bullet_info(bullet_info); |
519 |
} |
520 |
last_lb = keys->lb_pressed; |
521 |
mouse_set_counter_mouse_ih(0);
|
522 |
} |
523 |
break;
|
524 |
|
525 |
case COM1_IRQ: nctp_ih(); break; |
526 |
} |
527 |
} |
528 |
} |
529 |
} |
530 |
|
531 |
while(list_size(shooter_list) > 0){ |
532 |
gunner_t *p = list_erase(shooter_list, list_begin(shooter_list)); |
533 |
gunner_dtor(p); |
534 |
} |
535 |
if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n"); |
536 |
|
537 |
while(list_size(bullet_list) > 0){ |
538 |
bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list)); |
539 |
bullet_dtor(p); |
540 |
} |
541 |
if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n"); |
542 |
|
543 |
host_info_dtor(host_info); |
544 |
remote_info_dtor(remote_info); |
545 |
bullet_info_dtor(bullet_info); |
546 |
|
547 |
timer_dtor(in_game_timer); in_game_timer = NULL;
|
548 |
|
549 |
return 0; |
550 |
} |
551 |
|
552 |
static int (campaign)(void); |
553 |
static int (zombies)(void); |
554 |
static int (singleplayer)(void) { |
555 |
|
556 |
int r;
|
557 |
|
558 |
menu_t *main_menu = menu_ctor(font_get_default()); |
559 |
menu_add_item(main_menu, "Campaign");
|
560 |
menu_add_item(main_menu, "Zombies");
|
561 |
menu_add_item(main_menu, "Back");
|
562 |
|
563 |
//uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
564 |
uint8_t last_lb = 0;
|
565 |
struct packet pp;
|
566 |
keys_t *keys = get_key_presses(); |
567 |
|
568 |
/// loop stuff
|
569 |
int click = 0; |
570 |
uint64_t int_vector = 0;
|
571 |
int good = true; |
572 |
while (good) {
|
573 |
/* Get a request message. */
|
574 |
if((r = get_interrupts_vector(&int_vector))) return r; |
575 |
uint32_t n = 1;
|
576 |
for (uint8_t i = 0; i < 32; i++, n <<= 1) { |
577 |
if (int_vector & n) {
|
578 |
interrupt_handler(i); |
579 |
switch (i) {
|
580 |
case TIMER0_IRQ:
|
581 |
|
582 |
graph_clear_screen(); |
583 |
switch(menu_update_state(main_menu, click)){
|
584 |
case -1: break; |
585 |
case 0: campaign(); break; |
586 |
case 1: zombies(); break; |
587 |
case 2: good = false; break; |
588 |
} |
589 |
menu_draw(main_menu); |
590 |
|
591 |
click = 0;
|
592 |
|
593 |
sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
594 |
sprite_draw(sp_crosshair); |
595 |
graph_draw(); |
596 |
|
597 |
break;
|
598 |
case KBC_IRQ:
|
599 |
if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) good = false; |
600 |
case MOUSE_IRQ:
|
601 |
if (mouse_get_counter_mouse_ih() >= 3) { |
602 |
mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp); |
603 |
update_mouse(&pp); |
604 |
if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
|
605 |
last_lb = keys->lb_pressed; |
606 |
mouse_set_counter_mouse_ih(0);
|
607 |
} |
608 |
break;
|
609 |
case COM1_IRQ: nctp_ih(); break; |
610 |
} |
611 |
} |
612 |
} |
613 |
} |
614 |
|
615 |
return 0; |
616 |
} |
617 |
|
618 |
static int (campaign)(void){ |
619 |
|
620 |
int r;
|
621 |
|
622 |
ent_set_scale(DEFAULT_SCALE); |
623 |
text_timer_t *in_game_timer = timer_ctor(font_get_default()); |
624 |
|
625 |
list_t *shooter_list = list_ctor(); |
626 |
|
627 |
gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n"); |
628 |
gunner_set_spawn(shooter1, 75, 75); |
629 |
gunner_set_pos(shooter1, 75, 75); |
630 |
|
631 |
gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing, GUNNER_PLAYER, 2);
|
632 |
gunner_set_spawn(shooter2, 975, 75); |
633 |
gunner_set_pos(shooter2, 775, 75); |
634 |
|
635 |
list_insert(shooter_list, list_end(shooter_list), shooter1); |
636 |
list_insert(shooter_list, list_end(shooter_list), shooter2); |
637 |
|
638 |
list_t *bullet_list = list_ctor(); |
639 |
|
640 |
ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
641 |
gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
642 |
|
643 |
//uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
644 |
uint8_t last_lb = 0;
|
645 |
struct packet pp;
|
646 |
keys_t *keys = get_key_presses(); |
647 |
|
648 |
/// loop stuff
|
649 |
uint64_t int_vector = 0;
|
650 |
int good = true; |
651 |
while (good) {
|
652 |
/* Get a request message. */
|
653 |
if((r = get_interrupts_vector(&int_vector))) return r; |
654 |
uint32_t n = 1;
|
655 |
for (uint8_t i = 0; i < 32; i++, n <<= 1) { |
656 |
if(!good) break; |
657 |
if (int_vector & n) {
|
658 |
interrupt_handler(i); |
659 |
switch (i) {
|
660 |
case TIMER0_IRQ:
|
661 |
|
662 |
if (timer_get_no_interrupts() % 60 == 0) timer_update(in_game_timer); |
663 |
update_movement(map1, shooter1, keys, shooter_list); |
664 |
|
665 |
update_game_state(map1, shooter_list, bullet_list); |
666 |
|
667 |
//update_scale();
|
668 |
double angle = get_mouse_angle(shooter1);
|
669 |
gunner_set_angle(shooter1, angle - M_PI_2); |
670 |
|
671 |
ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
672 |
gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
673 |
|
674 |
graph_clear_screen(); |
675 |
map_draw (map1); |
676 |
bullet_draw_list(bullet_list); |
677 |
gunner_draw_list(shooter_list); |
678 |
|
679 |
text_draw(in_game_timer->text); |
680 |
|
681 |
sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
682 |
sprite_draw(sp_crosshair); |
683 |
graph_draw(); |
684 |
|
685 |
break;
|
686 |
case KBC_IRQ:
|
687 |
if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) { |
688 |
good = false;
|
689 |
} |
690 |
break;
|
691 |
case MOUSE_IRQ:
|
692 |
if (mouse_get_counter_mouse_ih() >= 3) { |
693 |
mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp); |
694 |
update_mouse(&pp); |
695 |
if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
|
696 |
shoot_bullet(shooter1, bullet_list, bsp_bullet); |
697 |
last_lb = keys->lb_pressed; |
698 |
mouse_set_counter_mouse_ih(0);
|
699 |
|
700 |
} |
701 |
break;
|
702 |
case COM1_IRQ: nctp_ih(); break; |
703 |
} |
704 |
} |
705 |
} |
706 |
} |
707 |
|
708 |
while(list_size(shooter_list) > 0){ |
709 |
gunner_t *p = list_erase(shooter_list, list_begin(shooter_list)); |
710 |
gunner_dtor(p); |
711 |
} |
712 |
if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n"); |
713 |
|
714 |
while(list_size(bullet_list) > 0){ |
715 |
bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list)); |
716 |
bullet_dtor(p); |
717 |
} |
718 |
if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n"); |
719 |
|
720 |
timer_dtor(in_game_timer); in_game_timer = NULL;
|
721 |
|
722 |
return SUCCESS;
|
723 |
} |
724 |
|
725 |
#define ZOMBIES_NUM 5 |
726 |
#define ZOMBIE_HEALTH_FACTOR 1.1 |
727 |
static int (zombies)(void){ |
728 |
|
729 |
int r;
|
730 |
|
731 |
ent_set_scale(DEFAULT_SCALE); |
732 |
text_timer_t *in_game_timer = timer_ctor(font_get_default()); |
733 |
|
734 |
list_t *shooter_list = list_ctor(); |
735 |
|
736 |
gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n"); |
737 |
gunner_set_spawn(shooter1, 980, 790); |
738 |
gunner_set_pos(shooter1, 980, 790); |
739 |
|
740 |
list_insert(shooter_list, list_end(shooter_list), shooter1); |
741 |
|
742 |
list_t *bullet_list = list_ctor(); |
743 |
|
744 |
ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
745 |
gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
746 |
|
747 |
//uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
|
748 |
uint8_t last_lb = 0;
|
749 |
struct packet pp;
|
750 |
keys_t *keys = get_key_presses(); |
751 |
|
752 |
/// loop stuff
|
753 |
uint64_t int_vector = 0;
|
754 |
int good = true; |
755 |
int dead = false; |
756 |
|
757 |
int health = 50; |
758 |
|
759 |
map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1)); |
760 |
|
761 |
while (good && !dead) {
|
762 |
/* Get a request message. */
|
763 |
if((r = get_interrupts_vector(&int_vector))) return r; |
764 |
uint32_t n = 1;
|
765 |
for (uint8_t i = 0; i < 32; i++, n <<= 1) { |
766 |
if(!good || dead) break; |
767 |
if (int_vector & n) {
|
768 |
interrupt_handler(i); |
769 |
switch (i) {
|
770 |
case TIMER0_IRQ:
|
771 |
if (timer_get_no_interrupts() % 60 == 0) timer_update(in_game_timer); |
772 |
if (timer_get_no_interrupts() % 6 == 0){ |
773 |
map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1)); |
774 |
} |
775 |
|
776 |
update_movement(map1, shooter1, keys, shooter_list); |
777 |
|
778 |
update_game_state(map1, shooter_list, bullet_list); |
779 |
|
780 |
if(list_find(shooter_list, shooter1) == list_end(shooter_list)){
|
781 |
good = false;
|
782 |
dead = true;
|
783 |
break;
|
784 |
} |
785 |
|
786 |
double angle = get_mouse_angle(shooter1);
|
787 |
gunner_set_angle(shooter1, angle - M_PI_2); |
788 |
|
789 |
ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
790 |
gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
791 |
|
792 |
while(list_size(shooter_list) < ZOMBIES_NUM+1){ |
793 |
gunner_t *zombie = gunner_ctor(bsp_zombie, bsp_nothing, GUNNER_MELEE | GUNNER_FOLLOW, 3);
|
794 |
gunner_set_health(zombie, health); |
795 |
gunner_set_curr_health(zombie, health); |
796 |
health *= ZOMBIE_HEALTH_FACTOR; |
797 |
get_random_spawn(map1, zombie, shooter_list); |
798 |
list_push_back(shooter_list, zombie); |
799 |
} |
800 |
|
801 |
graph_clear_screen(); |
802 |
map_draw (map1); |
803 |
bullet_draw_list(bullet_list); |
804 |
gunner_draw_list(shooter_list); |
805 |
|
806 |
text_draw(in_game_timer->text); |
807 |
|
808 |
sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
809 |
sprite_draw(sp_crosshair); |
810 |
graph_draw(); |
811 |
|
812 |
break;
|
813 |
case KBC_IRQ:
|
814 |
if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) { |
815 |
good = false;
|
816 |
} |
817 |
break;
|
818 |
case MOUSE_IRQ:
|
819 |
if (mouse_get_counter_mouse_ih() >= 3) { |
820 |
mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp); |
821 |
update_mouse(&pp); |
822 |
if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
|
823 |
shoot_bullet(shooter1, bullet_list, bsp_bullet); |
824 |
last_lb = keys->lb_pressed; |
825 |
mouse_set_counter_mouse_ih(0);
|
826 |
|
827 |
} |
828 |
break;
|
829 |
case COM1_IRQ: nctp_ih(); break; |
830 |
} |
831 |
} |
832 |
} |
833 |
} |
834 |
|
835 |
while(list_size(shooter_list) > 0){ |
836 |
gunner_t *p = list_erase(shooter_list, list_begin(shooter_list)); |
837 |
gunner_dtor(p); |
838 |
} |
839 |
if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n"); |
840 |
|
841 |
while(list_size(bullet_list) > 0){ |
842 |
bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list)); |
843 |
bullet_dtor(p); |
844 |
} |
845 |
if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n"); |
846 |
|
847 |
if(dead){
|
848 |
printf("YOU DIED\n");
|
849 |
} |
850 |
|
851 |
timer_dtor(in_game_timer); in_game_timer = NULL;
|
852 |
|
853 |
return SUCCESS;
|
854 |
} |
855 |
|
856 |
#define CHAT_MAX_SIZE 75 |
857 |
#define CHAT_MAX_NUM 19 |
858 |
static text_t *t_text[CHAT_MAX_NUM] = {NULL}; |
859 |
static rectangle_t *r_text = NULL; |
860 |
static void chat_process(const uint8_t *p, const size_t sz){ |
861 |
char buffer2[CHAT_MAX_NUM+3]; |
862 |
void *dest = NULL; |
863 |
hltp_type tp = hltp_interpret(p, sz, &dest); |
864 |
switch(tp){
|
865 |
case hltp_type_string:
|
866 |
strcpy(buffer2, dest); |
867 |
strncat(buffer2, " <", 2); |
868 |
for(size_t i = CHAT_MAX_NUM-1; i; --i) |
869 |
text_set_string(t_text[i], text_get_string(t_text[i-1]));
|
870 |
text_set_string(t_text[0], buffer2);
|
871 |
for(size_t i = 0; i < CHAT_MAX_NUM; ++i){ |
872 |
if(text_get_string(t_text[i])[0] == '>'){ |
873 |
text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
|
874 |
text_set_halign(t_text[i], text_halign_left); |
875 |
}else{
|
876 |
text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
|
877 |
text_set_halign(t_text[i], text_halign_right); |
878 |
} |
879 |
} |
880 |
break;
|
881 |
case hltp_type_invalid: break; |
882 |
case hltp_type_bullet : break; |
883 |
case hltp_type_host : break; |
884 |
case hltp_type_remote : break; |
885 |
} |
886 |
} |
887 |
static int (chat)(void){ |
888 |
int r;
|
889 |
|
890 |
nctp_dump(); |
891 |
nctp_set_processor(chat_process); |
892 |
|
893 |
struct packet pp;
|
894 |
|
895 |
char buffer[CHAT_MAX_SIZE] = ""; |
896 |
rectangle_t *r_buffer = NULL; {
|
897 |
r_buffer = rectangle_ctor(0,0,900,70); |
898 |
rectangle_set_pos(r_buffer, graph_get_XRes()/2 -rectangle_get_w(r_buffer)/2, |
899 |
(int16_t)(graph_get_YRes()*0.87-rectangle_get_h(r_buffer)/2)); |
900 |
rectangle_set_fill_color (r_buffer, GRAPH_BLACK); |
901 |
rectangle_set_outline_width(r_buffer, 2);
|
902 |
rectangle_set_outline_color(r_buffer, GRAPH_WHITE); |
903 |
rectangle_set_fill_trans(r_buffer, GRAPH_TRANSPARENT); |
904 |
} |
905 |
text_t *t_buffer = NULL; {
|
906 |
t_buffer = text_ctor(font_get_default(), "");
|
907 |
text_set_pos(t_buffer, rectangle_get_x(r_buffer)+50,
|
908 |
rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)/2);
|
909 |
text_set_halign(t_buffer, text_halign_left); |
910 |
text_set_valign(t_buffer, text_valign_center); |
911 |
text_set_color (t_buffer, TEXT_COLOR); |
912 |
} |
913 |
text_t *t_size = NULL; {
|
914 |
t_size = text_ctor(font_get_default(), "");
|
915 |
text_set_pos(t_size, rectangle_get_x(r_buffer)+rectangle_get_w(r_buffer)-5,
|
916 |
rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)-5);
|
917 |
text_set_halign(t_size, text_halign_right); |
918 |
text_set_valign(t_size, text_valign_bottom); |
919 |
text_set_color (t_size, TEXT_COLOR); |
920 |
text_set_size (t_size, 18);
|
921 |
char buffer2[20]; |
922 |
sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
|
923 |
text_set_string(t_size, buffer2); |
924 |
} |
925 |
|
926 |
/** r_text */ {
|
927 |
r_text = rectangle_ctor(0,0,900,550); |
928 |
rectangle_set_pos(r_text, graph_get_XRes()/2 -rectangle_get_w(r_buffer)/2, |
929 |
(int16_t)(graph_get_YRes()*0.09)); |
930 |
rectangle_set_fill_color (r_text, GRAPH_BLACK); |
931 |
rectangle_set_outline_width(r_text, 2);
|
932 |
rectangle_set_outline_color(r_text, GRAPH_WHITE); |
933 |
rectangle_set_fill_trans(r_text, GRAPH_TRANSPARENT); |
934 |
} |
935 |
/** t_text */ {
|
936 |
for(uint16_t i = 0; i < CHAT_MAX_NUM; ++i){ |
937 |
t_text[i] = text_ctor(font_get_default(), " ");
|
938 |
text_set_pos(t_text[i], rectangle_get_x(r_text)+50,
|
939 |
rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-25*i); |
940 |
text_set_halign(t_text[i], text_halign_left); |
941 |
text_set_valign(t_text[i], text_valign_bottom); |
942 |
text_set_color (t_text[i], TEXT_COLOR); |
943 |
} |
944 |
} |
945 |
|
946 |
/// loop stuff
|
947 |
uint64_t int_vector = 0;
|
948 |
int good = true; |
949 |
while (good) {
|
950 |
/* Get a request message. */
|
951 |
if((r = get_interrupts_vector(&int_vector))) return r; |
952 |
uint32_t n = 1;
|
953 |
for (uint8_t i = 0; i < 32; i++, n <<= 1) { |
954 |
if (int_vector & n) {
|
955 |
interrupt_handler(i); |
956 |
switch (i) {
|
957 |
case TIMER0_IRQ:
|
958 |
graph_clear_screen(); |
959 |
sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y()); |
960 |
|
961 |
rectangle_draw(r_buffer); |
962 |
text_draw(t_buffer); |
963 |
text_draw(t_size); |
964 |
|
965 |
rectangle_draw(r_text); |
966 |
for(size_t j = 0; j < CHAT_MAX_NUM; ++j) text_draw(t_text[j]); |
967 |
|
968 |
sprite_draw(sp_crosshair); |
969 |
graph_draw(); |
970 |
break;
|
971 |
case KBC_IRQ:
|
972 |
if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) good = false; |
973 |
else if (keyboard_get_scancode()[0] == ENTER_MAKE_CODE) { |
974 |
hltp_send_string(buffer); |
975 |
char buffer2[CHAT_MAX_SIZE+3] = "> "; |
976 |
strncat(buffer2, buffer, strlen(buffer)); |
977 |
for(size_t j = CHAT_MAX_NUM-1; j; --j) text_set_string(t_text[i], text_get_string(t_text[i-1])); |
978 |
text_set_string(t_text[0], buffer2);
|
979 |
for(size_t j = 0; j < CHAT_MAX_NUM; ++j){ |
980 |
if(text_get_string(t_text[j])[0] == '>'){ |
981 |
text_set_pos(t_text[j], rectangle_get_x(r_text)+50, text_get_y(t_text[j]));
|
982 |
text_set_halign(t_text[j], text_halign_left); |
983 |
}else{
|
984 |
text_set_pos(t_text[j], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[j]));
|
985 |
text_set_halign(t_text[j], text_halign_right); |
986 |
} |
987 |
} |
988 |
buffer[0] = '\0'; |
989 |
} else if(keyboard_get_scancode()[0] == BACKSPACE_MAKE_CODE){ |
990 |
buffer[strlen(buffer)-1] = '\0'; |
991 |
} else {
|
992 |
char c = map_makecode(keyboard_get_scancode()[0]); |
993 |
if (c == ERROR_CODE) break; |
994 |
if(strlen(buffer) < CHAT_MAX_SIZE) strncat(buffer, &c, 1); |
995 |
else printf("Char limit exceeded\n"); |
996 |
} |
997 |
text_set_string(t_buffer, buffer); |
998 |
char buffer2[20]; |
999 |
sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
|
1000 |
text_set_string(t_size, buffer2); |
1001 |
case MOUSE_IRQ:
|
1002 |
if (mouse_get_counter_mouse_ih() >= 3) { |
1003 |
mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp); |
1004 |
update_mouse(&pp); |
1005 |
mouse_set_counter_mouse_ih(0);
|
1006 |
} |
1007 |
break;
|
1008 |
case COM1_IRQ: nctp_ih(); break; |
1009 |
} |
1010 |
} |
1011 |
} |
1012 |
} |
1013 |
|
1014 |
rectangle_dtor(r_buffer); |
1015 |
text_dtor (t_buffer); |
1016 |
|
1017 |
rectangle_dtor(r_text); |
1018 |
for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_dtor(t_text[i]); |
1019 |
|
1020 |
nctp_set_processor(NULL);
|
1021 |
|
1022 |
return SUCCESS;
|
1023 |
} |