-- переменные --
Diff = 0;
-- difficultyHard = 1; -- для хард-режима на уровне герой, реализуется позднее...
player = 1;
killerCount = 0;
dwellingsCount = 0;

Gilbert = 'Sarge';
Arix = 'Nur';

-- режим сложности --
function difficulty()
	if GetDifficulty() == DIFFICULTY_EASY then
		Diff = 1;
	elseif GetDifficulty() == DIFFICULTY_NORMAL then
		Diff = 2;
	elseif GetDifficulty() == DIFFICULTY_HARD then
		Diff = 3;
		difficultyHard = 2;
	elseif GetDifficulty() == DIFFICULTY_HEROIC then
		Diff = 4;
		difficultyHard = 3;
	end;
	initMapSettings();
end;

-- загрузка дополнительных свойств --
function initMapSettings()
	SetPlayerStartResources(player, 0, 0, 0, 0, 0, 0, 5000-Diff*1000);
	AddHeroCreatures(Gilbert, 1, 150-Diff*30);
	AddHeroCreatures(Gilbert, 11, 10-Diff*2);
	
	EnableHeroAI(Arix, nil);
	EnableHeroAI('Razzak', nil);
	SetObjectEnabled('Razzak', nil);
	
	SetObjectEnabled('PeasantQ01', nil);
	SetObjectEnabled('TombWarrior', nil);
	SetObjectEnabled('ArcherPost', nil);
	SetObjectEnabled('Barracks', nil);
	SetObjectEnabled('PriestPost', nil);
	SetObjectEnabled('CavalierPost', nil);
	SetObjectEnabled('AngelPost', nil);
	SetObjectEnabled('SeerHut', nil);
	
	SetObjectEnabled('PyramidHouse', nil);
	SetObjectEnabled('PyramidTarget', nil);
	
	SetTownBuildingLimitLevel('HeavenTown', 7, 0);
	SetTownBuildingLimitLevel('HeavenTown', 8, 0);
	SetTownBuildingLimitLevel('HeavenTown', 9, 0);
	SetTownBuildingLimitLevel('HeavenTown', 11, 0);
	SetTownBuildingLimitLevel('HeavenTown', 12, 0);
	SetTownBuildingLimitLevel('HeavenTown', 13, 0);
	
	AddObjectCreatures('AcademyTown', 159, 150*Diff, 0);
	AddObjectCreatures('AcademyTown', 160, 105*Diff, 1);
	AddObjectCreatures('AcademyTown', 161, 76*Diff, 2);
	AddObjectCreatures('AcademyTown', 162, 52*Diff, 3);
	AddObjectCreatures('AcademyTown', 163, 37*Diff, 4);
	AddObjectCreatures('AcademyTown', 164, 18*Diff, 5);
	AddObjectCreatures('AcademyTown', 165, 6*Diff, 6);
	ChangeHeroStat(Arix, 0, 1120000+Diff*1000000);
	ChangeHeroStat(Arix, 1, 17+Diff*4);
	ChangeHeroStat(Arix, 2, 15+Diff*4);
	ChangeHeroStat(Arix, 3, 12+Diff*4);
	ChangeHeroStat(Arix, 4, 9+Diff*3);
end;

-- дополнительные функции --
function setUnblockedDwelling(buildingId)
	if buildingId == 1 then
		MessageBox(GetMapDataPath()..'unblockedDwelling.txt');
		SetTownBuildingLimitLevel('HeavenTown', 8, 2);
	elseif buildingId == 2 then
		MessageBox(GetMapDataPath()..'unblockedDwelling.txt');
		SetTownBuildingLimitLevel('HeavenTown', 9, 2);
	elseif buildingId == 3 then
		MessageBox(GetMapDataPath()..'unblockedDwelling.txt');
		SetTownBuildingLimitLevel('HeavenTown', 11, 2);
	elseif buildingId == 4 then
		MessageBox(GetMapDataPath()..'unblockedDwelling.txt');
		SetTownBuildingLimitLevel('HeavenTown', 12, 2);
	elseif buildingId == 5 then
		MessageBox(GetMapDataPath()..'unblockedDwelling.txt');
		SetTownBuildingLimitLevel('HeavenTown', 13, 2);
	end;
	dwellingsCount = dwellingsCount+1;
	if dwellingsCount == 5 then
		SetObjectiveState('SecondQuest03', OBJECTIVE_COMPLETED);
        SetPlayerResource(1, GOLD, GetPlayerResource(1, GOLD)+125000-Diff*25000, Gilbert);
	end;
