Debug system v2 -- now, it supports debug levels, there's a global level, but overrid...
[dbw.git] / src / CMap.cpp
blobb60af5fd6c3be5fe3e3820dd1742c541aeb9b533
1 /*
2 Copyright © 2004 Parallel Realities
3 Copyright © 2007-2008 Kővágó Zoltán <DirtY.iCE.hu@gmail.com>
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include "CMap.h"
23 #include "CObjective.h"
24 #include "CParticle.h"
25 #include "CSpawnPoint.h"
26 #include "CSwitch.h"
27 #include "CTrain.h"
28 #include "console/console.hpp"
30 Map::Map()
32 clear();
34 for (int i = 0 ; i < 10 ; i++)
36 boss[i] = NULL;
41 void Map::clear()
43 fightingGaldov = false;
44 bossNextThink = 0;
46 foundItems = totalItems = 0;
47 foundMIAs = totalMIAs = 0;
48 requiredMIAs = 0;
50 limitLeft = limitUp = 0;
51 limitRight = ((MAPWIDTH - 40) * BRICKSIZE);
52 limitDown = ((MAPHEIGHT - 30) * BRICKSIZE);
54 strcpy(name, "");
56 for (int x = 0 ; x < MAPWIDTH ; x++)
57 for (int y = 0 ; y < MAPHEIGHT ; y++)
58 data[x][y] = 0;
60 for (int i = 0 ; i < 10 ; i++)
61 allowableEnemy[i] = NULL;
63 maxAllowableEnemies = 0;
65 trainList.clear();
66 itemList.clear();
67 bulletList.clear();
68 enemyList.clear();
69 miaList.clear();
70 obstacleList.clear();
71 particleList.clear();
72 switchList.clear();
73 spawnList.clear();
74 effectList.clear();
75 objectiveList.clear();
76 teleportList.clear();
77 lineList.clear();
78 trapList.clear();
80 remainingMinutes = 0;
81 remainingSeconds = 0;
83 waterLevel = requiredWaterLevel = -1;
84 isBossMission = false;
85 isIceLevel = false;
86 isBlizzardLevel = false;
87 isCavesTileset = false;
88 isGrasslandsTileset = false;
89 bossNextThink = 0;
90 mainBossPart = NULL;
91 doBossLevelAction = NULL;
92 bossEnergyMeterBit = 0;
94 for (int i = 0 ; i < 10 ; i++)
96 if (boss[i] != NULL)
98 delete boss[i];
100 boss[i] = NULL;
103 windPower = windChangeTime = 0;
106 void Map::destroy()
108 clear();
110 persistantList.clear();
113 bool Map::isPracticeMission()
115 if (strstr(name, "Practice"))
116 return true;
118 return false;
121 bool Map::isSolid(int x, int y)
123 if ((data[x][y] >= MAP_BREAKABLE) && (data[x][y] < MAP_DECORATION))
125 return true;
128 return false;
131 bool Map::isBreakable(int x, int y)
133 if ((data[x][y] >= MAP_BREAKABLE) && (data[x][y] <= MAP_BREAKABLE2))
135 return true;
138 return false;
141 bool Map::isNoReset(int x, int y)
143 if ((data[x][y] >= MAP_NORESET) && (data[x][y] < MAP_DECORATION))
145 return true;
148 return false;
151 bool Map::isLiquid(int x, int y)
153 if (data[x][y] == 0)
155 return false;
157 else if ((data[x][y] >= MAP_WATER) && (data[x][y] <= MAP_LAVA))
159 return true;
161 else if ((data[x][y] >= MAP_WATERANIM) && (data[x][y] <= MAP_LAVAANIM))
163 return true;
166 return false;
169 bool Map::isTopLayer(int x, int y)
171 if (data[x][y] >= MAP_TOPLAYER)
173 return true;
176 return false;
179 Persistant *Map::getPersistant(char *name)
181 Persistant *p = (Persistant*)persistantList.getHead();
183 while (p->next != NULL)
185 p = (Persistant*)p->next;
187 if (strcmp(p->stageName, name) == 0)
189 return p;
193 return NULL;
196 Persistant *Map::createPersistant(char *name)
198 Persistant *p = (Persistant*)persistantList.getHead();
200 while (p->next != NULL)
202 p = (Persistant*)p->next;
204 if (strcmp(p->stageName, name) == 0)
206 return p;
210 debug("pers", 1) << "Creating '" << name << "' in persistance list..." << std::endl;
212 p = new Persistant();
213 p->setName(name);
215 persistantList.add(p);
217 return p;
220 void Map::destroyPersistant(char *name)
222 Persistant *p = (Persistant*)persistantList.getHead();
224 while (p->next != NULL)
226 p = (Persistant*)p->next;
228 if (strcmp(p->stageName, name) == 0)
230 strcpy(p->stageName, "@none@");
231 p->clear();
232 return;
237 void Map::setName(char *name)
239 strcpy(this->name, name);
241 if (strstr(name, "BioMech"))
243 isBossMission = true;
246 if (strcmp(name, "Galdov") == 0)
248 isBossMission = true;
251 if (strcmp(name, "Final Battle") == 0)
253 isBossMission = true;
256 if (strstr(name, "Ice"))
258 isIceLevel = true;
261 if (strstr(name, "Arctic"))
263 isIceLevel = true;
264 isBlizzardLevel = true;
268 void Map::setClipping(int limitLeft, int limitRight, int limitUp, int limitDown)
270 if (limitLeft != -1) this->limitLeft = limitLeft;
271 if (limitRight != -1) this->limitRight = limitRight;
272 if (limitUp != -1) this->limitUp = limitUp;
273 if (limitDown != -1) this->limitDown = limitDown;
276 void Map::addTrain(char *name, int startX, int startY, int endX, int endY, int pause, bool atStart, bool active)
278 Train *train = new Train();
279 train->setName(name);
280 train->type = TR_TRAIN;
281 train->set(startX, startY, endX, endY, pause, atStart);
282 train->active = active;
284 train->width = 64;
285 train->height = 16;
287 if (pause == 0)
289 warning << "Train with 0 wait time added '" << name << "' (" << startX << ":" << startY << ")" << std::endl;
292 trainList.add(train);
295 void Map::addDoor(char *name, int type, int startX, int startY, int endX, int endY, bool active)
297 Train *train = new Train();
298 train->setName(name);
300 train->type = type;
302 train->set(startX, startY, endX, endY, 0, false);
303 train->active = active;
305 if (type < TR_SLIDEDOOR)
307 train->width = 16;
308 train->height = 64;
310 else
312 train->width = 64;
313 train->height = 16;
316 trainList.add(train);
319 void Map::addSwitch(char *name, char *linkName, char *requiredObjectName, char *activateMessage, int type, int x, int y, bool activated)
321 Switch *swt = new Switch();
322 swt->set(name, linkName, requiredObjectName, activateMessage, type, x, y, activated);
324 switchList.add(swt);
327 void Map::addItem(Entity *item)
329 itemList.add(item);
332 void Map::addBullet(Entity *bullet)
334 bulletList.add(bullet);
337 void Map::addParticle(float x, float y, float dx, float dy, int health, int color, Sprite *sprite, int flags)
339 Particle *particle = new Particle();
340 particle->set(x, y, dx, dy, color, health, flags);
341 particle->setSprite(sprite);
343 particleList.add(particle);
346 void Map::addEnemy(Entity *enemy)
348 enemyList.add(enemy);
351 void Map::addMIA(Entity *mia)
353 miaList.add(mia);
356 void Map::addObstacle(Entity *obstacle)
358 obstacleList.add(obstacle);
361 void Map::addSpawnPoint(char *name, int x, int y, int type, int subtype, int min, int max, bool active)
363 SpawnPoint *spawnPoint = new SpawnPoint();
364 spawnPoint->create(name, x, y, type, subtype, min, max, active);
366 spawnList.add(spawnPoint);
369 void Map::addEffect(Effect *effect)
371 effectList.add(effect);
374 void Map::addObjective(char *description, char *target, int targetValue, bool required)
376 Objective *objective = new Objective(description, target, targetValue, required);
378 objectiveList.add(objective);
381 void Map::addTeleporter(Teleporter *teleporter)
383 teleportList.add(teleporter);
386 void Map::addLineDef(LineDef *lineDef)
388 lineList.add(lineDef);
391 void Map::addTrap(Trap *trap)
393 trapList.add(trap);
396 void Map::evalTileset(char *baseDir)
398 if (strstr(baseDir, "caves"))
400 isCavesTileset = true;
402 else if (strstr(baseDir, "grasslands"))
404 isGrasslandsTileset = true;
408 void Map::killAllEnemies()
410 Entity *enemy = (Entity*)enemyList.getHead()->next;
412 while (enemy != NULL)
414 enemy->health = -1;
416 enemy = (Entity*)enemy->next;
420 void Map::setAllowableEnemy(Entity *enemy)
422 for (int i = 0 ; i < 10 ; i++)
424 if (allowableEnemy[i] == NULL)
426 allowableEnemy[i] = enemy;
427 maxAllowableEnemies = i + 1;
428 return;
432 warning << "Can't add anymore spawnable enemies to list!" << std::endl;
435 char *Map::getSpawnableEnemy(int i)
437 if (allowableEnemy[i] == NULL)
438 return NULL;
440 return allowableEnemy[i]->name;
443 char *Map::getSpawnableEnemy()
445 if (maxAllowableEnemies == 0)
447 printf("ERROR: No enemy spawn list defined for map '%s'!! Please report this Error!\n", name);
448 exit(1);
451 return allowableEnemy[rand() % maxAllowableEnemies]->name;
454 void Map::getRandomEntityPosition(int *x, int *y)
456 Entity *ent = (Entity*)miaList.getHead();
458 while (ent->next != NULL)
460 ent = (Entity*)ent->next;
462 if ((rand() % 5) == 0)
464 *x = (int)ent->x;
465 *y = (int)ent->y;
466 return;
470 ent = (Entity*)enemyList.getHead();
472 while (ent->next != NULL)
474 ent = (Entity*)ent->next;
476 if ((rand() % 5) == 0)
478 *x = (int)ent->x;
479 *y = (int)ent->y;
480 return;
484 ent = (Entity*)itemList.getHead();
486 while (ent->next != NULL)
488 ent = (Entity*)ent->next;
490 if ((rand() % 5) == 0)
492 *x = (int)ent->x;
493 *y = (int)ent->y;
494 return;
499 void Map::setMainBossPart(Boss *boss)
501 mainBossPart = boss;
503 if (mainBossPart != NULL)
505 bossEnergyMeterBit = 200;
506 bossEnergyMeterBit /= boss->maxHealth;