Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 311

History | View | Annotate | Download (29.6 KB)

1 144 up20180655
#include <lcom/lcf.h>
2
#include <lcom/proj.h>
3
#include <lcom/liblm.h>
4 185 up20180655
#include <math.h>
5 144 up20180655
6 149 up20180655
#include "proj_macros.h"
7 179 up20180642
#include "proj_func.h"
8 147 up20180655
9 149 up20180655
#include "kbc.h"
10
#include "timer.h"
11
#include "keyboard.h"
12 150 up20180655
#include "mouse.h"
13 189 up20180655
#include "graph.h"
14 291 up20180642
#include "menu.h"
15 234 up20180655
#include "rtc.h"
16 287 up20180642
#include "hltp.h"
17 153 up20180655
#include "interrupts_func.h"
18 270 up20180655
#include "makecode_map.h"
19 149 up20180655
20 179 up20180642
#include "graph.h"
21
#include "rectangle.h"
22 182 up20180642
#include "font.h"
23 192 up20180642
#include "ent.h"
24 168 up20180642
25 192 up20180642
#include "crosshair.h"
26
#include "shooter.h"
27 303 up20180642
#include "zombie.h"
28 192 up20180642
#include "pistol.h"
29
#include "nothing.h"
30 237 up20180642
#include "bullet.h"
31 216 up20180642
#include "map1.h"
32 183 up20180642
33 294 up20180642
#include "errors.h"
34
35 226 up20180642
#include "list.h"
36
37 144 up20180655
int main(int argc, char* argv[]) {
38
39
    lcf_set_language("EN-US");
40
41 310 up20180642
    //lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
42 144 up20180655
43 310 up20180642
    //lcf_log_output("/home/lcom/labs/proj/output.txt");
44 144 up20180655
45
    if (lcf_start(argc, argv)) return 1;
46
47
    lcf_cleanup();
48
49
    return 0;
50
}
51 147 up20180655
52 294 up20180642
font_t               *consolas      = NULL;
53
basic_sprite_t       *bsp_crosshair = NULL;
54
basic_sprite_t       *bsp_shooter   = NULL;
55 301 up20180642
basic_sprite_t       *bsp_zombie    = NULL;
56 294 up20180642
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 300 up20180642
static int (singleplayer)(void);
63 311 up20180655
static int (multiplayer)(void);
64 300 up20180642
static int (chat)(void);
65 149 up20180655
int(proj_main_loop)(int argc, char *argv[]) {
66 170 up20180642
67
    int r;
68
69 294 up20180642
    consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
70 261 up20180642
    if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
71 192 up20180642
72 261 up20180642
    /// subscribe interrupts
73
    if (subscribe_all()) { return 1; }
74 170 up20180642
75 297 up20180642
    /// 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 152 up20180642
82 261 up20180642
    /// Load stuff
83
    {
84 297 up20180642
        graph_clear_screen();
85
        text_t *txt = text_ctor(consolas, "Loading...");
86 309 up20180642
        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 297 up20180642
        text_draw(txt);
91
        text_dtor(txt);
92
        graph_draw();
93 183 up20180642
94 261 up20180642
        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 303 up20180642
        bsp_zombie    = get_zombie   (); if(bsp_zombie    == NULL) printf("Failed to get zombie\n");
97 261 up20180642
        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 226 up20180642
102 261 up20180642
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
103
    }
104 226 up20180642
105 297 up20180642
    menu_t *main_menu = menu_ctor(consolas);
106 299 up20180642
    menu_add_item(main_menu, "Single player");
107
    menu_add_item(main_menu, "Multiplayer");
108 297 up20180642
    menu_add_item(main_menu, "Chat");
109
    menu_add_item(main_menu, "Exit");
110 234 up20180655
111 297 up20180642
    //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 261 up20180642
    /// loop stuff
117 297 up20180642
    int click = 0;
118 307 up20180642
    uint32_t int_vector = 0;
119 295 up20180642
    int good = true;
120
    while (good) {
121 261 up20180642
        /* Get a request message. */
122 307 up20180642
        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 192 up20180642
129 307 up20180642
                    graph_clear_screen();
130
                    switch(menu_update_state(main_menu, click)){
131
                        case -1: break;
132
                        case  0: singleplayer(); break; //campaign(); break;
133 311 up20180655
                        case  1: multiplayer() ; break;
134 307 up20180642
                        case  2: chat(); break;
135
                        case  3: good = false; break;
136
                    }
137
                    menu_draw(main_menu);
138 192 up20180642
139 307 up20180642
                    click = 0;
140 202 up20180655
141 307 up20180642
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
142
                    sprite_draw(sp_crosshair);
143
                    graph_draw();
144 231 up20180655
145 307 up20180642
                    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 261 up20180642
                    }
156 307 up20180642
                    break;
157
                    case COM1_IRQ: nctp_ih(); break;
158 305 up20180642
                }
159 147 up20180655
            }
160
        }
161 261 up20180642
    }
162 149 up20180655
163 261 up20180642
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
164
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
165 307 up20180642
    basic_sprite_dtor      (bsp_zombie   ); bsp_zombie    = NULL;
