| 1 |
// ____ _ __ |
| 2 |
// / __ )____ _____ | | / /___ ___________ |
| 3 |
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/ |
| 4 |
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ ) |
| 5 |
// /_____/\____/____/ |__/|__/\__,_/_/ /____/ |
| 6 |
// |
| 7 |
// A futuristic real-time strategy game. |
| 8 |
// This file is part of Bos Wars. |
| 9 |
// |
| 10 |
/**@name minimap.cpp - The patch manager. */ |
| 11 |
// |
| 12 |
// (c) Copyright 2008-2010 by Jimmy Salmon |
| 13 |
// |
| 14 |
// This program is free software; you can redistribute it and/or modify |
| 15 |
// it under the terms of the GNU General Public License as published by |
| 16 |
// the Free Software Foundation; only version 2 of the License. |
| 17 |
// |
| 18 |
// This program is distributed in the hope that it will be useful, |
| 19 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
// GNU General Public License for more details. |
| 22 |
// |
| 23 |
// You should have received a copy of the GNU General Public License |
| 24 |
// along with this program; if not, write to the Free Software |
| 25 |
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 26 |
// 02111-1307, USA. |
| 27 |
// |
| 28 |
|
| 29 |
//@{ |
| 30 |
|
| 31 |
/*---------------------------------------------------------------------------- |
| 32 |
-- Includes |
| 33 |
----------------------------------------------------------------------------*/ |
| 34 |
|
| 35 |
#include "stratagus.h" |
| 36 |
#include "patch_manager.h" |
| 37 |
#include "patch_type.h" |
| 38 |
#include "patch.h" |
| 39 |
#include "iolib.h" |
| 40 |
#include "map.h" |
| 41 |
#include "script.h" |
| 42 |
|
| 43 |
#include <algorithm> |
| 44 |
#include <sstream> |
| 45 |
#include <iomanip> |
| 46 |
|
| 47 |
/*---------------------------------------------------------------------------- |
| 48 |
-- Functions |
| 49 |
----------------------------------------------------------------------------*/ |
| 50 |
|
| 51 |
|
| 52 |
CPatchManager::CPatchManager() : |
| 53 |
loadedAll(false) |
| 54 |
{ |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
CPatchManager::~CPatchManager() |
| 59 |
{ |
| 60 |
std::map<std::string, CPatchType *>::iterator i; |
| 61 |
|
| 62 |
for (i = this->patchTypesMap.begin(); i != this->patchTypesMap.end(); ++i) { |
| 63 |
delete i->second; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
void |
| 69 |
CPatchManager::updateMapFlags(int x1, int y1, int x2, int y2) |
| 70 |
{ |
| 71 |
// The patch editor doesn't have a fully initialized map |
| 72 |
if (!Map.Fields) { |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
x1 = std::max(x1, 0); |
| 77 |
y1 = std::max(y1, 0); |
| 78 |
x2 = std::min(x2, Map.Info.MapWidth - 1); |
| 79 |
y2 = std::min(y2, Map.Info.MapHeight - 1); |
| 80 |
|
| 81 |
for (int j = y1; j <= y2; ++j) { |
| 82 |
for (int i = x1; i <= x2; ++i) { |
| 83 |
int offsetX, offsetY; |
| 84 |
CPatch *patch = this->getPatch(i, j, &offsetX, &offsetY); |
| 85 |
unsigned short flags; |
| 86 |
|
| 87 |
if (patch) { |
| 88 |
flags = patch->getType()->getFlag(offsetX, offsetY); |
| 89 |
} else { |
| 90 |
// No patch here. Allow this so that |
| 91 |
// authors can test their incomplete maps. |
| 92 |
flags = MapFieldUnpassable | MapFieldNormalSpeed; |
| 93 |
} |
| 94 |
|
| 95 |
Map.Field(i, j)->Flags = (flags & MapFieldPatchMask) |
| 96 |
| (Map.Field(i, j)->Flags & ~MapFieldPatchMask); |
| 97 |
|
| 98 |
Map.Field(i, j)->Cost = 1 << (flags & MapFieldSpeedMask); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
CPatch * |
| 104 |
CPatchManager::add(const std::string &typeName, int x, int y) |
| 105 |
{ |
| 106 |
CPatchType *type = this->patchTypesMap[typeName]; |
| 107 |
Assert(type); |
| 108 |
if (!type) { |
| 109 |
fprintf(stderr, "Patch not found: %s\n", typeName.c_str()); |
| 110 |
return NULL; |
| 111 |
} |
| 112 |
|
| 113 |
CPatch *patch = new CPatch(type, x, y); |
| 114 |
this->patches.push_back(patch); |
| 115 |
|
| 116 |
updateMapFlags(x, y, x + type->getTileWidth() - 1, y + type->getTileHeight() - 1); |
| 117 |
|
| 118 |
return patch; |
| 119 |
} |
| 120 |
|
| 121 |
void |
| 122 |
CPatchManager::remove(CPatch *patch) |
| 123 |
{ |
| 124 |
this->patches.remove(patch); |
| 125 |
this->removedPatches.push_back(patch); |
| 126 |
|
| 127 |
updateMapFlags(patch->getX(), patch->getY(), |
| 128 |
patch->getX() + patch->getType()->getTileWidth(), |
| 129 |
patch->getY() + patch->getType()->getTileHeight()); |
| 130 |
} |
| 131 |
|
| 132 |
void |
| 133 |
CPatchManager::move(CPatch *patch, int x, int y) |
| 134 |
{ |
| 135 |
if (x != patch->getX() || y != patch->getY()) { |
| 136 |
int x1, y1, x2, y2; |
| 137 |
|
| 138 |
if (x < patch->getX()) { |
| 139 |
x1 = x; |
| 140 |
x2 = patch->getX() + patch->getType()->getTileWidth() - 1; |
| 141 |
} else { |
| 142 |
x1 = patch->getX(); |
| 143 |
x2 = x + patch->getType()->getTileWidth() - 1; |
| 144 |
} |
| 145 |
if (y < patch->getY()) { |
| 146 |
y1 = y; |
| 147 |
y2 = patch->getY() + patch->getType()->getTileHeight() - 1; |
| 148 |
} else { |
| 149 |
y1 = patch->getY(); |
| 150 |
y2 = y + patch->getType()->getTileHeight() - 1; |
| 151 |
} |
| 152 |
|
| 153 |
patch->setPos(x, y); |
| 154 |
|
| 155 |
updateMapFlags(x1, y1, x2, y2); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
void |
| 160 |
CPatchManager::moveToTop(CPatch *patch) |
| 161 |
{ |
| 162 |
this->patches.remove(patch); |
| 163 |
this->patches.push_back(patch); |
| 164 |
|
| 165 |
updateMapFlags(patch->getX(), patch->getY(), |
| 166 |
patch->getX() + patch->getType()->getTileWidth() - 1, |
| 167 |
patch->getY() + patch->getType()->getTileHeight() - 1); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
void |
| 172 |
CPatchManager::moveToBottom(CPatch *patch) |
| 173 |
{ |
| 174 |
this->patches.remove(patch); |
| 175 |
this->patches.push_front(patch); |
| 176 |
|
| 177 |
updateMapFlags(patch->getX(), patch->getY(), |
| 178 |
patch->getX() + patch->getType()->getTileWidth() - 1, |
| 179 |
patch->getY() + patch->getType()->getTileHeight() - 1); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
CPatch * |
| 184 |
CPatchManager::getPatch(int x, int y, int *xOffset, int *yOffset) const |
| 185 |
{ |
| 186 |
std::list<CPatch *>::const_reverse_iterator i; |
| 187 |
std::list<CPatch *>::const_reverse_iterator rend; |
| 188 |
|
| 189 |
// Search the patches from top to bottom |
| 190 |
rend = this->patches.rend(); |
| 191 |
for (i = this->patches.rbegin(); i != rend; ++i) { |
| 192 |
const CPatch *patch = *i; |
| 193 |
int patchX = patch->getX(); |
| 194 |
int patchY = patch->getY(); |
| 195 |
CPatchType *patchType = patch->getType(); |
| 196 |
|
| 197 |
// See if the patch is at location x,y |
| 198 |
if (patchX <= x && x < patchX + patchType->getTileWidth() && |
| 199 |
patchY <= y && y < patchY + patchType->getTileHeight()) { |
| 200 |
int xPatchOffset = x - patchX; |
| 201 |
int yPatchOffset = y - patchY; |
| 202 |
unsigned short flag = patchType->getFlag(xPatchOffset, yPatchOffset); |
| 203 |
|
| 204 |
// Make sure the patch tile isn't transparent |
| 205 |
if (!(flag & MapFieldTransparent)) { |
| 206 |
if (xOffset != NULL && yOffset != NULL) { |
| 207 |
*xOffset = xPatchOffset; |
| 208 |
*yOffset = yPatchOffset; |
| 209 |
} |
| 210 |
|
| 211 |
return *i; |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
return NULL; |
| 216 |
} |
| 217 |
|
| 218 |
const std::list<CPatch *> & |
| 219 |
CPatchManager::getPatches() const |
| 220 |
{ |
| 221 |
return this->patches; |
| 222 |
} |
| 223 |
|
| 224 |
std::vector<std::string> |
| 225 |
CPatchManager::getPatchTypeNames() const |
| 226 |
{ |
| 227 |
std::vector<std::string> names; |
| 228 |
std::map<std::string, CPatchType *>::const_iterator i; |
| 229 |
|
| 230 |
for (i = this->patchTypesMap.begin(); i != this->patchTypesMap.end(); ++i) { |
| 231 |
names.push_back(i->second->getName()); |
| 232 |
} |
| 233 |
|
| 234 |
return names; |
| 235 |
} |
| 236 |
|
| 237 |
std::vector<std::string> |
| 238 |
CPatchManager::getPatchTypeNamesUsingGraphic( |
| 239 |
const std::string &graphicFile) const |
| 240 |
{ |
| 241 |
std::vector<std::string> names; |
| 242 |
std::map<std::string, CPatchType *>::const_iterator i; |
| 243 |
|
| 244 |
for (i = this->patchTypesMap.begin(); i != this->patchTypesMap.end(); ++i) { |
| 245 |
if (i->second->getFile() == graphicFile) { |
| 246 |
names.push_back(i->second->getName()); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
return names; |
| 251 |
} |
| 252 |
|
| 253 |
static bool themeCompare(const std::string &a, const std::string &b) |
| 254 |
{ |
| 255 |
return strcasecmp(a.c_str(), b.c_str()) < 0; |
| 256 |
} |
| 257 |
|
| 258 |
std::vector<std::string> |
| 259 |
CPatchManager::getPatchTypeThemes() const |
| 260 |
{ |
| 261 |
std::vector<std::string> themes; |
| 262 |
|
| 263 |
// Get all themes |
| 264 |
for (std::map<std::string, CPatchType *>::const_iterator i = this->patchTypesMap.begin(); i != this->patchTypesMap.end(); ++i) { |
| 265 |
const std::string &theme = i->second->getTheme(); |
| 266 |
if (!theme.empty()) |
| 267 |
{ |
| 268 |
themes.push_back(theme); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
// Sort |
| 273 |
std::sort(themes.begin(), themes.end(), themeCompare); |
| 274 |
|
| 275 |
// Remove duplicates |
| 276 |
std::vector<std::string>::iterator it; |
| 277 |
it = std::unique(themes.begin(), themes.end()); |
| 278 |
themes.resize(it - themes.begin()); |
| 279 |
|
| 280 |
return themes; |
| 281 |
} |
| 282 |
|
| 283 |
void |
| 284 |
CPatchManager::load() |
| 285 |
{ |
| 286 |
std::list<CPatch *>::iterator i; |
| 287 |
for (i = this->patches.begin(); i != this->patches.end(); ++i) { |
| 288 |
(*i)->getType()->load(); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
void |
| 293 |
CPatchManager::loadAll() |
| 294 |
{ |
| 295 |
std::map<std::string, CPatchType *>::const_iterator i; |
| 296 |
|
| 297 |
for (i = this->patchTypesMap.begin(); i != this->patchTypesMap.end(); ++i) { |
| 298 |
i->second->load(); |
| 299 |
} |
| 300 |
|
| 301 |
loadedAll = true; |
| 302 |
} |
| 303 |
|
| 304 |
static void clearPatches(std::list<CPatch *> &patches) |
| 305 |
{ |
| 306 |
std::list<CPatch *>::iterator i; |
| 307 |
for (i = patches.begin(); i != patches.end(); ++i) { |
| 308 |
(*i)->getType()->clean(); |
| 309 |
delete *i; |
| 310 |
} |
| 311 |
patches.clear(); |
| 312 |
} |
| 313 |
|
| 314 |
void |
| 315 |
CPatchManager::clear() |
| 316 |
{ |
| 317 |
clearPatches(this->patches); |
| 318 |
clearPatches(this->removedPatches); |
| 319 |
|
| 320 |
std::map<std::string, CPatchType *>::iterator i; |
| 321 |
|
| 322 |
if (loadedAll) { |
| 323 |
for (i = this->patchTypesMap.begin(); i != this->patchTypesMap.end(); ++i) { |
| 324 |
i->second->clean(); |
| 325 |
} |
| 326 |
loadedAll = false; |
| 327 |
} |
| 328 |
|
| 329 |
i = this->patchTypesMap.begin(); |
| 330 |
while(i != this->patchTypesMap.end()) { |
| 331 |
std::map<std::string, CPatchType *>::iterator p = i; |
| 332 |
++i; |
| 333 |
if (p->second->isCustomPatch()) { |
| 334 |
delete p->second; |
| 335 |
this->patchTypesMap.erase(p); |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
CPatchType * |
| 343 |
CPatchManager::newPatchType(const std::string &name, const std::string &file, |
| 344 |
int tileWidth, int tileHeight, int *flags, const std::string &theme) |
| 345 |
{ |
| 346 |
unsigned short *newFlags = new unsigned short[tileWidth * tileHeight]; |
| 347 |
for (int i = 0; i < tileWidth * tileHeight; ++i) { |
| 348 |
newFlags[i] = flags[i]; |
| 349 |
} |
| 350 |
|
| 351 |
CPatchType *patchType = newPatchType(name, file, tileWidth, tileHeight, newFlags, theme); |
| 352 |
|
| 353 |
delete[] newFlags; |
| 354 |
return patchType; |
| 355 |
} |
| 356 |
|
| 357 |
CPatchType * |
| 358 |
CPatchManager::newPatchType(const std::string &name, const std::string &file, |
| 359 |
int tileWidth, int tileHeight, unsigned short *flags, const std::string &theme) |
| 360 |
{ |
| 361 |
// Loading a game might redefine a patch, just ignore it |
| 362 |
if (this->patchTypesMap[name] != NULL) { |
| 363 |
return this->patchTypesMap[name]; |
| 364 |
} |
| 365 |
|
| 366 |
CPatchType *patchType = new CPatchType(name, file, tileWidth, tileHeight, flags, !CclInConfigFile, theme); |
| 367 |
this->patchTypesMap[name] = patchType; |
| 368 |
|
| 369 |
return patchType; |
| 370 |
} |
| 371 |
|
| 372 |
CPatchType * |
| 373 |
CPatchManager::getPatchType(const std::string &name) |
| 374 |
{ |
| 375 |
return this->patchTypesMap[name]; |
| 376 |
} |
| 377 |
|
| 378 |
std::string |
| 379 |
CPatchManager::savePatches(bool patchesOnly) const |
| 380 |
{ |
| 381 |
std::map<std::string, bool> patchTypeSaved; |
| 382 |
std::list<CPatch *>::const_iterator i; |
| 383 |
std::ostringstream ostr; |
| 384 |
|
| 385 |
for (i = this->patches.begin(); i != this->patches.end(); ++i) { |
| 386 |
const std::string &name = (*i)->getType()->getName(); |
| 387 |
|
| 388 |
if (!patchesOnly && !patchTypeSaved[name]) { |
| 389 |
ostr << this->savePatchType((*i)->getType()); |
| 390 |
patchTypeSaved[name] = true; |
| 391 |
} |
| 392 |
|
| 393 |
ostr << "patch(\"" << name << "\", " |
| 394 |
<< (*i)->getX() << ", " << (*i)->getY() << ")\n"; |
| 395 |
} |
| 396 |
|
| 397 |
return ostr.str(); |
| 398 |
} |
| 399 |
|
| 400 |
std::string |
| 401 |
CPatchManager::savePatchType(CPatchType *patchType) const |
| 402 |
{ |
| 403 |
std::ostringstream ostr; |
| 404 |
|
| 405 |
ostr << "patchType(\"" << patchType->getName() << "\", \"" |
| 406 |
<< patchType->getGraphic()->File << "\", " |
| 407 |
<< patchType->getTileWidth() << ", " |
| 408 |
<< patchType->getTileHeight() << ", {\n"; |
| 409 |
|
| 410 |
for (int j = 0; j < patchType->getTileHeight(); ++j) { |
| 411 |
for (int i = 0; i < patchType->getTileWidth(); ++i) { |
| 412 |
std::ostringstream flag; |
| 413 |
flag << " 0x" |
| 414 |
<< std::hex << std::setw(4) << std::setfill('0') |
| 415 |
<< patchType->getFlag(i, j); |
| 416 |
ostr << flag.str() << ","; |
| 417 |
} |
| 418 |
ostr << "\n"; |
| 419 |
} |
| 420 |
|
| 421 |
ostr << "})\n"; |
| 422 |
|
| 423 |
return ostr.str(); |
| 424 |
} |
| 425 |
|
| 426 |
bool |
| 427 |
CPatchManager::computePatchSize(const std::string &graphicFile, |
| 428 |
int *width, int *height) const |
| 429 |
{ |
| 430 |
CGraphic *graphic = CGraphic::New(graphicFile); |
| 431 |
int graphicWidth = 0; |
| 432 |
int graphicHeight = 0; |
| 433 |
bool ok = graphic->LoadGraphicSize(&graphicWidth, &graphicHeight); |
| 434 |
if (ok) { |
| 435 |
*width = (graphicWidth + TileSizeX - 1) / TileSizeX; |
| 436 |
*height = (graphicHeight + TileSizeY - 1) / TileSizeY; |
| 437 |
} else { |
| 438 |
*width = 0; |
| 439 |
*height = 0; |
| 440 |
} |
| 441 |
CGraphic::Free(graphic); |
| 442 |
return ok; |
| 443 |
} |
| 444 |
|
| 445 |
//@} |