-- глобальные переменные
Diff = 0;
aTown = '';
aPlayer = 0;
aHero = '';
attack = 0;
heroCount = 0;
townCount = 0;
progress = 0;

-- переменные героев
Demon = 'Jazaz';
Ork = 'Hero3';
Undead = 'Pelt';
demonIsActive = 0;
orkIsActive = 0;
necroIsActive = 0;
Fandross = 'Sarge';

-- таблицы
orepits = {'OrePit1', 'OrePit2', 'OrePit3', 'OrePit4', 'OrePit5'};
caverns = {'CrCav1', 'CrCav2', 'CrCav3', 'CrCav4'};
labs = {'AcLab1', 'AcLab2', 'AcLab3', 'AcLab4'};
garrisonList = {'Garrison1', 'Garrison2', 'Garrison3', 'Garrison4'};

-- загрузка сложности --
function difficulty()
	if GetDifficulty() == DIFFICULTY_EASY then
		Diff = 1;
	elseif GetDifficulty() == DIFFICULTY_NORMAL then
		Diff = 2;
	elseif GetDifficulty() == DIFFICULTY_HARD then
		Diff = 3;
	elseif GetDifficulty() == DIFFICULTY_HEROIC then
		Diff = 4;
	end;
	startThread(initSetMap);
end;

-- загрузка данных карты --
function initSetMap()
	SetRegionBlocked('humReg1', true, PLAYER_5);
	SetRegionBlocked('humReg2', true, PLAYER_5);
	SetRegionBlocked('humReg3', true, PLAYER_5);
	SetRegionBlocked('humReg4', true, PLAYER_5);
	SetRegionBlocked('blockUnderground01', true, PLAYER_5);
	SetRegionBlocked('blockUnderground02', true, PLAYER_5);
	SetRegionBlocked('blockUnderground03', true, PLAYER_5);
	SetRegionBlocked('blockUnderground04', true, PLAYER_5);
	SetRegionBlocked('blockUnderground01', true, PLAYER_4);
	SetRegionBlocked('blockUnderground02', true, PLAYER_4);
	SetRegionBlocked('blockUnderground03', true, PLAYER_4);
	SetRegionBlocked('blockUnderground04', true, PLAYER_4);
	
	BlockTownGarrisonForAI('Htown5', true);
	AddObjectCreatures('Htown5', 14, 12*Diff, 1);
	AddObjectCreatures('Htown5', 12, 32*Diff, 2);
	AddObjectCreatures('Htown5', 10, 54*Diff, 3);
	AddObjectCreatures('Htown5', 14, 18*Diff, 4);
	AddObjectCreatures('Htown5', 10, 54*Diff, 5);
	AddObjectCreatures('Htown5', 12, 32*Diff, 6);
	AddObjectCreatures('Htown5', 14, 12*Diff, 7);
	ChangeHeroStat(Fandross, 0, 36500*Diff);
	ChangeHeroStat(Fandross, 1, 15*Diff);
	ChangeHeroStat(Fandross, 2, 11*Diff);
	ChangeHeroStat(Fandross, 3, 10*Diff);
	ChangeHeroStat(Fandross, 4, 8*Diff);
	EnableHeroAI(Fandross, nil);
	EnableHeroAI('Maeve', nil);
	EnableHeroAI('RedHeavenHero03', nil);
	SetObjectEnabled('RedHeavenHero03', nil);
	EnableHeroAI('Ottar', nil);
	AddObjectCreatures('UnGarrison', 104, 52*Diff, 1);
	AddObjectCreatures('UnGarrison', 104, 52*Diff, 2);
	AddObjectCreatures('UnGarrison', 104, 52*Diff, 3);
	
	startThread(cc);
	
	if IsAIPlayer(PLAYER_1) == 0 then
		loadPersonalSettings(1);
	elseif IsAIPlayer(PLAYER_2) == 0 then
		loadPersonalSettings(2);
	elseif IsAIPlayer(PLAYER_3) == 0 then
		loadPersonalSettings(3);
	end;
end;

