Project

General

Profile

Statistics
| Revision:

root / proj / project / src / proj.c @ 366

History | View | Annotate | Download (19.9 KB)

1
#include <lcom/lcf.h>
2
#include <lcom/proj.h>
3
#include <lcom/liblm.h>
4

    
5
#include "proj.h"
6

    
7
#include <math.h>
8

    
9
#include "proj_macros.h"
10
#include "proj_func.h"
11

    
12
#include "interrupts_func.h"
13
#include "makecode_map.h"
14

    
15
#include "crosshair.h"
16
#include "shooter.h"
17
#include "zombie.h"
18
#include "pistol.h"
19
#include "nothing.h"
20
#include "bullet.h"
21
#include "map1.h"
22

    
23
#include "hltp.h"
24

    
25

    
26
int main(int argc, char* argv[]) {
27

    
28
    lcf_set_language("EN-US");
29

    
30
    lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
31

    
32
    lcf_log_output("/home/lcom/labs/proj/output.txt");
33

    
34
    if (lcf_start(argc, argv)) return 1;
35

    
36
    lcf_cleanup();
37

    
38
    return 0;
39
}
40

    
41
#include "singleplayer.h"
42
static int (multiplayer)(void);
43
#include "chat.h"
44
int(proj_main_loop)(int argc, char *argv[]) {
45
    (void)argc; (void)argv;
46

    
47
    int r;
48

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

    
51
    /// subscribe interrupts
52
    if (subscribe_all()) { return 1; }
53

    
54
    /// initialize graphics
55
    if(graph_init(GRAPH_MODE)){
56
        printf("%s: failed to initalize graphics.\n", __func__);
57
        if (cleanup()) printf("%s: failed to cleanup.\n", __func__);
58
        return 1;
59
    }
60

    
61
    /// Load stuff
62
    {
63
        graph_clear_screen();
64
        text_t *txt = text_ctor(font_get_default(), "Loading...");
65
        text_set_pos(txt, graph_get_XRes()/2, graph_get_YRes()/2);
66
        text_set_valign(txt, text_valign_center);
67
        text_set_halign(txt, text_halign_center);
68
        text_set_color(txt, TEXT_COLOR);
69
        text_draw(txt);
70
        text_dtor(txt);
71
        graph_draw();
72

    
73
        bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n");
74
        bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
75
        bsp_zombie    = get_zombie   (); if(bsp_zombie    == NULL) printf("Failed to get zombie\n");
76
        bsp_pistol    = get_pistol   (); if(bsp_pistol    == NULL) printf("Failed to get pistol\n");
77
        bsp_nothing   = get_nothing  (); if(bsp_nothing   == NULL) printf("Failed to get nothing\n");
78
        bsp_bullet    = get_bullet   (); if(bsp_bullet    == NULL) printf("Failed to get bullet\n");
79
        map1          = get_map1     (); if(map1          == NULL) printf("Failed to get map1\n");
80

    
81
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
82
    }
83

    
84
    text_t *title     = text_ctor(font_get_default(), "LabWars");
85
    text_set_color(title, TEXT_COLOR);
86
    text_set_size(title, 70);
87
    text_set_pos(title, graph_get_XRes()/2, graph_get_YRes()*0.17);
88
    text_set_valign(title, text_valign_center);
89
    text_set_halign(title, text_halign_center);
90

    
91
    menu_t *main_menu = menu_ctor(font_get_default());
92
    menu_add_item(main_menu, "Single player");
93
    menu_add_item(main_menu, "Multiplayer");
94
    menu_add_item(main_menu, "Chat");
95
    menu_add_item(main_menu, "Exit");
96

    
97
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
98
    uint8_t last_lb = 0;
99
    struct packet pp;
100
    keys_t *keys = get_key_presses();
101

    
102
    /// loop stuff
103
    int click = 0;
104
    uint64_t int_vector = 0;
105
    int good = true;
106
    while (good) {
107
        /* Get a request message. */
108
        if((r = get_interrupts_vector(&int_vector))) return r;
109
        uint32_t n = 1;
110
        for (uint8_t i = 0; i < 32; i++, n <<= 1) {
111
            if (int_vector & n) {
112
                interrupt_handler(i);
113
                switch (i) {
114
                    case TIMER0_IRQ:
115

    
116
                    graph_clear_screen();
117
                    switch(menu_update_state(main_menu, click)){
118
                        case -1: break;
119
                        case  0: singleplayer(); break; //campaign(); break;
120
                        case  1: multiplayer() ; break;
121
                        case  2: chat(); break;
122
                        case  3: good = false; break;
123
                    }
124
                    menu_draw(main_menu);
125
                    text_draw(title);
126

    
127
                    click = 0;
128

    
129
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
130
                    sprite_draw(sp_crosshair);
131
                    graph_draw();
132

    
133
                    break;
134
                    case KBC_IRQ:
135
                    if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) good = false;
136
                    case MOUSE_IRQ:
137
                    if (mouse_get_counter_mouse_ih() >= 3) {
138
                        mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp);
139
                        update_mouse(&pp);
140
                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
141
                        last_lb = keys->lb_pressed;
142
                        mouse_set_counter_mouse_ih(0);
143
                    }
144
                    break;
145
                    case COM1_IRQ: nctp_ih(); break;
146
                }
147
            }
148
        }
