Pol  Revision:cb584c9
movecost.cpp
Go to the documentation of this file.
1 
9 #include "movecost.h"
10 
11 #include <string.h>
12 #include <string>
13 
14 #include "../clib/cfgelem.h"
15 #include "../clib/cfgfile.h"
16 #include "../clib/fileutil.h"
17 #include "../clib/logfacility.h"
18 #include "../clib/stlutil.h"
19 #include "../clib/strutil.h"
20 #include "../plib/systemstate.h"
21 #include "dynproperties.h"
22 #include "globals/settings.h"
23 #include "mobile/charactr.h"
24 
25 namespace Pol
26 {
27 namespace Core
28 {
29 // dave messed with this function 1/26/3 - remove_first_prop was reading carrying percentages in
30 // lexographic order, not
31 // numerical order so it would interpolate first 1 to 100, then to 25, etc. costs were processed in
32 // this kind of
33 // order: 1,10,100,2,3,4,...
34 // So first read into a Map<percentage,cost>, and build from that.
36 {
37  std::map<int, double> Costs;
38  std::map<int, double>::iterator itr;
39 
40  movecost.over = elem.remove_double( "over", 16.0 );
41  for ( int i = 0; i <= MAX_CARRY_PERC; ++i )
42  {
43  movecost.cost[i] = movecost.over;
44  }
45 
46  int last_perc = -1;
47  double last_mult = 0.0;
48  std::string perc_str, mult_str;
49  int perc;
50  double mult;
51  while ( elem.remove_first_prop( &perc_str, &mult_str ) )
52  {
53  ISTRINGSTREAM is1( perc_str );
54  ISTRINGSTREAM is2( mult_str );
55  if ( ( is1 >> perc ) && ( is2 >> mult ) )
56  {
57  if ( perc > MAX_CARRY_PERC )
58  {
59  elem.throw_error( "Max carrying capacity percentage is " + Clib::decint( MAX_CARRY_PERC ) );
60  }
61  Costs.insert( std::pair<int, double>( perc, mult ) );
62  }
63  else
64  {
65  elem.throw_error( "poorly formed movecost spec: " + perc_str + " " + mult_str );
66  }
67  }
68 
69  for ( itr = Costs.begin(); itr != Costs.end(); ++itr )
70  {
71  perc = itr->first;
72  mult = itr->second;
73  for ( int i = last_perc + 1; i <= perc; ++i )
74  {
75  double m = ( ( last_mult - mult ) / ( last_perc - perc ) );
76  double b = mult - m * perc;
77 
78  movecost.cost[i] = i * m + b;
79 
80  // last_end + (mult - last_mult) *
81  }
82  last_perc = perc;
83  last_mult = mult;
84  }
85 }
86 
87 unsigned short movecost( const Mobile::Character* chr, int carry_perc, bool running, bool mounted )
88 {
89  MovementCost* mc;
90  double costmod( 1.0 );
91  if ( mounted )
92  {
95  if ( chr->has_movement_cost() )
96  costmod = running ? chr->movement_cost().run_mounted : chr->movement_cost().walk_mounted;
97  }
98  else
99  {
101  if ( chr->has_movement_cost() )
102  costmod = running ? chr->movement_cost().run : chr->movement_cost().walk;
103  }
104 
105  double mult;
106  if ( carry_perc < 0 )
107  mult = mc->cost[0];
108  else if ( carry_perc > MAX_CARRY_PERC )
109  mult = mc->cost[MAX_CARRY_PERC];
110  else
111  mult = mc->cost[carry_perc];
112 
113  return static_cast<unsigned short>( 8 * mult * costmod );
114 }
115 
116 void load_movecost( bool reload )
117 {
119  return;
120  else if ( !Clib::FileExists( "config/movecost.cfg" ) )
121  {
122  if ( !reload && Plib::systemstate.config.loglevel > 0 )
123  INFO_PRINT << "File config/movecost.cfg not found, skipping.\n";
124  return;
125  }
126 
127  Clib::ConfigFile cf( "config/movecost.cfg",
128  "MovementCost Walking Running Walking_Mounted Running_Mounted" );
129  Clib::ConfigElem elem;
130 
131  bool walking_mounted_set = false;
132  bool running_mounted_set = false;
133 
134  while ( cf.read( elem ) )
135  {
136  if ( elem.type_is( "MovementCost" ) )
137  {
141  }
142  else if ( elem.type_is( "Walking" ) )
143  {
145  }
146  else if ( elem.type_is( "Running" ) )
147  {
149  }
150  else if ( elem.type_is( "Walking_Mounted" ) )
151  {
153  walking_mounted_set = true;
154  }
155  else if ( elem.type_is( "Running_Mounted" ) )
156  {
158  running_mounted_set = true;
159  }
160  }
161 
162  if ( !walking_mounted_set )
165  if ( !running_mounted_set )
168 }
169 }
170 }
MovementCost movecost_walking
Definition: settings.h:35
bool remove_first_prop(std::string *propname, std::string *value)
Definition: cfgfile.cpp:100
MovementCost movecost_walking_mounted
Definition: settings.h:37
SystemState systemstate
Definition: systemstate.cpp:12
std::string decint(unsigned short v)
Definition: strutil.cpp:64
void read_movecost(Clib::ConfigElem &elem, MovementCost &movecost)
Definition: movecost.cpp:35
double cost[MAX_CARRY_PERC+1]
Definition: movecost.h:23
double remove_double(const char *propname, double dflt)
Definition: cfgfile.cpp:448
POL_NORETURN void throw_error(const std::string &errmsg) const
Definition: cfgfile.cpp:285
MovementCost movecost_running
Definition: settings.h:36
MovementCost movecost_running_mounted
Definition: settings.h:38
SettingsManager settingsManager
Definition: settings.cpp:14
unsigned short movecost(const Mobile::Character *chr, int carry_perc, bool running, bool mounted)
Definition: movecost.cpp:87
#define MAX_CARRY_PERC
Definition: movecost.h:19
void load_movecost(bool reload)
Definition: movecost.cpp:116
#define ISTRINGSTREAM
Definition: stlutil.h:73
bool type_is(const char *name) const
Definition: cfgfile.cpp:95
bool FileExists(const char *filename)
Definition: fileutil.cpp:118
bool read(ConfigElem &elem)
Definition: cfgfile.cpp:1015
#define INFO_PRINT
Definition: logfacility.h:223
Definition: berror.cpp:12
bool movement_uses_stamina
Definition: ssopt.h:51