Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[comps-as-state] added ability for systems to add and consume components #107

Merged
merged 1 commit into from
Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions src/systems/cecs_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,32 +106,3 @@ int cecs_rem_system(struct cecs* cecs, const char* name)
sys->init = NULL;
return cecserr(CECS_NOERR);
}

// TODO: remove _vile_ loops
int cecs_resolve_sys_deps(struct cecs* cecs)
{
struct cecs_system * const systems = cecs->systems;

for(int i = 0; i < cecs->num_systems; ++i) {
for(int j = 0; j < systems[i].dependNames.length; ++j) {
int index = -1;
for(int k = 0; k < cecs->num_systems; ++k){
if(strcmp(systems[i].dependNames.data[j],
cecs->systems[k].name) != 0){
continue;
}
if(cecs->systems[k].registered == true) {
index = k;
free(systems[i].dependNames.data[j]);
systems[i].dependNames.data[j] == NULL;
break;
}
else break;
}
if(index != -1) {
array_push(systems[i].dependIndices, index);
}
}
array_free(systems[i].dependNames);
}
}
11 changes: 2 additions & 9 deletions src/systems/cecs_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,10 @@ struct cecs_system
const char *name;
bool registered;

/*
* tracking systems that must be executed prior
* to this one. Once dependencies have been resolved, names
* will be cleared and indices stored instead to reduce memory overhead
* and enable quicker parsing.
*/
array(int) dependIndices;
array(char *)dependNames;
array(CECS_COMP_KEY) addKeys;
array(CECS_COMP_KEY) consumeKeys;
};

int cecs_reg_system(struct cecs* cecs, struct cecs_system* sys);
int cecs_rem_system(struct cecs* cecs, const char* name);
int cecs_resolve_sys_deps(struct cecs* cecs);
struct cecs_system* cecs_system(struct cecs *cecs, const char* name);
57 changes: 40 additions & 17 deletions src/systems/system_load.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "arr.h"
#include <yaml.h>
#include "cecs.h"
#include <yaml.h>
Expand All @@ -12,7 +13,7 @@
#include "cecs_component.h"
#include "cecs_component_key.h"

#define N_ELEMENTS 5
#define N_ELEMENTS 7
#define N_FUNCTIONS 3
#define SYS_NAME_MAX 32

Expand Down Expand Up @@ -61,16 +62,17 @@
static LIBTYPE sysfuncs = NULL;
static LIBTYPE usrfuncs = NULL;

