Cuberite
A lightweight, fast and extensible game server for Minecraft
MonsterConfig.cpp
Go to the documentation of this file.
1 
2 #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
3 
4 #include "MonsterConfig.h"
5 #include "Mobs/Monster.h"
6 #include "IniFile.h"
7 
8 
9 
10 
11 
13 {
17  double m_AttackRange;
18  double m_AttackRate;
19  double m_MaxHealth;
22 };
23 
24 
25 
26 
27 
29 {
31  std::list< sAttributesStruct > AttributesList;
32 };
33 
34 
35 
36 
37 
39  : m_pState( new sMonsterConfigState)
40 {
41  Initialize();
42 }
43 
44 
45 
46 
47 
49 {
50  delete m_pState;
51  m_pState = nullptr;
52 }
53 
54 
55 
56 
57 
59 {
60  cIniFile MonstersIniFile;
61 
62  if (!MonstersIniFile.ReadFile("monsters.ini"))
63  {
64  LOGWARNING("%s: Cannot read monsters.ini file, monster attributes not available", __FUNCTION__);
65  return;
66  }
67 
68  for (int i = static_cast<int>(MonstersIniFile.GetNumKeys()); i >= 0; i--)
69  {
70  sAttributesStruct Attributes;
71  AString Name = MonstersIniFile.GetKeyName(i);
72  Attributes.m_Name = Name;
73  Attributes.m_AttackDamage = MonstersIniFile.GetValueI(Name, "AttackDamage", 0);
74  Attributes.m_AttackRange = MonstersIniFile.GetValueF(Name, "AttackRange", 0);
75  Attributes.m_SightDistance = MonstersIniFile.GetValueI(Name, "SightDistance", 0);
76  Attributes.m_AttackRate = MonstersIniFile.GetValueF(Name, "AttackRate", 0);
77  Attributes.m_MaxHealth = MonstersIniFile.GetValueF(Name, "MaxHealth", 1);
78  Attributes.m_IsFireproof = MonstersIniFile.GetValueB(Name, "IsFireproof", false);
79  Attributes.m_BurnsInDaylight = MonstersIniFile.GetValueB(Name, "BurnsInDaylight", false);
80  m_pState->AttributesList.push_front(Attributes);
81  } // for i - SplitList[]
82 }
83 
84 
85 
86 
87 
88 void cMonsterConfig::AssignAttributes(cMonster * a_Monster, const AString & a_Name)
89 {
90  std::list<sAttributesStruct>::const_iterator itr;
91  for (itr = m_pState->AttributesList.begin(); itr != m_pState->AttributesList.end(); ++itr)
92  {
93  if (itr->m_Name.compare(a_Name) == 0)
94  {
95  a_Monster->SetAttackDamage (itr->m_AttackDamage);
96  a_Monster->SetAttackRange (itr->m_AttackRange);
97  a_Monster->SetSightDistance (itr->m_SightDistance);
98  a_Monster->SetAttackRate (static_cast<float>(itr->m_AttackRate));
99  a_Monster->SetMaxHealth (static_cast<float>(itr->m_MaxHealth));
100  a_Monster->SetIsFireproof (itr->m_IsFireproof);
101  a_Monster->SetBurnsInDaylight(itr->m_BurnsInDaylight);
102  return;
103  }
104  } // for itr - m_pState->AttributesList[]
105 }
106 
107 
108 
109 
110 
void LOGWARNING(std::string_view a_Format, const Args &... args)
Definition: LoggerSimple.h:67
std::string AString
Definition: StringUtils.h:11
void SetIsFireproof(bool a_IsFireproof)
Sets whether the entity is fireproof.
Definition: Entity.cpp:1899
void SetMaxHealth(float a_MaxHealth)
Sets the maximum value for the health.
Definition: Entity.cpp:1887
AString GetKeyName(const int keyID) const
Definition: IniFile.cpp:293
bool ReadFile(const AString &a_FileName, bool a_AllowExampleRedirect=true)
Reads the contents of the specified ini file If the file doesn't exist and a_AllowExampleRedirect is ...
Definition: IniFile.cpp:50
int GetValueI(const AString &keyname, const AString &valuename, const int defValue=0) const
Definition: IniFile.cpp:506
bool GetValueB(const AString &keyname, const AString &valuename, const bool defValue=false) const
Definition: IniFile.h:140
int GetNumKeys(void) const
Returns number of keys currently in the ini.
Definition: IniFile.h:117
double GetValueF(const AString &keyname, const AString &valuename, const double defValue=0) const
Definition: IniFile.cpp:516
void SetBurnsInDaylight(bool a_BurnsInDaylight)
Definition: Monster.h:145
void SetAttackRate(double a_AttackRate)
Definition: Monster.h:126
void SetAttackDamage(int a_AttackDamage)
Definition: Monster.h:128
void SetAttackRange(double a_AttackRange)
Definition: Monster.h:127
void SetSightDistance(int a_SightDistance)
Definition: Monster.h:129
std::list< sAttributesStruct > AttributesList
sMonsterConfigState * m_pState
Definition: MonsterConfig.h:25
void AssignAttributes(cMonster *a_Monster, const AString &a_Name)