Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 311

History | View | Annotate | Download (29.6 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
font_t               *consolas      = NULL;
53
basic_sprite_t       *bsp_crosshair = NULL;
54
basic_sprite_t       *bsp_shooter   = NULL;
55
basic_sprite_t       *bsp_zombie    = NULL;
56
basic_sprite_t       *bsp_pistol    = NULL;
57
basic_sprite_t       *bsp_nothing   = NULL;
58
basic_sprite_t       *bsp_bullet    = NULL;
59
map_t                *map1          = NULL;
60
sprite_t             *sp_crosshair  = NULL;
61

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

    
67
    int r;
68

    
69
    consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
70
    if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
71

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

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

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

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

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

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

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

    
116
    /// loop stuff
117
    int click = 0;
118
    uint32_t int_vector = 0;
119
    int good = true;
120
    while (good) {
121
        /* Get a request message. */
122
        if((r = get_interrupts_vector(&int_vector))) return r;
123
        for (uint32_t i = 0, n = 1; 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 ((scancode[0]) == ESC_BREAK_CODE) good = false;
148
                    case MOUSE_IRQ:
149
                    if (counter_mouse_ih >= 3) {
150
                        mouse_parse_packet(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
                        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_dtor              (consolas     ); consolas      = NULL;
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
host_info_t     *host   = NULL;
188
remote_info_t   *remote = 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);
196
            host = (host_info_t*)dest;
197
            break;
198
        case hltp_type_remote:
199
            remote_info_dtor(remote);
200
            remote = (remote_info_t*)dest;
201
            break;
202
        default: break;
203
    }
204
}
205
static int (multiplayer_host)(void);
206
static int (multiplayer_remote)(void);
207
static int (multiplayer)(void) {
208
    int r;
209

    
210
    menu_t *main_menu = menu_ctor(consolas);
211
    menu_add_item(main_menu, "Create");
212
    menu_add_item(main_menu, "Connect");
213
    menu_add_item(main_menu, "Back");
214

    
215
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
216
    uint8_t last_lb = 0;
217
    struct packet pp;
218
    keys_t *keys = get_key_presses();
219

    
220
    /// loop stuff
221
    int click = 0;
222
    uint32_t int_vector = 0;
223
    int good = true;
224
    while (good) {
225
        /* Get a request message. */
226
        if((r = get_interrupts_vector(&int_vector))) return r;
227
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
228
            if (int_vector & n) {
229
                interrupt_handler(i);
230
                switch (i) {
231
                    case TIMER0_IRQ:
232

    
233
                    graph_clear_screen();
234
                    switch(menu_update_state(main_menu, click)){
235
                        case -1: break;
236
                        case  0: multiplayer_host(); break;
237
                        case  1: multiplayer_remote(); break;
238
                        case  2: good = false; break;
239
                    }
240
                    menu_draw(main_menu);
241

    
242
                    click = 0;
243

    
244
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
245
                    sprite_draw(sp_crosshair);
246
                    graph_draw();
247

    
248
                    break;
249
                    case KBC_IRQ:
250
                    if ((scancode[0]) == ESC_BREAK_CODE) good = false;
251
                    case MOUSE_IRQ:
252
                    if (counter_mouse_ih >= 3) {
253
                        mouse_parse_packet(packet_mouse_ih, &pp);
254
                        update_mouse(&pp);
255
                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
256
                        last_lb = keys->lb_pressed;
257
                        counter_mouse_ih = 0;
258
                    }
259
                    break;
260
                    case COM1_IRQ: nctp_ih(); break;
261
                }
262
            }
263
        }
264
    }
265
    return 0;
266
}
267

    
268
static int (multiplayer_host)(void) {
269
    //int r;
270

    
271
    nctp_dump();
272
    nctp_set_processor(multiplayer_process);/*
273

274
    ent_set_scale(DEFAULT_SCALE);
275
    text_timer_t *in_game_timer = timer_ctor(consolas);
276

277
    list_t *shooter_list = list_ctor();
278

279
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
280
    gunner_set_spawn(shooter1, 75, 75);
281

282
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter2 == NULL) printf("Failed to get shooter2\n");
283
    gunner_set_spawn(shooter2, 975, 75);
284

285
    list_insert(shooter_list, list_end(shooter_list), shooter1);
286
    list_insert(shooter_list, list_end(shooter_list), shooter2);
287

288
    do {
289
        get_random_spawn(map1, shooter1, shooter_list);
290
        get_random_spawn(map1, shooter2, shooter_list);
291
    } while (distance_gunners(shooter1, shooter2) < 700);
292

293
    list_t *bullet_list  = list_ctor();
294

295
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
296
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
297

298
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
299
    uint8_t last_lb = 0;
300
    struct packet pp;
301
    keys_t *keys = get_key_presses();
302

303
    /// loop stuff
304
    uint32_t int_vector = 0;
305
    int good = true;
306
    int state = 0; // -1 for remote win, 0 for draw, 1 for host win
307
    while (good) {
308
        if ((r = get_interrupts_vector(&int_vector))) return r;
309
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
310
            interrupt_handler(i);
311
            switch (i) {
312
                case TIMER0_IRQ:
313

314
                break;
315

316
                case KBC_IRQ:
317

318
                break;
319

320
                case MOUSE_IRQ:
321

322

323
                break;
324

325
                case COM1_IRQ: nctp_ih(); break;
326
            }
327
        }
328
    }*/
329

    
330
    return 0;
331
}
332
static int (multiplayer_remote)(void) {/*
333
    int r;
334

335
    nctp_dump();
336
    nctp_set_processor(multiplayer_process);
337

338
    ent_set_scale(DEFAULT_SCALE);
339
    text_timer_t *in_game_timer = timer_ctor(consolas);
340

341
    list_t *shooter_list = list_ctor();
342

343
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
344
    gunner_set_spawn(shooter1, 75, 75);
345

346
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter2 == NULL) printf("Failed to get shooter2\n");
347
    gunner_set_spawn(shooter2, 975, 75);
348

349
    list_insert(shooter_list, list_end(shooter_list), shooter1);
350
    list_insert(shooter_list, list_end(shooter_list), shooter2);
351

352
    do {
353
        get_random_spawn(map1, shooter1, shooter_list);
354
        get_random_spawn(map1, shooter2, shooter_list);
355
    } while (distance_gunners(shooter1, shooter2) < 700);
356

357
    list_t *bullet_list  = list_ctor();
358

359
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
360
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
361

362
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
363
    uint8_t last_lb = 0;
364
    struct packet pp;
365
    keys_t *keys = get_key_presses();
366

367
    /// loop stuff
368
    uint32_t int_vector = 0;
369
    int good = true;
370
    int state = 0; // -1 for remote win, 0 for draw, 1 for host win
371
    while (good) {
372
        if ((r = get_interrupts_vector(&int_vector))) return r;
373
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
374
            interrupt_handler(i);
375
            switch (i) {
376
                case TIMER0_IRQ:
377

378
                break;
379

380
                case KBC_IRQ:
381

382
                break;
383

384
                case MOUSE_IRQ:
385

386

387
                break;
388

389
                case COM1_IRQ: nctp_ih(); break;
390
            }
391
        }
392
    }*/
393

    
394
    return 0;
395
}
396

    
397
static int (campaign)(void);
398
static int (zombies)(void);
399
static int (singleplayer)(void) {
400

    
401
    int r;
402

    
403
    menu_t *main_menu = menu_ctor(consolas);
404
    menu_add_item(main_menu, "Campaign");
405
    menu_add_item(main_menu, "Zombies");
406
    menu_add_item(main_menu, "Back");
407

    
408
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
409
    uint8_t last_lb = 0;
410
    struct packet pp;
411
    keys_t *keys = get_key_presses();
412

    
413
    /// loop stuff
414
    int click = 0;
415
    uint32_t int_vector = 0;
416
    int good = true;
417
    while (good) {
418
        /* Get a request message. */
419
        if((r = get_interrupts_vector(&int_vector))) return r;
420
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
421
            if (int_vector & n) {
422
                interrupt_handler(i);
423
                switch (i) {
424
                    case TIMER0_IRQ:
425

    
426
                    graph_clear_screen();
427
                    switch(menu_update_state(main_menu, click)){
428
                        case -1: break;
429
                        case  0: campaign(); break;
430
                        case  1: zombies(); break;
431
                        case  2: good = false; break;
432
                    }
433
                    menu_draw(main_menu);
434

    
435
                    click = 0;
436

    
437
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
438
                    sprite_draw(sp_crosshair);
439
                    graph_draw();
440

    
441
                    break;
442
                    case KBC_IRQ:
443
                    if ((scancode[0]) == ESC_BREAK_CODE) good = false;
444
                    case MOUSE_IRQ:
445
                    if (counter_mouse_ih >= 3) {
446
                        mouse_parse_packet(packet_mouse_ih, &pp);
447
                        update_mouse(&pp);
448
                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
449
                        last_lb = keys->lb_pressed;
450
                        counter_mouse_ih = 0;
451
                    }
452
                    break;
453
                    case COM1_IRQ: nctp_ih(); break;
454
                }
455
            }
456
        }
457
    }
458

    
459
    return 0;
460
}
461

    
462
static int (campaign)(void){
463

    
464
    int r;
465

    
466
    ent_set_scale(DEFAULT_SCALE);
467
    text_timer_t *in_game_timer = timer_ctor(consolas);
468

    
469
    list_t *shooter_list = list_ctor();
470

    
471
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
472
    gunner_set_spawn(shooter1, 75, 75);
473
    gunner_set_pos(shooter1, 75, 75);
474

    
475
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing, gunner_player, 2);
476
    gunner_set_spawn(shooter2, 975, 75);
