Project

General

Profile

Revision 311

multiplayer initial

View differences:

proj/include/proj_func.h
3 3

  
4 4
#include "ent.h"
5 5
#include "font.h"
6
#include "proj_structures.h"
6 7

  
7 8
#include <stdint.h>
8 9

  
......
13 14
 */
14 15
int cleanup(void);
15 16

  
16
typedef struct keys {
17
    uint8_t w_pressed       : 1;
18
    uint8_t a_pressed       : 1;
19
    uint8_t s_pressed       : 1;
20
    uint8_t d_pressed       : 1;
21
    uint8_t ctrl_pressed    : 1;
22
    uint8_t plus_pressed    : 1;
23
    uint8_t minus_pressed   : 1;
24
    uint8_t lb_pressed      : 1;
25
} keys_t;
26

  
27 17
void update_key_presses(void);
28 18

  
29 19
/**
proj/libs/uart/include/hltp.h
2 2
#define HLTP_H_INCLUDED
3 3

  
4 4
#include "uart.h"
5
#include "proj_structures.h"
5 6

  
6 7
typedef enum{
7 8
    hltp_type_invalid = 0x00,
8
    hltp_type_string  = 0x53
9
    hltp_type_string  = 0x53,
10
    hltp_type_host    = 0x54,
11
    hltp_type_remote  = 0x55
9 12
} hltp_type;
10 13

  
11 14
int hltp_send_string(const char *p);
15
int hltp_send_host_info(const host_info_t *p);
16
int hltp_send_remote_info(const remote_info_t *p);
12 17
hltp_type hltp_interpret(const uint8_t *p, const size_t sz, void **dest);
13

  
14 18
#endif //HLTP_H_INCLUDED
proj/libs/uart/src/hltp.c
17 17
    return nctp_send(2, ptr, sz);
18 18
}
19 19

  
20
static host_info_t* hltp_interpret_host_info(const uint8_t *p) {
21
    host_info_t *ret = (host_info_t*)malloc(sizeof(host_info_t));
22
    size_t pos = 0;
23
    // players information
24
    memcpy(ret, p + pos, sizeof(double)*8);
25
    pos += sizeof(double)*8;
26
    // size of arrays
27
    memcpy(&(ret->no_bullets), p + pos, sizeof(size_t));
28
    pos += sizeof(size_t);
29
    size_t sz = ret->no_bullets;
30
    // array containing the X positions of the bullets
31
    (ret->bullets_x) = (double*)malloc(sizeof(double)*sz);
32
    memcpy((ret->bullets_x), p + pos, sizeof(double)*sz);
33
    pos += sizeof(double)*sz;
34
    // array containing the Y positions of the bullets
35
    (ret->bullets_y) = (double*)malloc(sizeof(double)*(ret->no_bullets));
36
    memcpy((ret->bullets_y), p + pos, sizeof(double)*sz);
37
    pos += sizeof(double)*sz;
38
    // array containing the X velocity of the bullets
39
    (ret->bullets_vx) = (double*)malloc(sizeof(double)*(ret->no_bullets));
40
    memcpy((ret->bullets_vx), p + pos, sizeof(double)*sz);
41
    pos += sizeof(double)*sz;
42
    // array containing the Y velocity of the bullets
43
    (ret->bullets_vy) = (double*)malloc(sizeof(double)*(ret->no_bullets));
44
    memcpy((ret->bullets_vy), p + pos, sizeof(double)*sz);
45
    pos += sizeof(double)*sz;
46
    // array containing the shooter id of the bullets
47
    (ret->bullets_shooter) = (bool*)malloc(sizeof(bool)*(ret->no_bullets));
48
    memcpy((ret->bullets_shooter), p + pos, sizeof(bool)*sz);
49

  
50
    return ret;
51
}
52
int hltp_send_host_info(const host_info_t *p) {
53
    // struct size
54
    size_t struct_size = sizeof(double) * (8 + 4*p->no_bullets) + p->no_bullets * sizeof(bool) + sizeof(size_t);
55
    uint8_t *base = (uint8_t*)malloc(struct_size);
56
    uint8_t *aux = base;
57
    // players information
58
    memcpy(aux, p, sizeof(double) * 8);
59
    aux = aux + sizeof(double) * 8;
60
    // size of arrays
61
    memcpy(aux, &(p->no_bullets), sizeof(size_t));
62
    aux = aux + sizeof(size_t);
63
    // array containing the X positions of the bullets
64
    memcpy(aux, p->bullets_x, sizeof(double) * (p->no_bullets));
65
    aux = aux + sizeof(double) * (p->no_bullets);
66
    // array containing the Y positions of the bullets
67
    memcpy(aux, p->bullets_y, sizeof(double) * (p->no_bullets));
68
    aux = aux + sizeof(double) * (p->no_bullets);
69
    // array containing the X velocity of the bullets
70
    memcpy(aux, p->bullets_vx, sizeof(double) * (p->no_bullets));
71
    aux = aux + sizeof(double) * (p->no_bullets);
72
    // array containing the Y velocity of the bullets
73
    memcpy(aux, p->bullets_vy, sizeof(double) * (p->no_bullets));
74
    aux = aux + sizeof(double) * (p->no_bullets);
75
    // array containing the shooter id of the bullets
76
    memcpy(aux, p->bullets_shooter, sizeof(bool) * (p->no_bullets));
77

  
78
    uint8_t type = hltp_type_host;
79
    uint8_t* ptr[2]; ptr[0] = &type; ptr[1] = base;
80
    size_t    sz[2]; sz [0] =     1; sz [1] = struct_size;
81
    return nctp_send(2, ptr, sz);
82
}
83

  
84
static remote_info_t* hltp_interpret_remote_info(const uint8_t *p) {
85
    remote_info_t *ret = (remote_info_t*)malloc(sizeof(remote_info_t));
86
    size_t pos = 0;
87
    // keys pressed
88
    memcpy(&(ret->remote_keys_pressed), p + pos, sizeof(keys_t));
89
    pos += sizeof(keys_t);
90
    // mouse positions
91
    memcpy(&(ret->remote_mouse_x), p + pos, sizeof(int32_t));
92
    pos += sizeof(int32_t);
93
    memcpy(&(ret->remote_mouse_y), p + pos, sizeof(int32_t));
94
    pos += sizeof(int32_t);
95
    // bullets shot
96
    memcpy(&(ret->bullets_shot), p + pos, sizeof(size_t));
97
    pos += sizeof(size_t);
98
    size_t sz = ret->bullets_shot;
99
    // array containing the X positions of the bullets
100
    (ret->bullets_x) = (double*)malloc(sizeof(double)*sz);
101
    memcpy((ret->bullets_x), p + pos, sizeof(double)*sz);
102
    pos += sizeof(double)*sz;
103
    // array containing the Y positions of the bullets
104
    (ret->bullets_y) = (double*)malloc(sizeof(double)*sz);
105
    memcpy((ret->bullets_y), p + pos, sizeof(double)*sz);
106
    pos += sizeof(double)*sz;
107
    // array containing the angle of the bullets
108
    (ret->bullets_angle) = (double*)malloc(sizeof(double)*sz);
109
    memcpy((ret->bullets_angle), p + pos, sizeof(double)*sz);
110
    pos += sizeof(double)*sz;
111

  
112
    return ret;
113
}
114

  
115
int hltp_send_remote_info(const remote_info_t *p) {
116
    // struct size
117
    size_t struct_size = sizeof(keys_t) + sizeof(int32_t) * 2 + sizeof(size_t) + sizeof(double) * (3 *p->bullets_shot);
118
    uint8_t *base = (uint8_t*)malloc(struct_size);
119
    uint8_t *aux = base;
120
    memcpy(aux, p, sizeof(keys_t));
121
    aux = aux + sizeof(keys_t);
122
    memcpy(aux, p, sizeof(int32_t)*2);
123
    aux = aux + sizeof(int32_t)*2;
124
    memcpy(aux, &(p->bullets_shot), sizeof(size_t));
125
    aux = aux + sizeof(size_t);
126
    memcpy(aux, p->bullets_x, sizeof(double)*(p->bullets_shot));
127
    aux = aux + sizeof(double)*(p->bullets_shot);
128
    memcpy(aux, p->bullets_y, sizeof(double)*(p->bullets_shot));
129
    aux = aux + sizeof(double)*(p->bullets_shot);
130
    memcpy(aux, p->bullets_angle, sizeof(double)*(p->bullets_shot));
131

  
132
    uint8_t type = hltp_type_remote;
133
    uint8_t* ptr[2]; ptr[0] = &type; ptr[1] = base;
134
    size_t    sz[2]; sz [0] =     1; sz [1] = struct_size;
135
    return nctp_send(2, ptr, sz);
136
}
137

  
20 138
hltp_type hltp_interpret(const uint8_t *p, const size_t sz, void **dest){
21 139
    uint8_t ret = p[0];
22 140
    switch(ret){
23 141
        case hltp_type_string: *dest = hltp_interpret_string(p+1, sz-1); break;
142
        case hltp_type_host  : *dest = hltp_interpret_host_info(p+1); break;
143
        case hltp_type_remote: *dest = hltp_interpret_remote_info(p+1); break;
24 144
        default: *dest = NULL; break;
25 145
    }
26 146
    return ret;
proj/src/proj.c
60 60
sprite_t             *sp_crosshair  = NULL;
61 61

  
62 62
static int (singleplayer)(void);
63
//static int (multiplayer)(void);
63
static int (multiplayer)(void);
64 64
static int (chat)(void);
65 65
int(proj_main_loop)(int argc, char *argv[]) {
66 66

  
......
130 130
                    switch(menu_update_state(main_menu, click)){
131 131
                        case -1: break;
132 132
                        case  0: singleplayer(); break; //campaign(); break;
133
                        case  1: break;
133
                        case  1: multiplayer() ; break;
134 134
                        case  2: chat(); break;
135 135
                        case  3: good = false; break;
136 136
                    }
......
184 184
    return 0;
185 185
}
186 186

  
187
typedef struct __attribute__((packed)) {
188
    // player 1
189
    double player1_x;
190
    double player1_y;
191
    double player1_health;
192
    double player1_current_health;
187
host_info_t     *host   = NULL;
188
remote_info_t   *remote = NULL;
193 189

  
194
    // player 2
195
    double player2_x;
196
    double player2_y;
197
    double player2_health;
198
    double player2_current_health;
190
static void multiplayer_process(const uint8_t *p, const size_t sz) {
191
    void *dest = NULL;
192
    hltp_type tp = hltp_interpret(p, sz, &dest);
193
    switch(tp){
194
        case hltp_type_host:
195
            host_info_dtor(host);
196
            host = (host_info_t*)dest;
197
            break;
198
        case hltp_type_remote:
199
            remote_info_dtor(remote);
200
            remote = (remote_info_t*)dest;
201
            break;
202
        default: break;
203
    }
204
}
205
static int (multiplayer_host)(void);
206
static int (multiplayer_remote)(void);
207
static int (multiplayer)(void) {
208
    int r;
199 209

  
200
    // bullets
201
    size_t no_bullets;
202
    double *bullets_x;
203
    double *bullets_y;
204
    double *bullets_vx;
205
    double *bullets_vy;
206
    bool   *bullet_shooter; // 0 for player 1, otherwise 1 for player 2
207
} host_info;
210
    menu_t *main_menu = menu_ctor(consolas);
211
    menu_add_item(main_menu, "Create");
212
    menu_add_item(main_menu, "Connect");
213
    menu_add_item(main_menu, "Back");
208 214

  
209
typedef struct __attribute__((packed)) {
210
    keys_t client_keys_pressed;
211
    int32_t client_mouse_x;
212
    int32_t client_mouse_y;
213
    size_t bullets_shot;
214
    double *bullets_x;
215
    double *bullets_y;
216
} client_info;
217
/*
218
static void host_to_client_process(const uint8_t *p, const size_t sz) {
215
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
216
    uint8_t last_lb = 0;
217
    struct packet pp;
218
    keys_t *keys = get_key_presses();
219 219

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

  
222
static void client_to_host_process(const uint8_t *p, const size_t sz) {
233
                    graph_clear_screen();
234
                    switch(menu_update_state(main_menu, click)){
235
                        case -1: break;
236
                        case  0: multiplayer_host(); break;
237
                        case  1: multiplayer_remote(); break;
238
                        case  2: good = false; break;
239
                    }
240
                    menu_draw(main_menu);
241

  
242
                    click = 0;
243

  
244
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
245
                    sprite_draw(sp_crosshair);
246
                    graph_draw();
247

  
248
                    break;
249
                    case KBC_IRQ:
250
                    if ((scancode[0]) == ESC_BREAK_CODE) good = false;
251
                    case MOUSE_IRQ:
252
                    if (counter_mouse_ih >= 3) {
253
                        mouse_parse_packet(packet_mouse_ih, &pp);
254
                        update_mouse(&pp);
255
                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
256
                        last_lb = keys->lb_pressed;
257
                        counter_mouse_ih = 0;
258
                    }
259
                    break;
260
                    case COM1_IRQ: nctp_ih(); break;
261
                }
262
            }
263
        }
264
    }
265
    return 0;
223 266
}
224 267

  
225
static int (multiplayer)(void) {
226

  
268
static int (multiplayer_host)(void) {
227 269
    //int r;
228 270

  
271
    nctp_dump();
272
    nctp_set_processor(multiplayer_process);/*
273

  
229 274
    ent_set_scale(DEFAULT_SCALE);
230
    //text_timer_t *in_game_timer = timer_ctor(consolas);
275
    text_timer_t *in_game_timer = timer_ctor(consolas);
231 276

  
232 277
    list_t *shooter_list = list_ctor();
233 278

  
......
258 303
    /// loop stuff
259 304
    uint32_t int_vector = 0;
260 305
    int good = true;
306
    int state = 0; // -1 for remote win, 0 for draw, 1 for host win
307
    while (good) {
308
        if ((r = get_interrupts_vector(&int_vector))) return r;
309
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
310
            interrupt_handler(i);
311
            switch (i) {
312
                case TIMER0_IRQ:
261 313

  
262
    // incomplete
314
                break;
263 315

  
316
                case KBC_IRQ:
317

  
318
                break;
319

  
320
                case MOUSE_IRQ:
321

  
322

  
323
                break;
324

  
325
                case COM1_IRQ: nctp_ih(); break;
326
            }
327
        }
328
    }*/
