Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 335

History | View | Annotate | Download (37.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 "sprite.h"
22
#include "rectangle.h"
23
#include "font.h"
24
#include "ent.h"
25

    
26
#include "crosshair.h"
27
#include "shooter.h"
28
#include "zombie.h"
29
#include "pistol.h"
30
#include "nothing.h"
31
#include "bullet.h"
32
#include "map1.h"
33

    
34
#include "errors.h"
35

    
36
#include "list.h"
37

    
38
int main(int argc, char* argv[]) {
39

    
40
    lcf_set_language("EN-US");
41

    
42
    lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
43

    
44
    lcf_log_output("/home/lcom/labs/proj/output.txt");
45

    
46
    if (lcf_start(argc, argv)) return 1;
47

    
48
    lcf_cleanup();
49

    
50
    return 0;
51
}
52

    
53
static basic_sprite_t       *bsp_crosshair = NULL;
54
static basic_sprite_t       *bsp_shooter   = NULL;
55
static basic_sprite_t       *bsp_zombie    = NULL;
56
static basic_sprite_t       *bsp_pistol    = NULL;
57
static basic_sprite_t       *bsp_nothing   = NULL;
58
static basic_sprite_t       *bsp_bullet    = NULL;
59
static map_t                *map1          = NULL;
60
static sprite_t             *sp_crosshair  = NULL;
61

    
62
static int (singleplayer)(void);
63
static int (multiplayer)(void);
64
static int (chat)(void);
65
int(proj_main_loop)(int argc, char *argv[]) {
66
    (void)argc; (void)argv;
67

    
68
    int r;
69

    
70
    if(font_init()){ printf("Failed to initialize fonts\n"); return 1; }
71

    
72
    /// subscribe interrupts
73
    if (subscribe_all()) { return 1; }
74

    
75
    /// initialize graphics
76
    if(graph_init(GRAPH_MODE)){
77
        printf("%s: failed to initalize graphics.\n", __func__);
78
        if (cleanup()) printf("%s: failed to cleanup.\n", __func__);
79
        return 1;
80
    }
81

    
82
    /// Load stuff
83
    {
84
        graph_clear_screen();
85
        text_t *txt = text_ctor(font_get_default(), "Loading...");
86
        text_set_pos(txt, graph_get_XRes()/2, graph_get_YRes()/2);
87
        text_set_valign(txt, text_valign_center);
88
        text_set_halign(txt, text_halign_center);
89
        text_set_color(txt, TEXT_COLOR);
90
        text_draw(txt);
91
        text_dtor(txt);
92
        graph_draw();
93

    
94
        bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n");
95
        bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
96
        bsp_zombie    = get_zombie   (); if(bsp_zombie    == NULL) printf("Failed to get zombie\n");
97
        bsp_pistol    = get_pistol   (); if(bsp_pistol    == NULL) printf("Failed to get pistol\n");
98
        bsp_nothing   = get_nothing  (); if(bsp_nothing   == NULL) printf("Failed to get nothing\n");
99
        bsp_bullet    = get_bullet   (); if(bsp_bullet    == NULL) printf("Failed to get bullet\n");
100
        map1          = get_map1     (); if(map1          == NULL) printf("Failed to get map1\n");
101

    
102
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
103
    }
104

    
105
    menu_t *main_menu = menu_ctor(font_get_default());
106
    menu_add_item(main_menu, "Single player");
107
    menu_add_item(main_menu, "Multiplayer");
108
    menu_add_item(main_menu, "Chat");
109
    menu_add_item(main_menu, "Exit");
110

    
111
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
112
    uint8_t last_lb = 0;
113
    struct packet pp;
114
    keys_t *keys = get_key_presses();
115

    
116
    /// loop stuff
117
    int click = 0;
118
    uint64_t int_vector = 0;
119
    int good = true;
120
    while (good) {
121
        /* Get a request message. */
122
        if((r = get_interrupts_vector(&int_vector))) return r;
123
        uint32_t n = 1;
124
        for (uint8_t i = 0; i < 32; i++, n <<= 1) {
125
            if (int_vector & n) {
126
                interrupt_handler(i);
127
                switch (i) {
128
                    case TIMER0_IRQ:
129

    
130
                    graph_clear_screen();
131
                    switch(menu_update_state(main_menu, click)){
132
                        case -1: break;
133
                        case  0: singleplayer(); break; //campaign(); break;
134
                        case  1: multiplayer() ; break;
135
                        case  2: chat(); break;
136
                        case  3: good = false; break;
137
                    }
138
                    menu_draw(main_menu);
139

    
140
                    click = 0;
141

    
142
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
143
                    sprite_draw(sp_crosshair);
144
                    graph_draw();
145

    
146
                    break;
147
                    case KBC_IRQ:
148
                    if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) good = false;
149
                    case MOUSE_IRQ:
150
                    if (mouse_get_counter_mouse_ih() >= 3) {
151
                        mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp);
152
                        update_mouse(&pp);
153
                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
154
                        last_lb = keys->lb_pressed;
155
                        mouse_set_counter_mouse_ih(0);
156
                    }
157
                    break;
158
                    case COM1_IRQ: nctp_ih(); break;
159
                }
160
            }
161
        }
162
    }
