Skip to content

Commit

Permalink
ready for web
Browse files Browse the repository at this point in the history
  • Loading branch information
xinpw8 committed Dec 23, 2024
1 parent 5aad39b commit af9eabc
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 153 deletions.
201 changes: 83 additions & 118 deletions pufferlib/ocean/blastar/blastar.c
Original file line number Diff line number Diff line change
@@ -1,137 +1,102 @@
// #include "blastar_env.h"
// #include "blastar_renderer.h"
// #include "puffernet.h"
// #include <assert.h>
// #include <stdio.h>
// #include <stdlib.h>
// #include <time.h>

// #define OBSERVATIONS_SIZE 27
// #define ACTIONS_SIZE 6

// void get_input(BlastarEnv* env) {
// if ((IsKeyDown(KEY_DOWN) && IsKeyDown(KEY_RIGHT))) {
// env->actions[0] = 4; // Move down-right
// } else if ((IsKeyDown(KEY_DOWN) && IsKeyDown(KEY_LEFT))) {
// env->actions[0] = 5; // Move down-left
// } else if (IsKeyDown(KEY_SPACE) && (IsKeyDown(KEY_RIGHT))) {
// env->actions[0] = 6; // Fire and move right
// } else if (IsKeyDown(KEY_SPACE) && (IsKeyDown(KEY_LEFT))) {
// env->actions[0] = 7; // Fire and move left
// } else if (IsKeyDown(KEY_SPACE)) {
// env->actions[0] = 3; // Fire
// } else if (IsKeyDown(KEY_DOWN)) {
// env->actions[0] = 2; // Move down
// } else if (IsKeyDown(KEY_LEFT)) {
// env->actions[0] = 1; // Move left
// } else if (IsKeyDown(KEY_RIGHT)) {
// env->actions[0] = 0; // Move right
// } else {
// env->actions[0] = 0; // No action
// }
// }

// int demo() {
// // Load weights for the AI model
// Weights* weights = load_weights("resources/blastar/blastar_2.bin", 136583);
// LinearLSTM* net = make_linearlstm(weights, 1, OBSERVATIONS_SIZE, ACTIONS_SIZE);

// BlastarEnv env;
// init_blastar(&env);
// allocate_env(&env);

// Client* client = make_client(&env);

// unsigned int seed = 12345;
// srand(seed);
// reset_blastar(&env);

// int running = 1;
// while (running) {
// if (IsKeyDown(KEY_LEFT_SHIFT)) {
// get_input(&env); // Human input
// } else {
// forward_linearlstm(net, env.observations, env.actions); // AI input
// }

// c_step(&env);
// c_render(client, &env);

// if (WindowShouldClose() || env.game_over) {
// running = 0;
// }
// }

// free_linearlstm(net);
// free(weights);
// close_client(client);
// free_allocated_env(&env);
// close_blastar(&env);
// return 0;
// }

// void perftest(float test_time) {
// BlastarEnv env;
// init_blastar(&env);
// allocate_env(&env);

// unsigned int seed = 12345;
// srand(seed);
// reset_blastar(&env);

// int start = time(NULL);
// int steps = 0;
// while (time(NULL) - start < test_time) {
// env.actions[0] = rand() % ACTIONS_SIZE; // Random actions
// c_step(&env);
// steps++;
// }

// int end = time(NULL);
// printf("Steps per second: %f\n", steps / (float)(end - start));

// free_allocated_env(&env);
// close_blastar(&env);
// }

// int main() {
// demo();
// // perftest(30.0f);
// return 0;
// }






// blastar.c
#include "blastar_env.h"
#include "blastar_renderer.h"
#include "puffernet.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define OBSERVATIONS_SIZE 31
#define ACTIONS_SIZE 6

