Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 316

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
basic_sprite_t       *bsp_crosshair = NULL;
53
basic_sprite_t       *bsp_shooter   = NULL;
54 301 up20180642
basic_sprite_t       *bsp_zombie    = NULL;
55 294 up20180642
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 300 up20180642
static int (singleplayer)(void);
62 311 up20180655
static int (multiplayer)(void);
63 300 up20180642
static int (chat)(void);
64 149 up20180655
int(proj_main_loop)(int argc, char *argv[]) {
65 170 up20180642
66
    int r;
67
68 313 up20180642
    if(font_init()){ printf("Failed to initialize fonts\n"); return 1; }
69 192 up20180642
70 261 up20180642
    /// subscribe interrupts
71
    if (subscribe_all()) { return 1; }
72 170 up20180642
73 297 up20180642
    /// 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 152 up20180642
80 261 up20180642
    /// Load stuff
81
    {
82 297 up20180642
        graph_clear_screen();
83 314 up20180642
        text_t *txt = text_ctor(font_get_default(), "Loading...");
84 309 up20180642
        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 297 up20180642
        text_draw(txt);
89
        text_dtor(txt);
90
        graph_draw();
91 183 up20180642
92 261 up20180642
        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 303 up20180642
        bsp_zombie    = get_zombie   (); if(bsp_zombie    == NULL) printf("Failed to get zombie\n");
95 261 up20180642
        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 226 up20180642
100 261 up20180642
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
101
    }
102 226 up20180642
103 314 up20180642
    menu_t *main_menu = menu_ctor(font_get_default());
104 299 up20180642
    menu_add_item(main_menu, "Single player");
105
    menu_add_item(main_menu, "Multiplayer");
106 297 up20180642
    menu_add_item(main_menu, "Chat");
107
    menu_add_item(main_menu, "Exit");
108 234 up20180655
109 297 up20180642
    //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 261 up20180642
    /// loop stuff
115 297 up20180642
    int click = 0;
116 307 up20180642
    uint32_t int_vector = 0;
117 295 up20180642
    int good = true;
118
    while (good) {
119 261 up20180642
        /* Get a request message. */
120 307 up20180642
        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 192 up20180642
127 307 up20180642
                    graph_clear_screen();
128
                    switch(menu_update_state(main_menu, click)){
129
                        case -1: break;
130
                        case  0: singleplayer(); break; //campaign(); break;
131 311 up20180655
                        case  1: multiplayer() ; break;
132 307 up20180642
                        case  2: chat(); break;
133
                        case  3: good = false; break;
134
                    }
135
                    menu_draw(main_menu);
136 192 up20180642
137 307 up20180642
                    click = 0;
138 202 up20180655
139 307 up20180642
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
140
                    sprite_draw(sp_crosshair);
141
                    graph_draw();
142 231 up20180655
143 307 up20180642
                    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 261 up20180642
                    }
154 307 up20180642
                    break;
155
                    case COM1_IRQ: nctp_ih(); break;
156 305 up20180642
                }
157 147 up20180655
            }
158
        }
159 261 up20180642
    }
160 149 up20180655
161 261 up20180642
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
162
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
163 307 up20180642
    basic_sprite_dtor      (bsp_zombie   ); bsp_zombie    = NULL;
164 261 up20180642
    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 313 up20180642
    if(font_free()){ printf("Failed to free fonts\n"); return 1; }
169 243 up20180642
170 261 up20180642
    // Unsubscribe interrupts
171
    if (unsubscribe_all()) {
172
        if (cleanup())
173 305 up20180642
        printf("%s: failed to cleanup.\n", __func__);
174 261 up20180642
        return 1;
175
    }
176 188 up20180642
177 261 up20180642
    if (cleanup()) {
178
        printf("%s: failed to cleanup.\n", __func__);
179
        return 1;
180
    }
181 155 up20180655
182 149 up20180655
    return 0;
