Initial Commit
This commit is contained in:
581
PlayerScript.gd
Normal file
581
PlayerScript.gd
Normal file
@@ -0,0 +1,581 @@
|
||||
|
||||
extends CharacterBody2D
|
||||
|
||||
var Check = 0
|
||||
|
||||
var dir = Vector2.ZERO
|
||||
var Current_Position = [0,0]
|
||||
var Cords = [[0,0]]
|
||||
var Cords_Copy = []
|
||||
var Intersections = [[]]
|
||||
var Intersections_Copy = []
|
||||
|
||||
var Moves_From_Last_Intersection = []
|
||||
var Moves_From_Last_Intersection_Copy = []
|
||||
var Back_From_DeadEnd = []
|
||||
var Distance_From_Intersection = 0
|
||||
var Duplicates = {}
|
||||
var Tree_Array = [
|
||||
|
||||
]
|
||||
var Options = []
|
||||
var BackTrack_Array = []
|
||||
|
||||
var Traveled = false
|
||||
|
||||
var Coordinate_Dict = {"Position": [0,0]}
|
||||
var Detail_Dict = { "Type": "",
|
||||
"Came_From": 0,
|
||||
"Been_There": false}
|
||||
|
||||
var Preffered_Choice = {"Up": "",
|
||||
"Down": "",
|
||||
"Left": "",
|
||||
"Right": ""}
|
||||
|
||||
|
||||
var Cords_Minus_Curr = []
|
||||
var Move_Strength = 1
|
||||
var HasPrinted = false
|
||||
var cmd_state = 0
|
||||
var state = 0
|
||||
var state2 = 0
|
||||
var state3 = 0
|
||||
var state4 = 0
|
||||
var State = 0
|
||||
var Prev_dir = ""
|
||||
var Prev_dir_Opp = ""
|
||||
var Result = ""
|
||||
var move = 0
|
||||
var move_check = 0
|
||||
var ways = ["None", "Dead End", "Plain Path", "3 Way Intersection", "4 Way Intersection"]
|
||||
var Printer = -1
|
||||
var val = 0
|
||||
var ignore = false
|
||||
var Command_Size = 0
|
||||
var Last_cmd
|
||||
var Ordered_Commands_Forward = ["Start"]
|
||||
var Ordered_Commands_Backward = []
|
||||
var Individual_Steps = []
|
||||
var Back_To_Start = []
|
||||
var Command = []
|
||||
var Couplets = []
|
||||
var Was_DeadEnd_Array = []
|
||||
var Tree_Intersection_Array = []
|
||||
var Deleted = false
|
||||
var Travel_Check = 0
|
||||
var RebuildAfterDeadEnd = []
|
||||
var TakeOut = 0
|
||||
var SaveSize = 0
|
||||
var TrackBack = []
|
||||
var Been_There_Array = []
|
||||
var Move_Check2 = 0
|
||||
var Move_Check = 0
|
||||
var CircleState = 0
|
||||
var CirclingBack = false
|
||||
var Temporary_Instructions = []
|
||||
var move_check2 = 0
|
||||
var Move_Check3 = 0
|
||||
var Been_There_Overide = false
|
||||
var LogDump = []
|
||||
var IntersectionDump = []
|
||||
var move_check3 = 0
|
||||
var move_check4 = 0
|
||||
var move_check5 = 0
|
||||
var Move_Check6 = 0
|
||||
var Move_Check7 = 0
|
||||
var Cicling = false
|
||||
var CircleInts = 0
|
||||
var LogDumpOrdered = []
|
||||
var Order = []
|
||||
var Intersections_from_root = []
|
||||
var TargetCord = [100,-10]
|
||||
var CircleCompleted = null
|
||||
var AutoBacktrack = false
|
||||
var ChoiceFromInts = []
|
||||
var IntersectionDict = {"Cord": [],
|
||||
"Data": [] }
|
||||
var IntersectionDir = []
|
||||
var IntersectionMove = 0
|
||||
var IntOptions = []
|
||||
|
||||
func _input(event):
|
||||
if Current_Position != TargetCord:
|
||||
if event.is_action_pressed("ui_left"):
|
||||
goLeft()
|
||||
Been_There()
|
||||
AutoBacktrack = false
|
||||
#print(Current_Position)
|
||||
elif event.is_action_pressed("ui_right"):
|
||||
goRight()
|
||||
Been_There()
|
||||
AutoBacktrack = false
|
||||
#print(Current_Position)
|
||||
elif event.is_action_pressed("ui_up"):
|
||||
goUp()
|
||||
Been_There()
|
||||
AutoBacktrack = false
|
||||
#print(Current_Position)
|
||||
elif event.is_action_pressed("ui_down"):
|
||||
goDown()
|
||||
Been_There()
|
||||
AutoBacktrack = false
|
||||
#print(Current_Position)
|
||||
elif event.is_action_pressed("ui_accept"):
|
||||
AutoSolve()
|
||||
#Mark(Prev_dir)
|
||||
#print(LogDump)
|
||||
#print(BackTrack_Array)
|
||||
#print(move - BackTrack_Array[-1]+1)
|
||||
pass
|
||||
|
||||
func _process(delta):
|
||||
if Current_Position != TargetCord:
|
||||
Mark(Prev_dir)
|
||||
WhatMove()
|
||||
BackTracking()
|
||||
GiveOrder()
|
||||
Intersection_Tree()
|
||||
Circling()
|
||||
CheckForTreeDeadEnds()
|
||||
TravelLog()
|
||||
DeadEndCircles()
|
||||
CircleRootsAndInts()
|
||||
SolvinCircleDeadEnds()
|
||||
MoveAfterIntersection()
|
||||
IntersectionWaysAvailable()
|
||||
#BeenTheirFromPrevCord()
|
||||
#print(AutoBacktrack)var IntersectionDict = {
|
||||
#CircleComplete()
|
||||
# CircleRootsAndInts()
|
||||
#print(Cicling)
|
||||
#print(LogDump)
|
||||
#DeadEndMovement()
|
||||
#print(Individual_Steps)
|
||||
#print(Intersections_from_root)
|
||||
#print(Tree_Array)
|
||||
#print(Intersections)
|
||||
#AutoSolve()
|
||||
|
||||
else:
|
||||
print("We Done!!!!!!!!!!!!!!!!!!!!!!!")
|
||||
|
||||
func goDown():
|
||||
var D = $Forwardv
|
||||
dir = Vector2.DOWN
|
||||
if not D.is_colliding():
|
||||
self.global_position.y += 24
|
||||
Current_Position[1] -=1
|
||||
Prev_dir = "D"
|
||||
Prev_dir_Opp = "U"
|
||||
move += Move_Strength
|
||||
Been_There_Overide = false
|
||||
else:
|
||||
Been_There_Overide = true
|
||||
move += Move_Strength
|
||||
print("There is something below you")
|
||||
|
||||
func goUp():
|
||||
var U = $Backv
|
||||
dir = Vector2.UP
|
||||
if not U.is_colliding():
|
||||
self.global_position.y -= 24
|
||||
Current_Position[1] +=1
|
||||
Prev_dir = "U"
|
||||
Prev_dir_Opp = "D"
|
||||
move += Move_Strength
|
||||
Been_There_Overide = false
|
||||
else:
|
||||
Been_There_Overide = true
|
||||
move += Move_Strength
|
||||
print("There is something above you")
|
||||
#BeenTheirFromPrevCord()
|
||||
|
||||
func goRight():
|
||||
var R = $Rightv
|
||||
dir = Vector2.RIGHT
|
||||
if not R.is_colliding():
|
||||
self.global_position.x += 24.4
|
||||
Current_Position[0] +=1
|
||||
Prev_dir = "R"
|
||||
Prev_dir_Opp = "L"
|
||||
move += Move_Strength
|
||||
Been_There_Overide = false
|
||||
else:
|
||||
Been_There_Overide = true
|
||||
move += Move_Strength
|
||||
print("There is something to your right")
|
||||
|
||||
func goLeft():
|
||||
var L = $Leftv
|
||||
dir = Vector2.LEFT
|
||||
if not L.is_colliding():
|
||||
self.global_position.x -= 24.4
|
||||
Current_Position[0] -=1
|
||||
Prev_dir = "L"
|
||||
Prev_dir_Opp = "R"
|
||||
move += Move_Strength
|
||||
Been_There_Overide = false
|
||||
else:
|
||||
Been_There_Overide = true
|
||||
move += Move_Strength
|
||||
print("There is something to your left")
|
||||
|
||||
|
||||
func Mark(Prev_Direction):
|
||||
|
||||
var response = ways[IntersectionCounter()]
|
||||
Detail_Dict.Type = response
|
||||
Detail_Dict.Came_From = Prev_Direction
|
||||
Detail_Dict.Been_There = Traveled
|
||||
Detail_Dict.Move = move
|
||||
Coordinate_Dict.Position = Current_Position
|
||||
Coordinate_Dict.Details = Detail_Dict
|
||||
var Coordinate_Dict_Copy = Coordinate_Dict.duplicate(true)
|
||||
Coordinate_Dict = Coordinate_Dict_Copy
|
||||
#print(Coordinate_Dict)
|
||||
|
||||
|
||||
func WhatMove():
|
||||
|
||||
if Detail_Dict.Been_There == true:
|
||||
if state4 != 1:
|
||||
Move_Strength = 0
|
||||
move = move - 1
|
||||
state4 = 1
|
||||
if Detail_Dict.Been_There == false:
|
||||
if state4 != 2:
|
||||
Move_Strength = 1
|
||||
move = move + 1
|
||||
state4 = 2
|
||||
|
||||
func TravelLog():
|
||||
if Detail_Dict.Been_There == false:
|
||||
if move_check3 != move:
|
||||
LogDump.push_front(Prev_dir_Opp)
|
||||
LogDumpOrdered.push_front(Prev_dir)
|
||||
move_check3 = move
|
||||
|
||||
func BackTracking():
|
||||
if Cicling == false:
|
||||
if Detail_Dict.Type != "Plain Path" and Detail_Dict.Been_There == false:
|
||||
if BackTrack_Array.find(move) == -1:
|
||||
BackTrack_Array.push_back(move)
|
||||
if Intersections.find(Current_Position) == -1 and Intersections.find(Current_Position) != Intersections.size() -1 and BackTrack_Array.size() >= 2:
|
||||
TrackBack = GiveOrder()
|
||||
Distance_From_Intersection = BackTrack_Array[-1] - BackTrack_Array[-2]
|
||||
print("The distance from the last intersection is : ", Distance_From_Intersection)
|
||||
print("To get to the last intersection follow these steps : ", TrackBack)
|
||||
print(Individual_Steps)
|
||||
if Detail_Dict.Type == "Dead End" and Current_Position != [0,0]:
|
||||
GoBack(TrackBack)
|
||||
pass
|
||||
|
||||
|
||||
func Circling():
|
||||
if Been_There_Array.size() > 3:
|
||||
if move_check5 != move:
|
||||
if Intersections.find(Current_Position) != Intersections.size()-1 and Intersections.find(Current_Position) != -1 and Been_There_Array[-2] == false and Been_There_Array[-3] == false:
|
||||
var holder = []
|
||||
for n in (move - BackTrack_Array[-1]):
|
||||
holder.push_back(LogDump[n])
|
||||
holder.push_front(Prev_dir_Opp)
|
||||
print("Circling")
|
||||
Cicling = true
|
||||
Individual_Steps.push_front(holder)
|
||||
CircleRootsAndInts()
|
||||
HowManyIntsInCircle()
|
||||
FirstCirclePart()
|
||||
move_check5 = move
|
||||
|
||||
func HowManyIntsInCircle():
|
||||
CircleInts = Tree_Array.size() - Intersections.find(Current_Position)+1
|
||||
print(CircleInts)
|
||||
|
||||
|
||||
func FirstCirclePart():
|
||||
var count = CircleInts
|
||||
|
||||
if count == 0 and Current_Position == Intersections_from_root[0]:
|
||||
Cicling = false
|
||||
Intersection_Tree()
|
||||
Ordered_Commands_Backward.clear()
|
||||
print("Ignoreeeeeee")
|
||||
for takeout in CircleInts + 1:
|
||||
Individual_Steps.pop_front()
|
||||
Tree_Array.pop_back()
|
||||
print("Goin back")
|
||||
print(Individual_Steps)
|
||||
GoBack(Individual_Steps[0])
|
||||
CircleCompleted = true
|
||||
#** Returning if it needs more work or not
|
||||
return false
|
||||
|
||||
elif count != 0 and Current_Position == Intersections_from_root[0]:
|
||||
GoBack(Individual_Steps[0])
|
||||
Individual_Steps.pop_front()
|
||||
print("Number Of Ints: ", CircleInts)
|
||||
print(Individual_Steps)
|
||||
#** Returning if it needs more work or not
|
||||
return true
|
||||
|
||||
func CircleRootsAndInts():
|
||||
if Cicling == true:
|
||||
if Detail_Dict.Type != "Plain Path":
|
||||
if Intersections_from_root.find(Current_Position) == -1:
|
||||
Intersections_from_root.push_back(Current_Position)
|
||||
var Duplicate = Intersections_from_root.duplicate(true)
|
||||
Intersections_from_root = Duplicate
|
||||
|
||||
func DeadEndCircles():
|
||||
if Cicling == true:
|
||||
if Detail_Dict.Type == "Dead End":
|
||||
if AutoBacktrack == true:
|
||||
CircleIntSubtractor()
|
||||
return true
|
||||
else:
|
||||
return false
|
||||
|
||||
func SolvinCircleDeadEnds():
|
||||
if CircleManualDistance() != null:
|
||||
for n in CircleManualDistance():
|
||||
GoBack(LogDump[n])
|
||||
|
||||
func CircleComplete():
|
||||
if CircleCompleted == true:
|
||||
if Detail_Dict.Type != "Plain Path" and Detail_Dict.Been_There == false:
|
||||
Individual_Steps.push_front(GiveOrder())
|
||||
print("Circle Complete")
|
||||
print(Individual_Steps)
|
||||
print(Tree_Array)
|
||||
CircleCompleted = false
|
||||
else:
|
||||
pass
|
||||
|
||||
func CircleIntSubtractor():
|
||||
CircleInts -= 1
|
||||
print(CircleInts)
|
||||
|
||||
func Intersection_Tree():
|
||||
if Cicling == false:
|
||||
if Detail_Dict.Been_There == false:
|
||||
if Move_Check2 != move:
|
||||
if Detail_Dict.Type == "3 Way Intersection" and Current_Position != [0,0]:
|
||||
Tree_Array.push_back(2)
|
||||
Individual_Steps.push_front(TrackBack)
|
||||
print(Tree_Array)
|
||||
print(Intersections)
|
||||
if Detail_Dict.Type == "4 Way Intersection" and Current_Position != [0,0]:
|
||||
Tree_Array.push_back(3)
|
||||
Individual_Steps.push_front(TrackBack)
|
||||
print(Tree_Array)
|
||||
print(Intersections)
|
||||
if Detail_Dict.Type == "Dead End" and Current_Position != [0,0]:
|
||||
Tree_Array[-1] -= 1
|
||||
print(Tree_Array)
|
||||
print(Intersections)
|
||||
Move_Check2 = move
|
||||
|
||||
func CheckForTreeDeadEnds():
|
||||
if Tree_Array.size() > 0:
|
||||
if Tree_Array[-1] <= 0:
|
||||
Tree_Array.pop_back()
|
||||
Tree_Array[-1] -= 1
|
||||
print(Tree_Array)
|
||||
GoBack(Individual_Steps[0])
|
||||
Individual_Steps.pop_front()
|
||||
|
||||
else:
|
||||
pass
|
||||
|
||||
func CircleManualDistance():
|
||||
if DeadEndCircles() == true and Intersections_from_root.size() >= 2:
|
||||
var Starting_Pos = Intersections_from_root[-2]
|
||||
var Ending_Pos = Intersections_from_root[-1]
|
||||
|
||||
var x = abs(Starting_Pos[0] - Ending_Pos[0])
|
||||
var y = abs(Starting_Pos[1] - Ending_Pos[1])
|
||||
return x+y
|
||||
|
||||
func GiveOrder():
|
||||
Ordered_Commands_Backward.erase("")
|
||||
if move_check != move:
|
||||
TrackIntersections()
|
||||
if Detail_Dict.Been_There == false:
|
||||
Ordered_Commands_Backward.push_back(Prev_dir_Opp)
|
||||
if Detail_Dict.Type != "Plain Path":
|
||||
TakeOut = (Cords.size() - Cords.find(Intersections[-2]))
|
||||
var test = Ordered_Commands_Backward.slice(0 , TakeOut)
|
||||
test.reverse()
|
||||
Ordered_Commands_Backward.clear()
|
||||
return test
|
||||
move_check = move
|
||||
|
||||
|
||||
func TrackIntersections():
|
||||
if Intersections.find(Current_Position) == -1:
|
||||
if Detail_Dict.Type != "Plain Path" and Detail_Dict.Type != "Dead End":
|
||||
Intersections.push_back(Current_Position)
|
||||
Intersections_Copy = Intersections.duplicate(true)
|
||||
Intersections = Intersections_Copy
|
||||
IntersectionMove = move
|
||||
|
||||
func MoveAfterIntersection():
|
||||
if move == IntersectionMove+1:
|
||||
if Move_Check6 != move:
|
||||
if TrackBack.size() != 1:
|
||||
IntersectionDir.push_back(LogDumpOrdered[0])
|
||||
else:
|
||||
IntersectionDir.push_back(LogDump[0])
|
||||
print(Intersections)
|
||||
print(IntersectionDir)
|
||||
Move_Check6 = move
|
||||
|
||||
func IntersectionWaysAvailable():
|
||||
var R: bool = $Rightv.is_colliding()
|
||||
var L: bool = $Leftv.is_colliding()
|
||||
var D: bool = $Forwardv.is_colliding()
|
||||
var U: bool = $Backv.is_colliding()
|
||||
|
||||
var Sender = []
|
||||
var Colliders = [R,L,U,D]
|
||||
var DirArray = ["R","L","U","D"]
|
||||
if Move_Check7 != move and Detail_Dict.Type != "Plain Path" and Detail_Dict.Type != "Dead End" and Detail_Dict.Been_There == false:# and Intersections.find(Current_Position) == -1:
|
||||
for n in Colliders.size():
|
||||
if Colliders[n] == false:
|
||||
Sender.push_back(DirArray[n])
|
||||
IntOptions.push_back(Sender)
|
||||
print(IntOptions)
|
||||
if Move_Check7 != move and Detail_Dict.Type != "Plain Path" and Detail_Dict.Type != "Dead End" and Detail_Dict.Been_There == true:
|
||||
print(IntOptions)
|
||||
Move_Check7 = move
|
||||
|
||||
|
||||
func BackToStart():
|
||||
if Detail_Dict.Type == "Plain Path":
|
||||
Individual_Steps.push_front(Ordered_Commands_Backward)
|
||||
|
||||
|
||||
func Been_There():
|
||||
if Been_There_Overide == false:
|
||||
if Cords.find(Current_Position) == -1:
|
||||
Cords.push_back(Current_Position)
|
||||
Cords_Copy = Cords.duplicate(true)
|
||||
Cords = Cords_Copy
|
||||
Traveled = false
|
||||
|
||||
else:
|
||||
Traveled = true
|
||||
Been_There_Array.push_back(Traveled)
|
||||
|
||||
func recall():
|
||||
print(Individual_Steps)
|
||||
for Sections in Individual_Steps:
|
||||
#print(Sections)
|
||||
for Section in Sections:
|
||||
#Move(Section)
|
||||
pass
|
||||
|
||||
func GoBack(Set):
|
||||
AutoBacktrack = true
|
||||
for n in Set:
|
||||
Move(n)
|
||||
|
||||
|
||||
func IntersectionCounter():
|
||||
var R: bool = $Rightv.is_colliding()
|
||||
var L: bool = $Leftv.is_colliding()
|
||||
var D: bool = $Forwardv.is_colliding()
|
||||
var U: bool = $Backv.is_colliding()
|
||||
var WaysToGo = 0
|
||||
var Directions = [R,L,D,U]
|
||||
for Direction in Directions:
|
||||
if Direction == false:
|
||||
WaysToGo += 1
|
||||
if Direction == true:
|
||||
pass
|
||||
return WaysToGo
|
||||
|
||||
|
||||
|
||||
func Move(SetDirection):
|
||||
|
||||
if SetDirection == "R":
|
||||
self.global_position.x += 24.4
|
||||
Current_Position[0] +=1
|
||||
Prev_dir = "R"
|
||||
Prev_dir_Opp = "L"
|
||||
move += Move_Strength
|
||||
Been_There()
|
||||
|
||||
if SetDirection == "L":
|
||||
self.global_position.x -= 24.4
|
||||
Current_Position[0] -=1
|
||||
Prev_dir = "L"
|
||||
Prev_dir_Opp = "R"
|
||||
move += Move_Strength
|
||||
Been_There()
|
||||
if SetDirection == "D":
|
||||
self.global_position.y += 24
|
||||
Current_Position[1] -=1
|
||||
Prev_dir = "D"
|
||||
Prev_dir_Opp = "U"
|
||||
move += Move_Strength
|
||||
Been_There()
|
||||
if SetDirection == "U":
|
||||
self.global_position.y -= 24
|
||||
Current_Position[1] +=1
|
||||
Prev_dir = "U"
|
||||
Prev_dir_Opp = "D"
|
||||
move += Move_Strength
|
||||
Been_There()
|
||||
#print(Current_Position)
|
||||
|
||||
|
||||
func AutoSolve():
|
||||
var R: bool = $Rightv.is_colliding()
|
||||
var L: bool = $Leftv.is_colliding()
|
||||
var D: bool = $Forwardv.is_colliding()
|
||||
var U: bool = $Backv.is_colliding()
|
||||
|
||||
var DirectionStrengths = [6,6,5,6]
|
||||
var Colliders = [R,L,U,D]
|
||||
var DirArray = ["R","L","U","D"]
|
||||
var DumpDuplicate = LogDump
|
||||
|
||||
|
||||
var cp = Current_Position
|
||||
var OptionArrays = [[cp[0],cp[1]+1],[cp[0],cp[1]-1],[cp[0]+1,cp[1]],[cp[0]-1,cp[1]]]
|
||||
|
||||
for n in DirectionStrengths.size():
|
||||
var MoveScore = DumpDuplicate.rfind(DirArray[n])
|
||||
if Colliders[n] == true:
|
||||
DirectionStrengths[n] -= 1000
|
||||
if DirArray.find(Prev_dir_Opp) == n:
|
||||
DirectionStrengths[n] -= 50
|
||||
if Individual_Steps.size() < 1 and Ordered_Commands_Backward.size() > 1:
|
||||
if Ordered_Commands_Backward[0] == DirArray[n]:
|
||||
DirectionStrengths[n] -= 25
|
||||
elif Individual_Steps.size() >= 1:
|
||||
var Wayback = Individual_Steps[0]
|
||||
if Wayback[0] == DirArray[n]:
|
||||
DirectionStrengths[n] -= 25
|
||||
if MoveScore < 15:
|
||||
DirectionStrengths[n] -= MoveScore
|
||||
|
||||
for i in OptionArrays:
|
||||
if Intersections.find(i) != -1:
|
||||
DirectionStrengths[n] -= 100
|
||||
|
||||
|
||||
|
||||
|
||||
AutoBacktrack = false
|
||||
var HighestPoints = DirectionStrengths.find(max(DirectionStrengths[0],DirectionStrengths[1],DirectionStrengths[2],DirectionStrengths[3]))
|
||||
#print(DirArray[HighestPoints])
|
||||
Move(DirArray[HighestPoints])
|
||||
#print(DirectionStrengths)
|
||||
#print(DirArray)
|
||||
#print(Individual_Steps)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user