end;

function setKillerInfo()
	killerCount = killerCount+1;
	if killerCount == 1 then
		MessageBox(GetMapDataPath()..'secondQuests/second02_count6.txt');
	elseif killerCount == 2 then
		MessageBox(GetMapDataPath()..'secondQuests/second02_count5.txt');
	elseif killerCount == 3 then
		MessageBox(GetMapDataPath()..'secondQuests/second02_count4.txt');
	elseif killerCount == 4 then
		MessageBox(GetMapDataPath()..'secondQuests/second02_count3.txt');
	elseif killerCount == 5 then
		MessageBox(GetMapDataPath()..'secondQuests/second02_count2.txt');
	elseif killerCount == 6 then
		MessageBox(GetMapDataPath()..'secondQuests/second02_count1.txt');
	elseif killerCount == 7 then
		MessageBox(GetMapDataPath()..'secondQuests/second02_end.txt');
	end;
end;

function SetKillHeroArmy()
    monster1 = GetObjectCreatures('AcademyTown', 159);
    monster2 = GetObjectCreatures('AcademyTown', 160);
    monster3 = GetObjectCreatures('AcademyTown', 161);
    monster4 = GetObjectCreatures('AcademyTown', 162);
    monster5 = GetObjectCreatures('AcademyTown', 163);
    monster6 = GetObjectCreatures('AcademyTown', 164);
    monster7 = GetObjectCreatures('AcademyTown', 165);
    RemoveObjectCreatures('AcademyTown', 159, monster1/2);
    RemoveObjectCreatures('AcademyTown', 160, monster2/2);
    RemoveObjectCreatures('AcademyTown', 161, monster3/2);
    RemoveObjectCreatures('AcademyTown', 162, monster4/2);
    RemoveObjectCreatures('AcademyTown', 163, monster5/2);
    RemoveObjectCreatures('AcademyTown', 164, monster6/2);
    RemoveObjectCreatures('AcademyTown', 165, monster7/2);
end;

function NewDayEvent()
	if (GetDate(MONTH) == 3) and (GetDate(WEEK) == 1) then
		AddObjectCreatures('AcademyTown', 159, 26*Diff, 0);
		AddObjectCreatures('AcademyTown', 160, 18*Diff, 1);
		AddObjectCreatures('AcademyTown', 161, 14*Diff, 2);
		AddObjectCreatures('AcademyTown', 162, 10*Diff, 3);
		AddObjectCreatures('AcademyTown', 163, 7*Diff, 4);
		AddObjectCreatures('AcademyTown', 164, 2*Diff, 5);
		AddObjectCreatures('AcademyTown', 165, 1*Diff, 6);
	end;
	if (GetDate(MONTH) == 5) and (GetDate(WEEK) == 1) then
		AddObjectCreatures('AcademyTown', 159, 26*Diff, 0);
		AddObjectCreatures('AcademyTown', 160, 12*Diff, 1);
		AddObjectCreatures('AcademyTown', 161, 14*Diff, 2);
		AddObjectCreatures('AcademyTown', 162, 10*Diff, 3);
		AddObjectCreatures('AcademyTown', 163, 7*Diff, 4);
		AddObjectCreatures('AcademyTown', 164, 2*Diff, 5);
		AddObjectCreatures('AcademyTown', 165, 1*Diff, 6);
	end;
	if (GetDate(MONTH) == 7) and (GetDate(WEEK) == 1) then
		AddObjectCreatures('AcademyTown', 159, 26*Diff, 0);
		AddObjectCreatures('AcademyTown', 160, 12*Diff, 1);
		AddObjectCreatures('AcademyTown', 161, 14*Diff, 2);
		AddObjectCreatures('AcademyTown', 162, 10*Diff, 3);
		AddObjectCreatures('AcademyTown', 163, 7*Diff, 4);
		AddObjectCreatures('AcademyTown', 164, 2*Diff, 5);
		AddObjectCreatures('AcademyTown', 165, 1*Diff, 6);
	end;
	if (GetDate(MONTH) == 10) and (GetDate(WEEK) == 1) then
		AddObjectCreatures('AcademyTown', 159, 26*Diff, 0);
		AddObjectCreatures('AcademyTown', 160, 12*Diff, 1);
		AddObjectCreatures('AcademyTown', 161, 14*Diff, 2);
		AddObjectCreatures('AcademyTown', 162, 10*Diff, 3);
		AddObjectCreatures('AcademyTown', 163, 7*Diff, 4);
		AddObjectCreatures('AcademyTown', 164, 2*Diff, 5);
		AddObjectCreatures('AcademyTown', 165, 1*Diff, 6);
	end;
	if (GetDate(MONTH) == 12) and (GetDate(WEEK) == 1) then
		Trigger(NEW_DAY_TRIGGER, nil);
		Loose();
	end;
