2010-02-08 16:39:19 +02:00
|
|
|
#define VCMI_DLL
|
|
|
|
|
|
|
|
#include "CCampaignHandler.h"
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
2010-02-09 15:48:14 +02:00
|
|
|
#include "CLodHandler.h"
|
2010-02-12 17:04:01 +02:00
|
|
|
#include "../lib/VCMI_Lib.h"
|
|
|
|
#include "CGeneralTextHandler.h"
|
2010-08-04 14:18:13 +03:00
|
|
|
#include "../StartInfo.h"
|
2010-08-28 17:52:20 +03:00
|
|
|
#include "CArtHandler.h" //for hero crossover
|
|
|
|
#include "CObjectHandler.h" //for hero crossover
|
|
|
|
#include "CHeroHandler.h"
|
|
|
|
#include <boost/foreach.hpp>
|
2010-02-08 16:39:19 +02:00
|
|
|
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
2010-02-09 15:48:14 +02:00
|
|
|
/*
|
|
|
|
* CCampaignHandler.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-02-08 16:39:19 +02:00
|
|
|
|
2010-02-18 14:34:44 +02:00
|
|
|
std::vector<CCampaignHeader> CCampaignHandler::getCampaignHeaders(GetMode mode)
|
2010-02-08 16:39:19 +02:00
|
|
|
{
|
|
|
|
std::vector<CCampaignHeader> ret;
|
|
|
|
|
|
|
|
std::string dirname = DATA_DIR "/Maps";
|
2010-02-18 14:34:44 +02:00
|
|
|
std::string ext = ".H3C";
|
2010-02-08 16:39:19 +02:00
|
|
|
|
|
|
|
if(!boost::filesystem::exists(dirname))
|
|
|
|
{
|
|
|
|
tlog1 << "Cannot find " << dirname << " directory!\n";
|
|
|
|
}
|
|
|
|
|
2010-02-18 14:34:44 +02:00
|
|
|
if (mode == Custom || mode == ALL) //add custom campaigns
|
2010-02-08 16:39:19 +02:00
|
|
|
{
|
2010-02-18 14:34:44 +02:00
|
|
|
fs::path tie(dirname);
|
|
|
|
fs::directory_iterator end_iter;
|
|
|
|
for ( fs::directory_iterator file (tie); file!=end_iter; ++file )
|
2010-02-08 16:39:19 +02:00
|
|
|
{
|
2010-02-18 14:34:44 +02:00
|
|
|
if(fs::is_regular_file(file->status())
|
2011-02-24 16:01:58 +02:00
|
|
|
&& boost::ends_with(file->path().filename(), ext))
|
2010-02-18 14:34:44 +02:00
|
|
|
{
|
|
|
|
ret.push_back( getHeader( file->path().string(), false ) );
|
|
|
|
}
|
2010-02-08 16:39:19 +02:00
|
|
|
}
|
|
|
|
}
|
2010-02-18 14:34:44 +02:00
|
|
|
if (mode == ALL) //add all lod campaigns
|
|
|
|
{
|
2010-11-09 13:27:58 +02:00
|
|
|
BOOST_FOREACH(Entry e, bitmaph->entries)
|
2010-02-18 14:34:44 +02:00
|
|
|
{
|
2010-11-09 13:27:58 +02:00
|
|
|
if( e.type == FILE_CAMPAIGN && e.name != "TOSBLK1" )
|
2010-02-18 14:34:44 +02:00
|
|
|
{
|
2010-11-09 13:27:58 +02:00
|
|
|
ret.push_back( getHeader(e.name, true) );
|
2010-02-18 14:34:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-08 16:39:19 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-02-18 14:34:44 +02:00
|
|
|
CCampaignHeader CCampaignHandler::getHeader( const std::string & name, bool fromLod )
|
2010-02-08 16:39:19 +02:00
|
|
|
{
|
2010-02-09 15:48:14 +02:00
|
|
|
int realSize;
|
2010-02-18 16:09:16 +02:00
|
|
|
unsigned char * cmpgn = getFile(name, fromLod, realSize);
|
2010-02-08 16:39:19 +02:00
|
|
|
|
2010-02-09 15:48:14 +02:00
|
|
|
int it = 0;//iterator for reading
|
2010-02-11 16:12:22 +02:00
|
|
|
CCampaignHeader ret = readHeaderFromMemory(cmpgn, it);
|
2010-02-18 14:34:44 +02:00
|
|
|
ret.filename = name;
|
2010-02-18 16:09:16 +02:00
|
|
|
ret.loadFromLod = fromLod;
|
2010-02-11 16:12:22 +02:00
|
|
|
|
|
|
|
delete [] cmpgn;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-02-18 14:34:44 +02:00
|
|
|
CCampaign * CCampaignHandler::getCampaign( const std::string & name, bool fromLod )
|
2010-02-11 16:12:22 +02:00
|
|
|
{
|
|
|
|
CCampaign * ret = new CCampaign();
|
|
|
|
|
|
|
|
int realSize;
|
2010-02-18 16:09:16 +02:00
|
|
|
unsigned char * cmpgn = getFile(name, fromLod, realSize);
|
2010-02-11 16:12:22 +02:00
|
|
|
|
|
|
|
int it = 0; //iterator for reading
|
|
|
|
ret->header = readHeaderFromMemory(cmpgn, it);
|
2010-02-18 14:34:44 +02:00
|
|
|
ret->header.filename = name;
|
|
|
|
ret->header.loadFromLod = fromLod;
|
2010-02-11 16:12:22 +02:00
|
|
|
|
2010-02-12 17:04:01 +02:00
|
|
|
int howManyScenarios = VLC->generaltexth->campaignRegionNames[ret->header.mapVersion].size();
|
|
|
|
for(int g=0; g<howManyScenarios; ++g)
|
|
|
|
{
|
2010-02-15 15:28:33 +02:00
|
|
|
CCampaignScenario sc = readScenarioFromMemory(cmpgn, it, ret->header.version, ret->header.mapVersion);
|
2010-02-12 17:04:01 +02:00
|
|
|
ret->scenarios.push_back(sc);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ui32> h3mStarts = locateH3mStarts(cmpgn, it, realSize);
|
|
|
|
|
2010-02-16 19:28:56 +02:00
|
|
|
assert(h3mStarts.size() <= howManyScenarios);
|
2010-07-30 14:29:42 +03:00
|
|
|
//it looks like we can have more scenarios than we should..
|
|
|
|
if(h3mStarts.size() > howManyScenarios)
|
|
|
|
{
|
|
|
|
tlog1<<"Our heuristic for h3m start points gave wrong results for campaign " << name <<std::endl;
|
|
|
|
tlog1<<"Please send this campaign to VCMI Project team to help us fix this problem" << std::endl;
|
|
|
|
delete [] cmpgn;
|
|
|
|
assert(0);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int scenarioID = 0;
|
2010-02-16 19:28:56 +02:00
|
|
|
|
|
|
|
for (int g=0; g<h3mStarts.size(); ++g)
|
2010-02-12 17:04:01 +02:00
|
|
|
{
|
2010-07-30 14:29:42 +03:00
|
|
|
while(!ret->scenarios[scenarioID].isNotVoid()) //skip void scenarios
|
|
|
|
{
|
|
|
|
scenarioID++;
|
|
|
|
}
|
|
|
|
//set map piece appropriately
|
2010-02-16 19:28:56 +02:00
|
|
|
if(g == h3mStarts.size() - 1)
|
2010-02-12 17:04:01 +02:00
|
|
|
{
|
2010-07-30 14:29:42 +03:00
|
|
|
ret->mapPieces[scenarioID] = std::string( cmpgn + h3mStarts[g], cmpgn + realSize );
|
2010-02-12 17:04:01 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-07-30 14:29:42 +03:00
|
|
|
ret->mapPieces[scenarioID] = std::string( cmpgn + h3mStarts[g], cmpgn + h3mStarts[g+1] );
|
2010-02-12 17:04:01 +02:00
|
|
|
}
|
2010-07-30 14:29:42 +03:00
|
|
|
scenarioID++;
|
2010-02-12 17:04:01 +02:00
|
|
|
}
|
2010-02-08 16:39:19 +02:00
|
|
|
|
2010-02-09 15:48:14 +02:00
|
|
|
delete [] cmpgn;
|
2010-02-08 16:39:19 +02:00
|
|
|
|
2010-02-11 16:12:22 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCampaignHeader CCampaignHandler::readHeaderFromMemory( const unsigned char *buffer, int & outIt )
|
|
|
|
{
|
|
|
|
CCampaignHeader ret;
|
|
|
|
ret.version = readNormalNr(buffer, outIt); outIt+=4;
|
|
|
|
ret.mapVersion = readChar(buffer, outIt);
|
2010-02-15 14:12:21 +02:00
|
|
|
ret.mapVersion -= 1; //change range of it from [1, 20] to [0, 19]
|
2010-02-11 16:12:22 +02:00
|
|
|
ret.name = readString(buffer, outIt);
|
|
|
|
ret.description = readString(buffer, outIt);
|
|
|
|
ret.difficultyChoosenByPlayer = readChar(buffer, outIt);
|
|
|
|
ret.music = readChar(buffer, outIt);
|
2010-02-12 17:04:01 +02:00
|
|
|
if(ret.version == 4) //I saw one campaign with this version, without either difficulty or music - it's
|
|
|
|
{ //not editable by any editor so I'll just go back by one byte.
|
|
|
|
outIt--;
|
|
|
|
}
|
2010-02-11 16:12:22 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-02-15 15:28:33 +02:00
|
|
|
CCampaignScenario CCampaignHandler::readScenarioFromMemory( const unsigned char *buffer, int & outIt, int version, int mapVersion )
|
2010-02-11 16:12:22 +02:00
|
|
|
{
|
|
|
|
struct HLP
|
|
|
|
{
|
|
|
|
//reads prolog/epilog info from memory
|
|
|
|
static CCampaignScenario::SScenarioPrologEpilog prologEpilogReader( const unsigned char *buffer, int & outIt )
|
|
|
|
{
|
|
|
|
CCampaignScenario::SScenarioPrologEpilog ret;
|
|
|
|
ret.hasPrologEpilog = buffer[outIt++];
|
|
|
|
if(ret.hasPrologEpilog)
|
|
|
|
{
|
|
|
|
ret.prologVideo = buffer[outIt++];
|
|
|
|
ret.prologMusic = buffer[outIt++];
|
|
|
|
ret.prologText = readString(buffer, outIt);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
CCampaignScenario ret;
|
2010-02-16 19:28:56 +02:00
|
|
|
ret.conquered = false;
|
2010-02-11 16:12:22 +02:00
|
|
|
ret.mapName = readString(buffer, outIt);
|
|
|
|
ret.packedMapSize = readNormalNr(buffer, outIt); outIt += 4;
|
2010-02-15 15:28:33 +02:00
|
|
|
if(mapVersion == 18)//unholy alliance
|
2010-02-16 19:28:56 +02:00
|
|
|
{
|
|
|
|
ret.preconditionRegion = readNormalNr(buffer, outIt, 2); outIt += 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret.preconditionRegion = buffer[outIt++];
|
|
|
|
}
|
2010-02-11 16:12:22 +02:00
|
|
|
ret.regionColor = buffer[outIt++];
|
|
|
|
ret.difficulty = buffer[outIt++];
|
|
|
|
ret.regionText = readString(buffer, outIt);
|
|
|
|
ret.prolog = HLP::prologEpilogReader(buffer, outIt);
|
|
|
|
ret.epilog = HLP::prologEpilogReader(buffer, outIt);
|
|
|
|
|
2010-02-15 14:12:21 +02:00
|
|
|
ret.travelOptions = readScenarioTravelFromMemory(buffer, outIt, version);
|
2010-02-11 16:12:22 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-02-15 14:12:21 +02:00
|
|
|
CScenarioTravel CCampaignHandler::readScenarioTravelFromMemory( const unsigned char * buffer, int & outIt , int version )
|
2010-02-11 16:12:22 +02:00
|
|
|
{
|
|
|
|
CScenarioTravel ret;
|
|
|
|
|
|
|
|
ret.whatHeroKeeps = buffer[outIt++];
|
|
|
|
memcpy(ret.monstersKeptByHero, buffer+outIt, ARRAY_COUNT(ret.monstersKeptByHero));
|
|
|
|
outIt += ARRAY_COUNT(ret.monstersKeptByHero);
|
2010-02-15 14:12:21 +02:00
|
|
|
int artifBytes;
|
|
|
|
if (version == 5) //AB
|
|
|
|
{
|
|
|
|
artifBytes = 17;
|
|
|
|
ret.artifsKeptByHero[17] = 0;
|
|
|
|
}
|
|
|
|
else //SoD+
|
|
|
|
{
|
|
|
|
artifBytes = 18;
|
|
|
|
}
|
|
|
|
memcpy(ret.artifsKeptByHero, buffer+outIt, artifBytes);
|
|
|
|
outIt += artifBytes;
|
2010-02-11 16:12:22 +02:00
|
|
|
|
|
|
|
ret.startOptions = buffer[outIt++];
|
|
|
|
|
|
|
|
switch(ret.startOptions)
|
|
|
|
{
|
2010-02-16 19:28:56 +02:00
|
|
|
case 0:
|
|
|
|
//no bonuses. Seems to be OK
|
|
|
|
break;
|
2010-02-11 16:12:22 +02:00
|
|
|
case 1: //reading of bonuses player can choose
|
|
|
|
{
|
|
|
|
ret.playerColor = buffer[outIt++];
|
|
|
|
ui8 numOfBonuses = buffer[outIt++];
|
|
|
|
for (int g=0; g<numOfBonuses; ++g)
|
|
|
|
{
|
|
|
|
CScenarioTravel::STravelBonus bonus;
|
|
|
|
bonus.type = buffer[outIt++];
|
2010-07-31 16:55:05 +03:00
|
|
|
//hero: FFFD means 'most powerful' and FFFE means 'generated'
|
2010-02-11 16:12:22 +02:00
|
|
|
switch(bonus.type)
|
|
|
|
{
|
|
|
|
case 0: //spell
|
|
|
|
{
|
|
|
|
bonus.info1 = readNormalNr(buffer, outIt, 2); outIt += 2; //hero
|
|
|
|
bonus.info2 = buffer[outIt++]; //spell ID
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1: //monster
|
|
|
|
{
|
|
|
|
bonus.info1 = readNormalNr(buffer, outIt, 2); outIt += 2; //hero
|
|
|
|
bonus.info2 = readNormalNr(buffer, outIt, 2); outIt += 2; //monster type
|
|
|
|
bonus.info3 = readNormalNr(buffer, outIt, 2); outIt += 2; //monster count
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2: //building
|
|
|
|
{
|
2010-02-12 17:04:01 +02:00
|
|
|
bonus.info1 = buffer[outIt++]; //building ID (0 - town hall, 1 - city hall, 2 - capitol, etc)
|
2010-02-11 16:12:22 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 3: //artifact
|
|
|
|
{
|
|
|
|
bonus.info1 = readNormalNr(buffer, outIt, 2); outIt += 2; //hero
|
|
|
|
bonus.info2 = readNormalNr(buffer, outIt, 2); outIt += 2; //artifact ID
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 4: //spell scroll
|
|
|
|
{
|
|
|
|
bonus.info1 = readNormalNr(buffer, outIt, 2); outIt += 2; //hero
|
2010-02-15 14:12:21 +02:00
|
|
|
bonus.info2 = buffer[outIt++]; //spell ID
|
2010-02-11 16:12:22 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 5: //prim skill
|
|
|
|
{
|
|
|
|
bonus.info1 = readNormalNr(buffer, outIt, 2); outIt += 2; //hero
|
|
|
|
bonus.info2 = readNormalNr(buffer, outIt, 4); outIt += 4; //bonuses (4 bytes for 4 skills)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 6: //sec skills
|
|
|
|
{
|
|
|
|
bonus.info1 = readNormalNr(buffer, outIt, 2); outIt += 2; //hero
|
|
|
|
bonus.info2 = buffer[outIt++]; //skill ID
|
|
|
|
bonus.info3 = buffer[outIt++]; //skill level
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 7: //resources
|
|
|
|
{
|
|
|
|
bonus.info1 = buffer[outIt++]; //type
|
|
|
|
//FD - wood+ore
|
|
|
|
//FE - mercury+sulfur+crystal+gem
|
|
|
|
bonus.info2 = readNormalNr(buffer, outIt, 4); outIt += 4; //count
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret.bonusesToChoose.push_back(bonus);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2: //reading of players (colors / scenarios ?) player can choose
|
|
|
|
{
|
|
|
|
ui8 numOfBonuses = buffer[outIt++];
|
|
|
|
for (int g=0; g<numOfBonuses; ++g)
|
|
|
|
{
|
|
|
|
CScenarioTravel::STravelBonus bonus;
|
|
|
|
bonus.type = 8;
|
|
|
|
bonus.info1 = buffer[outIt++]; //player color
|
|
|
|
bonus.info2 = buffer[outIt++]; //from what scenario
|
|
|
|
|
|
|
|
ret.bonusesToChoose.push_back(bonus);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 3: //heroes player can choose between
|
|
|
|
{
|
|
|
|
ui8 numOfBonuses = buffer[outIt++];
|
|
|
|
for (int g=0; g<numOfBonuses; ++g)
|
|
|
|
{
|
|
|
|
CScenarioTravel::STravelBonus bonus;
|
|
|
|
bonus.type = 9;
|
|
|
|
bonus.info1 = buffer[outIt++]; //player color
|
|
|
|
bonus.info2 = readNormalNr(buffer, outIt, 2); outIt += 2; //hero, FF FF - random
|
|
|
|
|
|
|
|
ret.bonusesToChoose.push_back(bonus);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
tlog1<<"Corrupted h3c file"<<std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-08 16:39:19 +02:00
|
|
|
return ret;
|
2010-02-12 17:04:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<ui32> CCampaignHandler::locateH3mStarts( const unsigned char * buffer, int start, int size )
|
|
|
|
{
|
|
|
|
std::vector<ui32> ret;
|
|
|
|
for(int g=start; g<size; ++g)
|
|
|
|
{
|
|
|
|
if(startsAt(buffer, size, g))
|
|
|
|
{
|
|
|
|
ret.push_back(g);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCampaignHandler::startsAt( const unsigned char * buffer, int size, int pos )
|
|
|
|
{
|
|
|
|
struct HLP
|
|
|
|
{
|
|
|
|
static unsigned char at(const unsigned char * buffer, int size, int place)
|
|
|
|
{
|
|
|
|
if(place < size)
|
|
|
|
return buffer[place];
|
|
|
|
|
|
|
|
throw std::string("Out of bounds!");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
try
|
|
|
|
{
|
|
|
|
//check minimal length of given region
|
|
|
|
HLP::at(buffer, size, 100);
|
|
|
|
//check version
|
|
|
|
|
|
|
|
unsigned char tmp = HLP::at(buffer, size, pos);
|
|
|
|
if(!(tmp == 0x0e || tmp == 0x15 || tmp == 0x1c || tmp == 0x33))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//3 bytes following version
|
|
|
|
if(HLP::at(buffer, size, pos+1) != 0 || HLP::at(buffer, size, pos+2) != 0 || HLP::at(buffer, size, pos+3) != 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//unknown strange byte
|
|
|
|
tmp = HLP::at(buffer, size, pos+4);
|
|
|
|
if(tmp != 0 && tmp != 1 )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//size of map
|
|
|
|
int mapsize = readNormalNr(buffer, pos+5);
|
|
|
|
if(mapsize < 10 || mapsize > 530)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//underground or not
|
|
|
|
tmp = HLP::at(buffer, size, pos+9);
|
|
|
|
if( tmp != 0 && tmp != 1 )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//map name
|
|
|
|
int len = readNormalNr(buffer, pos+10);
|
|
|
|
if(len < 0 || len > 100)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for(int t=0; t<len; ++t)
|
|
|
|
{
|
|
|
|
tmp = HLP::at(buffer, size, pos+14+t);
|
|
|
|
if(tmp == 0 || (tmp > 15 && tmp < 32)) //not a valid character
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2010-02-16 19:28:56 +02:00
|
|
|
}
|
|
|
|
|
2010-02-18 16:09:16 +02:00
|
|
|
unsigned char * CCampaignHandler::getFile( const std::string & name, bool fromLod, int & outSize )
|
|
|
|
{
|
2011-03-19 16:27:51 +02:00
|
|
|
unsigned char * cmpgn = 0;
|
2010-02-18 16:09:16 +02:00
|
|
|
if(fromLod)
|
|
|
|
{
|
2011-03-19 16:27:51 +02:00
|
|
|
if (bitmaph->haveFile(name, FILE_CAMPAIGN))
|
|
|
|
cmpgn = bitmaph->giveFile(name, FILE_CAMPAIGN, &outSize);
|
|
|
|
else if (bitmaph_ab->haveFile(name, FILE_CAMPAIGN))
|
|
|
|
cmpgn = bitmaph_ab->giveFile(name, FILE_CAMPAIGN, &outSize);
|
|
|
|
else
|
|
|
|
tlog1 << "Cannot find file: " << name << std::endl;
|
2010-02-18 16:09:16 +02:00
|
|
|
FILE * tmp = fopen("tmp_cmpgn", "wb");
|
|
|
|
fwrite(cmpgn, 1, outSize, tmp);
|
|
|
|
fclose(tmp);
|
|
|
|
delete [] cmpgn;
|
|
|
|
cmpgn = CLodHandler::getUnpackedFile("tmp_cmpgn", &outSize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cmpgn = CLodHandler::getUnpackedFile(name, &outSize);
|
|
|
|
}
|
|
|
|
return cmpgn;
|
|
|
|
}
|
|
|
|
|
2010-02-16 19:28:56 +02:00
|
|
|
bool CCampaign::conquerable( int whichScenario ) const
|
|
|
|
{
|
2010-07-30 14:29:42 +03:00
|
|
|
//check for void scenraio
|
|
|
|
if (!scenarios[whichScenario].isNotVoid())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-02-16 19:28:56 +02:00
|
|
|
if (scenarios[whichScenario].conquered)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
//check preconditioned regions
|
|
|
|
for (int g=0; g<scenarios.size(); ++g)
|
|
|
|
{
|
|
|
|
if(( (1 << g) & scenarios[whichScenario].preconditionRegion ) && !scenarios[g].conquered)
|
|
|
|
return false; //prerequisite does not met
|
|
|
|
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2010-05-08 21:56:38 +03:00
|
|
|
|
|
|
|
CCampaign::CCampaign()
|
|
|
|
{
|
|
|
|
|
2010-07-30 14:29:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCampaignScenario::isNotVoid() const
|
|
|
|
{
|
|
|
|
return mapName.size() > 0;
|
|
|
|
}
|
2010-07-31 16:55:05 +03:00
|
|
|
|
2010-08-28 17:52:20 +03:00
|
|
|
void CCampaignScenario::prepareCrossoverHeroes( std::vector<CGHeroInstance *> heroes )
|
|
|
|
{
|
|
|
|
crossoverHeroes = heroes;
|
2010-09-04 17:49:15 +03:00
|
|
|
|
2010-08-28 17:52:20 +03:00
|
|
|
|
|
|
|
if (!(travelOptions.whatHeroKeeps & 1))
|
|
|
|
{
|
|
|
|
//trimming experience
|
|
|
|
BOOST_FOREACH(CGHeroInstance * cgh, crossoverHeroes)
|
|
|
|
{
|
|
|
|
cgh->initExp();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(travelOptions.whatHeroKeeps & 2))
|
|
|
|
{
|
|
|
|
//trimming prim skills
|
|
|
|
BOOST_FOREACH(CGHeroInstance * cgh, crossoverHeroes)
|
|
|
|
{
|
|
|
|
#define RESET_PRIM_SKILL(NAME, VALNAME) \
|
|
|
|
cgh->getBonus(Selector::type(Bonus::PRIMARY_SKILL) && \
|
|
|
|
Selector::subtype(PrimarySkill::NAME) && \
|
|
|
|
Selector::sourceType(Bonus::HERO_BASE_SKILL) )->val = cgh->type->heroClass->VALNAME;
|
|
|
|
|
|
|
|
RESET_PRIM_SKILL(ATTACK, initialAttack);
|
|
|
|
RESET_PRIM_SKILL(DEFENSE, initialDefence);
|
|
|
|
RESET_PRIM_SKILL(SPELL_POWER, initialPower);
|
|
|
|
RESET_PRIM_SKILL(KNOWLEDGE, initialKnowledge);
|
|
|
|
#undef RESET_PRIM_SKILL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(travelOptions.whatHeroKeeps & 4))
|
|
|
|
{
|
|
|
|
//trimming sec skills
|
|
|
|
BOOST_FOREACH(CGHeroInstance * cgh, crossoverHeroes)
|
|
|
|
{
|
|
|
|
cgh->secSkills = cgh->type->secSkillsInit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(travelOptions.whatHeroKeeps & 8))
|
|
|
|
{
|
|
|
|
//trimming spells
|
|
|
|
BOOST_FOREACH(CGHeroInstance * cgh, crossoverHeroes)
|
|
|
|
{
|
|
|
|
cgh->spells.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(travelOptions.whatHeroKeeps & 16))
|
|
|
|
{
|
|
|
|
//trimming artifacts
|
|
|
|
for (int g=0; g<VLC->arth->artifacts.size(); ++g)
|
|
|
|
{
|
|
|
|
bool takeable = travelOptions.artifsKeptByHero[g / 8] & ( 1 << (g%8) ) ;
|
|
|
|
if (!takeable)
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(CGHeroInstance * cgh, crossoverHeroes)
|
|
|
|
{
|
2011-01-18 20:56:14 +02:00
|
|
|
tlog1 << "TODO TODO TODO - take artifacts from hero\n";
|
|
|
|
//TODO how was that supposed to work with worn artifacts?
|
|
|
|
//cgh->artifactsInBackpack -= VLC->arth->artifacts[g];
|
2010-08-28 17:52:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//trimming creatures
|
|
|
|
BOOST_FOREACH(CGHeroInstance * cgh, crossoverHeroes)
|
|
|
|
{
|
2010-12-06 01:10:02 +02:00
|
|
|
for (TSlots::const_iterator j = cgh->Slots().begin(); j != cgh->Slots().end(); j++)
|
2010-08-28 17:52:20 +03:00
|
|
|
{
|
2010-12-06 01:10:02 +02:00
|
|
|
if (!(travelOptions.monstersKeptByHero[j->first / 8] & (1 << (j->first%8)) ))
|
2010-08-28 17:52:20 +03:00
|
|
|
{
|
2010-12-06 01:10:02 +02:00
|
|
|
cgh->eraseStack(j->first);
|
|
|
|
j = cgh->Slots().begin();
|
2010-08-28 17:52:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-31 16:55:05 +03:00
|
|
|
bool CScenarioTravel::STravelBonus::isBonusForHero() const
|
|
|
|
{
|
|
|
|
return type == 0 || type == 1 || type == 3 || type == 4 || type == 5 || type == 6;
|
|
|
|
}
|
2010-08-04 14:18:13 +03:00
|
|
|
|
|
|
|
void CCampaignState::initNewCampaign( const StartInfo &si )
|
|
|
|
{
|
2010-08-18 12:50:25 +03:00
|
|
|
assert(si.mode == StartInfo::CAMPAIGN);
|
2010-08-04 14:18:13 +03:00
|
|
|
campaignName = si.mapname;
|
|
|
|
currentMap = si.whichMapInCampaign;
|
|
|
|
|
|
|
|
//check if campaign is in lod or not
|
|
|
|
bool inLod = campaignName.find('/') == std::string::npos;
|
|
|
|
|
|
|
|
camp = CCampaignHandler::getCampaign(campaignName, inLod); //TODO lod???
|
|
|
|
for (ui8 i = 0; i < camp->mapPieces.size(); i++)
|
|
|
|
mapsRemaining.push_back(i);
|
|
|
|
}
|
2010-08-20 16:34:39 +03:00
|
|
|
|
2010-08-28 17:52:20 +03:00
|
|
|
void CCampaignState::mapConquered( const std::vector<CGHeroInstance*> & heroes )
|
2010-08-20 16:34:39 +03:00
|
|
|
{
|
2010-08-28 17:52:20 +03:00
|
|
|
camp->scenarios[currentMap].prepareCrossoverHeroes(heroes);
|
2010-08-20 16:34:39 +03:00
|
|
|
mapsConquered.push_back(currentMap);
|
|
|
|
mapsRemaining -= currentMap;
|
|
|
|
camp->scenarios[currentMap].conquered = true;
|
|
|
|
}
|