--#########################--
--- Карта: Последний рубеж ---
--- Версия карты: 1.3.3    ---
--- Версия игры: 3.1       ---
--- Автор: KioM            ---
--#########################--

--- Константы ---
-----------------

obelisk = {"obelisk1", "obelisk2", "obelisk3"};

Diff = 0;
player = 1;
disable = 0;
enable = 1;
attackMove = 0;
attackCount = 0;
openFogForSiege = 0;
TOWN_NONE = "";

DragonSet = {36, 37, 38, 39, 40, 41, 42, 43}; -- комплект из чешуи дракона

Sound_destroyed = "/Maps/SingleMissions/OnLastPoint/Siege_WallCrash02sound.xdb#xpointer(/Sound)";

Effect_destroyed01 = "/Effects/_(Effect)/Start_dust_S.xdb#xpointer(/Effect)";
Effect_destroyed02 = "/Effects/_(Effect)/Level_2b.xdb#xpointer(/Effect)";

PortalEntrance = "AdvMapStaticShared:/Maps/SingleMissions/OnLastPoint/Portal_entrance.(AdvMapStaticShared).xdb#xpointer(/AdvMapStaticShared)";
PortalExit = "AdvMapStaticShared:/Maps/SingleMissions/OnLastPoint/Portal_exit.(AdvMapStaticShared).xdb#xpointer(/AdvMapStaticShared)";
lightStatic = "AdvMapStaticShared:/Maps/SingleMissions/OnLastPoint/lightStatic.(AdvMapStaticShared).xdb#xpointer(/AdvMapStaticShared)";
darkStatic = "AdvMapStaticShared:/Maps/SingleMissions/OnLastPoint/darkStatic.(AdvMapStaticShared).xdb#xpointer(/AdvMapStaticShared)";

-- атак-счетчики
a_ATTACK_COMPLETE = 25;
a1_ATTACK01 = 1; -- Dwarven
a1_ATTACK02 = 2; -- Dwarven, Efion
a1_ATTACK03 = 3; -- Dwarven, Marder
a1_ATTACK04 = 4; -- Dwarven, Orlando
a1_ATTACK05 = 5; -- Dwarven, Deleb
a2_ATTACK01 = 6; -- Heaven
a2_ATTACK02 = 7; -- Heaven, Efion
a2_ATTACK03 = 8; -- Heaven, Marder
a2_ATTACK04 = 9; -- Heaven, Orlando
a2_ATTACK05 = 10; -- Heaven, Deleb
a3_ATTACK01 = 11; -- Sylvan
a3_ATTACK02 = 12; -- Sylvan, Efion
a3_ATTACK03 = 13; -- Sylvan, Marder
a3_ATTACK04 = 14; -- Sylvan, Orlando
a3_ATTACK05 = 15; -- Sylvan, Deleb

-- Baghar Attack 
-- This is last attack
a1_ATTACK06 = 17;
a2_ATTACK06 = 18;
a3_ATTACK06 = 19;

-- герои игрока
Olifand = "Una";
Boris = "Ving";
Silein = "Linaas";
Atlant = "Atlant";

-- демоны
Oddrema = "Oddrema";
Efion = "Efion";
Marder = "Marder";
Orlando = "Orlando";
Deleb = "Deleb";
Baghar = "Arantir"; -- combat script custom (necro+demonlord)
Ahmad = "Nymus"; -- combat script custom +1

-- города
TOWN_INFERNO = "InfernoTown";
TOWN_DWARVEN = "DwarvenTown";
TOWN_HEAVEN = "HeavenTown";
TOWN_SYLVAN = "SylvanTown";

WALL_DWARVEN = "Wall01";
WALL_HEAVEN = "Wall02";
WALL_SYLVAN = "Wall03";

-- режим сложности --
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 AttackToTown(hero, town)
    heroName = hero;
    townName = town;
    x, y = 0;
    
    if IsHeroAlive(heroName) == true then
        SetObjectPosition(heroName, 172, 159, 0, 0);
        sleep(25);
        if townName == TOWN_DWARVEN then
            x = 144;
            y = 145;
            attackMove = 1;
        elseif townName == TOWN_HEAVEN then
            x = 48;
            y = 88;
            attackMove = 2;
        elseif townName == TOWN_SYLVAN then
            x = 142;
            y = 56;
            attackMove = 3;
        end;
        print(townName.." Siege Started");
        MoveHeroAndReachPoint(heroName, x, y, 0);
    else
        print("AttackToTown: "..heroName.." not found...");
    end;
end;

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

-- разрушает город и стены
function RazedTownAndPlayEffects(town, wall)
    local x, y, floor = GetObjectPosition(town);
    Play2DSound(Sound_destroyed);
    PlayVisualEffect(Effect_destroyed01, "","destTown", x, y, 0, 0);
    PlayVisualEffect(Effect_destroyed02, "", "destTown", x, y, 0, 0);
    RazeTown(town);
    RemoveObject(wall);
    sleep(15);
    StopVisualEffects("destTown");
end;

function ResetCamera(objectName)
    local x, y, floor = GetObjectPosition(objectName);
    MoveCamera(x, y, 0, 30, 1, 0, 1, 1, 1);
end;

-- загрузка данных --
function InitSetMap()
    -- игрок
    SetPlayerStartResources(player, 50-Diff*10, 50-Diff*10, 50-Diff*10, 25-Diff*5, 25-Diff*5, 25-Diff*5, 30000-Diff*5000);
    AddHeroCreatures(Olifand, 92, 92-Diff*7);
    AddHeroCreatures(Olifand, 94, 41-Diff*3);
    AddHeroCreatures(Olifand, 96, 16-Diff*2);
    
    AddHeroCreatures(Boris, 7, 7-Diff, 0);
    AddHeroCreatures(Boris, 7, 9-Diff, 1);
    AddHeroCreatures(Boris, 7, 8-Diff, 2);
    
    AddHeroCreatures(Silein, 43, 62-Diff*3);
    AddHeroCreatures(Silein, 45, 41-Diff*3);
    AddHeroCreatures(Silein, 47, 15-Diff*2);

    -- комп
    EnableHeroAI(Oddrema, nil);
    EnableHeroAI(Efion, nil);
    EnableHeroAI(Marder, nil);
    EnableHeroAI(Orlando, nil);
    EnableHeroAI(Deleb, nil);
    EnableHeroAI(Baghar, nil);
    EnableHeroAI(Ahmad, nil);
    EnableHeroAI(Atlant, nil);
    EnableHeroAI("Biara", nil);
    EnableHeroAI("Djyn", nil);
    EnableHeroAI("Nur", nil);
    
    SetHeroLootable(Oddrema, nil);
    SetHeroLootable(Efion, nil);
    SetHeroLootable(Marder, nil);
    SetHeroLootable(Orlando, nil);
    SetHeroLootable(Deleb, nil);
    SetHeroLootable(Baghar, nil);
    SetHeroLootable("Nur", nil);
    DoNotGiveTurnToPlayerAIIfNoTownsAndActiveHeroes(3, true);
    DenyAIHeroesFlee(PLAYER_2, 1);
    
    -- первая фаза
    AddHeroCreatures(Oddrema, 15, 24*Diff);
    AddHeroCreatures(Oddrema, 17, 18*Diff);
    AddHeroCreatures(Oddrema, 19, 8*Diff);
    ChangeHeroStat(Oddrema, 0, 3000+Diff*1000);
    ChangeHeroStat(Oddrema, 1, 1+Diff*2);
    ChangeHeroStat(Oddrema, 2, 1+Diff);
    ChangeHeroStat(Oddrema, 3, 1+Diff);
    ChangeHeroStat(Oddrema, 4, 1+Diff);
    
    -- вторая фаза
    AddHeroCreatures(Efion, 15, 55*Diff);
    AddHeroCreatures(Efion, 17, 32*Diff);
    AddHeroCreatures(Efion, 21, 14*Diff);
    AddHeroCreatures(Efion, 23, 8*Diff);
    ChangeHeroStat(Efion, 0, 8000+Diff*2000);
    ChangeHeroStat(Efion, 1, 2+Diff*2);
    ChangeHeroStat(Efion, 2, 1+Diff*2);
    ChangeHeroStat(Efion, 3, 2+Diff);
    ChangeHeroStat(Efion, 4, 2+Diff);
    
    -- третья фаза
    AddHeroCreatures(Marder, 15, 65*Diff);
    AddHeroCreatures(Marder, 17, 52*Diff);
    AddHeroCreatures(Marder, 19, 36*Diff);
    AddHeroCreatures(Marder, 21, 24*Diff);
    AddHeroCreatures(Marder, 23, 12*Diff);
    AddHeroCreatures(Marder, 25, 6*Diff);
    AddHeroCreatures(Marder, 27, 2*Diff);
    ChangeHeroStat(Marder, 0, 30000+Diff*3000);
    ChangeHeroStat(Marder, 1, 3+Diff*2);
    ChangeHeroStat(Marder, 2, 1+Diff*2);
    ChangeHeroStat(Marder, 3, 2+Diff);
    ChangeHeroStat(Marder, 4, 4+Diff);
    
    -- четвертая фаза
    AddHeroCreatures(Orlando, 16, 98*Diff);
    AddHeroCreatures(Orlando, 18, 72*Diff);
    AddHeroCreatures(Orlando, 20, 54*Diff);
    AddHeroCreatures(Orlando, 22, 33*Diff);
    AddHeroCreatures(Orlando, 24, 18*Diff);
    AddHeroCreatures(Orlando, 26, 8*Diff);
    AddHeroCreatures(Orlando, 28, 4*Diff);
    ChangeHeroStat(Orlando, 0, 80000+Diff*15000);
    ChangeHeroStat(Orlando, 1, 6+Diff*2);
    ChangeHeroStat(Orlando, 2, 4+Diff*2);
    ChangeHeroStat(Orlando, 3, 8+Diff);
    ChangeHeroStat(Orlando, 4, 6+Diff);
    
    -- пятая фаза
    AddHeroCreatures(Deleb, 131, 156*Diff);
    AddHeroCreatures(Deleb, 132, 92*Diff);
    AddHeroCreatures(Deleb, 133, 67*Diff);
    AddHeroCreatures(Deleb, 134, 42*Diff);
    AddHeroCreatures(Deleb, 135, 29*Diff);
    AddHeroCreatures(Deleb, 136, 12*Diff);
    AddHeroCreatures(Deleb, 137, 6*Diff);
    ChangeHeroStat(Deleb, 0, 120000+Diff*25000);
    ChangeHeroStat(Deleb, 1, 12+Diff*3);
    ChangeHeroStat(Deleb, 2, 10+Diff*2);
    ChangeHeroStat(Deleb, 3, 11+Diff);
    ChangeHeroStat(Deleb, 4, 8+Diff);
    
    -- шестая фаза
    AddHeroCreatures(Baghar, 137, 12+random(5)*Diff);
    AddHeroCreatures(Baghar, 28, 14+random(5)*Diff);
    AddHeroCreatures(Baghar, 134, 95+random(15)*Diff);
    AddHeroCreatures(Baghar, 158, 10+random(5)*Diff);
    AddHeroCreatures(Baghar, 42, 18+random(5)*Diff);
    AddHeroCreatures(Baghar, 154, 85+random(15)*Diff);
    AddHeroCreatures(Baghar, 24, 57+random(6)*Diff);
    ChangeHeroStat(Baghar, 0, 305000+Diff*85000);
    ChangeHeroStat(Baghar, 1, 16+Diff*3);
    ChangeHeroStat(Baghar, 2, 11+Diff*3);
    ChangeHeroStat(Baghar, 3, 16+Diff);
    ChangeHeroStat(Baghar, 4, 9+Diff);
	SetHeroCombatScript(Baghar, "Script:Maps/SingleMissions/OnLastPoint/BagharSiege.(Script).xdb#xpointer(/Script)");
    
    -- замок инферно
    AddObjectCreatures(TOWN_INFERNO, 28, 26+random(5)*Diff, 1);
    AddObjectCreatures(TOWN_INFERNO, 26, 65+random(15)*Diff, 2);
    AddObjectCreatures(TOWN_INFERNO, 22, 90+random(30)*Diff, 3);
    AddObjectCreatures(TOWN_INFERNO, 16, 850+random(50)*Diff, 4);
    AddObjectCreatures(TOWN_INFERNO, 137, 90+random(35)*Diff, 5);
    AddObjectCreatures(TOWN_INFERNO, 26, 65+random(15)*Diff, 6);
    AddObjectCreatures(TOWN_INFERNO, 28, 26+random(5)*Diff, 7);
    ChangeHeroStat(Ahmad, 0, 1120000+Diff*1000000);
    ChangeHeroStat(Ahmad, 1, 32+Diff*3);
    ChangeHeroStat(Ahmad, 2, 27+Diff*3);
    ChangeHeroStat(Ahmad, 3, 21+Diff*2);
    ChangeHeroStat(Ahmad, 4, 14+Diff);
    SetHeroCombatScript(Ahmad, "Script:Maps/SingleMissions/OnLastPoint/InfernoTownCombat.(Script).xdb#xpointer(/Script)");
    
    -- армия атланта
    AddHeroCreatures(Atlant, 58, 450+random(25)*Diff);
    AddHeroCreatures(Atlant, 60, 370+random(12)*Diff);
    AddHeroCreatures(Atlant, 62, 260+random(10)*Diff);
    AddHeroCreatures(Atlant, 64, 110+random(6)*Diff);
    AddHeroCreatures(Atlant, 66, 82+random(4)*Diff);
    AddHeroCreatures(Atlant, 68, 45+random(3)*Diff);
    AddHeroCreatures(Atlant, 70, 22+random(2)*Diff);
    ChangeHeroStat(Atlant, 1, 17+Diff*3);
    if Diff == 4 then
        ChangeHeroStat(Atlant, 2, 30+Diff*3);
    else
        ChangeHeroStat(Atlant, 2, 11+Diff*3);
    end;
    ChangeHeroStat(Atlant, 3, 12+Diff*2);
    ChangeHeroStat(Atlant, 4, 9+Diff*2);

    -- испытание хаос
    AddHeroCreatures("Nur", 64, 20+random(5)*Diff);
    AddHeroCreatures("Nur", 162, 20+random(5)*Diff);
    AddHeroCreatures("Nur", 101, 18+random(5)*Diff);
    AddHeroCreatures("Nur", 170, 18+random(5)*Diff);
    AddHeroCreatures("Nur", 69, 8+random(4)*Diff);
    ChangeHeroStat("Nur", 0, 120000+Diff*25000);
    ChangeHeroStat("Nur", 1, 3+Diff*3);
    ChangeHeroStat("Nur", 2, 2+Diff*3);
    ChangeHeroStat("Nur", 3, 5+Diff*2);
    ChangeHeroStat("Nur", 4, 7+Diff*2);

    SetObjectEnabled("oDwellingDwarwen_01", nil);
    SetObjectEnabled("oDwellingDwarwen_02", nil);
    SetObjectEnabled("oDwellingDwarwen_03", nil);
    SetObjectEnabled("oDwellingDwarwen_04", nil);
    SetObjectEnabled("oDwellingDwarwen_05", nil);
    SetObjectEnabled("oDwellingDwarwen_06", nil);
    SetObjectEnabled("oDwellingHeaven_01", nil);
    SetObjectEnabled("oDwellingHeaven_02", nil);
    SetObjectEnabled("oDwellingHeaven_03", nil);
    SetObjectEnabled("oDwellingHeaven_04", nil);
    SetObjectEnabled("oDwellingHeaven_05", nil);
    SetObjectEnabled("oDwellingHeaven_06", nil);
    SetObjectEnabled("oDwellingSylvan_01", nil);
    SetObjectEnabled("oDwellingSylvan_02", nil);
    SetObjectEnabled("oDwellingSylvan_03", nil);
    SetObjectEnabled("oDwellingSylvan_04", nil);
    SetObjectEnabled("oDwellingSylvan_05", nil);
    SetObjectEnabled("oDwellingSylvan_06", nil);
    SetObjectEnabled("qHouse", nil);
    SetObjectEnabled("tradingPost1", nil);
    SetObjectEnabled("tradingPost2", nil);
    SetObjectEnabled("tradingPost3", nil);
    SetObjectEnabled("phoenixDwell", nil);

    SetRegionBlocked("rLabBlock01", true, 3);
    SetRegionBlocked("rLabBlock02", true, 3);
    SetRegionBlocked("rLabBlock03", true, 3);
    SetRegionBlocked("rLabBlock04", true, 3);
