Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (20.2 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/src/trace.txt");
31

    
32
    lcf_log_output("/home/lcom/labs/proj/src/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

    
45
#include "scoreboards.h"
46
int(proj_main_loop)(int argc, char *argv[]) {
47
    (void)argc; (void)argv;
48

    
49
    int r;
50

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

    
53
    /// subscribe interrupts
54
    if (subscribe_all()) { return 1; }
55

    
56
    /// initialize graphics
57
    if(graph_init(GRAPH_MODE)){
58
        printf("%s: failed to initalize graphics.\n", __func__);
59
        if (unsubscribe_all()) printf("%s: failed to unsubscribe all.\n", __func__);
60
        return 1;
61
    }
62

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

    
75
        bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n");
76
        bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
77
        bsp_zombie    = get_zombie   (); if(bsp_zombie    == NULL) printf("Failed to get zombie\n");
78
        bsp_pistol    = get_pistol   (); if(bsp_pistol    == NULL) printf("Failed to get pistol\n");
79
        bsp_nothing   = get_nothing  (); if(bsp_nothing   == NULL) printf("Failed to get nothing\n");
80
        bsp_bullet    = get_bullet   (); if(bsp_bullet    == NULL) printf("Failed to get bullet\n");
81
        map1          = get_map1     (); if(map1          == NULL) printf("Failed to get map1\n");
82
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
83
    }
84

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

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

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

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

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

    
128
                    click = 0;
129

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

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

    
152
    if ((r = unsubscribe_all()))
153
        printf("%s: failed to unsubscribe drivers.\n", __func__);
154
    if ((r = graph_cleanup()))
155
        printf("%s: graph cleanup failed\n", __func__);
156

    
157
    text_dtor(title);
158
    menu_dtor(main_menu);
159

    
160
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
161
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
162
    basic_sprite_dtor      (bsp_zombie   ); bsp_zombie    = NULL;
163
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
164
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
165
    basic_sprite_dtor      (bsp_bullet   ); bsp_bullet    = NULL;
166
    map_dtor               (map1         ); map1          = NULL;
167
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
168

    
169

    
170

    
171
    font_free();
172

    
173
    return r;
174
}
175

    
176
static host_info_t     *host_info   = NULL;
177
static remote_info_t   *remote_info = NULL;
178
static bullet_info_t   *bullet_info = NULL;
179

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

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

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

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

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

    
236
                    click = 0;
237

    
238
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
239
                    sprite_draw(sp_crosshair);
240
                    graph_draw();
241

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

    
263
static int (multiplayer_host)(void) {
264
    int r;
265

    
266
    nctp_dump();
267
    nctp_set_processor(multiplayer_process);
268

    
269
    ent_set_scale(DEFAULT_SCALE);
270
    text_timer_t *in_game_timer = text_timer_ctor(font_get_default());
271

    
272
    list_t *shooter_list = list_ctor();
273

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

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

    
280
    list_insert(shooter_list, list_end(shooter_list), shooter1);
281
    list_insert(shooter_list, list_end(shooter_list), shooter2);
282

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

    
288
    host_info = host_info_ctor(shooter1, shooter2);
289
    remote_info = remote_info_ctor();
290
    bullet_info = bullet_info_ctor();
291

    
292
    list_t *bullet_list  = list_ctor();
293

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

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

    
319
                    update_game_state(map1, shooter_list, bullet_list);
320

    
321
                    p1 = list_find(shooter_list, shooter1);
322
                    p2 = list_find(shooter_list, shooter2);
323

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

    
330
                    double angle = get_mouse_angle(shooter1);
331
                    gunner_set_angle(shooter1, angle - M_PI_2);
332

    
333
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
334
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
335

    
336
                    gunner_set_angle(shooter2, remote_info->remote_angle);
337
                    //build_host_structure(host_info, shooter1, shooter2);
338

    
339
                    //hltp_send_host_info(host_info);
340

    
341
                    graph_clear_screen();
342
                    map_draw   (map1);
343
                    bullet_draw_list(bullet_list);
344
                    gunner_draw_list(shooter_list);
345

    
346
                    text_draw(in_game_timer->text);
347

    
348
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
349
                    sprite_draw(sp_crosshair);
350
                    graph_draw();
351

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

    
370
                    case COM1_IRQ:
371
                        printf("L371\n");
372
                        nctp_ih();
373
                        printf("L373\n");
374
                        if (bullet_info->new_bullet) { printf("L374\n");
375
                            shoot_bullet(shooter2, bullet_list, bsp_bullet);
376
                            bullet_info->new_bullet = false;
377
                        }
378
                        printf("L378\n");
379
                        break;
380
                }
381
            }
