Jump to content

script help


 Share

Recommended Posts

Hello, I'm trying to code a way point system path maker for a tile based game.

The thing is that i have it nailed down and working, but when it trys to auto add way points when i walk, it doesn't seem to add

all of the coordinates, it adds them yes, but not everyone for every step i take, like i want it to.

I'm after a few tips or ideas on how i can improve the insertcoordinates function so that when every step i take it will then add them to the path[] array.

here is a look at the code, the problem is inside the _insertcoordinate() function. help appreicated.

#region 7.10 by Dean
;Include Functions
#include <ReadWriteMemory.au3>
#include <PostMessage_UDF.au3>
#include <Array.au3>

;Create varible to store Window Title
Dim $windowtitle = False, $nmsg = "Status: Searching...  "
Global $start, $stop
;Store Value to Window Title, if need be.
While (Not $windowtitle) Or ($windowtitle = "")
    $windowtitle = "Darkages"
WEnd

;We set GetDAFile to global because we call it before its primarly initlized, this eliminates errors.
Global $GetDAFile

;Create the Handle Array $hWnd to hold process infomation about the DA client
Dim $handle[2] = [WinGetHandle("Darkages"), _OpenProcess (WinGetHandle("Darkages")) ]
;We create our boolean varibles here and set them to false.
Dim $error = 0, $valid = False
Dim $validclient = False

;Call Function to Check DA File Path
_PathCheck()

;Check hwnd for any errors before continuing
For $x = 1 To UBound($handle) - 1
    If $handle[$x] = @error Then
        $error = "Darkages was not attached."
        MsgBox(80, "Error", $error)
        Exit
    Else
    ;check DA file version before continuing
        Dim $DAVersion[3] = [FileGetVersion($GetDAFile), FileGetAttrib($GetDAFile), False]
        If (StringLeft($DAVersion[0], 3) = 360) And (StringLeft($DAVersion[1], 1) = "A") Then $DAVersion[2] = True
        If $DAVersion[2] Then $validclient = True
    EndIf
Next

;check the DA path setting
If Not $validclient Then
    MsgBox(80, "Error", "Incorrect Path Setting, Press ok to Edit")
    $GetDAFile = ""
    While $GetDAFile = ""
        $GetDAFile = FileOpenDialog("Select Darkages Path", "C:/Program Files/KRU/Dark Ages/", "")
        If $GetDAFile <> "" Then
            FileWrite(@ScriptDir & "\DAPATH.txt", $GetDAFile)
            ExitLoop
        EndIf
    WEnd
EndIf

;checking the DA version before continuing
If ($validclient = True) And StringLeft($DAVersion[0], 3) <> 360 Then
    MsgBox(80, "Error", "Version Outdated. Program Terminated.")
    Exit
Else
    $valid = True
EndIf

Dim $overwrite = True; true/false to allow/disallow overwriting waypoints.
Dim $lstr = 70 ; increase/decrease this to set distance between the auto waypoint creater
Dim $path[1]; coordinate Array

HotKeySet("{F1}", "_Add"); Create F1 HotKey.
HotKeySet("{F2}", "_Walk"); Create F2 HotKey.
HotKeySet("{F3}", "_Delete"); Create F3 HotKey.
HotKeySet("{F4}", "_Done"); Create F4 Hotkey.



;===================================================================================================
=================================
While ($valid)
;Display ToolTip
    $coords = _MapCoords()
    $msg = " [ Press F1 to Add   --   Press F2 to Test   --   Press F3 to Reset  ] "
    $msg &= @CRLF & "    ~ Press F4 When you have created a valid hunting path ~  "
    $msg &= @CRLF & "---------------------------------------------------------------------------------" & @CRLF & _InsertCoordinate()
    ToolTip($msg, 0, 0, "7.10 Bot")
    Sleep(10)
WEnd

ToolTip("");reset ToolTip
$msg = ""

While ((Not $valid) And UBound($path) > 0)
    ToolTip("This Profile has " & (UBound($path)) & " Waypoints." & @CRLF & "Press F6 to Begin" & @CRLF _
             & "Closest Waypoint: " & _FindWaypoints(5), 0, 0, "7.10 Bot")
    Sleep(100)
    