end;

-- заставы --
function postBegin01(hero, obj)
	if hero == Gilbert then
		QuestionBoxForPlayers(GetPlayerFilter(player), GetMapDataPath()..'dwellings/questions/archerPostQuestion.txt', 'postYes01');
	end;
end;

function postYes01()
	StartCombat(Gilbert, nil, 7, 3, 4*Diff, 3, 5*Diff, 3, 6*Diff, 3, 5*Diff, 3, 6*Diff, 3, 3*Diff, 3, 4*Diff, nil);
	sleep(5);
	if IsHeroAlive(Gilbert) == true then
		Trigger(OBJECT_TOUCH_TRIGGER, 'ArcherPost', nil);
		SetObjectEnabled('ArcherPost', true);
		SetObjectOwner('ArcherPost', player);
		setUnblockedDwelling(1);
	end;
end;

function postBegin02(hero, obj)
	if hero == Gilbert then
		QuestionBoxForPlayers(GetPlayerFilter(player), GetMapDataPath()..'dwellings/questions/barracksQuestion.txt', 'postYes02');
	end;
end;

function postYes02()
	StartCombat(Gilbert, nil, 6, 5, 4*Diff, 5, 5*Diff, 5, 6*Diff, 5, 5*Diff, 5, 4*Diff, 5, 6*Diff, nil);
	sleep(5);
	if IsHeroAlive(Gilbert) == true then
		Trigger(OBJECT_TOUCH_TRIGGER, 'Barracks', nil);
		SetObjectEnabled('Barracks', true);
		SetObjectOwner('Barracks', player);
		setUnblockedDwelling(2);
	end;
end;

function postBegin03(hero, obj)
	if hero == Gilbert then
		QuestionBoxForPlayers(GetPlayerFilter(player), GetMapDataPath()..'dwellings/questions/priestPostQuestion.txt', 'postYes03');
	end;
end;

function postYes03()
	StartCombat(Gilbert, nil, 5, 9, 5*Diff, 9, 6*Diff, 9, 7*Diff, 9, 6*Diff, 9, 5*Diff, nil);
	sleep(5);
	if IsHeroAlive(Gilbert) == true then
		Trigger(OBJECT_TOUCH_TRIGGER, 'PriestPost', nil);
		SetObjectEnabled('PriestPost', true);
		SetObjectOwner('PriestPost', player);
		setUnblockedDwelling(3);
	end;
end;

function postBegin04(hero, obj)
	if hero == Gilbert then
		QuestionBoxForPlayers(GetPlayerFilter(player), GetMapDataPath()..'dwellings/questions/cavalierPostQuestion.txt', 'postYes04');
	end;
end;

