summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortom <tom@ground-control>2016-05-02 22:02:25 -0500
committertom <tom@ground-control>2016-05-02 22:02:25 -0500
commite7cac5098145d75a680e35787f7c504caf16965b (patch)
tree8256bd87b35c5aadcef0e5bd16030a4196cbbc7e
parent8d9ad3eb74bc0dfc647abf03b7b99ed16f2a9115 (diff)
fixed issue, was related to erasing nodes within vectors while using iterators
-rw-r--r--inc/creature.hpp10
-rw-r--r--inc/list.hpp6
-rw-r--r--inc/window.hpp2
-rw-r--r--src/creature.cpp15
-rw-r--r--src/list.cpp16
5 files changed, 24 insertions, 25 deletions
diff --git a/inc/creature.hpp b/inc/creature.hpp
index 082e8d9..7da82e5 100644
--- a/inc/creature.hpp
+++ b/inc/creature.hpp
@@ -12,7 +12,7 @@ class Creature: public Entity
void Behavior();
void Action();
void Priority();
- void giveN(vector<Entity*> n){N = n;};
+ void giveN(list<Entity*> 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));};
@@ -22,8 +22,6 @@ class Creature: public Entity
private:
bool hasTarget;
- bool wandering;
-
int health;
int maxHealth;
int hunger;
@@ -31,10 +29,8 @@ class Creature: public Entity
bool able;
int bestSense = 100;
- vector<Entity*> N;
- Entity *target;
-
- int n;
+ list<Entity*> N;
+ Entity *target;
};
#endif
diff --git a/inc/list.hpp b/inc/list.hpp
index 9d05fec..3ad8477 100644
--- a/inc/list.hpp
+++ b/inc/list.hpp
@@ -13,13 +13,13 @@ class List
void Behavior();
void Place();
double Distance(Location A, Location B){return sqrt(pow(A.x-B.x,2)+pow(A.y-B.y,2));};
- vector<Entity*> getNear(Creature C);
+ list<Entity*> getNear(Creature C);
private:
//vectors containing objects of each type
Window main = Window("no");//will be needed for adding R's and C's after constructor.
- vector<Resource> R;
- vector<Creature> C;
+ list<Resource> R;
+ list<Creature> C;
};
#endif
diff --git a/inc/window.hpp b/inc/window.hpp
index b9f9175..97e6c09 100644
--- a/inc/window.hpp
+++ b/inc/window.hpp
@@ -12,6 +12,8 @@
#include <vector>
#include <chrono>
#include <random>
+#include <list>
+
using namespace std;
diff --git a/src/creature.cpp b/src/creature.cpp
index 24508bf..3ae02ce 100644
--- a/src/creature.cpp
+++ b/src/creature.cpp
@@ -13,14 +13,12 @@ Creature::Creature(Window m, std::string s)
type = 1;
hasTarget = false;
- wandering = false;
able = true;
- n=0;
}
void Creature::Behavior()
{
- //health-=1;
+ health-=1;
this->Priority();
@@ -29,11 +27,10 @@ void Creature::Behavior()
void Creature::Priority()
{
- for(vector <Entity*>::iterator it = N.begin(); it!=N.end(); it++){
- if((*it)->getType() == 2){
+ for(list <Entity*>::iterator it = N.begin(); it!=N.end(); it++){
+ if((*it)->getType() == 2){
if(!hasTarget){
target = *it;
- wandering = false;
hasTarget = true;
}
else
@@ -44,14 +41,18 @@ void Creature::Priority()
void Creature::Action()
{
+ //cout << "My coords:" << L.x << "," << L.y << endl;
+ cout << "Mytcoords:" << target->getLocation().x << "," << target->getLocation().y << endl << endl;
if(hasTarget){
if(Distance(L,target->getLocation())<5){
target->eat();
health+=1000;
}
- if(target->getAmount()==0)
+ if(target->getAmount()==0){
hasTarget = false;
+ cout << "done\n";
+ }
}
//Makes moves towards target coordinates
diff --git a/src/list.cpp b/src/list.cpp
index ff9722f..5d8ae35 100644
--- a/src/list.cpp
+++ b/src/list.cpp
@@ -21,10 +21,10 @@ List::List(Window m)
void List::Place()
{
- for(vector<Creature>::iterator it = C.begin(); it!=C.end(); it++)
+ for(list<Creature>::iterator it = C.begin(); it!=C.end(); it++)
it->Place();
- for(vector<Resource>::iterator it = R.begin(); it!=R.end(); it++){
+ for(list<Resource>::iterator it = R.begin(); it!=R.end(); it++){
if(it->getAmount()<=0)
R.erase(it--);
else
@@ -34,9 +34,9 @@ void List::Place()
void List::Behavior()
{
- for(vector<Creature>::iterator it = C.begin(); it!=C.end(); it++){
+ for(list<Creature>::iterator it = C.begin(); it!=C.end(); it++){
- vector<Entity*> N = getNear(*it);
+ list<Entity*> N = getNear(*it);
it->giveN(N);
it->Behavior();
@@ -50,16 +50,16 @@ void List::Behavior()
}
}
-vector<Entity*> List::getNear(Creature nC)
+list<Entity*> List::getNear(Creature nC)
{
- vector<Entity*> N;
+ list<Entity*> N;
- for(vector <Resource>::iterator it = R.begin(); it!=R.end(); it++){
+ for(list <Resource>::iterator it = R.begin(); it!=R.end(); it++){
if( nC.getBestSense() > Distance(nC.getLocation(),it->getLocation()) )
N.push_back(&(*it));
}
- for(vector <Creature>::iterator it = C.begin(); it!=C.end(); it++){
+ for(list <Creature>::iterator it = C.begin(); it!=C.end(); it++){
if( &nC == &(*it))
continue;
else if( nC.getBestSense() > Distance(nC.getLocation(),it->getLocation()) )