Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 9024 for code


Ignore:
Timestamp:
Feb 26, 2012, 10:36:04 PM (13 years ago)
Author:
jo
Message:

I restructured the inheritance of gametypes: The base class of teamgametypes is now the class 'TeamGametype'. Now it is possible to create teamgametypes without needing the messages contained in TeamDeathmatch. By doing so the class 'TeamDeathmatch' exactly does the same as the Deathmatch class.

Location:
code/branches/pCuts/src/orxonox
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/pCuts/src/orxonox/OrxonoxPrereqs.h

    r8858 r9024  
    109109    class TeamDeathmatch;
    110110    class UnderAttack;
     111    class TeamGametype;
    111112
    112113    // graphics
  • code/branches/pCuts/src/orxonox/gametypes/TeamDeathmatch.cc

    r7182 r9024  
    3131#include "core/CoreIncludes.h"
    3232#include "core/ConfigValueIncludes.h"
     33#include "chat/ChatManager.h"
    3334#include "interfaces/TeamColourable.h"
     35#include "infos/PlayerInfo.h"
    3436#include "worldentities/TeamSpawnPoint.h"
    3537#include "worldentities/pawns/Pawn.h"
     
    3941    CreateUnloadableFactory(TeamDeathmatch);
    4042
    41     TeamDeathmatch::TeamDeathmatch(BaseObject* creator) : Deathmatch(creator)
     43    TeamDeathmatch::TeamDeathmatch(BaseObject* creator) : TeamGametype(creator)
    4244    {
    4345        RegisterObject(TeamDeathmatch);
    44 
    45         this->teams_ = 2;
    46 
    47         this->setConfigValues();
    4846    }
    4947
    50     void TeamDeathmatch::setConfigValues()
     48    void TeamDeathmatch::start()
    5149    {
    52         SetConfigValue(teams_, 2);
     50        TeamGametype::start();
    5351
    54         static ColourValue colours[] =
    55         {
    56             ColourValue(1.0f, 0.3f, 0.3f),
    57             ColourValue(0.3f, 0.3f, 1.0f),
    58             ColourValue(0.3f, 1.0f, 0.3f),
    59             ColourValue(1.0f, 1.0f, 0.0f)
    60         };
    61         static std::vector<ColourValue> defaultcolours(colours, colours + sizeof(colours) / sizeof(ColourValue));
     52        std::string message("The match has started!");
     53        ChatManager::message(message);
     54    }
    6255
    63         SetConfigValue(teamcolours_, defaultcolours);
     56    void TeamDeathmatch::end()
     57    {
     58        TeamGametype::end();
     59
     60        std::string message("The match has ended.");
     61        ChatManager::message(message);
    6462    }
    6563
    6664    void TeamDeathmatch::playerEntered(PlayerInfo* player)
    6765    {
    68         Deathmatch::playerEntered(player);
     66        TeamGametype::playerEntered(player);
    6967
    70         std::vector<unsigned int> playersperteam(this->teams_, 0);
    71 
    72         for (std::map<PlayerInfo*, int>::iterator it = this->teamnumbers_.begin(); it != this->teamnumbers_.end(); ++it)
    73             if (it->second < static_cast<int>(this->teams_) && it->second >= 0)
    74                 playersperteam[it->second]++;
    75 
    76         unsigned int minplayers = static_cast<unsigned int>(-1);
    77         size_t minplayersteam = 0;
    78         for (size_t i = 0; i < this->teams_; ++i)
    79         {
    80             if (playersperteam[i] < minplayers)
    81             {
    82                 minplayers = playersperteam[i];
    83                 minplayersteam = i;
    84             }
    85         }
    86 
    87         this->teamnumbers_[player] = minplayersteam;
     68        const std::string& message = player->getName() + " entered the game";
     69        ChatManager::message(message);
    8870    }
    8971
    9072    bool TeamDeathmatch::playerLeft(PlayerInfo* player)
    9173    {
    92         bool valid_player = Deathmatch::playerLeft(player);
     74        bool valid_player = TeamGametype::playerLeft(player);
    9375
    9476        if (valid_player)
    95             this->teamnumbers_.erase(player);
     77        {
     78            const std::string& message = player->getName() + " left the game";
     79            ChatManager::message(message);
     80        }
     81
     82        return valid_player;
     83    }
     84    bool TeamDeathmatch::playerChangedName(PlayerInfo* player)
     85    {
     86        bool valid_player = TeamGametype::playerChangedName(player);
     87
     88        if (valid_player)
     89        {
     90            const std::string& message = player->getOldName() + " changed name to " + player->getName();
     91            ChatManager::message(message);
     92        }
    9693
    9794        return valid_player;
    9895    }
    9996
    100     bool TeamDeathmatch::allowPawnHit(Pawn* victim, Pawn* originator)
     97    void TeamDeathmatch::pawnKilled(Pawn* victim, Pawn* killer)
    10198    {
    102         return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator);
     99        if (victim && victim->getPlayer())
     100        {
     101            std::string message;
     102            if (killer)
     103            {
     104                if (killer->getPlayer())
     105                    message = victim->getPlayer()->getName() + " was killed by " + killer->getPlayer()->getName();
     106                else
     107                    message = victim->getPlayer()->getName() + " was killed";
     108            }
     109            else
     110                message = victim->getPlayer()->getName() + " died";
     111
     112            ChatManager::message(message);
     113        }
     114
     115        Gametype::pawnKilled(victim, killer);
    103116    }
    104117
    105     bool TeamDeathmatch::allowPawnDamage(Pawn* victim, Pawn* originator)
     118    void TeamDeathmatch::playerScored(PlayerInfo* player)
    106119    {
    107         return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator);
    108     }
     120        TeamGametype::playerScored(player);
    109121
    110     bool TeamDeathmatch::allowPawnDeath(Pawn* victim, Pawn* originator)
    111     {
    112         return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator);
    113     }
    114 
    115     SpawnPoint* TeamDeathmatch::getBestSpawnPoint(PlayerInfo* player) const
    116     {
    117         int desiredTeamNr = -1;
    118         std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player);
    119         if (it_player != this->teamnumbers_.end())
    120             desiredTeamNr = it_player->second;
    121 
    122         // Only use spawnpoints of the own team (or non-team-spawnpoints)
    123         std::set<SpawnPoint*> teamSpawnPoints = this->spawnpoints_;
    124         for (std::set<SpawnPoint*>::iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); )
     122        if (player)
    125123        {
    126             if ((*it)->isA(Class(TeamSpawnPoint)))
    127             {
    128                 TeamSpawnPoint* tsp = orxonox_cast<TeamSpawnPoint*>(*it);
    129                 if (tsp && static_cast<int>(tsp->getTeamNumber()) != desiredTeamNr)
    130                 {
    131                     teamSpawnPoints.erase(it++);
    132                     continue;
    133                 }
    134             }
    135 
    136             ++it;
    137         }
    138 
    139         SpawnPoint* fallbackSpawnPoint = NULL;
    140         if (teamSpawnPoints.size() > 0)
    141         {
    142             unsigned int randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(teamSpawnPoints.size())));
    143             unsigned int index = 0;
    144             // Get random fallback spawnpoint in case there is no active SpawnPoint.
    145             for (std::set<SpawnPoint*>::const_iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ++it)
    146             {
    147                 if (index == randomspawn)
    148                 {
    149                     fallbackSpawnPoint = (*it);
    150                     break;
    151                 }
    152 
    153                 ++index;
    154             }
    155 
    156             // Remove all inactive SpawnPoints from the list.
    157             for (std::set<SpawnPoint*>::iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); )
    158             {
    159                 if(!(*it)->isActive())
    160                 {
    161                     teamSpawnPoints.erase(it++);
    162                     continue;
    163                 }
    164 
    165                 ++it;
    166             }
    167 
    168             randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(teamSpawnPoints.size())));
    169             index = 0;
    170             for (std::set<SpawnPoint*>::const_iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ++it)
    171             {
    172                 if (index == randomspawn)
    173                     return (*it);
    174 
    175                 ++index;
    176             }
    177 
    178             return fallbackSpawnPoint;
    179         }
    180 
    181         return 0;
    182     }
    183 
    184     void TeamDeathmatch::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn)
    185     {
    186         if (!player)
    187             return;
    188 
    189         // Set the team colour
    190         std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player);
    191         if (it_player != this->teamnumbers_.end() && it_player->second >= 0 && it_player->second < static_cast<int>(this->teamcolours_.size()))
    192         {
    193             if (pawn)
    194             {
    195                 pawn->setRadarObjectColour(this->teamcolours_[it_player->second]);
    196 
    197                 std::set<WorldEntity*> pawnAttachments = pawn->getAttachedObjects();
    198                 for (std::set<WorldEntity*>::iterator it = pawnAttachments.begin(); it != pawnAttachments.end(); ++it)
    199                 {
    200                     if ((*it)->isA(Class(TeamColourable)))
    201                     {
    202                         TeamColourable* tc = orxonox_cast<TeamColourable*>(*it);
    203                         tc->setTeamColour(this->teamcolours_[it_player->second]);
    204                     }
    205                 }
    206             }
     124            const std::string& message = player->getName() + " scores!";
     125            ChatManager::message(message);
    207126        }
    208127    }
    209128
    210     bool TeamDeathmatch::pawnsAreInTheSameTeam(Pawn* pawn1, Pawn* pawn2)
    211     {
    212         if (pawn1 && pawn2)
    213         {
    214             std::map<PlayerInfo*, int>::const_iterator it1 = this->teamnumbers_.find(pawn1->getPlayer());
    215             std::map<PlayerInfo*, int>::const_iterator it2 = this->teamnumbers_.find(pawn2->getPlayer());
    216 
    217             if (it1 != this->teamnumbers_.end() && it2 != this->teamnumbers_.end())
    218                 return (it1->second == it2->second);
    219         }
    220         return false;
    221     }
    222 
    223     int TeamDeathmatch::getTeam(PlayerInfo* player)
    224     {
    225         std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player);
    226         if (it_player != this->teamnumbers_.end())
    227             return it_player->second;
    228         else
    229             return -1;
    230     }
    231129}
  • code/branches/pCuts/src/orxonox/gametypes/TeamDeathmatch.h

    r9016 r9024  
    3131
    3232#include "OrxonoxPrereqs.h"
    33 
    34 #include <map>
    35 #include <vector>
    36 #include "Deathmatch.h"
     33#include "TeamGametype.h"
    3734
    3835namespace orxonox
    3936{
    40     class _OrxonoxExport TeamDeathmatch : public Deathmatch
     37    class _OrxonoxExport TeamDeathmatch : public TeamGametype
    4138    {
    4239        public:
     
    4441            virtual ~TeamDeathmatch() {}
    4542
    46             void setConfigValues();
    47 
     43            virtual void start();
     44            virtual void end();
    4845            virtual void playerEntered(PlayerInfo* player);
    4946            virtual bool playerLeft(PlayerInfo* player);
     47            virtual bool playerChangedName(PlayerInfo* player);
    5048
    51             virtual bool allowPawnHit(Pawn* victim, Pawn* originator = 0);
    52             virtual bool allowPawnDamage(Pawn* victim, Pawn* originator = 0);
    53             virtual bool allowPawnDeath(Pawn* victim, Pawn* originator = 0);
    54 
    55             virtual void playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn);
    56 
    57             int getTeam(PlayerInfo* player);
    58             inline const ColourValue& getTeamColour(int teamnr) const
    59                 { return this->teamcolours_[teamnr]; }
    60 
    61         protected:
    62             virtual SpawnPoint* getBestSpawnPoint(PlayerInfo* player) const;
    63             bool pawnsAreInTheSameTeam(Pawn* pawn1, Pawn* pawn2);
    64 
    65             std::map<PlayerInfo*, int> teamnumbers_;
    66             std::vector<ColourValue> teamcolours_;
    67             unsigned int teams_;
     49            virtual void pawnKilled(Pawn* victim, Pawn* killer = 0);
     50            virtual void playerScored(PlayerInfo* player);
    6851    };
    6952}
Note: See TracChangeset for help on using the changeset viewer.