summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--inc/constants.hpp6
-rw-r--r--inc/dna.hpp3
-rw-r--r--inc/functions.hpp8
-rw-r--r--src/dna.cpp44
4 files changed, 49 insertions, 12 deletions
diff --git a/inc/constants.hpp b/inc/constants.hpp
index 518f2ac..6cb2982 100644
--- a/inc/constants.hpp
+++ b/inc/constants.hpp
@@ -22,6 +22,8 @@ const int CREATURE_EXP_PREG_TIME = 100;
const int CREATURE_EXP_AGE = 1000000;
const int CREATURE_SIZE_MAX = 10;
const float CREATURE_SPEED = .1;
+const float CREATURE_MUTATION_PERCENT = .25;
+const float CREATURE_MUTATION_CHANCE = .05;
// Resource
const int RESOURCE_SIZE_START = 1;
@@ -37,8 +39,8 @@ const float CREATURE_SIDES = 4.0;
const float RESOURCE_SIDES = 10;
// Quadtree
-const int MAX_OBJECTS = 5;
-const int MAX_LEVELS = 6;
+const int MAX_OBJECTS = 10;
+const int MAX_LEVELS = 10;
// Camera
const float MOVE_AMOUNT = .2;
diff --git a/inc/dna.hpp b/inc/dna.hpp
index d7312f6..b549c27 100644
--- a/inc/dna.hpp
+++ b/inc/dna.hpp
@@ -2,6 +2,7 @@
#define dna_h
#include "constants.hpp"
+#include "functions.hpp"
class DNA
{
@@ -20,6 +21,8 @@ class DNA
int sizeMax;
float speed;
+ float mutationPercent;
+ float mutationChance;
};
#endif
diff --git a/inc/functions.hpp b/inc/functions.hpp
index b0ecb65..ef6d555 100644
--- a/inc/functions.hpp
+++ b/inc/functions.hpp
@@ -1,6 +1,7 @@
#ifndef functions_h
#define functions_h
+#include <cstdlib>
#include <cmath>
#include "rectangle.hpp"
@@ -17,4 +18,11 @@ static float getRandom(float x){
return (-x + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/(x-(-x)))));
}
+static bool roll(float x){
+ float y = (float)(rand()%101)/100;
+ if(x >= y)
+ return true;
+ else
+ return false;
+}
#endif
diff --git a/src/dna.cpp b/src/dna.cpp
index c3ffd9c..5d3fc8f 100644
--- a/src/dna.cpp
+++ b/src/dna.cpp
@@ -11,21 +11,45 @@ DNA::DNA()
expectedPregnancyTime = CREATURE_EXP_PREG_TIME;
expectedAge = CREATURE_EXP_AGE;
sizeMax = CREATURE_SIZE_MAX;
+ mutationPercent = CREATURE_MUTATION_PERCENT;
+ mutationChance = CREATURE_MUTATION_CHANCE;
}
DNA DNA::combine(DNA D)
{
DNA N;
+
+ N.maxHealth = (maxHealth + D.maxHealth)/2;
+ N.speed = (speed + D.speed)/2;
+ N.reach = (reach + D.reach)/2;
+ N.bestSense = (bestSense + D.bestSense)/2;
+ N.bite = (bite + D.bite)/2;
+ N.amountToGrow = (amountToGrow + D.amountToGrow)/2;
+ N.expectedPregnancyTime = (expectedPregnancyTime + D.expectedPregnancyTime)/2;
+ N.expectedAge = (expectedAge + D.expectedAge)/2;
+ N.sizeMax = (sizeMax + D.sizeMax)/2;
+ N.mutationPercent = (mutationPercent + D.mutationPercent)/2;
+ N.mutationChance = (mutationChance + D.mutationChance)/2;
- N.maxHealth = (this->maxHealth + D.maxHealth)/2;
- N.speed = (this->speed + D.speed)/2;
- N.reach = (this->reach + D.reach)/2;
- N.bestSense = (this->bestSense + D.bestSense)/2;
- N.bite = (this->bite + D.bite)/2;
- N.amountToGrow = (this->amountToGrow + D.amountToGrow)/2;
- N.expectedPregnancyTime = (this->expectedPregnancyTime + D.expectedPregnancyTime)/2;
- N.expectedAge = (this->expectedAge + D.expectedAge)/2;
- N.sizeMax = (this->sizeMax + D.sizeMax)/2;
-
+ if(roll(mutationChance)){
+ float pn;
+ if(rand()%2)
+ pn = 1;
+ else
+ pn = -1;
+ switch(rand()%11){
+ case 0: N.maxHealth = abs(N.maxHealth *(N.mutationPercent+pn)); break;
+ case 1: N.speed = abs(N.speed *(N.mutationPercent+pn)); break;
+ case 2: N.reach = abs(N.reach *(N.mutationPercent+pn)); break;
+ case 3: N.bestSense = abs(N.bestSense *(N.mutationPercent+pn)); break;
+ case 4: N.bite = abs(N.bite *(N.mutationPercent+pn)); break;
+ case 5: N.amountToGrow = abs(N.amountToGrow *(N.mutationPercent+pn)); break;
+ case 6: N.expectedPregnancyTime = abs(N.expectedPregnancyTime *(N.mutationPercent+pn)); break;
+ case 7: N.expectedAge = abs(N.expectedAge *(N.mutationPercent+pn)); break;
+ case 8: N.sizeMax = abs(N.sizeMax *(N.mutationPercent+pn)); break;
+ case 9: N.mutationPercent = abs(N.expectedAge *(N.mutationPercent+pn)); break;
+ case 10:N.mutationChance = abs(N.mutationChance *(N.mutationPercent+pn)); break;
+ }
+ }
return N;
}