163

    
164
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
165
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
166
    basic_sprite_dtor      (bsp_zombie   ); bsp_zombie    = NULL;
167
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
168
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
169
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
170
    map_dtor               (map1         ); map1          = NULL;
171
    font_free();
172

    
173
    // Unsubscribe interrupts
174
    if (unsubscribe_all()) {
175
        if (cleanup())
176
        printf("%s: failed to cleanup.\n", __func__);
177
        return 1;
178
    }
179

    
180
    if (cleanup()) {
181
        printf("%s: failed to cleanup.\n", __func__);
182
        return 1;
183
    }
184

    
185
    return 0;
186
}
187

    
188
static host_info_t     *host_info   = NULL;
189
static remote_info_t   *remote_info = NULL;
190
static bullet_info_t   *bullet_info = NULL;
191

    
192
static void multiplayer_process(const uint8_t *p, const size_t sz) {
193
    void *dest = NULL;
194
    hltp_type tp = hltp_interpret(p, sz, &dest);
195
    switch(tp){
196
        case hltp_type_host:
197
            host_info_dtor(host_info);
198
            host_info = (host_info_t*)dest;
199
            break;
200
        case hltp_type_remote:
201
            remote_info_dtor(remote_info);
202
            remote_info = (remote_info_t*)dest;
203
            break;
204
        case hltp_type_bullet:
205
            bullet_info_dtor(bullet_info);
206
            bullet_info = (bullet_info_t*)dest;
207
            break;
208
        case hltp_type_invalid: break;
209
        case hltp_type_string : break;
210
    }
211
}
212
static int (multiplayer_host)(void);
213
static int (multiplayer_remote)(void);
214
static int (multiplayer)(void) {
215
    int r;
216

    
217
    menu_t *main_menu = menu_ctor(font_get_default());
218
    menu_add_item(main_menu, "Create");
219
    menu_add_item(main_menu, "Connect");
220
    menu_add_item(main_menu, "Back");
221

    
222
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
223
    uint8_t last_lb = 0;
224
    struct packet pp;
225
    keys_t *keys = get_key_presses();
226

    
227
    /// loop stuff
228
    int click = 0;
229
    uint64_t int_vector = 0;
230
    int good = true;
231
    while (good) {
232
        /* Get a request message. */
233
        if((r = get_interrupts_vector(&int_vector))) return r;
234
        uint32_t n = 1;
235
        for (uint8_t i = 0; i < 32; i++, n <<= 1) {
236
            if (int_vector & n) {
237
                interrupt_handler(i);
238
                switch (i) {
239
                    case TIMER0_IRQ:
240

    
241
                    graph_clear_screen();
242
                    switch(menu_update_state(main_menu, click)){
243
                        case -1: break;
244
                        case  0: multiplayer_host(); break;
245
                        case  1: multiplayer_remote(); break;
246
                        case  2: good = false; break;
247
                    }
248
                    menu_draw(main_menu);
249

    
250
                    click = 0;
251

    
252
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
253
                    sprite_draw(sp_crosshair);
254
                    graph_draw();
255

    
256
                    break;
257
                    case KBC_IRQ:
258
                    if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) good = false;
259
                    case MOUSE_IRQ:
260
                    if (mouse_get_counter_mouse_ih() >= 3) {
261
                        mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp);
262
                        update_mouse(&pp);
263
                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
264
                        last_lb = keys->lb_pressed;
265
                        mouse_set_counter_mouse_ih(0);
266
                    }
267
                    break;
268
                    case COM1_IRQ: nctp_ih(); break;
269
                }
270
            }
271
        }
272
    }
273
    return 0;
