Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 354

History | View | Annotate | Download (19.4 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
    menu_t *main_menu = menu_ctor(font_get_default());
85
    menu_add_item(main_menu, "Single player");
86
    menu_add_item(main_menu, "Multiplayer");
87
    menu_add_item(main_menu, "Chat");
88
    menu_add_item(main_menu, "Exit");
89

    
90
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
91
    uint8_t last_lb = 0;
92
    struct packet pp;
93
    keys_t *keys = get_key_presses();
94

    
95
    /// loop stuff
96
    int click = 0;
97
    uint64_t int_vector = 0;
98
    int good = true;
99
    while (good) {
100
        /* Get a request message. */
101
        if((r = get_interrupts_vector(&int_vector))) return r;
102
        uint32_t n = 1;
103
        for (uint8_t i = 0; i < 32; i++, n <<= 1) {
104
            if (int_vector & n) {
105
                interrupt_handler(i);
106
                switch (i) {
107
                    case TIMER0_IRQ:
108

    
109
                    graph_clear_screen();
110
                    switch(menu_update_state(main_menu, click)){
111
                        case -1: break;
112
                        case  0: singleplayer(); break; //campaign(); break;
113
                        case  1: multiplayer() ; break;
114
                        case  2: chat(); break;
115
                        case  3: good = false; break;
116
                    }
117
                    menu_draw(main_menu);
118

    
119
                    click = 0;
120

    
121
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
122
                    sprite_draw(sp_crosshair);
123
                    graph_draw();
124

    
125
                    break;
126
                    case KBC_IRQ:
127
                    if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) good = false;
128
                    case MOUSE_IRQ:
129
                    if (mouse_get_counter_mouse_ih() >= 3) {
130
                        mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp);
131
                        update_mouse(&pp);
132
                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
133
                        last_lb = keys->lb_pressed;
134
                        mouse_set_counter_mouse_ih(0);
135
                    }
136
                    break;
137
                    case COM1_IRQ: nctp_ih(); break;
138
                }
139
            }
140
        }
141
    }
142

    
143
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
144
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
145
    basic_sprite_dtor      (bsp_zombie   ); bsp_zombie    = NULL;
146
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
147
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
148
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
149
    map_dtor               (map1         ); map1          = NULL;
150
    font_free();
151

    
152
    // Unsubscribe interrupts
153
    if (unsubscribe_all()) {
154
        if (cleanup())
155
        printf("%s: failed to cleanup.\n", __func__);
156
        return 1;
157
    }
158

    
159
    if (cleanup()) {
160
        printf("%s: failed to cleanup.\n", __func__);
161
        return 1;
162
    }
163

    
164
    return 0;
165
}
166

    
167
static host_info_t     *host_info   = NULL;
168
static remote_info_t   *remote_info = NULL;
169
static bullet_info_t   *bullet_info = NULL;
170

    
171
static void multiplayer_process(const uint8_t *p, const size_t sz) {
172
    void *dest = NULL;
173
    hltp_type tp = hltp_interpret(p, sz, &dest);
174
    if (dest == NULL) return;
175
    switch(tp){
176
        case hltp_type_host:
177
            host_info = (host_info_t*)dest;
178
            break;
179
        case hltp_type_remote:
180
            remote_info = (remote_info_t*)dest;
181
            break;
182
        case hltp_type_bullet:
183
            bullet_info = (bullet_info_t*)dest;
184
            break;
185
        case hltp_type_invalid: break;
186
        case hltp_type_string : break;
187
    }
188
}
189
static int (multiplayer_host)(void);
190
static int (multiplayer_remote)(void);
191
static int (multiplayer)(void) {
192
    int r;
193

    
194
    menu_t *main_menu = menu_ctor(font_get_default());
195
    menu_add_item(main_menu, "Create");
196
    menu_add_item(main_menu, "Connect");
197
    menu_add_item(main_menu, "Back");
198

    
199
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
200
    uint8_t last_lb = 0;
201
    struct packet pp;
202
    keys_t *keys = get_key_presses();
203

    
204
    /// loop stuff
205
    int click = 0;
206
    uint64_t int_vector = 0;
207
    int good = true;
208
    while (good) {
209
        /* Get a request message. */
210
        if((r = get_interrupts_vector(&int_vector))) return r;
211
        uint32_t n = 1;
212
        for (uint8_t i = 0; i < 32; i++, n <<= 1) {
213
            if (int_vector & n) {
214
                interrupt_handler(i);
215
                switch (i) {
216
                    case TIMER0_IRQ:
217

    
218
                    graph_clear_screen();
219
                    switch(menu_update_state(main_menu, click)){
220
                        case -1: break;
221
                        case  0: multiplayer_host(); break;
222
                        case  1: multiplayer_remote(); break;
223
                        case  2: good = false; break;
224
                    }
225
                    menu_draw(main_menu);
226

    
227
                    click = 0;
228

    
229
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
230
                    sprite_draw(sp_crosshair);
231
                    graph_draw();
232

    
233
                    break;
234
                    case KBC_IRQ:
235
                    if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) good = false;
236
                    case MOUSE_IRQ:
237
                    if (mouse_get_counter_mouse_ih() >= 3) {
238
                        mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp);
239
                        update_mouse(&pp);
240
                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
241
                        last_lb = keys->lb_pressed;
242
                        mouse_set_counter_mouse_ih(0);
243
                    }
244
                    break;
245
                    case COM1_IRQ: nctp_ih(); break;
246
                }
247
            }
248
        }
