Browse Source

Amélioration de l'IA en cours

master
Fife 6 years ago
parent
commit
4da0f9d0f2
  1. 120
      iaghost1.cpp
  2. 107
      iaghost2.cpp
  3. 1
      iaghost2.hpp
  4. 24
      pacman.cpp

120
iaghost1.cpp

@ -6,8 +6,8 @@ void IA1::Start()
{
couldown=0;
direction=1;
directionuser=1;
direction=0;
directionuser=0;
}
void IA1::UpdateEverySecond()
@ -37,61 +37,77 @@ void IA1::Update()
//Intersection//
if(!(!GetObject()->GetRigibody()->CollisionDecor(GetX(), GetY() + 1) && !GetObject()->GetRigibody()->CollisionDecor(GetX() , GetY() - 1) &&!GetObject()->GetRigibody()->CollisionDecor(GetX() + 1 , GetY()) && !GetObject()->GetRigibody()->CollisionDecor(GetX() - 1 , GetY())))
if(!GetObject()->CollisionTag("Jail"))
{
switch(direction)
if(!(!GetObject()->GetRigibody()->CollisionDecor(GetX(), GetY() + 1) && !GetObject()->GetRigibody()->CollisionDecor(GetX() , GetY() - 1) &&!GetObject()->GetRigibody()->CollisionDecor(GetX() + 1 , GetY()) && !GetObject()->GetRigibody()->CollisionDecor(GetX() - 1 , GetY())))
{
switch(direction)
{
case 1:
if(!GetObject()->GetRigibody()->CollisionDecor(GetX(), GetY() + 1) || !GetObject()->GetRigibody()->CollisionDecor(GetX() , GetY() - 1))
{
hasard = rand_int(3) + 1;
switch(hasard)
{
case 1: directionuser = 1;break;
case 2: directionuser = 2;break;
case 3: directionuser = 4;break;
}
}
break;
case 2:
if(!GetObject()->GetRigibody()->CollisionDecor(GetX() + 1 , GetY()) || !GetObject()->GetRigibody()->CollisionDecor(GetX() - 1 , GetY()))
{
hasard = rand_int(3) + 1;
switch(hasard)
{
case 1: directionuser = 2;break;
case 2: directionuser = 1;break;
case 3: directionuser = 3;break;
}
}
break;
case 3:
if(!GetObject()->GetRigibody()->CollisionDecor(GetX(), GetY() + 1) || !GetObject()->GetRigibody()->CollisionDecor(GetX(), GetY() - 1))
{
hasard = rand_int(3) + 1;
switch(hasard)
{
case 1: directionuser = 3;break;
case 2: directionuser = 2;break;
case 3: directionuser = 4;break;
}
}
break;
case 4:
if(!GetObject()->GetRigibody()->CollisionDecor(GetX() + 1 , GetY()) || !GetObject()->GetRigibody()->CollisionDecor(GetX() - 1 , GetY()))
{
hasard = rand_int(3) + 1;
switch(hasard)
{
case 1: directionuser = 4;break;
case 2: directionuser = 1;break;
case 3: directionuser = 3;break;
}
}
break;
}
}
}
else
{
case 1:
if(!GetObject()->GetRigibody()->CollisionDecor(GetX(), GetY() + 1) || !GetObject()->GetRigibody()->CollisionDecor(GetX() , GetY() - 1))
{
hasard = rand_int(3) + 1;
switch(hasard)
{
case 1: directionuser = 1;break;
case 2: directionuser = 2;break;
case 3: directionuser = 4;break;
}
}
break;
case 2:
if(!GetObject()->GetRigibody()->CollisionDecor(GetX() + 1 , GetY()) || !GetObject()->GetRigibody()->CollisionDecor(GetX() - 1 , GetY()))
{
hasard = rand_int(3) + 1;
switch(hasard)
{
case 1: directionuser = 2;break;
case 2: directionuser = 1;break;
case 3: directionuser = 3;break;
}
}
break;
case 3:
if(!GetObject()->GetRigibody()->CollisionDecor(GetX(), GetY() + 1) || !GetObject()->GetRigibody()->CollisionDecor(GetX(), GetY() - 1))
{
hasard = rand_int(3) + 1;
switch(hasard)
{
case 1: directionuser = 3;break;
case 2: directionuser = 2;break;
case 3: directionuser = 4;break;
}
}
break;
case 4:
if(!GetObject()->GetRigibody()->CollisionDecor(GetX() + 1 , GetY()) || !GetObject()->GetRigibody()->CollisionDecor(GetX() - 1 , GetY()))
if(GetObject()->GetRigibody()->CollisionDecor(GetX() , GetY()+1))
{
hasard = rand_int(3) + 1;
switch(hasard)
{
case 1: directionuser = 4;break;
case 2: directionuser = 1;break;
case 3: directionuser = 3;break;
}
Object * Jail = GetObject()->CollisionTagO("Jail");
if(Jail != NULL)
{
if(Jail->GetTransforms()->GetX() + 20 < GetX())directionuser = 4;
else directionuser = 2;
}
}
break;
else directionuser = 1;
}
}
//***********Deplacement***********//

107
iaghost2.cpp