274
}
275

    
276
static int (multiplayer_host)(void) {
277
    int r;
278

    
279
    nctp_dump();
280
    nctp_set_processor(multiplayer_process);
281

    
282
    ent_set_scale(DEFAULT_SCALE);
283
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
284

    
285
    list_t *shooter_list = list_ctor();
286

    
287
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
288
    gunner_set_spawn(shooter1, 75, 75);
289

    
290
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 2); if(shooter2 == NULL) printf("Failed to get shooter2\n");
291
    gunner_set_spawn(shooter2, 975, 75);
292

    
293
    list_insert(shooter_list, list_end(shooter_list), shooter1);
294
    list_insert(shooter_list, list_end(shooter_list), shooter2);
295

    
296
    do {
297
        get_random_spawn(map1, shooter1, shooter_list);
298
        get_random_spawn(map1, shooter2, shooter_list);
299
    } while (distance_gunners(shooter1, shooter2) < 500);
300

    
301
    host_info = host_info_ctor(shooter1, shooter2);
302
    remote_info = remote_info_ctor();
303
    bullet_info = bullet_info_ctor();
304

    
305
    list_t *bullet_list  = list_ctor();
306

    
307
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
308
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
309

    
310
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
311
    uint8_t last_lb = 0;
312
    struct packet pp;
313
    keys_t *keys = get_key_presses();
314
    /// loop stuff
315
    uint64_t int_vector = 0;
316
    int good = true;
317
    int state = 0; // -1 for remote win, 0 for draw, 1 for host win
318
    list_node_t *p1, *p2; // player states
319
    int state_1, state_2;
320
    while (good) {
321
        if ((r = get_interrupts_vector(&int_vector))) return r;
322
        uint32_t n = 1;
323
        for (uint8_t i = 0; i < 32; i++, n <<= 1) {
324
            if (int_vector & n) {
325
                interrupt_handler(i);
326
                switch (i) {
327
                    case TIMER0_IRQ:
328
                    if (timer_get_no_interrupts() % 60 == 0) timer_update(in_game_timer);
329

    
330
                    update_movement(map1, shooter1, keys, shooter_list);
331
                    update_movement(map1, shooter2, &(remote_info->remote_keys_pressed), shooter_list);
332

    
333
                    update_game_state(map1, shooter_list, bullet_list);
334

    
335
                    p1 = list_find(shooter_list, shooter1);
336
                    p2 = list_find(shooter_list, shooter2);
337

    
338
                    if ((state_1 = (p1 == list_end(shooter_list))) || (state_2 = (p2 == list_end(shooter_list)))) {
339
                        state = state_1 - state_2;
340
                        good = false;
341
                        break;
342
                    }
343

    
344
                    double angle = get_mouse_angle(shooter1);
345
                    gunner_set_angle(shooter1, angle - M_PI_2);
346

    
347
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
348
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
349

    
350
                    gunner_set_angle(shooter2, remote_info->remote_angle);
351

    
352
                    build_host_structure(host_info, shooter1, shooter2, bullet_list);
353

    
354

    
355

    
356
                    hltp_send_host_info(host_info);
357

    
358
                    graph_clear_screen();
359
                    map_draw   (map1);
360
                    bullet_draw_list(bullet_list);
361
                    gunner_draw_list(shooter_list);
362

    
363
                    text_draw(in_game_timer->text);
364

    
365
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
366
                    sprite_draw(sp_crosshair);
367
                    graph_draw();
368

    
369
                    break;
370
                    case KBC_IRQ:
371
                    if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) {
372
                        good = false;
373
                    }
374
                    break;
375
                    case MOUSE_IRQ:
376
                    if (mouse_get_counter_mouse_ih() >= 3) {
377
                        mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp);
378
                        update_mouse(&pp);
379
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
380
                        shoot_bullet(shooter1, bullet_list, bsp_bullet);
381
                        last_lb = keys->lb_pressed;
382
                        mouse_set_counter_mouse_ih(0);
383
                    }
384
                    break;
385

    
386
                    case COM1_IRQ:
387
                        nctp_ih();
388
                        if (bullet_info->new_bullet) {
389
                            shoot_bullet(shooter2, bullet_list, bsp_bullet);
390
                            bullet_info->new_bullet = false;
391
                        }
392
                        break;
393
                }
394
            }
395
        }
396
    }
397

    
398
    while(list_size(shooter_list) > 0){
399
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
400
        gunner_dtor(p);
401
    }
402
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
403

    
404
    while(list_size(bullet_list) > 0){
405
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
406
        bullet_dtor(p);
407
    }
408
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
409

    
410
    host_info_dtor(host_info);
411
    remote_info_dtor(remote_info);
412
    bullet_info_dtor(bullet_info);