382
        }
383
    }
384

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

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

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

    
401
    nctp_set_processor(NULL);
402

    
403
    text_timer_dtor(in_game_timer); in_game_timer = NULL;
404

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

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

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

    
416
    list_t *shooter_list = list_ctor();
417

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

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

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

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

    
431
    list_t *bullet_list  = list_ctor();
432

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

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

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

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

    
456
                    if (timer_get_no_interrupts() % 6 ==0){
457
                        build_remote_structure(remote_info, keys, angle);
458

    
459
                        hltp_send_remote_info(remote_info);
460
                    }
461

    
462

    
463
                    gunner_set_pos(shooter1, (double)host_info->remote_x, (double)host_info->remote_y);
464
                    gunner_set_angle(shooter1, (double)host_info->remote_angle);
465
                    gunner_set_health(shooter1, (double)host_info->remote_health);
466
                    gunner_set_curr_health(shooter1, (double)host_info->remote_current_health);
467

    
468
                    gunner_set_pos(shooter2, (double)host_info->host_x, (double)host_info->host_y);
469
                    gunner_set_angle(shooter2, (double)host_info->host_angle);
470
                    gunner_set_health(shooter2, (double)host_info->host_health);
471
                    gunner_set_curr_health(shooter2, (double)host_info->host_current_health);
472

    
473
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
474
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
475

    
476
                    /*
477
                    for (size_t j = 0; j < host_info->no_bullets; j++) {
478
                        if (host_info->bullets_shooter[j]) { // remote
479
                            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]);
480
                            list_insert(bullet_list, list_end(bullet_list), bullet);
481
                        } else { // host
482
                            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]);
483
                            list_insert(bullet_list, list_end(bullet_list), bullet);
484
                        }
485
                    }*/
486

    
487
                    graph_clear_screen();
488
                    map_draw   (map1);
489
                    bullet_draw_list(bullet_list);
490
                    gunner_draw_list(shooter_list);
491

    
492
                    text_draw(in_game_timer->text);
493

    
494
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
495
                    sprite_draw(sp_crosshair);
496
                    graph_draw();
497
                    /*
498
                    while(list_size(bullet_list) > 0){
499
                        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
500
                        bullet_dtor(p);
501
                    }
502
                    */
503

    
504
                    break;
505
                    case KBC_IRQ:
506
                    if(!keyboard_get_done()) break;
507
                    if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) {
508
                        good = false;
509
                    }
510
                    break;
511
                    case MOUSE_IRQ:
512
                    if (mouse_get_counter_mouse_ih() >= 3) {
513
                        mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp);
514
                        update_mouse(&pp);
515
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed) {
516
                            bullet_info->new_bullet = true;
517
                            hltp_send_bullet_info(bullet_info);
518
                        }
519
                        last_lb = keys->lb_pressed;
520
                        mouse_set_counter_mouse_ih(0);
521
                    }
522
                    break;
523

    
524
                    case COM1_IRQ:
525
                        nctp_ih();
526
                        break;
527
                }
528
            }
529
        }
530
    }
531

    
532
    while(list_size(shooter_list) > 0){
533
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
534
        gunner_dtor(p);
535
    }
536
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
537

    
538
    while(list_size(bullet_list) > 0){
539
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
540
        bullet_dtor(p);
541
    }
542
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
543

    
544
    host_info_dtor(host_info);
545
    remote_info_dtor(remote_info);
546
    bullet_info_dtor(bullet_info);
547

    
548
    nctp_set_processor(NULL);
549

    
550
    text_timer_dtor(in_game_timer); in_game_timer = NULL;
551

    
552
    return 0;
553
}