Skip to content

Commit 8eaab1c

Browse files
author
Joseph Suarez
committed
Slimevolley fixes
1 parent 753914d commit 8eaab1c

File tree

3 files changed

+62
-36
lines changed

3 files changed

+62
-36
lines changed

pufferlib/config/ocean/slimevolley.ini

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@ policy_name = Policy
55

66
[env]
77
; 1 for single-agent (vs bot), 2 for two-agent (self-play)
8-
num_agents=2
8+
num_agents=1
9+
num_envs=4096
910

1011
[train]
11-
learning_rate = 0.015
12-
total_timesteps = 10_000_000
13-
num_envs=128
14-
num_workers=8
15-
batch_size=1024
16-
minibatch_size=128
12+
total_timesteps = 100_000_000

pufferlib/ocean/slimevolley/slimevolley.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ void abranti_simple_policy(float* obs, float* action) {
2020
}
2121

2222
void random_policy(float* obs, float* action) {
23-
action[0] = rand() * 2 - 1;
24-
action[1] = rand() * 2 - 1;
25-
action[2] = rand() * 2 - 1;
23+
action[0] = 2*randf() - 1;
24+
action[1] = 2*randf() - 1;
25+
action[2] = 2*randf() - 1;
2626
}
2727

2828
int main() {
@@ -41,20 +41,18 @@ int main() {
4141
fprintf(stderr, "num agents: %d\n", env.num_agents);
4242

4343
while (!WindowShouldClose()) {
44-
for (int i=0; i<env.num_agents; i++) {
45-
if (i == 0) {
46-
random_policy(&env.observations[12*i], &env.actions[3*i]);
47-
48-
} else {
49-
abranti_simple_policy(&env.observations[12*i], &env.actions[3*i]);
50-
}
44+
env.actions[0] = 0.0;
45+
env.actions[1] = 0.0;
46+
env.actions[2] = 0.0;
47+
if (IsKeyDown(KEY_LEFT_SHIFT)) {
48+
if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D)) env.actions[0] = 1.0;
49+
if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A)) env.actions[1] = 1.0;
50+
if (IsKeyDown(KEY_UP) || IsKeyDown(KEY_W) || IsKeyDown(KEY_SPACE)) env.actions[2] = 1.0;
51+
} else {
52+
abranti_simple_policy(env.observations, env.actions);
5153
}
5254
c_step(&env);
5355
c_render(&env);
54-
if (env.terminals[0] || env.terminals[1]) {
55-
fprintf(stderr, "Episode ended. Rewards: %f, %f\n", env.rewards[0], env.rewards[1]);
56-
break;
57-
}
5856
}
5957

6058
// Try to clean up after yourself

pufferlib/ocean/slimevolley/slimevolley.h

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ const Color FENCE_COLOR = {240, 210, 130, 255};
3939
const Color COIN_COLOR = {240, 210, 130, 255};
4040
const Color GROUND_COLOR = {128, 227, 153, 255};
4141