;TODO
;ScanMonster Struct
;Walk To Monster
;Walk to Nearest waypoint
;continue waypoint
;Else
;walk()
WEnd
;===================================================================================================
=================================


;Function to Find the closest waypoints
Func _FindWaypoints($acc)
    Local $coords, $ret, $x, $y, $ret1
    Local $mapdiff = _MapCoords()
    For $i = 1 To UBound($path) - 1
        $coords = StringSplit($path[$i], ",")
        If IsArray($coords) Then
            $x = $coords[1]
            $y = $coords[2]
        EndIf
        If ($mapdiff[1] - $x) <= $acc Then
            $ret = $i
        EndIf
        If ($mapdiff[2] - $y) <= $acc Then
            $ret1 = $ret & "," & $i
        EndIf
    Next
    Return $path[$ret]
EndFunc  ;==>_FindWaypoints

;Function to Toggle off Waypoint Adding.
Func _Done()
    If UBound($path) >= 10 Then
        $valid = False
    Else
        $valid = True
    EndIf
EndFunc  ;==>_Done

;This Calls Movetowaypoint and initlize's path array
Func _Walk()
    If UBound($path) > 10 Then
    ;Walk Thru WayPoints
        For $i = 1 To UBound($path) - 1
            _MoveToWaypoint($path[$i], 1, $i)
        Next
    Else
        MsgBox(80, "Walk Error", "Not Enough waypoints to create path.")
    EndIf
EndFunc  ;==>_Walk

;Function to forcefully Create a waypoint
Func _Add()
    Local $coords = _MapCoords()
    If IsArray($coords) Then
        ReDim $path[UBound($path) + 1]
        $path[UBound($path) - 1] = $coords[1] & "," & $coords[2]
        SetError(0)
        Return 1
    Else
        SetError(1)
        Return 0
    EndIf
EndFunc  ;==>_Add

;Function that clears the path array to clear it.
Func _Delete()
    If UBound($path) > 0 Then
        For $i = 1 To UBound($path) - 1
            _ArrayDelete($path, 1)
        Next
    Else
        MsgBox(80, "Delete Error", "Try Creating some waypoints before trying to delete them. =p")
    EndIf
EndFunc  ;==>_Delete

;Function to insert coordinates
Func _InsertCoordinate()
;Update Coordinates
    Local $i = 0
    While ($i < $lstr)
        Local $add = False
        For $x = 1 To UBound($coords) - 1
            $lastcoords = _MapCoords()
        Next
        $i += 1
    WEnd
;Compare coordinates to see if you moved.
    If $lastcoords[1] <> $coords[1] Or $lastcoords[2] <> $coords[2] Then $add = True
;Add coordinates to array
    If $add = True Then
        For $i = 1 To UBound($path) - 1
            If ($path[$i] = $coords[1] & "," & $coords[2]) Then
                $add = $overwrite
            EndIf
        Next
        If ($add = True) Then _ArrayAdd($path, $coords[1] & "," & $coords[2])
    EndIf
;return
    Return ((UBound($path) - 1)) & " Waypoints Stored." & @CRLF & _CoordList($path)
EndFunc  ;==>_InsertCoordinate

;Function to recv last player coordinates
Func _LastCoords()
    Local $array[1]
    _ArrayInsert($array, 1, _
            _ReadMemory ($handle[1], _ReadMemory ($handle[1], _ReadMemory ($handle[1], _ReadMemory ($handle[1], _ReadMemory ($handle[1], 0x6711F8 + 0x48, 4) + 0x278, 4) + 0x4C, 4) + 0x48, 4) + 0x14, 2))
    _ArrayInsert($array, 1, _
            _ReadMemory ($handle[1], _ReadMemory ($handle[1], _ReadMemory ($handle[1], _ReadMemory ($handle[1], _ReadMemory ($handle[1], 0x6711F8 + 0x48, 4) + 0x278, 4) + 0x4C, 4) + 0x48, 4) + 0x12, 2))
    Return $array
