pkt: PKT_KEEP_IF implementation

isolation_bkp/dynres
Dominik Madarász 2021-08-09 16:33:46 +02:00
parent 83fa4b6daf
commit 55ad205441
3 changed files with 11 additions and 6 deletions

View File

@ -10,14 +10,14 @@ pkt_desc pkt_entity_view_desc[] = {
{ PKT_HALF(entity_view, x) }, { PKT_HALF(entity_view, x) },
{ PKT_HALF(entity_view, y) }, { PKT_HALF(entity_view, y) },
{ PKT_SKIP_IF(entity_view, blocks_used, 1, 2) }, // NOTE(zaklaus): skip velocity for chunks { PKT_KEEP_IF(entity_view, blocks_used, 0, 2) }, // NOTE(zaklaus): skip velocity for chunks
{ PKT_HALF(entity_view, vx) }, { PKT_HALF(entity_view, vx) },
{ PKT_HALF(entity_view, vy) }, { PKT_HALF(entity_view, vy) },
{ PKT_SKIP_IF(entity_view, blocks_used, 0, 1) }, // NOTE(zaklaus): skip blocks for anything else { PKT_SKIP_IF(entity_view, blocks_used, 0, 1) }, // NOTE(zaklaus): skip blocks for anything else
{ PKT_ARRAY(entity_view, blocks) }, { PKT_ARRAY(entity_view, blocks) },
{ PKT_SKIP_IF(entity_view, blocks_used, 1, 2) }, // NOTE(zaklaus): skip hp for chunks { PKT_KEEP_IF(entity_view, blocks_used, 0, 2) }, // NOTE(zaklaus): skip hp for chunks
{ PKT_HALF(entity_view, hp) }, { PKT_HALF(entity_view, hp) },
{ PKT_HALF(entity_view, max_hp) }, { PKT_HALF(entity_view, max_hp) },

View File

@ -106,9 +106,10 @@ int32_t pkt_pack_struct(cw_pack_context *pc, pkt_desc *desc, void *raw_blob, uin
for (pkt_desc *field = desc; field->type != CWP_NOT_AN_ITEM; ++field) { for (pkt_desc *field = desc; field->type != CWP_NOT_AN_ITEM; ++field) {
if (field->skip_count != 0) { if (field->skip_count != 0) {
uint8_t val = *(uint8_t*)(blob + field->offset); uint8_t val = *(uint8_t*)(blob + field->offset);
if (val == field->skip_eq) { if ((field->skip_count > 0 && val == field->skip_eq) ||
cw_pack_unsigned(pc, field->skip_count); (field->skip_count < 0 && val != field->skip_eq)) {
field += field->skip_count; cw_pack_unsigned(pc, zpl_abs(field->skip_count));
field += zpl_abs(field->skip_count);
} else { } else {
cw_pack_unsigned(pc, 0); cw_pack_unsigned(pc, 0);
} }

View File

@ -101,6 +101,10 @@ static inline int32_t pkt_world_write(pkt_messages id, size_t pkt_size, int8_t i
#define PKT_SKIP_IF(t, a, e, n) .skip_count = n, .offset = PKT_OFFSETOF(t, a), .skip_eq = e, .name = #a #define PKT_SKIP_IF(t, a, e, n) .skip_count = n, .offset = PKT_OFFSETOF(t, a), .skip_eq = e, .name = #a
#endif #endif
#ifndef PKT_KEEP_IF
#define PKT_KEEP_IF(t, a, e, n) .skip_count = -(n), .offset = PKT_OFFSETOF(t, a), .skip_eq = e, .name = #a
#endif
#ifndef PKT_END #ifndef PKT_END
#define PKT_END .type = CWP_NOT_AN_ITEM #define PKT_END .type = CWP_NOT_AN_ITEM
#endif #endif
@ -115,7 +119,7 @@ typedef struct pkt_desc {
size_t offset; size_t offset;
size_t size; size_t size;
size_t it_size; size_t it_size;
size_t skip_count; int16_t skip_count;
uint8_t skip_eq; uint8_t skip_eq;
} pkt_desc; } pkt_desc;