Ultimate Amiga

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: How to implement path AI into my game?  (Read 5383 times)

0 Members and 5 Guests are viewing this topic.

xboxisfinished

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 205
  • Generic Amiga User
    • gamemakermagazine.com
How to implement path AI into my game?
« on: October 24, 2015, 10:33:21 PM »

I will give in details an explanation of what I wanted to do for the computer AI part. (There is no monsters in my game so I intend to use the term computer AI instead.)  Each level such as level1, level2, level3, level4 etc have a certain pattern that I want the computer to follow. For example down below:
Code: [Select]
                                                     Y            X
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0      0*16       (13X16)=208     
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0      1*16       (13X16)=208
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0      2*16       (13X16)=208
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0      3*16       (13X16)=208
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0      4*16       (13X16)=208
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0      5*16       (13X16)=208
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0      6*16       (13X16)=208
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,100,0,0,0,0,0,0  7*16       (13X16)=208
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,33,0,0,0,0,0,0    8*16       (13X16)=208
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,33,0,0,0,0,0,0    9*16       (13X16)=208
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,33,0,0,0,0,0,0   10*16      (13X16)=208
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,33,0,0,0,0,0,0   11*16      (13X16)=208
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,99,0,0,0,0,0,0   12*16      (13X16)=208
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0     13*16      (13X16)=208

So let me explain above now what is this all about. It read from data the X,Y coordinate of 99, 33 and 100. 99 is the starting base where the computer starts from this point...so it plots or appear at 99 and 33 is the path it needs to go to. It will go all the way up to 100 where it ends and every time it moves on 33 it plots 33 on that spot before it goes to the second 33 until it reaches to 100 where that computer (sprite disappears) disappears. However, it is not just 33, it could be 33, 34, 35, 36, 37, 38, 39, 40, 41,42,43 and 44 as the pattern it follows. Now I don't know how do this where it starts at 99 and ends at 100 as it follows 33. But I want to be able to do something like this as well below:

Code: [Select]
                                                     Y            X
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0      0*16       ...
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0      1*16       ...
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0      2*16       ...
Data 0,0,0,0,35,35,40,39,38,37,0,0,0,0,0,0,0,0,0,0      3*16       ...
Data 0,0,0,0,36,0,0,0,0,36,0,0,0,0,0,0,0,0,0,0      4*16       ...
Data 0,0,0,0,33,0,0,0,0,35,0,0,0,0,0,0,0,0,0,0      5*16       ...
Data 0,0,0,0,33,0,0,0,0,33,0,0,0,0,0,0,0,0,0,0      6*16       ...
Data 0,0,0,0,33,0,0,0,0,34,33,33,33,33,0,0,0,0,0,0  7*16       ...
Data 0,0,0,0,33,0,0,0,0,0,0,0,0,33,0,0,0,0,0,0    8*16       ...
Data 0,0,0,0,33,0,0,0,0,0,0,0,0,33,0,0,0,0,0,0    9*16       ...
Data 0,0,0,0,33,0,0,0,0,0,0,0,0,33,0,0,0,0,0,0   10*16      ...
Data 0,0,0,0,100,0,0,0,0,0,0,0,33,0,0,0,0,0,0,0   11*16      ...
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,99,0,0,0,0,0,0   12*16      ...
Data 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0     13*16      ...

So with the above scenario it would start at 99 and complete all the patterns until it reaches to 100. I want it to be able to move and do whatever path it needs but in the end it would fill the screen with that EXACT pattern and always finish at 100...and always move and reach 100 before the computer object disappears.  Any idea how I can do something like this? Any psuedoe code? Any partial code? Any advice on how to do this? Please don't send me links to wikipedia or universe where this is complex C/C++ code or theory of path taken and I have to read 100 page article...it would not help me, increase frustration and put in stuck mode. Any help on how do I start this would be great.

Thanks in advance.
« Last Edit: October 24, 2015, 10:35:25 PM by xboxisfinished »
Logged

