//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *pRallyPoint - 
//			assaultcue - 
//-----------------------------------------------------------------------------
void CAI_AssaultBehavior::SetParameters( CBaseEntity *pRallyEnt, AssaultCue_t assaultcue )
{
	VPROF_BUDGET( "CAI_AssaultBehavior::SetParameters", VPROF_BUDGETGROUP_NPCS );

	// Firstly, find a rally point. 
	CRallyPoint *pRallyPoint = dynamic_cast<CRallyPoint *>(pRallyEnt);

	if( pRallyPoint )
	{
		if( !pRallyPoint->IsLocked() )
		{
			// Claim it.
			m_hRallyPoint = pRallyPoint;
			m_hRallyPoint->Lock( GetOuter() );
			
			m_AssaultCue = assaultcue;
			InitializeBehavior();
			return;
		}
		else
		{
			DevMsg("**ERROR: Specified a rally point that is LOCKED!\n" );
		}
	}
	else
	{
		DevMsg("**ERROR: Bad RallyPoint in SetParameters\n" );

		// Bomb out of assault behavior.
		m_AssaultCue = CUE_NO_ASSAULT;
		ClearSchedule();
	}

}
//-----------------------------------------------------------------------------
// Purpose: 
// Input  : rallypointname - 
//			assaultcue - 
//-----------------------------------------------------------------------------
void CAI_AssaultBehavior::SetParameters( string_t rallypointname, AssaultCue_t assaultcue, int rallySelectMethod )
{
	VPROF_BUDGET( "CAI_AssaultBehavior::SetParameters", VPROF_BUDGETGROUP_NPCS );

	// Firstly, find a rally point. 
	CRallyPoint *pRallyEnt = dynamic_cast<CRallyPoint *>(gEntList.FindEntityByName( NULL, rallypointname ) );

	CRallyPoint *pBest = NULL;
	int iBestPriority = -1;

	switch( rallySelectMethod )
	{
	case RALLY_POINT_SELECT_DEFAULT:
		{
			while( pRallyEnt )
			{
				if( !pRallyEnt->IsLocked() )
				{
					// Consider this point.
					if( pRallyEnt->m_iPriority > iBestPriority )
					{
						// This point is higher priority. I must take it.
						pBest = pRallyEnt;
						iBestPriority = pRallyEnt->m_iPriority;
					}
					else if ( pRallyEnt->m_iPriority == iBestPriority )
					{
						// This point is the same priority as my current best. 
						// I must take it if it is closer.
						Vector vecStart = GetOuter()->GetAbsOrigin();

						float flNewDist, flBestDist;

						flNewDist = ( pRallyEnt->GetAbsOrigin() - vecStart ).LengthSqr();
						flBestDist = ( pBest->GetAbsOrigin() - vecStart ).LengthSqr();

						if( flNewDist < flBestDist )
						{
							// Priority is already identical. Just take this point.
							pBest = pRallyEnt;
						}
					}
				}

				pRallyEnt = dynamic_cast<CRallyPoint *>(gEntList.FindEntityByName( pRallyEnt, rallypointname, NULL ) );
			}
		}
		break;

	case RALLY_POINT_SELECT_RANDOM:
		{
			// Gather all available points into a utilvector, then pick one at random.
			
			CUtlVector<CRallyPoint *> rallyPoints; // List of rally points that are available to choose from.

			while( pRallyEnt )
			{
				if( !pRallyEnt->IsLocked() )
				{
					rallyPoints.AddToTail( pRallyEnt );
				}

				pRallyEnt = dynamic_cast<CRallyPoint *>(gEntList.FindEntityByName( pRallyEnt, rallypointname ) );
			}

			if( rallyPoints.Count() > 0 )
			{
				pBest = rallyPoints[ random->RandomInt(0, rallyPoints.Count()- 1) ];
			}
		}
		break;

	default:
		DevMsg( "ERROR: INVALID RALLY POINT SELECTION METHOD. Assault will not function.\n");
		break;
	}

	if( !pBest )
	{
		DevMsg("%s Didn't find a best rally point!\n", STRING(GetOuter()->GetEntityName()) );
		return;
	}

	pBest->Lock( GetOuter() );
	m_hRallyPoint = pBest;

	if( !m_hRallyPoint )
	{
		DevMsg("**ERROR: Can't find a rally point named '%s'\n", STRING( rallypointname ));

		// Bomb out of assault behavior.
		m_AssaultCue = CUE_NO_ASSAULT;
		ClearSchedule();
		return;
	}

	m_AssaultCue = assaultcue;
	InitializeBehavior();
}
void ArrivalPaxLandsideBehavior::processEntryLandisde( const ElapsedTime& eEventTime )
{
	if(!m_pPerson)
		return;

	InitializeBehavior();
	SetEnterLandsideLocation(location);

	if(simEngineConfig()->isSimTerminalMode())
	{	
		//WriteLogEntry(t);
		Processor* pProc=  m_pPerson->getTerminalBehavior()->getLastTerminalProc();
		ASSERT(pProc);
		if(pProc)//find 
		{
			CString strProcName = pProc->getIDName();
			CFacilityBehaviorsInSim *pFacilityBehavior = m_pPerson->getEngine()->GetLandsideSimulation()->getFacilityBehaviors();
			LandsideResourceInSim* pLandsideLinkageObject = pFacilityBehavior->GetLandsideTerminalLinkage(*pProc->getID());
			if(pLandsideLinkageObject)
			{
				//it could be curbside, bus station ...
				pLandsideLinkageObject->PassengerMoveInto(this, eEventTime);
				return;
			}
			else //the last processor is not link to any 
			{
				//check parking spot
				LandsideResourceInSim *pDestResource = NULL;
				LandsideVehicleInSim* pVehicle = m_pPerson->getEngine()->GetLandsideSimulation()->FindPaxVehicle(m_pPerson->getID());
				if(pVehicle)
				{
					setVehicle(pVehicle);
				}
				else  //vehicle not entry simulation?
				{
					//LandsideSimErrorShown::PaxSimWarning(m_pPerson,_T("Pick Vehicle is not entry simulation yet"),_T("Pax Runtime Error"),eEventTime);
					terminate(eEventTime);
					return;
				}

				if(LandsideResourceInSim* pDestResource = pVehicle->getLastState().getLandsideRes() )
				{
					setResource(pDestResource);
					//destination is parking lot
					setDestResource(pDestResource);
					setState(MoveToFacility);
					GenerateEvent(eEventTime + ElapsedTime(2L)); //@CodeWarn ?2sec what for
					return;
				}
				else 
				{
					LandsideSimErrorShown::PaxSimWarning(m_pPerson,_T("Pax vehicle is not in any resource"),_T("Pax Runtime Error"),eEventTime);
					terminate(eEventTime);
					return;
				}
			}			
		}
		else  //no proc?
		{
			LandsideSimErrorShown::PaxSimWarning(m_pPerson,_T("No Last Processor"),_T("Pax Runtime Error"),eEventTime);
			terminate(eEventTime);
			return;
		}
	}
	else
	{
		if(!m_pVehicle)
		{
			LandsideVehicleInSim* pVehicle = m_pPerson->getEngine()->GetLandsideSimulation()->FindPaxVehicle(m_pPerson->getID());
			if(pVehicle)
			{
				setVehicle(pVehicle);
			}
			else
			{
				ASSERT(FALSE);
				terminate(eEventTime);
				return;
			}
		}

		UpdateEntryTime(eEventTime);
		//get on to the vehicle straight
		CPoint2008 pos= m_pVehicle->getOffVehiclePos( CPoint2008(getPoint()) );
		LandsideResourceInSim* pRes= m_pVehicle->getState(eEventTime).getLandsideRes();
		setResource(pRes);
		setLocation(pos);
		setDestination(pos);
		WriteLogEntry(eEventTime);

		m_pPerson->setState(MoveToVehicle);
		GenerateEvent(eEventTime);
	}
	

	
	
}