JNR
laPropertyList.cpp
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
30 #include "stdafx.h"
31 #include "laParser.h"
32 #include "Core.h"
33 #include "RPG/RPG.h"
34 
35 laPropertyList::laPropertyList(void)
36 {
37  strcpy(_strName, "END");
38  _bReadName = M_TRUE;
39 
40  _pParent = NULL;
41 }
42 
43 laPropertyList::~laPropertyList(void)
44 {
45  //discard(); // NOTE: Data elements canoutlive the element, so don't discard automatically
46 }
47 
48 void laPropertyList::discard()
49 {
50  _mapBool.clear();
51  _mapInt.clear();
52  _mapDouble.clear();
53  _mapColor.clear();
54  _mapPoint.clear();
55  _mapAttack.clear();
56 
57  for(std::map<std::string, char*>::iterator i1=_mapText.begin(); i1!=_mapText.end(); i1++)
58  delete [] (i1->second);
59  _mapText.clear();
60 
61  for(std::map<std::string, class laAnimatedModel*>::iterator i2=_mapModel.begin(); i2!=_mapModel.end(); i2++)
62  delete (i2->second);
63  _mapModel.clear();
64  _vModel.clear();
65 
66  for(std::map<std::string, class fxParticleSystem*>::iterator i3=_mapFx.begin(); i3!=_mapFx.end(); i3++)
67  delete (i3->second);
68  _mapFx.clear();
69  _vFx.clear();
70 
71  for(std::map<std::string, class laPropertyList*>::iterator i4=_mapChildren.begin(); i4!=_mapChildren.end(); i4++)
72  {
73  (i4->second)->discard();
74  delete (i4->second);
75  }
76  _mapChildren.clear();
77 }
78 
79 
80 void laPropertyList::load(class laFileParser *fp)
81 {
82  if(_bReadName) fp->readSectionSeparator(_strName);
83 
84  //If the name is "END" this is not an acutal element
85  //but a marker that there are no more elements
86  if( !strcmp(_strName, "END") )
87  throw laError(_strName);
88 
89  //Anticipated closing tag
90  char strEndTag[32], strName[64];
91  sprintf(strEndTag, "/%s", _strName);
92 
93  //Read components
94  _read_components(fp, strEndTag);
95 }
96 
97 void laPropertyList::_read_components(laFileParser* fp, char* strEndTag)
98 {
99  //Anticipated closing tag
100  char strTag[32], strName[64];
101 
102  while(1)
103  {
104  fp->readSectionSeparator(strTag);
105  //MSG(strTag);
106 
107  if( !strcmp(strTag, strEndTag) ) break;
108 
109  laTypedObject t(strTag);
110  if( (t.type() != M_TYPE_COMMENT) && ( t.type() !=M_TYPE_PLIST) )
111  fp->readLabel( strName );
112 
113  fp->readObj( &t, M_FALSE );
114 
115  switch(t.type())
116  {
117  case M_TYPE_BOOL:
118  _mapBool[strName] = *t.getBool();
119  t.discard();
120  break;
121  case M_TYPE_INT:
122  _mapInt[strName] = *t.getInt();
123  t.discard();
124  break;
125  case M_TYPE_DOUBLE:
126  _mapDouble[strName] = *t.getDouble();
127  t.discard();
128  break;
129  case M_TYPE_POINT3:
130  _mapPoint[strName] = *t.getPoint();
131  t.discard();
132  break;
133  case M_TYPE_COLOR:
134  _mapColor[strName] = *t.getColor();
135  t.discard();
136  break;
137  case M_TYPE_ATTACK:
138  _mapAttack[strName] = *t.attack();
139  t.discard();
140  break;
141  /*case M_TYPE_TRAP:
142  _mapTrap[strName] = *t.trap();
143  t.discard();
144  break;*/
145  case M_TYPE_TEXT:
146  _mapText[strName] = t.getText();
147  //don't discard
148  break;
149  case M_TYPE_MODEL:
150  _mapModel[strName] = t.getModel();
151  _vModel.push_back( t.getModel() );
152  //don't discard
153  break;
154  case M_TYPE_FX:
155  _mapFx[strName] = t.getFx();
156  _vFx.push_back( t.getFx() );
157  //don't discard
158  break;
159 
160  case M_TYPE_PLIST:
161  _mapChildren[strTag] = t.getPList();
162  _mapChildren[strTag]->_pParent = this;
163  //don't discard
164  break;
165 
166  case M_TYPE_INCLUDE:
167  {
168  char *strInclude = t.getText();
169  laFileParser fp(strInclude);
170  _read_components(&fp, "END");
171  }
172  break;
173  }
174  }
175 }
176 
177 M_BOOL laPropertyList::getBool(std::string name)
178 {
179  std::map<std::string, M_BOOL>::iterator i;
180  i = _mapBool.find(name);
181 
182  //ASSERT( i!=_mapBool.end(), "Unable to find componend of this type", name.c_str());
183  if( i==_mapBool.end() ) throw laError_PropertyNotDefined(name);
184 
185  return (i->second);
186 }
187 
188 int laPropertyList::getInt(std::string name)
189 {
190  std::map<std::string, int>::iterator i;
191  i = _mapInt.find(name);
192 
193  //ASSERT( i!=_mapInt.end(), "Unable to find componend of this type", name.c_str());
194  if( i==_mapInt.end() ) throw laError_PropertyNotDefined(name);
195 
196  return (i->second);
197 }
198 
199 double laPropertyList::getDouble(std::string name)
200 {
201  std::map<std::string, double>::iterator i;
202  i = _mapDouble.find(name);
203 
204  //ASSERT( i!=_mapDouble.end(), "Unable to find componend of this type", name.c_str());
205  if( i==_mapDouble.end() ) throw laError_PropertyNotDefined(name);
206 
207  return (i->second);
208 }
209 
210 char* laPropertyList::getText(std::string name)
211 {
212  std::map<std::string, char*>::iterator i;
213  i = _mapText.find(name);
214 
215  //ASSERT( i!=_mapText.end(), "Unable to find componend of this type", name.c_str());
216  if( i==_mapText.end() ) throw laError_PropertyNotDefined(name);
217 
218  return (i->second);
219 }
220 
221 laColor laPropertyList::getColor(std::string name)
222 {
223  std::map<std::string, laColor>::iterator i;
224  i = _mapColor.find(name);
225 
226  //ASSERT( i!=_mapColor.end(), "Unable to find componend of this type", name.c_str());
227  if( i==_mapColor.end() ) throw laError_PropertyNotDefined(name);
228 
229  return (i->second);
230 }
231 
232 laPoint3 laPropertyList::getPoint(std::string name)
233 {
234  std::map<std::string, laPoint3>::iterator i;
235  i = _mapPoint.find(name);
236 
237  //ASSERT( i!=_mapPoint.end(), "Unable to find componend of this type", name.c_str());
238  if( i==_mapPoint.end() ) throw laError_PropertyNotDefined(name);
239 
240  return (i->second);
241 }
242 
243 rpgAttack laPropertyList::attack(std::string name)
244 {
245  std::map<std::string, rpgAttack>::iterator i;
246  i = _mapAttack.find(name);
247 
248  //ASSERT( i!=_mapAttack.end(), "Unable to find componend of this type", name.c_str());
249  if( i==_mapAttack.end() ) throw laError_PropertyNotDefined(name);
250 
251  return (i->second);
252 }
253 
254 /*rpgTrap laPropertyList::trap(std::string name)
255 {
256  std::map<std::string, rpgTrap>::iterator i;
257  i = _mapTrap.find(name);
258 
259  //ASSERT( i!=_mapTrap.end(), "Unable to find componend of this type", name.c_str());
260  if( i==_mapTrap.end() ) throw laError_PropertyNotDefined(name);
261 
262  return (i->second);
263 }*/
264 
265 laAnimatedModel* laPropertyList::getModel(std::string name)
266 {
267  std::map<std::string, laAnimatedModel*>::iterator i;
268  i = _mapModel.find(name);
269 
270  //ASSERT( i!=_mapTrap.end(), "Unable to find componend of this type", name.c_str());
271  if( i==_mapModel.end() ) throw laError_PropertyNotDefined(name);
272 
273  return (i->second);
274 }
275 
276 fxParticleSystem* laPropertyList::getFx(std::string name)
277 {
278  std::map<std::string, fxParticleSystem*>::iterator i;
279  i = _mapFx.find(name);
280 
281  //ASSERT( i!=_mapTrap.end(), "Unable to find componend of this type", name.c_str());
282  if( i==_mapFx.end() ) throw laError_PropertyNotDefined(name);
283 
284  return (i->second);
285 }
286 
287 laPropertyList* laPropertyList::getChild(std::string name)
288 {
289  std::map<std::string, laPropertyList*>::iterator i;
290  i = _mapChildren.find(name);
291 
292  //ASSERT( i!=_mapTrap.end(), "Unable to find componend of this type", name.c_str());
293  if( i==_mapChildren.end() ) throw laError_PropertyNotDefined(name);
294 
295  return (i->second);
296 }
297 
298 laAnimatedModel* laPropertyList::getModel(unsigned i)
299 {
300  ASSERT( i < getModelCnt(), "Invalid model" );
301  return _vModel[i];
302 }
303 
304 fxParticleSystem* laPropertyList::getFx(unsigned i)
305 {
306  ASSERT( i < getFxCnt(), "Invalid model" );
307 
308  return _vFx[i];
309 }
310 
311 laPropertyList* laPropertyList::getTopmost()
312 {
313  if(_pParent)
314  {
315  //MSG("%lu", (unsigned long)_pParent);
316  return _pParent->getTopmost();
317  }
318 
319  //MSG("B");
320  return this;
321 }
Animated 3D Model.
Attack RPG Properties.
Definition: rpgAttack.h:55
Error handling class.
Definition: laError.h:46
File Parser.
Definition: laFileParser.h:41