Looking for some help on pathfinding for a pirate game. The game consists of multiple ships where they can sail across the game world. The game world is divided into cells starting at A1 thru A16 and S1 thru S16. Ships move to a new location using a trigger such as !P A1 or !P port name. This moves the ship across the map slowly across the map in a nondiagonal path.
Example: Ship is at Margarita (Q14) and sails to Barbados (S12) the path would be Q14, R14, R13, S13, S12

[Linked Image from piratesirc.com]


I would like for ships to sail around the cells designated as land (in red) and any help would be appreciated.
A copy of map.ini is located here

Here is the code I currently use:

Code
alias Cell.Restricted {
  ;$Cell.Restricted(cell) - returns if a cell (a1 thru s16) is land or inaccessible/restricted area

  var %file map.ini, %x $left($1,1), %y $right($1,-1)
  if (!%y) || (!%x) || ($len(%x $+ %y) < 2) || (%x !isalpha) || ($alph(%x) > 19) || (%y > 16) || (%y < 1) return $true
  elseif ($readini(%file,landorsea,%x $+ %y) == land) return $true
  elseif ($readini(%file,landorsea,%x $+ %y) == restricted) return $true
  else return $false
}

alias FindPath {
  ;$FindPath(start,destination,N) - returns the estimated (straight line) path it will take to get to goal/destination
  ;if N is 1, returns cells to move and saves findpath q
  ;if N is 2, returns cells to move
  ;if N is 3, shows # of moves

  var %current.x $left($1,1), %current.y $right($1,-1), %d.x $left($2,1), %d.y $right($2,-1)
  var %total 0, %moves $1, %i 0

  if ($3 == 1) hdel epirate.pathfind q

  while (%i < 75) {
    inc %i
    if ($isodd(%i)) {
      if ($alph(%current.x) < $alph(%d.x)) set %current.x $alph($calc($alph(%current.x) + 1))
      elseif ($alph(%current.x) > $alph(%d.x)) set %current.x $alph($calc($alph(%current.x) - 1))
      inc %total
    }
    else {
      if (%current.y < %d.y) set %current.y $calc(%current.y + 1)
      elseif (%current.y > %d.y) set %current.y $calc(%current.y - 1)
      inc %total
    }
    set %current.x $upper(%current.x)
    set %current.y $upper(%current.y)
    set %moves $addtok(%moves,%current.x $+ %current.y,44)

    if (%current.x == %d.x) && (%current.y == %d.y) {
      set %total $gettok(%moves,0,44)

      if ($3) {
        if ($3 == 1) {
          if (!$hget(pathfind)) hmake pathfind
          hadd pathfind q $addtok($hget(pathfind,q),%moves,44)
          return %moves
        }
        elseif ($3 == 2) return %moves
      }
      return %total

      break
    }
  }
}



Last edited by mruno; 11/03/21 09:12 PM.