329

  
264 330
    return 0;
265
}*/
331
}
332
static int (multiplayer_remote)(void) {/*
333
    int r;
266 334

  
335
    nctp_dump();
336
    nctp_set_processor(multiplayer_process);
337

  
338
    ent_set_scale(DEFAULT_SCALE);
339
    text_timer_t *in_game_timer = timer_ctor(consolas);
340

  
341
    list_t *shooter_list = list_ctor();
342

  
343
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
344
    gunner_set_spawn(shooter1, 75, 75);
345

  
346
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter2 == NULL) printf("Failed to get shooter2\n");
347
    gunner_set_spawn(shooter2, 975, 75);
348

  
349
    list_insert(shooter_list, list_end(shooter_list), shooter1);
350
    list_insert(shooter_list, list_end(shooter_list), shooter2);
351

  
352
    do {
353
        get_random_spawn(map1, shooter1, shooter_list);
354
        get_random_spawn(map1, shooter2, shooter_list);
355
    } while (distance_gunners(shooter1, shooter2) < 700);
356

  
357
    list_t *bullet_list  = list_ctor();
358

  
359
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
360
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
361

  
362
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
363
    uint8_t last_lb = 0;
364
    struct packet pp;
365
    keys_t *keys = get_key_presses();
366

  
367
    /// loop stuff
368
    uint32_t int_vector = 0;
369
    int good = true;
370
    int state = 0; // -1 for remote win, 0 for draw, 1 for host win
371
    while (good) {
372
        if ((r = get_interrupts_vector(&int_vector))) return r;
373
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
374
            interrupt_handler(i);
375
            switch (i) {
376
                case TIMER0_IRQ:
377

  
378
                break;
379

  
380
                case KBC_IRQ:
381

  
382
                break;
383

  
384
                case MOUSE_IRQ:
385

  
386

  
387
                break;
388

  
389
                case COM1_IRQ: nctp_ih(); break;
390
            }
391
        }
392
    }*/
393

  
394
    return 0;
395
}
396

  
267 397
static int (campaign)(void);
268 398
static int (zombies)(void);
269 399
static int (singleplayer)(void) {
......
649 779
    rectangle_set_outline_width(r_text, 2);
650 780
    rectangle_set_outline_color(r_text, GRAPH_WHITE);
651 781
    rectangle_set_fill_trans(r_text, GRAPH_TRANSPARENT);
782
    }
