Created
March 26, 2025 11:01
-
-
Save MX-2000/ea0487c6bb0f798b25391191da264a7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void step_agent(CCpr *env, int i) { | |
int action = env->actions[i]; | |
int dr = 0; | |
int dc = 0; | |
switch (action) { | |
case 0: | |
dr = -1; | |
break; // UP | |
case 1: | |
dr = 1; | |
break; // DOWN | |
case 2: | |
dc = -1; | |
break; // LEFT | |
case 3: | |
dc = 1; | |
break; // RIGHT | |
} | |
if (action != 4) | |
env->logs[i].moves += 1; | |
// Get next row and column | |
Agent *agent = &env->agents[i]; | |
int next_r = agent->r + dr; | |
int next_c = agent->c + dc; | |
int prev_grid_idx = grid_index(env, agent->r, agent->c); | |
int next_grid_idx = env->width * next_r + next_c; | |
int tile = env->grid[next_grid_idx]; | |
// Anything above should be obstacle | |
if (tile >= INTERACTIVE_FOOD) { | |
env->logs[i].score += env->reward_move; | |
env->rewards[i] += env->reward_move; | |
next_r = agent->r; | |
next_c = agent->c; | |
next_grid_idx = env->width * next_r + next_c; | |
tile = env->grid[next_grid_idx]; | |
} | |
// Interactive food logic | |
int neighboors[4] = { | |
grid_index(env, next_r - 1, next_c), // Up | |
grid_index(env, next_r + 1, next_c), // Down | |
grid_index(env, next_r, next_c + 1), // Right | |
grid_index(env, next_r, next_c - 1) // Left | |
}; | |
for (int j = 0; j < 4; j++) { | |
int grid_idx = neighboors[j]; | |
// If neighbooring grid tile is interactive food | |
if (env->grid[grid_idx] == INTERACTIVE_FOOD) { | |
// If was already marked as "ready to collect" | |
if (CHECK_BIT(env->interactive_food_agent_count, grid_idx)) { | |
reward_agents_near(env, grid_idx); | |
} else { | |
// First agent detected | |
SET_BIT(env->interactive_food_agent_count, grid_idx); | |
} | |
} | |
} | |
switch (tile) { | |
case NORMAL_FOOD: | |
env->logs[i].score += env->reward_food; | |
env->rewards[i] = env->reward_food; | |
// spawn_food(env); | |
remove_food(env, next_grid_idx); | |
add_log(env->log_buffer, &env->logs[i]); | |
env->logs[i] = (Log){0}; | |
break; | |
case EMPTY: | |
env->logs[i].score += env->reward_move; | |
env->rewards[i] = env->reward_move; | |
break; | |
} | |
int agent_tile = get_agent_tile_from_id(agent->id); | |
env->grid[prev_grid_idx] = EMPTY; | |
env->grid[next_grid_idx] = agent_tile; | |
agent->r = next_r; | |
agent->c = next_c; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment