A*Star in Hollywood
Posted: Mon May 08, 2023 11:26 am
Three files to this one. I will post one at a a time.
astar.hws
astar.hws
Code: Select all
/* MIT License
Copyright (c) 2020 Xiejiangzhi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Converted to Hollywood in 2023 by Dwayne Jarvis <dwayne.jarvis@djitalverse.com>
*/
If IsNil (AStar)
Local Function CheckIndex (thisSet, thisNode)
;DebugPrint ("--> ASTAR:Local Function CheckIndex (thisSet, thisNode)", Debug.Table (thisSet), Debug.Table (thisNode))
Local CheckIndex = False
If (HaveItem (thisSet, thisNode)) Then CheckIndex = thisSet [thisNode]
;DebugPrint ("<-- ASTAR:EndFunction CheckIndex (checkIndex)", CheckIndex)
Return (CheckIndex)
EndFunction
Global AStar = { }
AStar.__index = AStar
Local private = { }
Local inf = 255
Function AStar.New (...)
;DebugPrint ("--> ASTAR:Function AStar.New (...)", arg.n)
Local newAStar = SetMetaTable ({ }, AStar)
newAStar:Init (Unpack (arg))
;DebugPrint ("<-- ASTAR:EndFunction AStar.New (newAStar)", Debug.Table (newAStar))
Return (newAStar)
EndFunction
Function AStar:Init (thisMap)
;DebugPrint ("--> ASTAR:Function AStar:Init (thisMap)", Debug.Table(thisMap))
self.map = thisMap
Assert (HaveItem (thisMap, "neighboursget") And HaveItem (thisMap, "costget") And HaveItem (thisMap, "costestimate"))
;DebugPrint ("<-- ASTAR:EndFunction AStar:Init ()")
EndFunction
Function AStar:Find (startNode, goalNode, userdata)
;DebugPrint ("Function AStar:Find (start, goal, userdata)", Debug.Table (startNode), Debug.Table (goalNode), userdata)
Local thisMap = self.map
Local openSet = {[startNode] = True }
Local closedSet = { }
Local cameFrom = { }
Local gScore, hScore, fScore = { }, { }, { }
gScore [startNode] = 0
;DebugPrint ("gScore=", Debug.Table (gScore))
hScore [startNode] = self.map.CostEstimate (startNode, goalNode, userdata)
;DebugPrint ("hScore=", Debug.Table (hScore))
fScore [startNode] = hScore [startNode]
;DebugPrint ("fScore=", Debug.Table (fScore))
Local currentNode
Local Function AddNeighbour (neighbourNode, cost)
;DebugPrint ("\t--> ASTAR:Local Function AddNeighbour (neighbourNode, cost)", Debug.Table (neighbourNode), cost)
If Not (CheckIndex (closedSet, neighbourNode))
If IsNil (cost) Then cost = self.map.CostGet (currentNode, neighbourNode, userdata)
Local gScoreTemp = gScore [currentNode] + cost
Local openSetIndex = CheckIndex (openSet, neighbourNode)
If Not (openSetIndex) Or (gScoreTemp < gScore [neighbourNode])
cameFrom [neighbourNode] = currentNode
gScore [neighbourNode] = gScoreTemp
If (CheckIndex (hScore, neighbourNode))
hScore [neighbourNode] = hScore [neighbourNode]
Else
hScore [neighbourNode] = self.map.CostEstimate (neighbourNode, goalNode, userdata)
EndIf
fScore [neighbourNode] = gScoreTemp + hScore [neighbourNode]
openSet [neighbourNode] = True
EndIf
;DebugPrint ("\t\t<-- ASTAR:EndFunction NeighbourAdd ()")
EndIf
;DebugPrint ("\t<-- ASTAR:EndFunction AddNeighbour ()")
EndFunction
While Not (IsTableEmpty (openSet))
currentNode = private.PopBestNode (openSet, fScore)
;DebugPrint ("currentNode=", Debug.Table (currentNode))
openSet [currentNode] = Nil
If (currentNode = goalNode)
Local path = private.UnwindPath ({ }, cameFrom, goalNode)
InsertItem (path, goalNode)
;DebugPrint ("<-- ASTAR:EndFunction Find (path, gScore, hScore, fScore, cameFrom)", path, Debug.Table (gScore), Debug.Table (hScore), Debug.Table (fScore), cameFrom)
Return (path, gScore, hScore, fScore, cameFrom)
EndIf
closedSet [currentNode] = True
Local fromNode = Nil
If (HaveItem (cameFrom, currentNode)) Then fromNode = cameFrom [currentNode]
self.map.NeighboursGet (currentNode, fromNode, AddNeighbour, userdata)
Wend
;DebugPrint ("<-- ASTAR:EndFunction Find (Nil, gScore, hScore, fScore, cameFrom)", Nil, Debug.Table (gScore), Debug.Table (hScore), Debug.Table (fScore), cameFrom)
Return (Nil, gScore, hScore, fScore, cameFrom)
EndFunction
Function private.PopBestNode (set, score)
;DebugPrint ("--> ASTAR:Function private.PopBestNode (set, score)", set, score)
Local best, thisNode = inf, Nil
For k, v In Pairs (set)
;DebugPrint ("\tk=", k, ", v=", v)
Local s = score [k]
;DebugPrint ("\ts=", s, ", best=", best)
If (s < best) Then best, thisNode = s, k
Next
If (IsNil (thisNode))
;DebugPrint ("<-- ASTAR:EndFunction private.PopBestNode ()")
Return ()
EndIf
set [thisNode] = Nil
;DebugPrint ("<-- ASTAR:EndFunction private.PopBestNode (thisNode)", Debug.Table (thisNode))
Return (thisNode)
EndFunction
Function private.UnwindPath (flatPath, thisMap, currentNode)
;DebugPrint ("--> ASTAR:Function private.UnwindPath (flatPath, thisMap, currentNode)", flatPath, thisMap, currentNode)
If (HaveItem (thisMap, currentNode))
InsertItem (flatPath, thisMap [currentNode], 0)
;DebugPrint ("<-- ASTAR:EndFunction private.UnwindPath (private.PathUnwind, thisMap, thisMap [currentNode]", private.UnwindPath, thisMap, Debug.Table (thisMap [currentNode]))
Return (private.UnwindPath (flatPath, thisMap, thisMap [currentNode]))
Else
;DebugPrint ("<-- ASTAR:EndFunction private.UnwindPath (flatPath)", flatPath)
Return (flatPath)
EndIf
;DebugPrint ("<-- ASTAR:EndFunction private.UnwindPath ()")
EndFunction
EndIf