eco2d/code/apps/client/source/world_view.c

95 lines
3.6 KiB
C
Raw Normal View History

2021-05-09 14:41:19 +00:00
#include "zpl.h"
2021-05-06 15:30:38 +00:00
#include "world_view.h"
#include "entity_view.h"
#include "prediction.h"
2021-05-06 15:30:38 +00:00
#include "librg.h"
2021-05-09 14:41:19 +00:00
#include "world/world.h"
2021-05-06 15:30:38 +00:00
int32_t tracker_read_remove(librg_world *w, librg_event *e) {
int64_t owner_id = librg_event_owner_get(w, e);
int64_t entity_id = librg_event_entity_get(w, e);
zpl_printf("[INFO] An entity %d was removed for owner: %d\n", (int)entity_id, (int)owner_id);
world_view *view = (world_view*)librg_world_userdata_get(w);
entity_view_destroy(&view->entities, entity_id);
return 0;
}
int32_t tracker_read_update(librg_world *w, librg_event *e) {
2021-05-06 16:26:52 +00:00
int64_t entity_id = librg_event_entity_get(w, e);
2021-05-06 15:30:38 +00:00
size_t actual_length = librg_event_size_get(w, e);
char *buffer = librg_event_buffer_get(w, e);
2021-05-06 18:24:01 +00:00
world_view *view = (world_view*)librg_world_userdata_get(w);
2021-05-06 15:30:38 +00:00
2021-05-06 18:24:01 +00:00
entity_view data = entity_view_unpack_struct(buffer, actual_length);
entity_view *d = entity_view_get(&view->entities, entity_id);
2021-05-09 14:41:19 +00:00
#if 1
if (d && d->layer_id < view->active_layer_id) {
if (zpl_time_rel_ms() - d->last_update > WORLD_TRACKER_UPDATE_NORMAL_MS) {
d->layer_id = zpl_min(WORLD_TRACKER_LAYERS-1, d->layer_id+1);
}
// NOTE(zaklaus): reject updates from slower layers
2021-05-09 14:41:19 +00:00
else return 0;
}
2021-05-09 14:41:19 +00:00
#endif
data.last_update = zpl_time_rel_ms();
data.layer_id = view->active_layer_id;
predict_receive_update(d, &data);
2021-05-06 18:24:01 +00:00
entity_view_update_or_create(&view->entities, entity_id, data);
return 0;
}
int32_t tracker_read_create(librg_world *w, librg_event *e) {
int64_t owner_id = librg_event_owner_get(w, e);
int64_t entity_id = librg_event_entity_get(w, e);
zpl_printf("[INFO] An entity %d was created for owner: %d\n", (int)entity_id, (int)owner_id);
size_t actual_length = librg_event_size_get(w, e);
char *buffer = librg_event_buffer_get(w, e);
world_view *view = (world_view*)librg_world_userdata_get(w);
2021-05-09 14:41:19 +00:00
#ifdef WORLD_LAYERING
if (view->active_layer_id != WORLD_TRACKER_LAYERS-1) {
// NOTE(zaklaus): reject updates from smaller layers
return 0;
}
#endif
2021-05-06 18:24:01 +00:00
entity_view data = entity_view_unpack_struct(buffer, actual_length);
data.layer_id = view->active_layer_id;
if (data.flag & EFLAG_INTERP) {
data.tx = data.x;
data.ty = data.y;
}
2021-05-06 18:24:01 +00:00
entity_view_update_or_create(&view->entities, entity_id, data);
2021-05-06 15:30:38 +00:00
return 0;
}
2021-05-07 14:43:54 +00:00
world_view world_view_create(uint16_t view_id) {
2021-05-06 15:30:38 +00:00
world_view view = {0};
2021-05-07 14:43:54 +00:00
view.view_id = view_id;
2021-05-06 15:30:38 +00:00
view.tracker = librg_world_create();
entity_view_init(&view.entities);
return view;
}
2021-05-08 06:55:12 +00:00
void world_view_init(world_view *view, uint64_t ent_id, uint16_t block_size, uint16_t chunk_size, uint16_t chunk_amount) {
2021-05-06 15:30:38 +00:00
view->owner_id = ent_id;
2021-05-08 07:07:24 +00:00
view->block_size = block_size;
2021-05-06 18:24:01 +00:00
view->chunk_size = chunk_size;
2021-05-08 06:55:12 +00:00
view->chunk_amount = chunk_amount;
2021-05-08 07:07:24 +00:00
view->dim = block_size * chunk_size * chunk_amount;
view->size = view->dim * view->dim;
2021-05-06 18:24:01 +00:00
2021-05-06 15:30:38 +00:00
librg_config_chunksize_set(view->tracker, block_size * chunk_size, block_size * chunk_size, 1);
2021-05-08 06:55:12 +00:00
librg_config_chunkamount_set(view->tracker, chunk_amount, chunk_amount, 1);
2021-05-08 20:32:31 +00:00
librg_config_chunkoffset_set(view->tracker, LIBRG_OFFSET_BEG, LIBRG_OFFSET_BEG, 0);
2021-05-06 15:30:38 +00:00
librg_event_set(view->tracker, LIBRG_READ_CREATE, tracker_read_create);
librg_event_set(view->tracker, LIBRG_READ_REMOVE, tracker_read_remove);
librg_event_set(view->tracker, LIBRG_READ_UPDATE, tracker_read_update);
librg_world_userdata_set(view->tracker, view);
}
void world_view_destroy(world_view *view) {
librg_world_destroy(view->tracker);
entity_view_free(&view->entities);
}