From 8d9ad3eb74bc0dfc647abf03b7b99ed16f2a9115 Mon Sep 17 00:00:00 2001 From: tom Date: Mon, 2 May 2016 21:24:02 -0500 Subject: implementing better modularity, came into bad bug. All creatures seem to have same near vector. --- Makefile | 4 +- inc/creature.hpp | 17 +++----- inc/entity.hpp | 3 ++ inc/list.hpp | 3 +- src/creature.cpp | 126 ++++++++++++++++--------------------------------------- src/list.cpp | 46 +++++++++++--------- 6 files changed, 75 insertions(+), 124 deletions(-) diff --git a/Makefile b/Makefile index a47226d..4fc413f 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,11 @@ OBJS = src/*cpp DEPS = inc/ -CC = g++ +CC = g++ COMPILER_FLAGS = -w -I$(DEPS) -LINKER_FLAGS = -lSDL2 -lSDL2_image -std=c++11 +LINKER_FLAGS = -g -lm -lSDL2 -lSDL2_image -std=c++11 all : $(OBJS) $(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o natures diff --git a/inc/creature.hpp b/inc/creature.hpp index ffff888..082e8d9 100644 --- a/inc/creature.hpp +++ b/inc/creature.hpp @@ -9,12 +9,11 @@ class Creature: public Entity { public: Creature(Window m, std::string s); - int Behavior(); - bool Action(); + void Behavior(); + void Action(); void Priority(); - void giveR(vector n){nR=n;}; - void giveC(vector n){nC=n;}; - + void giveN(vector n){N = n;}; + Location getLocation(){return L;}; double Distance(Location A, Location B){return sqrt(pow(A.x-B.x,2)+pow(A.y-B.y,2));}; int getHealth(){return health;}; @@ -22,9 +21,6 @@ class Creature: public Entity int getBestSense(){return bestSense;}; private: - int xTarget; - int yTarget; - bool hasTarget; bool wandering; @@ -34,10 +30,9 @@ class Creature: public Entity int speed = 1; bool able; int bestSense = 100; - - vector nR; //vector containing resources near the creature - vector nC; //vector containing creatures near the creature + vector N; + Entity *target; int n; }; diff --git a/inc/entity.hpp b/inc/entity.hpp index 0f35e86..540e7b9 100644 --- a/inc/entity.hpp +++ b/inc/entity.hpp @@ -9,7 +9,10 @@ class Entity public: void Place(); SDL_Texture* loadTexture(std::string path, Window main); + Location getLocation(){return L;}; int getType(){return type;}; + virtual void eat(void) {}; + virtual int getAmount(void) {}; protected: Location L; diff --git a/inc/list.hpp b/inc/list.hpp index 14a6a9a..9d05fec 100644 --- a/inc/list.hpp +++ b/inc/list.hpp @@ -12,7 +12,8 @@ class List List(Window m); void Behavior(); void Place(); - double Distance(Location A, Location B){return sqrt(pow(A.x-B.x,2)+pow(A.y-B.y,2));}; + double Distance(Location A, Location B){return sqrt(pow(A.x-B.x,2)+pow(A.y-B.y,2));}; + vector getNear(Creature C); private: //vectors containing objects of each type diff --git a/src/creature.cpp b/src/creature.cpp index f2b85e5..24508bf 100644 --- a/src/creature.cpp +++ b/src/creature.cpp @@ -8,8 +8,8 @@ Creature::Creature(Window m, std::string s) maxHealth = 1000; hunger = 0; - L.y=yTarget=rand()%800; - L.x=xTarget=rand()%1200; + L.y=rand()%800; + L.x=rand()%1200; type = 1; hasTarget = false; @@ -18,114 +18,62 @@ Creature::Creature(Window m, std::string s) n=0; } -int Creature::Behavior() +void Creature::Behavior() { - health-=1; + //health-=1; this->Priority(); - if(this->Action()){ - if(nR.size()){ - nR[n]->eat(); - if(healthAction(); } void Creature::Priority() -{ - double d; - - // Gets location for closest resource - for(int i = 0; i < nR.size(); i++) - { - if(i==0) - { - d = Distance(this->getLocation(),nR[0]->getLocation()); - n = 0; - continue; - } - - if(d>Distance(this->getLocation(),nR[i]->getLocation())) - { - d=Distance(this->getLocation(),nR[i]->getLocation()); - n=i; - } - } - - if(nR.size()==0) - hasTarget=false; - - // If there is available targets and the unit doesnt have a target, assign the closest one. - //cout << "size: " << nR.size() << endl; - //cout << "hastarget: "<< hasTarget << endl; - if(nR.size()>0&&!hasTarget) - { - xTarget = nR[n]->getLocation().x; - yTarget = nR[n]->getLocation().y; - hasTarget = true; - wandering = false; - } - // If there is not available targets and doesnt have a target, set a random location as a target - else if(nR.size()==0&&!hasTarget) - { - if(!wandering) - { - xTarget = rand()%1200; - yTarget = rand()%800; - wandering = true; - hasTarget = false; - } - else - { - Location L(xTarget,yTarget); - if(Distance(this->getLocation(),L)<5) - wandering = false; - hasTarget = false; - } - } +{ + for(vector ::iterator it = N.begin(); it!=N.end(); it++){ + if((*it)->getType() == 2){ + if(!hasTarget){ + target = *it; + wandering = false; + hasTarget = true; + } + else + break; + } + } } -bool Creature::Action() -{ - //If the distance is close, will return an bool - //if(xPosition == xTarget && yPosition == yTarget) - // return false; +void Creature::Action() +{ - if(nR.size()) - if(5 > Distance(this->getLocation(),nR[n]->getLocation())) - { - if(hasTarget) - { - hasTarget = false; - return true; - } - else - return false; - } + if(hasTarget){ + if(Distance(L,target->getLocation())<5){ + target->eat(); + health+=1000; + } + if(target->getAmount()==0) + hasTarget = false; + } //Makes moves towards target coordinates - if(L.x==xTarget) + if(L.x==target->getLocation().x) { - if(L.ygetLocation().y) L.y+=speed; else L.y-=speed; } - else if(L.y==yTarget) + else if(L.y==target->getLocation().y) { - if(L.xgetLocation().x) L.x+=speed; else L.x-=speed; } - else if(L.xgetLocation().x) { - if(L.ygetLocation().y) { L.x+=speed; L.y+=speed; @@ -138,9 +86,9 @@ bool Creature::Action() } } - else if (L.x>xTarget) + else if (L.x>target->getLocation().x) { - if(L.ygetLocation().y) { L.x-=speed; L.y+=speed; @@ -151,7 +99,5 @@ bool Creature::Action() L.x-=speed; L.y-=speed; } - } - - return false; + } } diff --git a/src/list.cpp b/src/list.cpp index ad9ab58..ff9722f 100644 --- a/src/list.cpp +++ b/src/list.cpp @@ -4,7 +4,7 @@ List::List(Window m) { int i; - for(i=0;i<10;i++) + for(i=0;i<2;i++) { Creature X(m,"img/Cbasic.png"); C.push_back(X); @@ -35,27 +35,12 @@ void List::Place() void List::Behavior() { for(vector::iterator it = C.begin(); it!=C.end(); it++){ - it->Behavior(); - - vector N; - for(vector ::iterator jt = R.begin(); jt!=R.end(); jt++){ - if( it->getBestSense() > Distance(it->getLocation(),jt->getLocation()) ) - N.push_back(&(*jt)); - } - it->giveR(N); - vector M; - for(vector ::iterator jt = C.begin(); jt!=C.end(); jt++){ - if( jt == it) - continue; - else if( it->getBestSense() > Distance(it->getLocation(),jt->getLocation()) ) - M.push_back(&(*jt)); - } - it->giveC(M); + vector N = getNear(*it); + it->giveN(N); - M.clear(); - N.clear(); - + it->Behavior(); + if(it->getHealth()<=0){ Location z = it->getLocation(); Resource r = Resource(main,"img/Cdead.png",z); @@ -64,3 +49,24 @@ void List::Behavior() } } } + +vector List::getNear(Creature nC) +{ + vector N; + + for(vector ::iterator it = R.begin(); it!=R.end(); it++){ + if( nC.getBestSense() > Distance(nC.getLocation(),it->getLocation()) ) + N.push_back(&(*it)); + } + + for(vector ::iterator it = C.begin(); it!=C.end(); it++){ + if( &nC == &(*it)) + continue; + else if( nC.getBestSense() > Distance(nC.getLocation(),it->getLocation()) ) + N.push_back(&(*it)); + } + + return N; +} + + -- cgit v1.2.3