end;

-- обвновление армии --
function UpdateArmyForHero(hero, delta)
    deltaDiff = Diff*delta;
    if hero == Oddrema then
        AddHeroCreatures(Oddrema, 15, 32*deltaDiff);
        AddHeroCreatures(Oddrema, 17, 20*deltaDiff);
        AddHeroCreatures(Oddrema, 21, 12*deltaDiff);
    elseif hero == Efion then
        AddHeroCreatures(Efion, 15, 50*deltaDiff);
        AddHeroCreatures(Efion, 17, 35*deltaDiff);
        AddHeroCreatures(Efion, 21, 16*deltaDiff);
        AddHeroCreatures(Efion, 23, 6*deltaDiff);
    elseif hero == Marder then
        AddHeroCreatures(Marder, 15, 65*deltaDiff);
        AddHeroCreatures(Marder, 17, 52*deltaDiff);
        AddHeroCreatures(Marder, 19, 36*deltaDiff);
        AddHeroCreatures(Marder, 21, 16*deltaDiff);
        AddHeroCreatures(Marder, 23, 11*deltaDiff);
        AddHeroCreatures(Marder, 25, 6*deltaDiff);
        AddHeroCreatures(Marder, 27, 2*deltaDiff);
    elseif hero == Orlando then
        AddHeroCreatures(Orlando, 16, 98*deltaDiff);
        AddHeroCreatures(Orlando, 18, 66*deltaDiff);
        AddHeroCreatures(Orlando, 20, 44*deltaDiff);
        AddHeroCreatures(Orlando, 22, 23*deltaDiff);
        AddHeroCreatures(Orlando, 24, 16*deltaDiff);
        AddHeroCreatures(Orlando, 26, 7*deltaDiff);
        AddHeroCreatures(Orlando, 28, 3*deltaDiff);
    elseif hero == Deleb then
        AddHeroCreatures(Deleb, 131, 146*deltaDiff);
        AddHeroCreatures(Deleb, 132, 82*deltaDiff);
        AddHeroCreatures(Deleb, 133, 57*deltaDiff);
        AddHeroCreatures(Deleb, 134, 35*deltaDiff);
        AddHeroCreatures(Deleb, 135, 21*deltaDiff);
        AddHeroCreatures(Deleb, 136, 9*deltaDiff);
        AddHeroCreatures(Deleb, 137, 4*deltaDiff);
    elseif hero == Baghar then
        AddHeroCreatures(Baghar, 137, 6+random(5)*deltaDiff);
        AddHeroCreatures(Baghar, 28, 7+random(5)*deltaDiff);
        AddHeroCreatures(Baghar, 134, 35+random(16)*deltaDiff);
        AddHeroCreatures(Baghar, 158, 7+random(5)*deltaDiff);
        AddHeroCreatures(Baghar, 42, 6+random(5)*deltaDiff);
        AddHeroCreatures(Baghar, 154, 45+random(25)*deltaDiff);
        AddHeroCreatures(Baghar, 24, 37+random(10)*deltaDiff);
    end;
    print("UpdateArmyForHero: The army for hero "..hero.." updated");
end;

function openFog(fogLevel)
    if fogLevel == 0 then
        OpenCircleFog(171, 158, 0, 10, player);
        OpenCircleFog(166, 152, 0, 10, player);
        OpenCircleFog(157, 148, 0, 10, player);
        OpenCircleFog(148, 146, 0, 10, player);
    elseif fogLevel == 1 then
        OpenCircleFog(44, 91, 0, 9, player);
        OpenCircleFog(40, 93, 0, 9, player);
        OpenCircleFog(34, 100, 0, 9, player);
        OpenCircleFog(31, 102, 0, 9, player);
    elseif fogLevel == 2 then
        OpenCircleFog(145, 58, 0, 9, player);
        OpenCircleFog(151, 64, 0, 9, player);
    end;
end;

-- битвы: наступление битв, награды, счетчики --
function addDwellingCreatures()
    dwellingRnd = random(6);
    dwellingCreatures = 0;
    dwellingCount = 0;
    text = nil;
    
    if IsObjectExists(TOWN_DWARVEN) == true then
        if dwellingRnd == 0 then
            dwellingCreatures = 92;
            text = GetMapDataPath().."dwelling_d01.txt";
        elseif dwellingRnd == 1 then
            dwellingCreatures = 94;
            text = GetMapDataPath().."dwelling_d02.txt";
        elseif dwellingRnd == 2 then
            dwellingCreatures = 96;
            text = GetMapDataPath().."dwelling_d03.txt";
        elseif dwellingRnd == 3 then
            dwellingCreatures = 98;
            text = GetMapDataPath().."dwelling_d04.txt";
        elseif dwellingRnd == 4 then
            dwellingCreatures = 100;
            text = GetMapDataPath().."dwelling_d05.txt";
        elseif dwellingRnd == 5 then
            dwellingCreatures = 102;
            text = GetMapDataPath().."dwelling_d06.txt";
        elseif dwellingRnd == 6 then
            dwellingCreatures = 104;
            text = GetMapDataPath().."dwelling_d07.txt";
        end;
        count = GetObjectDwellingCreatures(TOWN_DWARVEN, dwellingCreatures);
        if count > 0 then
            dwellingCount = count*3;
            SetObjectDwellingCreatures(TOWN_DWARVEN, dwellingCreatures, dwellingCount);
            MessageBox(text);
        else
            print("addDwellingCreatures: Dwelling (creatures id: "..dwellingCreatures.." in Dwarven town not found");
        end;
    elseif IsObjectExists(TOWN_HEAVEN) == true then
        if dwellingRnd == 0 then
            dwellingCreatures = 1;
            text = GetMapDataPath().."dwelling_h01.txt";
        elseif dwellingRnd == 1 then
            dwellingCreatures = 3;
            text = GetMapDataPath().."dwelling_h02.txt";
        elseif dwellingRnd == 2 then
            dwellingCreatures = 5;
            text = GetMapDataPath().."dwelling_h03.txt";
        elseif dwellingRnd == 3 then
            dwellingCreatures = 7;
            text = GetMapDataPath().."dwelling_h04.txt";
        elseif dwellingRnd == 4 then
            dwellingCreatures = 9;
            text = GetMapDataPath().."dwelling_h05.txt";
        elseif dwellingRnd == 5 then
            dwellingCreatures = 11;
            text = GetMapDataPath().."dwelling_h06.txt";
        elseif dwellingRnd == 6 then
            dwellingCreatures = 13;
            text = GetMapDataPath().."dwelling_h07.txt";
        end;
        count = GetObjectDwellingCreatures(TOWN_HEAVEN, dwellingCreatures);
        if count > 0 then
            dwellingCount = count*3;
            SetObjectDwellingCreatures(TOWN_HEAVEN, dwellingCreatures, dwellingCount);
            MessageBox(text);
        else
            print("addDwellingCreatures: Dwelling (creatures id: "..dwellingCreatures.." in Heaven town not found");
        end;
    elseif IsObjectExists(TOWN_SYLVAN) == true then
        if dwellingRnd == 0 then
            dwellingCreatures = 43;
            text = GetMapDataPath().."dwelling_s01.txt";
        elseif dwellingRnd == 1 then
            dwellingCreatures = 45;
            text = GetMapDataPath().."dwelling_s02.txt";
        elseif dwellingRnd == 2 then
            dwellingCreatures = 47;
            text = GetMapDataPath().."dwelling_s03.txt";
        elseif dwellingRnd == 3 then
            dwellingCreatures = 49;
            text = GetMapDataPath().."dwelling_s04.txt";
        elseif dwellingRnd == 4 then
            dwellingCreatures = 51;
            text = GetMapDataPath().."dwelling_s05.txt";
        elseif dwellingRnd == 5 then
            dwellingCreatures = 53;
            text = GetMapDataPath().."dwelling_s06.txt";
        elseif dwellingRnd == 6 then
            dwellingCreatures = 55;
            text = GetMapDataPath().."dwelling_s07.txt";
        end;
        count = GetObjectDwellingCreatures(TOWN_SYLVAN, dwellingCreatures);
        print("addDwellingCreatures: creatures count = "..count);
        if count > 0 then
            dwellingCount = count*3;
            SetObjectDwellingCreatures(TOWN_SYLVAN, dwellingCreatures, dwellingCount);
            MessageBox(text);
        else
            print("addDwellingCreatures: Dwelling (creatures id: "..dwellingCreatures.." in Sylvan town not found");
        end;
    end;
end;

