There's really no such thing as simple pathfinding. You generally stuck with needing some kind of recursion. I have found a way to do it without recursion in a generally straight-forward way. You have to essentially cover the entire field with ghosted rectangles, in which every rectangle can get to any point in an adjacent rectangle using a straight-line path.
Example (gray spots are walls, colored rectangles are the "invisible" rectangles):
Then, to circumvent the recursion algorithm, you have to manually create a list of "if I am in rectangle X and I want to get to rectangle Y, I should point at rectangle Z". Once the sprite is in the rectangle it wants to be in, it can take a straight-line path to the exact point. This approach can take a very long time to program if you have a lot of rectangles, but it gets the job done.
Edit: the ability to import lists can come in handy, as it is easier to type up these incredibly long lists than to manually create them.
Last edited by MoreGamesNow (2012-08-18 12:05:19)
Offline
If it always takes the same path, you can also set the exact path. Not true AI though.
Example:
whenclicked
iflevel=1setalive ▼to1go to x:0y:0showrepeat untilalive=0glide1secs to x:5y:0glide1secs to x:5y:5glide1secs to x:0y:5glide1secs to x:0y:0hide
Offline
All AIs are different. Try this.
foreverReplace this with your loop...deleteall ▼ofpaths ▼change x by20Width of path.iftouchingwalls ▼?add100000topaths ▼elseadddistance totoplayer ▼paths ▼change x by-20Width of path, negative.change y by-20Width of path, negative.iftouchingwalls ▼?add100000topaths ▼elseadddistance totoplayer ▼paths ▼change y by20Width of path.change x by-20Width of path, negative.iftouchingwalls ▼?add100000topaths ▼elseadddistance totoplayer ▼paths ▼change x by20Width of path.change y by20Width of path.iftouchingwalls ▼?add100000topaths ▼elseadddistance totoplayer ▼paths ▼change y by-20Width of path, negative.deleteall ▼ofdirections ▼add1todirections ▼add2todirections ▼add3todirections ▼add4todirections ▼setsorted ▼to0repeat untilsorted=1setsorted ▼to1setcounter ▼to1repeat3ifitem>counterofpaths ▼itemofcounter+1paths ▼setsorted ▼to0settemp ▼toitemofcounter+1paths ▼replace itemofcounter+1paths ▼withitemcounterofpaths ▼replace itemcounterofpaths ▼withtempsettemp ▼toitemofcounter+1directions ▼replace itemofcounter+1directions ▼withitemcounterofdirections ▼replace itemcounterofdirections ▼withtempchangecounter ▼by1point in direction90*item1 ▼ofdirections ▼move20stepsWidth of path.point in direction90 ▼
Offline
BirdByte wrote:
All AIs are different. Try this.
foreverReplace this with your loop...deleteall ▼ofpaths ▼change x by20Width of path.iftouchingwalls ▼?add100000topaths ▼elseadddistance totoplayer ▼paths ▼change x by-20Width of path, negative.change y by-20Width of path, negative.iftouchingwalls ▼?add100000topaths ▼elseadddistance totoplayer ▼paths ▼change y by20Width of path.change x by-20Width of path, negative.iftouchingwalls ▼?add100000topaths ▼elseadddistance totoplayer ▼paths ▼change x by20Width of path.change y by20Width of path.iftouchingwalls ▼?add100000topaths ▼elseadddistance totoplayer ▼paths ▼change y by-20Width of path, negative.deleteall ▼ofdirections ▼add1todirections ▼add2todirections ▼add3todirections ▼add4todirections ▼setsorted ▼to0repeat untilsorted=1setsorted ▼to1setcounter ▼to1repeat3ifitem>counterofpaths ▼itemofcounter+1paths ▼setsorted ▼to0settemp ▼toitemofcounter+1paths ▼replace itemofcounter+1paths ▼withitemcounterofpaths ▼replace itemcounterofpaths ▼withtempsettemp ▼toitemofcounter+1directions ▼replace itemofcounter+1directions ▼withitemcounterofdirections ▼replace itemcounterofdirections ▼withtempchangecounter ▼by1point in direction90*item1 ▼ofdirections ▼move20stepsWidth of path.point in direction90 ▼
Oops! Fixed. Use this one instead.
Offline
2 things ,1, thank you very much ,if i make a project with this i will give a very high credit to you and 2 can anyone please explain this script in detail
Offline
Jodymoses wrote:
Isn't paths not going to any use because no motion blocks are used?
What do you mean by no motion blocks used? I see several...
Offline
I don't have the time to interpret that entire script, birdbyte? Perhaps you could explain this massive script to Jodymoses?
Offline
I'm not going to try to understand the script, but it seems that "paths" affects "directions", which affects movement:
point in direction90*item1 ▼ofdirections ▼move20steps
Offline
http://wiki.scratch.mit.edu/wiki/Artificial_Intelligence
http://scratch.mit.edu/projects/Lucario621/803990
Offline
Jodymoses wrote:
Why do you add the below?
whenclicked
iftouching {walls}add10000to Paths
Assuming he's following a sort of recursive algorithm that's trying to find the shortest path, making one of the path-ways "10000" basically means such a path will never be optimal. Rather than adding extra conditionals to take walls into account, such routes are simply ignored because they are so high (or bad).
Offline