//#include "_persist_01a"
#include "aps_include"

void CreateAnObject(string sResource, object oPC, int iStackSize);


void main()
{

  object oPC = GetLastDisturbed();
  object oItem = GetInventoryDisturbItem();
  string sItemTag = GetTag(oItem);
  string sSuccess = "";

   // 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 (sItemTag!="ITEM_KINDLING")
    {
     if (GetStringLeft(sItemTag,5)!="WOOD_")
      {
       if (GetStringLeft(sItemTag,6)!="CURED_")
        {
         if (sItemTag!="SEED_RICE")
          {
           CopyItem(oItem,oPC,TRUE);
           FloatingTextStringOnCreature("This is not wood, kindling, rice or a cured hide...",oPC,FALSE);
           DestroyObject(oItem);
           return;
          }
        }
      }
    }

  if (GetLocalInt(OBJECT_SELF,"iAmInUse")!=0)
   {
    CopyItem(oItem,oPC,TRUE);
    FloatingTextStringOnCreature("The digester is not yet ready to process more materials...",oPC,FALSE);
    DestroyObject(oItem);
    return;
   }

  object oAcid = GetItemPossessedBy(oPC,"ACID_MODERATE");
  if (oAcid == OBJECT_INVALID)
   {
    CopyItem(oItem,oPC,TRUE);
    DestroyObject(oItem);
    FloatingTextStringOnCreature("You must have some moderate acid in order to activate the digester.",oPC,FALSE);
    return;
   }

  //int iPaperSkill = GetTokenPair(oPC,13,13);

  //nwn database
  //int iPaperSkill = GetCampaignInt("UOACraft","iPaperSkill",oPC);

  //external database
  int iPaperSkill = GetPersistentInt(oPC,"iPaperSkill","UOACraft");

  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 iProduct = 0;
  int iDifficulty = 0;
  string sProduct = "pulp_wood_raw";
  string sItemTag2 = sItemTag;
  sItemTag = GetStringRight(sItemTag,GetStringLength(sItemTag)-5);

  if (sItemTag=="KINDLING")iProduct = 1;
  if (sItemTag=="RICE")
   {
    iProduct=2;
    sProduct = "pulp_rice_raw";
   }
  if (sItemTag=="NORMAL") iProduct = 3;
  if (sItemTag=="OLIVE") iProduct = 3;

  if (sItemTag=="WILLOW")
   {
    iProduct = 5;
    iDifficulty = 100;
   }
  if (sItemTag=="PINE")
   {
    iProduct = 5;
    iDifficulty = 100;
   }
  if (sItemTag=="FIR")
   {
    iProduct = 5;
    iDifficulty = 100;
   }
  if (sItemTag=="MANGROVE")
   {
    iProduct = 5;
    iDifficulty = 100;
   }

  if (sItemTag=="CHERRY")
   {
    iProduct = 7;
    iDifficulty = 200;
   }
  if (sItemTag=="MAPLE")
   {
    iProduct = 7;
    iDifficulty = 200;
   }


  if (sItemTag=="ALMOND")
   {
    iProduct = 9;
    iDifficulty = 250;
   }

  if (sItemTag=="HICKORY")
   {
    iProduct = 11;
    iDifficulty = 300;
   }

  if (sItemTag=="BIRCH")
   {
    iProduct = 13;
    iDifficulty = 350;
   }

  if (sItemTag=="ELDER")
   {
    iProduct = 15;
    iDifficulty = 400;
   }

  if (sItemTag=="WALNUT")
   {
    iProduct = 15;
    iDifficulty = 400;
   }

  if (sItemTag=="ELM")
   {
    iProduct = 17;
    iDifficulty = 450;
   }

  if (sItemTag=="MAHOGANY")
   {
    iProduct = 18;
    iDifficulty = 500;
   }

  if (sItemTag=="OAK")
   {
    iProduct = 18;
    iDifficulty = 500;
   }

  if (sItemTag=="YEW")
   {
    iProduct = 20;
    iDifficulty = 550;
   }

  if (sItemTag=="REDWOOD")
   {
    iProduct = 20;
    iDifficulty = 600;
   }

  if (sItemTag=="IRONWOOD")
   {
    iProduct = 25;
    iDifficulty = 700;
   }

  //Check to see if parchment is being attempted
  if (GetStringLeft(sItemTag2,6)=="CURED_")iProduct=1;
  if (sItemTag2=="CURED_TINY")
   {
    sProduct = "item_parchmen001";
    iDifficulty=200;
   }
  if (sItemTag2=="CURED_TINYBLACK")
   {
    sProduct = "item_parchmen002";
    iDifficulty=300;
   }
  if (sItemTag2=="CURED_TINYWHITE")
   {
    sProduct = "item_parchmen003";
    iDifficulty=400;
   }
  if (sItemTag2=="CURED_SMALL")
   {
    sProduct = "item_parchmen004";
    iDifficulty=300;
   }
  if (sItemTag2=="CURED_SMALLBLACK")
   {
    sProduct = "item_parchmen005";
    iDifficulty=400;
   }
  if (sItemTag2=="CURED_SMALLWHITE")
   {
    sProduct = "item_parchmen006";
    iDifficulty=500;
   }
  if (sItemTag2=="CURED_MEDIUM")
   {
    sProduct = "item_parchmen007";
    iDifficulty=400;
   }
  if (sItemTag2=="CURED_MEDIUMBLACK")
   {
    sProduct = "item_parchmen008";
    iDifficulty=500;
   }
  if (sItemTag2=="CURED_MEDIUMWHITE")
   {
    sProduct = "item_parchmen009";
    iDifficulty=600;
   }
  if (sItemTag2=="CURED_LARGE")
   {
    sProduct = "item_parchmen010";
    iDifficulty=500;
   }
  if (sItemTag2=="CURED_LARGEBLACK")
   {
    sProduct = "item_parchmen011";
    iDifficulty=600;
   }
  if (sItemTag2=="CURED_LARGEWHITE")
   {
    sProduct = "item_parchmen012";
    iDifficulty=700;
   }
  if (sItemTag2=="CURED_XLARGE")
   {
    sProduct = "item_parchmen013";
    iDifficulty=600;
   }
  if (sItemTag2=="CURED_XLARGEBLACK")
   {
    sProduct = "item_parchmen014";
    iDifficulty=700;
   }
  if (sItemTag2=="CURED_XLARGEWHITE")
   {
    sProduct = "item_parchmen015";
    iDifficulty=800;
   }

  if (iPaperChance<iDifficulty)
   {
    FloatingTextStringOnCreature("You have no idea how to process this material..",oPC,FALSE);
    CopyItem(oItem,oPC,TRUE);
    DestroyObject(oItem);
    return;
   }

  int iAcidStack = GetNumStackedItems(oAcid)-1;
  DestroyObject(oAcid);
  if (iAcidStack >0) DelayCommand(1.0,CreateAnObject("acid002",oPC,iAcidStack));
  sSuccess = "The acid digests the material into a milky pulp....";
  if (GetStringLeft(sItemTag2,6)=="CURED_") sSuccess = "The acid digests the hairs from the hide, stripping the surface smooth.";
  // 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.
 //The only modification to this code is locAnvil was changed to locBath.

  location locBath = GetLocation(OBJECT_SELF);
  vector vEffectPos = GetPositionFromLocation(locBath);
  vEffectPos.z += 1.0;
  location locEffect = Location( GetAreaFromLocation(locBath), vEffectPos,GetFacingFromLocation(locBath) );
 // End snippet

  ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_SMOKE_PUFF), locEffect);
  ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_COM_HIT_ACID), locEffect);
  DelayCommand(3.0,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_SMOKE_PUFF), locEffect));
  DelayCommand(6.0,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_SMOKE_PUFF), locEffect));
  DelayCommand(9.0,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_SMOKE_PUFF), locEffect));
  DelayCommand(12.0,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_SMOKE_PUFF), locEffect));
  DelayCommand(15.0,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_SMOKE_PUFF), locEffect));
  DelayCommand(18.0,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_SMOKE_PUFF), locEffect));
  DelayCommand(21.0,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_SMOKE_PUFF), locEffect));
  DelayCommand(24.0,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_SMOKE_PUFF), locEffect));
  DelayCommand(27.0,ApplyEffectAtLocation (DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_SMOKE_PUFF), locEffect));


  DelayCommand(2.0,ApplyEffectAtLocation(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(5.0,ApplyEffectAtLocation(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(8.0,ApplyEffectAtLocation(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(10.0,ApplyEffectAtLocation(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_COM_SPECIAL_PINK_ORANGE),locEffect));
  DelayCommand(13.0,ApplyEffectAtLocation(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(16.0,ApplyEffectAtLocation(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(20.0,ApplyEffectAtLocation(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_COM_SPECIAL_RED_ORANGE),locEffect));
  DelayCommand(22.0,ApplyEffectAtLocation(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(25.0,ApplyEffectAtLocation(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_COM_SPECIAL_PINK_ORANGE),locEffect));
  DelayCommand(27.0,ApplyEffectAtLocation(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_COM_SPARKS_PARRY),locEffect));
  DelayCommand(29.0,ApplyEffectAtLocation(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_COM_SPECIAL_BLUE_RED),locEffect));

  AssignCommand(oPC,ActionPlayAnimation(ANIMATION_LOOPING_GET_MID,1.0,28.0));

  AssignCommand(oPC,PlaySound("al_cv_firepot1"));
  AssignCommand(oPC,DelayCommand(5.9,PlaySound("al_cv_firepot1")));
  AssignCommand(oPC,DelayCommand(11.9,PlaySound("al_cv_firepot1")));
  AssignCommand(oPC,DelayCommand(17.9,PlaySound("al_cv_firepot1")));
  AssignCommand(oPC,DelayCommand(23.9,PlaySound("al_cv_firepot1")));


  DestroyObject(oItem);



  SetLocalInt(OBJECT_SELF,"iAmInUse",99);
  DelayCommand(30.0,SetLocalInt(OBJECT_SELF,"iAmInUse",0));

  if (GetStringLeft(sProduct,5)!="item_")
    {
     DelayCommand(30.0,FloatingTextStringOnCreature(sSuccess,oPC,FALSE));
     float fDelay = 29.0;
     for (iProduct; iProduct>0; iProduct=iProduct-10)
      {
      // SendMessageToPC(oPC,"iProduct = "+IntToString(iProduct));
       fDelay=fDelay+1.0;
       if (iProduct>10)
         {
          AssignCommand(oPC,DelayCommand(fDelay,CreateAnObject(sProduct,oPC,10)));
         }
        else
         {
          if (iProduct>0) AssignCommand(oPC,DelayCommand(fDelay,CreateAnObject(sProduct,oPC,iProduct)));
         }
      }
    }
   else
    {
     int iSkillGain=0;
     iPaperChance = iPaperChance-iDifficulty;
     if (Random(1000)<=iPaperChance)
       {
        if (Random(1000) >= iPaperSkill)
         {
          if (d10(1)+1 >= iPaperChance/100) iSkillGain = 1;
         }

         //Ensure no more than 1 skill gain every 10 seconds to avoid token droppage.
        if (iSkillGain ==1)
         {
          if (GetLocalInt(oPC,"iSkillGain")!= 0)
            {
             iSkillGain = 0;
            }
           else
            {
             SetLocalInt(oPC,"iSkillGain",99);
             DelayCommand(10.0,SetLocalInt(oPC,"iSkillGain",0));
            }
         }

        if (iSkillGain ==1)
         {
          string sOldSkill = "";
          string sOldSkill2 = "";
          iPaperSkill++;
          sOldSkill2 = IntToString(iPaperSkill);
          sOldSkill = "."+GetStringRight(sOldSkill2,1);
          if (iPaperSkill > 9)
            {
             sOldSkill = GetStringLeft(sOldSkill2,GetStringLength(sOldSkill2)-1)+sOldSkill;
            }
           else
            {
             sOldSkill = "0"+sOldSkill;
            }
          if (iPaperSkill <= 1000)
           {
            //DelayCommand(30.0,SetTokenPair(oPC,13,13,iPaperSkill));

            //nwn database
            //DelayCommand(30.0,SetCampaignInt("UOACraft","iPaperSkill",iPaperSkill,oPC));

            //external database
            DelayCommand(30.0,SetPersistentInt(oPC,"iPaperSkill",iPaperSkill,0,"UOACraft"));

            DelayCommand(30.0,SendMessageToPC(oPC,"======================================"));
            DelayCommand(30.0,SendMessageToPC(oPC,"Your skill in papermaking has gone up!"));
            DelayCommand(30.0,SendMessageToPC(oPC,"Current papermaking skill : "+ sOldSkill+"%"));
            DelayCommand(30.0,SendMessageToPC(oPC,"======================================"));
            if (GetLocalInt(GetModule(),"_UOACraft_XP")!=0) DelayCommand(29.9,GiveXPToCreature(oPC,GetLocalInt(GetModule(),"_UOACraft_XP")));
           }
         }
        AssignCommand(oPC,DelayCommand(30.0,CreateAnObject(sProduct,oPC,1)));
        AssignCommand(oPC,DelayCommand(29.0,FloatingTextStringOnCreature(sSuccess,oPC,FALSE)));
       }
      else
       {
        //failed
        AssignCommand(oPC,DelayCommand(29.0,FloatingTextStringOnCreature("The acid burns holes in the hide, completely ruining it.",oPC,FALSE)));
       }
    }




}




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