summaryrefslogtreecommitdiffstats
path: root/source/cMonsterConfig.cpp
blob: c399902c3cc703935291d947716c8abe050fc0e0 (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
108
109
110
111
112
113
114

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

#include "cMonsterConfig.h"
#include "Mobs/Monster.h"
#include "../iniFile/iniFile.h"
//#include <cstdio>





struct cMonsterConfig::sAttributesStruct
{
	AString m_name;
	float   m_SightDistance;
	float   m_AttackDamage;
	float   m_AttackRange;
	float   m_AttackRate;
	int     m_MaxHealth;
};





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





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





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





void cMonsterConfig::Initialize() {
	
	sAttributesStruct Attributes;
	cIniFile SettingsIniFile("settings.ini");
	cIniFile MonstersIniFile("monsters.ini");
	
	if (!SettingsIniFile.ReadFile() || !MonstersIniFile.ReadFile())
	{
		LOGWARNING("cMonsterConfig: Must have both settings.ini and monsters.ini to configure attributes\n\tusing default attributes \n");
		return;
	}
	
	m_pState->MonsterTypes = SettingsIniFile.GetValue("Monsters","Types","");
	
	if ( m_pState->MonsterTypes.empty() )
	{
		LOGWARNING("cMonsterConfig: No Monster types listed in config file, using default attributes \n");
		return;
	}
	
	AStringVector SplitList = StringSplit(m_pState->MonsterTypes,",");
	for (unsigned int i = 0; i < SplitList.size(); ++i)
	{
		if (!SplitList[i].empty())
		{
			Attributes.m_name = SplitList[i].c_str();
			Attributes.m_AttackDamage  = (float)MonstersIniFile.GetValueF(SplitList[i].c_str(), "AttackDamage",  0);
			Attributes.m_AttackRange   = (float)MonstersIniFile.GetValueF(SplitList[i].c_str(), "AttackRange",   0);
			Attributes.m_SightDistance = (float)MonstersIniFile.GetValueF(SplitList[i].c_str(), "SightDistance", 0);
			Attributes.m_AttackRate    = (float)MonstersIniFile.GetValueF(SplitList[i].c_str(), "AttackRate",    0);
			Attributes.m_MaxHealth     =        MonstersIniFile.GetValueI(SplitList[i].c_str(), "MaxHealth",     0);
			m_pState->AttributesList.push_front(Attributes);
		}
	}  // for i - SplitList[]
}





void cMonsterConfig::AssignAttributes(cMonster *m, const char* n)
{
	std::list<sAttributesStruct>::const_iterator itr;
	for (itr = m_pState->AttributesList.begin(); itr != m_pState->AttributesList.end(); ++itr)
	{
		if(itr->m_name.compare(n) == 0)
		{
			m->SetAttackDamage (itr->m_AttackDamage);
			m->SetAttackRange  (itr->m_AttackRange);
			m->SetSightDistance(itr->m_SightDistance);
			m->SetAttackRate   ((int)itr->m_AttackRate);
			m->SetMaxHealth    ((short)itr->m_MaxHealth);
		}
	}  // for itr - m_pState->AttributesList[]
}