//#include "_persist_01a"

void ItemCreate(object oPC, string sSeedResRef);

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

void main()
{
  object oPC = OBJECT_SELF;
  string sPlantSeedTag = GetLocalString(oPC,"sItemActivated");
  string sPlantResRef = "";
  string sSeedResRef = "";
  int iFailPlant = 0;
  object oTool = OBJECT_INVALID;
  object oWater = OBJECT_INVALID;
  int iWater = GetLocalInt(oPC,"iWaterFull");

  //int iFarmSkill = GetTokenPair(oPC,14,6);
  int iFarmSkill = GetCampaignInt("UOACraft","iFarmSkill",oPC);
  int iFarmChance = iFarmSkill;
  int iSuccess = 0;

  oTool = GetItemPossessedBy(oPC,"TOOL_SHOVEL");
  oWater = GetItemPossessedBy(oPC,"ITEM_BUCKETOFWATER");

  if (GetLocalInt(oPC,"iAmInField") != 99)
   {
    FloatingTextStringOnCreature("You must be in a fertilized field in order to plant seeds.",oPC,FALSE);
    iFailPlant = 99;
   }

  if (oTool == OBJECT_INVALID)
   {
    if (iFailPlant != 99) FloatingTextStringOnCreature("You must have a shovel in order to plant these seeds.",oPC,FALSE);
    iFailPlant = 99;
   }

  if (oWater == OBJECT_INVALID)
   {
    if (iFailPlant != 99) FloatingTextStringOnCreature("You must have a bucket of water in order to plant seeds.",oPC,FALSE);
    iFailPlant = 99;
   }

  if (iFarmChance <350)
   {
    iFarmChance = GetAbilityScore(oPC,ABILITY_WISDOM)*5;
    iFarmChance = iFarmChance + (GetAbilityScore(oPC,ABILITY_INTELLIGENCE)*3);
    iFarmChance = iFarmChance+ (GetAbilityScore(oPC,ABILITY_CHARISMA)*2);
    iFarmChance = iFarmChance *3;
    if (iFarmChance > 350) iFarmChance = 350;
    if (iFarmSkill > iFarmChance) iFarmChance = iFarmSkill;
   }



  if (sPlantSeedTag=="SEED_CORN")
   {
    sPlantResRef = "plant001";
    sSeedResRef = "cornkernels";
   }

  if (sPlantSeedTag =="SEED_WHEAT")
   {
    sPlantResRef = "plant002";
    sSeedResRef = "wheatgrains";
   }

  if (sPlantSeedTag =="SEED_OATS")
   {
    sPlantResRef = "plant004";
    sSeedResRef = "oatgrains";
   }

  if (sPlantSeedTag =="SEED_BARLEY")
   {
    sPlantResRef = "plant005";
    sSeedResRef = "barleygrains";
   }

  if (sPlantSeedTag =="SEED_SORGHUM")
   {
    sPlantResRef = "plant006";
    sSeedResRef = "sorghumgrains";
   }


  if (sPlantSeedTag=="SEED_SPEARMINT")
   {
    sPlantResRef = "plant008";
    sSeedResRef = "spearmint";
   }

  if (sPlantSeedTag =="SEED_PEPPERMINT")
   {
    sPlantResRef = "plant009";
    sSeedResRef = "peppermint";
   }

  if (sPlantSeedTag =="SEED_GARLIC")
   {
    sPlantResRef = "plant010";
    sSeedResRef = "cloveofgarlic";
   }

  if (sPlantSeedTag =="SEED_ONION")
   {
    sPlantResRef = "plant011";
    sSeedResRef = "onionbulb";
   }


  if (sPlantSeedTag =="SEED_BLUEBERRY")
   {
    sPlantResRef = "plant012";
    sSeedResRef = "blueberry";
   }

  if (sPlantSeedTag =="SEED_BLACKBERRY")
   {
    sPlantResRef = "plant013";
    sSeedResRef = "blackberry";
   }

  if (sPlantSeedTag =="SEED_RASPBERRY")
   {
    sPlantResRef = "plant014";
    sSeedResRef = "raspberry";
   }

  if (sPlantSeedTag =="SEED_CRANBERRY")
   {
    sPlantResRef = "plant015";
    sSeedResRef = "cranberry";
   }

  if (sPlantSeedTag =="SEED_GRAPE1")
   {
    sPlantResRef = "plant016";
    sSeedResRef = "grapepurple";
   }

  if (sPlantSeedTag =="SEED_GRAPE2")
   {
    sPlantResRef = "plant017";
    sSeedResRef = "grapered";
   }

  if (sPlantSeedTag =="SEED_GRAPE3")
   {
    sPlantResRef = "plant018";
    sSeedResRef = "grapeyellow";
   }

  if (sPlantSeedTag =="SEED_GOOSEBERRY")
   {
    sPlantResRef = "plant019";
    sSeedResRef = "gooseberry";
   }

  if (sPlantSeedTag =="SEED_JUNIPERBERRY")
   {
    sPlantResRef = "plant020";
    sSeedResRef = "juniperberry";
   }

  if (sPlantSeedTag =="SEED_MARIGOLD")
   {
    sPlantResRef = "plant021";
    sSeedResRef = "marigold";
   }

  if (sPlantSeedTag =="SEED_SNAPDRAGON")
   {
    sPlantResRef = "plant022";
    sSeedResRef = "snapdragon";
   }

  if (sPlantSeedTag =="SEED_NASTURTIUM")
   {
    sPlantResRef = "plant023";
    sSeedResRef = "nasturtium";
   }

  if (sPlantSeedTag =="SEED_BLACKIRIS")
   {
    sPlantResRef = "plant024";
    sSeedResRef = "blackiristuber";
   }

  if (sPlantSeedTag =="SEED_YELLOWIRIS")
   {
    sPlantResRef = "plant025";
    sSeedResRef = "yellowiristuber";
   }

  if (sPlantSeedTag =="SEED_BLUEIRIS")
   {
    sPlantResRef = "plant026";
    sSeedResRef = "blueiristuber";
   }

  if (sPlantSeedTag =="SEED_REDIRIS")
   {
    sPlantResRef = "plant027";
    sSeedResRef = "rediristuber";
   }

  if (sPlantSeedTag =="SEED_WHITEIRIS")
   {
    sPlantResRef = "plant028";
    sSeedResRef = "whiteiristuber";
   }

  if (sPlantSeedTag =="SEED_COTTON")
   {
    sPlantResRef = "plant029";
    sSeedResRef = "cottonseeds";
   }


  if (sPlantSeedTag =="SEED_GINGER")
   {
    sPlantResRef = "plant030";
    sSeedResRef = "gingerroot";
   }
  if (sPlantSeedTag =="SEED_MANDRAKE")
   {
    sPlantResRef = "plant031";
    sSeedResRef = "mandrakeroot";
   }
  if (sPlantSeedTag =="SEED_SASSAFRASS")
   {
    sPlantResRef = "plant032";
    sSeedResRef = "sassafrassroot";
   }
  if (sPlantSeedTag =="SEED_REDTULIP")
   {
    sPlantResRef = "plant033";
    sSeedResRef = "redtulipbulb";
   }
  if (sPlantSeedTag =="SEED_BLACKTULIP")
   {
    sPlantResRef = "plant034";
    sSeedResRef = "blacktulipbulb";
   }
  if (sPlantSeedTag =="SEED_WHITETULIP")
   {
    sPlantResRef = "plant035";
    sSeedResRef = "whitetulipbulb";
   }
  if (sPlantSeedTag =="SEED_YELLOWTULIP")
   {
    sPlantResRef = "plant036";
    sSeedResRef = "yellowtulipbulb";
   }
  if (sPlantSeedTag =="SEED_BLUETULIP")
   {
    sPlantResRef = "plant037";
    sSeedResRef = "bluetulipbulb";
   }

  if (sPlantSeedTag =="SEED_TARRAGON")
   {
    sPlantResRef = "plant038";
    sSeedResRef = "tarragon";
   }
  if (sPlantSeedTag =="SEED_SAGE")
   {
    sPlantResRef = "plant039";
    sSeedResRef = "sage";
   }
  if (sPlantSeedTag =="SEED_LAVENDER")
   {
    sPlantResRef = "plant040";
    sSeedResRef = "lavender";
   }
  if (sPlantSeedTag =="SEED_PERIWINKLE")
   {
    sPlantResRef = "plant041";
    sSeedResRef = "periwinkle";
   }
  if (sPlantSeedTag =="SEED_MISTLETOE")
   {
    sPlantResRef = "plant042";
    sSeedResRef = "mistletoe";
   }

  if (sPlantSeedTag =="SEED_ORIENTALPOPPY")
   {
    sPlantResRef = "plant043";
    sSeedResRef = "orientalpoppy";
   }
  if (sPlantSeedTag =="SEED_LARKSPUR")
   {
    sPlantResRef = "plant044";
    sSeedResRef = "larkspur";
   }
  if (sPlantSeedTag =="SEED_MAYAPPLE")
   {
    sPlantResRef = "plant045";
    sSeedResRef = "mayapple";
   }
  if (sPlantSeedTag =="SEED_CHIVES")
   {
    sPlantResRef = "plant046";
    sSeedResRef = "chives";
   }
  if (sPlantSeedTag =="SEED_RADISH")
   {
    sPlantResRef = "plant047";
    sSeedResRef = "radish";
   }
  if (sPlantSeedTag =="SEED_LETTUCE")
   {
    sPlantResRef = "plant048";
    sSeedResRef = "lettuce";
   }
  if (sPlantSeedTag =="SEED_TURNIP")
   {
    sPlantResRef = "plant049";
    sSeedResRef = "turnip";
   }
  if (sPlantSeedTag =="SEED_PUMPKIN")
   {
    sPlantResRef = "plant050";
    sSeedResRef = "pumpkinseeds";
   }
  if (sPlantSeedTag =="SEED_WATERMELON")
   {
    sPlantResRef = "plant051";
    sSeedResRef = "watermelonseeds";
   }
  if (sPlantSeedTag =="SEED_SQUASH")
   {
    sPlantResRef = "plant052";
    sSeedResRef = "wintersquashseed";
   }

  if (sPlantSeedTag =="SEED_ZUCCHINI")
   {
    sPlantResRef = "plant053";
    sSeedResRef = "zucchinisquashse";
   }
  if (sPlantSeedTag =="SEED_CARROT")
   {
    sPlantResRef = "plant054";
    sSeedResRef = "carrot";
   }
  if (sPlantSeedTag =="SEED_POTATO")
   {
    sPlantResRef = "plant055";
    sSeedResRef = "potato";
   }
  if (sPlantSeedTag =="SEED_SWEETPOTATO")
   {
    sPlantResRef = "plant056";
    sSeedResRef = "sweetpotato";
   }
  if (sPlantSeedTag =="SEED_PEANUTS")
   {
    sPlantResRef = "plant057";
    sSeedResRef = "peanuts";
   }

  if (sPlantSeedTag =="SEED_INDIANCORN")
   {
    sPlantResRef = "plant058";
    sSeedResRef = "indiancornkernel";
   }
  if (sPlantSeedTag =="SEED_POPCORN")
   {
    sPlantResRef = "plant059";
    sSeedResRef = "popcornkernels";
   }
  if (sPlantSeedTag =="SEED_CATNIP")
   {
    sPlantResRef = "plant060";
    sSeedResRef = "catnip";
   }
  if (sPlantSeedTag =="SEED_GREENBEANS")
   {
    sPlantResRef = "plant061";
    sSeedResRef = "greenbeans";
   }
  if (sPlantSeedTag =="SEED_PEAS")
   {
    sPlantResRef = "plant062";
    sSeedResRef = "peas";
   }
  if (sPlantSeedTag =="SEED_BEETS")
   {
    sPlantResRef = "plant063";
    sSeedResRef = "beets";
   }
  if (sPlantSeedTag =="SEED_OREGANO")
   {
    sPlantResRef = "plant064";
    sSeedResRef = "oregano";
   }
  if (sPlantSeedTag =="SEED_BASIL")
   {
    sPlantResRef = "plant065";
    sSeedResRef = "basil";
   }
  if (sPlantSeedTag =="SEED_PARSLEY")
   {
    sPlantResRef = "plant066";
    sSeedResRef = "parsley";
   }
  if (sPlantSeedTag =="SEED_ARTICHOKE")
   {
    sPlantResRef = "plant067";
    sSeedResRef = "artichoke";
   }

  if (sPlantSeedTag =="SEED_CELERY")
   {
    sPlantResRef = "plant068";
    sSeedResRef = "celery";
   }
  if (sPlantSeedTag =="SEED_OKRA")
   {
    sPlantResRef = "plant069";
    sSeedResRef = "okra";
   }
  if (sPlantSeedTag =="SEED_TOMATO")
   {
    sPlantResRef = "plant070";
    sSeedResRef = "tomato";
   }
  if (sPlantSeedTag =="SEED_HOPS")
   {
    sPlantResRef = "plant071";
    sSeedResRef = "seed_hops_001";
    iFarmChance = iFarmChance - 200;
   }


  if (iFailPlant == 99)
   {
    CreateItemOnObject(sSeedResRef,oPC,1);
    return;
   }

  AssignCommand(oPC,ActionPlayAnimation(ANIMATION_LOOPING_GET_LOW,1.0,6.0));
  PlaySound("as_cv_mineshovl1");
  DelayCommand(3.0,PlaySound("as_na_splash2"));

  if (iWater == 0) iWater=5;
  iWater--;
  if (iWater <1)
    {
     DelayCommand(3.0,FloatingTextStringOnCreature("Your water bucket is now empty...",oPC,FALSE));
     DestroyObject(oWater);
     DelayCommand(3.0,ItemCreate(oPC,"item001"));
    }
   else
    {
     SendMessageToPC(oPC,"Your water bucket has "+IntToString(iWater)+" uses left.");
    }
  SetLocalInt(oPC,"iWaterFull",iWater);

  int iRandom = Random(1000);
  int iSkillGain = 0;
  if (iRandom <= iFarmChance)
   {
    iSuccess = 1;
    iRandom = Random(1000);
     if (iRandom >= iFarmSkill)
      {
       if (d10(1)+1 >= iFarmChance/100) iSkillGain = 1;
      }
    }

  if (iSuccess == 0)
   {
    DelayCommand(6.0,FloatingTextStringOnCreature("You are unable to make the plant grow.",oPC,FALSE));
    return;
   }

  DelayCommand(6.0,FloatingTextStringOnCreature("You plant and water the seed with tender care.",oPC,FALSE));

  if (iSkillGain ==1)
    {
     string sOldSkill = "";
     string sOldSkill2 = "";
     iFarmSkill++;
     sOldSkill2 = IntToString(iFarmSkill);
     sOldSkill = "."+GetStringRight(sOldSkill2,1);
     if (iFarmSkill > 9)
       {
        sOldSkill = GetStringLeft(sOldSkill2,GetStringLength(sOldSkill2)-1)+sOldSkill;
       }
      else
       {
        sOldSkill = "0"+sOldSkill;
       }
     if (iFarmSkill <= 1000)
      {
       //DelayCommand(5.0,SetTokenPair(oPC,14,6,iFarmSkill));
       DelayCommand(6.0,SetCampaignInt("UOACraft","iFarmSkill",iFarmSkill,oPC));
       DelayCommand(6.0,SendMessageToPC(oPC,"==============================="));
       DelayCommand(6.0,SendMessageToPC(oPC,"Your Farming skill has gone up!"));
       DelayCommand(6.0,SendMessageToPC(oPC,"Current Farming skill : "+ sOldSkill+"%"));
       DelayCommand(6.0,SendMessageToPC(oPC,"==============================="));
       if (GetLocalInt(GetModule(),"_UOACraft_XP")!=0) DelayCommand(5.9,GiveXPToCreature(oPC,GetLocalInt(GetModule(),"_UOACraft_XP")));
      }
   }

  float fPause = 1800.0 - IntToFloat(iFarmSkill);
  //fPause = fPause/10.0;
  object oTemp = CreateObject(OBJECT_TYPE_PLACEABLE,"temporaryhole",GetLocation(oPC),FALSE);
  AssignCommand(oTemp,DelayCommand(fPause,CreatePlaceable(sPlantResRef,GetLocation(oTemp),0.0)));
  DestroyObject(oTemp,fPause+10.0);
  DelayCommand(7.0,SendMessageToPC(oPC,"Be sure to check back later to see if your plant has grown yet."));

  int iToolBreak=GetLocalInt(oPC,"iToolWillBreak");
  iToolBreak++;
  if (iToolBreak > 100)
   {
    DelayCommand(2.0,FloatingTextStringOnCreature("Your shovel has broken while digging..",oPC,FALSE));
    DestroyObject(oTool,2.0);
    iToolBreak = 0;
   }
  SetLocalInt(oPC,"iToolWillBreak",iToolBreak);

}

void ItemCreate(object oPC, string sSeedResRef)
{
 object oSeed = CreateItemOnObject(sSeedResRef,oPC,1);
 return;
}