-- переменные --
Diff = 0;
Gilbert = 'Sarge';
Gorhen = 'Muscip';
player = 1;
necro = 3;
timeOfWar = 0;
weekCount = 0;
lastTime = 0;

SetHeroesExpCoef(0.8);

-- объекты на карте --
SetObjectEnabled('bCamp', nil);
SetObjectEnabled('ArcherJoin', nil);
SetObjectEnabled('PeasantJoin', nil);
SetObjectEnabled('GriffinJoin', nil);
SetObjectEnabled('ConsJoin', nil);
SetObjectEnabled('Barracks', nil);
SetObjectEnabled('ATower', nil);
SetObjectEnabled('Military', nil);

-- кампания --
SetGameVar('gilbert_spell', 0);
SetGameVar('gilbert_warmashine01', 0);
SetGameVar('gilbert_warmashine02', 0);

-- режим сложности --
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()
	-- опции игрока
	SetWarfogBehaviour(1, 0);
	SetPlayerStartResources(player, 0, 0, 0, 0, 0, 0, 25000-Diff*5000);
	AddHeroCreatures(Gilbert, 11,7-Diff);
	AddHeroCreatures(Gilbert, 1,50-Diff*10);

	-- армия нежити в городе
	AddObjectCreatures('MainTown', 30, 19*Diff);
	AddObjectCreatures('MainTown', 32, 11*Diff);
	AddObjectCreatures('MainTown', 34, 6*Diff);

	-- мусор...
	count = GetObjectCreatures('MainTown', 3);
	if count > 0 then -- check error
		RemoveObjectCreatures('MainTown', 3, count);
	end;

	-- блокировка Горхена
	EnableHeroAI(Gorhen, nil);
	EnableHeroAI('Orrin', nil);
end;

--
function TimedEvents()
	-- первое задание
	if weekCount ~= 0 then
		if (GetDate(ABSOLUTE_DAY) == weekCount+29) and (GetObjectiveState('CaptureTownQuest') == OBJECTIVE_ACTIVE) then
			AddObjectCreatures('MainTown', 30, 25*Diff);
			AddObjectCreatures('MainTown', 32, 16*Diff);
			AddObjectCreatures('MainTown', 34, 9*Diff);
			MessageBox(GetMapDataPath()..'TownRein.txt');
		end;
		if (GetDate(ABSOLUTE_DAY) == weekCount+70) and (GetObjectiveState('CaptureTownQuest') == OBJECTIVE_ACTIVE) then
			Loose();
		end;
	end;
	-- второе задание
	if timeOfWar ~= 0 then
		if (GetDate(DAY) == timeOfWar+7) then
			MessageBox(GetMapDataPath().."Quest02_rum.txt");
		end;
		if (GetDate(DAY) == timeOfWar+11) then
			MessageBox(GetMapDataPath().."Quest02_rum2.txt");
		end;
		if (GetDate(DAY) == timeOfWar+14) then
			MessageBox(GetMapDataPath().."Quest02_rum3.txt");
			sleep(5);
			BlockGame();
			SetObjectPosition(Gorhen, 90, 131, 0);
			heroCavalier = GetHeroCreatures(Gilbert, 11);
			AddHeroCreatures(Gorhen, 30, 44*Diff);
			AddHeroCreatures(Gorhen, 32, 23*Diff);
			AddHeroCreatures(Gorhen, 34, 7*Diff);
			if heroCavalier > 1 then
				AddHeroCreatures(Gorhen, 40, heroCavalier);
			end;
			MoveCamera(90, 131, 0, 50, 2, 0, 1, 1);
			OpenCircleFog(90, 131, 0, 10, PLAYER_1);
			sleep(20);
			UnblockGame();
			MakeHeroInteractWithObject(Gorhen, 'MainTown');
		end;
	end;
	-- последнее задание
	if lastTime ~= 0 then
		lastTime = lastTime+1;
	end;
end;

