From 2155be63d154d7d613c6227cc0376144fa254c03 Mon Sep 17 00:00:00 2001 From: venomade Date: Thu, 27 Feb 2025 17:30:36 +0000 Subject: Add experimental branch --- src/game.c | 104 ++++++++++++++++++++++++++++--------------------------------- 1 file changed, 47 insertions(+), 57 deletions(-) (limited to 'src/game.c') diff --git a/src/game.c b/src/game.c index 9714cde..2059d32 100644 --- a/src/game.c +++ b/src/game.c @@ -76,25 +76,11 @@ Dir random_dir(void) { int is_cell_empty(const Game *game, Coord pos) { - //if (game->agents_map[pos.y][pos.x]) { - // return 0; - //} - - for (size_t i = 0; i < AGENTS_COUNT; ++i) { - if (coord_equals(game->agents[i].pos, pos)) { - return 0; - } + if (game->env_map[pos.y][pos.x]>=ENV_MAP_AGENTS) { + return game->agents[game->env_map[pos.y][pos.x]-ENV_MAP_AGENTS].health<=0; } - if (game->foods_map[pos.y][pos.x]) { - return 0; - } - - if (game->walls_map[pos.y][pos.x]) { - return 0; - } - - return 1; + return game->env_map[pos.y][pos.x]==ENV_MAP_NONE; } Coord random_coord_on_board(void) { @@ -121,7 +107,9 @@ void init_game(Game *game) { memset(game, 0, sizeof(*game)); for (size_t i = 0; i < AGENTS_COUNT; ++i) { - game->agents[i].pos = random_empty_coord_on_board(game); + Coord pos = random_empty_coord_on_board(game); + game->env_map[pos.y][pos.x] = ENV_MAP_AGENTS + i; + game->agents[i].pos = pos; game->agents[i].dir = random_dir(); game->agents[i].hunger = HUNGER_MAX; game->agents[i].health = HEALTH_MAX; @@ -139,12 +127,12 @@ void init_game(Game *game) { for (size_t i = 0; i < FOODS_COUNT; ++i) { Coord pos = random_empty_coord_on_board(game); - game->foods_map[pos.y][pos.x] = 1; + game->env_map[pos.y][pos.x] = ENV_MAP_FOOD; } for (size_t i = 0; i < WALLS_COUNT; ++i) { Coord pos = random_empty_coord_on_board(game); - game->walls_map[pos.y][pos.x] = 1; + game->env_map[pos.y][pos.x] = ENV_MAP_WALL; } } @@ -166,8 +154,8 @@ int *food_infront_of_agent(Game *game, size_t agent_index) { Coord infront = coord_infront_of_agent(&game->agents[agent_index]); - if (game->foods_map[infront.y][infront.x]) { - return &game->foods_map[infront.y][infront.x]; + if (game->env_map[infront.y][infront.x] == ENV_MAP_FOOD) { + return &game->env_map[infront.y][infront.x]; } return NULL; @@ -176,23 +164,23 @@ int *food_infront_of_agent(Game *game, size_t agent_index) { Agent *agent_infront_of_agent(Game *game, size_t agent_index) { Coord infront = coord_infront_of_agent(&game->agents[agent_index]); + int env = game->env_map[infront.y][infront.x]; //if (game->agents_map[infront.y][infront.x]) { // size_t infront_index = game->agents_map[infront.y][infront.x] - 1; - // // TODO: when agent dies, remove from agents_map + // // when agent dies, remove from agents_map // // Then this can be removed // if (game->agents[infront_index].health > 0) { // return &game->agents[infront_index]; // } //} - for (size_t i = 0; i < AGENTS_COUNT; ++i) { - if (i != agent_index && - game->agents[i].health > 0 && - coord_equals(infront, game->agents[i].pos)) - { - return &game->agents[i]; - } + if(env >= ENV_MAP_AGENTS) { + int infront_agent_index = env - ENV_MAP_AGENTS; + if(infront_agent_index != agent_index && + game->agents[infront_agent_index].health>0) { + return &game->agents[infront_agent_index]; + } } return NULL; @@ -202,8 +190,8 @@ int *wall_infront_of_agent(Game *game, size_t agent_index) { Coord infront = coord_infront_of_agent(&game->agents[agent_index]); - if (game->walls_map[infront.y][infront.x]) { - return &game->walls_map[infront.y][infront.x]; + if (game->env_map[infront.y][infront.x] == ENV_MAP_WALL) { + return &game->env_map[infront.y][infront.x]; } return NULL; @@ -226,38 +214,40 @@ Env env_infront_of_agent(Game *game, size_t agent_index) { return ENV_NOTHING; } -void step_agent(Agent *agent) { - Coord d = coord_dirs[agent->dir]; - agent->pos.x = mod_int(agent->pos.x + d.x, BOARD_WIDTH); - agent->pos.y = mod_int(agent->pos.y + d.y, BOARD_HEIGHT); +void step_agent(Game *game, size_t agent_index) { + Agent *agent = &game->agents[agent_index]; + Coord new_pos = coord_infront_of_agent(agent); + game->env_map[agent->pos.y][agent->pos.x] = ENV_MAP_NONE; // set prev tile to empty + agent->pos.x = new_pos.x; + agent->pos.y = new_pos.y; + game->env_map[agent->pos.y][agent->pos.x] = ENV_MAP_AGENTS + agent_index; // register infront tile to agent } -// TODO: void step_agent(Game *game, size_t agent_index) {} -// Update Agent Map - void execute_action(Game *game, size_t agent_index, Action action) { switch (action) { case ACTION_NOP: { } break; case ACTION_STEP: { - int *food = food_infront_of_agent(game, agent_index); - Agent *other_agent = agent_infront_of_agent(game, agent_index); + Coord infront = coord_infront_of_agent(&game->agents[agent_index]); + int *env = &game->env_map[infront.y][infront.x]; + Agent *other_agent = agent_at(game, infront); - if (food != NULL) { - *food = 0; + if (*env == ENV_MAP_FOOD) { + *env = ENV_MAP_NONE; // set food tile to empty game->agents[agent_index].hunger += FOOD_HUNGER_RECOVERY; if (game->agents[agent_index].hunger > HUNGER_MAX) { game->agents[agent_index].hunger = HUNGER_MAX; } - step_agent(&game->agents[agent_index]); } else if (other_agent != NULL) { other_agent->health -= ATTACK_DAMAGE; if (other_agent->health <= 0){ + *env = ENV_MAP_NONE; // set other agent tile to empty; game->agents[agent_index].hunger += FOOD_HUNGER_RECOVERY; - step_agent(&game->agents[agent_index]); } - } else if (wall_infront_of_agent(game, agent_index) != NULL) { - step_agent(&game->agents[agent_index]); + + if (*env == ENV_MAP_NONE) { + step_agent(game, agent_index); + } } } break; case ACTION_TURN_LEFT: { @@ -268,8 +258,8 @@ void execute_action(Game *game, size_t agent_index, Action action) { game->agents[agent_index].dir = mod_int(game->agents[agent_index].dir - 1, 4); } break; - case ACTION_COUNT: { - assert(0 && "UNREACHABLE ACTION_COUNT"); + default: { + assert(0 && "UNREACHABLE DEFAULT ACTION"); } break; } } @@ -300,12 +290,10 @@ void step_game(Game *game) { } Agent *agent_at(Game *game, Coord pos) { - for (size_t i = 0; i < AGENTS_COUNT; ++i) { - if (coord_equals(game->agents[i].pos, pos)) { - return &game->agents[i]; - } + int env = game->env_map[pos.y][pos.x]; + if(env >= ENV_MAP_AGENTS) { + return &game->agents[env]; } - return NULL; } @@ -354,12 +342,12 @@ void make_next_generation(Game *prev_game, Game *next_game){ for (size_t i = 0; i < FOODS_COUNT; ++i) { Coord pos = random_empty_coord_on_board(next_game); - next_game->foods_map[pos.x][pos.y] = 1; + next_game->env_map[pos.x][pos.y] = ENV_MAP_FOOD; } for (size_t i = 0; i < WALLS_COUNT; ++i) { Coord pos = random_empty_coord_on_board(next_game); - next_game->walls_map[pos.x][pos.y] = 1; + next_game->env_map[pos.x][pos.y] = ENV_MAP_WALL; } for (size_t i = 0; i < AGENTS_COUNT; ++i) { @@ -372,7 +360,9 @@ void make_next_generation(Game *prev_game, Game *next_game){ mutate_agent(&next_game->agents[i]); // Work out agent positioning on new game - next_game->agents[i].pos = random_empty_coord_on_board(next_game); + Coord pos = random_empty_coord_on_board(next_game); + next_game->env_map[pos.y][pos.x] = ENV_MAP_AGENTS + i; // register tile for agent[i] + next_game->agents[i].pos = pos; next_game->agents[i].dir = random_dir(); next_game->agents[i].hunger = HUNGER_MAX; next_game->agents[i].health = HEALTH_MAX; -- cgit 1.4.1-2-gfad0