function NewDayEvent()
    if (GetDate(DAY_OF_WEEK) == 1) and (GetDate(WEEK) ~= 1) then
        addDwellingCreatures();
    end;
    if GetDate(ABSOLUTE_DAY) == 2 then
        MessageBox(GetMapDataPath().."OddremaBegin.txt");
    end;
    if GetDate(ABSOLUTE_DAY) == 9 then
        MessageBox(GetMapDataPath().."BeginDemonAttack.txt");
    end;
    if GetDate(ABSOLUTE_DAY) == 16 then -- 1
        MessageBox(GetMapDataPath().."DemonAttack.txt");
        sleep(5);
        BlockGame();
        MoveCamera(172, 159, 0, 30, 1.2, 0, 1, 1, 0);
        openFog(0);
        UnblockGame();
        sleep(35);
        AttackToTown(Oddrema, TOWN_DWARVEN);
        sleep(7);
        ResetCamera(TOWN_DWARVEN);
    end;
    if GetDate(ABSOLUTE_DAY) == 23 then
        MessageBox(GetMapDataPath().."BeginDemonAttack.txt");
    end;
    if GetDate(ABSOLUTE_DAY) == 30 then -- 2
        MessageBox(GetMapDataPath().."DemonAttack.txt");
        sleep(5);
        BlockGame();
        MoveCamera(172, 159, 0, 30, 1.2, 0, 1, 1, 0);
        UnblockGame();
        sleep(35);
        if attackCount == a1_ATTACK02 then
            AttackToTown(Efion, TOWN_DWARVEN);
            sleep(7);
            ResetCamera(TOWN_DWARVEN);
        elseif attackCount == a2_ATTACK01 then
            UpdateArmyForHero(Oddrema, 1.5); -- +50% for army
            if openFogForSiege == 0 then
                openFog(1);
                openFogForSiege = 1;
            end;
            AttackToTown(Oddrema, TOWN_HEAVEN);
            sleep(7);
            ResetCamera(TOWN_HEAVEN);
        end;
    end;
    if GetDate(ABSOLUTE_DAY) == 37 then
        MessageBox(GetMapDataPath().."BeginDemonAttack.txt");
    end;
    if GetDate(ABSOLUTE_DAY) == 44 then -- 3
        MessageBox(GetMapDataPath().."DemonAttack.txt");
        sleep(5);
        BlockGame();
        MoveCamera(172, 159, 0, 30, 1.2, 0, 1, 1, 0);
        UnblockGame();
        sleep(35);
        if attackCount == a1_ATTACK03 then
            AttackToTown(Marder, TOWN_DWARVEN);
            sleep(7);
            ResetCamera(TOWN_DWARVEN);
        elseif attackCount == a2_ATTACK02 then
            UpdateArmyForHero(Efion, 1.5); -- +50% for army
            if openFogForSiege == 0 then
                openFog(1);
                openFogForSiege = 1;
            end;
            AttackToTown(Efion, TOWN_HEAVEN);
            sleep(7);
            ResetCamera(TOWN_HEAVEN);
        elseif attackCount == a3_ATTACK01 then
            UpdateArmyForHero(Oddrema, 2); -- +100% for army
            if openFogForSiege == 1 then
                openFog(2);
                openFogForSiege = 2;
            end;
            AttackToTown(Oddrema, TOWN_SYLVAN);
            sleep(7);
            ResetCamera(TOWN_SYLVAN);
        end;
    end;
    if GetDate(ABSOLUTE_DAY) == 51 then
        MessageBox(GetMapDataPath().."BeginDemonAttack.txt");
    end;
    if GetDate(ABSOLUTE_DAY) == 58 then -- 4
        MessageBox(GetMapDataPath().."DemonAttack.txt");
        sleep(5);
        BlockGame();
        MoveCamera(172, 159, 0, 30, 1.2, 0, 1, 1, 0);
        UnblockGame();
        sleep(35);
        if attackCount == a1_ATTACK04 then
            AttackToTown(Orlando, TOWN_DWARVEN);
            sleep(7);
            ResetCamera(TOWN_DWARVEN);
        elseif attackCount == a2_ATTACK03 then
            UpdateArmyForHero(Marder, 1.5); -- +50% for army
            if openFogForSiege == 0 then
                openFog(1);
                openFogForSiege = 1;
            end;
            AttackToTown(Marder, TOWN_HEAVEN);
            sleep(5);
            ResetCamera(TOWN_HEAVEN);
        elseif attackCount == a3_ATTACK02 then
            UpdateArmyForHero(Efion, 2); -- +100% for army
            if openFogForSiege == 1 then
                openFog(2);
                openFogForSiege = 2;
            end;
            AttackToTown(Efion, TOWN_SYLVAN);
            sleep(5);
            ResetCamera(TOWN_SYLVAN);
        end;
    end;
    if GetDate(ABSOLUTE_DAY) == 65 then
        MessageBox(GetMapDataPath().."BeginDemonAttack.txt");
    end;
    if GetDate(ABSOLUTE_DAY) == 72 then -- 5
        MessageBox(GetMapDataPath().."DemonAttack.txt");
        sleep(5);
        BlockGame();
        MoveCamera(172, 159, 0, 30, 1.2, 0, 1, 1, 0);
        UnblockGame();
        sleep(35);
        if attackCount == a1_ATTACK05 then
            AttackToTown(Deleb, TOWN_DWARVEN);
            sleep(5);
            ResetCamera(TOWN_DWARVEN);
        elseif attackCount == a2_ATTACK04 then
            UpdateArmyForHero(Orlando, 1.5); -- +50% for army
            if openFogForSiege == 0 then
                openFog(1);
                openFogForSiege = 1;
            end;
            AttackToTown(Orlando, TOWN_HEAVEN);
            sleep(5);
            ResetCamera(TOWN_HEAVEN);
        elseif attackCount == a3_ATTACK03 then
            UpdateArmyForHero(Marder, 2); -- +100% for army
            if openFogForSiege == 1 then
                openFog(2);
                openFogForSiege = 2;
            end;
            AttackToTown(Marder, TOWN_SYLVAN);
            sleep(5);
            ResetCamera(TOWN_SYLVAN);
        end;
    end;
    if GetDate(ABSOLUTE_DAY) == 79 then
        if attackCount ~= a_ATTACK_COMPLETE then
            MessageBox(GetMapDataPath().."BeginDemonAttack.txt");
        end;
    end;
    if GetDate(ABSOLUTE_DAY) == 86 then -- 5/6
        MessageBox(GetMapDataPath().."DemonAttack.txt");
        sleep(5);
        BlockGame();
        MoveCamera(172, 159, 0, 30, 1.2, 0, 1, 1, 0);
        UnblockGame();
        sleep(35);
        if attackCount == a1_ATTACK06 then
            AttackToTown(Baghar, TOWN_DWARVEN);
            sleep(7);
            ResetCamera(TOWN_DWARVEN);
        elseif attackCount == a2_ATTACK05 then
            UpdateArmyForHero(Deleb, 1.5); -- +50% for army
            if openFogForSiege == 0 then
                openFog(1);
                openFogForSiege = 1;
            end;
            AttackToTown(Deleb, TOWN_HEAVEN);
            sleep(7);
            ResetCamera(TOWN_HEAVEN);
        elseif attackCount == a3_ATTACK04 then
            UpdateArmyForHero(Orlando, 2); -- +100% for army
            if openFogForSiege == 1 then
                openFog(2);
                openFogForSiege = 2;
            end;
            AttackToTown(Orlando, TOWN_SYLVAN);
            sleep(7);
            ResetCamera(TOWN_SYLVAN);
        end;
    end;
    if GetDate(ABSOLUTE_DAY) == 93 then
        if attackCount ~= a_ATTACK_COMPLETE then
            MessageBox(GetMapDataPath().."BeginDemonAttack.txt");
        end;
    end;
    if GetDate(ABSOLUTE_DAY) == 100 then -- 5/7
        MessageBox(GetMapDataPath().."DemonAttack.txt");
        sleep(5);
        BlockGame();
        MoveCamera(172, 159, 0, 30, 1.2, 0, 1, 1, 0);
        UnblockGame();
        sleep(35);
        if attackCount == a2_ATTACK06 then
            UpdateArmyForHero(Baghar, 1.5); -- +50% for army
            if openFogForSiege == 0 then
                openFog(1);
                openFogForSiege = 1;
            end;
            AttackToTown(Baghar, TOWN_HEAVEN);
            sleep(7);
            ResetCamera(TOWN_HEAVEN);
        elseif attackCount == a3_ATTACK05 then
            UpdateArmyForHero(Deleb, 2); -- +100% for army
            if openFogForSiege == 1 then
                openFog(2);
                openFogForSiege = 2;
            end;
            AttackToTown(Deleb, TOWN_SYLVAN);
            sleep(7);
            ResetCamera(TOWN_SYLVAN);
        end;
    end;
    if GetDate(ABSOLUTE_DAY) == 107 then
        if attackCount ~= a_ATTACK_COMPLETE then
            MessageBox(GetMapDataPath().."BeginDemonAttack.txt");
        end;
    end;
    if GetDate(ABSOLUTE_DAY) == 114 then
        MessageBox(GetMapDataPath().."DemonAttack.txt");
        sleep(5);
        BlockGame();
        MoveCamera(172, 159, 0, 30, 1.2, 0, 1, 1, 0);
        UnblockGame();
        sleep(35);
        if attackCount == a3_ATTACK06 then
            UpdateArmyForHero(Baghar, 2); -- +100% for army
            if openFogForSiege == 1 then
                openFog(2);
                openFogForSiege = 2;
            end;
            AttackToTown(Baghar, TOWN_SYLVAN);
            sleep(7);
            ResetCamera(TOWN_SYLVAN);
        end;
    end;

    if sulfurTradeData == enable then
        dateTrading1 = dateTrading1+1;
        local bonusCost = 1+sulfurTradeSkill;
        local dayCost = 10-sulfurTradeSkill;
        local timeCost = 70-dateTrading1*7;
        if dateTrading1 <= 7 then
            local sulfur = GetPlayerResource(1, SULFUR);
            if sulfur > timeCost then
                SetPlayerResource(1, SULFUR, GetPlayerResource(1, SULFUR)-dayCost);
                SetPlayerResource(1, WOOD, GetPlayerResource(1, WOOD)+bonusCost); -- дерево
                SetPlayerResource(1, ORE, GetPlayerResource(1, ORE)+bonusCost); -- руда
                SetPlayerResource(1, CRYSTAL, GetPlayerResource(1, CRYSTAL)+bonusCost); -- кристаллы
                SetPlayerResource(1, MERCURY, GetPlayerResource(1, MERCURY)+bonusCost); -- ртуть
                SetPlayerResource(1, GEM, GetPlayerResource(1, GEM)+bonusCost); -- самоцветы
            elseif sulfur < timeCost then
                MessageBox(GetMapDataPath().."trading/sulfurContractDestroyed.txt");
                trade1Title = GetMapDataPath().."trading/contractSulfurTitle.txt";
                dateTrading1 = 0;
                sulfurTradeData = disable;
                if sulfurTradeSkill > 0 then
                    sulfurTradeSkill = sulfurTradeSkill - 1;
                end;
            end;
        else
            dateTrading1 = 0;
            sulfurTradeData = disable;
            MessageBox(GetMapDataPath().."trading/sulfurContractCompleted.txt");
            trade1Title = GetMapDataPath().."trading/contractSulfurTitle.txt";
            if sulfurTradeSkill < 9 then
                sulfurTradeSkill = sulfurTradeSkill + 1;
                oreTradeSkill = oreTradeSkill + 1;
                woodTradeSkill = woodTradeSkill + 1;
            end;
        end;
    end;
    if oreTradeData == enable then
        dateTrading2 = dateTrading2+1;
        local bonusCost = 1+oreTradeSkill;
        local dayCost = 10-oreTradeSkill;
        local timeCost = 70-dateTrading2*7;
        if dateTrading2 <= 7 then
            local ore = GetPlayerResource(1, ORE);
            if ore > timeCost then
                SetPlayerResource(1, ORE, GetPlayerResource(1, ORE)-dayCost);
                SetPlayerResource(1, WOOD, GetPlayerResource(1, WOOD)+bonusCost); -- дерево
                SetPlayerResource(1, SULFUR, GetPlayerResource(1, SULFUR)+bonusCost); -- сера
                SetPlayerResource(1, CRYSTAL, GetPlayerResource(1, CRYSTAL)+bonusCost); -- кристаллы
                SetPlayerResource(1, MERCURY, GetPlayerResource(1, MERCURY)+bonusCost); -- ртуть
                SetPlayerResource(1, GEM, GetPlayerResource(1, GEM)+bonusCost); -- самоцветы
            elseif ore < timeCost then
                MessageBox(GetMapDataPath().."trading/oreContractDestroyed.txt");
                trade2Title = GetMapDataPath().."trading/contractOreTitle.txt";
                dateTrading2 = 0;
                oreTradeData = disable;
                if oreTradeSkill > 0 then
                    oreTradeSkill = oreTradeSkill - 1;
                end;
            end;
        else
            dateTrading2 = 0;
            oreTradeData = disable;
            MessageBox(GetMapDataPath().."trading/oreContractCompleted.txt");
            trade2Title = GetMapDataPath().."trading/contractOreTitle.txt";
            if oreTradeSkill < 9 then
                oreTradeSkill = oreTradeSkill + 1;
                woodTradeSkill = woodTradeSkill + 1;
                sulfurTradeSkill = sulfurTradeSkill + 1;
            end;
        end;
    end;
    if woodTradeData == enable then
        dateTrading3 = dateTrading3+1;
        local bonusCost = 1+woodTradeSkill;
        local dayCost = 10-woodTradeSkill;
        local timeCost = 70-dateTrading3*7;
        if dateTrading3 <= 7 then
            local wood = GetPlayerResource(1, WOOD);
            if wood > timeCost then
                SetPlayerResource(1, WOOD, GetPlayerResource(1, WOOD)-dayCost);
                SetPlayerResource(1, ORE, GetPlayerResource(1, ORE)+bonusCost); -- руда
                SetPlayerResource(1, SULFUR, GetPlayerResource(1, SULFUR)+bonusCost); -- сера
                SetPlayerResource(1, CRYSTAL, GetPlayerResource(1, CRYSTAL)+bonusCost); -- кристаллы
                SetPlayerResource(1, MERCURY, GetPlayerResource(1, MERCURY)+bonusCost); -- ртуть
                SetPlayerResource(1, GEM, GetPlayerResource(1, GEM)+bonusCost); -- самоцветы
            elseif wood < timeCost then
                MessageBox(GetMapDataPath().."trading/woodContractDestroyed.txt");
                trade3Title = GetMapDataPath().."trading/contractWoodTitle.txt";
                dateTrading3 = 0;
                woodTradeData = disable;
                if woodTradeSkill > 0 then
                    woodTradeSkill = woodTradeSkill - 1;
                end;
            end;
        else
            dateTrading3 = 0;
            woodTradeData = disable;
            MessageBox(GetMapDataPath().."trading/woodContractCompleted.txt");
            trade3Title = GetMapDataPath().."trading/contractWoodTitle.txt";
            if woodTradeSkill < 9 then
                woodTradeSkill = woodTradeSkill + 1;
                sulfurTradeSkill = sulfurTradeSkill + 1;
                oreTradeSkill = oreTradeSkill + 1;
            end;
        end;
    end;
end;

function SiegeStart01(hero)
    demon = GetObjectOwner(hero);
    if demon == PLAYER_2 then
        sleep(3);
        MakeHeroInteractWithObject(hero, TOWN_DWARVEN);
    end;
end;

function SiegeStart02(hero)
    demon = GetObjectOwner(hero);
    if demon == PLAYER_2 then
        sleep(3);
        MakeHeroInteractWithObject(hero, TOWN_HEAVEN);
    end;
end;

function SiegeStart03(hero)
    demon = GetObjectOwner(hero);
    if demon == PLAYER_2 then
        sleep(3);
        MakeHeroInteractWithObject(hero, TOWN_SYLVAN);
    end;
end;

function informationClosedRegion()
    MessageBox(GetMapDataPath().."ClosedRegion.txt");
    Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rInfoClosed", nil);
end;

function transitUp(hero)
    demon = GetObjectOwner(hero);
    if demon == PLAYER_2 then
        if attackMove == 1 then
            MoveHeroAndReachPoint(hero, 144, 145, 0);
        elseif attackMove == 2 then
            MoveHeroAndReachPoint(hero, 48, 88, 0);
        elseif attackMove == 3 then
            MoveHeroAndReachPoint(hero, 142, 56, 0);
        end;
    end;
end;

