dropped component caching

This was fixed in flecs already
master
Dominik Madarász 2023-02-13 10:31:08 +01:00
parent e302d1c865
commit a9898527ee
17 changed files with 42 additions and 90 deletions

View File

@ -2,20 +2,12 @@
#include "flecs.h"
#include "models/assets.h"
#define ecs_get_mut_ex(world, entity, T) \
(ECS_CAST(T*, ecs_get_mut(world, entity, T)))
#define ecs_get_if(world, entity, T) \
(world_entity_valid(entity) ? ecs_get(world, entity, T) : NULL)
#define ecs_get_mut_if_ex(world, entity, component) \
#define ecs_get_mut_if(world, entity, component) \
(ecs_get_if(world, entity, component) ? ecs_get_mut(world, entity, component) : NULL)
#ifndef ecs_get_mut_if
#define ecs_get_mut_if(world, entity, component)\
(ecs_get(world, entity, component) ? ecs_get_mut(world, entity, component) : NULL)
#endif
#define ITEMS_INVENTORY_SIZE 9
#define ITEMS_CONTAINER_SIZE 16

View File

@ -71,7 +71,7 @@ void entity_despawn(uint64_t ent_id) {
void entity_set_position(uint64_t ent_id, float x, float y) {
ecs_set(world_ecs(), ent_id, Position, {x, y});
Position *p = ecs_get_mut_ex(world_ecs(), ent_id, Position);
Position *p = ecs_get_mut(world_ecs(), ent_id, Position);
p->x = x;
p->y = y;
librg_entity_chunk_set(world_tracker(), ent_id, librg_chunk_from_realpos(world_tracker(), x, y, 0));
@ -79,7 +79,7 @@ void entity_set_position(uint64_t ent_id, float x, float y) {
}
void entity_wake(uint64_t ent_id) {
StreamInfo *si = ecs_get_mut_ex(world_ecs(), ent_id, StreamInfo);
StreamInfo *si = ecs_get_mut(world_ecs(), ent_id, StreamInfo);
si->tick_delay = 0.0f;
si->last_update = 0.0f;
}
@ -110,6 +110,6 @@ void entity_update_action_timers() {
}
bool entity_can_stream(uint64_t ent_id) {
StreamInfo *si = ecs_get_mut_ex(world_ecs(), ent_id, StreamInfo);
StreamInfo *si = ecs_get_mut(world_ecs(), ent_id, StreamInfo);
return (si->last_update < get_cached_time());
}

View File

@ -81,7 +81,7 @@ item_id item_find_no_proxy(asset_id kind) {
Item *item_get_data(uint64_t ent) {
if (!world_entity_valid(ent)) return NULL;
return ecs_get_mut_if_ex(world_ecs(), ent, Item);
return ecs_get_mut_if(world_ecs(), ent, Item);
}
const Item *item_get_data_const(uint64_t ent) {

View File

@ -68,7 +68,7 @@ uint64_t craftbench_spawn(void) {
uint64_t creature_spawn(void) {
ecs_entity_t e = entity_spawn(EKIND_DEMO_NPC);
Creature *c = ecs_get_mut_ex(world_ecs(), e, Creature);
Creature *c = ecs_get_mut(world_ecs(), e, Creature);
c->hunger_satisfied = 0;
c->mating_satisfied = rand() % 1800;
c->life_remaining = 500 + rand() % 5200;
@ -121,7 +121,7 @@ uint64_t storage_spawn(void) {
uint64_t mob_spawn(void) {
ecs_entity_t e = entity_spawn(EKIND_MONSTER);
Health *hp = ecs_get_mut_ex(world_ecs(), e, Health);
Health *hp = ecs_get_mut(world_ecs(), e, Health);
hp->max_hp = hp->hp = 100.0f;
ecs_add(world_ecs(), e, Mob);

View File

@ -64,11 +64,11 @@ void CreatureSeekFood(ecs_iter_t *it) {
for (size_t j = 0; j < ents_count; j++) {
Item *drop = 0;
uint64_t ent_id = ents[j];
if ((drop = ecs_get_mut_if_ex(it->world, ent_id, Item))) {
if ((drop = ecs_get_mut_if(it->world, ent_id, Item))) {
if (drop->kind != ASSET_CREATURE_FOOD)
continue;
p2 = ecs_get_mut_ex(it->world, ent_id, Position);
p2 = ecs_get_mut(it->world, ent_id, Position);
float dx = p2->x - p[i].x;
float dy = p2->y - p[i].y;
float range = (dx*dx + dy*dy);
@ -120,7 +120,7 @@ void CreatureSeekCompanion(ecs_iter_t *it) {
for (size_t j = 0; j < ents_count; j++) {
uint64_t ent_id = ents[j];
if (ent_id != it->entities[i] && ecs_get_if(it->world, ent_id, SeeksCompanion)) {
p2 = ecs_get_mut_ex(it->world, ent_id, Position);
p2 = ecs_get_mut(it->world, ent_id, Position);
float dx = p2->x - p[i].x;
float dy = p2->y - p[i].y;
float range = (dx*dx + dy*dy);

View File

@ -46,7 +46,7 @@ void ProcessHealthDamage(ecs_iter_t *it) {
void OnDead(ecs_iter_t *it) {
for (int i = 0; i < it->count; i++) {
const ClientInfo *ci = ecs_get(it->world, it->entities[i], ClientInfo);
Input *pi = ecs_get_mut_if_ex(it->world, it->entities[i], Input);
Input *pi = ecs_get_mut_if(it->world, it->entities[i], Input);
if (ci) {
pkt_notification_send(0, 0, "Someone died!", zpl_bprintf("Player %d has died!", it->entities[i]));

View File

@ -13,9 +13,9 @@ void PickItem(ecs_iter_t *it) {
for (size_t j = 0; j < ents_count; j++) {
Item *drop = 0;
uint64_t ent_id = ents[j];
if ((drop = ecs_get_mut_if_ex(it->world, ent_id, Item))) {
Position *p2 = ecs_get_mut_ex(it->world, ent_id, Position);
Velocity *v2 = ecs_get_mut_ex(it->world, ent_id, Velocity);
if ((drop = ecs_get_mut_if(it->world, ent_id, Item))) {
Position *p2 = ecs_get_mut(it->world, ent_id, Position);
Velocity *v2 = ecs_get_mut(it->world, ent_id, Velocity);
float dx = p2->x - p[i].x;
float dy = p2->y - p[i].y;
@ -63,7 +63,7 @@ void CraftItem(ecs_iter_t *it) {
if (in[i].craft_item == 0) continue;
if (world_entity_valid(in[i].storage_ent)){
Producer *ic = 0;
if ((ic = ecs_get_mut_if_ex(it->world, in[i].storage_ent, Producer))){
if ((ic = ecs_get_mut_if(it->world, in[i].storage_ent, Producer))){
ic->target_item = in[i].craft_item;
if (ic->pending_task == PRODUCER_CRAFT_WAITING) {
ic->pending_task = PRODUCER_CRAFT_ENQUEUED;
@ -87,7 +87,7 @@ void DropItem(ecs_iter_t *it) {
if (in[i].storage_action){
if (world_entity_valid(in[i].storage_ent)){
ItemContainer *ic = 0;
if ((ic = ecs_get_mut_if_ex(it->world, in[i].storage_ent, ItemContainer))){
if ((ic = ecs_get_mut_if(it->world, in[i].storage_ent, ItemContainer))){
items = ic->items;
}else{
continue;
@ -116,10 +116,10 @@ void DropItem(ecs_iter_t *it) {
item_show(item_slot_ent, true);
Position *ipos = ecs_get_mut_ex(it->world, item_slot_ent, Position);
Position *ipos = ecs_get_mut(it->world, item_slot_ent, Position);
entity_set_position(item_slot_ent, p[i].x, p[i].y);
Velocity *v = ecs_get_mut_ex(it->world, item_slot_ent, Velocity);
Velocity *v = ecs_get_mut(it->world, item_slot_ent, Velocity);
v->x = in[i].mx * 800.0f;
v->y = in[i].my * 800.0f;
@ -151,7 +151,7 @@ void DropItem(ecs_iter_t *it) {
// for (size_t j = 0; j < ents_count; j++) {
// ItemDrop *drop = 0;
// if ((drop = ecs_get_mut_if_ex(it->world, ents[j], ItemDrop))) {
// if ((drop = ecs_get_mut_if(it->world, ents[j], ItemDrop))) {
// if (drop->kind != item->kind || (ecs_entity_t)ents[j] == it->entities[i] || drop->quantity == 0 || item->quantity == 0)
// continue;
@ -182,7 +182,7 @@ void SwapItems(ecs_iter_t *it) {
if (in[i].storage_action){
if (world_entity_valid(in[i].storage_ent)){
ItemContainer *ic = 0;
if ((ic = ecs_get_mut_if_ex(it->world, in[i].storage_ent, ItemContainer))){
if ((ic = ecs_get_mut_if(it->world, in[i].storage_ent, ItemContainer))){
items = ic->items;
}else{
continue;
@ -208,7 +208,7 @@ void SwapItems(ecs_iter_t *it) {
}else{
if (world_entity_valid(in[i].storage_ent)){
ItemContainer *ic = 0;
if ((ic = ecs_get_mut_if_ex(it->world, in[i].storage_ent, ItemContainer))){
if ((ic = ecs_get_mut_if(it->world, in[i].storage_ent, ItemContainer))){
from_ent = &ic->items[in[i].swap_from];
from = item_get_data(*from_ent);
}else{
@ -363,8 +363,8 @@ void HarvestIntoContainers(ecs_iter_t *it) {
for (size_t j = 0; j < ents_count; j++) {
Item *drop = 0;
if ((drop = ecs_get_mut_if_ex(it->world, ents[j], Item))) {
Position *p2 = ecs_get_mut_ex(it->world, ents[j], Position);
if ((drop = ecs_get_mut_if(it->world, ents[j], Item))) {
Position *p2 = ecs_get_mut(it->world, ents[j], Position);
uint64_t ent_id = ents[j];
float dx = p2->x - p[i].x;

View File

@ -65,7 +65,7 @@ void PushItemsOnNodes(ecs_iter_t *it) {
// We need a way to refer to specific blocks in the world so we can do easy block ID checks
// and re-build the cache when a change is detected.
Producer *producer = ecs_get_mut_if_ex(it->world, it->entities[i], Producer);
Producer *producer = ecs_get_mut_if(it->world, it->entities[i], Producer);
if (producer) {
if (producer->push_filter == PRODUCER_PUSH_NONE)
@ -113,7 +113,7 @@ void PushItemsOnNodes(ecs_iter_t *it) {
uint64_t e = item_spawn(item->kind, zpl_min(r->push_qty, item->quantity));
entity_set_position(e, p[i].x + push_dx[r[i].counter], p[i].y + push_dy[r[i].counter]);
Velocity *e_vel = ecs_get_mut_ex(it->world, e, Velocity);
Velocity *e_vel = ecs_get_mut(it->world, e, Velocity);
e_vel->x = push_dx[r[i].counter];
e_vel->y = push_dy[r[i].counter];

View File

@ -10,7 +10,7 @@ void LeaveVehicle(ecs_iter_t *it) {
if (!in[i].use) continue;
Vehicle *veh = 0;
if ((veh = ecs_get_mut_if_ex(it->world, vehp->veh, Vehicle))) {
if ((veh = ecs_get_mut_if(it->world, vehp->veh, Vehicle))) {
for (int k = 0; k < 4; k++) {
if (veh->seats[k] == it->entities[i]) {
veh->seats[k] = 0;
@ -50,9 +50,9 @@ void EnterVehicle(ecs_iter_t *it) {
if (has_entered_veh) break;
veh = ecs_get_mut_if_ex(it->world, ents[j], Vehicle);
veh = ecs_get_mut_if(it->world, ents[j], Vehicle);
if ((veh = ecs_get_mut_if_ex(it->world, ents[j], Vehicle))) {
if ((veh = ecs_get_mut_if(it->world, ents[j], Vehicle))) {
Position const* p2 = ecs_get(it->world, ents[j], Position);
float dx = p2->x - p[i].x;
@ -153,7 +153,7 @@ void VehicleHandling(ecs_iter_t *it) {
// NOTE(zaklaus): Update passenger position
{
Velocity *v2 = ecs_get_mut_ex(it->world, pe, Velocity);
Velocity *v2 = ecs_get_mut(it->world, pe, Velocity);
entity_set_position(pe, p[i].x, p[i].y);
*v2 = v[i];
}

View File

@ -98,7 +98,7 @@ entity_view* world_build_entity_view(int64_t e) {
view.inside_vehicle = ecs_get(world_ecs(), e, IsInVehicle) != 0 ? true : false;
Inventory* inv = 0;
if ((inv = ecs_get_mut_if_ex(world_ecs(), e, Inventory))) {
if ((inv = ecs_get_mut_if(world_ecs(), e, Inventory))) {
view.has_items = true;
for (int i = 0; i < ITEMS_INVENTORY_SIZE; i += 1) {
@ -114,7 +114,7 @@ entity_view* world_build_entity_view(int64_t e) {
if (world_entity_valid(in->storage_ent)) {
ItemContainer* ic = 0;
if ((ic = ecs_get_mut_if_ex(world_ecs(), in->storage_ent, ItemContainer))) {
if ((ic = ecs_get_mut_if(world_ecs(), in->storage_ent, ItemContainer))) {
view.has_storage_items = true;
for (int i = 0; i < ITEMS_CONTAINER_SIZE; i += 1) {
@ -733,23 +733,3 @@ bool world_entity_valid(ecs_entity_t e) {
if (!e) return false;
return ecs_is_alive(world_ecs(), e);
}
void* world_component_cached(ecs_world_t* world_ecs, ecs_entity_t entity, ecs_id_t id) {
if (!component_cache.entries || world.ecs_stage == NULL) {
return ecs_get_mut_id(world_ecs, entity, id);
}
static char buffer[256] = { 0 };
zpl_snprintf(buffer, 256, "%llu_%llu", entity, id);
uint64_t uid = zpl_crc64(buffer, zpl_strlen(buffer));
zpl_uintptr* value = world_component_cache_get(&component_cache, uid);
if (!value) {
void* the_value = ecs_get_mut_id(world_ecs, entity, id);
world_component_cache_set(&component_cache, uid, (zpl_uintptr)the_value);
value = world_component_cache_get(&component_cache, uid);
}
return (void*)*value;
}

View File

@ -126,4 +126,3 @@ int64_t *world_chunk_fetch_entities_realpos(float x, float y, size_t *ents_len);
int64_t *world_chunk_query_entities(int64_t e, size_t *ents_len, int8_t radius);
bool world_entity_valid(ecs_entity_t e);
void *world_component_cached(ecs_world_t *world, ecs_entity_t entity, ecs_id_t id);

View File

@ -15,36 +15,17 @@
#include "world/worldgen_utils.h"
block_id worldgen_biome_find(uint32_t biome, uint32_t kind) {
asset_id asset = ASSET_INVALID;
switch (biome) {
case BLOCK_BIOME_DEV: {
switch (kind) {
case BLOCK_KIND_GROUND: asset = ASSET_GROUND; break;
case BLOCK_KIND_DIRT: asset = ASSET_DIRT; break;
case BLOCK_KIND_WALL: asset = ASSET_WALL; break;
case BLOCK_KIND_HILL_SNOW:
case BLOCK_KIND_HILL: asset = ASSET_HILL; break;
case BLOCK_KIND_WATER: asset = ASSET_WATER; break;
case BLOCK_KIND_LAVA: asset = ASSET_LAVA; break;
}
}
}
return blocks_find(asset);
}
int32_t worldgen_build(world_data *wld) {
// TODO(zaklaus): pass world as an arg instead
world = wld;
// TODO: perform world gen
// atm, we will fill the world with ground and surround it by walls
block_id wall_id = worldgen_biome_find(BLOCK_BIOME_DEV, BLOCK_KIND_WALL);
block_id grnd_id = worldgen_biome_find(BLOCK_BIOME_DEV, BLOCK_KIND_GROUND);
block_id dirt_id = worldgen_biome_find(BLOCK_BIOME_DEV, BLOCK_KIND_DIRT);
block_id watr_id = worldgen_biome_find(BLOCK_BIOME_DEV, BLOCK_KIND_WATER);
block_id lava_id = worldgen_biome_find(BLOCK_BIOME_DEV, BLOCK_KIND_LAVA);
block_id wall_id = blocks_find(ASSET_WALL);
block_id grnd_id = blocks_find(ASSET_GROUND);
block_id dirt_id = blocks_find(ASSET_DIRT);
block_id watr_id = blocks_find(ASSET_WATER);
block_id lava_id = blocks_find(ASSET_LAVA);
block_id tree_id = blocks_find(ASSET_TREE);
srand(world->seed);
@ -63,6 +44,5 @@ int32_t worldgen_build(world_data *wld) {
// narrow boy cirlce
world_fill_circle(world->data, grnd_id, world->dim / 2, world->dim / 2, (uint32_t)(radius * 0.7f), NULL);
return WORLD_ERROR_NONE;
}

View File

@ -18,7 +18,7 @@ void mob_systems(ecs_world_t *ecs) {
ECS_SYSTEM_TICKED_EX(ecs, MobDetectPlayers, EcsPostUpdate, 100.0f, components.Position, components.Mob);
ECS_SYSTEM(ecs, MobMovement, EcsPostUpdate, components.Velocity, components.Position, components.MobHuntPlayer);
ECS_SYSTEM_TICKED(ecs, MobMeleeAtk, EcsPostUpdate, components.Position, components.Mob, components.MobHuntPlayer, components.MobMelee);
//NOTE(DavoSK): weapons
ecs_mobpos_query = ecs_query_new(world_ecs(), "components.Mob, components.Position, components.Health, components.Velocity");
ECS_SYSTEM_TICKED(ecs, WeaponKnifeMechanic, EcsPostUpdate, components.WeaponKnife, components.Position, components.Input, !components.Dead);

View File

@ -23,7 +23,7 @@ ZPL_DIAGNOSTIC_POP
#define DEFAULT_WORLD_SEED 302097
#define DEFAULT_CHUNK_SIZE 16 /* amount of blocks within a chunk (single axis) */
#define DEFAULT_WORLD_SIZE 5 /* amount of chunks within a world (single axis) */
#define DEFAULT_WORLD_SIZE 20 /* amount of chunks within a world (single axis) */
/*
TODO

View File

@ -74,7 +74,7 @@ void MobMeleeAtk(ecs_iter_t *it) {
float range = (dx*dx + dy*dy);
if (range < MOB_MELEE_DIST) {
Health *health = ecs_get_mut_ex(it->world, m->plr, Health);
Health *health = ecs_get_mut(it->world, m->plr, Health);
health->dmg += MOB_MELEE_DMG;
mob[i].atk_delay = MOB_ATK_DELAY;
}

View File

@ -55,7 +55,7 @@ int32_t worldgen_build(world_data *wld) {
// ground
world_fill_rect(world->data, wall_id, 1, 1, world->dim-2, world->dim-2, NULL);
int radius = 30;
int radius = 90;
// wide boy circle
world_fill_circle(world->data, dirt_id, world->dim / 2, world->dim / 2, radius, NULL);
@ -63,7 +63,7 @@ int32_t worldgen_build(world_data *wld) {
// narrow boy cirlce
world_fill_circle(world->data, grnd_id, world->dim / 2, world->dim / 2, (uint32_t)(radius * 0.7f), NULL);
world_fill_circle(world->data, wall_id, world->dim / 2 + 9, world->dim / 2+ 9, (uint32_t)(radius * 0.2f), NULL);
world_fill_circle(world->data, wall_id, world->dim / 2 + radius/3, world->dim / 2 + radius/3, (uint32_t)(radius * 0.2f), NULL);
return WORLD_ERROR_NONE;
}

View File

@ -5,6 +5,7 @@
<ExcludeFilter>*.obj,*.lib,*.pch,*.dll,*.pdb,.vs,Debug,Release,x64,obj,*.user,Intermediate,</ExcludeFilter>
<SyncFiles>true</SyncFiles>
<Recursive>true</Recursive>
<ShowEmptyFolders>true</ShowEmptyFolders>
<IsVirtual>false</IsVirtual>
<IsFolder>false</IsFolder>
<BuildCommand>build.bat</BuildCommand>