ALMaSS Vole ODDox  1.1
The vole model description following ODdox protocol
OFieldPeas.cpp
Go to the documentation of this file.
1 //
2 // OFieldPeas.cpp
3 //
4 /*
5 *******************************************************************************************************
6 Copyright (c) 2011, 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/OFieldPeas.h"
30 
31 extern CfgFloat cfg_strigling_prop;
32 
33 bool OFieldPeas::Do( Farm *a_farm, LE *a_field, FarmEvent *a_ev )
34 {
35  m_farm = a_farm;
36  m_field = a_field;
37  m_ev = a_ev;
38 
39  bool done = false;
40 
41  switch ( m_ev->m_todo )
42  {
43  case ofp_start:
44  {
45  // Set up the date management stuff
46  m_last_date=g_date->DayInYear(25,8);
47  // Start and stop dates for all events after harvest
48  int noDates= 1;
49  m_field->SetMDates(0,0,g_date->DayInYear(10,8));
50  // Determined by harvest date - used to see if at all possible
51  m_field->SetMDates(1,0,g_date->DayInYear(25,8));
52  // Check the next crop for early start, unless it is a spring crop
53  // in which case we ASSUME that no checking is necessary!!!!
54  // So DO NOT implement a crop that runs over the year boundary
55  if (m_ev->m_startday>g_date->DayInYear(1,7))
56  {
57  if (m_field->GetMDates(0,0) >=m_ev->m_startday)
58  {
59  g_msg->Warn( WARN_BUG, "OFieldPeas::Do(): "
60  "Harvest too late for the next crop to start!!!", "" );
61  exit( 1 );
62  }
63  // Now fix any late finishing problems
64  for (int i=0; i<noDates; i++)
65  {
66  if (m_field->GetMDates(0,i)>=m_ev->m_startday)
67  m_field->SetMDates(0,i,m_ev->m_startday-1);
68  if (m_field->GetMDates(1,i)>=m_ev->m_startday)
69  m_field->SetMDates(1,i,m_ev->m_startday-1);
70  }
71  }
72  // Now no operations can be timed after the start of the next crop.
73 
74  int d1;
75  int today=g_date->Date();
76  d1 = g_date->OldDays() + m_first_date +365; // Add 365 for spring crop
77  if (today > d1)
78  {
79  // Yes too late - should not happen - raise an error
80  g_msg->Warn( WARN_BUG, "OFieldPeas::Do(): "
81  "Crop start attempt after last possible start date", "" );
82  exit( 1 );
83  }
84  // End single block date checking code. Please see next line
85  // comment as well.
86  // Reinit d1 to first possible starting date.
87  d1 = g_date->OldDays()+m_first_date;;
88  if ( ! m_ev->m_first_year ) d1+=365; // Add 365 for spring crop (not 1st yr)
89  if ( g_date->Date() > d1 ) {
90  d1 = g_date->Date();
91  }
92  // OK, let's go.
93  SimpleEvent( d1, ofp_spring_plough, false );
94  }
95  break;
96 
97  case ofp_spring_plough:
98  if (!m_farm->SpringPlough( m_field, 0.0,
99  g_date->DayInYear( 30,3 ) - g_date->DayInYear())) {
100  SimpleEvent( g_date->Date() + 1, ofp_spring_plough, true );
101  break;
102  }
103  SimpleEvent( g_date->Date()+1, ofp_spring_harrow, false );
104  break;
105 
106  case ofp_spring_harrow:
107  if (!m_farm->SpringHarrow( m_field, 0.0,
108  g_date->DayInYear( 5,4 ) - g_date->DayInYear())) {
109  SimpleEvent( g_date->Date() + 1, ofp_spring_harrow, true );
110  break;
111  }
112  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1,4 ),
113  ofp_spring_sow, false );
114  break;
115 
116  case ofp_spring_sow:
117  if (!m_farm->SpringSow( m_field, 0.0,
118  g_date->DayInYear( 15,4 ) - g_date->DayInYear())) {
119  SimpleEvent( g_date->Date() + 1, ofp_spring_sow, true );
120  break;
121  }
122  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1,4 ),
123  ofp_spring_roll, false );
124  // --FN--
125  break;
126 
127  case ofp_spring_roll:
128  if (!m_farm->SpringRoll( m_field, 0.0,
129  g_date->DayInYear( 20,4 ) - g_date->DayInYear())) {
130  SimpleEvent( g_date->Date() + 1, ofp_spring_roll, true );
131  break;
132  }
133  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 20,4 ),
134  ofp_strigling1, false );
135  break;
136 
137  case ofp_strigling1:
138  if (!m_farm->Strigling( m_field, 0.0,
139  g_date->DayInYear( 30,4 ) - g_date->DayInYear())) {
140  SimpleEvent( g_date->Date() + 1, ofp_strigling1, true );
141  break;
142  }
143  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1,5 ),
144  ofp_strigling2, false );
145  break;
146 
147  case ofp_strigling2:
148  if (!m_farm->Strigling( m_field, 0.0,
149  g_date->DayInYear( 10,5 ) - g_date->DayInYear())) {
150  SimpleEvent( g_date->Date() + 1, ofp_strigling2, true );
151  break;
152  }
153  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 11,5 ),
154  ofp_strigling3, false );
155  break;
156 
157  case ofp_strigling3:
158  if (!m_farm->Strigling( m_field, 0.0,
159  g_date->DayInYear( 15,5 ) - g_date->DayInYear())) {
160  SimpleEvent( g_date->Date() + 1, ofp_strigling3, true );
161  break;
162  }
163  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 16,5 ),
164  ofp_strigling4, false );
165  break;
166 
167  case ofp_strigling4:
168  if ( m_ev->m_lock || m_farm->DoIt( 50 ))
169  {
170  if (!m_farm->Strigling( m_field, 0.0,
171  g_date->DayInYear( 26,5 ) - g_date->DayInYear())) {
172  SimpleEvent( g_date->Date() + 1, ofp_strigling4, true );
173  break;
174  }
175  }
176  // --FN--
177  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 10,6 ),
178  ofp_water1, false );
179  break;
180 
181  case ofp_water1:
182  if ( m_ev->m_lock || m_farm->DoIt( 20 ))
183  {
184  if (!m_farm->Water( m_field, 0.0,
185  g_date->DayInYear( 20,6 ) - g_date->DayInYear())) {
186  SimpleEvent( g_date->Date() + 1, ofp_water1, true );
187  break;
188  }
189  SimpleEvent( g_date->Date()+5, ofp_water2, false );
190  }
191  SimpleEvent( g_date->OldDays() + m_field->GetMDates(0,0),
192  ofp_harvest, false );
193  break;
194 
195  case ofp_water2:
196  if (!m_farm->Water( m_field, 0.0,
197  g_date->DayInYear( 30,6 ) - g_date->DayInYear())) {
198  // --FN--
199  SimpleEvent( g_date->Date() + 1, ofp_water2, true );
200  break;
201  }
202  // --FN--
203  //SimpleEvent( g_date->OldDays() + g_date->DayInYear( 10,8 ),
204  // ofp_harvest, false );
205  break;
206 
207  case ofp_harvest:
208  if (!m_farm->Harvest( m_field, 0.0,
209  m_field->GetMDates(1,0) - g_date->DayInYear())) {
210  SimpleEvent( g_date->Date() + 1, ofp_harvest, true );
211  break;
212  }
213  SimpleEvent( g_date->Date(), ofp_straw_chopping, false ); // Must do it
214  break;
215 
216  case ofp_straw_chopping:
217  if ( m_ev->m_lock || m_farm->DoIt( 100 ))
218  {
219  if (!m_farm->StrawChopping( m_field, 0.0,
220  m_field->GetMDates(1,0) - g_date->DayInYear())) {
221  SimpleEvent( g_date->Date()+1, ofp_straw_chopping, true );
222  break;
223  }
224  }
225  // End Main Thread
226  done=true;
227  break;
228 
229  default:
230  g_msg->Warn( WARN_BUG, "OFieldPeas::Do(): "
231  "Unknown event type! ", "" );
232  exit( 1 );
233  }
234 
235  return done;
236 }
237 
238 
ofp_strigling3
Definition: OFieldPeas.h:42
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
ofp_spring_harrow
Definition: OFieldPeas.h:37
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
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
FarmEvent
A struct to hold the information required to trigger a farm event.
Definition: farm.h:463
Farm::DoIt
bool DoIt(double a_probability)
Return chance out of 0 to 100.
Definition: farm.cpp:800
ofp_spring_sow
Definition: OFieldPeas.h:39
FarmEvent::m_first_year
bool m_first_year
Definition: farm.h:467
ofp_water1
Definition: OFieldPeas.h:44
Crop::m_first_date
int m_first_date
Definition: farm.h:540
FarmEvent::m_startday
int m_startday
Definition: farm.h:466
ofp_strigling1
Definition: OFieldPeas.h:40
Crop::SimpleEvent
void SimpleEvent(long a_date, int a_todo, bool a_lock)
Adds an event to this crop management.
Definition: farm.cpp:307
ofp_spring_plough
Definition: OFieldPeas.h:36
ofp_strigling4
Definition: OFieldPeas.h:43
Farm::StrawChopping
virtual bool StrawChopping(LE *a_field, double a_user, int a_days)
Carry out straw chopping on a_field.
Definition: farmfuncs.cpp:2132
ofp_start
Definition: OFieldPeas.h:34
ofp_spring_roll
Definition: OFieldPeas.h:38
Crop::m_farm
Farm * m_farm
Definition: farm.h:537
Crop::m_field
LE * m_field
Definition: farm.h:538
ofp_harvest
Definition: OFieldPeas.h:35
FarmEvent::m_todo
int m_todo
Definition: farm.h:469
cfg_strigling_prop
CfgFloat cfg_strigling_prop
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
ofp_water2
Definition: OFieldPeas.h:45
Farm
The base class for all farm types.
Definition: farm.h:767
ofp_strigling2
Definition: OFieldPeas.h:41
ofp_straw_chopping
Definition: OFieldPeas.h:46
Crop::m_ev
FarmEvent * m_ev
Definition: farm.h:539
OFieldPeas::Do
bool Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
Definition: OFieldPeas.cpp:33
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