//////////////////////////////////////////////////
// rts_pathing
//================================================
// By Deva Bryson Winblood
// 12/08/2003
//////////////////////////////////////////////////
#include "rts_wp_header"
///////////////////////
// PROTOTYPES
///////////////////////

// FILE: rts_pathing
// oCurrent = Current Area
// oDest = Object trying to reach
// sDestID = UNC, UND, DWF, SPID
// nOpt = 0 = Path choosing preferably the same area type as current
//        1 = Path choosing preferably underground ways
//        2 = Path choosing preferably aboveground ways
//        3 = Path choosing below ground during day, above ground at night
//        4 = Below ground a MUST! Also seek below ground shelter
object fnReversePath(object oCurrent,object oDest,string sDestID,int nOpt=0);

/////////////////////////////////////////////////////////
// SUPPORT FUNCTIONS
/////////////////////////////////////////////////////////
void fnPathingError(string sMsg)
{ // PATHING ERROR
  object oPC=GetFirstPC();
  while(oPC!=OBJECT_INVALID)
  { // pathing error
    SendMessageToPC(oPC,"ERROR-ERROR-:"+sMsg);
    oPC=GetNextPC();
  } // pathing error
  PrintString("ERROR-ERROR-:"+sMsg);
} // fnPathingError()

object fnRandomTrans(object oArea)
{ // return a random transition door or trigger
  object oRet=OBJECT_INVALID;
  int nC=GetLocalInt(oArea,"nPathingTC");
  if (nC<1)
  {
    fnBuildTransRand(oArea);
    return OBJECT_INVALID;
  }
  else
  {
    nC=Random(nC)+1;
    oRet=GetLocalObject(oArea,"oPathingTrans"+IntToString(nC));
  }
  return oRet;
} // fnRandomTrans()

object fnRandomObjects(object oArea)
{ // return an object in this area
  object oRet=OBJECT_INVALID;
  int nC=GetLocalInt(oArea,"nPathingOC");
  if (nC<1)
  {
    fnBuildObjectRand(oArea);
    return OBJECT_INVALID;
  }
  else
  {
    nC=Random(nC)+1;
    oRet=GetLocalObject(oArea,"oPathingObs"+IntToString(nC));
  }
  return oRet;
} // fnRandomObjects()

string fnParse(string sSource,string sDelim=".")
{ // parse
  string sRet="";
  string sMaster=sSource;
  while(GetStringLeft(sMaster,1)!=sDelim&&GetStringLength(sMaster)>0)
  { // parse
    sRet=sRet+GetStringLeft(sMaster,1);
    sMaster=GetStringRight(sMaster,GetStringLength(sMaster)-1);
  } // parse
  return sRet;
} // fnParse()

string fnRemoveParsed(string sSource,string sParsed,string sDelim=".")
{ // remove the portion parsed from the original string
  string sRet="";
  if (GetStringLength(sSource)<GetStringLength(sParsed)) return sRet;
  sRet=GetStringRight(sSource,GetStringLength(sSource)-GetStringLength(sParsed));
  if (GetStringLeft(sRet,1)==sDelim) sRet=GetStringRight(sRet,GetStringLength(sRet)-1);
  return sRet;
} // fnRemoveParsed()

//////////////////////////////////////
// PAthing
//////////////////////////////////////
object fnGetTransitionToArea(object oCurrentArea,object oTransitionTo)
{ // will return a transition if one exists
  object oRet=OBJECT_INVALID;
  int nTC=GetLocalInt(oCurrentArea,"nPathinTC");
  object oT;
  object oTarg;
  int nC=1;
  while(nC<=nTC)
  { // test
    oT=GetLocalObject(oCurrentArea,"oPathingTrans"+IntToString(nC));
    oTarg=GetTransitionTarget(oT);
    if (GetArea(oTarg)==oTransitionTo) return oT;
    nC++;
  } // test
  return oRet;
} // fnGetTransitionToArea()

object fnReversePath(object oCurrent,object oDest,string sDestID,int nOpt=0)
{ // build a path in reverse
  object oRet=OBJECT_INVALID;
  object oPath;
  object oConnection;
  object oLeadsTo;
  int nStop=FALSE;
  /// Try a strait run
  oPath=fnPathNextDestination(GetArea(oDest),sDestID,nOpt);
  if (oPath!=OBJECT_INVALID)
  { // path
    oLeadsTo=GetTransitionTarget(oPath);
    if (GetArea(oLeadsTo)==oCurrent)
    { // link
      oConnection=fnGetTransitionToArea(oCurrent,GetArea(oPath));
      return oConnection;
    } // link
    else
    { // while
      while(oPath!=OBJECT_INVALID&&nStop!=TRUE)
      { // build path
        if (GetArea(oLeadsTo)==oCurrent)
        { // direct link
          oConnection=fnGetTransitionToArea(oCurrent,GetArea(oPath));
          return oConnection;
        } // direct link
        else
        { // adjoining link?
          oConnection=fnGetTransitionToArea(oCurrent,GetArea(oPath));
          if (oConnection!=OBJECT_INVALID) return oConnection;
        } // adjoining link?
        oPath=fnPathNextDestination(GetArea(oLeadsTo),sDestID,nOpt);
        oLeadsTo=GetTransitionTarget(oPath);
      } // build path
    } // while
  } // path
  return oRet;
} // fnReversePath()

//void main() {}