183 147 up20180655
}
184 294 up20180642
185 311 up20180655
host_info_t     *host   = NULL;
186
remote_info_t   *remote = NULL;
187 308 up20180655
188 311 up20180655
static void multiplayer_process(const uint8_t *p, const size_t sz) {
189
    void *dest = NULL;
190
    hltp_type tp = hltp_interpret(p, sz, &dest);
191
    switch(tp){
192
        case hltp_type_host:
193
            host_info_dtor(host);
194
            host = (host_info_t*)dest;
195
            break;
196
        case hltp_type_remote:
197
            remote_info_dtor(remote);
198
            remote = (remote_info_t*)dest;
199
            break;
200
        default: break;
201
    }
202
}
203
static int (multiplayer_host)(void);
204
static int (multiplayer_remote)(void);
205
static int (multiplayer)(void) {
206
    int r;
207 308 up20180655
208 314 up20180642
    menu_t *main_menu = menu_ctor(font_get_default());
209 311 up20180655
    menu_add_item(main_menu, "Create");
210
    menu_add_item(main_menu, "Connect");
211
    menu_add_item(main_menu, "Back");
212 308 up20180655
213 311 up20180655
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
214
    uint8_t last_lb = 0;
215
    struct packet pp;
216
    keys_t *keys = get_key_presses();
217 308 up20180655
218 311 up20180655
    /// loop stuff
219
    int click = 0;
220
    uint32_t int_vector = 0;
221
    int good = true;
222
    while (good) {
223
        /* Get a request message. */
224
        if((r = get_interrupts_vector(&int_vector))) return r;
225
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
226
            if (int_vector & n) {
227
                interrupt_handler(i);
228
                switch (i) {
229
                    case TIMER0_IRQ:
230 308 up20180655
231 311 up20180655
                    graph_clear_screen();
232
                    switch(menu_update_state(main_menu, click)){
233
                        case -1: break;
234
                        case  0: multiplayer_host(); break;
235
                        case  1: multiplayer_remote(); break;
236
                        case  2: good = false; break;
237
                    }
238
                    menu_draw(main_menu);
239
240
                    click = 0;
241
242
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
243
                    sprite_draw(sp_crosshair);
244
                    graph_draw();
245
246
                    break;
247
                    case KBC_IRQ:
248
                    if ((scancode[0]) == ESC_BREAK_CODE) good = false;
249
                    case MOUSE_IRQ:
250
                    if (counter_mouse_ih >= 3) {
251
                        mouse_parse_packet(packet_mouse_ih, &pp);
252
                        update_mouse(&pp);
253
                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
254
                        last_lb = keys->lb_pressed;
255
                        counter_mouse_ih = 0;
256
                    }
257
                    break;
258
                    case COM1_IRQ: nctp_ih(); break;
259
                }
260
            }
261
        }
262
    }
263
    return 0;
