[Orxonox-commit 4230] r8901 - code/branches/gamecontent/src/orxonox/gametypes
jo at orxonox.net
jo at orxonox.net
Fri Oct 21 15:28:09 CEST 2011
Author: jo
Date: 2011-10-21 15:28:08 +0200 (Fri, 21 Oct 2011)
New Revision: 8901
Added:
code/branches/gamecontent/src/orxonox/gametypes/TeamGametype.cc
code/branches/gamecontent/src/orxonox/gametypes/TeamGametype.h
Log:
Prototype of a baseclass for team gametypes. Work in progress.
Added: code/branches/gamecontent/src/orxonox/gametypes/TeamGametype.cc
===================================================================
--- code/branches/gamecontent/src/orxonox/gametypes/TeamGametype.cc (rev 0)
+++ code/branches/gamecontent/src/orxonox/gametypes/TeamGametype.cc 2011-10-21 13:28:08 UTC (rev 8901)
@@ -0,0 +1,231 @@
+/*
+ * ORXONOX - the hottest 3D action shooter ever to exist
+ * > www.orxonox.net <
+ *
+ *
+ * License notice:
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Author:
+ * Johannes Ritz
+ * Co-authors:
+ * ...
+ *
+ */
+
+#include "TeamGametype.h"
+
+#include "core/CoreIncludes.h"
+#include "core/ConfigValueIncludes.h"
+#include "interfaces/TeamColourable.h"
+#include "worldentities/TeamSpawnPoint.h"
+#include "worldentities/pawns/Pawn.h"
+
+namespace orxonox
+{
+ CreateUnloadableFactory(TeamGametype);
+
+ TeamGametype::TeamGametype(BaseObject* creator) : Gametype(creator)
+ {
+ RegisterObject(TeamGametype);
+
+ this->teams_ = 2;
+
+ this->setConfigValues();
+ }
+
+ void TeamGametype::setConfigValues()
+ {
+ SetConfigValue(teams_, 2);
+
+ static ColourValue colours[] =
+ {
+ ColourValue(1.0f, 0.3f, 0.3f),
+ ColourValue(0.3f, 0.3f, 1.0f),
+ ColourValue(0.3f, 1.0f, 0.3f),
+ ColourValue(1.0f, 1.0f, 0.0f)
+ };
+ static std::vector<ColourValue> defaultcolours(colours, colours + sizeof(colours) / sizeof(ColourValue));
+
+ SetConfigValue(teamcolours_, defaultcolours);
+ }
+
+ void TeamGametype::playerEntered(PlayerInfo* player)
+ {
+ Gametype::playerEntered(player);
+
+ std::vector<unsigned int> playersperteam(this->teams_, 0);
+
+ for (std::map<PlayerInfo*, int>::iterator it = this->teamnumbers_.begin(); it != this->teamnumbers_.end(); ++it)
+ if (it->second < static_cast<int>(this->teams_) && it->second >= 0)
+ playersperteam[it->second]++;
+
+ unsigned int minplayers = static_cast<unsigned int>(-1);
+ size_t minplayersteam = 0;
+ for (size_t i = 0; i < this->teams_; ++i)
+ {
+ if (playersperteam[i] < minplayers)
+ {
+ minplayers = playersperteam[i];
+ minplayersteam = i;
+ }
+ }
+
+ this->teamnumbers_[player] = minplayersteam;
+ }
+
+ bool TeamGametype::playerLeft(PlayerInfo* player)
+ {
+ bool valid_player = Gametype::playerLeft(player);
+
+ if (valid_player)
+ this->teamnumbers_.erase(player);
+
+ return valid_player;
+ }
+
+ bool TeamGametype::allowPawnHit(Pawn* victim, Pawn* originator)
+ {
+ return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator);
+ }
+
+ bool TeamGametype::allowPawnDamage(Pawn* victim, Pawn* originator)
+ {
+ return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator);
+ }
+
+ bool TeamGametype::allowPawnDeath(Pawn* victim, Pawn* originator)
+ {
+ return (!this->pawnsAreInTheSameTeam(victim, originator) || !originator);
+ }
+
+ SpawnPoint* TeamGametype::getBestSpawnPoint(PlayerInfo* player) const
+ {
+ int desiredTeamNr = -1;
+ std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player);
+ if (it_player != this->teamnumbers_.end())
+ desiredTeamNr = it_player->second;
+
+ // Only use spawnpoints of the own team (or non-team-spawnpoints)
+ std::set<SpawnPoint*> teamSpawnPoints = this->spawnpoints_;
+ for (std::set<SpawnPoint*>::iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); )
+ {
+ if ((*it)->isA(Class(TeamSpawnPoint)))
+ {
+ TeamSpawnPoint* tsp = orxonox_cast<TeamSpawnPoint*>(*it);
+ if (tsp && static_cast<int>(tsp->getTeamNumber()) != desiredTeamNr)
+ {
+ teamSpawnPoints.erase(it++);
+ continue;
+ }
+ }
+
+ ++it;
+ }
+
+ SpawnPoint* fallbackSpawnPoint = NULL;
+ if (teamSpawnPoints.size() > 0)
+ {
+ unsigned int randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(teamSpawnPoints.size())));
+ unsigned int index = 0;
+ // Get random fallback spawnpoint in case there is no active SpawnPoint.
+ for (std::set<SpawnPoint*>::const_iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ++it)
+ {
+ if (index == randomspawn)
+ {
+ fallbackSpawnPoint = (*it);
+ break;
+ }
+
+ ++index;
+ }
+
+ // Remove all inactive SpawnPoints from the list.
+ for (std::set<SpawnPoint*>::iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); )
+ {
+ if(!(*it)->isActive())
+ {
+ teamSpawnPoints.erase(it++);
+ continue;
+ }
+
+ ++it;
+ }
+
+ randomspawn = static_cast<unsigned int>(rnd(static_cast<float>(teamSpawnPoints.size())));
+ index = 0;
+ for (std::set<SpawnPoint*>::const_iterator it = teamSpawnPoints.begin(); it != teamSpawnPoints.end(); ++it)
+ {
+ if (index == randomspawn)
+ return (*it);
+
+ ++index;
+ }
+
+ return fallbackSpawnPoint;
+ }
+
+ return 0;
+ }
+
+ void TeamGametype::playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn)
+ {
+ if (!player)
+ return;
+
+ // Set the team colour
+ std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player);
+ if (it_player != this->teamnumbers_.end() && it_player->second >= 0 && it_player->second < static_cast<int>(this->teamcolours_.size()))
+ {
+ if (pawn)
+ {
+ pawn->setRadarObjectColour(this->teamcolours_[it_player->second]);
+
+ std::set<WorldEntity*> pawnAttachments = pawn->getAttachedObjects();
+ for (std::set<WorldEntity*>::iterator it = pawnAttachments.begin(); it != pawnAttachments.end(); ++it)
+ {
+ if ((*it)->isA(Class(TeamColourable)))
+ {
+ TeamColourable* tc = orxonox_cast<TeamColourable*>(*it);
+ tc->setTeamColour(this->teamcolours_[it_player->second]);
+ }
+ }
+ }
+ }
+ }
+
+ bool TeamGametype::pawnsAreInTheSameTeam(Pawn* pawn1, Pawn* pawn2)
+ {
+ if (pawn1 && pawn2)
+ {
+ std::map<PlayerInfo*, int>::const_iterator it1 = this->teamnumbers_.find(pawn1->getPlayer());
+ std::map<PlayerInfo*, int>::const_iterator it2 = this->teamnumbers_.find(pawn2->getPlayer());
+
+ if (it1 != this->teamnumbers_.end() && it2 != this->teamnumbers_.end())
+ return (it1->second == it2->second);
+ }
+ return false;
+ }
+
+ int TeamGametype::getTeam(PlayerInfo* player)
+ {
+ std::map<PlayerInfo*, int>::const_iterator it_player = this->teamnumbers_.find(player);
+ if (it_player != this->teamnumbers_.end())
+ return it_player->second;
+ else
+ return -1;
+ }
+}
Added: code/branches/gamecontent/src/orxonox/gametypes/TeamGametype.h
===================================================================
--- code/branches/gamecontent/src/orxonox/gametypes/TeamGametype.h (rev 0)
+++ code/branches/gamecontent/src/orxonox/gametypes/TeamGametype.h 2011-10-21 13:28:08 UTC (rev 8901)
@@ -0,0 +1,72 @@
+/*
+ * ORXONOX - the hottest 3D action shooter ever to exist
+ * > www.orxonox.net <
+ *
+ *
+ * License notice:
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Author:
+ * Johannes Ritz
+ * Co-authors:
+ * ...
+ *
+ */
+
+#ifndef _TeamGametype_H__
+#define _TeamGametype_H__
+
+#include "OrxonoxPrereqs.h"
+
+#include <map>
+#include <vector>
+#include "Gametype.h"
+
+namespace orxonox
+{
+ class _OrxonoxExport TeamGametype : public Gametype
+ {
+ public:
+ TeamGametype(BaseObject* creator);
+ virtual ~TeamGametype() {}
+
+ void setConfigValues();
+
+ virtual void playerEntered(PlayerInfo* player);
+ virtual bool playerLeft(PlayerInfo* player);
+
+ virtual bool allowPawnHit(Pawn* victim, Pawn* originator = 0);
+ virtual bool allowPawnDamage(Pawn* victim, Pawn* originator = 0);
+ virtual bool allowPawnDeath(Pawn* victim, Pawn* originator = 0);
+
+ virtual void playerStartsControllingPawn(PlayerInfo* player, Pawn* pawn);
+
+ int getTeam(PlayerInfo* player);
+
+ inline const ColourValue& getTeamColour(int teamnr) const
+ { return this->teamcolours_[teamnr]; }
+
+ protected:
+ virtual SpawnPoint* getBestSpawnPoint(PlayerInfo* player) const;
+ bool pawnsAreInTheSameTeam(Pawn* pawn1, Pawn* pawn2);
+
+ std::map<PlayerInfo*, int> teamnumbers_;
+ std::vector<ColourValue> teamcolours_;
+ unsigned int teams_;
+ };
+}
+
+#endif /* _TeamGametype_H__ */
More information about the Orxonox-commit
mailing list