Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 320

History | View | Annotate | Download (36.7 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 319 up20180642
    font_free();
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 320 up20180655
host_info_t     *host_info   = NULL;
186
remote_info_t   *remote_info = NULL;
187
bullet_info_t   *bullet_info = NULL;
188 308 up20180655
189 311 up20180655
static void multiplayer_process(const uint8_t *p, const size_t sz) {
190
    void *dest = NULL;
191
    hltp_type tp = hltp_interpret(p, sz, &dest);
192
    switch(tp){
193
        case hltp_type_host:
194 320 up20180655
            host_info_dtor(host_info);
195
            host_info = (host_info_t*)dest;
196 311 up20180655
            break;
197
        case hltp_type_remote:
198 320 up20180655
            remote_info_dtor(remote_info);
199
            remote_info = (remote_info_t*)dest;
200 311 up20180655
            break;
201 320 up20180655
        case hltp_type_bullet:
202
            bullet_info_dtor(bullet_info);
203
            bullet_info = (bullet_info_t*)dest;
204
            break;
205 311 up20180655
        default: break;
206
    }
207
}
208
static int (multiplayer_host)(void);
209
static int (multiplayer_remote)(void);
210
static int (multiplayer)(void) {
211
    int r;
212 308 up20180655
213 314 up20180642
    menu_t *main_menu = menu_ctor(font_get_default());
214 311 up20180655
    menu_add_item(main_menu, "Create");
215
    menu_add_item(main_menu, "Connect");
216
    menu_add_item(main_menu, "Back");
217 308 up20180655
218 311 up20180655
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
219
    uint8_t last_lb = 0;
220
    struct packet pp;
221
    keys_t *keys = get_key_presses();
222 308 up20180655
223 311 up20180655
    /// loop stuff
224
    int click = 0;
225
    uint32_t int_vector = 0;
226
    int good = true;
227
    while (good) {
228
        /* Get a request message. */
229
        if((r = get_interrupts_vector(&int_vector))) return r;
230
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
231
            if (int_vector & n) {
232
                interrupt_handler(i);
233
                switch (i) {
234
                    case TIMER0_IRQ:
235 308 up20180655
236 311 up20180655
                    graph_clear_screen();
237
                    switch(menu_update_state(main_menu, click)){
238
                        case -1: break;
239
                        case  0: multiplayer_host(); break;
240
                        case  1: multiplayer_remote(); break;
241
                        case  2: good = false; break;
242
                    }
243
                    menu_draw(main_menu);
244
245
                    click = 0;
246
247
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
248
                    sprite_draw(sp_crosshair);
249
                    graph_draw();
250
251
                    break;
252
                    case KBC_IRQ:
253
                    if ((scancode[0]) == ESC_BREAK_CODE) good = false;
254
                    case MOUSE_IRQ:
255
                    if (counter_mouse_ih >= 3) {
256
                        mouse_parse_packet(packet_mouse_ih, &pp);
257
                        update_mouse(&pp);
258
                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
259
                        last_lb = keys->lb_pressed;
260
                        counter_mouse_ih = 0;
261
                    }
262
                    break;
263
                    case COM1_IRQ: nctp_ih(); break;
264
                }
265
            }
266
        }
267
    }
268
    return 0;
269 308 up20180655
}
270
271 311 up20180655
static int (multiplayer_host)(void) {
272 320 up20180655
    int r;
273 308 up20180655
274 311 up20180655
    nctp_dump();
275 320 up20180655
    nctp_set_processor(multiplayer_process);
276 311 up20180655
277 308 up20180655
    ent_set_scale(DEFAULT_SCALE);
278 314 up20180642
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
279 308 up20180655
280
    list_t *shooter_list = list_ctor();
281
282
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
283
    gunner_set_spawn(shooter1, 75, 75);
284
285 320 up20180655
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 2); if(shooter2 == NULL) printf("Failed to get shooter2\n");
286 308 up20180655
    gunner_set_spawn(shooter2, 975, 75);
287
288
    list_insert(shooter_list, list_end(shooter_list), shooter1);
289
    list_insert(shooter_list, list_end(shooter_list), shooter2);
