ItemRouter system
parent
6385ea4461
commit
4cb85f069f
|
@ -18,6 +18,7 @@ ECS_COMPONENT_DECLARE(ItemContainer);
|
||||||
ECS_COMPONENT_DECLARE(Producer);
|
ECS_COMPONENT_DECLARE(Producer);
|
||||||
ECS_COMPONENT_DECLARE(EnergySource);
|
ECS_COMPONENT_DECLARE(EnergySource);
|
||||||
ECS_COMPONENT_DECLARE(Ingredient);
|
ECS_COMPONENT_DECLARE(Ingredient);
|
||||||
|
ECS_COMPONENT_DECLARE(ItemRouter);
|
||||||
ECS_COMPONENT_DECLARE(Device);
|
ECS_COMPONENT_DECLARE(Device);
|
||||||
ECS_COMPONENT_DECLARE(Blueprint);
|
ECS_COMPONENT_DECLARE(Blueprint);
|
||||||
ECS_COMPONENT_DECLARE(DemoNPC);
|
ECS_COMPONENT_DECLARE(DemoNPC);
|
||||||
|
@ -44,6 +45,7 @@ void ComponentsImport(ecs_world_t *ecs) {
|
||||||
ECS_COMPONENT_DEFINE(ecs, Producer);
|
ECS_COMPONENT_DEFINE(ecs, Producer);
|
||||||
ECS_COMPONENT_DEFINE(ecs, EnergySource);
|
ECS_COMPONENT_DEFINE(ecs, EnergySource);
|
||||||
ECS_COMPONENT_DEFINE(ecs, Ingredient);
|
ECS_COMPONENT_DEFINE(ecs, Ingredient);
|
||||||
|
ECS_COMPONENT_DEFINE(ecs, ItemRouter);
|
||||||
ECS_COMPONENT_DEFINE(ecs, Device);
|
ECS_COMPONENT_DEFINE(ecs, Device);
|
||||||
ECS_COMPONENT_DEFINE(ecs, Blueprint);
|
ECS_COMPONENT_DEFINE(ecs, Blueprint);
|
||||||
ECS_COMPONENT_DEFINE(ecs, DemoNPC);
|
ECS_COMPONENT_DEFINE(ecs, DemoNPC);
|
||||||
|
|
|
@ -135,6 +135,10 @@ typedef struct {
|
||||||
float energy_level;
|
float energy_level;
|
||||||
} Producer;
|
} Producer;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char _unused;
|
||||||
|
} ItemRouter;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
asset_id kind;
|
asset_id kind;
|
||||||
float energy_level;
|
float energy_level;
|
||||||
|
@ -185,6 +189,7 @@ extern ECS_COMPONENT_DECLARE(ItemContainer);
|
||||||
extern ECS_COMPONENT_DECLARE(Producer);
|
extern ECS_COMPONENT_DECLARE(Producer);
|
||||||
extern ECS_COMPONENT_DECLARE(EnergySource);
|
extern ECS_COMPONENT_DECLARE(EnergySource);
|
||||||
extern ECS_COMPONENT_DECLARE(Ingredient);
|
extern ECS_COMPONENT_DECLARE(Ingredient);
|
||||||
|
extern ECS_COMPONENT_DECLARE(ItemRouter);
|
||||||
extern ECS_COMPONENT_DECLARE(Device);
|
extern ECS_COMPONENT_DECLARE(Device);
|
||||||
extern ECS_COMPONENT_DECLARE(Blueprint);
|
extern ECS_COMPONENT_DECLARE(Blueprint);
|
||||||
extern ECS_COMPONENT_DECLARE(DemoNPC);
|
extern ECS_COMPONENT_DECLARE(DemoNPC);
|
||||||
|
|
|
@ -14,6 +14,8 @@ uint64_t furnace_spawn(void) {
|
||||||
Producer *producer = ecs_get_mut(world_ecs(), e, Producer);
|
Producer *producer = ecs_get_mut(world_ecs(), e, Producer);
|
||||||
*producer = (Producer){0};
|
*producer = (Producer){0};
|
||||||
producer->energy_level = 69.0f;
|
producer->energy_level = 69.0f;
|
||||||
|
|
||||||
|
ecs_add(world_ecs(), e, ItemRouter);
|
||||||
return (uint64_t)e;
|
return (uint64_t)e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,110 @@
|
||||||
|
static inline
|
||||||
|
asset_id FetchAssetAtPos(float x, float y) {
|
||||||
|
world_block_lookup lookup = world_block_from_realpos(x, y);
|
||||||
|
if (lookup.is_outer) {
|
||||||
|
return blocks_get_asset(lookup.bid);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ASSET_INVALID;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// NOTE(zaklaus): Expects float[4] dx, dy
|
||||||
|
static inline
|
||||||
|
uint8_t CheckForNearbyBelts(Position *p, float *dx, float *dy) {
|
||||||
|
asset_id bid = ASSET_INVALID;
|
||||||
|
float o = WORLD_BLOCK_SIZE;
|
||||||
|
uint8_t nodes = 0x0;
|
||||||
|
|
||||||
|
// up
|
||||||
|
bid = FetchAssetAtPos(p->x + 0, p->y - o);
|
||||||
|
if (bid == ASSET_BELT_UP) {
|
||||||
|
dx[0] = 0;
|
||||||
|
dy[0] = -o;
|
||||||
|
nodes |= ZPL_BIT(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// down
|
||||||
|
bid = FetchAssetAtPos(p->x + 0, p->y + o);
|
||||||
|
if (bid == ASSET_BELT_DOWN) {
|
||||||
|
dx[1] = 0;
|
||||||
|
dy[1] = o;
|
||||||
|
nodes |= ZPL_BIT(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// left
|
||||||
|
bid = FetchAssetAtPos(p->x - o, p->y + 0);
|
||||||
|
if (bid == ASSET_BELT_LEFT) {
|
||||||
|
dx[2] = -o;
|
||||||
|
dy[2] = 0;
|
||||||
|
nodes |= ZPL_BIT(2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// right
|
||||||
|
bid = FetchAssetAtPos(p->x + o, p->y + 0);
|
||||||
|
if (bid == ASSET_BELT_RIGHT) {
|
||||||
|
dx[3] = o;
|
||||||
|
dy[3] = 0;
|
||||||
|
nodes |= ZPL_BIT(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
void PushItemsOnNodes(ecs_iter_t *it) {
|
||||||
|
ItemContainer *storage = ecs_field(it, ItemContainer, 1);
|
||||||
|
Position *p = ecs_field(it, Position, 2);
|
||||||
|
Device *d = ecs_field(it, Device, 3);
|
||||||
|
ItemRouter *r = ecs_field(it, ItemRouter, 4);
|
||||||
|
|
||||||
|
for (int i = 0; i < it->count; i++) {
|
||||||
|
// TODO(zaklaus): Cache output nodes so we avoid
|
||||||
|
// calling world_block_from_realpos each time.
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
float push_dx[4], push_dy[4];
|
||||||
|
uint8_t nodes = CheckForNearbyBelts(&p[i], push_dx, push_dy);
|
||||||
|
uint8_t num_nodes = (uint8_t)zpl_count_set_bits(nodes);
|
||||||
|
uint8_t counter = 0;
|
||||||
|
|
||||||
|
if (num_nodes == 0) {
|
||||||
|
// NOTE(zaklaus): We don't have any output nodes yet.
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int j = 0; j < ITEMS_CONTAINER_SIZE; j++) {
|
||||||
|
ecs_entity_t item_slot_ent = storage[i].items[j];
|
||||||
|
if (item_slot_ent == 0) continue;
|
||||||
|
Item *item = item_get_data(item_slot_ent);
|
||||||
|
|
||||||
|
const Ingredient *ing = 0;
|
||||||
|
// NOTE(zaklaus): Make sure we don't push out items from input node
|
||||||
|
if ((ing = ecs_get_if(it->world, item_slot_ent, Ingredient))) {
|
||||||
|
if (ing->producer == d->asset) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (item->quantity > 0 && num_nodes > 0) {
|
||||||
|
// NOTE(zaklaus): Use a rolling counter to select an output node.
|
||||||
|
while (!(nodes & (1 << counter)) && counter < 4) ++counter;
|
||||||
|
if (counter > 3) {
|
||||||
|
counter = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t e = item_spawn(item->kind, 1);
|
||||||
|
entity_set_position(e, p[i].x + push_dx[counter], p[i].y + push_dy[counter]);
|
||||||
|
|
||||||
|
Velocity *e_vel = ecs_get_mut_ex(it->world, e, Velocity);
|
||||||
|
e_vel->x = push_dx[counter];
|
||||||
|
e_vel->y = push_dy[counter];
|
||||||
|
|
||||||
|
--item->quantity;
|
||||||
|
--num_nodes;
|
||||||
|
++counter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,58 +1,3 @@
|
||||||
static inline
|
|
||||||
asset_id FetchAssetAtPos(float x, float y) {
|
|
||||||
world_block_lookup lookup = world_block_from_realpos(x, y);
|
|
||||||
if (lookup.is_outer) {
|
|
||||||
return blocks_get_asset(lookup.bid);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ASSET_INVALID;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline
|
|
||||||
bool CheckForNearbyBelt(Position *p, float *dx, float *dy) {
|
|
||||||
asset_id bid = ASSET_INVALID;
|
|
||||||
float o = WORLD_BLOCK_SIZE;
|
|
||||||
|
|
||||||
// up
|
|
||||||
bid = FetchAssetAtPos(p->x + 0, p->y - o);
|
|
||||||
{
|
|
||||||
debug_v2 a = {p->x, p->y};
|
|
||||||
debug_v2 b = {p->x, p->y-WORLD_BLOCK_SIZE};
|
|
||||||
debug_push_line(a, b, 0xFFFFFFFF);
|
|
||||||
}
|
|
||||||
if (bid == ASSET_BELT_UP) {
|
|
||||||
*dx = 0;
|
|
||||||
*dy = -o;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// down
|
|
||||||
bid = FetchAssetAtPos(p->x + 0, p->y + o);
|
|
||||||
if (bid == ASSET_BELT_DOWN) {
|
|
||||||
*dx = 0;
|
|
||||||
*dy = o;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// left
|
|
||||||
bid = FetchAssetAtPos(p->x - o, p->y + 0);
|
|
||||||
if (bid == ASSET_BELT_LEFT) {
|
|
||||||
*dx = -o;
|
|
||||||
*dy = 0;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// right
|
|
||||||
bid = FetchAssetAtPos(p->x + o, p->y + 0);
|
|
||||||
if (bid == ASSET_BELT_RIGHT) {
|
|
||||||
*dx = o;
|
|
||||||
*dy = 0;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ProduceItems(ecs_iter_t *it) {
|
void ProduceItems(ecs_iter_t *it) {
|
||||||
ItemContainer *storage = ecs_field(it, ItemContainer, 1);
|
ItemContainer *storage = ecs_field(it, ItemContainer, 1);
|
||||||
Producer *producer = ecs_field(it, Producer, 2);
|
Producer *producer = ecs_field(it, Producer, 2);
|
||||||
|
@ -60,9 +5,6 @@ void ProduceItems(ecs_iter_t *it) {
|
||||||
Device *d = ecs_field(it, Device, 4);
|
Device *d = ecs_field(it, Device, 4);
|
||||||
|
|
||||||
for (int i = 0; i < it->count; i++) {
|
for (int i = 0; i < it->count; i++) {
|
||||||
float push_dx=0.0f, push_dy=0.0f;
|
|
||||||
bool has_output_node = CheckForNearbyBelt(&p[i], &push_dx, &push_dy);
|
|
||||||
|
|
||||||
for (int j = 0; j < ITEMS_CONTAINER_SIZE; j++) {
|
for (int j = 0; j < ITEMS_CONTAINER_SIZE; j++) {
|
||||||
ecs_entity_t item_slot_ent = storage[i].items[j];
|
ecs_entity_t item_slot_ent = storage[i].items[j];
|
||||||
Item *item = item_get_data(item_slot_ent);
|
Item *item = item_get_data(item_slot_ent);
|
||||||
|
@ -81,16 +23,8 @@ void ProduceItems(ecs_iter_t *it) {
|
||||||
if (producer[i].process_time < game_time()) {
|
if (producer[i].process_time < game_time()) {
|
||||||
if (producer[i].processed_item > 0) {
|
if (producer[i].processed_item > 0) {
|
||||||
uint64_t e = item_spawn(producer[i].processed_item, 1);
|
uint64_t e = item_spawn(producer[i].processed_item, 1);
|
||||||
producer[i].processed_item = 0;
|
|
||||||
|
|
||||||
if (has_output_node) {
|
|
||||||
entity_set_position(e, p[i].x+push_dx, p[i].y+push_dy);
|
|
||||||
Velocity *e_vel = ecs_get_mut_ex(it->world, e, Velocity);
|
|
||||||
e_vel->x = push_dx;
|
|
||||||
e_vel->y = push_dy;
|
|
||||||
} else {
|
|
||||||
entity_set_position(e, p[i].x, p[i].y);
|
entity_set_position(e, p[i].x, p[i].y);
|
||||||
}
|
producer[i].processed_item = 0;
|
||||||
} else {
|
} else {
|
||||||
const Ingredient *ing = 0;
|
const Ingredient *ing = 0;
|
||||||
if ((ing = ecs_get_if(it->world, item_slot_ent, Ingredient))) {
|
if ((ing = ecs_get_if(it->world, item_slot_ent, Ingredient))) {
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
#include "modules/system_demo.c"
|
#include "modules/system_demo.c"
|
||||||
#include "modules/system_vehicle.c"
|
#include "modules/system_vehicle.c"
|
||||||
#include "modules/system_items.c"
|
#include "modules/system_items.c"
|
||||||
|
#include "modules/system_logistics.c"
|
||||||
#include "modules/system_producer.c"
|
#include "modules/system_producer.c"
|
||||||
#include "modules/system_blueprint.c"
|
#include "modules/system_blueprint.c"
|
||||||
|
|
||||||
|
@ -232,6 +233,11 @@ void DisableWorldEdit(ecs_iter_t *it) {
|
||||||
ECS_SYSTEM(world, id, stage, __VA_ARGS__);\
|
ECS_SYSTEM(world, id, stage, __VA_ARGS__);\
|
||||||
ecs_set_tick_source(world, id, timer);
|
ecs_set_tick_source(world, id, timer);
|
||||||
|
|
||||||
|
#define ECS_SYSTEM_TICKED_EX(world, id, stage, time, ...)\
|
||||||
|
ECS_SYSTEM(world, id, stage, __VA_ARGS__);\
|
||||||
|
ecs_entity_t timer_##id = ecs_set_interval(ecs, 0, ECO2D_TICK_RATE*time);\
|
||||||
|
ecs_set_tick_source(world, id, timer_##id);
|
||||||
|
|
||||||
void SystemsImport(ecs_world_t *ecs) {
|
void SystemsImport(ecs_world_t *ecs) {
|
||||||
ECS_MODULE(ecs, Systems);
|
ECS_MODULE(ecs, Systems);
|
||||||
|
|
||||||
|
@ -261,6 +267,7 @@ void SystemsImport(ecs_world_t *ecs) {
|
||||||
|
|
||||||
ECS_SYSTEM_TICKED(ecs, HarvestIntoContainers, EcsPostUpdate, components.ItemContainer, components.Position, !components.BlockHarvest);
|
ECS_SYSTEM_TICKED(ecs, HarvestIntoContainers, EcsPostUpdate, components.ItemContainer, components.Position, !components.BlockHarvest);
|
||||||
ECS_SYSTEM_TICKED(ecs, ProduceItems, EcsPostUpdate, components.ItemContainer, components.Producer, components.Position, components.Device);
|
ECS_SYSTEM_TICKED(ecs, ProduceItems, EcsPostUpdate, components.ItemContainer, components.Producer, components.Position, components.Device);
|
||||||
|
ECS_SYSTEM_TICKED_EX(ecs, PushItemsOnNodes, EcsPostUpdate, 20, components.ItemContainer, components.Position, components.Device, components.ItemRouter);
|
||||||
ECS_SYSTEM_TICKED(ecs, BuildBlueprints, EcsPostUpdate, components.Blueprint, components.Device, components.Position);
|
ECS_SYSTEM_TICKED(ecs, BuildBlueprints, EcsPostUpdate, components.Blueprint, components.Device, components.Position);
|
||||||
|
|
||||||
ECS_SYSTEM(ecs, ResetActivators, EcsPostUpdate, components.Input);
|
ECS_SYSTEM(ecs, ResetActivators, EcsPostUpdate, components.Input);
|
||||||
|
|
Loading…
Reference in New Issue