Pol  Revision:4b29d2b
attribute.cpp
Go to the documentation of this file.
1 
7 #include "attribute.h"
8 
9 #include <stddef.h>
10 
11 #include "../../clib/cfgelem.h"
12 #include "../../clib/passert.h"
13 #include "../../plib/pkg.h"
14 #include "../globals/settings.h"
15 #include "../globals/uvars.h"
16 #include "../syshook.h"
17 
18 namespace Pol
19 {
20 namespace Mobile
21 {
22 Attribute* Attribute::FindAttribute( const std::string& str )
23 {
24  Core::AttributesByName::const_iterator citr = Core::gamestate.attributes_byname.find( str );
25  if ( citr != Core::gamestate.attributes_byname.end() )
26  return ( *citr ).second;
27  else
28  return nullptr;
29 }
30 
32 {
33  if ( attrid < Core::gamestate.attributes.size() )
34  return Core::gamestate.attributes[attrid];
35  else
36  return nullptr;
37 }
38 
40  : pkg( pkg ),
41  name( elem.rest() ),
42  attrid( 0 ),
43  aliases(),
44  next( nullptr ),
45  getintrinsicmod_func( nullptr ),
46  delay_seconds( elem.remove_ushort( "DELAY", 0 ) ),
47  unhides( elem.remove_bool( "UNHIDES", true ) ),
48  disable_core_checks( elem.remove_bool( "DisableCoreChecks", false ) ),
50  elem.remove_ushort( "DefaultCap", Core::settingsManager.ssopt.default_attribute_cap ) ),
51  script_( elem.remove_string( "SCRIPT", "" ), pkg, "scripts/skills/" )
52 {
53  aliases.push_back( name );
54  std::string tmp;
55  while ( elem.remove_prop( "Alias", &tmp ) )
56  aliases.push_back( tmp );
57 
58  if ( elem.remove_prop( "GetIntrinsicModFunction", &tmp ) )
59  {
60  getintrinsicmod_func = Core::FindExportedFunction( elem, pkg, tmp, 1 );
61  }
62 }
64 {
65  if ( getintrinsicmod_func != nullptr )
66  delete getintrinsicmod_func;
67 }
68 
70 {
71  size_t size = sizeof( Attribute );
72  size += name.capacity();
73  for ( const auto& alias : aliases )
74  size += alias.capacity();
75  size += sizeof( Core::ExportedFunction );
76  size += script_.estimatedSize();
77  return size;
78 }
79 
81 {
82  auto attr = new Attribute( pkg, elem );
83 
84  const Attribute* existing = Attribute::FindAttribute( attr->name );
85  if ( existing )
86  {
87  elem.throw_error( "Attribute " + attr->name + " already defined by " + existing->pkg->desc() );
88  }
89  attr->attrid = static_cast<unsigned int>( Core::gamestate.attributes.size() );
90  if ( !Core::gamestate.attributes.empty() )
91  Core::gamestate.attributes.back()->next = attr;
92  Core::gamestate.attributes.push_back( attr );
93  for ( unsigned i = 0; i < attr->aliases.size(); ++i )
94  {
95  Core::gamestate.attributes_byname[attr->aliases[i]] = attr;
96  }
97 }
98 
100 {
101  load_packaged_cfgs( "attributes.cfg", "Attribute", load_attribute_entry );
102  Core::gamestate.numAttributes = static_cast<unsigned int>( Core::gamestate.attributes.size() );
103 
107 
110 
111  passert_always( Core::gamestate.pAttrStrength );
112  passert_always( Core::gamestate.pAttrIntelligence );
113  passert_always( Core::gamestate.pAttrDexterity );
114  passert_always( Core::gamestate.pAttrParry );
115  passert_always( Core::gamestate.pAttrTactics );
116 }
117 
119 {
120  auto iter = Core::gamestate.attributes.begin();
121  for ( ; iter != Core::gamestate.attributes.end(); ++iter )
122  {
123  delete *iter;
124  *iter = nullptr;
125  }
126  Core::gamestate.attributes.clear();
128 }
129 }
130 }
unsigned delay_seconds
Definition: attribute.h:50
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
const Mobile::Attribute * pAttrParry
Definition: uvars.h:182
size_t estimatedSize() const
Definition: scrdef.cpp:140
AttributesByName attributes_byname
Definition: uvars.h:178
const Mobile::Attribute * pAttrTactics
Definition: uvars.h:183
POL_NORETURN void throw_error(const std::string &errmsg) const
Definition: cfgfile.cpp:285
const Mobile::Attribute * pAttrStrength
Definition: uvars.h:179
Core::ExportedFunction * getintrinsicmod_func
Definition: attribute.h:47
void clean_attributes()
Definition: attribute.cpp:118
std::vector< Mobile::Attribute * > attributes
Definition: uvars.h:176
void load_attributes_cfg()
Definition: attribute.cpp:99
const Plib::Package * pkg
Definition: attribute.h:39
GameState gamestate
Definition: uvars.cpp:74
Attribute * next
Definition: attribute.h:45
SettingsManager settingsManager
Definition: settings.cpp:14
std::vector< std::string > aliases
Definition: attribute.h:43
bool remove_prop(const char *propname, std::string *value)
Definition: cfgfile.cpp:128
const Mobile::Attribute * pAttrIntelligence
Definition: uvars.h:180
size_t estimateSize() const
Definition: attribute.cpp:69
ExportedFunction * FindExportedFunction(Clib::ConfigElem &elem, const Plib::Package *pkg, const std::string &descriptor, unsigned nargs, bool complain_if_missing)
Definition: syshook.cpp:336
unsigned numAttributes
Definition: uvars.h:177
void load_attribute_entry(const Plib::Package *pkg, Clib::ConfigElem &elem)
Definition: attribute.cpp:80
const Mobile::Attribute * pAttrDexterity
Definition: uvars.h:181
Core::ScriptDef script_
Definition: attribute.h:57
#define passert_always(exp)
Definition: passert.h:80
Attribute(const Plib::Package *pkg, Clib::ConfigElem &elem)
Definition: attribute.cpp:39
Definition: berror.cpp:12
std::string name
Definition: attribute.h:40
static Attribute * FindAttribute(const std::string &str)
Definition: attribute.cpp:22
unsigned short default_cap
Definition: attribute.h:55