Handle OptionEntryEnum in the console
[scorched3d/parasti.git] / src / server / tankai / TankAIStore.cpp
blob14730592d9dd048c99f137269d3054681a616f08
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // Scorched3D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #include <XML/XMLFile.h>
22 #include <common/Defines.h>
23 #include <tankai/TankAIStore.h>
24 #include <tankai/TankAICurrent.h>
25 #include <tankai/TankAIShallow.h>
26 #include <tankai/TankAIRandom.h>
27 #include <stdlib.h>
29 TankAIStore::TankAIStore()
34 TankAIStore::~TankAIStore()
39 void TankAIStore::clearAIs()
41 while (!ais_.empty())
43 TankAI *ai = ais_.front();
44 ais_.pop_front();
45 delete ai;
49 bool TankAIStore::loadAIs(bool shallow)
51 // Load key definition file
52 XMLFile file;
53 if (!file.readFile(S3D::getDataFile("data/tankais.xml")))
55 S3D::dialogMessage("TankAIStore",
56 S3D::formatStringBuffer("Failed to parse \"%s\"\n%s",
57 "data/tankais.xml",
58 file.getParserError()));
59 return false;
62 // Check file exists
63 if (!file.getRootNode())
65 S3D::dialogMessage("TankAIStore",
66 S3D::formatStringBuffer("Failed to find tank ai definition file \"%s\"",
67 "data/tankais.xml"));
68 return false;
71 // Itterate all of the keys in the file
72 std::list<XMLNode *>::iterator childrenItor;
73 std::list<XMLNode *> &children = file.getRootNode()->getChildren();
74 for (childrenItor = children.begin();
75 childrenItor != children.end();
76 childrenItor++)
78 // Parse the ai entry
79 XMLNode *currentNode = (*childrenItor);
80 if (strcmp(currentNode->getName(), "ai"))
82 S3D::dialogMessage("TankAIStore",
83 "Failed to find ai node");
84 return false;
87 TankAI *computer = 0;
88 if (shallow) computer = new TankAIShallow;
89 else computer = new TankAICurrent;
90 if (!computer->parseConfig(currentNode))
92 return false;
95 addAI(computer);
98 // Add the random player
100 TankAIRandom *tankAI = new TankAIRandom;
101 bool foundRandom = false;
102 std::list<TankAI *>::iterator itor;
103 for (itor = ais_.begin();
104 itor != ais_.end();
105 itor++)
107 TankAI *ai = (*itor);
108 if (ai->availableForRandom())
110 foundRandom = true;
111 tankAI->addTankAI(ai);
114 if (!foundRandom)
116 S3D::dialogMessage("TankAIStore",
117 "No tank ais are marked availableforrandom");
118 return false;
121 addAI(tankAI);
124 return true;
127 TankAI *TankAIStore::getAIByName(const char *name)
129 // Computers
130 std::list<TankAI *>::iterator itor;
131 for (itor = ais_.begin();
132 itor != ais_.end();
133 itor++)
135 if (!strcmp((*itor)->getName(), name)) return (*itor);
138 return 0;
141 void TankAIStore::addAI(TankAI *ai)
143 ais_.push_back(ai);