/* Area based effect example.
 * Author: Philip Jones <pjones@firepool.net>
 *
 * There aren't many effects for which this would be usefull, so only two are
 * currently implemented.
 * Effects placed with this script are super-natural, and there for can not be
 * removed with spells/potions or resting.
 *
 * Bugs:
 *  -   Other effect icons next to the players portrait seem to dissapear when
 *      removing the effects, though this doesn't seem to affect the effects
 *      themselves. For example, wearing the boots of speed my hasted icon
 *      dissapeared, though the character sheet still listed hasted and immunity
 *      to slow and speed decrease et cetra.
 */

int AFX_FLAG_DARK = 0x0001;
int AFX_FLAG_SPEEDDECREASE = 0x0002; // Thick fog?

void afx_debug(string sMessage) {
     string sMessage = ("The supernatural darkness here nearly chokes you.");
     SendMessageToPC(GetFirstPC(), sMessage);

     PrintString(sMessage);
}

void afx_apply_effects(object oCreature, object oArea) {
//    afx_debug("afx_apply_effects: Entered.");
    if(oCreature == OBJECT_INVALID) {
//        afx_debug("afx_apply_effects: Creature object is invalid, returning.");
        return;
    }
    if(oArea == OBJECT_INVALID) {
        oArea = GetArea(oCreature);
        if(oArea == OBJECT_INVALID) {
//            afx_debug("afx_apply_effects: Area object is invalid, returning.");
            return;
        }
    }
    if(GetObjectType(oCreature) == OBJECT_TYPE_CREATURE) {
//        afx_debug("afx_apply_effects: Object is a creature.");
        int iAFXFlags = GetLocalInt(oArea,"afx_flags");
        if(iAFXFlags & AFX_FLAG_DARK) {
//            afx_debug("afx_apply_effects: Applying area effect darkness.");
            effect eDark = SupernaturalEffect(EffectDarkness());
//            object oDarkCreator = GetEffectCreator(eDark);
//            afx_debug("afx_apply_effects: GetEffectCreator(eDark): "+GetTag(oDarkCreator)+" (0x"+ObjectToString(oDarkCreator)+")");
            ApplyEffectToObject(DURATION_TYPE_PERMANENT,eDark,oCreature,0.0);
        }
        if(iAFXFlags & AFX_FLAG_SPEEDDECREASE) {
            int iSpeedDecrease = GetLocalInt(oArea,"afx_sd_per");
            if(iSpeedDecrease < 1) iSpeedDecrease = 50;
//            afx_debug("afx_apply_effects: Applying area effect speed decrease " + IntToString(iSpeedDecrease) + "%.");
            ApplyEffectToObject(DURATION_TYPE_PERMANENT,EffectMovementSpeedDecrease(iSpeedDecrease),oCreature,0.0);
        }
    }
//    afx_debug("afx_apply_effects: Leaving.");
}

void afx_remove_effects(object oCreature, object oArea) {
//    afx_debug("afx_remove_effects: Entered.");
    if(oCreature == OBJECT_INVALID) {
//        afx_debug("afx_remove_effects: Creature object is invalid, returning.");
        return;
    }
    if(oArea == OBJECT_INVALID) {
        oArea = GetArea(oCreature);
        if(oArea == OBJECT_INVALID) {
//            afx_debug("afx_remove_effects: Area object is invalid, returning.");
            return;
        }
    }
    if(GetObjectType(oCreature) == OBJECT_TYPE_CREATURE) {
//        afx_debug("afx_remove_effects: Object is a creature.");
//        afx_debug("afx_remove_effects: Area: "+GetTag(oArea)+" (0x"+ObjectToString(oArea)+")");
        effect eEffect = GetFirstEffect(oCreature);
        while(GetIsEffectValid(eEffect)) {
            object oEffectCreator = GetEffectCreator(eEffect);
//            afx_debug("afx_remove_effects: Effect '"+IntToString(GetEffectType(eEffect))+"' created by: "+GetTag(oEffectCreator)+" (0x"+ObjectToString(oEffectCreator)+")");
//            if((oEffectCreator == oArea || oEffectCreator == GetModule()) && GetEffectSubType(eEffect) == SUBTYPE_SUPERNATURAL) {
            if(oEffectCreator == oArea && GetEffectSubType(eEffect) == SUBTYPE_SUPERNATURAL) {
//                afx_debug("afx_remove_effects: Effect added by area or module, removing.");
                RemoveEffect(oCreature,eEffect);
            }
            eEffect = GetNextEffect(oCreature);
        }
    }
//    afx_debug("afx_remove_effects: Leaving.");
}