264 308 up20180655
}
265
266 311 up20180655
static int (multiplayer_host)(void) {
267 308 up20180655
    //int r;
268
269 311 up20180655
    nctp_dump();
270
    nctp_set_processor(multiplayer_process);/*
271

272 308 up20180655
    ent_set_scale(DEFAULT_SCALE);
273 314 up20180642
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
274 308 up20180655

275
    list_t *shooter_list = list_ctor();
276

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

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

283
    list_insert(shooter_list, list_end(shooter_list), shooter1);
284
    list_insert(shooter_list, list_end(shooter_list), shooter2);
285

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

291
    list_t *bullet_list  = list_ctor();
292

293
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
294
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
295

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

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

312 311 up20180655
                break;
313 308 up20180655

314 311 up20180655
                case KBC_IRQ:
315

316
                break;
317

318
                case MOUSE_IRQ:
319

320

321
                break;
322

323
                case COM1_IRQ: nctp_ih(); break;
324
            }
325
        }
326
    }*/
327
328 308 up20180655
    return 0;
329 311 up20180655
}
330
static int (multiplayer_remote)(void) {/*
331
    int r;
332 308 up20180655

333 311 up20180655
    nctp_dump();
334
    nctp_set_processor(multiplayer_process);
335

336
    ent_set_scale(DEFAULT_SCALE);
337 314 up20180642
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
338 311 up20180655

339
    list_t *shooter_list = list_ctor();
340

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

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

347
    list_insert(shooter_list, list_end(shooter_list), shooter1);
348
    list_insert(shooter_list, list_end(shooter_list), shooter2);
349

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

355
    list_t *bullet_list  = list_ctor();
356

357
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
358
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
359

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

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

376
                break;
377

378
                case KBC_IRQ:
379

380
                break;
381

382
                case MOUSE_IRQ:
383

384

385
                break;
386

387
                case COM1_IRQ: nctp_ih(); break;
388
            }
389
        }
390
    }*/
391
392
    return 0;
393
}
394
395 300 up20180642
static int (campaign)(void);
396 301 up20180642
static int (zombies)(void);
397 300 up20180642
static int (singleplayer)(void) {
398
399 295 up20180642
    int r;
400
401 314 up20180642
    menu_t *main_menu = menu_ctor(font_get_default());
402 300 up20180642
    menu_add_item(main_menu, "Campaign");
403
    menu_add_item(main_menu, "Zombies");
404
    menu_add_item(main_menu, "Back");
405
406
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
407
    uint8_t last_lb = 0;
408
    struct packet pp;
409
    keys_t *keys = get_key_presses();
410
411
    /// loop stuff
412
    int click = 0;
413 304 up20180642
    uint32_t int_vector = 0;
414 300 up20180642
    int good = true;
415
    while (good) {
416
        /* Get a request message. */
417 304 up20180642
        if((r = get_interrupts_vector(&int_vector))) return r;
418
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
419
            if (int_vector & n) {
420
                interrupt_handler(i);
421
                switch (i) {
422 305 up20180642
                    case TIMER0_IRQ:
423 300 up20180642
424 304 up20180642
                    graph_clear_screen();
425
                    switch(menu_update_state(main_menu, click)){
426
                        case -1: break;
427
                        case  0: campaign(); break;
428
                        case  1: zombies(); break;
429
                        case  2: good = false; break;
430
                    }
431
                    menu_draw(main_menu);
432 300 up20180642
433 304 up20180642
                    click = 0;
434 300 up20180642
435 304 up20180642
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
436
                    sprite_draw(sp_crosshair);
437
                    graph_draw();
438 300 up20180642
439 304 up20180642
                    break;
440 305 up20180642
                    case KBC_IRQ:
441 304 up20180642
                    if ((scancode[0]) == ESC_BREAK_CODE) good = false;
442 305 up20180642
                    case MOUSE_IRQ:
443 304 up20180642
                    if (counter_mouse_ih >= 3) {
444
                        mouse_parse_packet(packet_mouse_ih, &pp);
445
                        update_mouse(&pp);
446
                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
447
                        last_lb = keys->lb_pressed;
448
                        counter_mouse_ih = 0;
449 300 up20180642
                    }
450
                    break;
451 305 up20180642
                    case COM1_IRQ: nctp_ih(); break;
452 304 up20180642
                }
453 300 up20180642
            }
454
        }
455
    }
456
457
    return 0;
458
}
459
460
static int (campaign)(void){
461
462
    int r;
463
464 295 up20180642
    ent_set_scale(DEFAULT_SCALE);
465 314 up20180642
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
466 295 up20180642
467
    list_t *shooter_list = list_ctor();
468
469 302 up20180642
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
470 295 up20180642
    gunner_set_spawn(shooter1, 75, 75);
471
    gunner_set_pos(shooter1, 75, 75);
472
473 302 up20180642
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing, gunner_player, 2);
474 295 up20180642
    gunner_set_spawn(shooter2, 975, 75);
475
    gunner_set_pos(shooter2, 775, 75);
476
477
    list_insert(shooter_list, list_end(shooter_list), shooter1);
478
    list_insert(shooter_list, list_end(shooter_list), shooter2);
479
480
    list_t *bullet_list  = list_ctor();
481
482
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
483 305 up20180642
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
484 295 up20180642
485 305 up20180642
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
486
    uint8_t last_lb = 0;
