| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281 | /* * MapFormatH3M.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 * */#include "StdInc.h"#include <boost/crc.hpp>#include "MapFormatH3M.h"#include "CMap.h"#include "../CStopWatch.h"#include "../filesystem/Filesystem.h"#include "../filesystem/CBinaryReader.h"#include "../spells/CSpellHandler.h"#include "../CSkillHandler.h"#include "../CCreatureHandler.h"#include "../CGeneralTextHandler.h"#include "../CHeroHandler.h"#include "../mapObjects/CObjectClassesHandler.h"#include "../mapObjects/MapObjects.h"#include "../VCMI_Lib.h"#include "../TextOperations.h"#include "../TerrainHandler.h"#include "../RoadHandler.h"#include "../RiverHandler.h"#include "../NetPacksBase.h"VCMI_LIB_NAMESPACE_BEGINconst bool CMapLoaderH3M::IS_PROFILING_ENABLED = false;CMapLoaderH3M::CMapLoaderH3M(const std::string & mapName, const std::string & encodingName, CInputStream * stream)	: map(nullptr)	, reader(new CBinaryReader(stream))	, inputStream(stream)	, mapName(mapName)	, fileEncoding(encodingName){}//must be instantiated in .cpp file for access to complete types of all member fieldsCMapLoaderH3M::~CMapLoaderH3M() = default;std::unique_ptr<CMap> CMapLoaderH3M::loadMap(){	// Init map object by parsing the input buffer	map = new CMap();	mapHeader = std::unique_ptr<CMapHeader>(dynamic_cast<CMapHeader *>(map));	init();	return std::unique_ptr<CMap>(dynamic_cast<CMap *>(mapHeader.release()));}std::unique_ptr<CMapHeader> CMapLoaderH3M::loadMapHeader(){	// Read header	mapHeader = std::make_unique<CMapHeader>();	readHeader();	return std::move(mapHeader);}void CMapLoaderH3M::init(){	//FIXME: get rid of double input process	si64 temp_size = inputStream->getSize();	inputStream->seek(0);	auto * temp_buffer = new ui8[temp_size];	inputStream->read(temp_buffer,temp_size);	// Compute checksum	boost::crc_32_type  result;	result.process_bytes(temp_buffer, temp_size);	map->checksum = result.checksum();	delete [] temp_buffer;	inputStream->seek(0);	CStopWatch sw;	struct MapLoadingTime	{		std::string name;		si64 time;		MapLoadingTime(std::string name, si64 time): name(std::move(name)), time(time) {}	};	std::vector<MapLoadingTime> times;	readHeader();	times.emplace_back("header", sw.getDiff());	map->allHeroes.resize(map->allowedHeroes.size());	readDisposedHeroes();	times.emplace_back("disposed heroes", sw.getDiff());	readAllowedArtifacts();	times.emplace_back("allowed artifacts", sw.getDiff());	readAllowedSpellsAbilities();	times.emplace_back("allowed spells and abilities", sw.getDiff());	readRumors();	times.emplace_back("rumors", sw.getDiff());	readPredefinedHeroes();	times.emplace_back("predefined heroes", sw.getDiff());	readTerrain();	times.emplace_back("terrain", sw.getDiff());	readDefInfo();	times.emplace_back("def info", sw.getDiff());	readObjects();	times.emplace_back("objects", sw.getDiff());	readEvents();	times.emplace_back("events", sw.getDiff());	times.emplace_back("blocked/visitable tiles", sw.getDiff());	// Print profiling times	if(IS_PROFILING_ENABLED)	{		for(MapLoadingTime & mlt : times)		{			logGlobal->debug("\tReading %s took %d ms", mlt.name, mlt.time);		}	}	map->calculateGuardingGreaturePositions();	afterRead();}void CMapLoaderH3M::readHeader(){	// Check map for validity	// Note: disabled, causes decompression of the entire file ( = SLOW)	//if(inputStream->getSize() < 50)	//{	//	throw std::runtime_error("Corrupted map file.");	//}	// Map version	mapHeader->version = static_cast<EMapFormat::EMapFormat>(reader->readUInt32());	if(mapHeader->version != EMapFormat::ROE && mapHeader->version != EMapFormat::AB && mapHeader->version != EMapFormat::SOD			&& mapHeader->version != EMapFormat::WOG)	{		throw std::runtime_error("Invalid map format!");	}	// Read map name, description, dimensions,...	mapHeader->areAnyPlayers = reader->readBool();	mapHeader->height = mapHeader->width = reader->readUInt32();	mapHeader->twoLevel = reader->readBool();	mapHeader->name = readLocalizedString();	mapHeader->description = readLocalizedString();	mapHeader->difficulty = reader->readInt8();	if(mapHeader->version != EMapFormat::ROE)	{		mapHeader->levelLimit = reader->readUInt8();	}	else	{		mapHeader->levelLimit = 0;	}	readPlayerInfo();	readVictoryLossConditions();	readTeamInfo();	readAllowedHeroes();}void CMapLoaderH3M::readPlayerInfo(){	for(int i = 0; i < mapHeader->players.size(); ++i)	{		mapHeader->players[i].canHumanPlay = reader->readBool();		mapHeader->players[i].canComputerPlay = reader->readBool();		// If nobody can play with this player		if((!(mapHeader->players[i].canHumanPlay || mapHeader->players[i].canComputerPlay)))		{			switch(mapHeader->version)			{			case EMapFormat::SOD:			case EMapFormat::WOG:				reader->skip(13);				break;			case EMapFormat::AB:				reader->skip(12);				break;			case EMapFormat::ROE:				reader->skip(6);				break;			}			continue;		}		mapHeader->players[i].aiTactic = static_cast<EAiTactic::EAiTactic>(reader->readUInt8());		if(mapHeader->version == EMapFormat::SOD || mapHeader->version == EMapFormat::WOG)		{			mapHeader->players[i].p7 = reader->readUInt8();		}		else		{			mapHeader->players[i].p7 = -1;		}		// Factions this player can choose		ui16 allowedFactions = reader->readUInt8();		// How many factions will be read from map		ui16 totalFactions = GameConstants::F_NUMBER;		if(mapHeader->version != EMapFormat::ROE)			allowedFactions += reader->readUInt8() * 256; // 256 = 2^8 = 0b100000000		else			totalFactions--; //exclude conflux for ROE		const bool isFactionRandom = mapHeader->players[i].isFactionRandom = reader->readBool();		const ui16 allFactionsMask = (mapHeader->version == EMapFormat::ROE)			? 0b11111111   // 8 towns for ROE			: 0b111111111; // 8 towns + Conflux		const bool allFactionsAllowed = mapHeader->version == EMapFormat::VCMI			|| (isFactionRandom && ((allowedFactions & allFactionsMask) == allFactionsMask));		if(!allFactionsAllowed)		{			mapHeader->players[i].allowedFactions.clear();			for(int fact = 0; fact < totalFactions; ++fact)			{				if(allowedFactions & (1 << fact))					mapHeader->players[i].allowedFactions.insert(fact);			}		}		mapHeader->players[i].hasMainTown = reader->readBool();		if(mapHeader->players[i].hasMainTown)		{			if(mapHeader->version != EMapFormat::ROE)			{				mapHeader->players[i].generateHeroAtMainTown = reader->readBool();				mapHeader->players[i].generateHero = reader->readBool();			}			else			{				mapHeader->players[i].generateHeroAtMainTown = true;				mapHeader->players[i].generateHero = false;			}			mapHeader->players[i].posOfMainTown = readInt3();		}		mapHeader->players[i].hasRandomHero = reader->readBool();		mapHeader->players[i].mainCustomHeroId = reader->readUInt8();		if(mapHeader->players[i].mainCustomHeroId != 0xff)		{			mapHeader->players[i].mainCustomHeroPortrait = reader->readUInt8();			if (mapHeader->players[i].mainCustomHeroPortrait == 0xff)				mapHeader->players[i].mainCustomHeroPortrait = -1; //correct 1-byte -1 (0xff) into 4-byte -1			mapHeader->players[i].mainCustomHeroName = readLocalizedString();		}		else			mapHeader->players[i].mainCustomHeroId = -1; //correct 1-byte -1 (0xff) into 4-byte -1		if(mapHeader->version != EMapFormat::ROE)		{			mapHeader->players[i].powerPlaceholders = reader->readUInt8(); //unknown byte			int heroCount = reader->readUInt8();			reader->skip(3);			for(int pp = 0; pp < heroCount; ++pp)			{				SHeroName vv;				vv.heroId = reader->readUInt8();				vv.heroName = readLocalizedString();				mapHeader->players[i].heroesNames.push_back(vv);			}		}	}}namespace EVictoryConditionType{	enum EVictoryConditionType { ARTIFACT, GATHERTROOP, GATHERRESOURCE, BUILDCITY, BUILDGRAIL, BEATHERO,		CAPTURECITY, BEATMONSTER, TAKEDWELLINGS, TAKEMINES, TRANSPORTITEM, WINSTANDARD = 255 };}namespace ELossConditionType{	enum ELossConditionType { LOSSCASTLE, LOSSHERO, TIMEEXPIRES, LOSSSTANDARD = 255 };}void CMapLoaderH3M::readVictoryLossConditions(){	mapHeader->triggeredEvents.clear();	auto vicCondition = static_cast<EVictoryConditionType::EVictoryConditionType>(reader->readUInt8());	EventCondition victoryCondition(EventCondition::STANDARD_WIN);	EventCondition defeatCondition(EventCondition::DAYS_WITHOUT_TOWN);	defeatCondition.value = 7;	TriggeredEvent standardVictory;	standardVictory.effect.type = EventEffect::VICTORY;	standardVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[5];	standardVictory.identifier = "standardVictory";	standardVictory.description.clear(); // TODO: display in quest window	standardVictory.onFulfill = VLC->generaltexth->allTexts[659];	standardVictory.trigger = EventExpression(victoryCondition);	TriggeredEvent standardDefeat;	standardDefeat.effect.type = EventEffect::DEFEAT;	standardDefeat.effect.toOtherMessage = VLC->generaltexth->allTexts[8];	standardDefeat.identifier = "standardDefeat";	standardDefeat.description.clear(); // TODO: display in quest window	standardDefeat.onFulfill = VLC->generaltexth->allTexts[7];	standardDefeat.trigger = EventExpression(defeatCondition);	// Specific victory conditions	if(vicCondition == EVictoryConditionType::WINSTANDARD)	{		// create normal condition		mapHeader->triggeredEvents.push_back(standardVictory);		mapHeader->victoryIconIndex = 11;		mapHeader->victoryMessage = VLC->generaltexth->victoryConditions[0];	}	else	{		TriggeredEvent specialVictory;		specialVictory.effect.type = EventEffect::VICTORY;		specialVictory.identifier = "specialVictory";		specialVictory.description.clear(); // TODO: display in quest window		mapHeader->victoryIconIndex = static_cast<ui16>(vicCondition);		mapHeader->victoryMessage = VLC->generaltexth->victoryConditions[static_cast<size_t>(vicCondition) + 1];		bool allowNormalVictory = reader->readBool();		bool appliesToAI = reader->readBool();		if (allowNormalVictory)		{			size_t playersOnMap = boost::range::count_if(mapHeader->players,[](const PlayerInfo & info) { return info.canAnyonePlay();});			if (playersOnMap == 1)			{				logGlobal->warn("Map %s has only one player but allows normal victory?", mapHeader->name);				allowNormalVictory = false; // makes sense? Not much. Works as H3? Yes!			}		}		switch(vicCondition)		{		case EVictoryConditionType::ARTIFACT:			{				EventCondition cond(EventCondition::HAVE_ARTIFACT);				cond.objectType = reader->readUInt8();				if (mapHeader->version != EMapFormat::ROE)					reader->skip(1);				specialVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[281];				specialVictory.onFulfill = VLC->generaltexth->allTexts[280];				specialVictory.trigger = EventExpression(cond);				break;			}		case EVictoryConditionType::GATHERTROOP:			{				EventCondition cond(EventCondition::HAVE_CREATURES);				cond.objectType = reader->readUInt8();				if (mapHeader->version != EMapFormat::ROE)					reader->skip(1);				cond.value = reader->readUInt32();				specialVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[277];				specialVictory.onFulfill = VLC->generaltexth->allTexts[276];				specialVictory.trigger = EventExpression(cond);				break;			}		case EVictoryConditionType::GATHERRESOURCE:			{				EventCondition cond(EventCondition::HAVE_RESOURCES);				cond.objectType = reader->readUInt8();				cond.value = reader->readUInt32();				specialVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[279];				specialVictory.onFulfill = VLC->generaltexth->allTexts[278];				specialVictory.trigger = EventExpression(cond);				break;			}		case EVictoryConditionType::BUILDCITY:			{				EventExpression::OperatorAll oper;				EventCondition cond(EventCondition::HAVE_BUILDING);				cond.position = readInt3();				cond.objectType = BuildingID::TOWN_HALL + reader->readUInt8();				oper.expressions.emplace_back(cond);				cond.objectType = BuildingID::FORT + reader->readUInt8();				oper.expressions.emplace_back(cond);				specialVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[283];				specialVictory.onFulfill = VLC->generaltexth->allTexts[282];				specialVictory.trigger = EventExpression(oper);				break;			}		case EVictoryConditionType::BUILDGRAIL:			{				EventCondition cond(EventCondition::HAVE_BUILDING);				cond.objectType = BuildingID::GRAIL;				cond.position = readInt3();				if(cond.position.z > 2)					cond.position = int3(-1,-1,-1);				specialVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[285];				specialVictory.onFulfill = VLC->generaltexth->allTexts[284];				specialVictory.trigger = EventExpression(cond);				break;			}		case EVictoryConditionType::BEATHERO:			{				EventCondition cond(EventCondition::DESTROY);				cond.objectType = Obj::HERO;				cond.position = readInt3();				specialVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[253];				specialVictory.onFulfill = VLC->generaltexth->allTexts[252];				specialVictory.trigger = EventExpression(cond);				break;			}		case EVictoryConditionType::CAPTURECITY:			{				EventCondition cond(EventCondition::CONTROL);				cond.objectType = Obj::TOWN;				cond.position = readInt3();				specialVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[250];				specialVictory.onFulfill = VLC->generaltexth->allTexts[249];				specialVictory.trigger = EventExpression(cond);				break;			}		case EVictoryConditionType::BEATMONSTER:			{				EventCondition cond(EventCondition::DESTROY);				cond.objectType = Obj::MONSTER;				cond.position = readInt3();				specialVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[287];				specialVictory.onFulfill = VLC->generaltexth->allTexts[286];				specialVictory.trigger = EventExpression(cond);				break;			}		case EVictoryConditionType::TAKEDWELLINGS:			{				EventExpression::OperatorAll oper;				oper.expressions.emplace_back(EventCondition(EventCondition::CONTROL, 0, Obj::CREATURE_GENERATOR1));				oper.expressions.emplace_back(EventCondition(EventCondition::CONTROL, 0, Obj::CREATURE_GENERATOR4));				specialVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[289];				specialVictory.onFulfill = VLC->generaltexth->allTexts[288];				specialVictory.trigger = EventExpression(oper);				break;			}		case EVictoryConditionType::TAKEMINES:			{				EventCondition cond(EventCondition::CONTROL);				cond.objectType = Obj::MINE;				specialVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[291];				specialVictory.onFulfill = VLC->generaltexth->allTexts[290];				specialVictory.trigger = EventExpression(cond);				break;			}		case EVictoryConditionType::TRANSPORTITEM:			{				EventCondition cond(EventCondition::TRANSPORT);				cond.objectType = reader->readUInt8();				cond.position = readInt3();				specialVictory.effect.toOtherMessage = VLC->generaltexth->allTexts[293];				specialVictory.onFulfill = VLC->generaltexth->allTexts[292];				specialVictory.trigger = EventExpression(cond);				break;			}		default:			assert(0);		}		// if condition is human-only turn it into following construction: AllOf(human, condition)		if (!appliesToAI)		{			EventExpression::OperatorAll oper;			EventCondition notAI(EventCondition::IS_HUMAN);			notAI.value = 1;			oper.expressions.emplace_back(notAI);			oper.expressions.push_back(specialVictory.trigger.get());			specialVictory.trigger = EventExpression(oper);		}		// if normal victory allowed - add one more quest		if (allowNormalVictory)		{			mapHeader->victoryMessage += " / ";			mapHeader->victoryMessage += VLC->generaltexth->victoryConditions[0];			mapHeader->triggeredEvents.push_back(standardVictory);		}		mapHeader->triggeredEvents.push_back(specialVictory);	}	// Read loss conditions	auto lossCond = static_cast<ELossConditionType::ELossConditionType>(reader->readUInt8());	if (lossCond == ELossConditionType::LOSSSTANDARD)	{		mapHeader->defeatIconIndex = 3;		mapHeader->defeatMessage = VLC->generaltexth->lossCondtions[0];	}	else	{		TriggeredEvent specialDefeat;		specialDefeat.effect.type = EventEffect::DEFEAT;		specialDefeat.effect.toOtherMessage = VLC->generaltexth->allTexts[5];		specialDefeat.identifier = "specialDefeat";		specialDefeat.description.clear(); // TODO: display in quest window		mapHeader->defeatIconIndex = static_cast<ui16>(lossCond);		mapHeader->defeatMessage = VLC->generaltexth->lossCondtions[static_cast<size_t>(lossCond) + 1];		switch(lossCond)		{		case ELossConditionType::LOSSCASTLE:			{				EventExpression::OperatorNone noneOf;				EventCondition cond(EventCondition::CONTROL);				cond.objectType = Obj::TOWN;				cond.position = readInt3();				noneOf.expressions.emplace_back(cond);				specialDefeat.onFulfill = VLC->generaltexth->allTexts[251];				specialDefeat.trigger = EventExpression(noneOf);				break;			}		case ELossConditionType::LOSSHERO:			{				EventExpression::OperatorNone noneOf;				EventCondition cond(EventCondition::CONTROL);				cond.objectType = Obj::HERO;				cond.position = readInt3();				noneOf.expressions.emplace_back(cond);				specialDefeat.onFulfill = VLC->generaltexth->allTexts[253];				specialDefeat.trigger = EventExpression(noneOf);				break;			}		case ELossConditionType::TIMEEXPIRES:			{				EventCondition cond(EventCondition::DAYS_PASSED);				cond.value = reader->readUInt16();				specialDefeat.onFulfill = VLC->generaltexth->allTexts[254];				specialDefeat.trigger = EventExpression(cond);				break;			}		}		// turn simple loss condition into complete one that can be evaluated later:		// - any of :		//   - days without town: 7		//   - all of:		//     - is human		//     - (expression)		EventExpression::OperatorAll allOf;		EventCondition isHuman(EventCondition::IS_HUMAN);		isHuman.value = 1;		allOf.expressions.emplace_back(isHuman);		allOf.expressions.push_back(specialDefeat.trigger.get());		specialDefeat.trigger = EventExpression(allOf);		mapHeader->triggeredEvents.push_back(specialDefeat);	}	mapHeader->triggeredEvents.push_back(standardDefeat);}void CMapLoaderH3M::readTeamInfo(){	mapHeader->howManyTeams = reader->readUInt8();	if(mapHeader->howManyTeams > 0)	{		// Teams		for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; ++i)		{			mapHeader->players[i].team = TeamID(reader->readUInt8());		}	}	else	{		// No alliances		for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; i++)		{			if(mapHeader->players[i].canComputerPlay || mapHeader->players[i].canHumanPlay)			{				mapHeader->players[i].team = TeamID(mapHeader->howManyTeams++);			}		}	}}void CMapLoaderH3M::readAllowedHeroes(){	mapHeader->allowedHeroes.resize(VLC->heroh->size(), true);	const int bytes = mapHeader->version == EMapFormat::ROE ? 16 : 20;	readBitmask(mapHeader->allowedHeroes,bytes,GameConstants::HEROES_QUANTITY, false);	// Probably reserved for further heroes	if(mapHeader->version > EMapFormat::ROE)	{		int placeholdersQty = reader->readUInt32();		reader->skip(placeholdersQty * 1);//		std::vector<ui16> placeholdedHeroes;////		for(int p = 0; p < placeholdersQty; ++p)//		{//			placeholdedHeroes.push_back(reader->readUInt8());//		}	}}void CMapLoaderH3M::readDisposedHeroes(){	// Reading disposed heroes (20 bytes)	if(map->version >= EMapFormat::SOD)	{		ui8 disp = reader->readUInt8();		map->disposedHeroes.resize(disp);		for(int g = 0; g < disp; ++g)		{			map->disposedHeroes[g].heroId = reader->readUInt8();			map->disposedHeroes[g].portrait = reader->readUInt8();			map->disposedHeroes[g].name = readLocalizedString();			map->disposedHeroes[g].players = reader->readUInt8();		}	}	//omitting NULLS	reader->skip(31);}void CMapLoaderH3M::readAllowedArtifacts(){	map->allowedArtifact.resize (VLC->arth->objects.size(),true); //handle new artifacts, make them allowed by default	// Reading allowed artifacts:  17 or 18 bytes	if(map->version != EMapFormat::ROE)	{		const int bytes = map->version == EMapFormat::AB ? 17 : 18;		readBitmask(map->allowedArtifact,bytes,GameConstants::ARTIFACTS_QUANTITY);	}	// ban combo artifacts	if (map->version == EMapFormat::ROE || map->version == EMapFormat::AB)	{		for(CArtifact * artifact : VLC->arth->objects)		{			// combo			if (artifact->constituents)			{				map->allowedArtifact[artifact->getId()] = false;			}		}		if (map->version == EMapFormat::ROE)		{			map->allowedArtifact[ArtifactID::ARMAGEDDONS_BLADE] = false;		}	}	// Messy, but needed	for (TriggeredEvent & event : map->triggeredEvents)	{		auto patcher = [&](EventCondition cond) -> EventExpression::Variant		{			if (cond.condition == EventCondition::HAVE_ARTIFACT ||				cond.condition == EventCondition::TRANSPORT)			{				map->allowedArtifact[cond.objectType] = false;			}			return cond;		};		event.trigger = event.trigger.morph(patcher);	}}void CMapLoaderH3M::readAllowedSpellsAbilities(){	// Read allowed spells, including new ones	map->allowedSpell.resize(VLC->spellh->objects.size(), true);	// Read allowed abilities	map->allowedAbilities.resize(GameConstants::SKILL_QUANTITY, true);	if(map->version >= EMapFormat::SOD)	{		// Reading allowed spells (9 bytes)		const int spell_bytes = 9;		readBitmask(map->allowedSpell, spell_bytes, GameConstants::SPELLS_QUANTITY);		// Allowed hero's abilities (4 bytes)		const int abil_bytes = 4;		readBitmask(map->allowedAbilities, abil_bytes, GameConstants::SKILL_QUANTITY);	}	//do not generate special abilities and spells	for (auto spell : VLC->spellh->objects)		if (spell->isSpecial() || spell->isCreatureAbility())			map->allowedSpell[spell->id] = false;}void CMapLoaderH3M::readRumors(){	int rumNr = reader->readUInt32();	for(int it = 0; it < rumNr; it++)	{		Rumor ourRumor;		ourRumor.name = readLocalizedString();		ourRumor.text = readLocalizedString();		map->rumors.push_back(ourRumor);	}}void CMapLoaderH3M::readPredefinedHeroes(){	switch(map->version)	{	case EMapFormat::WOG:	case EMapFormat::SOD:		{			// Disposed heroes			for(int z = 0; z < GameConstants::HEROES_QUANTITY; z++)			{				int custom =  reader->readUInt8();				if(!custom) continue;				auto * hero = new CGHeroInstance();				hero->ID = Obj::HERO;				hero->subID = z;				bool hasExp = reader->readBool();				if(hasExp)				{					hero->exp = reader->readUInt32();				}				else				{					hero->exp = 0;				}				bool hasSecSkills = reader->readBool();				if(hasSecSkills)				{					int howMany = reader->readUInt32();					hero->secSkills.resize(howMany);					for(int yy = 0; yy < howMany; ++yy)					{						hero->secSkills[yy].first = SecondarySkill(reader->readUInt8());						hero->secSkills[yy].second = reader->readUInt8();					}				}				loadArtifactsOfHero(hero);				bool hasCustomBio = reader->readBool();				if(hasCustomBio)				{					hero->biographyCustom = readLocalizedString();				}				// 0xFF is default, 00 male, 01 female				hero->sex = reader->readUInt8();				bool hasCustomSpells = reader->readBool();				if(hasCustomSpells)				{					readSpells(hero->spells);				}				bool hasCustomPrimSkills = reader->readBool();				if(hasCustomPrimSkills)				{					for(int xx = 0; xx < GameConstants::PRIMARY_SKILLS; xx++)					{						hero->pushPrimSkill(static_cast<PrimarySkill::PrimarySkill>(xx), reader->readUInt8());					}				}				map->predefinedHeroes.emplace_back(hero);			}			break;		}	case EMapFormat::ROE:		break;	}}void CMapLoaderH3M::loadArtifactsOfHero(CGHeroInstance * hero){	bool artSet = reader->readBool();	// True if artifact set is not default (hero has some artifacts)	if(artSet)	{	if(!hero->artifactsWorn.empty() || !hero->artifactsInBackpack.empty())	{		logGlobal->warn("Hero %s at %s has set artifacts twice (in map properties and on adventure map instance). Using the latter set...",						hero->getNameTranslated(),						hero->pos.toString());		hero->artifactsInBackpack.clear();		while(!hero->artifactsWorn.empty())				hero->eraseArtSlot(hero->artifactsWorn.begin()->first);	}		for(int pom = 0; pom < 16; pom++)		{			loadArtifactToSlot(hero, pom);		}		// misc5 art //17		if(map->version >= EMapFormat::SOD)		{			assert(!hero->getArt(ArtifactPosition::MACH4));			if(!loadArtifactToSlot(hero, ArtifactPosition::MACH4))			{				// catapult by default				assert(!hero->getArt(ArtifactPosition::MACH4));				hero->putArtifact(ArtifactPosition::MACH4, CArtifactInstance::createArtifact(map, ArtifactID::CATAPULT));			}		}		loadArtifactToSlot(hero, ArtifactPosition::SPELLBOOK);		// 19 //???what is that? gap in file or what? - it's probably fifth slot..		if(map->version > EMapFormat::ROE)		{			loadArtifactToSlot(hero, ArtifactPosition::MISC5);		}		else		{			reader->skip(1);		}		// bag artifacts //20		// number of artifacts in hero's bag		int amount = reader->readUInt16();		for(int ss = 0; ss < amount; ++ss)		{			loadArtifactToSlot(hero, GameConstants::BACKPACK_START + static_cast<int>(hero->artifactsInBackpack.size()));		}	}}bool CMapLoaderH3M::loadArtifactToSlot(CGHeroInstance * hero, int slot){	const int artmask = map->version == EMapFormat::ROE ? 0xff : 0xffff;	ArtifactID aid;	if(map->version == EMapFormat::ROE)	{		aid = ArtifactID(reader->readUInt8());	}	else	{		aid = ArtifactID(reader->readUInt16());	}	bool isArt  =  aid != artmask;	if(isArt)	{		const Artifact * art = ArtifactID(aid).toArtifact(VLC->artifacts());		if(nullptr == art)		{			logGlobal->warn("Invalid artifact in hero's backpack, ignoring...");			return false;		}		if(art->isBig() && slot >= GameConstants::BACKPACK_START)		{			logGlobal->warn("A big artifact (war machine) in hero's backpack, ignoring...");			return false;		}		if(aid == 0 && slot == ArtifactPosition::MISC5)		{			//TODO: check how H3 handles it -> art 0 in slot 18 in AB map			logGlobal->warn("Spellbook to MISC5 slot? Putting it spellbook place. AB format peculiarity? (format %d)", static_cast<int>(map->version));			slot = ArtifactPosition::SPELLBOOK;		}		// this is needed, because some H3M maps (last scenario of ROE map) contain invalid data like misplaced artifacts		auto * artifact = CArtifactInstance::createArtifact(map, aid);		auto artifactPos = ArtifactPosition(slot);		if (artifact->canBePutAt(ArtifactLocation(hero, artifactPos)))		{			hero->putArtifact(artifactPos, artifact);		}		else		{			logGlobal->debug("Artifact can't be put at the specified location."); //TODO add more debugging information		}	}	return isArt;}void CMapLoaderH3M::readTerrain(){	map->initTerrain();	// Read terrain	int3 pos;	for(pos.z = 0; pos.z < map->levels(); ++pos.z)	{		//OH3 format is [z][y][x]		for(pos.y = 0; pos.y < map->height; pos.y++)		{			for(pos.x = 0; pos.x < map->width; pos.x++)			{				auto & tile = map->getTile(pos);				tile.terType = const_cast<TerrainType*>(VLC->terrainTypeHandler->getByIndex(reader->readUInt8()));				tile.terView = reader->readUInt8();				tile.riverType = const_cast<RiverType*>(VLC->riverTypeHandler->getByIndex(reader->readUInt8()));				tile.riverDir = reader->readUInt8();				tile.roadType = const_cast<RoadType*>(VLC->roadTypeHandler->getByIndex(reader->readUInt8()));				tile.roadDir = reader->readUInt8();				tile.extTileFlags = reader->readUInt8();				tile.blocked = !tile.terType->isPassable();				tile.visitable = false;				assert(tile.terType->getId() != ETerrainId::NONE);			}		}	}}void CMapLoaderH3M::readDefInfo(){	int defAmount = reader->readUInt32();	templates.reserve(defAmount);	// Read custom defs	for(int idd = 0; idd < defAmount; ++idd)	{		auto * tmpl = new ObjectTemplate;		tmpl->readMap(*reader);		templates.push_back(std::shared_ptr<const ObjectTemplate>(tmpl));	}}void CMapLoaderH3M::readObjects(){	int howManyObjs = reader->readUInt32();	for(int ww = 0; ww < howManyObjs; ++ww)	{		CGObjectInstance * nobj = nullptr;		int3 objPos = readInt3();		int defnum = reader->readUInt32();		ObjectInstanceID idToBeGiven = ObjectInstanceID(static_cast<si32>(map->objects.size()));		std::shared_ptr<const ObjectTemplate> objTempl = templates.at(defnum);		reader->skip(5);		switch(objTempl->id)		{		case Obj::EVENT:			{				auto * evnt = new CGEvent();				nobj = evnt;				readMessageAndGuards(evnt->message, evnt);				evnt->gainedExp = reader->readUInt32();				evnt->manaDiff = reader->readUInt32();				evnt->moraleDiff = reader->readInt8();				evnt->luckDiff = reader->readInt8();				readResourses(evnt->resources);				evnt->primskills.resize(GameConstants::PRIMARY_SKILLS);				for(int x = 0; x < 4; ++x)				{					evnt->primskills[x] = static_cast<PrimarySkill::PrimarySkill>(reader->readUInt8());				}				int gabn = reader->readUInt8(); // Number of gained abilities				for(int oo = 0; oo < gabn; ++oo)				{					evnt->abilities.emplace_back(reader->readUInt8());					evnt->abilityLevels.push_back(reader->readUInt8());				}				int gart = reader->readUInt8(); // Number of gained artifacts				for(int oo = 0; oo < gart; ++oo)				{					if(map->version == EMapFormat::ROE)					{						evnt->artifacts.emplace_back(reader->readUInt8());					}					else					{						evnt->artifacts.emplace_back(reader->readUInt16());					}				}				int gspel = reader->readUInt8(); // Number of gained spells				for(int oo = 0; oo < gspel; ++oo)				{					evnt->spells.emplace_back(reader->readUInt8());				}				int gcre = reader->readUInt8(); //number of gained creatures				readCreatureSet(&evnt->creatures, gcre);				reader->skip(8);				evnt->availableFor = reader->readUInt8();				evnt->computerActivate = reader->readUInt8();				evnt->removeAfterVisit = reader->readUInt8();				evnt->humanActivate = true;				reader->skip(4);				break;			}		case Obj::HERO:		case Obj::RANDOM_HERO:		case Obj::PRISON:			{				nobj = readHero(idToBeGiven, objPos);				break;			}		case Obj::MONSTER:  //Monster		case Obj::RANDOM_MONSTER:		case Obj::RANDOM_MONSTER_L1:		case Obj::RANDOM_MONSTER_L2:		case Obj::RANDOM_MONSTER_L3:		case Obj::RANDOM_MONSTER_L4:		case Obj::RANDOM_MONSTER_L5:		case Obj::RANDOM_MONSTER_L6:		case Obj::RANDOM_MONSTER_L7:			{				auto * cre = new CGCreature();				nobj = cre;				if(map->version > EMapFormat::ROE)				{					cre->identifier = reader->readUInt32();					map->questIdentifierToId[cre->identifier] = idToBeGiven;				}				auto * hlp = new CStackInstance();				hlp->count = reader->readUInt16();				//type will be set during initialization				cre->putStack(SlotID(0), hlp);				cre->character = reader->readUInt8();				bool hasMessage = reader->readBool();				if(hasMessage)				{					cre->message = readLocalizedString();					readResourses(cre->resources);					int artID = 0;					if (map->version == EMapFormat::ROE)					{						artID = reader->readUInt8();					}					else					{						artID = reader->readUInt16();					}					if(map->version == EMapFormat::ROE)					{						if(artID != 0xff)						{							cre->gainedArtifact = ArtifactID(artID);						}						else						{							cre->gainedArtifact = ArtifactID::NONE;						}					}					else					{						if(artID != 0xffff)						{							cre->gainedArtifact = ArtifactID(artID);						}						else						{							cre->gainedArtifact = ArtifactID::NONE;						}					}				}				cre->neverFlees = reader->readUInt8();				cre->notGrowingTeam =reader->readUInt8();				reader->skip(2);				break;			}		case Obj::OCEAN_BOTTLE:		case Obj::SIGN:			{				auto * sb = new CGSignBottle();				nobj = sb;				sb->message = readLocalizedString();				reader->skip(4);				break;			}		case Obj::SEER_HUT:			{				nobj = readSeerHut();				break;			}		case Obj::WITCH_HUT:			{				auto * wh = new CGWitchHut();				nobj = wh;				// in RoE we cannot specify it - all are allowed (I hope)				if(map->version > EMapFormat::ROE)				{					for(int i = 0 ; i < 4; ++i)					{						ui8 c = reader->readUInt8();						for(int yy = 0; yy < 8; ++yy)						{							if(i * 8 + yy < GameConstants::SKILL_QUANTITY)							{								if(c == (c | static_cast<ui8>(std::pow(2., yy))))								{									wh->allowedAbilities.push_back(i * 8 + yy);								}							}						}					}					// enable new (modded) skills					if(wh->allowedAbilities.size() != 1)					{						for(int skillID = GameConstants::SKILL_QUANTITY; skillID < VLC->skillh->size(); ++skillID)							wh->allowedAbilities.push_back(skillID);					}				}				else				{					// RoE map					for(int skillID = 0; skillID < VLC->skillh->size(); ++skillID)						wh->allowedAbilities.push_back(skillID);				}				break;			}		case Obj::SCHOLAR:			{				auto * sch = new CGScholar();				nobj = sch;				sch->bonusType = static_cast<CGScholar::EBonusType>(reader->readUInt8());				sch->bonusID = reader->readUInt8();				reader->skip(6);				break;			}		case Obj::GARRISON:		case Obj::GARRISON2:			{				auto * gar = new CGGarrison();				nobj = gar;				nobj->setOwner(PlayerColor(reader->readUInt8()));				reader->skip(3);				readCreatureSet(gar, 7);				if(map->version > EMapFormat::ROE)				{					gar->removableUnits = reader->readBool();				}				else				{					gar->removableUnits = true;				}				reader->skip(8);				break;			}		case Obj::ARTIFACT:		case Obj::RANDOM_ART:		case Obj::RANDOM_TREASURE_ART:		case Obj::RANDOM_MINOR_ART:		case Obj::RANDOM_MAJOR_ART:		case Obj::RANDOM_RELIC_ART:		case Obj::SPELL_SCROLL:			{				auto artID = ArtifactID::NONE; //random, set later				int spellID = -1;				auto * art = new CGArtifact();				nobj = art;				readMessageAndGuards(art->message, art);				if(objTempl->id == Obj::SPELL_SCROLL)				{					spellID = reader->readUInt32();					artID = ArtifactID::SPELL_SCROLL;				}				else if(objTempl->id == Obj::ARTIFACT)				{					//specific artifact					artID = ArtifactID(objTempl->subid);				}				art->storedArtifact = CArtifactInstance::createArtifact(map, artID, spellID);				break;			}		case Obj::RANDOM_RESOURCE:		case Obj::RESOURCE:			{				auto * res = new CGResource();				nobj = res;				readMessageAndGuards(res->message, res);				res->amount = reader->readUInt32();				if(objTempl->subid == Res::GOLD)				{					// Gold is multiplied by 100.					res->amount *= 100;				}				reader->skip(4);				break;			}		case Obj::RANDOM_TOWN:		case Obj::TOWN:			{				nobj = readTown(objTempl->subid);				break;			}		case Obj::MINE:		case Obj::ABANDONED_MINE:			{				nobj = new CGMine();				nobj->setOwner(PlayerColor(reader->readUInt8()));				reader->skip(3);				break;			}		case Obj::CREATURE_GENERATOR1:		case Obj::CREATURE_GENERATOR2:		case Obj::CREATURE_GENERATOR3:		case Obj::CREATURE_GENERATOR4:			{				nobj = new CGDwelling();				nobj->setOwner(PlayerColor(reader->readUInt8()));				reader->skip(3);				break;			}		case Obj::SHRINE_OF_MAGIC_INCANTATION:		case Obj::SHRINE_OF_MAGIC_GESTURE:		case Obj::SHRINE_OF_MAGIC_THOUGHT:			{				auto * shr = new CGShrine();				nobj = shr;				ui8 raw_id = reader->readUInt8();				if (255 == raw_id)				{					shr->spell = SpellID(SpellID::NONE);				}				else				{					shr->spell = SpellID(raw_id);				}				reader->skip(3);				break;			}		case Obj::PANDORAS_BOX:			{				auto * box = new CGPandoraBox();				nobj = box;				readMessageAndGuards(box->message, box);				box->gainedExp = reader->readUInt32();				box->manaDiff = reader->readUInt32();				box->moraleDiff = reader->readInt8();				box->luckDiff = reader->readInt8();				readResourses(box->resources);				box->primskills.resize(GameConstants::PRIMARY_SKILLS);				for(int x = 0; x < 4; ++x)				{					box->primskills[x] = static_cast<PrimarySkill::PrimarySkill>(reader->readUInt8());				}				int gabn = reader->readUInt8();//number of gained abilities				for(int oo = 0; oo < gabn; ++oo)				{					box->abilities.emplace_back(reader->readUInt8());					box->abilityLevels.push_back(reader->readUInt8());				}				int gart = reader->readUInt8(); //number of gained artifacts				for(int oo = 0; oo < gart; ++oo)				{					if(map->version > EMapFormat::ROE)					{						box->artifacts.emplace_back(reader->readUInt16());					}					else					{						box->artifacts.emplace_back(reader->readUInt8());					}				}				int gspel = reader->readUInt8(); //number of gained spells				for(int oo = 0; oo < gspel; ++oo)				{					box->spells.emplace_back(reader->readUInt8());				}				int gcre = reader->readUInt8(); //number of gained creatures				readCreatureSet(&box->creatures, gcre);				reader->skip(8);				break;			}		case Obj::GRAIL:			{				map->grailPos = objPos;				map->grailRadius = reader->readUInt32();				continue;			}		case Obj::RANDOM_DWELLING: //same as castle + level range		case Obj::RANDOM_DWELLING_LVL: //same as castle, fixed level		case Obj::RANDOM_DWELLING_FACTION: //level range, fixed faction			{				auto * dwelling = new CGDwelling();				nobj = dwelling;				CSpecObjInfo * spec = nullptr;				switch(objTempl->id)				{				case Obj::RANDOM_DWELLING:					spec = new CCreGenLeveledCastleInfo();					break;				case Obj::RANDOM_DWELLING_LVL:					spec = new CCreGenAsCastleInfo();					break;				case Obj::RANDOM_DWELLING_FACTION:					spec = new CCreGenLeveledInfo();					break;				default:					throw std::runtime_error("Invalid random dwelling format");				}				spec->owner = dwelling;				nobj->setOwner(PlayerColor(reader->readUInt32()));				//216 and 217				if(auto * castleSpec = dynamic_cast<CCreGenAsCastleInfo *>(spec))				{					castleSpec->instanceId = "";					castleSpec->identifier = reader->readUInt32();					if(!castleSpec->identifier)					{						castleSpec->asCastle = false;						const int MASK_SIZE = 8;						ui8 mask[2];						mask[0] = reader->readUInt8();						mask[1] = reader->readUInt8();						castleSpec->allowedFactions.clear();						castleSpec->allowedFactions.resize(VLC->townh->size(), false);						for(int i = 0; i < MASK_SIZE; i++)							castleSpec->allowedFactions[i] = ((mask[0] & (1 << i))>0);						for(int i = 0; i < (GameConstants::F_NUMBER-MASK_SIZE); i++)							castleSpec->allowedFactions[i+MASK_SIZE] = ((mask[1] & (1 << i))>0);					}					else					{						castleSpec->asCastle = true;					}				}				//216 and 218				if(auto * lvlSpec = dynamic_cast<CCreGenLeveledInfo *>(spec))				{					lvlSpec->minLevel = std::max(reader->readUInt8(), static_cast<ui8>(1));					lvlSpec->maxLevel = std::min(reader->readUInt8(), static_cast<ui8>(7));				}				dwelling->info = spec;				break;			}		case Obj::QUEST_GUARD:			{				auto * guard = new CGQuestGuard();				readQuest(guard);				nobj = guard;				break;			}		case Obj::SHIPYARD:			{				nobj = new CGShipyard();				nobj->setOwner(PlayerColor(reader->readUInt32()));				break;			}		case Obj::HERO_PLACEHOLDER: //hero placeholder			{				auto * hp = new CGHeroPlaceholder();				nobj = hp;				hp->setOwner(PlayerColor(reader->readUInt8()));				int htid = reader->readUInt8(); //hero type id				nobj->subID = htid;				if(htid == 0xff)				{					hp->power = reader->readUInt8();					logGlobal->info("Hero placeholder: by power at %s", objPos.toString());				}				else				{					logGlobal->info("Hero placeholder: %s at %s", VLC->heroh->objects[htid]->getNameTranslated(), objPos.toString());					hp->power = 0;				}				break;			}		case Obj::BORDERGUARD:			{				nobj = new CGBorderGuard();				break;			}		case Obj::BORDER_GATE:			{				nobj = new CGBorderGate();				break;			}		case Obj::PYRAMID: //Pyramid of WoG object			{				if(objTempl->subid == 0)				{					nobj = new CBank();				}				else				{					//WoG object					//TODO: possible special handling					nobj = new CGObjectInstance();				}				break;			}		case Obj::LIGHTHOUSE: //Lighthouse			{				nobj = new CGLighthouse();				nobj->tempOwner = PlayerColor(reader->readUInt32());				break;			}		default: //any other object			{				if (VLC->objtypeh->knownSubObjects(objTempl->id).count(objTempl->subid))				{					nobj = VLC->objtypeh->getHandlerFor(objTempl->id, objTempl->subid)->create(objTempl);				}				else				{					logGlobal->warn("Unrecognized object: %d:%d at %s on map %s", objTempl->id.toEnum(), objTempl->subid, objPos.toString(), map->name);					nobj = new CGObjectInstance();				}				break;			}		}		nobj->pos = objPos;		nobj->ID = objTempl->id;		nobj->id = idToBeGiven;		if(nobj->ID != Obj::HERO && nobj->ID != Obj::HERO_PLACEHOLDER && nobj->ID != Obj::PRISON)		{			nobj->subID = objTempl->subid;		}		nobj->appearance = objTempl;		assert(idToBeGiven == ObjectInstanceID((si32)map->objects.size()));		{			//TODO: define valid typeName and subtypeName fro H3M maps			//boost::format fmt("%s_%d");			//fmt % nobj->typeName % nobj->id.getNum();			boost::format fmt("obj_%d");			fmt % nobj->id.getNum();			nobj->instanceName = fmt.str();		}		map->addNewObject(nobj);	}	std::sort(map->heroesOnMap.begin(), map->heroesOnMap.end(), [](const ConstTransitivePtr<CGHeroInstance> & a, const ConstTransitivePtr<CGHeroInstance> & b)	{		return a->subID < b->subID;	});}void CMapLoaderH3M::readCreatureSet(CCreatureSet * out, int number){	const bool version = (map->version > EMapFormat::ROE);	const int maxID = version ? 0xffff : 0xff;	for(int ir = 0; ir < number; ++ir)	{		CreatureID creID;		int count = 0;		if (version)		{			creID = CreatureID(reader->readUInt16());		}		else		{			creID = CreatureID(reader->readUInt8());		}		count = reader->readUInt16();		// Empty slot		if(creID == maxID)			continue;		auto * hlp = new CStackInstance();		hlp->count = count;		if(creID > maxID - 0xf)		{			//this will happen when random object has random army			hlp->idRand = maxID - creID - 1;		}		else		{			hlp->setType(creID);		}		out->putStack(SlotID(ir), hlp);	}	out->validTypes(true);}CGObjectInstance * CMapLoaderH3M::readHero(const ObjectInstanceID & idToBeGiven, const int3 & initialPos){	auto * nhi = new CGHeroInstance();	if(map->version > EMapFormat::ROE)	{		unsigned int identifier = reader->readUInt32();		map->questIdentifierToId[identifier] = idToBeGiven;	}	PlayerColor owner = PlayerColor(reader->readUInt8());	nhi->subID = reader->readUInt8();	assert(!nhi->getArt(ArtifactPosition::MACH4));	//If hero of this type has been predefined, use that as a base.	//Instance data will overwrite the predefined values where appropriate.	for(auto & elem : map->predefinedHeroes)	{		if(elem->subID == nhi->subID)		{			logGlobal->debug("Hero %d will be taken from the predefined heroes list.", nhi->subID);			delete nhi;			nhi = elem;			break;		}	}	nhi->setOwner(owner);	nhi->portrait = nhi->subID;	for(auto & elem : map->disposedHeroes)	{		if(elem.heroId == nhi->subID)		{			nhi->nameCustom = elem.name;			nhi->portrait = elem.portrait;			break;		}	}	bool hasName = reader->readBool();	if(hasName)	{		nhi->nameCustom = readLocalizedString();	}	if(map->version > EMapFormat::AB)	{		bool hasExp = reader->readBool();		if(hasExp)		{			nhi->exp = reader->readUInt32();		}		else		{			nhi->exp = CGHeroInstance::UNINITIALIZED_EXPERIENCE;		}	}	else	{		nhi->exp = reader->readUInt32();		//0 means "not set" in <=AB maps		if(!nhi->exp)		{			nhi->exp = CGHeroInstance::UNINITIALIZED_EXPERIENCE;		}	}	bool hasPortrait = reader->readBool();	if(hasPortrait)	{		nhi->portrait = reader->readUInt8();	}	bool hasSecSkills = reader->readBool();	if(hasSecSkills)	{		if(!nhi->secSkills.empty())		{			nhi->secSkills.clear();			//logGlobal->warn("Hero %s subID=%d has set secondary skills twice (in map properties and on adventure map instance). Using the latter set...", nhi->name, nhi->subID);		}		int howMany = reader->readUInt32();		nhi->secSkills.resize(howMany);		for(int yy = 0; yy < howMany; ++yy)		{			nhi->secSkills[yy].first = SecondarySkill(reader->readUInt8());			nhi->secSkills[yy].second = reader->readUInt8();		}	}	bool hasGarison = reader->readBool();	if(hasGarison)	{		readCreatureSet(nhi, 7);	}	nhi->formation = reader->readUInt8();	loadArtifactsOfHero(nhi);	nhi->patrol.patrolRadius = reader->readUInt8();	nhi->patrol.patrolling = (nhi->patrol.patrolRadius != 0xff);	if(map->version > EMapFormat::ROE)	{		bool hasCustomBiography = reader->readBool();		if(hasCustomBiography)		{			nhi->biographyCustom = readLocalizedString();		}		nhi->sex = reader->readUInt8();		// Remove trash		if (nhi->sex != 0xFF)		{			nhi->sex &= 1;		}	}	else	{		nhi->sex = 0xFF;	}	// Spells	if(map->version > EMapFormat::AB)	{		bool hasCustomSpells = reader->readBool();		if(!nhi->spells.empty())		{			nhi->clear();			logGlobal->warn("Hero %s subID=%d has spells set twice (in map properties and on adventure map instance). Using the latter set...", nhi->getNameTranslated(), nhi->subID);		}		if(hasCustomSpells)		{			nhi->spells.insert(SpellID::PRESET); //placeholder "preset spells"			readSpells(nhi->spells);		}	}	else if(map->version == EMapFormat::AB)	{		//we can read one spell		ui8 buff = reader->readUInt8();		if(buff != 254)		{			nhi->spells.insert(SpellID::PRESET); //placeholder "preset spells"			if(buff < 254) //255 means no spells			{				nhi->spells.insert(SpellID(buff));			}		}	}	if(map->version > EMapFormat::AB)	{		bool hasCustomPrimSkills = reader->readBool();		if(hasCustomPrimSkills)		{			auto ps = nhi->getAllBonuses(Selector::type()(Bonus::PRIMARY_SKILL)								.And(Selector::sourceType()(Bonus::HERO_BASE_SKILL)), nullptr);			if(ps->size())			{				logGlobal->warn("Hero %s subID=%d has set primary skills twice (in map properties and on adventure map instance). Using the latter set...", nhi->getNameTranslated(), nhi->subID);				for(const auto & b : *ps)					nhi->removeBonus(b);			}			for(int xx = 0; xx < GameConstants::PRIMARY_SKILLS; ++xx)			{				nhi->pushPrimSkill(static_cast<PrimarySkill::PrimarySkill>(xx), reader->readUInt8());			}		}	}	reader->skip(16);	return nhi;}CGSeerHut * CMapLoaderH3M::readSeerHut(){	auto * hut = new CGSeerHut();	if(map->version > EMapFormat::ROE)	{		readQuest(hut);	}	else	{		//RoE		auto artID = ArtifactID(reader->readUInt8());		if (artID != 255)		{			//not none quest			hut->quest->addArtifactID(artID);			hut->quest->missionType = CQuest::MISSION_ART;		}		else		{			hut->quest->missionType = CQuest::MISSION_NONE;		}		hut->quest->lastDay = -1; //no timeout		hut->quest->isCustomFirst = hut->quest->isCustomNext = hut->quest->isCustomComplete = false;	}	if (hut->quest->missionType)	{		auto rewardType = static_cast<CGSeerHut::ERewardType>(reader->readUInt8());		hut->rewardType = rewardType;		switch(rewardType)		{		case CGSeerHut::EXPERIENCE:			{				hut->rVal = reader->readUInt32();				break;			}		case CGSeerHut::MANA_POINTS:			{				hut->rVal = reader->readUInt32();				break;			}		case CGSeerHut::MORALE_BONUS:			{				hut->rVal = reader->readUInt8();				break;			}		case CGSeerHut::LUCK_BONUS:			{				hut->rVal = reader->readUInt8();				break;			}		case CGSeerHut::RESOURCES:			{				hut->rID = reader->readUInt8();				// Only the first 3 bytes are used. Skip the 4th.				hut->rVal = reader->readUInt32() & 0x00ffffff;				break;			}		case CGSeerHut::PRIMARY_SKILL:			{				hut->rID = reader->readUInt8();				hut->rVal = reader->readUInt8();				break;			}		case CGSeerHut::SECONDARY_SKILL:			{				hut->rID = reader->readUInt8();				hut->rVal = reader->readUInt8();				break;			}		case CGSeerHut::ARTIFACT:			{				if (map->version == EMapFormat::ROE)				{					hut->rID = reader->readUInt8();				}				else				{					hut->rID = reader->readUInt16();				}				break;			}		case CGSeerHut::SPELL:			{				hut->rID = reader->readUInt8();				break;			}		case CGSeerHut::CREATURE:			{				if(map->version > EMapFormat::ROE)				{					hut->rID = reader->readUInt16();					hut->rVal = reader->readUInt16();				}				else				{					hut->rID = reader->readUInt8();					hut->rVal = reader->readUInt16();				}				break;			}		}		reader->skip(2);	}	else	{		// missionType==255		reader->skip(3);	}	return hut;}void CMapLoaderH3M::readQuest(IQuestObject * guard){	guard->quest->missionType = static_cast<CQuest::Emission>(reader->readUInt8());	switch(guard->quest->missionType)	{	case CQuest::MISSION_NONE:		return;	case CQuest::MISSION_PRIMARY_STAT:		{			guard->quest->m2stats.resize(4);			for(int x = 0; x < 4; ++x)			{				guard->quest->m2stats[x] = reader->readUInt8();			}		}		break;	case CQuest::MISSION_LEVEL:	case CQuest::MISSION_KILL_HERO:	case CQuest::MISSION_KILL_CREATURE:		{			guard->quest->m13489val = reader->readUInt32();			break;		}	case CQuest::MISSION_ART:		{			int artNumber = reader->readUInt8();			for(int yy = 0; yy < artNumber; ++yy)			{				auto artid = ArtifactID(reader->readUInt16());				guard->quest->addArtifactID(artid);				map->allowedArtifact[artid] = false; //these are unavailable for random generation			}			break;		}	case CQuest::MISSION_ARMY:		{			int typeNumber = reader->readUInt8();			guard->quest->m6creatures.resize(typeNumber);			for(int hh = 0; hh < typeNumber; ++hh)			{				guard->quest->m6creatures[hh].type = VLC->creh->objects[reader->readUInt16()];				guard->quest->m6creatures[hh].count = reader->readUInt16();			}			break;		}	case CQuest::MISSION_RESOURCES:		{			guard->quest->m7resources.resize(7);			for(int x = 0; x < 7; ++x)			{				guard->quest->m7resources[x] = reader->readUInt32();			}			break;		}	case CQuest::MISSION_HERO:	case CQuest::MISSION_PLAYER:		{			guard->quest->m13489val = reader->readUInt8();			break;		}	}	int limit = reader->readUInt32();	if(limit == (static_cast<int>(0xffffffff)))	{		guard->quest->lastDay = -1;	}	else	{		guard->quest->lastDay = limit;	}	guard->quest->firstVisitText = readLocalizedString();	guard->quest->nextVisitText = readLocalizedString();	guard->quest->completedText = readLocalizedString();	guard->quest->isCustomFirst = !guard->quest->firstVisitText.empty();	guard->quest->isCustomNext = !guard->quest->nextVisitText.empty();	guard->quest->isCustomComplete = !guard->quest->completedText.empty();}CGTownInstance * CMapLoaderH3M::readTown(int castleID){	auto * nt = new CGTownInstance();	if(map->version > EMapFormat::ROE)	{		nt->identifier = reader->readUInt32();	}	nt->tempOwner = PlayerColor(reader->readUInt8());	bool hasName = reader->readBool();	if(hasName)	{		nt->setNameTranslated( readLocalizedString());	}	bool hasGarrison = reader->readBool();	if(hasGarrison)	{		readCreatureSet(nt, 7);	}	nt->formation = reader->readUInt8();	bool hasCustomBuildings = reader->readBool();	if(hasCustomBuildings)	{		readBitmask(nt->builtBuildings,6,48,false);		readBitmask(nt->forbiddenBuildings,6,48,false);		nt->builtBuildings = convertBuildings(nt->builtBuildings, castleID);		nt->forbiddenBuildings = convertBuildings(nt->forbiddenBuildings, castleID);	}	// Standard buildings	else	{		bool hasFort = reader->readBool();		if(hasFort)		{			nt->builtBuildings.insert(BuildingID::FORT);		}		//means that set of standard building should be included		nt->builtBuildings.insert(BuildingID::DEFAULT);	}	if(map->version > EMapFormat::ROE)	{		for(int i = 0; i < 9; ++i)		{			ui8 c = reader->readUInt8();			for(int yy = 0; yy < 8; ++yy)			{				if(i * 8 + yy < GameConstants::SPELLS_QUANTITY)				{					if(c == (c | static_cast<ui8>(std::pow(2., yy)))) //add obligatory spell even if it's banned on a map (?)					{						nt->obligatorySpells.emplace_back(i * 8 + yy);					}				}			}		}	}	for(int i = 0; i < 9; ++i)	{		ui8 c = reader->readUInt8();		for(int yy = 0; yy < 8; ++yy)		{			int spellid = i * 8 + yy;			if(spellid < GameConstants::SPELLS_QUANTITY)			{				if(c != (c | static_cast<ui8>(std::pow(2., yy))) && map->allowedSpell[spellid]) //add random spell only if it's allowed on entire map				{					nt->possibleSpells.emplace_back(spellid);				}			}		}	}	//add all spells from mods	//TODO: allow customize new spells in towns	for (int i = SpellID::AFTER_LAST; i < VLC->spellh->objects.size(); ++i)	{		nt->possibleSpells.emplace_back(i);	}	// Read castle events	int numberOfEvent = reader->readUInt32();	for(int gh = 0; gh < numberOfEvent; ++gh)	{		CCastleEvent nce;		nce.town = nt;		nce.name = readLocalizedString();		nce.message = readLocalizedString();		readResourses(nce.resources);		nce.players = reader->readUInt8();		if(map->version > EMapFormat::AB)		{			nce.humanAffected = reader->readUInt8();		}		else		{			nce.humanAffected = true;		}		nce.computerAffected = reader->readUInt8();		nce.firstOccurence = reader->readUInt16();		nce.nextOccurence =  reader->readUInt8();		reader->skip(17);		// New buildings		readBitmask(nce.buildings,6,48,false);		nce.buildings = convertBuildings(nce.buildings, castleID, false);		nce.creatures.resize(7);		for(int vv = 0; vv < 7; ++vv)		{			nce.creatures[vv] = reader->readUInt16();		}		reader->skip(4);		nt->events.push_back(nce);	}	if(map->version > EMapFormat::AB)	{		nt->alignment = reader->readUInt8();	}	reader->skip(3);	return nt;}std::set<BuildingID> CMapLoaderH3M::convertBuildings(const std::set<BuildingID> & h3m, int castleID, bool addAuxiliary) const{	std::map<int, BuildingID> mapa;	std::set<BuildingID> ret;	// Note: this file is parsed many times.	const JsonNode config(ResourceID("config/buildings5.json"));	for(const JsonNode & entry : config["table"].Vector())	{		int town = static_cast<int>(entry["town"].Float());		if (town == castleID || town == -1)		{			mapa[static_cast<int>(entry["h3"].Float())] = BuildingID(static_cast<si32>(entry["vcmi"].Float()));		}	}	for(const auto & elem : h3m)	{		if(mapa[elem] >= 0)		{			ret.insert(mapa[elem]);		}		// horde buildings		else if(mapa[elem] >= (-GameConstants::CREATURES_PER_TOWN))		{			int level = (mapa[elem]);			//(-30)..(-36) - horde buildings (for game loading only), don't see other way to handle hordes in random towns			ret.insert(BuildingID(level - 30));		}		else		{			logGlobal->warn("Conversion warning: unknown building %d in castle %d", elem.num, castleID);		}	}	if(addAuxiliary)	{		//village hall is always present		ret.insert(BuildingID::VILLAGE_HALL);		if(ret.find(BuildingID::CITY_HALL) != ret.end())		{			ret.insert(BuildingID::EXTRA_CITY_HALL);		}		if(ret.find(BuildingID::TOWN_HALL) != ret.end())		{			ret.insert(BuildingID::EXTRA_TOWN_HALL);		}		if(ret.find(BuildingID::CAPITOL) != ret.end())		{			ret.insert(BuildingID::EXTRA_CAPITOL);		}	}	return ret;}void CMapLoaderH3M::readEvents(){	int numberOfEvents = reader->readUInt32();	for(int yyoo = 0; yyoo < numberOfEvents; ++yyoo)	{		CMapEvent ne;		ne.name = readLocalizedString();		ne.message = readLocalizedString();		readResourses(ne.resources);		ne.players = reader->readUInt8();		if(map->version > EMapFormat::AB)		{			ne.humanAffected = reader->readUInt8();		}		else		{			ne.humanAffected = true;		}		ne.computerAffected = reader->readUInt8();		ne.firstOccurence = reader->readUInt16();		ne.nextOccurence = reader->readUInt8();		reader->skip(17);		map->events.push_back(ne);	}}void CMapLoaderH3M::readMessageAndGuards(std::string& message, CCreatureSet* guards){	bool hasMessage = reader->readBool();	if(hasMessage)	{		message = readLocalizedString();		bool hasGuards = reader->readBool();		if(hasGuards)		{			readCreatureSet(guards, 7);		}		reader->skip(4);	}}void CMapLoaderH3M::readSpells(std::set<SpellID>& dest){	readBitmask(dest,9,GameConstants::SPELLS_QUANTITY,false);}void CMapLoaderH3M::readResourses(TResources& resources){	resources.resize(GameConstants::RESOURCE_QUANTITY); //needed?	for(int x = 0; x < 7; ++x)	{		resources[x] = reader->readUInt32();	}}template <class Indentifier>void CMapLoaderH3M::readBitmask(std::set<Indentifier>& dest, const int byteCount, const int limit, bool negate){	std::vector<bool> temp;	temp.resize(limit,true);	readBitmask(temp, byteCount, limit, negate);	for(int i = 0; i< std::min(temp.size(), static_cast<size_t>(limit)); i++)	{		if(temp[i])		{			dest.insert(static_cast<Indentifier>(i));		}	}}void CMapLoaderH3M::readBitmask(std::vector<bool>& dest, const int byteCount, const int limit, bool negate){	for(int byte = 0; byte < byteCount; ++byte)	{		const ui8 mask = reader->readUInt8();		for(int bit = 0; bit < 8; ++bit)		{			if(byte * 8 + bit < limit)			{				const bool flag = mask & (1 << bit);				if((negate && flag) || (!negate && !flag)) // FIXME: check PR388					dest[byte * 8 + bit] = false;			}		}	}}ui8 CMapLoaderH3M::reverse(ui8 arg) const{	ui8 ret = 0;	for(int i = 0; i < 8; ++i)	{		if((arg & (1 << i)) >> i)		{			ret |= (128 >> i);		}	}	return ret;}int3 CMapLoaderH3M::readInt3(){	int3 p;	p.x = reader->readUInt8();	p.y = reader->readUInt8();	p.z = reader->readUInt8();	return p;}std::string CMapLoaderH3M::readLocalizedString(){	return TextOperations::toUnicode(reader->readBaseString(), fileEncoding);}void CMapLoaderH3M::afterRead(){	//convert main town positions for all players to actual object position, in H3M it is position of active tile	for(auto & p : map->players)	{		int3 posOfMainTown = p.posOfMainTown;		if(posOfMainTown.valid() && map->isInTheMap(posOfMainTown))		{			const TerrainTile & t = map->getTile(posOfMainTown);			const CGObjectInstance * mainTown = nullptr;			for(auto * obj : t.visitableObjects)			{				if(obj->ID == Obj::TOWN || obj->ID == Obj::RANDOM_TOWN)				{					mainTown = obj;					break;				}			}			if(mainTown == nullptr)				continue;			p.posOfMainTown = posOfMainTown + mainTown->getVisitableOffset();		}	}}VCMI_LIB_NAMESPACE_END
 |