function postYes04()
	StartCombat(Gilbert, nil, 4, 11, 3*Diff, 11, 5*Diff, 11, 4*Diff, 11, 3*Diff, nil);
	sleep(5);
	if IsHeroAlive(Gilbert) == true then
		Trigger(OBJECT_TOUCH_TRIGGER, 'CavalierPost', nil);
		SetObjectEnabled('CavalierPost', true);
		SetObjectOwner('CavalierPost', player);
		setUnblockedDwelling(4);
	end;
end;

function postBegin05(hero, obj)
	if hero == Gilbert then
		QuestionBoxForPlayers(GetPlayerFilter(player), GetMapDataPath()..'dwellings/questions/angelPostQuestion.txt', 'postYes05');
	end;
end;

function postYes05()
	StartCombat(Gilbert, nil, 3, 13, 2*Diff, 13, 4*Diff, 13, 3*Diff, nil);
	sleep(5);
	if IsHeroAlive(Gilbert) == true then
		Trigger(OBJECT_TOUCH_TRIGGER, 'AngelPost', nil);
		SetObjectEnabled('AngelPost', true);
		SetObjectOwner('AngelPost', player);
		setUnblockedDwelling(5);
	end;
end;

-- прочее --
function unblockedPeasantPost()
	Trigger(OBJECT_CAPTURE_TRIGGER, 'PeasantPost', nil);
	MessageBox(GetMapDataPath()..'unblockedDwelling.txt');
	SetTownBuildingLimitLevel('HeavenTown', 7, 2);
end;

function dwellingBlockedInfo()
	Trigger(OBJECT_TOUCH_TRIGGER, 'HeavenTown', nil);
	sleep(5);
	MessageBox(GetMapDataPath()..'blockedDwelling.txt');
	sleep(5);
	SetObjectiveState('SecondQuest03', OBJECTIVE_ACTIVE);
end;

function searchArtefact(hero, object)
	local rnd = random(3);
	if hero == Gilbert then
		Trigger(OBJECT_TOUCH_TRIGGER, 'TombWarrior', nil);
		if rnd == 0 then
			GiveHeroBattleBonus(hero, HERO_BATTLE_BONUS_MORALE, -3);
		elseif rnd == 1 then
			GiveHeroBattleBonus(hero, HERO_BATTLE_BONUS_MORALE, -3);
			GiveHeroBattleBonus(hero, HERO_BATTLE_BONUS_LUCK, -3);
		elseif rnd == 2 then
			GiveArtefact(hero, 1);
			GiveHeroBattleBonus(hero, HERO_BATTLE_BONUS_MORALE, -4);
		end;
		MarkObjectAsVisited('TombWarrior', hero);
	end;
end;

function pyramidQuestion()
	QuestionBoxForPlayers(GetPlayerFilter(player), GetMapDataPath()..'pyramid_enterQuestion.txt', 'pyramidYes');
end;

function pyramidYes()
	StartCombat(Gilbert, nil, 2, 115, 16*Diff, 115, 15*Diff, '/Maps/campaign/GilbertPart3/PyramidGuardScript.xdb#xpointer(/Script)', nil, '/Maps/newArenas/Scenes/CombatArenas/PyramidArena.xdb#xpointer(/AdventureFlybyScene)');
	sleep(5);
	if IsHeroAlive(Gilbert) == true then
		Trigger(OBJECT_TOUCH_TRIGGER, 'PyramidTarget', nil);
		SetHeroCombatScript(Gilbert, '/Maps/campaign/GilbertPart3/MummyCombatScript.xdb#xpointer(/Script)');
		ShowFlyingSign(GetMapDataPath()..'pyramid_fly.txt', Gilbert, player, 4);
		MarkObjectAsVisited('PyramidTarget', Gilbert);
		SetObjectOwner('PyramidHouse', player);
	end;
end;

