ALMaSS Hare ODDox  1.1
The hare model description following ODdox protocol
OGrazingPigs.cpp
Go to the documentation of this file.
1 //
2 // OGrazingPigs.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/OGrazingPigs.h"
30 
31 bool OGrazingPigs::Do( Farm *a_farm, LE *a_field, FarmEvent *a_ev )
32 {
33  m_farm = a_farm;
34  m_field = a_field;
35  m_ev = a_ev;
36 
37  bool done = false;
38 
39  switch ( m_ev->m_todo )
40  {
41  case ogp_start:
42  {
43  // Set up the date management stuff
44  m_last_date=g_date->DayInYear(2,11);
45  m_field->SetMDates(0,0,g_date->DayInYear(2,11));
46  m_field->SetMDates(1,0,g_date->DayInYear(2,11));
47 
48  // This is a nasty one because it runs one year from when it starts, less one
49  // month
50  // So if the next crop is an autumn one - then it will push the year on one
51  // We cannot allow this - the rotation will be out of sync. So only spring crops
52  // can follow this
53  if ((m_ev->m_startday>g_date->DayInYear(1, 7)) && m_ev->m_startday<g_date->DayInYear(1, 11)) // This allows pigs to follow pigs
54  {
55  g_msg->Warn( WARN_BUG, "GrazingPigs::Do(): "
56  "Autumn Crop Following Grazing Pigs is not allowed!!!", "" );
57  exit( 1 );
58  }
59  if ( ! m_ev->m_first_year )
60  {
61  int today=g_date->Date();
62  // Are we before July 1st?
63  int d1 = g_date->OldDays() + g_date->DayInYear( 1,7 );
64  if (today < d1)
65  {
66  // Yes, too early. We assumme this is because the last crop was late
67  g_msg->Warn( WARN_BUG, "GrazingPigs::Do(): "
68  "Crop start attempt between 1st Jan & 1st July", "" );
69  exit( 1 );
70  }
71  else
72  {
73  d1 = g_date->OldDays() + m_first_date; // Add 365 for spring crop
74  if (today > d1)
75  {
76  // Yes too late - should not happen - raise an error
77  g_msg->Warn( WARN_BUG, "GrazingPigs::Do(): "
78  "Crop start attempt after last possible start date", "" );
79  exit( 1 );
80  }
81  }
82  }
83  // OK so if we reach here it is after 1/7 and before 1,11
84 
85  m_field->SetLastSownVeg( m_field->GetVegType() ); //Force last sown, needed for goose habitat classification
86 
87  int d1=g_date->OldDays() + m_first_date;
88  PGP_FIRST_PIGS_IN_DATE=d1+334; // 31 days less than one year
89  PGP_LAST_PIGS_IN_DATE=d1+355; // just less than one year later (So not near the max before 1,11)
90  SimpleEvent( d1, ogp_pigs_out, false );
91  break;
92  }
93  case ogp_pigs_out:
94  {
95  if (!m_farm->PigsOut( m_field, 0.0,
96  PGP_LAST_PIGS_IN_DATE - g_date->Date())) {
97  SimpleEvent( g_date->Date() + 1, ogp_pigs_out, true );
98  break;
99  }
100  SimpleEvent( g_date->Date() + 1, ogp_pigs_are_out_forced, false );
101  break;
102 
104  // Keep the pigs out there
105  m_farm->PigsAreOutForced( m_field, 0.0, 0 );
106  // PGP_FIRST_PIGS_IN_DATE specifies the first day the pigs may come home.
107  // However this event type forces them to stay out. Thus we want to
108  // abandon this chain of events one day before PGP_FIRST_PIGS_IN_DATE.
109  if ( g_date->Date() < PGP_FIRST_PIGS_IN_DATE - 1 )
110  {
111  SimpleEvent( g_date->Date() + 1, ogp_pigs_are_out_forced, false );
112  break;
113  }
114  SimpleEvent( g_date->Date() + 1, ogp_pigs_are_out, false );
115  }
116  break;
117 
118  case ogp_pigs_are_out:
119  // Start testing for taking the pigs home.
120  // PigsAreOut() returns false if it is not time to stop grazing
121  if (!m_farm->PigsAreOut( m_field, 0.0,
122  PGP_LAST_PIGS_IN_DATE - g_date->Date())) {
123  // --FN--
124  SimpleEvent( g_date->Date() + 1, ogp_pigs_are_out, true );
125  break;
126  }
127  done=true;
128  break;
129 
130  default:
131  g_msg->Warn( WARN_BUG, "OGrazingPigs::Do(): "
132  "Unknown event type! ", "" );
133  exit( 1 );
134  }
135 
136  return done;
137 }
138 
139 
140 
Farm::PigsAreOut
virtual bool PigsAreOut(LE *a_field, double a_user, int a_days)
Start a pig grazing event on a_field today or soon.
Definition: farmfuncs.cpp:2112
Farm::PigsAreOutForced
virtual bool PigsAreOutForced(LE *a_field, double a_user, int a_days)
Start a pig grazing event on a_field today - no exceptions.
Definition: farmfuncs.cpp:2090
FarmEvent
A struct to hold the information required to trigger a farm event.
Definition: farm.h:463
ogp_pigs_are_out_forced
Definition: OGrazingPigs.h:39
ogp_start
Definition: OGrazingPigs.h:36
FarmEvent::m_first_year
bool m_first_year
Definition: farm.h:467
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
ogp_pigs_are_out
Definition: OGrazingPigs.h:38
ogp_pigs_out
Definition: OGrazingPigs.h:37
OGrazingPigs::Do
bool Do(Farm *a_farm, LE *a_field, FarmEvent *a_ev)
Definition: OGrazingPigs.cpp:31
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
Crop::m_last_date
int m_last_date
Definition: farm.h:542
Farm
The base class for all farm types.
Definition: farm.h:767
PGP_FIRST_PIGS_IN_DATE
#define PGP_FIRST_PIGS_IN_DATE
Definition: OGrazingPigs.h:32
PGP_LAST_PIGS_IN_DATE
#define PGP_LAST_PIGS_IN_DATE
Definition: OGrazingPigs.h:33
Farm::PigsOut
virtual bool PigsOut(LE *a_field, double a_user, int a_days)
Generate a 'pigs_out' event for every day the cattle are on a_field.
Definition: farmfuncs.cpp:2062
Crop::m_ev
FarmEvent * m_ev
Definition: farm.h:539