#include "nw_i0_tool"

void main()
{
    object oStar1 = GetObjectByTag("STAR_NIGHT");
    object oStar2 = GetObjectByTag("STAR_HEAVEN");
    object oStar3 = GetObjectByTag("STAR_DAY");
    object oStar4 = GetObjectByTag("STAR_EARTH");
    object oStar5 = GetObjectByTag("STAR_SEA");

    object oStone1 = GetItemPossessedBy(oStar3, "gem_moon");
    object oStone2 = GetItemPossessedBy(oStar3, "gem_star");
    object oStone3 = GetItemPossessedBy(oStar3, "gem_sun");
    object oStone4 = GetItemPossessedBy(oStar3, "gem_diam");
    object oStone5 = GetItemPossessedBy(oStar3, "gem_water");

    object oItem1 = GetItemPossessedBy(oStar1, "gem_MOON");
    object oItem2 = GetItemPossessedBy(oStar2, "gem_star");
    object oItem3 = GetItemPossessedBy(oStar3, "gem_sun");
    object oItem4 = GetItemPossessedBy(oStar4, "gem_diam");
    object oItem5 = GetItemPossessedBy(oStar5, "gem_water");

    object oPC = GetLastClosedBy();

    object oDest1 = GetObjectByTag("WP_CORNU1");
    location lDest1 = GetLocation(oDest1);
    object oDest2 = GetObjectByTag("WP_CORNU2");
    location lDest2 = GetLocation(oDest2);
    object oDest3 = GetObjectByTag("WP_CORNU3");
    location lDest3 = GetLocation(oDest3);
    object oDest4 = GetObjectByTag("WP_CORNU4");
    location lDest4 = GetLocation(oDest4);
    object oDest5 = GetObjectByTag("WP_PORTAL");
    location lDest5 = GetLocation(oDest5);

    SetLocalInt(OBJECT_SELF, "duration", DURATION_TYPE_TEMPORARY);
    int duration = GetLocalInt(OBJECT_SELF, "duration");
    SetLocalInt(OBJECT_SELF, "vfx", VFX_IMP_FLAME_M);
    int vfx = GetLocalInt(OBJECT_SELF, "vfx");
    effect eEffect = EffectVisualEffect(vfx);

    SetLocalInt(OBJECT_SELF, "duration2", DURATION_TYPE_TEMPORARY);
    int duration2 = GetLocalInt(OBJECT_SELF, "duration2");
    SetLocalInt(OBJECT_SELF, "vfx2", VFX_FNF_SUMMON_GATE);
    int vfx2 = GetLocalInt(OBJECT_SELF, "vfx2");
    effect eEffect2 = EffectVisualEffect(vfx2);

    {
        if(GetIsObjectValid(oStone3))
        {
        ApplyEffectToObject(duration, eEffect, oStar3, 4.0);
        }

        if(GetIsObjectValid(oStone2))
        {
        ApplyEffectAtLocation(duration, eEffect2, lDest1, 4.0);
        ApplyEffectAtLocation(duration, eEffect2, lDest2, 4.0);
        ApplyEffectAtLocation(duration, eEffect2, lDest3, 4.0);
        ApplyEffectAtLocation(duration, eEffect2, lDest4, 4.0);
        CreateObject(OBJECT_TYPE_CREATURE,"cornugon",lDest1,FALSE);
        CreateObject(OBJECT_TYPE_CREATURE,"cornugon",lDest2,FALSE);
        CreateObject(OBJECT_TYPE_CREATURE,"cornugon",lDest3,FALSE);
        CreateObject(OBJECT_TYPE_CREATURE,"cornugon",lDest4,FALSE);
        DestroyObject(oStone2);
        CreateItemOnObject("gem_star", oPC, 1);
        }

        if(GetIsObjectValid(oStone1))
        {
        ApplyEffectAtLocation(duration, eEffect2, lDest1, 4.0);
        ApplyEffectAtLocation(duration, eEffect2, lDest2, 4.0);
        ApplyEffectAtLocation(duration, eEffect2, lDest3, 4.0);
        ApplyEffectAtLocation(duration, eEffect2, lDest4, 4.0);
        CreateObject(OBJECT_TYPE_CREATURE,"cornugon",lDest1,FALSE);
        CreateObject(OBJECT_TYPE_CREATURE,"cornugon",lDest2,FALSE);
        CreateObject(OBJECT_TYPE_CREATURE,"cornugon",lDest3,FALSE);
        CreateObject(OBJECT_TYPE_CREATURE,"cornugon",lDest4,FALSE);
        DestroyObject(oStone1);
        CreateItemOnObject("gem_moon", oPC, 1);
        }

        if(GetIsObjectValid(oStone4))
        {
        ApplyEffectAtLocation(duration, eEffect2, lDest1, 4.0);
        ApplyEffectAtLocation(duration, eEffect2, lDest2, 4.0);
        ApplyEffectAtLocation(duration, eEffect2, lDest3, 4.0);
        ApplyEffectAtLocation(duration, eEffect2, lDest4, 4.0);
        CreateObject(OBJECT_TYPE_CREATURE,"cornugon",lDest1,FALSE);
        CreateObject(OBJECT_TYPE_CREATURE,"cornugon",lDest2,FALSE);
        CreateObject(OBJECT_TYPE_CREATURE,"cornugon",lDest3,FALSE);
        CreateObject(OBJECT_TYPE_CREATURE,"cornugon",lDest4,FALSE);
        DestroyObject(oStone4);
        CreateItemOnObject("gem_diam", oPC, 1);
        }

        if(GetIsObjectValid(oStone5))
        {
        ApplyEffectAtLocation(duration, eEffect2, lDest1, 4.0);
        ApplyEffectAtLocation(duration, eEffect2, lDest2, 4.0);
        ApplyEffectAtLocation(duration, eEffect2, lDest3, 4.0);
        ApplyEffectAtLocation(duration, eEffect2, lDest4, 4.0);
        CreateObject(OBJECT_TYPE_CREATURE,"cornugon",lDest1,FALSE);
        CreateObject(OBJECT_TYPE_CREATURE,"cornugon",lDest2,FALSE);
        CreateObject(OBJECT_TYPE_CREATURE,"cornugon",lDest3,FALSE);
        CreateObject(OBJECT_TYPE_CREATURE,"cornugon",lDest4,FALSE);
        DestroyObject(oStone5);
        CreateItemOnObject("gem_water", oPC, 1);
        }
    }

    if(GetIsObjectValid(oItem1) && GetIsObjectValid(oItem2) && GetIsObjectValid(oItem3) && GetIsObjectValid(oItem4) && GetIsObjectValid(oItem5))
    {
    DestroyObject(oItem1);
    DestroyObject(oItem2);
    DestroyObject(oItem3);
    DestroyObject(oItem4);
    DestroyObject(oItem5);
    RewardPartyXP(5000, oPC);
    CreateObject(OBJECT_TYPE_PLACEABLE,"end_portal",lDest5,FALSE);
    }
}