function setAppInSands(hero)
	local rnd = random(4);
	if hero == Gilbert then
		if rnd == 0 then
			ShowFlyingSign(GetMapDataPath()..'quickSand_fly.txt', hero, player, 4);
			sleep(10);
			GiveHeroBattleBonus(hero, HERO_BATTLE_BONUS_MORALE, -5);
		elseif rnd == 1 then
			ShowFlyingSign(GetMapDataPath()..'quickSand_fly.txt', hero, player, 4);
			sleep(10);
			GiveHeroBattleBonus(hero, HERO_BATTLE_BONUS_LUCK, -5);
		elseif rnd == 2 then
			ShowFlyingSign(GetMapDataPath()..'quickSand_fly.txt', hero, player, 4);
			sleep(10);
			GiveHeroBattleBonus(hero, HERO_BATTLE_BONUS_INITIATIVE, -3);
		elseif rnd == 3 then
			ShowFlyingSign(GetMapDataPath()..'quickSand_fly.txt', hero, player, 4);
			sleep(10);
			movePoints = GetHeroStat(hero, 7);
			ChangeHeroStat(hero, 7, -movePoints);
		end;
	end;
end;

-- задания --
function showMessageQuest01()
	Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'rSecondQ1', nil);
	if IsObjectExists('PeasantQ01') == true then
		ShowFlyingSign(GetMapDataPath()..'secondQuests/second01_fly.txt', 'PeasantQ01', player, 3.5);
	end;
end;

function secondQuest01Prepare()
	if GetObjectiveState('SecondQuest01') == OBJECTIVE_UNKNOWN then
		QuestionBoxForPlayers(GetPlayerFilter(player), GetMapDataPath()..'secondQuests/second01_question.txt', 'secondQuest01Yes', 'secondQuest01No');
	end;
	if GetObjectiveState('SecondQuest01') == OBJECTIVE_ACTIVE and HasArtefact(Gilbert, 28) then
		Trigger(OBJECT_TOUCH_TRIGGER, 'PeasantQ01', nil);
		RemoveArtefact(Gilbert, 28); -- должен быть 29, разрабы как всегда отличились ;)
		RemoveObject('PeasantQ01');
		SetObjectiveState('SecondQuest01', OBJECTIVE_COMPLETED);
        SetPlayerResource(1, GOLD, GetPlayerResource(1, GOLD)+25000-Diff*5000, Gilbert);
	end;
end;

function secondQuest01Yes()
	SetObjectiveState('SecondQuest01', OBJECTIVE_ACTIVE);
	BlockGame();
	OpenCircleFog(109, 6, 0, 7, player);
	MoveCamera(109, 6, 0, 30, 1.2, 0, 1, 1, 0);
	sleep(25);
	local x, y = GetObjectPosition(Gilbert);
	MoveCamera(x, y, 0, 30, 1.2, 0, 1, 1, 1);
	UnblockGame();
end;

function secondQuest01No()
	SetObjectEnabled('PeasantQ01', true);
	MakeHeroInteractWithObject(Gilbert, 'PeasantQ01');
end;

function secondQuest01Item()
	if GetObjectiveState('SecondQuest01') == OBJECTIVE_ACTIVE then
		sleep(7);
		ShowFlyingSign(GetMapDataPath()..'secondQuests/second01_flyEnd.txt', Gilbert, player, 4);
	end;
end;

function secondQuest02Prepare()
	if GetObjectiveState('SecondQuest02') == OBJECTIVE_UNKNOWN then
		SetObjectiveState('SecondQuest02', OBJECTIVE_ACTIVE);
	elseif GetObjectiveState('SecondQuest02') == OBJECTIVE_ACTIVE and killerCount ~= 7 then
		MessageBox(GetMapDataPath()..'secondQuests/second02_notEnd.txt');
	elseif GetObjectiveState('SecondQuest02') == OBJECTIVE_ACTIVE and killerCount == 7 then
		Trigger(OBJECT_TOUCH_TRIGGER, 'SeerHut', nil);
		SetObjectiveState('SecondQuest02', OBJECTIVE_COMPLETED);
		GiveArtefact(Gilbert, 57);
        SetKillHeroArmy();
	end;
end;

function checkQuest02(hero, obj)
	sleep(5);
	if IsHeroAlive(hero) == true then
		setKillerInfo();
	end;
end;

function showMessageQuestMain()
	Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'rMainInfo', nil);
	ShowFlyingSign(GetMapDataPath()..'secondQuests/main_fly.txt', Gilbert, player, 3);
	sleep(5);
	SetObjectiveState('MainQuest', OBJECTIVE_ACTIVE);
