From 9d6993d7b1a50c6ac42888854e4e058ff3dde814 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Wed, 3 Nov 2021 16:29:45 +0100 Subject: [PATCH] improve build mode --- code/game/src/debug_ui_actions.c | 13 +------------ code/game/src/items.c | 7 +++++-- code/game/src/world/world.c | 23 ++++++++++++++++++++++- code/game/src/world/world.h | 6 ++++++ 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/code/game/src/debug_ui_actions.c b/code/game/src/debug_ui_actions.c index 5ca7af8..7b5a8dd 100644 --- a/code/game/src/debug_ui_actions.c +++ b/code/game/src/debug_ui_actions.c @@ -80,18 +80,7 @@ ActEraseWorldChanges(void) { for (int y = 0; y < 100; y++) { for (int x = 0; x < 100; x++) { - world_block_lookup l = world_block_from_realpos((p->x - (x*bs)/2.0f), p->y - (y*bs)/2.0f); - world_chunk_place_block(l.chunk_id, l.id, 0); - - if (l.is_outer && l.block_id > 0) { - asset_id item_asset = blocks_get_asset(l.block_id); - if (item_find(item_asset) == ASSET_INVALID) continue; - uint64_t e = item_spawn(item_asset, 1); - - Position *dest = ecs_get_mut(world_ecs(), e, Position, NULL); - dest->x = (p->x - (x*bs)/2.0f); - dest->y = (p->y - (y*bs)/2.0f); - } + world_chunk_destroy_block((p->x - (x*bs)/2.0f), (p->y - (y*bs)/2.0f), true); } } diff --git a/code/game/src/items.c b/code/game/src/items.c index 1c44485..e54e88b 100644 --- a/code/game/src/items.c +++ b/code/game/src/items.c @@ -54,8 +54,11 @@ void item_use(ecs_world_t *ecs, ItemDrop *it, Position p, uint64_t udata) { case UKIND_HOLD: /* NOOP */ break; case UKIND_PLACE:{ world_block_lookup l = world_block_from_realpos(p.x, p.y); - if (world_chunk_place_block(l.chunk_id, l.id, blocks_find(desc->place.kind + (asset_id)udata)) ) - it->quantity--; + if (l.block_id > 0) { + world_chunk_destroy_block(p.x, p.y, true); + } + world_chunk_replace_block(l.chunk_id, l.id, blocks_find(desc->place.kind + (asset_id)udata)); + it->quantity--; }break; } } diff --git a/code/game/src/world/world.c b/code/game/src/world/world.c index 693457a..0c99f37 100644 --- a/code/game/src/world/world.c +++ b/code/game/src/world/world.c @@ -430,6 +430,21 @@ world_block_lookup world_block_from_realpos(float x, float y) { 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.block_id > 0 && drop_item) { + asset_id item_asset = blocks_get_asset(l.block_id); + 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, NULL); + dest->x = x; + dest->y = y; + } +} + world_block_lookup world_block_from_index(int64_t id, uint16_t block_idx) { uint8_t block_id = world.outer_block_mapping[id][block_idx]; if (block_id == 0) { @@ -455,12 +470,18 @@ int64_t world_chunk_from_entity(ecs_entity_t id) { return librg_entity_chunk_get(world.tracker, id); } -void world_chunk_replace_block(int64_t id, uint16_t block_idx, uint8_t block_id) { +void world_chunk_replace_worldgen_block(int64_t id, uint16_t block_idx, uint8_t block_id) { ZPL_ASSERT(block_idx >= 0 && block_idx < zpl_square(world.chunk_size)); world.block_mapping[id][block_idx] = block_id; world_chunk_mark_dirty(world.chunk_mapping[id]); } +void world_chunk_replace_block(int64_t id, uint16_t block_idx, uint8_t block_id) { + ZPL_ASSERT(block_idx >= 0 && block_idx < zpl_square(world.chunk_size)); + world.outer_block_mapping[id][block_idx] = block_id; + world_chunk_mark_dirty(world.chunk_mapping[id]); +} + bool world_chunk_place_block(int64_t id, uint16_t block_idx, uint8_t block_id) { ZPL_ASSERT(block_idx >= 0 && block_idx < zpl_square(world.chunk_size)); if (world.outer_block_mapping[id][block_idx] != 0 && block_id != 0) return false; diff --git a/code/game/src/world/world.h b/code/game/src/world/world.h index eacac25..7afbb1d 100644 --- a/code/game/src/world/world.h +++ b/code/game/src/world/world.h @@ -89,8 +89,14 @@ world_block_lookup world_block_from_realpos(float x, float y); world_block_lookup world_block_from_index(int64_t id, uint16_t block_idx); int64_t world_chunk_from_realpos(float x, float y); int64_t world_chunk_from_entity(ecs_entity_t id); + +void world_chunk_replace_worldgen_block(int64_t id, uint16_t block_idx, uint8_t block_id); + void world_chunk_replace_block(int64_t id, uint16_t block_idx, uint8_t block_id); bool world_chunk_place_block(int64_t id, uint16_t block_idx, uint8_t block_id); + +void world_chunk_destroy_block(float x, float y, bool drop_item); + uint8_t *world_chunk_get_blocks(int64_t id); void world_chunk_mark_dirty(ecs_entity_t e); uint8_t world_chunk_is_dirty(ecs_entity_t e);