Skip to content

Instantly share code, notes, and snippets.

@slucero
Created August 8, 2018 14:51
Show Gist options
  • Save slucero/e493c7657d3e17f9ac5369670b84fa6b to your computer and use it in GitHub Desktop.
Save slucero/e493c7657d3e17f9ac5369670b84fa6b to your computer and use it in GitHub Desktop.
Farmville auto-farm script
; Change this to set the current farm size. This can change as the farm levels up.
Const $farm_size = 20
Dim $farm_x[$farm_size][$farm_size] = [[0]]
Dim $farm_y[$farm_size][$farm_size] = [[0]]
; Calculate farm square dimensions for traversal.
$res_height = @DesktopHeight
$res_width = @DesktopWidth
$square_width = 50
$square_height = 24
; Calculate full farm size dimensions.
$farm_height = ($square_height * $farm_size) + ($square_height/2)
$vertical_adj = ($res_height-$farm_height)/2
; Determine interface adjustments.
$menu_width = 760
$menu_height = 150
$button_height = 28
$width_adj = ($res_width-$menu_width)/2
$button_y = $res_height-$menu_height+($button_height/2)
; Plan for zoom level since this will be needed as the farm gets larger.
$zoom_in_x = 638 + $width_adj
$zoom_out_x = 664 + $width_adj
$zoom_level = 3
getFarmCoords($farm_x, $farm_y, $square_height, $square_width, $vertical_adj)
; Listen for events while running.
While(1)
; Run while holding the '~' key.
If _IsPressed(0xC0) Then
; TODO: Improve zoom adjustments.
;If($zoom_level > 0) Then
;zoomOut($zoom_out_x, $button_y, $zoom_level)
;EndIf
clickPlots($farm_x, $farm_y)
EndIf
WEnd
; Check if a key is pressed.
Func _IsPressed($hexKey)
$UserDll = DllOpen("user32.dll")
Local $aR
$aR = DllCall($UserDll, "int", "GetAsyncKeyState", "int", $hexKey)
If $aR[0] <> 0 Then
Return 1
Else
Return 0
EndIf
EndFunc
; Click the interface button to zoom out.
Func zoomOut($zoom_out_x, $button_y, ByRef $zoom_level)
While($zoom_level > 0)
MouseClick("left", $zoom_out_x, $button_y)
$zoom_level -= 1
Sleep(500)
WEnd
EndFunc
; Click through the farm plots to harvest them.
Func clickPlots(ByRef $farm_x, ByRef $farm_y)
$pause = False
$ignore_center = True
For $i=0 To $farm_size-1
For $j=0 To $farm_size-1
If($ignore_center And $j==$farm_size/2 And $i==($farm_size/2)-1) Then
ContinueLoop
EndIf
; Stop if we hit the escape key.
If(_IsPressed(0x1B)) Then
ExitLoop 2
EndIf
; Pause if we hit the space bar.
If(_IsPressed(0x20)) Then
$pause=True
EndIf
; Listen for space bar again to start again.
While($pause)
If(_IsPressed(0x20)) Then
$pause = False
EndIf
WEnd
; Click the farm plots.
;ConsoleWrite("x=" & $farm_x[$i][$j] & " y=" & $farm_y[$i][$j] & @LF)
MouseClick("Left", $farm_x[$j][$i], $farm_y[$j][$i])
sleep(5)
Next
Next
EndFunc
; Build the matrix of farm plot coordinates.
Func getFarmCoords(ByRef $farm_x, ByRef $farm_y, $square_height, $square_width, $vertical_adj)
; Set farm coordinates.
$center = $res_width/2
$count = 1
$center_col = $farm_size-1
$center_row = 0
; Step through farm size to identify farm plot center points.
For $step=1 To $farm_size Step 0.5
; Work from the top down identifying square coordinates.
$coord_y = $square_height * $step + $vertical_adj
; Find the central axis for each row to calculate from.
If(mod($step, 1) > 0) Then ;central axis is on a corner
For $c=0.5 To $count/2 Step 1
$col = $center_col + Floor($c)
$row = $center_row + Ceiling($c)
$coord_x = $center + ($c * $square_width)
If($coord_x < 0) Then
$coord_x = 0
ElseIf($coord_x > $res_width) Then
$coord_x = $res_width
EndIf
$farm_x[$col][$row] = $coord_x
$farm_y[$col][$row] = $coord_y
$col = $center_col - Ceiling($c)
$row = $center_row - Floor($c)
$coord_x = $center - ($c * $square_width)
If($coord_x < 0) Then
$coord_x = 0
ElseIf($coord_x > $res_width) Then
$coord_x = $res_width
EndIf
$farm_x[$col][$row] = $coord_x
$farm_y[$col][$row] = $coord_y
Next
$center_col -= 1
$center_row += 1
Else ;central axis is in a square
$farm_x[$center_col][$center_row] = $center
$farm_y[$center_col][$center_row] = $coord_y
For $c=1 To Floor($count/2)
$col = $center_col + $c
$row = $center_row + $c
$coord_x = $center + ($c * $square_width)
If($coord_x < 0) Then
$coord_x = 0
ElseIf($coord_x > $res_width) Then
$coord_x = $res_width
EndIf
$farm_x[$col][$row] = $coord_x
$farm_y[$col][$row] = $coord_y
$col = $center_col - $c
$row = $center_row - $c
$coord_x = $center - ($c * $square_width)
If($coord_x < 0) Then
$coord_x = 0
ElseIf($coord_x > $res_width) Then
$coord_x = $res_width
EndIf
$farm_x[$col][$row] = $coord_x
$farm_y[$col][$row] = $coord_y
Next
EndIf
; If we've passed the halfway point in height, the rows will be narrowing again.
If($step > $farm_size/2) Then
$count -= 1
Else
$count += 1
EndIf
Next
EndFunc
@slucero
Copy link
Author

slucero commented Aug 8, 2018

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment