ALMaSS Rabbit ODdox  1.1
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 "ls.h"
34 
35 CfgStr l_map_rotation_files_prefix("MAP_ROTATION_FILES_PREFIX",
36  CFG_CUSTOM, "" );
37 
39 
40 
41 
42 CropRotation::CropRotation( int a_num_crops )
43 {
44  FILE *inpfile;
45  char filename[50];
46 
47  m_rots.resize( a_num_crops );
48  m_start.resize( a_num_crops );
49 
50  for (int i=0; i<a_num_crops; i++) {
51  m_rots[ i ] = new Rotation;
52  m_start[ i ] = new Starter;
53  }
54 
55  for (int i=0; i<NoFarmTypes; i++) {
56  // Now works for any number of rotations.
57  sprintf( filename, "%sFarmType_%d.rot",
59  i );
60 
61  inpfile = fopen(filename, "r" );
62  if (!inpfile) {
63  g_msg->Warn( WARN_FILE, "CropRotation::CropRotation():"
64  " Unable to open file ", filename);
65  exit(1);
66  }
67  for (int j=0; j<a_num_crops; j++) {
68  fscanf( inpfile, "%d", (int*)&(m_rots[ j ]->CropNum[ i ]) );
69  for (int k=0; k<4; k++) {
70  fscanf( inpfile, "%d", (int*)&m_rots[ j ]->NewCrop[ i ][ k ] );
71  fscanf( inpfile, "%d", &m_rots[ j ]->Percent[ i ][ k ] );
72  }
73  }
74  fclose( inpfile );
75 
76  sprintf( filename, "FarmType_%d.stt", i );
77 
78  inpfile = fopen(filename, "r" );
79  if (!inpfile) {
80  g_msg->Warn( WARN_FILE, "CropRotation::CropRotation():"
81  " Unable to open file ", filename);
82  exit(1);
83  }
84  for (int j=0; j<a_num_crops; j++) {
85  fscanf( inpfile, "%d %d",
86  (int*)&m_start[ j ]->CropNum[ i ],
87  &m_start[ j ]->Percent[ i ] );
88  }
89  fclose(inpfile);
90  }
91 }
92 
93 
94 
96 {
97  for (unsigned int i=0; i<m_rots.size(); i++) {
98  delete m_rots[ i ];
99  delete m_start[ i ];
100  }
101 }
102 
103 
105  CropRotation::GetNextCrop( int a_farmtype, int a_current_crop )
106 {
107  //int NumCrops = g_crops->GetNumCrops();
108  for ( int i=0; i<4; i++ ) {
109  int percent = m_rots[ a_current_crop ]->Percent[ a_farmtype ][ i ];
110 
111  // Lazy evaluation for experts. ;-)
112  if ( (3==i) ||
113  (-1 == m_rots[ a_current_crop ]->Percent[ a_farmtype ][ i+1 ]) ||
114  (rand()%100 < percent) ) {
115  return m_rots[ a_current_crop ]->NewCrop[ a_farmtype ][ i ];
116  }
117  }
118  g_msg->Warn( WARN_BUG, "CropRotation::GetNextCrop():"
119  " Unable to recover crop type?!", "");
120  exit(1);
121 }
122 
123 
124 
126  CropRotation::GetFirstCrop( int a_farmtype, bool *a_low_nutrient )
127 {
128  int num_crops = g_crops->GetNumCrops();
129 
130  for ( int i=0; i<num_crops; i++ ) {
131  int percent = m_start[ i ]->Percent[ a_farmtype ];
132 
133  if ( (num_crops==i) ||
134  (-1 == m_start[ i+1 ]->Percent[ a_farmtype ]) ||
135  (rand()%100 < percent) ) {
136  *a_low_nutrient = g_crops->GetNutStatus( i );
137  return (TTypesOfVegetation) i;
138  }
139  }
140  g_msg->Warn( WARN_BUG, "CropRotation::GetFirstCrop():"
141  " Unable to recover crop type?!", "");
142  exit(1);
143 }
144 
CropRotation::m_start
vector< Starter * > m_start
Definition: croprotation.h:55
CfgStr::value
const char * value(void)
Definition: configurator.h:152
l_map_rotation_files_prefix
CfgStr l_map_rotation_files_prefix("MAP_ROTATION_FILES_PREFIX", CFG_CUSTOM, "")
Rotation
Definition: croprotation.h:39
CropRotation::CropRotation
CropRotation(int a_num_crops)
Definition: croprotation.cpp:42
Starter
Definition: croprotation.h:46
NoFarmTypes
const int NoFarmTypes
Definition: croprotation.h:37
CropRotation::GetFirstCrop
TTypesOfVegetation GetFirstCrop(int a_farmtype, bool *a_low_nutrient)
Definition: croprotation.cpp:126
CfgStr
String configurator entry class.
Definition: configurator.h:144
PlantGrowthData::GetNumCrops
int GetNumCrops()
Definition: plants.h:135
ls.h
WARN_BUG
Definition: maperrormsg.h:34
g_msg
class MapErrorMsg * g_msg
Definition: maperrormsg.cpp:41
CropRotation
Definition: croprotation.h:52
TTypesOfVegetation
TTypesOfVegetation
Definition: tov_declaration.h:30
PlantGrowthData::GetNutStatus
bool GetNutStatus(int a_plant_num)
Definition: plants.h:170
maperrormsg.h
MapErrorMsg::Warn
void Warn(MapErrorState a_level, std::string a_msg1, std::string a_msg2)
Definition: maperrormsg.cpp:59
CropRotation::GetNextCrop
TTypesOfVegetation GetNextCrop(int a_farmtype, int a_current_crop)
Definition: croprotation.cpp:105
g_crops
class PlantGrowthData * g_crops
Definition: plants.cpp:41
CFG_CUSTOM
Definition: configurator.h:60
WARN_FILE
Definition: maperrormsg.h:37
configurator.h
CropRotation::~CropRotation
~CropRotation(void)
Definition: croprotation.cpp:95
g_rotation
class CropRotation * g_rotation
Definition: croprotation.cpp:38
CropRotation::m_rots
vector< Rotation * > m_rots
Definition: croprotation.h:54