477
    gunner_set_pos(shooter2, 775, 75);
478

    
479
    list_insert(shooter_list, list_end(shooter_list), shooter1);
480
    list_insert(shooter_list, list_end(shooter_list), shooter2);
481

    
482
    list_t *bullet_list  = list_ctor();
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
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
488
    uint8_t last_lb = 0;
489
    struct packet pp;
490
    keys_t *keys = get_key_presses();
491

    
492
    /// loop stuff
493
    uint32_t int_vector = 0;
494
    int good = true;
495
    while (good) {
496
        /* Get a request message. */
497
        if((r = get_interrupts_vector(&int_vector))) return r;
498
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
499
            if(!good) break;
500
            if (int_vector & n) {
501
                interrupt_handler(i);
502
                switch (i) {
503
                    case TIMER0_IRQ:
504

    
505
                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
506
                    update_movement(map1, shooter1, keys, shooter_list);
507

    
508
                    update_game_state(map1, shooter_list, bullet_list);
509

    
510
                    //update_scale();
511
                    double angle = get_mouse_angle(shooter1);
512
                    gunner_set_angle(shooter1, angle - M_PI_2);
513

    
514
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
515
                    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
516

    
517
                    graph_clear_screen();
518
                    map_draw   (map1);
519
                    bullet_draw_list(bullet_list);
520
                    gunner_draw_list(shooter_list);
521

    
522
                    text_draw(in_game_timer->text);
523

    
524
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
525
                    sprite_draw(sp_crosshair);
526
                    graph_draw();
527

    
528
                    break;
529
                    case KBC_IRQ:
530
                    if ((scancode[0]) == ESC_BREAK_CODE) {
531
                        good = false;
532
                    }
533
                    break;
534
                    case MOUSE_IRQ:
535
                    if (counter_mouse_ih >= 3) {
536
                        mouse_parse_packet(packet_mouse_ih, &pp);
537
                        update_mouse(&pp);
538
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
539
                        shoot_bullet(shooter1, bullet_list, bsp_bullet);
540
                        last_lb = keys->lb_pressed;
541
                        counter_mouse_ih = 0;
542

    
543
                    }
544
                    break;
545
                    case COM1_IRQ: nctp_ih(); break;
546
                }
547
            }
548
        }
