ALMaSS Hare ODDox  1.1
The hare model description following ODdox protocol
OWinterWheatUndersown.cpp
Go to the documentation of this file.
1 //
2 // OWinterWheatUndersown.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/OWinterWheatUndersown.h"
30 
31 extern CfgFloat cfg_strigling_prop;
32 
33 bool OWinterWheatUndersown::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 owwu_start:
44  {
45  OWWU_PLOUGH_RUNS = false;
46  OWWU_HARROW_RUNS = false;
47  // Set up the date management stuff
48  m_last_date=g_date->DayInYear(15,9);
49  // Start and stop dates for all events after harvest
50  int noDates= 7;
51  m_field->SetMDates(0,0,g_date->DayInYear(1,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(5,8));
55  m_field->SetMDates(1,1,g_date->DayInYear(5,9));
56  m_field->SetMDates(0,2,g_date->DayInYear(25,8));
57  m_field->SetMDates(1,2,g_date->DayInYear(25,8));
58  m_field->SetMDates(0,3,g_date->DayInYear(25,8));
59  m_field->SetMDates(1,3,g_date->DayInYear(30,8));
60  m_field->SetMDates(0,4,g_date->DayInYear(5,8));
61  m_field->SetMDates(1,4,g_date->DayInYear(5,8)); // not needed
62  m_field->SetMDates(0,5,g_date->DayInYear(15,9));
63  m_field->SetMDates(1,5,g_date->DayInYear(15,9));
64  m_field->SetMDates(0,6,g_date->DayInYear(1,8));
65  m_field->SetMDates(1,6,g_date->DayInYear(30,8));
66  // Check the next crop for early start, unless it is a spring crop
67  // in which case we ASSUME that no checking is necessary!!!!
68  // So DO NOT implement a crop that runs over the year boundary
69  if (m_ev->m_startday>g_date->DayInYear(1,7))
70  {
71  if (m_field->GetMDates(0,0) >=m_ev->m_startday)
72  {
73  g_msg->Warn( WARN_BUG, "OWinterWheatUndersown::Do(): "
74  "Harvest too late for the next crop to start!!!", "" );
75  exit( 1 );
76  }
77  // Now fix any late finishing problems
78  for (int i=0; i<noDates; i++)
79  {
80  if (m_field->GetMDates(0,i)>=m_ev->m_startday)
81  m_field->SetMDates(0,i,m_ev->m_startday-1);
82  if (m_field->GetMDates(1,i)>=m_ev->m_startday)
83  m_field->SetMDates(1,i,m_ev->m_startday-1);
84  }
85  }
86  // Now no operations can be timed after the start of the next crop.
87 
88  int d1;
89  if ( ! m_ev->m_first_year )
90  {
91  int today=g_date->Date();
92  // Are we before July 1st?
93  d1 = g_date->OldDays() + g_date->DayInYear( 1,7 );
94  if (today < d1)
95  {
96  // Yes, too early. We assumme this is because the last crop was late
97  g_msg->Warn( WARN_BUG, "OWinterWheatUndersown::Do(): "
98  "Crop start attempt between 1st Jan & 1st July", "" );
99  exit( 1 );
100  }
101  else
102  {
103  d1 = g_date->OldDays() + m_first_date; // Add 365 for spring crop
104  if (today > d1)
105  {
106  // Yes too late - should not happen - raise an error
107  g_msg->Warn( WARN_BUG, "OWinterWheatUndersown::Do(): "
108  "Crop start attempt after last possible start date", "" );
109  exit( 1 );
110  }
111  }
112  }
113  else
114  {
115  SimpleEvent( g_date->OldDays() + g_date->DayInYear(28,8),
116  owwu_harvest, false );
117  break;
118  }
119  // End single block date checking code. Please see next line
120  // comment as well.
121  // Reinit d1 to first possible starting date.
122  d1 = g_date->OldDays() + m_first_date; // Add 365 for spring crop
123  if ( g_date->Date() > d1 ) {
124  d1 = g_date->Date();
125  }
126  m_field->SetLastSownVeg( m_field->GetVegType() ); //Force last sown, needed for goose habitat classification
127 
128  // OK, let's go.
129  if (m_farm->IsStockFarmer()) // StockFarmer
130  {
131  SimpleEvent( d1, owwu_ferti_s1, false );
132  }
133  else SimpleEvent( d1, owwu_ferti_p1, false );
134  }
135  break;
136 
137  case owwu_ferti_p1:
138  if ( m_ev->m_lock || m_farm->DoIt( 10 ))
139  {
140  if (!m_farm->FP_Manure( m_field, 0.0,
141  g_date->DayInYear( 30,9 ) - g_date->DayInYear())) {
142  SimpleEvent( g_date->Date() + 1, owwu_ferti_p1, true );
143  break;
144  }
145  }
146  SimpleEvent( g_date->Date(),owwu_autumn_plough, false );
147  break;
148 
149  case owwu_ferti_s1:
150  if ( m_ev->m_lock || m_farm->DoIt( 40 ))
151  {
152  if (!m_farm->FA_Manure( m_field, 0.0,
153  g_date->DayInYear( 30,9 ) - g_date->DayInYear())) {
154  SimpleEvent( g_date->Date() + 1, owwu_ferti_s1, true );
155  break;
156  }
157  }
158  SimpleEvent( g_date->Date(),owwu_autumn_plough, false );
159  break;
160 
161  case owwu_autumn_plough:
162  if (!m_farm->AutumnPlough( m_field, 0.0,
163  g_date->DayInYear( 30,9 ) - g_date->DayInYear())) {
164  SimpleEvent( g_date->Date() + 1, owwu_autumn_plough, true );
165  break;
166  }
167  SimpleEvent( g_date->Date()+1,owwu_autumn_harrow, false );
168  break;
169 
170  case owwu_autumn_harrow:
171  if (!m_farm->AutumnHarrow( m_field, 0.0,
172  g_date->DayInYear( 10,10 ) - g_date->DayInYear())) {
173  SimpleEvent( g_date->Date() + 1, owwu_autumn_harrow, true );
174  break;
175  }
176  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 20,9 ),
177  owwu_autumn_sow, false );
178  break;
179 
180  case owwu_autumn_sow:
181  if (!m_farm->AutumnSow( m_field, 0.0,
182  g_date->DayInYear( 10,10 ) - g_date->DayInYear())) {
183  SimpleEvent( g_date->Date() + 1, owwu_autumn_sow, true );
184  break;
185  }
186  {
187  long newdate1 = g_date->OldDays() + g_date->DayInYear( 10,9 );
188  long newdate2 = g_date->Date() + 10;
189  if ( newdate2 > newdate1 )
190  newdate1 = newdate2;
191  SimpleEvent( newdate1, owwu_strigling1, false );
192  }
193  break;
194 
195  case owwu_strigling1:
196  if (!m_farm->Strigling( m_field, 0.0,
197  g_date->DayInYear( 10,10 ) - g_date->DayInYear())) {
198  SimpleEvent( g_date->Date() + 1, owwu_strigling1, true );
199  break;
200  }
201  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 24,9 ),
202  owwu_strigling2, false );
203  break;
204 
205  case owwu_strigling2:
206  // --FN--
207  if ( m_ev->m_lock || (cfg_strigling_prop.value() * m_farm->DoIt( 70 )))
208  // **CJT** Soil Type 2-4 later implementation
209  {
210  if (!m_farm->Strigling( m_field, 0.0,
211  g_date->DayInYear( 24,10 ) - g_date->DayInYear())) {
212  SimpleEvent( g_date->Date() + 1, owwu_strigling2, true );
213  break;
214  }
215  }
216  // Next year
217  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 25,3 )+365,
218  owwu_strigling_sow, false );
219  break;
220 
221  case owwu_strigling_sow:
222  if (!m_farm->StriglingSow( m_field, 0.0,
223  g_date->DayInYear( 15,4 ) - g_date->DayInYear())) {
224  SimpleEvent( g_date->Date() + 1, owwu_strigling_sow, true );
225  break;
226  }
227  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1,4 ),
228  owwu_spring_roll1, false );
229  break;
230 
231  case owwu_spring_roll1:
232  if ( m_ev->m_lock || m_farm->DoIt( 5 ))
233  {
234  if (!m_farm->SpringRoll( m_field, 0.0,
235  g_date->DayInYear( 30,4 ) - g_date->DayInYear())) {
236  SimpleEvent( g_date->Date() + 1, owwu_spring_roll1, true );
237  break;
238  }
239  }
240  if (m_farm->IsStockFarmer()) // StockFarmer
241  {
242  SimpleEvent( g_date->Date() + 1, owwu_ferti_s2, false );
243  }
244  else
245  SimpleEvent( g_date->Date() + 1, owwu_ferti_p2, false );
246  break;
247 
248  case owwu_ferti_p2:
249  if (!m_farm->FP_Slurry( m_field, 0.0,
250  g_date->DayInYear( 30,4 ) - g_date->DayInYear())) {
251  SimpleEvent( g_date->Date() + 1, owwu_ferti_p2, true );
252  break;
253  }
254  SimpleEvent( g_date->Date()+1,owwu_spring_roll2, false );
255  break;
256 
257  case owwu_ferti_s2:
258  if (!m_farm->FA_Slurry( m_field, 0.0,
259  g_date->DayInYear( 30,4 ) - g_date->DayInYear())) {
260  SimpleEvent( g_date->Date() + 1, owwu_ferti_s2, true );
261  break;
262  }
263  SimpleEvent( g_date->Date()+1,owwu_spring_roll2, false );
264  break;
265 
266  case owwu_spring_roll2:
267  if ( m_ev->m_lock || m_farm->DoIt( 5 ))
268  {
269  if (!m_farm->SpringRoll( m_field, 0.0,
270  g_date->DayInYear( 30,4 ) - g_date->DayInYear())) {
271  SimpleEvent( g_date->Date() + 1, owwu_spring_roll2, true );
272  break;
273  }
274  }
275  if (m_farm->IsStockFarmer()) // StockFarmer
276  {
277  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1,5 ),
278  owwu_ferti_s3, false );
279  }
280  else
281  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1,8 ),
282  owwu_harvest, false );
283  break;
284 
285  case owwu_ferti_s3:
286  if ( m_ev->m_lock || m_farm->DoIt( 50 ))
287  {
288  if (!m_farm->FA_Slurry( m_field, 0.0,
289  g_date->DayInYear( 30,5 ) - g_date->DayInYear())) {
290  SimpleEvent( g_date->Date() + 1, owwu_ferti_s3, true );
291  break;
292  }
293  }
294  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1,8 ),
295  owwu_harvest, false );
296  break;
297 
298  case owwu_harvest:
299  if (!m_farm->Harvest( m_field, 0.0,
300  m_field->GetMDates(1,0) - g_date->DayInYear())) {
301  SimpleEvent( g_date->Date() + 1, owwu_harvest, true );
302  break;
303  }
304  SimpleEvent(g_date->Date(), owwu_straw_chopping, false);
305  break;
306 
307  case owwu_straw_chopping:
308  {
309  if ( m_ev->m_lock || m_farm->DoIt( 60 ))
310  {
311  if (!m_farm->StrawChopping( m_field, 0.0,
312  m_field->GetMDates(1,0) - g_date->DayInYear()))
313  {
314  SimpleEvent( g_date->Date() + 1, owwu_straw_chopping, true );
315  break;
316  }
317  else
318  {
319  // Did Chopping so test for stubble harrow/deep_plough
320  SimpleEvent( g_date->OldDays() + g_date->DayInYear( 1,8 ),
321  owwu_deep_plough, false );
322  SimpleEvent( g_date->OldDays() + m_field->GetMDates(0,1),
323  owwu_stubble_harrow1, false );
324  break;
325  }
326  }
327  int d1=g_date->Date() + 3;
328  if (d1>m_field->GetMDates(0,2)) d1=m_field->GetMDates(0,2);
329  SimpleEvent( d1, owwu_hay_turning, false );
330  }
331  break;
332 
333  case owwu_hay_turning:
334  if ( m_ev->m_lock || m_farm->DoIt( 20 ))
335  {
336  if (!m_farm->HayTurning( m_field, 0.0,
337  m_field->GetMDates(1,2) - g_date->DayInYear())) {
338  SimpleEvent( g_date->Date() + 1, owwu_hay_turning, true );
339  break;
340  }
341  }
342  {
343  long d1 = g_date->OldDays() + g_date->DayInYear( 7,8 );
344  long d2 = g_date->Date() + 3;
345  if ( d2 > d1 ) d1 = d2;
346  if (d1>m_field->GetMDates(0,3)) d1=m_field->GetMDates(0,3);
347  SimpleEvent( d1, owwu_hay_baling, false );
348  }
349  break;
350 
351  case owwu_hay_baling:
352  if (!m_farm->HayBailing( m_field, 0.0,
353  m_field->GetMDates(1,3) - g_date->DayInYear())) {
354  SimpleEvent( g_date->Date() + 1, owwu_hay_baling, true );
355  break;
356  }
357  // These events will almost certainly predate
358  // 'today' due to the ending date used above.
359  SimpleEvent( g_date->OldDays() + m_field->GetMDates(0,6),
360  owwu_deep_plough, false );
361  SimpleEvent( g_date->OldDays() + m_field->GetMDates(0,4),
362  owwu_stubble_harrow1, false );
363  break;
364 
366  OWWU_HARROW_RUNS = true;
367  if ( m_ev->m_lock || m_farm->DoIt( 20 ))
368  {
369  if (!m_farm->StubbleHarrowing( m_field, 0.0,
370  m_field->GetMDates(1,1) - g_date->DayInYear())) {
371  SimpleEvent( g_date->Date() + 1, owwu_stubble_harrow1, true );
372  break;
373  }
374  int d1=g_date->Date()+10;
375  if (d1>m_field->GetMDates(0,5)) d1=m_field->GetMDates(0,5);
376  SimpleEvent( d1,owwu_stubble_harrow2, false );
377  break;
378  }
379  OWWU_HARROW_RUNS = false;
380  SimpleEvent( g_date->Date(), owwu_catch_all, false );
381  break;
382 
384  if (!m_farm->StubbleHarrowing( m_field, 0.0,
385  m_field->GetMDates(1,5) - g_date->DayInYear()))
386  {
387  SimpleEvent( g_date->Date() + 1, owwu_stubble_harrow2, true );
388  break;
389  }
390  OWWU_HARROW_RUNS = false;
391  SimpleEvent( g_date->Date(), owwu_catch_all, false );
392  break;
393 
394  case owwu_deep_plough:
395  OWWU_PLOUGH_RUNS = true;
396  if ( m_ev->m_lock || m_farm->DoIt( 5 ))
397  {
398  if (!m_farm->DeepPlough( m_field, 0.0,
399  m_field->GetMDates(1,6) - g_date->DayInYear()))
400  {
401  SimpleEvent( g_date->Date() + 1, owwu_deep_plough, true );
402  break;
403  }
404  // if we deep plough then finish plan here
405  done=true;
406  // END OF MAIN THREAD
407  break; //
408  }
409  OWWU_PLOUGH_RUNS = false;
410  SimpleEvent( g_date->Date(), owwu_catch_all, false );
411  break;
412 
413  case owwu_catch_all:
415  done = true;
416  break;
417 
418  default:
419  g_msg->Warn( WARN_BUG, "OWinterWheatUndersown::Do(): "
420  "Unknown event type! ", "" );
421  exit( 1 );
422  }
423  return done;
424 }
425 
owwu_spring_roll1
Definition: OWinterWheatUndersown.h:50
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
owwu_ferti_s2
Definition: OWinterWheatUndersown.h:39
owwu_autumn_sow
Definition: OWinterWheatUndersown.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
owwu_stubble_harrow1
Definition: OWinterWheatUndersown.h:56
owwu_straw_chopping
Definition: OWinterWheatUndersown.h:54
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
owwu_start
Definition: OWinterWheatUndersown.h:36
OWinterWheatUndersown::Do
virtual bool Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
Definition: OWinterWheatUndersown.cpp:33
owwu_deep_plough
Definition: OWinterWheatUndersown.h:58
Crop::m_first_date
int m_first_date
Definition: farm.h:540
FarmEvent::m_startday
int m_startday
Definition: farm.h:466
owwu_ferti_p1
Definition: OWinterWheatUndersown.h:41
Crop::SimpleEvent
void SimpleEvent(long a_date, int a_todo, bool a_lock)
Adds an event to this crop management.
Definition: farm.cpp:307
owwu_catch_all
Definition: OWinterWheatUndersown.h:59
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
cfg_strigling_prop
CfgFloat cfg_strigling_prop
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
owwu_strigling1
Definition: OWinterWheatUndersown.h:46
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
owwu_hay_baling
Definition: OWinterWheatUndersown.h:55
owwu_autumn_plough
Definition: OWinterWheatUndersown.h:43
OWWU_HARROW_RUNS
#define OWWU_HARROW_RUNS
Definition: OWinterWheatUndersown.h:33
owwu_strigling2
Definition: OWinterWheatUndersown.h:47
OWWU_PLOUGH_RUNS
#define OWWU_PLOUGH_RUNS
Definition: OWinterWheatUndersown.h:32
Crop::m_farm
Farm * m_farm
Definition: farm.h:537
Crop::m_field
LE * m_field
Definition: farm.h:538
owwu_spring_roll2
Definition: OWinterWheatUndersown.h:51
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
owwu_hay_turning
Definition: OWinterWheatUndersown.h:53
Crop::m_last_date
int m_last_date
Definition: farm.h:542
owwu_ferti_p2
Definition: OWinterWheatUndersown.h:42
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
owwu_ferti_s1
Definition: OWinterWheatUndersown.h:38
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
owwu_ferti_s3
Definition: OWinterWheatUndersown.h:40
Farm::StriglingSow
virtual bool StriglingSow(LE *a_field, double a_user, int a_days)
Carry out a mechanical weeding followed by sowing on a_field.
Definition: farmfuncs.cpp:1597
Farm
The base class for all farm types.
Definition: farm.h:767
Farm::DeepPlough
virtual bool DeepPlough(LE *a_field, double a_user, int a_days)
Carry out a deep ploughing event on a_field.
Definition: farmfuncs.cpp:417
owwu_autumn_harrow
Definition: OWinterWheatUndersown.h:44
owwu_stubble_harrow2
Definition: OWinterWheatUndersown.h:57
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
owwu_strigling_sow
Definition: OWinterWheatUndersown.h:48
owwu_harvest
Definition: OWinterWheatUndersown.h:52
Farm::StubbleHarrowing
virtual bool StubbleHarrowing(LE *a_field, double a_user, int a_days)
Carry out stubble harrowing on a_field.
Definition: farmfuncs.cpp:2209