-- загрузка дополнительных данных карты --
function loadPersonalSettings(rom)
	if rom == 1 then
		SetRegionBlocked('demonReg1', true, PLAYER_3);
		SetRegionBlocked('necroReg1', true, PLAYER_2);
		SetRegionBlocked('demonReg1', true, PLAYER_4);
		SetRegionBlocked('necroReg1', true, PLAYER_4);
		SetRegionBlocked('humReg1', true, PLAYER_4);
		SetRegionBlocked('humReg3', true, PLAYER_4);
		SetRegionBlocked('humReg5', true, PLAYER_4);
		SetRegionBlocked('blockUnderground01', true, PLAYER_3);
		SetRegionBlocked('blockUnderground02', true, PLAYER_3);
		SetRegionBlocked('blockUnderground03', true, PLAYER_3);
		SetRegionBlocked('blockUnderground04', true, PLAYER_3);
		SetRegionBlocked('blockUnderground01', true, PLAYER_2);
		SetRegionBlocked('blockUnderground02', true, PLAYER_2);
		SetRegionBlocked('blockUnderground03', true, PLAYER_2);
		SetRegionBlocked('blockUnderground04', true, PLAYER_2);
		Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'blockUnderground01', 'demonRegUnblock');
		Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'blockUnderground03', 'necroRegUnblock');
		Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'demonReg1', 'demonRegUnblock');
		Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'necroReg1', 'necroRegUnblock');
		Trigger(OBJECT_TOUCH_TRIGGER, 'DemonTown', 'checkQuestTown3');
		Trigger(OBJECT_TOUCH_TRIGGER, 'NecroTown', 'checkQuestTown2');
		Trigger(PLAYER_REMOVE_HERO_TRIGGER, PLAYER_3, 'checkQuestHero');
		Trigger(PLAYER_REMOVE_HERO_TRIGGER, PLAYER_2, 'checkQuestHero');
		
		EnableHeroAI(Demon, nil);
		EnableHeroAI(Undead, nil);
		SetAIPlayerAttractor('DemonTown', PLAYER_4, -1);
		SetAIPlayerAttractor('NecroTown', PLAYER_4, -1);
		SetAIPlayerAttractor('HordeTown', PLAYER_4, 1);
		SetAIPlayerAttractor('HordeTown', PLAYER_5, 1);
		SetAIPlayerAttractor('DemonTown', PLAYER_5, 0);
		SetAIPlayerAttractor('NecroTown', PLAYER_5, 0);
		SetAIPlayerAttractor('Htown5', PLAYER_3, -1);
		SetAIPlayerAttractor('Htown5', PLAYER_2, -1);
		SetAIPlayerAttractor(Demon, PLAYER_4, 0);
		SetAIPlayerAttractor(Undead, PLAYER_4, 0);
		
		AddHeroCreatures(Ork, 117, 15-Diff*2);
		AddHeroCreatures(Ork, 119, 22-Diff*4);
		AddHeroCreatures(Ork, 121, 12-Diff*3);
		aTown = 'HordeTown';
		aPlayer = PLAYER_1;
		aHero = Ork;
	elseif rom == 2 then
		SetRegionBlocked('demonReg1', true, PLAYER_3);
		SetRegionBlocked('orkReg2', true, PLAYER_1);
		SetRegionBlocked('demonReg1', true, PLAYER_4);
		SetRegionBlocked('orkReg2', true, PLAYER_4);
		SetRegionBlocked('humReg1', true, PLAYER_4);
		SetRegionBlocked('humReg2', true, PLAYER_4);
		SetRegionBlocked('humReg5', true, PLAYER_4);
		SetRegionBlocked('blockUnderground01', true, PLAYER_3);
		SetRegionBlocked('blockUnderground02', true, PLAYER_3);
		SetRegionBlocked('blockUnderground03', true, PLAYER_3);
		SetRegionBlocked('blockUnderground04', true, PLAYER_3);
		SetRegionBlocked('blockUnderground01', true, PLAYER_1);
		SetRegionBlocked('blockUnderground02', true, PLAYER_1);
		SetRegionBlocked('blockUnderground03', true, PLAYER_1);
		SetRegionBlocked('blockUnderground04', true, PLAYER_1);
		Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'blockUnderground01', 'demonRegUnblock');
		Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'blockUnderground02', 'orkRegUnblock');
		Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'demonReg1', 'demonRegUnblock');
		Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'orkReg2', 'orkRegUnblock');
		Trigger(OBJECT_TOUCH_TRIGGER, 'DemonTown', 'checkQuestTown3');
		Trigger(OBJECT_TOUCH_TRIGGER, 'HordeTown', 'checkQuestTown1');
		Trigger(PLAYER_REMOVE_HERO_TRIGGER, PLAYER_3, 'checkQuestHero');
		Trigger(PLAYER_REMOVE_HERO_TRIGGER, PLAYER_1, 'checkQuestHero');
		
		EnableHeroAI(Demon, nil);
		EnableHeroAI(Ork, nil);
		SetAIPlayerAttractor('DemonTown', PLAYER_4, -1);
		SetAIPlayerAttractor('HordeTown', PLAYER_4, -1);
		SetAIPlayerAttractor('NecroTown', PLAYER_4, 1);
		SetAIPlayerAttractor('NecroTown', PLAYER_5, 1);
		SetAIPlayerAttractor('DemonTown', PLAYER_5, 0);
		SetAIPlayerAttractor('HordeTown', PLAYER_5, 0);
		SetAIPlayerAttractor('Htown5', PLAYER_3, -1);
		SetAIPlayerAttractor('Htown5', PLAYER_1, -1);
		SetAIPlayerAttractor(Demon, PLAYER_4, 0);
		SetAIPlayerAttractor(Ork, PLAYER_4, 0);
		
		AddHeroCreatures(Undead, 29, 17-Diff*2);
		AddHeroCreatures(Undead, 31, 25-Diff*4);
		AddHeroCreatures(Undead, 33, 16-Diff*4);
		aTown = 'NecroTown';
		aPlayer = PLAYER_2;
		aHero = Undead;
	elseif rom == 3 then
		SetRegionBlocked('necroReg1', true, PLAYER_2);
		SetRegionBlocked('orkReg1', true, PLAYER_1);
		SetRegionBlocked('necroReg1', true, PLAYER_4);
		SetRegionBlocked('orkReg1', true, PLAYER_4);
		SetRegionBlocked('humReg2', true, PLAYER_4);
		SetRegionBlocked('humReg3', true, PLAYER_4);
		SetRegionBlocked('blockUnderground01', true, PLAYER_1);
		SetRegionBlocked('blockUnderground02', true, PLAYER_1);
		SetRegionBlocked('blockUnderground03', true, PLAYER_1);
		SetRegionBlocked('blockUnderground04', true, PLAYER_1);
		SetRegionBlocked('blockUnderground01', true, PLAYER_2);
		SetRegionBlocked('blockUnderground02', true, PLAYER_2);
		SetRegionBlocked('blockUnderground03', true, PLAYER_2);
		SetRegionBlocked('blockUnderground04', true, PLAYER_2);
		Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'blockUnderground03', 'necroRegUnblock');
		Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'blockUnderground02', 'orkRegUnblock');
		Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'necroReg1', 'necroRegUnblock');
		Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'orkReg1', 'orkRegUnblock');
		Trigger(OBJECT_TOUCH_TRIGGER, 'HordeTown', 'checkQuestTown1');
		Trigger(OBJECT_TOUCH_TRIGGER, 'NecroTown', 'checkQuestTown2');
		Trigger(PLAYER_REMOVE_HERO_TRIGGER, PLAYER_1, 'checkQuestHero');
		Trigger(PLAYER_REMOVE_HERO_TRIGGER, PLAYER_2, 'checkQuestHero');
		
		EnableHeroAI(Ork, nil);
		EnableHeroAI(Undead, nil);
		SetAIPlayerAttractor('NecroTown', PLAYER_4, -1);
		SetAIPlayerAttractor('HordeTown', PLAYER_4, -1);
		SetAIPlayerAttractor('DemonTown', PLAYER_4, 1);
		SetAIPlayerAttractor('DemonTown', PLAYER_5, 1);
		SetAIPlayerAttractor('NecroTown', PLAYER_5, 0);
		SetAIPlayerAttractor('HordeTown', PLAYER_5, 0);
		SetAIPlayerAttractor('Htown5', PLAYER_1, -1);
		SetAIPlayerAttractor('Htown5', PLAYER_2, -1);
		SetAIPlayerAttractor(Undead, PLAYER_4, 0);
		SetAIPlayerAttractor(Ork, PLAYER_4, 0);
		
		AddHeroCreatures(Demon, 15, 16-Diff*2);
		AddHeroCreatures(Demon, 17, 24-Diff*4);
		AddHeroCreatures(Demon, 19, 12-Diff*3);
		aTown = 'DemonTown';
		aPlayer = PLAYER_3;
		aHero = Demon;
	end;
