Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 336

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 "text_timer.h"
16
#include "rtc.h"
17
#include "hltp.h"
18
#include "interrupts_func.h"
19
#include "makecode_map.h"
20

    
21
#include "graph.h"
22
#include "sprite.h"
23
#include "rectangle.h"
24
#include "font.h"
25
#include "ent.h"
26

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

    
35
#include "errors.h"
36

    
37
#include "list.h"
38

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

    
41
    lcf_set_language("EN-US");
42

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

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

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

    
49
    lcf_cleanup();
50

    
51
    return 0;
52
}
53

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

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

    
69
    int r;
70

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

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

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

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

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

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

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

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

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

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

    
141
                    click = 0;
142

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

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

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

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

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

    
186
    return 0;
187
}
188

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

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

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

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

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

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

    
251
                    click = 0;
252

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

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

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

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

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

    
286
    list_t *shooter_list = list_ctor();
287

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

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

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

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

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

    
306
    list_t *bullet_list  = list_ctor();
307

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

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

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

    
334
                    update_game_state(map1, shooter_list, bullet_list);
335

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

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

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

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

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

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

    
355

    
356

    
357
                    hltp_send_host_info(host_info);
358

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

    
364
                    text_draw(in_game_timer->text);
365

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

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

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

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

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

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

    
415
    nctp_set_processor(NULL);
416

    
417
    timer_dtor(in_game_timer); in_game_timer = NULL;
418

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

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

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

    
430
    list_t *shooter_list = list_ctor();
431

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

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

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

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

    
445
    list_t *bullet_list  = list_ctor();
446

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

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

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

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

    
470
                    build_remote_structure(remote_info, keys, angle);
471

    
472
                    //hltp_send_remote_info(remote_info);
473

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

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

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

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

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

    
502
                    text_draw(in_game_timer->text);
503

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

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

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

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

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

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

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

    
554
    nctp_set_processor(NULL);
555

    
556
    timer_dtor(in_game_timer); in_game_timer = NULL;
557

    
558
    return 0;
559
}
560

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

    
565
    int r;
566

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

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

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

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

    
600
                    click = 0;
601

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

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

    
624
    return 0;
625
}
626

    
627
static int (campaign)(void){
628

    
629
    int r;
630

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

    
634
    list_t *shooter_list = list_ctor();
635

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

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

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

    
647
    list_t *bullet_list  = list_ctor();
648

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

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

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

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

    
674
                    update_game_state(map1, shooter_list, bullet_list);
675

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

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

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

    
688
                    text_draw(in_game_timer->text);
689

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

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

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

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

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

    
729
    timer_dtor(in_game_timer); in_game_timer = NULL;
730

    
731
    return SUCCESS;
732
}
733

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

    
738
    int r;
739

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

    
743
    list_t *shooter_list = list_ctor();
744

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

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

    
751
    list_t *bullet_list  = list_ctor();
752

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

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

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

    
766
    int health = 50;
767

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

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

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

    
787
                    update_game_state(map1, shooter_list, bullet_list);
788

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

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

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

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

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

    
815
                    text_draw(in_game_timer->text);
816

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

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

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

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

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

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

    
860
    timer_dtor(in_game_timer); in_game_timer = NULL;
861

    
862
    return SUCCESS;
863
}
864

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

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

    
902
    struct packet pp;
903

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

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

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

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

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

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

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

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

    
1029
nctp_set_processor(NULL);
1030

    
1031
return SUCCESS;
1032
}