Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 337

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