//#include "_persist_01a"
#include "nw_i0_plot"

void CreateAnObject(string sResource, object oPC, int iStackSize);
void GetNextStackedItem(object oPC, string sItemTag, int iCount, int iMode, string sStackResRef);
object CreatePlaceable(string sObject, location lPlace, float fDuration);
void DelaySilk(object oPC);

void main()
{
  int iUseMode = 0;
  if (GetInventoryDisturbType()!= INVENTORY_DISTURB_TYPE_ADDED)iUseMode = 99;

  object oPC = GetLastDisturbed();
  object oItem = GetInventoryDisturbItem();
  string sItemTag = GetTag(oItem);
  string sSuccess = "";
  object oSelf = OBJECT_SELF;
   // The following 3 lines are to ensure compatability with UOAbigal's Persistent Token System.
   // You can replace them with whatever 'no-drop' code you have or comment them out.
   string sNoDropFlag = (GetStringLeft(GetTag(oItem),6));
   if (sNoDropFlag == "NoDrop" || sNoDropFlag == "TOKEN_"||sNoDropFlag=="_TBOX_")
    return;
   if (GetBaseItemType(oItem)==BASE_ITEM_LARGEBOX)
    {
     DestroyObject(oItem);
     SendMessageToPC(oPC,"To avoid possible dupe exploits, the container placed in this bag may be destroyed.");
     return;
    }
   // End of compatability portion.

  if (GetLocalInt(OBJECT_SELF,"iAmInUse")!=0)
   {
    if (iUseMode!=99) CopyItem(oItem,oPC,TRUE);
    if (iUseMode==99) CopyObject(oItem,GetLocation(oSelf),oSelf,GetTag(oItem));
    FloatingTextStringOnCreature("The pulp basin is not yet ready for more materials...",oPC,FALSE);
    DestroyObject(oItem);
    return;
   }


  if (iUseMode==99)
   {
    if (sItemTag=="P_PULP_BLEACHED")
     {
      SetLocalInt(oPC,"iPulpType",0);
      FloatingTextStringOnCreature("Setting up to bleach normal wood or rice pulp.",oPC,FALSE);
     }
    if (sItemTag=="P_PULP_BLEACHED_SILK")
     {
      SetLocalInt(oPC,"iPulpType",1);
      FloatingTextStringOnCreature("Setting up to bleach wood pulp with silk addative.",oPC,FALSE);
     }
    string sTagSelf = GetTag(oSelf);
    AssignCommand(oPC,DoPlaceableObjectAction(oSelf,PLACEABLE_ACTION_USE));
    AssignCommand(oPC,DelayCommand(1.5,DoPlaceableObjectAction(GetNearestObjectByTag(sTagSelf,oPC,1),PLACEABLE_ACTION_USE)));
    DestroyObject(oItem);
    return;
   }

  int iPulpType = GetLocalInt(oPC,"iPulpType");


  if (sItemTag!="pulp_wood_raw")
   {
    if (sItemTag!="pulp_rice_raw")
     {
      if (GetStringLeft(sItemTag,13)!= "item_parchmen")
       {
        FloatingTextStringOnCreature("This is not raw pulp or digested parchment!",oPC,FALSE);
        CopyItem(oItem,oPC,TRUE);
        DestroyObject(oItem);
        return;
       }
     }
   }

  //int iSubtract = 0;
  //if (GetResRef(oItem)=="")
  // {
  //  object oTemp2 = CreateItemOnObject(sItemTag,OBJECT_SELF,1);
  //  if (oTemp2==OBJECT_INVALID)
  //   {
  //    CopyObject(oItem,GetLocation(oPC),oPC,sItemTag);
  //    DestroyObject(oItem);
  //    FloatingTextStringOnCreature("This item has been returned due to missing ResRef! (It is either a split stack or an imported object)",oPC,FALSE);
  //    return;
  //   }
  //  oItem=GetItemPossessedBy(oSelf,sItemTag);
  //  iSubtract=1;
    //DestroyObject(oTemp2);
  // }





  //int iPaperSkill = GetTokenPair(oPC,13,7);
  int iPaperSkill = GetCampaignInt("UOACraft","iPaperSkill",oPC);
  int iPaperChance = iPaperSkill;
  if (iPaperSkill <350)
   {
    iPaperChance = GetAbilityScore(oPC,ABILITY_DEXTERITY)*5;
    iPaperChance = iPaperChance + (GetAbilityScore(oPC,ABILITY_WISDOM)*3);
    iPaperChance = iPaperChance + (GetAbilityScore(oPC,ABILITY_INTELLIGENCE)*2);
    iPaperChance = iPaperChance *3;
    if (iPaperChance>350)iPaperChance = 350;
    if (iPaperSkill>iPaperChance) iPaperChance = iPaperSkill;
   }

  //int iStackSize = GetNumStackedItems(oItem);
  int iStackSize = GetNumItems(oSelf,GetTag(oItem)); //-iSubtract;

  //if (iStackSize==0)
  // {
  //  FloatingTextStringOnCreature("Invalid StackSize..",oPC,FALSE);
  //  CopyObject(oItem,GetLocation(oPC),oPC,sItemTag);
  //  DestroyObject(oItem);
  //  return;
  // }


  int iStackSize2 = GetNumItems(oPC,"item_chlorine");

  if (iStackSize2<iStackSize)
   {
    if (sItemTag=="pulp_wood_raw")FloatingTextStringOnCreature("You do not have enough bleaching agent to process this much wood pulp.",oPC,FALSE);
    if (sItemTag=="pulp_rice_raw")FloatingTextStringOnCreature("You do not have enough bleaching agent to process this much rice pulp.",oPC,FALSE);
    if (GetStringLeft(sItemTag,13)=="item_parchmen")FloatingTextStringOnCreature("You do not have enough bleaching agent to process this digested parchment.",oPC,FALSE);
    CopyObject(oItem,GetLocation(oPC),oPC,sItemTag);
    DestroyObject(oItem);
    return;
   }

  if (iPulpType==1)
   {
    if (GetNumItems(oPC,"ITEM_SPIDERSILK")<iStackSize)
     {
      FloatingTextStringOnCreature("You do not have enough spider's silk to process this much pulp.",oPC,FALSE);
      CopyObject(oItem,GetLocation(oPC),oPC,sItemTag);
      DestroyObject(oItem);
      return;
     }
   }

  string sResRef="";
  int iStack = 0;

  if (sItemTag=="pulp_wood_raw")
   {
    sResRef = "pulp_wood_raw001";
    iStack = iStackSize;
    sSuccess = "You carefully process the wood pulp with the pulp bleach.";
    if (iPulpType==1)
     {
      sResRef = "pulp_silk_raw001";
      sSuccess = "You carefully stir in the silk strands and process the silk and wood pulp with the pulp bleach.";
     }
   }
  if (sItemTag=="pulp_rice_raw")
   {
    sResRef = "pulp_rice_raw001";
    iStack = iStackSize;
    sSuccess = "You carefully process the rice pulp with the pulp bleach.";
   }
  if (GetStringLeft(sItemTag,13)=="item_parchmen")
   {
    int iValue = StringToInt(GetStringRight(sItemTag,3));
    string sType = "norma";
    iStack = 1;
    if (iValue>3) iStack = 2;
    if (iValue>6) iStack = 4;
    if (iValue>9) iStack = 7;
    if (iValue>12) iStack = 10;
    switch (iValue)
     {
      case 2:{sType="black";break;}
      case 3:{sType="white";break;}
      case 5:{sType="black";break;}
      case 6:{sType="white";break;}
      case 8:{sType="black";break;}
      case 9:{sType="white";break;}
      case 11:{sType="black";break;}
      case 12:{sType="white";break;}
      case 14:{sType="black";break;}
      case 15:{sType="white";break;}
      default:{sType="norma";break;}
     }
    sResRef = "scrl_blank_"+sType;
    if (sType=="norma") sType = "normal";
    sSuccess = "You carefully bleach, dry, and cut the digested "+sType+" parchment.";
    iStackSize = iStack;
   }

  //Remove Components (bleaching agent)
  object oTemp = GetItemPossessedBy(oPC,"item_chlorine");
  string sComponentResRef = GetResRef(oTemp);
  iStackSize2 = GetNumStackedItems(oTemp);
  DestroyObject(oTemp);
  if (iStackSize2 < iStack)
    {
     iStack = iStack - iStackSize2;
     DelayCommand(2.0,GetNextStackedItem(oPC,"item_chlorine",iStack,1,"item_chlorine"));
    }
   else
    {
     if (iStackSize2 > iStack)
      {
       iStackSize2 = iStackSize2 - iStack;
       DelayCommand(1.0,CreateAnObject("item_chlorine",oPC,iStackSize2));
      }
    }

  //remove the spider silk
  if (iPulpType==1)
   {
    int iSilk = GetNumStackedItems(oItem);
    float fDelay2 = 0.0;
    for (iSilk; iSilk>0; iSilk--)
     {
      fDelay2=fDelay2+0.5;
      DelayCommand(fDelay2,DelaySilk(oPC));
     }
   }

  AssignCommand(oPC,ActionPlayAnimation(ANIMATION_LOOPING_GET_LOW,1.0,19.0));
  PlaySound("as_na_splash2");
  DelayCommand(0.5,PlaySound("al_na_waterpool1"));
  DelayCommand(10.0,PlaySound("al_na_waterpool1"));
  DelayCommand(1.0,PlaySound("as_na_waterlap3"));
  DelayCommand(7.0,PlaySound("as_na_waterlap3"));
  DelayCommand(13.0,PlaySound("as_na_waterlap3"));
  if (GetStringLeft(sResRef,4)=="scrl")
   {
    AssignCommand(oPC,DelayCommand(19.0,PlaySound("as_cv_shopmetal2")));
    AssignCommand(oPC,DelayCommand(20.0,PlaySound("as_cv_shopmetal2")));
    AssignCommand(oPC,DelayCommand(21.0,PlaySound("as_cv_shopmetal2")));
   }

  DelayCommand(20.0,CreateAnObject(sResRef,oPC,iStackSize));
  DelayCommand(20.0,FloatingTextStringOnCreature(sSuccess,oPC,FALSE));


  // This snippet of code is cut-n-paste direct from ATS
 // Reason for this is because I had no clue how to assign an increase
 // in the z-axis of the location of the anvil for sparks to display.
 // After reading through this code, it is obvious that vEffecrPos.z
 // is the line which assigns this.  Due to my own ignorance in this issue
 // I have decided to leave this snippet of code intact with this credit to
 // the original ATS script coders, whomever they may have been.

  location locAnvil = GetLocation(OBJECT_SELF);
  vector vEffectPos = GetPositionFromLocation(locAnvil);
  vEffectPos.z += 0.3;
  location locEffect = Location( GetAreaFromLocation(locAnvil), vEffectPos,GetFacingFromLocation(locAnvil) );
  CreatePlaceable("plc_dustplume",locEffect,18.5);

  SetLocalInt(OBJECT_SELF,"iAmInUse",99);
  DelayCommand(20.1,SetLocalInt(OBJECT_SELF,"iAmInUse",0));
  DestroyObject(oItem);


}


void CreateAnObject(string sResource, object oPC, int iStackSize)
 {
  CreateItemOnObject(sResource,oPC,iStackSize);
  return;
 }

void GetNextStackedItem(object oPC, string sItemTag, int iCount, int iMode, string sStackResRef)
 {
  object oTemp = GetItemPossessedBy(oPC,sItemTag);
  int iStackCount = GetNumStackedItems(oTemp);
  int iTemp = iCount - iStackCount;
  iStackCount = iStackCount-iCount;
  DestroyObject(oTemp);
  if (iStackCount > 0)
   {
    SendMessageToPC(oPC,"You should get back "+IntToString(iStackCount));
    DelayCommand(1.0,CreateAnObject(sStackResRef,oPC,iStackCount));
   }
  // this next line *should* recursively call this function if the number of
  // stacked items does not meet the required number of items to be destroyed.
  if (iTemp > 0) DelayCommand(1.0,GetNextStackedItem(oPC,sItemTag,iTemp, iMode, sStackResRef));
  return;
 }

object CreatePlaceable(string sObject, location lPlace, float fDuration)
{
  object oPlaceable = CreateObject(OBJECT_TYPE_PLACEABLE,sObject,lPlace,FALSE);
  if (fDuration != 0.0)
    DestroyObject(oPlaceable,fDuration);
  return oPlaceable;
}

void DelaySilk(object oPC)
 {
  object oTemp3 = GetItemPossessedBy(oPC,"ITEM_SILK");
  DestroyObject(oTemp3);
  return;
 }