Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 3, 2012, 12:04:35 AM (12 years ago)
Author:
landauf
Message:

store the next checkpoints in a set instead of Vector3
+ some refactoring in SpaceRaceManager

Location:
code/trunk/src/modules/gametypes
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/modules/gametypes/RaceCheckPoint.cc

    r9260 r9263  
    5858
    5959        this->checkpointIndex_ = 0;
    60         this->nextcheckpoints_ = Vector3::ZERO;
    6160        this->bIsLast_ = false;
    6261        this->timeLimit_ = 0;
     
    7675        XMLPortParam(RaceCheckPoint, "islast", setLast, isLast, xmlelement, mode).defaultValues(false);
    7776        XMLPortParam(RaceCheckPoint, "timelimit", setTimelimit, getTimeLimit, xmlelement, mode).defaultValues(0);
    78         XMLPortParamTemplate(RaceCheckPoint, "nextcheckpoints", setNextcheckpoint, getNextcheckpoint, xmlelement, mode, const Vector3&).defaultValues(Vector3::ZERO);
     77        XMLPortParam(RaceCheckPoint, "nextcheckpoints", setNextCheckpointsAsVector3, getNextCheckpointsAsVector3, xmlelement, mode);
    7978    }
    8079
     
    102101        }
    103102    }
     103
     104    void RaceCheckPoint::setNextCheckpointsAsVector3(const Vector3& checkpoints)
     105    {
     106        this->nextCheckpoints_.clear();
     107
     108        if (checkpoints.x > -1)
     109            this->nextCheckpoints_.insert(static_cast<int>(checkpoints.x + 0.5));
     110        if (checkpoints.y > -1)
     111            this->nextCheckpoints_.insert(static_cast<int>(checkpoints.y + 0.5));
     112        if (checkpoints.z > -1)
     113            this->nextCheckpoints_.insert(static_cast<int>(checkpoints.z + 0.5));
     114    }
     115
     116    Vector3 RaceCheckPoint::getNextCheckpointsAsVector3() const
     117    {
     118        Vector3 checkpoints = Vector3(-1, -1, -1);
     119
     120        size_t count = 0;
     121        for (std::set<int>::iterator it = this->nextCheckpoints_.begin(); it != this->nextCheckpoints_.end(); ++it)
     122        {
     123            switch (count)
     124            {
     125                case 0: checkpoints.x = static_cast<Ogre::Real>(*it); break;
     126                case 1: checkpoints.y = static_cast<Ogre::Real>(*it); break;
     127                case 2: checkpoints.z = static_cast<Ogre::Real>(*it); break;
     128            }
     129            ++count;
     130        }
     131
     132        return checkpoints;
     133    }
    104134}
  • code/trunk/src/modules/gametypes/RaceCheckPoint.h

    r9262 r9263  
    5454                { return this->checkpointIndex_; }
    5555
    56             inline void setNextcheckpoint(const Vector3& checkpoints)
    57                 { this->nextcheckpoints_ = checkpoints; }
    58             inline const Vector3& getNextcheckpoint() const
    59                 { return this->nextcheckpoints_; }
     56            void setNextCheckpointsAsVector3(const Vector3& checkpoints);
     57            Vector3 getNextCheckpointsAsVector3() const;
     58            const std::set<int>& getNextCheckpoints() const
     59                { return this->nextCheckpoints_; }
    6060
    6161            inline void setLast(bool isLast)
     
    8080
    8181        private:
    82             int checkpointIndex_;     ///< The index of this check point. The race starts with the check point with the index 0
    83             Vector3 nextcheckpoints_; ///< the indexes of the next check points
    84             bool bIsLast_;            ///< True if this check point is the last of the level. There can be only one last check point for each level and there must be a last check point in the level.
    85             float timeLimit_;         ///< The time limit (from the start of the level) to reach this check point. If the check point is reached after this time, the game ends and the player looses.
    86             PlayerInfo* player_;      ///< The player that reached the checkpoint
     82            int checkpointIndex_;           ///< The index of this check point. The race starts with the check point with the index 0
     83            std::set<int> nextCheckpoints_; ///< the indexes of the next check points
     84            bool bIsLast_;                  ///< True if this check point is the last of the level. There can be only one last check point for each level and there must be a last check point in the level.
     85            float timeLimit_;               ///< The time limit (from the start of the level) to reach this check point. If the check point is reached after this time, the game ends and the player looses.
     86            PlayerInfo* player_;            ///< The player that reached the checkpoint
    8787    };
    8888}
  • code/trunk/src/modules/gametypes/SpaceRace.cc

    r9262 r9263  
    104104    void SpaceRace::newCheckpointReached(RaceCheckPoint* checkpoint, PlayerInfo* player)
    105105    {
    106         int index = checkpoint->getCheckpointIndex();
    107         this->checkpointReached_[player] = index;
     106        this->checkpointReached_[player] = checkpoint;
    108107
    109108        this->clock_.capture();
     
    111110        int ms = this->clock_.getMilliseconds() % 1000;
    112111
    113         const std::string& message = "Checkpoint " + multi_cast<std::string>(index + 1)
     112        const std::string& message = "Checkpoint " + multi_cast<std::string>(checkpoint->getCheckpointIndex() + 1)
    114113            + " reached after " + multi_cast<std::string>(s) + "." + multi_cast<std::string>(ms) + " seconds.";
    115114        const_cast<GametypeInfo*>(this->getGametypeInfo())->sendAnnounceMessage(message);
    116115        ChatManager::message(message);
    117     }
    118 
    119     void SpaceRace::playerEntered(PlayerInfo* player)
    120     {
    121         Gametype::playerEntered(player);
    122 
    123         this->checkpointReached_[player] = -1;
    124116    }
    125117
  • code/trunk/src/modules/gametypes/SpaceRace.h

    r9262 r9263  
    6262
    6363            void newCheckpointReached(RaceCheckPoint* checkpoint, PlayerInfo* player);
    64             inline int getCheckpointReached(PlayerInfo* player)
     64            inline RaceCheckPoint* getCheckpointReached(PlayerInfo* player)
    6565                { return this->checkpointReached_[player]; }
    6666
     
    7474            bool allowPawnDeath(Pawn* victim, Pawn* originator);
    7575
    76         protected:
    77             virtual void playerEntered(PlayerInfo* player); ///< Initializes values.
    78 
    7976        private:
    80             bool cantMove_;                                ///< Helper variable, used to stall the engines before the race starts.
    81             std::map<PlayerInfo*, int> checkpointReached_; ///< The number of the last check point reached by each player.
    82             bool bTimeIsUp_;                               ///< True if one of the check points is reached too late.
     77            bool cantMove_;                                            ///< Helper variable, used to stall the engines before the race starts.
     78            std::map<PlayerInfo*, RaceCheckPoint*> checkpointReached_; ///< The number of the last check point reached by each player.
     79            bool bTimeIsUp_;                                           ///< True if one of the check points is reached too late.
    8380
    8481            Clock clock_; ///< The clock starts running at the beginning of the game. It is used to give the time at each check point, the give the time at the end of the game, and to stop the game if a check point is reached too late.
  • code/trunk/src/modules/gametypes/SpaceRaceManager.cc

    r9262 r9263  
    105105    }
    106106
    107     void SpaceRaceManager::checkpointReached(RaceCheckPoint* check, PlayerInfo* player)
     107    bool SpaceRaceManager::reachedValidCheckpoint(RaceCheckPoint* oldCheckpoint, RaceCheckPoint* newCheckpoint, PlayerInfo* player) const
     108    {
     109        if (oldCheckpoint)
     110        {
     111            // the player already visited an old checkpoint; see which checkpoints are possible now
     112            const std::set<int>& possibleCheckpoints = oldCheckpoint->getNextCheckpoints();
     113            for (std::set<int>::const_iterator it = possibleCheckpoints.begin(); it != possibleCheckpoints.end(); ++it)
     114                if (this->findCheckpoint(*it) == newCheckpoint)
     115                    return true;
     116            return false;
     117        }
     118        else
     119        {
     120            // the player hasn't visited a checkpoint yet, so he must reach the checkpoint with index 0 (hack?)
     121            return (newCheckpoint->getCheckpointIndex() == 0);
     122        }
     123    }
     124
     125    void SpaceRaceManager::checkpointReached(RaceCheckPoint* newCheckpoint, PlayerInfo* player)
    108126    {
    109127        SpaceRace* gametype = orxonox_cast<SpaceRace*>(this->getGametype().get());
    110128        assert(gametype);
     129        if (!gametype)
     130            return;
    111131
    112         bool reachedValidCheckpoint = false;
     132        RaceCheckPoint* oldCheckpoint = gametype->getCheckpointReached(player);
    113133
    114         int index = gametype->getCheckpointReached(player);
    115         if (index > -1)
     134        if (this->reachedValidCheckpoint(oldCheckpoint, newCheckpoint, player))
    116135        {
    117             Vector3 v = this->findCheckpoint(index)->getNextcheckpoint();
    118 
    119             if (this->findCheckpoint(v.x) == check)
    120             {
    121                 reachedValidCheckpoint = true;
    122             }
    123             if (this->findCheckpoint(v.y) == check)
    124             {
    125                 reachedValidCheckpoint = true;
    126             }
    127             if (this->findCheckpoint(v.z) == check)
    128             {
    129                 reachedValidCheckpoint = true;
    130             }
    131         }
    132         else
    133         {
    134             reachedValidCheckpoint = (check->getCheckpointIndex() == 0);
    135         }
    136 
    137         if (gametype && reachedValidCheckpoint)
    138         {
     136            // the player reached a valid checkpoint
    139137            gametype->getClock().capture();
    140138            float time = gametype->getClock().getSecondsPrecise();
    141             if (check->getTimeLimit() != 0 && time > check->getTimeLimit())
     139            if (newCheckpoint->getTimeLimit() != 0 && time > newCheckpoint->getTimeLimit())
    142140            {
     141                // time's up - the player has lost the game
    143142                gametype->setTimeIsUp();
    144143                gametype->end();
    145144            }
    146             else if (check->isLast())
     145            else if (newCheckpoint->isLast())
     146            {
     147                // the last checkpoint was reached - the player has won the game
    147148                gametype->end();
     149            }
    148150            else
    149151                        {
    150                 if (index > -1)
    151                     this->setRadarVisibility(player, false);
    152                 else
    153                     this->findCheckpoint(0)->setRadarVisibility(false);
    154 
    155                 gametype->newCheckpointReached(check, player);
    156                 this->setRadarVisibility(player, true);
     152                // adjust the radarvisibility
     153                gametype->newCheckpointReached(newCheckpoint, player);
     154                this->updateRadarVisibility(oldCheckpoint, newCheckpoint);
    157155            }
    158156        }
    159157
    160         check->resetPlayer();
     158        newCheckpoint->resetPlayer();
    161159    }
    162160
    163     void SpaceRaceManager::setRadarVisibility(PlayerInfo* player, bool bVisible)
     161    void SpaceRaceManager::updateRadarVisibility(RaceCheckPoint* oldCheckpoint, RaceCheckPoint* newCheckpoint) const
    164162    {
    165         SpaceRace* gametype = orxonox_cast<SpaceRace*>(this->getGametype().get());
    166         assert(gametype);
    167         int index = gametype->getCheckpointReached(player);
    168         Vector3 v = this->findCheckpoint(index)->getNextcheckpoint();
     163        if (oldCheckpoint)
     164        {
     165            const std::set<int>& oldVisible = oldCheckpoint->getNextCheckpoints();
     166            for (std::set<int>::const_iterator it = oldVisible.begin(); it != oldVisible.end(); ++it)
     167                this->findCheckpoint(*it)->setRadarVisibility(false);
     168        }
    169169
    170         if (v.x > -1)
     170        if (newCheckpoint)
    171171        {
    172             this->findCheckpoint(v.x)->setRadarVisibility(bVisible);
    173             this->findCheckpoint(v.x)->settingsChanged();
    174         }
    175         if (v.y > -1)
    176         {
    177             this->findCheckpoint(v.y)->setRadarVisibility(bVisible);
    178             this->findCheckpoint(v.y)->settingsChanged();
    179         }
    180         if (v.z > -1)
    181         {
    182             this->findCheckpoint(v.z)->setRadarVisibility(bVisible);
    183             this->findCheckpoint(v.z)->settingsChanged();
     172            newCheckpoint->setRadarVisibility(false);
     173
     174            const std::set<int>& newVisible = newCheckpoint->getNextCheckpoints();
     175            for (std::set<int>::const_iterator it = newVisible.begin(); it != newVisible.end(); ++it)
     176                this->findCheckpoint(*it)->setRadarVisibility(true);
    184177        }
    185178    }
  • code/trunk/src/modules/gametypes/SpaceRaceManager.h

    r9262 r9263  
    6565            RaceCheckPoint* findCheckpoint(int index) const;
    6666
    67             void checkpointReached(RaceCheckPoint* check, PlayerInfo* player);
     67            void checkpointReached(RaceCheckPoint* newCheckpoint, PlayerInfo* player);
    6868
    6969            void tick(float dt);
    7070
    7171        protected:
    72             void setRadarVisibility(PlayerInfo* player, bool bVisible); ///< sets RadarVisibility of the checkpoints the player can reach.
     72            bool reachedValidCheckpoint(RaceCheckPoint* oldCheckpoint, RaceCheckPoint* newCheckpoint, PlayerInfo* player) const;
     73            void updateRadarVisibility(RaceCheckPoint* oldCheckpoint, RaceCheckPoint* newCheckpoint) const;
    7374
    7475        private:
Note: See TracChangeset for help on using the changeset viewer.