Removed rescue-code duplication in battle mode and soccer mode.

This commit is contained in:
hiker 2015-07-31 16:27:52 +10:00
parent 431853b451
commit a81366c414
7 changed files with 68 additions and 151 deletions

View File

@ -160,6 +160,37 @@ void OverWorld::update(float dt)
}
} // update
// ----------------------------------------------------------------------------
/** Finds the starting position which is closest to the kart.
* \param kart The kart for which a rescue position needs to be determined.
*/
unsigned int OverWorld::getRescuePositionIndex(AbstractKart *kart)
{
// find closest point to drop kart on
const int start_spots_amount = getNumberOfRescuePositions();
assert(start_spots_amount > 0);
int closest_id = -1;
float closest_distance = 999999999.0f;
for (int n=0; n<start_spots_amount; n++)
{
const btTransform &s = getStartTransform(n);
const Vec3 &v = s.getOrigin();
float abs_distance = (v - kart->getXYZ()).length();
if (abs_distance < closest_distance)
{
closest_distance = abs_distance;
closest_id = n;
}
}
assert(closest_id != -1);
return closest_id;
} // getRescuePositionIndex
//-----------------------------------------------------------------------------
/** This function is not used in the overworld race gui.
*/

View File

@ -48,6 +48,7 @@ public:
static void enterOverWorld();
virtual void update(float delta) OVERRIDE;
unsigned int getRescuePositionIndex(AbstractKart *kart) OVERRIDE;
virtual void getKartsDisplayInfo(
std::vector<RaceGUIBase::KartIconDisplayInfo> *info) OVERRIDE;
// ------------------------------------------------------------------------

View File

@ -315,88 +315,6 @@ void SoccerWorld::getKartsDisplayInfo(
*/
} // getKartsDisplayInfo
//-----------------------------------------------------------------------------
/** Moves a kart to its rescue position.
* \param kart The kart that was rescued.
*/
void SoccerWorld::moveKartAfterRescue(AbstractKart* kart)
{
// find closest point to drop kart on
World *world = World::getWorld();
const int start_spots_amount = world->getTrack()->getNumberOfStartPositions();
assert(start_spots_amount > 0);
float largest_accumulated_distance_found = -1;
int furthest_id_found = -1;
const float kart_x = kart->getXYZ().getX();
const float kart_z = kart->getXYZ().getZ();
for(int n=0; n<start_spots_amount; n++)
{
// no need for the overhead to compute exact distance with sqrt(),
// so using the 'manhattan' heuristic which will do fine enough.
const btTransform &s = getStartTransform(n);
const Vec3 &v=s.getOrigin();
float accumulatedDistance = .0f;
bool spawnPointClear = true;
for(unsigned int k=0; k<getCurrentNumKarts(); k++)
{
const AbstractKart *currentKart = World::getWorld()->getKart(k);
const float currentKart_x = currentKart->getXYZ().getX();
const float currentKartk_z = currentKart->getXYZ().getZ();
if(kart_x!=currentKart_x && kart_z !=currentKartk_z)
{
float absDistance = fabs(currentKart_x - v.getX()) +
fabs(currentKartk_z - v.getZ());
if(absDistance < CLEAR_SPAWN_RANGE)
{
spawnPointClear = false;
break;
}
accumulatedDistance += absDistance;
}
}
if(largest_accumulated_distance_found < accumulatedDistance && spawnPointClear)
{
furthest_id_found = n;
largest_accumulated_distance_found = accumulatedDistance;
}
}
assert(furthest_id_found != -1);
const btTransform &s = getStartTransform(furthest_id_found);
const Vec3 &xyz = s.getOrigin();
kart->setXYZ(xyz);
kart->setRotation(s.getRotation());
//position kart from same height as in World::resetAllKarts
btTransform pos;
pos.setOrigin(kart->getXYZ()+btVector3(0, 0.5f*kart->getKartHeight(), 0.0f));
pos.setRotation( btQuaternion(btVector3(0.0f, 1.0f, 0.0f), 0 /* angle */) );
kart->getBody()->setCenterOfMassTransform(pos);
//project kart to surface of track
bool kart_over_ground = m_track->findGround(kart);
if (kart_over_ground)
{
//add vertical offset so that the kart starts off above the track
float vertical_offset = kart->getKartProperties()->getVertRescueOffset() *
kart->getKartHeight();
kart->getBody()->translate(btVector3(0, vertical_offset, 0));
}
else
{
Log::warn("[SoccerWorld]", " Invalid position after rescue for kart %s on track %s.",
kart->getIdent().c_str(), m_track->getIdent().c_str());
}
} // moveKartAfterRescue
//-----------------------------------------------------------------------------
/** Set position and team for the karts */
void SoccerWorld::initKartList()

View File