@ -4,8 +4,9 @@
void IA2::Start()
{
direction=1;
directionuser=1;
direction=0;
directionuser=0;
couldown = 0;
}
void IA2::Initialisation(Script_GUI * v)
@ -24,55 +25,77 @@ void IA2::Update()
int difx = GetEngine()->GetListeObject()[0]->GetTransforms()->GetX() - GetX();
int dify = GetEngine()->GetListeObject()[0]->GetTransforms()->GetY() - GetY();
if(!GetObject()->GetRigibody()->CollisionDecor(GetX() + (direction%2), GetY() + (!(direction%2))) || !GetObject()->GetRigibody()->CollisionDecor(GetX() - (direction%2), GetY() - (!(direction%2))))
if(couldown <= 0)
{
PrintMini(10,10,"Test",0);
bool possibledirection[4];
for(int i = 0; i < 4 ; i++)possibledirection[i] = !(GetObject()->GetRigibody()->CollisionDecor(GetX() + (i == 1) - (i == 3), GetY() + (i == 0) - (i == 2)));
int betterdirection[4]; int best;
if(ABS(difx) > ABS(dify))best = 1;
else best = 2;
if(difx == 0)best = 2;
if(dify == 0)best = 1;
if(difx > 0)
if(!GetObject()->CollisionTag("Jail"))
{
betterdirection[ (best == 2)] = 2;
betterdirection[ 3 -(best == 2)] = 4;
}
else
{
betterdirection[ (best == 2)] = 4;
betterdirection[ 3 -(best == 2)] = 2;
}
if(dify > 0)
{
betterdirection[ (best == 1)] = 1;
betterdirection[ 3 -(best == 1)] = 3;
if(!GetObject()->GetRigibody()->CollisionDecor(GetX() + (direction%2), GetY() + (!(direction%2))) || !GetObject()->GetRigibody()->CollisionDecor(GetX() - (direction%2), GetY() - (!(direction%2))))
{
PrintMini(10,10,"Test",0);
bool possibledirection[4];
for(int i = 0; i < 4 ; i++)possibledirection[i] = !(GetObject()->GetRigibody()->CollisionDecor(GetX() + (i == 1) - (i == 3), GetY() + (i == 0) - (i == 2)));
if(GetObject()->CollisionTag("Jail",0,-1))possibledirection[2] = false;
int betterdirection[4]; int best;
if(ABS(difx) > ABS(dify))best = 1;
else best = 2;
if(difx == 0)best = 2;
if(dify == 0)best = 1;
if(difx > 0)
{
betterdirection[ (best == 2)] = 2;
betterdirection[ 3 -(best == 2)] = 4;
}
else
{
betterdirection[ (best == 2)] = 4;
betterdirection[ 3 -(best == 2)] = 2;
}
if(dify > 0)
{
betterdirection[ (best == 1)] = 1;
betterdirection[ 3 -(best == 1)] = 3;
}
else
{
betterdirection[ (best == 1)] = 3;
betterdirection[ 3 -(best == 1)] = 1;
}
int end = 4;
while(end > 0)
{
if(possibledirection[betterdirection[4 - end] - 1])
{
directionuser = betterdirection[4 - end];
end = 0;
}
else end --;
}
}
}
else
{
betterdirection[ (best == 1)] = 3;
betterdirection[ 3 -(best == 1)] = 1;
}
int end = 4;
while(end > 0)
{
if(possibledirection[betterdirection[4 - end] - 1])
{
directionuser = betterdirection[4 - end];
end = 0;
}
else end --;
if(GetObject()->GetRigibody()->CollisionDecor(GetX() , GetY()+1))
{
Object * Jail = GetObject()->CollisionTagO("Jail");
if(Jail != NULL)
{
if(Jail->GetTransforms()->GetX() + 20 < GetX())directionuser = 4;
else directionuser = 2;
}
}
else directionuser = 1;
}
}
else couldown --;
//***********Deplacement***********//

1
iaghost2.hpp

@ -12,6 +12,7 @@ class IA2: public Script
private :
int couldown;
int direction;
int directionuser;

24
pacman.cpp

@ -80,13 +80,21 @@ int Jeu()
TP2->GetRigibody()->UseFixeBody(15,15);
TP2->AffectTag("TP2");
/*IA2 * Script_Ghost1 = new IA2;
IA1 * Script_Ghost2 = new IA2;
IA2 * Script_Ghost3 = new IA2;*/
Object * Jail = new Object;
Jail->GetTransforms()->SetXY(31 * 3, (91 - 48) * 3);
Jail->AddRigibody();
Jail->GetRigibody()->UseFixeBody(60, 36);
Jail->AffectTag("Jail");
IA1 * Script_Ghost1 = new IA1;
IA1 * Script_Ghost2 = new IA1;
IA2 * Script_Ghost3 = new IA2;
IA2 * Script_Ghost4 = new IA2;
/* Ghost1->AffectScript(Script_Ghost1);
Ghost1->AffectScript(Script_Ghost1);
Ghost2->AffectScript(Script_Ghost2);
Ghost3->AffectScript(Script_Ghost3);*/
Ghost3->AffectScript(Script_Ghost3);
Ghost4->AffectScript(Script_Ghost4);
//***********Creation de la map***************
@ -106,6 +114,7 @@ int Jeu()
Game.AddObject( Ghost4);
Game.AddObject( TP1);
Game.AddObject( TP2);
Game.AddObject( Jail);
Game.AffectMap( &Niveau1);
@ -113,10 +122,11 @@ int Jeu()
Script_GUI SGUI;
Game.AffectScript(SGUI);
Script_PacMan->Initialisation(&SGUI);
/*Script_Ghost1->Initialisation(&SGUI);
Script_Ghost1->Initialisation(&SGUI);
Script_Ghost2->Initialisation(&SGUI);
Script_Ghost3->Initialisation(&SGUI);*/
Script_Ghost3->Initialisation(&SGUI);
Script_Ghost4->Initialisation(&SGUI);
Game.SetFpsWish(45);

Loading…
Cancel
Save