end;

function cc()
	for pt = 1, length(orepits) do
		Trigger(OBJECT_CAPTURE_TRIGGER, orepits[pt], 'checkOwner1');
		AddObjectCreatures(orepits[pt], 1, (20+random(5))*Diff);
		AddObjectCreatures(orepits[pt], 3, (10+random(3))*Diff);
		SetAIPlayerAttractor(orepits[pt], PLAYER_5, 0);
	end;
	for cv = 1, length(caverns) do
		Trigger(OBJECT_CAPTURE_TRIGGER, caverns[cv], 'checkOwner2');
		AddObjectCreatures(caverns[cv], 1, (20+random(5))*Diff);
		AddObjectCreatures(caverns[cv], 3, (10+random(3))*Diff);
		SetAIPlayerAttractor(caverns[cv], PLAYER_5, 0);
	end;
	for lb = 1, length(labs) do
		Trigger(OBJECT_CAPTURE_TRIGGER, labs[lb], 'checkOwner3');
		AddObjectCreatures(labs[lb], 1, (20+random(5))*Diff);
		AddObjectCreatures(labs[lb], 3, (10+random(3))*Diff);
		SetAIPlayerAttractor(labs[lb], PLAYER_5, 0);
	end;
	
	for g = 1, length(garrisonList) do
		AddObjectCreatures(garrisonList[g], 13, (8+random(3))*Diff, 1);
		AddObjectCreatures(garrisonList[g], 13, (8+random(3))*Diff, 2);
		AddObjectCreatures(garrisonList[g], 13, (8+random(3))*Diff, 3);
		DenyGarrisonCreaturesTakeAway(garrisonList[g], 0);
	end;
