Revision 320
multiplayer almost working, serial data not being transmitted?
hltp.c | ||
---|---|---|
21 | 21 |
host_info_t *ret = (host_info_t*)malloc(sizeof(host_info_t)); |
22 | 22 |
size_t pos = 0; |
23 | 23 |
// players information |
24 |
memcpy(ret, p + pos, sizeof(double)*8);
|
|
25 |
pos += sizeof(double)*8;
|
|
24 |
memcpy(ret, p + pos, sizeof(double)*10);
|
|
25 |
pos += sizeof(double)*10;
|
|
26 | 26 |
// size of arrays |
27 | 27 |
memcpy(&(ret->no_bullets), p + pos, sizeof(size_t)); |
28 | 28 |
pos += sizeof(size_t); |
... | ... | |
52 | 52 |
int hltp_send_host_info(const host_info_t *p) { |
53 | 53 |
|
54 | 54 |
uint8_t type = hltp_type_host; |
55 |
uint8_t* ptr[15]; size_t sz[15]; |
|
56 |
ptr[0] = (uint8_t*)& type ; sz[0] = 1; |
|
57 |
ptr[1] = (uint8_t*)&p->host_x ; sz[0] = sizeof(double); |
|
58 |
ptr[2] = (uint8_t*)&p->host_y ; sz[0] = sizeof(double); |
|
59 |
ptr[3] = (uint8_t*)&p->host_health ; sz[0] = sizeof(double); |
|
60 |
ptr[4] = (uint8_t*)&p->host_current_health ; sz[0] = sizeof(double); |
|
61 |
ptr[5] = (uint8_t*)&p->remote_x ; sz[0] = sizeof(double); |
|
62 |
ptr[6] = (uint8_t*)&p->remote_y ; sz[0] = sizeof(double); |
|
63 |
ptr[7] = (uint8_t*)&p->remote_health ; sz[0] = sizeof(double); |
|
64 |
ptr[8] = (uint8_t*)&p->remote_current_health ; sz[0] = sizeof(double); |
|
65 |
ptr[9] = (uint8_t*)&p->no_bullets ; sz[0] = sizeof(size_t); |
|
66 |
ptr[10] = (uint8_t*) p->bullets_x ; sz[0] = sizeof(double) * p->no_bullets; |
|
67 |
ptr[11] = (uint8_t*) p->bullets_y ; sz[0] = sizeof(double) * p->no_bullets; |
|
68 |
ptr[12] = (uint8_t*) p->bullets_vx ; sz[0] = sizeof(double) * p->no_bullets; |
|
69 |
ptr[13] = (uint8_t*) p->bullets_vy ; sz[0] = sizeof(double) * p->no_bullets; |
|
70 |
ptr[14] = (uint8_t*) p->bullets_shooter ; sz[0] = sizeof(double) * p->no_bullets; |
|
71 |
return nctp_send(2, ptr, sz); |
|
55 |
uint8_t* ptr[17]; size_t sz[17]; |
|
56 |
ptr[0] = (uint8_t*)& type ; sz[0] = 1; |
|
57 |
ptr[1] = (uint8_t*)&p->host_x ; sz[1] = sizeof(double); |
|
58 |
ptr[2] = (uint8_t*)&p->host_y ; sz[2] = sizeof(double); |
|
59 |
ptr[3] = (uint8_t*)&p->host_angle ; sz[3] = sizeof(double); |
|
60 |
ptr[4] = (uint8_t*)&p->host_health ; sz[4] = sizeof(double); |
|
61 |
ptr[5] = (uint8_t*)&p->host_current_health ; sz[5] = sizeof(double); |
|
62 |
ptr[6] = (uint8_t*)&p->remote_x ; sz[6] = sizeof(double); |
|
63 |
ptr[7] = (uint8_t*)&p->remote_y ; sz[7] = sizeof(double); |
|
64 |
ptr[8] = (uint8_t*)&p->remote_angle ; sz[8] = sizeof(double); |
|
65 |
ptr[9] = (uint8_t*)&p->remote_health ; sz[9] = sizeof(double); |
|
66 |
ptr[10] = (uint8_t*)&p->remote_current_health ; sz[10] = sizeof(double); |
|
67 |
ptr[11] = (uint8_t*)&p->no_bullets ; sz[11] = sizeof(size_t); |
|
68 |
ptr[12] = (uint8_t*) p->bullets_x ; sz[12] = sizeof(double) * p->no_bullets; |
|
69 |
ptr[13] = (uint8_t*) p->bullets_y ; sz[13] = sizeof(double) * p->no_bullets; |
|
70 |
ptr[14] = (uint8_t*) p->bullets_vx ; sz[14] = sizeof(double) * p->no_bullets; |
|
71 |
ptr[15] = (uint8_t*) p->bullets_vy ; sz[15] = sizeof(double) * p->no_bullets; |
|
72 |
ptr[16] = (uint8_t*) p->bullets_shooter ; sz[16] = sizeof(double) * p->no_bullets; |
|
73 |
return nctp_send(17, ptr, sz); |
|
72 | 74 |
} |
73 | 75 |
|
74 | 76 |
static remote_info_t* hltp_interpret_remote_info(const uint8_t *p) { |
... | ... | |
78 | 80 |
memcpy(&(ret->remote_keys_pressed), p + pos, sizeof(keys_t)); |
79 | 81 |
pos += sizeof(keys_t); |
80 | 82 |
// mouse positions |
81 |
memcpy(&(ret->remote_mouse_x), p + pos, sizeof(int32_t)); |
|
82 |
pos += sizeof(int32_t); |
|
83 |
memcpy(&(ret->remote_mouse_y), p + pos, sizeof(int32_t)); |
|
84 |
pos += sizeof(int32_t); |
|
85 |
// bullets shot |
|
86 |
memcpy(&(ret->bullets_shot), p + pos, sizeof(size_t)); |
|
87 |
pos += sizeof(size_t); |
|
88 |
size_t sz = ret->bullets_shot; |
|
89 |
// array containing the X positions of the bullets |
|
90 |
(ret->bullets_x) = (double*)malloc(sizeof(double)*sz); |
|
91 |
memcpy((ret->bullets_x), p + pos, sizeof(double)*sz); |
|
92 |
pos += sizeof(double)*sz; |
|
93 |
// array containing the Y positions of the bullets |
|
94 |
(ret->bullets_y) = (double*)malloc(sizeof(double)*sz); |
|
95 |
memcpy((ret->bullets_y), p + pos, sizeof(double)*sz); |
|
96 |
pos += sizeof(double)*sz; |
|
97 |
// array containing the angle of the bullets |
|
98 |
(ret->bullets_angle) = (double*)malloc(sizeof(double)*sz); |
|
99 |
memcpy((ret->bullets_angle), p + pos, sizeof(double)*sz); |
|
100 |
pos += sizeof(double)*sz; |
|
83 |
memcpy(&(ret->remote_angle), p + pos, sizeof(double)); |
|
101 | 84 |
|
102 | 85 |
return ret; |
103 | 86 |
} |
104 | 87 |
int hltp_send_remote_info(const remote_info_t *p) { |
105 | 88 |
|
106 | 89 |
uint8_t type = hltp_type_remote; |
107 |
uint8_t* ptr[8]; size_t sz[8];
|
|
90 |
uint8_t* ptr[3]; size_t sz[3];
|
|
108 | 91 |
ptr[0] = (uint8_t*)& type ; sz[0] = 1; |
109 |
ptr[1] = (uint8_t*)&p->remote_keys_pressed ; sz[0] = sizeof(keys_t); |
|
110 |
ptr[2] = (uint8_t*)&p->remote_mouse_x ; sz[0] = sizeof(int32_t); |
|
111 |
ptr[3] = (uint8_t*)&p->remote_mouse_y ; sz[0] = sizeof(int32_t); |
|
112 |
ptr[4] = (uint8_t*)&p->bullets_shot ; sz[0] = sizeof(size_t); |
|
113 |
ptr[5] = (uint8_t*) p->bullets_x ; sz[0] = sizeof(double) * p->bullets_shot; |
|
114 |
ptr[6] = (uint8_t*) p->bullets_y ; sz[0] = sizeof(double) * p->bullets_shot; |
|
115 |
ptr[7] = (uint8_t*) p->bullets_angle ; sz[0] = sizeof(double) * p->bullets_shot; |
|
92 |
ptr[1] = (uint8_t*)&p->remote_keys_pressed ; sz[1] = sizeof(keys_t); |
|
93 |
ptr[2] = (uint8_t*)&p->remote_angle ; sz[2] = sizeof(double); |
|
94 |
return nctp_send(3, ptr, sz); |
|
95 |
} |
|
96 |
|
|
97 |
static bullet_info_t* hltp_interpret_bullet_info(const uint8_t *p) { |
|
98 |
bullet_info_t *ret = (bullet_info_t*)malloc(sizeof(bullet_info_t)); |
|
99 |
memcpy(&(ret->new_bullet), p, sizeof(bool)); |
|
100 |
|
|
101 |
return ret; |
|
102 |
} |
|
103 |
|
|
104 |
int hltp_send_bullet_info(const bullet_info_t *p) { |
|
105 |
uint8_t type = hltp_type_bullet; |
|
106 |
uint8_t* ptr[2]; size_t sz[2]; |
|
107 |
ptr[0] = (uint8_t*)& type ; sz[0] = 1; |
|
108 |
ptr[1] = (uint8_t*)&p->new_bullet ; sz[1] = sizeof(bool); |
|
116 | 109 |
return nctp_send(2, ptr, sz); |
117 | 110 |
} |
118 | 111 |
|
119 | 112 |
hltp_type hltp_interpret(const uint8_t *p, const size_t sz, void **dest){ |
120 | 113 |
uint8_t ret = p[0]; |
121 | 114 |
switch(ret){ |
122 |
case hltp_type_string: *dest = hltp_interpret_string(p+1, sz-1); break; |
|
123 |
case hltp_type_host : *dest = hltp_interpret_host_info(p+1); break; |
|
124 |
case hltp_type_remote: *dest = hltp_interpret_remote_info(p+1); break; |
|
115 |
case hltp_type_string: *dest = hltp_interpret_string (p+1, sz-1); break; |
|
116 |
case hltp_type_host : *dest = hltp_interpret_host_info (p+1); break; |
|
117 |
case hltp_type_remote: *dest = hltp_interpret_remote_info(p+1); break; |
|
118 |
case hltp_type_bullet: *dest = hltp_interpret_bullet_info(p+1); break; |
|
125 | 119 |
default: *dest = NULL; break; |
126 | 120 |
} |
127 | 121 |
return ret; |
Also available in: Unified diff