166 261 up20180642
    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 243 up20180642
172 261 up20180642
    // Unsubscribe interrupts
173
    if (unsubscribe_all()) {
174
        if (cleanup())
175 305 up20180642
        printf("%s: failed to cleanup.\n", __func__);
176 261 up20180642
        return 1;
177
    }
178 188 up20180642
179 261 up20180642
    if (cleanup()) {
180
        printf("%s: failed to cleanup.\n", __func__);
181
        return 1;
182
    }
183 155 up20180655
184 149 up20180655
    return 0;
185 147 up20180655
}
186 294 up20180642
187 311 up20180655
host_info_t     *host   = NULL;
188
remote_info_t   *remote = NULL;
189 308 up20180655
190 311 up20180655
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 308 up20180655
210 311 up20180655
    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 308 up20180655
215 311 up20180655
    //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 308 up20180655
220 311 up20180655
    /// 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 308 up20180655
233 311 up20180655
                    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 308 up20180655
}
267
268 311 up20180655
static int (multiplayer_host)(void) {
269 308 up20180655
    //int r;
270
271 311 up20180655
    nctp_dump();
272
    nctp_set_processor(multiplayer_process);/*
273

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

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 311 up20180655
    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 308 up20180655

314 311 up20180655
                break;
315 308 up20180655

316 311 up20180655
                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 308 up20180655
    return 0;
331 311 up20180655
}
332
static int (multiplayer_remote)(void) {/*
333
    int r;
334 308 up20180655

335 311 up20180655
    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 300 up20180642
static int (campaign)(void);
398 301 up20180642
static int (zombies)(void);
399 300 up20180642
static int (singleplayer)(void) {
400
401 295 up20180642
    int r;
402
403 300 up20180642
    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 304 up20180642
    uint32_t int_vector = 0;
416 300 up20180642
    int good = true;
417
    while (good) {
418
        /* Get a request message. */
419 304 up20180642
        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 305 up20180642
                    case TIMER0_IRQ:
425 300 up20180642
426 304 up20180642
                    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 300 up20180642
435 304 up20180642
                    click = 0;
436 300 up20180642
437 304 up20180642
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
438
                    sprite_draw(sp_crosshair);
439
                    graph_draw();
440 300 up20180642
441 304 up20180642
                    break;
442 305 up20180642
                    case KBC_IRQ:
443 304 up20180642
                    if ((scancode[0]) == ESC_BREAK_CODE) good = false;
444 305 up20180642
                    case MOUSE_IRQ:
445 304 up20180642
                    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 300 up20180642
                    }
452
                    break;
453 305 up20180642
                    case COM1_IRQ: nctp_ih(); break;
454 304 up20180642
                }
455 300 up20180642
            }
456
        }
457
    }
458
459
    return 0;
460
}
461
462
static int (campaign)(void){
463
464
    int r;
465
466 295 up20180642
    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 302 up20180642
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
472 295 up20180642
    gunner_set_spawn(shooter1, 75, 75);
473
    gunner_set_pos(shooter1, 75, 75);
474
475 302 up20180642
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing, gunner_player, 2);
476 295 up20180642
    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 305 up20180642
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
486 295 up20180642
487 305 up20180642
    //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 295 up20180642
492
    /// loop stuff
493 304 up20180642
    uint32_t int_vector = 0;
494 296 up20180642
    int good = true;
