From 68b57468a4670924e7544f2cfa7e9457c808bc29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Mon, 8 Aug 2022 13:19:12 +0200 Subject: [PATCH] ecs: fix demo npc wake --- code/game/src/entity_view.h | 14 ++-- code/game/src/renderer_v0.c | 1 - code/game/src/world/blocks.c | 2 +- code/game/src/world/world.c | 108 +++++++++++++++--------------- code/modules/source/system_demo.c | 2 + 5 files changed, 64 insertions(+), 63 deletions(-) diff --git a/code/game/src/entity_view.h b/code/game/src/entity_view.h index b8e4d8f..85c3620 100644 --- a/code/game/src/entity_view.h +++ b/code/game/src/entity_view.h @@ -41,10 +41,10 @@ typedef struct entity_view { float vy; float tx; float ty; - + float hp; float max_hp; - + // TODO(zaklaus): Find a way to stream dynamic arrays uint8_t blocks_used; block_id blocks[256]; @@ -52,24 +52,24 @@ typedef struct entity_view { uint32_t color; uint8_t is_dirty; int64_t tex; - + // NOTE(zaklaus): vehicle float heading, theading; bool inside_vehicle; - + // NOTE(zaklaus): items, ... asset_id asset; uint32_t quantity; - + // NOTE(zaklaus): inventory uint8_t has_items; ItemDrop items[ITEMS_INVENTORY_SIZE]; uint8_t selected_item; - + // NOTE(zaklaus): internals uint8_t layer_id; uint64_t last_update; - + // NOTE(zaklaus): fade in-out effect entity_transition_effect tran_effect; float tran_time; diff --git a/code/game/src/renderer_v0.c b/code/game/src/renderer_v0.c index 65f0a2b..c5403ed 100644 --- a/code/game/src/renderer_v0.c +++ b/code/game/src/renderer_v0.c @@ -8,7 +8,6 @@ float zpl_lerp(float,float,float); float zpl_to_degrees(float); void DEBUG_draw_ground(uint64_t key, entity_view * data) { - (void)key; switch (data->kind) { case EKIND_CHUNK: { world_view *view = game_world_view_get_active(); diff --git a/code/game/src/world/blocks.c b/code/game/src/world/blocks.c index 488fe5d..fc64654 100644 --- a/code/game/src/world/blocks.c +++ b/code/game/src/world/blocks.c @@ -106,7 +106,7 @@ void blocks_build_chunk_tex(uint64_t id, block_id *chunk_blocks, void *raw_view) float rot = rots[(int32_t)(perlin_fbm(view->seed, x, y, 1.2f, 3) * 4.0f) % 4]; float half_block = blk_dims / 2.0f; Texture2D blk = *(Texture2D*)blocks_get_img(chunk_blocks[(y*view->chunk_size)+x]); - Rectangle src = {0, 0, WORLD_BLOCK_SIZE, WORLD_BLOCK_SIZE}; + static Rectangle src = {0, 0, WORLD_BLOCK_SIZE, WORLD_BLOCK_SIZE}; Rectangle dst = {x*blk_dims + half_block, y*blk_dims + half_block, blk_dims, blk_dims}; DrawTexturePro(blk, src, dst, (Vector2){half_block, half_block}, rot, WHITE); } diff --git a/code/game/src/world/world.c b/code/game/src/world/world.c index 9d9432b..3602bef 100644 --- a/code/game/src/world/world.c +++ b/code/game/src/world/world.c @@ -22,59 +22,59 @@ static world_snapshot streamer_snapshot; entity_view world_build_entity_view(int64_t e) { entity_view *cached_ev = world_snapshot_get(&streamer_snapshot, e); if (cached_ev) return *cached_ev; - + entity_view view = {0}; - + const Classify *classify = ecs_get(world_ecs(), e, Classify); ZPL_ASSERT(classify); - + view.kind = classify->id; - + const Position *pos = ecs_get(world_ecs(), e, Position); if (pos) { view.x = pos->x; view.y = pos->y; } - + const Velocity *vel = ecs_get(world_ecs(), e, Velocity); if (vel) { view.flag |= EFLAG_INTERP; view.vx = vel->x; view.vy = vel->y; } - + const Health *health = ecs_get(world_ecs(), e, Health); if (health) { view.hp = health->hp; view.max_hp = health->max_hp; } - + if (ecs_get(world_ecs(), e, Vehicle)) { Vehicle const* veh = ecs_get(world_ecs(), e, Vehicle); view.heading = veh->heading; } - + if (ecs_get(world_ecs(), e, ItemDrop)) { ItemDrop const* dr = ecs_get(world_ecs(), e, ItemDrop); view.asset = dr->kind; view.quantity = dr->quantity; } - + view.inside_vehicle = ecs_get(world_ecs(), e, IsInVehicle) != 0 ? true : false; - + Inventory *inv = 0; if ((inv = ecs_get_mut_if(world_ecs(), e, Inventory))) { view.has_items = true; - + for (int i = 0; i < ITEMS_INVENTORY_SIZE; i += 1) { view.items[i] = inv->items[i]; } - + const Input *in = ecs_get(world_ecs(), e, Input); if (in) view.selected_item = in->selected_item; } - + Chunk *chunk = 0; if ((chunk = ecs_get_mut_if(world_ecs(), e, Chunk))) { view.x = chunk->x; @@ -82,16 +82,16 @@ entity_view world_build_entity_view(int64_t e) { view.blocks_used = 1; view.is_dirty = chunk->is_dirty; chunk->is_dirty = false; - + for (int i = 0; i < world.chunk_size*world.chunk_size; i += 1) { view.blocks[i] = world.block_mapping[chunk->id][i]; } - + for (int i = 0; i < world.chunk_size*world.chunk_size; i += 1) { view.outer_blocks[i] = world.outer_block_mapping[chunk->id][i]; } } - + world_snapshot_set(&streamer_snapshot, e, view); return view; } @@ -106,7 +106,7 @@ int32_t tracker_write_create(librg_world *w, librg_event *e) { #endif size_t actual_length = librg_event_size_get(w, e); char *buffer = librg_event_buffer_get(w, e); - + return (int32_t)entity_view_pack_struct(buffer, actual_length, world_build_entity_view(entity_id)); } @@ -127,21 +127,21 @@ int32_t tracker_write_update(librg_world *w, librg_event *e) { size_t actual_length = librg_event_size_get(w, e); char *buffer = librg_event_buffer_get(w, e); entity_view view = world_build_entity_view(entity_id); - + // NOTE(zaklaus): exclude chunks from updates as they never move { if (view.kind == EKIND_CHUNK && !view.is_dirty) { return LIBRG_WRITE_REJECT; } } - + // NOTE(zaklaus): action-based updates { if (view.kind != EKIND_CHUNK && !entity_can_stream(entity_id)) { return LIBRG_WRITE_REJECT; } } - + return (int32_t)entity_view_pack_struct(buffer, actual_length, view); } @@ -164,15 +164,15 @@ void world_chunk_setup_grid(void) { world.outer_block_mapping[i] = zpl_malloc(sizeof(block_id)*zpl_square(world.chunk_size)); chunk->id = i; chunk->is_dirty = false; - + for (int y = 0; y < world.chunk_size; y += 1) { for (int x = 0; x < world.chunk_size; x += 1) { int chk_x = chunk->x * world.chunk_size; int chk_y = chunk->y * world.chunk_size; - + block_id *c = &world.block_mapping[i][(y*world.chunk_size)+x]; *c = world.data[(chk_y+y)*world.dim + (chk_x+x)]; - + c = &world.outer_block_mapping[i][(y*world.chunk_size)+x]; *c = world.outer_data[(chk_y+y)*world.dim + (chk_x+x)]; } @@ -183,14 +183,14 @@ void world_chunk_setup_grid(void) { static inline void world_configure_tracker(void) { world.tracker = librg_world_create(); - + ZPL_ASSERT_MSG(world.tracker, "[ERROR] An error occurred while trying to create a server world."); - + /* config our world grid */ librg_config_chunksize_set(world.tracker, WORLD_BLOCK_SIZE * world.chunk_size, WORLD_BLOCK_SIZE * world.chunk_size, 0); librg_config_chunkamount_set(world.tracker, world.chunk_amount, world.chunk_amount, 0); librg_config_chunkoffset_set(world.tracker, LIBRG_OFFSET_BEG, LIBRG_OFFSET_BEG, LIBRG_OFFSET_BEG); - + librg_event_set(world.tracker, LIBRG_WRITE_CREATE, tracker_write_create); librg_event_set(world.tracker, LIBRG_WRITE_REMOVE, tracker_write_remove); librg_event_set(world.tracker, LIBRG_WRITE_UPDATE, tracker_write_update); @@ -200,14 +200,14 @@ static inline void world_init_worldgen_data(void) { world.data = zpl_malloc(sizeof(block_id)*world.size); world.outer_data = zpl_malloc(sizeof(block_id)*world.size); - + ZPL_ASSERT(world.data && world.outer_data); } static inline void world_setup_ecs(void) { world.ecs = ecs_init(); - + ECS_IMPORT(world.ecs, Components); ECS_IMPORT(world.ecs, Systems); world.ecs_update = ecs_query_new(world.ecs, "components.ClientInfo, components.Position"); @@ -226,7 +226,7 @@ static inline void world_generate_instance(void) { int32_t world_build_status = worldgen_test(&world); ZPL_ASSERT(world_build_status >= 0); - + for (int i = 0; i < zpl_square(world.dim); ++i) { if (world.data[i] == 0) { ZPL_PANIC("Worldgen failure! Block %d is unset!\n", i); @@ -248,10 +248,10 @@ int32_t world_init(int32_t seed, uint16_t chunk_size, uint16_t chunk_amount) { world.seed = seed; world.chunk_size = chunk_size; world.chunk_amount = chunk_amount; - + world.dim = (world.chunk_size * world.chunk_amount); world.size = world.dim * world.dim; - + world_configure_tracker(); world_setup_ecs(); world_init_worldgen_data(); @@ -259,9 +259,9 @@ int32_t world_init(int32_t seed, uint16_t chunk_size, uint16_t chunk_amount) { world_init_mapping(); world_chunk_setup_grid(); world_free_worldgen_data(); - + zpl_printf("[INFO] Created a new server world\n"); - + return WORLD_ERROR_NONE; } @@ -286,33 +286,33 @@ int32_t world_destroy(void) { static void world_tracker_update(uint8_t ticker, float freq, uint8_t radius) { if (world.tracker_update[ticker] > (float)zpl_time_rel()) return; world.tracker_update[ticker] = (float)zpl_time_rel() + freq; - + profile(PROF_WORLD_WRITE) { ecs_iter_t it = ecs_query_iter(world_ecs(), world.ecs_update); static char buffer[WORLD_LIBRG_BUFSIZ] = {0}; world.active_layer_id = ticker; - + while (ecs_query_next(&it)) { ClientInfo *p = ecs_field(&it, ClientInfo, 1); - + for (int i = 0; i < it.count; i++) { size_t datalen = WORLD_LIBRG_BUFSIZ; - + if (!p[i].active) continue; - + int32_t result = librg_world_write(world_tracker(), it.entities[i], radius, buffer, &datalen, NULL); - + if (result > 0) { zpl_printf("[info] buffer size was not enough, please increase it by at least: %d\n", result); } else if (result < 0) { zpl_printf("[error] an error happened writing the world %d\n", result); } - + pkt_send_librg_update((uint64_t)p[i].peer, p[i].view_id, ticker, buffer, datalen); } } - + // NOTE(zaklaus): clear out our streaming snapshot // TODO(zaklaus): move this to zpl { @@ -326,21 +326,21 @@ int32_t world_update() { profile (PROF_UPDATE_SYSTEMS) { ecs_progress(world.ecs, 0.0f); } - + float fast_ms = WORLD_TRACKER_UPDATE_FAST_MS; float normal_ms = WORLD_TRACKER_UPDATE_NORMAL_MS; float slow_ms = WORLD_TRACKER_UPDATE_SLOW_MS; - + if (game_get_kind() != GAMEKIND_SINGLE) { fast_ms = WORLD_TRACKER_UPDATE_MP_FAST_MS; normal_ms = WORLD_TRACKER_UPDATE_MP_NORMAL_MS; slow_ms = WORLD_TRACKER_UPDATE_MP_SLOW_MS; } - + world_tracker_update(0, fast_ms, 1); world_tracker_update(1, normal_ms, 2); world_tracker_update(2, slow_ms, 3); - + entity_update_action_timers(); debug_replay_update(); return 0; @@ -436,11 +436,11 @@ world_block_lookup world_block_from_realpos(float x, float y) { int32_t size = world.chunk_size * WORLD_BLOCK_SIZE; int16_t chunk_x, chunk_y; librg_chunk_to_chunkpos(world.tracker, chunk_id, &chunk_x, &chunk_y, NULL); - + // NOTE(zaklaus): pos relative to chunk float chx = x - chunk_x * size; float chy = y - chunk_y * size; - + uint16_t bx = (uint16_t)chx / WORLD_BLOCK_SIZE; uint16_t by = (uint16_t)chy / WORLD_BLOCK_SIZE; uint16_t block_idx = (by*world.chunk_size)+bx; @@ -450,11 +450,11 @@ world_block_lookup world_block_from_realpos(float x, float y) { bid = world.block_mapping[chunk_id][block_idx]; is_outer = false; } - + // NOTE(zaklaus): pos relative to block's center float box = chx - bx * WORLD_BLOCK_SIZE - WORLD_BLOCK_SIZE/2.0f; float boy = chy - by * WORLD_BLOCK_SIZE - WORLD_BLOCK_SIZE/2.0f; - + world_block_lookup lookup = { .id = block_idx, .bid = bid, @@ -464,19 +464,19 @@ world_block_lookup world_block_from_realpos(float x, float y) { .oy = boy, .is_outer = is_outer, }; - + return lookup; } void world_chunk_destroy_block(float x, float y, bool drop_item) { world_block_lookup l = world_block_from_realpos(x, y); world_chunk_replace_block(l.chunk_id, l.id, 0); - + if (l.is_outer && l.bid > 0 && drop_item) { asset_id item_asset = blocks_get_asset(l.bid); if (item_find(item_asset) == ASSET_INVALID) return; uint64_t e = item_spawn(item_asset, 1); - + Position *dest = ecs_get_mut(world_ecs(), e, Position); dest->x = x; dest->y = y; @@ -488,14 +488,14 @@ world_block_lookup world_block_from_index(int64_t id, uint16_t block_idx) { if (bid == 0) { bid = world.block_mapping[id][block_idx]; } - + world_block_lookup lookup = { .id = block_idx, .bid = bid, .chunk_id = id, .chunk_e = world.chunk_mapping[id], }; - + return lookup; } diff --git a/code/modules/source/system_demo.c b/code/modules/source/system_demo.c index bee32a1..43468e1 100644 --- a/code/modules/source/system_demo.c +++ b/code/modules/source/system_demo.c @@ -10,5 +10,7 @@ void DemoNPCMoveAround(ecs_iter_t *it) { float d = zpl_quake_rsqrt(v[i].x*v[i].x + v[i].y*v[i].y); v[i].x += (v[i].x*d*DEMO_NPC_MOVE_SPEED*safe_dt(it) + zpl_cos(zpl_to_radians((float)(rand()%360)))*DEMO_NPC_STEER_SPEED*safe_dt(it)); v[i].y += (v[i].y*d*DEMO_NPC_MOVE_SPEED*safe_dt(it) + zpl_sin(zpl_to_radians((float)(rand()%360)))*DEMO_NPC_STEER_SPEED*safe_dt(it)); + + entity_wake(it->entities[i]); } }