Quote:
Its sounds interesting...but I wouldn`t trust it very much. I mean, with fv going out of sync quite frequently and eventual updates that forces us to refresh...
It would ruin everything....unless the script starts by running farmville, waiting for it to load, setting zoom, and so on and so forth.
Here's my script, complete with timer and start-up code. Every cycle it closes out any existing farmville shockwave app, then restarts.
I run at 1920,1200 resolution and my script is hard coded for that, so you will likely have trouble if you try to use it without modification.
Also the startupFarmville function included uses some image files of the "play" button up top and the zoom and full screen buttons that you will have to create for yourself.
Code:
;
; AutoHotkey Version: 1.x
; Language: English
; Platform: Win9x/NT
; Author: A.N.Other <myemail@nowhere.com>
;
; Script Function:
; Template script (you can customize this template by editing "ShellNew\Template.ahk" in your Windows folder)
;
#SingleInstance Force
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
;#Include C:\DSC\AutoHotkey\SearchImages.AHK
isRunning:=0
DeltaX1:=20
DeltaY1:=10
DeltaX2:=20
DeltaY2:=10
;NextHarvest:=200909131117
IniRead, NextHarvest,C:\FVAHK.ini,Timer,NextHarvest
IniRead, LastHarvest,C:\FVAHK.ini,Timer,LastHarvest
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
Gui, Add, Radio, vRadio1 , Blue Berries
Gui, Add, Radio, vRadio2 , Tomatoes (8h)
Gui, Add, Radio, vRadio3 , Carrots
Gui, Add, Radio, vRadio4 , Peas(1 day)
Gui, Add, Radio, vRadio5 , Coffee
Gui, Add, Radio, vRadio6 Checked, Blackberries
Gui, Add, Radio, vRadio7 , Sugar Cane
Gui, Add, Text, vCurTime,Time:
GUI, Add,Edit,w30 vAlarmHour,5
GUI, Add,Edit,w30 vAlarmMinute,5
GUI, Add,Edit,w30 vAlarmSeconds,5
Gui Add,Button, gSetAlarm,Set
Gui Add,Button, gStart,Start
Gui, Add, Text, vLastHarvestTime,Last Harvest:
Gui, Add, Text, vLHTDisplay w120,
Gui, Add, Text, vNextHarvestTime,Next Harvest:
Gui, Add, Text, vNHTDisplay w120,
Gui, Add, Text, vTTNH,Remaining Time to Next:
Gui, Add, Text, vTTNDisplay w100,
Gui, Add, Text, vStatus1 w250,
Gui, Add, Text, vStatus2 w250,
Gui Add,Button, gReloadBut,Reload
UpdateAlarmControls(NextHarvest)
Gui Show, x1600 y100
Gosub, UpdateStatus
UpdateStatusMsg(msg,idx)
{
GuiControl, Text, Status%idx%,%msg%
}
SetTimer, UpdateStatus, 1000
Return
leadingZero(str)
{
if(str="")
{
str=0
}
if(str<10)
{
retVal=0%str%
}
Else
{
retVal:=str
}
return retVal
}
UpdateAlarmControls(val)
{
FormatTime, Hour, %val%, HH
FormatTime, Min, %val%, mm
FormatTime, Sec, %val%, ss
GuiControl, Text, AlarmHour, %Hour%
GuiControl, Text, AlarmMinute, %Min%
GuiControl, Text, AlarmSeconds, %Sec%
}
SetAlarm:
{
FormatTime, New,, yyyyMMdd
GuiControlGet, Hour,,AlarmHour
GuiControlGet, Min,,AlarmMinute
GuiControlGet, Sec,,AlarmSeconds
;GUI, Add,Edit,w30 vAlarmMinute
;GUI, Add,Edit,w30 vAlarmSeconds
; Hour:=leadingZero(Hour)
; Min:=leadingZero(Min)
; Sec:=leadingZero(Sec)
New=%New%%Hour%%Min%%Sec%
Dif:= New
EnvSub, Dif,A_Now, seconds
if(Dif<0)
{
EnvAdd,New,1, days
}
SetNextHarvest(New)
return
}
UpdateDisplay:
{
FormatTime, LHT, %LastHarvest%, M/d/yyyy h:mm:ss tt
NHTDisplay:= LHT
GuiControl,Text,LHTDisplay, %LHT%
FormatTime, NHT, %NextHarvest%, M/d/yyyy h:mm:ss tt
NHTDisplay:= NHT
GuiControl,Text,NHTDisplay, %NHT%
Remaining:=NextHarvest
EnvSub, Remaining, %A_Now%, Seconds
GuiControl,Text,TTNDisplay, %Remaining% Seconds
return
}
UpdateStatus:
{
Gosub, UpdateDisplay
if(Remaining<0)
{
if (Remaining>-60)
{
isRunning:=0
TimerExpired()
}
}
if(remaining<500)
{
if(remaining>330)
{
IfWinExist, ahk_class ShockwaveFlashFullScreen
{
WinClose, ahk_class ShockwaveFlashFullScreen
}
IfWinExist, ahk_class IEFrame
{
WinClose, ahk_class IEFrame
}
}
}
if(remaining<300)
{
;Make sure the windows exist
if (remaining>10)
{
IfWinNotExist, ahk_class ShockwaveFlashFullScreen
{
initialize()
}
}
}
;SetTimer, UpdateStatus, 1000
;SetNextHarvest(10)
;MsgBox, %NHT%
return
}
TimerExpired()
{
SetTimer, UpdateStatus, Off
;Reset Next Harvest
Global NextHarvest
SetLastHarvest(NextHarvest)
; SetNextHarvest(20991231235959)
;Gosub, UpdateDisplay
TimerHarvest(18)
SetTimer, UpdateStatus, 1000
}
TimerHarvest(times)
{
startX:=532
startY:=598
GuiControlGet,r1,,Radio1
GuiControlGet,r2,,Radio2
GuiControlGet,r3,,Radio3
GuiControlGet,r4,,Radio4
GuiControlGet,r5,,Radio5
GuiControlGet,r6,,Radio6
GuiControlGet,r7,,Radio7
if(r1)
{
Cycle=4
}
if(r2)
{
Cycle=8
}
if(r3)
{
Cycle=12
}
if(r4)
{
Cycle=23
}
if(r5)
{
Cycle=16
}
if(r6)
{
Cycle=4
}
if(r7)
{
Cycle=8
}
idx:=0
posX:=startX
posY:=startY
TimerHarvestRepeat:
if( CheckAbort("Begining"+%idx%))
{
return
}
if(idx=8)
{
HarvestAndPlantRow(posX+(25*idx),posY+(12*idx),times,9)
}
Else
{
HarvestAndPlantRow(posX+(25*idx),posY+(12*idx),times,-1)
}
if(idx=0)
{
NewHarvestTime:=A_Now
Delay:=Cycle*60*60 + 70
UpdateStatusMsg(Delay,2)
EnvAdd, NewHarvestTime,Delay, Seconds
SetNextHarvest(NewHarvestTime)
Gosub, UpdateDisplay
}
idx:=idx+1
UpdateStatusMsg(idx,2)
if(idx<times)
{
goto,TimerHarvestRepeat
}
UpdateStatusMsg( Finished Cycle, 1)
}
CheckAbort(msg)
{
IfWinNotActive, ahk_class ShockwaveFlashFullScreen
{
;For now, if we have a problem, then just bail
UpdateStatusMsg( Msg, 1)
Return 1
}
return 0
}
SetNextHarvestDelta(delta)
{
; Sets the time for the next harvest in seconds from now
Global NextHarvest:=A_Now
EnvAdd, NextHarvest, delta, seconds
SetNextHarvest(NextHarvest)
}
SetNextHarvest(value)
{
Global NextHarvest:=value
IniWrite, %NextHarvest%,C:\FVAHK.ini,Timer,NextHarvest
}
SetLastHarvest(value)
{
Global LastHarvest:=value
IniWrite, %value%,C:\FVAHK.ini,Timer,LastHarvest
}
^!o::
ExitApp
return
^!p::
ReloadBut:
{
Reload
Sleep 1000 ; If successful, the reload will close this instance during the Sleep, so the line below will never be reached.
MsgBox, 4,, The script could not be reloaded. Would you like to open it for editing?
IfMsgBox, Yes, Edit
return
}
ClickFullField(size,dir1,dir2,dir3)
{
MouseClick, L
repeatCount:=0
RepeatFull:
RepeatClick(dir1,size-1,-1)
RepeatClick(dir2,1,-1)
RepeatClick(dir3,size-1,-1)
repeatCount := repeatCount + 2
if(repeatCount<size)
{
RepeatClick(dir2,1,-1)
Goto RepeatFull
}
}
ClickRowSkip(X,Y,times,skip)
{
if(skip=-1)
{
ClickRow(X,Y,times)
}
Else
{
ClickRow(X,Y,skip)
nextX:=X+skip*25+25
nextY:=Y-skip*12-12
ClickRow(nextX,nextY,times-skip - 1)
}
}
ClickRow(X,Y,times)
{
if(CheckAbort("Click"))
{
Return
}
curX:=X
curY:=Y
idx:=0
RepeatClickRow:
MouseClick, L, curX, CurY
curX:=curX+25
curY:=curY-12
idx:=idx+1
Sleep, 120
if(idx<times)
{
goto, RepeatClickRow
}
}
RepeatClick(quad,times,skip)
{
deltaXBase=25
deltaYBase=12
idx:=0
if (quad=1)
{
deltaX:=deltaXBase
deltaY:=-deltaYBase
}
if (quad=2)
{
deltaX:=-deltaXBase
deltaY:=-deltaYBase
}
if (quad=3)
{
deltaX:=-deltaXBase
deltaY:=deltaYBase
}
if (quad=4)
{
deltaX:=deltaXBase
deltaY:=deltaYBase
}
MouseGetPos, curX, curY
MouseClick, L, curX, curY
RepeatLoop:
if (idx<times)
{
curX:=curX+deltaX
curY:=curY+deltaY
if(idx<>skip)
{
MouseClick, L, curX,CurY
}
Else
{
MouseMove, curX, CurY
}
idx:=idx+1
Goto, RepeatLoop
}
}
HarvestAndPlantFull(startX,startY,times)
{
idx:=0
posX:=startX
posY:=startY
FullHarvestRepeat:
HarvestAndPlantRow(posX+(25*idx),posY+(12*idx),times,-1)
FirstRowTime:=%A_Now%
idx:=idx+1
if(idx<times)
{
goto,FullHarvestRepeat
}
return, %FirstRowTime%
}
^d::
MouseGetPos, curX, curY
;ClickRow(curX,CurY,18,9)
HarvestAndPlantRow(curX,curY,18,9)
Return
^e::
MouseGetPos, curX, curY
HarvestAndPlantRow(curX,curY,18,-1)
Return
HarvestAndPlantRow(X,Y,times,skip)
{
GuiControlGet,r1,,Radio1
GuiControlGet,r2,,Radio2
GuiControlGet,r3,,Radio3
GuiControlGet,r4,,Radio4
GuiControlGet,r5,,Radio5
GuiControlGet,r6,,Radio6
GuiControlGet,r7,,Radio7
;Select Multi Tool
;Harvest
GuiControl, Text, Status1, Harvesting
MouseClick,L,1210,1110
MouseClick,L,1210,1110
ClickRowSkip( X, Y, times,skip)
;return ; insert return here for testing purposes to skip remaining steps and pauses
Sleep, times * 890
;Plow
GuiControl, Text, Status1, Plowing
MouseClick,L,1260,1110
MouseClick,L,1260,1110
ClickRowSkip( X, Y, times,skip)
if(r1)
{
SeedTime:=BlueBerries()
}
if(r2)
{
SeedTime:=Tomatoes()
}
if(r3)
{
SeedTime:=Carrots()
}
if(r4)
{
SeedTime:=Peas()
}
if(r5)
{
SeedTime:=Coffee()
}
if(r6)
{
SeedTime:=Blackberries()
}
if(r7)
{
SeedTime:=SugarCane()
}
Sleep, times * 890 - SeedTime
GuiControl, Text, Status1, Planting
;;Plant
ClickRowSkip( X, Y, times,skip)
Sleep, times * 890
return
}
SoyBeanRepeat(X,Y,times)
{
idx:=0
SetTimer, UpdateStatus, Off
retries:=0
SoyBeanRepeatLabel:
if(idx<times)
{
idx:=idx+1
if(SoyBeanDelete(X,Y)=0)
{
Status=%idx%/%times% r:%retries%
UpdateStatusMsg(status,2)
Sleep, 100
Goto SoyBeanRepeatLabel
}
Else
{
UpdateStatusMsg("Failed SD",2)
Initialize()
WinClose
retries:=retries+1
Goto, SoyBeanRepeatLabel
}
}
SetTimer, UpdateStatus, 1000
}
BlueBerries()
{
return SelectPlant(1020,820,1)
}
SoyBean()
{
return SelectPlant2(1150,650,0)
}
Tomatoes()
{
;Tomatoes
return SelectPlant(880,650,2)
}
Peas()
{
;Peas
return SelectPlant(752,821,3)
}
Carrots()
{
;Tomatoes
return SelectPlant(1150,650,2)
}
Coffee()
{
;Coffee
return SelectPlant(752,821,2)
}
Blackberries()
{
;Blackberries
return SelectPlant(880,650,3)
}
SugarCane()
{
;Blackberries
return SelectPlant(1020,650,3)
}
SelectPlant(X,Y,Page)
{
;Market
MouseCLick, L,1260,1160
Sleep 2300
idx:=0
RepeatMarketPage2:
if(idx<Page)
{
;Next Arrow
MouseCLick, L,1280,660
Sleep, 1200
idx:=idx+1
Goto, RepeatMarketPage2
}
;
MouseCLick, L,X,Y
return 1200 * Page + 2300
}
SelectPlant2(X,Y,Page)
{
;Market
StartTime:=A_TickCount
MouseCLick, L,1260,1160
; Sleep 2300
WaitForArrow:
ArrowWait:=0
PixelGetColor, Arrow, 1280,660
Msg="Arrow:"%Arrow%
UpdateStatusMsg(Msg,1)
if(Arrow<>"0x3A5470")
{
ArrowWait:=ArrowWait+1
Sleep, 10
if(ArrowWait>300)
{
return, -1
}
Goto, WaitForArrow
}
Sleep, 100
idx:=0
RepeatMarketPage:
if(idx<Page)
{
;Next Arrow
MouseCLick, L,1280,660
Sleep, 1200
idx:=idx+1
Goto, RepeatMarketPage
}
;
ButtonWait:=0
WaitForButton:
PixelGetColor, ButColor, X,Y
if(ButColor<>"0x41BC94")
{
Sleep, 10
ButtonWait:=ButtonWait+1
if(ButtonWait>300)
{
return -1
}
Goto, WaitForButton
}
Sleep, 50
MouseCLick, L,X,Y
Elapsed:=A_tickcount - StartTime
return, Elapsed
}
;41BC94
;40C631
ClickVerified(X,Y)
{
IfWinActive, ahk_class ShockwaveFlashFullScreen
{
MouseClick, L, X,Y
return, 1
}
}
ClickGreenButton(X,Y)
{
Failures:=0
RetryGreen:
Sleep, 10
PixelGetColor, C1, X, Y, Slow
if((C1="0x41BC94") OR (C1="0x40C631"))
{
ButtonFound:
MouseMove, X, Y
Sleep, 50
PixelGetColor, C2, X, Y, Slow
;MsgBox,,,%C2%
if(C2="0x40C631")
{
MouseClick, L, X, Y
Sleep, 50
WaitForDismiss:
if(C2<>"0x40C631")
{
return 0
}
else
{
PixelGetColor, C2, X, Y, Slow
UpdateStatusMsg("Unable to Dismiss Delete",1)
Goto, WaitForDismiss
}
}
}
Failures:=Failures+1
;MsgBox, ,, %C1%
if(Failures<50)
{
; MsgBox,,,Failed + %C1%
UpdateStatusMsg(C1,1)
Sleep, 50
Goto, RetryGreen
}
UpdateStatusMsg(C1,1)
return -1
}
SoyBeanDelete(X,Y)
{
;plow
ClickVerified(1260,1110)
ClickVerified(1260,1110)
ClickVerified(X,Y)
IfWinNotActive, ahk_class ShockwaveFlashFullScreen
{
return 1
}
;soybean
PixelGetColor, LastColor, X-8, Y-8, Slow
Msg="Plow" %LastColor%
UpdateStatusMsg(Msg,1)
Sleep, 100
retval:=Soybean()
if(Soybean<0)
{
; return 1
}
IfWinNotActive, ahk_class ShockwaveFlashFullScreen
{
return 1
}
Sleep, 500
ClickVerified(X-8,Y-8)
DisplayColor:
;delete
ClickVerified(1300, 1100)
idx:=0
;965,588
WaitPlant:
PixelGetColor, PlantColor, 965,588, Slow
Msg="Soybean" %idx% %PlantColor%
UpdateStatusMsg(Msg,1)
idx:=idx+1
Sleep, 1500
; if(PlantColor<>"0x1B478A")
if(0)
{
sleep, 10
if(idx>200)
{
return 1
}
goto, WaitPlant
}
;MsgBox,,,%PlantColor%
;0x265E68
ClickVerified(X-8,Y-8)
;Accept
Sleep, 100
if( ClickGreenButton(890,680)=0)
{
}
Else
{
ClickVerified(X-8,Y-8)
UpdateStatusMsg("Retried Delete",1)
if( ClickGreenButton(890,680)<>0)
{
return 1
}
}
IfWinNotActive, ahk_class ShockwaveFlashFullScreen
{
return 1
}
Sleep, 500
; ClickVerified(890,680)
return, 0
}
Initialize()
{
retryStartup:
Success:=StartUpFarmville()
if(Success<>0)
{
UpdateStatusMsg("Startup Retry",2)
goto, retryStartup
}
UpdateStatusMsg("Startup Successful",1)
global isRunning:=1
}
SearchImageClick(image,MaxRetries)
{
retries:=0
MouseMove, 100,100
UpdateStatusMsg(image,1)
RepeatSearch:
ImageSearch, XLoc,YLoc, 0,0,1800,1100, %image%
if(ErrorLevel=2)
{
MsgBox,,,"Search problem")
}
if(ErrorLevel=1)
{
retries:=retries+1
Sleep, 50
if(retries>MaxRetries)
{
return, 1
}
msg=%image% %retries%
UpdateStatusMsg(msg,1)
Goto, RepeatSearch
}
if(ErrorLevel=0)
{
Global xFound:=Xloc
Global yFound:=YLoc
}
MouseClick, L, XLoc,YLoc
MouseMove, 100,100
return, 0
}
StartUpFarmville()
{
UpdateStatusMsg("Starting Up Farmville",2)
FVURL=http://apps.facebook.com/onthefarm/index.php?ref=tab
IfWinExist, ahk_class ShockwaveFlashFullScreen
{
; Assume this is the farmville App
WinActivate, ahk_class ShockwaveFlashFullScreen
WinClose
;return
}
IfWinExist,, FarmVille on Facebook
{
WinActivate,,FarmVille on Facebook
}
;Activate Browser
IfWinNotExist, ahk_class IEFrame
{
Run, C:\Program Files\Internet Explorer\iexplore.exe http://apps.facebook.com/onthefarm/index.php?ref=tab
Sleep, 15000
WinMaximize, ahk_class IEFrame
}
;Find the play button and click
if( SearchImageClick("C:\dsc\Autohotkey\Farmville\playWhite.png",10))
{
if( SearchImageClick("C:\dsc\AutoHotkey\Farmville\play.png",100))
{
if(SearchImageClick("C:\dsc\Autohotkey\Farmville\playWhite.png",100))
{
return 1
}
}
}
Sleep, 1500
CanRepeat:=0
CanX:=880
CanY:=650
CheckCan:
PixelGetColor, CanColor, CanX,CanY Slow
UpdateStatusMsg(CanColor %CanRepeat%,1)
if(CanColor="0x3A46C3")
{
Sleep, 100
MouseClick, L, CanX,CanY
}
Else
{
Sleep, 50
CanRepeat:=CanRepeat+1
if(CanRepeat<100)
{
goto, CheckCan
}
}
;SearchImageClick("C:\dsc\AutoHotkey\Farmville\Ca.png",100)
SearchImageClick("C:\dsc\AutoHotkey\Farmville\ZoomOut.png",500)
SearchImageClick("C:\dsc\AutoHotkey\Farmville\ZoomOut.png",500)
SearchImageClick("C:\dsc\AutoHotkey\Farmville\ZoomOut.png",500)
SearchImageClick("C:\dsc\AutoHotkey\Farmville\ZoomOut.png",500)
SearchImageClick("C:\dsc\AutoHotkey\Farmville\ZoomOut.png",500)
SearchImageClick("C:\dsc\AutoHotkey\Farmville\Fullscreen.png",500)
SearchImageClick("C:\dsc\AutoHotkey\Farmville\ZoomOut.png",500)
SearchImageClick("C:\dsc\AutoHotkey\Farmville\ZoomOut.png",500)
SearchImageClick("C:\dsc\AutoHotkey\Farmville\ZoomOut.png",500)
SearchImageClick("C:\dsc\AutoHotkey\Farmville\ZoomOut.png",500)
swcnt:=0
waitShockWave:
IfWinExist, ahk_class ShockwaveFlashFullScreen
{
UpdateStatusMsg("Init Successful",2)
return, 0
}
if(swcnt<100)
{
UpdateStatusMsg("WaitSW" %swcnt%,2)
swcnt:=swcnt+1
Sleep, 100
goto, waitShockWave
}
return, 1
}
BlockHay()
{
Bale1X:=0
Bale1Y:=0
Bale2X:=0
Bale2Y:=0
PixelGetColor,B1C,Bale1X,Bale2Y
PixelGetColor,B2C,Bale2X,Bale2Y
;Check if haybales in corner already
;Move Farmer to corner
}
Start:
Initialize()
Return
SoyBean2(X,Y)
{
;Plow
MouseClick,L, 1172,763
MouseClick,L, 1172,763
Sleep, 50
MouseClick, L,861,585
MouseClick, L, 1160,814
Sleep, 1500
MouseClick, L ,1058,622
Sleep, 500
MouseClick, L,876,536
Sleep, 1000
; Delete
MouseClick, L,1220, 770
Sleep, 100
;Square
MouseClick, L,876,536
Sleep, 600
MouseClick, L,780,670
Sleep, 500
}
SoyBean3(X,Y)
{
;Plow
MouseClick,L, 1172,813
MouseClick,L, 1172,813
Sleep, 50
MouseClick, L,861,635
MouseClick, L, 1160,864
Sleep, 1400
MouseClick, L ,1058,672
Sleep, 400
MouseClick, L,876,586
Sleep, 1000
; Delete
MouseClick, L,1220, 820
Sleep, 100
;Square
MouseClick, L,876,586
Sleep, 680
MouseClick, L,780,720
Sleep, 500
}
^!f::
WinActivate,,FarmVille
idx:=0
RepeatSB2:
IfWinNotActive,, FarmVille
{
return
}
SoyBean2(0,0)
idx:=idx+1
UpdateStatusMsg(idx,1)
if(idx<3000)
{
Goto, RepeatSB2
}
Return
#IfWinActive, ahk_class ShockwaveFlashFullScreen
^!q::
XButton1::
TimerExpired()
Return
Return
^+LButton::
MouseGetPos, curX, curY
SoyBeanRepeat(curX,curY,1700)
Return
^!s::
RepeatClick(1,17,-1)
Return
^!a::
RepeatClick(2,17,-1)
Return
^!z::
RepeatClick(3,17,-1)
Return
^!x::
RepeatClick(4,17,-1)
Return
deleteBalloon(X,Y)
{
mouseClick, L, X,Y
sleep, 100
ClickGreenButton(890,680)
sleep, 400
}
deleteRowBalloons(X,Y)
{
idx:=0
RepeatDeleteB:
deleteBalloon(X,Y)
X:=X+25
Y:=Y+12
idx:=idx+1
if(idx<18)
{
goto, RepeatDeleteB
}
}
^!e::
;delete balloons
MouseGetPos, CurX, CurY
deleteRowBalloons(curX,CurY)
return
; FullScreen Button 1180,740