end;

-- задания --
function checkQuestTown1(hero, object)
	sleep(5);
	if IsHeroAlive (hero) == true and hero ~= Ork then
		Trigger(OBJECT_TOUCH_TRIGGER, 'HordeTown', nil);
		if IsHeroInTown(Ork, 'HordeTown') == true then
			Trigger(PLAYER_REMOVE_HERO_TRIGGER, PLAYER_1, nil);
			heroCount = heroCount + 1;
		end;
		local x, y = GetObjectPosition('HordeTown');
		Play2DSound('/Maps/SingleMissions/Aliralys/Siege_WallCrash02sound.xdb#xpointer(/Sound)');
		PlayVisualEffect('/Effects/_(Effect)/Start_dust_S.xdb#xpointer(/Effect)', '','rTown1', x, y, 0, 0);
		PlayVisualEffect('/Effects/_(Effect)/Level_2b.xdb#xpointer(/Effect)', '', 'rTown1', x, y, 0, 0);
		sleep(5);
		RazeTown('HordeTown');
		sleep(10);
		StopVisualEffects('rTown1');
		townCount = townCount + 1;
		sleep(1);
		startThread(checkQuest);
	end;
end;

function checkQuestTown2(hero, object)
	sleep(5);
	if IsHeroAlive (hero) == true and hero ~= Undead then
		Trigger(OBJECT_TOUCH_TRIGGER, 'NecroTown', nil);
		if IsHeroInTown(Undead, 'NecroTown') == true then
			Trigger(PLAYER_REMOVE_HERO_TRIGGER, PLAYER_2, nil);
			heroCount = heroCount + 1;
		end;
		local x, y = GetObjectPosition('NecroTown');
		Play2DSound('/Maps/SingleMissions/Aliralys/Siege_WallCrash02sound.xdb#xpointer(/Sound)');
		PlayVisualEffect('/Effects/_(Effect)/Start_dust_S.xdb#xpointer(/Effect)', '','rTown2', x, y, 0, 0);
		PlayVisualEffect('/Effects/_(Effect)/Level_2b.xdb#xpointer(/Effect)', '', 'rTown2', x, y, 0, 0);
		sleep(5);
		RazeTown('NecroTown');
		sleep(10);
		StopVisualEffects('rTown2');
		townCount = townCount + 1;
		sleep(1);
		startThread(checkQuest);
	end;
end;

function checkQuestTown3(hero, object)
	sleep(5);
	if IsHeroAlive (hero) == true and hero ~= Demon then
		Trigger(OBJECT_TOUCH_TRIGGER, 'DemonTown', nil);
		if IsHeroInTown(Demon, 'DemonTown') == true then
			Trigger(PLAYER_REMOVE_HERO_TRIGGER, PLAYER_3, nil);
			heroCount = heroCount + 1;
		end;
		local x, y = GetObjectPosition('DemonTown');
		Play2DSound('/Maps/SingleMissions/Aliralys/Siege_WallCrash02sound.xdb#xpointer(/Sound)');
		PlayVisualEffect('/Effects/_(Effect)/Start_dust_S.xdb#xpointer(/Effect)', '','rTown3', x, y, 0, 0);
		PlayVisualEffect('/Effects/_(Effect)/Level_2b.xdb#xpointer(/Effect)', '', 'rTown3', x, y, 0, 0);
		sleep(5);
		RazeTown('DemonTown');
		sleep(10);
		StopVisualEffects('rTown3');
		townCount = townCount + 1;
		sleep(1);
		startThread(checkQuest);
	end;
end;

function heavenTownDestroyed(hero, object)
	local x, y = GetObjectPosition(object);
	Play2DSound('/Maps/SingleMissions/Aliralys/Siege_WallCrash02sound.xdb#xpointer(/Sound)');
	PlayVisualEffect('/Effects/_(Effect)/Start_dust_S.xdb#xpointer(/Effect)', '','hTown', x, y, 0, 0);
	PlayVisualEffect('/Effects/_(Effect)/Level_2b.xdb#xpointer(/Effect)', '', 'hTown', x, y, 0, 0);
	sleep(5);
	RazeTown(object);
	sleep(10);
	StopVisualEffects('hTown');
end;

function checkMission()
	Trigger(OBJECT_CAPTURE_TRIGGER, 'Htown5', nil);
	sleep(10);
	SetObjectiveState('MainQuest', OBJECTIVE_COMPLETED);
end;

function checkQuestHero()
	heroCount = heroCount + 1;
	sleep(3);
	startThread(checkQuest);
end;

function checkQuest()
	if heroCount == 2 and townCount == 2 then
		SetObjectiveState('SecondQuest', OBJECTIVE_COMPLETED);
	end;
end;

-- контроль исскуственного интеллекта --
function demonRegUnblock()
	if demonIsActive == 0 then
		Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'demonReg1', nil);
		Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'blockUnderground01', nil);
		SetRegionBlocked('demonReg1', nil);
		SetRegionBlocked('humReg5', nil);
		ChangeHeroStat(Demon, 0, 1000*GetDate(ABSOLUTE_DAY));
		ChangeHeroStat(Demon, 1, 2*Diff*GetDate(MONTH));
		ChangeHeroStat(Demon, 2, 1*Diff*GetDate(MONTH));
		ChangeHeroStat(Demon, 3, 2*Diff*GetDate(MONTH));
		ChangeHeroStat(Demon, 4, 2*Diff*GetDate(MONTH));
		
		AddHeroCreatures(Demon, 15, 30*Diff*GetDate(MONTH));
		AddHeroCreatures(Demon, 17, 26*Diff*GetDate(MONTH));
		AddHeroCreatures(Demon, 19, 22*Diff*GetDate(MONTH));
		AddHeroCreatures(Demon, 21, 16*Diff*GetDate(MONTH));
		AddHeroCreatures(Demon, 23, 12*Diff*GetDate(MONTH));
		AddHeroCreatures(Demon, 25, 8*Diff*GetDate(MONTH));
		AddHeroCreatures(Demon, 27, 2*Diff*GetDate(MONTH));
		EnableHeroAI(Demon, true);
		demonIsActive = 1;
	end;
