Autohotkey to plant crops or trees

Bones + Beech seeds = Chance of Birch Tree?

I made a Autohotkey script to automate the planting of crops. I found that this was a huge QoL improvement for myself, so hopefully it helps you as well.

You can adjust the rows and columns based on the size of land you're using. Make sure you run the script again after adjusting the values. In my farming area, I have 6 plots, each farming plot is 16 x 7 (columns x rows).

https://preview.redd.it/t0rlfjbbjzn71.png?width=1920&format=png&auto=webp&s=deae82d11c8f387287af2974f9719395fd1e036f

I found it best to align myself by looking down (2 meters in front of me, and 1 meter to the right will get you going forward almost perfectly straight). I built 2 x 1m floor panels and stand at the left-bottom corner and put my crosshair on the opposite corner of the rectangle.

https://preview.redd.it/jh9qiyj1jzn71.jpg?width=1920&format=pjpg&auto=webp&s=5f0b610adb4a55be4d8e7aa633fd0f4f92ca4a32

It's important to start with a completely flat piece of cultivated land, otherwise you'll find yourself going off on an angle.

Don't touch the keyboard or mouse when planting is in progress

Commands are:

F3 to start planting for crop dimensions (you need to equip the crop/seeds yourself)

Ctrl+F3 to start planting for tree dimensions (you need to equip the trees yourself)

F4 toggles pause/resume

F5 stops planting

; **********************************************************
;
; A script to automate valheim planting in a grid
; defined by setting the Rows (walking up/down) 
; and Columns (walking left/right). The distance 
; between each crop is controlled by the WalkDuration.
;
; Checklist before you start planting: 
; 0. The land is cultivated and COMPLETELY flat
; 1. Inventory contains sufficient crops/seeds
; 2. Cultivator is repaired and equipped
; 3. Cultivator has the correct crop selected
; 4. Stamina bar is full
;
; F3 starts the crop planting
; F4 pauses/resumes the current planting
; F5 stops the current planting
;
; **********************************************************

; ----------------------------------------------------------
; Script intialisation
; ----------------------------------------------------------
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance Force ; Only allow a single instance of the script to run
SendMode Event ; Allows for fine control of input delays

; ----------------------------------------------------------
; Change these according to the crop grid dimensions
; ----------------------------------------------------------
global CropColumns := 16 ; crops in a row
global CropRows := 7 ; rows of crops
global CropWalkDuration := 305 ; distance between each crop


; ----------------------------------------------------------
; Change these according to the tree grid dimensions
; ----------------------------------------------------------
global TreeColumns := 10 ; trees in a row
global TreeRows := 10 ; rows of trees
global TreeWalkDuration := 800 ; distance between each tree

; ----------------------------------------------------------
; Tune these according to Valheim game animations
; ----------------------------------------------------------
global PlantDelay := 500 ; allow for planting animation and stamina recovery
global WalkDelay := 600 ; allow for walking animation

; ----------------------------------------------------------
; Keybinds
; ----------------------------------------------------------
global Forward := "w"
global Left := "a"
global Backward := "s"
global Right := "d"

; ----------------------------------------------------------
; Global status
; ----------------------------------------------------------
global StopPlanting := false

; ----------------------------------------------------------
; Script Commands
; ----------------------------------------------------------
F3::
    StopPlanting := false
    LastDirection := StartPlanting(CropColumns, CropRows, CropWalkDuration)
    return

^F3::
    StopPlanting := false
    LastDirection := StartPlanting(TreeColumns, TreeRows, TreeWalkDuration)
    return

F4::
    Pause Toggle
    return

F5::
    StopPlanting := true
    return

; ----------------------------------------------------------
; Function to begin the planting 
; ----------------------------------------------------------
StartPlanting(Columns, Rows, WalkDuration)
{
    Direction := Backward
    Loop
    {       

        Direction := Direction = Forward ? Backward : Forward
        pcResult := PlantColumn(Direction, Columns, WalkDuration)

        ; Allow loop to be stopped mid way
        Sleep 30
        if ( StopPlanting = true )
            break

        if ( A_Index < Rows )
        {
            wfResult := Walk(Right, WalkDuration)
        }

        ; Stop loop after the needed rows
        if ( A_Index >= Rows )
            break

    }

    return Direction
}

; ----------------------------------------------------------
; Function to plant the crops in each row
; ----------------------------------------------------------
PlantColumn(Direction, Columns, WalkDuration)
{

    Loop
    {
        Sleep 30
        ; Allow loop to be stopped mid way
        if ( StopPlanting = true )
            break

        ; Stop loop after the needed columns
        if ( A_Index > Columns )
            break

        ; Walk then plant if walking forwards, otherwise
        ; plant then walk if we're walking backwards
        if ( Direction = Forward ) 
        {
            wfResult := Walk(Direction, WalkDuration)   ;Walk
            pResult := Plant() ;Plant

        }
        else if ( Direction = Backward )
        {
            pResult := Plant() ;Plant
            wfResult := Walk(Direction, WalkDuration)   ;Walk
        }
    }

    return true
}

; ----------------------------------------------------------
; Function to plant a crop
; ----------------------------------------------------------
Plant()
{   
    SetMouseDelay, PlantDelay
    Click

    return true
}

; ----------------------------------------------------------
; Function to walk in the direction given
; ----------------------------------------------------------
Walk(Wasd, WalkDuration)
{   
    SetKeyDelay, WalkDelay, WalkDuration
    Send, {%Wasd%}

    return true
}

Source: https://www.reddit.com/r/valheim/comments/ppsoj0/autohotkey_to_plant_crops_or_trees/

leave a comment

Your email address will not be published. Required fields are marked *