AI for a grid-based board game

Get help using Construct 2

Post » Tue Sep 10, 2013 8:13 am

This is for my board game. I am now at the final stage before it can be considered a working prototype. This is also the hardest stage: creating the computer behavior.

I've created AI systems in the past, and computer behavior is an area that greatly fascinates me. Here are my notes regarding how the computer should behave.

Computer program selection order:
1. Computer uses programs in order of highest range (ComputerPrograms.Range) to shortest range.

Priority order:
1. Destroy player programs if they are in range and capable of being killed by a computer program this turn.
2. (Default) Move toward and attack the nearest player program.

Local variables:
TargetPlayerProgram: The UID of the player program targeted by the computer program.

Destruction potential check:
1. Loop through each player program and compare its active parts (PlayerPrograms.PartsActive) to the currently selected computer program's damage (ComputerPrograms.Damage).
2. If a player program is able to be killed by the currently selected computer program (PlayerPrograms.PartsActive = ComputerPrograms.Damage), then set its UID to TargetPlayerProgram.
Highlight all movable squares (call function "HighlightMove") and pick the highlighted square closest to the player program, then do a range calculation for X and Y (squares are 64px in size, so closest square X + ComputerPrograms.Range * 64 >= PlayerPrograms.X OR PlayerProgramParts.X; repeat for Y).
3. If no player program is able to be killed this turn, proceed to 'Default behavior'
4. If range calculation succeeds, then move computer program to nearest square and attack.
5. If range calculation fails, then proceed to 'Default behavior'

Default behavior:
1. Loop through each player program and compare distance along X and Y axes (distance(PlayerPrograms, ComputerPrograms)). Set closest player program to TargetPlayerProgram.
2. Move computer program to the closest square.
3. Try to attack the player program. Call 'AttackHighlight' and check if target program is overlapping an attack indicator.
If so, then deal damage. If not, then do nothing (set ComputerPrograms.Finished = true).

This thread is intended to keep all my questions in a single location instead of spreading them out over several 'How To' posts in the forum.

First issue: How do I pick the program with the highest range? In the case of conflicts, I am seeking to just pick randomly among the ones that are tied for the highest.



BoardGame.capx

Basically you have to compare the variable ComputerPrograms.Range. ComputerPrograms is a family, and Range is a family instance variable. I'm having trouble figuring out how I would compare family members against each other.Excal2013-09-10 08:14:52
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,234

Post » Tue Sep 10, 2013 12:49 pm

Currently working at a similar AI picking problem for collision prevention.

Got the stage, that you need to store an ID of the family Instance to compare "Range" against.

Hope this helps.
B
15
S
3
G
5
Posts: 396
Reputation: 4,977

Post » Tue Sep 10, 2013 9:12 pm

I understand I have to store an ID of the family instance.

However, in terms of the comparison, how would I reference two objects within a family in the same block of code?
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,234

Post » Wed Sep 11, 2013 6:33 am

This is a solution (although a little sloppy).

Cool project, by the way. Please let me know if there is anything else I can do to help.makotto2013-09-11 06:39:15
B
28
S
10
G
10
Posts: 97
Reputation: 6,584

Post » Wed Sep 11, 2013 7:30 am

I just realized that the previous capx will work if you only want the ComputerPrograms.Range with highest value.
If you have multiple instances of family members, it will be better to store the UIDs in a 1D array, sorted from highest range to lowest range. This way the AI will start with UID at array position=0 (highest range), then array position=1, and so on.
This is the CAPX
The text instances are only there so you can see the array. I initialized all the elements on the array to some negative number. When you implement the function to read the instances from the array, you can stop when the function reads a number < 0.

Hope that this helps!
B
28
S
10
G
10
Posts: 97
Reputation: 6,584

Post » Wed Sep 11, 2013 12:08 pm

@makotto, that does it, thanks!



I've also confirmed the array contents in the debugger.

I'll get to work on the default behavior next and let you know if I have any issues. AI is a tough subject!
Project Lead of Zems Online Card Game

Producer at Impulse Limited
B
18
S
6
G
3
Posts: 677
Reputation: 5,234


Return to How do I....?

Who is online

Users browsing this forum: evillair, NumberOne, rbailey83 and 7 guests