ALMaSS Hare ODDox  1.1
The hare model description following ODdox protocol
OWinterRye.cpp
Go to the documentation of this file.
1 //
2 // OWinterRye.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/OWinterRye.h"
30 
31 extern CfgFloat cfg_strigling_prop;
32 
33 bool OWinterRye::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 owry_start:
44  {
45  // Start single block date checking code to be cut-'n-pasted...
46  // Set up the date management stuff
47  m_last_date=g_date->DayInYear(25,8);
48  // Start and stop dates for all events after harvest
49  int noDates= 2;
50  m_field->SetMDates(0,0,g_date->DayInYear(6,8));
51  // Determined by harvest date - used to see if at all possible
52  m_field->SetMDates(1,0,g_date->DayInYear(15,8));
53  m_field->SetMDates(0,1,g_date->DayInYear(20,8));
54  m_field->SetMDates(1,1,g_date->DayInYear(25,8));
55  // Check the next crop for early start, unless it is a spring crop
56  // in which case we ASSUME that no checking is necessary!!!!
57  // So DO NOT implement a crop that runs over the year boundary
58  if (m_ev->m_startday>g_date->DayInYear(1,7))
59  {
60  if (m_field->GetMDates(0,0) >=m_ev->m_startday)
61  {
62  g_msg->Warn( WARN_BUG, "OWinterRye::Do(): "
63  "Harvest too late for the next crop to start!!!", "" );
64  exit( 1 );
65  }
66  // Now fix any late finishing problems
67  for (int i=0; i<noDates; i++)
68  {
69  if (m_field->GetMDates(0,i)>=m_ev->m_startday)
70  m_field->SetMDates(0,i,m_ev->m_startday-1);
71  if (m_field->GetMDates(1,i)>=m_ev->m_startday)
72  m_field->SetMDates(1,i,m_ev->m_startday-1);
73  }
74  }
75  // Now no operations can be timed after the start of the next crop.
76 
77  int d1;
78  if ( ! m_ev->m_first_year )
79  {
80  int today=g_date->Date();
81  // Are we before July 1st?
82  d1 = g_date->OldDays() + g_date->DayInYear( 1,7 );
83  if (today < d1)
84  {
85  // Yes, too early. We assumme this is because the last crop was late
86  g_msg->Warn( WARN_BUG, " OWinterRye::Do(): "
87  "Crop start attempt between 1st Jan & 1st July", "" );
88  exit( 1 );
89  }
90  else
91  {
92  d1 = g_date->OldDays() + m_first_date; // Add 365 for spring crop
93  if (today > d1)
94  {
95  // Yes too late - should not happen - raise an error
96  g_msg->Warn( WARN_BUG, " OWinterRye::Do(): "
97  "Crop start attempt after last possible start date", "" );
98  exit( 1 );
99  }
100  }
101  }
102  else
103  {
104  SimpleEvent( g_date->OldDays() + g_date->DayInYear(1,8),
105  owry_harvest, false );
106  break;
107  }
108  // End single block date checking code. Please see next line
109  // comment as well.
110  // Reinit d1 to first possible starting date.
111  d1 = g_date->OldDays() + m_first_date; // Add 365 for spring crop
112  if ( g_date->Date() > d1 ) {
113  d1 = g_date->Date();
114  }
115  // OK, let's go.
116  if ( m_farm->IsStockFarmer()) {
117  SimpleEvent( d1, owry_fertmanure_stock, false );
118  } else {
119  SimpleEvent( d1, owry_fertmanure_plant, false );
120  }
121  }
122  break;
123 
125  if ( m_ev->m_lock || m_farm->DoIt( 50 )) {
126  if (!m_farm->FA_Manure( m_field, 0.0,
127  g_date->DayInYear( 30, 9 ) -
128  g_date->DayInYear())) {
129  SimpleEvent( g_date->Date() + 1, owry_fertmanure_stock, true );
130  break;
131  }
132  }
133  SimpleEvent( g_date->Date() +1, owry_fertslurry_stock, false );
134  break;
135 
137  if (!m_farm->FA_Slurry( m_field, 0.0,
138  g_date->DayInYear( 30, 9 ) -
139  g_date->DayInYear())) {
140  SimpleEvent( g_date->Date() + 1, owry_fertslurry_stock, true );
141  break;
142  }
143  SimpleEvent( g_date->Date(), owry_autumn_plough, false );
144  break;
145 
147  if ( m_ev->m_lock || m_farm->DoIt( 10 )) {
148  if (!m_farm->FP_Manure( m_field, 0.0,
149  g_date->DayInYear( 30, 9 ) -
150  g_date->DayInYear())) {
151  SimpleEvent( g_date->Date() + 1, owry_fertmanure_plant, true );
152  break;
153  }
154  }
155  SimpleEvent( g_date->Date() + 1, owry_fertslurry_plant, false );
156  break;
157 
159  if (!m_farm->FP_Slurry( m_field, 0.0,
160  g_date->DayInYear( 30, 9 ) -
161  g_date->DayInYear())) {
162  SimpleEvent( g_date->Date() + 1, owry_fertslurry_plant, true );
163  break;
164  }
165  SimpleEvent( g_date->Date(), owry_autumn_plough, false );
166  break;
167 
168  case owry_autumn_plough:
169  if (!m_farm->AutumnPlough( m_field, 0.0,
170  g_date->DayInYear( 5, 10 ) -
171  g_date->DayInYear())) {
172  SimpleEvent( g_date->Date() + 1, owry_autumn_plough, true );
173  break;
174  }
175  SimpleEvent( g_date->Date(), owry_autumn_harrow, false );
176  break;
177 
178  case owry_autumn_harrow:
179  if (!m_farm->AutumnHarrow( m_field, 0.0,
180  g_date->DayInYear( 10, 10 ) -
181  g_date->DayInYear())) {
182  SimpleEvent( g_date->Date() + 1, owry_autumn_harrow, true );
183  break;
184  }
185  {
186  int d1 = g_date->Date();
187  if ( d1 < g_date->OldDays() + g_date->DayInYear( 1,9 )) {
188  d1 = g_date->OldDays() + g_date->DayInYear( 1,9 );
189  }
190  SimpleEvent( d1, owry_autumn_sow, false );
191  }
192  break;
193 
194  case owry_autumn_sow:
195  if (!m_farm->AutumnSow( m_field, 0.0,
196  g_date->DayInYear( 15, 10 ) -
197  g_date->DayInYear())) {
198  SimpleEvent( g_date->Date() + 1, owry_autumn_sow, true );
199  break;
200  }
201  {
202  int d1 = g_date->Date() + 10;
203  if ( d1 < g_date->OldDays() + g_date->DayInYear( 11,9 )) {
204  d1 = g_date->OldDays() + g_date->DayInYear( 11,9 );
205  }
206  SimpleEvent( d1, owry_strigling_one, false );
207  }
208  break;
209 
210  case owry_strigling_one:
211  if ( m_ev->m_lock || (cfg_strigling_prop.value() * m_farm->DoIt( 70 ))) {
212  if (!m_farm->Strigling( m_field, 0.0,
213  g_date->DayInYear( 25, 10 ) -
214  g_date->DayInYear())) {
215  SimpleEvent( g_date->Date() + 1, owry_strigling_one, true );
216  break;
217  }
218  }
219  {
220  int d1 = g_date->Date() + 7;
221  if ( d1 < g_date->OldDays() + g_date->DayInYear( 5,10 )) {
222  d1 = g_date->OldDays() + g_date->DayInYear( 5,10 );
223  }
224  SimpleEvent( d1, owry_strigling_two, false );
225  }
226  break;
227 
228  case owry_strigling_two:
229  if ( m_ev->m_lock || (cfg_strigling_prop.value() * m_farm->DoIt( 15 ))) {
230  if (!m_farm->Strigling( m_field, 0.0,
231  g_date->DayInYear( 2, 11 ) -
232  g_date->DayInYear())) {
233  SimpleEvent( g_date->Date() + 1, owry_strigling_two, true );
234  break;
235  }
236  }
237  if ( m_farm->IsStockFarmer()) {
238  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 25,3 ) + 365,
239  owry_strigling_three, false );
240  } else {
241  // Skip to spring sow.
242  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 25,3 ) + 365,
243  owry_spring_sow, false );
244  }
245  break;
246 
247  case owry_spring_sow:
248  if ( m_ev->m_lock || m_farm->DoIt( 60 )) {
249  if (!m_farm->SpringSow( m_field, 0.0,
250  g_date->DayInYear( 15, 4 ) -
251  g_date->DayInYear())) {
252  SimpleEvent( g_date->Date() + 1, owry_spring_sow, true );
253  break;
254  }
255  {
256  // Did sow, so skip third strigling.
257  int d1 = g_date->Date();
258  if ( d1 < g_date->OldDays() + g_date->DayInYear( 10,4 )) {
259  d1 = g_date->OldDays() + g_date->DayInYear( 10,4 );
260  }
261  SimpleEvent( d1, owry_spring_roll, false );
262  break;
263  }
264  }
265  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 25,3 ),
266  owry_strigling_three, false );
267  break;
268 
270  if ( m_ev->m_lock || (cfg_strigling_prop.value() * m_farm->DoIt( 60 ))) {
271  if (!m_farm->Strigling( m_field, 0.0,
272  g_date->DayInYear( 15, 4 ) -
273  g_date->DayInYear())) {
274  SimpleEvent( g_date->Date() + 1, owry_strigling_three, true );
275  break;
276  }
277  }
278  {
279  int d1 = g_date->Date();
280  if ( d1 < g_date->OldDays() + g_date->DayInYear( 10,4 )) {
281  d1 = g_date->OldDays() + g_date->DayInYear( 10,4 );
282  }
283  SimpleEvent( d1, owry_spring_roll, false );
284  }
285  break;
286 
287  case owry_spring_roll:
288  if ( m_ev->m_lock || m_farm->DoIt( 10 )) {
289  if (!m_farm->SpringRoll( m_field, 0.0,
290  g_date->DayInYear( 30,4 ) -
291  g_date->DayInYear())) {
292  SimpleEvent( g_date->Date()+1, owry_spring_roll, true );
293  break;
294  }
295  }
296  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1,8 ),
297  owry_harvest, false );
298  break;
299 
300  case owry_harvest:
301  if (!m_farm->Harvest( m_field, 0.0,
302  m_field->GetMDates(1,0) -
303  g_date->DayInYear())) {
304  SimpleEvent( g_date->Date() + 1, owry_harvest, true );
305  break;
306  }
307  if ( m_farm->IsStockFarmer()) {
308  SimpleEvent( g_date->Date(), owry_straw_chopping_stock, false );
309  } else {
310  SimpleEvent( g_date->Date(), owry_straw_chopping_plant, false );
311  }
312  break;
313 
315  if (!m_farm->StrawChopping( m_field, 0.0,
316  m_field->GetMDates(1,0) -
317  g_date->DayInYear())) {
318  SimpleEvent( g_date->Date()+1, owry_straw_chopping_plant, true );
319  break;
320  }
321  // End of program for plant farmers.
322  done = true;
323  break;
324 
326  if ( m_ev->m_lock || m_farm->DoIt( 80 )) {
327  if (!m_farm->StrawChopping( m_field, 0.0,
328  m_field->GetMDates(1,0) -
329  g_date->DayInYear())) {
330  SimpleEvent( g_date->Date()+1, owry_straw_chopping_stock, true );
331  break;
332  }
333  // Did chop, so stop here.
334  done = true;
335  break;
336  }
337  // No chopping, so do hay turning and bailing.
338  SimpleEvent( g_date->Date(), owry_hay_turning, false );
339  break;
340 
341  case owry_hay_turning:
342  if (!m_farm->HayTurning( m_field, 0.0,
343  m_field->GetMDates(0,1) -
344  g_date->DayInYear())) {
345  SimpleEvent( g_date->Date() + 1, owry_hay_turning, true );
346  break;
347  }
348  {
349  int d1 = g_date->Date();
350  if ( d1 < g_date->OldDays() + g_date->DayInYear( 5,8 )) {
351  d1 = g_date->OldDays() + g_date->DayInYear( 5,8 );
352  }
353  SimpleEvent( d1, owry_hay_bailing, false );
354  }
355  break;
356 
357  case owry_hay_bailing:
358  if (!m_farm->HayBailing( m_field, 0.0,
359  m_field->GetMDates(1,1) -
360  g_date->DayInYear())) {
361  SimpleEvent( g_date->Date() + 1, owry_hay_bailing, true );
362  break;
363  }
364  // End of program.
365  done = true;
366  break;
367 
368  default:
369  g_msg->Warn( WARN_BUG, "OWinterRye::Do(): "
370  "Unknown event type! ", "" );
371  exit( 1 );
372  }
373 
374  return done;
375 }
376 
377 
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
owry_start
Definition: OWinterRye.h:34
owry_strigling_three
Definition: OWinterRye.h:45
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
FarmEvent::m_first_year
bool m_first_year
Definition: farm.h:467
owry_spring_sow
Definition: OWinterRye.h:44
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
owry_straw_chopping_plant
Definition: OWinterRye.h:48
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
Farm::FP_Manure
virtual bool FP_Manure(LE *a_field, double a_user, int a_days)
Spread manure on a_field owned by an arable farmer.
Definition: farmfuncs.cpp:773
owry_autumn_harrow
Definition: OWinterRye.h:40
Farm::AutumnSow
virtual bool AutumnSow(LE *a_field, double a_user, int a_days)
Carry out a sowing event in the autumn on a_field.
Definition: farmfuncs.cpp:364
owry_fertslurry_plant
Definition: OWinterRye.h:37
owry_harvest
Definition: OWinterRye.h:47
owry_strigling_two
Definition: OWinterRye.h:43
owry_fertmanure_stock
Definition: OWinterRye.h:36
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
owry_fertmanure_plant
Definition: OWinterRye.h:35
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
Crop::m_farm
Farm * m_farm
Definition: farm.h:537
Crop::m_field
LE * m_field
Definition: farm.h:538
owry_autumn_sow
Definition: OWinterRye.h:41
FarmEvent::m_todo
int m_todo
Definition: farm.h:469
Farm::FP_Slurry
virtual bool FP_Slurry(LE *a_field, double a_user, int a_days)
Apply slurry to a_field owned by an arable farmer.
Definition: farmfuncs.cpp:701
Crop::m_last_date
int m_last_date
Definition: farm.h:542
Farm::AutumnPlough
virtual bool AutumnPlough(LE *a_field, double a_user, int a_days)
Carry out a ploughing event in the autumn on a_field.
Definition: farmfuncs.cpp:132
owry_strigling_one
Definition: OWinterRye.h:42
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::AutumnHarrow
virtual bool AutumnHarrow(LE *a_field, double a_user, int a_days)
Carry out a harrow event in the autumn on a_field.
Definition: farmfuncs.cpp:261
Farm
The base class for all farm types.
Definition: farm.h:767
owry_hay_bailing
Definition: OWinterRye.h:51
owry_hay_turning
Definition: OWinterRye.h:50
owry_autumn_plough
Definition: OWinterRye.h:39
cfg_strigling_prop
CfgFloat cfg_strigling_prop
owry_straw_chopping_stock
Definition: OWinterRye.h:49
owry_fertslurry_stock
Definition: OWinterRye.h:38
owry_spring_roll
Definition: OWinterRye.h:46
Farm::HayTurning
virtual bool HayTurning(LE *a_field, double a_user, int a_days)
Carry out hay turning on a_field.
Definition: farmfuncs.cpp:2159
Crop::m_ev
FarmEvent * m_ev
Definition: farm.h:539
OWinterRye::Do
bool Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
Definition: OWinterRye.cpp:33