Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 320

History | View | Annotate | Download (36.7 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
basic_sprite_t       *bsp_crosshair = NULL;
53
basic_sprite_t       *bsp_shooter   = NULL;
54
basic_sprite_t       *bsp_zombie    = NULL;
55
basic_sprite_t       *bsp_pistol    = NULL;
56
basic_sprite_t       *bsp_nothing   = NULL;
57
basic_sprite_t       *bsp_bullet    = NULL;
58
map_t                *map1          = NULL;
59
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
    uint32_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
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
122
            if (int_vector & n) {
123
                interrupt_handler(i);
124
                switch (i) {
125
                    case TIMER0_IRQ:
126

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

    
137
                    click = 0;
138

    
139
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
140
                    sprite_draw(sp_crosshair);
141
                    graph_draw();
142

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

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

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

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

    
182
    return 0;
183
}
184

    
185
host_info_t     *host_info   = NULL;
186
remote_info_t   *remote_info = NULL;
187
bullet_info_t   *bullet_info = NULL;
188

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

    
213
    menu_t *main_menu = menu_ctor(font_get_default());
214
    menu_add_item(main_menu, "Create");
215
    menu_add_item(main_menu, "Connect");
216
    menu_add_item(main_menu, "Back");
217

    
218
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
219
    uint8_t last_lb = 0;
220
    struct packet pp;
221
    keys_t *keys = get_key_presses();
222

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

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

    
245
                    click = 0;
246

    
247
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
248
                    sprite_draw(sp_crosshair);
249
                    graph_draw();
250

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

    
271
static int (multiplayer_host)(void) {
272
    int r;
273

    
274
    nctp_dump();
275
    nctp_set_processor(multiplayer_process);
276

    
277
    ent_set_scale(DEFAULT_SCALE);
278
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
279

    
280
    list_t *shooter_list = list_ctor();
281

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

    
285
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 2); if(shooter2 == NULL) printf("Failed to get shooter2\n");
286
    gunner_set_spawn(shooter2, 975, 75);
287

    
288
    list_insert(shooter_list, list_end(shooter_list), shooter1);
289
    list_insert(shooter_list, list_end(shooter_list), shooter2);
290

    
291
    do {
292
        get_random_spawn(map1, shooter1, shooter_list);
293
        get_random_spawn(map1, shooter2, shooter_list);
294
    } while (distance_gunners(shooter1, shooter2) < 500);
295

    
296
    host_info = host_info_ctor(shooter1, shooter2);
297
    remote_info = remote_info_ctor();
298
    bullet_info = bullet_info_ctor();
299

    
300
    list_t *bullet_list  = list_ctor();
301

    
302
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
303
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
304

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

    
324
                    update_movement(map1, shooter1, keys, shooter_list);
325
                    update_movement(map1, shooter2, &(remote_info->remote_keys_pressed), shooter_list);
326

    
327
                    update_game_state(map1, shooter_list, bullet_list);
328

    
329
                    p1 = list_find(shooter_list, shooter1);
330
                    p2 = list_find(shooter_list, shooter2);
331

    
332
                    if ((state_1 = (p1 == list_end(shooter_list))) || (state_2 = (p2 == list_end(shooter_list)))) {
333
                        state = state_1 - state_2;
334
                        good = false;
335
                        break;
336
                    }
337

    
338
                    double angle = get_mouse_angle(shooter1);
339
                    gunner_set_angle(shooter1, angle - M_PI_2);
340

    
341
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
342
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
343

    
344
                    gunner_set_angle(shooter2, remote_info->remote_angle);
345

    
346
                    build_host_structure(host_info, shooter1, shooter2, bullet_list);
347

    
348
                    hltp_send_host_info(host_info);
349

    
350
                    graph_clear_screen();
351
                    map_draw   (map1);
352
                    bullet_draw_list(bullet_list);
353
                    gunner_draw_list(shooter_list);
354

    
355
                    text_draw(in_game_timer->text);
356

    
357
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
358
                    sprite_draw(sp_crosshair);
359
                    graph_draw();
360

    
361
                    break;
362
                    case KBC_IRQ:
363
                    if ((scancode[0]) == ESC_BREAK_CODE) {
364
                        good = false;
365
                    }
366
                    break;
367
                    case MOUSE_IRQ:
368
                    if (counter_mouse_ih >= 3) {
369
                        mouse_parse_packet(packet_mouse_ih, &pp);
370
                        update_mouse(&pp);
371
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
372
                        shoot_bullet(shooter1, bullet_list, bsp_bullet);
373
                        last_lb = keys->lb_pressed;
374
                        counter_mouse_ih = 0;
375
                    }
376
                    break;
377

    
378
                    case COM1_IRQ:
379
                        nctp_ih();
380
                        if (bullet_info->new_bullet) {
381
                            shoot_bullet(shooter2, bullet_list, bsp_bullet);
382
                            bullet_info->new_bullet = false;
383
                        }
384
                        break;
385
                }
386
            }
