@ -12,7 +12,7 @@ var selectedFaction
var boardUnits = { }
var occupiedTiles = [ ]
var enemyOccupiedTiles = [ ]
var route
var stepsStartToTileInCoords
var routeVertices
var hoveredCoords
var regionID = 123
@ -39,7 +39,7 @@ func receiveTiles(newTileRect, newTiles, newSuperTiles):
func requestUnits ( ) :
WorldManager . requestUnits ( regionID )
func receiveUnits ( newBoardUnits ) : #overwork this
func receiveUnits ( newBoardUnits ) :
boardUnits = newBoardUnits
for child in $ TileMap / Units . get_children ( ) :
child . queue_free ( )
@ -55,31 +55,22 @@ func receiveUnits(newBoardUnits):#overwork this
newUnit . on_ready ( )
boardUnitsIndex += 1
func unitSelectedMethod ( unit , debugOriginUnit ) : #this is called when a unit is selected
print ( " from: " , boardUnits [ debugOriginUnit . boardUnitsIndex ] . id )
if unit == null :
clearPathingInfo ( )
$ moveapcost . text = " "
$ unitapcounter . text = " "
selectedFaction = null
selectedUnit = null
for unit in boardUnits :
unit . isSelected = false
$ TileMap / LineManager . drawAdvancedLine ( [ ] , [ ] , 0 , 0 )
else :
selectedNode = unit
selectedUnit = boardUnits [ unit . boardUnitsIndex ]
requestTiles ( selectedUnit )
selectedFaction = selectedUnit . faction
for unit in boardUnits :
if unit . id != selectedUnit . id :
unit . isSelected = false
$ TileMap / LineManager . drawAdvancedLine ( [ ] , [ ] , 0 , 0 )
# print(selectedUnit)
func _input ( event ) : #this responds to any input. Has various functions depending on input
if selectedUnit != null :
if event is InputEventMouseButton and Input . is_action_just_released ( " ui_left_mouse_button " ) :
if event is InputEventMouse :
mouseEvent ( event )
if event is InputEvent and Input . is_action_just_released ( " ui_accept " ) :
if animationInProgress == false :
nextRound ( )
if event is InputEvent and Input . is_action_just_released ( " ui_right " ) :
print ( " reset pathing " )
resetPathing ( )
func mouseEvent ( event ) :
if ! selectedUnit :
if Input . is_action_just_released ( " ui_left_mouse_button " ) :
pass
else :
if Input . is_action_just_released ( " ui_left_mouse_button " ) :
if ! selectedUnit . superUnit :
clickPath ( )
else :
@ -91,27 +82,21 @@ func _input(event):#this responds to any input. Has various functions depending
hoverPath ( )
else :
hoverSuperPath ( )
if event is InputEvent and Input . is_action_just_released ( " ui_accept " ) :
if animationInProgress == false :
nextRound ( )
if event is InputEvent and Input . is_action_just_released ( " ui_right " ) :
print ( " reset pathing " )
resetPathing ( )
func clickPath ( ) : #this finalizes the selected path while calculating ap and new ap and shortening the path if ap is insufficient. Then it calls to execute the path
if animationInProgress == false and route . size ( ) > 1 :
if animationInProgress == false and stepsStartToTileInCoords . size ( ) > 1 :
var totalApCost = addUpArray ( routeVertices )
if totalApCost > selectedUnit . moveAp :
var apDebt = totalApCost - selectedUnit . moveAp
if apDebt > selectedUnit . ap :
while addUpArray ( routeVertices ) > selectedUnit . ap + selectedUnit . moveAp :
route . pop_back ( )
stepsStartToTileInCoords . pop_back ( )
routeVertices . pop_back ( )
while route . size ( ) != 0 and occupiedTiles . has ( route [ - 1 ] ) : #prevents overlap
route . pop_back ( )
while stepsStartToTileInCoords . size ( ) != 0 and occupiedTiles . has ( stepsStartToTileInCoords [ - 1 ] ) : #prevents overlap
stepsStartToTileInCoords . pop_back ( )
routeVertices . pop_back ( )
if route . size ( ) > 1 :
executePath ( route , routeVertices )
if stepsStartToTileInCoords . size ( ) > 1 :
executePath ( stepsStartToTileInCoords , routeVertices )
func hoverPath ( coord = hoveredCoords ) : #this gets the hovered tile and tells the pathfinding to make the path and tells the line to draw the returned path
if selectedUnit != null and coord != null :
@ -132,11 +117,10 @@ func nextRound():#this sends a nextRound signal to units on the board and refres
WorldManager . endTurn ( regionID )
WorldManager . requestUnits ( regionID )
func executePath ( route , vertices ) : #this takes the current selected route and tells the unit to execute it/ follow the selected route.
WorldManager . requestMove ( route , selectedUnit . id , regionID )
func executePath ( stepsStartToTileInCoords , stepsStartToTileInDistances ) : #this takes the current selected stepsStartToTileInCoords and tells the unit to execute it/ follow the selected stepsStartToTileInCoords.
WorldManager . requestMove ( stepsStartToTileInCoords , selectedUnit . id , regionID )
var convertedRoute = [ ]
for coords in route :
for coords in stepsStartToTileInCoords :
convertedRoute . append ( $ TileMap . map_to_world ( coords ) )
selectedNode . moveOnPath ( convertedRoute )
animationInProgress = true
@ -145,44 +129,44 @@ func executePath(route, vertices):#this takes the current selected route and tel
func clearPathingInfo ( ) : #this erases the Dijkstra/pathfinding-specific tile info for when a new tile is selected and new pathfinding info is needed.
for column in tiles :
for tile in column :
tile . vertice Explored = false
tile . route = [ ]
tile . surroundingTiles Explored = false
tile . stepsStartToTileInCoords = [ ]
tile . shortestKnownPath = INF
tile . verti ces = [ ]
tile . stepsStartToTileInDistan ces = [ ]
for column in superTiles :
for tile in column :
tile . vertice Explored = false
tile . route = [ ]
tile . surroundingTiles Explored = false
tile . stepsStartToTileInCoords = [ ]
tile . shortestKnownPath = INF
tile . verti ces = [ ]
tile . stepsStartToTileInDistan ces = [ ]
pass
func setDistance ( target , origin , index , diagonal = false ) : #this sets the distances to the connections between tiles.
if not diagonal :
match target . type :
" grass " :
origin . verticeDistance [ index ] = 5
origin . distancesToSurroundingTiles [ index ] = 5
" road " :
origin . verticeDistance [ index ] = 2
origin . distancesToSurroundingTiles [ index ] = 2
" mountain " :
origin . verticeDistance [ index ] = INF
origin . distancesToSurroundingTiles [ index ] = INF
" enemyOccupied " :
origin . verticeDistance [ index ] = INF
origin . distancesToSurroundingTiles [ index ] = INF
else :
match target . type :
" grass " :
origin . verticeDistance [ index ] = 8
origin . distancesToSurroundingTiles [ index ] = 8
" road " :
origin . verticeDistance [ index ] = 3
origin . distancesToSurroundingTiles [ index ] = 3
" mountain " :
origin . verticeDistance [ index ] = INF
origin . distancesToSurroundingTiles [ index ] = INF
" enemyOccupied " :
origin . verticeDistance [ index ] = INF
origin . distancesToSurroundingTiles [ index ] = INF
func findPath ( start , end ) : #this actually finds the path. returns an array that at [0] contains the route and at [1] contains the distances of each step on the route (verti ces).
func findPath ( start , end ) : #this actually finds the path. returns an array that at [0] contains the stepsStartToTileInCoords and at [1] contains the distances of each step on the stepsStartToTileInCoords (stepsStartToTileInDistan ces).
var startingTile = tiles [ start . x ] [ start . y ]
var endTile = tiles [ end . x ] [ end . y ]
startingTile . verti ces = [ 0 ]
startingTile . stepsStartToTileInDistan ces = [ 0 ]
var smallestVerticeDistance
var a = [ 1 ]
for number in a :
@ -190,28 +174,28 @@ func findPath(start, end):#this actually finds the path. returns an array that a
var smallestVertice = [ INF ]
for columns in tiles :
for tile in columns :
if tile . route . size ( ) == 0 :
tile . route . append ( startingTile . coords )
if tile . verti ces == null :
print ( " \n ERROR Some verti ces are null \n " )
if tile . stepsStartToTileInCoords . size ( ) == 0 :
tile . stepsStartToTileInCoords . append ( startingTile . coords )
if tile . stepsStartToTileInDistan ces == null :
print ( " \n ERROR Some stepsStartToTileInDistan ces are null \n " )
break
if tile . vertice Explored == false and tile . verti ces. size ( ) > 0 :
if tile . surroundingTiles Explored == false and tile . stepsStartToTileInDistan ces. size ( ) > 0 :
smallestVerticeDistance = addUpArray ( smallestVertice )
var tileTotalVertice = 0
tileTotalVertice = addUpArray ( tile . verti ces)
tileTotalVertice = addUpArray ( tile . stepsStartToTileInDistan ces)
if tileTotalVertice < smallestVerticeDistance :
smallestVertice = tile . verti ces
currentTile = tile # Hier wird die neue currentTile gesetzt. DIe neue CurrenTile ist die mit dem kleinsten zusammengerechneten Verti ces/ dem kurzesten Pfad
smallestVertice = tile . stepsStartToTileInDistan ces
currentTile = tile # Hier wird die neue currentTile gesetzt. DIe neue CurrenTile ist die mit dem kleinsten zusammengerechneten stepsStartToTileInDistan ces/ dem kurzesten Pfad
if currentTile == null :
animationInProgress = false
break
if currentTile == endTile :
route = endTile . route
routeVertices = endTile . verti ces
return [ endTile . route , endTile . verti ces]
stepsStartToTileInCoords = endTile . stepsStartToTileInCoords
routeVertices = endTile . stepsStartToTileInDistan ces
return [ endTile . stepsStartToTileInCoords , endTile . stepsStartToTileInDistan ces]
var translation = null
var direction = 0
for entry in currentTile . verticeDistance :
for entry in currentTile . distancesToSurroundingTiles :
match direction :
0 :
if entry != INF :
@ -239,17 +223,17 @@ func findPath(start, end):#this actually finds the path. returns an array that a
translation = Vector2 ( - 1 , 1 )
if translation != null :
var tileToModify = tiles [ ( currentTile . coords + translation ) . x ] [ ( currentTile . coords + translation ) . y ]
if addUpArray ( currentTile . verti ces) + currentTile . verticeDistance [ direction ] < addUpArray ( tileToModify . verti ces) or tileToModify . verti ces. size ( ) == 0 :
if addUpArray ( currentTile . stepsStartToTileInDistan ces) + currentTile . distancesToSurroundingTiles [ direction ] < addUpArray ( tileToModify . stepsStartToTileInDistan ces) or tileToModify . stepsStartToTileInDistan ces. size ( ) == 0 :
# print("Shorter Path found.")
var newShortestPath = currentTile . duplicate ( ) . verti ces. duplicate ( )
var newnewShortestPath = newShortestPath . append ( currentTile . duplicate ( ) . verticeDistance . duplicate ( ) [ direction ] )
tileToModify . verti ces = newShortestPath . duplicate ( )
var newShortestRoute = currentTile . duplicate ( ) . route . duplicate ( )
var newShortestPath = currentTile . duplicate ( ) . stepsStartToTileInDistan ces. duplicate ( )
var newnewShortestPath = newShortestPath . append ( currentTile . duplicate ( ) . distancesToSurroundingTiles . duplicate ( ) [ direction ] )
tileToModify . stepsStartToTileInDistan ces = newShortestPath . duplicate ( )
var newShortestRoute = currentTile . duplicate ( ) . stepsStartToTileInCoords . duplicate ( )
var newnewShortestRoute = newShortestRoute . append ( tileToModify . coords ) #this is really bodged but i dont know how to fix it and it somehow works fine despite everything
tileToModify . route = newShortestRoute
tileToModify . stepsStartToTileInCoords = newShortestRoute
direction += 1
tiles [ currentTile . coords . x ] [ currentTile . coords . y ] = currentTile
currentTile . vertice Explored = true
currentTile . surroundingTiles Explored = true
a . append ( 1 )
func addUpArray ( array ) : #adds up each int in an array. useful for calculating total distance.
@ -266,18 +250,18 @@ func _on_pathAnimationCompleted():#this gets a signal from the selected unit and
animationInProgress = false
selectedUnit = null
func resetPathing ( ) : #this clears the current route and removes the line. Without it weird bugs can happen where It can do a path that it already did again.
route = [ ]
func resetPathing ( ) : #this clears the current stepsStartToTileInCoords and removes the line. Without it weird bugs can happen where It can do a path that it already did again.
stepsStartToTileInCoords = [ ]
routeVertices = [ ]
if selectedUnit != null :
$ TileMap / LineManager . drawAdvancedLine ( route , routeVertices , selectedUnit . ap , selectedUnit . moveAp )
$ TileMap / LineManager . drawAdvancedLine ( stepsStartToTileInCoords , routeVertices , selectedUnit . ap , selectedUnit . moveAp )
else :
$ TileMap / LineManager . drawAdvancedLine ( route , routeVertices , 0 , 0 )
$ TileMap / LineManager . drawAdvancedLine ( stepsStartToTileInCoords , routeVertices , 0 , 0 )
func findSuperPath ( start , end ) :
var startingTile = superTiles [ start . x ] [ start . y ]
var endTile = superTiles [ end . x ] [ end . y ]
startingTile . verti ces = [ 0 ]
startingTile . stepsStartToTileInDistan ces = [ 0 ]
var smallestVerticeDistance
var a = [ 1 ]
for number in a :
@ -285,28 +269,28 @@ func findSuperPath(start,end):
var smallestVertice = [ INF ]
for columns in superTiles :
for tile in columns :
if tile . route . size ( ) == 0 :
tile . route . append ( startingTile . coords )
if tile . verti ces == null :
print ( " \n ERROR Some verti ces are null \n " )
if tile . stepsStartToTileInCoords . size ( ) == 0 :
tile . stepsStartToTileInCoords . append ( startingTile . coords )
if tile . stepsStartToTileInDistan ces == null :
print ( " \n ERROR Some stepsStartToTileInDistan ces are null \n " )
break
if tile . vertice Explored == false and tile . verti ces. size ( ) > 0 :
if tile . surroundingTiles Explored == false and tile . stepsStartToTileInDistan ces. size ( ) > 0 :
smallestVerticeDistance = addUpArray ( smallestVertice )
var tileTotalVertice = 0
tileTotalVertice = addUpArray ( tile . verti ces)
tileTotalVertice = addUpArray ( tile . stepsStartToTileInDistan ces)
if tileTotalVertice < smallestVerticeDistance :
smallestVertice = tile . verti ces
currentTile = tile # Hier wird die neue currentTile gesetzt. DIe neue CurrenTile ist die mit dem kleinsten zusammengerechneten Verti ces/ dem kurzesten Pfad
smallestVertice = tile . stepsStartToTileInDistan ces
currentTile = tile # Hier wird die neue currentTile gesetzt. DIe neue CurrenTile ist die mit dem kleinsten zusammengerechneten stepsStartToTileInDistan ces/ dem kurzesten Pfad
if currentTile == null :
animationInProgress = false
break
if currentTile == endTile :
route = endTile . route
routeVertices = endTile . verti ces
return [ endTile . route , endTile . verti ces]
stepsStartToTileInCoords = endTile . stepsStartToTileInCoords
routeVertices = endTile . stepsStartToTileInDistan ces
return [ endTile . stepsStartToTileInCoords , endTile . stepsStartToTileInDistan ces]
var translation = null
var direction = 0
for entry in currentTile . verticeDistance :
for entry in currentTile . distancesToSurroundingTiles :
match direction :
0 :
if entry != INF :
@ -334,17 +318,17 @@ func findSuperPath(start,end):
translation = Vector2 ( - 1 , 1 )
if translation != null :
var tileToModify = superTiles [ ( currentTile . coords + translation ) . x ] [ ( currentTile . coords + translation ) . y ]
if addUpArray ( currentTile . verti ces) + currentTile . verticeDistance [ direction ] < addUpArray ( tileToModify . verti ces) or tileToModify . verti ces. size ( ) == 0 :
if addUpArray ( currentTile . stepsStartToTileInDistan ces) + currentTile . distancesToSurroundingTiles [ direction ] < addUpArray ( tileToModify . stepsStartToTileInDistan ces) or tileToModify . stepsStartToTileInDistan ces. size ( ) == 0 :
# print("Shorter Path found.")
var newShortestPath = currentTile . duplicate ( ) . verti ces. duplicate ( )
var newnewShortestPath = newShortestPath . append ( currentTile . duplicate ( ) . verticeDistance . duplicate ( ) [ direction ] )
tileToModify . verti ces = newShortestPath . duplicate ( )
var newShortestRoute = currentTile . duplicate ( ) . route . duplicate ( )
var newShortestPath = currentTile . duplicate ( ) . stepsStartToTileInDistan ces. duplicate ( )
var newnewShortestPath = newShortestPath . append ( currentTile . duplicate ( ) . distancesToSurroundingTiles . duplicate ( ) [ direction ] )
tileToModify . stepsStartToTileInDistan ces = newShortestPath . duplicate ( )
var newShortestRoute = currentTile . duplicate ( ) . stepsStartToTileInCoords . duplicate ( )
var newnewShortestRoute = newShortestRoute . append ( tileToModify . coords ) #this is really bodged but i dont know how to fix it and it somehow works fine despite everything
tileToModify . route = newShortestRoute
tileToModify . stepsStartToTileInCoords = newShortestRoute
direction += 1
superTiles [ currentTile . coords . x ] [ currentTile . coords . y ] = currentTile
currentTile . vertice Explored = true
currentTile . surroundingTiles Explored = true
a . append ( 1 )
func hoverSuperPath ( coord = hoveredCoords ) :
@ -361,20 +345,103 @@ func hoverSuperPath(coord = hoveredCoords):
$ TileMap / LineManager . drawAdvancedSuperLine ( path [ 0 ] , path [ 1 ] , selectedUnit . ap , selectedUnit . moveAp )
func clickSuperPath ( ) :
if animationInProgress == false and route . size ( ) > 1 :
if animationInProgress == false and stepsStartToTileInCoords . size ( ) > 1 :
var totalApCost = addUpArray ( routeVertices )
if totalApCost > selectedUnit . moveAp :
var apDebt = totalApCost - selectedUnit . moveAp
if apDebt > selectedUnit . ap :
# print("Not enough AP for this move.")
while addUpArray ( routeVertices ) > selectedUnit . ap + selectedUnit . moveAp :
route . pop_back ( )
stepsStartToTileInCoords . pop_back ( )
routeVertices . pop_back ( )
while route . size ( ) != 0 and ( occupiedTiles . has ( route [ - 1 ] ) == true or occupiedTiles . has ( route [ - 1 ] + Vector2 ( 1 , 0 ) ) == true or occupiedTiles . has ( rou te [ - 1 ] + Vector2 ( 0 , 1 ) ) == true or occupiedTiles . has ( route [ - 1 ] + Vector2 ( 1 , 1 ) ) == true ) : #prevents overlap
route . pop_back ( )
while stepsStartToTileInCoords . size ( ) != 0 and ( occupiedTiles . has ( stepsStartToTileInCoords [ - 1 ] ) == true or occupiedTiles . has ( stepsStartToTileInCoords [ - 1 ] + Vector2 ( 1 , 0 ) ) == true or occupiedTiles . has ( stepsSta rtToTil eInCoords [ - 1 ] + Vector2 ( 0 , 1 ) ) == true or occupiedTiles . has ( stepsStartToTileInCoords [ - 1 ] + Vector2 ( 1 , 1 ) ) == true ) : #prevents overlap
stepsStartToTileInCoords . pop_back ( )
routeVertices . pop_back ( )
if route . size ( ) != 0 and route [ 0 ] != route [ - 1 ] :
if stepsStartToTileInCoords . size ( ) != 0 and stepsStartToTileInCoords [ 0 ] != stepsStartToTileInCoords [ - 1 ] :
totalApCost = addUpArray ( routeVertices )
apDebt = totalApCost - selectedUnit . moveAp
if route . size ( ) > 1 :
executePath ( route , routeVertices )
if stepsStartToTileInCoords . size ( ) > 1 :
executePath ( stepsStartToTileInCoords , routeVertices )
func findPathFromCoordToCoord ( coord1 , coord2 , tiles ) :
var startingTile = tiles [ coord1 . x ] [ coord1 . y ]
var endTile = tiles [ coord2 . x ] [ coord2 . y ]
startingTile . stepsStartToTileInDistances = [ 0 , ]
for columns in tiles :
for tile in columns :
tile . stepsStartToTileInCoords = [ startingTile . coords , ]
var shortestPathDistance #smallestVerticeDistance
var shortestPathFound = false
while ! shortestPathFound :
var currentTile = null #The unexplored Tile with the shortest Path
#Selects next currentTile / Tile with shortest path to it
for columns in tiles :
for tile in columns :
if tile . surroundingTilesExplored == false and tile . stepsStartToTileInDistances . size ( ) > 0 :
if addUpArray ( tile . stepsStartToTileInDistances ) < shortestPathDistance :
shortestPathDistance = addUpArray ( tile . stepsStartToTileInDistances )
currentTile = tile
#Checks whether the search is completed
if currentTile == endTile :
stepsStartToTileInCoords = endTile . stepsStartToTileInCoords
routeVertices = endTile . stepsStartToTileInDistances
shortestPathFound = true
return [ endTile . stepsStartToTileInCoords , endTile . stepsStartToTileInDistances ]
#Explore surrounding Tiles. Add the path taken to reach them via the current Tile if it is the shortest current path or the first.
var translation = null
var direction = 0
for entry in currentTile . distancesToSurroundingTiles :
match direction :
0 :
if entry != INF :
translation = Vector2 ( 0 , 1 )
1 :
if entry != INF :
translation = Vector2 ( 1 , 1 )
2 :
if entry != INF :
translation = Vector2 ( 1 , 0 )
3 :
if entry != INF :
translation = Vector2 ( 1 , - 1 )
4 :
if entry != INF :
translation = Vector2 ( 0 , - 1 )
5 :
if entry != INF :
translation = Vector2 ( - 1 , - 1 )
6 :
if entry != INF :
translation = Vector2 ( - 1 , 0 )
7 :
if entry != INF :
translation = Vector2 ( - 1 , 1 )
if translation != null :
var surroundingTile = tiles [ ( currentTile . coords + translation ) . x ] [ ( currentTile . coords + translation ) . y ]
if ( addUpArray ( currentTile . stepsStartToTileInDistances ) + currentTile . distancesToSurroundingTiles [ direction ] ) < addUpArray ( surroundingTile . stepsStartToTileInDistances ) :
pass
surroundingTile . stepsStartToTileInDistances . append ( )
surroundingTile . stepsStartToTileInCoords . append ( )
if addUpArray ( currentTile . stepsStartToTileInDistances ) + currentTile . distancesToSurroundingTiles [ direction ] < addUpArray ( surroundingTile . stepsStartToTileInDistances ) or surroundingTile . stepsStartToTileInDistances . size ( ) == 0 :
var newShortestPath = currentTile . duplicate ( ) . stepsStartToTileInDistances . duplicate ( )
var newnewShortestPath = newShortestPath . append ( currentTile . duplicate ( ) . distancesToSurroundingTiles . duplicate ( ) [ direction ] )
surroundingTile . stepsStartToTileInDistances = newShortestPath . duplicate ( )
var newShortestRoute = currentTile . duplicate ( ) . stepsStartToTileInCoords . duplicate ( )
var newnewShortestRoute = newShortestRoute . append ( surroundingTile . coords ) #this is really bodged but i dont know how to fix it and it somehow works fine despite everything
surroundingTile . stepsStartToTileInCoords = newShortestRoute
direction += 1
currentTile . surroundingTilesExplored = true
return