290
291
    do {
292
        get_random_spawn(map1, shooter1, shooter_list);
293
        get_random_spawn(map1, shooter2, shooter_list);
294 320 up20180655
    } while (distance_gunners(shooter1, shooter2) < 500);
295 308 up20180655
296 320 up20180655
    host_info = host_info_ctor(shooter1, shooter2);
297
    remote_info = remote_info_ctor();
298
    bullet_info = bullet_info_ctor();
299
300 308 up20180655
    list_t *bullet_list  = list_ctor();
301
302
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
303
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
304
305
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
306
    uint8_t last_lb = 0;
307
    struct packet pp;
308
    keys_t *keys = get_key_presses();
309
    /// loop stuff
310
    uint32_t int_vector = 0;
311
    int good = true;
312 311 up20180655
    int state = 0; // -1 for remote win, 0 for draw, 1 for host win
313 320 up20180655
    list_node_t *p1, *p2; // player states
314
    int state_1, state_2;
315 311 up20180655
    while (good) {
316
        if ((r = get_interrupts_vector(&int_vector))) return r;
317
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
318 320 up20180655
            if (int_vector & n) {
319
                interrupt_handler(i);
320
                switch (i) {
321
                    case TIMER0_IRQ:
322
                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
323 308 up20180655
324 320 up20180655
                    update_movement(map1, shooter1, keys, shooter_list);
325
                    update_movement(map1, shooter2, &(remote_info->remote_keys_pressed), shooter_list);
326 308 up20180655
327 320 up20180655
                    update_game_state(map1, shooter_list, bullet_list);
328 311 up20180655
329 320 up20180655
                    p1 = list_find(shooter_list, shooter1);
330
                    p2 = list_find(shooter_list, shooter2);
331 311 up20180655
332 320 up20180655
                    if ((state_1 = (p1 == list_end(shooter_list))) || (state_2 = (p2 == list_end(shooter_list)))) {
333
                        state = state_1 - state_2;
334
                        good = false;
335
                        break;
336
                    }
337 311 up20180655
338 320 up20180655
                    double angle = get_mouse_angle(shooter1);
339
                    gunner_set_angle(shooter1, angle - M_PI_2);
340 311 up20180655
341 320 up20180655
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
342
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
343 311 up20180655
344 320 up20180655
                    gunner_set_angle(shooter2, remote_info->remote_angle);
345
346
                    build_host_structure(host_info, shooter1, shooter2, bullet_list);
347
348
                    hltp_send_host_info(host_info);
349
350
                    graph_clear_screen();
351
                    map_draw   (map1);
352
                    bullet_draw_list(bullet_list);
353
                    gunner_draw_list(shooter_list);
354
355
                    text_draw(in_game_timer->text);
356
357
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
358
                    sprite_draw(sp_crosshair);
359
                    graph_draw();
360
361
                    break;
362
                    case KBC_IRQ:
363
                    if ((scancode[0]) == ESC_BREAK_CODE) {
364
                        good = false;
365
                    }
366
                    break;
367
                    case MOUSE_IRQ:
368
                    if (counter_mouse_ih >= 3) {
369
                        mouse_parse_packet(packet_mouse_ih, &pp);
370
                        update_mouse(&pp);
371
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
372
                        shoot_bullet(shooter1, bullet_list, bsp_bullet);
373
                        last_lb = keys->lb_pressed;
374
                        counter_mouse_ih = 0;
375
                    }
376
                    break;
377
378
                    case COM1_IRQ:
379
                        nctp_ih();
380
                        if (bullet_info->new_bullet) {
381
                            shoot_bullet(shooter2, bullet_list, bsp_bullet);
382
                            bullet_info->new_bullet = false;
383
                        }
384
                        break;
385
                }
386 311 up20180655
            }
387
        }
388 320 up20180655
    }
389 311 up20180655
390 320 up20180655
    while(list_size(shooter_list) > 0){
391
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
392
        gunner_dtor(p);
393
    }
394
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
395
396
    while(list_size(bullet_list) > 0){
397
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
398
        bullet_dtor(p);
399
    }
400
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
401
402
    host_info_dtor(host_info);
403
    remote_info_dtor(remote_info);
404
    bullet_info_dtor(bullet_info);
405
406
    timer_dtor(in_game_timer); in_game_timer = NULL;
407
408 308 up20180655
    return 0;
409 311 up20180655
}
410 320 up20180655
static int (multiplayer_remote)(void) {
411 311 up20180655
    int r;
412 308 up20180655
413 311 up20180655
    nctp_dump();
414
    nctp_set_processor(multiplayer_process);
415
416
    ent_set_scale(DEFAULT_SCALE);
417 314 up20180642
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
418 311 up20180655
419
    list_t *shooter_list = list_ctor();
420
421 320 up20180655
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 2); if(shooter1 == NULL) printf("Failed to get shooter1\n");
422 311 up20180655
    gunner_set_spawn(shooter1, 75, 75);
423
424
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter2 == NULL) printf("Failed to get shooter2\n");
425
    gunner_set_spawn(shooter2, 975, 75);
426
427
    list_insert(shooter_list, list_end(shooter_list), shooter1);
428
    list_insert(shooter_list, list_end(shooter_list), shooter2);
429
430 320 up20180655
    host_info = host_info_ctor(shooter2, shooter1);
431
    remote_info = remote_info_ctor();
432
    bullet_info = bullet_info_ctor();
433 311 up20180655
434
    list_t *bullet_list  = list_ctor();
435
436
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
437
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
438
439
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
440
    uint8_t last_lb = 0;
441
    struct packet pp;
442
    keys_t *keys = get_key_presses();
443
444
    /// loop stuff
445
    uint32_t int_vector = 0;
446
    int good = true;
447
    while (good) {
448
        if ((r = get_interrupts_vector(&int_vector))) return r;
449
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
450 320 up20180655
            if (int_vector & n) {
451
                interrupt_handler(i);
452
                switch (i) {
453
                    case TIMER0_IRQ:
454
                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
455 311 up20180655
456 320 up20180655
                    double angle = get_mouse_angle(shooter1);
457 311 up20180655
458 320 up20180655
                    build_remote_structure(remote_info, keys, angle);
459 311 up20180655
460 320 up20180655
                    hltp_send_remote_info(remote_info);
461 311 up20180655
462 320 up20180655
                    gunner_set_pos(shooter1, host_info->remote_x, host_info->remote_y);
463
                    gunner_set_angle(shooter1, host_info->remote_angle);
464
                    gunner_set_health(shooter1, host_info->remote_health);
465
                    gunner_set_curr_health(shooter1, host_info->remote_current_health);
466 311 up20180655
467 320 up20180655
                    gunner_set_pos(shooter2, host_info->host_x, host_info->host_y);
468
                    gunner_set_angle(shooter2, host_info->host_angle);
469
                    gunner_set_health(shooter2, host_info->host_health);
470
                    gunner_set_curr_health(shooter2, host_info->host_current_health);
471 311 up20180655
472 320 up20180655
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
473
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
474 311 up20180655
475 320 up20180655
                    for (size_t i = 0; i < host_info->no_bullets; i++) {
476
                        if (host_info->bullets_shooter[i]) { // remote
477
                            bullet_t *bullet = bullet_ctor(shooter1, bsp_bullet, host_info->bullets_x[i], host_info->bullets_y[i], host_info->bullets_vx[i], host_info->bullets_vy[i]);
478
                            list_insert(bullet_list, list_end(bullet_list), bullet);
479
                        } else { // host
480
                            bullet_t *bullet = bullet_ctor(shooter2, bsp_bullet, host_info->bullets_x[i], host_info->bullets_y[i], host_info->bullets_vx[i], host_info->bullets_vy[i]);
481
                            list_insert(bullet_list, list_end(bullet_list), bullet);
482
                        }
483
                    }
484
485
                    graph_clear_screen();
486
                    map_draw   (map1);
487
                    bullet_draw_list(bullet_list);
488
                    gunner_draw_list(shooter_list);
489
490
                    text_draw(in_game_timer->text);
491
492
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
493
                    sprite_draw(sp_crosshair);
494
                    graph_draw();
495
496
                    while(list_size(bullet_list) > 0){
497
                        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
498
                        bullet_dtor(p);
499
                    }
500
501
                    break;
502
                    case KBC_IRQ:
503
                    if ((scancode[0]) == ESC_BREAK_CODE) {
504
                        good = false;
505
                    }
506
                    break;
507
                    case MOUSE_IRQ:
508
                    if (counter_mouse_ih >= 3) {
509
                        mouse_parse_packet(packet_mouse_ih, &pp);
510
                        update_mouse(&pp);
511
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed) {
512
                            bullet_info->new_bullet = true;
513
                            hltp_send_bullet_info(bullet_info);
514
                        }
515
                        last_lb = keys->lb_pressed;
516
                        counter_mouse_ih = 0;
517
                    }
518
                    break;
519
520
                    case COM1_IRQ: nctp_ih(); break;
521
                }
522 311 up20180655
            }
