From aa5c31be6ec6a688e8d3a66c770ba99be8e060c9 Mon Sep 17 00:00:00 2001 From: tom Date: Sun, 12 Feb 2017 05:59:12 -0600 Subject: -minor constant changes -implemented dna structure, which carries the creatures attributes suchas speed and reach -currently a child inherits 100% the same dna as the mother -removed sing namespace std --- inc/constants.hpp | 44 +++++++++++++++--------------- inc/creature.hpp | 23 +++++++--------- inc/dna.hpp | 21 +++++++++++++++ inc/list.hpp | 6 ++--- inc/window.hpp | 4 +-- src/creature.cpp | 81 +++++++++++++++++++++++-------------------------------- src/dna.cpp | 14 ++++++++++ src/list.cpp | 33 ++++++++++++----------- 8 files changed, 121 insertions(+), 105 deletions(-) create mode 100644 inc/dna.hpp create mode 100644 src/dna.cpp diff --git a/inc/constants.hpp b/inc/constants.hpp index 5ec55b2..fd3413b 100644 --- a/inc/constants.hpp +++ b/inc/constants.hpp @@ -1,31 +1,29 @@ #ifndef constants_h #define constants_h -const int CREATURES = 10; -const int RESOURCES = 100; -const int MINIMUM_RESOURCES = 80; -const int WINDOW_X = 500; -const int WINDOW_Y = 500; +const int CREATURES = 10; +const int RESOURCES = 100; +const int MINIMUM_RESOURCES = 80; +const int WINDOW_X = 500; +const int WINDOW_Y = 500; -const int CREATURE_TYPE = 1; -const int RESOURCE_TYPE = 2; +const int CREATURE_TYPE = 1; +const int RESOURCE_TYPE = 2; -const int CREATURE_START_HEALTH = 500; -const int CREATURE_MAX_HEALTH = 1000; -const int CREATURE_BEST_SENSE = 100; -const int CREATURE_SPEED = 1; -const int CREATURE_REACH = 5; -const int CREATURE_SIZE_MAX = 10; -const int CREATURE_SIZE_START = 5; -const int CREATURE_BITE = 10; -const int CREATURE_AMOUNT_TO_GROW = 50; -const int CREATURE_EXPECTED_PREGNANCY_TIME = 100; -const int CREATURE_EXPECTED_AGE = 100000; +const int CREATURE_MAX_HEALTH = 1000; +const int CREATURE_SPEED = 1; +const int CREATURE_REACH = 5; +const int CREATURE_BEST_SENSE = 100; +const int CREATURE_BITE = 10; +const int CREATURE_AMOUNT_TO_GROW = 50; +const int CREATURE_EXP_PREG_TIME = 100; +const int CREATURE_EXP_AGE = 1000000; +const int CREATURE_SIZE_MAX = 10; -const int RESOURCE_SIZE_START = 1; -const int RESOURCE_SIZE_MAX = 4; -const int RESOURCE_AMOUNT_START = 100; -const int RESOURCE_AMOUNT_MAX = 200; -const int RESOURCE_GROW = 1; +const int RESOURCE_SIZE_START = 1; +const int RESOURCE_SIZE_MAX = 4; +const int RESOURCE_AMOUNT_START = 100; +const int RESOURCE_AMOUNT_MAX = 200; +const int RESOURCE_GROW = 1; #endif diff --git a/inc/creature.hpp b/inc/creature.hpp index 1676be0..95b1329 100644 --- a/inc/creature.hpp +++ b/inc/creature.hpp @@ -7,11 +7,12 @@ #include "entity.hpp" #include "constants.hpp" #include "functions.hpp" +#include "dna.hpp" class Creature: public Entity { public: - Creature(Window m, SDL_Rect R); + Creature(Window M, SDL_Rect R, Dna D); void Behavior(); void Action(); void Priority(); @@ -19,31 +20,25 @@ class Creature: public Entity void checkTarget(); void Move(SDL_Rect R); void impregnate(); - void giveN(list n){N = n;}; + void giveN(std::list n){N = n;}; + Dna getDNA(){return mine;}; int getHealth(){return health;}; - int getBestSense(){return bestSense;}; + int getBestSense(){return mine.bestSense;}; bool getGender(){return gender;}; bool getPregnancyReady(){return pregnancyReady;}; void hadPregnancy(){pregnate = pregnancyReady = false;}; private: - SDL_Rect wTarget; - Entity *target; - list N; + SDL_Rect wTarget; + Entity *target; + std::list N; + Dna mine; int health; - int reach; - int maxHealth; - int speed; - int bestSense; - int bite; int amountAte; - int amountToGrow; int pregnancyTime; - int expectedPregnancyTime; int age; - int expectedAge; bool hungry; bool pregnancyReady; diff --git a/inc/dna.hpp b/inc/dna.hpp new file mode 100644 index 0000000..4924e15 --- /dev/null +++ b/inc/dna.hpp @@ -0,0 +1,21 @@ +#ifndef dna_h +#define dna_h + +#include "constants.hpp" + +class Dna +{ + public: + Dna(); + int maxHealth; + int speed; + int reach; + int bestSense; + int bite; + int amountToGrow; + int expectedPregnancyTime; + int expectedAge; + int sizeMax; +}; + +#endif diff --git a/inc/list.hpp b/inc/list.hpp index cc78031..7d3d8a2 100644 --- a/inc/list.hpp +++ b/inc/list.hpp @@ -17,12 +17,12 @@ class List void Behavior(); void Place(); void Remove(); - list getNear(Creature C); + std::list getNear(Creature C); private: Window main = Window("do not create new window."); - list R; - list C; + std::list R; + std::list C; }; #endif diff --git a/inc/window.hpp b/inc/window.hpp index 1a1a5a7..b7ec500 100644 --- a/inc/window.hpp +++ b/inc/window.hpp @@ -6,13 +6,11 @@ #include "constants.hpp" -using namespace std; - class Window { public: Window(); - Window(string){}; + Window(std::string){}; void Destroy(); void Clear(); diff --git a/src/creature.cpp b/src/creature.cpp index 9e51b04..7a86af9 100644 --- a/src/creature.cpp +++ b/src/creature.cpp @@ -1,34 +1,21 @@ #include "creature.hpp" -Creature::Creature(Window m, SDL_Rect R) +Creature::Creature(Window M, SDL_Rect R, Dna D) { - renderer = m.getRenderer(); - rect = R; + renderer = M.getRenderer(); + rect = R; + mine = D; if(rect.x == 0 && rect.y == 0){ - rect.x = rand()%WINDOW_X; - rect.y = rand()%WINDOW_Y; + rect.x = rand()%WINDOW_X; + rect.y = rand()%WINDOW_Y; } - type = CREATURE_TYPE; - health = CREATURE_START_HEALTH; - maxHealth = CREATURE_MAX_HEALTH; - reach = CREATURE_REACH; - speed = CREATURE_SPEED; - bestSense = CREATURE_BEST_SENSE; - bite = CREATURE_BITE; - amountToGrow = CREATURE_AMOUNT_TO_GROW; - expectedPregnancyTime = CREATURE_EXPECTED_PREGNANCY_TIME; - expectedAge = CREATURE_EXPECTED_AGE; - - gender = rand() % 2; - age = 0; - hungry = false; - hasTarget = false; - wander = false; - pregnate = false; - pregnancyReady = false; - able = true; + type = CREATURE_TYPE; + health = mine.maxHealth/2; + gender = rand() % 2; + able = true; + pregnancyReady = false; } void Creature::Behavior() @@ -46,18 +33,18 @@ void Creature::Behavior() if(pregnate){ pregnancyTime++; - if(pregnancyTime > expectedPregnancyTime) + if(pregnancyTime > mine.expectedPregnancyTime) pregnancyReady = true; } age++; - if(age > expectedAge) + if(age > mine.expectedAge) health = 0; } void Creature::Priority() { - if(health < maxHealth/2){ + if(health < mine.maxHealth / 2){ hungry = true; able = false; } @@ -69,7 +56,7 @@ void Creature::Priority() void Creature::setTarget() { - for(list ::iterator it = N.begin(); it!=N.end(); it++){ + for(std::list ::iterator it = N.begin(); it!=N.end(); it++){ if( (*it)->getType() == RESOURCE_TYPE && hungry){ target = *it; hasTarget = true; @@ -93,7 +80,7 @@ void Creature::setTarget() void Creature::checkTarget() { - for(list ::iterator it = N.begin(); it!=N.end(); it++) + for(std::list ::iterator it = N.begin(); it!=N.end(); it++) if( target == *it ) return; @@ -105,18 +92,18 @@ void Creature::checkTarget() void Creature::Action() { if(hasTarget){ - if( Distance(rect,target->getRect()) < reach && target->getType() == RESOURCE_TYPE){ - target->eat(bite); - health+=bite; + if( Distance(rect,target->getRect()) < mine.reach && target->getType() == RESOURCE_TYPE){ + target->eat(mine.bite); + health+=mine.bite; amountAte++; - if(rect.w <= CREATURE_SIZE_MAX && amountToGrow <= amountAte){ + if(rect.w <= mine.sizeMax && mine.amountToGrow <= amountAte){ amountAte = 0; rect.w = rect.h = rect.w + 1; } if(target->getAmount()<=0) hasTarget = false; } - else if( Distance(rect,target->getRect()) < reach && target->getType() == CREATURE_TYPE && target->getGender() != gender ){ + else if( Distance(rect,target->getRect()) < mine.reach && target->getType() == CREATURE_TYPE && target->getGender() != gender ){ target->impregnate(); hasTarget = false; } @@ -124,7 +111,7 @@ void Creature::Action() Move(target->getRect()); } else if(wander){ - if(Distance(rect,wTarget) < reach) + if(Distance(rect,wTarget) < mine.reach) wander = false; else Move(wTarget); @@ -135,34 +122,34 @@ void Creature::Move(SDL_Rect R) { if( rect.x == R.x ){ if( rect.y < R.y ) - rect.y+=speed; + rect.y+=mine.speed; else - rect.y-=speed; + rect.y-=mine.speed; } else if( rect.y == R.y ){ if( rect.x < R.x ) - rect.x+=speed; + rect.x+=mine.speed; else - rect.x-=speed; + rect.x-=mine.speed; } else if( rect.x < R.x ){ if( rect.y < R.y ){ - rect.x+=speed; - rect.y+=speed; + rect.x+=mine.speed; + rect.y+=mine.speed; } else{ - rect.x+=speed; - rect.y-=speed; + rect.x+=mine.speed; + rect.y-=mine.speed; } } else if ( rect.x > R.x ){ if( rect.y < R.y ){ - rect.x-=speed; - rect.y+=speed; + rect.x-=mine.speed; + rect.y+=mine.speed; } else{ - rect.x-=speed; - rect.y-=speed; + rect.x-=mine.speed; + rect.y-=mine.speed; } } } diff --git a/src/dna.cpp b/src/dna.cpp new file mode 100644 index 0000000..36eda3d --- /dev/null +++ b/src/dna.cpp @@ -0,0 +1,14 @@ +#include "dna.hpp" + +Dna::Dna() +{ + 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; +} diff --git a/src/list.cpp b/src/list.cpp index d49f97a..edf3063 100644 --- a/src/list.cpp +++ b/src/list.cpp @@ -5,9 +5,10 @@ List::List(Window m) main = m; int i; - SDL_Rect Rect = {0,0,CREATURE_SIZE_START,CREATURE_SIZE_START}; + Dna defaultDNA; + SDL_Rect Rect = {0,0,defaultDNA.sizeMax/5,defaultDNA.sizeMax/5}; for(i=0;i::iterator it = C.begin(); it!=C.end(); it++) + for(std::list::iterator it = C.begin(); it!=C.end(); it++) if(it->getHealth()<=0){ SDL_Rect Rect = it->getRect(); Resource r = Resource(main,Rect); @@ -29,27 +30,29 @@ void List::Remove() C.erase(it--); } - for(list::iterator it = R.begin(); it!=R.end(); it++) + for(std::list::iterator it = R.begin(); it!=R.end(); it++) if(it->getAmount()<=0) R.erase(it--); } void List::Behavior() { - for(list::iterator it = C.begin(); it!=C.end(); it++){ - list N = getNear(*it); + for(std::list::iterator it = C.begin(); it!=C.end(); it++){ + std::list N = getNear(*it); it->giveN(N); it->Behavior(); + if(it->getPregnancyReady()){ + Dna D = it->getDNA(); SDL_Rect Rect = it->getRect(); - Rect.h = Rect.w = CREATURE_SIZE_START; - Creature X(main,Rect); + Rect.h = Rect.w = D.sizeMax / 5; + Creature X(main,Rect,D); C.push_back(X); it->hadPregnancy(); } } - for(list::iterator it = R.begin(); it!=R.end(); it++) + for(std::list::iterator it = R.begin(); it!=R.end(); it++) it->grow(); } @@ -61,22 +64,22 @@ void List::Place() R.push_back(Y); } - for(list::iterator it = C.begin(); it!=C.end(); it++) + for(std::list::iterator it = C.begin(); it!=C.end(); it++) it->Place(); - for(list::iterator it = R.begin(); it!=R.end(); it++) + for(std::list::iterator it = R.begin(); it!=R.end(); it++) it->Place(); } -list List::getNear(Creature nC) +std::list List::getNear(Creature nC) { - list N; + std::list N; - for(list ::iterator it = R.begin(); it!=R.end(); it++) + for(std::list::iterator it = R.begin(); it!=R.end(); it++) if( nC.getBestSense() > Distance(nC.getRect(),it->getRect()) ) N.push_back(&(*it)); - for(list ::iterator it = C.begin(); it!=C.end(); it++) + for(std::list::iterator it = C.begin(); it!=C.end(); it++) if( &nC == &(*it)) continue; else if( nC.getBestSense() > Distance(nC.getRect(),it->getRect()) ) -- cgit v1.2.3