#include <GDIP.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Math.au3>
#include <Color.au3>
#include <File.au3>
#include <Misc.au3>
#include <Array.au3>
If WinExists(@ScriptName) Then Exit
AutoItWinSetTitle(@ScriptName)
Opt("GUIOnEventMode", 1)
Opt('MustDeclareVars', 1)
Opt("TrayIconHide", 0)
Opt("MouseCoordMode", 2)
HotKeySet("{ESC}", "GUIExit")
Global $Label[99][99],$CellSize = 5, $Data = 1
Global $SaveMap,$LoadMap,$Color='0x000000',$GeoData[UBound($Label,1)][UBound($Label,2)]
Global $GUI,$Stena,$Prohod,$Start,$Finish,$bStart
Global $Loadgui=0
_LoadGUI()
Func _LoadGUI()
$Loadgui = GUICreate("Loading", 60, 20, -1, -1, BitOR($WS_MINIMIZEBOX,$WS_POPUP,$WS_GROUP), BitOR($WS_EX_TOOLWINDOW,$WS_EX_TOPMOST,$WS_EX_WINDOWEDGE))
GUICtrlCreateLabel("Loading...", 5, 5, 60, 20)
GUISetBkColor(0xFFFFFF)
GUISetState(@SW_SHOW, $Loadgui)
EndFunc
GUI()
Func GUI()
Local $RGBColor,$yMax = UBound($Label,2)-1, $xMax = UBound($Label,1)-1
$GUI = GUICreate("Map Editor", 600, 520)
$Stena = GUICtrlCreateRadio("Стена", 0, 0, 101, 17)
$Prohod = GUICtrlCreateRadio("Проход", 105, 0, 89, 17)
$Start = GUICtrlCreateRadio("Старт", 210,0,100,17)
$Finish = GUICtrlCreateRadio("Финиш", 315,0,100,17)
$SaveMap=GUICtrlCreateButton("Save map",510,30,60)
GUICtrlSetOnEvent(-1, "SaveMap")
$bStart=GUICtrlCreateButton("Start",510,60,60)
GUICtrlSetOnEvent(-1, "Start")
$LoadMap=GUICtrlCreateButton("Load map",510,90,60)
GUICtrlSetOnEvent(-1, "LoadMap")
GUISetOnEvent($GUI_EVENT_PRIMARYDOWN,"Mouse1")
GUISetOnEvent($GUI_EVENT_SECONDARYDOWN,"Mouse2")
_GDIPlus_Startup()
Local $hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & '\map.JPG')
Local $iWidth = _GDIPlus_ImageGetWidth($hImage)
Local $iHeight = _GDIPlus_ImageGetHeight($hImage)
Local $iBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
Local $hBitmap = _GDIPlus_BitmapCreateFromHBITMAP($iBitmap)
For $y = 0 To $yMax Step 1
For $x = 0 To $xMax Step 1
$Label[$x][$y] = GUICtrlCreateLabel(1, $x*$CellSize, $y*$CellSize+17, $CellSize, $CellSize, $WS_BORDER)
$Color = '0x' & Hex(_GDIPlus_BitmapGetPixel($hBitmap, 33+$x*7, 55+$y*7),6)
GUICtrlSetData($Label[$x][$y], 0) ; Стена
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlSetBkColor(-1, 0xFFFFFF)
Next
Next
LoadMap() ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
GUISetOnEvent($GUI_EVENT_CLOSE, "GUIExit")
GUISetState()
GUIDelete($Loadgui)
While 1
Sleep(1000)
WEnd
EndFunc
Func GUIExit()
Exit
EndFunc
Func Mouse1()
Local $pos,$x,$y
Do
$pos = MouseGetPos()
$x = Ceiling($pos[0]/$CellSize)-1
$y = Ceiling(($pos[1]-17)/$CellSize)-1
If $x > UBound($Label,1)-1 Or $x < 0 Then
ExitLoop
EndIf
If $y > UBound($Label,2)-1 Or $y < 0 Then
ExitLoop
EndIf
Select
Case GUICtrlRead($Prohod) = $GUI_CHECKED
GUICtrlSetData($Label[$x][$y], 0) ; Проход
GUICtrlSetBkColor($Label[$x][$y], 0xFFFFFF) ; Проход
Case GUICtrlRead($Stena) = $GUI_CHECKED
GUICtrlSetData($Label[$x][$y], 1) ; Стена
GUICtrlSetBkColor($Label[$x][$y], 0x000000) ; Стена
Case GUICtrlRead($Start) = $GUI_CHECKED
GUICtrlSetData($Label[$x][$y], 4)
GUICtrlSetBkColor($Label[$x][$y], 0x00FF00)
Case GUICtrlRead($Finish) = $GUI_CHECKED
GUICtrlSetData($Label[$x][$y], 5)
GUICtrlSetBkColor($Label[$x][$y], 0xFF0000)
EndSelect
Sleep(10)
Until Not _IsPressed("01")
EndFunc
Func Mouse2()
Local $pos,$x,$y
Do
$pos = MouseGetPos()
$x = Ceiling($pos[0]/$CellSize)-1
$y = Ceiling(($pos[1]-17)/$CellSize)-1
If $x > UBound($Label,1)-1 Or $x < 0 Then
ExitLoop
EndIf
If $y > UBound($Label,2)-1 Or $y < 0 Then
ExitLoop
EndIf
GUICtrlSetData($Label[$x][$y], 1) ; Стена
GUICtrlSetBkColor($Label[$x][$y], 0x000000) ; Стена
Sleep(10)
Until Not _IsPressed("02")
EndFunc
Func SaveMap()
Local $txMax = UBound($Label,1)-1,$tyMax = UBound($Label,2)-1
GUICtrlSetState($SaveMap, $GUI_DISABLE)
Local $File = @ScriptDir & "\map_geodata.txt"
FileDelete($File)
For $x = 0 To $txMax
For $y = 0 To $tyMax
$GeoData[$x][$y] = GUICtrlRead($Label[$x][$y])
ToolTip($GeoData[$x][$y])
GUICtrlSetData($SaveMap," "&$x&"% ")
Next
Next
GUICtrlSetData($SaveMap," 99% ")
_Array4File($File,$GeoData)
GUICtrlSetState($SaveMap, $GUI_ENABLE)
GUICtrlSetData($SaveMap,"Save map")
EndFunc
Func LoadMap()
Local $txMax = UBound($Label,1)-1,$tyMax = UBound($Label,2)-1
Local $File = @ScriptDir & "\map_geodata.txt"
If Not FileExists($File) Then Return 0
GUICtrlSetState($LoadMap, $GUI_DISABLE)
$GeoData=_File4Array($File)
ReDim $GeoData[$txMax+1][$tyMax+1]
For $y = 0 To $txMax
For $x = 0 To $txMax
Select
Case $GeoData[$y][$x]=0
GUICtrlSetData($Label[$x][$y], 0) ; Проход
GUICtrlSetBkColor($Label[$x][$y], 0xFFFFFF) ; Проход
Case $GeoData[$y][$x]=1
GUICtrlSetData($Label[$x][$y], 1) ; Стена
GUICtrlSetBkColor($Label[$x][$y], 0x000000) ; Стена
Case $GeoData[$y][$x]=4
GUICtrlSetData($Label[$x][$y], 4)
GUICtrlSetBkColor($Label[$x][$y], 0x00FF00)
Case $GeoData[$y][$x]=5
GUICtrlSetData($Label[$x][$y], 5)
GUICtrlSetBkColor($Label[$x][$y], 0xFF0000)
EndSelect
Next
Next
GUICtrlSetState($LoadMap, $GUI_ENABLE)
GUICtrlSetData($LoadMap,"Load map")
EndFunc
Func Start()
GUICtrlSetState($bStart, $GUI_DISABLE)
Local $ttStart[2]=[0,0],$ttFinish[2]=[0,0],$temp
For $x = 0 To UBound($Label,1)-1
For $y = 0 To UBound($Label,2)-1
$GeoData[$x][$y] = GUICtrlRead($Label[$x][$y])
If $GeoData[$x][$y]=4 Then Local $ttStart[2]=[$x,$y]
If $GeoData[$x][$y]=5 Then Local $ttFinish[2]=[$x,$y]
Next
Next
$temp=AnalysisTravel($GeoData,$ttStart,$ttFinish)
_ArrayDisplay($temp)
GUICtrlSetState($bStart, $GUI_ENABLE)
EndFunc
Func AnalysisTravel($GeoMap,$tStart,$tFinish)
Local $txMax = UBound($GeoMap,1)-1, $tyMax = UBound($GeoMap,2)-1
Local $t2=Round($txMax*$tyMax/1.5)
Local $t1=10,$t3=0,$t4=0
$GeoMap[$tStart[0]][$tStart[1]]=$t1
Local $tX=0,$tY=0,$kletki[9]
Do
ToolTip($t1)
For $y = 0 To $tyMax Step 1
For $x = 0 To $txMax Step 1
If $GeoMap[$x][$y]=$t1 Then
$t3=0
$t4=0
For $i = -1 To 1 Step 2
If $x+$i<0 Then ContinueLoop
If $x+$i>$txMax Then ContinueLoop
If $GeoMap[$x+$i][$y] = 0 Then
$GeoMap[$x+$i][$y] = $t1+1
GUICtrlSetBkColor($Label[$x+$i][$y], 0x0066CC)
$t3+=1
EndIf
If $GeoMap[$x+$i][$y] = $GeoMap[$tFinish[0]][$tFinish[1]] Then
MsgBox(0,0,"Start found!")
$tX = $x+$i
$tY = $y
ExitLoop(4)
EndIf
Next
For $i = -1 To 1 Step 2
If $y+$i<0 Then ContinueLoop
If $y+$i>$tyMax Then ContinueLoop
If $GeoMap[$x][$y+$i] = 0 Then
$GeoMap[$x][$y+$i] = $t1+1
GUICtrlSetBkColor($Label[$x][$y+$i], 0x0066CC)
$t3+=1
EndIf
If $GeoMap[$x][$y+$i] = $GeoMap[$tFinish[0]][$tFinish[1]] Then
MsgBox(0,0,"Start found!")
$tX = $x
$tY = $y+$i
ExitLoop(4)
EndIf
Next
If $t3=$t4 Then
For $i = -1 To 1
If $x+$i<0 Then ContinueLoop
If $x+$i>$txMax Then ContinueLoop
For $j = -1 To 1
If $y+$j<0 Then ContinueLoop
If $y+$j>$tyMax Then ContinueLoop
If $GeoMap[$x+$i][$y+$j] = 0 Then
$GeoMap[$x+$i][$y+$j] = $t1+1
GUICtrlSetBkColor($Label[$x+$i][$y+$j], 0x0066CC)
$t3+=1
EndIf
If $GeoMap[$x+$i][$y+$j] = $GeoMap[$tFinish[0]][$tFinish[1]] Then
MsgBox(0,0,"Start found!")
$tX = $x + $i
$tY = $y + $j
ExitLoop(5)
EndIf
Next
Next
EndIf
EndIf
Next
Next
$t1+=1
Until $t1=$t2
ToolTip("")
If $t1=$t2 Then
ToolTip("путь не найден")
SetError(1)
Return 0
EndIf
Local $tt=-1
Local $Return[1][2]
While 1
$tt+=1
If $tt > 0 Then ReDim $Return[$tt+1][2]
$x = $tX
$y = $tY
For $i=0 To 8
$kletki[$i] = $t2
Next
For $i = -1 To 1
If $x+$i<0 Then ContinueLoop
If $x+$i>$txMax Then ContinueLoop
For $j = -1 To 1
If $y+$j<0 Then ContinueLoop
If $y+$j>$tyMax Then ContinueLoop
If $GeoMap[$x+$i][$y+$j] = 0 Or $GeoMap[$x+$i][$y+$j] = 1 Or $GeoMap[$x+$i][$y+$j] = 5 Then $GeoMap[$x+$i][$y+$j] = $t2
$kletki[4 + $j*3 + $i] = $GeoMap[$x+$i][$y+$j]
Next
Next
Switch _ArrayMinIndex($kletki)
Case 0
$tX = $x-1
$tY = $y-1
$Return[$tt][0]=$tX
$Return[$tt][1]=$tY
GUICtrlSetBkColor($Label[$tX][$tY], 0x00FF00)
Case 1
$tX = $x
$tY = $y-1
$Return[$tt][0]=$tX
$Return[$tt][1]=$tY
GUICtrlSetBkColor($Label[$tX][$tY], 0x00FF00)
Case 2
$tX = $x+1
$tY = $y-1
$Return[$tt][0]=$tX
$Return[$tt][1]=$tY
GUICtrlSetBkColor($Label[$tX][$tY], 0x00FF00)
Case 3
$tX = $x-1
$tY = $y
$Return[$tt][0]=$tX
$Return[$tt][1]=$tY
GUICtrlSetBkColor($Label[$tX][$tY], 0x00FF00)
Case 5
$tX = $x+1
$tY = $y
$Return[$tt][0]=$tX
$Return[$tt][1]=$tY
GUICtrlSetBkColor($Label[$tX][$tY], 0x00FF00)
Case 6
$tX = $x-1
$tY = $y+1
$Return[$tt][0]=$tX
$Return[$tt][1]=$tY
GUICtrlSetBkColor($Label[$tX][$tY], 0x00FF00)
Case 7
$tX = $x
$tY = $y+1
$Return[$tt][0]=$tX
$Return[$tt][1]=$tY
GUICtrlSetBkColor($Label[$tX][$tY], 0x00FF00)
Case 8
$tX = $x+1
$tY = $y+1
$Return[$tt][0]=$tX
$Return[$tt][1]=$tY
GUICtrlSetBkColor($Label[$tX][$tY], 0x00FF00)
Case Else
ToolTip("Пришли")
ReDim $Return[$tt][2]
Local $t[UBound($Return,1)][2]
For $i=0 To UBound($Return,1)-1
$t[UBound($Return,1)-1-$i][0]=$Return[$i][0]
$t[UBound($Return,1)-1-$i][1]=$Return[$i][1]
Next
Return $t
EndSwitch
WEnd
EndFunc
Func _File4Array($lFileDir,$lSection="Base")
;-----------------------------------------------------------------------------------
Local $iUb2 = 2
Dim $aSection = IniReadSection($lFileDir, $lSection)
If @error Then
Return @error
EndIf
;Author: "madmasles"
Dim $aResult[$aSection[0][0]][$iUb2]
For $i = 0 To $aSection[0][0] - 1
Local $aTemp = StringSplit($aSection[$i + 1][1], '|')
If $iUb2 < $aTemp[0] Then
$iUb2 = $aTemp[0]
ReDim $aResult[$aSection[0][0]][$iUb2]
EndIf
For $j = 0 To $aTemp[0] - 1
$aResult[$i][$j] = $aTemp[$j + 1]
Next
Next
Return $aResult
;----------------------------------------------------------------------------------
EndFunc
Func _Array4File($lFileDir,$Array4File,$lSection='Base')
If Not IsArray($Array4File) Then
Return @error
EndIf
FileOpen($lFileDir,1)
FileWrite($lFileDir,"[Base]"&@CRLF)
For $j=0 To UBound($Array4File,2)-1
FileWrite($lFileDir,$j&"=")
For $i=0 To UBound($Array4File,1)-1
FileWrite($lFileDir,$Array4File[$i][$j]&"|")
Next
FileWrite($lFileDir,@CRLF)
Next
FileClose($lFileDir)
EndFunc