EndFunc  ;==>_LastCoords

;Display Waypoints
Func _CoordList(Const ByRef $avArray)
    Local $iCounter = 0, $sMsg = ""
    If (Not IsArray($avArray)) Then
        SetError(1)
        Return 0
    EndIf
    For $iCounter = 1 To UBound($avArray) - 1
        $sMsg &= "Waypoint [" & $iCounter & "]    " & "  " & StringStripCR($avArray[$iCounter]) & "   " & @CR
    Next
    Return $sMsg
EndFunc  ;==>_CoordList

;Initilize pointer function
Func _InitPointer($ptr)
    $ptr = _ReadMemory ($handle[1], $ptr, 4)
    If $ptr > 0 Then Return $ptr
    Return 0
EndFunc  ;==>_InitPointer

;Function to Read Map Coordinates
Func _MapCoords()
    Local $ptr = _InitPointer(0x8719E0), $array[1]
    If $ptr > 0 Then
        _ArrayInsert($array, 1, _ReadMemory ($handle[1], $ptr + 0x238, 4))
        _ArrayInsert($array, 1, _ReadMemory ($handle[1], $ptr + 0x23C, 4))
        Return $array
    EndIf
EndFunc  ;==>_MapCoords

;open names file
Func _openfile($f)
    Local $file = FileOpen($f, 0)
    If $file = -1 Then
        $reg = False
    EndIf
EndFunc  ;==>_openfile

;Function that saves our DA Path text file with a valid path.
Func _PathCheck()
    If FileExists("DAPATH.txt") Then
        FileOpen("DAPATH.txt", 0)
        $GetDAFile = FileReadLine("DAPATH.txt")
    Else
        While 1
            $GetDAFile = FileOpenDialog("Select Darkages Path", "C:/Program Files/KRU/Dark Ages/", "Darkages (Darkages.exe)", "Darkages.exe")
            If $GetDAFile <> "" Then
                FileWrite(@ScriptDir & "\DAPATH.txt", $GetDAFile)
                ExitLoop
            EndIf
        WEnd
    EndIf
    FileClose("DAPATH.txt")
EndFunc  ;==>_PathCheck

;Function that walks character to coordinates
Func _MoveToWaypoint($wArray, $accuracy, $ci)
    Local $xdiff, $ydiff, $xkey, $ykey, $coords
    Local $ret, $lastx, $lasty, $count
    $ret = StringSplit($wArray, ",")
    If IsArray($ret) Then
        $move_x = $ret[1]
        $move_y = $ret[2]
    EndIf
    $coords = _MapCoords()
    If IsArray($coords) Then
        $xdiff = ($coords[1] - $move_x)
        $ydiff = ($coords[2] - $move_y)
    EndIf
    Do
        ToolTip("Walking to Waypoint [" & $ci & "]" & "(" & $move_x & "," & $move_y & ")", 0, 0)
        $ret = StringSplit($wArray, ",")
        If IsArray($ret) Then
            $move_x = $ret[1]
            $move_y = $ret[2]
        EndIf
        
        $coords = _MapCoords()
        If IsArray($coords) Then
            $xdiff = ($coords[1] - $move_x)
            $ydiff = ($coords[2] - $move_y)
        EndIf
        
        $count += 1
        
        If _IsStuck($coords[1], $coords[2], $lastx, $lasty, $count) Then
            _GetUnstuck($xdiff, $ydiff, $move_x, $move_y, $accuracy)
            $coords = _MapCoords()
            If IsArray($coords) Then
                $xdiff = ($coords[1] - $move_x)
                $ydiff = ($coords[2] - $move_y)
            EndIf
        EndIf
        
        If $xdiff < 0 Then
            $xkey = "right"
        Else
            $xkey = "left"
        EndIf
        If $ydiff < 0 Then
            $ykey = "down"
        Else
            $ykey = "up"
        EndIf
        If (Abs($xdiff) > Abs($ydiff)) Then
            If Abs($xdiff) > $accuracy Then _ArrowKey ($handle[0], $xkey)
            Sleep(150)
            If Abs($xdiff) > $accuracy Then _ArrowKey ($handle[0], $xkey)
        Else
            If Abs($ydiff) > $accuracy Then _ArrowKey ($handle[0], $ykey)
            Sleep(150)
            If Abs($ydiff) > $accuracy Then _ArrowKey ($handle[0], $ykey)
        EndIf
        $coords = _MapCoords()
        If IsArray($coords) Then
            $xdiff = ($coords[1] - $move_x)
            $ydiff = ($coords[2] - $move_y)
        EndIf
        If Abs($xdiff) <= 2 And Abs($ydiff) <= 2 Then
            Sleep(150)
        Else
            Sleep(150)
        EndIf
    Until (Abs($xdiff) <= $accuracy And Abs($ydiff) <= $accuracy)
