#define VCMI_DLL #include "../stdafx.h" #include "CSpellHandler.h" #include "CLodHandler.h" #include "CSoundBase.h" #include "../lib/VCMI_Lib.h" #include #include extern CLodHandler *bitmaph; /* * CSpellHandler.cpp, part of VCMI engine * * Authors: listed in file AUTHORS in main folder * * License: GNU General Public License v2.0 or later * Full text of license available in license.txt file, in main folder * */ namespace SRSLPraserHelpers { static int XYToHex(int x, int y) { return x + 17 * y; } static int XYToHex(std::pair xy) { return XYToHex(xy.first, xy.second); } static int hexToY(int battleFieldPosition) { return battleFieldPosition/17; } static int hexToX(int battleFieldPosition) { int pos = battleFieldPosition - hexToY(battleFieldPosition) * 17; return pos; } static std::pair hexToPair(int battleFieldPosition) { return std::make_pair(hexToX(battleFieldPosition), hexToY(battleFieldPosition)); } //moves hex by one hex in given direction //0 - left top, 1 - right top, 2 - right, 3 - right bottom, 4 - left bottom, 5 - left static std::pair gotoDir(int x, int y, int direction) { switch(direction) { case 0: //top left return std::make_pair(y%2 ? x-1 : x, y-1); case 1: //top right return std::make_pair(y%2 ? x : x+1, y-1); case 2: //right return std::make_pair(x+1, y); case 3: //right bottom return std::make_pair(y%2 ? x : x+1, y+1); case 4: //left bottom return std::make_pair(y%2 ? x-1 : x, y+1); case 5: //left return std::make_pair(x-1, y); default: throw std::string("Disaster: wrong direction in SRSLPraserHelpers::gotoDir!\n"); } } static std::pair gotoDir(std::pair xy, int direction) { return gotoDir(xy.first, xy.second, direction); } static bool isGoodHex(std::pair xy) { return xy.first >=0 && xy.first < 17 && xy.second >= 0 && xy.second < 11; } //helper fonction for std::set CSpell::rangeInHexes(unsigned int centralHex, ui8 schoolLvl ) const static std::set getInRange(unsigned int center, int low, int high) { std::set ret; if(low == 0) { ret.insert(center); } std::pair mainPointForLayer[6]; //A, B, C, D, E, F points for(int b=0; b<6; ++b) mainPointForLayer[b] = hexToPair(center); for(int it=1; it<=high; ++it) //it - distance to the center { for(int b=0; b<6; ++b) mainPointForLayer[b] = gotoDir(mainPointForLayer[b], b); if(it>=low) { std::pair curHex; //adding lines (A-b, B-c, C-d, etc) for(int v=0; v<6; ++v) { curHex = mainPointForLayer[v]; for(int h=0; h=low) } return ret; } } using namespace SRSLPraserHelpers; CSpellHandler::CSpellHandler() { VLC->spellh = this; } std::set CSpell::rangeInHexes(unsigned int centralHex, ui8 schoolLvl ) const { std::set ret; std::string rng = range[schoolLvl] + ','; //copy + artificial comma for easier handling if(rng.size() >= 1 && rng[0] != 'X') //there is at lest one hex in range { std::string number1, number2; int beg, end; bool readingFirst = true; for(int it=0; it curLayer; if(readingFirst) { curLayer = getInRange(centralHex, beg, beg); } else { curLayer = getInRange(centralHex, beg, end); readingFirst = true; } //adding abtained hexes for(std::set::iterator it = curLayer.begin(); it != curLayer.end(); ++it) { ret.insert(*it); } } else if(rng[it] == '-') //dash { beg = atoi(number1.c_str()); number1 = ""; readingFirst = false; } } } return ret; } static bool startsWithX(const std::string &s) { return s.size() && s[0] == 'x'; } bool DLL_EXPORT isInScreenRange(const int3 ¢er, const int3 &pos) { int3 diff = pos - center; if(diff.x >= -9 && diff.x <= 9 && diff.y >= -8 && diff.y <= 8) return true; else return false; } void CSpellHandler::loadSpells() { std::string buf = bitmaph->getTextFile("SPTRAITS.TXT"), pom; int andame = buf.size(), i=0; //buf iterator for(int z=0; z<5; ++z) loadToIt(pom,buf,i,3); bool combSpells=false; //true, if we are reading combat spells bool creatureAbility=false; //if true, only creature can use this spell int ifHit = 0; while(i>dump; //reading exact info int spellID; ast>>spellID; while(spellID != -1) { int buf; ast >> buf; spells[spellID].positiveness = buf; ast >> buf; spells[spellID].mainEffectAnim = buf; spells[spellID].range.resize(4); for(int g=0; g<4; ++g) ast>>spells[spellID].range[g]; ast>>spellID; } } }