Skip to content

Instantly share code, notes, and snippets.

MAKE_FNS = {
'breakout': lambda: lazy_import('pufferlib.ocean.breakout.breakout', 'Breakout'),
'pong': lambda: lazy_import('pufferlib.ocean.pong.pong', 'Pong'),
'enduro': lambda: lazy_import('pufferlib.ocean.enduro.enduro', 'Enduro'),
'moba': lambda: lazy_import('pufferlib.ocean.moba.moba', 'Moba'),
'nmmo3': lambda: lazy_import('pufferlib.ocean.nmmo3.nmmo3', 'NMMO3'),
'snake': lambda: lazy_import('pufferlib.ocean.snake.snake', 'Snake'),
'squared': lambda: lazy_import('pufferlib.ocean.squared.squared', 'Squared'),
'pysquared': lambda: lazy_import('pufferlib.ocean.squared.pysquared', 'PySquared'),
'connect4': lambda: lazy_import('pufferlib.ocean.connect4.connect4', 'Connect4'),
#define EMPTY 0
#define NORMAL_FOOD 1
#define INTERACTIVE_FOOD 2
// Anything above Wall should be obstacles
#define WALL 3
#define AGENTS 4
#define LOG_BUFFER_SIZE 8192
#define SET_BIT(arr, i) (arr[(i) / 8] |= (1 << ((i) % 8)))
void step_agent(CCpr *env, int i) {
int action = env->actions[i];
int dr = 0;
int dc = 0;
switch (action) {
case 0:
dr = -1;
void c_step(CCpr *env) {
memset(env->rewards, 0, env->num_agents * sizeof(float));
memset(env->interactive_food_agent_count, 0,
(env->width * env->height + 7) / 8);
for (int i = 0; i < env->num_agents; i++) {
step_agent(env, i);
}
void c_reset(CCpr *env) {
env->tick = 0;
for (int r = 0; r < env->height; r++) {
for (int c = 0; c < env->width; c++) {
int adr = grid_index(env, r, c);
env->grid[adr] = EMPTY;
}
}
void compute_observations(CCpr *env) {
for (int i = 0; i < env->num_agents; i++) {
Agent *agent = &env->agents[i];
int obs_offset = i * env->obs_size;
int r_offset = agent->r - env->vision;
int c_offset = agent->c - env->vision;
for (int r = 0; r < 2 * env->vision + 1; r++) {
for (int c = 0; c < 2 * env->vision + 1; c++) {
int grid_idx = (r_offset + r) * env->width + c_offset + c;
int obs_idx = obs_offset + r * env->vision_window + c;
void spawn_foods(CCpr *env) {
// After each step, check existing foods and spawns new food in the
// neighborhood Iterates over food_list for efficiency instead of the entire
// grid.
FoodList *foods = env->foods;
int original_size = foods->size;
for (int i = 0; i < original_size; i++) {
int idx = foods->indexes[i];
int offset = idx - env->width - 1; // Food spawn in 1 radius
int r = offset / env->width;
typedef struct FoodList FoodList;
struct FoodList {
int *indexes; // Grid flattened index positions
int size;
};
@MX-2000
MX-2000 / cpr.h
Last active March 26, 2025 10:39
typedef struct CCpr CCpr;
struct CCpr {
int width;
int height;
int num_agents;
int vision;
int vision_window;
int obs_size;
typedef struct Agent Agent;
struct Agent {
int r;
int c;
int id;
};