487
    struct packet pp;
488
    keys_t *keys = get_key_presses();
489 295 up20180642
490
    /// loop stuff
491 304 up20180642
    uint32_t int_vector = 0;
492 296 up20180642
    int good = true;
493
    while (good) {
494 304 up20180642
        /* Get a request message. */
495
        if((r = get_interrupts_vector(&int_vector))) return r;
496
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
497 309 up20180642
            if(!good) break;
498 305 up20180642
            if (int_vector & n) {
499
                interrupt_handler(i);
500
                switch (i) {
501
                    case TIMER0_IRQ:
502 295 up20180642
503 305 up20180642
                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
504
                    update_movement(map1, shooter1, keys, shooter_list);
505 295 up20180642
506 305 up20180642
                    update_game_state(map1, shooter_list, bullet_list);
507 295 up20180642
508 305 up20180642
                    //update_scale();
509
                    double angle = get_mouse_angle(shooter1);
510
                    gunner_set_angle(shooter1, angle - M_PI_2);
511 295 up20180642
512 305 up20180642
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
513
                    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
514 295 up20180642
515 305 up20180642
                    graph_clear_screen();
516
                    map_draw   (map1);
517
                    bullet_draw_list(bullet_list);
518
                    gunner_draw_list(shooter_list);
519 295 up20180642
520 305 up20180642
                    text_draw(in_game_timer->text);
521 295 up20180642
522 305 up20180642
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
523
                    sprite_draw(sp_crosshair);
524
                    graph_draw();
525 295 up20180642
526 305 up20180642
                    break;
527
                    case KBC_IRQ:
528
                    if ((scancode[0]) == ESC_BREAK_CODE) {
529
                        good = false;
530
                    }
531
                    break;
532
                    case MOUSE_IRQ:
533
                    if (counter_mouse_ih >= 3) {
534
                        mouse_parse_packet(packet_mouse_ih, &pp);
535
                        update_mouse(&pp);
536
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
537
                        shoot_bullet(shooter1, bullet_list, bsp_bullet);
538
                        last_lb = keys->lb_pressed;
539
                        counter_mouse_ih = 0;
540 304 up20180642
541 305 up20180642
                    }
542
                    break;
543
                    case COM1_IRQ: nctp_ih(); break;
544
                }
545
            }
546 304 up20180642
        }
547 295 up20180642
    }
548
549
    while(list_size(shooter_list) > 0){
550
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
551
        gunner_dtor(p);
552
    }
553 296 up20180642
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
554 295 up20180642
555 301 up20180642
    while(list_size(bullet_list) > 0){
556
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
557
        bullet_dtor(p);
558
    }
559
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
560
561
    timer_dtor(in_game_timer); in_game_timer = NULL;
562
563
    return SUCCESS;
564
}
565
566
#define ZOMBIES_NUM             5
567
#define ZOMBIE_HEALTH_FACTOR    1.1
568
static int (zombies)(void){
569
570
    int r;
571
572
    ent_set_scale(DEFAULT_SCALE);
573 314 up20180642
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
574 301 up20180642
575
    list_t *shooter_list = list_ctor();
576
577 302 up20180642
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
578 305 up20180642
    gunner_set_spawn(shooter1, 980, 790);
579
    gunner_set_pos(shooter1, 980, 790);
580 301 up20180642
581
    list_insert(shooter_list, list_end(shooter_list), shooter1);
582
583
    list_t *bullet_list  = list_ctor();
584
585
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
586 305 up20180642
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
587 301 up20180642
588 305 up20180642
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
589
    uint8_t last_lb = 0;
590
    struct packet pp;
591
    keys_t *keys = get_key_presses();
592 301 up20180642
593
    /// loop stuff
594 304 up20180642
    uint32_t int_vector = 0;
595 301 up20180642
    int good = true;
596 302 up20180642
    int dead = false;
597 301 up20180642
598
    int health = 50;
599
600 309 up20180642
    /** #DEV */ /* {
601
        gunner_t *zombie = gunner_ctor(bsp_zombie, bsp_nothing, gunner_melee | gunner_follow, 3);
602
        gunner_set_health(zombie, health);
603
        gunner_set_curr_health(zombie, health);
604
        health *= ZOMBIE_HEALTH_FACTOR;
605
        gunner_set_pos(zombie, 1100, 75);
606
        list_push_back(shooter_list, zombie);
607
    }*/ //\#DEV
608
609
    map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1));
610
611 302 up20180642
    while (good && !dead) {
612 304 up20180642
        /* Get a request message. */
613
        if((r = get_interrupts_vector(&int_vector))) return r;
614
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
615 309 up20180642
            if(!good || dead) break;
616 304 up20180642
            if (int_vector & n) {
617 305 up20180642
                interrupt_handler(i);
618
                switch (i) {
619
                    case TIMER0_IRQ:
620
                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
621 309 up20180642
                    if (no_interrupts %  6 == 0){
622
                        map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1));
623
                    }
624 301 up20180642
625 305 up20180642
                    update_movement(map1, shooter1, keys, shooter_list);
626 301 up20180642
627 305 up20180642
                    update_game_state(map1, shooter_list, bullet_list);
628 302 up20180642
629 305 up20180642
                    if(list_find(shooter_list, shooter1) == list_end(shooter_list)){
630
                        good = false;
631
                        dead = true;
632
                        break;
633
                    }
634 301 up20180642
635 305 up20180642
                    double angle = get_mouse_angle(shooter1);
636
                    gunner_set_angle(shooter1, angle - M_PI_2);
637 301 up20180642
638 305 up20180642
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
639
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
640 301 up20180642
641 309 up20180642
                    while(list_size(shooter_list) < ZOMBIES_NUM+1){
642 305 up20180642
                        gunner_t *zombie = gunner_ctor(bsp_zombie, bsp_nothing, gunner_melee | gunner_follow, 3);
643
                        gunner_set_health(zombie, health);
644
                        gunner_set_curr_health(zombie, health);
645
                        health *= ZOMBIE_HEALTH_FACTOR;
646 307 up20180642
                        get_random_spawn(map1, zombie, shooter_list);
647 305 up20180642
                        list_push_back(shooter_list, zombie);
648
                    }
649 301 up20180642
650 305 up20180642
                    graph_clear_screen();
651
                    map_draw   (map1);
652
                    bullet_draw_list(bullet_list);
653
                    gunner_draw_list(shooter_list);
654 301 up20180642
655 305 up20180642
                    text_draw(in_game_timer->text);
656 301 up20180642
657 305 up20180642
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
658
                    sprite_draw(sp_crosshair);
659
                    graph_draw();
660 304 up20180642
661 305 up20180642
                    break;
662
                    case KBC_IRQ:
663
                    if ((scancode[0]) == ESC_BREAK_CODE) {
664
                        good = false;
665
                    }
666
                    break;
667
                    case MOUSE_IRQ:
668
                    if (counter_mouse_ih >= 3) {
669
                        mouse_parse_packet(packet_mouse_ih, &pp);
670
                        update_mouse(&pp);
671
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
672
                        shoot_bullet(shooter1, bullet_list, bsp_bullet);
673
                        last_lb = keys->lb_pressed;
674
                        counter_mouse_ih = 0;
675
676
                    }
677
                    break;
678
                    case COM1_IRQ: nctp_ih(); break;
679
                }
680
            }
681
        }
682 301 up20180642
    }
683
684
    while(list_size(shooter_list) > 0){
685
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
686
        gunner_dtor(p);
687
    }
688
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
689
690 295 up20180642
    while(list_size(bullet_list) > 0){
691
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
692
        bullet_dtor(p);
693
    }
694
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
695
696 302 up20180642
    if(dead){
697
        printf("YOU DIED\n");
698
    }
699
700 295 up20180642
    timer_dtor(in_game_timer); in_game_timer = NULL;
701
702 294 up20180642
    return SUCCESS;