149
    }
150

    
151
    text_dtor(title);
152
    menu_dtor(main_menu);
153

    
154
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
155
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
156
    basic_sprite_dtor      (bsp_zombie   ); bsp_zombie    = NULL;
157
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
158
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
159
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
160
    map_dtor               (map1         ); map1          = NULL;
161
    font_free();
162

    
163
    // Unsubscribe interrupts
164
    if (unsubscribe_all()) {
165
        if (cleanup())
166
        printf("%s: failed to cleanup.\n", __func__);
167
        return 1;
168
    }
169

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

    
175
    return 0;
176
}
177

    
178
static host_info_t     *host_info   = NULL;
179
static remote_info_t   *remote_info = NULL;
180
static bullet_info_t   *bullet_info = NULL;
181

    
182
static void multiplayer_process(const uint8_t *p, const size_t sz) {
183
    void *dest = NULL;
184
    hltp_type tp = hltp_interpret(p, sz, &dest);
185
    if (dest == NULL) return;
186
    switch(tp){
187
        case hltp_type_host:
188
            host_info = (host_info_t*)dest;
189
            break;
190
        case hltp_type_remote:
191
            remote_info = (remote_info_t*)dest;
192
            break;
193
        case hltp_type_bullet:
194
            bullet_info = (bullet_info_t*)dest;
195
            break;
196
        case hltp_type_invalid: break;
197
        case hltp_type_string : break;
198
    }
199
}
200
static int (multiplayer_host)(void);
201
static int (multiplayer_remote)(void);
202
static int (multiplayer)(void) {
203
    int r;
204

    
205
    menu_t *main_menu = menu_ctor(font_get_default());
206
    menu_add_item(main_menu, "Create");
207
    menu_add_item(main_menu, "Connect");
208
    menu_add_item(main_menu, "Back");
209

    
210
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
211
    uint8_t last_lb = 0;
212
    struct packet pp;
213
    keys_t *keys = get_key_presses();
214

    
215
    /// loop stuff
216
    int click = 0;
217
    uint64_t int_vector = 0;
218
    int good = true;
219
    while (good) {
220
        /* Get a request message. */
221
        if((r = get_interrupts_vector(&int_vector))) return r;
222
        uint32_t n = 1;
223
        for (uint8_t i = 0; i < 32; i++, n <<= 1) {
224
            if (int_vector & n) {
225
                interrupt_handler(i);
226
                switch (i) {
227
                    case TIMER0_IRQ:
228

    
229
                    graph_clear_screen();
230
                    switch(menu_update_state(main_menu, click)){
231
                        case -1: break;
232
                        case  0: multiplayer_host(); break;
233
                        case  1: multiplayer_remote(); break;
234
                        case  2: good = false; break;
235
                    }
236
                    menu_draw(main_menu);
237

    
238
                    click = 0;
239

    
240
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
241
                    sprite_draw(sp_crosshair);
242
                    graph_draw();
243

    
244
                    break;
245
                    case KBC_IRQ:
246
                    if(!keyboard_get_done()) break;
247
                    if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) good = false;
248
                    case MOUSE_IRQ:
249
                    if (mouse_get_counter_mouse_ih() >= 3) {
250
                        mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp);
251
                        update_mouse(&pp);
252
                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
253
                        last_lb = keys->lb_pressed;
254
                        mouse_set_counter_mouse_ih(0);
255
                    }
256
                    break;
257
                    case COM1_IRQ: nctp_ih(); break;
258
                }
259
            }
260
        }