387
        }
388
    }
389

    
390
    while(list_size(shooter_list) > 0){
391
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
392
        gunner_dtor(p);
393
    }
394
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
395

    
396
    while(list_size(bullet_list) > 0){
397
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
398
        bullet_dtor(p);
399
    }
400
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
401

    
402
    host_info_dtor(host_info);
403
    remote_info_dtor(remote_info);
404
    bullet_info_dtor(bullet_info);
405

    
406
    timer_dtor(in_game_timer); in_game_timer = NULL;
407

    
408
    return 0;
409
}
410
static int (multiplayer_remote)(void) {
411
    int r;
412

    
413
    nctp_dump();
414
    nctp_set_processor(multiplayer_process);
415

    
416
    ent_set_scale(DEFAULT_SCALE);
417
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
418

    
419
    list_t *shooter_list = list_ctor();
420

    
421
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 2); if(shooter1 == NULL) printf("Failed to get shooter1\n");
422
    gunner_set_spawn(shooter1, 75, 75);
423

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

    
427
    list_insert(shooter_list, list_end(shooter_list), shooter1);
428
    list_insert(shooter_list, list_end(shooter_list), shooter2);
429

    
430
    host_info = host_info_ctor(shooter2, shooter1);
431
    remote_info = remote_info_ctor();
432
    bullet_info = bullet_info_ctor();
433

    
434
    list_t *bullet_list  = list_ctor();
435

    
436
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
437
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
438

    
439
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
440
    uint8_t last_lb = 0;
441
    struct packet pp;
442
    keys_t *keys = get_key_presses();
443

    
444
    /// loop stuff
445
    uint32_t int_vector = 0;
446
    int good = true;
447
    while (good) {
448
        if ((r = get_interrupts_vector(&int_vector))) return r;
449
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
450
            if (int_vector & n) {
451
                interrupt_handler(i);
452
                switch (i) {
453
                    case TIMER0_IRQ:
454
                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
455

    
456
                    double angle = get_mouse_angle(shooter1);
457

    
458
                    build_remote_structure(remote_info, keys, angle);
459

    
460
                    hltp_send_remote_info(remote_info);
461

    
462
                    gunner_set_pos(shooter1, host_info->remote_x, host_info->remote_y);
463
                    gunner_set_angle(shooter1, host_info->remote_angle);
464
                    gunner_set_health(shooter1, host_info->remote_health);
465
                    gunner_set_curr_health(shooter1, host_info->remote_current_health);
466

    
467
                    gunner_set_pos(shooter2, host_info->host_x, host_info->host_y);
468
                    gunner_set_angle(shooter2, host_info->host_angle);
469
                    gunner_set_health(shooter2, host_info->host_health);
470
                    gunner_set_curr_health(shooter2, host_info->host_current_health);
471

    
472
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
473
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
474

    
475
                    for (size_t i = 0; i < host_info->no_bullets; i++) {
476
                        if (host_info->bullets_shooter[i]) { // remote
477
                            bullet_t *bullet = bullet_ctor(shooter1, bsp_bullet, host_info->bullets_x[i], host_info->bullets_y[i], host_info->bullets_vx[i], host_info->bullets_vy[i]);
478
                            list_insert(bullet_list, list_end(bullet_list), bullet);
479
                        } else { // host
480
                            bullet_t *bullet = bullet_ctor(shooter2, bsp_bullet, host_info->bullets_x[i], host_info->bullets_y[i], host_info->bullets_vx[i], host_info->bullets_vy[i]);
481
                            list_insert(bullet_list, list_end(bullet_list), bullet);
482
                        }
483
                    }
484

    
485
                    graph_clear_screen();
486
                    map_draw   (map1);
487
                    bullet_draw_list(bullet_list);
488
                    gunner_draw_list(shooter_list);
489

    
490
                    text_draw(in_game_timer->text);
491

    
492
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
493
                    sprite_draw(sp_crosshair);
494
                    graph_draw();
495

    
496
                    while(list_size(bullet_list) > 0){
497
                        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
498
                        bullet_dtor(p);
499
                    }
500

    
501
                    break;
502
                    case KBC_IRQ:
503
                    if ((scancode[0]) == ESC_BREAK_CODE) {
504
                        good = false;
505
                    }
506
                    break;
507
                    case MOUSE_IRQ:
508
                    if (counter_mouse_ih >= 3) {
509
                        mouse_parse_packet(packet_mouse_ih, &pp);
510
                        update_mouse(&pp);
511
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed) {
512
                            bullet_info->new_bullet = true;
513
                            hltp_send_bullet_info(bullet_info);
514
                        }
515
                        last_lb = keys->lb_pressed;
516
                        counter_mouse_ih = 0;
517
                    }
518
                    break;
519

    
520
                    case COM1_IRQ: nctp_ih(); break;
521
                }
522
            }
