ALMaSS Hare ODDox  1.1
The hare model description following ODdox protocol
OOats.cpp
Go to the documentation of this file.
1 //
2 // OOats.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/OOats.h"
30 
31 extern CfgFloat cfg_strigling_prop;
32 
33 bool OOats::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 ooa_start:
44  {
45  // Set up the date management stuff
46  // Could save the start day in case it is needed later
47  // m_field->m_startday = m_ev->m_startday;
48  m_last_date = g_date->DayInYear(25, 8);
49  // Start and stop dates for all events after harvest
50  int noDates = 2;
51  m_field->SetMDates(0, 0, g_date->DayInYear(11, 8));
52  // Determined by harvest date - used to see if at all possible
53  m_field->SetMDates(1, 0, g_date->DayInYear(20, 8));
54  m_field->SetMDates(0, 1, g_date->DayInYear(25, 8));
55  m_field->SetMDates(1, 1, g_date->DayInYear(25, 8));
56  // Check the next crop for early start, unless it is a spring crop
57  // in which case we ASSUME that no checking is necessary!!!!
58  // So DO NOT implement a crop that runs over the year boundary
59  if (m_ev->m_startday > g_date->DayInYear(1, 7))
60  {
61  if (m_field->GetMDates(0, 0) >= m_ev->m_startday)
62  {
63  g_msg->Warn(WARN_BUG, "OOats::Do(): "
64  "Harvest too late for the next crop to start!!!", "");
65  exit(1);
66  }
67  // Now fix any late finishing problems
68  for (int i = 0; i < noDates; i++)
69  {
70  if (m_field->GetMDates(0, i) >= m_ev->m_startday)
71  m_field->SetMDates(0, i, m_ev->m_startday - 1);
72  if (m_field->GetMDates(1, i) >= m_ev->m_startday)
73  m_field->SetMDates(1, i, m_ev->m_startday - 1);
74  }
75  }
76  // Now no operations can be timed after the start of the next crop.
77 
78  int d1;
79  if (!m_ev->m_first_year)
80  {
81  int today = g_date->Date();
82  // Are we before July 1st?
83  d1 = g_date->OldDays() + g_date->DayInYear(1, 7);
84  if (today < d1)
85  {
86  // Yes, too early. We assumme this is because the last crop was late
87  g_msg->Warn(WARN_BUG, " OOats::Do(): "
88  "Crop start attempt between 1st Jan & 1st July", "");
89  exit(1);
90  }
91  else
92  {
93  d1 = g_date->OldDays() + m_first_date +365; // Add 365 for spring crop
94  if (today > d1)
95  {
96  // Yes too late - should not happen - raise an error
97  g_msg->Warn(WARN_BUG, " OOats::Do(): "
98  "Crop start attempt after last possible start date", "");
99  exit(1);
100  }
101  }
102  }
103  else
104  {
105  SimpleEvent(g_date->OldDays() + g_date->DayInYear(1, 4),
106  ooa_spring_harrow, false);
107  break;
108  }
109  // End single block date checking code. Please see next line
110  // comment as well.
111  // Reinit d1 to first possible starting date.
112  d1 = g_date->OldDays() + m_first_date + 365;
113  if (g_date->Date() > d1)
114  {
115  d1 = g_date->Date();
116  }
117  // OK, let's go.
118  OOA_SOW_DATE = 0;
119  OOA_DID_STRIGLING_ONE = false;
120  if (m_farm->IsStockFarmer())
121  {
122  SimpleEvent(d1, ooa_fertmanure_stock, false);
123  }
124  else
125  {
126  SimpleEvent(d1, ooa_spring_plough, false);
127  }
128  break;
129  }
130 
132  if ( m_ev->m_lock || m_farm->DoIt( 70 ))
133  {
134  if (!m_farm->FA_Manure( m_field, 0.0,
135  g_date->DayInYear( 30,3 ) -
136  g_date->DayInYear())) {
137  SimpleEvent( g_date->Date() + 1, ooa_fertmanure_stock, true );
138  break;
139  }
140  }
141  SimpleEvent( g_date->Date(), ooa_spring_plough, false );
142  break;
143 
144  case ooa_spring_plough:
145  if (!m_farm->SpringPlough( m_field, 0.0,
146  g_date->DayInYear( 30,3 ) -
147  g_date->DayInYear())) {
148  SimpleEvent( g_date->Date() + 1, ooa_spring_plough, true );
149  break;
150  }
151  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1,4 ),
152  ooa_spring_harrow, false );
153  break;
154 
155  case ooa_spring_harrow:
156  if (!m_farm->SpringHarrow( m_field, 0.0,
157  g_date->DayInYear( 20,4 ) -
158  g_date->DayInYear())) {
159  SimpleEvent( g_date->Date() + 1, ooa_spring_harrow, true );
160  break;
161  }
162  if ( m_farm->IsStockFarmer()) {
163  SimpleEvent( g_date->Date(), ooa_fertslurry_stock, false );
164  } else {
165  SimpleEvent( g_date->Date(), ooa_spring_sow_one, false );
166  }
167  break;
168 
170  if (!m_farm->FA_Slurry( m_field, 0.0,
171  g_date->DayInYear( 20,4 ) -
172  g_date->DayInYear())) {
173  SimpleEvent( g_date->Date() + 1, ooa_fertslurry_stock, true );
174  break;
175  }
176  SimpleEvent( g_date->Date(), ooa_spring_sow_one, false );
177  break;
178 
179  case ooa_spring_sow_one:
180  if (!m_farm->SpringSow( m_field, 0.0,
181  g_date->DayInYear( 20,4 ) -
182  g_date->DayInYear())) {
183  SimpleEvent( g_date->Date() + 1, ooa_spring_sow_one, true );
184  break;
185  }
186  OOA_SOW_DATE = g_date->Date();
187  if ( m_farm->IsStockFarmer()) {
188  int d1 = g_date->Date();
189  if ( d1 < g_date->OldDays() + g_date->DayInYear( 5,4 )) {
190  d1 = g_date->OldDays() + g_date->DayInYear( 5,4 );
191  }
192  SimpleEvent( d1, ooa_spring_roll, false );
193  } else {
194  SimpleEvent( g_date->Date() + 1, ooa_spring_sow_two, false );
195  }
196  break;
197 
198  case ooa_spring_sow_two:
199  if ( m_ev->m_lock || m_farm->DoIt( 60 ))
200  {
201  if (!m_farm->SpringSow( m_field, 0.0,
202  g_date->DayInYear( 22,4 ) -
203  g_date->DayInYear())) {
204  SimpleEvent( g_date->Date() + 1, ooa_spring_sow_two, true );
205  break;
206  }
207  OOA_SOW_DATE = g_date->Date();
208  }
209  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 5,4 ),
210  ooa_spring_roll, false );
211  break;
212 
213  case ooa_spring_roll:
214  if ( m_ev->m_lock || m_farm->DoIt( 80 ))
215  {
216  if (!m_farm->SpringRoll( m_field, 0.0,
217  g_date->DayInYear( 20,4 ) -
218  g_date->DayInYear())) {
219  SimpleEvent( g_date->Date() + 1, ooa_spring_roll, true );
220  break;
221  }
222  }
223  {
224  int d1 = g_date->OldDays() + g_date->DayInYear( 15,4 );
225  if ( d1 < OOA_SOW_DATE + 10 ) {
226  d1 = OOA_SOW_DATE + 10;
227  }
228  SimpleEvent( d1, ooa_strigling_one, false );
229  }
230  break;
231 
232  case ooa_strigling_one:
233  if ( m_ev->m_lock || (cfg_strigling_prop.value() * m_farm->DoIt( 80 )))
234  {
235  if (!m_farm->Strigling( m_field, 0.0,
236  g_date->DayInYear( 30,4 ) -
237  g_date->DayInYear())) {
238  SimpleEvent( g_date->Date() + 1, ooa_strigling_one, true );
239  break;
240  }
241  OOA_DID_STRIGLING_ONE = true;
242  }
243  if ( ! OOA_DID_STRIGLING_ONE ) {
244  // No strigling, so jump directly to harvest.
245  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 10,8 ),
246  ooa_harvest, false );
247  break;
248  }
249  {
250  int d1 = g_date->Date() + 10;
251  if ( d1 < g_date->OldDays() + g_date->DayInYear( 25,4 ) ) {
252  d1 = g_date->OldDays() + g_date->DayInYear( 25,4 );
253  }
254  SimpleEvent( d1, ooa_strigling_two, false );
255  }
256  break;
257 
258  case ooa_strigling_two:
259  if (!m_farm->Strigling( m_field, 0.0,
260  g_date->DayInYear( 5,5 ) -
261  g_date->DayInYear())) {
262  SimpleEvent( g_date->Date() + 1, ooa_strigling_two, true );
263  break;
264  }
265  {
266  int d1 = g_date->Date() + 10;
267  if ( d1 < g_date->OldDays() + g_date->DayInYear( 5,5 ) ) {
268  d1 = g_date->OldDays() + g_date->DayInYear( 5,5 );
269  }
270  SimpleEvent( d1, ooa_strigling_three, false );
271  }
272  break;
273 
274  case ooa_strigling_three:
275  if ( m_ev->m_lock || (cfg_strigling_prop.value() * m_farm->DoIt( 60 )))
276  {
277  if (!m_farm->Strigling( m_field, 0.0,
278  g_date->DayInYear( 20,5 ) -
279  g_date->DayInYear())) {
280  SimpleEvent( g_date->Date() + 1, ooa_strigling_three, true );
281  break;
282  }
283  }
284  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 10,8 ),
285  ooa_harvest, false );
286  break;
287 
288  case ooa_harvest:
289  if (!m_farm->Harvest( m_field, 0.0,
290  m_field->GetMDates(1,0) -
291  g_date->DayInYear())) {
292  SimpleEvent( g_date->Date() + 1, ooa_harvest, true );
293  break;
294  }
295  SimpleEvent( g_date->Date(), ooa_straw_chopping, false );
296  break;
297 
298  case ooa_straw_chopping:
299  if ( m_ev->m_lock || m_farm->DoIt( 80 ))
300  {
301  if (!m_farm->StrawChopping( m_field, 0.0,
302  m_field->GetMDates(0,1) -
303  g_date->DayInYear())) {
304  SimpleEvent( g_date->Date() + 1, ooa_straw_chopping, true );
305  break;
306  }
307  done = true;
308  break;
309  }
310  SimpleEvent( g_date->Date(), ooa_hay_bailing, false );
311  break;
312 
313  case ooa_hay_bailing:
314  if (!m_farm->HayBailing( m_field, 0.0,
315  m_field->GetMDates(1,1) -
316  g_date->DayInYear())) {
317  SimpleEvent( g_date->Date() + 1, ooa_hay_bailing, true );
318  break;
319  }
320  done = true;
321  break;
322 
323  default:
324  g_msg->Warn( WARN_BUG, "OOats::Do(): "
325  "Unknown event type! ", "" );
326  exit( 1 );
327  }
328 
329  return done;
330 }
331 
332 
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::FA_Slurry
virtual bool FA_Slurry(LE *a_field, double a_user, int a_days)
Spready slurry on a_field owned by an stock farmer.
Definition: farmfuncs.cpp:965
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
OOA_DID_STRIGLING_ONE
#define OOA_DID_STRIGLING_ONE
Definition: OOats.h:34
Farm::IsStockFarmer
bool IsStockFarmer(void)
Definition: farm.h:905
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
ooa_strigling_three
Definition: OOats.h:48
FarmEvent::m_first_year
bool m_first_year
Definition: farm.h:467
ooa_spring_sow_two
Definition: OOats.h:44
ooa_straw_chopping
Definition: OOats.h:50
ooa_spring_roll
Definition: OOats.h:45
Farm::FA_Manure
virtual bool FA_Manure(LE *a_field, double a_user, int a_days)
Spread manure on a_field owned by an stock farmer.
Definition: farmfuncs.cpp:1036
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
ooa_strigling_two
Definition: OOats.h:47
ooa_harvest
Definition: OOats.h:49
Farm::HayBailing
virtual bool HayBailing(LE *a_field, double a_user, int a_days)
Carry out hay bailing on a_field.
Definition: farmfuncs.cpp:2184
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
ooa_hay_bailing
Definition: OOats.h:51
ooa_spring_plough
Definition: OOats.h:40
Crop::m_farm
Farm * m_farm
Definition: farm.h:537
Crop::m_field
LE * m_field
Definition: farm.h:538
OOA_SOW_DATE
#define OOA_SOW_DATE
Definition: OOats.h:33
ooa_fertmanure_stock
Definition: OOats.h:39
FarmEvent::m_todo
int m_todo
Definition: farm.h:469
Crop::m_last_date
int m_last_date
Definition: farm.h:542
ooa_strigling_one
Definition: OOats.h:46
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
ooa_spring_harrow
Definition: OOats.h:41
ooa_fertslurry_stock
Definition: OOats.h:42
Farm
The base class for all farm types.
Definition: farm.h:767
ooa_spring_sow_one
Definition: OOats.h:43
cfg_strigling_prop
CfgFloat cfg_strigling_prop
OOats::Do
bool Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
Definition: OOats.cpp:33
Crop::m_ev
FarmEvent * m_ev
Definition: farm.h:539
ooa_start
Definition: OOats.h:37
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