523
        }
524 320 up20180655
    }
525 311 up20180655
526 320 up20180655
    while(list_size(shooter_list) > 0){
527
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
528
        gunner_dtor(p);
529
    }
530
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
531
532
    while(list_size(bullet_list) > 0){
533
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
534
        bullet_dtor(p);
535
    }
536
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
537
538
    host_info_dtor(host_info);
539
    remote_info_dtor(remote_info);
540
    bullet_info_dtor(bullet_info);
541
542
    timer_dtor(in_game_timer); in_game_timer = NULL;
543
544 311 up20180655
    return 0;
545
}
546
547 300 up20180642
static int (campaign)(void);
548 301 up20180642
static int (zombies)(void);
549 300 up20180642
static int (singleplayer)(void) {
550
551 295 up20180642
    int r;
552
553 314 up20180642
    menu_t *main_menu = menu_ctor(font_get_default());
554 300 up20180642
    menu_add_item(main_menu, "Campaign");
555
    menu_add_item(main_menu, "Zombies");
556
    menu_add_item(main_menu, "Back");
557
558
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
559
    uint8_t last_lb = 0;
560
    struct packet pp;
561
    keys_t *keys = get_key_presses();
562
563
    /// loop stuff
564
    int click = 0;
565 304 up20180642
    uint32_t int_vector = 0;
566 300 up20180642
    int good = true;
567
    while (good) {
568
        /* Get a request message. */
569 304 up20180642
        if((r = get_interrupts_vector(&int_vector))) return r;
570
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
571
            if (int_vector & n) {
572
                interrupt_handler(i);
573
                switch (i) {
574 305 up20180642
                    case TIMER0_IRQ:
575 300 up20180642
576 304 up20180642
                    graph_clear_screen();
577
                    switch(menu_update_state(main_menu, click)){
578
                        case -1: break;
579
                        case  0: campaign(); break;
580
                        case  1: zombies(); break;
581
                        case  2: good = false; break;
582
                    }
583
                    menu_draw(main_menu);
584 300 up20180642
585 304 up20180642
                    click = 0;
586 300 up20180642
587 304 up20180642
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
588
                    sprite_draw(sp_crosshair);
589
                    graph_draw();
590 300 up20180642
591 304 up20180642
                    break;
592 305 up20180642
                    case KBC_IRQ:
593 304 up20180642
                    if ((scancode[0]) == ESC_BREAK_CODE) good = false;
594 305 up20180642
                    case MOUSE_IRQ:
595 304 up20180642
                    if (counter_mouse_ih >= 3) {
596
                        mouse_parse_packet(packet_mouse_ih, &pp);
597
                        update_mouse(&pp);
598
                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
599
                        last_lb = keys->lb_pressed;
600
                        counter_mouse_ih = 0;
601 300 up20180642
                    }
602
                    break;
603 305 up20180642
                    case COM1_IRQ: nctp_ih(); break;
604 304 up20180642
                }
605 300 up20180642
            }
606
        }
607
    }
608
609
    return 0;
610
}
611
612
static int (campaign)(void){
613
614
    int r;
615
616 295 up20180642
    ent_set_scale(DEFAULT_SCALE);
617 314 up20180642
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
618 295 up20180642
619
    list_t *shooter_list = list_ctor();
620
621 302 up20180642
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
622 295 up20180642
    gunner_set_spawn(shooter1, 75, 75);
623
    gunner_set_pos(shooter1, 75, 75);
624
625 302 up20180642
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing, gunner_player, 2);
626 295 up20180642
    gunner_set_spawn(shooter2, 975, 75);