-- указатель --
function OpenFogOfTown()
	BlockGame();
	OpenCircleFog(69, 85, 0, 10, PLAYER_1);
	MoveCamera(69, 85, 0, 50, 2, 0, 1, 1, 0);
	Trigger(OBJECT_TOUCH_TRIGGER, 'Signpost01', nil);
	sleep(20);
	local x, y = GetObjectPosition(Gilbert);
	MoveCamera(x, y, 0, 50, 2, 0, 1, 1, 1);
	UnblockGame();
end;

-- лагерь --
function CampBattle()
	MessageBox(GetMapDataPath().."Camp_rum.txt");
	local rnd = random(2);
	if rnd == 0 then
		StartCombat(Gilbert, nil, 4, 46, 3*Diff, 46, 6*Diff, 46, 8*Diff, 46, 5*Diff, nil);
	elseif rnd == 1 then
		StartCombat(Gilbert, nil, 4, 138, 4*Diff, 138, 6*Diff, 138, 7*Diff, 138, 5*Diff, nil);
	end;
	sleep(5);
	if IsHeroAlive(Gilbert) == true then
		Trigger(OBJECT_TOUCH_TRIGGER, 'bCamp', nil);
		SetObjectEnabled('bCamp', true);
	end;
end;

-- существа --
function JoinCreatures(creatures)
	step = creatures;
	local rnd = random(3);
	if step == 1 then
		if rnd == 2 then
			MessageBox(GetMapDataPath()..'Archer_rum.txt');
			AddHeroCreatures(Gilbert, 3, 46-Diff*10);
		end;
	elseif step == 2 then
		if rnd == 1 then
			MessageBox(GetMapDataPath()..'Peasant_rum.txt');
			AddHeroCreatures(Gilbert, 1, 85-Diff*20);
		end;
	elseif step == 3 then
		if rnd == 2 then
			MessageBox(GetMapDataPath()..'Griffin_rum.txt');
			AddHeroCreatures(Gilbert, 7, 30-Diff*6);
		end;
	elseif step == 4 then
		if rnd == 1 then
			MessageBox(GetMapDataPath()..'Cons_rum.txt');
			AddHeroCreatures(Gilbert, 2, 72-Diff*15);
		end;
	end;
end;

function ArcherBattle()
	local rnd = random(2);
	if rnd == 0 then
		StartCombat(Gilbert, nil, 3, 3, 11*Diff, 4, 8*Diff, 3, 12*Diff, nil);
	elseif rnd == 1 then
		StartCombat(Gilbert, nil, 3, 3, 12*Diff, 107, 8*Diff, 3, 11*Diff, nil);
	end;
	sleep(5);
	if IsHeroAlive(Gilbert) == true then
		Trigger(OBJECT_TOUCH_TRIGGER, 'ArcherJoin', nil);
		JoinCreatures(1);
		RemoveObject('ArcherJoin');
	end;
end;

function PeasantBattle()
	local rnd = random(2);
	if rnd == 0 then
		StartCombat(Gilbert, nil, 3, 1, 24*Diff, 2, 23*Diff, 1, 26*Diff, nil);
	elseif rnd == 1 then
		StartCombat(Gilbert, nil, 3, 1, 26*Diff, 106, 22*Diff, 1, 24*Diff, nil);
	end;
	sleep(5);
	if IsHeroAlive(Gilbert) == true then
		Trigger(OBJECT_TOUCH_TRIGGER, 'PeasantJoin', nil);
		JoinCreatures(2);
		RemoveObject('PeasantJoin');
	end;
end;

function GriffinBattle()
	local rnd = random(2);
	if rnd == 0 then
		StartCombat(Gilbert, nil, 3, 7, 4*Diff, 109, 3*Diff, 7, 5*Diff, nil);
	elseif rnd == 1 then
		StartCombat(Gilbert, nil, 3, 7, 5*Diff, 8, 3*Diff, 7, 4*Diff, nil);
	end;
	sleep(5);
	if IsHeroAlive(Gilbert) == true then
		Trigger(OBJECT_TOUCH_TRIGGER, 'GriffinJoin', nil);
		JoinCreatures(3);
		RemoveObject('GriffinJoin');
	end;
end;

