Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 328

History | View | Annotate | Download (37.3 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
    (void)argc; (void)argv;
66

    
67
    int r;
68

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

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

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

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

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

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

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

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

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

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

    
139
                    click = 0;
140

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

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

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

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

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

    
184
    return 0;
185
}
186

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

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

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

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

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

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

    
249
                    click = 0;
250

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

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

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

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

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

    
284
    list_t *shooter_list = list_ctor();
285

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

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

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

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

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

    
304
    list_t *bullet_list  = list_ctor();
305

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

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

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

    
332
                    update_game_state(map1, shooter_list, bullet_list);
333

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

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

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

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

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

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

    
353

    
354

    
355
                    hltp_send_host_info(host_info);
356

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

    
362
                    text_draw(in_game_timer->text);
363

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

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

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

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

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

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

    
413
    timer_dtor(in_game_timer); in_game_timer = NULL;
414

    
415
    return 0;
416
}
417
static int (multiplayer_remote)(void) {
418
    int r;
419

    
420
    nctp_dump();
421
    nctp_set_processor(multiplayer_process);
422

    
423
    ent_set_scale(DEFAULT_SCALE);
424
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
425

    
426
    list_t *shooter_list = list_ctor();
427

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

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

    
434
    list_insert(shooter_list, list_end(shooter_list), shooter1);
435
    list_insert(shooter_list, list_end(shooter_list), shooter2);
436

    
437
    host_info = host_info_ctor(shooter2, shooter1);
438
    remote_info = remote_info_ctor();
439
    bullet_info = bullet_info_ctor();
440

    
441
    list_t *bullet_list  = list_ctor();
442

    
443
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
444
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
445

    
446
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
447
    uint8_t last_lb = 0;
448
    struct packet pp;
449
    keys_t *keys = get_key_presses();
450

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

    
464
                    double angle = get_mouse_angle(shooter1) - M_PI_2;
465

    
466
                    build_remote_structure(remote_info, keys, angle);
467

    
468
                    //hltp_send_remote_info(remote_info);
469

    
470
                    gunner_set_pos(shooter1, host_info->remote_x, host_info->remote_y);
471
                    gunner_set_angle(shooter1, host_info->remote_angle);
472
                    gunner_set_health(shooter1, host_info->remote_health);
473
                    gunner_set_curr_health(shooter1, host_info->remote_current_health);
474

    
475
                    gunner_set_pos(shooter2, host_info->host_x, host_info->host_y);
476
                    gunner_set_angle(shooter2, host_info->host_angle);
477
                    gunner_set_health(shooter2, host_info->host_health);
478
                    gunner_set_curr_health(shooter2, host_info->host_current_health);
479

    
480
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
481
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
482

    
483
                    for (size_t j = 0; j < host_info->no_bullets; j++) {
484
                        if (host_info->bullets_shooter[j]) { // remote
485
                            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]);
486
                            list_insert(bullet_list, list_end(bullet_list), bullet);
487
                        } else { // host
488
                            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]);
489
                            list_insert(bullet_list, list_end(bullet_list), bullet);
490
                        }
491
                    }
492

    
493
                    graph_clear_screen();
494
                    map_draw   (map1);
495
                    bullet_draw_list(bullet_list);
496
                    gunner_draw_list(shooter_list);
497

    
498
                    text_draw(in_game_timer->text);
499

    
500
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
501
                    sprite_draw(sp_crosshair);
502
                    graph_draw();
503

    
504
                    while(list_size(bullet_list) > 0){
505
                        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
506
                        bullet_dtor(p);
507
                    }
508

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

    
528
                    case COM1_IRQ: nctp_ih(); break;
529
                }
530
            }
531
        }
532
    }
533

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

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

    
546
    host_info_dtor(host_info);
547
    remote_info_dtor(remote_info);
548
    bullet_info_dtor(bullet_info);
549

    
550
    timer_dtor(in_game_timer); in_game_timer = NULL;
551

    
552
    return 0;
553
}
554

    
555
static int (campaign)(void);
556
static int (zombies)(void);
557
static int (singleplayer)(void) {
558

    
559
    int r;
560

    
561
    menu_t *main_menu = menu_ctor(font_get_default());
562
    menu_add_item(main_menu, "Campaign");
563
    menu_add_item(main_menu, "Zombies");
564
    menu_add_item(main_menu, "Back");
565

    
566
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
567
    uint8_t last_lb = 0;
568
    struct packet pp;
569
    keys_t *keys = get_key_presses();
570

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

    
585
                    graph_clear_screen();
586
                    switch(menu_update_state(main_menu, click)){
587
                        case -1: break;
588
                        case  0: campaign(); break;
589
                        case  1: zombies(); break;
590
                        case  2: good = false; break;
591
                    }
592
                    menu_draw(main_menu);
593

    
594
                    click = 0;
595

    
596
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
597
                    sprite_draw(sp_crosshair);
598
                    graph_draw();
599

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

    
618
    return 0;
619
}
620

    
621
static int (campaign)(void){
622

    
623
    int r;
624

    
625
    ent_set_scale(DEFAULT_SCALE);
626
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
627

    
628
    list_t *shooter_list = list_ctor();
629

    
630
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
631
    gunner_set_spawn(shooter1, 75, 75);
632
    gunner_set_pos(shooter1, 75, 75);
633

    
634
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing, GUNNER_PLAYER, 2);
635
    gunner_set_spawn(shooter2, 975, 75);
636
    gunner_set_pos(shooter2, 775, 75);