495
    while (good) {
496 304 up20180642
        /* 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 309 up20180642
            if(!good) break;
500 305 up20180642
            if (int_vector & n) {
501
                interrupt_handler(i);
502
                switch (i) {
503
                    case TIMER0_IRQ:
504 295 up20180642
505 305 up20180642
                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
506
                    update_movement(map1, shooter1, keys, shooter_list);
507 295 up20180642
508 305 up20180642
                    update_game_state(map1, shooter_list, bullet_list);
509 295 up20180642
510 305 up20180642
                    //update_scale();
511
                    double angle = get_mouse_angle(shooter1);
512
                    gunner_set_angle(shooter1, angle - M_PI_2);
513 295 up20180642
514 305 up20180642
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
515
                    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
516 295 up20180642
517 305 up20180642
                    graph_clear_screen();
518
                    map_draw   (map1);
519
                    bullet_draw_list(bullet_list);
520
                    gunner_draw_list(shooter_list);
521 295 up20180642
522 305 up20180642
                    text_draw(in_game_timer->text);
523 295 up20180642
524 305 up20180642
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
525
                    sprite_draw(sp_crosshair);
526
                    graph_draw();
527 295 up20180642
528 305 up20180642
                    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 304 up20180642
543 305 up20180642
                    }
544
                    break;
545
                    case COM1_IRQ: nctp_ih(); break;
546
                }
547
            }
548 304 up20180642
        }
549 295 up20180642
    }
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 296 up20180642
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
556 295 up20180642
557 301 up20180642
    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 302 up20180642
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
580 305 up20180642
    gunner_set_spawn(shooter1, 980, 790);
581
    gunner_set_pos(shooter1, 980, 790);
582 301 up20180642
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 305 up20180642
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
589 301 up20180642
590 305 up20180642
    //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 301 up20180642
595
    /// loop stuff
596 304 up20180642
    uint32_t int_vector = 0;
597 301 up20180642
    int good = true;
598 302 up20180642
    int dead = false;
599 301 up20180642
600
    int health = 50;
601
602 309 up20180642
    /** #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 302 up20180642
    while (good && !dead) {
614 304 up20180642
        /* 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 309 up20180642
            if(!good || dead) break;
618 304 up20180642
            if (int_vector & n) {
619 305 up20180642
                interrupt_handler(i);
620
                switch (i) {
621
                    case TIMER0_IRQ:
622
                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
623 309 up20180642
                    if (no_interrupts %  6 == 0){
624
                        map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1));
625
                    }
626 301 up20180642
627 305 up20180642
                    update_movement(map1, shooter1, keys, shooter_list);
628 301 up20180642
629 305 up20180642
                    update_game_state(map1, shooter_list, bullet_list);
630 302 up20180642
631 305 up20180642
                    if(list_find(shooter_list, shooter1) == list_end(shooter_list)){
632
                        good = false;
633
                        dead = true;
634
                        break;
635
                    }
636 301 up20180642
637 305 up20180642
                    double angle = get_mouse_angle(shooter1);
638
                    gunner_set_angle(shooter1, angle - M_PI_2);
639 301 up20180642
640 305 up20180642
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
641
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
642 301 up20180642
643 309 up20180642
                    while(list_size(shooter_list) < ZOMBIES_NUM+1){
644 305 up20180642
                        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 307 up20180642
                        get_random_spawn(map1, zombie, shooter_list);
649 305 up20180642
                        list_push_back(shooter_list, zombie);
650
                    }
651 301 up20180642
652 305 up20180642
                    graph_clear_screen();
653
                    map_draw   (map1);
654
                    bullet_draw_list(bullet_list);
655
                    gunner_draw_list(shooter_list);
656 301 up20180642
657 305 up20180642
                    text_draw(in_game_timer->text);
658 301 up20180642
659 305 up20180642
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
660
                    sprite_draw(sp_crosshair);
661
                    graph_draw();
662 304 up20180642
663 305 up20180642
                    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 301 up20180642
    }
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 295 up20180642
    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 302 up20180642
    if(dead){
699
        printf("YOU DIED\n");
700
    }
701
702 295 up20180642
    timer_dtor(in_game_timer); in_game_timer = NULL;
703
704 294 up20180642
    return SUCCESS;
705
}
706 296 up20180642
707 298 up20180642
#define CHAT_MAX_SIZE   75
708
#define CHAT_MAX_NUM    19
709 297 up20180642
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 305 up20180642
        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 297 up20180642
            }
730 305 up20180642
        }
731
        break;
732 297 up20180642
        default: break;
733
    }
734
}
735 300 up20180642
static int (chat)(void){
736 297 up20180642
    int r;
737
738 298 up20180642
    nctp_dump();
739 297 up20180642
    nctp_set_processor(chat_process);
740
741
    struct packet pp;
742
743 298 up20180642
    char buffer[CHAT_MAX_SIZE] = "";
744 297 up20180642
    rectangle_t *r_buffer = NULL; {
745 298 up20180642
        r_buffer = rectangle_ctor(0,0,900,70);
746 297 up20180642
        rectangle_set_pos(r_buffer, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
747 305 up20180642
        graph_get_YRes()*0.87-rectangle_get_h(r_buffer)/2);
748 297 up20180642
        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 305 up20180642
        rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)/2);
757 297 up20180642
        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 298 up20180642
    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 305 up20180642
        rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)-5);
765 298 up20180642
        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 297 up20180642
774
    /** r_text */ {
775 305 up20180642
    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 311 up20180655
    }
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 305 up20180642
}
793 297 up20180642
794 305 up20180642
/// 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 304 up20180642
                case TIMER0_IRQ:
805 305 up20180642
                graph_clear_screen();
806
                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
807 297 up20180642
808 305 up20180642
                rectangle_draw(r_buffer);
809
                text_draw(t_buffer);
810
                text_draw(t_size);
811 297 up20180642
812 305 up20180642
                rectangle_draw(r_text);
813
                for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_draw(t_text[i]);
814 297 up20180642
815 305 up20180642
                sprite_draw(sp_crosshair);
816
                graph_draw();
817
                break;
818 304 up20180642
                case KBC_IRQ:
819 305 up20180642
                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 297 up20180642
                        }
835
                    }
836 305 up20180642
                    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 304 up20180642
                case MOUSE_IRQ:
850 305 up20180642
                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 304 up20180642
                case COM1_IRQ: nctp_ih(); break;
857 297 up20180642
            }
858
        }
859
    }
860 305 up20180642
}
861 297 up20180642
862 305 up20180642
rectangle_dtor(r_buffer);
863
text_dtor     (t_buffer);
864 297 up20180642
865 305 up20180642
rectangle_dtor(r_text);
866
for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_dtor(t_text[i]);
867 297 up20180642
868 305 up20180642
nctp_set_processor(NULL);
869 298 up20180642
870 305 up20180642
return SUCCESS;
871 296 up20180642
}