function ConscriptBattle()
	StartCombat(Gilbert, nil, 3, 2, 19*Diff, 2, 23*Diff, 2, 16*Diff, nil);
	sleep(5);
	if IsHeroAlive(Gilbert) == true then
		Trigger(OBJECT_TOUCH_TRIGGER, 'ConsJoin', nil);
		JoinCreatures(4);
		RemoveObject('ConsJoin');
	end;
end;


-- Заставы --
function BarracksBattle(hero)
	if hero == Gilbert then
		QuestionBoxForPlayers(GetPlayerFilter(player), GetMapDataPath()..'Barracks_Question.txt', 'BarracksYes');
	end;
end;

function BarracksYes()
	StartCombat(Gilbert, nil, 3, 5, 5*Diff, 5, 5*Diff, 5, 5*Diff, nil);
	sleep(5);
	if IsHeroAlive(Gilbert) == true then
		SetObjectEnabled('Barracks', true);
		SetObjectOwner('Barracks', player);
		Trigger(OBJECT_TOUCH_TRIGGER, 'Barracks', nil);
	end;
end;

function ATowerBattle(hero)
	if hero == Gilbert then
		QuestionBoxForPlayers(GetPlayerFilter(player), GetMapDataPath()..'ATower_Question.txt', 'ATowerYes');
	end;
end;

function ATowerYes()
	StartCombat(Gilbert, nil, 3, 3, 12*Diff, 3, 12*Diff, 3, 12*Diff, nil);
	sleep(5);
	if IsHeroAlive(Gilbert) == true then
		SetObjectEnabled('ATower', true);
		SetObjectOwner('ATower', player);
		Trigger(OBJECT_TOUCH_TRIGGER, 'ATower', nil);
	end;
end;

function MilitaryBattle(hero)
	if hero == Gilbert then
		QuestionBoxForPlayers(GetPlayerFilter(player), GetMapDataPath()..'Military_Question.txt', 'MilitaryYes');
	end;
end;

function MilitaryYes()
	StartCombat(Gilbert, nil, 3, 9, 3+Diff+random(2), 9, 3+Diff+random(3), 9, 3+Diff, nil);
	sleep(5);
	if IsHeroAlive(Gilbert) == true then
		SetObjectEnabled('Military', true);
		SetObjectOwner('Military', player);
		Trigger(OBJECT_TOUCH_TRIGGER, 'Military', nil);
	end;
end;


-- задания --
function Quest01add()
	MessageBox(GetMapDataPath()..'Quest01_rum.txt');
	AddHeroCreatures(Gilbert, 3, 100-Diff*20);
	SetObjectiveState('CaptureTownQuest', OBJECTIVE_ACTIVE);
	weekCount = GetDate(ABSOLUTE_DAY);
	Trigger(REGION_ENTER_AND_STOP_TRIGGER, 'rQuest01', nil);
end;

function CheckQuest02(oldOwner, newOwner, hero)
	if newOwner == PLAYER_3 then
		SetObjectiveState('DefendTownQuest', OBJECTIVE_FAILED);
		Trigger(OBJECT_CAPTURE_TRIGGER, 'MainTown', nil);
		sleep(10);
		Loose();
	end;
end;

function Quest02Complete(looser, winner)
	if (looser == Gorhen) and (winner == Gilbert) then
		SetObjectiveState('DefendTownQuest', OBJECTIVE_COMPLETED);
		Trigger(PLAYER_REMOVE_HERO_TRIGGER, necro, nil);
		GiveArtefact(Gilbert, 16);
		sleep(25);
		startThread(Quest03add);
	end;
end;

function Quest03add()
	if GetObjectiveState('DefendTownQuest') == OBJECTIVE_COMPLETED then
		SetObjectiveState('MainQuest', OBJECTIVE_ACTIVE);
		lastTime = 1;
		OpenCircleFog(66, 69, 0, 15, PLAYER_1);
		sleep(1);
		OpenCircleFog(47, 61, 0, 15, PLAYER_1);
		sleep(1);
		OpenCircleFog(29, 53, 0, 15, PLAYER_1);
		sleep(1);
		OpenCircleFog(14, 47, 0, 15, PLAYER_1);
		sleep(1);
		MessageBox(GetMapDataPath()..'TownIsHealed.txt');
		SetTownBuildingLimitLevel('MainTown', 1, 2);
		SetTownBuildingLimitLevel('MainTown', 10, 1);
		SetTownBuildingLimitLevel('MainTown', 11, 1);
		SetTownBuildingLimitLevel('MainTown', 12, 1);
	end;