KevG

  • A600
  • *
  • Karma: 1
  • Offline Offline
  • Posts: 87
Re: How to implement path AI into my game?
« Reply #1 on: October 25, 2015, 09:13:45 AM »

Quote
Any idea how I can do something like this? Any psuedoe code? Any partial code? Any advice on how to do this? Please don't send me links to wikipedia or universe where this is complex C/C++ code or theory of path taken and I have to read 100 page article...it would not help me, increase frustration and put in stuck mode. Any help on how do I start this would be great.

What you are trying to do is NOT AI. You have defined a predetermined path so how can that be AI?
What you need is a path finding algorithm and one of the easiest to implement is called A* (pronounced A star).
If you are not willing to do any research what-so-ever and only rely on people posting some code for you then good luck.
Logged

xboxisfinished

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 205
  • Generic Amiga User
    • gamemakermagazine.com
Re: How to implement path AI into my game?
« Reply #2 on: October 25, 2015, 08:47:38 PM »

Quote
Any idea how I can do something like this? Any psuedoe code? Any partial code? Any advice on how to do this? Please don't send me links to wikipedia or universe where this is complex C/C++ code or theory of path taken and I have to read 100 page article...it would not help me, increase frustration and put in stuck mode. Any help on how do I start this would be great.

What you are trying to do is NOT AI. You have defined a predetermined path so how can that be AI?
What you need is a path finding algorithm and one of the easiest to implement is called A* (pronounced A star).
If you are not willing to do any research what-so-ever and only rely on people posting some code for you then good luck.

You helped me but I am looking at the A-Star and it is not exactly what I want. With the A-Star it looks for the shortest road to reach it's goal..what I am looking for is explore or fill all the patterns before I reach my goal. So it is a bit different than the A-Star. I am going to put my own pseudo code and see if what I am doing is wrong...

1) Loop and find the starter point and in this case it is 99 and get the X,Y coordinate of that location and plot the computer in that location.
2) Loop through the array for all the points that are not 99,100 and 0 and each loop where I find a value number such as 33 on that X,Y coordinate plot the computer object there until I reach 99. So it will always start from top and come to the end which is the reverse of the first data which it starts at the bottom and completes at the top. How do I solve this problem?
3) Problem: Problem I have with this method while very simple, archiach and hack like it gives the simulation that the computer is jumping from one point to another point while I want it to move smooth from point to another going through this loop. How do I go about doing that?

« Last Edit: October 25, 2015, 09:02:43 PM by xboxisfinished »
Logged

KevG

  • A600
  • *
  • Karma: 1
  • Offline Offline
  • Posts: 87
Re: How to implement path AI into my game?
« Reply #3 on: October 26, 2015, 03:19:33 PM »

OK I think I know what you mean now but why don't you use way points instead?
You could easily do this with the Move command.

Move Y,n,m$
Move X,n,m$

For example Move X 1,"(1,5,20)"
Logged

xboxisfinished

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 205
  • Generic Amiga User
    • gamemakermagazine.com
Re: How to implement path AI into my game?
« Reply #4 on: October 26, 2015, 04:48:52 PM »

OK I think I know what you mean now but why don't you use way points instead?
You could easily do this with the Move command.

Move Y,n,m$
Move X,n,m$

For example Move X 1,"(1,5,20)"

I never heard of this command before. But I want to be able to read from the Data the starting point, the end point, the paths it takes before reaching the end point (not skipping all the available paths) and starts from the start point regardless of it being on the top of the Data, bottom of the Data, right side of the Data, middle side of the Data etc. But when I do the loop statement it always starts from top to bottom since the loop is:

For i=0 to <Not sure what this value is and where I get it from> so it starts from Data where X,Y (0,0) at the top all the time and reaches till the end. So I don't know how to start the path from the point where 99 is (even though I can plot the sprite very well on that spot on the screen no problem).

Can you tell me more about this command? Why is there X,Y Move command, what is n and what is m$ and why is m$ (1,5,20) what is that?
Logged

KevG

  • A600
  • *
  • Karma: 1
  • Offline Offline
  • Posts: 87