413

    
414
    nctp_set_processor(NULL);
415

    
416
    timer_dtor(in_game_timer); in_game_timer = NULL;
417

    
418
    return 0;
419
}
420
static int (multiplayer_remote)(void) {
421
    int r;
422

    
423
    nctp_dump();
424
    nctp_set_processor(multiplayer_process);
425

    
426
    ent_set_scale(DEFAULT_SCALE);
427
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
428

    
429
    list_t *shooter_list = list_ctor();
430

    
431
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 2); if(shooter1 == NULL) printf("Failed to get shooter1\n");
432
    gunner_set_spawn(shooter1, 75, 75);
433

    
434
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 1); if(shooter2 == NULL) printf("Failed to get shooter2\n");
435
    gunner_set_spawn(shooter2, 975, 75);
436

    
437
    list_insert(shooter_list, list_end(shooter_list), shooter1);
438
    list_insert(shooter_list, list_end(shooter_list), shooter2);
439

    
440
    host_info = host_info_ctor(shooter2, shooter1);
441
    remote_info = remote_info_ctor();
442
    bullet_info = bullet_info_ctor();
443

    
444
    list_t *bullet_list  = list_ctor();
445

    
446
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
447
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
448

    
449
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
450
    uint8_t last_lb = 0;
451
    struct packet pp;
452
    keys_t *keys = get_key_presses();
453

    
454
    /// loop stuff
455
    uint64_t int_vector = 0;
456
    int good = true;
457
    while (good) {
458
        if ((r = get_interrupts_vector(&int_vector))) return r;
459
        uint32_t n = 1;
460
        for (uint8_t i = 0; i < 32; i++, n <<= 1) {
461
            if (int_vector & n) {
462
                interrupt_handler(i);
463
                switch (i) {
464
                    case TIMER0_IRQ:
465
                    if (timer_get_no_interrupts() % 60 == 0) timer_update(in_game_timer);
466

    
467
                    double angle = get_mouse_angle(shooter1) - M_PI_2;
468

    
469
                    build_remote_structure(remote_info, keys, angle);
470

    
471
                    //hltp_send_remote_info(remote_info);
472

    
473
                    gunner_set_pos(shooter1, (double)host_info->remote_x, (double)host_info->remote_y);
474
                    gunner_set_angle(shooter1, (double)host_info->remote_angle);
475
                    gunner_set_health(shooter1, (double)host_info->remote_health);
476
                    gunner_set_curr_health(shooter1, (double)host_info->remote_current_health);
477

    
478
                    gunner_set_pos(shooter2, (double)host_info->host_x, (double)host_info->host_y);
479
                    gunner_set_angle(shooter2, (double)host_info->host_angle);
480
                    gunner_set_health(shooter2, (double)host_info->host_health);
481
                    gunner_set_curr_health(shooter2, (double)host_info->host_current_health);
482

    
483
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
484
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
485

    
486
                    for (size_t j = 0; j < host_info->no_bullets; j++) {
487
                        if (host_info->bullets_shooter[j]) { // remote
488
                            bullet_t *bullet = bullet_ctor(shooter1, bsp_bullet, (double)host_info->bullets_x[j], (double)host_info->bullets_y[j], (double)host_info->bullets_vx[j], (double)host_info->bullets_vy[j]);
489
                            list_insert(bullet_list, list_end(bullet_list), bullet);
490
                        } else { // host
491
                            bullet_t *bullet = bullet_ctor(shooter2, bsp_bullet, (double)host_info->bullets_x[j], (double)host_info->bullets_y[j], (double)host_info->bullets_vx[j], (double)host_info->bullets_vy[j]);
492
                            list_insert(bullet_list, list_end(bullet_list), bullet);
493
                        }
494
                    }
495

    
496
                    graph_clear_screen();
497
                    map_draw   (map1);
498
                    bullet_draw_list(bullet_list);
499
                    gunner_draw_list(shooter_list);
500

    
501
                    text_draw(in_game_timer->text);
502

    
503
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
504
                    sprite_draw(sp_crosshair);
505
                    graph_draw();
506

    
507
                    while(list_size(bullet_list) > 0){
508
                        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
509
                        bullet_dtor(p);
510
                    }
511

    
512
                    break;
513
                    case KBC_IRQ:
514
                    if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) {
515
                        good = false;
516
                    }
517
                    break;
518
                    case MOUSE_IRQ:
519
                    if (mouse_get_counter_mouse_ih() >= 3) {
520
                        mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp);
521
                        update_mouse(&pp);
522
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed) {
523
                            bullet_info->new_bullet = true;
524
                            //hltp_send_bullet_info(bullet_info);
525
                        }
526
                        last_lb = keys->lb_pressed;
527
                        mouse_set_counter_mouse_ih(0);
528
                    }
529
                    break;
530

    
531
                    case COM1_IRQ: nctp_ih(); break;
532
                }
