From a81366c4140ec5610c9d8fbfaf4b1b23720980e5 Mon Sep 17 00:00:00 2001 From: hiker Date: Fri, 31 Jul 2015 16:27:52 +1000 Subject: [PATCH] Removed rescue-code duplication in battle mode and soccer mode. --- src/modes/overworld.cpp | 31 +++++++++++ src/modes/overworld.hpp | 1 + src/modes/soccer_world.cpp | 82 ------------------------------ src/modes/soccer_world.hpp | 3 +- src/modes/three_strikes_battle.cpp | 50 ------------------ src/modes/three_strikes_battle.hpp | 1 - src/modes/world_with_rank.cpp | 51 +++++++++++++------ 7 files changed, 68 insertions(+), 151 deletions(-) diff --git a/src/modes/overworld.cpp b/src/modes/overworld.cpp index 0f5166caa..ba7e7df2d 100644 --- a/src/modes/overworld.cpp +++ b/src/modes/overworld.cpp @@ -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; ngetXYZ()).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. */ diff --git a/src/modes/overworld.hpp b/src/modes/overworld.hpp index 4001f0faa..62d510994 100644 --- a/src/modes/overworld.hpp +++ b/src/modes/overworld.hpp @@ -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 *info) OVERRIDE; // ------------------------------------------------------------------------ diff --git a/src/modes/soccer_world.cpp b/src/modes/soccer_world.cpp index 0fc8f553c..2b18d14f6 100644 --- a/src/modes/soccer_world.cpp +++ b/src/modes/soccer_world.cpp @@ -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; ngetKart(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() diff --git a/src/modes/soccer_world.hpp b/src/modes/soccer_world.hpp index d33f1dd74..8d896d47c 100644 --- a/src/modes/soccer_world.hpp +++ b/src/modes/soccer_world.hpp @@ -76,8 +76,7 @@ public: virtual void getKartsDisplayInfo( std::vector *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; diff --git a/src/modes/three_strikes_battle.cpp b/src/modes/three_strikes_battle.cpp index d24555bf8..6fa48f882 100644 --- a/src/modes/three_strikes_battle.cpp +++ b/src/modes/three_strikes_battle.cpp @@ -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; ngetWorldKartId()==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 - diff --git a/src/modes/three_strikes_battle.hpp b/src/modes/three_strikes_battle.hpp index 46a18674c..fe09b9340 100644 --- a/src/modes/three_strikes_battle.hpp +++ b/src/modes/three_strikes_battle.hpp @@ -97,7 +97,6 @@ public: virtual void getKartsDisplayInfo( std::vector *info); virtual bool raceHasLaps(){ return false; } - virtual unsigned int getRescuePositionIndex(AbstractKart *kart); virtual const std::string& getIdent() const; diff --git a/src/modes/world_with_rank.cpp b/src/modes/world_with_rank.cpp index 9482d1f95..f24ef58c3 100644 --- a/src/modes/world_with_rank.cpp +++ b/src/modes/world_with_rank.cpp @@ -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; ngetXYZ()).length(); - - if (abs_distance < closest_distance) + for(unsigned int k=0; kgetWorldKartId()==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 // ----------------------------------------------------------------------------