Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 327

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

    
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

    
353

    
354
                    hltp_send_host_info(host_info);
355

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

    
361
                    text_draw(in_game_timer->text);
362

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

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

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

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

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

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

    
412
    timer_dtor(in_game_timer); in_game_timer = NULL;
413

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

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

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

    
425
    list_t *shooter_list = list_ctor();
426

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

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

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

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

    
440
    list_t *bullet_list  = list_ctor();
441

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

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

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

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

    
465
                    build_remote_structure(remote_info, keys, angle);
466

    
467
                    //hltp_send_remote_info(remote_info);
468

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

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

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

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

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

    
497
                    text_draw(in_game_timer->text);
498

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

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

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

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

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

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

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

    
549
    timer_dtor(in_game_timer); in_game_timer = NULL;
550

    
551
    return 0;
552
}
553

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

    
558
    int r;
559

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

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

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

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

    
593
                    click = 0;
594

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

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

    
617
    return 0;
618
}
619

    
620
static int (campaign)(void){
621

    
622
    int r;
623

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

    
627
    list_t *shooter_list = list_ctor();
628

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

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

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

    
640
    list_t *bullet_list  = list_ctor();
641

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

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

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

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

    
667
                    update_game_state(map1, shooter_list, bullet_list);
668

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

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

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

    
681
                    text_draw(in_game_timer->text);
682

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

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

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

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

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

    
722
    timer_dtor(in_game_timer); in_game_timer = NULL;
723

    
724
    return SUCCESS;
725
}
726

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

    
731
    int r;
732

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

    
736
    list_t *shooter_list = list_ctor();
737

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

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

    
744
    list_t *bullet_list  = list_ctor();
745

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

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

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

    
759
    int health = 50;
760

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

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

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

    
780
                    update_game_state(map1, shooter_list, bullet_list);
781

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

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

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

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

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

    
808
                    text_draw(in_game_timer->text);
809

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

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

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

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

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

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

    
853
    timer_dtor(in_game_timer); in_game_timer = NULL;
854

    
855
    return SUCCESS;
856
}
857

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

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

    
895
    struct packet pp;
896

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

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

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

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

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

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

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

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

    
1022
nctp_set_processor(NULL);
1023

    
1024
return SUCCESS;
1025
}