Tan = "Tan";  --Cayrus!!!
Berein = "Berein";
EnableHeroAI("Tan",nil);

SetPlayerResource(1, 0, 0);
SetPlayerResource(1, 1, 0);
SetPlayerResource(1, 2, 0);
SetPlayerResource(1, 3, 0);
SetPlayerResource(1, 4, 0);
SetPlayerResource(1, 5, 0);
SetPlayerResource(1, 6, 1);

ChangeHeroStat("Tan", STAT_EXPERIENCE, 140000);

if GetDifficulty() == DIFFICULTY_EASY then
	diff = 0;
	AddObjectCreatures("Berein", 30, 900);
	RemoveObjectCreatures("Berein", 29, 900);
	AddObjectCreatures("Berein", 42, 20);
	RemoveObjectCreatures("Berein", 41, 20);
	print("isi");
end;

if GetDifficulty() == DIFFICULTY_NORMAL then
	diff = 1;
	print("normal");
end;

if GetDifficulty() == DIFFICULTY_HARD then
	diff = 2;
	ChangeHeroStat("Tan", 0, 100000);
	for i=1,4 do
		ChangeHeroStat("Tan", i, 3);
	end;
	TeachHeroSpell("Tan", 9);
	TeachHeroSpell("Tan", 35);
	TeachHeroSpell("Tan", 40);
	TeachHeroSpell("Tan", 235);
	ChangeHeroStat("Tan", 5, 2);
	ChangeHeroStat("Tan", 6, 2);
	AddObjectCreatures("Tan", 62, 65);
	print("hard!");
end;

if GetDifficulty() == DIFFICULTY_HEROIC then
	diff = 4;
	ChangeHeroStat("Tan", 0, 400000);
	ChangeHeroStat("Tan", 1, 2 + random(3));
	ChangeHeroStat("Tan", 2, 4 + random(4));
	ChangeHeroStat("Tan", 3, 8 + random(5));
	ChangeHeroStat("Tan", 4, 10 + random(7));
	TeachHeroSpell("Tan", 9);
	TeachHeroSpell("Tan", 35);
	TeachHeroSpell("Tan", 40);
	TeachHeroSpell("Tan", 234);
	ChangeHeroStat("Tan", 5, 4);
	ChangeHeroStat("Tan", 6, 4);
	GiveHeroWarMachine("Tan", 1);
	GiveHeroWarMachine("Tan", 3);
	GiveHeroWarMachine("Tan", 4);
	SetHeroLootable("Tan", nil);
	GiveArtefact("Tan", 44);
	GiveArtefact("Tan", 45);
	GiveArtefact("Tan", 46);
	GiveArtefact("Tan", 47);
	GiveArtefact("Tan", 51);
	GiveArtefact("Tan", 22);
	GiveArtefact("Tan", 69);
	GiveArtefact("Tan", 49);
	GiveArtefact("Tan", 40);
	AddObjectCreatures("Tan", 62, 100);
	print("heroic!");
end;

function sairus_up()
	sleep(1);
	if diff > 2 then
		SetHeroCombatScript("Tan", '/Maps/Scenario/C3M4/SairusCombat.xdb#xpointer(/Script)');
	end;
end;

----------------------------------//Titans
function mob1()
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "100", nil );
MessageBox ("/Maps/Scenario/C3M4/Message/C3M4_1.txt");
CreateMob(1,CREATURE_TITAN,3+diff,66,90,1,2,1,90);
end;

function mob2()	
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "200", nil );
CreateMob(2,CREATURE_TITAN,2*diff+4,117,92,1,2,1);
end;

function mob3()
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "300", nil );
CreateMob(3,CREATURE_TITAN,3*diff+7,38,20,1,2,1,90);
end;

function mob4()
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "400", nil );
CreateMob(3,CREATURE_TITAN,5*diff+7,12,63,71,1,2,1);
CreateMob(3,CREATURE_TITAN,10*diff+45,63,64,1,2,1,180);
end;

--------------------------------//Start first dialog
function Dialog1()
StartDialogScene("/DialogScenes/C3/M4/R1/DialogScene.xdb#xpointer(/DialogScene)"); ----//Start final dialog
end;

wizards = {"GREMLIN", "GARGOYLE", "GOLEM", "MAGI", "RAKSHASA", "GENIE", "GIANT", "TITAN"};
transfer_arts = {4, 6, 33, 70};
path = "C3M3_";
new_path = "C3M4_";

-------------------------------//Objectives

function PObjective1()
	while 1 do	
		sleep( 10 );	
		if IsHeroAlive("Tan") == nil then
			print("Cayrus dead.................................");
			SetObjectiveState('prim1',OBJECTIVE_COMPLETED);
			break;
		end;
	end;
end;

function PObjective2()
	while 1 do
		sleep(10);
		if IsHeroAlive("Berein") == nil then
			print("Berein dead.................................");
			SetObjectiveState('prim2',OBJECTIVE_FAILED);
			break;
		end;
	end;
end;

-----------------------------//Winner
function WinLoose()
	while 1 do
		if GetObjectiveState("prim1") == OBJECTIVE_COMPLETED then
			StartDialogScene("/DialogScenes/C3/M4/D1/DialogScene.xdb#xpointer(/DialogScene)"); ----//Start final dialog
			GiveExp( "Berein", 500 ); ---addexp!!!
			GiveArtefact("Berein", 70);
			sleep(1);
			startThread(save_artefact, "Berein", new_path, transfer_arts);
			sleep(20);
			Win();
			return
		end;
		if GetObjectiveState("prim2") == OBJECTIVE_FAILED then
			Loose();
			return
		end;
		sleep();
	end;
end;
----------------------------------
function setupNeutrals()
	sleep(1);
	if diff > 1 then
		startThread(add_neutrals, diff - 1);
	end;
end;

function add_army(mon, ind)
	if IsObjectExists(mon) then
		for i = 1, 90 do
			if GetObjectCreatures(mon, i) > 0 then
				AddObjectCreatures(mon, i, ind * GetObjectCreatures(mon, i));
			end;
		end;
	end;
end;

function add_neutrals(coef)
	local creatures = {};
	for i, name in wizards do
		creatures = GetObjectNamesByType(name);
		if creatures ~= {} then
			print("add some "..name);
			for j, mon in creatures do
				startThread(add_army, mon, coef);
			end;
		end;
	end;
end;

function load_arts(hero, mission)
	sleep(1);
	if IsHeroAlive(hero) then
		for i = 1, 71 do
			if GetGameVar(mission..hero.."_"..i) == "1" then
				GiveArtefact(hero, i);
			end;
		end;
	end;
end;

function save_artefact(hero, mission, array)
	if IsHeroAlive(hero) then
		for i, art in array do
			if HasArtefact(hero, art) then
				SetGameVar(mission..hero.."_"..art, "1");
				print("saving artefact "..art);
			end;
		end;
	end;
end;

function Post_Bone2()
	Trigger( REGION_ENTER_AND_STOP_TRIGGER, "Bone",nil);
	SetObjectOwner("B1",1);
	SetObjectOwner("B2",1);
	MessageBox ("/Maps/Scenario/C3M4/Outpost.txt");
end;
--------------------------------//Main thread

startThread(Dialog1);
startThread(setupNeutrals);
startThread(load_arts, "Berein", path);

Trigger( REGION_ENTER_AND_STOP_TRIGGER, "100","mob1" );
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "200","mob2" );
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "300","mob3" );
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "400","mob4" );
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "Bone","Post_Bone2");

startThread(PObjective1);
startThread(PObjective2);
startThread(WinLoose);
startThread(sairus_up);
print("all started");
