-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScenarioHandler.cpp
executable file
·176 lines (146 loc) · 6.29 KB
/
ScenarioHandler.cpp
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "ScenarioHandler.h"
#include <dtUtil/stringutils.h>
#include <dtUtil/xercesutils.h>
#include <xercesc/sax2/XMLReaderFactory.hpp>
ScenarioHandler::ScenarioHandler()
{
mScenarioData = new ScenarioData();
}
ScenarioHandler::~ScenarioHandler()
{
}
void ScenarioHandler::characters(const XMLCh* const chars, const unsigned int length)
{
/*if (mCurrentElement == UnitConfigSchema::LIBRARY_PATH)
{
std::string path(dtUtil::XMLStringConverter(chars).ToString());
mConfigData.LIBRARY_PATHS.push_back(path);
}*/
}
void ScenarioHandler::endDocument() {}
void ScenarioHandler::endElement(const XMLCh* const uri,const XMLCh* const localname,const XMLCh* const qname) {
dtUtil::XMLStringConverter stringConv(localname);
mCurrentElement = stringConv.ToString();
if( mCurrentElement == "scenario" ) {
//end of the document
mScenarioData->numInstances = mInstanceIDs.size();
}
}
void ScenarioHandler::ignorableWhitespace(const XMLCh* const chars, const unsigned int length) {}
void ScenarioHandler::processingInstruction(const XMLCh* const target, const XMLCh* const data) {}
void ScenarioHandler::setDocumentLocator(const XERCES_CPP_NAMESPACE_QUALIFIER Locator* const locator) {}
void ScenarioHandler::startDocument() {}
void ScenarioHandler::startPrefixMapping(const XMLCh* const prefix,const XMLCh* const uri) {}
void ScenarioHandler::endPrefixMapping(const XMLCh* const prefix) {}
void ScenarioHandler::skippedEntity(const XMLCh* const name) {}
///\todo use ApplicationConfigSchema for attribute and node name searches.
///\todo optimize out string and data copies by just using the default applicatioinconfigdata struct
void ScenarioHandler::startElement(const XMLCh* const uri,
const XMLCh* const localname,
const XMLCh* const qname,
const XERCES_CPP_NAMESPACE_QUALIFIER Attributes& attrs)
{
typedef dtUtil::AttributeSearch::ResultMap RMap;
dtUtil::XMLStringConverter stringConv(localname);
mCurrentElement = stringConv.ToString();
if( mCurrentElement == "scenario" )
{
// search for specific named attributes, append the list of searched strings
dtUtil::AttributeSearch attrsearch;
// perform the search
RMap results = attrsearch( attrs );
RMap::iterator iter = results.find("map");
if( iter != results.end() )
mScenarioData->mapName = iter->second;
iter = results.find("time_limit");
if( iter != results.end() )
mScenarioData->timeLimit = dtUtil::ToType<int>((iter->second).c_str());
iter = results.find("goal_time");
if( iter != results.end() )
mScenarioData->goalTime = dtUtil::ToType<int>((iter->second).c_str());
iter = results.find("controller");
if( iter != results.end() )
mScenarioData->controllerId = dtUtil::ToType<int>((iter->second).c_str());
iter = results.find("game");
if( iter != results.end() )
mScenarioData->gameType = iter->second;
iter = results.find("sim_time");
if( iter != results.end() )
mScenarioData->simTime = dtUtil::ToType<float>((iter->second).c_str());
iter = results.find("speedup");
if( iter != results.end() )
mScenarioData->speedup = dtUtil::ToType<float>((iter->second).c_str());
iter = results.find("auto_fire");
if( iter != results.end() )
mScenarioData->autoFire = dtUtil::ToType<bool>((iter->second).c_str());
}
else if( mCurrentElement == "side" )
{
dtUtil::AttributeSearch attrsearch;
RMap results = attrsearch( attrs );
RMap::iterator iter;
mCurrentSide = new ScenarioSideData();
//record side in scenario data structure
mScenarioData->sides.push_back(mCurrentSide);
iter = results.find("name");
if( iter != results.end() )
mCurrentSide->setSide(iter->second);
}
else if( mCurrentElement == "unit" )
{
dtUtil::AttributeSearch attrsearch;
RMap results = attrsearch( attrs );
RMap::iterator iter;
mCurrentUnit = new ScenarioUnitData();
mCurrentSide->units.push_back(mCurrentUnit);
iter = results.find("type");
if( iter != results.end() )
mCurrentUnit->setType(iter->second);
iter = results.find("number");
if( iter != results.end() ) {
mCurrentUnit->number = dtUtil::ToType<int>((iter->second).c_str());
mCurrentSide->total += mCurrentUnit->number;
}
iter = results.find("instance");
if( iter != results.end() ) {
if (iter->second == "local")
{
mCurrentUnit->instance = theApp->getInstanceID();
}
else
{
mCurrentUnit->instance = dtUtil::ToType<int>((iter->second).c_str());
}
//add instance id if its not already there
bool found = false;
for (unsigned int i=0; i < mInstanceIDs.size(); i++) {
if (mInstanceIDs.at(i) == mCurrentUnit->instance) {
found = true;
break;
}
}
if (!found) {
mInstanceIDs.push_back(mCurrentUnit->instance);
}
}
}
else if ( mCurrentElement == "pos" || mCurrentElement == "startpos" || mCurrentElement == "endpos")
{
dtUtil::AttributeSearch attrsearch;
RMap results = attrsearch( attrs );
RMap::iterator iter;
osg::Vec2 pos;
iter = results.find("x");
if( iter != results.end() )
pos[0] = dtUtil::ToType<float>((iter->second).c_str());
iter = results.find("y");
if( iter != results.end() )
pos[1] = dtUtil::ToType<float>((iter->second).c_str());
if (mCurrentElement == "pos") {
mCurrentUnit->addPosition(pos);
mCurrentSide->total++;
} else {
mCurrentUnit->addStartOrEndPosition(pos);
}
}
}