This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-07-31 13:28:59

bullelk12
Scratcher
Registered: 2012-05-26
Posts: 100+

pathing AI

I'm working on a type of tower defense game and I need the enemies to move across the map towards a castle. But they can't move through any walls, over any mountains etc. I know there's a way to do it but the scripts are astronomically large. Is there a way to do this.


http://mag.racked.eu/cimage/i6000/Achievement++get%21/Scratcher+love+minecraft%21/mca.png

Offline

 

#2 2012-08-01 03:23:56

BirdByte
Scratcher
Registered: 2012-07-07
Posts: 1000+

Re: pathing AI

I'll look into this further and post a script.  smile


http://i50.tinypic.com/312u714.jpg

Offline

 

#3 2012-08-01 03:30:41

BirdByte
Scratcher
Registered: 2012-07-07
Posts: 1000+

Re: pathing AI

Well, the best way to do this is by mapping it out manually. Here is an example:

repeat (240)
 change x by (1)
end
repeat (20)
 change y by (1)
end
repeat (180)
 change x by (1)
end
repeat (20)
 change y by (-1)
end
repeat (60)
 change x by (1)
end
If your path needs to be AI, computing the shortest unblocked way, I have a few questions:

Are your mountains/walls separate sprites, or are they part of the stage?
Is your map simple enough for the touching color blocks to be usable?
Are you trying to make this 1s1s?


http://i50.tinypic.com/312u714.jpg

Offline

 

#4 2012-08-01 04:49:20

joefarebrother
Scratcher
Registered: 2011-04-08
Posts: 1000+

Re: pathing AI

Well you could have a preset list of directions in a format

direction
steps
direction
steps...

and then a script

set [index v] to [1]
go to x:(start x) y:(start y)
point in direction (start direction)
repeat (length of [path list v])
  turn left (item (index) of [path list v]) degrees
  change [index v] by (1)
  move (item (index) of [path list v]) steps
  change [index v] by (1)
end
and then to generate the paths, make a sprite shaped like an arrowhead and a local variable called current action and do

when gf clicked
delete (all v) of [path list v]
set [current action v] to [turn]
add [0] to [path list v]
go to x:(start x) y:(start y)
point in direction (start direction)

when key [up arrow v] pressed
move (1) steps
if <(current action) = [move]>
  replace item (last v) of [path list v] with ((item (last v) of [path list v]) + (1))
else
  set [current action v] to [move]
  add [1] to [path list v]
end

when key [down arrow v] pressed
move (-1) steps
if <(current action) = [move]>
  replace item (last v) of [path list v] with ((item (last v) of [path list v]) - (1))
else
  set [current action v] to [move]
  add [-1] to [path list v]
end

when key [left arrow v] pressed
turn left (1) degrees
if <(current action) = [turn]>
  replace item (last v) of [path list v] with ((item (last v) of [path list v]) + (1))
else
  set [current action v] to [turn]
  add [1] to [path list v]
end

when key [right arrow v] pressed
turn right (1) degrees
if <(current action) = [turn]>
  replace item (last v) of [path list v] with ((item (last v) of [path list v]) - (1))
else
  set [current action v] to [turn]
  add [-1] to [path list v]
end
You can delete this sprite after the path has bees generated, or just hide it and detach the when key pressed blocks from their scripts so you could use it again later.


My latest project is called http://tinyurl.com/d2m8hne! It has http://tinyurl.com/d395ygk views, http://tinyurl.com/cnasmt7 love-its, and http://tinyurl.com/bwjy8xs comments.
http://tinyurl.com/756anbk   http://tinyurl.com/iplaychess

Offline

 

#5 2012-08-01 16:22:48

Prestige
Scratcher
Registered: 2008-12-15
Posts: 100+

Re: pathing AI

I also like the list suggestion. You could also expand it to make it more advanced by allowing enemies of different speeds.

Use this simple equation to work out the time taken by a sprite of a known speed over a known distance - time = distance/speed.

So:

1) Add all your positions for the AI to a list, add them as x position, y position.

(In this example, my points are (150, 0), (20,40), (0,0)...)

add (150) to [list v]
add (0) to [list v]
add (20) to [list v]
add (40) to [list v]
add (0) to [list v]
add (0) to [list v]
2) Now, we'll need to move to the next point. You might also want the enemy to point towards the position that they're moving to. For that, see this tutorial on how to make a sprite point towards a co-ordinate.

set [counter v] to (1)
set x to (item (counter) of [list v])
set y to (item ((counter) + (1)) of [list v])
Change [counter v] by (2)
Repeat (((length of [list v]) / (2)) - (1))


Set [distance v] to (Sqrt((((X position) - (item (counter) of [list v]) * (X position) - (item (counter) of [list v])) + ((Y position) - (item ((counter) + (1)) of [list v]) * (Y position) - (item ((counter) + (1)) of [list v])))))


Glide ((distance) / (speed)) secs to x: (item (counter) of [list v]) y: (item ((counter) +(1)) of [list v])
change [counter v] by (2)
end

Last edited by Prestige (2012-08-01 16:27:45)


"Don't insult someone until you've walked a mile in their shoes. That way, if they don't like what you have to say, you'll be a mile away and still have their shoes  smile  "

Offline

 

#6 2012-08-01 16:34:45

Wes64
Scratcher
Registered: 2011-08-19
Posts: 1000+

Re: pathing AI

Prestige wrote:

set [counter v] to (1)
set x to (item (counter) of [list v])
set y to (item ((counter) + (1)) of [list v])
Change [counter v] by (2)
Repeat (((length of [list v]) / (2)) - (1))


Set [distance v] to ([sqrt v] of ((((X position) - (item (counter) of [list v])) * ((X position) - (item (counter) of [list v]))) + (((Y position) - (item ((counter) + (1)) of [list v])) * ((Y position) - (item ((counter) + (1)) of [list v])))))


Glide ((distance) / (speed)) secs to x: (item (counter) of [list v]) y: (item ((counter) +(1)) of [list v])
change [counter v] by (2)
end

fixed


Experienced 2.0 Tester: Ask me questions!
Using Firefox 13.0, Flash plugin version 11.4.402.287, and Windows XP Professional.

Offline

 

#7 2012-08-01 18:26:47

BoltBait
Scratcher
Registered: 2009-03-09
Posts: 1000+

Re: pathing AI

I have implemented a pathing algorithm in the following game: http://scratch.mit.edu/projects/BoltBait/1897019

I wrote it from the description given in this post: http://scratch.mit.edu/forums/viewtopic.php?id=35318

Hope this helps.


Animated sigs must be banned!
http://boltbait.com/j.pnghttp://boltbait.com/s.pnghttp://boltbait.com/d.pnghttp://boltbait.com/a.pnghttp://boltbait.com/p.png

Offline

 

Board footer