@ -76,8 +76,7 @@ public:
virtual void getKartsDisplayInfo(
std::vector<RaceGUIBase::KartIconDisplayInfo> *info);
int getScore(unsigned int i);
virtual bool raceHasLaps(){ return false; }
virtual void moveKartAfterRescue(AbstractKart* kart);
virtual bool raceHasLaps() { return false; }
virtual const std::string& getIdent() const;

View File

@ -477,53 +477,3 @@ void ThreeStrikesBattle::getKartsDisplayInfo(
}
} // getKartsDisplayInfo
//-----------------------------------------------------------------------------
/** Determines the rescue position for a kart. The rescue position is the
* start position which is has the biggest accumulated distance to all other
* karts, and which has no other kart very close. The latter avoids dropping
* a kart on top of another kart.
* \param kart The kart that is going to be rescued.
* \returns The index of the start position to which the rescued kart
* should be moved to.
*/
unsigned int ThreeStrikesBattle::getRescuePositionIndex(AbstractKart *kart)
{
const int start_spots_amount = getTrack()->getNumberOfStartPositions();
assert(start_spots_amount > 0);
float largest_accumulated_distance_found = -1;
int furthest_id_found = -1;
for(int n=0; n<start_spots_amount; n++)
{
const btTransform &s = getStartTransform(n);
const Vec3 &v=s.getOrigin();
float accumulated_distance = .0f;
bool spawn_point_clear = true;
for(unsigned int k=0; k<getCurrentNumKarts(); k++)
{
if(kart->getWorldKartId()==k) continue;
float abs_distance2 = (getKart(k)->getXYZ()-v).length2_2d();
const float CLEAR_SPAWN_RANGE2 = 5*5;
if( abs_distance2 < CLEAR_SPAWN_RANGE2)
{
spawn_point_clear = false;
break;
}
accumulated_distance += sqrt(abs_distance2);
}
if(accumulated_distance > largest_accumulated_distance_found &&
spawn_point_clear)
{
furthest_id_found = n;
largest_accumulated_distance_found = accumulated_distance;
}
}
assert(furthest_id_found != -1);
return furthest_id_found;
} // getRescuePositionIndex

View File

@ -97,7 +97,6 @@ public:
virtual void getKartsDisplayInfo(
std::vector<RaceGUIBase::KartIconDisplayInfo> *info);
virtual bool raceHasLaps(){ return false; }
virtual unsigned int getRescuePositionIndex(AbstractKart *kart);
virtual const std::string& getIdent() const;

View File

@ -134,35 +134,54 @@ unsigned int WorldWithRank::getNumberOfRescuePositions() const
return getTrack()->getNumberOfStartPositions();
} // getNumberOfRescuePositions
// ----------------------------------------------------------------------------
/** Finds the starting position which is closest to the kart.
* \param kart The kart for which a rescue position needs to be determined.
//-----------------------------------------------------------------------------
/** Determines the rescue position for a kart. The rescue position is the
* start position which is has the biggest accumulated distance to all other
* karts, and which has no other kart very close. The latter avoids dropping
* a kart on top of another kart. This is the method used
* \param kart The kart that is going to be rescued.
* \returns The index of the start position to which the rescued kart
* should be moved to.
*/
unsigned int WorldWithRank::getRescuePositionIndex(AbstractKart *kart)
{
// find closest point to drop kart on
const int start_spots_amount = getNumberOfRescuePositions();
const int start_spots_amount = getTrack()->getNumberOfStartPositions();
assert(start_spots_amount > 0);
int closest_id = -1;
float closest_distance = 999999999.0f;
float largest_accumulated_distance_found = -1;
int furthest_id_found = -1;
for (int n=0; n<start_spots_amount; n++)
for(int n=0; n<start_spots_amount; n++)
{
const btTransform &s = getStartTransform(n);
const Vec3 &v = s.getOrigin();
const Vec3 &v=s.getOrigin();
float accumulated_distance = .0f;
bool spawn_point_clear = true;
float abs_distance = (v - kart->getXYZ()).length();
if (abs_distance < closest_distance)
for(unsigned int k=0; k<getCurrentNumKarts(); k++)
{
closest_distance = abs_distance;
closest_id = n;
if(kart->getWorldKartId()==k) continue;
float abs_distance2 = (getKart(k)->getXYZ()-v).length2_2d();
const float CLEAR_SPAWN_RANGE2 = 5*5;
if( abs_distance2 < CLEAR_SPAWN_RANGE2)
{
spawn_point_clear = false;
break;
}
accumulated_distance += sqrt(abs_distance2);
}
if(accumulated_distance > largest_accumulated_distance_found &&
spawn_point_clear)
{
furthest_id_found = n;
largest_accumulated_distance_found = accumulated_distance;
}
}
assert(closest_id != -1);
return closest_id;
assert(furthest_id_found != -1);
return furthest_id_found;
} // getRescuePositionIndex
// ----------------------------------------------------------------------------