home *** CD-ROM | disk | FTP | other *** search
/ 100 Plus Great Games 2 / 100PLUSV2.BIN / games / SnowFight.dxr / 00008_PathFinding.ls < prev    next >
Encoding:
Text File  |  2002-01-25  |  4.6 KB  |  210 lines

  1. global gStartX, gStartY, gGoalX, gGoalY, gInfluence, gClosed, gOpen, gMapWidth, gMapHeight
  2.  
  3. on initSearchVar StartX, StartY, GoalX, GoalY
  4.   gMapWidth = 22
  5.   gMapHeight = 22
  6.   gStartX = StartX
  7.   gStartY = StartY
  8.   gGoalX = GoalX
  9.   gGoalY = GoalY
  10.   gOpen = [0]
  11.   gClosed = [0]
  12.   startNode = [gStartX, gStartY, 0, pathCostEstimate(gStartX, gStartY, gGoalX, gGoalY), pathCostEstimate(gStartX, gStartY, gGoalX, gGoalY), 0]
  13.   gOpen[1] = startNode
  14. end
  15.  
  16. on pathCostEstimate sX, sY, gX, gY
  17.   x = sX
  18.   y = sY
  19.   sum = 0
  20.   repeat while 1
  21.     if x < gX then
  22.       x = x + 1
  23.     end if
  24.     if x > gX then
  25.       x = x - 1
  26.     end if
  27.     if y < gY then
  28.       y = y + 1
  29.     end if
  30.     if y > gY then
  31.       y = y - 1
  32.     end if
  33.     sum = sum + 1
  34.     if (x = gX) and (y = gY) then
  35.       return sum
  36.     end if
  37.   end repeat
  38.   return -666
  39. end
  40.  
  41. on getAdjecentTile aNode, aDirection
  42.   locX = aNode[1]
  43.   locY = aNode[2]
  44.   yay = 1
  45.   newX = -1
  46.   newY = -1
  47.   case aDirection of
  48.     1:
  49.       if (locY - 1) < 1 then
  50.         yay = 0
  51.       end if
  52.       newX = locX
  53.       newY = locY - 1
  54.     2:
  55.       if ((locY - 1) < 1) or ((locX + 1) > gMapWidth) then
  56.         yay = 0
  57.       end if
  58.       newX = locX + 1
  59.       newY = locY - 1
  60.     3:
  61.       if (locX + 1) > gMapWidth then
  62.         yay = 0
  63.       end if
  64.       newX = locX + 1
  65.       newY = locY
  66.     4:
  67.       if ((locY + 1) > gMapHeight) or ((locX + 1) > gMapWidth) then
  68.         yay = 0
  69.       end if
  70.       newX = locX + 1
  71.       newY = locY + 1
  72.     5:
  73.       if (locY + 1) > gMapHeight then
  74.         yay = 0
  75.       end if
  76.       newX = locX
  77.       newY = locY + 1
  78.     6:
  79.       if ((locY + 1) > gMapHeight) or ((locX - 1) < 1) then
  80.         yay = 0
  81.       end if
  82.       newX = locX - 1
  83.       newY = locY + 1
  84.     7:
  85.       if (locX - 1) < 1 then
  86.         yay = 0
  87.       end if
  88.       newX = locX - 1
  89.       newY = locY
  90.     8:
  91.       if ((locY - 1) < 1) or ((locX - 1) < 1) then
  92.         yay = 0
  93.       end if
  94.       newX = locX - 1
  95.       newY = locY - 1
  96.     otherwise:
  97.       put "invalid direction"
  98.   end case
  99.   if not yay then
  100.     return 0
  101.   end if
  102.   newNode = [newX, newY, 0, 0, 0, 0]
  103.   return newNode
  104. end
  105.  
  106. on needsToBeUpdated aState, aCost
  107.   temp = 0
  108.   if (not isThisStateHere(gOpen, aState) = 0) or (not isThisStateHere(gClosed, aState) = 0) then
  109.     temp = isThisStateHere(gOpen, aState)
  110.     if not temp = 0 then
  111.       if aCost <= temp[4] then
  112.         return 1
  113.       end if
  114.     end if
  115.     temp = isThisStateHere(gClosed, aState)
  116.     if not temp = 0 then
  117.       if aCost <= temp[4] then
  118.         return 1
  119.       end if
  120.     end if
  121.     return 0
  122.   else
  123.     return 1
  124.   end if
  125. end
  126.  
  127. on aStarGoGoGo
  128.   node = 0
  129.   newNode = 0
  130.   newCost = 0
  131.   repeat while count(gOpen) >= 1
  132.     node = gOpen[1]
  133.     deleteAt(gOpen, 1)
  134.     if (node[1] = gGoalX) and (node[2] = gGoalY) then
  135.       put "Found the path"
  136.       return node
  137.     end if
  138.     repeat with aDirection = 1 to 8
  139.       newNode = getAdjecentTile(node, aDirection)
  140.       if not ((newNode = 0) or (newNode = [0])) then
  141.         newCost = node[3] + gInfluence[newNode[1]][newNode[2]]
  142.         if needsToBeUpdated(newNode, newCost) then
  143.           newNode[6] = node
  144.           newNode[3] = newCost
  145.           newNode[4] = pathCostEstimate(newNode[1], newNode[2], gGoalX, gGoalY)
  146.           newNode[5] = newNode[3] + newNode[4]
  147.           if not isThisStateHere(gClosed, newNode) = 0 then
  148.             removeNode(newNode, gClosed)
  149.           end if
  150.           if not isThisStateHere(gOpen, newNode) = 0 then
  151.             removeNode(newNode, gOpen)
  152.             pushInList(gOpen, newNode)
  153.             next repeat
  154.           end if
  155.           pushInList(gOpen, newNode)
  156.         end if
  157.       end if
  158.     end repeat
  159.     append(gClosed, newNode)
  160.   end repeat
  161.   put "No path exists"
  162.   return 0
  163. end
  164.  
  165. on removeNode theNode, theList
  166.   if theList = 0 then
  167.     return 
  168.   end if
  169.   repeat with counter = 1 to count(theList)
  170.     if (theList[counter][1] = theNode[1]) and (theList[counter][2] = theNode[2]) then
  171.       deleteAt(theList, counter)
  172.       return 
  173.     end if
  174.   end repeat
  175. end
  176.  
  177. on isThisStateHere aList, aNode
  178.   if (aList = 0) or (aList = [0]) then
  179.     return 0
  180.     exit
  181.   end if
  182.   repeat with counter = 1 to count(aList)
  183.     if aList[counter] = 0 then
  184.       return 0
  185.       exit
  186.     end if
  187.     if (aList[counter][1] = aNode[1]) and (aList[counter][2] = aNode[2]) then
  188.       return aList[counter]
  189.     end if
  190.   end repeat
  191.   return 0
  192. end
  193.  
  194. on pushInList aList, aNode
  195.   if (aList = 0) or (aList = [0]) then
  196.     aList = [0]
  197.     aList[1] = aNode
  198.   else
  199.     repeat with counter = 1 to count(aList)
  200.       if aList[counter][5] >= aNode[5] then
  201.         addAt(aList, counter, aNode)
  202.         return 1
  203.       end if
  204.     end repeat
  205.     append(aList, aNode)
  206.     return 1
  207.   end if
  208.   return 0
  209. end
  210.