261
    }
262
    return 0;
263
}
264

    
265
static int (multiplayer_host)(void) {
266
    int r;
267

    
268
    nctp_dump();
269
    nctp_set_processor(multiplayer_process);
270

    
271
    ent_set_scale(DEFAULT_SCALE);
272
    text_timer_t *in_game_timer = text_timer_ctor(font_get_default());
273

    
274
    list_t *shooter_list = list_ctor();
275

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

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

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

    
285
    do {
286
        get_random_spawn(map1, shooter1, shooter_list);
287
        get_random_spawn(map1, shooter2, shooter_list);
288
    } while (gunner_distance(shooter1, shooter2) < 500);
289

    
290
    host_info = host_info_ctor(shooter1, shooter2);
291
    remote_info = remote_info_ctor();
292
    bullet_info = bullet_info_ctor();
293

    
294
    list_t *bullet_list  = list_ctor();
295

    
296
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
297
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
298

    
299
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
300
    uint8_t last_lb = 0;
301
    struct packet pp;
302
    keys_t *keys = get_key_presses();
303
    /// loop stuff
304
    uint64_t int_vector = 0;
305
    int good = true;
306
    int state = 0; // -1 for remote win, 0 for draw, 1 for host win
307
    list_node_t *p1, *p2; // player states
308
    int state_1, state_2;
309
    while (good) {
310
        if ((r = get_interrupts_vector(&int_vector))) return r;
311
        uint32_t n = 1;
312
        for (uint8_t i = 0; i < 32; i++, n <<= 1) {
313
            if (int_vector & n) {
314
                interrupt_handler(i);
315
                switch (i) {
316
                    case TIMER0_IRQ:
317
                    if (timer_get_no_interrupts() % 60 == 0) text_timer_update(in_game_timer);
318
                    update_movement(map1, shooter1, keys, shooter_list);
319
                    update_movement(map1, shooter2, &(remote_info->remote_keys_pressed), shooter_list);
320

    
321
                    update_game_state(map1, shooter_list, bullet_list);
322

    
323
                    p1 = list_find(shooter_list, shooter1);
324
                    p2 = list_find(shooter_list, shooter2);
325

    
326
                    if ((state_1 = (p1 == list_end(shooter_list))) || (state_2 = (p2 == list_end(shooter_list)))) {
327
                        state = state_1 - state_2;
328
                        good = false;
329
                        break;
330
                    }
331

    
332
                    double angle = get_mouse_angle(shooter1);
333
                    gunner_set_angle(shooter1, angle - M_PI_2);
334

    
335
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
336
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
337

    
338
                    gunner_set_angle(shooter2, remote_info->remote_angle);
339
                    build_host_structure(host_info, shooter1, shooter2);
340

    
341
                    hltp_send_host_info(host_info);
342

    
343
                    graph_clear_screen();
344
                    map_draw   (map1);
345
                    bullet_draw_list(bullet_list);
346
                    gunner_draw_list(shooter_list);
347

    
348
                    text_draw(in_game_timer->text);
349

    
350
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
351
                    sprite_draw(sp_crosshair);
352
                    graph_draw();
353

    
354
                    break;
355
                    case KBC_IRQ:
356
                    if(!keyboard_get_done()) break;
357
                    if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) {
358
                        good = false;
359
                    }
360
                    break;
361
                    case MOUSE_IRQ:
362
                    if (mouse_get_counter_mouse_ih() >= 3) {
363
                        mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp);
364
                        update_mouse(&pp);
365
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
366
                        shoot_bullet(shooter1, bullet_list, bsp_bullet);
367
                        last_lb = keys->lb_pressed;
368
                        mouse_set_counter_mouse_ih(0);
369
                    }
370
                    break;
371

    
372
                    case COM1_IRQ:
373
                        nctp_ih();
374
                        if (bullet_info->new_bullet) {
375
                            shoot_bullet(shooter2, bullet_list, bsp_bullet);
376
                            bullet_info->new_bullet = false;
377
                        }
378
                        break;
379
                }
380
            }
381
        }
382
    }
