File: DamageArrayHandler.cpp

package info (click to toggle)
spring 0.81.2.1%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 28,496 kB
  • ctags: 37,096
  • sloc: cpp: 238,659; ansic: 13,784; java: 12,175; awk: 3,428; python: 1,159; xml: 738; perl: 405; sh: 297; makefile: 267; pascal: 228; objc: 192
file content (88 lines) | stat: -rw-r--r-- 2,002 bytes parent folder | download
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
#include "StdAfx.h"
#include <algorithm>
#include <locale>
#include <cctype>
#include "mmgr.h"

#include "DamageArrayHandler.h"
#include "DamageArray.h"
#include "LogOutput.h"
#include "Game/Game.h"
#include "Lua/LuaParser.h"
#include "creg/STL_Map.h"
#include "Util.h"
#include "Exceptions.h"

using namespace std;


CR_BIND(CDamageArrayHandler, );

CR_REG_METADATA(CDamageArrayHandler, (
		CR_MEMBER(numTypes),
		CR_MEMBER(name2type),
		CR_MEMBER(typeList),
		CR_RESERVED(16)
		));


CDamageArrayHandler* damageArrayHandler;


CDamageArrayHandler::CDamageArrayHandler(void)
{
	try {
		const LuaTable rootTable = game->defsParser->GetRoot().SubTable("ArmorDefs");
		if (!rootTable.IsValid()) {
			throw content_error("Error loading ArmorDefs");
		}

		rootTable.GetKeys(typeList);

		numTypes = typeList.size() + 1;
		typeList.insert(typeList.begin(), "default");
		name2type["default"] = 0;

		logOutput.Print("Number of damage types: %d", numTypes);

		for (int armorID = 1; armorID < (int)typeList.size(); armorID++) {
			const string armorName = StringToLower(typeList[armorID]);
			if (armorName == "default") {
				throw content_error("Tried to define the \"default\" armor type\n");
			}
			name2type[armorName] = armorID;

			LuaTable armorTable = rootTable.SubTable(typeList[armorID]);
			vector<string> units; // the values are not used (afaict)
			armorTable.GetKeys(units);

			vector<string>::const_iterator ui;
			for (ui = units.begin(); ui != units.end(); ++ui) {
				const string unitName = StringToLower(*ui); // NOTE: not required
				name2type[unitName] = armorID;
			}
		}
	}
	catch (content_error) {
		numTypes = 1;
		name2type.clear();
		name2type["default"] = 0;
		typeList.clear();
		typeList.push_back("default");
	}
}


CDamageArrayHandler::~CDamageArrayHandler(void)
{
}


int CDamageArrayHandler::GetTypeFromName(string name)
{
	StringToLowerInPlace(name);
	if (name2type.find(name) != name2type.end()) {
		return name2type[name];
	}
	return 0; // 'default' armor index
}