end;

function Quest03battle(hero)
	if hero == Gilbert then
		lastCountArmy = 1+lastTime;
		StartCombat(Gilbert, nil, 4, 71, lastCountArmy+22*Diff, 72, lastCountArmy+34*Diff, 72, lastCountArmy+34*Diff, 71, lastCountArmy+22*Diff, nil);
		sleep(5);
		StartCombat(Gilbert, nil, 4, 58, lastCountArmy+30*Diff, 62, lastCountArmy+20*Diff, 62, lastCountArmy+20*Diff, 58, lastCountArmy+30*Diff, nil);
		sleep(5);
		if IsHeroAlive(Gilbert) == true then
			Trigger(REGION_ENTER_AND_STOP_TRIGGER, 'rQuest03', nil);
			sleep(5);
			MessageBox(GetMapDataPath()..'RotarinFreedom.txt');
			sleep(10);
			if KnowHeroSpell(Gilbert, 1) == true then
				SetGameVar('gilbert_spell', 1);
			end;
			if HasHeroWarMachine(Gilbert, 3) == true then
				SetGameVar('gilbert_warmashine01', 3);
			end;
			if HasHeroWarMachine(Gilbert, 4) == true then
				SetGameVar('gilbert_warmashine02', 4);
			end;
			SetObjectiveState('MainQuest', OBJECTIVE_COMPLETED);
		end;
	end;
end;

-- информация --
function MainTownMess(hero)
	if hero == Gilbert then
		sleep(5);
		if IsHeroAlive(Gilbert) == true then
			MessageBox(GetMapDataPath()..'TownIsInfected.txt');
			Trigger(OBJECT_TOUCH_TRIGGER, 'MainTown', nil);
			SetObjectiveState('DefendTownQuest', OBJECTIVE_ACTIVE);
			weekCount = 0;
			timeOfWar = GetDate(DAY);
			print('two weeks started');
		end;
	end;
end;

-- триггеры --
Trigger(NEW_DAY_TRIGGER,'TimedEvents');
Trigger(OBJECT_TOUCH_TRIGGER, 'MainTown', 'MainTownMess');
Trigger(OBJECT_TOUCH_TRIGGER, 'ATower', 'ATowerBattle');
Trigger(OBJECT_TOUCH_TRIGGER, 'Barracks', 'BarracksBattle');
Trigger(OBJECT_TOUCH_TRIGGER, 'Military', 'MilitaryBattle');
Trigger(OBJECT_TOUCH_TRIGGER, 'ConsJoin', 'ConscriptBattle');
Trigger(OBJECT_TOUCH_TRIGGER, 'GriffinJoin', 'GriffinBattle');
Trigger(OBJECT_TOUCH_TRIGGER, 'PeasantJoin', 'PeasantBattle');
Trigger(OBJECT_TOUCH_TRIGGER, 'ArcherJoin', 'ArcherBattle');
Trigger(OBJECT_TOUCH_TRIGGER, 'Signpost01', 'OpenFogOfTown');
Trigger(OBJECT_TOUCH_TRIGGER, 'bCamp', 'CampBattle');
Trigger(OBJECT_CAPTURE_TRIGGER, 'MainTown', 'CheckQuest02');
-- Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, 'rQuest02', 'Quest02add');
Trigger(REGION_ENTER_AND_STOP_TRIGGER, 'rQuest03', 'Quest03battle');
Trigger(REGION_ENTER_AND_STOP_TRIGGER, 'rQuest01', 'Quest01add');
Trigger(PLAYER_REMOVE_HERO_TRIGGER, necro, 'Quest02Complete');
-- потоки --
startThread(Difficulty);

--
print('custom campaign: colonization, part 1 /version v.1.3 by KioM/');