249
    }
250
    return 0;
251
}
252

    
253
static int (multiplayer_host)(void) {
254
    int r;
255

    
256
    nctp_dump();
257
    nctp_set_processor(multiplayer_process);
258

    
259
    ent_set_scale(DEFAULT_SCALE);
260
    text_timer_t *in_game_timer = text_timer_ctor(font_get_default());
261

    
262
    list_t *shooter_list = list_ctor();
263

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

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

    
270
    list_insert(shooter_list, list_end(shooter_list), shooter1);
271
    list_insert(shooter_list, list_end(shooter_list), shooter2);
272

    
273
    do {
274
        get_random_spawn(map1, shooter1, shooter_list);
275
        get_random_spawn(map1, shooter2, shooter_list);
276
    } while (gunner_distance(shooter1, shooter2) < 500);
277

    
278
    host_info = host_info_ctor(shooter1, shooter2);
279
    remote_info = remote_info_ctor();
280
    bullet_info = bullet_info_ctor();
281

    
282
    list_t *bullet_list  = list_ctor();
283

    
284
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
285
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
286

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

    
309
                    update_game_state(map1, shooter_list, bullet_list);
310

    
311
                    p1 = list_find(shooter_list, shooter1);
312
                    p2 = list_find(shooter_list, shooter2);
313

    
314
                    if ((state_1 = (p1 == list_end(shooter_list))) || (state_2 = (p2 == list_end(shooter_list)))) {
315
                        state = state_1 - state_2;
316
                        good = false;
317
                        break;
318
                    }
319

    
320
                    double angle = get_mouse_angle(shooter1);
321
                    gunner_set_angle(shooter1, angle - M_PI_2);
322

    
323
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
324
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
325

    
326
                    gunner_set_angle(shooter2, remote_info->remote_angle);
327
                    build_host_structure(host_info, shooter1, shooter2);
328

    
329
                    hltp_send_host_info(host_info);
330

    
331
                    graph_clear_screen();
332
                    map_draw   (map1);
333
                    bullet_draw_list(bullet_list);
334
                    gunner_draw_list(shooter_list);
335

    
336
                    text_draw(in_game_timer->text);
337

    
338
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
339
                    sprite_draw(sp_crosshair);
340
                    graph_draw();
341

    
342
                    break;
343
                    case KBC_IRQ:
344
                    if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) {
345
                        good = false;
346
                    }
347
                    break;
348
                    case MOUSE_IRQ:
349
                    if (mouse_get_counter_mouse_ih() >= 3) {
350
                        mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp);
351
                        update_mouse(&pp);
352
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
353
                        shoot_bullet(shooter1, bullet_list, bsp_bullet);
354
                        last_lb = keys->lb_pressed;
355
                        mouse_set_counter_mouse_ih(0);
356
                    }
357
                    break;
358

    
359
                    case COM1_IRQ:
360
                        nctp_ih();
361
                        if (bullet_info->new_bullet) {
362
                            shoot_bullet(shooter2, bullet_list, bsp_bullet);
363
                            bullet_info->new_bullet = false;
364
                        }
365
                        break;
366
                }
367
            }
368
        }
369
    }
370

    
371
    while(list_size(shooter_list) > 0){
372
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
373
        gunner_dtor(p);
374
    }
375
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
376

    
377
    while(list_size(bullet_list) > 0){
378
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
379
        bullet_dtor(p);
380
    }
381
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
382

    
383
    host_info_dtor(host_info);
384
    remote_info_dtor(remote_info);
385
    bullet_info_dtor(bullet_info);
386

    
387
    nctp_set_processor(NULL);
388

    
389
    text_timer_dtor(in_game_timer); in_game_timer = NULL;
390

    
391
    return 0;