783
    /** t_text */ {
784
    for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
785
        t_text[i] = text_ctor(consolas, " ");
786
        text_set_pos(t_text[i], rectangle_get_x(r_text)+50,
787
        rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-25*i);
788
        text_set_halign(t_text[i], text_halign_left);
789
        text_set_valign(t_text[i], text_valign_bottom);
790
        text_set_color (t_text[i], TEXT_COLOR);
791
    }
652 792
}
653
/** t_text */ {
654
for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
655
    t_text[i] = text_ctor(consolas, " ");
656
    text_set_pos(t_text[i], rectangle_get_x(r_text)+50,
657
    rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-25*i);
658
    text_set_halign(t_text[i], text_halign_left);
659
    text_set_valign(t_text[i], text_valign_bottom);
660
    text_set_color (t_text[i], TEXT_COLOR);
661
}
662
}
663 793

  
664 794
/// loop stuff
665 795
uint32_t int_vector = 0;
proj/src/proj_func.c
27 27

  
28 28
static keys_t key_presses;
29 29

  
30
void host_info_dtor(host_info_t *p) {
31
    if (p==NULL) return;
32

  
33
    if ((p->bullets_x) != NULL){ free(p->bullets_x); p->bullets_x = NULL; }
34

  
35
    if ((p->bullets_y) != NULL){ free(p->bullets_y); p->bullets_y = NULL; }
36

  
37
    if ((p->bullets_vx) != NULL){ free(p->bullets_vx); p->bullets_vx = NULL; }
38

  
39
    if ((p->bullets_vy) != NULL){ free(p->bullets_vy); p->bullets_vy = NULL; }
40

  
41
    if ((p->bullets_shooter) != NULL){ free(p->bullets_shooter); p->bullets_shooter = NULL; }
42

  
43
    free(p);
44
}
45

  
46
void remote_info_dtor(remote_info_t *p) {
47
    if (p==NULL) return;
48

  
49
    if ((p->bullets_x) != NULL){ free(p->bullets_x); p->bullets_x = NULL; }
50

  
51
    if ((p->bullets_y) != NULL){ free(p->bullets_y); p->bullets_y = NULL; }
52

  
53
    if ((p->bullets_angle) != NULL){ free(p->bullets_angle); p->bullets_angle = NULL; }
54

  
55
    free(p);
56
}
57

  
30 58
void update_key_presses(void) {
31 59
    if (sz == 1) {
32 60
        switch(scancode[0]) {

Also available in: Unified diff