549
    }
550

    
551
    while(list_size(shooter_list) > 0){
552
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
553
        gunner_dtor(p);
554
    }
555
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
556

    
557
    while(list_size(bullet_list) > 0){
558
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
559
        bullet_dtor(p);
560
    }
561
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
562

    
563
    timer_dtor(in_game_timer); in_game_timer = NULL;
564

    
565
    return SUCCESS;
566
}
567

    
568
#define ZOMBIES_NUM             5
569
#define ZOMBIE_HEALTH_FACTOR    1.1
570
static int (zombies)(void){
571

    
572
    int r;
573

    
574
    ent_set_scale(DEFAULT_SCALE);
575
    text_timer_t *in_game_timer = timer_ctor(consolas);
576

    
577
    list_t *shooter_list = list_ctor();
578

    
579
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
580
    gunner_set_spawn(shooter1, 980, 790);
581
    gunner_set_pos(shooter1, 980, 790);
582

    
583
    list_insert(shooter_list, list_end(shooter_list), shooter1);
584

    
585
    list_t *bullet_list  = list_ctor();
586

    
587
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
588
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
589

    
590
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
591
    uint8_t last_lb = 0;
592
    struct packet pp;
593
    keys_t *keys = get_key_presses();
594

    
595
    /// loop stuff
596
    uint32_t int_vector = 0;
597
    int good = true;
598
    int dead = false;
599

    
600
    int health = 50;
601

    
602
    /** #DEV */ /* {
603
        gunner_t *zombie = gunner_ctor(bsp_zombie, bsp_nothing, gunner_melee | gunner_follow, 3);
604
        gunner_set_health(zombie, health);
605
        gunner_set_curr_health(zombie, health);
606
        health *= ZOMBIE_HEALTH_FACTOR;
607
        gunner_set_pos(zombie, 1100, 75);
608
        list_push_back(shooter_list, zombie);
609
    }*/ //\#DEV
610

    
611
    map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1));
