Skip to content

Commit

Permalink
Added mutation parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattHolliday committed May 26, 2024
1 parent a34b48b commit ace5bb0
Show file tree
Hide file tree
Showing 11 changed files with 372 additions and 190 deletions.
7 changes: 7 additions & 0 deletions SpinBoxMax.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extends SpinBox


# Changes min so cant be above max
func _value_changed(value):
var min_spin_box = get_parent().find_child("SpinBoxMin")
min_spin_box.max_value = value
7 changes: 7 additions & 0 deletions SpinBoxMin.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extends SpinBox


# Changes max so cant be below min
func _value_changed(value):
var max_spin_box = get_parent().find_child("SpinBoxMax")
max_spin_box.min_value = value
101 changes: 100 additions & 1 deletion cell_spawner.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=37 format=3 uid="uid://dm4rsl52782k2"]
[gd_scene load_steps=39 format=3 uid="uid://dm4rsl52782k2"]

[ext_resource type="PackedScene" uid="uid://b8e2qe8b5d8pw" path="res://cell.tscn" id="1_dm1x8"]
[ext_resource type="Theme" uid="uid://cnptgal3iyuy5" path="res://dark_mode.tres" id="2_d86u0"]
Expand All @@ -20,7 +20,9 @@
[ext_resource type="Script" path="res://MenuPauseCheck.gd" id="9_0ibtq"]
[ext_resource type="Script" path="res://MenuPanel.gd" id="9_i2ah6"]
[ext_resource type="Script" path="res://ExitMenuButton.gd" id="10_j1ug1"]
[ext_resource type="Script" path="res://SpinBoxMin.gd" id="10_t051x"]
[ext_resource type="Script" path="res://EXIT_TO_MENUButton.gd" id="11_md7wg"]
[ext_resource type="Script" path="res://SpinBoxMax.gd" id="11_w6gas"]
[ext_resource type="Script" path="res://Cell.gd" id="12_51nej"]
[ext_resource type="Script" path="res://SlowDownButton.gd" id="19_g2bfb"]
[ext_resource type="Texture2D" uid="uid://dghgkdthd3uwf" path="res://sprites/Buttons/FastForward.svg" id="20_udhdp"]
Expand Down Expand Up @@ -336,6 +338,19 @@ max_value = 10000.0
step = 50.0
value = 1000.0

