public static void MountHorse(CLRScriptBase script, uint Character, uint Horse)
        {
            if (!isWarhorse.ContainsKey(Character)) isWarhorse.Add(Character, true);

            string cloakResRef;
            switch(script.GetTag(Horse))
            {
                case "abr_cr_an_horse01":
                    cloakResRef = "acr_ooc_horse01";
                    isWarhorse[Character] = true;
                    break;
                case "abr_cr_an_horse02":
                    cloakResRef = "acr_ooc_horse02";
                    isWarhorse[Character] = true;
                    break;
                case "abr_cr_an_horse03":
                    cloakResRef = "acr_ooc_horse03";
                    isWarhorse[Character] = true;
                    break;
                default:
                    cloakResRef = "acr_ooc_horse03";
                    isWarhorse[Character] = true;
                    break;
            }
            
            uint horseCloak = script.CreateItemOnObject(cloakResRef, Character, 1, "", CLRScriptBase.FALSE);
            if (script.GetLocalInt(Horse, ACR_IS_WARHORSE) == 1)
            {
                script.RemoveHenchman(Character, Horse);
                script.SetLocalInt(horseCloak, ACR_IS_WARHORSE, 1);
            }
            script.SetLocalInt(horseCloak, ACR_HORSE_ID, script.GetLocalInt(Horse, ACR_HORSE_ID));
            script.SetLocalInt(horseCloak, ACR_HORSE_HP, script.GetCurrentHitPoints(Horse));

            uint equippedCloak = script.GetItemInSlot(CLRScriptBase.INVENTORY_SLOT_CLOAK, Character);
            
            if (script.GetIsObjectValid(equippedCloak) == CLRScriptBase.TRUE)
            {
                foreach (NWItemProperty prop in script.GetItemPropertiesOnItem(equippedCloak))
                {
                    // copying property duration type prevents us from turning temporary properties into
                    // permanent ones. But because we don't know how long the non-permanent ones have left,
                    // we pretty much have to assign them with the expectation that they immediately expire.
                    script.AddItemProperty(script.GetItemPropertyDurationType(prop), prop, horseCloak, 0.0f);
                }
                script.SetFirstName(horseCloak, script.GetName(equippedCloak) + "(( Horse Appearance ))");
                script.AddItemProperty(CLRScriptBase.DURATION_TYPE_PERMANENT, script.ItemPropertyWeightReduction(CLRScriptBase.IP_CONST_REDUCEDWEIGHT_80_PERCENT), horseCloak, 0.0f);
            }
            script.SetPlotFlag(horseCloak, CLRScriptBase.TRUE);
            script.SetPlotFlag(Horse, CLRScriptBase.FALSE);
            
            script.AssignCommand(Horse, delegate { script.SetIsDestroyable(CLRScriptBase.TRUE, CLRScriptBase.FALSE, CLRScriptBase.FALSE); });
            script.AssignCommand(Horse, delegate { script.DestroyObject(Horse, 0.0f, CLRScriptBase.FALSE); });
            script.AssignCommand(Character, delegate { script.ActionEquipItem(horseCloak, CLRScriptBase.INVENTORY_SLOT_CLOAK); });

            if (!isWarhorse[Character]) script.DelayCommand(6.0f, delegate { RidingHeartbeat(script, Character); });
        }
 public static void LoadAreas(CLRScriptBase s)
 {
     if (!ACR_Candlekeep.ArchivesInstance.WaitForResourcesLoaded(false))
     {
         s.DelayCommand(6.0f, delegate { LoadAreas(s); });
         return;
     }
     ALFA.Shared.Modules.InfoStore.ActiveAreas = new Dictionary<uint, ALFA.Shared.ActiveArea>();
     List<uint> areas = new List<uint>();
     foreach (uint area in s.GetAreas())
     {
         ALFA.Shared.ActiveArea activeArea = new ALFA.Shared.ActiveArea();
         activeArea.Id = area;
         activeArea.Name = s.GetName(area).Trim();
         activeArea.Tag = s.GetTag(area);
         activeArea.GlobalQuests.Add("Infestation", s.GetLocalInt(area, "ACR_QST_MAX_INFESTATION"));
         ALFA.Shared.Modules.InfoStore.ActiveAreas.Add(area, activeArea);
         areas.Add(area);
     }
     int count = 0;
     foreach(KeyValuePair<string, string> keyValue in ALFA.Shared.Modules.InfoStore.AreaNames)
     {
         ALFA.Shared.Modules.InfoStore.ActiveAreas[areas[count]].LocalizedName = keyValue.Value;
         ALFA.Shared.Modules.InfoStore.ActiveAreas[areas[count]].ConfigureDisplayName();
         s.SetLocalString(areas[count], "ACR_AREA_RESREF", keyValue.Key);
         count++;
     }
     foreach (ALFA.Shared.ActiveArea activeArea in ALFA.Shared.Modules.InfoStore.ActiveAreas.Values)
     {
         foreach (uint thing in s.GetObjectsInArea(activeArea.Id))
         {
             uint target = s.GetTransitionTarget(thing);
             if (s.GetIsObjectValid(target) != FALSE)
             {
                 ALFA.Shared.ActiveTransition activeTransition = new ALFA.Shared.ActiveTransition();
                 activeTransition.AreaTarget = ALFA.Shared.Modules.InfoStore.ActiveAreas[s.GetArea(target)];
                 activeTransition.Id = thing;
                 activeTransition.Target = target;
                 activeArea.ExitTransitions.Add(activeTransition, activeTransition.AreaTarget);
             }
         }
     }
 }
        public static void TrackPersistentChestValues(CLRScriptBase script, uint Character, uint Chest, int CountedLoot)
        {
            string tag = script.GetTag(Chest);
            if(!pChestValues.ContainsKey(tag))
            {
                pChestValues.Add(tag, CountedLoot);
            }
            else
            {
                pChestValues[tag] = CountedLoot;
            }

            // TODO: persistently save pChest tags
            if(!pChestAccess.ContainsKey(Character))
            {
                pChestAccess.Add(Character, new List<string>());
            }
            if(!pChestAccess[Character].Contains(tag))
            {
                pChestAccess[Character].Add(tag);
            }
        }
        public static int NewWand(CLRScriptBase script, int maxValue)
        {
            if (maxValue < 375)
            {
                return 0;
            }
            int wandValue = 0;
            uint scrollSource = OBJECT_INVALID;
            if (maxValue >= 21000)
            {
                switch (Generation.rand.Next(5))
                {
                    case 0:
                        wandNumber++;
                        scrollSource = script.CreateItemOnObject(GenerateScroll.Level4Scrolls[Generation.rand.Next(GenerateScroll.Level4Scrolls.Count)], script.OBJECT_SELF, 1, "soonwand" + wandNumber.ToString(), FALSE);
                        wandValue = 21000;
                        break;
                    case 1:
                        wandNumber++;
                        scrollSource = script.CreateItemOnObject(GenerateScroll.Level3Scrolls[Generation.rand.Next(GenerateScroll.Level3Scrolls.Count)], script.OBJECT_SELF, 1, "soonwand" + wandNumber.ToString(), FALSE);
                        wandValue = 11250;
                        break;
                    case 2:
                        wandNumber++;
                        scrollSource = script.CreateItemOnObject(GenerateScroll.Level2Scrolls[Generation.rand.Next(GenerateScroll.Level2Scrolls.Count)], script.OBJECT_SELF, 1, "soonwand" + wandNumber.ToString(), FALSE);
                        wandValue = 4500;
                        break;
                    case 3:
                        wandNumber++;
                        scrollSource = script.CreateItemOnObject(GenerateScroll.Level1Scrolls[Generation.rand.Next(GenerateScroll.Level1Scrolls.Count)], script.OBJECT_SELF, 1, "soonwand" + wandNumber.ToString(), FALSE);
                        wandValue = 750;
                        break;
                    case 4:
                        wandNumber++;
                        scrollSource = script.CreateItemOnObject(GenerateScroll.Level0Scrolls[Generation.rand.Next(GenerateScroll.Level0Scrolls.Count)], script.OBJECT_SELF, 1, "soonwand" + wandNumber.ToString(), FALSE);
                        wandValue = 375;
                        break;
                }
            }
            else if (maxValue >= 11250)
            {
                switch (Generation.rand.Next(4))
                {
                    case 0:
                        wandNumber++;
                        scrollSource = script.CreateItemOnObject(GenerateScroll.Level3Scrolls[Generation.rand.Next(GenerateScroll.Level3Scrolls.Count)], script.OBJECT_SELF, 1, "soonwand" + wandNumber.ToString(), FALSE);
                        wandValue = 11250;
                        break;
                    case 1:
                        wandNumber++;
                        scrollSource = script.CreateItemOnObject(GenerateScroll.Level2Scrolls[Generation.rand.Next(GenerateScroll.Level2Scrolls.Count)], script.OBJECT_SELF, 1, "soonwand" + wandNumber.ToString(), FALSE);
                        wandValue = 4500;
                        break;
                    case 2:
                        wandNumber++;
                        scrollSource = script.CreateItemOnObject(GenerateScroll.Level1Scrolls[Generation.rand.Next(GenerateScroll.Level1Scrolls.Count)], script.OBJECT_SELF, 1, "soonwand" + wandNumber.ToString(), FALSE);
                        wandValue = 750;
                        break;
                    case 3:
                        wandNumber++;
                        scrollSource = script.CreateItemOnObject(GenerateScroll.Level0Scrolls[Generation.rand.Next(GenerateScroll.Level0Scrolls.Count)], script.OBJECT_SELF, 1, "soonwand" + wandNumber.ToString(), FALSE);
                        wandValue = 375;
                        break;
                }
            }
            else if (maxValue >= 4500)
            {
                switch (Generation.rand.Next(3))
                {
                    case 0:
                        wandNumber++;
                        scrollSource = script.CreateItemOnObject(GenerateScroll.Level2Scrolls[Generation.rand.Next(GenerateScroll.Level2Scrolls.Count)], script.OBJECT_SELF, 1, "soonwand" + wandNumber.ToString(), FALSE);
                        wandValue = 4500;
                        break;
                    case 1:
                        wandNumber++;
                        scrollSource = script.CreateItemOnObject(GenerateScroll.Level1Scrolls[Generation.rand.Next(GenerateScroll.Level1Scrolls.Count)], script.OBJECT_SELF, 1, "soonwand" + wandNumber.ToString(), FALSE);
                        wandValue = 750;
                        break;
                    case 2:
                        wandNumber++;
                        scrollSource = script.CreateItemOnObject(GenerateScroll.Level0Scrolls[Generation.rand.Next(GenerateScroll.Level0Scrolls.Count)], script.OBJECT_SELF, 1, "soonwand" + wandNumber.ToString(), FALSE);
                        wandValue = 375;
                        break;
                }
            }
            else if (maxValue >= 750)
            {
                switch (Generation.rand.Next(2))
                {
                    case 0:
                        wandNumber++;
                        scrollSource = script.CreateItemOnObject(GenerateScroll.Level1Scrolls[Generation.rand.Next(GenerateScroll.Level1Scrolls.Count)], script.OBJECT_SELF, 1, "soonwand" + wandNumber.ToString(), FALSE);
                        wandValue = 750;
                        break;
                    case 1:
                        wandNumber++;
                        scrollSource = script.CreateItemOnObject(GenerateScroll.Level0Scrolls[Generation.rand.Next(GenerateScroll.Level0Scrolls.Count)], script.OBJECT_SELF, 1, "soonwand" + wandNumber.ToString(), FALSE);
                        wandValue = 375;
                        break;
                }
            }
            else if (maxValue >= 375)
            {
                wandNumber++;
                scrollSource = script.CreateItemOnObject(GenerateScroll.Level0Scrolls[Generation.rand.Next(GenerateScroll.Level0Scrolls.Count)], script.OBJECT_SELF, 1, "soonwand" + wandNumber.ToString(), FALSE);
                wandValue = 375;
            }

            uint wandItem = script.CreateItemOnObject(wandResRef, script.OBJECT_SELF, 1, script.GetTag(scrollSource) + wandNumber.ToString(), FALSE);
            script.SetItemCharges(wandItem, 50);
            string wandName = script.GetFirstName(scrollSource);
            wandName.Replace("Scroll", "Wand");
            script.SetFirstName(wandItem, wandName);
            foreach (NWItemProperty prop in script.GetItemPropertiesOnItem(scrollSource))
            {
                if (script.GetItemPropertyType(prop) == ITEM_PROPERTY_CAST_SPELL)
                {
                    script.AddItemProperty(DURATION_TYPE_PERMANENT, script.ItemPropertyCastSpell(script.GetItemPropertySubType(prop), IP_CONST_CASTSPELL_NUMUSES_1_CHARGE_PER_USE), wandItem, 0.0f);
                }
                else if (script.GetItemPropertyType(prop) == ITEM_PROPERTY_USE_LIMITATION_CLASS)
                {
                    script.AddItemProperty(DURATION_TYPE_PERMANENT, script.ItemPropertyLimitUseByClass(script.GetItemPropertySubType(prop)), wandItem, 0.0f);
                }
            }
            script.DestroyObject(scrollSource, 0.0f, FALSE);
            return wandValue;
        }
 private void PopulateArea(CLRScriptBase script)
 {
     List<Vector3> trapsToSpawn = new List<Vector3>();
     if (!DungeonStore.DungeonSpawns.ContainsKey(SpawnType)) return;
     foreach(uint wp in script.GetObjectsInArea(AreaId))
     {
         if(script.GetTag(wp) == "MONSTER_LOW")
         {
             if (DungeonStore.DungeonSpawns[SpawnType].ContainsKey(CR / 3))
             {
                 script.CreateObject(CLRScriptBase.OBJECT_TYPE_CREATURE, DungeonStore.DungeonSpawns[SpawnType][CR/3][rand.Next(DungeonStore.DungeonSpawns[SpawnType][CR/3].Count)], script.GetLocation(wp), CLRScriptBase.TRUE, "");
             }
         }
         else if(script.GetTag(wp) == "MONSTER_MED")
         {
             if (DungeonStore.DungeonSpawns[SpawnType].ContainsKey(CR / 2))
             {
                 script.CreateObject(CLRScriptBase.OBJECT_TYPE_CREATURE, DungeonStore.DungeonSpawns[SpawnType][CR/2][rand.Next(DungeonStore.DungeonSpawns[SpawnType][CR/2].Count)], script.GetLocation(wp), CLRScriptBase.TRUE, "");
             }
         }
         else if(script.GetTag(wp) == "MONSTER_HIGH")
         {
             
             if (DungeonStore.DungeonSpawns[SpawnType].ContainsKey(CR))
             {
                 script.CreateObject(CLRScriptBase.OBJECT_TYPE_CREATURE, DungeonStore.DungeonSpawns[SpawnType][CR][rand.Next(DungeonStore.DungeonSpawns[SpawnType][CR].Count)], script.GetLocation(wp), CLRScriptBase.TRUE, "");
             }
             if (!questSpawned)
             {
                 questSpawned = true;
                 NWEffect AoE = script.EffectAreaOfEffect(86, "acf_trg_onenter", "acf_trg_onheartbeat", "acf_trg_onexit", "AOE"+Quest);
                 script.ApplyEffectAtLocation(CLRScriptBase.DURATION_TYPE_PERMANENT, script.SupernaturalEffect(AoE), script.GetLocation(wp), 0.0f);
                 uint spawnedAoE = script.GetObjectByTag("AOE" + Quest, 0);
                 script.SetLocalString(spawnedAoE, "ACR_QST_NAME", Quest);
                 script.SetLocalInt(spawnedAoE, "ACR_QST_LOWER_STATE", 1);
                 script.SetLocalInt(spawnedAoE, "ACR_QST_UPPER_STATE", 2);
                 script.SetLocalString(spawnedAoE, "ACR_QST_MESSAGE", "This appears to be the end of the dungeon, and your path here is scouted.");
             }
         }
         else if (script.GetTag(wp) == "TRAP")
         { 
             if(DungeonStore.DungeonTraps[TrapType].ContainsKey(CR))
             {
                 trapsToSpawn.Add(script.GetPosition(wp));
             }
         }
     }
     foreach (Vector3 trap in trapsToSpawn)
     {
         script.ClearScriptParams();
         script.AddScriptParameterInt(1);
         script.AddScriptParameterFloat(trap.x);
         script.AddScriptParameterFloat(trap.y);
         script.AddScriptParameterFloat(trap.z);
         script.AddScriptParameterObject(AreaId);
         script.AddScriptParameterInt(-1);
         script.AddScriptParameterInt(-1);
         script.AddScriptParameterFloat(-1.0f);
         script.AddScriptParameterInt(-1);
         script.AddScriptParameterInt(-1);
         script.AddScriptParameterInt(-1);
         script.AddScriptParameterInt(-1);
         script.AddScriptParameterInt(-1);
         script.AddScriptParameterInt(-1);
         script.AddScriptParameterObject(CLRScriptBase.OBJECT_INVALID);
         script.AddScriptParameterInt(-1);
         script.AddScriptParameterInt(-1);
         script.AddScriptParameterInt(-1);
         script.AddScriptParameterInt(-1);
         script.AddScriptParameterInt(-1);
         script.AddScriptParameterString(DungeonStore.DungeonTraps[TrapType][CR][rand.Next(DungeonStore.DungeonTraps[TrapType][CR].Count)]);
         script.ExecuteScriptEnhanced("ACR_Traps", script.GetModule(), CLRScriptBase.FALSE);
     }
 }
        public void TransitionToArea(CLRScriptBase script, ExitDirection exit)
        {
            string doorTag = "DOOR_NORTH";
            switch(exit)
            {
                case ExitDirection.North:
                    doorTag = "DOOR_NORTH";
                    break;
                case ExitDirection.East:
                    doorTag = "DOOR_EAST";
                    break;
                case ExitDirection.South:
                    doorTag = "DOOR_SOUTH";
                    break;
                case ExitDirection.West:
                    doorTag = "DOOR_WEST";
                    break;
                case ExitDirection.Up:
                    doorTag = "DOOR_UP";
                    break;
                case ExitDirection.Down:
                    doorTag = "DOOR_DOWN";
                    break;
            }
            if(script.GetIsObjectValid(script.GetLocalObject(AreaId, doorTag)) == CLRScriptBase.TRUE)
            {
                uint door = script.GetLocalObject(AreaId, doorTag);
                script.JumpToObject(door, CLRScriptBase.TRUE);
                return;
            }

            foreach(uint obj in script.GetObjectsInArea(AreaId))
            {
                if(script.GetTag(obj) == doorTag)
                {
                    script.SetLocalObject(AreaId, doorTag, obj);
                    script.JumpToObject(obj, CLRScriptBase.TRUE);
                    return;
                }
            }
            script.SendMessageToPC(script.OBJECT_SELF, "I can't find a door to jump you to.");
        }
        public static uint _SpawnObject(string sResRef, int nObjectType, uint oWP, NWLocation lLoc, int nFlags, int nAlternate, CLRScriptBase s)
        {
            // if the object is not being spawned at it's waypoint location, we need to make sure
            // the actual spawn-in location isn't violating the "in PC sight" guidelines.
            if ((nAlternate != CLRScriptBase.FALSE) && ((nFlags & _SPAWN_FLAGS_IN_PC_SIGHT) == 0))
            {
                uint oNeighbor = s.GetNearestCreatureToLocation(CLRScriptBase.CREATURE_TYPE_PLAYER_CHAR, CLRScriptBase.PLAYER_CHAR_IS_PC, lLoc, 1, -1, -1, -1, -1);
                if (s.GetIsObjectValid(oNeighbor) == CLRScriptBase.FALSE && (s.GetDistanceBetweenLocations(lLoc, s.GetLocation(oNeighbor)) <= PC_PERCEPTION_RANGE))
                { // ACR_GetPCVisualRange() )) {
                    return CLRScriptBase.OBJECT_INVALID;
                }
                if (GetPrespawnPrediction(s) == CLRScriptBase.FALSE)
                {
                    uint oTestWP = s.GetNearestObjectToLocation(CLRScriptBase.OBJECT_TYPE_WAYPOINT, lLoc, 1);
                    int nWP_Index = 1;
                    while ((oTestWP != CLRScriptBase.OBJECT_INVALID) && (s.GetDistanceBetweenLocations(lLoc, s.GetLocation(oTestWP)) <= PC_PERCEPTION_RANGE))
                    { // ACR_GetPC_VisualRange() )) {
                        if (s.GetTag(oTestWP) == "ACR_SA_WP")
                        {
                            return CLRScriptBase.OBJECT_INVALID;
                        }
                        else
                        {
                            nWP_Index = nWP_Index + 1;
                            oTestWP = s.GetNearestObjectToLocation(CLRScriptBase.OBJECT_TYPE_WAYPOINT, lLoc, nWP_Index);
                        }
                    }
                }
            }

            uint oSpawned = s.CreateObject(nObjectType, sResRef, lLoc, nFlags & _SPAWN_FLAGS_WITH_ANIMATION, "");

            // Check to make sure it spawned ok, print an error and exit if not.
            if (s.GetIsObjectValid(oSpawned) == CLRScriptBase.FALSE)
            {
                return CLRScriptBase.OBJECT_INVALID;
            }

            // If it should be in stealth mode, place it there.
            if ((nFlags & _SPAWN_IN_STEALTH) == _SPAWN_IN_STEALTH)
            {
                s.SetActionMode(oSpawned, CLRScriptBase.ACTION_MODE_STEALTH, 1);
            }

            // If it should be in detect mode, place it there.
            if ((nFlags & _SPAWN_IN_DETECT) == _SPAWN_IN_DETECT)
            {
                s.SetActionMode(oSpawned, CLRScriptBase.ACTION_MODE_DETECT, 1);
            }

            // If this creature should buff himself, do it.
            if ((nFlags & _SPAWN_BUFFED) == _SPAWN_BUFFED)
            {
                ActivateLongTermBuffs(oSpawned, s);
            }

            // Play the spawn animation.
            s.PlayAnimation(s.GetLocalInt(oWP, _WP_SPAWN_ANIMATION), 1.0f, 0.0f);

            // Play the spawn in VFX.
            s.ApplyEffectAtLocation(CLRScriptBase.DURATION_TYPE_INSTANT, s.EffectVisualEffect(s.GetLocalInt(oWP, _WP_SPAWN_IN_VFX), CLRScriptBase.FALSE), s.GetLocation(oSpawned), 0.0f);

            // Play the spawn in SFX.
            s.AssignCommand(oSpawned, delegate { s.PlaySound(s.GetLocalString(oWP, _WP_SPAWN_IN_SFX), CLRScriptBase.FALSE); });

            // Determine facing.
            if ((nFlags & _SPAWN_FLAGS_RANDOM_FACING) == _SPAWN_FLAGS_RANDOM_FACING)
            {
                // Spawn facing is random.
                s.AssignCommand(oSpawned, delegate { s.SetFacing(new Random().Next() * 360.0f, CLRScriptBase.FALSE); });
            }

            // Colorize name if needed.
            if (s.GetLocalString(oSpawned, ACR_COLOR_NAME) != "")
            {
                s.SetFirstName(oSpawned, "<C='" + s.GetLocalString(oSpawned, ACR_COLOR_NAME) + "'>" + s.GetName(oSpawned) + "</C>");
                s.SetLastName(oSpawned, "");
            }

            // Run the spawn-in scripts, if any.
            int i = 1;
            while (true)
            {
                string sScript = s.GetLocalString(oWP, _WP_SPAWN_IN_SCRIPT_ARRAY + s.IntToString(i));
                if (sScript == "")
                {
                    break;
                }
                s.ExecuteScript(sScript, oSpawned);
                i++;
            }

            _AddObjectToSpawnPoint(oWP, oSpawned, s);

            return oSpawned;
        }
        public static void DrawObjects(CLRScriptBase script, User currentUser, uint currentArea)
        {
            float fDelay = 0.1f;
            List<uint> ChooserAoEs = new List<uint>();
            List<uint> ChooserCreatures = new List<uint>();
            List<uint> ChooserDoors = new List<uint>();
            List<uint> ChooserItems = new List<uint>();
            List<uint> ChooserLights = new List<uint>();
            List<uint> ChooserPlaceables = new List<uint>();
            List<uint> ChooserPlacedEffects = new List<uint>();
            List<uint> ChooserStores = new List<uint>();
            List<uint> ChooserTriggers = new List<uint>();
            List<uint> ChooserWaypoints = new List<uint>();
            script.DelayCommand(fDelay, delegate
            {
                foreach (uint thing in script.GetObjectsInArea(currentArea))
                {
                    int objectType = script.GetObjectType(thing);
                    switch (objectType)
                    {
                        case OBJECT_TYPE_AREA_OF_EFFECT:
                            ChooserAoEs.Add(thing);
                            break;
                        case OBJECT_TYPE_CREATURE:
                            ChooserCreatures.Add(thing);
                            break;
                        case OBJECT_TYPE_DOOR:
                            ChooserDoors.Add(thing);
                            break;
                        case OBJECT_TYPE_ITEM:
                            ChooserItems.Add(thing);
                            break;
                        case OBJECT_TYPE_LIGHT:
                            ChooserLights.Add(thing);
                            break;
                        case OBJECT_TYPE_PLACEABLE:
                            ChooserPlaceables.Add(thing);
                            break;
                        case OBJECT_TYPE_PLACED_EFFECT:
                            ChooserPlacedEffects.Add(thing);
                            break;
                        case OBJECT_TYPE_STORE:
                            ChooserStores.Add(thing);
                            break;
                        case OBJECT_TYPE_TRIGGER:
                            ChooserTriggers.Add(thing);
                            break;
                        case OBJECT_TYPE_WAYPOINT:
                            ChooserWaypoints.Add(thing);
                            break;
                    }
                }
                script.ClearListBox(currentUser.Id, "SCREEN_DMC_CHOOSER", "LISTBOX_ACR_CHOOSER_OBJECTS");
                if (ChooserAoEs.Count > 0 && currentUser.ChooserShowAOE)
                {
                    fDelay += 0.1f;
                    script.DelayCommand(fDelay, delegate
                    {
                        foreach (uint thing in ChooserAoEs)
                        {
                            script.AddListBoxRow(currentUser.Id, "SCREEN_DMC_CHOOSER", "LISTBOX_ACR_CHOOSER_OBJECTS", thing.ToString(), "LISTBOX_ITEM_TEXT=  " + script.GetTag(thing), "LISTBOX_ITEM_ICON=trap.tga", "5="+thing.ToString(), "");
                        }
                    });
                }
                if (ChooserCreatures.Count > 0 && currentUser.ChooserShowCreature)
                {
                    fDelay += 0.1f;
                    script.DelayCommand(fDelay, delegate
                    {
                        foreach (uint thing in ChooserCreatures)
                        {
                            script.AddListBoxRow(currentUser.Id, "SCREEN_DMC_CHOOSER", "LISTBOX_ACR_CHOOSER_OBJECTS", thing.ToString(), "LISTBOX_ITEM_TEXT=  " + script.GetName(thing), "LISTBOX_ITEM_ICON=creature.tga", "5=" + thing.ToString(), "");
                        }
                    });
                }
                if (ChooserDoors.Count > 0 && currentUser.ChooserShowDoor)
                {
                    fDelay += 0.1f;
                    script.DelayCommand(fDelay, delegate
                    {
                        foreach (uint thing in ChooserDoors)
                        {
                            script.AddListBoxRow(currentUser.Id, "SCREEN_DMC_CHOOSER", "LISTBOX_ACR_CHOOSER_OBJECTS", thing.ToString(), "LISTBOX_ITEM_TEXT=  " + script.GetName(thing), "LISTBOX_ITEM_ICON=door.tga", "5=" + thing.ToString(), "");
                        }
                    });
                }
                if (ChooserItems.Count > 0 && currentUser.ChooserShowItem)
                {
                    fDelay += 0.1f;
                    script.DelayCommand(fDelay, delegate
                    {
                        foreach (uint thing in ChooserItems)
                        {
                            script.AddListBoxRow(currentUser.Id, "SCREEN_DMC_CHOOSER", "LISTBOX_ACR_CHOOSER_OBJECTS", thing.ToString(), "LISTBOX_ITEM_TEXT=  " + script.GetName(thing), "LISTBOX_ITEM_ICON=item.tga", "5=" + thing.ToString(), "");
                        }
                    });
                }
                if (ChooserLights.Count > 0 && currentUser.ChooserShowLight)
                {
                    fDelay += 0.1f;
                    script.DelayCommand(fDelay, delegate
                    {
                        foreach (uint thing in ChooserLights)
                        {
                            script.AddListBoxRow(currentUser.Id, "SCREEN_DMC_CHOOSER", "LISTBOX_ACR_CHOOSER_OBJECTS", thing.ToString(), "LISTBOX_ITEM_TEXT=  " + script.GetName(thing) + ";LISTBOX_ITEM_TEXT2= Light", "LISTBOX_ITEM_ICON=light.tga", "5=" + thing.ToString(), "");
                        }
                    });
                }
                if (ChooserPlaceables.Count > 0 && currentUser.ChooserShowPlaceable)
                {
                    fDelay += 0.1f;
                    script.DelayCommand(fDelay, delegate
                    {
                        foreach (uint thing in ChooserPlaceables)
                        {

                            script.AddListBoxRow(currentUser.Id, "SCREEN_DMC_CHOOSER", "LISTBOX_ACR_CHOOSER_OBJECTS", thing.ToString(), "LISTBOX_ITEM_TEXT=  " + script.GetName(thing) + ";LISTBOX_ITEM_TEXT2= Placeable", "LISTBOX_ITEM_ICON=placeable.tga", "5=" + thing.ToString(), "");
                        }
                    });
                }
                if (ChooserPlacedEffects.Count > 0 && currentUser.ChooserShowPlacedEffect)
                {
                    fDelay += 0.1f;
                    script.DelayCommand(fDelay, delegate
                    {
                        foreach (uint thing in ChooserPlacedEffects)
                        {
                            script.AddListBoxRow(currentUser.Id, "SCREEN_DMC_CHOOSER", "LISTBOX_ACR_CHOOSER_OBJECTS", thing.ToString(), "LISTBOX_ITEM_TEXT=  " + script.GetName(thing) + ";LISTBOX_ITEM_TEXT2= Placed Effect", "LISTBOX_ITEM_ICON=vfx.tga", "5=" + thing.ToString(), "");
                        }
                    });
                }
                if (ChooserStores.Count > 0 && currentUser.ChooserShowStore)
                {
                    fDelay += 0.1f;
                    script.DelayCommand(fDelay, delegate
                    {
                        foreach (uint thing in ChooserStores)
                        {
                            script.AddListBoxRow(currentUser.Id, "SCREEN_DMC_CHOOSER", "LISTBOX_ACR_CHOOSER_OBJECTS", thing.ToString(), "LISTBOX_ITEM_TEXT=  " + script.GetName(thing) + ";LISTBOX_ITEM_TEXT2= Store", "LISTBOX_ITEM_ICON=store.tga", "5=" + thing.ToString(), "");
                        }
                    });
                }
                if (ChooserTriggers.Count > 0 && currentUser.ChooserShowTrigger)
                {
                    fDelay += 0.1f;
                    script.DelayCommand(fDelay, delegate
                    {
                        foreach (uint thing in ChooserTriggers)
                        {
                            script.AddListBoxRow(currentUser.Id, "SCREEN_DMC_CHOOSER", "LISTBOX_ACR_CHOOSER_OBJECTS", thing.ToString(), "LISTBOX_ITEM_TEXT=  " + script.GetName(thing) + ";LISTBOX_ITEM_TEXT2= Trigger", "LISTBOX_ITEM_ICON=trigger.tga", "5=" + thing.ToString(), "");
                        }
                    });
                }
                if (ChooserWaypoints.Count > 0 && currentUser.ChooserShowWaypoint)
                {
                    fDelay += 0.1f;
                    script.DelayCommand(fDelay, delegate
                    {
                        foreach (uint thing in ChooserWaypoints)
                        {
                            script.AddListBoxRow(currentUser.Id, "SCREEN_DMC_CHOOSER", "LISTBOX_ACR_CHOOSER_OBJECTS", thing.ToString(), "LISTBOX_ITEM_TEXT=  " + script.GetName(thing) + ";LISTBOX_ITEM_TEXT2=  Waypoint", "LISTBOX_ITEM_ICON=waypoint.tga", "5=" + thing.ToString(), "");
                        }
                    });
                }
            });
        }
 private static uint GetNearestTrapEmitter(CLRScriptBase script, NWLocation loc)
 {
     float nearestDist = -1.0f;
     uint nearestObject = CLRScriptBase.OBJECT_INVALID;
     Vector3 trapPos = script.GetPositionFromLocation(loc);
     foreach(uint obj in script.GetObjectsInArea(script.GetAreaFromLocation(loc)))
     {
         if(script.GetObjectType(obj) == OBJECT_TYPE_PLACEABLE &&
            (script.GetTag(obj) == "TRAP_EMITTER" ||
             script.GetTag(obj) == "TRAP_ORIGIN"))
         {
             Vector3 emitterPos = script.GetPosition(obj);
             float newDist = (trapPos.x - emitterPos.x)*(trapPos.x - emitterPos.x)+(trapPos.y - emitterPos.y)*(trapPos.y - emitterPos.y);
             if(nearestDist < 0 || nearestDist > newDist)
             {
                 nearestDist = newDist;
                 nearestObject = obj;
             }
         }
     }
     if(script.GetIsObjectValid(nearestObject) == CLRScriptBase.FALSE || nearestDist > 400.0f) // 20 meters; nearestDist is squared to be cheaper
     {
         nearestObject = CLRScriptBase.OBJECT_INVALID;
     }
     return nearestObject;
 }
        public static void Dismount(CLRScriptBase script, uint Character, uint Cloak, NWLocation Location)
        {
            string resRef = "";
            if (script.GetLocalInt(Cloak, ACR_IS_WARHORSE) == 1)
            {
                resRef = "abr_cr_an_horse_pal_";
                int nPalLevel = script.GetLevelByClass(CLRScriptBase.CLASS_TYPE_PALADIN, Character);
                if (nPalLevel >= 15) resRef += "15";
                else if (nPalLevel >= 11) resRef += "11";
                else if (nPalLevel >= 8) resRef += "8";
                else if (nPalLevel >= 5) resRef += "5";
                else resRef = "abr_cr_an_horse03";
            }
            else
            {
                switch (script.GetTag(Cloak))
                {
                    case "acr_ooc_horse01":
                        resRef = "abr_cr_an_horse01";
                        break;
                    case "acr_ooc_horse02":
                        resRef = "abr_cr_an_horse02";
                        break;
                    case "acr_ooc_horse03":
                        resRef = "abr_cr_an_horse03";
                        break;
                    default:
                        // Looks like we're not actually dismounting a horse.
                        return;
                }
            }
            
            uint Horse = script.CreateObject(CLRScriptBase.OBJECT_TYPE_CREATURE, resRef, Location, CLRScriptBase.FALSE, "");
            script.SetLocalInt(Horse, ACR_HORSE_OWNER, script.GetLocalInt(Character, ACR_CID));
            script.SetLocalInt(Horse, ACR_HORSE_ID, script.GetLocalInt(Cloak, ACR_HORSE_ID));
            int damage = script.GetCurrentHitPoints(Horse) - script.GetLocalInt(Cloak, ACR_HORSE_HP);
            if(damage > 0)
            {
                script.ApplyEffectToObject(CLRScriptBase.DURATION_TYPE_INSTANT, script.EffectDamage(damage, CLRScriptBase.DAMAGE_TYPE_MAGICAL, CLRScriptBase.DAMAGE_POWER_PLUS_TWENTY, CLRScriptBase.TRUE), Horse, 0.0f);
            }
            if (script.GetLocalInt(Cloak, ACR_IS_WARHORSE) == 1)
            {
                script.AddHenchman(Character, Horse);
                script.SetLocalInt(Horse, ACR_IS_WARHORSE, 1);
                script.SetLocalObject(Character, ACR_PAL_WARHORSE, Horse);
            }

            uint Item = GetOwnershipItemById(script, Character, script.GetLocalInt(Cloak, ACR_HORSE_ID));
            script.SetLocalObject(Item , ACR_HORSE_OBJECT, Horse);
            script.SetLocalObject(Horse, ACR_HORSE_OBJECT, Character);

            script.SetLocalString(Item, ACR_HORSE_PERS_LOC_AREA, script.GetTag(script.GetArea(Horse)));
            script.SetLocalFloat(Item, ACR_HORSE_PERS_LOC_X, script.GetPosition(Horse).x);
            script.SetLocalFloat(Item, ACR_HORSE_PERS_LOC_Y, script.GetPosition(Horse).y);
            script.SetLocalFloat(Item, ACR_HORSE_PERS_LOC_Z, script.GetPosition(Horse).z);

            script.SetPlotFlag(Cloak, CLRScriptBase.FALSE);
            script.DestroyObject(Cloak, 0.0f, CLRScriptBase.FALSE);
            isWarhorse.Remove(Character);
        }