Skip to content

Commit

Permalink
Apply organelle reposition before cell layout finalize to fix a crash (
Browse files Browse the repository at this point in the history
…#4672)

with modified cell types no longer fitting in the places selected for them
  • Loading branch information
hhyyrylainen authored Dec 15, 2023
1 parent 2872495 commit 67861d8
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/early_multicellular_stage/CellTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public int Orientation
public ISimulationPhotographable.SimulationType SimulationToPhotograph =>
ISimulationPhotographable.SimulationType.MicrobeGraphics;

public void RepositionToOrigin()
public bool RepositionToOrigin()
{
CellType.RepositionToOrigin();
return CellType.RepositionToOrigin();
}

public void UpdateNameIfValid(string newName)
Expand Down
5 changes: 3 additions & 2 deletions src/early_multicellular_stage/CellType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ public CellType(MicrobeSpecies microbeSpecies) : this(microbeSpecies.MembraneTyp
public ISimulationPhotographable.SimulationType SimulationToPhotograph =>
ISimulationPhotographable.SimulationType.MicrobeGraphics;

public void RepositionToOrigin()
public bool RepositionToOrigin()
{
Organelles.RepositionToOrigin();
var changes = Organelles.RepositionToOrigin();
CalculateRotationSpeed();
return changes;
}

public void UpdateNameIfValid(string newName)
Expand Down
16 changes: 14 additions & 2 deletions src/early_multicellular_stage/EarlyMulticellularSpecies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Godot;
using Newtonsoft.Json;
using Systems;

Expand Down Expand Up @@ -48,23 +49,34 @@ public override void OnEdited()
// Make certain these are all up to date
foreach (var cellType in CellTypes)
{
cellType.RepositionToOrigin();
// See the comment in CellBodyPlanEditorComponent.OnFinishEditing
if (cellType.RepositionToOrigin())
{
GD.Print(
"Repositioned an early multicellular species' cell type. This might break / crash the " +
"body plan layout.");
}
}
}

public override void RepositionToOrigin()
public override bool RepositionToOrigin()
{
// TODO: should this actually reposition things as the cell at index 0 is always the colony leader so if it
// isn't centered, that'll cause issues?
// var centerOfMass = Cells.CenterOfMass;

var centerOfMass = Cells[0].Position;

if (centerOfMass.Q == 0 && centerOfMass.R == 0)
return false;

foreach (var cell in Cells)
{
// This calculation aligns the center of mass with the origin by moving every organelle of the microbe.
cell.Position -= centerOfMass;
}

return true;
}

public override void UpdateInitialCompounds()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,18 @@ public override void OnFinishEditing()
{
var editedSpecies = Editor.EditedSpecies;

// Note that for the below calculations to work all cell types need to be positioned correctly. So we need
// to force that to happen here first. This also ensures that the skipped positioning to origin of the cell
// editor component (that is used as a special mode in multicellular) is performed.
foreach (var cellType in editedSpecies.CellTypes)
{
cellType.RepositionToOrigin();
}

// Compute final cell layout positions and update the species
// TODO: maybe in the future we want to switch to editing the full hex layout with the entire cells in this
// editor so this step can be skipped
// editor so this step can be skipped. Or another approach that keeps the shape the player worked on better
// than this approach that can move around the cells a lot.
editedSpecies.Cells.Clear();

foreach (var hexWithData in editedMicrobeCells)
Expand Down
3 changes: 2 additions & 1 deletion src/general/Species.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ public virtual void OnEdited()
/// <summary>
/// Repositions the structure of the species according to stage specific rules
/// </summary>
public abstract void RepositionToOrigin();
/// <returns>True when repositioning happened, false if this was already positioned correctly</returns>
public abstract bool RepositionToOrigin();

public void SetPopulationFromPatches(long population)
{
Expand Down
4 changes: 2 additions & 2 deletions src/late_multicellular_stage/LateMulticellularSpecies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public override void OnEdited()
SetTypeFromBrainPower();
}

public override void RepositionToOrigin()
public override bool RepositionToOrigin()
{
BodyLayout.RepositionToGround();
return BodyLayout.RepositionToGround();
}

public override void UpdateInitialCompounds()
Expand Down
8 changes: 7 additions & 1 deletion src/late_multicellular_stage/MulticellularMetaballLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class MulticellularMetaballLayout : MetaballLayout<MulticellularMetaball>
/// Repositions the bottom-most metaballs to touch the ground, and the center of all metaballs to be aligned
/// above 0,0
/// </summary>
public void RepositionToGround()
public bool RepositionToGround()
{
float lowestCoordinate = 0;
var center = Vector3.Zero;
Expand All @@ -25,9 +25,15 @@ public void RepositionToGround()
var adjustment = center / Count;
adjustment.y = lowestCoordinate;

// Should this allow slight movement?
if (adjustment.x == 0 && adjustment.y == 0 && adjustment.z == 0)
return false;

foreach (var metaball in this)
{
metaball.Position -= adjustment;
}

return true;
}
}
3 changes: 2 additions & 1 deletion src/microbe_stage/ICellProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public interface ICellProperties : ISimulationPhotographable
/// <summary>
/// Repositions the cell to the origin and recalculates any properties dependant on its position.
/// </summary>
public void RepositionToOrigin();
/// <returns>True when changes were made, false if everything was positioned well already</returns>
public bool RepositionToOrigin();

public void UpdateNameIfValid(string newName);
}
Expand Down
5 changes: 3 additions & 2 deletions src/microbe_stage/MicrobeSpecies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ public override void OnEdited()
UpdateInitialCompounds();
}

public override void RepositionToOrigin()
public override bool RepositionToOrigin()
{
Organelles.RepositionToOrigin();
var changes = Organelles.RepositionToOrigin();
CalculateRotationSpeed();
return changes;
}

public override void UpdateInitialCompounds()
Expand Down
8 changes: 7 additions & 1 deletion src/microbe_stage/OrganelleLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,21 @@ public bool CanPlaceAndIsTouching(T organelle,
return IsTouchingExistingHex(organelle) || (allowReplacingLastCytoplasm && IsReplacingLast(organelle));
}

public void RepositionToOrigin()
public bool RepositionToOrigin()
{
var centerOfMass = CenterOfMass;

// Skip if center of mass is already correct
if (centerOfMass.Q == 0 && centerOfMass.R == 0)
return false;

foreach (var organelle in Organelles)
{
// This calculation aligns the center of mass with the origin by moving every organelle of the microbe.
organelle.Position -= centerOfMass;
}

return true;
}

protected override IEnumerable<Hex> GetHexComponentPositions(T hex)
Expand Down

0 comments on commit 67861d8

Please sign in to comment.