Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with custom AI crashing the game
#1
Hi,

I've been trying to build an AI for one of my characters, however, when I tested it, the game crashes.

My character has a skill, "Self-Immolation", which converts his HP into MP by pressing DvJ, followed by A; each A alternates between losing HP and regaining MP. Pressing D ends the skill.

My plan is that if the AI has high HP and low MP, it will attempt to use that skill. If the difficulty is normal or higher, it will also check if there are enemies nearby before attempting to use it. My code is as follows:

    AI-Code:
int ego()
{
	//Self-Immolation
	if (self.hp > 300 && self.mp < 300 && (abs(self.x - target.x) > 300 || difficulty == 2 ))
	{
		DdJ(); //Begin Self-Immolation
		while(self.hp > 300 && self.mp < 300 && (abs(self.x - target.x) > 300 || difficulty == 2 ))
		{
			if(self.frame == 254)
			{
				A(1,0); //activate HP loss
				if(self.frame == 256)
				{
					A(1,0); //activate MP gain
				}
			}
			if(self.frame < 250 || self.frame > 258)
			{
				return 0;
			}
		}
		if(self.frame == 254)
		{
			D(1,0); //End Self-Immolation
		}else if(self.frame < 250 || self.frame > 258)
		{
			return 0;
		}
	}
return 0;
}


However, when I tested it, as soon as the AI starts to use Self-Immolation, the game crashes. There are no error messages in the console, and using the skill as a human player doesn't cause problems.

Here's the code for the skill for reference:

    DATA-Code:
<frame> 250 sacrifice
   pic: 140  state: 15  wait: 2  next: 251  dvx: 0  dvy: 0  dvz: 0  centerx: 39  centery: 79  hit_a: 0  hit_d: 0  hit_j: 0
  sound: data\050.wav 
   wpoint:
      kind: 1  x: 14  y: 51  weaponact: 20  attacking: 0  cover: 0  dvx: 0  dvy: 0  dvz: 0 
   wpoint_end:
   bdy:
      kind: 0  x: 20  y: 13  w: 37  h: 67
   bdy_end:
<frame_end>
 
<frame> 251 sacrifice
   pic: 141  state: 15  wait: 2  next: 252  dvx: 0  dvy: 0  dvz: 0  centerx: 39  centery: 79  hit_a: 0  hit_d: 0  hit_j: 0
  sound: data\052.wav 
   wpoint:
      kind: 1  x: 14  y: 52  weaponact: 20  attacking: 0  cover: 0  dvx: 0  dvy: 0  dvz: 0 
   wpoint_end:
   bdy:
      kind: 0  x: 18  y: 14  w: 39  h: 66
   bdy_end:
<frame_end>
 
<frame> 252 sacrifice
   pic: 142  state: 15  wait: 2  next: 253  dvx: 0  dvy: 0  dvz: 0  centerx: 39  centery: 79  hit_a: 0  hit_d: 0  hit_j: 0
   wpoint:
      kind: 1  x: 14  y: 52  weaponact: 20  attacking: 0  cover: 0  dvx: 0  dvy: 0  dvz: 0 
   wpoint_end:
   bdy:
      kind: 0  x: 18  y: 14  w: 38  h: 66
   bdy_end:
<frame_end>
 
<frame> 253 sacrifice
   pic: 143  state: 15  wait: 2  next: 254  dvx: 0  dvy: 0  dvz: 0  centerx: 39  centery: 79  hit_a: 0  hit_d: 0  hit_j: 0
   wpoint:
      kind: 1  x: 13  y: 49  weaponact: 20  attacking: 0  cover: 0  dvx: 0  dvy: 0  dvz: 0 
   wpoint_end:
   bdy:
      kind: 0  x: 17  y: 16  w: 39  h: 64
   bdy_end:
<frame_end>
 
<frame> 254 sacrifice
   pic: 144  state: 15  wait: 45  next: 999  dvx: 0  dvy: 0  dvz: 0  centerx: 39  centery: 79  hit_a: 255  hit_d: 999  hit_j: 0
   wpoint:
      kind: 1  x: 15  y: 47  weaponact: 20  attacking: 0  cover: 0  dvx: 0  dvy: 0  dvz: 0 
   wpoint_end:
   bdy:
      kind: 0  x: 15  y: 15  w: 42  h: 65
   bdy_end:
<frame_end>
 
