Skip to content

Commit

Permalink
Added separate random number generator for the distractor image axis
Browse files Browse the repository at this point in the history
  • Loading branch information
tabula-rosa committed Feb 28, 2024
1 parent 68905fe commit 0aed62b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
7 changes: 7 additions & 0 deletions procgen/src/basic-abstract-game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,13 @@ void BasicAbstractGame::choose_random_theme(const std::shared_ptr<Entity> &ent)
ent->image_theme = rand_gen.randn(asset_num_themes[ent->image_type]);
}

// This will choose a random theme from a specified random number generator
// Note that initialize_asset_if_necessary will pull from asset_rand_gen if an asset does not exist and/or using generated assets
void BasicAbstractGame::choose_random_theme_from_rand_gen(const std::shared_ptr<Entity> &ent, RandGen &rand_num_gen) {
initialize_asset_if_necessary(ent->image_type);
ent->image_theme = rand_num_gen.randn(asset_num_themes[ent->image_type]);
}

void BasicAbstractGame::choose_step_random_theme(const std::shared_ptr<Entity> &ent) {
initialize_asset_if_necessary(ent->image_type);
ent->image_theme = step_rand_int % asset_num_themes[ent->image_type];
Expand Down
1 change: 1 addition & 0 deletions procgen/src/basic-abstract-game.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class BasicAbstractGame : public Game {
void match_aspect_ratio(const std::shared_ptr<Entity> &ent, bool match_width = true);
void fit_aspect_ratio(const std::shared_ptr<Entity> &ent);
void choose_random_theme(const std::shared_ptr<Entity> &ent);
void choose_random_theme_from_rand_gen(const std::shared_ptr<Entity> &ent, RandGen &rand_num_gen);
int mask_theme_if_necessary(int theme, int type);
void tile_image(QPainter &p, QImage *image, const QRectF &rect, float tile_ratio);

Expand Down
15 changes: 13 additions & 2 deletions procgen/src/games/leaper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class LeaperGame : public BasicAbstractGame {
std::vector<float> water_lane_speeds;
int goal_y = 0;

// random number generator for the third axis so it remains an independent causal mechanism
RandGen rand_gen_distractor;

LeaperGame()
: BasicAbstractGame(NAME) {
maxspeed = MAX_SPEED;
Expand Down Expand Up @@ -191,11 +194,17 @@ class LeaperGame : public BasicAbstractGame {

add_entity_rxy(main_width / 2.0, goal_y - .5, 0, 0, main_width / 2.0, .5, FINISH_LINE);

// Adding distractor images
if (options.level_options_3 != -1) {

// Seed the random number generator
rand_gen_distractor.seed(current_level_seed);

// Overlay the images
for (int distractor_id = 0; distractor_id < options.level_options_3; distractor_id++) {
auto d = std::make_shared<Entity>(main_width*rand_gen.rand01(), main_height*rand_gen.rand01(), 0, 0, 1.0, 1.0, DISTRACTOR);
auto d = std::make_shared<Entity>(main_width*rand_gen_distractor.rand01(), main_height*rand_gen_distractor.rand01(), 0, 0, 1.0, 1.0, DISTRACTOR);
d->render_z = 1;
choose_random_theme(d);
choose_random_theme_from_rand_gen(d, rand_gen_distractor);
entities.push_back(d);
}
}
Expand Down Expand Up @@ -308,6 +317,7 @@ class LeaperGame : public BasicAbstractGame {
b->write_int(bottom_water_y);
b->write_vector_float(water_lane_speeds);
b->write_int(goal_y);
rand_gen_distractor.serialize(b);
}

void deserialize(ReadBuffer *b) override {
Expand All @@ -317,6 +327,7 @@ class LeaperGame : public BasicAbstractGame {
bottom_water_y = b->read_int();
water_lane_speeds = b->read_vector_float();
goal_y = b->read_int();
rand_gen_distractor.deserialize(b);
}

void observe() override {
Expand Down

0 comments on commit 0aed62b

Please sign in to comment.