[node name="MutationChanceContainer" type="HBoxContainer" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Cell/ScrollContainer/VBoxContainer"]
layout_mode = 2

[node name="Label" type="Label" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Cell/ScrollContainer/VBoxContainer/MutationChanceContainer"]
layout_mode = 2
text = "Base Mutation Chance"

[node name="SpinBox" type="SpinBox" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Cell/ScrollContainer/VBoxContainer/MutationChanceContainer"]
layout_mode = 2
max_value = 1.0
step = 0.01
value = 0.99

[node name="Nutrient Zone" type="VBoxContainer" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer"]
visible = false
layout_mode = 2
Expand Down Expand Up @@ -477,6 +492,22 @@ min_value = 0.1
step = 0.1
value = 0.5

[node name="mutationChanceRange" type="HBoxContainer" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Cell Membrane/ScrollContainer/VBoxContainer"]
layout_mode = 2

[node name="Label" type="Label" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Cell Membrane/ScrollContainer/VBoxContainer/mutationChanceRange"]
layout_mode = 2
text = "Mutation Chance Range"

[node name="SpinBoxMin" type="SpinBox" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Cell Membrane/ScrollContainer/VBoxContainer/mutationChanceRange"]
layout_mode = 2
script = ExtResource("10_t051x")

[node name="SpinBoxMax" type="SpinBox" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Cell Membrane/ScrollContainer/VBoxContainer/mutationChanceRange"]
layout_mode = 2
value = 15.0
script = ExtResource("11_w6gas")

[node name="Flagella" type="VBoxContainer" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer"]
visible = false
layout_mode = 2
Expand Down Expand Up @@ -591,6 +622,23 @@ min_value = 0.1
step = 0.1
value = 0.5

[node name="mutationChanceRange" type="HBoxContainer" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Flagella/ScrollContainer/VBoxContainer"]
layout_mode = 2

[node name="Label" type="Label" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Flagella/ScrollContainer/VBoxContainer/mutationChanceRange"]
layout_mode = 2
text = "Mutation Chance Range"

[node name="SpinBoxMin" type="SpinBox" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Flagella/ScrollContainer/VBoxContainer/mutationChanceRange"]
layout_mode = 2
value = 15.0
script = ExtResource("10_t051x")

[node name="SpinBoxMax" type="SpinBox" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Flagella/ScrollContainer/VBoxContainer/mutationChanceRange"]
layout_mode = 2
value = 30.0
script = ExtResource("11_w6gas")

[node name="Nucleus" type="VBoxContainer" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer"]
visible = false
layout_mode = 2
Expand Down Expand Up @@ -668,6 +716,23 @@ min_value = 0.1
step = 0.1
value = 0.5

[node name="mutationChanceRange" type="HBoxContainer" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Nucleus/ScrollContainer/VBoxContainer"]
layout_mode = 2

[node name="Label" type="Label" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Nucleus/ScrollContainer/VBoxContainer/mutationChanceRange"]
layout_mode = 2
text = "Mutation Chance Range"

[node name="SpinBoxMin" type="SpinBox" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Nucleus/ScrollContainer/VBoxContainer/mutationChanceRange"]
layout_mode = 2
value = 40.0
script = ExtResource("10_t051x")

[node name="SpinBoxMax" type="SpinBox" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Nucleus/ScrollContainer/VBoxContainer/mutationChanceRange"]
layout_mode = 2
value = 45.0
script = ExtResource("11_w6gas")

[node name="Mitochondria" type="VBoxContainer" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer"]
visible = false
layout_mode = 2
Expand Down Expand Up @@ -816,6 +881,23 @@ min_value = 0.1
step = 0.1
value = 0.5

[node name="mutationChanceRange" type="HBoxContainer" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Mitochondria/ScrollContainer/VBoxContainer"]
layout_mode = 2

[node name="Label" type="Label" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Mitochondria/ScrollContainer/VBoxContainer/mutationChanceRange"]
layout_mode = 2
text = "Mutation Chance Range"

[node name="SpinBoxMin" type="SpinBox" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Mitochondria/ScrollContainer/VBoxContainer/mutationChanceRange"]
layout_mode = 2
value = 30.0
script = ExtResource("10_t051x")

[node name="SpinBoxMax" type="SpinBox" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Mitochondria/ScrollContainer/VBoxContainer/mutationChanceRange"]
layout_mode = 2
value = 40.0
script = ExtResource("11_w6gas")

[node name="Ribosomes" type="VBoxContainer" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer"]
visible = false
layout_mode = 2
Expand Down Expand Up @@ -964,6 +1046,23 @@ min_value = 0.1
step = 0.1
value = 0.5

[node name="mutationChanceRange" type="HBoxContainer" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Ribosomes/ScrollContainer/VBoxContainer"]
layout_mode = 2

[node name="Label" type="Label" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Ribosomes/ScrollContainer/VBoxContainer/mutationChanceRange"]
layout_mode = 2
text = "Mutation Chance Range"

[node name="SpinBoxMin" type="SpinBox" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Ribosomes/ScrollContainer/VBoxContainer/mutationChanceRange"]
layout_mode = 2
value = 45.0
script = ExtResource("10_t051x")

[node name="SpinBoxMax" type="SpinBox" parent="UI/MenuPanel/TabContainer/Parameters/TabContainer/Ribosomes/ScrollContainer/VBoxContainer/mutationChanceRange"]
layout_mode = 2
value = 55.0
script = ExtResource("11_w6gas")

[node name="StatsPanel" type="Panel" parent="UI"]
visible = false
z_index = 20
Expand Down
176 changes: 88 additions & 88 deletions dark_menu.tres

Large diffs are not rendered by default.

176 changes: 88 additions & 88 deletions dark_mode.tres

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions src/cell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ void Cell::seteq(Cell *otherCell) {
_cellState->setTotalEnergy(otherCell->_cellState->getTotalEnergy());
_cellState->increaseProtectedGenes(otherCell->_cellState->getProtectedGenes());

this->setGenomeProbabilities();

// Mutate based on otherCell's mutation chances

for (int i = 0; i < otherCell->_cellState->getMutationChanceCount(); i++) {
Expand Down Expand Up @@ -466,4 +468,28 @@ void Cell::_on_cell_growth() {
cellMembrane->getSprite()->set_frame(1);
cellMembrane->getSprite()->play("activate");
}
}

void Cell::setGenomeProbabilities() {
CellSpawner *spawner = Object::cast_to<CellSpawner>(this->find_parent("CellSpawner"));
// Set gene probabilities from sim parameters
SpinBox *cellMembraneMutationChanceMinSpinBox = spawner->get_node<SpinBox>("UI/MenuPanel/TabContainer/Parameters/TabContainer/Cell Membrane/ScrollContainer/VBoxContainer/mutationChanceRange/SpinBoxMin");
SpinBox *cellMembraneMutationChanceMaxSpinBox = spawner->get_node<SpinBox>("UI/MenuPanel/TabContainer/Parameters/TabContainer/Cell Membrane/ScrollContainer/VBoxContainer/mutationChanceRange/SpinBoxMax");
this->_cellGenome.setCellMembraneChance(cellMembraneMutationChanceMinSpinBox->get_value(), cellMembraneMutationChanceMaxSpinBox->get_value());

SpinBox *flagellaMutationChanceMinSpinBox = spawner->get_node<SpinBox>("UI/MenuPanel/TabContainer/Parameters/TabContainer/Flagella/ScrollContainer/VBoxContainer/mutationChanceRange/SpinBoxMin");
SpinBox *flagellaMutationChanceMaxSpinBox = spawner->get_node<SpinBox>("UI/MenuPanel/TabContainer/Parameters/TabContainer/Flagella/ScrollContainer/VBoxContainer/mutationChanceRange/SpinBoxMax");
this->_cellGenome.setFlagellaChance(flagellaMutationChanceMinSpinBox->get_value(), flagellaMutationChanceMaxSpinBox->get_value());

SpinBox *mitochondriaMutationChanceMinSpinBox = spawner->get_node<SpinBox>("UI/MenuPanel/TabContainer/Parameters/TabContainer/Mitochondria/ScrollContainer/VBoxContainer/mutationChanceRange/SpinBoxMin");
SpinBox *mitochondriaMutationChanceMaxSpinBox = spawner->get_node<SpinBox>("UI/MenuPanel/TabContainer/Parameters/TabContainer/Mitochondria/ScrollContainer/VBoxContainer/mutationChanceRange/SpinBoxMax");
this->_cellGenome.setMitochondriaChance(mitochondriaMutationChanceMinSpinBox->get_value(), mitochondriaMutationChanceMaxSpinBox->get_value());

SpinBox *nucleusMutationChanceMinSpinBox = spawner->get_node<SpinBox>("UI/MenuPanel/TabContainer/Parameters/TabContainer/Nucleus/ScrollContainer/VBoxContainer/mutationChanceRange/SpinBoxMin");
SpinBox *nucleusMutationChanceMaxSpinBox = spawner->get_node<SpinBox>("UI/MenuPanel/TabContainer/Parameters/TabContainer/Nucleus/ScrollContainer/VBoxContainer/mutationChanceRange/SpinBoxMax");
this->_cellGenome.setNucleusChance(nucleusMutationChanceMinSpinBox->get_value(), nucleusMutationChanceMaxSpinBox->get_value());

SpinBox *ribosomeMutationChanceMinSpinBox = spawner->get_node<SpinBox>("UI/MenuPanel/TabContainer/Parameters/TabContainer/Ribosomes/ScrollContainer/VBoxContainer/mutationChanceRange/SpinBoxMin");
SpinBox *ribosomeMutationChanceMaxSpinBox = spawner->get_node<SpinBox>("UI/MenuPanel/TabContainer/Parameters/TabContainer/Ribosomes/ScrollContainer/VBoxContainer/mutationChanceRange/SpinBoxMax");
this->_cellGenome.setRibosomeChance(ribosomeMutationChanceMinSpinBox->get_value(), ribosomeMutationChanceMaxSpinBox->get_value());
}
2 changes: 2 additions & 0 deletions src/cell.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class Cell : public RigidBody2D {
String formatDecimal(float value) const;
Array getStats() const;

void setGenomeProbabilities();

private:
CellState *_cellState = NULL;
Size2 _spriteSize;
Expand Down
6 changes: 6 additions & 0 deletions src/cell_spawner.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "cell_spawner.hpp"
#include "cell.hpp"
#include "cell_environment.hpp"
#include "genome.hpp"
#include "stats.hpp"

#include <godot_cpp/core/class_db.hpp>
Expand Down Expand Up @@ -130,6 +131,11 @@ void CellSpawner::spawnCell(bool isImmortal) {
SpinBox *GrowthEnergyCostSpinBox = this->get_node<SpinBox>("UI/MenuPanel/TabContainer/Parameters/TabContainer/Cell/ScrollContainer/VBoxContainer/GrowthEnergyCostContainer/SpinBox");
cellState->setGrowthEnergyCost(GrowthEnergyCostSpinBox->get_value());

SpinBox *mutationChanceSpinBox = this->get_node<SpinBox>("UI/MenuPanel/TabContainer/Parameters/TabContainer/Cell/ScrollContainer/VBoxContainer/MutationChanceContainer/SpinBox");
cellState->addMutationChance(mutationChanceSpinBox->get_value());

cellObject->setGenomeProbabilities(); // also from simulation parameters menu

// Set Cell position to random location in viewport
cellObject->set_global_position(Vector2(
rand.randi_range(cellSize.x / 4, viewportSize.x - cellSize.x / 4),
Expand Down
1 change: 0 additions & 1 deletion src/cell_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ CellState::CellState() {
_nextMovementVector = Vector2();
_receptorVectors = Vector<Vector2>();
_mutationChances = Vector<float>();
_mutationChances.push_back(0.99);
}
CellState::~CellState() {}

Expand Down
47 changes: 35 additions & 12 deletions src/genome.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
#include "genome.hpp"
#include "cell_membrane_gene.hpp"
#include "cell_spawner.hpp"
#include "flagella_gene.hpp"
#include "mitochondria_gene.hpp"
#include "modifier_gene.hpp"
#include "nucleus_gene.hpp"
#include "ribosomes_gene.hpp"

//Gene Probs. Currently setup to be out of 100. any values not in these ranges are defaulted to a random modifier gene
const static float CELL_MEMBRANE_CHANCE[2] = { 0, 15 };
const static float FLAGELLA_CHANCE[2] = { 15, 30 };
const static float MITOCHONDRIA_CHANCE[2] = { 30, 40 };
const static float NUCLEUS_CHANCE[2] = { 40, 45 };
const static float RIBOSOME_CHANCE[2] = { 45, 55 };
#include "helpers.hpp"

#include <godot_cpp/classes/spin_box.hpp>

Genome::Genome() {
}
Expand All @@ -22,6 +20,31 @@ Genome::~Genome() {
}
}

void Genome::setCellMembraneChance(float min, float max) {
this->cell_membrane_chance[0] = min;
this->cell_membrane_chance[1] = max;
}

void Genome::setFlagellaChance(float min, float max) {
this->flagella_chance[0] = min;
this->flagella_chance[1] = max;
}

void Genome::setMitochondriaChance(float min, float max) {
this->mitochondria_chance[0] = min;
this->mitochondria_chance[1] = max;
}

void Genome::setNucleusChance(float min, float max) {
this->nucleus_chance[0] = min;
this->nucleus_chance[1] = max;
}

void Genome::setRibosomeChance(float min, float max) {
this->ribosome_chance[0] = min;
this->ribosome_chance[1] = max;
}

Vector<CellStructure *> Genome::expressGenes() {
Vector<CellStructure *> cellStructures;
CellStructure *currentStructure = nullptr;
Expand Down Expand Up @@ -76,21 +99,21 @@ Gene *Genome::GenerateRandomGene() {
RandomNumberGenerator rand = RandomNumberGenerator();
int generatedValue = rand.randf_range(0, 100);

if (generatedValue >= CELL_MEMBRANE_CHANCE[0] && generatedValue < CELL_MEMBRANE_CHANCE[1])
if (generatedValue >= this->cell_membrane_chance[0] && generatedValue < this->cell_membrane_chance[1])
return new CellMembraneGene();

if (generatedValue >= FLAGELLA_CHANCE[0] && generatedValue < FLAGELLA_CHANCE[1])
if (generatedValue >= this->flagella_chance[0] && generatedValue < this->flagella_chance[1])
return new FlagellaGene();

if (generatedValue >= MITOCHONDRIA_CHANCE[0] && generatedValue < MITOCHONDRIA_CHANCE[1])
if (generatedValue >= this->mitochondria_chance[0] && generatedValue < this->mitochondria_chance[1])
return new MitochondriaGene();

if (generatedValue >= NUCLEUS_CHANCE[0] && generatedValue < NUCLEUS_CHANCE[1])
if (generatedValue >= this->nucleus_chance[0] && generatedValue < this->nucleus_chance[1])
return new NucleusGene();

if (generatedValue >= RIBOSOME_CHANCE[0] && generatedValue < RIBOSOME_CHANCE[1])
if (generatedValue >= this->ribosome_chance[0] && generatedValue < this->ribosome_chance[1])
return new RibosomesGene();

//Default to a Random modifier gene.
return randomModifierGene();
}
}
13 changes: 13 additions & 0 deletions src/genome.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ class Genome {
Genome();
~Genome();

void setCellMembraneChance(float, float);
void setFlagellaChance(float, float);
void setMitochondriaChance(float, float);
void setNucleusChance(float, float);
void setRibosomeChance(float, float);

Vector<CellStructure *> expressGenes();

void addGene(Gene *);
Expand All @@ -25,5 +31,12 @@ class Genome {
Gene *GenerateRandomGene();

private:
//Gene Probs. Currently setup to be out of 100. any values not in these ranges are defaulted to a random modifier gene
float cell_membrane_chance[2] = { 0, 15 };
float flagella_chance[2] = { 15, 30 };
float mitochondria_chance[2] = { 30, 40 };
float nucleus_chance[2] = { 40, 45 };
float ribosome_chance[2] = { 45, 55 };

Vector<Gene *> genes;
};

0 comments on commit ace5bb0

Please sign in to comment.