[7626] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Damian 'Mozork' Frick |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "LevelInfo.h" |
---|
| 30 | |
---|
| 31 | #include <sstream> |
---|
| 32 | #include <vector> |
---|
| 33 | |
---|
| 34 | #include "util/SubString.h" |
---|
| 35 | #include "core/CoreIncludes.h" |
---|
| 36 | #include "core/XMLPort.h" |
---|
| 37 | |
---|
| 38 | #include "LevelManager.h" |
---|
| 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
| 42 | |
---|
| 43 | // LevelInfoItem |
---|
[7638] | 44 | |
---|
| 45 | //! The list of allowed tags. |
---|
| 46 | /*static*/ std::set<std::string> LevelInfoItem::possibleTags_s = std::set<std::string>(); |
---|
[7626] | 47 | |
---|
| 48 | /** |
---|
| 49 | @brief |
---|
| 50 | Default constructor. |
---|
| 51 | */ |
---|
| 52 | LevelInfoItem::LevelInfoItem() |
---|
| 53 | { |
---|
| 54 | |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | /** |
---|
| 58 | @brief |
---|
| 59 | Constructor. Initializes the object. |
---|
| 60 | @param name |
---|
| 61 | The name of the Level. |
---|
| 62 | @param filename |
---|
| 63 | The XML-filename of the Level. |
---|
| 64 | */ |
---|
| 65 | LevelInfoItem::LevelInfoItem(const std::string& name, const std::string filename) |
---|
| 66 | { |
---|
| 67 | this->setName(name); |
---|
| 68 | this->setXMLFilename(filename); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | @brief |
---|
| 73 | Destructor. |
---|
| 74 | */ |
---|
| 75 | LevelInfoItem::~LevelInfoItem() |
---|
| 76 | { |
---|
| 77 | |
---|
| 78 | } |
---|
[7645] | 79 | |
---|
| 80 | /** |
---|
| 81 | @brief |
---|
| 82 | Initialize the set of allowed tags. |
---|
| 83 | */ |
---|
| 84 | /*static*/ void LevelInfoItem::initializeTags(void) |
---|
[7638] | 85 | { |
---|
| 86 | if(!LevelInfoItem::initialized_s) |
---|
| 87 | { |
---|
| 88 | LevelInfoItem::possibleTags_s.insert("test"); |
---|
| 89 | LevelInfoItem::possibleTags_s.insert("showcase"); |
---|
| 90 | LevelInfoItem::possibleTags_s.insert("tutorial"); |
---|
[7645] | 91 | LevelInfoItem::possibleTags_s.insert("presentation"); |
---|
[9016] | 92 | LevelInfoItem::possibleTags_s.insert("mission"); |
---|
| 93 | LevelInfoItem::possibleTags_s.insert("gametype"); |
---|
| 94 | LevelInfoItem::possibleTags_s.insert("minigame"); |
---|
[9208] | 95 | LevelInfoItem::possibleTags_s.insert("shipselection"); |
---|
[7638] | 96 | } |
---|
| 97 | } |
---|
[7626] | 98 | |
---|
| 99 | /** |
---|
| 100 | @brief |
---|
| 101 | Set the tags the Level is tagged with. |
---|
| 102 | @param tags |
---|
| 103 | A comma-seperated string of all the tags to be set. |
---|
| 104 | */ |
---|
| 105 | void LevelInfoItem::setTags(const std::string& tags) |
---|
| 106 | { |
---|
| 107 | SubString substr = SubString(tags, ",", " "); // Split the string into tags. |
---|
| 108 | const std::vector<std::string>& strings = substr.getAllStrings(); |
---|
| 109 | for(std::vector<std::string>::const_iterator it = strings.begin(); it != strings.end(); it++) |
---|
| 110 | this->addTag(*it, false); |
---|
| 111 | |
---|
| 112 | this->tagsUpdated(); |
---|
| 113 | } |
---|
[9208] | 114 | /** |
---|
| 115 | @brief |
---|
| 116 | Set the starting ship models of the level |
---|
| 117 | @param tags |
---|
| 118 | A comma-seperated string of all the allowed ship models for the shipselection. |
---|
| 119 | */ |
---|
| 120 | void LevelInfoItem::setShips(const std::string& ships) |
---|
| 121 | { |
---|
| 122 | SubString substr = SubString(ships, ",", " "); // Split the string into tags. |
---|
| 123 | const std::vector<std::string>& strings = substr.getAllStrings(); |
---|
| 124 | for(std::vector<std::string>::const_iterator it = strings.begin(); it != strings.end(); it++) |
---|
| 125 | this->addShip(*it, false); |
---|
[7626] | 126 | |
---|
[9208] | 127 | this->shipsUpdated(); |
---|
| 128 | } |
---|
| 129 | |
---|
[7626] | 130 | /** |
---|
| 131 | @brief |
---|
| 132 | Add a tag to the set of tags the Level is tagged with. |
---|
| 133 | @param tag |
---|
| 134 | The tag to be added. |
---|
| 135 | @param update |
---|
| 136 | Whether the comma-seperated string of all tags should be updated. Default is true. |
---|
| 137 | @return |
---|
| 138 | Returns true if the tag was successfully added, if the tag was already present it returns false. |
---|
| 139 | */ |
---|
| 140 | bool LevelInfoItem::addTag(const std::string& tag, bool update) |
---|
| 141 | { |
---|
[7638] | 142 | if(!this->validateTag(tag)) |
---|
| 143 | { |
---|
[8858] | 144 | orxout(internal_warning) << "Bad LevelInfo tag '" << tag << "' in " << this->getXMLFilename() << ". Ignoring..." << endl; |
---|
[7638] | 145 | return false; |
---|
| 146 | } |
---|
[7639] | 147 | bool success = this->tags_.insert(*LevelInfoItem::possibleTags_s.find(tag)).second; |
---|
[7626] | 148 | if(update && success) |
---|
| 149 | this->tagsUpdated(); |
---|
| 150 | return success; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | /** |
---|
| 154 | @brief |
---|
[9208] | 155 | Add a ship model to allowed models for the shipselection |
---|
| 156 | @param ship |
---|
| 157 | The ship model to be added. |
---|
| 158 | @param update |
---|
| 159 | Whether the comma-seperated string of all ship models should be updated. Default is true. |
---|
| 160 | @return |
---|
| 161 | Returns true if the ship was successfully added, if the ship was already present it returns false. |
---|
| 162 | */ |
---|
| 163 | bool LevelInfoItem::addShip(const std::string& ship, bool update) |
---|
| 164 | { |
---|
| 165 | bool success = this->ships_.insert(ship).second; |
---|
| 166 | if(update && success) |
---|
| 167 | this->shipsUpdated(); |
---|
| 168 | |
---|
| 169 | return success; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | |
---|
| 173 | /** |
---|
| 174 | @brief |
---|
[7626] | 175 | Updates the comma-seperated string of all tags, if the set of tags has changed. |
---|
| 176 | */ |
---|
| 177 | void LevelInfoItem::tagsUpdated(void) |
---|
| 178 | { |
---|
| 179 | std::stringstream stream; |
---|
| 180 | std::set<std::string>::iterator temp; |
---|
| 181 | for(std::set<std::string>::iterator it = this->tags_.begin(); it != this->tags_.end(); ) |
---|
| 182 | { |
---|
| 183 | temp = it; |
---|
| 184 | if(++it == this->tags_.end()) // If this is the last tag we don't add a comma. |
---|
| 185 | stream << *temp; |
---|
| 186 | else |
---|
| 187 | stream << *temp << ", "; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | this->tagsString_ = std::string(stream.str()); |
---|
| 191 | } |
---|
| 192 | |
---|
[9208] | 193 | /** |
---|
| 194 | @brief |
---|
| 195 | Updates the comma-seperated string of all ships, if the set of tags has changed. |
---|
| 196 | */ |
---|
| 197 | void LevelInfoItem::shipsUpdated(void) |
---|
| 198 | { |
---|
| 199 | std::stringstream stream; |
---|
| 200 | std::set<std::string>::iterator temp; |
---|
| 201 | for(std::set<std::string>::iterator it = this->ships_.begin(); it != this->ships_.end(); ) |
---|
| 202 | { |
---|
| 203 | temp = it; |
---|
| 204 | if(++it == this->ships_.end()) // If this is the last ship we don't add a comma. |
---|
| 205 | stream << *temp; |
---|
| 206 | else |
---|
| 207 | stream << *temp << ", "; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | this->startingShipsString_ = std::string(stream.str()); |
---|
| 211 | } |
---|
[7626] | 212 | // LevelInfo |
---|
| 213 | |
---|
| 214 | CreateFactory(LevelInfo); |
---|
| 215 | |
---|
| 216 | /** |
---|
| 217 | @brief |
---|
| 218 | |
---|
| 219 | @param creator |
---|
| 220 | The creator of this object. |
---|
| 221 | */ |
---|
| 222 | LevelInfo::LevelInfo(BaseObject* creator) : BaseObject(creator) |
---|
| 223 | { |
---|
| 224 | RegisterObject(LevelInfo); |
---|
| 225 | |
---|
| 226 | this->xmlfilename_ = this->getFilename(); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | /** |
---|
| 230 | @brief |
---|
| 231 | Destructor. |
---|
| 232 | */ |
---|
| 233 | LevelInfo::~LevelInfo() |
---|
| 234 | { |
---|
| 235 | |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | /** |
---|
| 239 | @brief |
---|
| 240 | Creates a LevelInfo object through XML. |
---|
| 241 | */ |
---|
| 242 | void LevelInfo::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 243 | { |
---|
| 244 | SUPER(LevelInfo, XMLPort, xmlelement, mode); |
---|
| 245 | |
---|
| 246 | XMLPortParam(LevelInfo, "description", setDescription, getDescription, xmlelement, mode); |
---|
[9016] | 247 | XMLPortParam(LevelInfo, "screenshot", setScreenshot, getScreenshot, xmlelement, mode); |
---|
[7626] | 248 | XMLPortParam(LevelInfo, "tags", setTags, getTags, xmlelement, mode); |
---|
[9208] | 249 | XMLPortParam(LevelInfo, "startingships", setShips, getShips, xmlelement, mode); |
---|
[7626] | 250 | } |
---|
| 251 | |
---|
| 252 | /** |
---|
| 253 | @brief |
---|
| 254 | Copies the contents of this LevelInfo object to a new LevelInfoItem object. |
---|
| 255 | This is needed, because a LeveInfo object is only created within the scope of the XML-file it is loaded with and is destroyed once that is unloaded. |
---|
| 256 | @return |
---|
| 257 | Returns a new LevelInfoItem with the same contents as the LevelInfo object. |
---|
| 258 | */ |
---|
| 259 | LevelInfoItem* LevelInfo::copy(void) |
---|
| 260 | { |
---|
| 261 | LevelInfoItem* info = new LevelInfoItem(this->BaseObject::getName(), this->getXMLFilename()); |
---|
| 262 | info->setDescription(this->getDescription()); |
---|
[9016] | 263 | info->setScreenshot(this->getScreenshot()); |
---|
[7626] | 264 | info->setTags(this->getTags()); |
---|
[9208] | 265 | info->setShips(this->getShips()); |
---|
[7626] | 266 | return info; |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | } |
---|
| 270 | |
---|