Pol  Revision:f37d500
equipmnt.cpp
Go to the documentation of this file.
1 
8 #include "equipmnt.h"
9 
10 #include <stddef.h>
11 
12 #include "../../clib/cfgelem.h"
13 #include "../../clib/cfgfile.h"
14 #include "../../clib/clib_endian.h"
15 #include "../../clib/fileutil.h"
16 #include "../../clib/passert.h"
17 #include "../../clib/random.h"
18 #include "../../clib/streamsaver.h"
19 #include "../../plib/pkg.h"
20 #include "../../plib/systemstate.h"
21 #include "../equipdsc.h"
22 #include "../globals/object_storage.h"
23 #include "../globals/state.h"
24 #include "../globals/uvars.h"
25 #include "../layers.h"
26 #include "../mobile/charactr.h"
27 #include "../tooltips.h"
28 #include "../ufunc.h"
29 #include "armor.h"
30 #include "item.h"
31 #include "itemdesc.h"
32 #include "weapon.h"
33 
34 namespace Pol
35 {
36 namespace Items
37 {
38 Equipment::Equipment( const ItemDesc& itemdesc, Core::UOBJ_CLASS uobj_class,
39  const Core::EquipDesc* permanent_descriptor )
40  : Item( itemdesc, uobj_class ), tmpl( permanent_descriptor ), _quality( itemdesc.quality )
41 {
42 }
43 
45 {
46  // Every intrinsic equipment has its own local itemdesc element that should be deleted here.
47  // Only exception is the wrestling weapon, which should be deferred
48  // to the global desctable cleaning.
49 
50  if ( is_intrinsic() )
51  {
52  Core::EquipDesc* ed = const_cast<Core::EquipDesc*>( tmpl );
53  if ( !ed->is_pc_intrinsic )
54  {
55  ed->unload_scripts();
56 
57  delete tmpl;
58  tmpl = NULL;
59  }
60  }
61 }
62 
64 {
65  Equipment* eq = static_cast<Equipment*>( base::clone() );
66 
67  return eq;
68 }
69 
71 {
73 }
74 
76 {
77  base::readProperties( elem );
78 }
79 
81 {
82  // Intrinsic equipment does ot get damaged
83  if ( tmpl->is_intrinsic )
84  return;
85 
86  if ( hp_ >= 1 && Clib::random_int( 99 ) == 0 )
87  {
88  set_dirty();
89  --hp_;
90  increv();
92  {
94  if ( chr != NULL )
95  chr->refresh_ar();
96  }
98  }
99 }
100 
105 {
106  return tmpl->is_intrinsic;
107 }
108 
110 {
111  size_t size = base::estimatedSize() + sizeof( double ) /*_quality*/
112  + sizeof( const Core::EquipDesc* ); /*tmpl*/
113  if ( is_intrinsic() && !tmpl->is_pc_intrinsic )
114  size += tmpl->estimatedSize();
115  return size;
116 }
117 
118 double Equipment::getQuality() const
119 {
120  return _quality;
121 }
122 void Equipment::setQuality( double value )
123 {
124  _quality = value;
125 }
126 
129 {
130  auto itr = Core::gamestate.intrinsic_equipments.find( Core::NameAndLayer( name, layer ) );
131  if ( itr == Core::gamestate.intrinsic_equipments.end() )
132  return NULL;
133  return itr->second;
134 }
135 
137 void register_intrinsic_equipment( const std::string& name, Equipment* equip )
138 {
139  equip->inuse( true );
140 
141  // during system startup, defer serial allocation in order to avoid clashes with
142  // saved items.
143  if ( !Core::stateManager.gflag_in_system_startup )
144  {
146  equip->serial_ext = ctBEu32( equip->serial );
148  }
149 
150  insert_intrinsic_equipment( name, equip );
151 }
152 
154 void insert_intrinsic_equipment( const std::string& name, Equipment* equip )
155 {
156  passert_always_r( equip->layer,
157  "Trying to use register equipment without a layer as intrinsic. Please report "
158  "this bug on the forums." );
160  equip->is_intrinsic(),
161  "Trying to register non-intrinsic equipment. Please report this bug on the forums." );
163  Core::IntrinsicEquipments::value_type( Core::NameAndLayer( name, equip->layer ), equip ) );
164 }
165 
168 {
169  for ( auto it = Core::gamestate.intrinsic_equipments.begin();
170  it != Core::gamestate.intrinsic_equipments.end(); ++it )
171  {
172  Equipment* eqp = it->second;
174  eqp->serial_ext = ctBEu32( eqp->serial );
176  }
177 }
178 
182 {
183  if ( Clib::FileExists( "config/npcdesc.cfg" ) )
184  {
185  Clib::ConfigFile cf( "config/npcdesc.cfg" );
186  Clib::ConfigElem elem;
187  while ( cf.read( elem ) )
188  {
191  }
192  }
193  for ( const auto& pkg : Plib::systemstate.packages )
194  {
195  std::string filename = Plib::GetPackageCfgPath( pkg, "npcdesc.cfg" );
196 
197  if ( Clib::FileExists( filename.c_str() ) )
198  {
199  Clib::ConfigFile cf( filename.c_str() );
200  Clib::ConfigElem elem;
201  while ( cf.read( elem ) )
202  {
203  std::string newrest = ":" + pkg->name() + ":" + std::string( elem.rest() );
204  elem.set_rest( newrest.c_str() );
205 
208  }
209  }
210  }
211 }
212 }
213 }
unsigned char u8
Definition: rawtypes.h:25
virtual Item * clone() const POL_OVERRIDE
Definition: equipmnt.cpp:63
IntrinsicEquipments intrinsic_equipments
Definition: uvars.h:213
void register_intrinsic_equipment(const std::string &name, Equipment *equip)
Must be called when a new intrinsic equipment is created.
Definition: equipmnt.cpp:137
SystemState systemstate
Definition: systemstate.cpp:12
bool is_intrinsic() const
Tells eather an equipment is intrinsic or not Intrinsic equipment is, by example, NPCs "natural" weap...
Definition: equipmnt.cpp:104
virtual std::string name() const POL_OVERRIDE
Definition: item.cpp:110
virtual void printProperties(Clib::StreamWriter &sw) const POL_OVERRIDE
Definition: item.cpp:337
const Core::EquipDesc * tmpl
Definition: equipmnt.h:84
void load_npc_intrinsic_equip()
Recreates intrinsic equipment for defined NPCs must be called at startup.
Definition: equipmnt.cpp:181
void insert_intrinsic_equipment(const std::string &name, Equipment *equip)
Adds a new intrisinc equipment to the map of known ones.
Definition: equipmnt.cpp:154
virtual void printProperties(Clib::StreamWriter &sw) const POL_OVERRIDE
Definition: equipmnt.cpp:70
virtual size_t estimatedSize() const POL_OVERRIDE
Definition: equipmnt.cpp:109
virtual Item * clone() const
Definition: item.cpp:56
void allocate_intrinsic_equipment_serials()
Deferred allocator for serials during startup, see comments in register_intrinsic_equipment() ...
Definition: equipmnt.cpp:167
void set_dirty()
Definition: uobject.h:291
virtual void readProperties(Clib::ConfigElem &elem) POL_OVERRIDE
Definition: item.cpp:434
u32 GetNewItemSerialNumber(void)
Definition: ufunc.cpp:142
int random_int(int i)
Definition: random.cpp:34
UArmor * create_intrinsic_shield_from_npctemplate(Clib::ConfigElem &elem, const Plib::Package *pkg)
Creates a new intrinic shield for an NPC template and returns it.
Definition: armor.cpp:237
virtual double getQuality() const POL_OVERRIDE
Definition: equipmnt.cpp:118
GameState gamestate
Definition: uvars.cpp:74
#define passert_always_r(exp, reason)
Definition: passert.h:84
#define ctBEu32(x)
Definition: clib_endian.h:45
virtual void readProperties(Clib::ConfigElem &elem) POL_OVERRIDE
Definition: equipmnt.cpp:75
StateManager stateManager
Definition: state.cpp:8
virtual size_t estimatedSize() const POL_OVERRIDE
Definition: item00.cpp:58
bool Insert(UObject *obj)
Definition: objecthash.cpp:30
std::pair< std::string, u8 > NameAndLayer
Definition: uvars.h:113
ObjectStorageManager objStorageManager
unsigned short hp_
Definition: item.h:271
bool isa(UOBJ_CLASS uobj_class) const
Definition: baseobject.h:99
std::string GetPackageCfgPath(const Package *pkg, const std::string &filename)
Definition: pkg.cpp:491
UWeapon * create_intrinsic_weapon_from_npctemplate(Clib::ConfigElem &elem, const Plib::Package *pkg)
Creates a new intrinic weapon for an NPC template and returns it.
Definition: weapon.cpp:278
virtual void refresh_ar()
Definition: charactr.cpp:2484
bool inuse() const
Definition: item.h:314
bool FileExists(const char *filename)
Definition: fileutil.cpp:118
virtual void setQuality(double value) POL_OVERRIDE
Definition: equipmnt.cpp:122
bool read(ConfigElem &elem)
Definition: cfgfile.cpp:1047
Equipment(const ItemDesc &itemdesc, Core::UOBJ_CLASS uobj_class, const Core::EquipDesc *permanent_descriptor)
Definition: equipmnt.cpp:38
void send_object_cache_to_inrange(const UObject *obj)
Definition: tooltips.cpp:77
virtual size_t estimatedSize() const POL_OVERRIDE
Definition: equipdsc.cpp:22
void set_rest(const char *newrest)
Definition: cfgfile.cpp:89
virtual ~Equipment()
Definition: equipmnt.cpp:44
Mobile::Character * GetCharacterOwner()
Definition: item.cpp:1168
Definition: berror.cpp:12
Equipment * find_intrinsic_equipment(const std::string &name, u8 layer)
Looks up for an existing intrinsic equipment and return it or NULL if not found.
Definition: equipmnt.cpp:128