533
            }
534
        }
535
    }
536

    
537
    while(list_size(shooter_list) > 0){
538
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
539
        gunner_dtor(p);
540
    }
541
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
542

    
543
    while(list_size(bullet_list) > 0){
544
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
545
        bullet_dtor(p);
546
    }
547
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
548

    
549
    host_info_dtor(host_info);
550
    remote_info_dtor(remote_info);
551
    bullet_info_dtor(bullet_info);
552

    
553
    nctp_set_processor(NULL);
554

    
555
    timer_dtor(in_game_timer); in_game_timer = NULL;
556

    
557
    return 0;
558
}
559

    
560
static int (campaign)(void);
561
static int (zombies)(void);
562
static int (singleplayer)(void) {
563

    
564
    int r;
565

    
566
    menu_t *main_menu = menu_ctor(font_get_default());
567
    menu_add_item(main_menu, "Campaign");
568
    menu_add_item(main_menu, "Zombies");
569
    menu_add_item(main_menu, "Back");
570

    
571
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
572
    uint8_t last_lb = 0;
573
    struct packet pp;
574
    keys_t *keys = get_key_presses();
575

    
576
    /// loop stuff
577
    int click = 0;
578
    uint64_t int_vector = 0;
579
    int good = true;
580
    while (good) {
581
        /* Get a request message. */
582
        if((r = get_interrupts_vector(&int_vector))) return r;
583
        uint32_t n = 1;
584
        for (uint8_t i = 0; i < 32; i++, n <<= 1) {
585
            if (int_vector & n) {
586
                interrupt_handler(i);
587
                switch (i) {
588
                    case TIMER0_IRQ:
589

    
590
                    graph_clear_screen();
591
                    switch(menu_update_state(main_menu, click)){
592
                        case -1: break;
593
                        case  0: campaign(); break;
594
                        case  1: zombies(); break;
595
                        case  2: good = false; break;
596
                    }
597
                    menu_draw(main_menu);
598

    
599
                    click = 0;
600

    
601
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
602
                    sprite_draw(sp_crosshair);
603
                    graph_draw();
604

    
605
                    break;
606
                    case KBC_IRQ:
607
                    if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) good = false;
608
                    case MOUSE_IRQ:
609
                    if (mouse_get_counter_mouse_ih() >= 3) {
610
                        mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp);
611
                        update_mouse(&pp);
612
                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
613
                        last_lb = keys->lb_pressed;
614
                        mouse_set_counter_mouse_ih(0);
615
                    }
616
                    break;
617
                    case COM1_IRQ: nctp_ih(); break;
618
                }
619
            }
620
        }
621
    }
622

    
623
    return 0;
624
}
625

    
626
static int (campaign)(void){
627

    
628
    int r;
629

    
630
    ent_set_scale(DEFAULT_SCALE);
631
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
632

    
633
    list_t *shooter_list = list_ctor();
634

    
635
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
636
    gunner_set_spawn(shooter1, 75, 75);
637
    gunner_set_pos(shooter1, 75, 75);
638

    
639
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing, GUNNER_PLAYER, 2);
640
    gunner_set_spawn(shooter2, 975, 75);
641
    gunner_set_pos(shooter2, 775, 75);
642

    
643
    list_insert(shooter_list, list_end(shooter_list), shooter1);
644
    list_insert(shooter_list, list_end(shooter_list), shooter2);
645

    
646
    list_t *bullet_list  = list_ctor();
647

    
648
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
649
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
650

    
651
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
652
    uint8_t last_lb = 0;
653
    struct packet pp;
654
    keys_t *keys = get_key_presses();
655

    
656
    /// loop stuff
657
    uint64_t int_vector = 0;
658
    int good = true;
