Skip to content

Commit

Permalink
Fix where engulfing depends on number of hexes instead of organelles (R…
Browse files Browse the repository at this point in the history
…evolutionary-Games#745)

* Fix where engulfing depends on number of hexes instead of organelles

* Cache hex count on organelle and microbe and use that in engulfing instead of recalculating it every time.

* Moved caching from the placed organelle into the organelle.

* Updated organelle cache on removal and addition of an organelle.

* Applied review
Made methods and variables private
Added comment

* Made function const

* Removed intermediate getHexCount function

* Small improvements
  • Loading branch information
turoni authored and hhyyrylainen committed Feb 27, 2019
1 parent 756b3e8 commit a83a2ad
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
8 changes: 8 additions & 0 deletions scripts/microbe_stage/microbe.as
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class MicrobeComponent : ScriptComponent{
this.microbeEntity = forEntity;
this.agentEmissionCooldown = 0;

//cache hexes in microbe
for(uint i = 0; i < organelles.length(); ++i){
totalHexCountCache += organelles[i].organelle.getHexCount();
}

// Microbe system update should initialize this component on next tick
}

Expand Down Expand Up @@ -186,6 +191,9 @@ class MicrobeComponent : ScriptComponent{
ObjectID microbeEntity = NULL_OBJECT;

Float3 queuedMovementForce = Float3(0, 0, 0);

// This variable is used to cache the size of the hexes
int totalHexCountCache = 0;
}

//! Helper for MicrobeSystem
Expand Down
4 changes: 4 additions & 0 deletions scripts/microbe_stage/microbe_operations.as
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ bool removeOrganelle(CellStageWorld@ world, ObjectID microbeEntity, Int2 hex)

organelle.onRemovedFromMicrobe(microbeEntity, rigidBodyComponent.Body.Shape);

microbeComponent.totalHexCountCache -= organelle.organelle.getHexCount();

// TODO: there seriously needs to be some caching here to make this less expensive
rigidBodyComponent.ChangeShape(world.GetPhysicalWorld(), rigidBodyComponent.Body.Shape);

Expand Down Expand Up @@ -197,6 +199,8 @@ bool addOrganelle(CellStageWorld@ world, ObjectID microbeEntity, PlacedOrganelle

microbeComponent.organelles.insertLast(@organelle);

microbeComponent.totalHexCountCache += organelle.organelle.getHexCount();

// Update collision shape
if(editShape !is null){
// Initial adding
Expand Down
7 changes: 7 additions & 0 deletions scripts/microbe_stage/organelle.as
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ class Organelle{
Hex@ hex = Hex(q, r);

@hexes[formatInt(s)] = hex;

return true;
}

Expand Down Expand Up @@ -199,13 +200,19 @@ class Organelle{
}
}

int getHexCount() const
{
return hexes.getSize();
}

private string _name;
float mass;
string gene;

array<OrganelleComponentFactory@> components;
private dictionary hexes;


// The initial amount of compounds this organelle consists of
dictionary initialComposition;

Expand Down
24 changes: 12 additions & 12 deletions scripts/microbe_stage/setup.as
Original file line number Diff line number Diff line change
Expand Up @@ -375,12 +375,12 @@ void cellOnCellActualContact(GameWorld@ world, ObjectID firstEntity, ObjectID se
if (firstMicrobeComponent !is null && secondMicrobeComponent !is null)
{
// Get microbe sizes here
int firstMicrobeComponentOrganelles = firstMicrobeComponent.organelles.length();
int secondMicrobeComponentOrganelles = secondMicrobeComponent.organelles.length();
int firstMicrobeComponentHexCount = firstMicrobeComponent.totalHexCountCache;
int secondMicrobeComponentHexCount = secondMicrobeComponent.totalHexCountCache;
if (firstMicrobeComponent.engulfMode)
{
if(firstMicrobeComponentOrganelles >
(ENGULF_HP_RATIO_REQ * secondMicrobeComponentOrganelles) &&
if(firstMicrobeComponentHexCount >
(ENGULF_HP_RATIO_REQ * secondMicrobeComponentHexCount) &&
firstMicrobeComponent.dead == false && secondMicrobeComponent.dead == false)
{
secondMicrobeComponent.isBeingEngulfed = true;
Expand All @@ -390,8 +390,8 @@ void cellOnCellActualContact(GameWorld@ world, ObjectID firstEntity, ObjectID se
}
if (secondMicrobeComponent.engulfMode)
{
if(secondMicrobeComponentOrganelles >
(ENGULF_HP_RATIO_REQ * firstMicrobeComponentOrganelles) &&
if(secondMicrobeComponentHexCount >
(ENGULF_HP_RATIO_REQ * firstMicrobeComponentHexCount) &&
secondMicrobeComponent.dead == false && firstMicrobeComponent.dead == false)
{
firstMicrobeComponent.isBeingEngulfed = true;
Expand Down Expand Up @@ -425,16 +425,16 @@ bool beingEngulfed(GameWorld@ world, ObjectID firstEntity, ObjectID secondEntity
if (firstMicrobeComponent !is null && secondMicrobeComponent !is null)
{
// Get microbe sizes here
int firstMicrobeComponentOrganelles = firstMicrobeComponent.organelles.length();
int secondMicrobeComponentOrganelles = secondMicrobeComponent.organelles.length();
int firstMicrobeComponentHexCount = firstMicrobeComponent.totalHexCountCache;
int secondMicrobeComponentHexCount = secondMicrobeComponent.totalHexCountCache;
// If either cell is engulfing we need to do things
//return false;
//LOG_INFO(""+firstMicrobeComponent.engulfMode);
// LOG_INFO(""+secondMicrobeComponent.engulfMode);
if (firstMicrobeComponent.engulfMode)
{
if(firstMicrobeComponentOrganelles >
(ENGULF_HP_RATIO_REQ * secondMicrobeComponentOrganelles) &&
if(firstMicrobeComponentHexCount >
(ENGULF_HP_RATIO_REQ * secondMicrobeComponentHexCount) &&
firstMicrobeComponent.dead == false && secondMicrobeComponent.dead == false)
{
secondMicrobeComponent.isBeingEngulfed = true;
Expand All @@ -446,8 +446,8 @@ bool beingEngulfed(GameWorld@ world, ObjectID firstEntity, ObjectID secondEntity
}
if (secondMicrobeComponent.engulfMode)
{
if(secondMicrobeComponentOrganelles >
(ENGULF_HP_RATIO_REQ * firstMicrobeComponentOrganelles) &&
if(secondMicrobeComponentHexCount >
(ENGULF_HP_RATIO_REQ * firstMicrobeComponentHexCount) &&
secondMicrobeComponent.dead == false && firstMicrobeComponent.dead == false)
{
firstMicrobeComponent.isBeingEngulfed = true;
Expand Down

0 comments on commit a83a2ad

Please sign in to comment.