Hey all.
I'm working on the AI system (very basic so far) for a turn based style. So far I have it so that as a character/enemies ability activates it adds them to a queue where runs through each character one at a time and reorders them after the attack is done. So far, it seems to work fine, for the most part, until the fourth or fifth player is added to the list (there are currently only 3 or 4 players in the battle.
This is where my issue is. When the attack initiates, the attack timer begins. When it reaches a certain value (10000), the attack plays through and damage assigned (soon) and the list moves up. However, on the fourth or fifth list person, the attack timer continues to loop but never finishes the attack and reorders the list. I'm assuming that there is some variable issue or array issue at play here, but just seeing if anyone can pick out the flaw, because I'm getting a bit stumped since it works for a number of players, and will even run through that player a second time before it stops.
Here's the code:
Function PlayerAI(n)
////////////////////////
//PLAYER AI
////////////////////////
///////////////////////////////////////////////////////
//AUTO ATTACK TIMER
///////////////////////////////////////////////////////
If Stats(n).AttackTimer = 9000 and Stats(n).APCurrent = 2 and Stats(n).Sequence = 1
//If Stats(n).ActivePlayer = 0 //NOT THE CURRENTLY HUMAN CONTROLLED PLAYER
Reorder()
Stats(n).APCurrent = Stats(n).APCurrent-2
Stats(n).Sequence = 0
Stats(n).APTotal = 0
BattleOrder(Stats(n).Sequence).Sequence = 0
Stats(n).AttackTimer = 0
Endif
If Stats(n).APTimer > 6000 and Stats(n).APCurrent < Stats(n).APMax and Stats(n).APTotal < Stats(n).APMax //See if timer is at point to increase AP
Stats(n).APCurrent = Stats(n).APCurrent+1
Stats(n).APTotal = Stats(n).APTotal+1
Stats(n).APTimer = 0
Endif
/////////////////////////////////////////////////////////////////
//ACTIVE PLAYER DOES NOT ADD TO LIST UNTIL ACTION IS ASSIGNED
//////////////////////////////////////////////////////////////////
If Stats(n).APCurrent > 0 and Stats(n).ActivePlayer = 0 and Stats(n).Sequence = 0 //Check if AP Point applied
Repeat
If Stats(n).Sequence = 0
inc x
Endif
Until Stats(n).Sequence || x
Stats(n).Sequence = x
/////////////////////////////
//BATTLEORDERING
/////////////////////////////
BattleOrder(x).Sequence = Stats(n).Sequence
BattleOrder(x).CharacterName = Stats(n).Name
Endif
Stats(n).APTimer = Stats(n).APTimer+Stats(n).SPD //maybe at bottom instead
If Stats(n).APTimer > 6000 and Stats(n).APCurrent = Stats(n).APMax
Stats(n).APTimer = 0
Endif
If Stats(n).Sequence = 1
Stats(n).AttackTimer = Stats(n).AttackTimer+Stats(n).SPD
If Stats(n).AttackTimer > 10000
Stats(n).AttackTimer = 0
Endif
Endif
EndFunction
Function EnemyAI(q)
///////////////////////////////
//BATTLEORDER VARIABLES
///////////////////////////////
If Enemy(q).AttackTimer = 10000 and Enemy(q).APCurrent = 2 and Enemy(q).Sequence = 1
Reorder()
Enemy(q).APCurrent = Enemy(q).APCurrent-2
Enemy(q).Sequence = 0
Enemy(q).APTotal = 0
BattleOrder(Enemy(q).Sequence).Sequence = 0
Enemy(q).AttackTimer = 0
Endif
If Enemy(q).APTimer > 6000 and Enemy(q).APCurrent < Enemy(q).APMax and Enemy(q).APTotal < Enemy(q).APMax
Enemy(q).APCurrent = Enemy(q).APCurrent+1
Enemy(q).APTotal = Enemy(q).APTotal+1
Enemy(q).APTimer = 0
Endif
If Enemy(q).APCurrent > 0 and Enemy(q).Sequence = 0//Check if AP Point applied
Repeat
If Enemy(q).Sequence = 0
inc x
Endif
Until Enemy(q).Sequence || x
Enemy(q).Sequence = x
/////////////////////////////
//BATTLEORDERING
/////////////////////////////
BattleOrder(x).Sequence = Enemy(q).Sequence
BattleOrder(x).CharacterName = Enemy(q).Name
Endif
Enemy(q).APTimer = Enemy(q).APTimer+Enemy(q).SPD //maybe at bottom instead
If Enemy(q).APTimer > 6000 and Enemy(q).APCurrent = Enemy(q).APMax
Enemy(q).APTimer = 0
Endif
If Enemy(q).Sequence = 1
Enemy(q).AttackTimer = Enemy(q).AttackTimer+Enemy(q).SPD
If Enemy(q).AttackTimer >10000
Enemy(q).AttackTimer = 0
Endif
Endif
EndFunction
Function Reorder()
/////////////////////////////////////////////////
//SOMETHING HERE STOPS IT AFTER 4 or 5 ATTACKS AND CONTINUES TO LOOP THE ONE NEVER CHANGING ORDER
/////////////////////////////////////////////////////
x = x - 1
For FindNext = 0 to 11
Stats(FindNext).Sequence = Stats(FindNext).Sequence - 1
If Stats(FindNext).Sequence < 0
Stats(FindNext).Sequence = 0
Endif
Enemy(FindNext).Sequence = Enemy(FindNext).Sequence - 1
If Enemy(FindNext).Sequence < 0
Enemy(FindNext).Sequence = 0
Endif
BattleOrder(FindNext).Sequence = BattleOrder(FindNext+1).Sequence-1
BattleOrder(FindNext).CharacterName = BattleOrder(FindNext+1).CharacterName
If BattleOrder(FindNext).Sequence < 0
BattleOrder(FindNext).Sequence = 0
Endif
Next FindNext
EndFunction
The triggering code is:
//////////////////////////////////////
//AUTO ATTACK ON OTHER HEROES
//////////////////////////////////////
For n = 0 to 12
If Stats(n).Party = 1
PlayerAI(n)
Endif
Next n
For q = 0 to 12
If Enemy(q).Present = 1
EnemyAI(q)
Endif
Next q
Thanks for any suggestions,
If you need any further explanation of the arrays, let me know.
OK, since I'm sure you'll be asking what the heck these arrays mean... I'll quickly explain. Stats(x) is the hero stats, enemy(x) is self explanatory. Sequence is the order to which they get placed in the queue. AP is the action points allowed to do an attack. And, BattleOrder(x) is the queue that holds all the info (in the end). Hope that helps a bit.