ALMaSS Rabbit ODdox  1.00
The rabbit model description following ODdox protocol
croprotation.cpp
Go to the documentation of this file.
1 /*
2 *******************************************************************************************************
3 Copyright (c) 2011, Christopher John Topping, University of Aarhus
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided
7 that the following conditions are met:
8 
9 Redistributions of source code must retain the above copyright notice, this list of conditions and the
10 following disclaimer.
11 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
12 the following disclaimer in the documentation and/or other materials provided with the distribution.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
15 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
17 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
19 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 ********************************************************************************************************
23 */
24 
25 #define _CRT_SECURE_NO_DEPRECATE
26 
27 #include <cstdio>
28 #include <math.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include "configurator.h"
32 #include "maperrormsg.h"
33 #include "plants.h"
34 #include "croprotation.h"
35 
36 CfgStr l_map_rotation_files_prefix("MAP_ROTATION_FILES_PREFIX",
37  CFG_CUSTOM, "" );
38 
40 
41 
42 
43 CropRotation::CropRotation( int a_num_crops )
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 }
93 
94 
95 
97 {
98  for (unsigned int i=0; i<m_rots.size(); i++) {
99  delete m_rots[ i ];
100  delete m_start[ i ];
101  }
102 }
103 
104 
106  CropRotation::GetNextCrop( int a_farmtype, int a_current_crop )
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 }
123 
124 
125 
127  CropRotation::GetFirstCrop( int a_farmtype, bool *a_low_nutrient )
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 }
145 
CfgStr l_map_rotation_files_prefix("MAP_ROTATION_FILES_PREFIX", CFG_CUSTOM,"")
class CropData * g_crops
Definition: plants.cpp:39
TTypesOfVegetation GetFirstCrop(int a_farmtype, bool *a_low_nutrient)
vector< Starter * > m_start
Definition: croprotation.h:55
TTypesOfVegetation
TTypesOfVegetation GetNextCrop(int a_farmtype, int a_current_crop)
CropRotation(int a_num_crops)
const char * value(void)
Definition: configurator.h:139
int GetNumCrops()
Definition: plants.h:113
class MapErrorMsg * g_msg
Definition: maperrormsg.cpp:38
const int NoFarmTypes
Definition: croprotation.h:37
~CropRotation(void)
String configurator entry class.
Definition: configurator.h:132
bool GetNutStatus(int a_plant_num)
Definition: plants.h:148
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
class CropRotation * g_rotation