#include "nw_i0_plot"

void GiveToMerchant(object oMerchant, object oItem)
{
    if(d2())
    {
    string oItemResRef = GetResRef(oItem);
    CreateItemOnObject(oItemResRef, oMerchant);
    }
}


void main()
{
//get the entering object that triggered the event
object oPC = GetEnteringObject();
object oItem = GetFirstItemInInventory(oPC);
object oMerchant = GetObjectByTag("etumstoreouter");
AssignCommand(oPC, ClearAllActions(TRUE));

//cycle through each item in the inventory, destroying each one
   do
   {
       GiveToMerchant(oMerchant, oItem);
       DestroyObject(oItem);
       oItem = GetNextItemInInventory(oPC);
   } while(oItem != OBJECT_INVALID); // until none are left

//get each item in the players equipment slots and destroy them
oItem = GetItemInSlot(INVENTORY_SLOT_ARMS, oPC);
GiveToMerchant(oMerchant, oItem);
  DestroyObject(oItem);
oItem = GetItemInSlot(INVENTORY_SLOT_ARROWS, oPC);
GiveToMerchant(oMerchant, oItem);
  DestroyObject(oItem);
oItem = GetItemInSlot(INVENTORY_SLOT_BELT, oPC);
GiveToMerchant(oMerchant, oItem);
  DestroyObject(oItem);
oItem = GetItemInSlot(INVENTORY_SLOT_BOLTS, oPC);
GiveToMerchant(oMerchant, oItem);
  DestroyObject(oItem);
oItem = GetItemInSlot(INVENTORY_SLOT_BOOTS, oPC);
GiveToMerchant(oMerchant, oItem);
  DestroyObject(oItem);
oItem = GetItemInSlot(INVENTORY_SLOT_BULLETS, oPC);
GiveToMerchant(oMerchant, oItem);
  DestroyObject(oItem);
oItem = GetItemInSlot(INVENTORY_SLOT_CHEST, oPC);
GiveToMerchant(oMerchant, oItem);
  DestroyObject(oItem);
oItem = GetItemInSlot(INVENTORY_SLOT_CLOAK, oPC);
GiveToMerchant(oMerchant, oItem);
  DestroyObject(oItem);
oItem = GetItemInSlot(INVENTORY_SLOT_HEAD, oPC);
GiveToMerchant(oMerchant, oItem);
  DestroyObject(oItem);
oItem = GetItemInSlot(INVENTORY_SLOT_LEFTHAND, oPC);
GiveToMerchant(oMerchant, oItem);
  DestroyObject(oItem);
oItem = GetItemInSlot(INVENTORY_SLOT_LEFTRING, oPC);
GiveToMerchant(oMerchant, oItem);
  DestroyObject(oItem);
oItem = GetItemInSlot(INVENTORY_SLOT_NECK, oPC);
GiveToMerchant(oMerchant, oItem);
  DestroyObject(oItem);
oItem = GetItemInSlot(INVENTORY_SLOT_RIGHTHAND, oPC);
GiveToMerchant(oMerchant, oItem);
  DestroyObject(oItem);
oItem = GetItemInSlot(INVENTORY_SLOT_RIGHTRING, oPC);
GiveToMerchant(oMerchant, oItem);
  DestroyObject(oItem);

//finally take all the players money and destroy it.
int oGold = GetGold(oPC);
AssignCommand(oPC, TakeGoldFromCreature(oGold,oPC,TRUE));
GiveGoldToCreature(oPC, 100);
CreateItemOnObject("prisonoutfit", oPC, 1);
object oOutfit = GetObjectByTag("RHUN_PRISOUTFT");
AssignCommand(oPC, ClearAllActions(TRUE));
DelayCommand(2.0, AssignCommand(oPC, ActionEquipItem(oOutfit, INVENTORY_SLOT_CHEST)));
DelayCommand(1200.0, AssignCommand(oPC, JumpToObject(GetWaypointByTag("FREE_MAN"))));

// * make friendly to Each of the 3 common factions
    if (GetStandardFactionReputation(STANDARD_FACTION_COMMONER, oPC) <= 10)
    {   SetLocalInt(oPC, "NW_G_Playerhasbeenbad", 10); // * Player bad
        SetStandardFactionReputation(STANDARD_FACTION_COMMONER, 80, oPC);
    }
    if (GetStandardFactionReputation(STANDARD_FACTION_MERCHANT, oPC) <= 10)
    {   SetLocalInt(oPC, "NW_G_Playerhasbeenbad", 10); // * Player bad
        SetStandardFactionReputation(STANDARD_FACTION_MERCHANT, 80, oPC);
    }
    if (GetStandardFactionReputation(STANDARD_FACTION_DEFENDER, oPC) <= 10)
    {   SetLocalInt(oPC, "NW_G_Playerhasbeenbad", 10); // * Player bad
        SetStandardFactionReputation(STANDARD_FACTION_DEFENDER, 80, oPC);
    }

ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectHeal(GetMaxHitPoints(oPC)), oPC);

        //Search for negative effects
        effect eBad = GetFirstEffect(oPC);
        while(GetIsEffectValid(eBad))
        {
            if (GetEffectType(eBad) == EFFECT_TYPE_ABILITY_DECREASE ||
                GetEffectType(eBad) == EFFECT_TYPE_AC_DECREASE ||
                GetEffectType(eBad) == EFFECT_TYPE_ATTACK_DECREASE ||
                GetEffectType(eBad) == EFFECT_TYPE_DAMAGE_DECREASE ||
                GetEffectType(eBad) == EFFECT_TYPE_DAMAGE_IMMUNITY_DECREASE ||
                GetEffectType(eBad) == EFFECT_TYPE_SAVING_THROW_DECREASE ||
                GetEffectType(eBad) == EFFECT_TYPE_SPELL_RESISTANCE_DECREASE ||
                GetEffectType(eBad) == EFFECT_TYPE_SKILL_DECREASE ||
                GetEffectType(eBad) == EFFECT_TYPE_BLINDNESS ||
                GetEffectType(eBad) == EFFECT_TYPE_DEAF ||
                GetEffectType(eBad) == EFFECT_TYPE_PARALYZE ||
                GetEffectType(eBad) == EFFECT_TYPE_NEGATIVELEVEL)
                {
                    //Remove effect if it is negative.
                    RemoveEffect(oPC, eBad);
                }
            eBad = GetNextEffect(oPC);
        }
}