627
    gunner_set_pos(shooter2, 775, 75);
628
629
    list_insert(shooter_list, list_end(shooter_list), shooter1);
630
    list_insert(shooter_list, list_end(shooter_list), shooter2);
631
632
    list_t *bullet_list  = list_ctor();
633
634
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
635 305 up20180642
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
636 295 up20180642
637 305 up20180642
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
638
    uint8_t last_lb = 0;
639
    struct packet pp;
640
    keys_t *keys = get_key_presses();
641 295 up20180642
642
    /// loop stuff
643 304 up20180642
    uint32_t int_vector = 0;
644 296 up20180642
    int good = true;
645
    while (good) {
646 304 up20180642
        /* Get a request message. */
647
        if((r = get_interrupts_vector(&int_vector))) return r;
648
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
649 309 up20180642
            if(!good) break;
650 305 up20180642
            if (int_vector & n) {
651
                interrupt_handler(i);
652
                switch (i) {
653
                    case TIMER0_IRQ:
654 295 up20180642
655 305 up20180642
                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
656
                    update_movement(map1, shooter1, keys, shooter_list);
657 295 up20180642
658 305 up20180642
                    update_game_state(map1, shooter_list, bullet_list);
659 295 up20180642
660 305 up20180642
                    //update_scale();
661
                    double angle = get_mouse_angle(shooter1);
662
                    gunner_set_angle(shooter1, angle - M_PI_2);
663 295 up20180642
664 305 up20180642
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
665
                    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
666 295 up20180642
667 305 up20180642
                    graph_clear_screen();
668
                    map_draw   (map1);
669
                    bullet_draw_list(bullet_list);
670
                    gunner_draw_list(shooter_list);
671 295 up20180642
672 305 up20180642
                    text_draw(in_game_timer->text);
673 295 up20180642
674 305 up20180642
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
675
                    sprite_draw(sp_crosshair);
676
                    graph_draw();
677 295 up20180642
678 305 up20180642
                    break;
679
                    case KBC_IRQ:
680
                    if ((scancode[0]) == ESC_BREAK_CODE) {
681
                        good = false;
682
                    }
683
                    break;
684
                    case MOUSE_IRQ:
685
                    if (counter_mouse_ih >= 3) {
686
                        mouse_parse_packet(packet_mouse_ih, &pp);
687
                        update_mouse(&pp);
688
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
689
                        shoot_bullet(shooter1, bullet_list, bsp_bullet);
690
                        last_lb = keys->lb_pressed;
691
                        counter_mouse_ih = 0;
692 304 up20180642
693 305 up20180642
                    }
694
                    break;
695
                    case COM1_IRQ: nctp_ih(); break;
696
                }
697
            }
698 304 up20180642
        }
699 295 up20180642
    }
700
701
    while(list_size(shooter_list) > 0){
702
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
703
        gunner_dtor(p);
704
    }
705 296 up20180642
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
706 295 up20180642
707 301 up20180642
    while(list_size(bullet_list) > 0){
708
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
709
        bullet_dtor(p);
710
    }
711
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
712
713
    timer_dtor(in_game_timer); in_game_timer = NULL;
714
715
    return SUCCESS;