end;

function questMainComplete(oldOwner, newOwner)
	if newOwner == PLAYER_1 then
		SetObjectiveState('MainQuest', OBJECTIVE_COMPLETED);
	end;
end;

-- триггеры -- rQuickSand0
Trigger(NEW_DAY_TRIGGER, 'NewDayEvent');
Trigger(OBJECT_TOUCH_TRIGGER, 'PeasantQ01', 'secondQuest01Prepare');
Trigger(OBJECT_TOUCH_TRIGGER, 'GoldItem', 'secondQuest01Item');
Trigger(OBJECT_TOUCH_TRIGGER, 'HeavenTown', 'dwellingBlockedInfo');
Trigger(OBJECT_TOUCH_TRIGGER, 'TombWarrior', 'searchArtefact');
Trigger(OBJECT_TOUCH_TRIGGER, 'ArcherPost', 'postBegin01');
Trigger(OBJECT_TOUCH_TRIGGER, 'Barracks', 'postBegin02');
Trigger(OBJECT_TOUCH_TRIGGER, 'PriestPost', 'postBegin03');
Trigger(OBJECT_TOUCH_TRIGGER, 'CavalierPost', 'postBegin04');
Trigger(OBJECT_TOUCH_TRIGGER, 'AngelPost', 'postBegin05');
Trigger(OBJECT_TOUCH_TRIGGER, 'SeerHut', 'secondQuest02Prepare');
Trigger(OBJECT_TOUCH_TRIGGER, 'monster01', 'checkQuest02');
Trigger(OBJECT_TOUCH_TRIGGER, 'monster02', 'checkQuest02');
Trigger(OBJECT_TOUCH_TRIGGER, 'monster03', 'checkQuest02');
Trigger(OBJECT_TOUCH_TRIGGER, 'monster04', 'checkQuest02');
Trigger(OBJECT_TOUCH_TRIGGER, 'monster05', 'checkQuest02');
Trigger(OBJECT_TOUCH_TRIGGER, 'monster06', 'checkQuest02');
Trigger(OBJECT_TOUCH_TRIGGER, 'monster07', 'checkQuest02');
Trigger(OBJECT_TOUCH_TRIGGER, 'PyramidTarget', 'pyramidQuestion');
Trigger(OBJECT_CAPTURE_TRIGGER, 'PeasantPost', 'unblockedPeasantPost');
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'rSecondQ1', 'showMessageQuest01');
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'rMainInfo', 'showMessageQuestMain');
Trigger(REGION_ENTER_AND_STOP_TRIGGER, 'rQuickSand01', 'setAppInSands');
Trigger(REGION_ENTER_AND_STOP_TRIGGER, 'rQuickSand02', 'setAppInSands');
Trigger(REGION_ENTER_AND_STOP_TRIGGER, 'rQuickSand03', 'setAppInSands');
Trigger(REGION_ENTER_AND_STOP_TRIGGER, 'rQuickSand04', 'setAppInSands');
Trigger(REGION_ENTER_AND_STOP_TRIGGER, 'rQuickSand05', 'setAppInSands');
Trigger(REGION_ENTER_AND_STOP_TRIGGER, 'rQuickSand06', 'setAppInSands');
Trigger(REGION_ENTER_AND_STOP_TRIGGER, 'rQuickSand07', 'setAppInSands');
Trigger(REGION_ENTER_AND_STOP_TRIGGER, 'rQuickSand08', 'setAppInSands');
Trigger(REGION_ENTER_AND_STOP_TRIGGER, 'rQuickSand09', 'setAppInSands');
Trigger(REGION_ENTER_AND_STOP_TRIGGER, 'rQuickSand10', 'setAppInSands');
Trigger(OBJECT_CAPTURE_TRIGGER, 'AcademyTown', 'questMainComplete');
-- потоки --
startThread(difficulty);
--
print('loaded all script functions');
print('custom campaign: colonization, part 3 /map version v.1.4 by KioM/');