Results 1 to 5 of 5

Thread: Pathing

  1. #1

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Pathing

    Lately I've been trying to create a good pathing code... well good for a intermediate-beginner..

    Anyways, what I'm going on is something like this:

    Instead of active pathing (constantly calculating) I just get the path at start and save it in a string, excepting when a unit gets in way, then I make a "subpath" around it.

    The order would be something similar to:

    *I'm using a tile system, any tile with value above X is impassable
    *The unit's can move in 8 directions

    -Set terrain (temporarily) to show unit has already stepped there
    -start loop here
    -Get angle from unit to destination, round off to nearest 45.
    -start a for loop for checking movement
    -Check if tile at x+int(sin(angle)+.5),y+int(sin(angle)+.5) is passable
    *-If passable then move, set terrain to show unit has been here, add to string
    *-If not passable then start rotating check around (if it started at 0, it would then check 45, 315, 90, 270 etc), exit for
    -end for loop
    -If variable of for loop is greater then 315 then back up, look for new path
    -If you back up so much your back at starting point, then you can't reach, don't bother moving.
    -End loop
    **-Subpath between some points to check if path can be shortened

    Is this a very efficient system? Or is there a better way?
    Don't pay attention to this signature, it's contradictory.

  2. #2
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    The industry standard algorithm for pathfinding is the A* (A Star) algorithm. A couple of google searches should turn up some usable pseudocode for you. However, the VB implementation is likely to be hellish, and you wont find any actual code, as the implementation is really very specific to the game itself.

    Z.

  3. #3

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    hmmm

    http://www.gameai.com/antraces.html

    This one was really interesting for anyone else who wants to learn about it.. Take every possible route until you reach your destination
    Don't pay attention to this signature, it's contradictory.

  4. #4
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Look at this page...there is an introduction to pathfinding using A*...

    http://www.advantage.co.nz/ur/

  5. #5

    Thread Starter
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Well

    I've finished most of it... it's in QBasic though, so some commands won't work in VB.

    VB Code:
    1. DECLARE SUB PathMove (x!, y!, Speed!, Direction!)
    2. DECLARE FUNCTION CoSng! (x AS STRING)
    3. DECLARE FUNCTION Deg! (x!)
    4. DECLARE FUNCTION Arcsin! (x!)
    5. DECLARE FUNCTION Rad! (x!)
    6. DECLARE FUNCTION CoStr$ (x!)
    7. DECLARE FUNCTION Target! (SourceX!, SourceY!, DestX!, DestY!)
    8. CONST PI = 3.141592654#
    9. COMMON SHARED GameSpeed AS SINGLE
    10. GameSpeed = 1
    11. SCREEN 13
    12. DIM TerStat(-1 TO 20, -1 TO 12)
    13. DIM Path AS STRING
    14. DIM TempString AS STRING
    15. DIM SubPath AS STRING
    16. RANDOMIZE TIMER
    17. 1
    18.  
    19. FOR a = 0 TO 19
    20.  FOR b = 0 TO 11
    21.   IF INT(RND * 4) THEN TerStat(a, b) = 0 ELSE TerStat(a, b) = 1
    22.   LINE (a * 16, b * 16)-(a * 16 + 15, b * 16 + 15), TerStat(a, b), BF
    23.  NEXT b
    24. NEXT a
    25.  
    26. 'Set impassable border around map to stop out-of-bounds
    27. a = -1
    28. FOR b = -1 TO 12
    29. TerStat(a, b) = 1
    30. NEXT b
    31. a = 20
    32. FOR b = -1 TO 12
    33. TerStat(a, b) = 1
    34. NEXT b
    35. b = -1
    36. FOR a = -1 TO 20
    37. TerStat(a, b) = 1
    38. NEXT a
    39. b = 12
    40. FOR a = 0 TO 19
    41. TerStat(a, b) = 1
    42. NEXT a
    43.  
    44. x = INT(RND * 19)
    45. y = INT(RND * 11)
    46. Dx = INT(RND * 19)
    47. Dy = INT(RND * 11)
    48.  
    49. Tx = x
    50. Ty = y
    51. LINE (Tx * 16, Ty * 16)-(Tx * 16 + 15, Ty * 16 + 15), 14, BF
    52. LINE (Dx * 16 + 5, Dy * 16 + 5)-(Dx * 16 + 10, Dy * 16 + 10), 4, BF
    53.  
    54. GOSUB FindPath
    55.  
    56. FOR a = 1 TO LEN(Path)
    57. Angle = CoSng(MID$(Path, a, 1)) * 45
    58. CALL PathMove(x, y, 1, Angle)
    59. NEXT a
    60.  
    61. Path =
    62.  
    63. CLS
    64. GOTO 1
    65.  
    66.  
    67. FindPath:
    68.  TerStat(Tx, Ty) = TerStat(Tx, Ty) + 8
    69.  IF TerStat(Dx, Dy) > 0 THEN
    70.   FOR Increase = 0 TO 5
    71.    FOR a = Dx - Increase TO Dx + Increase
    72.     FOR b = Dy - Increase TO Dy + Increase
    73.      LINE (a * 16 + 5, b * 16 + 5)-(a * 16 + 10, b * 16 + 10), 4, BF
    74.      IF a < 20 AND a > -1 AND b < 12 AND b > -1 THEN IF TerStat(a, b) = 0 THEN Dx = a: Dy = b: GOTO FoundSpot
    75.     NEXT b
    76.    NEXT a
    77.   NEXT Increase
    78.  END IF
    79. FoundSpot:
    80.  DO UNTIL INKEY$ = CHR$(27)
    81.   Angle = INT(Target(Tx, Ty, Dx, Dy) / 45) * 45
    82.   FOR r = 0 TO 10000
    83.    IF INKEY$ = CHR$(27) THEN END
    84.   NEXT r
    85.   FOR Increase = 45 TO 315 STEP 45
    86.    IF TerStat(Tx + INT(SIN(Rad(Angle)) + .5), Ty + INT(COS(Rad(Angle)) + .5)) < 1 THEN
    87.     CALL PathMove(Tx, Ty, 1, Angle)
    88.     TerStat(Tx, Ty) = TerStat(Tx, Ty) + 8
    89.     Path = Path + CoStr$(Angle / 45)
    90.     LINE (Tx * 16, Ty * 16)-(Tx * 16 + 15, Ty * 16 + 15), 12, BF
    91.     IF Tx = Dx AND Ty = Dy THEN EXIT DO
    92.     EXIT FOR
    93.    ELSE
    94.     IF UpDown = 0 THEN Angle = Angle + Increase: UpDown = 1 ELSE Angle = Angle - Increase: UpDown = 0
    95.    END IF
    96.   NEXT Increase
    97.   IF Increase = 360 AND Tx = x AND Ty = y THEN Path =
    98. : EXIT DO
    99.   IF Increase = 360 THEN LINE (Tx * 16, Ty * 16)-(Tx * 16 + 15, Ty * 16 + 15), 2, BF: Angle = CoSng(RIGHT$(Path, 1)) * 45 + 180: CALL PathMove(Tx, Ty, 1, Angle): Path = LEFT$(Path, LEN(Path) - 1)
    100.  LOOP
    101.  GOSUB ShortenPath
    102.  GOSUB ClearFootPrints
    103. RETURN
    104.  
    105.  
    106. FindSubPath:
    107.  TerStat(Tx, Ty) = TerStat(Tx, Ty) + 8
    108.  DO UNTIL INKEY$ = CHR$(27)
    109.   Angle = INT(Target(Tx, Ty, Dx, Dy) / 45) * 45
    110.   FOR a = 0 TO 25000
    111.   NEXT a
    112.   FOR Increase = 45 TO 315 STEP 45
    113.    IF TerStat(Tx + INT(SIN(Rad(Angle)) + .5), Ty + INT(COS(Rad(Angle)) + .5)) < 1 THEN
    114.     CALL PathMove(Tx, Ty, 1, Angle)
    115.     TerStat(Tx, Ty) = TerStat(Tx, Ty) + 8
    116.     SubPath = SubPath + CoStr$(Angle / 45)
    117.     LINE (Tx * 16, Ty * 16)-(Tx * 16 + 15, Ty * 16 + 15), 12, BF
    118.     IF Tx = Dx AND Ty = Dy THEN EXIT DO
    119.     EXIT FOR
    120.    ELSE
    121.     IF UpDown = 0 THEN Angle = Angle + Increase: UpDown = 1 ELSE Angle = Angle - Increase: UpDown = 0
    122.    END IF
    123.   NEXT Increase
    124.   IF Increase = 360 AND Tx = x AND Ty = y THEN SubPath =
    125. : EXIT DO
    126.   IF Increase = 360 THEN Angle = CoSng(RIGHT$(SubPath, 1)) * 45 + 180: CALL PathMove(Tx, Ty, 1, Angle): SubPath = LEFT$(SubPath, LEN(SubPath) - 1)
    127.  LOOP
    128.  Path = LEFT$(Path, ShortStart) + SubPath + RIGHT$(Path, 0 + SubPathStart + LEN(SubPath))
    129.  GOSUB ClearFootPrints
    130. RETURN
    131.  
    132.  
    133. ShortenPath:
    134.  ShortJump = 2
    135.  FOR a = 1 TO LEN(Path)
    136.   ShortStart = a
    137.   TempString = Path
    138.   FOR b = 1 TO ShortJump
    139.    Angle = CoSng(MID$(Path, b, 1)) * 45
    140.    CALL PathMove(x, y, 1, Angle)
    141.   NEXT b
    142.   Path = TempString
    143.   Tx = x: Ty = y
    144.   GOSUB FindSubPath
    145.  NEXT a
    146. RETURN
    147.  
    148.  
    149. FollowPath:
    150.  IF TerStat(x + INT(SIN(Rad(Angle)) + .5), y + INT(COS(Rad(Angle)) + .5)) < 1 THEN
    151.   Angle = CoSng(LEFT$(Path, 1)) * 45
    152.   CALL PathMove(x, y, 1, Angle)
    153.   Path = RIGHT$(Path, LEN(Path) - 1)
    154.  ELSE
    155.   Dx = x + CoSng(MID$(Path, 1, 1)) * 45
    156.   Dy = y + CoSng(MID$(Path, 1, 1)) * 45
    157.   Dx = Dx + CoSng(MID$(Path, 2, 1)) * 45
    158.   Dy = Dy + CoSng(MID$(Path, 2, 1)) * 45
    159.   Tx = x: Ty = y
    160.   ShortStart = 0
    161.   GOTO FindSubPath
    162.  END IF
    163. RETURN
    164.  
    165.  
    166. ClearFootPrints:
    167.  FOR a = 0 TO 19
    168.   FOR b = 0 TO 11
    169.    IF TerStat(a, b) >= 8 THEN TerStat(a, b) = TerStat(a, b) - 8
    170.   NEXT b
    171.  NEXT a
    172. RETURN
    173.  
    174. FUNCTION Arcsin (x)
    175. IF SQR(-x * x + 1) = 0 THEN EXIT FUNCTION
    176. x = ATN(x / SQR(-x * x + 1))
    177. Arcsin = Deg(x)
    178. END FUNCTION
    179.  
    180. FUNCTION CoSng (x AS STRING)
    181. IF x =
    182. 0
    183.  THEN CoSng = 0
    184.  
    185. IF x =
    186. 1
    187.  THEN CoSng = 1
    188. IF x =
    189. 2
    190.  THEN CoSng = 2
    191. IF x =
    192. 3
    193.  THEN CoSng = 3
    194. IF x =
    195. 4
    196.  THEN CoSng = 4
    197. IF x =
    198. 5
    199.  THEN CoSng = 5
    200. IF x =
    201. 6
    202.  THEN CoSng = 6
    203. IF x =
    204. 7
    205.  THEN CoSng = 7
    206. IF x =
    207. 8
    208.  THEN CoSng = 8
    209. END FUNCTION
    210.  
    211. FUNCTION CoStr$ (x)
    212. IF x > 16 THEN x = x - 16
    213. IF x > 8 THEN x = x - 8
    214. IF x < 0 THEN x = x + 8
    215. IF x < -8 THEN x = x + 16
    216. IF x = 0 THEN CoStr =
    217. 0
    218.  
    219. IF x = 1 THEN CoStr =
    220. 1
    221.  
    222. IF x = 2 THEN CoStr =
    223. 2
    224.  
    225. IF x = 3 THEN CoStr =
    226. 3
    227.  
    228. IF x = 4 THEN CoStr =
    229. 4
    230.  
    231. IF x = 5 THEN CoStr =
    232. 5
    233.  
    234. IF x = 6 THEN CoStr =
    235. 6
    236.  
    237. IF x = 7 THEN CoStr =
    238. 7
    239.  
    240. IF x = 8 THEN CoStr =
    241. 8
    242.  
    243. END FUNCTION
    244.  
    245. FUNCTION Deg (x)
    246. Deg = x * (180 / PI)
    247. END FUNCTION
    248.  
    249. SUB PathMove (x, y, Speed, Direction)
    250. x = x + (Speed * GameSpeed) * INT(SIN(Rad(Direction)) + .5)
    251. y = y + (Speed * GameSpeed) * INT(COS(Rad(Direction)) + .5)
    252. END SUB
    253.  
    254. FUNCTION Rad (x)
    255. Rad = x * (PI / 180)
    256. END FUNCTION
    257.  
    258. FUNCTION Target (SourceX, SourceY, DestX, DestY)
    259. DIM Vertical AS SINGLE
    260. DIM Horizontal AS SINGLE
    261. DIM Diagonal AS SINGLE
    262. DIM Vector AS SINGLE
    263. Horizontal = ABS(SourceX - DestX)
    264. Vertical = ABS(SourceY - DestY)
    265. IF Horizontal AND Vertical <> 0 THEN
    266.  Diagonal = SQR((Horizontal ^ 2) + (Vertical ^ 2))
    267.  Vector = (Arcsin(Vertical / Diagonal))
    268.  IF DestX > SourceX AND DestY > SourceY THEN Vector = 0 - Vector: Vector = Vector + 90
    269.  IF DestX > SourceX AND DestY < SourceY THEN Vector = Vector + 90
    270.  IF DestX < SourceX AND DestY > SourceY THEN Vector = Vector + 270
    271.  IF DestX < SourceX AND DestY < SourceY THEN Vector = 180 - Vector: Vector = Vector + 90
    272. ELSE
    273.  IF Horizontal = 0 AND DestY > SourceY THEN Vector = 0
    274.  IF Horizontal = 0 AND DestY < SourceY THEN Vector = 180
    275.  IF Vertical = 0 AND DestX > SourceX THEN Vector = 90
    276.  IF Vertical = 0 AND DestX < SourceX THEN Vector = 270
    277.  IF Vertical = 0 AND Horizontal = 0 THEN Vector = 0: Target = 0: EXIT FUNCTION
    278. END IF
    279. DO UNTIL Vector <= 360 AND Vector > 0
    280.  IF Vector <= 0 THEN Vector = Vector + 360
    281.  IF Vector > 360 THEN Vector = Vector - 360
    282. LOOP
    283. Target = Vector
    284. END FUNCTION
    Last edited by alkatran; Dec 15th, 2002 at 04:53 PM.
    Don't pay attention to this signature, it's contradictory.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width