ALMaSS Rabbit ODdox  1.00
The rabbit model description following ODdox protocol
Public Member Functions | Private Attributes | List of all members
CropRotation Class Reference

#include <croprotation.h>

Public Member Functions

TTypesOfVegetation GetNextCrop (int a_farmtype, int a_current_crop)
 
TTypesOfVegetation GetFirstCrop (int a_farmtype, bool *a_low_nutrient)
 
 CropRotation (int a_num_crops)
 
 ~CropRotation (void)
 

Private Attributes

vector< Rotation * > m_rots
 
vector< Starter * > m_start
 

Detailed Description

Definition at line 52 of file croprotation.h.

Constructor & Destructor Documentation

CropRotation::CropRotation ( int  a_num_crops)

Definition at line 43 of file croprotation.cpp.

References g_msg, l_map_rotation_files_prefix, m_rots, m_start, NoFarmTypes, CfgStr::value(), MapErrorMsg::Warn(), and WARN_FILE.

44 {
45  FILE *inpfile;
46  char filename[50];
47 
48  m_rots.resize( a_num_crops );
49  m_start.resize( a_num_crops );
50 
51  for (int i=0; i<a_num_crops; i++) {
52  m_rots[ i ] = new Rotation;
53  m_start[ i ] = new Starter;
54  }
55 
56  for (int i=0; i<NoFarmTypes; i++) {
57  // Now works for any number of rotations.
58  sprintf( filename, "%sFarmType_%d.rot",
60  i );
61 
62  inpfile = fopen(filename, "r" );
63  if (!inpfile) {
64  g_msg->Warn( WARN_FILE, "CropRotation::CropRotation():"
65  " Unable to open file ", filename);
66  exit(1);
67  }
68  for (int j=0; j<a_num_crops; j++) {
69  fscanf( inpfile, "%d", (int*)&(m_rots[ j ]->CropNum[ i ]) );
70  for (int k=0; k<4; k++) {
71  fscanf( inpfile, "%d", (int*)&m_rots[ j ]->NewCrop[ i ][ k ] );
72  fscanf( inpfile, "%d", &m_rots[ j ]->Percent[ i ][ k ] );
73  }
74  }
75  fclose( inpfile );
76 
77  sprintf( filename, "FarmType_%d.stt", i );
78 
79  inpfile = fopen(filename, "r" );
80  if (!inpfile) {
81  g_msg->Warn( WARN_FILE, "CropRotation::CropRotation():"
82  " Unable to open file ", filename);
83  exit(1);
84  }
85  for (int j=0; j<a_num_crops; j++) {
86  fscanf( inpfile, "%d %d",
87  (int*)&m_start[ j ]->CropNum[ i ],
88  &m_start[ j ]->Percent[ i ] );
89  }
90  fclose(inpfile);
91  }
92 }
CfgStr l_map_rotation_files_prefix("MAP_ROTATION_FILES_PREFIX", CFG_CUSTOM,"")
vector< Starter * > m_start
Definition: croprotation.h:55
const char * value(void)
Definition: configurator.h:139
class MapErrorMsg * g_msg
Definition: maperrormsg.cpp:38
const int NoFarmTypes
Definition: croprotation.h:37
vector< Rotation * > m_rots
Definition: croprotation.h:54
void Warn(MapErrorState a_level, std::string a_msg1, std::string a_msg2)
Definition: maperrormsg.cpp:56
CropRotation::~CropRotation ( void  )

Definition at line 96 of file croprotation.cpp.

References m_rots, and m_start.

97 {
98  for (unsigned int i=0; i<m_rots.size(); i++) {
99  delete m_rots[ i ];
100  delete m_start[ i ];
101  }
102 }
vector< Starter * > m_start
Definition: croprotation.h:55
vector< Rotation * > m_rots
Definition: croprotation.h:54

Member Function Documentation

TTypesOfVegetation CropRotation::GetFirstCrop ( int  a_farmtype,
bool *  a_low_nutrient 
)

Definition at line 127 of file croprotation.cpp.

References g_crops, g_msg, CropData::GetNumCrops(), CropData::GetNutStatus(), m_start, MapErrorMsg::Warn(), and WARN_BUG.

128 {
129  int num_crops = g_crops->GetNumCrops();
130 
131  for ( int i=0; i<num_crops; i++ ) {
132  int percent = m_start[ i ]->Percent[ a_farmtype ];
133 
134  if ( (num_crops==i) ||
135  (-1 == m_start[ i+1 ]->Percent[ a_farmtype ]) ||
136  (rand()%100 < percent) ) {
137  *a_low_nutrient = g_crops->GetNutStatus( i );
138  return (TTypesOfVegetation) i;
139  }
140  }
141  g_msg->Warn( WARN_BUG, "CropRotation::GetFirstCrop():"
142  " Unable to recover crop type?!", "");
143  exit(1);
144 }
class CropData * g_crops
Definition: plants.cpp:39
vector< Starter * > m_start
Definition: croprotation.h:55
TTypesOfVegetation
int GetNumCrops()
Definition: plants.h:113
class MapErrorMsg * g_msg
Definition: maperrormsg.cpp:38
bool GetNutStatus(int a_plant_num)
Definition: plants.h:148
void Warn(MapErrorState a_level, std::string a_msg1, std::string a_msg2)
Definition: maperrormsg.cpp:56
TTypesOfVegetation CropRotation::GetNextCrop ( int  a_farmtype,
int  a_current_crop 
)

Definition at line 106 of file croprotation.cpp.

References g_msg, m_rots, MapErrorMsg::Warn(), and WARN_BUG.

107 {
108  //int NumCrops = g_crops->GetNumCrops();
109  for ( int i=0; i<4; i++ ) {
110  int percent = m_rots[ a_current_crop ]->Percent[ a_farmtype ][ i ];
111 
112  // Lazy evaluation for experts. ;-)
113  if ( (3==i) ||
114  (-1 == m_rots[ a_current_crop ]->Percent[ a_farmtype ][ i+1 ]) ||
115  (rand()%100 < percent) ) {
116  return m_rots[ a_current_crop ]->NewCrop[ a_farmtype ][ i ];
117  }
118  }
119  g_msg->Warn( WARN_BUG, "CropRotation::GetNextCrop():"
120  " Unable to recover crop type?!", "");
121  exit(1);
122 }
class MapErrorMsg * g_msg
Definition: maperrormsg.cpp:38
vector< Rotation * > m_rots
Definition: croprotation.h:54
void Warn(MapErrorState a_level, std::string a_msg1, std::string a_msg2)
Definition: maperrormsg.cpp:56

Member Data Documentation

vector< Rotation* > CropRotation::m_rots
private

Definition at line 54 of file croprotation.h.

Referenced by CropRotation(), GetNextCrop(), and ~CropRotation().

vector< Starter* > CropRotation::m_start
private

Definition at line 55 of file croprotation.h.

Referenced by CropRotation(), GetFirstCrop(), and ~CropRotation().


The documentation for this class was generated from the following files: