ALMaSS Hare ODDox  1.1
The hare model description following ODdox protocol
BroadBeans.cpp
Go to the documentation of this file.
1 //
2 // BroadBeans.cpp
3 //
4 /*
5 *******************************************************************************************************
6 Copyright (c) 2015, Christopher John Topping, University of Aarhus
7 All rights reserved.
8 
9 Redistribution and use in source and binary forms, with or without modification, are permitted provided
10 that the following conditions are met:
11 
12 Redistributions of source code must retain the above copyright notice, this list of conditions and the
13 following disclaimer.
14 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
15 the following disclaimer in the documentation and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
20 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
22 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 ********************************************************************************************************
26 */
27 
28 #include "../../Landscape/ls.h"
29 #include "../../Landscape/cropprogs/BroadBeans.h"
30 
31 
32 extern CfgFloat cfg_strigling_prop;
33 
34 bool BroadBeans::Do( Farm *a_farm, LE *a_field, FarmEvent *a_ev ) {
35  m_farm = a_farm;
36  m_field = a_field;
37  m_ev = a_ev;
38 
39  bool done = false;
40  int d1 = 0;
41  int d2 = 0;
42 
43  switch (m_ev->m_todo) {
44  case bb_start:
45  m_field->SetVegPatchy( true ); // A crop with wide rows, so set patchy
46 
47  // Set up the date management stuff
48  // Could save the start day in case it is needed later
49  // m_field->m_startday = m_ev->m_startday;
50  m_last_date = g_date->DayInYear( 28, 2 );
51  // Start and stop dates for all events after harvest
52  m_field->SetMDates( 0, 0, g_date->DayInYear( 28, 2 ) );
53 
54  // Broad beans does not fit into the normal crop management, so must precede a spring sown crop (SpringBarleySpr probably!)
55 
56  //new if: do the check only for non-optimising farms and if year>0. (030713 - m_rotation used only in the hidden year, so I modified the condition from >7 to >0)
57  if (!(m_farm->GetType() == tof_OptimisingFarm && g_date->GetYearNumber() > 0)) {
58 
59  if ((m_ev->m_startday > g_date->DayInYear( 1, 7 )) // this means spring start crop is OK
60  || (m_field->GetMDates( 0, 0 ) >= m_ev->m_startday)) { // This checks that the spring start is not too early
61  char veg_type[ 20 ];
62  sprintf( veg_type, "%d", m_ev->m_next_tov );
63  g_msg->Warn( WARN_FILE, "BroadBeans::Do(): Harvest too late for the next crop to start!!! The next crop is: ", veg_type );
64  exit( 1 );
65  }
66  // Not allowed to fix any late finishing problems!
67  /*
68  for (int i = 0; i < noDates; i++) {
69  if (m_field->GetMDates( 0, i ) >= m_ev->m_startday) {
70  m_field->SetMDates( 0, i, m_ev->m_startday - 1 ); //move the starting date
71  }
72  if (m_field->GetMDates( 1, i ) >= m_ev->m_startday) {
73  m_field->SetMConstants( i, 0 );
74  m_field->SetMDates( 1, i, m_ev->m_startday - 1 ); //move the finishing date
75  }
76  }
77  */
78 
79  if (!m_ev->m_first_year) {
80  // Here we need to allow a start before 1/7 in case we run over the year - the only crop to do this (2802215)
81  d1 = g_date->OldDays() + m_first_date + 365; // Add 365 for spring crop
82  if (g_date->Date() > d1) {
83  // Yes too late - should not happen - raise an error
84  g_msg->Warn( WARN_BUG, "BroadBeans::Do(): Crop start attempt after last possible start date", "" );
85  exit( 1 );
86  }
87  }
88  else {
89  // If this is the first year of running then it is possible to start
90  // on day 0, so need this to tell us what to do:
91  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1, 3 ),
92  bb_spring_plough, false );
93  break;
94  }
95  }//if
96  // End single block date checking code. Please see next line
97  // comment as well.
98  // Reinit d1 to first possible starting date.
99  d1 = g_date->OldDays() + g_date->DayInYear( 1, 3 );
100  if (g_date->Date() > d1) {
101  d1 = g_date->Date();
102  }
103  // OK, let's go.
104  SimpleEvent( d1, bb_spring_plough, false );
105  break;
106 
107  case bb_spring_plough:
108  if (m_ev->m_lock || m_farm->DoIt( 100 )) {
109  if (!m_farm->SpringPlough( m_field, 0.0,
110  g_date->DayInYear( 1, 4 ) -
111  g_date->DayInYear() )) {
112  SimpleEvent( g_date->Date() + 1, bb_spring_plough, true );
113  break;
114  }
115  }
116  d1 = g_date->Date() + 1;
117  d2 = g_date->OldDays() + g_date->DayInYear( 5, 3 );
118  if (d1 < d2) d1 = d2;
119  SimpleEvent( d1, bb_spring_harrow, false );
120  break;
121 
122  case bb_spring_harrow:
123  if (!m_farm->SpringHarrow( m_field, 0.0, g_date->DayInYear( 5, 4 ) - g_date->DayInYear() )) {
124  SimpleEvent( g_date->Date() + 1, bb_spring_harrow, true );
125  break;
126  }
127  d1 = g_date->Date();
128  d2 = g_date->OldDays() + g_date->DayInYear( 25, 3 );
129  if (d1 < d2) d1 = d2;
130  SimpleEvent( d1, bb_spring_sow, false );
131  break;
132 
133  case bb_spring_sow:
134  if (!m_farm->SpringSow( m_field, 0.0, g_date->DayInYear( 15, 4 ) - g_date->DayInYear() )) {
135  SimpleEvent( g_date->Date() + 1, bb_spring_sow, true );
136  break;
137  }
138  SimpleEvent( g_date->Date(), bb_spring_roll, false );
139  break;
140 
141  case bb_spring_roll:
142  if (!m_farm->SpringRoll( m_field, 0.0, g_date->DayInYear( 15, 4 ) - g_date->DayInYear() )) {
143  SimpleEvent( g_date->Date() + 1, bb_spring_roll, true );
144  break;
145  }
146  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 20, 4 ), bb_strigling, false );
147  break;
148 
149  case bb_strigling:
150  if (m_ev->m_lock || (cfg_strigling_prop.value()*m_farm->DoIt( 100 ))) {
151  if (!m_farm->Strigling( m_field, 0.0, g_date->DayInYear( 30, 4 ) - g_date->DayInYear() )) {
152  SimpleEvent( g_date->Date() + 1, bb_strigling, true );
153  break;
154  }
155  }
156  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 15, 5 ), bb_rowcultivation, false );
157  break;
158 
159 
160  case bb_rowcultivation:
161  if (!m_farm->RowCultivation( m_field, 0.0, g_date->DayInYear( 30, 5 ) - g_date->DayInYear() )) {
162  SimpleEvent( g_date->Date() + 1, bb_rowcultivation, true );
163  break;
164  }
165  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 15, 6 ), bb_water_one, false );
166  break;
167 
168  case bb_water_one:
169  if (m_ev->m_lock || (m_ev->m_field->GetSoilTypeR() == 0)) { // Here we ask a soil question, if sandy water is needed
170  if (!m_farm->Water( m_field, 0.0, g_date->DayInYear( 30, 6 ) - g_date->DayInYear() )) {
171  SimpleEvent( g_date->Date() + 1, bb_water_one, true );
172  break;
173  }
174  // Did first water, so do the second one too.
175  SimpleEvent( g_date->Date() + 14, bb_water_two, false );
176  break;
177  }
178  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 20, 8 ), bb_harvest, false );
179  break;
180  case bb_water_two:
181  if (!m_farm->Water( m_field, 0.0, g_date->DayInYear( 21, 7 ) - g_date->DayInYear( ) )) {
182  SimpleEvent( g_date->Date() + 1, bb_water_two, true );
183  break;
184  }
185  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 20, 8 ), bb_harvest, false );
186  break;
187 
188  case bb_harvest:
189  if (m_ev->m_lock || m_farm->DoIt( 50 )) {
190  if (!m_farm->Harvest( m_field, 0.0, g_date->DayInYear( 30, 9 ) - g_date->DayInYear() )) {
191  SimpleEvent( g_date->Date() + 1, bb_harvest, true );
192  break;
193  }
194  }
195  SimpleEvent( g_date->OldDays( ) + 365 + g_date->DayInYear( 28, 2 ), bb_wait, true );
196  break;
197 
198  case bb_wait:
199  // End Main Thread
200  m_field->SetVegPatchy( false ); // reverse the patchy before the next crop
201  done = true;
202  break;
203 
204  default:
205  g_msg->Warn( WARN_BUG, "BroadBeans::Do(): " "Unknown event type! ", "" );
206  exit( 1 );
207  }
208 
209  return done;
210 }
211 
212 
Farm::SpringRoll
virtual bool SpringRoll(LE *a_field, double a_user, int a_days)
Carry out a roll event in the spring on a_field.
Definition: farmfuncs.cpp:525
Farm::SpringPlough
virtual bool SpringPlough(LE *a_field, double a_user, int a_days)
Carry out a ploughing event in the spring on a_field.
Definition: farmfuncs.cpp:444
Farm::Strigling
virtual bool Strigling(LE *a_field, double a_user, int a_days)
Carry out a mechanical weeding on a_field.
Definition: farmfuncs.cpp:1545
bb_water_two
Definition: BroadBeans.h:44
Farm::Harvest
virtual bool Harvest(LE *a_field, double a_user, int a_days)
Carry out a harvest on a_field.
Definition: farmfuncs.cpp:1769
FarmEvent::m_lock
bool m_lock
Definition: farm.h:465
bb_water_one
Definition: BroadBeans.h:43
bb_spring_harrow
Definition: BroadBeans.h:38
FarmEvent
A struct to hold the information required to trigger a farm event.
Definition: farm.h:463
bb_wait
Definition: BroadBeans.h:45
Farm::DoIt
bool DoIt(double a_probability)
Return chance out of 0 to 100.
Definition: farm.cpp:800
FarmEvent::m_first_year
bool m_first_year
Definition: farm.h:467
bb_spring_plough
Definition: BroadBeans.h:37
tof_OptimisingFarm
Definition: farm.h:273
Farm::GetType
TTypesOfFarm GetType(void)
Definition: farm.h:901
bb_start
Definition: BroadBeans.h:35
Crop::m_first_date
int m_first_date
Definition: farm.h:540
FarmEvent::m_startday
int m_startday
Definition: farm.h:466
Crop::SimpleEvent
void SimpleEvent(long a_date, int a_todo, bool a_lock)
Adds an event to this crop management.
Definition: farm.cpp:307
BroadBeans::Do
bool Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
Definition: BroadBeans.cpp:34
bb_spring_roll
Definition: BroadBeans.h:40
bb_spring_sow
Definition: BroadBeans.h:39
bb_harvest
Definition: BroadBeans.h:36
FarmEvent::m_field
LE * m_field
Definition: farm.h:472
FarmEvent::m_next_tov
TTypesOfVegetation m_next_tov
Definition: farm.h:471
Crop::m_farm
Farm * m_farm
Definition: farm.h:537
Crop::m_field
LE * m_field
Definition: farm.h:538
FarmEvent::m_todo
int m_todo
Definition: farm.h:469
Farm::RowCultivation
virtual bool RowCultivation(LE *a_field, double a_user, int a_days)
Carry out a harrowing between crop rows on a_field.
Definition: farmfuncs.cpp:1510
Farm::Water
virtual bool Water(LE *a_field, double a_user, int a_days)
Carry out a watering on a_field.
Definition: farmfuncs.cpp:1717
Crop::m_last_date
int m_last_date
Definition: farm.h:542
Farm::SpringSow
virtual bool SpringSow(LE *a_field, double a_user, int a_days)
Carry out a sowing event in the spring on a_field.
Definition: farmfuncs.cpp:546
Farm
The base class for all farm types.
Definition: farm.h:767
cfg_strigling_prop
CfgFloat cfg_strigling_prop
bb_rowcultivation
Definition: BroadBeans.h:42
bb_strigling
Definition: BroadBeans.h:41
Crop::m_ev
FarmEvent * m_ev
Definition: farm.h:539
Farm::SpringHarrow
virtual bool SpringHarrow(LE *a_field, double a_user, int a_days)
Carry out a harrow event in the spring on a_field.
Definition: farmfuncs.cpp:471