end;

function necroRegUnblock()
	if necroIsActive == 0 then
		Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'necroReg1', nil);
		Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'blockUnderground03', nil);
		SetRegionBlocked('necroReg1', nil);
		ChangeHeroStat(Undead, 0, 1000*GetDate(ABSOLUTE_DAY));
		ChangeHeroStat(Undead, 1, 2*Diff*GetDate(MONTH));
		ChangeHeroStat(Undead, 2, 1*Diff*GetDate(MONTH));
		ChangeHeroStat(Undead, 3, 3*Diff*GetDate(MONTH));
		ChangeHeroStat(Undead, 4, 2*Diff*GetDate(MONTH));
		
		AddHeroCreatures(Undead, 29, 45*Diff*GetDate(MONTH));
		AddHeroCreatures(Undead, 31, 32*Diff*GetDate(MONTH));
		AddHeroCreatures(Undead, 33, 28*Diff*GetDate(MONTH));
		AddHeroCreatures(Undead, 35, 21*Diff*GetDate(MONTH));
		AddHeroCreatures(Undead, 37, 15*Diff*GetDate(MONTH));
		AddHeroCreatures(Undead, 39, 9*Diff*GetDate(MONTH));
		AddHeroCreatures(Undead, 41, 5*Diff*GetDate(MONTH));
		EnableHeroAI(Undead, true);
		necroIsActive = 1;
	end;
end;

function orkRegUnblock()
	if orkIsActive == 0 then
		Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'orkReg1', nil);
		Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'orkReg2', nil);
		Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'blockUnderground02', nil);
		SetRegionBlocked('orkReg1', nil);
		SetRegionBlocked('orkReg2', nil);
		ChangeHeroStat(Ork, 0, 1000*GetDate(ABSOLUTE_DAY));
		ChangeHeroStat(Ork, 1, 3*Diff*GetDate(MONTH));
		ChangeHeroStat(Ork, 2, 1*Diff*GetDate(MONTH));
		ChangeHeroStat(Ork, 3, 1*Diff*GetDate(MONTH));
		ChangeHeroStat(Ork, 4, 2*Diff*GetDate(MONTH));
		
		AddHeroCreatures(Ork, 117, 35*Diff*GetDate(MONTH));
		AddHeroCreatures(Ork, 119, 28*Diff*GetDate(MONTH));
		AddHeroCreatures(Ork, 121, 23*Diff*GetDate(MONTH));
		AddHeroCreatures(Ork, 123, 19*Diff*GetDate(MONTH));
		AddHeroCreatures(Ork, 125, 12*Diff*GetDate(MONTH));
		AddHeroCreatures(Ork, 127, 9*Diff*GetDate(MONTH));
		AddHeroCreatures(Ork, 129, 3*Diff*GetDate(MONTH));
		EnableHeroAI(Ork, true);
		orkIsActive = 1;
	end;
end;

function NewDayAttack()
	if (GetDate(DAY_OF_WEEK) == 1) and (GetDate(WEEK) ~= 1) then
		DeployReserveHero('Brem', 213, 76, 0, 0, 0);
		ChangeHeroStat('Brem', 0, 1000*GetDate(ABSOLUTE_DAY));
		ChangeHeroStat('Brem', 1, 2*GetDate(WEEK));
		ChangeHeroStat('Brem', 2, 1*GetDate(WEEK));
		ChangeHeroStat('Brem', 3, 1*GetDate(WEEK));
		ChangeHeroStat('Brem', 4, 1*GetDate(WEEK));
		AddHeroCreatures('Brem', 106, 35*Diff*GetDate(WEEK));
		AddHeroCreatures('Brem', 107, 24*Diff*GetDate(WEEK));
		AddHeroCreatures('Brem', 108, 15*Diff*GetDate(WEEK));
		AddHeroCreatures('Brem', 109, 11*Diff*GetDate(WEEK));
		AddHeroCreatures('Brem', 110, 8*Diff*GetDate(WEEK));
		AddHeroCreatures('Brem', 111, 3*Diff*GetDate(WEEK));
		AddHeroCreatures('Brem', 112, 1*Diff*GetDate(WEEK));
		SetAIHeroAttractor(aTown, 'Brem', 2);
	end;
