Skip to content

Commit

Permalink
17593: Fixes issue where entities recreated with the same name would …
Browse files Browse the repository at this point in the history
…receive the same random number seed if the seed state were not advanced (#11)
  • Loading branch information
howsohazard authored Sep 21, 2023
1 parent 8653121 commit 59b9f4e
Show file tree
Hide file tree
Showing 5 changed files with 3,900 additions and 3,914 deletions.
2 changes: 1 addition & 1 deletion src/Amalgam/AssetManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ Entity *AssetManager::LoadEntityFromResourcePath(std::string &resource_path, std


//don't escape filename again because it's already escaped in this loop
std::string default_seed = new_entity->CreateOtherRandomStreamStateViaString(entity_name);
std::string default_seed = new_entity->CreateRandomStreamFromStringAndRand(entity_name);
std::string contained_resource_path = resource_base_path + ce_file_base + "." + ce_extension;
Entity *contained_entity = LoadEntityFromResourcePath(contained_resource_path, file_type,
false, true, false, escape_contained_filenames, default_seed);
Expand Down
14 changes: 3 additions & 11 deletions src/Amalgam/entity/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -867,21 +867,13 @@ void Entity::SetRandomStream(const RandomStream &new_stream, std::vector<EntityW
}
}

std::string Entity::CreateOtherRandomStreamStateViaString(const std::string &seed_string)
std::string Entity::CreateRandomStreamFromStringAndRand(const std::string &seed_string)
{
//consume a random number to advance the state for creating the new state
randomStream.RandUInt32();
return randomStream.CreateOtherStreamStateViaString(seed_string);
}

RandomStream Entity::CreateOtherRandomStreamViaString(const std::string &seed_string)
{
return randomStream.CreateOtherStreamViaString(seed_string);
}

RandomStream Entity::CreateOtherRandomStreamViaRand()
{
return randomStream.CreateOtherStreamViaRand();
}

void Entity::SetRoot(EvaluableNode *_code, bool allocated_with_entity_enm, EvaluableNodeManager::EvaluableNodeMetadataModifier metadata_modifier, std::vector<EntityWriteListener *> *write_listeners)
{
EvaluableNode *previous_root = evaluableNodeManager.GetRootNode();
Expand Down
10 changes: 2 additions & 8 deletions src/Amalgam/entity/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,8 @@ class Entity
// write_listeners is optional, and if specified, will log the event
void SetRandomStream(const RandomStream &new_stream, std::vector<EntityWriteListener *> *write_listeners = nullptr);

//returns a random seed based on this stream's current state and seed_string parameter
std::string CreateOtherRandomStreamStateViaString(const std::string &seed_string);

//returns a Randomstream based on this stream's current state and seed_string parameter
RandomStream CreateOtherRandomStreamViaString(const std::string &seed_string);

//consumes random numbers from the stream to create a new RandomStream
RandomStream CreateOtherRandomStreamViaRand();
//returns a random seed based on a random number consumed from the entity and seed_string parameter
std::string CreateRandomStreamFromStringAndRand(const std::string &seed_string);

//Returns true if the Entity is a named entity, that is, its ID is not autogenerated
// An identity is considered named if the string represents anything other than an integer
Expand Down
4 changes: 2 additions & 2 deletions src/Amalgam/interpreter/InterpreterOpcodesEntityControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_CREATE_ENTITIES(EvaluableN
curNumExecutionNodesAllocatedToEntities += new_entity->GetDeepSizeInNodes();

const std::string &new_entity_id_string = string_intern_pool.GetStringFromID(new_entity_id);
new_entity->SetRandomState(destination_entity_parent->CreateOtherRandomStreamStateViaString(new_entity_id_string), false);
new_entity->SetRandomState(destination_entity_parent->CreateRandomStreamFromStringAndRand(new_entity_id_string), false);

destination_entity_parent->AddContainedEntityViaReference(new_entity, new_entity_id, writeListeners);

Expand Down Expand Up @@ -618,7 +618,7 @@ EvaluableNodeReference Interpreter::InterpretNode_ENT_LOAD_ENTITY_and_LOAD_PERSI
file_type = file_type_temp;
}

std::string random_seed = destination_entity_parent->CreateOtherRandomStreamStateViaString(resource_name);
std::string random_seed = destination_entity_parent->CreateRandomStreamFromStringAndRand(resource_name);
Entity *loaded_entity = asset_manager.LoadEntityFromResourcePath(resource_name, file_type,
persistent, true, escape_filename, escape_contained_filenames, random_seed);

Expand Down
Loading

0 comments on commit 59b9f4e

Please sign in to comment.