Saturday 16 April 2016

Further AI Implementation

Following on from my last post, AI Implementation, I have now discovered where I was going wrong with the path lengths, and furthered the AI decision making to encompass one side of my decision tree.

With the path lengths issue, I now realise that my base understanding of Unity was flawed, and this is where the issue stemmed from. While I was changing the target in the turn update, the path was not being retraced. I had believed that Update() methods ran parallel, meaning all update methods ran at the same time. My lecturer explained to me that instead, Unity ran through game objects, running the update method on them sequentially, based on Unity's built-in building system.

To deal with this issue, I was told that I would need to retrace the path after the target had moved. This was done by creating a public FindPath() method in pathfinding.cs. This is then called on every time after the target has been moved, returning the correct values for the path count.

Once this was working correctly, I could move onto the first point of my decision tree: 

Is the enemy in range?
As I could work out the path lengths, this meant I could now determine which User Units would be in range of the AI. For each User unit, the target positioned itself on them, retraced the path, then compared the path<> Count - 1 to the AI moveLimit. The minus 1 is added, as the AI does not need to be on the same square as the user unit, but an adjacent square. If the path count is up to and including the user units current node, it does not need to be counted. If the path count - 1 is less than or equal to to the moveLimit, that user unit is added to the inRange<> list.

Is there more than one? and Does my weapon beat any of theirs?
Originally I had intended to check the inRange list once it was populated, check to see if there is more than 1 in the list, and if so, check through that list to see if any user unit equippedWeapon weaponTypes matched the AI's equippedWeapon's posType. If true, they would have been added to the posWeapType<> list.

Instead of this, I ran an if statement after a unit had been added to inRange, checking the weapon type. If it returns true, that unit is also added to the posWeapType list. So, if there is only one unit in range, that will become the target, but if there are multiple, and one falls in the posWeapType list, they will default to the target.

While working on the AI, I started to notice some issues with the movement:

  • When a user unit moved, the path would remain constant, from the initial starting point, to the target, until the unit had reached the end of the path.
  • At the same time, when the AI moved, the path was retracing, shrinking with the movement of the AI, throwing off numbers, meaning the AI would often be one or two tiles behind where they should have been.
  • I changed a lot of the moveCurrentPlayer's if statements, in an attempt to streamline them, and ensure it worked with no errors for both a target set beyond the current units move limit and a target's which has a path shorter than the current units move limit.
  • The main issue was stemming from the fact I was setting the pathfinding.cs bool pathTraced to false, so the path was getting retraced when it shouldn't. I placed the FindPath() method at the end of the function, and this seemed to sort out all movement issues.


Currently, the AI defaults to attack the first in the list, depending on if the bool for a unit / units being in the posWeapType returns true, or just units inRange.

My next steps will be to implement the AI decisions for no enemies in range, and then tidy up the AI coding, streamlining it as much as possible.

No comments:

Post a Comment