//::///////////////////////////////////////////////
//:: BODY TAILOR: increase
//:: onconv bodytailor
//:://////////////////////////////////////////////
/*
   this just increments the piece number and slaps it on.
   well, checking for restrictions and looping back to 0,
   too.
*/
//:://////////////////////////////////////////////
//:: Modified By: styzygy
//:: Created By: bloodsong
//:: based on the mil tailor by Jake E. Fitch (Milambus Mandragon)
//:://////////////////////////////////////////////
#include "inc_bodytailor"

void main()
{
    object oPC = GetPCSpeaker();

    string sToModify = GetLocalString(oPC, "ToModify");
    //-- WINGS, TAIL, HEAD, EYES...
    int iToModify = GetLocalInt(oPC, "ToModify");
    //-- bodypart #...
    string s2DAFile = GetLocalString(oPC, "2DAFile");
    //-- "" for items with no 2da file...?
    string s2DAend; //-- the endoffile check

    int iNewApp;

//--WING SECTION vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    if(sToModify == "WINGS")
    {
      //-- advance wing type.
      iNewApp = GetCreatureWingType(oPC)+1;

      //--if it is restricted, skip ahead.
      while(WingIsInvalid(iNewApp))
      {//-- increase
        iNewApp++;

        s2DAend = Get2DACheck(s2DAFile, iNewApp);
        //-- check for blank lines
        if (s2DAend == "SKIP")
        {//-- be careful reading 2da in a fast loop.
          iNewApp++;
          s2DAend = Get2DACheck(s2DAFile, iNewApp);
        }
        //-- check we didnt hit the end
        if (s2DAend == "FAIL" || iNewApp > GetCachedLimit(s2DAFile))
        {//-- if hit the end, loop back to 0
           iNewApp = 0;
        }
      }

     //--do the loop around check
     s2DAend = Get2DACheck(s2DAFile, iNewApp);
    while (s2DAend == "SKIP" || s2DAend == "FAIL")
    {
        if (s2DAend == "FAIL" || iNewApp > GetCachedLimit(s2DAFile))
        {
            iNewApp = 0;
        } else {
            iNewApp++;
        }

        s2DAend = Get2DACheck(s2DAFile, iNewApp);
    }  //-- end loop around check

      SetCreatureWingType(iNewApp, oPC); //--now slap the new wing on!
    }
//--END WINGS ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


//--TAIL SECTION vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    if(sToModify == "TAIL")
    {
      //-- advance tail type.
      iNewApp = GetCreatureTailType(oPC)+1;

      //--if it is restricted, skip ahead.
      while(TailIsInvalid(iNewApp))
      {//-- increase
        iNewApp++;

        s2DAend = Get2DACheck(s2DAFile, iNewApp);
        //-- check for blank lines
        if (s2DAend == "SKIP")
        {//-- be careful reading 2da in a fast loop.
          iNewApp++;
          s2DAend = Get2DACheck(s2DAFile, iNewApp);
        }
        //-- check we didnt hit the end
        if (s2DAend == "FAIL" || iNewApp > GetCachedLimit(s2DAFile))
        {//-- if hit the end, loop back to 0
           iNewApp = 0;
        }
      }

     //--do the loop around check
     s2DAend = Get2DACheck(s2DAFile, iNewApp);
    while (s2DAend == "SKIP" || s2DAend == "FAIL")
    {
        if (s2DAend == "FAIL" || iNewApp > GetCachedLimit(s2DAFile))
        {
            iNewApp = 0;
        } else {
            iNewApp++;
        }

        s2DAend = Get2DACheck(s2DAFile, iNewApp);
    }  //-- end loop around check

      SetCreatureTailType(iNewApp, oPC); //--now slap the new tail on!
    }
//--END TAIL ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

//--the rest of the parts dont have 2da files to read from.
//-- be sure you set your max numbers in the include file (btlr__inc)

  int nGender = GetGender(OBJECT_SELF);
  int nRace = GetAppearanceType(OBJECT_SELF);
  int nMax = 256;

//--HEAD BITS-------------------------------------------------------
  if(sToModify == "HEAD")
  {
    iNewApp = GetCreatureBodyPart(CREATURE_PART_HEAD, oPC)+1;
       //-- check we didnt hit the end(s)
    if(nGender==GENDER_FEMALE)
    {
       switch(nRace)
       {
         case 0: nMax=DFHEADMAX; break;
         case 1: nMax = EFHEADMAX; break;
         case 2: nMax = GFHEADMAX; break;
         case 4:
         case 6: nMax = HFHEADMAX; break;
         case 3: nMax = AFHEADMAX; break;
         case 5: nMax = OFHEADMAX; break;
       }
     }
     else //--males and everything else
     {
       switch(nRace)
       {
         case 0: nMax=DMHEADMAX; break;
         case 1: nMax = EMHEADMAX; break;
         case 2: nMax = GMHEADMAX; break;
         case 4:
         case 6: nMax = HMHEADMAX; break;
         case 3: nMax = AMHEADMAX; break;
         case 5: nMax = OMHEADMAX; break;
       }
    }

    if(iNewApp > nMax)
    {//-- loop to 0
      iNewApp = 0;
    }

     SetCreatureBodyPart(CREATURE_PART_HEAD, iNewApp, oPC);
  }  //-- end HEAD section


//--MISC BITS------------------------------------------------------

    if(sToModify == "PARTS")
    {
     iNewApp = GetCreatureBodyPart(iToModify, oPC) +1;
     //--check the ceiling
     if(iNewApp > CUSTOMPARTS)
     { //-- more than our 3 parts
       if(iNewApp >= 255)
       { //-- if it is 256, loop to 0
         iNewApp = 0;
       }
       else
       { //-- or else jump from max custom to 256
        iNewApp = 255;
       }
     }  //-- end loop check

     SetCreatureBodyPart(iToModify, iNewApp, oPC);
     //SendMessageToPC(oPC, "Part=" + IntToString(iToModify) + ", #=" + IntToString(iNewApp));
    }


//--PHENO PART------------------------------------------------------
    if(sToModify == "PHENO")
    {
     iNewApp = GetPhenoType(oPC) +1;
     //--check the ceiling
     if(iNewApp > MAXPHENO)
     { //-- more than our 3 parts
         iNewApp = 0;
     }  //-- end loop check

     SetPhenoType(iNewApp, oPC);
    }
//--END PHENO PART------------------------------------------------------



//--EVERYBODY/THING SECTION ---------------------------------------------
    SendMessageToPC(oPC, "New Appearance: " + IntToString(iNewApp));
}
