Pol  Revision:cb584c9
vital.cpp
Go to the documentation of this file.
1 
8 #include "vital.h"
9 
10 #include <stddef.h>
11 #include <string>
12 
13 #include "../clib/cfgelem.h"
14 #include "../clib/passert.h"
15 #include "../plib/pkg.h"
16 #include "checkpnt.h"
17 #include "globals/uvars.h"
18 
19 namespace Pol
20 {
21 namespace Core
22 {
24  : pkg( pkg ),
25  name( elem.rest() ),
26  aliases(),
27  vitalid( 0 ),
28  next( nullptr ),
29  get_regenrate_func(
30  FindExportedFunction( elem, pkg, elem.remove_string( "RegenRateFunction" ), 1 ) ),
31  get_maximum_func(
32  FindExportedFunction( elem, pkg, elem.remove_string( "MaximumFunction" ), 1 ) ),
33  underflow_func( nullptr ),
34  regen_while_dead( elem.remove_bool( "RegenWhileDead", false ) )
35 {
36  aliases.push_back( name );
37  std::string tmp;
38  while ( elem.remove_prop( "Alias", &tmp ) )
39  aliases.push_back( tmp );
40 
41  if ( elem.remove_prop( "UnderflowFunction", &tmp ) )
42  {
43  underflow_func = FindExportedFunction( elem, pkg, tmp, 2 );
44  }
45 }
46 
48 {
49  if ( this->underflow_func != nullptr )
50  delete this->underflow_func;
51 
52  if ( this->get_maximum_func != nullptr )
53  delete this->get_maximum_func;
54 
55  if ( this->get_regenrate_func != nullptr )
56  delete this->get_regenrate_func;
57 
58  this->get_maximum_func = nullptr;
59  this->underflow_func = nullptr;
60  this->get_regenrate_func = nullptr;
61 }
62 
63 size_t Vital::estimateSize() const
64 {
65  size_t size = sizeof( Vital ) + name.capacity() + 3 * sizeof( ExportedFunction );
66  for ( const auto& alias : aliases )
67  size += alias.capacity();
68  return size;
69 }
70 
72 {
73  std::vector<Vital*>::iterator iter = gamestate.vitals.begin();
74  for ( ; iter != gamestate.vitals.end(); ++iter )
75  {
76  delete *iter;
77  *iter = nullptr;
78  }
79  gamestate.vitals.clear();
80  gamestate.vitals_byname.clear();
81 }
82 
83 Vital* FindVital( const std::string& str )
84 {
85  VitalsByName::const_iterator citr = gamestate.vitals_byname.find( str );
86  if ( citr != gamestate.vitals_byname.end() )
87  return ( *citr ).second;
88  else
89  return nullptr;
90 }
91 
92 Vital* FindVital( unsigned vitalid )
93 {
94  if ( vitalid < gamestate.vitals.size() )
95  return gamestate.vitals[vitalid];
96  else
97  return nullptr;
98 }
99 
101 {
102  Vital* vital = new Vital( pkg, elem );
103 
104  const Vital* existing = FindVital( vital->name );
105  if ( existing )
106  {
107  elem.throw_error( "Vital " + vital->name + " already defined by " + existing->pkg->desc() );
108  }
109  vital->vitalid = static_cast<unsigned int>( gamestate.vitals.size() );
110  if ( !gamestate.vitals.empty() )
111  gamestate.vitals.back()->next = vital;
112  gamestate.vitals.push_back( vital );
113 
114  for ( unsigned i = 0; i < vital->aliases.size(); ++i )
115  {
116  gamestate.vitals_byname[vital->aliases[i]] = vital;
117  }
118 }
119 
121 {
122  checkpoint( "load_vitals_cfg: load_packed_cfgs" );
123  load_packaged_cfgs( "vitals.cfg", "Vital", load_vital_entry );
124 
125  gamestate.numVitals = static_cast<unsigned int>( gamestate.vitals.size() );
126 
127  checkpoint( "load_vitals_cfg: find Life vital" );
128  gamestate.pVitalLife = FindVital( "Life" );
129  checkpoint( "load_vitals_cfg: find Stamina vital" );
130  gamestate.pVitalStamina = FindVital( "Stamina" );
131  checkpoint( "load_vitals_cfg: find Mana vital" );
132  gamestate.pVitalMana = FindVital( "Mana" );
133 
134  checkpoint( "load_vitals_cfg: verify vital vitals" );
138 }
139 }
140 }
Vital * FindVital(const std::string &str)
Definition: vital.cpp:83
void load_packaged_cfgs(const char *cfgname, const char *taglist, void(*loadentry)(const Package *, Clib::ConfigElem &))
Definition: pkg.cpp:454
std::string desc() const
Definition: pkg.cpp:176
void load_vital_entry(const Plib::Package *pkg, Clib::ConfigElem &elem)
Definition: vital.cpp:100
const Vital * pVitalLife
Definition: uvars.h:199
VitalsByName vitals_byname
Definition: uvars.h:202
std::string name
Definition: vital.h:36
void clean_vitals()
Definition: vital.cpp:71
ExportedFunction * get_maximum_func
Definition: vital.h:42
POL_NORETURN void throw_error(const std::string &errmsg) const
Definition: cfgfile.cpp:285
void checkpoint(const char *msg, unsigned short minlvl)
Definition: checkpnt.cpp:17
ExportedFunction * get_regenrate_func
Definition: vital.h:41
const Plib::Package * pkg
Definition: vital.h:35
std::vector< std::string > aliases
Definition: vital.h:37
ExportedFunction * underflow_func
Definition: vital.h:43
void load_vitals_cfg()
Definition: vital.cpp:120
const Vital * pVitalStamina
Definition: uvars.h:200
GameState gamestate
Definition: uvars.cpp:74
const Vital * pVitalMana
Definition: uvars.h:201
unsigned numVitals
Definition: uvars.h:198
bool remove_prop(const char *propname, std::string *value)
Definition: cfgfile.cpp:128
Vital(const Plib::Package *pkg, Clib::ConfigElem &elem)
Definition: vital.cpp:23
size_t estimateSize() const
Definition: vital.cpp:63
std::string name
Definition: osmod.cpp:943
ExportedFunction * FindExportedFunction(Clib::ConfigElem &elem, const Plib::Package *pkg, const std::string &descriptor, unsigned nargs, bool complain_if_missing)
Definition: syshook.cpp:344
unsigned vitalid
Definition: vital.h:38
#define passert_always(exp)
Definition: passert.h:80
Definition: berror.cpp:12
std::vector< Vital * > vitals
Definition: uvars.h:197