end;

function humRegUnblock1()
	Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'humReg1', nil);
	SetRegionBlocked('humReg1', nil);
	if attack == 0 then
		Trigger(NEW_DAY_TRIGGER, 'NewDayAttack');
		attack = 1;
	end;
	if aPlayer ~= 3 then
		demonRegUnblock();
	end;
end;

function humRegUnblock2()
	Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'humReg2', nil);
	SetRegionBlocked('humReg2', nil);
	if attack == 0 then
		Trigger(NEW_DAY_TRIGGER, 'NewDayAttack');
		attack = 1;
	end;
	if aPlayer ~= 1 then
		orkRegUnblock();
	end;
end;

function humRegUnblock3()
	Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'humReg3', nil);
	SetRegionBlocked('humReg3', nil);
	if attack == 0 then
		Trigger(NEW_DAY_TRIGGER, 'NewDayAttack');
		attack = 1;
	end;
	if aPlayer ~= 2 then
		necroRegUnblock();
	end;
end;

function humRegUnblock4()
	Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'humReg4', nil);
	SetRegionBlocked('humReg4', nil);
	EnableHeroAI('Maeve', true);
	ChangeHeroStat('Maeve', 0, 3000*GetDate(ABSOLUTE_DAY));
	
	AddHeroCreatures('Maeve', 106, 20*Diff*GetDate(MONTH));
	AddHeroCreatures('Maeve', 107, 12*Diff*GetDate(MONTH));
	AddHeroCreatures('Maeve', 108, 9*Diff*GetDate(MONTH));
	AddHeroCreatures('Maeve', 109, 5*Diff*GetDate(MONTH));
	AddHeroCreatures('Maeve', 110, 3*Diff*GetDate(MONTH));
	
	if attack == 0 then
		Trigger(NEW_DAY_TRIGGER, 'NewDayAttack');
		attack = 1;
	end;
end;

function checkOwner1(oldOwner, newOwner, heroname, object)
	if newOwner == PLAYER_5 then
		AddObjectCreatures(object, 1, (20+random(5))*Diff);
		AddObjectCreatures(object, 3, (10+random(3))*Diff);
		SetAIPlayerAttractor(object, PLAYER_5, 0);
	else
		SetAIPlayerAttractor(object, PLAYER_5, 2);
	end;
end;

function checkOwner2(oldOwner, newOwner, heroname, object)
	if newOwner == PLAYER_5 then
		AddObjectCreatures(object, 9, (5+random(4))*Diff);
		AddObjectCreatures(object, 11, (2+random(2))*Diff);
		SetAIPlayerAttractor(object, PLAYER_5, 0);
	else
		SetAIPlayerAttractor(object, PLAYER_5, 2);
	end;
end;

function checkOwner3(oldOwner, newOwner, heroname, object)
	if newOwner == PLAYER_5 then
		AddObjectCreatures(object, 5, (26+random(3))*Diff);
		AddObjectCreatures(object, 7, (12+random(3))*Diff);
		SetAIPlayerAttractor(object, PLAYER_5, 0);
	else
		SetAIPlayerAttractor(object, PLAYER_5, 2);
	end;
end;

function MoveHeroAndReachPoint(hero, x, y, floor)
	ChangeHeroStat(hero, 7, 3500);
	sleep(1);
	MoveHeroRealTime(hero, x, y, 1);
end

-- подземелье
function undergroundEventStart()
	Trigger(REGION_ENTER_AND_STOP_TRIGGER, 'unReg01', nil);
	BlockGame();
	SetObjectPosition('Ottar', 126, 70, 1);
	sleep(5);
	UnblockGame();
	MoveHeroAndReachPoint('Ottar', 117, 60, 1);
	sleep(30);
	MessageBox(GetMapDataPath()..'undergroundEvent01.txt');
	SetObjectiveState('UnderGroundQuest', OBJECTIVE_ACTIVE);
	sleep(1);
	BlockGame();
	MoveHeroAndReachPoint('Ottar', 141, 42, 1);
	sleep(35);
	SetObjectPosition('Ottar', 202, 189, 1);
	SetObjectRotation('Ottar', 0);
	sleep(1);
	UnblockGame();