703
}
704 296 up20180642
705 298 up20180642
#define CHAT_MAX_SIZE   75
706
#define CHAT_MAX_NUM    19
707 297 up20180642
text_t      *t_text[CHAT_MAX_NUM] = {NULL};
708
rectangle_t *r_text               =  NULL;
709
static void chat_process(const uint8_t *p, const size_t sz){
710
    char buffer2[CHAT_MAX_NUM+3];
711
    void *dest = NULL;
712
    hltp_type tp = hltp_interpret(p, sz, &dest);
713
    switch(tp){
714
        case hltp_type_string:
715 305 up20180642
        strcpy(buffer2, dest);
716
        strncat(buffer2, " <", 2);
717
        for(size_t i = CHAT_MAX_NUM-1; i; --i)
718 314 up20180642
        text_set_string(t_text[i], text_get_string(t_text[i-1]));
719
        text_set_string(t_text[0], buffer2);
720 305 up20180642
        for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
721
            if(text_get_string(t_text[i])[0] == '>'){
722
                text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
723
                text_set_halign(t_text[i], text_halign_left);
724
            }else{
725
                text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
726
                text_set_halign(t_text[i], text_halign_right);
727 297 up20180642
            }
728 305 up20180642
        }
729
        break;
730 297 up20180642
        default: break;
731
    }
732
}
733 300 up20180642
static int (chat)(void){
734 297 up20180642
    int r;
735
736 298 up20180642
    nctp_dump();
737 297 up20180642
    nctp_set_processor(chat_process);
738
739
    struct packet pp;
740
741 298 up20180642
    char buffer[CHAT_MAX_SIZE] = "";
742 297 up20180642
    rectangle_t *r_buffer = NULL; {
743 298 up20180642
        r_buffer = rectangle_ctor(0,0,900,70);
744 297 up20180642
        rectangle_set_pos(r_buffer, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
745 305 up20180642
        graph_get_YRes()*0.87-rectangle_get_h(r_buffer)/2);
746 297 up20180642
        rectangle_set_fill_color   (r_buffer, GRAPH_BLACK);
747
        rectangle_set_outline_width(r_buffer, 2);
748
        rectangle_set_outline_color(r_buffer, GRAPH_WHITE);
749
        rectangle_set_fill_trans(r_buffer, GRAPH_TRANSPARENT);
750
    }
751
    text_t      *t_buffer = NULL; {
752 314 up20180642
        t_buffer = text_ctor(font_get_default(), "");
753 297 up20180642
        text_set_pos(t_buffer, rectangle_get_x(r_buffer)+50,
754 305 up20180642
        rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)/2);
755 297 up20180642
        text_set_halign(t_buffer, text_halign_left);
756
        text_set_valign(t_buffer, text_valign_center);
757
        text_set_color (t_buffer, TEXT_COLOR);
758
    }
759 298 up20180642
    text_t      *t_size   = NULL; {
760 314 up20180642
        t_size = text_ctor(font_get_default(), "");
761 298 up20180642
        text_set_pos(t_size, rectangle_get_x(r_buffer)+rectangle_get_w(r_buffer)-5,
762 305 up20180642
        rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)-5);
763 298 up20180642
        text_set_halign(t_size, text_halign_right);
764
        text_set_valign(t_size, text_valign_bottom);
765
        text_set_color (t_size, TEXT_COLOR);
766
        text_set_size  (t_size, 18);
767
        char buffer2[20];
768
        sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
769 314 up20180642
        text_set_string(t_size, buffer2);
770 298 up20180642
    }
771 297 up20180642
772
    /** r_text */ {
773 305 up20180642
    r_text = rectangle_ctor(0,0,900,550);
774
    rectangle_set_pos(r_text, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
775
    graph_get_YRes()*0.09);
776
    rectangle_set_fill_color   (r_text, GRAPH_BLACK);
777
    rectangle_set_outline_width(r_text, 2);
778
    rectangle_set_outline_color(r_text, GRAPH_WHITE);
779
    rectangle_set_fill_trans(r_text, GRAPH_TRANSPARENT);
780 311 up20180655
    }