392
}
393
static int (multiplayer_remote)(void) {
394
    int r;
395

    
396
    nctp_dump();
397
    nctp_set_processor(multiplayer_process);
398

    
399
    ent_set_scale(DEFAULT_SCALE);
400
    text_timer_t *in_game_timer = text_timer_ctor(font_get_default());
401

    
402
    list_t *shooter_list = list_ctor();
403

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

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

    
410
    list_insert(shooter_list, list_end(shooter_list), shooter1);
411
    list_insert(shooter_list, list_end(shooter_list), shooter2);
412

    
413
    host_info = host_info_ctor(shooter2, shooter1);
414
    remote_info = remote_info_ctor();
415
    bullet_info = bullet_info_ctor();
416

    
417
    list_t *bullet_list  = list_ctor();
418

    
419
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
420
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
421

    
422
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
423
    uint8_t last_lb = 0;
424
    struct packet pp;
425
    keys_t *keys = get_key_presses();
426

    
427
    /// loop stuff
428
    uint64_t int_vector = 0;
429
    int good = true;
430
    while (good) {
431
        if ((r = get_interrupts_vector(&int_vector))) return r;
432
        uint32_t n = 1;
433
        for (uint8_t i = 0; i < 32; i++, n <<= 1) {
434
            if (int_vector & n) {
435
                interrupt_handler(i);
436
                switch (i) {
437
                    case TIMER0_IRQ:
438
                    if (timer_get_no_interrupts() % 60 == 0) text_timer_update(in_game_timer);
439

    
440
                    double angle = get_mouse_angle(shooter1) - M_PI_2;
441

    
442
                    build_remote_structure(remote_info, keys, angle);
443

    
444

    
445
                    //hltp_send_remote_info(remote_info);
446
                    gunner_set_pos(shooter1, (double)host_info->remote_x, (double)host_info->remote_y);
447
                    gunner_set_angle(shooter1, (double)host_info->remote_angle);
448
                    gunner_set_health(shooter1, (double)host_info->remote_health);
449
                    gunner_set_curr_health(shooter1, (double)host_info->remote_current_health);
450

    
451
                    gunner_set_pos(shooter2, (double)host_info->host_x, (double)host_info->host_y);
452
                    gunner_set_angle(shooter2, (double)host_info->host_angle);
453
                    gunner_set_health(shooter2, (double)host_info->host_health);
454
                    gunner_set_curr_health(shooter2, (double)host_info->host_current_health);
455

    
456
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
457
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
458

    
459
                    /*
460
                    for (size_t j = 0; j < host_info->no_bullets; j++) {
461
                        if (host_info->bullets_shooter[j]) { // remote
462
                            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]);
463
                            list_insert(bullet_list, list_end(bullet_list), bullet);
464
                        } else { // host
465
                            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]);
466
                            list_insert(bullet_list, list_end(bullet_list), bullet);
467
                        }
468
                    }*/
469

    
470
                    graph_clear_screen();
471
                    map_draw   (map1);
472
                    bullet_draw_list(bullet_list);
473
                    gunner_draw_list(shooter_list);
474

    
475
                    text_draw(in_game_timer->text);
476

    
477
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
478
                    sprite_draw(sp_crosshair);
479
                    graph_draw();
480
                    /*
481
                    while(list_size(bullet_list) > 0){
482
                        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
483
                        bullet_dtor(p);
484
                    }
485
                    */
486

    
487
                    break;
488
                    case KBC_IRQ:
489
                    if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) {
490
                        good = false;
491
                    }
492
                    break;
493
                    case MOUSE_IRQ:
494
                    if (mouse_get_counter_mouse_ih() >= 3) {
495
                        mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp);
496
                        update_mouse(&pp);
497
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed) {
498
                            bullet_info->new_bullet = true;
499
                            //hltp_send_bullet_info(bullet_info);
500
                        }
501
                        last_lb = keys->lb_pressed;
502
                        mouse_set_counter_mouse_ih(0);
503
                    }
504
                    break;
505

    
506
                    case COM1_IRQ:
507
                        nctp_ih();
508
                        break;
509
                }
510
            }
511
        }
512
    }
513

    
514
    while(list_size(shooter_list) > 0){
515
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
516
        gunner_dtor(p);
517
    }
518
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
519

    
520
    while(list_size(bullet_list) > 0){
521
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
522
        bullet_dtor(p);
523
    }
524
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
525

    
526
    host_info_dtor(host_info);
527
    remote_info_dtor(remote_info);
528
    bullet_info_dtor(bullet_info);
529

    
530
    nctp_set_processor(NULL);
531

    
532
    text_timer_dtor(in_game_timer); in_game_timer = NULL;
533

    
534
    return 0;
535
}