eco2d/code/game/source/packets/pkt_send_librg_update.c

50 lines
1.6 KiB
C
Raw Normal View History

#include "zpl.h"
2021-05-05 13:14:02 +00:00
#include "packet_utils.h"
#include "packets/pkt_send_librg_update.h"
#include "world/world.h"
2021-05-06 15:30:38 +00:00
#include "game.h"
2021-05-05 13:14:02 +00:00
2021-05-10 14:33:32 +00:00
size_t pkt_send_librg_update(uint64_t peer_id,
uint16_t view_id,
uint8_t ticker,
void *data,
size_t datalen) {
2021-05-12 14:42:22 +00:00
return pkt_world_write(MSG_ID_LIBRG_UPDATE, pkt_send_librg_update_encode(data, (int32_t)datalen, ticker), 1, view_id, (void*)peer_id);
2021-05-10 14:33:32 +00:00
}
size_t pkt_send_librg_update_encode(void *data, int32_t data_length, uint8_t layer_id) {
2021-05-05 13:14:02 +00:00
cw_pack_context pc = {0};
pkt_pack_msg(&pc, 2);
cw_pack_unsigned(&pc, layer_id);
2021-05-05 13:14:02 +00:00
cw_pack_bin(&pc, data, data_length);
return pkt_pack_msg_size(&pc);
}
int32_t pkt_send_librg_update_handler(pkt_header *header) {
cw_unpack_context uc = {0};
pkt_unpack_msg(&uc, header, 2);
cw_unpack_next(&uc);
if (uc.item.type != CWP_ITEM_POSITIVE_INTEGER)
return -1;
uint8_t layer_id = (uint8_t)uc.item.as.u64;
2021-05-05 13:14:02 +00:00
cw_unpack_next(&uc);
if (uc.item.type != CWP_ITEM_BIN)
return -1;
2021-05-05 13:52:28 +00:00
2021-05-06 15:30:38 +00:00
world_view *view = game_world_view_get(header->view_id);
view->active_layer_id = layer_id;
2021-05-06 15:30:38 +00:00
int32_t state = librg_world_read(view->tracker, header->view_id, uc.item.as.bin.start, uc.item.as.bin.length, NULL);
2021-05-05 22:03:43 +00:00
if (state < 0) zpl_printf("[ERROR] world read error: %d\n", state);
uint64_t now = zpl_time_rel_ms();
view->delta_time[layer_id] = now - view->last_update[layer_id];
view->last_update[layer_id] = now;
2021-05-05 22:03:43 +00:00
return state;
2021-05-05 13:14:02 +00:00
}