Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
farmplot.jl 4.29 KiB
### Persefone.jl - a model of agricultural landscapes and ecosystems in Europe.
###
### This file contains code for the fields that farmers manage.
###

"""
    FarmPlot

A struct representing a single field, on which a crop can be grown.
"""
mutable struct FarmPlot{T} <: ModelAgent
    const id::Int64
    pixels::Vector{Tuple{Int64, Int64}}
    farmer::Int64
    cropstate::T
end

croptype(f::FarmPlot{T}) where {T} = croptype(f.cropstate)
cropname(f::FarmPlot{T}) where {T} = cropname(croptype(f))
cropheight(f::FarmPlot{T}) where {T} = cropheight(f.cropstate)
cropcover(f::FarmPlot{T}) where {T} = cropcover(f.cropstate)
cropyield(f::FarmPlot{T}) where {T} = cropyield(f.cropstate)
isharvestable(f::FarmPlot{T}) where {T} = isharvestable(f.cropstate)

"""
    stepagent!(farmplot, model)

Update a farm plot by one day.
"""
function stepagent!(farmplot::FarmPlot{T}, model::SimulationModel) where T
    stepagent!(farmplot.cropstate, model)
end

"""
    sow!(farmplot, model, cropname)

Sow the specified crop on the farmplot.
"""
function sow!(farmplot::FarmPlot, model::SimulationModel, cropname::String)
    createevent!(model, farmplot.pixels, sowing)
    sow!(farmplot.cropstate, model, cropname)
    @debug "Farmer $(farmplot.farmer) sowed $(cropname) on farmplot $(farmplot.id)."
end

"""
    harvest!(farmplot, model)

Harvest the crop of this farmplot.
"""
function harvest!(farmplot::FarmPlot{T}, model::SimulationModel) where T
    createevent!(model, farmplot.pixels, harvesting)
    harvest!(farmplot.cropstate, model)  # TODO: multiply with area to return units of `g`
    @debug "Farmer $(farmplot.farmer) harvested $(cropname(farmplot)) from farmplot $(farmplot.id)."
end

"""
    @sow(cropname)

Sow the named crop on the current field. Requires the variables `field` and `model`.
"""
macro sow(cropname)
    :(sow!($(esc(:field)), $(esc(:model)), $(esc(cropname))))
end

"""
    @harvest()

Harvest the current field. Requires the variables `field` and `model`.
"""
macro harvest()