716
}
717
718
#define ZOMBIES_NUM             5
719
#define ZOMBIE_HEALTH_FACTOR    1.1
720
static int (zombies)(void){
721
722
    int r;
723
724
    ent_set_scale(DEFAULT_SCALE);
725 314 up20180642
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
726 301 up20180642
727
    list_t *shooter_list = list_ctor();
728
729 302 up20180642
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
730 305 up20180642
    gunner_set_spawn(shooter1, 980, 790);
731
    gunner_set_pos(shooter1, 980, 790);
732 301 up20180642
733
    list_insert(shooter_list, list_end(shooter_list), shooter1);
734
735
    list_t *bullet_list  = list_ctor();
736
737
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
738 305 up20180642
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
739 301 up20180642
740 305 up20180642
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
741
    uint8_t last_lb = 0;
742
    struct packet pp;
743
    keys_t *keys = get_key_presses();
744 301 up20180642
745
    /// loop stuff
746 304 up20180642
    uint32_t int_vector = 0;
747 301 up20180642
    int good = true;
748 302 up20180642
    int dead = false;
749 301 up20180642
750
    int health = 50;
751
752 309 up20180642
    /** #DEV */ /* {
753
        gunner_t *zombie = gunner_ctor(bsp_zombie, bsp_nothing, gunner_melee | gunner_follow, 3);
754
        gunner_set_health(zombie, health);
755
        gunner_set_curr_health(zombie, health);
756
        health *= ZOMBIE_HEALTH_FACTOR;
757
        gunner_set_pos(zombie, 1100, 75);
758
        list_push_back(shooter_list, zombie);
759
    }*/ //\#DEV
760
761
    map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1));
762
763 302 up20180642
    while (good && !dead) {
764 304 up20180642
        /* Get a request message. */
765
        if((r = get_interrupts_vector(&int_vector))) return r;
766
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
767 309 up20180642
            if(!good || dead) break;
768 304 up20180642
            if (int_vector & n) {
769 305 up20180642
                interrupt_handler(i);
770
                switch (i) {
771
                    case TIMER0_IRQ:
772
                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
773 309 up20180642
                    if (no_interrupts %  6 == 0){
774
                        map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1));
775
                    }
776 301 up20180642
777 305 up20180642
                    update_movement(map1, shooter1, keys, shooter_list);
778 301 up20180642
779 305 up20180642
                    update_game_state(map1, shooter_list, bullet_list);
780 302 up20180642
781 305 up20180642
                    if(list_find(shooter_list, shooter1) == list_end(shooter_list)){
782
                        good = false;
783
                        dead = true;
784
                        break;
785
                    }
786 301 up20180642
787 305 up20180642
                    double angle = get_mouse_angle(shooter1);
788
                    gunner_set_angle(shooter1, angle - M_PI_2);
789 301 up20180642
790 305 up20180642
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
791
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
792 301 up20180642
793 309 up20180642
                    while(list_size(shooter_list) < ZOMBIES_NUM+1){
794 305 up20180642
                        gunner_t *zombie = gunner_ctor(bsp_zombie, bsp_nothing, gunner_melee | gunner_follow, 3);
795
                        gunner_set_health(zombie, health);
796
                        gunner_set_curr_health(zombie, health);
797
                        health *= ZOMBIE_HEALTH_FACTOR;
798 307 up20180642
                        get_random_spawn(map1, zombie, shooter_list);
799 305 up20180642
                        list_push_back(shooter_list, zombie);
800
                    }
801 301 up20180642
802 305 up20180642
                    graph_clear_screen();
803
                    map_draw   (map1);
804
                    bullet_draw_list(bullet_list);
805
                    gunner_draw_list(shooter_list);
806 301 up20180642
807 305 up20180642
                    text_draw(in_game_timer->text);
808 301 up20180642
809 305 up20180642
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
810
                    sprite_draw(sp_crosshair);
811
                    graph_draw();
812 304 up20180642
813 305 up20180642
                    break;
814
                    case KBC_IRQ:
815
                    if ((scancode[0]) == ESC_BREAK_CODE) {
816
                        good = false;
817
                    }
818
                    break;
819
                    case MOUSE_IRQ:
820
                    if (counter_mouse_ih >= 3) {
821
                        mouse_parse_packet(packet_mouse_ih, &pp);
822
                        update_mouse(&pp);
823
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
824
                        shoot_bullet(shooter1, bullet_list, bsp_bullet);
825
                        last_lb = keys->lb_pressed;
826
                        counter_mouse_ih = 0;
827
828
                    }
829
                    break;
830
                    case COM1_IRQ: nctp_ih(); break;
831
                }
832
            }
833
        }
834 301 up20180642
    }
835
836
    while(list_size(shooter_list) > 0){
837
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
838
        gunner_dtor(p);
839
    }
840
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
841
842 295 up20180642
    while(list_size(bullet_list) > 0){
843
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
844
        bullet_dtor(p);
845
    }
846
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
847
848 302 up20180642
    if(dead){
849
        printf("YOU DIED\n");
850
    }
851
852 295 up20180642
    timer_dtor(in_game_timer); in_game_timer = NULL;
853
854 294 up20180642
    return SUCCESS;
855
}
856 296 up20180642
857 298 up20180642
#define CHAT_MAX_SIZE   75
858
#define CHAT_MAX_NUM    19
859 297 up20180642
text_t      *t_text[CHAT_MAX_NUM] = {NULL};
860
rectangle_t *r_text               =  NULL;
861
static void chat_process(const uint8_t *p, const size_t sz){
862
    char buffer2[CHAT_MAX_NUM+3];
863
    void *dest = NULL;
864
    hltp_type tp = hltp_interpret(p, sz, &dest);
865
    switch(tp){
866
        case hltp_type_string:
867 305 up20180642
        strcpy(buffer2, dest);
868
        strncat(buffer2, " <", 2);
869
        for(size_t i = CHAT_MAX_NUM-1; i; --i)
870 314 up20180642
        text_set_string(t_text[i], text_get_string(t_text[i-1]));
871
        text_set_string(t_text[0], buffer2);
872 305 up20180642
        for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
873
            if(text_get_string(t_text[i])[0] == '>'){
874
                text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
875
                text_set_halign(t_text[i], text_halign_left);
876
            }else{
877
                text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
878
                text_set_halign(t_text[i], text_halign_right);
879 297 up20180642
            }
880 305 up20180642
        }
881
        break;
882 297 up20180642
        default: break;
883
    }
884
}
885 300 up20180642
static int (chat)(void){
886 297 up20180642
    int r;
887
888 298 up20180642
    nctp_dump();
889 297 up20180642
    nctp_set_processor(chat_process);
890
891
    struct packet pp;
892
893 298 up20180642
    char buffer[CHAT_MAX_SIZE] = "";
894 297 up20180642
    rectangle_t *r_buffer = NULL; {
895 298 up20180642
        r_buffer = rectangle_ctor(0,0,900,70);
896 297 up20180642
        rectangle_set_pos(r_buffer, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
897 305 up20180642
        graph_get_YRes()*0.87-rectangle_get_h(r_buffer)/2);
898 297 up20180642
        rectangle_set_fill_color   (r_buffer, GRAPH_BLACK);
899
        rectangle_set_outline_width(r_buffer, 2);
900
        rectangle_set_outline_color(r_buffer, GRAPH_WHITE);
901
        rectangle_set_fill_trans(r_buffer, GRAPH_TRANSPARENT);
902
    }
903
    text_t      *t_buffer = NULL; {
904 314 up20180642
        t_buffer = text_ctor(font_get_default(), "");
905 297 up20180642
        text_set_pos(t_buffer, rectangle_get_x(r_buffer)+50,
906 305 up20180642
        rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)/2);
907 297 up20180642
        text_set_halign(t_buffer, text_halign_left);
908
        text_set_valign(t_buffer, text_valign_center);
909
        text_set_color (t_buffer, TEXT_COLOR);
910
    }
911 298 up20180642
    text_t      *t_size   = NULL; {
912 314 up20180642
        t_size = text_ctor(font_get_default(), "");
913 298 up20180642
        text_set_pos(t_size, rectangle_get_x(r_buffer)+rectangle_get_w(r_buffer)-5,
914 305 up20180642
        rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)-5);
915 298 up20180642
        text_set_halign(t_size, text_halign_right);
916
        text_set_valign(t_size, text_valign_bottom);
917
        text_set_color (t_size, TEXT_COLOR);
918
        text_set_size  (t_size, 18);
919
        char buffer2[20];
920
        sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
921 314 up20180642
        text_set_string(t_size, buffer2);
922 298 up20180642
    }
923 297 up20180642
924
    /** r_text */ {
925 305 up20180642
    r_text = rectangle_ctor(0,0,900,550);
926
    rectangle_set_pos(r_text, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
927
    graph_get_YRes()*0.09);
928
    rectangle_set_fill_color   (r_text, GRAPH_BLACK);
929
    rectangle_set_outline_width(r_text, 2);
930
    rectangle_set_outline_color(r_text, GRAPH_WHITE);
931
    rectangle_set_fill_trans(r_text, GRAPH_TRANSPARENT);
932 311 up20180655
    }