end;

function undergroundEventEnd()
	if (HasArtefact(aHero, 48) == true and HasArtefact(aHero, 49) == true) and (HasArtefact(aHero, 50) == true and HasArtefact(aHero, 51) == true) then
		Trigger(REGION_ENTER_AND_STOP_TRIGGER, 'unReg02', nil);
		BlockGame();
		RemoveObjectCreatures('UnGarrison', 104, 52*Diff, 1);
		RemoveObjectCreatures('UnGarrison', 104, 52*Diff, 2);
		RemoveObjectCreatures('UnGarrison', 104, 52*Diff, 3);
		MoveHeroAndReachPoint('Ottar', 202, 199, 1);
		sleep(40);
		SetObjectPosition('Ottar', 115, 100, 1);
		SetObjectEnabled('Ottar', nil);
		SetObjectiveState('UnderGroundQuest', OBJECTIVE_COMPLETED);
		sleep(5);
		RemoveArtefact(aHero, 48);
		RemoveArtefact(aHero, 49);
		RemoveArtefact(aHero, 50);
		RemoveArtefact(aHero, 51);
		if Diff == 4 then
			GiveArtefact(aHero, 88);
		end;
		UnblockGame();
		MessageBox(GetMapDataPath()..'undergroundEvent02.txt');
	else
		MessageBox(GetMapDataPath()..'undergroundEvent03.txt');
		BlockGame();
		EnableHeroAI(aHero, nil);
		MoveHeroAndReachPoint(aHero, 202, 180, 1);
		sleep(25);
		EnableHeroAI(aHero, true);
		UnblockGame();
	end;
end;

function progressCheck()
	progress = progress + 1;
	if GetObjectiveState('UnderGroundQuest') == OBJECTIVE_ACTIVE then
		SetObjectiveProgress('UnderGroundQuest', progress, PLAYER_1);
		if progress == 4 then
			MessageBox(GetMapDataPath()..'undergroundEvent04.txt');
			sleep(3);
			CreateMonster('mon1', 41, (11+random(2))*Diff, 183, 163, 1, 1, 1, 4.71, 0);
			CreateMonster('mon2', 55, (9+random(2))*Diff, 199, 164, 1, 1, 1, 5.5, 0);
			CreateMonster('mon3', 83, (8+random(2))*Diff, 201, 169, 1, 1, 1, 5.5, 0);
			CreateMonster('mon4', 127, (16+random(5))*Diff, 202, 177, 1, 1, 1, 0, 0);
		end;
	end;
end;

-- триггеры
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'humReg1', 'humRegUnblock1');
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'humReg2', 'humRegUnblock2');
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'humReg3', 'humRegUnblock3');
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'humReg4', 'humRegUnblock4');

Trigger(REGION_ENTER_AND_STOP_TRIGGER, 'unReg01', 'undergroundEventStart');
Trigger(REGION_ENTER_AND_STOP_TRIGGER, 'unReg02', 'undergroundEventEnd');

Trigger(OBJECT_CAPTURE_TRIGGER, 'Htown1', 'heavenTownDestroyed');
Trigger(OBJECT_CAPTURE_TRIGGER, 'Htown2', 'heavenTownDestroyed');
Trigger(OBJECT_CAPTURE_TRIGGER, 'Htown3', 'heavenTownDestroyed');
Trigger(OBJECT_CAPTURE_TRIGGER, 'Htown4', 'heavenTownDestroyed');
Trigger(OBJECT_CAPTURE_TRIGGER, 'Htown5', 'checkMission');

Trigger(OBJECT_TOUCH_TRIGGER, 'item1', 'progressCheck');
Trigger(OBJECT_TOUCH_TRIGGER, 'item2', 'progressCheck');
Trigger(OBJECT_TOUCH_TRIGGER, 'item3', 'progressCheck');
Trigger(OBJECT_TOUCH_TRIGGER, 'item4', 'progressCheck');
-- потоки
startThread(difficulty);
--
print('Map version 1.0 by KioM');