summaryrefslogtreecommitdiffstats
path: root/src/MonsterConfig.cpp
blob: a15e413ba6fbacfba7e9ef17edb4510a56b46323 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules

#include "MonsterConfig.h"
#include "Mobs/Monster.h"
#include "inifile/iniFile.h"





struct cMonsterConfig::sAttributesStruct
{
	AString m_Name;
	int     m_SightDistance;
	int     m_AttackDamage;
	int     m_AttackRange;
	double  m_AttackRate;
	int     m_MaxHealth;
	bool    m_IsFireproof;
};





struct cMonsterConfig::sMonsterConfigState
{
	AString MonsterTypes;
	std::list< sAttributesStruct > AttributesList;
};





cMonsterConfig::cMonsterConfig(void)
	: m_pState( new sMonsterConfigState)
{
	Initialize();
}





cMonsterConfig::~cMonsterConfig()
{
	delete m_pState;
	m_pState = nullptr;
}





void cMonsterConfig::Initialize()
{
	cIniFile MonstersIniFile;
	
	if (!MonstersIniFile.ReadFile("monsters.ini"))
	{
		LOGWARNING("%s: Cannot read monsters.ini file, monster attributes not available", __FUNCTION__);
		return;
	}
	
	for (int i = (int)MonstersIniFile.GetNumKeys(); i >= 0; i--)
	{
		sAttributesStruct Attributes;
		AString Name = MonstersIniFile.GetKeyName(i);
		Attributes.m_Name = Name;
		Attributes.m_AttackDamage  = MonstersIniFile.GetValueI(Name, "AttackDamage",  0);
		Attributes.m_AttackRange   = MonstersIniFile.GetValueI(Name, "AttackRange",   0);
		Attributes.m_SightDistance = MonstersIniFile.GetValueI(Name, "SightDistance", 0);
		Attributes.m_AttackRate    = MonstersIniFile.GetValueF(Name, "AttackRate",    0);
		Attributes.m_MaxHealth     = MonstersIniFile.GetValueI(Name, "MaxHealth",     1);
		Attributes.m_IsFireproof   = MonstersIniFile.GetValueB(Name, "IsFireproof",   false);
		m_pState->AttributesList.push_front(Attributes);
	}  // for i - SplitList[]
}





void cMonsterConfig::AssignAttributes(cMonster * a_Monster, const AString & a_Name)
{
	std::list<sAttributesStruct>::const_iterator itr;
	for (itr = m_pState->AttributesList.begin(); itr != m_pState->AttributesList.end(); ++itr)
	{
		if (itr->m_Name.compare(a_Name) == 0)
		{
			a_Monster->SetAttackDamage (itr->m_AttackDamage);
			a_Monster->SetAttackRange  (itr->m_AttackRange);
			a_Monster->SetSightDistance(itr->m_SightDistance);
			a_Monster->SetAttackRate   ((float)itr->m_AttackRate);
			a_Monster->SetMaxHealth    (itr->m_MaxHealth);
			a_Monster->SetIsFireproof  (itr->m_IsFireproof);
			return;
		}
	}  // for itr - m_pState->AttributesList[]
}