00001 //////////////////////////////////////////////////////////////////////////////// 00002 // Scorched3D (c) 2000-2009 00003 // 00004 // This file is part of Scorched3D. 00005 // 00006 // Scorched3D is free software; you can redistribute it and/or modify 00007 // it under the terms of the GNU General Public License as published by 00008 // the Free Software Foundation; either version 2 of the License, or 00009 // (at your option) any later version. 00010 // 00011 // Scorched3D is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU General Public License 00017 // along with Scorched3D; if not, write to the Free Software 00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 //////////////////////////////////////////////////////////////////////////////// 00020 00021 #include <XML/XMLFile.h> 00022 #include <common/Defines.h> 00023 #include <tankai/TankAIStore.h> 00024 #include <tankai/TankAICurrent.h> 00025 #include <tankai/TankAIShallow.h> 00026 #include <tankai/TankAIRandom.h> 00027 #include <stdlib.h> 00028 00029 TankAIStore::TankAIStore() 00030 { 00031 00032 } 00033 00034 TankAIStore::~TankAIStore() 00035 { 00036 00037 } 00038 00039 void TankAIStore::clearAIs() 00040 { 00041 while (!ais_.empty()) 00042 { 00043 TankAI *ai = ais_.front(); 00044 ais_.pop_front(); 00045 delete ai; 00046 } 00047 } 00048 00049 bool TankAIStore::loadAIs(bool shallow) 00050 { 00051 // Load key definition file 00052 XMLFile file; 00053 if (!file.readFile(S3D::getDataFile("data/tankais.xml"))) 00054 { 00055 S3D::dialogMessage("TankAIStore", 00056 S3D::formatStringBuffer("Failed to parse \"%s\"\n%s", 00057 "data/tankais.xml", 00058 file.getParserError())); 00059 return false; 00060 } 00061 00062 // Check file exists 00063 if (!file.getRootNode()) 00064 { 00065 S3D::dialogMessage("TankAIStore", 00066 S3D::formatStringBuffer("Failed to find tank ai definition file \"%s\"", 00067 "data/tankais.xml")); 00068 return false; 00069 } 00070 00071 // Itterate all of the keys in the file 00072 std::list<XMLNode *>::iterator childrenItor; 00073 std::list<XMLNode *> &children = file.getRootNode()->getChildren(); 00074 for (childrenItor = children.begin(); 00075 childrenItor != children.end(); 00076 childrenItor++) 00077 { 00078 // Parse the ai entry 00079 XMLNode *currentNode = (*childrenItor); 00080 if (strcmp(currentNode->getName(), "ai")) 00081 { 00082 S3D::dialogMessage("TankAIStore", 00083 "Failed to find ai node"); 00084 return false; 00085 } 00086 00087 TankAI *computer = 0; 00088 if (shallow) computer = new TankAIShallow; 00089 else computer = new TankAICurrent; 00090 if (!computer->parseConfig(currentNode)) 00091 { 00092 return false; 00093 } 00094 00095 addAI(computer); 00096 } 00097 00098 // Add the random player 00099 { 00100 TankAIRandom *tankAI = new TankAIRandom; 00101 bool foundRandom = false; 00102 std::list<TankAI *>::iterator itor; 00103 for (itor = ais_.begin(); 00104 itor != ais_.end(); 00105 itor++) 00106 { 00107 TankAI *ai = (*itor); 00108 if (ai->availableForRandom()) 00109 { 00110 foundRandom = true; 00111 tankAI->addTankAI(ai); 00112 } 00113 } 00114 if (!foundRandom) 00115 { 00116 S3D::dialogMessage("TankAIStore", 00117 "No tank ais are marked availableforrandom"); 00118 return false; 00119 } 00120 00121 addAI(tankAI); 00122 } 00123 00124 return true; 00125 } 00126 00127 TankAI *TankAIStore::getAIByName(const char *name) 00128 { 00129 // Computers 00130 std::list<TankAI *>::iterator itor; 00131 for (itor = ais_.begin(); 00132 itor != ais_.end(); 00133 itor++) 00134 { 00135 if (!strcmp((*itor)->getName(), name)) return (*itor); 00136 } 00137 00138 return 0; 00139 } 00140 00141 void TankAIStore::addAI(TankAI *ai) 00142 { 00143 ais_.push_back(ai); 00144 }
1.5.3