383

    
384
    while(list_size(shooter_list) > 0){
385
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
386
        gunner_dtor(p);
387
    }
388
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
389

    
390
    while(list_size(bullet_list) > 0){
391
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
392
        bullet_dtor(p);
393
    }
394
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
395

    
396
    host_info_dtor(host_info);
397
    remote_info_dtor(remote_info);
398
    bullet_info_dtor(bullet_info);
399

    
400
    nctp_set_processor(NULL);
401

    
402
    text_timer_dtor(in_game_timer); in_game_timer = NULL;
403

    
404
    return 0;
405
}
406
static int (multiplayer_remote)(void) {
407
    int r;
408

    
409
    nctp_dump();
410
    nctp_set_processor(multiplayer_process);
411

    
412
    ent_set_scale(DEFAULT_SCALE);
413
    text_timer_t *in_game_timer = text_timer_ctor(font_get_default());
414

    
415
    list_t *shooter_list = list_ctor();
416

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

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

    
423
    list_insert(shooter_list, list_end(shooter_list), shooter1);
424
    list_insert(shooter_list, list_end(shooter_list), shooter2);
425

    
426
    host_info = host_info_ctor(shooter2, shooter1);
427
    remote_info = remote_info_ctor();
428
    bullet_info = bullet_info_ctor();
429

    
430
    list_t *bullet_list  = list_ctor();
431

    
432
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
433
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
434

    
435
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
436
    uint8_t last_lb = 0;
437
    struct packet pp;
438
    keys_t *keys = get_key_presses();
439

    
440
    /// loop stuff
441
    uint64_t int_vector = 0;
442
    int good = true;
443
    while (good) {
444
        if ((r = get_interrupts_vector(&int_vector))) return r;
445
        uint32_t n = 1;
446
        for (uint8_t i = 0; i < 32; i++, n <<= 1) {
447
            if (int_vector & n) {
448
                interrupt_handler(i);
449
                switch (i) {
450
                    case TIMER0_IRQ:
451
                    if (timer_get_no_interrupts() % 60 == 0) text_timer_update(in_game_timer);
452

    
453
                    double angle = get_mouse_angle(shooter1) - M_PI_2;
454

    
455
                    build_remote_structure(remote_info, keys, angle);
456

    
457

    
458
                    //hltp_send_remote_info(remote_info);
459
                    gunner_set_pos(shooter1, (double)host_info->remote_x, (double)host_info->remote_y);
460
                    gunner_set_angle(shooter1, (double)host_info->remote_angle);
461
                    gunner_set_health(shooter1, (double)host_info->remote_health);
462
                    gunner_set_curr_health(shooter1, (double)host_info->remote_current_health);
463

    
464
                    gunner_set_pos(shooter2, (double)host_info->host_x, (double)host_info->host_y);
465
                    gunner_set_angle(shooter2, (double)host_info->host_angle);
466
                    gunner_set_health(shooter2, (double)host_info->host_health);
467
                    gunner_set_curr_health(shooter2, (double)host_info->host_current_health);
468

    
469
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
470
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
471

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

    
483
                    graph_clear_screen();
484
                    map_draw   (map1);
485
                    bullet_draw_list(bullet_list);
486
                    gunner_draw_list(shooter_list);
487

    
488
                    text_draw(in_game_timer->text);
489

    
490
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
491
                    sprite_draw(sp_crosshair);
492
                    graph_draw();
493
                    /*
494
                    while(list_size(bullet_list) > 0){
495
                        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
496
                        bullet_dtor(p);
497
                    }
498
                    */
499

    
500
                    break;
501
                    case KBC_IRQ:
502
                    if(!keyboard_get_done()) break;
503
                    if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) {
504
                        good = false;
505
                    }
506
                    break;
507
                    case MOUSE_IRQ:
508
                    if (mouse_get_counter_mouse_ih() >= 3) {
509
                        mouse_parse_packet(mouse_get_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
                        mouse_set_counter_mouse_ih(0);
517
                    }
518
                    break;
519

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

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

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

    
540
    host_info_dtor(host_info);
541
    remote_info_dtor(remote_info);
542
    bullet_info_dtor(bullet_info);
543

    
544
    nctp_set_processor(NULL);
545

    
546
    text_timer_dtor(in_game_timer); in_game_timer = NULL;
547

    
548
    return 0;
549
}