void get_input(BlastarEnv* env) {
if ((IsKeyDown(KEY_DOWN) && IsKeyDown(KEY_RIGHT))) {
env->actions[0] = 4; // Move down-right
} else if ((IsKeyDown(KEY_DOWN) && IsKeyDown(KEY_LEFT))) {
env->actions[0] = 5; // Move down-left
} else if (IsKeyDown(KEY_SPACE) && (IsKeyDown(KEY_RIGHT))) {
env->actions[0] = 6; // Fire and move right
} else if (IsKeyDown(KEY_SPACE) && (IsKeyDown(KEY_LEFT))) {
env->actions[0] = 7; // Fire and move left
} else if (IsKeyDown(KEY_SPACE)) {
env->actions[0] = 3; // Fire
} else if (IsKeyDown(KEY_DOWN)) {
env->actions[0] = 2; // Move down
} else if (IsKeyDown(KEY_LEFT)) {
env->actions[0] = 1; // Move left
} else if (IsKeyDown(KEY_RIGHT)) {
env->actions[0] = 0; // Move right
} else {
env->actions[0] = 0; // No action
}
}

int demo() {
// Load weights for the AI model
Weights* weights = load_weights("./pufferlib/resources/blastar/blastar_weights.bin", 137095);
LinearLSTM* net = make_linearlstm(weights, 1, OBSERVATIONS_SIZE, ACTIONS_SIZE);

int main() {
BlastarEnv env;
init_blastar(&env);

allocate_env(&env);
allocate_env(&env);

Client* client = make_client(&env);

while (!WindowShouldClose() && !env.game_over) {
int action = 0;
if (IsKeyDown(KEY_LEFT)) action = 1;
if (IsKeyDown(KEY_RIGHT)) action = 2;
if (IsKeyDown(KEY_UP)) action = 3;
if (IsKeyDown(KEY_DOWN)) action = 4;
if (IsKeyPressed(KEY_SPACE)) action = 5;
unsigned int seed = 12345;
srand(seed);
reset_blastar(&env);

int running = 1;
while (running) {
if (IsKeyDown(KEY_LEFT_SHIFT)) {
get_input(&env); // Human input
} else {
forward_linearlstm(net, env.observations, env.actions); // AI input
}

if (env.actions) env.actions[0] = action;
c_step(&env);
c_render(client, &env);

if (WindowShouldClose() || env.game_over) {
running = 0;
}
}

free_linearlstm(net);
free(weights);
close_client(client);
free_allocated_env(&env);
close_blastar(&env);
return 0;
}

void perftest(float test_time) {
BlastarEnv env;
init_blastar(&env);
allocate_env(&env);

unsigned int seed = 12345;
srand(seed);
reset_blastar(&env);

int start = time(NULL);
int steps = 0;
while (time(NULL) - start < test_time) {
env.actions[0] = rand() % ACTIONS_SIZE; // Random actions
c_step(&env);
steps++;
}

int end = time(NULL);
printf("Steps per second: %f\n", steps / (float)(end - start));

free_allocated_env(&env);
close_blastar(&env);
}

int main() {
demo();
// perftest(30.0f);
return 0;
}
36 changes: 7 additions & 29 deletions pufferlib/ocean/blastar/blastar_env.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// blastar_env.c
#include "blastar_env.h"
#include <stdlib.h>
// #include <numpy/arrayobject.h>

LogBuffer* allocate_logbuffer(int size) {
LogBuffer* logs = (LogBuffer*)calloc(1, sizeof(LogBuffer));
Expand Down Expand Up @@ -31,7 +30,6 @@ void free_reward_buffer(RewardBuffer* buffer) {
}
}


void add_log(LogBuffer* logs, Log* log) {
if (logs->idx == logs->length) {
return;
Expand Down Expand Up @@ -127,40 +125,30 @@ int is_valid_pointer(void* ptr) {

void free_allocated_env(BlastarEnv* env) {
if (env) {
// Debugging: Print pointers to ensure validity
printf("Freeing pointers: observations=%p, actions=%p, rewards=%p, terminals=%p, log_buffer=%p, reward_buffer=%p\n",
env->observations, env->actions, env->rewards, env->terminals, env->log_buffer, env->reward_buffer);

if (is_valid_pointer(env->observations)) {
free(env->observations);
env->observations = NULL;
}

if (is_valid_pointer(env->actions)) {
free(env->actions);
env->actions = NULL;
}

if (is_valid_pointer(env->rewards)) {
free(env->rewards);
env->rewards = NULL;
}

if (is_valid_pointer(env->terminals)) {
free(env->terminals);
env->terminals = NULL;
}

if (is_valid_pointer(env->log_buffer)) {
free_logbuffer(env->log_buffer);
env->log_buffer = NULL;
}

if (is_valid_pointer(env->reward_buffer)) {
free_reward_buffer(env->reward_buffer);
env->reward_buffer = NULL;
}

}
}

Expand Down Expand Up @@ -217,7 +205,6 @@ void init_blastar(BlastarEnv *env) {
env->enemy.bullet.y = env->enemy.y;
env->enemy.bullet.last_x = env->enemy.bullet.x;
env->enemy.bullet.last_y = env->enemy.bullet.y;

env->player_bullet_width = 17;
env->player_bullet_height = 6;
env->enemy_bullet_width = 10;
Expand All @@ -230,7 +217,6 @@ void reset_blastar(BlastarEnv* env) {
env->log = (Log){0};
env->tick = 0;
env->game_over = false;

init_blastar(env);
}

Expand All @@ -240,12 +226,11 @@ void close_blastar(BlastarEnv* env) {

void compute_observations(BlastarEnv* env) {
if (env && env->observations) {
// Update env variables

// Infinite lives
if (env->player.lives < 5) {
env->player.lives = 5;
}
// // Infinite lives
// if (env->player.lives < 5) {
// env->player.lives = 5;
// }

env->log.lives = env->player.lives;
env->log.score = env->player.score;
Expand Down Expand Up @@ -363,17 +348,15 @@ void compute_observations(BlastarEnv* env) {
} else {
env->observations[30] = 0.0f; // Default to zero if denominator is zero
}


}
}
}

// Combined step function
void c_step(BlastarEnv *env) {
if (env == NULL) return;

if (!env->actions || !env->rewards || !env->terminals) {
// Blank
// Empty
}

if (env->game_over) {
Expand Down Expand Up @@ -412,10 +395,7 @@ void c_step(BlastarEnv *env) {
float hit_enemy_with_bullet_rew = 0.0f;
float hit_by_enemy_bullet_penalty_rew = 0.0f;
int crossed_screen = 0;
float improvement_reward = 0.0f;
float flat_reward = 0.0f;


int action = 0;
action = env->actions[0];

Expand Down Expand Up @@ -587,7 +567,7 @@ void c_step(BlastarEnv *env) {
env->log.score += 1.0f;
env->enemyExplosionTimer = 30;
if (crossed_screen == 0) {
hit_enemy_with_bullet_rew += 2.5f; // 2.5f; // Big reward for quick kill
hit_enemy_with_bullet_rew += 2.5f; // Big reward for quick kill
} else {
hit_enemy_with_bullet_rew += 1.5f - (0.1f * env->enemy.crossed_screen); // Less rew if enemy crossed screen
}
Expand Down Expand Up @@ -652,7 +632,6 @@ void c_step(BlastarEnv *env) {
env->log.hit_by_enemy_bullet_penalty_rew = hit_by_enemy_bullet_penalty_rew;
env->log.flat_below_enemy_rew = flat_reward;
env->enemy.crossed_screen = crossed_screen;
// env->log.bullet_distance_to_enemy_rew += improvement_reward;

// Reward player only if below enemy
if (env->player.y > env->enemy.y + env->enemy.height) {
Expand All @@ -666,7 +645,6 @@ void c_step(BlastarEnv *env) {
env->log.episode_return += 0.0f;
}


if (env->bad_guy_score > 100.0f) {
// env->player.lives = 0;
env->game_over = true;
Expand Down
Loading

0 comments on commit af9eabc

Please sign in to comment.