bugs.h
Code
#include "Food.h"
#include <vector>
class Bugs{
public:
string Type;
string Orientation;
Coord Location;
int ID;
int Health;
int See;
Bugs(string t,int i)
{
Type = t;
ID = i;
Orientation = "N";
Health = 500;
See = 100;
}
Bugs(string t, string o, int x, int y, int i)
{
Type = t;
Orientation = o;
Location.x = x;
Location.y = y;
ID = i;
Health = 500;
See = 100;
}
void PlaceBug()
{
int tempx = rand() % 660 + 20;
int tempy = rand() % 460 + 20;
while (AnythingNearby(tempx,tempy))
{
tempx = rand() % 640 + 30;
tempy = rand() % 440 + 30;
}
Location.x = tempx;
Location.y = tempy;
}
void RotateLeft()
{
if (Orientation == "N"){Orientation = "WN";}
else if (Orientation == "NE"){Orientation = "N";}
else if (Orientation == "E"){Orientation = "NE";}
else if (Orientation == "ES"){Orientation = "E";}
else if (Orientation == "S"){Orientation = "ES";}
else if (Orientation == "SW"){Orientation = "S";}
else if (Orientation == "W"){Orientation = "SW";}
else if (Orientation == "WN"){Orientation = "W";}
}
void RotateRight()
{
if (Orientation == "N"){Orientation = "NE";}
else if (Orientation == "NE"){Orientation = "E";}
else if (Orientation == "E"){Orientation = "ES";}
else if (Orientation == "ES"){Orientation = "S";}
else if (Orientation == "S"){Orientation = "SW";}
else if (Orientation == "SW"){Orientation = "W";}
else if (Orientation == "W"){Orientation = "WN";}
else if (Orientation == "WN"){Orientation = "N";}
}
void StampBug()
{
if (Type == "One"){
if (Orientation == "N"){Stamp(Ret1N(),Location.x-22,Location.y-22);}
else if (Orientation == "NE"){Stamp(Ret1NE(),Location.x-22,Location.y-22);}
else if (Orientation == "E"){Stamp(Ret1E(),Location.x-22,Location.y-22);}
else if (Orientation == "ES"){Stamp(Ret1ES(),Location.x-22,Location.y-22);}
else if (Orientation == "S"){Stamp(Ret1S(),Location.x-22,Location.y-22);}
else if (Orientation == "SW"){Stamp(Ret1SW(),Location.x-22,Location.y-22);}
else if (Orientation == "W"){Stamp(Ret1W(),Location.x-22,Location.y-22);}
else if (Orientation == "WN"){Stamp(Ret1WN(),Location.x-22,Location.y-22);}}
else{
if (Orientation == "N"){Stamp(Ret2N(),Location.x-22,Location.y-22);}
else if (Orientation == "NE"){Stamp(Ret2NE(),Location.x-22,Location.y-22);}
else if (Orientation == "E"){Stamp(Ret2E(),Location.x-22,Location.y-22);}
else if (Orientation == "ES"){Stamp(Ret2ES(),Location.x-22,Location.y-22);}
else if (Orientation == "S"){Stamp(Ret2S(),Location.x-22,Location.y-22);}
else if (Orientation == "SW"){Stamp(Ret2SW(),Location.x-22,Location.y-22);}
else if (Orientation == "W"){Stamp(Ret2W(),Location.x-22,Location.y-22);}
else if (Orientation == "WN"){Stamp(Ret2WN(),Location.x-22,Location.y-22);}
}
}
void PlaceBugOnMap()
{
PlaceToMap(ID,Location.x,Location.y);
}
void RemoveBugFromMap()
{
RemoveFromMap(Location.x,Location.y);
}
void Step()
{
RemoveBugFromMap();
if (Orientation == "N"){Location.y-=1;}
else if (Orientation == "NE"){Location.x+=1; Location.y-=1;}
else if (Orientation == "E"){Location.x+=1;}
else if (Orientation == "ES"){Location.x+=1; Location.y+=1;}
else if (Orientation == "S"){Location.y+=1;}
else if (Orientation == "SW"){Location.x-=1; Location.y+=1;}
else if (Orientation == "W"){Location.x-=1;}
else if (Orientation == "WN"){Location.x-=1; Location.y-=1;}
PlaceBugOnMap();
Health--;
}
void RandomMove()
{
int temp = rand() % 100 + 1;
if (temp <= 10){
RotateLeft();
}
if (temp >=90){
RotateRight();
}
Step();
}
void MoveAway(int x, int y)
{
if (x > Location.x)
{
if (y > Location.y)
{
Orientation = "WN";
}
else if (y < Location.y)
{
Orientation = "SW";
}
else
{
Orientation = "W";
}
}
else if (x < Location.x)
{
if (y > Location.y)
{
Orientation = "NE";
}
else if (y < Location.y)
{
Orientation = "ES";
}
else
{
Orientation = "E";
}
}
else
{
if (y > Location.y)
{
Orientation = "N";
}
else if (y < Location.y)
{
Orientation = "S";
}
}
Step();
}
void MoveTowards(int x, int y)
{
if (x > Location.x)
{
if (y > Location.y)
{
Orientation = "ES";
}
else if (y < Location.y)
{
Orientation = "NE";
}
else
{
Orientation = "E";
}
}
else if (x < Location.x)
{
if (y > Location.y)
{
Orientation = "SW";
}
else if (y < Location.y)
{
Orientation = "WN";
}
else
{
Orientation = "W";
}
}
else
{
if (y > Location.y)
{
Orientation = "S";
}
else if (y < Location.y)
{
Orientation = "N";
}
}
Step();
}
Coord SenseClosestFood()
{
Coord Temp;
Temp.x = 1500;
Temp.y = 1500;
int dist = See;
int where = 0;
vector <Coord> TempV;
for (int i = 0; i < 500; i++)
{
for (int j = 0; j < 700; j++)
{
if ((Mappino[i][j] == 2) && (Distance(Location.x,Location.y,j,i) < See) && (Distance(Location.x,Location.y,j,i) > 20))
{
Temp.x = j;
Temp.y = i;
TempV.push_back(Temp);
}
else if ((Mappino[i][j] == 2) && (Distance(Location.x,Location.y,j,i) <= 20))
{
Health += 300;
Mappino[i][j] = 0;
}
}
}
if (TempV.size() == 0)
{
return Temp;
}
else if (TempV.size() == 1)
{
return TempV[0];
}
else
{
for (unsigned int i = 0; i < TempV.size(); i++)
{
if (Distance(Location.x,Location.y,TempV[i].x,TempV[i].y) < dist)
{
dist = Distance(Location.x,Location.y,TempV[i].x,TempV[i].y);
where = i;
}
}
return TempV[where];
}
}
Coord SenseClosestObstacle()
{
Coord Temp;
Temp.x = 1500;
Temp.y = 1500;
int dist = See;
int where = 0;
vector <Coord> TempV;
for (int i = 0; i < 500; i++)
{
for (int j = 0; j < 700; j++)
{
if ((Mappino[i][j] == 1) && (Distance(Location.x,Location.y,j,i) < See))
{
Temp.x = j;
Temp.y = i;
TempV.push_back(Temp);
}
}
}
if (TempV.size() == 0)
{
return Temp;
}
else if (TempV.size() == 1)
{
return TempV[0];
}
else
{
for (unsigned int i = 0; i < TempV.size(); i++)
{
if (Distance(Location.x,Location.y,TempV[i].x,TempV[i].y) < dist)
{
dist = Distance(Location.x,Location.y,TempV[i].x,TempV[i].y);
where = i;
}
}
return TempV[where];
}
}
Coord SenseClosestOne()
{
Coord Temp;
Temp.x = 1500;
Temp.y = 1500;
int dist = See;
int where = 0;
vector <Coord> TempV;
for (int i = 0; i < 500; i++)
{
for (int j = 0; j < 700; j++)
{
if ((Mappino[i][j] > 2) && ((i != Location.y) || (j != Location.x)) && (Mappino[i][j] < 500) && (Distance(Location.x,Location.y,j,i) < See))
{
Temp.x = j;
Temp.y = i;
TempV.push_back(Temp);
}
}
}
if (TempV.size() == 0)
{
return Temp;
}
else if (TempV.size() == 1)
{
return TempV[0];
}
else
{
for (unsigned int i = 0; i < TempV.size(); i++)
{
if (Distance(Location.x,Location.y,TempV[i].x,TempV[i].y) < dist)
{
dist = Distance(Location.x,Location.y,TempV[i].x,TempV[i].y);
where = i;
}
}
return TempV[where];
}
}
Coord SenseClosestTwo()
{
Coord Temp;
Temp.x = 1500;
Temp.y = 1500;
int dist = See;
int where = 0;
vector <Coord> TempV;
for (int i = 0; i < 500; i++)
{
for (int j = 0; j < 700; j++)
{
if ((Mappino[i][j] > 500) && ((i != Location.y) || (j != Location.x)) && (Distance(Location.x,Location.y,j,i) < See))
{
Temp.x = j;
Temp.y = i;
TempV.push_back(Temp);
}
}
}
if (TempV.size() == 0)
{
return Temp;
}
else if (TempV.size() == 1)
{
return TempV[0];
}
else
{
for (unsigned int i = 0; i < TempV.size(); i++)
{
if (Distance(Location.x,Location.y,TempV[i].x,TempV[i].y) < dist)
{
dist = Distance(Location.x,Location.y,TempV[i].x,TempV[i].y);
where = i;
}
}
return TempV[where];
}
}
void MoveWithBrain()
{
Coord Temp1 = SenseClosestFood();
Coord Temp2 = SenseClosestObstacle();
Coord Temp3 = SenseClosestOne();
Coord Temp4 = SenseClosestTwo();
if (Type == "One")
{
if (Distance(Location.x,Location.y, Temp2.x, Temp2.y) < 30)
{
MoveAway(Temp2.x,Temp2.y);
MoveAway(Temp2.x,Temp2.y);
}
else if (Distance(Location.x,Location.y, Temp3.x, Temp3.y) < 30)
{
MoveAway(Temp3.x,Temp3.y);
MoveAway(Temp3.x,Temp3.y);
}
else if ((Distance(Location.x,Location.y, Temp4.x, Temp4.y) < See) && (Distance(Location.x,Location.y, Temp4.x, Temp4.y) >20) && (Distance(Location.x,Location.y, Temp4.x, Temp4.y)*2 < Distance(Location.x,Location.y, Temp1.x, Temp1.y)))
{
MoveTowards(Temp4.x,Temp4.y);
MoveTowards(Temp4.x,Temp4.y);
}
else if (Distance(Location.x,Location.y, Temp4.x, Temp4.y) <= 20)
{
Mappino[Temp4.y][Temp4.x] = 0;
Health+= 200;
RandomMove();
}
else if ((Distance(Location.x,Location.y, Temp1.x, Temp1.y) < See) && (Distance(Location.x,Location.y, Temp1.x, Temp1.y) >20))
{
MoveTowards(Temp1.x,Temp1.y);
MoveTowards(Temp1.x,Temp1.y);
}
else if (Distance(Location.x,Location.y, Temp1.x, Temp1.y) < 20)
{
Health += 200;
RandomMove();
Mappino[Temp1.y][Temp1.x] = 0;
}
else
{
RandomMove();
RandomMove();
}
}
else
{
if (Distance(Location.x,Location.y, Temp2.x, Temp2.y) < 30)
{
MoveAway(Temp2.x,Temp2.y);
MoveAway(Temp2.x,Temp2.y);
}
else if (Distance(Location.x,Location.y, Temp4.x, Temp4.y) < 30)
{
MoveAway(Temp4.x,Temp4.y);
MoveAway(Temp4.x,Temp4.y);
}
else if ((Distance(Location.x,Location.y, Temp3.x, Temp3.y) < See) && (Distance(Location.x,Location.y, Temp3.x, Temp3.y) >20) && (Distance(Location.x,Location.y, Temp3.x, Temp3.y)*2 < Distance(Location.x,Location.y, Temp1.x, Temp1.y)))
{
MoveAway(Temp3.x,Temp3.y);
MoveAway(Temp3.x,Temp3.y);
MoveAway(Temp3.x,Temp3.y);
}
else if (Distance(Location.x,Location.y, Temp3.x, Temp3.y) <= 20)
{
Health = -100;
}
else if ((Distance(Location.x,Location.y, Temp1.x, Temp1.y) < See) && (Distance(Location.x,Location.y, Temp1.x, Temp1.y) >20))
{
MoveTowards(Temp1.x,Temp1.y);
MoveTowards(Temp1.x,Temp1.y);
MoveTowards(Temp1.x,Temp1.y);
}
else if (Distance(Location.x,Location.y, Temp1.x, Temp1.y) < 20)
{
Mappino[Temp1.y][Temp1.x] = 0;
Health += 200;
RandomMove();
}
else
{
RandomMove();
RandomMove();
}
}
}
void WriteLocation(int x, int y)
{
MoveTo(x-2,y-15);
Color(0,0,0);
Box(290,17);
MoveTo(x,y);
Color(255,255,255);
gout << text("Buggy : ( ") << text(Convert(Location.x)) << text(", ") << text(Convert(Location.y)) << text(" - ") << text(Orientation) << text(" )") << text(" HP : ") << text(Convert(Health));
}
};