Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 331

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

    
20
#include "graph.h"
21
#include "rectangle.h"
22
#include "font.h"
23
#include "ent.h"
24

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

    
33
#include "errors.h"
34

    
35
#include "list.h"
36

    
37
int main(int argc, char* argv[]) {
38

    
39
    lcf_set_language("EN-US");
40

    
41
    lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
42

    
43
    lcf_log_output("/home/lcom/labs/proj/output.txt");
44

    
45
    if (lcf_start(argc, argv)) return 1;
46

    
47
    lcf_cleanup();
48

    
49
    return 0;
50
}
51

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

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

    
67
    int r;
68

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

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

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

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

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

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

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

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

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

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

    
139
                    click = 0;
140

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

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

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

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

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

    
184
    return 0;
185
}
186

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

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

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

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

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

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

    
249
                    click = 0;
250

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

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

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

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

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

    
284
    list_t *shooter_list = list_ctor();
285

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

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

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

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

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

    
304
    list_t *bullet_list  = list_ctor();
305

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

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

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

    
332
                    update_game_state(map1, shooter_list, bullet_list);
333

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

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

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

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

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

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

    
353

    
354

    
355
                    hltp_send_host_info(host_info);
356

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

    
362
                    text_draw(in_game_timer->text);
363

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

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

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

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

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

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

    
413
    nctp_set_processor(NULL);
414

    
415
    timer_dtor(in_game_timer); in_game_timer = NULL;
416

    
417
    return 0;
418
}
419
static int (multiplayer_remote)(void) {
420
    int r;
421

    
422
    nctp_dump();
423
    nctp_set_processor(multiplayer_process);
424

    
425
    ent_set_scale(DEFAULT_SCALE);
426
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
427

    
428
    list_t *shooter_list = list_ctor();
429

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

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

    
436
    list_insert(shooter_list, list_end(shooter_list), shooter1);
437
    list_insert(shooter_list, list_end(shooter_list), shooter2);
438

    
439
    host_info = host_info_ctor(shooter2, shooter1);
440
    remote_info = remote_info_ctor();
441
    bullet_info = bullet_info_ctor();
442

    
443
    list_t *bullet_list  = list_ctor();
444

    
445
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
446
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
447

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

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

    
466
                    double angle = get_mouse_angle(shooter1) - M_PI_2;
467

    
468
                    build_remote_structure(remote_info, keys, angle);
469

    
470
                    //hltp_send_remote_info(remote_info);
471

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

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

    
482
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
483
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
484

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

    
495
                    graph_clear_screen();
496
                    map_draw   (map1);
497
                    bullet_draw_list(bullet_list);
498
                    gunner_draw_list(shooter_list);
499

    
500
                    text_draw(in_game_timer->text);
501

    
502
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
503
                    sprite_draw(sp_crosshair);
504
                    graph_draw();
505

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

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

    
530
                    case COM1_IRQ: nctp_ih(); break;
531
                }
532
            }
533
        }
534
    }
535

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

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

    
548
    host_info_dtor(host_info);
549
    remote_info_dtor(remote_info);
550
    bullet_info_dtor(bullet_info);
551

    
552
    nctp_set_processor(NULL);
553

    
554
    timer_dtor(in_game_timer); in_game_timer = NULL;
555

    
556
    return 0;
557
}
558

    
559
static int (campaign)(void);
560
static int (zombies)(void);
561
static int (singleplayer)(void) {
562

    
563
    int r;
564

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

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

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

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

    
598
                    click = 0;
599

    
600
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
601
                    sprite_draw(sp_crosshair);
602
                    graph_draw();
603

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

    
622
    return 0;
623
}
624

    
625
static int (campaign)(void){
626

    
627
    int r;
628

    
629
    ent_set_scale(DEFAULT_SCALE);
630
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
631

    
632
    list_t *shooter_list = list_ctor();
633

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

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

    
642
    list_insert(shooter_list, list_end(shooter_list), shooter1);
643
    list_insert(shooter_list, list_end(shooter_list), shooter2);
644

    
645
    list_t *bullet_list  = list_ctor();
646

    
647
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
648
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
649

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

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

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

    
672
                    update_game_state(map1, shooter_list, bullet_list);
673

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

    
678
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
679
                    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
680

    
681
                    graph_clear_screen();
682
                    map_draw   (map1);
683
                    bullet_draw_list(bullet_list);
684
                    gunner_draw_list(shooter_list);
685

    
686
                    text_draw(in_game_timer->text);
687

    
688
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
689
                    sprite_draw(sp_crosshair);
690
                    graph_draw();
691

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

    
707
                    }
708
                    break;
709
                    case COM1_IRQ: nctp_ih(); break;
710
                }
711
            }
712
        }
713
    }
714

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

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

    
727
    timer_dtor(in_game_timer); in_game_timer = NULL;
728

    
729
    return SUCCESS;
730
}
731

    
732
#define ZOMBIES_NUM             5
733
#define ZOMBIE_HEALTH_FACTOR    1.1
734
static int (zombies)(void){
735

    
736
    int r;
737

    
738
    ent_set_scale(DEFAULT_SCALE);
739
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
740

    
741
    list_t *shooter_list = list_ctor();
742

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

    
747
    list_insert(shooter_list, list_end(shooter_list), shooter1);
748

    
749
    list_t *bullet_list  = list_ctor();
750

    
751
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
752
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
753

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

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

    
764
    int health = 50;
765

    
766
    map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1));
767

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

    
783
                    update_movement(map1, shooter1, keys, shooter_list);
784

    
785
                    update_game_state(map1, shooter_list, bullet_list);
786

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

    
793
                    double angle = get_mouse_angle(shooter1);
794
                    gunner_set_angle(shooter1, angle - M_PI_2);
795

    
796
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
797
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
798

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

    
808
                    graph_clear_screen();
809
                    map_draw   (map1);
810
                    bullet_draw_list(bullet_list);
811
                    gunner_draw_list(shooter_list);
812

    
813
                    text_draw(in_game_timer->text);
814

    
815
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
816
                    sprite_draw(sp_crosshair);
817
                    graph_draw();
818

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

    
834
                    }
835
                    break;
836
                    case COM1_IRQ: nctp_ih(); break;
837
                }
838
            }
839
        }
840
    }
841

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

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

    
854
    if(dead){
855
        printf("YOU DIED\n");
856
    }
857

    
858
    timer_dtor(in_game_timer); in_game_timer = NULL;
859

    
860
    return SUCCESS;
861
}
862

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

    
897
    nctp_dump();
898
    nctp_set_processor(chat_process);
899

    
900
    struct packet pp;
901

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

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

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

    
968
                rectangle_draw(r_buffer);
969
                text_draw(t_buffer);
970
                text_draw(t_size);
971

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

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

    
1021
rectangle_dtor(r_buffer);
1022
text_dtor     (t_buffer);
1023

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

    
1027
nctp_set_processor(NULL);
1028

    
1029
return SUCCESS;
1030
}