42+
const Color PUFF_RED = (Color){187, 0, 0, 255};
43+
const Color PUFF_CYAN = (Color){0, 187, 187, 255};
44+
const Color PUFF_WHITE = (Color){241, 241, 241, 241};
45+
const Color PUFF_BACKGROUND = (Color){6, 24, 24, 255};
46+
4247
// UTILS
4348
typedef struct {
4449
float x;
@@ -62,6 +67,10 @@ float to_y_pixel(float y){
6267
return WINDOW_HEIGHT - y * FACTOR;
6368
}
6469

70+
float randf() {
71+
return (float)rand() / (float)RAND_MAX;
72+
}
73+
6574
// OBJECTS
6675
typedef struct {
6776
float x;
@@ -74,10 +83,6 @@ typedef struct {
7483
Color c;
7584
} Ball;
7685

77-
void ball_display(Ball* ball){
78-
DrawCircleV((Vector2){to_x_pixel(ball->x), to_y_pixel(ball->y)}, to_p(ball->r), ball->c);
79-
}
80-
8186
void ball_move(Ball* ball){
8287
ball->prev_x = ball->x;
8388
ball->prev_y = ball->y;
@@ -258,8 +263,8 @@ void agent_display(Agent *agent, float bx, float by) {
258263
float dist = sqrtf(ballX * ballX + ballY * ballY);
259264
float eyeX = 0, eyeY = 0;
260265
if (dist > 0) {
261-
float eyeX = ballX / dist;
262-
float eyeY = ballY / dist;
266+
eyeX = ballX / dist;
267+
eyeY = ballY / dist;
263268
}
264269

265270
// Draw white of the eye
@@ -391,6 +396,7 @@ typedef struct {
391396
float* bot_observations; // Optional, for bot control
392397
float* bot_actions; // Optional, for bot control
393398
int tick;
399+
Texture2D puffers;
394400
} SlimeVolley;
395401

396402

@@ -417,8 +423,8 @@ void init(SlimeVolley* env) {
417423
void c_reset(SlimeVolley* env) {
418424
env->tick = 0;
419425
env->delay_frames = INIT_DELAY_FRAMES;
420-
float ball_vx = ((float) rand() / RAND_MAX)*40.0f - 20.0f;
421-
float ball_vy = ((float) rand() / RAND_MAX)*15.0f + 10.0f;
426+
float ball_vx = 40.0f*randf() - 20.0f;
427+
float ball_vy = 15.0f*randf() + 10.0f;
422428
*env->ball = (Ball){
423429
.x = 0,
424430
.y = REF_W/4,
@@ -444,7 +450,7 @@ void c_reset(SlimeVolley* env) {
444450
.dir = i == 0 ? -1 : 1,
445451
.x = i == 0 ? -REF_W/4 : REF_W/4,
446452
.y = REF_U,
447-
.c = i == 0 ? AGENT_LEFT_COLOR : AGENT_RIGHT_COLOR,
453+
.c = i == 0 ? PUFF_RED : PUFF_CYAN,
448454
.r = 1.5,
449455
.lives = MAXLIVES,
450456
.observations = observations
@@ -464,8 +470,8 @@ float clip(float val, float min, float max) {
464470
}
465471

466472
void new_match(SlimeVolley* env) {
467-
float ball_vx = ((float) rand() / RAND_MAX)*40.0f - 20.0f;
468-
float ball_vy = ((float) rand() / RAND_MAX)*15.0f + 10.0f;
473+
float ball_vx = 40.0f*randf() - 20.0f;
474+
float ball_vy = 15.0f*randf() + 10.0f;
469475
*env->ball = (Ball){
470476
.x = 0,
471477
.y = REF_W/4,
@@ -575,20 +581,46 @@ void c_step(SlimeVolley* env) {
575581
// Required function. Should handle creating the client on first call
576582
void c_render(SlimeVolley* env) {
577583
if (!IsWindowReady()) {
584+
SetConfigFlags(FLAG_MSAA_4X_HINT);
578585
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "PufferLib SlimeVolley");
579-
SetTargetFPS(60);
586+
SetTargetFPS(50); // From original
587+
env->puffers = LoadTexture("resources/shared/puffers.png");
580588
}
581589

582590
// Standard across our envs so exiting is always the same
583591
if (IsKeyDown(KEY_ESCAPE)) {
584592
exit(0);
585593
}
586594
BeginDrawing();
587-
ClearBackground(BACKGROUND_COLOR);
595+
ClearBackground(PUFF_BACKGROUND);
588596
wall_display(env->ground);
589597
wall_display(env->fence);
590-
ball_display(env->fence_stub);
591-
ball_display(env->ball);
598+
599+
// Fence
600+
Ball* stub = env->fence_stub;
601+
DrawCircleV(
602+
(Vector2){to_x_pixel(stub->x), to_y_pixel(stub->y)},
603+
to_p(stub->r), stub->c
604+
);
605+
606+
Ball* puff = env->ball;
607+
DrawTexturePro(
608+
env->puffers,
609+
(Rectangle){
610+
0,
611+
(puff->vx > 0 ? 576 : 608),
612+
32, 32,
613+
},
614+
(Rectangle){
615+
to_x_pixel(puff->x) - 16,
616+
to_y_pixel(puff->y) - 16,
617+
32,
618+
32
619+
},
620+
(Vector2){0, 0},
621+
0,
622+
WHITE
623+
);
592624

593625
for (int i=0; i<2; i++) {
594626
agent_display(&env->agents[i], env->ball->x, env->ball->y);

0 commit comments

Comments
 (0)