From a83a2ad64247472fbf4a69e743145719310e2cce Mon Sep 17 00:00:00 2001 From: turoni Date: Wed, 27 Feb 2019 08:22:44 +0100 Subject: [PATCH] Fix where engulfing depends on number of hexes instead of organelles (#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 --- scripts/microbe_stage/microbe.as | 8 +++++++ scripts/microbe_stage/microbe_operations.as | 4 ++++ scripts/microbe_stage/organelle.as | 7 ++++++ scripts/microbe_stage/setup.as | 24 ++++++++++----------- 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/scripts/microbe_stage/microbe.as b/scripts/microbe_stage/microbe.as index 41644fac989..f3726059c64 100644 --- a/scripts/microbe_stage/microbe.as +++ b/scripts/microbe_stage/microbe.as @@ -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 } @@ -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 diff --git a/scripts/microbe_stage/microbe_operations.as b/scripts/microbe_stage/microbe_operations.as index 24b3decdc7a..389228ad4c5 100644 --- a/scripts/microbe_stage/microbe_operations.as +++ b/scripts/microbe_stage/microbe_operations.as @@ -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); @@ -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 diff --git a/scripts/microbe_stage/organelle.as b/scripts/microbe_stage/organelle.as index daac41b24ea..0b6bc190d7b 100644 --- a/scripts/microbe_stage/organelle.as +++ b/scripts/microbe_stage/organelle.as @@ -107,6 +107,7 @@ class Organelle{ Hex@ hex = Hex(q, r); @hexes[formatInt(s)] = hex; + return true; } @@ -199,6 +200,11 @@ class Organelle{ } } + int getHexCount() const + { + return hexes.getSize(); + } + private string _name; float mass; string gene; @@ -206,6 +212,7 @@ class Organelle{ array components; private dictionary hexes; + // The initial amount of compounds this organelle consists of dictionary initialComposition; diff --git a/scripts/microbe_stage/setup.as b/scripts/microbe_stage/setup.as index 4d0a624eaa8..735835f4ac6 100644 --- a/scripts/microbe_stage/setup.as +++ b/scripts/microbe_stage/setup.as @@ -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; @@ -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; @@ -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; @@ -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;