function RewardForSiege(heroLooser, heroWinner)
    -- 1 город
    if (heroLooser == Oddrema) and (heroWinner == Olifand) then
        GiveArtefact(Olifand, 85);
        MessageBox(GetMapDataPath().."EfionBegin.txt");
        attackCount = a1_ATTACK02;
    end;
    if (heroLooser == Efion) and (heroWinner == Olifand) then
        GiveArtefact(Olifand, 51);
        MessageBox(GetMapDataPath().."MarderBegin.txt");
        attackCount = a1_ATTACK03;
    end;
    if (heroLooser == Marder) and (heroWinner == Olifand) then
        GiveArtefact(Olifand, 50);
        MessageBox(GetMapDataPath().."OrlandoBegin.txt");
        attackCount = a1_ATTACK04;
    end;
    if (heroLooser == Orlando) and (heroWinner == Olifand) then
        GiveArtefact(Olifand, 49);
        MessageBox(GetMapDataPath().."DelebBegin.txt");
        attackCount = a1_ATTACK05;
    end;
    if (heroLooser == Deleb) and (heroWinner == Olifand) then
        GiveArtefact(Olifand, 48);
        MessageBox(GetMapDataPath().."BagharBegin.txt");
        attackCount = a1_ATTACK06;
    end;
    if (heroLooser == Baghar) and (heroWinner == Olifand) then
        for i = 1, length(DragonSet) do
            GiveArtefact(Olifand, DragonSet[i]);
            sleep(5);
        end;
        sleep(5);
        attackCount = a_ATTACK_COMPLETE;
        sleep(5);
        SetObjectiveState("SecondQuest", OBJECTIVE_COMPLETED);
        sleep(1);
        SetObjectiveState("MainQuest", OBJECTIVE_ACTIVE);
        startThread(CallTeleports);
    end;
    
    -- 2 город
    if (heroLooser == Oddrema) and (heroWinner == Boris) then
        MessageBox(GetMapDataPath().."EfionBegin.txt");
        attackCount = a2_ATTACK02;
    end;
    if (heroLooser == Efion) and (heroWinner == Boris) then
        MessageBox(GetMapDataPath().."MarderBegin.txt");
        attackCount = a2_ATTACK03;
    end;
    if (heroLooser == Marder) and (heroWinner == Boris) then
        GiveArtefact(Boris, 16);
        MessageBox(GetMapDataPath().."OrlandoBegin.txt");
        attackCount = a2_ATTACK04;
    end;
    if (heroLooser == Orlando) and (heroWinner == Boris) then
        GiveArtefact(Boris, 31);
        MessageBox(GetMapDataPath().."DelebBegin.txt");
        attackCount = a2_ATTACK05;
    end;
    if (heroLooser == Deleb) and (heroWinner == Boris) then
        GiveArtefact(Boris, 11);
        MessageBox(GetMapDataPath().."BagharBegin.txt");
        attackCount = a2_ATTACK06;
    end;
    if (heroLooser == Baghar) and (heroWinner == Boris) then
        for i = 1, length(DragonSet) do
            GiveArtefact(Boris, DragonSet[i]);
        end;
        sleep(5);
        attackCount = a_ATTACK_COMPLETE;
        sleep(5);
        SetObjectiveState("SecondQuest", OBJECTIVE_COMPLETED);
        sleep(1);
        SetObjectiveState("MainQuest", OBJECTIVE_ACTIVE);
        startThread(CallTeleports);
    end;
    
    -- 3 город
    if (heroLooser == Oddrema) and (heroWinner == Silein) then
        MessageBox(GetMapDataPath().."EfionBegin.txt");
        attackCount = a3_ATTACK02;
    end;
    if (heroLooser == Efion) and (heroWinner == Silein) then
        MessageBox(GetMapDataPath().."MarderBegin.txt");
        attackCount = a3_ATTACK03;
    end;
    if (heroLooser == Marder) and (heroWinner == Silein) then
        MessageBox(GetMapDataPath().."OrlandoBegin.txt");
        attackCount = a3_ATTACK04;
    end;
    if (heroLooser == Orlando) and (heroWinner == Silein) then
        GiveArtefact(Silein, 4);
        MessageBox(GetMapDataPath().."DelebBegin.txt");
        attackCount = a3_ATTACK05;
    end;
    if (heroLooser == Deleb) and (heroWinner == Silein) then
        GiveArtefact(Silein, 95);
        MessageBox(GetMapDataPath().."BagharBegin.txt");
        attackCount = a3_ATTACK06;
    end;
    if (heroLooser == Baghar) and (heroWinner == Silein) then
        for i = 1, length(DragonSet) do
            GiveArtefact(Silein, DragonSet[i]);
        end;
        sleep(5);
        attackCount = a_ATTACK_COMPLETE;
        sleep(5);
        SetObjectiveState("SecondQuest", OBJECTIVE_COMPLETED);
        sleep(1);
        SetObjectiveState("MainQuest", OBJECTIVE_ACTIVE);
        startThread(CallTeleports);
    end;
    if (heroLooser == Baghar) and (heroWinner == Atlant) then
        RazedTownAndPlayEffects(TOWN_SYLVAN, WALL_SYLVAN);
        sleep(5);
        SetObjectiveState("SecondQuest", OBJECTIVE_COMPLETED);
        sleep(5);
        SetObjectiveState("MainQuest", OBJECTIVE_ACTIVE);
    end;

    if (heroLooser == Ahmad) and (heroWinner == Atlant) then
        SetObjectiveState("MainQuest", OBJECTIVE_COMPLETED);
        Win();
    elseif (heroLooser == Ahmad) and (heroWinner == Olifand) then
        SetObjectiveState("MainQuest", OBJECTIVE_COMPLETED);
        Win();
    elseif (heroLooser == Ahmad) and (heroWinner == Boris) then
        SetObjectiveState("MainQuest", OBJECTIVE_COMPLETED);
        Win();
    elseif (heroLooser == Ahmad) and (heroWinner == Silein) then
        SetObjectiveState("MainQuest", OBJECTIVE_COMPLETED);
        Win();
    end;

    print("Player2 lost hero: "..heroLooser);
end;

function ResetForSiege(heroLooser, heroWinner)
    SetObjectPosition(heroWinner, 36, 141, 1);
    -- 1 город
    if (heroLooser == Olifand) and (heroWinner == Oddrema) then
        RazedTownAndPlayEffects(TOWN_DWARVEN, WALL_DWARVEN);
        Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rSiege01", nil);
        attackCount = a2_ATTACK01;
    elseif (heroLooser == Olifand) and (heroWinner == Efion) then
        RazedTownAndPlayEffects(TOWN_DWARVEN, WALL_DWARVEN);
        Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rSiege01", nil);
        attackCount = a2_ATTACK02;
    elseif (heroLooser == Olifand) and (heroWinner == Marder) then
        RazedTownAndPlayEffects(TOWN_DWARVEN, WALL_DWARVEN);
        Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rSiege01", nil);
        attackCount = a2_ATTACK03;
    elseif (heroLooser == Olifand) and (heroWinner == Orlando) then
        RazedTownAndPlayEffects(TOWN_DWARVEN, WALL_DWARVEN);
        Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rSiege01", nil);
        attackCount = a2_ATTACK04;
    elseif (heroLooser == Olifand) and (heroWinner == Deleb) then
        RazedTownAndPlayEffects(TOWN_DWARVEN, WALL_DWARVEN);
        Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rSiege01", nil);
        attackCount = a2_ATTACK05;
    elseif (heroLooser == Olifand) and (heroWinner == Baghar) then
        RazedTownAndPlayEffects(TOWN_DWARVEN, WALL_DWARVEN);
        Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rSiege01", nil);
        attackCount = a2_ATTACK06;
    end;
    
    -- 2 город
    if (heroLooser == Boris) and (heroWinner == Oddrema) then
        RazedTownAndPlayEffects(TOWN_HEAVEN, WALL_HEAVEN);
        Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rSiege02", nil);
        attackCount = a3_ATTACK01;
    elseif (heroLooser == Boris) and (heroWinner == Efion) then
        RazedTownAndPlayEffects(TOWN_HEAVEN, WALL_HEAVEN);
        Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rSiege02", nil);
        attackCount = a3_ATTACK02;
    elseif (heroLooser == Boris) and (heroWinner == Marder) then
        RazedTownAndPlayEffects(TOWN_HEAVEN, WALL_HEAVEN);
        Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rSiege02", nil);
        attackCount = a3_ATTACK03;
    elseif (heroLooser == Boris) and (heroWinner == Orlando) then
        RazedTownAndPlayEffects(TOWN_HEAVEN, WALL_HEAVEN);
        Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rSiege02", nil);
        attackCount = a3_ATTACK04;
    elseif (heroLooser == Boris) and (heroWinner == Deleb) then
        RazedTownAndPlayEffects(TOWN_HEAVEN, WALL_HEAVEN);
        Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rSiege02", nil);
        attackCount = a3_ATTACK05;
    elseif (heroLooser == Boris) and (heroWinner == Baghar) then
        RazedTownAndPlayEffects(TOWN_HEAVEN, WALL_HEAVEN);
        Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rSiege02", nil);
        attackCount = a3_ATTACK06;
    end;
    
    -- 3 город
    if (heroLooser == Silein) and (heroWinner == Oddrema) then
        RazedTownAndPlayEffects(TOWN_SYLVAN, WALL_SYLVAN);
        attackCount = 0;
        Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rSiege03", nil);
        sleep(5);
        Loose();
    elseif (heroLooser == Silein) and (heroWinner == Efion) then
        RazedTownAndPlayEffects(TOWN_SYLVAN, WALL_SYLVAN);
        attackCount = 0;
        Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rSiege03", nil);
        sleep(5);
        Loose();
    elseif (heroLooser == Silein) and (heroWinner == Marder) then
        RazedTownAndPlayEffects(TOWN_SYLVAN, WALL_SYLVAN);
        attackCount = 0;
        Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rSiege03", nil);
        sleep(5);
        Loose();
    elseif (heroLooser == Silein) and (heroWinner == Orlando) then
        RazedTownAndPlayEffects(TOWN_SYLVAN, WALL_SYLVAN);
        attackCount = 0;
        Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rSiege03", nil);
        sleep(5);
        Loose();
    elseif (heroLooser == Silein) and (heroWinner == Deleb) then
        RazedTownAndPlayEffects(TOWN_SYLVAN, WALL_SYLVAN);
        attackCount = 0;
        Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rSiege03", nil);
        sleep(5);
        Loose();
    elseif (heroLooser == Silein) and (heroWinner == Baghar) then
        SetObjectPosition(Baghar, 141, 41, 0);
        UpdateArmyForHero(Baghar, 2.5); -- +150% for army
        ResetHeroCombatScript(Baghar);
        EnableHeroAI(Baghar, true);
        SetObjectOwner(TOWN_SYLVAN, PLAYER_2);
        attackCount = 0;
        Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rSiege03", nil);
        SetObjectiveState("SecondQuest", OBJECTIVE_COMPLETED);
        startThread(AtlantCall);
        sleep(5);
        SetObjectiveState("MainQuest", OBJECTIVE_ACTIVED);
    end;
    
    if (heroLooser == Olifand) and (heroWinner == Ahmad) then
        startThread(AtlantCall);
    elseif (heroLooser == Boris) and (heroWinner == Ahmad) then
        startThread(AtlantCall);
    elseif (heroLooser == Silein) and (heroWinner == Ahmad) then
        startThread(AtlantCall);
    end;
    
    if (heroLooser == Atlant) and (heroWinner == Ahmad) then
        Loose();
    end;
    if (heroLooser == Atlant) and (heroWinner == Baghar) then
        Loose();
    end;
end;

-- призыв на помощь атланта --
function AtlantCall()
    SetObjectPosition(Atlant, 82, 14, 0);
    SetObjectOwner(Atlant, PLAYER_1);
    sleep(5);
    BlockGame();
    MoveCamera(82, 14, 0, 30, 1.2, 0, 1, 1, 1);
    UnblockGame();
    SetHeroCombatScript(Ahmad, "Script:Maps/SingleMissions/OnLastPoint/InfernoCombat.(Script).xdb#xpointer(/Script)");
    sleep(30);
    MessageBox(GetMapDataPath().."AtlantBegin.txt");
end;

-- эмул¤ци¤ работы порталов --
function TeleportTikay(hero, obj)
    if GetObjectiveState("MainQuest", OBJECTIVE_ACTIVE) then
        SetObjectPosition(hero, 157, 154, 0);
    end;
end;

function TeleportToHome(hero, obj)
    if hero == Olifand then
        SetObjectPosition(hero, 128, 146, 0);
    elseif hero == Boris then
        SetObjectPosition(hero, 64, 87, 0);
    elseif hero == Silein then
        SetObjectPosition(hero, 140, 40, 0);
    end;
end;

-- уничтожает лес и ставит порталы
function CallTeleports()
    BlockGame();
    MoveCamera(110, 172, 0, 30, 1.2, 0, 1, 1, 0);
    OpenCircleFog(110, 172, 0, 10, player);
    sleep(20);
    Play2DSound(Sound_destroyed);
    PlayVisualEffect(Effect_destroyed01, "","destTree1", 108, 174, 0, 0);
    PlayVisualEffect(Effect_destroyed02, "", "destTree2", 108, 174, 0, 0);
    RemoveObject("DwarvenTree");
    sleep(25);
    CreateStatic("PortalEnter1", PortalEntrance, 108, 174, 0, 0, -1, 100);
    sleep(10);
    MoveCamera(121, 63, 0, 30, 1.2, 0, 1, 1, 0);
    OpenCircleFog(121, 63, 0, 10, player);
    sleep(30);
    Play2DSound(Sound_destroyed);
    PlayVisualEffect(Effect_destroyed01, "","destTree3", 121, 63, 0, 0);
    PlayVisualEffect(Effect_destroyed02, "", "destTree4", 121, 63, 0, 0);
    RemoveObject("HeavenTree");
    sleep(25);
    CreateStatic("PortalEnter2", PortalEntrance, 121, 61, 0, 3.14, -1, 100);
    sleep(10);
    MoveCamera(147, 27, 0, 30, 1.2, 0, 1, 1, 0);
    OpenCircleFog(147, 27, 0, 10, player);
    sleep(30);
    Play2DSound(Sound_destroyed);
    PlayVisualEffect(Effect_destroyed01, "","destTree5", 147, 27, 0, 0);
    PlayVisualEffect(Effect_destroyed02, "", "destTree6", 147, 27, 0, 0);
    RemoveObject("ElfTree");
    sleep(25);
    StopVisualEffects("destTree");
    CreateStatic("PortalEnter3", PortalEntrance, 147, 27, 0, 0, -1, 100);
    sleep(15);
    MoveCamera(157, 156, 0, 30, 1.2, 0, 1, 1, 0);
    OpenCircleFog(157, 156, 0, 10, player);
    sleep(20);
    CreateStatic("PortalExit1", PortalExit, 157, 154, 0, 0, -1, 100);
    CreateStatic("PortalExit2", PortalExit, 156, 140, 0, 3.14, -1, 100);
    UnblockGame();
    --portalEnter = {"rTransport01", "rTransport02", "rTransport03"};
    Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rTransport01", "TeleportTikay");
    Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rTransport02", "TeleportTikay");
    Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rTransport03", "TeleportTikay");
    Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rTransport04", "TeleportToHome");
end;

function resetSiegeAndRazedTown(oldOwner, newOwner, heroname, object)
    if newOwner == PLAYER_2 then
        if object == TOWN_DWARVEN then
            ResetForSiege(Olifand, heroname);
        elseif object == TOWN_HEAVEN then
            ResetForSiege(Boris, heroname);
        elseif object == TOWN_SYLVAN then
            ResetForSiege(Silein, heroname);
        end;
    end;
end;

-- крестьяшки
function dwellingBattleHeaven01(hero, obj)
    heroName = hero;
    objectName = obj;
    if IsAIPlayer(GetObjectOwner(heroName)) == HUMAN then
        QuestionBoxForPlayers(GetPlayerFilter(GetObjectOwner(heroName)), GetMapDataPath().."dwelling1_search.txt", "dwellingBattleHeaven01_start");
    end;
end;

