private static void ProcessCreatureAI(ref HashSet <NWCreature> creatures) { // Iterate backwards so we can remove the creature if it's no longer valid. for (int x = creatures.Count - 1; x >= 0; x--) { NWCreature creature = creatures.ElementAt(x); NWArea area = creature.Area; bool areaHasPCs = NWModule.Get().Players.Count(p => p.Area.Resref == area.Resref) > 0; // Is this creature invalid or dead? If so, remove it and move to the next one. if (!creature.IsValid || creature.IsDead) { creatures.Remove(creature); continue; } // Are there no players in the area? Is the creature being possessed? If so, don't execute AI this frame. Move to the next one. if (creature.IsPossessedFamiliar || creature.IsDMPossessed || !areaHasPCs) { continue; } string script = GetBehaviourScript(creature); if (string.IsNullOrWhiteSpace(script)) { continue; } IAIBehaviour behaviour = GetAIBehaviour(script); behaviour.OnProcessObject(creature); } }