Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 339

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