function dwellingBattleHeaven01_start()
    local mod = GetDate(MONTH);
    local npc1 = 0;
    local npc2 = 0;
    local npc3 = 0;
    local rnd = random(5);
    if rnd == 0 then
        npc1 = 1;
        npc2 = 2;
    elseif rnd == 1 then
        npc1 = 1;
        npc2 = 106;
    elseif rnd == 2 then
        npc1 = 1;
        npc2 = 2;
        npc3 = 106;
        npc4 = 1;
    else
        npc1 = 1;
        npc2 = 1;
    end;
    if rnd == 2 then
        StartCombat(heroName, nil, 4, npc1, (25+Diff+random(3))*mod, npc2, (25+Diff+random(3))*mod, npc3, (25+Diff+random(3))*mod, npc4, (25+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/grassCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    else
        StartCombat(heroName, nil, 2, npc1, (22+Diff+random(3))*mod, npc2, (22+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/grassCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    end;
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingHeaven_01", nil);
        SetObjectEnabled("oDwellingHeaven_01", true);
        SetObjectOwner("oDwellingHeaven_01", 1); 
        sleep(1);
        MakeHeroInteractWithObject(heroName, "oDwellingHeaven_01");
    end;
end;

-- лучники
function dwellingBattleHeaven02(hero, obj)
    heroName = hero;
    objectName = obj;
    if IsAIPlayer(GetObjectOwner(heroName)) == HUMAN then
        QuestionBoxForPlayers(GetPlayerFilter(GetObjectOwner(heroName)), GetMapDataPath().."dwelling1_search.txt", "dwellingBattleHeaven02_start");
    end;
end;

function dwellingBattleHeaven02_start()
    local mod = GetDate(MONTH);
    local npc1 = 0;
    local npc2 = 0;
    local npc3 = 0;
    local rnd = random(5);
    if rnd == 0 then
        npc1 = 3;
        npc2 = 4;
    elseif rnd == 1 then
        npc1 = 3;
        npc2 = 107;
    elseif rnd == 2 then
        npc1 = 3;
        npc2 = 4;
        npc3 = 107;
        npc4 = 3;
    else
        npc1 = 3;
        npc2 = 3;
    end;
    if rnd == 2 then
        StartCombat(heroName, nil, 4, npc1, (20+Diff+random(3))*mod, npc2, (20+Diff+random(3))*mod, npc3, (20+Diff+random(3))*mod, npc4, (20+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/grassCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    else
        StartCombat(heroName, nil, 2, npc1, (19+Diff+random(3))*mod, npc2, (19+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/grassCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    end;
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingHeaven_02", nil);
        SetObjectEnabled("oDwellingHeaven_02", true);
        SetObjectOwner("oDwellingHeaven_02", 1); 
        sleep(1);
        MakeHeroInteractWithObject(heroName, "oDwellingHeaven_02");
    end;
end;

-- мечники
function dwellingBattleHeaven03(hero, obj)
    heroName = hero;
    objectName = obj;
    if IsAIPlayer(GetObjectOwner(heroName)) == HUMAN then
        QuestionBoxForPlayers(GetPlayerFilter(GetObjectOwner(heroName)), GetMapDataPath().."dwelling1_search.txt", "dwellingBattleHeaven03_start");
    end;
end;

function dwellingBattleHeaven03_start()
    local mod = GetDate(MONTH);
    local npc1 = 0;
    local npc2 = 0;
    local npc3 = 0;
    local rnd = random(5);
    if rnd == 0 then
        npc1 = 5;
        npc2 = 6;
    elseif rnd == 1 then
        npc1 = 5;
        npc2 = 108;
    elseif rnd == 2 then
        npc1 = 5;
        npc2 = 6;
        npc3 = 108;
        npc4 = 5;
    else
        npc1 = 5;
        npc2 = 5;
    end;
    if rnd == 2 then
        StartCombat(heroName, nil, 4, npc1, (18+Diff+random(3))*mod, npc2, (18+Diff+random(3))*mod, npc3, (18+Diff+random(3))*mod, npc4, (18+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/grassCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    else
        StartCombat(heroName, nil, 2, npc1, (17+Diff+random(3))*mod, npc2, (17+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/grassCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    end;
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingHeaven_03", nil);
        SetObjectEnabled("oDwellingHeaven_03", true);
        SetObjectOwner("oDwellingHeaven_03", 1); 
        sleep(1);
        MakeHeroInteractWithObject(heroName, "oDwellingHeaven_03");
    end;
end;

-- грифоны
function dwellingBattleHeaven04(hero, obj)
    heroName = hero;
    objectName = obj;
    if IsAIPlayer(GetObjectOwner(heroName)) == HUMAN then
        QuestionBoxForPlayers(GetPlayerFilter(GetObjectOwner(heroName)), GetMapDataPath().."dwelling1_search.txt", "dwellingBattleHeaven04_start");
    end;
end;

function dwellingBattleHeaven04_start()
    local mod = GetDate(MONTH);
    local npc1 = 0;
    local npc2 = 0;
    local npc3 = 0;
    local rnd = random(5);
    if rnd == 0 then
        npc1 = 7;
        npc2 = 8;
    elseif rnd == 1 then
        npc1 = 7;
        npc2 = 109;
    elseif rnd == 2 then
        npc1 = 7;
        npc2 = 8;
        npc3 = 109;
        npc4 = 7;
    else
        npc1 = 7;
        npc2 = 7;
    end;
    if rnd == 2 then
        StartCombat(heroName, nil, 4, npc1, (12+Diff+random(3))*mod, npc2, (12+Diff+random(3))*mod, npc3, (12+Diff+random(3))*mod, npc4, (12+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/grassCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    else
        StartCombat(heroName, nil, 2, npc1, (11+Diff+random(3))*mod, npc2, (11+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/grassCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    end;
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingHeaven_04", nil);
        SetObjectEnabled("oDwellingHeaven_04", true);
        SetObjectOwner("oDwellingHeaven_04", 1); 
        sleep(1);
        MakeHeroInteractWithObject(heroName, "oDwellingHeaven_04");
    end;
end;

-- монахи и рыцари
function dwellingBattleHeaven05(hero, obj)
    heroName = hero;
    objectName = obj;
    if IsAIPlayer(GetObjectOwner(heroName)) == HUMAN then
        QuestionBoxForPlayers(GetPlayerFilter(GetObjectOwner(heroName)), GetMapDataPath().."dwelling1_search.txt", "dwellingBattleHeaven05_start");
    end;
end;

function dwellingBattleHeaven05_start()
    local mod = GetDate(MONTH);
    local npc1 = 0;
    local npc2 = 0;
    local npc3 = 0;
    local rnd = random(5);
    if rnd == 0 then
        npc1 = 10;
        npc2 = 12;
    elseif rnd == 1 then
        npc1 = 110;
        npc2 = 111;
    elseif rnd == 2 then
        npc1 = 9;
        npc2 = 110;
        npc3 = 111;
        npc4 = 11;
    else
        npc1 = 9;
        npc2 = 11;
    end;
    if rnd == 2 then
        StartCombat(heroName, nil, 4, npc1, (9+Diff+random(3))*mod, npc2, (9+Diff+random(3))*mod, npc3, (6+Diff+random(3))*mod, npc4, (6+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/grassCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    else
        StartCombat(heroName, nil, 2, npc1, (8+Diff+random(3))*mod, npc2, (6+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/grassCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    end;
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingHeaven_05", nil);
        SetObjectEnabled("oDwellingHeaven_05", true);
        SetObjectOwner("oDwellingHeaven_05", 1); 
        sleep(1);
        MakeHeroInteractWithObject(heroName, "oDwellingHeaven_05");
    end;
end;

-- ангелы
function dwellingBattleHeaven06(hero, obj)
    heroName = hero;
    objectName = obj;
    if IsAIPlayer(GetObjectOwner(heroName)) == HUMAN then
        QuestionBoxForPlayers(GetPlayerFilter(GetObjectOwner(heroName)), GetMapDataPath().."dwelling1_search.txt", "dwellingBattleHeaven06_start");
    end;
end;

function dwellingBattleHeaven06_start()
    local mod = GetDate(MONTH);
    local npc1 = 0;
    local npc2 = 0;
    local npc3 = 0;
    local rnd = random(5);
    if rnd == 0 then
        npc1 = 13;
        npc2 = 14;
    elseif rnd == 1 then
        npc1 = 13;
        npc2 = 112;
    elseif rnd == 2 then
        npc1 = 13;
        npc2 = 14;
        npc3 = 112;
        npc4 = 13;
    else
        npc1 = 13;
        npc2 = 13;
    end;
    if rnd == 2 then
        StartCombat(heroName, nil, 4, npc1, (2+Diff+random(3))*mod, npc2, (1+Diff+random(3))*mod, npc3, (1+Diff+random(3))*mod, npc4, (2+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/grassCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    else
        StartCombat(heroName, nil, 2, npc1, (2+Diff+random(3))*mod, npc2, (2+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/grassCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    end;
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingHeaven_06", nil);
        SetObjectEnabled("oDwellingHeaven_06", true);
        SetObjectOwner("oDwellingHeaven_06", 1); 
        sleep(1);
        MakeHeroInteractWithObject(heroName, "oDwellingHeaven_06");
    end;
end;

-- алкаши
function dwellingBattleDwarwen01(hero, obj)
    heroName = hero;
    objectName = obj;
    if IsAIPlayer(GetObjectOwner(heroName)) == HUMAN then
        QuestionBoxForPlayers(GetPlayerFilter(GetObjectOwner(heroName)), GetMapDataPath().."dwelling1_search.txt", "dwellingBattleDwarwen01_start");
    end;
end;

function dwellingBattleDwarwen01_start()
    local mod = GetDate(MONTH);
    local npc1 = 0;
    local npc2 = 0;
    local npc3 = 0;
    local rnd = random(5);
    if rnd == 0 then
        npc1 = 92;
        npc2 = 93;
    elseif rnd == 1 then
        npc1 = 92;
        npc2 = 166;
    elseif rnd == 2 then
        npc1 = 92;
        npc2 = 93;
        npc3 = 166;
        npc4 = 92;
    else
        npc1 = 92;
        npc2 = 92;
    end;
    if rnd == 2 then
        StartCombat(heroName, nil, 4, npc1, (35+Diff+random(3))*mod, npc2, (35+Diff+random(3))*mod, npc3, (35+Diff+random(3))*mod, npc4, (35+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/snowCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    else
        StartCombat(heroName, nil, 2, npc1, (32+Diff+random(3))*mod, npc2, (32+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/snowCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    end;
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingDwarwen_01", nil);
        SetObjectEnabled("oDwellingDwarwen_01", true);
        SetObjectOwner("oDwellingDwarwen_01", 1); 
        sleep(1);
        MakeHeroInteractWithObject(heroName, "oDwellingDwarwen_01");
    end;
end;

-- гарпунеры
function dwellingBattleDwarwen02(hero, obj)
    heroName = hero;
    objectName = obj;
    if IsAIPlayer(GetObjectOwner(heroName)) == HUMAN then
        QuestionBoxForPlayers(GetPlayerFilter(GetObjectOwner(heroName)), GetMapDataPath().."dwelling1_search.txt", "dwellingBattleDwarwen02_start");
    end;
end;

function dwellingBattleDwarwen02_start()
    local mod = GetDate(MONTH);
    local npc1 = 0;
    local npc2 = 0;
    local npc3 = 0;
    local rnd = random(5);
    if rnd == 0 then
        npc1 = 94;
        npc2 = 95;
    elseif rnd == 1 then
        npc1 = 94;
        npc2 = 167;
    elseif rnd == 2 then
        npc1 = 94;
        npc2 = 95;
        npc3 = 167;
        npc4 = 94;
    else
        npc1 = 94;
        npc2 = 94;
    end;
    if rnd == 2 then
        StartCombat(heroName, nil, 4, npc1, (20+Diff+random(3))*mod, npc2, (20+Diff+random(3))*mod, npc3, (20+Diff+random(3))*mod, npc4, (20+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/snowCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    else
        StartCombat(heroName, nil, 2, npc1, (19+Diff+random(3))*mod, npc2, (19+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/snowCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    end;
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingDwarwen_02", nil);
        SetObjectEnabled("oDwellingDwarwen_02", true);
        SetObjectOwner("oDwellingDwarwen_02", 1); 
        sleep(1);
        MakeHeroInteractWithObject(heroName, "oDwellingDwarwen_02");
    end;
end;

-- мишки
function dwellingBattleDwarwen03(hero, obj)
    heroName = hero;
    objectName = obj;
    if IsAIPlayer(GetObjectOwner(heroName)) == HUMAN then
        QuestionBoxForPlayers(GetPlayerFilter(GetObjectOwner(heroName)), GetMapDataPath().."dwelling1_search.txt", "dwellingBattleDwarwen03_start");
    end;
end;

function dwellingBattleDwarwen03_start()
    local mod = GetDate(MONTH);
    local npc1 = 0;
    local npc2 = 0;
    local npc3 = 0;
    local rnd = random(5);
    if rnd == 0 then
        npc1 = 96;
        npc2 = 97;
    elseif rnd == 1 then
        npc1 = 96;
        npc2 = 168;
    elseif rnd == 2 then
        npc1 = 96;
        npc2 = 97;
        npc3 = 168;
        npc4 = 96;
    else
        npc1 = 96;
        npc2 = 96;
    end;
    if rnd == 2 then
        StartCombat(heroName, nil, 4, npc1, (18+Diff+random(3))*mod, npc2, (18+Diff+random(3))*mod, npc3, (18+Diff+random(3))*mod, npc4, (18+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/snowCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    else
        StartCombat(heroName, nil, 2, npc1, (17+Diff+random(3))*mod, npc2, (17+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/snowCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    end;
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingDwarwen_03", nil);
        SetObjectEnabled("oDwellingDwarwen_03", true);
        SetObjectOwner("oDwellingDwarwen_03", 1); 
        sleep(1);
        MakeHeroInteractWithObject(heroName, "oDwellingDwarwen_03");
    end;
end;

-- костоломы и жрецы
function dwellingBattleDwarwen04(hero, obj)
    heroName = hero;
    objectName = obj;
    if IsAIPlayer(GetObjectOwner(heroName)) == HUMAN then
        QuestionBoxForPlayers(GetPlayerFilter(GetObjectOwner(heroName)), GetMapDataPath().."dwelling1_search.txt", "dwellingBattleDwarwen04_start");
    end;
end;

function dwellingBattleDwarwen04_start()
    local mod = GetDate(MONTH);
    local npc1 = 0;
    local npc2 = 0;
    local npc3 = 0;
    local rnd = random(5);
    if rnd == 0 then
        npc1 = 99;
        npc2 = 101;
    elseif rnd == 1 then
        npc1 = 169;
        npc2 = 170;
    elseif rnd == 2 then
        npc1 = 99;
        npc2 = 169;
        npc3 = 170;
        npc4 = 101;
    else
        npc1 = 98;
        npc2 = 100;
    end;
    if rnd == 2 then
        StartCombat(heroName, nil, 4, npc1, (12+Diff+random(3))*mod, npc2, (12+Diff+random(3))*mod, npc3, (12+Diff+random(3))*mod, npc4, (12+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/snowCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    else
        StartCombat(heroName, nil, 2, npc1, (11+Diff+random(3))*mod, npc2, (12+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/snowCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    end;
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingDwarwen_04", nil);
        SetObjectEnabled("oDwellingDwarwen_04", true);
        SetObjectOwner("oDwellingDwarwen_04", 1); 
        sleep(1);
        MakeHeroInteractWithObject(heroName, "oDwellingDwarwen_04");
    end;
end;

-- таны
function dwellingBattleDwarwen05(hero, obj)
    heroName = hero;
    objectName = obj;
    if IsAIPlayer(GetObjectOwner(heroName)) == HUMAN then
        QuestionBoxForPlayers(GetPlayerFilter(GetObjectOwner(heroName)), GetMapDataPath().."dwelling1_search.txt", "dwellingBattleDwarwen05_start");
    end;
end;

function dwellingBattleDwarwen05_start()
    local mod = GetDate(MONTH);
    local npc1 = 0;
    local npc2 = 0;
    local npc3 = 0;
    local rnd = random(5);
    if rnd == 0 then
        npc1 = 102;
        npc2 = 103;
    elseif rnd == 1 then
        npc1 = 102;
        npc2 = 171;
    elseif rnd == 2 then
        npc1 = 102;
        npc2 = 103;
        npc3 = 171;
        npc4 = 102;
    else
        npc1 = 102;
        npc2 = 102;
    end;
    if rnd == 2 then
        StartCombat(heroName, nil, 4, npc1, (6+Diff+random(3))*mod, npc2, (5+Diff+random(3))*mod, npc3, (5+Diff+random(3))*mod, npc4, (6+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/snowCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    else
        StartCombat(heroName, nil, 2, npc1, (6+Diff+random(3))*mod, npc2, (6+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/snowCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    end;
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingDwarwen_05", nil);
        SetObjectEnabled("oDwellingDwarwen_05", true);
        SetObjectOwner("oDwellingDwarwen_05", 1); 
        sleep(1);
        MakeHeroInteractWithObject(heroName, "oDwellingDwarwen_05");
    end;
end;

-- огненные драконы
function dwellingBattleDwarwen06(hero, obj)
    heroName = hero;
    objectName = obj;
    if IsAIPlayer(GetObjectOwner(heroName)) == HUMAN then
        QuestionBoxForPlayers(GetPlayerFilter(GetObjectOwner(heroName)), GetMapDataPath().."dwelling1_search.txt", "dwellingBattleDwarwen06_start");
    end;
end;

function dwellingBattleDwarwen06_start()
    local mod = GetDate(MONTH);
    local npc1 = 0;
    local npc2 = 0;
    local npc3 = 0;
    local rnd = random(5);
    if rnd == 0 then
        npc1 = 104;
        npc2 = 105;
    elseif rnd == 1 then
        npc1 = 104;
        npc2 = 172;
    elseif rnd == 2 then
        npc1 = 104;
        npc2 = 105;
        npc3 = 172;
        npc4 = 104;
    else
        npc1 = 104;
        npc2 = 104;
    end;
    if rnd == 2 then
        StartCombat(heroName, nil, 4, npc1, (2+Diff+random(3))*mod, npc2, (1+Diff+random(3))*mod, npc3, (1+Diff+random(3))*mod, npc4, (2+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/snowCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    else
        StartCombat(heroName, nil, 2, npc1, (2+Diff+random(3))*mod, npc2, (2+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/snowCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    end;
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingDwarwen_06", nil);
        SetObjectEnabled("oDwellingDwarwen_06", true);
        SetObjectOwner("oDwellingDwarwen_06", 1); 
        sleep(1);
        MakeHeroInteractWithObject(heroName, "oDwellingDwarwen_06");
    end;
end;

-- фейки
function dwellingBattleSylvan01(hero, obj)
    heroName = hero;
    objectName = obj;
    if IsAIPlayer(GetObjectOwner(heroName)) == HUMAN then
        QuestionBoxForPlayers(GetPlayerFilter(GetObjectOwner(heroName)), GetMapDataPath().."dwelling1_search.txt", "dwellingBattleSylvan01_start");
    end;
end;

function dwellingBattleSylvan01_start()
    local mod = GetDate(MONTH);
    local npc1 = 0;
    local npc2 = 0;
    local npc3 = 0;
    local rnd = random(5);
    if rnd == 0 then
        npc1 = 43;
        npc2 = 44;
    elseif rnd == 1 then
        npc1 = 43;
        npc2 = 145;
    elseif rnd == 2 then
        npc1 = 43;
        npc2 = 44;
        npc3 = 145;
        npc4 = 43;
    else
        npc1 = 43;
        npc2 = 43;
    end;
    if rnd == 2 then
        StartCombat(heroName, nil, 4, npc1, (25+Diff+random(3))*mod, npc2, (25+Diff+random(3))*mod, npc3, (25+Diff+random(3))*mod, npc4, (25+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/elfCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    else
        StartCombat(heroName, nil, 2, npc1, (22+Diff+random(3))*mod, npc2, (22+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/elfCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    end;
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingSylvan_01", nil);
        SetObjectEnabled("oDwellingSylvan_01", true);
        SetObjectOwner("oDwellingSylvan_01", 1); 
        sleep(1);
        MakeHeroInteractWithObject(heroName, "oDwellingSylvan_01");
    end;
end;

-- танцоры
function dwellingBattleSylvan02(hero, obj)
    heroName = hero;
    objectName = obj;
    if IsAIPlayer(GetObjectOwner(heroName)) == HUMAN then
        QuestionBoxForPlayers(GetPlayerFilter(GetObjectOwner(heroName)), GetMapDataPath().."dwelling1_search.txt", "dwellingBattleSylvan02_start");
    end;
end;

function dwellingBattleSylvan02_start()
    local mod = GetDate(MONTH);
    local npc1 = 0;
    local npc2 = 0;
    local npc3 = 0;
    local rnd = random(5);
    if rnd == 0 then
        npc1 = 45;
        npc2 = 46;
    elseif rnd == 1 then
        npc1 = 45;
        npc2 = 146;
    elseif rnd == 2 then
        npc1 = 45;
        npc2 = 46;
        npc3 = 146;
        npc4 = 45;
    else
        npc1 = 45;
        npc2 = 45;
    end;
    if rnd == 2 then
        StartCombat(heroName, nil, 4, npc1, (20+Diff+random(3))*mod, npc2, (20+Diff+random(3))*mod, npc3, (20+Diff+random(3))*mod, npc4, (20+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/elfCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    else
        StartCombat(heroName, nil, 2, npc1, (19+Diff+random(3))*mod, npc2, (19+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/elfCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    end;
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingSylvan_02", nil);
        SetObjectEnabled("oDwellingSylvan_02", true);
        SetObjectOwner("oDwellingSylvan_02", 1); 
        sleep(1);
        MakeHeroInteractWithObject(heroName, "oDwellingSylvan_02");
    end;
end;

-- ослоухие лучники
function dwellingBattleSylvan03(hero, obj)
    heroName = hero;
    objectName = obj;
    if IsAIPlayer(GetObjectOwner(heroName)) == HUMAN then
        QuestionBoxForPlayers(GetPlayerFilter(GetObjectOwner(heroName)), GetMapDataPath().."dwelling1_search.txt", "dwellingBattleSylvan03_start");
    end;
end;

function dwellingBattleSylvan03_start()
    local mod = GetDate(MONTH);
    local npc1 = 0;
    local npc2 = 0;
    local npc3 = 0;
    local rnd = random(5);
    if rnd == 0 then
        npc1 = 47;
        npc2 = 48;
    elseif rnd == 1 then
        npc1 = 47;
        npc2 = 147;
    elseif rnd == 2 then
        npc1 = 47;
        npc2 = 48;
        npc3 = 147;
        npc4 = 47;
    else
        npc1 = 47;
        npc2 = 47;
    end;
    if rnd == 2 then
        StartCombat(heroName, nil, 4, npc1, (18+Diff+random(3))*mod, npc2, (18+Diff+random(3))*mod, npc3, (18+Diff+random(3))*mod, npc4, (18+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/elfCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    else
        StartCombat(heroName, nil, 2, npc1, (17+Diff+random(3))*mod, npc2, (17+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/elfCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    end;
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingSylvan_03", nil);
        SetObjectEnabled("oDwellingSylvan_03", true);
        SetObjectOwner("oDwellingSylvan_03", 1); 
        sleep(1);
        MakeHeroInteractWithObject(heroName, "oDwellingSylvan_03");
    end;
end;

-- друиды
function dwellingBattleSylvan04(hero, obj)
    heroName = hero;
    objectName = obj;
    if IsAIPlayer(GetObjectOwner(heroName)) == HUMAN then
        QuestionBoxForPlayers(GetPlayerFilter(GetObjectOwner(heroName)), GetMapDataPath().."dwelling1_search.txt", "dwellingBattleSylvan04_start");
    end;
end;

function dwellingBattleSylvan04_start()
    local mod = GetDate(MONTH);
    local npc1 = 0;
    local npc2 = 0;
    local npc3 = 0;
    local rnd = random(5);
    if rnd == 0 then
        npc1 = 49;
        npc2 = 50;
    elseif rnd == 1 then
        npc1 = 49;
        npc2 = 148;
    elseif rnd == 2 then
        npc1 = 49;
        npc2 = 50;
        npc3 = 148;
        npc4 = 49;
    else
        npc1 = 49;
        npc2 = 49;
    end;
    if rnd == 2 then
        StartCombat(heroName, nil, 4, npc1, (12+Diff+random(3))*mod, npc2, (12+Diff+random(3))*mod, npc3, (12+Diff+random(3))*mod, npc4, (12+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/elfCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    else
        StartCombat(heroName, nil, 2, npc1, (11+Diff+random(3))*mod, npc2, (11+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/elfCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    end;
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingSylvan_04", nil);
        SetObjectEnabled("oDwellingSylvan_04", true);
        SetObjectOwner("oDwellingSylvan_04", 1); 
        sleep(1);
        MakeHeroInteractWithObject(heroName, "oDwellingSylvan_04");
    end;
end;

-- роги и энты
function dwellingBattleSylvan05(hero, obj)
    heroName = hero;
    objectName = obj;
    if IsAIPlayer(GetObjectOwner(heroName)) == HUMAN then
        QuestionBoxForPlayers(GetPlayerFilter(GetObjectOwner(heroName)), GetMapDataPath().."dwelling1_search.txt", "dwellingBattleSylvan05_start");
    end;
end;

function dwellingBattleSylvan05_start()
    local mod = GetDate(MONTH);
    local npc1 = 0;
    local npc2 = 0;
    local npc3 = 0;
    local rnd = random(5);
    if rnd == 0 then
        npc1 = 52;
        npc2 = 54;
    elseif rnd == 1 then
        npc1 = 149;
        npc2 = 150;
    elseif rnd == 2 then
        npc1 = 52;
        npc2 = 149;
        npc3 = 150;
        npc4 = 54;
    else
        npc1 = 51;
        npc2 = 53;
    end;
    if rnd == 2 then
        StartCombat(heroName, nil, 4, npc1, (9+Diff+random(3))*mod, npc2, (9+Diff+random(3))*mod, npc3, (6+Diff+random(3))*mod, npc4, (6+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/elfCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    else
        StartCombat(heroName, nil, 2, npc1, (8+Diff+random(3))*mod, npc2, (6+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/elfCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    end;
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingSylvan_05", nil);
        SetObjectEnabled("oDwellingSylvan_05", true);
        SetObjectOwner("oDwellingSylvan_05", 1); 
        sleep(1);
        MakeHeroInteractWithObject(heroName, "oDwellingSylvan_05");
    end;
end;

-- зеленые драконы
function dwellingBattleSylvan06(hero, obj)
    heroName = hero;
    objectName = obj;
    if IsAIPlayer(GetObjectOwner(heroName)) == HUMAN then
        QuestionBoxForPlayers(GetPlayerFilter(GetObjectOwner(heroName)), GetMapDataPath().."dwelling1_search.txt", "dwellingBattleSylvan06_start");
    end;
end;

function dwellingBattleSylvan06_start()
    local mod = GetDate(MONTH);
    local npc1 = 0;
    local npc2 = 0;
    local npc3 = 0;
    local rnd = random(5);
    if rnd == 0 then
        npc1 = 55;
        npc2 = 56;
    elseif rnd == 1 then
        npc1 = 55;
        npc2 = 151;
    elseif rnd == 2 then
        npc1 = 55;
        npc2 = 56;
        npc3 = 151;
        npc4 = 55;
    else
        npc1 = 55;
        npc2 = 55;
    end;
    if rnd == 2 then
        StartCombat(heroName, nil, 4, npc1, (1+Diff+random(3))*mod, npc2, (2+Diff+random(3))*mod, npc3, (2+Diff+random(3))*mod, npc4, (1+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/elfCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    else
        StartCombat(heroName, nil, 2, npc1, (1+Diff+random(3))*mod, npc2, (1+Diff+random(3))*mod, nil, nil, "/Maps/SingleMissions/OnLastPoint/elfCombatArena.xdb#xpointer(/AdventureFlybyScene)");
    end;
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingSylvan_06", nil);
        SetObjectEnabled("oDwellingSylvan_06", true);
        SetObjectOwner("oDwellingSylvan_06", 1); 
        sleep(1);
        MakeHeroInteractWithObject(heroName, "oDwellingSylvan_06");
    end;
end;

-- торговая фактория
function checkTradeQuest()
    while 1 do
        sleep(5);
        if tradeQuestActive == enable then
            if sulfurTradeSkill == 9 or oreTradeSkill == 9 or woodTradeSkill == 9 then
                SetObjectiveState("qTradeSkill", OBJECTIVE_COMPLETED, PLAYER_1);
                MessageBox(GetMapDataPath().."trading/allContractCompleted.txt");
                SetTownBuildingLimitLevel(TOWN_DWARVEN, TOWN_BUILDING_DWELLING_7, 2);
                SetTownBuildingLimitLevel(TOWN_HEAVEN, TOWN_BUILDING_DWELLING_7, 2);
                SetTownBuildingLimitLevel(TOWN_SYLVAN, TOWN_BUILDING_DWELLING_7, 2);
                tradeQuestActive = disable;
            end;
        end;
    end;
end;

tradeQuestActive = 0;
resourceList = {0, 1, 2, 3, 4, 5};
dateTrading1 = 0;
dateTrading2 = 0;
dateTrading3 = 0;
mainTradeData = disable;
oreTradeData = disable;
woodTradeData = disable;
sulfurTradeData = disable;
mainTradeSkill = 0;
oreTradeSkill = 0;
woodTradeSkill = 0;
sulfurTradeSkill = 0;

trade1Title = GetMapDataPath().."trading/contractSulfurTitle.txt";
trade2Title = GetMapDataPath().."trading/contractOreTitle.txt";
trade3Title = GetMapDataPath().."trading/contractWoodTitle.txt";

-- tradeSkill = 0,  1,  2,  3,  4,  5,  6,  7,  8,  9
-- weekCount = 70, 63, 56, 49, 42, 35, 28, 21, 14, 7
-- - res/day = 10, 9,  8,  7,  6,  5,  4,  3,  2,  1
-- + res/day = 1,  2,  3,  4,  5,  6,  7,  8,  9,  10
function callTradeBoxMenu(heroName, objectName)
    heroName = heroName;
    objectName = objectName;
    TalkBoxForPlayers(PLAYER_1, "/Textures/Icons/Heroes/Heaven/Heaven_Rutger_128x128.(Texture).xdb#xpointer(/Texture)", 
        "", GetMapDataPath().."trading/tradeMenuDialog.txt", "", "callTrade", 1, GetMapDataPath().."trading/tradeMenuName.txt", "", 0,
        trade1Title, trade2Title, trade3Title);
end;

function callTrade(heroName, resource)
    resVar = resource;
    if resVar == 1 and sulfurTradeData == disable then
        if sulfurTradeSkill == 0 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/sulfur0.txt", "sulfurContractStart");
        elseif sulfurTradeSkill == 1 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/sulfur1.txt", "sulfurContractStart");
        elseif sulfurTradeSkill == 2 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/sulfur2.txt", "sulfurContractStart");
        elseif sulfurTradeSkill == 3 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/sulfur3.txt", "sulfurContractStart");
        elseif sulfurTradeSkill == 4 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/sulfur4.txt", "sulfurContractStart");
        elseif sulfurTradeSkill == 5 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/sulfur5.txt", "sulfurContractStart");
        elseif sulfurTradeSkill == 6 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/sulfur6.txt", "sulfurContractStart");
        elseif sulfurTradeSkill == 7 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/sulfur7.txt", "sulfurContractStart");
        elseif sulfurTradeSkill == 8 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/sulfur8.txt", "sulfurContractStart");
        elseif sulfurTradeSkill == 9 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/sulfur9.txt", "sulfurContractStart");
        end;
    elseif resVar == 1 and sulfurTradeData == enable then
        QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/contractRestart.txt", "allContractRestart");
    end;
    if resVar == 2 and oreTradeData == disable then
        if oreTradeSkill == 0 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/ore0.txt", "oreContractStart");
        elseif oreTradeSkill == 1 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/ore1.txt", "oreContractStart");
        elseif oreTradeSkill == 2 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/ore2.txt", "oreContractStart");
        elseif oreTradeSkill == 3 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/ore3.txt", "oreContractStart");
        elseif oreTradeSkill == 4 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/ore4.txt", "oreContractStart");
        elseif oreTradeSkill == 5 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/ore5.txt", "oreContractStart");
        elseif oreTradeSkill == 6 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/ore6.txt", "oreContractStart");
        elseif oreTradeSkill == 7 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/ore7.txt", "oreContractStart");
        elseif oreTradeSkill == 8 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/ore8.txt", "oreContractStart");
        elseif oreTradeSkill == 9 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/ore9.txt", "oreContractStart");
        end;
    elseif resVar == 2 and oreTradeData == enable then
        QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/contractRestart.txt", "allContractRestart");
    end;
    if resVar == 3 and woodTradeData == disable then
        if woodTradeSkill == 0 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/wood0.txt", "woodContractStart");
        elseif woodTradeSkill == 1 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/wood1.txt", "woodContractStart");
        elseif woodTradeSkill == 2 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/wood2.txt", "woodContractStart");
        elseif woodTradeSkill == 3 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/wood3.txt", "woodContractStart");
        elseif woodTradeSkill == 4 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/wood4.txt", "woodContractStart");
        elseif woodTradeSkill == 5 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/wood5.txt", "woodContractStart");
        elseif woodTradeSkill == 6 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/wood6.txt", "woodContractStart");
        elseif woodTradeSkill == 7 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/wood7.txt", "woodContractStart");
        elseif woodTradeSkill == 8 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/wood8.txt", "woodContractStart");
        elseif woodTradeSkill == 9 then
            QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/wood9.txt", "woodContractStart");
        end;
    elseif resVar == 3 and woodTradeData == enable then
        QuestionBoxForPlayers(GetPlayerFilter(1), GetMapDataPath().."trading/contractRestart.txt", "allContractRestart");
    end;
end;

function sulfurContractStart()
    local sulfur = GetPlayerResource(1, 4);
    local weekCost = 70 - sulfurTradeSkill*7;
    if sulfur > weekCost then
        sulfurTradeData = enable;
        trade1Title = GetMapDataPath().."trading/contractSulfurActiveTitle.txt";
        MessageBox(GetMapDataPath().."trading/contractActive.txt");
    else
        MessageBox(GetMapDataPath().."trading/deficitSulfur.txt");
    end;
end;

function oreContractStart()
    local ore = GetPlayerResource(1, 1);
    local weekCost = 70 - oreTradeSkill*7;
    if ore > weekCost then
        oreTradeData = enable;
        trade2Title = GetMapDataPath().."trading/contractOreActiveTitle.txt";
        MessageBox(GetMapDataPath().."trading/contractActive.txt");
    else
        MessageBox(GetMapDataPath().."trading/deficitOre.txt");
    end;
end;

function woodContractStart()
    local wood = GetPlayerResource(1, 0);
    local weekCost = 70 - woodTradeSkill*7;
    if wood > weekCost then
        woodTradeData = enable;
        trade3Title = GetMapDataPath().."trading/contractWoodActiveTitle.txt";
        MessageBox(GetMapDataPath().."trading/contractActive.txt");
    else
        MessageBox(GetMapDataPath().."trading/deficitWood.txt");
    end;
end;

function messageTradingEvent(heroName)
    Trigger(REGION_ENTER_AND_STOP_TRIGGER, "tradeReg1", nil);
    Trigger(REGION_ENTER_AND_STOP_TRIGGER, "tradeReg2", nil);
    Trigger(REGION_ENTER_AND_STOP_TRIGGER, "tradeReg3", nil);
    MessageBoxForPlayers(GetPlayerFilter(GetObjectOwner(heroName)), GetMapDataPath().."Text/trade_start.txt");
    sleep(2);
    SetObjectiveState("qTradeSkill", OBJECTIVE_ACTIVE, PLAYER_1);
    tradeQuestActive = enable;
end;

function graalActive(heroName)
    for i = 1, length(obelisk) do
        Trigger(OBJECT_TOUCH_TRIGGER, obelisk[i], nil);
    end;
    sleep(5);
    SetObjectiveState("qGraal", OBJECTIVE_ACTIVE, PLAYER_1);
end;

-- хижина
virtHero = "";
startQuestCount = 0;
startQuestActive = disable;
QUEST_PATH = "Maps/SingleMissions/OnLastPoint/Text/Quest/";
houseQuest1Title = QUEST_PATH.."firstQuestInHouseQ1_name.txt";
houseQuest2Title = QUEST_PATH.."firstQuestInHouseQ2_name.txt";
houseQuest3Title = QUEST_PATH.."firstQuestInHouseQ3_name.txt";
houseQuest4Title = QUEST_PATH.."firstQuestInHouseQ4_name.txt";

function questHouseEvent(heroName)
    local mod = GetDate(MONTH);
    virtHero = heroName;
    if startQuestCount ~= 4 then
        if startQuestActive == disable then
            TalkBoxForPlayers(PLAYER_1, "/Textures/Icons/Heroes/Necropolis/Necropolis_Deirdre_128x128.(Texture).xdb#xpointer(/Texture)", 
                "", QUEST_PATH.."firstQuestInHouseDialog.txt", "", "startQuestInHouseBoxChange", 1, QUEST_PATH.."firstQuestInHouseMain.txt", "", 0,
                houseQuest1Title, houseQuest2Title, houseQuest3Title, houseQuest4Title);
        else
            if GetObjectiveState("qLight", PLAYER_1) == OBJECTIVE_ACTIVE then
                if lightProgressCount >= 6 then
                    SetObjectiveState("qLight", OBJECTIVE_COMPLETED, PLAYER_1);
                    ChangeHeroStat(heroName, 0, 1050*Diff*mod);
                    GiveArtefact(heroName, 77);
                    houseQuest1Title = QUEST_PATH.."firstQuestInHouseQ1_nameC.txt";
                    startQuestCount = startQuestCount+1;
                    startQuestActive = disable;
                else
                    MessageBox(QUEST_PATH.."firstQuestInHouseQ1_emty.txt");
                end;
            end;
            if GetObjectiveState("qDark", PLAYER_1) == OBJECTIVE_ACTIVE then
                if darkProgressCount >= 3 then
                    SetObjectiveState("qDark", OBJECTIVE_COMPLETED, PLAYER_1);
                    ChangeHeroStat(heroName, 0, 1150*Diff*mod);
                    GiveArtefact(heroName, 78);
                    houseQuest2Title = QUEST_PATH.."firstQuestInHouseQ2_nameC.txt";
                    startQuestCount = startQuestCount+1;
                    startQuestActive = disable;
                else
                    MessageBox(QUEST_PATH.."firstQuestInHouseQ2_emty.txt");
                end;
            end;
            if GetObjectiveState("qChaos", PLAYER_1) == OBJECTIVE_ACTIVE then
                if chaosProgress == enable then
                    SetObjectiveState("qChaos", OBJECTIVE_COMPLETED, PLAYER_1);
                    ChangeHeroStat(heroName, 0, 1350*Diff*mod);
                    GiveArtefact(heroName, 76);
                    houseQuest3Title = QUEST_PATH.."firstQuestInHouseQ3_nameC.txt";
                    startQuestCount = startQuestCount+1;
                    startQuestActive = disable;
                else
                    MessageBox(QUEST_PATH.."firstQuestInHouseQ3_emty.txt");
                end;
            end;
            if GetObjectiveState("qSummon", PLAYER_1) == OBJECTIVE_ACTIVE then
                if GetHeroCreatures(heroName, 85) >= 1 and GetHeroCreatures(heroName, 86) >= 1 and 
                GetHeroCreatures(heroName, 87) >= 1 and GetHeroCreatures(heroName, 88) >= 1 then
                    SetObjectiveState("qSummon", OBJECTIVE_COMPLETED, PLAYER_1);
                    RemoveHeroCreatures(heroName, 85, 1);
                    RemoveHeroCreatures(heroName, 86, 1);
                    RemoveHeroCreatures(heroName, 87, 1);
                    RemoveHeroCreatures(heroName, 88, 1);
                    ChangeHeroStat(heroName, 0, 1050*Diff*mod);
                    GiveArtefact(heroName, 79);
                    houseQuest4Title = QUEST_PATH.."firstQuestInHouseQ4_nameC.txt";
                    startQuestCount = startQuestCount+1;
                    startQuestActive = disable;
                else
                    MessageBox(QUEST_PATH.."firstQuestInHouseQ4_emty.txt");
                end;
            end;
        end;
    else
        MessageBox(QUEST_PATH.."firstQuestInHouseAll_emty.txt");
    end;
end;

function startQuestInHouseBoxCancel()
    if GetObjectiveState("qLight", PLAYER_1) == OBJECTIVE_ACTIVE then
        SetObjectiveState("qLight", OBJECTIVE_FAILED, PLAYER_1);
        houseQuest1Title = QUEST_PATH.."firstQuestInHouseQ1_nameFailed.txt";
    end;
    if GetObjectiveState("qDark", PLAYER_1) == OBJECTIVE_ACTIVE then
        SetObjectiveState("qDark", OBJECTIVE_FAILED, PLAYER_1);
        houseQuest2Title = QUEST_PATH.."firstQuestInHouseQ2_nameFailed.txt";
    end;
    if GetObjectiveState("qChaos", PLAYER_1) == OBJECTIVE_ACTIVE then
        SetObjectiveState("qChaos", OBJECTIVE_FAILED, PLAYER_1);
        houseQuest3Title = QUEST_PATH.."firstQuestInHouseQ3_nameFailed.txt";
    end;
    if GetObjectiveState("qSummon", PLAYER_1) == OBJECTIVE_ACTIVE then
        SetObjectiveState("qSummon", OBJECTIVE_FAILED, PLAYER_1);
        houseQuest3Title = QUEST_PATH.."firstQuestInHouseQ4_nameFailed.txt";
    end;
    startQuestActive = disable;
end;

function startQuestInHouseBoxChange(heroName, questCount)
    if questCount == 1 then
        if GetObjectiveState("qLight", PLAYER_1) == OBJECTIVE_COMPLETED or GetObjectiveState("qLight", PLAYER_1) == OBJECTIVE_FAILED then
            MessageBox(QUEST_PATH.."firstQuestInHouse_emty.txt");
        else
            QuestionBox(QUEST_PATH.."firstQuestInHouseQ1_desc.txt", "startQuestInHouseFirst");
        end;
    end;
    if questCount == 2 then
        if GetObjectiveState("qDark", PLAYER_1) == OBJECTIVE_COMPLETED or GetObjectiveState("qDark", PLAYER_1) == OBJECTIVE_FAILED then
            MessageBox(QUEST_PATH.."firstQuestInHouse_emty.txt");
        else
            QuestionBox(QUEST_PATH.."firstQuestInHouseQ2_desc.txt", "startQuestInHouseSecond");
        end;
    end;
    if questCount == 3 then
        if GetObjectiveState("qChaos", PLAYER_1) == OBJECTIVE_COMPLETED or GetObjectiveState("qChaos", PLAYER_1) == OBJECTIVE_FAILED then
            MessageBox(QUEST_PATH.."firstQuestInHouse_emty.txt");
        else
            QuestionBox(QUEST_PATH.."firstQuestInHouseQ3_desc.txt", "startQuestInHouseThird");
        end;
    end;
    if questCount == 4 then
        if GetObjectiveState("qSummon", PLAYER_1) == OBJECTIVE_COMPLETED or GetObjectiveState("qSummon", PLAYER_1) == OBJECTIVE_FAILED then
            MessageBox(QUEST_PATH.."firstQuestInHouse_emty.txt");
        else
            QuestionBox(QUEST_PATH.."firstQuestInHouseQ4_desc.txt", "startQuestInHouseFour");
        end;
    end;
end;

-- квест на том света
--StartCombat(heroName, nil, 3, 146, (36*Diff)*mod, 149, (8*Diff)*mod, 46, (34*Diff)*mod, "Script:Maps/SingleMissions/OnLastPoint/HasteCombat.(Script).xdb#xpointer(/Script)");
lightProgressCount = 0;

function startQuestInHouseFirst()
    SetObjectiveState("qLight", OBJECTIVE_ACTIVE, PLAYER_1);
    BlockGame()
    CreateStatic("lightStatic01", lightStatic, 75, 159, 0, 0, -1, 100);
    Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rLight01", "lightEvent1");
    sleep(5);
    SetObjectFlashlight("lightStatic01", "lightPoint");
    CreateStatic("lightStatic02", lightStatic, 46, 155, 0, 0, -1, 100);
    Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rLight02", "lightEvent2");
    sleep(5);
    SetObjectFlashlight("lightStatic02", "lightPoint");
    CreateStatic("lightStatic03", lightStatic, 30, 36, 0, 0, -1, 100);
    Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rLight03", "lightEvent3");
    sleep(5);
    SetObjectFlashlight("lightStatic03", "lightPoint");
    CreateStatic("lightStatic04", lightStatic, 106, 92, 0, 0, -1, 100);
    Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rLight04", "lightEvent4");
    sleep(5);
    SetObjectFlashlight("lightStatic04", "lightPoint");
    CreateStatic("lightStatic05", lightStatic, 157, 10, 0, 0, -1, 100);
    Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rLight05", "lightEvent5");
    sleep(5);
    SetObjectFlashlight("lightStatic05", "lightPoint");
    CreateStatic("lightStatic06", lightStatic, 69, 25, 0, 0, -1, 100);
    Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rLight06", "lightEvent6");
    sleep(5);
    SetObjectFlashlight("lightStatic06", "lightPoint");
    UnblockGame();
    startQuestActive = enable;
end;

-- карающий удар ивент
function lightEvent1(hero)
    heroName = hero;
    local mod = GetDate(MONTH);
    BlockGame();
    PlayVisualEffect("/Effects/_(Effect)/Spells/Bloodlust.xdb#xpointer(/Effect)", heroName, "tag1", 0, 0, 1, 0, 0);
    UnblockGame();
    sleep(15);
    StartCombat(heroName, nil, 3, 168, (12*Diff)*mod, 167, (32*Diff)*mod, 169, (16*Diff)*mod, nil);
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rLight01", nil);
        lightProgressCount = lightProgressCount+1;
        SetObjectiveProgress("qLight", lightProgressCount, PLAYER_1);
        RemoveObject("lightStatic01");
    end;
end;

-- каменная кожа ивент
function lightEvent2(hero)
    heroName = hero;
    local mod = GetDate(MONTH);
    BlockGame();
    PlayVisualEffect("/Effects/_(Effect)/Spells/StoneSkin.xdb#xpointer(/Effect)", heroName, "tag2", 0, 0, 1, 0, 0);
    UnblockGame();
    sleep(15);
    StartCombat(heroName, nil, 2, 103, (2*Diff)*mod+random(3), 171, (2*Diff)*mod+random(3), nil);
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rLight02", nil);
        lightProgressCount = lightProgressCount+1;
        SetObjectiveProgress("qLight", lightProgressCount, PLAYER_1);
        RemoveObject("lightStatic02");
    end;
end;

-- божественная сила ивент
function lightEvent3(hero)
    heroName = hero;
    local mod = GetDate(MONTH);
    BlockGame();
    PlayVisualEffect("/Effects/_(Effect)/Spells/Bless.xdb#xpointer(/Effect)", heroName, "tag3", 0, 0, 1, 0, 0);
    UnblockGame();
    sleep(15);
    StartCombat(heroName, nil, 3, 107, (26*Diff)*mod, 108, (22*Diff)*mod, 111, (4*Diff)*mod, nil);
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rLight03", nil);
        lightProgressCount = lightProgressCount+1;
        SetObjectiveProgress("qLight", lightProgressCount, PLAYER_1);
        RemoveObject("lightStatic03");
    end;
end;

-- уклонение ивент
function lightEvent4(hero)
    heroName = hero;
    local mod = GetDate(MONTH);
    BlockGame();
    PlayVisualEffect("/Effects/_(Effect)/Spells/DeflectArrows.xdb#xpointer(/Effect)", heroName, "tag4", 0, 0, 1, 0, 0);
    UnblockGame();
    sleep(15);
    StartCombat(heroName, nil, 2, 14, (2*Diff)*mod+random(3), 112, (2*Diff)*mod+random(3), nil);
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rLight04", nil);
        lightProgressCount = lightProgressCount+1;
        SetObjectiveProgress("qLight", lightProgressCount, PLAYER_1);
        RemoveObject("lightStatic04");
    end;
end;

-- ускорение ивент
function lightEvent5(hero)
    heroName = hero;
    local mod = GetDate(MONTH);
    BlockGame();
    PlayVisualEffect("/Effects/_(Effect)/Spells/Haste.xdb#xpointer(/Effect)", heroName, "tag5", 0, 0, 1, 0, 0);
    UnblockGame();
    sleep(15);
    StartCombat(heroName, nil, 3, 146, (36*Diff)*mod, 149, (8*Diff)*mod, 46, (34*Diff)*mod, nil);
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rLight05", nil);
        lightProgressCount = lightProgressCount+1;
        SetObjectiveProgress("qLight", lightProgressCount, PLAYER_1);
        RemoveObject("lightStatic05");
    end;
end;

-- антимагия ивет
function lightEvent6(hero)
    heroName = hero;
    local mod = GetDate(MONTH);
    BlockGame();
    PlayVisualEffect("/Effects/_(Effect)/Spells/Anti-magic.xdb#xpointer(/Effect)", heroName, "tag6", 0, 0, 1, 0, 0);
    UnblockGame();
    sleep(15);
    StartCombat(heroName, nil, 2, 54, (6*Diff)*mod+random(3), 150, (6*Diff)*mod+random(3), nil);
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rLight06", nil);
        lightProgressCount = lightProgressCount+1;
        SetObjectiveProgress("qLight", lightProgressCount, PLAYER_1);
        RemoveObject("lightStatic06");
    end;
end;

-- квест на том магии тьмы
darkProgressCount = 0;

function startQuestInHouseSecond()
    SetObjectiveState("qDark", OBJECTIVE_ACTIVE, PLAYER_1);
    BlockGame();
    CreateStatic("darkStatic01", darkStatic, 111, 154, 0, 1.57, -1, 100);
    Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rDark01", "darkEvent1");
    sleep(5);
    SetObjectFlashlight("darkStatic01", "darkPoint");
    CreateStatic("darkStatic02", darkStatic, 3, 55, 0, 1.57, -1, 100);
    Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rDark02", "darkEvent2");
    sleep(5);
    SetObjectFlashlight("darkStatic02", "darkPoint");
    CreateStatic("darkStatic03", darkStatic, 94, 3, 0, 4, -1, 100);
    Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rDark03", "darkEvent3");
    sleep(5);
    SetObjectFlashlight("darkStatic03", "darkPoint");
    UnblockGame();
    startQuestActive = enable;
end;

-- чума ивент
function darkEvent1(hero)
    heroName = hero;
    local mod = GetDate(MONTH);
    BlockGame();
    PlayVisualEffect("/Effects/_(Effect)/Spells/Plague.xdb#xpointer(/Effect)", heroName, "tag7", 0, 0, 1, 0, 0);
    UnblockGame();
    sleep(15);
    StartCombat(heroName, nil, 4, 32, (76*Diff)*mod, 36, (24*Diff)*mod, 34, (38*Diff)*mod, 40, (4*Diff)*mod+random(3), nil);
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rDark01", nil);
        darkProgressCount = darkProgressCount+1;
        SetObjectiveProgress("qDark", darkProgressCount, PLAYER_1);
        RemoveObject("darkStatic01");
    end;
end;

-- замедление и скорбь ивент
function darkEvent2(hero)
    heroName = hero;
    local mod = GetDate(MONTH);
    BlockGame();
    PlayVisualEffect("/Effects/_(Effect)/Spells/Slow.xdb#xpointer(/Effect)", heroName, "tag8", 0, 0, 1, 0, 0);
    UnblockGame();
    sleep(15);
    StartCombat(heroName, nil, 3, 30, (76*Diff)*mod, 153, (48*Diff)*mod, 157, (4*Diff)*mod+random(3), nil);
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rDark02", nil);
        darkProgressCount = darkProgressCount+1;
        SetObjectiveProgress("qDark", darkProgressCount, PLAYER_1);
        RemoveObject("darkStatic02");
    end;
end;

-- разрушающий и немощность ивент
function darkEvent3(hero)
    heroName = hero;
    local mod = GetDate(MONTH);
    BlockGame();
    PlayVisualEffect("/Effects/_(Effect)/Spells/DisruptingRay.xdb#xpointer(/Effect)", heroName, "tag9", 0, 0, 1, 0, 0);
    UnblockGame();
    sleep(15);
    StartCombat(heroName, nil, 3, 155, (26*Diff)*mod, 156, (18*Diff)*mod, 154, (24*Diff)*mod, nil);
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rDark03", nil);
        darkProgressCount = darkProgressCount+1;
        SetObjectiveProgress("qDark", darkProgressCount, PLAYER_1);
        RemoveObject("darkStatic03");
    end;
end;

-- квест на том магии призыва
--labCount = 0;

function startQuestInHouseFour()
    SetObjectiveState("qSummon", OBJECTIVE_ACTIVE, PLAYER_1);
    SetObjectPosition(virtHero, 13, 34, 1);
    SetObjectPosition("Djyn", 19, 49, 1);
    EnableHeroAI("Djyn", true);
    SetAIHeroAttractor(virtHero, "Djyn", 2);
    startQuestActive = enable;
    sleep(5);
    OpenCircleFog(19, 49, 1, 10, 1);
end;

function checkTeleport(hero, obj)
    --if labCount >= 4 then
    sleep(5);
    if GetHeroCreatures(hero, 85) >= 1 and GetHeroCreatures(hero, 86) >= 1 and 
        GetHeroCreatures(hero, 87) >= 1 and GetHeroCreatures(hero, 88) >= 1 then
        Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rLabExit", "labTeleportActive");
        OpenCircleFog(79, 16, 1, 10, 1);
    end;
    MakeHeroInteractWithObject(hero, obj);
    --labCount = labCount+1;
end;

function labTeleportClosed(hero)
    MessageBox(QUEST_PATH.."labQuest_emty.txt");
end;

function labTeleportActive(hero)
    heroName = hero;
    SetObjectPosition(heroName, 25, 137, 0);
    SetObjectPosition("Djyn", 49, 128, 1);
    EnableHeroAI("Djyn", nil);
    Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rLabExit", nil);
end;

-- Хаос
chaosProgress = disable;

function startQuestInHouseThird()
    SetObjectiveState("qChaos", OBJECTIVE_ACTIVE, PLAYER_1);
    SetObjectPosition(virtHero, 134, 44, 1);
    startQuestActive = enable;
end;

function checkingChaos(heroName, heroWin)
    MessageBox(QUEST_PATH.."chaosQuest_complete.txt");
    sleep(5);
    SetObjectPosition(heroWin, 25, 137, 0);
    chaosProgress = enable;
end;

-- Пра-Феникс
function phoenixDwellingQuestion(hero, obj)
    heroName = hero;
    objectName = obj;
    if IsAIPlayer(GetObjectOwner(heroName)) == HUMAN then
        QuestionBoxForPlayers(GetPlayerFilter(GetObjectOwner(heroName)), GetMapDataPath().."dwelling1_search.txt", "phoenixDwelling_start");
    end;
end;

function phoenixDwelling_start()
    MakeHeroInteractWithObject(heroName, "phoenixBoss");
    sleep(5);
    if IsHeroAlive(heroName) == true then
        Trigger(OBJECT_TOUCH_TRIGGER, "phoenixDwell", nil);
        SetObjectEnabled("phoenixDwell", true);
        SetObjectOwner("phoenixDwell", 1); 
        sleep(1);
        MakeHeroInteractWithObject(heroName, "phoenixDwell");
        RemoveObject("mPhoenix");
    end;
end;

-- информация о двелинге фениксов
function informationPhoenix()
    MessageBox(QUEST_PATH.."phoenixInfo.txt");
    Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rPhoenixInfo", nil);
end;

-- триггеры --
Trigger(NEW_DAY_TRIGGER, "NewDayEvent");
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rSiege01", "SiegeStart01");
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rSiege02", "SiegeStart02");
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rSiege03", "SiegeStart03");
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rTransit01", "transitUp");
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rTransit02", "transitUp");
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rTransit03", "transitUp");
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rTransit04", "transitUp");
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rTransit05", "transitUp");
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rTransit06", "transitUp");
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rTransit07", "transitUp");
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rTransit08", "transitUp");
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rTransit09", "transitUp");
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rTransit10", "transitUp");
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rTransit12", "transitUp");
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rTransit13", "transitUp");
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rTransit14", "transitUp");
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rTransit15", "transitUp");
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "rLabExit", "labTeleportClosed");
Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingDwarwen_01", "dwellingBattleDwarwen01");
Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingDwarwen_02", "dwellingBattleDwarwen02");
Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingDwarwen_03", "dwellingBattleDwarwen03");
Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingDwarwen_04", "dwellingBattleDwarwen04");
Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingDwarwen_05", "dwellingBattleDwarwen05");
Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingDwarwen_06", "dwellingBattleDwarwen06");
Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingHeaven_01", "dwellingBattleHeaven01");
Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingHeaven_02", "dwellingBattleHeaven02");
Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingHeaven_03", "dwellingBattleHeaven03");
Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingHeaven_04", "dwellingBattleHeaven04");
Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingHeaven_05", "dwellingBattleHeaven05");
Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingHeaven_06", "dwellingBattleHeaven06");
Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingSylvan_01", "dwellingBattleSylvan01");
Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingSylvan_02", "dwellingBattleSylvan02");
Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingSylvan_03", "dwellingBattleSylvan03");
Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingSylvan_04", "dwellingBattleSylvan04");
Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingSylvan_05", "dwellingBattleSylvan05");
Trigger(OBJECT_TOUCH_TRIGGER, "oDwellingSylvan_06", "dwellingBattleSylvan06");
Trigger(OBJECT_TOUCH_TRIGGER, "phoenixDwell", "phoenixDwellingQuestion");
Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rInfoClosed", "informationClosedRegion");
Trigger(REGION_ENTER_AND_STOP_TRIGGER, "tradeReg1", "messageTradingEvent");
Trigger(REGION_ENTER_AND_STOP_TRIGGER, "tradeReg2", "messageTradingEvent");
Trigger(REGION_ENTER_AND_STOP_TRIGGER, "tradeReg3", "messageTradingEvent");
Trigger(REGION_ENTER_AND_STOP_TRIGGER, "rPhoenixInfo", "informationPhoenix");
Trigger(OBJECT_TOUCH_TRIGGER, "tradingPost1", "callTradeBoxMenu");
Trigger(OBJECT_TOUCH_TRIGGER, "tradingPost2", "callTradeBoxMenu");
Trigger(OBJECT_TOUCH_TRIGGER, "tradingPost3", "callTradeBoxMenu");
Trigger(OBJECT_TOUCH_TRIGGER, "elem01", "checkTeleport");
Trigger(OBJECT_TOUCH_TRIGGER, "elem02", "checkTeleport");
Trigger(OBJECT_TOUCH_TRIGGER, "elem03", "checkTeleport");
Trigger(OBJECT_TOUCH_TRIGGER, "elem04", "checkTeleport");
Trigger(PLAYER_REMOVE_HERO_TRIGGER, PLAYER_3, "checkingChaos");
Trigger(PLAYER_REMOVE_HERO_TRIGGER, PLAYER_2, "RewardForSiege");
Trigger(PLAYER_REMOVE_HERO_TRIGGER, PLAYER_1, "ResetForSiege");
Trigger(OBJECT_CAPTURE_TRIGGER, TOWN_DWARVEN, "resetSiegeAndRazedTown");
Trigger(OBJECT_CAPTURE_TRIGGER, TOWN_HEAVEN, "resetSiegeAndRazedTown");
Trigger(OBJECT_CAPTURE_TRIGGER, TOWN_SYLVAN, "resetSiegeAndRazedTown");
for i = 1, length(obelisk) do
    Trigger(OBJECT_TOUCH_TRIGGER, obelisk[i], "graalActive");
end;

Trigger(OBJECT_TOUCH_TRIGGER, "qHouse", "questHouseEvent");
-- потоки --
startThread(Difficulty);
startThread(checkTradeQuest);

--
print("map version 1.3.4 by KioM");