781
    /** t_text */ {
782
    for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
783 314 up20180642
        t_text[i] = text_ctor(font_get_default(), " ");
784 311 up20180655
        text_set_pos(t_text[i], rectangle_get_x(r_text)+50,
785
        rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-25*i);
786
        text_set_halign(t_text[i], text_halign_left);
787
        text_set_valign(t_text[i], text_valign_bottom);
788
        text_set_color (t_text[i], TEXT_COLOR);
789
    }
790 305 up20180642
}
791 297 up20180642
792 305 up20180642
/// loop stuff
793
uint32_t int_vector = 0;
794
int good = true;
795
while (good) {
796
    /* Get a request message. */
797
    if((r = get_interrupts_vector(&int_vector))) return r;
798
    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
799
        if (int_vector & n) {
800
            interrupt_handler(i);
801
            switch (i) {
802 304 up20180642
                case TIMER0_IRQ:
803 305 up20180642
                graph_clear_screen();
804
                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
805 297 up20180642
806 305 up20180642
                rectangle_draw(r_buffer);
807
                text_draw(t_buffer);
808
                text_draw(t_size);
809 297 up20180642
810 305 up20180642
                rectangle_draw(r_text);
811
                for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_draw(t_text[i]);
812 297 up20180642
813 305 up20180642
                sprite_draw(sp_crosshair);
814
                graph_draw();
815
                break;
816 304 up20180642
                case KBC_IRQ:
817 305 up20180642
                if      (scancode[0] == ESC_BREAK_CODE) good = false;
818
                else if (scancode[0] == ENTER_MAKE_CODE) {
819
                    hltp_send_string(buffer);
820
                    char buffer2[CHAT_MAX_SIZE+3] = "> ";
821
                    strncat(buffer2, buffer, strlen(buffer));
822
                    for(size_t i = CHAT_MAX_NUM-1; i; --i)
823 314 up20180642
                    text_set_string(t_text[i], text_get_string(t_text[i-1]));
824
                    text_set_string(t_text[0], buffer2);
825 305 up20180642
                    for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
826
                        if(text_get_string(t_text[i])[0] == '>'){
827
                            text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
828
                            text_set_halign(t_text[i], text_halign_left);
829
                        }else{
830
                            text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
831
                            text_set_halign(t_text[i], text_halign_right);
832 297 up20180642
                        }
833
                    }
834 305 up20180642
                    buffer[0] = '\0';
835
                } else if(scancode[0] == BACKSPACE_MAKE_CODE){
836
                    buffer[strlen(buffer)-1] = '\0';
837
                } else {
838
                    char c = map_makecode(scancode[0]);
839
                    if (c == ERROR_CODE) break;
840
                    if(strlen(buffer) < CHAT_MAX_SIZE) strncat(buffer, &c, 1);
841
                    else                               printf("Char limit exceeded\n");
842
                }
843 314 up20180642
                text_set_string(t_buffer, buffer);
844 305 up20180642
                char buffer2[20];
845
                sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
846 314 up20180642
                text_set_string(t_size, buffer2);
847 304 up20180642
                case MOUSE_IRQ:
848 305 up20180642
                if (counter_mouse_ih >= 3) {
849
                    mouse_parse_packet(packet_mouse_ih, &pp);
850
                    update_mouse(&pp);
851
                    counter_mouse_ih = 0;
852
                }
853
                break;
854 304 up20180642
                case COM1_IRQ: nctp_ih(); break;
855 297 up20180642
            }
856
        }
857
    }
858 305 up20180642
}
859 297 up20180642
860 305 up20180642
rectangle_dtor(r_buffer);
861
text_dtor     (t_buffer);
862 297 up20180642
863 305 up20180642
rectangle_dtor(r_text);
864
for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_dtor(t_text[i]);
865 297 up20180642
866 305 up20180642
nctp_set_processor(NULL);
867 298 up20180642
868 305 up20180642
return SUCCESS;
869 296 up20180642
}