1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#include "creature.hpp"
Creature::Creature(Window m, std::string s) //Constructor
{
texture = loadTexture(s, m);
renderer = m.getRenderer();
health = 500;
maxHealth = 1000;
hunger = 0;
//initializes random start coordinates for creature, target position is equivalent to it's position
yPosition=yTarget=rand()%800;
xPosition=xTarget=rand()%1200;
hasTarget = false;
wandering = false;
able = true;
n=0;
}
int Creature::Behavior()
{
health-=1; //Decrements health each time a behavior is executed
this->Priority();
if(this->Action())
{
if(nR.size())
{
nR[n]->eat();
if(health<maxHealth)
health+=10;
}
}
return 0;
}
void Creature::Priority()
{
double d; // lol
// 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,1);
if(Distance(this->getLocation(),L)<5)
wandering = false;
hasTarget = false;
}
}
}
bool Creature::Action()
{
//If the distance is close, will return an bool
//if(xPosition == xTarget && yPosition == yTarget)
// return false;
if(nR.size())
if(5 > Distance(this->getLocation(),nR[n]->getLocation()))
{
if(hasTarget)
{
hasTarget = false;
return true;
}
else
return false;
}
//Makes moves towards target coordinates
if(xPosition==xTarget)
{
if(yPosition<yTarget)
yPosition+=speed;
else
yPosition-=speed;
}
else if(yPosition==yTarget)
{
if(xPosition<xTarget)
xPosition+=speed;
else
xPosition-=speed;
}
else if(xPosition<xTarget)
{
if(yPosition<yTarget)
{
xPosition+=speed;
yPosition+=speed;
}
else
{
xPosition+=speed;
yPosition-=speed;
}
}
else if (xPosition>xTarget)
{
if(yPosition<yTarget)
{
xPosition-=speed;
yPosition+=speed;
}
else
{
xPosition-=speed;
yPosition-=speed;
}
}
return false;
}
Location Creature::getLocation()
{
//returns location object of the specific creature
Location L(xPosition, yPosition, 1);
return L;
}
double Creature::Distance(Location A, Location B)
{
//computes distance between two points
return sqrt(pow(A.x - B.x, 2) + pow(A.y - B.y, 2));
}
bool Creature::doesItHaveTarget()
{
return hasTarget;
}
|