612

    
613
    while (good && !dead) {
614
        /* Get a request message. */
615
        if((r = get_interrupts_vector(&int_vector))) return r;
616
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
617
            if(!good || dead) break;
618
            if (int_vector & n) {
619
                interrupt_handler(i);
620
                switch (i) {
621
                    case TIMER0_IRQ:
622
                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
623
                    if (no_interrupts %  6 == 0){
624
                        map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1));
625
                    }
626

    
627
                    update_movement(map1, shooter1, keys, shooter_list);
628

    
629
                    update_game_state(map1, shooter_list, bullet_list);
630

    
631
                    if(list_find(shooter_list, shooter1) == list_end(shooter_list)){
632
                        good = false;
633
                        dead = true;
634
                        break;
635
                    }
636

    
637
                    double angle = get_mouse_angle(shooter1);
638
                    gunner_set_angle(shooter1, angle - M_PI_2);
639

    
640
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
641
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
642

    
643
                    while(list_size(shooter_list) < ZOMBIES_NUM+1){
644
                        gunner_t *zombie = gunner_ctor(bsp_zombie, bsp_nothing, gunner_melee | gunner_follow, 3);
645
                        gunner_set_health(zombie, health);
646
                        gunner_set_curr_health(zombie, health);
647
                        health *= ZOMBIE_HEALTH_FACTOR;
648
                        get_random_spawn(map1, zombie, shooter_list);
649
                        list_push_back(shooter_list, zombie);
650
                    }
651

    
652
                    graph_clear_screen();
653
                    map_draw   (map1);
654
                    bullet_draw_list(bullet_list);
655
                    gunner_draw_list(shooter_list);
656

    
657
                    text_draw(in_game_timer->text);
658

    
659
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
660
                    sprite_draw(sp_crosshair);
661
                    graph_draw();
662

    
663
                    break;
664
                    case KBC_IRQ:
665
                    if ((scancode[0]) == ESC_BREAK_CODE) {
666
                        good = false;
667
                    }
668
                    break;
669
                    case MOUSE_IRQ:
670
                    if (counter_mouse_ih >= 3) {
671
                        mouse_parse_packet(packet_mouse_ih, &pp);
672
                        update_mouse(&pp);
673
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
674
                        shoot_bullet(shooter1, bullet_list, bsp_bullet);
675
                        last_lb = keys->lb_pressed;
676
                        counter_mouse_ih = 0;
677

    
678
                    }
679
                    break;
680
                    case COM1_IRQ: nctp_ih(); break;
681
                }
