#include "nw_i0_plot"

void main()
{
    object oBox = OBJECT_SELF;
    object oPC = GetLastUsedBy();
    string sOwner = GetLocalString(oBox, "OWNER");
    string sName = GetName(oPC);

    if(GetLocked(oBox)
       && !GetLocalInt(oBox, "JUST_CLOSED")
       && HasItem(oPC, sOwner+"_key"))
    {
        FloatingTextStringOnCreature("*you have the key*", oPC);
        SetLocked(oBox, FALSE);
        AssignCommand(oPC, ActionInteractObject(oBox));
    }
    else if (!GetCampaignInt("PW_"+sOwner+"_CASSA", "HAS_KEY")
             && !GetLocalInt(oBox, "OWNER_ONLY")
             && !GetLocalInt(oBox, "JUST_OPEN"))
    {
        FloatingTextStringOnCreature("*open*", oPC);
        SetLocked(oBox, FALSE);
        AssignCommand(oPC, ActionInteractObject(oBox));
    }
    else if(GetLocalInt(oBox, "OWNER_ONLY")
            && TestStringAgainstPattern("**"+sOwner+"**", sName)
            && !GetLocalInt(oBox, "JUST_OPEN"))
    {
        FloatingTextStringOnCreature("*only you can open this chest*", oPC);
        SetLocked(oBox, FALSE);
        AssignCommand(oPC, ActionInteractObject(oBox));
    }
    else
    {
        SetLocalInt(oBox, "JUST_CLOSED", 0);
        SetLocalInt(oBox, "JUST_OPEN", 0);
    }
}