Eliminated some problems resulting from last merge
[openstranded.git] / src / defparse / defaction.cc
blob8fae59a1059f1bc1b395898e0d5405a199031279
1 /*
2 * This file implements general functions and classes
3 * from the defaction.hh header
5 * Copyright (C) 2008 Hermann Walth
7 * This file is part of OpenStranded
9 * OpenStranded is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * OpenStranded is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with OpenStranded. If not, see <http://www.gnu.org/licenses/>.
23 #include <iostream>
24 #include <string>
25 #include <sstream>
26 #include <cstring>
27 #include "../s2string.hh"
28 #include "../kingdom.hh"
29 #include "../error.hh"
30 #include "defaction.hh"
32 ID currentType = 0;
35 * Action List methods
38 def::ActionList::ActionList ():
39 std::map <std::string, def::Action*> () {}
42 // Destructor, deletes all list's elements
43 def::ActionList::~ActionList ()
45 std::map <std::string, def::Action*>::iterator iter;
46 for (iter = this->begin (); iter != this->end (); iter++)
47 delete (iter->second);
50 // Wrapper operator[] method for const char* strings
51 def::Action*&
52 def::ActionList::listItem (const char* index)
54 return (*this) [std::string (index)];
55 //return (this->find (std::string (index)))->second;
60 * Action classes method definitions
63 def::Action::Action (ID kingdom)
65 this->kingdomID = kingdom;
66 this->lastType = 0;
70 def::IDAction::IDAction (ID kingdom):
71 Action (kingdom) {}
73 def::NameAction::NameAction (ID kingdom):
74 Action (kingdom) {}
76 def::ModelAction::ModelAction (ID kingdom):
77 Action (kingdom) {}
79 def::IconAction::IconAction (ID kingdom):
80 Action (kingdom) {}
82 def::XAction::XAction (ID kingdom):
83 Action (kingdom) {}
85 def::YAction::YAction (ID kingdom):
86 Action (kingdom) {}
88 def::ZAction::ZAction (ID kingdom):
89 Action (kingdom) {}
91 def::ScaleAction::ScaleAction (ID kingdom):
92 Action (kingdom) {}
94 def::RAction::RAction (ID kingdom):
95 Action (kingdom) {}
97 def::GAction::GAction (ID kingdom):
98 Action (kingdom) {}
100 def::BAction::BAction (ID kingdom):
101 Action (kingdom) {}
103 def::ColorAction::ColorAction (ID kingdom):
104 Action (kingdom) {}
106 def::AutofadeAction::AutofadeAction (ID kingdom):
107 Action (kingdom) {}
109 def::AlphaAction::AlphaAction (ID kingdom):
110 Action (kingdom) {}
112 def::ShineAction::ShineAction (ID kingdom):
113 Action (kingdom) {}
117 void
118 def::IDAction::operator() (std::string rtext)
120 ID newID;
121 std::istringstream (rtext, std::ios::in) >> newID;
123 currentType = newID;
125 if (Kingdom::kingdomList.find (kingdomID) != Kingdom::kingdomList.end ())
126 if (!Kingdom::kingdomList[kingdomID]->typeExists(newID))
127 Kingdom::kingdomList[kingdomID]->insertType (newID);
128 else
130 std::ostringstream oss (std::ios::out);
131 oss << "Type #" << newID << " has already been declared!";
132 throw StrandedFatal (oss.str ());
134 else
135 throw StrandedFatal ("Kingdom does not exist!");
136 //std::cerr << "Fatal: kingdom does not exist!" << std::endl;
141 void
142 def::NameAction::operator() (std::string rtext)
144 if (currentType == 0)
146 throw StrandedError ("No type ID specified, assignment of value \"name\" not possible");
147 return;
150 if (this->lastType != currentType)
151 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setName (rtext);
152 else
153 throw StrandedError ("Double assignment of value \"name\" in object type #" + currentType);
158 void
159 def::ModelAction::operator() (std::string rtext)
161 if (currentType == 0)
163 throw StrandedError ("No type ID specified, assignment of value \"model\" not possible");
164 return;
167 if (this->lastType != currentType)
168 Kingdom::kingdomList[kingdomID]->getType (currentType)->setModel (rtext);
169 else
170 throw StrandedError ("Double assignment of value \"model\" in type #" + currentType);
175 void
176 def::IconAction::operator() (std::string rtext)
178 if (currentType == 0)
179 throw StrandedError ("No type ID specified, assignment of value \"icon\" not possible");
181 if (this->lastType != currentType)
182 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setIconPath (rtext);
183 else
184 throw StrandedError ("Double assignment of value \"icon\" in type #" + currentType);
189 void
190 def::XAction::operator() (std::string rtext)
192 float newScaleX;
193 std::istringstream (rtext) >> newScaleX;
195 if (currentType == 0)
197 std::cerr << "Error: No type ID specified, assignment of value \"x\" not possible" << std::endl;
198 return;
201 if (newScaleX <= 0)
203 std::cerr << "Error: Invalid value \"x\"; fallback to 1.0 in type #" << currentType << std::endl;
204 newScaleX = 1.0;
207 if (this->lastType != currentType)
208 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setScaleX (newScaleX);
209 else
210 std::cerr << "Error: Double assignment of value \"x\" in type #" << currentType << std::endl;
215 void
216 def::YAction::operator() (std::string rtext)
218 float newScaleY;
219 std::istringstream (rtext) >> newScaleY;
221 if (currentType == 0)
223 std::cerr << "Error: No type ID specified, assignment of value \"y\" not possible" << std::endl;
224 return;
227 if (newScaleY <= 0)
229 std::cerr << "Error: Invalid value \"y\"; fallback to 1.0 in type #" << currentType << std::endl;
230 newScaleY = 1.0;
233 if (this->lastType != currentType)
234 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setScaleY (newScaleY);
235 else
236 std::cerr << "Error: Double assignment of value \"y\" in Object type #" << currentType << std::endl;
241 void
242 def::ZAction::operator() (std::string rtext)
244 float newScaleZ;
245 std::istringstream (rtext) >> newScaleZ;
247 if (currentType == 0)
249 std::cerr << "Error: No type ID specified, assignment of value \"z\" not possible" << std::endl;
250 return;
253 if (newScaleZ <= 0)
255 std::cerr << "Error: Invalid value \"z\"; fallback to 1.0 in type #" << currentType << std::endl;
256 newScaleZ = 1.0;
259 if (this->lastType != currentType)
260 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setScaleZ (newScaleZ);
261 else
262 std::cerr << "Error: Double assignment of value \"z\" in type #" << currentType << std::endl;
267 void
268 def::ScaleAction::operator() (std::string rtext)
270 std::string scaleValues[3];
271 int warnFlag;
272 float endValue;
275 if (currentType == 0)
277 std::cerr << "Error: No type ID specified, assignment of value \"scale\" not possible" << std::endl;
278 return;
281 warnFlag = s2str::explode (rtext, scaleValues, ",", 3);
283 // Display error message when number of arguments wasn't either 1 or 3
284 if (warnFlag != EXPLODE_OK && !inFlag (warnFlag, EXPLODE_NO_TOKEN))
286 std::cerr << "Warning: Wrong number of arguments for value \"scale\"" << std::endl;
290 if (this->lastType != currentType)
292 // Catch special case when only one value was given and needs to be applied
293 // to all x, y and z
294 if (inFlag (warnFlag, EXPLODE_NO_TOKEN))
296 if (!s2str::stof (scaleValues[0], endValue))
298 std::cerr << "Error: Invalid value in \"scale\"; fallback to 1.0 in type #" << currentType << std::endl;
299 endValue = 1.0;
301 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setScaleX (endValue);
302 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setScaleY (endValue);
303 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setScaleZ (endValue);
304 return;
307 // scaleX
308 if (!s2str::stof (scaleValues[0], endValue))
310 std::cerr << "Error: Invalid x value in \"scale\"; fallback to 1.0 in type #" << currentType << std::endl;
311 endValue = 1.0;
313 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setScaleX (endValue);
316 // scaleY
317 if (!s2str::stof (scaleValues[1], endValue))
319 std::cerr << "Error: Invalid y value in \"scale\"; fallback to 1.0 in type #" << currentType << std::endl;
320 endValue = 1.0;
322 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setScaleY (endValue);
326 // scaleZ
327 if (!s2str::stof (scaleValues[2], endValue))
329 std::cerr << "Error: Invalid z value in \"scale\"; fallback to 1.0 in type #" << currentType << std::endl;
330 endValue = 1.0;
332 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setScaleZ (endValue);
334 else
336 std::cerr << "Error: Double assignment of value \"scale\" in type #" << currentType << std::endl;
342 void
343 def::RAction::operator() (std::string rtext)
345 int newColorR;
346 bool strValid;
348 if (currentType == 0)
350 std::cerr << "Error: No type ID specified, assignment of value \"r\" not possible" << std::endl;
351 return;
354 strValid = s2str::stoi (rtext, newColorR);
355 if (!strValid || newColorR < 0 || newColorR > 255)
357 std::cerr << "Error: Invalid argument for \"r\" value in type #" << currentType << ", fallback to 0" << std::endl;
358 newColorR = 0;
361 if (this->lastType != currentType)
362 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setColorR ( (uint8_t) newColorR);
363 else
364 std::cerr << "Error: Double assignment of value \"r\" in type #" << currentType << std::endl;
369 void
370 def::GAction::operator() (std::string rtext)
372 int newColorG;
373 bool strValid;
375 if (currentType == 0)
377 std::cerr << "Error: No type ID specified, assignment of value \"g\" not possible" << std::endl;
378 return;
381 strValid = s2str::stoi (rtext, newColorG);
382 if (!strValid || newColorG < 0 || newColorG > 255)
384 std::cerr << "Error: Invalid argument for \"g\" value in type #" << currentType << ", fallback to 0" << std::endl;
385 newColorG = 0;
388 if (this->lastType != currentType)
389 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setColorG ( (uint8_t) newColorG);
390 else
391 std::cerr << "Error: Double assignment of value \"g\" in type #" << currentType << std::endl;
396 void
397 def::BAction::operator() (std::string rtext)
399 int newColorB;
400 bool strValid;
402 if (currentType == 0)
404 std::cerr << "Error: No type ID specified, assignment of value \"b\" not possible" << std::endl;
405 return;
408 strValid = s2str::stoi (rtext, newColorB);
409 if (!strValid || newColorB < 0 || newColorB > 255)
411 std::cerr << "Error: Invalid argument for \"b\" value in type #" << currentType << ", fallback to 0" << std::endl;
412 newColorB = 0;
415 if (this->lastType != currentType)
416 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setColorB ( (uint8_t) newColorB);
417 else
418 std::cerr << "Error: Double assignment of value \"b\" in type #" << currentType << std::endl;
423 void
424 def::ColorAction::operator() (std::string rtext)
426 std::string colorValues[3];
427 int endValue;
428 int warnFlag;
429 bool strValid;
431 if (currentType == 0)
433 std::cerr << "Error: No type ID specified, assignment of value \"color\" not possible" << std::endl;
434 return;
437 warnFlag = s2str::explode (rtext, colorValues, ",", 3);
439 // Display error message when number of arguments wasn't either 1 or 3
440 if (warnFlag != EXPLODE_OK && !inFlag (warnFlag, EXPLODE_NO_TOKEN))
442 std::cerr << "Warning: Wrong number of arguments for value \"color\"" << std::endl;
445 if (this->lastType != currentType)
447 // Catch special case when only one value was given and needs to be applied
448 // to all r, g and b
449 if (inFlag (warnFlag, EXPLODE_NO_TOKEN))
451 strValid = s2str::stoi (colorValues[0], endValue);
452 if (!strValid || endValue < 0 || endValue > 255)
454 std::cerr << "Error: Invalid value in \"color\"; fallback to 0.0 in type #" << currentType << std::endl;
455 endValue = 0;
457 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setColorR (endValue);
458 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setColorG (endValue);
459 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setColorB (endValue);
460 return;
463 // R
464 strValid = s2str::stoi (colorValues[0], endValue);
465 if (!strValid || endValue < 0 || endValue > 255)
467 std::cerr << "Error: Invalid argument for \"r\" value in \"color\" in type #" << currentType << ", fallback to 0" << std::endl;
468 endValue = 0;
471 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setColorR ( (uint8_t) endValue);
474 // G
475 strValid = s2str::stoi (colorValues[1], endValue);
476 if (!strValid || endValue < 0 || endValue > 255)
478 std::cerr << "Error: Invalid argument for \"g\" value in \"color\" in type #" << currentType << ", fallback to 0" << std::endl;
479 endValue = 0;
482 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setColorG ( (uint8_t) endValue);
485 // B
486 strValid = s2str::stoi (colorValues[2], endValue);
487 if (!strValid || endValue > 255)
489 std::cerr << "Error: Invalid argument for \"b\" value in \"color\" in type #" << currentType << ", fallback to 0" << std::endl;
490 endValue = 0;
493 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setColorB ( (uint8_t) endValue);
496 else
498 std::cerr << "Error: Double assignment of value \"color\" in type #" << currentType << std::endl;
504 void
505 def::AutofadeAction::operator() (std::string rtext)
507 float endValue;
509 if (currentType == 0)
511 std::cerr << "Error: No type id specified, assignment of value \"autofade\" not possible" << std::endl;
512 return;
515 if (this->lastType != currentType)
517 if (!s2str::stof (rtext, endValue))
519 std::cerr << "Error: Invalid argument for value \"autofade\" in type #" << currentType << ", fallback to 500" << std::endl;
520 endValue = 500.0;
523 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setFadingDistance (endValue);
525 else
527 std::cerr << "Error: Double assignment of value \"autofade\" in type #" << currentType << std::endl;
533 void
534 def::AlphaAction::operator() (std::string rtext)
536 float endValue;
538 if (currentType == 0)
540 std::cerr << "Error: No type id specified, assignment of value \"alpha\" not possible" << std::endl;
541 return;
544 if (this->lastType != currentType)
546 if (!s2str::stof (rtext, endValue) || endValue < 0.0 || endValue > 1.0)
548 std::cerr << "Error: Invalid argument for value \"alpha\" in type #" << currentType << ", fallback to 1.0" << std::endl;
549 endValue = 1.0;
552 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setAlpha (endValue);
554 else
556 std::cerr << "Error: Double assignment of value \"alpha\" in type #" << currentType << std::endl;
562 void
563 def::ShineAction::operator() (std::string rtext)
565 float endValue;
567 if (currentType == 0)
569 std::cerr << "Error: No type id specified, assignment of value \"shine\" not possible" << std::endl;
570 return;
573 if (this->lastType != currentType)
575 if (!s2str::stof (rtext, endValue) || endValue < 0.0 || endValue > 1.0)
577 std::cerr << "Error: Invalid argument for value \"shine\" in type #" << currentType << ", fallback to 0.0" << std::endl;
578 endValue = 0.0;
581 Kingdom::kingdomList[kingdomID]->getType (currentType) ->setShine (endValue);
583 else
585 std::cerr << "Error: Double assignment of value \"shine\" in type #" << currentType << std::endl;