682
            }
683
        }
684
    }
685

    
686
    while(list_size(shooter_list) > 0){
687
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
688
        gunner_dtor(p);
689
    }
690
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
691

    
692
    while(list_size(bullet_list) > 0){
693
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
694
        bullet_dtor(p);
695
    }
696
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
697

    
698
    if(dead){
699
        printf("YOU DIED\n");
700
    }
701

    
702
    timer_dtor(in_game_timer); in_game_timer = NULL;
703

    
704
    return SUCCESS;
705
}
706

    
707
#define CHAT_MAX_SIZE   75
708
#define CHAT_MAX_NUM    19
709
text_t      *t_text[CHAT_MAX_NUM] = {NULL};
710
rectangle_t *r_text               =  NULL;
711
static void chat_process(const uint8_t *p, const size_t sz){
712
    char buffer2[CHAT_MAX_NUM+3];
713
    void *dest = NULL;
714
    hltp_type tp = hltp_interpret(p, sz, &dest);
715
    switch(tp){
716
        case hltp_type_string:
717
        strcpy(buffer2, dest);
718
        strncat(buffer2, " <", 2);
719
        for(size_t i = CHAT_MAX_NUM-1; i; --i)
720
        text_set_text(t_text[i], text_get_string(t_text[i-1]));
721
        text_set_text(t_text[0], buffer2);
722
        for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
723
            if(text_get_string(t_text[i])[0] == '>'){
724
                text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
725
                text_set_halign(t_text[i], text_halign_left);
726
            }else{
727
                text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
728
                text_set_halign(t_text[i], text_halign_right);
729
            }
730
        }
731
        break;
732
        default: break;
733
    }
734
}
735
static int (chat)(void){
736
    int r;
737

    
738
    nctp_dump();
739
    nctp_set_processor(chat_process);
740

    
741
    struct packet pp;
742

    
743
    char buffer[CHAT_MAX_SIZE] = "";
744
    rectangle_t *r_buffer = NULL; {
745
        r_buffer = rectangle_ctor(0,0,900,70);
746
        rectangle_set_pos(r_buffer, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
747
        graph_get_YRes()*0.87-rectangle_get_h(r_buffer)/2);
748
        rectangle_set_fill_color   (r_buffer, GRAPH_BLACK);
749
        rectangle_set_outline_width(r_buffer, 2);
750
        rectangle_set_outline_color(r_buffer, GRAPH_WHITE);
751
        rectangle_set_fill_trans(r_buffer, GRAPH_TRANSPARENT);
752
    }
753
    text_t      *t_buffer = NULL; {
754
        t_buffer = text_ctor(consolas, "");
755
        text_set_pos(t_buffer, rectangle_get_x(r_buffer)+50,
756
        rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)/2);
757
        text_set_halign(t_buffer, text_halign_left);
758
        text_set_valign(t_buffer, text_valign_center);
759
        text_set_color (t_buffer, TEXT_COLOR);
760
    }
761
    text_t      *t_size   = NULL; {
762
        t_size = text_ctor(consolas, "");
763
        text_set_pos(t_size, rectangle_get_x(r_buffer)+rectangle_get_w(r_buffer)-5,
764
        rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)-5);
765
        text_set_halign(t_size, text_halign_right);
766
        text_set_valign(t_size, text_valign_bottom);
767
        text_set_color (t_size, TEXT_COLOR);