EndFunc  ;==>_MoveToWaypoint

;Function to See if were stuck.
Func _IsStuck($curx, $cury, ByRef $lastx, ByRef $lasty, ByRef $count)
    If $lastx <> $curx Then
        $lastx = $curx
        $count = 0
        Return 0
    EndIf
    If $lasty <> $cury Then
        $lasty = $cury
        $count = 0
        Return 0
    EndIf
    
    If $count >= 2 Then
        Return 1
    EndIf
EndFunc  ;==>_IsStuck

;Gets Us Unstuck in the event we get stuck.
Func _GetUnstuck($xdiff, $ydiff, $move_x, $move_y, $accuracy)
    Local $hWnd = $handle[0]
    If Abs($xdiff) <= 5 And Abs($ydiff) <= 5 Then
    ;click on the desired tile and loop until not moving
    EndIf

    If Abs($xdiff) < 5 And Abs($ydiff) < 5 Then;we're close
        If $xdiff = 0 Then
            If $ydiff >= 1 Then;traveling up
                _MouseClick ($hWnd, "right", Round(Random(320, 600)), Round(Random(25, 225)));top-right
            ElseIf $ydiff <= 1 Then;traveling down
                _MouseClick ($hWnd, "right", Round(Random(25, 300)), Round(Random(100, 300)));bottom-left
            Else
                _MouseClick ($hWnd, "right", Round(Random(25, 600)), Round(Random(25, 300)));random anywhere catch-all
            EndIf
        ElseIf $xdiff > 0 And $xdiff <= 2 Then;random click anywhere, basically, as long as it's to the left 2/3 of screen
            If $ydiff < 0 And $ydiff >= -2 Then;we're almost horizontally to the right of where we want to be, so click left
                _MouseClick ($hWnd, "right", Round(Random(100, 300)), Round(Random(25, 300)));random click LEFT
            ElseIf $ydiff < -2 Then;we want to go down and to the left
                _MouseClick ($hWnd, "right", Round(Random(25, 300)), Round(Random(100, 300)));bottom-left
            ElseIf $ydiff > 0 And $ydiff <= 2 Then;we want to go almost directly above us
                _MouseClick ($hWnd, "right", Round(Random(200, 400)), Round(Random(25, 150)));top-middle
            ElseIf $ydiff > 2 Then
                _MouseClick ($hWnd, "right", Round(Random(300, 600)), Round(Random(25, 150)));top-right
            Else
                _MouseClick ($hWnd, "right", Round(Random(25, 600)), Round(Random(25, 300)));random anywhere catch-all
            EndIf
        Else
            _MouseClick ($hWnd, "right", Round(Random(25, 600)), Round(Random(25, 300)));random anywhere catch-all
        EndIf
    Else
        _MouseClick ($hWnd, "right", Round(Random(25, 600)), Round(Random(25, 300)));random anywhere catch-all
    EndIf

    Local $i
    For $i = 1 To 10
        If Abs($xdiff) <= $accuracy And Abs($ydiff) <= $accuracy Then ExitLoop
        Sleep(100)
        $coords = _MapCoords()
        If IsArray($coords) Then
            $xdiff = ($coords[1] - $move_x)
            $ydiff = ($coords[2] - $move_y)
        EndIf
    Next
EndFunc  ;==>_GetUnstuck
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...