static char *elements[6] = {
static char *elements[N_ELEMENTS] = {
"runtype",
"reads",
"writes",
"ignores",
"functions",
"depends"
"adds",
"consumes"
};

static char *functions[3] = {
static char *functions[N_FUNCTIONS] = {
"init",
"work",
"tidy"
Expand All @@ -87,8 +89,10 @@ enum parse_state{
sys = 0,
func,
func_m,
dep,
dep_m,
add,
add_m,
con,
con_m,
rt,
rt_m,
r,
Expand Down Expand Up @@ -148,7 +152,8 @@ static void set_scalar_state(const char* value, enum parse_state* s)
if(strcmp(value, elements[2]) == 0) { *s = w; }
if(strcmp(value, elements[3]) == 0) { *s = i; }
if(strcmp(value, elements[4]) == 0) { *s = func; }
if(strcmp(value, elements[5]) == 0) { *s = dep; }
if(strcmp(value, elements[5]) == 0) { *s = add; }
if(strcmp(value, elements[6]) == 0) { *s = con; }
}

static void set_func(struct cecs_system* system, int iwt, const char* value)
Expand Down Expand Up @@ -219,6 +224,7 @@ static void parse_scalar(const char* value, enum parse_state* s,
struct cecs_system* system, struct cecs *cecs)
{
static char* prevValue = NULL;
int dummy = 1; // dummy variable for component tagging
set_scalar_state(value, s);


Expand All @@ -238,8 +244,11 @@ static void parse_scalar(const char* value, enum parse_state* s,
case func: // functions
*s = func_m;
break;
case dep: // dependencies
*s = dep_m;
case add: // adds
*s = add_m;
break;
case con: // consumes
*s = con_m;
break;
case sys:
{
Expand All @@ -248,15 +257,29 @@ static void parse_scalar(const char* value, enum parse_state* s,
system->name = value;
break;
}
case dep_m:
case add_m:
{
char *ptr = NULL;
int i = system->dependNames.length;
ptr = strndup(value, SYS_NAME_MAX);
array_push(system->dependNames, ptr);

printf("%s depends on %s\n\n", system->name,
system->dependNames.data[i]);
// If the component hasn't been added, add tag component
if(cecs_component_key(cecs, value) == 0) {
cecs_reg_component(cecs, value, &dummy, sizeof(dummy));
}
array_push(system->addKeys, cecs_component_key(cecs, value));
printf("%s adds %s on completion\n", system->name,
value);
printf("\tkey is %d\n", cecs_component_key(cecs, value));
break;
}
case con_m:
{
// If the component hasn't been added, add tag component
if(cecs_component_key(cecs, value) == 0) {
cecs_reg_component(cecs, value, &dummy, sizeof(dummy));
}
array_push(system->consumeKeys,
cecs_component_key(cecs, value));
printf("%s consumes %s on completion\n", system->name,
value);
printf("\tkey is %d\n", cecs_component_key(cecs, value));
break;
}
case rt_m:
Expand Down
28 changes: 12 additions & 16 deletions src/tests/sys_yaml_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,27 +105,23 @@ START_TEST(pathing_runs_correct)
}
END_TEST

START_TEST(system_depends)
START_TEST(adds_and_consumes)
{
struct cecs_system* movement = cecs_system(cecs, "movement");
struct cecs_system* pathing = cecs_system(cecs, "pathing");

ck_assert_ptr_nonnull(movement->dependNames.data);
ck_assert_ptr_null(movement->dependIndices.data);
ck_assert_int_eq(strcmp(movement->dependNames.data[0], "pathing"), 0);
ck_assert_ptr_nonnull(movement->consumeKeys.data);
ck_assert_ptr_null(movement->addKeys.data);

cecs_resolve_sys_deps(cecs);
ck_assert_ptr_null(movement->dependNames.data);
ck_assert_ptr_nonnull(movement->dependIndices.data);
ck_assert_ptr_nonnull(pathing->addKeys.data);
ck_assert_ptr_null(pathing->consumeKeys.data);

// finding the itr
int itr = -1;
CECS_COMP_KEY mkey = movement->consumeKeys.data[0];
CECS_COMP_KEY pkey = pathing->addKeys.data[0];

for(int i = 0; i < cecs->num_systems; ++i) {
if(strcmp(cecs->systems[i].name, "pathing") == 0){
itr = i;
}
}
ck_assert_int_eq(movement->dependIndices.data[0], itr);
printf("movement_s key is %d\n", cecs_component_key(cecs, "movement_s"));
ck_assert_int_ne( (pkey & cecs_component_key(cecs, "movement_s")), 0);
ck_assert_int_ne( (mkey & cecs_component_key(cecs, "movement_s")), 0);
}
END_TEST

Expand All @@ -139,7 +135,7 @@ Suite * sys_yaml_suite(void)

tcase_add_test(yaml_load, systems_array_size);
tcase_add_test(yaml_load, systems_added);
tcase_add_test(yaml_load, system_depends);
tcase_add_test(yaml_load, adds_and_consumes);
tcase_add_test(yaml_load, movement_load_correct);
tcase_add_test(yaml_load, movement_runs_correct);
tcase_add_test(yaml_load, pathing_load_correct);
Expand Down
6 changes: 4 additions & 2 deletions systems.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
init: tsys_init
work: tsys_run
tidy: tsys_clean
depends:
- pathing # need to know where to move
consumes:
- movement_s
- pathing:
reads:
- position
Expand All @@ -23,3 +23,5 @@
init: path_init
work: path_run
tidy: path_clean
adds:
- movement_s