933
    /** t_text */ {
934
    for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
935 314 up20180642
        t_text[i] = text_ctor(font_get_default(), " ");
936 311 up20180655
        text_set_pos(t_text[i], rectangle_get_x(r_text)+50,
937
        rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-25*i);
938
        text_set_halign(t_text[i], text_halign_left);
939
        text_set_valign(t_text[i], text_valign_bottom);
940
        text_set_color (t_text[i], TEXT_COLOR);
941
    }
942 305 up20180642
}
943 297 up20180642
944 305 up20180642
/// loop stuff
945
uint32_t int_vector = 0;
946
int good = true;
947
while (good) {
948
    /* Get a request message. */
949
    if((r = get_interrupts_vector(&int_vector))) return r;
950
    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
951
        if (int_vector & n) {
952
            interrupt_handler(i);
953
            switch (i) {
954 304 up20180642
                case TIMER0_IRQ:
955 305 up20180642
                graph_clear_screen();
956
                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
957 297 up20180642
958 305 up20180642
                rectangle_draw(r_buffer);
959
                text_draw(t_buffer);
960
                text_draw(t_size);
961 297 up20180642
962 305 up20180642
                rectangle_draw(r_text);
963
                for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_draw(t_text[i]);
964 297 up20180642
965 305 up20180642
                sprite_draw(sp_crosshair);
966
                graph_draw();
967
                break;
968 304 up20180642
                case KBC_IRQ:
969 305 up20180642
                if      (scancode[0] == ESC_BREAK_CODE) good = false;
970
                else if (scancode[0] == ENTER_MAKE_CODE) {
971
                    hltp_send_string(buffer);
972
                    char buffer2[CHAT_MAX_SIZE+3] = "> ";
973
                    strncat(buffer2, buffer, strlen(buffer));
974
                    for(size_t i = CHAT_MAX_NUM-1; i; --i)
975 314 up20180642
                    text_set_string(t_text[i], text_get_string(t_text[i-1]));
976
                    text_set_string(t_text[0], buffer2);
977 305 up20180642
                    for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
978
                        if(text_get_string(t_text[i])[0] == '>'){
979
                            text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
980
                            text_set_halign(t_text[i], text_halign_left);
981
                        }else{
982
                            text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
983
                            text_set_halign(t_text[i], text_halign_right);
984 297 up20180642
                        }
985
                    }
986 305 up20180642
                    buffer[0] = '\0';
987
                } else if(scancode[0] == BACKSPACE_MAKE_CODE){
988
                    buffer[strlen(buffer)-1] = '\0';
989
                } else {
990
                    char c = map_makecode(scancode[0]);
991
                    if (c == ERROR_CODE) break;
992
                    if(strlen(buffer) < CHAT_MAX_SIZE) strncat(buffer, &c, 1);
993
                    else                               printf("Char limit exceeded\n");
994
                }
995 314 up20180642
                text_set_string(t_buffer, buffer);
996 305 up20180642
                char buffer2[20];
997
                sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
998 314 up20180642
                text_set_string(t_size, buffer2);
999 304 up20180642
                case MOUSE_IRQ:
1000 305 up20180642
                if (counter_mouse_ih >= 3) {
1001
                    mouse_parse_packet(packet_mouse_ih, &pp);
1002
                    update_mouse(&pp);
1003
                    counter_mouse_ih = 0;
1004
                }
1005
                break;
1006 304 up20180642
                case COM1_IRQ: nctp_ih(); break;
1007 297 up20180642
            }
1008
        }
1009
    }
1010 305 up20180642
}
1011 297 up20180642
1012 305 up20180642
rectangle_dtor(r_buffer);
1013
text_dtor     (t_buffer);
1014 297 up20180642
1015 305 up20180642
rectangle_dtor(r_text);
1016
for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_dtor(t_text[i]);
1017 297 up20180642
1018 305 up20180642
nctp_set_processor(NULL);
1019 298 up20180642
1020 305 up20180642
return SUCCESS;
1021 296 up20180642
}