768
        text_set_size  (t_size, 18);
769
        char buffer2[20];
770
        sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
771
        text_set_text(t_size, buffer2);
772
    }
773

    
774
    /** r_text */ {
775
    r_text = rectangle_ctor(0,0,900,550);
776
    rectangle_set_pos(r_text, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
777
    graph_get_YRes()*0.09);
778
    rectangle_set_fill_color   (r_text, GRAPH_BLACK);
779
    rectangle_set_outline_width(r_text, 2);
780
    rectangle_set_outline_color(r_text, GRAPH_WHITE);
781
    rectangle_set_fill_trans(r_text, GRAPH_TRANSPARENT);
782
    }
783
    /** t_text */ {
784
    for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
785
        t_text[i] = text_ctor(consolas, " ");
786
        text_set_pos(t_text[i], rectangle_get_x(r_text)+50,
787
        rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-25*i);
788
        text_set_halign(t_text[i], text_halign_left);
789
        text_set_valign(t_text[i], text_valign_bottom);
790
        text_set_color (t_text[i], TEXT_COLOR);
791
    }
792
}
793

    
794
/// loop stuff
795
uint32_t int_vector = 0;
796
int good = true;
797
while (good) {
798
    /* Get a request message. */
799
    if((r = get_interrupts_vector(&int_vector))) return r;
800
    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
801
        if (int_vector & n) {
802
            interrupt_handler(i);
803
            switch (i) {
804
                case TIMER0_IRQ:
805
                graph_clear_screen();
806
                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
807

    
808
                rectangle_draw(r_buffer);
809
                text_draw(t_buffer);
810
                text_draw(t_size);
811

    
812
                rectangle_draw(r_text);
813
                for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_draw(t_text[i]);
814

    
815
                sprite_draw(sp_crosshair);
816
                graph_draw();
817
                break;
818
                case KBC_IRQ:
819
                if      (scancode[0] == ESC_BREAK_CODE) good = false;
820
                else if (scancode[0] == ENTER_MAKE_CODE) {
821
                    hltp_send_string(buffer);
822
                    char buffer2[CHAT_MAX_SIZE+3] = "> ";
823
                    strncat(buffer2, buffer, strlen(buffer));
824
                    for(size_t i = CHAT_MAX_NUM-1; i; --i)
825
                    text_set_text(t_text[i], text_get_string(t_text[i-1]));
826
                    text_set_text(t_text[0], buffer2);
827
                    for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
828
                        if(text_get_string(t_text[i])[0] == '>'){
829
                            text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
830
                            text_set_halign(t_text[i], text_halign_left);
831
                        }else{
832
                            text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
833
                            text_set_halign(t_text[i], text_halign_right);
834
                        }
835
                    }
836
                    buffer[0] = '\0';
837
                } else if(scancode[0] == BACKSPACE_MAKE_CODE){
838
                    buffer[strlen(buffer)-1] = '\0';
839
                } else {
840
                    char c = map_makecode(scancode[0]);
841
                    if (c == ERROR_CODE) break;
842
                    if(strlen(buffer) < CHAT_MAX_SIZE) strncat(buffer, &c, 1);
843
                    else                               printf("Char limit exceeded\n");
844
                }
845
                text_set_text(t_buffer, buffer);
846
                char buffer2[20];
847
                sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
848
                text_set_text(t_size, buffer2);
849
                case MOUSE_IRQ:
850
                if (counter_mouse_ih >= 3) {
851
                    mouse_parse_packet(packet_mouse_ih, &pp);
852
                    update_mouse(&pp);
853
                    counter_mouse_ih = 0;
854
                }
855
                break;
856
                case COM1_IRQ: nctp_ih(); break;
857
            }
858
        }
859
    }
860
}
861

    
862
rectangle_dtor(r_buffer);
863
text_dtor     (t_buffer);
864

    
865
rectangle_dtor(r_text);
866
for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_dtor(t_text[i]);
867

    
868
nctp_set_processor(NULL);
869

    
870
return SUCCESS;
871
}