659
    while (good) {
660
        /* Get a request message. */
661
        if((r = get_interrupts_vector(&int_vector))) return r;
662
        uint32_t n = 1;
663
        for (uint8_t i = 0; i < 32; i++, n <<= 1) {
664
            if(!good) break;
665
            if (int_vector & n) {
666
                interrupt_handler(i);
667
                switch (i) {
668
                    case TIMER0_IRQ:
669

    
670
                    if (timer_get_no_interrupts() % 60 == 0) timer_update(in_game_timer);
671
                    update_movement(map1, shooter1, keys, shooter_list);
672

    
673
                    update_game_state(map1, shooter_list, bullet_list);
674

    
675
                    //update_scale();
676
                    double angle = get_mouse_angle(shooter1);
677
                    gunner_set_angle(shooter1, angle - M_PI_2);
678

    
679
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
680
                    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
681

    
682
                    graph_clear_screen();
683
                    map_draw   (map1);
684
                    bullet_draw_list(bullet_list);
685
                    gunner_draw_list(shooter_list);
686

    
687
                    text_draw(in_game_timer->text);
688

    
689
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
690
                    sprite_draw(sp_crosshair);
691
                    graph_draw();
692

    
693
                    break;
694
                    case KBC_IRQ:
695
                    if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) {
696
                        good = false;
697
                    }
698
                    break;
699
                    case MOUSE_IRQ:
700
                    if (mouse_get_counter_mouse_ih() >= 3) {
701
                        mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp);
702
                        update_mouse(&pp);
703
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
704
                        shoot_bullet(shooter1, bullet_list, bsp_bullet);
705
                        last_lb = keys->lb_pressed;
706
                        mouse_set_counter_mouse_ih(0);
707

    
708
                    }
709
                    break;
710
                    case COM1_IRQ: nctp_ih(); break;
711
                }
712
            }
713
        }
714
    }
715

    
716
    while(list_size(shooter_list) > 0){
717
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
718
        gunner_dtor(p);
719
    }
720
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
721

    
722
    while(list_size(bullet_list) > 0){
723
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
724
        bullet_dtor(p);
725
    }
726
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
727

    
728
    timer_dtor(in_game_timer); in_game_timer = NULL;
729

    
730
    return SUCCESS;
731
}
732

    
733
#define ZOMBIES_NUM             5
734
#define ZOMBIE_HEALTH_FACTOR    1.1
735
static int (zombies)(void){
736

    
737
    int r;
738

    
739
    ent_set_scale(DEFAULT_SCALE);
740
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
741

    
742
    list_t *shooter_list = list_ctor();
743

    
744
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
745
    gunner_set_spawn(shooter1, 980, 790);
746
    gunner_set_pos(shooter1, 980, 790);
747

    
748
    list_insert(shooter_list, list_end(shooter_list), shooter1);
749

    
750
    list_t *bullet_list  = list_ctor();
751

    
752
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
753
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
754

    
755
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
756
    uint8_t last_lb = 0;
757
    struct packet pp;
758
    keys_t *keys = get_key_presses();
759

    
760
    /// loop stuff
761
    uint64_t int_vector = 0;
762
    int good = true;
763
    int dead = false;
764

    
765
    int health = 50;
766

    
767
    map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1));
768

    
769
    while (good && !dead) {
770
        /* Get a request message. */
771
        if((r = get_interrupts_vector(&int_vector))) return r;
772
        uint32_t n = 1;
773
        for (uint8_t i = 0; i < 32; i++, n <<= 1) {
774
            if(!good || dead) break;
775
            if (int_vector & n) {
776
                interrupt_handler(i);
777
                switch (i) {
778
                    case TIMER0_IRQ:
779
                    if (timer_get_no_interrupts() % 60 == 0) timer_update(in_game_timer);
780
                    if (timer_get_no_interrupts() %  6 == 0){
781
                        map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1));
782
                    }
783

    
784
                    update_movement(map1, shooter1, keys, shooter_list);
785

    
786
                    update_game_state(map1, shooter_list, bullet_list);
787

    
788
                    if(list_find(shooter_list, shooter1) == list_end(shooter_list)){
789
                        good = false;
790
                        dead = true;
791
                        break;
792
                    }
793

    
794
                    double angle = get_mouse_angle(shooter1);
795
                    gunner_set_angle(shooter1, angle - M_PI_2);
796

    
797
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
798
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
799

    
800
                    while(list_size(shooter_list) < ZOMBIES_NUM+1){
801
                        gunner_t *zombie = gunner_ctor(bsp_zombie, bsp_nothing, GUNNER_MELEE | GUNNER_FOLLOW, 3);
802
                        gunner_set_health(zombie, health);
803
                        gunner_set_curr_health(zombie, health);
804
                        health *= ZOMBIE_HEALTH_FACTOR;
805
                        get_random_spawn(map1, zombie, shooter_list);
806
                        list_push_back(shooter_list, zombie);
807
                    }
808

    
809
                    graph_clear_screen();
810
                    map_draw   (map1);
811
                    bullet_draw_list(bullet_list);
812
                    gunner_draw_list(shooter_list);
813

    
814
                    text_draw(in_game_timer->text);
815

    
816
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
817
                    sprite_draw(sp_crosshair);
818
                    graph_draw();
819

    
820
                    break;
821
                    case KBC_IRQ:
822
                    if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) {
823
                        good = false;
824
                    }
825
                    break;
826
                    case MOUSE_IRQ:
827
                    if (mouse_get_counter_mouse_ih() >= 3) {
828
                        mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp);
829
                        update_mouse(&pp);
830
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
831
                        shoot_bullet(shooter1, bullet_list, bsp_bullet);
832
                        last_lb = keys->lb_pressed;
833
                        mouse_set_counter_mouse_ih(0);
834

    
835
                    }
