diff options
author | majortom6 <tombarrett@siu.edu> | 2017-02-26 11:14:31 -0600 |
---|---|---|
committer | Tom Barrett <tombarrett@siu.edu> | 2017-03-07 13:23:42 -0600 |
commit | 7394b069537ed7a490a343381d62862eb22abdcf (patch) | |
tree | c8bff4f88cf962ec4148a2e133bf959035feedb7 /src | |
parent | e54170cfb8c0fb99ecdc3b1e57e832dec58ee76e (diff) |
-REMOVED ENTITY, RESOURCE, AND CREATURE !
-replaced them all with one class, organism (always subject to change)
-the dna type now is what differs creatures and resources
-removed dead constants
-may be a rogue segfault
-also weird artifacts start showing if running long
Diffstat (limited to 'src')
-rw-r--r-- | src/dna.cpp | 57 | ||||
-rw-r--r-- | src/entity.cpp | 7 | ||||
-rw-r--r-- | src/list.cpp | 50 | ||||
-rw-r--r-- | src/main.cpp | 4 | ||||
-rw-r--r-- | src/organism.cpp (renamed from src/creature.cpp) | 64 | ||||
-rw-r--r-- | src/quadtree.cpp | 8 | ||||
-rw-r--r-- | src/resource.cpp | 30 |
7 files changed, 108 insertions, 112 deletions
diff --git a/src/dna.cpp b/src/dna.cpp index 5d3fc8f..5d727d2 100644 --- a/src/dna.cpp +++ b/src/dna.cpp @@ -1,33 +1,46 @@ #include "dna.hpp" -DNA::DNA() +DNA::DNA(std::string s) { - maxHealth = CREATURE_MAX_HEALTH; - speed = CREATURE_SPEED; - reach = CREATURE_REACH; - bestSense = CREATURE_BEST_SENSE; - bite = CREATURE_BITE; - amountToGrow = CREATURE_AMOUNT_TO_GROW; - expectedPregnancyTime = CREATURE_EXP_PREG_TIME; - expectedAge = CREATURE_EXP_AGE; - sizeMax = CREATURE_SIZE_MAX; - mutationPercent = CREATURE_MUTATION_PERCENT; - mutationChance = CREATURE_MUTATION_CHANCE; + if(s == "creature"){ + type = CREATURE_TYPE; + maxHealth = CREATURE_MAX_HEALTH; + bestSense = CREATURE_BEST_SENSE; + bite = CREATURE_BITE; + expectedPregnancyTime = CREATURE_EXP_PREG_TIME; + expectedAge = CREATURE_EXP_AGE; + growAmount = 0; + reach = CREATURE_REACH; + speed = CREATURE_SPEED; + mutationPercent = CREATURE_MUTATION_PERCENT; + mutationChance = CREATURE_MUTATION_CHANCE; + } + else if(s == "resource"){ + type = RESOURCE_TYPE; + maxHealth = RESOURCE_MAX_HEALTH; + bestSense = 0; + bite = 0; + expectedPregnancyTime = 0; + expectedAge = 0; + growAmount = RESOURCE_GROW_AMOUNT; + reach = 0; + speed = 0; + mutationPercent = 0; + mutationChance = 0; + } } DNA DNA::combine(DNA D) { - DNA N; - + DNA N("empty"); + N.type = CREATURE_TYPE; 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; @@ -37,18 +50,16 @@ DNA DNA::combine(DNA D) pn = 1; else pn = -1; - switch(rand()%11){ + switch(rand()%9){ 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; + case 5: N.expectedPregnancyTime = abs(N.expectedPregnancyTime *(N.mutationPercent+pn)); break; + case 6: N.expectedAge = abs(N.expectedAge *(N.mutationPercent+pn)); break; + case 7: N.mutationPercent = abs(N.expectedAge *(N.mutationPercent+pn)); break; + case 8: N.mutationChance = abs(N.mutationChance *(N.mutationPercent+pn)); break; } } return N; diff --git a/src/entity.cpp b/src/entity.cpp deleted file mode 100644 index ab94d23..0000000 --- a/src/entity.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "entity.hpp" - -void Entity::Place() -{ - gfxData.x = rect.x; - gfxData.y = rect.y; -} diff --git a/src/list.cpp b/src/list.cpp index 30b0eb1..826208e 100644 --- a/src/list.cpp +++ b/src/list.cpp @@ -3,16 +3,17 @@ List::List() { int i; - DNA d; + DNA d = DNA("creature"); Rectangle tmp; for(i=0;i<CREATURES;i++){ - Creature X(tmp,d); + Organism X(tmp,d); creatures.push_back(X); } - + + d = DNA("resource"); for(i=0;i<RESOURCES;i++){ - Resource Y(tmp); - resources.push_back(Y); + Organism X(tmp, d); + resources.push_back(X); } Rectangle R1 = Rectangle(0,0,60,60); @@ -21,33 +22,34 @@ List::List() void List::Remove() { - for(std::list<Creature>::iterator it = creatures.begin(); it!= creatures.end(); it++) + for(std::list<Organism>::iterator it = creatures.begin(); it!= creatures.end(); it++) if(it->getHealth()<=0){ - Resource Y(it->getRectangle()); - resources.push_back(Y); + DNA d = DNA("resource"); + Organism X(it->getRectangle(), d); + resources.push_back(X); creatures.erase(it--); } - for(std::list<Resource>::iterator it = resources.begin(); it!= resources.end(); it++) - if(it->getAmount()<=0) + for(std::list<Organism>::iterator it = resources.begin(); it!= resources.end(); it++) + if(it->getHealth()<=0) resources.erase(it--); } void List::Behavior() { - for(std::list<Creature>::iterator it = creatures.begin(); it!= creatures.end(); it++){ - std::vector<Entity*> N = getNear(*it); - it->giveNearMe(N); + for(std::list<Organism>::iterator it = creatures.begin(); it!= creatures.end(); it++){ + std::vector<Organism*> near = getNear(*it); + it->giveNearMe(near); it->Behavior(); if(it->getPregnancyReady()){ - Creature X(it->getRectangle(),it->getChildsDNA()); + Organism X(it->getRectangle(),it->getChildsDNA()); creatures.push_back(X); it->hadPregnancy(); } } - for(std::list<Resource>::iterator it = resources.begin(); it!= resources.end(); it++) + for(std::list<Organism>::iterator it = resources.begin(); it!= resources.end(); it++) it->grow(); } @@ -56,35 +58,37 @@ void List::Place() tree.clear(); Rectangle tmp; + DNA d = DNA("resource"); while(resources.size() < MINIMUM_RESOURCES){ - Resource Y(tmp); - resources.push_back(Y); + Organism X(tmp, d); + resources.push_back(X); } - for(std::list<Creature>::iterator it = creatures.begin(); it!= creatures.end(); it++){ + for(std::list<Organism>::iterator it = creatures.begin(); it!= creatures.end(); it++){ it->Place(); tree.insert(&(*it));; } - for(std::list<Resource>::iterator it = resources.begin(); it!=resources.end(); it++){ + for(std::list<Organism>::iterator it = resources.begin(); it!=resources.end(); it++){ it->Place(); tree.insert(&(*it));; } } -std::vector<Entity*> List::getNear(Creature c) +std::vector<Organism*> List::getNear(Organism c) { - std::vector<Entity*> near; + std::vector<Organism*> near; near = tree.retrieve(near, c.getGFXD()); - for(std::vector<Entity*>::iterator it = near.begin(); it!= near.end(); it++) + for(std::vector<Organism*>::iterator it = near.begin(); it!= near.end(); it++) if(c.getBestSense() < Distance(c.getRectangle(),(*it)->getRectangle())) near.erase(it--); return near; } -std::vector<GraphicsData> List::drawQuadTree(){ +std::vector<GraphicsData> List::drawQuadTree() +{ return tree.Draw(); } diff --git a/src/main.cpp b/src/main.cpp index d5a192d..43ec1e4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,10 +47,10 @@ int main() shader.Bind();
_spriteBatch.begin();
- for(std::list<Creature>::iterator it = L.creatures.begin(); it != L.creatures.end(); it++)
+ for(std::list<Organism>::iterator it = L.creatures.begin(); it != L.creatures.end(); it++)
_spriteBatch.draw(it->getGFXD());
- for(std::list<Resource>::iterator it = L.resources.begin(); it != L.resources.end(); it++)
+ for(std::list<Organism>::iterator it = L.resources.begin(); it != L.resources.end(); it++)
_spriteBatch.draw(it->getGFXD());
_spriteBatch.end();
diff --git a/src/creature.cpp b/src/organism.cpp index 9cfdacb..27f6f8a 100644 --- a/src/creature.cpp +++ b/src/organism.cpp @@ -1,6 +1,6 @@ -#include "creature.hpp" +#include "organism.hpp" -Creature::Creature(Rectangle r, DNA d) +Organism::Organism(Rectangle r, DNA d) { rect = r; myDNA = d; @@ -10,7 +10,6 @@ Creature::Creature(Rectangle r, DNA d) rect.y = getRandom(30); } - type = CREATURE_TYPE; health = myDNA.maxHealth/2; gender = rand() % 2; age = 0; @@ -18,14 +17,19 @@ Creature::Creature(Rectangle r, DNA d) pregnancyReady = false; pregnate = false; hasTarget = false; + wander = false; - if(gender) - gfxData = GraphicsData(rect.x, rect.y, 1, 0, 0, CREATURE_SIDES); + if(myDNA.type == CREATURE_TYPE){ + if(gender) + gfxData = GraphicsData(rect.x, rect.y, 1, 0, 0, CREATURE_SIDES); + else + gfxData = GraphicsData(rect.x, rect.y, 0, 0, 1, CREATURE_SIDES); + } else - gfxData = GraphicsData(rect.x, rect.y, 0, 0, 1, CREATURE_SIDES); + gfxData = GraphicsData(rect.x, rect.y, 0, 1, 0, RESOURCE_SIDES); } -void Creature::Behavior() +void Organism::Behavior() { health-=1; @@ -49,7 +53,7 @@ void Creature::Behavior() health = 0; } -void Creature::Priority() +void Organism::Priority() { if(health < myDNA.maxHealth / 2){ hungry = true; @@ -61,11 +65,11 @@ void Creature::Priority() } } -void Creature::setTarget() +void Organism::setTarget() { std::random_shuffle(nearMe.begin(),nearMe.end()); - for(std::vector <Entity*>::iterator it = nearMe.begin(); it!=nearMe.end(); it++){ + for(std::vector<Organism*>::iterator it = nearMe.begin(); it!=nearMe.end(); it++){ if( (*it)->getType() == RESOURCE_TYPE && hungry){ target = *it; hasTarget = true; @@ -82,16 +86,14 @@ void Creature::setTarget() if(!hasTarget&&!wander){ wander = true; - Rectangle tmp; - tmp.x = getRandom(30); - tmp.y = getRandom(30); + Rectangle tmp(getRandom(30),getRandom(30),0,0); wTarget = tmp; } } -void Creature::checkTarget() +void Organism::checkTarget() { - for(std::vector <Entity*>::iterator it = nearMe.begin(); it!=nearMe.end(); it++) + for(std::vector<Organism*>::iterator it = nearMe.begin(); it!=nearMe.end(); it++) if( target == *it ) return; @@ -99,20 +101,19 @@ void Creature::checkTarget() } -void Creature::Action() +void Organism::Action() { if(hasTarget){ if(Distance(rect,target->getRectangle()) < myDNA.reach){ if(target->getType() == RESOURCE_TYPE){ - target->eat(myDNA.bite); + target->takeBite(myDNA.bite); health+=myDNA.bite; - amountAte++; - if(target->getAmount()<=0) + if(target->getHealth()<=0) hasTarget = false; } else if (target->getType() == CREATURE_TYPE){ if(target->getGender() != gender){ - target->impregnate(myDNA); + target->passDNA(myDNA); } hasTarget = false; } @@ -129,7 +130,7 @@ void Creature::Action() } } -void Creature::moveTowards(Rectangle t) +void Organism::moveTowards(Rectangle t) { if( rect.x == t.x ){ if( rect.y < t.y ) @@ -165,11 +166,28 @@ void Creature::moveTowards(Rectangle t) } } -void Creature::impregnate(DNA D) +void Organism::passDNA(DNA d) { if(!pregnate){ pregnate = true; pregnancyTime = 0; - childsDNA = myDNA.combine(D); + childsDNA = myDNA.combine(d); } } + +void Organism::grow() +{ + if(health < myDNA.maxHealth) + health+=myDNA.growAmount; +} + +void Organism::takeBite(int bite) +{ + health-=bite; +} + +void Organism::Place() +{ + gfxData.x = rect.x; + gfxData.y = rect.y; +} diff --git a/src/quadtree.cpp b/src/quadtree.cpp index 909c875..ada839a 100644 --- a/src/quadtree.cpp +++ b/src/quadtree.cpp @@ -76,7 +76,7 @@ int Quadtree::getIndex(GraphicsData object) { return index;
}
-void Quadtree::insert(Entity* iter){
+void Quadtree::insert(Organism* iter){
if (!nodes[0].isNull) {
int index = getIndex((*iter).getGFXD());
if (index != -1) {
@@ -92,7 +92,7 @@ void Quadtree::insert(Entity* iter){ split();
int index;
- for(std::vector<Entity*>::iterator it = objects.begin(); it!=objects.end();it++){
+ for(std::vector<Organism*>::iterator it = objects.begin(); it!=objects.end();it++){
index = getIndex((*it)->getGFXD());
if (index != -1) {
nodes[index].insert(*it);
@@ -126,12 +126,12 @@ std::vector<GraphicsData> Quadtree::Draw(){ return retdat;
}
-std::vector<Entity*> Quadtree::retrieve(std::vector<Entity*> returnObjects, GraphicsData obj) {
+std::vector<Organism*> Quadtree::retrieve(std::vector<Organism*> returnObjects, GraphicsData obj) {
int index = getIndex(obj);
if (index != -1 && !nodes[0].isNull)
returnObjects = nodes[index].retrieve(returnObjects, obj);
- for(std::vector<Entity*>::iterator it = objects.begin(); it!=objects.end(); it++)
+ for(std::vector<Organism*>::iterator it = objects.begin(); it!=objects.end(); it++)
returnObjects.emplace_back(*it);
return returnObjects;
diff --git a/src/resource.cpp b/src/resource.cpp deleted file mode 100644 index c3e61d6..0000000 --- a/src/resource.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include "resource.hpp" - -Resource::Resource(Rectangle r) -{ - rect = r; - - if(rect.x == 0 && rect.y == 0){ - rect.x = getRandom(30); - rect.y = getRandom(30); - } - - gfxData = GraphicsData(rect.x, rect.y, 0, 1, 0, RESOURCE_SIDES); - - type = RESOURCE_TYPE; - amount = RESOURCE_AMOUNT_START; - growAmount = RESOURCE_GROW; -} - -void Resource::eat(int bite) -{ - amount-=bite; -} - -void Resource::grow() -{ - if(amount < RESOURCE_AMOUNT_MAX){ - amount+=growAmount; - rect.h = rect.w = map(amount,0,RESOURCE_AMOUNT_MAX,0,RESOURCE_SIZE_MAX); - } -} |