637

    
638
    list_insert(shooter_list, list_end(shooter_list), shooter1);
639
    list_insert(shooter_list, list_end(shooter_list), shooter2);
640

    
641
    list_t *bullet_list  = list_ctor();
642

    
643
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
644
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
645

    
646
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
647
    uint8_t last_lb = 0;
648
    struct packet pp;
649
    keys_t *keys = get_key_presses();
650

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

    
665
                    if (timer_get_no_interrupts() % 60 == 0) timer_update(in_game_timer);
666
                    update_movement(map1, shooter1, keys, shooter_list);
667

    
668
                    update_game_state(map1, shooter_list, bullet_list);
669

    
670
                    //update_scale();
671
                    double angle = get_mouse_angle(shooter1);
672
                    gunner_set_angle(shooter1, angle - M_PI_2);
673

    
674
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
675
                    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
676

    
677
                    graph_clear_screen();
678
                    map_draw   (map1);
679
                    bullet_draw_list(bullet_list);
680
                    gunner_draw_list(shooter_list);
681

    
682
                    text_draw(in_game_timer->text);
683

    
684
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
685
                    sprite_draw(sp_crosshair);
686
                    graph_draw();
687

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

    
703
                    }
704
                    break;
705
                    case COM1_IRQ: nctp_ih(); break;
706
                }
707
            }
708
        }
709
    }
710

    
711
    while(list_size(shooter_list) > 0){
712
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
713
        gunner_dtor(p);
714
    }
715
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
716

    
717
    while(list_size(bullet_list) > 0){
718
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
719
        bullet_dtor(p);
720
    }
721
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
722

    
723
    timer_dtor(in_game_timer); in_game_timer = NULL;
724

    
725
    return SUCCESS;
726
}
727

    
728
#define ZOMBIES_NUM             5
729
#define ZOMBIE_HEALTH_FACTOR    1.1
730
static int (zombies)(void){
731

    
732
    int r;
733

    
734
    ent_set_scale(DEFAULT_SCALE);
735
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
736

    
737
    list_t *shooter_list = list_ctor();
738

    
739
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
740
    gunner_set_spawn(shooter1, 980, 790);
741
    gunner_set_pos(shooter1, 980, 790);
742

    
743
    list_insert(shooter_list, list_end(shooter_list), shooter1);
744

    
745
    list_t *bullet_list  = list_ctor();
746

    
747
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
748
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
749

    
750
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
751
    uint8_t last_lb = 0;
752
    struct packet pp;
753
    keys_t *keys = get_key_presses();
754

    
755
    /// loop stuff
756
    uint64_t int_vector = 0;
757
    int good = true;
758
    int dead = false;
759

    
760
    int health = 50;
761

    
762
    map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1));
763

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

    
779
                    update_movement(map1, shooter1, keys, shooter_list);
780

    
781
                    update_game_state(map1, shooter_list, bullet_list);
782

    
783
                    if(list_find(shooter_list, shooter1) == list_end(shooter_list)){
784
                        good = false;
785
                        dead = true;
786
                        break;
787
                    }
788

    
789
                    double angle = get_mouse_angle(shooter1);
790
                    gunner_set_angle(shooter1, angle - M_PI_2);
791

    
792
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
793
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
794

    
795
                    while(list_size(shooter_list) < ZOMBIES_NUM+1){
796
                        gunner_t *zombie = gunner_ctor(bsp_zombie, bsp_nothing, GUNNER_MELEE | GUNNER_FOLLOW, 3);
797
                        gunner_set_health(zombie, health);
798
                        gunner_set_curr_health(zombie, health);
799
                        health *= ZOMBIE_HEALTH_FACTOR;
800
                        get_random_spawn(map1, zombie, shooter_list);
801
                        list_push_back(shooter_list, zombie);
802
                    }
803

    
804
                    graph_clear_screen();
805
                    map_draw   (map1);
806
                    bullet_draw_list(bullet_list);
807
                    gunner_draw_list(shooter_list);
808

    
809
                    text_draw(in_game_timer->text);
810

    
811
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
812
                    sprite_draw(sp_crosshair);
813
                    graph_draw();
814

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

    
830
                    }
831
                    break;
832
                    case COM1_IRQ: nctp_ih(); break;
833
                }
834
            }
835
        }
836
    }
837

    
838
    while(list_size(shooter_list) > 0){
839
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
840
        gunner_dtor(p);
841
    }
842
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
843

    
844
    while(list_size(bullet_list) > 0){
845
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
846
        bullet_dtor(p);
847
    }
848
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
849

    
850
    if(dead){
851
        printf("YOU DIED\n");
852
    }
853

    
854
    timer_dtor(in_game_timer); in_game_timer = NULL;
855

    
856
    return SUCCESS;
857
}
858

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

    
893
    nctp_dump();
894
    nctp_set_processor(chat_process);
895

    
896
    struct packet pp;
897

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

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

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

    
964
                rectangle_draw(r_buffer);
965
                text_draw(t_buffer);
966
                text_draw(t_size);
967

    
968
                rectangle_draw(r_text);
969
                for(size_t j = 0; j < CHAT_MAX_NUM; ++j) text_draw(t_text[j]);
970

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

    
1017
rectangle_dtor(r_buffer);
1018
text_dtor     (t_buffer);
1019

    
1020
rectangle_dtor(r_text);
1021
for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_dtor(t_text[i]);
1022

    
1023
nctp_set_processor(NULL);
1024

    
1025
return SUCCESS;
1026
}