523
        }
524
    }
525

    
526
    while(list_size(shooter_list) > 0){
527
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
528
        gunner_dtor(p);
529
    }
530
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
531

    
532
    while(list_size(bullet_list) > 0){
533
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
534
        bullet_dtor(p);
535
    }
536
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
537

    
538
    host_info_dtor(host_info);
539
    remote_info_dtor(remote_info);
540
    bullet_info_dtor(bullet_info);
541

    
542
    timer_dtor(in_game_timer); in_game_timer = NULL;
543

    
544
    return 0;
545
}
546

    
547
static int (campaign)(void);
548
static int (zombies)(void);
549
static int (singleplayer)(void) {
550

    
551
    int r;
552

    
553
    menu_t *main_menu = menu_ctor(font_get_default());
554
    menu_add_item(main_menu, "Campaign");
555
    menu_add_item(main_menu, "Zombies");
556
    menu_add_item(main_menu, "Back");
557

    
558
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
559
    uint8_t last_lb = 0;
560
    struct packet pp;
561
    keys_t *keys = get_key_presses();
562

    
563
    /// loop stuff
564
    int click = 0;
565
    uint32_t int_vector = 0;
566
    int good = true;
567
    while (good) {
568
        /* Get a request message. */
569
        if((r = get_interrupts_vector(&int_vector))) return r;
570
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
571
            if (int_vector & n) {
572
                interrupt_handler(i);
573
                switch (i) {
574
                    case TIMER0_IRQ:
575

    
576
                    graph_clear_screen();
577
                    switch(menu_update_state(main_menu, click)){
578
                        case -1: break;
579
                        case  0: campaign(); break;
580
                        case  1: zombies(); break;
581
                        case  2: good = false; break;
582
                    }
583
                    menu_draw(main_menu);
584

    
585
                    click = 0;
586

    
587
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
588
                    sprite_draw(sp_crosshair);
589
                    graph_draw();
590

    
591
                    break;
592
                    case KBC_IRQ:
593
                    if ((scancode[0]) == ESC_BREAK_CODE) good = false;
594
                    case MOUSE_IRQ:
595
                    if (counter_mouse_ih >= 3) {
596
                        mouse_parse_packet(packet_mouse_ih, &pp);
597
                        update_mouse(&pp);
598
                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
599
                        last_lb = keys->lb_pressed;
600
                        counter_mouse_ih = 0;
601
                    }
602
                    break;
603
                    case COM1_IRQ: nctp_ih(); break;
604
                }
605
            }
606
        }
607
    }
608

    
609
    return 0;
610
}
611

    
612
static int (campaign)(void){
613

    
614
    int r;
615

    
616
    ent_set_scale(DEFAULT_SCALE);
617
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
618

    
619
    list_t *shooter_list = list_ctor();
620

    
621
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
622
    gunner_set_spawn(shooter1, 75, 75);
623
    gunner_set_pos(shooter1, 75, 75);
624

    
625
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing, gunner_player, 2);
626
    gunner_set_spawn(shooter2, 975, 75);
627
    gunner_set_pos(shooter2, 775, 75);
628

    
629
    list_insert(shooter_list, list_end(shooter_list), shooter1);
630
    list_insert(shooter_list, list_end(shooter_list), shooter2);
631

    
632
    list_t *bullet_list  = list_ctor();
633

    
634
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
635
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
636

    
637
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
638
    uint8_t last_lb = 0;
639
    struct packet pp;
640
    keys_t *keys = get_key_presses();
641

    
642
    /// loop stuff
643
    uint32_t int_vector = 0;
644
    int good = true;
