Optimisation of energetic and reproductive gains explains behavioural responses to environmental variation across seasons and years.