eco2d/code/foundation/src/world/entity_view.c

119 lines
3.9 KiB
C
Raw Normal View History

#include "world/entity_view.h"
#include "pkt/packet_utils.h"
2021-07-18 18:30:27 +00:00
#include "world/blocks.h"
2021-05-05 09:25:05 +00:00
ZPL_TABLE_DEFINE(entity_view_tbl, entity_view_tbl_, entity_view);
2021-05-06 16:26:52 +00:00
pkt_desc pkt_entity_view_desc[] = {
2021-08-09 17:30:39 +00:00
2021-05-06 18:24:01 +00:00
{ PKT_UINT(entity_view, kind) },
{ PKT_UINT(entity_view, flag) },
{ PKT_HALF(entity_view, x) },
{ PKT_HALF(entity_view, y) },
2021-08-09 13:59:50 +00:00
2021-08-09 14:33:46 +00:00
{ PKT_KEEP_IF(entity_view, blocks_used, 0, 2) }, // NOTE(zaklaus): skip velocity for chunks
{ PKT_HALF(entity_view, vx) },
{ PKT_HALF(entity_view, vy) },
2021-08-09 13:59:50 +00:00
2021-08-30 09:59:36 +00:00
{ PKT_SKIP_IF(entity_view, blocks_used, 0, 2) }, // NOTE(zaklaus): skip blocks for anything else
{ PKT_ARRAY(entity_view, blocks) },
2021-08-30 09:59:36 +00:00
{ PKT_ARRAY(entity_view, outer_blocks) },
2021-08-09 13:59:50 +00:00
2021-08-09 14:33:46 +00:00
{ PKT_KEEP_IF(entity_view, blocks_used, 0, 2) }, // NOTE(zaklaus): skip hp for chunks
2021-07-27 16:34:31 +00:00
{ PKT_HALF(entity_view, hp) },
{ PKT_HALF(entity_view, max_hp) },
2021-08-09 13:59:50 +00:00
2021-08-09 18:58:52 +00:00
{ PKT_KEEP_IF(entity_view, kind, EKIND_VEHICLE, 1) }, // NOTE(zaklaus): keep for vehicles
{ PKT_HALF(entity_view, heading) },
2021-09-09 07:54:02 +00:00
{ PKT_UINT(entity_view, inside_vehicle) },
2022-09-29 15:35:43 +00:00
{ PKT_UINT(entity_view, veh_kind) },
2021-08-09 18:58:52 +00:00
2021-08-30 15:50:05 +00:00
{ PKT_KEEP_IF(entity_view, kind, EKIND_ITEM, 2) },
2021-08-25 21:36:20 +00:00
{ PKT_UINT(entity_view, asset) },
2021-08-30 15:50:05 +00:00
{ PKT_UINT(entity_view, quantity) },
2022-08-09 15:32:58 +00:00
{ PKT_KEEP_IF(entity_view, kind, EKIND_DEVICE, 1) },
{ PKT_UINT(entity_view, asset) },
2021-08-30 15:50:05 +00:00
{ PKT_KEEP_IF(entity_view, has_items, true, 3) },
{ PKT_UINT(entity_view, has_items) },
{ PKT_UINT(entity_view, selected_item) },
2021-08-30 15:56:23 +00:00
{ PKT_ARRAY(entity_view, items) },
2021-08-25 21:36:20 +00:00
2022-08-09 14:46:23 +00:00
{ PKT_UINT(entity_view, pick_ent) },
{ PKT_UINT(entity_view, sel_ent) },
{ PKT_KEEP_IF(entity_view, has_storage_items, true, 3) },
{ PKT_UINT(entity_view, has_storage_items) },
{ PKT_UINT(entity_view, storage_selected_item) },
{ PKT_ARRAY(entity_view, storage_items) },
2021-05-06 16:26:52 +00:00
{ PKT_END },
};
size_t entity_view_pack_struct(void *data, size_t len, entity_view *view) {
2021-05-06 16:26:52 +00:00
cw_pack_context pc = {0};
2021-05-10 09:40:56 +00:00
cw_pack_context_init(&pc, data, (unsigned long)len, 0);
pkt_pack_struct(&pc, pkt_entity_view_desc, PKT_STRUCT_PTR(view));
2021-05-06 18:24:01 +00:00
return pc.current - pc.start;
2021-05-06 16:26:52 +00:00
}
entity_view entity_view_unpack_struct(void *data, size_t len) {
cw_unpack_context uc = {0};
2021-05-10 09:40:56 +00:00
cw_unpack_context_init(&uc, data, (unsigned long)len, 0);
2021-05-06 16:26:52 +00:00
2021-05-06 18:24:01 +00:00
entity_view view = {0};
2021-05-06 16:26:52 +00:00
pkt_unpack_struct(&uc, pkt_entity_view_desc, PKT_STRUCT_PTR(&view));
2021-05-06 16:26:52 +00:00
return view;
}
2021-05-06 15:30:38 +00:00
void entity_view_init(entity_view_tbl *map) {
entity_view_tbl_init(map, zpl_heap());
2021-05-05 09:25:05 +00:00
}
2021-05-06 15:30:38 +00:00
void entity_view_free(entity_view_tbl *map) {
entity_view_tbl_destroy(map);
2021-05-05 09:25:05 +00:00
}
2021-05-06 15:30:38 +00:00
void entity_view_update_or_create(entity_view_tbl *map, uint64_t ent_id, entity_view data) {
entity_view_tbl_set(map, ent_id, data);
2021-05-05 09:25:05 +00:00
}
2021-05-06 15:30:38 +00:00
void entity_view_destroy(entity_view_tbl *map, uint64_t ent_id) {
2021-07-18 18:30:27 +00:00
entity_view_remove_chunk_texture(map, ent_id);
2021-05-06 15:30:38 +00:00
entity_view_tbl_remove(map, ent_id);
2021-05-05 09:25:05 +00:00
}
2021-05-06 15:30:38 +00:00
entity_view *entity_view_get(entity_view_tbl *map, uint64_t ent_id) {
return entity_view_tbl_get(map, ent_id);
2021-05-05 09:25:05 +00:00
}
2021-05-09 22:40:25 +00:00
void entity_view_map(entity_view_tbl *map, void (*map_proc)(uint64_t key, entity_view * value)) {
entity_view_tbl_map_mut(map, map_proc);
}
void entity_view_mark_for_removal(entity_view_tbl *map, uint64_t ent_id) {
entity_view *view = entity_view_tbl_get(map, ent_id);
view->tran_effect = ETRAN_FADEOUT;
view->tran_time = 1.0f;
}
void entity_view_mark_for_fadein(entity_view_tbl *map, uint64_t ent_id) {
entity_view *view = entity_view_tbl_get(map, ent_id);
view->tran_effect = ETRAN_FADEIN;
view->tran_time = 0.0f;
2021-05-05 09:25:05 +00:00
}
2021-07-18 18:30:27 +00:00
void entity_view_update_chunk_texture(entity_view_tbl *map, uint64_t ent_id, void *world_view) {
entity_view *view = entity_view_tbl_get(map, ent_id);
if (view->kind != EKIND_CHUNK) return;
2021-11-02 11:49:03 +00:00
blocks_build_chunk_tex(ent_id, view->blocks, world_view);
2021-07-18 18:30:27 +00:00
}
void entity_view_remove_chunk_texture(entity_view_tbl *map, uint64_t ent_id) {
entity_view *view = entity_view_tbl_get(map, ent_id);
if (view->kind != EKIND_CHUNK) return;
2021-09-20 09:27:47 +00:00
blocks_remove_chunk_tex(ent_id);
}