836
                    break;
837
                    case COM1_IRQ: nctp_ih(); break;
838
                }
839
            }
840
        }
841
    }
842

    
843
    while(list_size(shooter_list) > 0){
844
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
845
        gunner_dtor(p);
846
    }
847
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
848

    
849
    while(list_size(bullet_list) > 0){
850
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
851
        bullet_dtor(p);
852
    }
853
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
854

    
855
    if(dead){
856
        printf("YOU DIED\n");
857
    }
858

    
859
    timer_dtor(in_game_timer); in_game_timer = NULL;
860

    
861
    return SUCCESS;
862
}
863

    
864
#define CHAT_MAX_SIZE   75
865
#define CHAT_MAX_NUM    19
866
static text_t      *t_text[CHAT_MAX_NUM] = {NULL};
867
static rectangle_t *r_text               =  NULL;
868
static void chat_process(const uint8_t *p, const size_t sz){
869
    char buffer2[CHAT_MAX_NUM+3];
870
    void *dest = NULL;
871
    hltp_type tp = hltp_interpret(p, sz, &dest);
872
    switch(tp){
873
        case hltp_type_string:
874
        strcpy(buffer2, dest);
875
        strncat(buffer2, " <", 2);
876
        for(size_t i = CHAT_MAX_NUM-1; i; --i)
877
        text_set_string(t_text[i], text_get_string(t_text[i-1]));
878
        text_set_string(t_text[0], buffer2);
879
        for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
880
            if(text_get_string(t_text[i])[0] == '>'){
881
                text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
882
                text_set_halign(t_text[i], text_halign_left);
883
            }else{
884
                text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
885
                text_set_halign(t_text[i], text_halign_right);
886
            }
887
        }
888
        break;
889
        case hltp_type_invalid: break;
890
        case hltp_type_bullet : break;
891
        case hltp_type_host   : break;
892
        case hltp_type_remote : break;
893
    }
894
}
895
static int (chat)(void){
896
    int r;
897

    
898
    nctp_dump();
899
    nctp_set_processor(chat_process);
900

    
901
    struct packet pp;
902

    
903
    char buffer[CHAT_MAX_SIZE] = "";
904
    rectangle_t *r_buffer = NULL; {
905
        r_buffer = rectangle_ctor(0,0,900,70);
906
        rectangle_set_pos(r_buffer, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
907
        (int16_t)(graph_get_YRes()*0.87-rectangle_get_h(r_buffer)/2));
908
        rectangle_set_fill_color   (r_buffer, GRAPH_BLACK);
909
        rectangle_set_outline_width(r_buffer, 2);
910
        rectangle_set_outline_color(r_buffer, GRAPH_WHITE);
911
        rectangle_set_fill_trans(r_buffer, GRAPH_TRANSPARENT);
912
    }
913
    text_t      *t_buffer = NULL; {
914
        t_buffer = text_ctor(font_get_default(), "");
915
        text_set_pos(t_buffer, rectangle_get_x(r_buffer)+50,
916
        rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)/2);
917
        text_set_halign(t_buffer, text_halign_left);
918
        text_set_valign(t_buffer, text_valign_center);
919
        text_set_color (t_buffer, TEXT_COLOR);
920
    }
921
    text_t      *t_size   = NULL; {
922
        t_size = text_ctor(font_get_default(), "");
923
        text_set_pos(t_size, rectangle_get_x(r_buffer)+rectangle_get_w(r_buffer)-5,
924
        rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)-5);
925
        text_set_halign(t_size, text_halign_right);
926
        text_set_valign(t_size, text_valign_bottom);
927
        text_set_color (t_size, TEXT_COLOR);
928
        text_set_size  (t_size, 18);
929
        char buffer2[20];
930
        sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