<frame> 255 sacrifice
   pic: 145  state: 15  wait: 0  next: 256  dvx: 0  dvy: 0  dvz: 0  centerx: 39  centery: 79  hit_a: 256  hit_d: 999  hit_j: 0 mp: 5000
   wpoint:
      kind: 1  x: 17  y: 51  weaponact: 20  attacking: 0  cover: 0  dvx: 0  dvy: 0  dvz: 0 
   wpoint_end:
   bdy:
      kind: 0  x: 18  y: 11  w: 37  h: 68
   bdy_end:
<frame_end>
 
<frame> 256 sacrifice
   pic: 145  state: 15  wait: 45  next: 999  dvx: 0  dvy: 0  dvz: 0  centerx: 39  centery: 79  hit_a: 257  hit_d: 999  hit_j: 0
   opoint:
      kind: 1  x: 40  y: 39  action: 10  dvx: 0  dvy: 0  oid: 210  facing: 0
   opoint_end:
   wpoint:
      kind: 1  x: 17  y: 51  weaponact: 20  attacking: 0  cover: 0  dvx: 0  dvy: 0  dvz: 0 
   wpoint_end:
   bdy:
      kind: 0  x: 18  y: 11  w: 37  h: 68
   bdy_end:
<frame_end>
 
<frame> 257 sacrifice
   pic: 146  state: 15  wait: 0  next: 258  dvx: 0  dvy: 0  dvz: 0  centerx: 39  centery: 79  hit_a: 0  hit_d: 999  hit_j: 0 mp: -50
   wpoint:
      kind: 1  x: 19  y: 52  weaponact: 21  attacking: 0  cover: 0  dvx: 0  dvy: 0  dvz: 0 
   wpoint_end:
   bdy:
      kind: 0  x: 20  y: 10  w: 36  h: 69
   bdy_end:
<frame_end>
 
<frame> 258 sacrifice
   pic: 144  state: 15  wait: 0  next: 254  dvx: 0  dvy: 0  dvz: 0  centerx: 39  centery: 79  hit_a: 255  hit_d: 999  hit_j: 0
   opoint:
      kind: 1  x: 40  y: 39  action: 40  dvx: 0  dvy: 0  oid: 9001  facing: 0
   opoint_end: 
   wpoint:
      kind: 1  x: 15  y: 47  weaponact: 20  attacking: 0  cover: 0  dvx: 0  dvy: 0  dvz: 0 
   wpoint_end:
   bdy:
      kind: 0  x: 15  y: 15  w: 42  h: 65
   bdy_end:
<frame_end>


Is there anything wrong with my code, or anything I can do to prevent these crashes?
Reply
Thanks given by:
#2
I've gotten a bit rusty in terms of AI-scripting but I suspect the while-loop being the culprit. If I remember correctly, the entire AI-script is run every frame and therefore, the
self.hp > 300 && self.mp < 300
-result will never change within one single frame, therefore run indefinitely, and cause LF2 to essentially lock up (or crash in this case).

Instead, you might want to change it to an if-clause with another
self.frame
-check.
Silverthorn / Blue Phoenix
~ Breaking LFE since 2008 ~

"Freeze, you're under vrest!" - Mark, probably.

» Gallery | » Sprites | » DeviantArt
Reply
Thanks given by: AscheLeee
#3
Yes. Your problem stems from an assumption you've made that the code runs asynchronously and independent of the game loop. But that's not the case as BP said. The ego function will have to run every single game-frame until it returns (or reaches the end of control).
[Image: signature.png]
A-Engine: A new beat em up game engine inspired by LF2. Coming soon

A-Engine Dev Blog - Update #8: Timeout

Reply
Thanks given by: AscheLeee
#4
Thanks everyone! I changed it to if-clauses and it works now!

Code:
int ego()
{
    //Self-Immolation
    if (self.hp > 300 && self.mp < 300 && (abs(self.x - target.x) > 300 || difficulty == 2 ))
    {
        DdJ(); //Begin Self-Immolation
    }
    if (self.frame == 254 )
    {
        if (self.hp > 300 && self.mp < 350 && (abs(self.x - target.x) > 300 || difficulty == 2 ))
        {
            A(1,0);
        }else
        {
            D(1,0);
        }
    }
    if (self.frame == 256)
    {
        A(1,0);
    }
return 0;
}

I guess LF2 hates while loops :/
Reply
Thanks given by:
#5
PLEASE SOMEONE TELL ME HOW TO MAKE AI SCRIPTS
Checkout my ongoing project: FTQ (beta)
Reply
Thanks given by:




Users browsing this thread: 1 Guest(s)