645
    while (good) {
646
        /* Get a request message. */
647
        if((r = get_interrupts_vector(&int_vector))) return r;
648
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
649
            if(!good) break;
650
            if (int_vector & n) {
651
                interrupt_handler(i);
652
                switch (i) {
653
                    case TIMER0_IRQ:
654

    
655
                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
656
                    update_movement(map1, shooter1, keys, shooter_list);
657

    
658
                    update_game_state(map1, shooter_list, bullet_list);
659

    
660
                    //update_scale();
661
                    double angle = get_mouse_angle(shooter1);
662
                    gunner_set_angle(shooter1, angle - M_PI_2);
663

    
664
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
665
                    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
666

    
667
                    graph_clear_screen();
668
                    map_draw   (map1);
669
                    bullet_draw_list(bullet_list);
670
                    gunner_draw_list(shooter_list);
671

    
672
                    text_draw(in_game_timer->text);
673

    
674
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
675
                    sprite_draw(sp_crosshair);
676
                    graph_draw();
677

    
678
                    break;
679
                    case KBC_IRQ:
680
                    if ((scancode[0]) == ESC_BREAK_CODE) {
681
                        good = false;
682
                    }
683
                    break;
684
                    case MOUSE_IRQ:
685
                    if (counter_mouse_ih >= 3) {
686
                        mouse_parse_packet(packet_mouse_ih, &pp);
687
                        update_mouse(&pp);
688
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
689
                        shoot_bullet(shooter1, bullet_list, bsp_bullet);
690
                        last_lb = keys->lb_pressed;
691
                        counter_mouse_ih = 0;
692

    
693
                    }
694
                    break;
695
                    case COM1_IRQ: nctp_ih(); break;
696
                }
697
            }
698
        }
699
    }
700

    
701
    while(list_size(shooter_list) > 0){
702
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
703
        gunner_dtor(p);
704
    }
705
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
706

    
707
    while(list_size(bullet_list) > 0){
708
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
709
        bullet_dtor(p);
710
    }
711
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
712

    
713
    timer_dtor(in_game_timer); in_game_timer = NULL;
714

    
715
    return SUCCESS;
716
}
717

    
718
#define ZOMBIES_NUM             5
719
#define ZOMBIE_HEALTH_FACTOR    1.1
720
static int (zombies)(void){
721

    
722
    int r;
723

    
724
    ent_set_scale(DEFAULT_SCALE);
725
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
726

    
727
    list_t *shooter_list = list_ctor();
728

    
729
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
730
    gunner_set_spawn(shooter1, 980, 790);
731
    gunner_set_pos(shooter1, 980, 790);
732

    
733
    list_insert(shooter_list, list_end(shooter_list), shooter1);
734

    
735
    list_t *bullet_list  = list_ctor();
736

    
737
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
738
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
739

    
740
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
741
    uint8_t last_lb = 0;
742
    struct packet pp;
743
    keys_t *keys = get_key_presses();
744

    
745
    /// loop stuff
746
    uint32_t int_vector = 0;
747
    int good = true;
748
    int dead = false;
749

    
750
    int health = 50;
751

    
752
    /** #DEV */ /* {
753
        gunner_t *zombie = gunner_ctor(bsp_zombie, bsp_nothing, gunner_melee | gunner_follow, 3);
754
        gunner_set_health(zombie, health);
755
        gunner_set_curr_health(zombie, health);
756
        health *= ZOMBIE_HEALTH_FACTOR;
757
        gunner_set_pos(zombie, 1100, 75);
758
        list_push_back(shooter_list, zombie);
759
    }*/ //\#DEV
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
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
767
            if(!good || dead) break;
768
            if (int_vector & n) {
769
                interrupt_handler(i);
770
                switch (i) {
771
                    case TIMER0_IRQ:
772
                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
773
                    if (no_interrupts %  6 == 0){
774
                        map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1));
775
                    }
776

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

    
779
                    update_game_state(map1, shooter_list, bullet_list);
780

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

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

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

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

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

    
807
                    text_draw(in_game_timer->text);
808

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

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

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

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

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

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

    
852
    timer_dtor(in_game_timer); in_game_timer = NULL;
853

    
854
    return SUCCESS;
855
}
856

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

    
888
    nctp_dump();
889
    nctp_set_processor(chat_process);
890

    
891
    struct packet pp;
892

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

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

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

    
958
                rectangle_draw(r_buffer);
959
                text_draw(t_buffer);
960
                text_draw(t_size);
961

    
962
                rectangle_draw(r_text);
963
                for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_draw(t_text[i]);
964

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

    
1012
rectangle_dtor(r_buffer);
1013
text_dtor     (t_buffer);
1014

    
1015
rectangle_dtor(r_text);
1016
for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_dtor(t_text[i]);
1017

    
1018
nctp_set_processor(NULL);
1019

    
1020
return SUCCESS;
1021
}