931
        text_set_string(t_size, buffer2);
932
    }
933

    
934
    /** r_text */ {
935
    r_text = rectangle_ctor(0,0,900,550);
936
    rectangle_set_pos(r_text, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
937
    (int16_t)(graph_get_YRes()*0.09));
938
    rectangle_set_fill_color   (r_text, GRAPH_BLACK);
939
    rectangle_set_outline_width(r_text, 2);
940
    rectangle_set_outline_color(r_text, GRAPH_WHITE);
941
    rectangle_set_fill_trans(r_text, GRAPH_TRANSPARENT);
942
    }
943
    /** t_text */ {
944
    for(uint16_t i = 0; i < CHAT_MAX_NUM; ++i){
945
        t_text[i] = text_ctor(font_get_default(), " ");
946
        text_set_pos(t_text[i], rectangle_get_x(r_text)+50,
947
        rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-25*i);
948
        text_set_halign(t_text[i], text_halign_left);
949
        text_set_valign(t_text[i], text_valign_bottom);
950
        text_set_color (t_text[i], TEXT_COLOR);
951
    }
952
}
953

    
954
/// loop stuff
955
uint64_t int_vector = 0;
956
int good = true;
957
while (good) {
958
    /* Get a request message. */
959
    if((r = get_interrupts_vector(&int_vector))) return r;
960
    uint32_t n = 1;
961
    for (uint8_t i = 0; i < 32; i++, n <<= 1) {
962
        if (int_vector & n) {
963
            interrupt_handler(i);
964
            switch (i) {
965
                case TIMER0_IRQ:
966
                graph_clear_screen();
967
                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
968

    
969
                rectangle_draw(r_buffer);
970
                text_draw(t_buffer);
971
                text_draw(t_size);
972

    
973
                rectangle_draw(r_text);
974
                for(size_t j = 0; j < CHAT_MAX_NUM; ++j) text_draw(t_text[j]);
975

    
976
                sprite_draw(sp_crosshair);
977
                graph_draw();
978
                break;
979
                case KBC_IRQ:
980
                if      (keyboard_get_scancode()[0] == ESC_BREAK_CODE) good = false;
981
                else if (keyboard_get_scancode()[0] == ENTER_MAKE_CODE) {
982
                    hltp_send_string(buffer);
983
                    char buffer2[CHAT_MAX_SIZE+3] = "> ";
984
                    strncat(buffer2, buffer, strlen(buffer));
985
                    for(size_t j = CHAT_MAX_NUM-1; j; --j) text_set_string(t_text[i], text_get_string(t_text[i-1]));
986
                    text_set_string(t_text[0], buffer2);
987
                    for(size_t j = 0; j < CHAT_MAX_NUM; ++j){
988
                        if(text_get_string(t_text[j])[0] == '>'){
989
                            text_set_pos(t_text[j], rectangle_get_x(r_text)+50, text_get_y(t_text[j]));
990
                            text_set_halign(t_text[j], text_halign_left);
991
                        }else{
992
                            text_set_pos(t_text[j], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[j]));
993
                            text_set_halign(t_text[j], text_halign_right);
994
                        }
995
                    }
996
                    buffer[0] = '\0';
997
                } else if(keyboard_get_scancode()[0] == BACKSPACE_MAKE_CODE){
998
                    buffer[strlen(buffer)-1] = '\0';
999
                } else {
1000
                    char c = map_makecode(keyboard_get_scancode()[0]);
1001
                    if (c == ERROR_CODE) break;
1002
                    if(strlen(buffer) < CHAT_MAX_SIZE) strncat(buffer, &c, 1);
1003
                    else                               printf("Char limit exceeded\n");
1004
                }
1005
                text_set_string(t_buffer, buffer);
1006
                char buffer2[20];
1007
                sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
1008
                text_set_string(t_size, buffer2);
1009
                case MOUSE_IRQ:
1010
                if (mouse_get_counter_mouse_ih() >= 3) {
1011
                    mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp);
1012
                    update_mouse(&pp);
1013
                    mouse_set_counter_mouse_ih(0);
1014
                }
1015
                break;
1016
                case COM1_IRQ: nctp_ih(); break;
1017
            }
1018
        }
1019
    }
1020
}
1021

    
1022
rectangle_dtor(r_buffer);
1023
text_dtor     (t_buffer);
1024

    
1025
rectangle_dtor(r_text);
1026
for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_dtor(t_text[i]);
1027

    
1028
nctp_set_processor(NULL);
1029

    
1030
return SUCCESS;
1031
}