Re: How to implement path AI into my game?
« Reply #5 on: October 26, 2015, 10:37:31 PM »

You have got to do some research! You cannot expect people to code the game for you.

1. Do a search in google for the AMOS manual.
2. Look in the index for the Move command.
3. Understand how it works.
4. Experiment with it by moving a bob around the screen.
5. Once you understand it, implement it in your code.
6. Test, test and test.....

People are here to help you but you have got to help yourself as well.

(I'm out. Someone else have a go PLEASE!)
Logged

Sidewinder

  • Forum Mod
  • A600
  • *****
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 155
    • http://www.liquido2.com/
Re: How to implement path AI into my game?
« Reply #6 on: October 27, 2015, 04:25:36 PM »

So you could approach it with a simple set of if statements that are executed just before the bob is updated:

Code: [Select]
Start at location 99
Before each update perform the following tests:
If current space = 100 then remove the object
Else If space above < 100 then move up 1 space
Else if space to the right < 100 then move right 1 space
Else if space below < 100 then move down 1 space
Else if space the the left < 100 then move left 1 space

If you want the object to prefer moving in a different direction, move that test nearer to the top of the list.  For instance, if you had a T intersection in a path, you could set the tests above to prefer moving left by switching the left/right if statements. 

A bit more complex solution could include testing multiple directions and taking the one that has the larger value.  You could encode the preference into the data so that the object would move to the larger number first:

Code: [Select]
Data 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0
Data 0,100, 33, 33, 33, 40, 33, 20, 33,100,  0
Data 0,  0,  0,  0,  0,  0, 33,  0,  0,  0,  0
Data 0,  0,  0,  0,  0,  0, 99,  0,  0,  0,  0
Data 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0

In the above case the object would come to the T intersection, the space above it would be 0, so it would then test both the right and left space.  Left = 40, right = 20.  40 is larger than 20, so it would go left.

Hopefully this is understandable and useful.
« Last Edit: October 27, 2015, 04:27:23 PM by Sidewinder »
Logged
- Sidewinder

xboxisfinished

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 205
  • Generic Amiga User
    • gamemakermagazine.com
Re: How to implement path AI into my game?
« Reply #7 on: October 27, 2015, 05:39:03 PM »

So you could approach it with a simple set of if statements that are executed just before the bob is updated:

Code: [Select]
Start at location 99
Before each update perform the following tests:
If current space = 100 then remove the object
Else If space above < 100 then move up 1 space
Else if space to the right < 100 then move right 1 space
Else if space below < 100 then move down 1 space
Else if space the the left < 100 then move left 1 space

If you want the object to prefer moving in a different direction, move that test nearer to the top of the list.  For instance, if you had a T intersection in a path, you could set the tests above to prefer moving left by switching the left/right if statements. 

A bit more complex solution could include testing multiple directions and taking the one that has the larger value.  You could encode the preference into the data so that the object would move to the larger number first:

Code: [Select]
Data 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0
Data 0,100, 33, 33, 33, 40, 33, 20, 33,100,  0
Data 0,  0,  0,  0,  0,  0, 33,  0,  0,  0,  0
Data 0,  0,  0,  0,  0,  0, 99,  0,  0,  0,  0
Data 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0

In the above case the object would come to the T intersection, the space above it would be 0, so it would then test both the right and left space.  Left = 40, right = 20.  40 is larger than 20, so it would go left.

Hopefully this is understandable and useful.

I understand now the logic that you are going at...and I love it. However, I have one more question to ask and I promise that will be it.

I will take your sample Data as a reference for example down below to ask my question and do little modification:

Code: [Select]
Data 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0
Data 0,100, 33, 33, 33, 40, 33, 20, 33,34,  0
Data 0,  0,  0,  0,  0,  0, 33,  0,  0,  0,  0
Data 0,  0,  0,  0,  0,  0, 99,  0,  0,  0,  0
Data 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0

I go through two nested loops that will loop through the X,Y coordinate of the entire Data set right? Within that loop I am looking for 99 as a starter to start plotting and while still looping from that point I want to search now for the path that have values not equal to 0 but not equal to 100 either and start following the path. Say 99 is almost at the end of the loop and I found the 99 finally....now how do I continue looping (when the loop is almost finished) and start from 99 and use the If statements you pointed out to move that one step. Assuming I moved to the right from the point of 40 and followed the path 33, 20, 33, and 34 and stopped there as there are no longer paths to follow but I did not find 100 still. (Remember the loop is almost about to finish as I am doing all of this) How do I go about making it return back to the point of 40 and search for new path and not make it find path 33 at the bottom of 40 where I came from at the beginning and go back to 99 and go all mixed up this way? How do I also avoid it from picking the left path first instead of the right path and then when it hits 100 it finished and it missed the right path and did not fill it up? There is a potential it could do that as well. What I need it to do is before reaching 100% it exhausts all paths first without returning back to the path where it starter from..from the first place. For example down below I will give you a scenario:

Code: [Select]
             100,33,33,33,40
                         33                   33
                         33                   33
           33,33,33,50,33,33,33,33,33
                         33
                         33
                         33
                         33
                         99


When it starts from 99 and it goes to 50 the point here at the road it have the choice between up, left and right (not down) since it will return back to where it came from. If it choose up and hit 40 and turn left it would reach 100 and skip the left and right side of the 50 point. But if it picked the left side of 50 point it would need to return back to the 50 point again. I don't want it to go back to the left side or go down again, if it picked the up point again...it would again miss the right side and go 100 and not fill up all the pattern. However, if it decided to go to the right side and fill up that pattern and return back to the point 50 it then knows it have explored left, right and down it would go up and then fill up to pattern 100. However, right now it could easily skip left, right and go all the way up and go to point 100 or go to the right, return back to point 50 and go up and skip the left side, or vice versa or go back to bottom where it starter...messy...and how many loops can I do to make scan all possible scenarios or would that mean I need a while loop instead where until I see 100 keeps looping instead of the for loop where eventually it will stop looping before all search is done?
Logged

Sidewinder

  • Forum Mod
  • A600
  • *****
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 155
    • http://www.liquido2.com/
Re: How to implement path AI into my game?
« Reply #8 on: October 27, 2015, 08:13:10 PM »

This is an interesting problem.

First things first, looping through a data set like this will be painfully slow, unpractical really for any purpose other than proof of concept.  What I would do is create a 2 dimensional array and copy the contents of the data set to the array during the first loop.  I would also record the X and Y coordinates of the start point.

Code: [Select]
Read XSIZE,YSIZE
Dim PATH(XSIZE,YSIZE)

For A=0 To YSIZE-1
   For B=0 To XSIZE-1
      Read VALUE
      ' Copy the read value to the array
      PATH(A,B)=VALUE
      ' Store the coordinates of the start position
      If VALUE=99
         X=A
         Y=B
      End If
   Next B
Next A

Do
   ' Get the value at our current location
   VALUE=PATH(X,Y)

   ' Bail out of the loop if we're at the end
   Exit If VALUE=100

   ' Get the surrounding values
   TOP=PATH(X,Y-1)
   BOTTOM=PATH(X,Y+1)
   LEFT=PATH(X-1,Y)
   RIGHT=PATH(X+1,Y)

   If RIGHT>0
      Inc X
   Else If LEFT>0
      Dec X
   Else If TOP>0
      Dec Y
   Else If BOTTOM>0
      Inc Y
   Else
      ' If we get here we're stuck on an island
      Exit
   End If
     
Loop

Data 11,10
Data 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0
Data 0,100, 33, 33, 33, 40,  0,  0,  0,  0,  0
Data 0,  0,  0,  0,  0, 33,  0,  0,  0, 33,  0
Data 0,  0,  0,  0,  0, 33,  0,  0,  0, 33,  0
Data 0,  0, 33, 33, 33, 50, 33, 33, 33, 33,  0
Data 0,  0,  0,  0,  0, 33,  0,  0,  0,  0,  0
Data 0,  0,  0,  0,  0, 33,  0,  0,  0,  0,  0
Data 0,  0,  0,  0,  0, 33,  0,  0,  0,  0,  0
Data 0,  0,  0,  0,  0, 99,  0,  0,  0,  0,  0
Data 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0

(The above code hasn't been tested since I'm not near my Amiga, but it should give you a general idea of what I'm thinking.)

Now, there may be a problem when exploring a maze like this where we try to go through a path more than once.  We'd like to prevent this.  To do so we can use weights.  Once we visit a location we set its value to something really high, say 90.  Then, when we get to our decision tree we test the values of all the possible paths and take the one that is greater than 0 but less than 90.  You could also use weights to prevent the object from going up the vertical path first and quickly finding the end before going left and right at the intersection.  To do this you give the spaces above the intersection a higher weight than the right and left paths, but still less than 90.  Then you write a routine that calculates the lowest weighted path and goes there first.
« Last Edit: October 27, 2015, 08:16:13 PM by Sidewinder »
Logged
- Sidewinder

xboxisfinished

  • A600
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 205
  • Generic Amiga User
    • gamemakermagazine.com
Re: How to implement path AI into my game?
« Reply #9 on: October 27, 2015, 11:11:27 PM »

Thank you so much for all your help sidewinder. You are the best :D You guided me to the starter steps that I need :) I will work on this and try to solve the problem or obstacle on my own when I am coding. I will post any update here as a reply to where I am...if I am stuck more or if I actually solved it and got it working.  The weight system you said here is really is the best solution or system I have. 

 I want to ask your opinion before I start cracking and working....I want to use your code as a reference to my idea. Say I am at a point 50 and it moves to the left..it will keep exploring all the sides of fifty until there is no more exploration in fifty. Then it will return back to it's track and everytime it returns back to it's track it sets in array in that array as -1. When it sets it as -1 it means it is a block, never explore that area again...this way it will prevent it from actually reexplore the same path more than ones. Also, what I would do is another system instead of weight I am thinking..before it explores it does a visual check till the end of the line...say as an example it decides to go to the left side but before it moves it looks at the end of the path is there a 100 at the end...if there is ...don't explore that path yet...go somewhere else. If it does not see a 100 at the end of the path...then it explores that side. I want it to be ever smarter...because when it does -1 it means it blocks that path..so I want to make it smart enough that it does not block the path in away it never reaches 100...do you know what I mean?

I want to write down the logic and AI completely on a piece of paper where it avoids any potential of getting stuck, run time error and all possible combinations where it at the end it can still achieve the goal of reaching 100 regardless of shape or design of the pathes...when I am satisfied I will implement that codee. What you give me here is the ground...the foundation the soil to plant my farm. I want to thank you so much for giving me the foundatstemion , ground to build my system on  ;D ;D :D
 
Code: [Select]
             100,33,33,33,40 
                         33                   33
                         33                   33
           33,33,33,50,33,33,33,33,33
                         33
                         33
                         33
                         33
                         99
Logged

Sidewinder

  • Forum Mod
  • A600
  • *****
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 155
    • http://www.liquido2.com/
Re: How to implement path AI into my game?
« Reply #10 on: October 28, 2015, 02:43:21 AM »

No problem, I'm glad you find my code useful.

One thing to watch out for when changing the values for visited paths to -1 or some other value is that it may be necessary to go back over some of the previous paths in order to return to unexplored paths.   You don't want to paint yourself into a corner.  :-)

Your idea to scan to the end of a path, looking for the end is an interesting one, and it could help prevent premature path ends.  I look forward to seeing the routine you come up with.  Feel free to ask more questions or share ideas as you think this through.  I've often found that attempting to explain a new routine to someone else helps me flush out the details, and having more than one pair of eyes can help spot bugs.

Happy programming!
Logged
- Sidewinder
Pages: [1]   Go Up
 

TinyPortal 2.2.2 © 2005-2022