//----------------------------- GetBotAtPosition ------------------------------
//
//  given a position on the map this method returns the bot found with its
//  bounding radius of that position.
//  If there is no bot at the position the method returns NULL
//-----------------------------------------------------------------------------
Raven_Bot* Raven_Game::GetBotAtPosition(Vector2D CursorPos)const
{
  std::list<Raven_Bot*>::const_iterator curBot = m_Bots.begin();

  for (curBot; curBot != m_Bots.end(); ++curBot)
  {
    if (Vec2DDistance((*curBot)->Pos(), CursorPos) < (*curBot)->BRadius())
    {
      if ((*curBot)->isAlive())
      {
        return *curBot;
      }
    }
  }

  return NULL;
}
Esempio n. 2
0
//---------------------------- ctor -------------------------------------------
//-----------------------------------------------------------------------------
Raven_Door::Raven_Door(Raven_Map* pMap,
                       std::ifstream& is):

                                  BaseGameEntity(GetValueFromStream<int>(is)),
                                  m_Status(closed),
                                  m_iNumTicksStayOpen(60)                   //MGC!
{
  Read(is);

  m_vtoP2Norm =  Vec2DNormalize(m_vP2 - m_vP1);
  m_dCurrentSize = m_dSize = Vec2DDistance(m_vP2, m_vP1);

  Vector2D perp = m_vtoP2Norm.Perp();
  
  //create the walls that make up the door's geometry
  m_pWall1 = pMap->AddWall(m_vP1+perp, m_vP2+perp);
  m_pWall2 = pMap->AddWall(m_vP2-perp, m_vP1-perp);
}
//----------------------------- AttemptToAddBot -------------------------------
//-----------------------------------------------------------------------------
bool Raven_Game::AttemptToAddBot(Raven_Bot* pBot)
{
  //make sure there are some spawn points available
  if (m_pMap->GetSpawnPoints().size() <= 0)
  {
    //ErrorBox("Map has no spawn points!"); return false;
	cocos2d::CCLog("Error: Map has no spawn points!");
	return false;
  }

  //we'll make the same number of attempts to spawn a bot this update as
  //there are spawn points
  int attempts = m_pMap->GetSpawnPoints().size();

  while (--attempts >= 0)
  { 
    //select a random spawn point
    Vector2D pos = m_pMap->GetRandomSpawnPoint();

    //check to see if it's occupied
    std::list<Raven_Bot*>::const_iterator curBot = m_Bots.begin();

    bool bAvailable = true;

    for (curBot; curBot != m_Bots.end(); ++curBot)
    {
      //if the spawn point is unoccupied spawn a bot
      if (Vec2DDistance(pos, (*curBot)->Pos()) < (*curBot)->BRadius())
      {
        bAvailable = false;
      }
    }

    if (bAvailable)
    {  
      pBot->Spawn(pos);

      return true;   
    }
  }

  return false;
}
//--------------- InflictDamageOnBotsWithinBlastRadius ------------------------
//
//  If the rocket has impacted we test all bots to see if they are within the 
//  blast radius and reduce their health accordingly
//-----------------------------------------------------------------------------
void Rocket::InflictDamageOnBotsWithinBlastRadius()
{
  std::list<Raven_Bot*>::const_iterator curBot = m_pWorld->GetAllBots().begin();

  for (curBot; curBot != m_pWorld->GetAllBots().end(); ++curBot)
  {
    if (Vec2DDistance(Pos(), (*curBot)->Pos()) < m_dBlastRadius + (*curBot)->BRadius())
    {
      //send a message to the bot to let it know it's been hit, and who the
      //shot came from
      Dispatcher->DispatchMsg(SEND_MSG_IMMEDIATELY,
                              m_iShooterID,
                              (*curBot)->ID(),
                              Msg_TakeThatMF,
                              (void*)&m_iDamageInflicted);
      
    }
  }  
}
//-------------------------------- SelectWeapon -------------------------------
//
//-----------------------------------------------------------------------------
void Raven_WeaponSystem::SelectWeapon()
{
	//if a target is present use fuzzy logic to determine the most desirable 
	//weapon.
	if (m_pOwner->GetTargetSys()->isTargetPresent())
	{
		//calculate the distance to the target
		double DistToTarget = Vec2DDistance(m_pOwner->Pos(), m_pOwner->GetTargetSys()->GetTarget()->Pos());

		//for each weapon in the inventory calculate its desirability given the 
		//current situation. The most desirable weapon is selected
		double BestSoFar = MinDouble;

		WeaponMap::const_iterator curWeap;
		for (curWeap = m_WeaponMap.begin(); curWeap != m_WeaponMap.end(); ++curWeap)
		{
			//grab the desirability of this weapon (desirability is based upon
			//distance to target and ammo remaining)
			if (curWeap->second)
			{
				double score = curWeap->second->GetDesirability(DistToTarget);

				//if it is the most desirable so far select it
				if (score > BestSoFar)
				{
					BestSoFar = score;

					//place the weapon in the bot's hand.
					m_pCurrentWeapon = curWeap->second;
				}
			}
		}
	}

	else
	{
		m_pCurrentWeapon = m_WeaponMap[type_blaster];
	}
}
//---------------------------- AddFuzzyDeviationToAim -------------------------
//
//  adds a deviation to the firing angle using fuzzy logic
//-------------------------------------- ---------------------------------------
void Raven_WeaponSystem::AddFuzzyDeviationToAim(Vector2D& AimingPos)
{
	double distToTarget = Vec2DDistance(m_pOwner->Pos(), m_pOwner->GetTargetSys()->GetTarget()->Pos());
	double targetVelocity = m_pOwner->Speed();
	double targetVisiblePeriod = m_pOwner->GetTargetSys()->GetTimeTargetHasBeenVisible();

	/* Fuzzification */
	m_FuzzyModule.Fuzzify("DistToTarget", distToTarget);
	m_FuzzyModule.Fuzzify("TargetVelocity", targetVelocity);
	m_FuzzyModule.Fuzzify("TargetVisiblePeriod", targetVisiblePeriod);

	double derivation = m_FuzzyModule.DeFuzzify("Deviation", FuzzyModule::max_av);
	bool isDerivationPosition = RandBool();

	/* Deviation computing */
	Vector2D toPos = AimingPos - m_pOwner->Pos();

	if (isDerivationPosition)
		Vec2DRotateAroundOrigin(toPos, derivation*m_dAimAccuracy);
	else
		Vec2DRotateAroundOrigin(toPos, -1 * derivation*m_dAimAccuracy);

	AimingPos = toPos + m_pOwner->Pos();
}
Esempio n. 7
0
//----------------- CalculateExpectedTimeToReachPosition ----------------------
//
//  returns a value indicating the time in seconds it will take the bot
//  to reach the given position at its current speed.
//-----------------------------------------------------------------------------
double Raven_Bot::CalculateTimeToReachPosition(Vector2D pos)const
{
  return Vec2DDistance(Pos(), pos) / (MaxSpeed() * FrameRate);
}
//--------------------------- DetermineBestSupportingPosition -----------------
//
//  see header or book for description
//-----------------------------------------------------------------------------
Vector2D Prior__SupportSpotCalculator::DetermineBestSupportingPosition()
{
  //only update the spots every few frames                              
  if (!m_pRegulator->isReady() && m_pBestSupportingSpot)
  {
    return m_pBestSupportingSpot->m_vPos;
  }

  //reset the best supporting spot
  m_pBestSupportingSpot = NULL;
 
  double BestScoreSoFar = 0.0;

  std::vector<SupportSpot>::iterator curSpot;

  for (curSpot = m_Spots.begin(); curSpot != m_Spots.end(); ++curSpot)
  {
    //first remove any previous score. (the score is set to one so that
    //the viewer can see the positions of all the spots if he has the 
    //aids turned on)
    curSpot->m_dScore = 1.0;

    //Test 1. is it possible to make a safe pass from the ball's position 
    //to this position?
    if(m_pTeam->isPassSafeFromAllOpponents(m_pTeam->ControllingPlayer()->Pos(),
                                           curSpot->m_vPos,
                                           NULL,
                                           Prm.MaxPassingForce))
    {
      curSpot->m_dScore += Prm.Spot_PassSafeScore;
    }
      
   
    //Test 2. Determine if a goal can be scored from this position.  
    if( m_pTeam->CanShoot(curSpot->m_vPos,            
                          Prm.MaxShootingForce))
    {
      curSpot->m_dScore += Prm.Spot_CanScoreFromPositionScore;
    }   

    
    //Test 3. calculate how far this spot is away from the controlling
    //player. The further away, the higher the score. Any distances further
    //away than OptimalDistance pixels do not receive a score.
    if (m_pTeam->SupportingPlayer())
    {
      const double OptimalDistance = 200.0;
        
      double dist = Vec2DDistance(m_pTeam->ControllingPlayer()->Pos(),
                                 curSpot->m_vPos);
      
      double temp = fabs(OptimalDistance - dist);

      if (temp < OptimalDistance)
      {

        //normalize the distance and add it to the score
        curSpot->m_dScore += Prm.Spot_DistFromControllingPlayerScore *
                             (OptimalDistance-temp)/OptimalDistance;  
      }
    }
    
    //check to see if this spot has the highest score so far
    if (curSpot->m_dScore > BestScoreSoFar)
    {
      BestScoreSoFar = curSpot->m_dScore;

      m_pBestSupportingSpot = &(*curSpot);
    }    
    
  }

  return m_pBestSupportingSpot->m_vPos;
}