TankColorGenerator.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <tank/TankColorGenerator.h>
00022 #include <tank/Tank.h>
00023 #include <stdlib.h>
00024 
00025 TankColorGenerator *TankColorGenerator::instance_ = 0;
00026 
00027 TankColorGenerator *TankColorGenerator::instance()
00028 {
00029         if (!instance_)
00030         {
00031                 instance_ = new TankColorGenerator;
00032         }
00033         return instance_;
00034 }
00035 
00036 TankColorGenerator::TankColorGenerator()
00037 {
00038         addColor(0x7F, 0xFF, 0xD4);
00039         addColor(0xFF, 0xE4, 0xC4);
00040         addColor(0xDE, 0xB8, 0x87);
00041         addColor(0xD2, 0x69, 0x1E);
00042         addColor(0x8B, 0x00, 0x8B);
00043         addColor(0x99, 0x32, 0xCC);
00044         addColor(0x8D, 0xBC, 0x8F);
00045         addColor(0x00, 0xDE, 0xD1);
00046         addColor(0x00, 0xBF, 0xFF);
00047         addColor(0xB2, 0x22, 0x22);
00048         addColor(0xFF, 0x00, 0xFF);
00049         addColor(0xFF, 0xD7, 0x00);
00050         addColor(0xAD, 0xD8, 0xE6);
00051         addColor(0xFF, 0xB6, 0xC1);
00052         addColor(0xFA, 0xFA, 0xD2);
00053         addColor(0xFF, 0x45, 0x00);
00054         addColor(0xDA, 0xA5, 0x20);
00055         addColor(0xFF, 0xFF, 0x20);
00056         addColor(0xFF, 0x00, 0x20);
00057         addColor(0x2E, 0x8B, 0x57);
00058         addColor(0xBD, 0xB7, 0x6B);
00059         addColor(0xFF, 0x8C, 0x00);
00060         addColor(0x00, 0x22, 0xFF);
00061         addColor(0x7F, 0xFF, 0x00);
00062 }
00063 
00064 TankColorGenerator::~TankColorGenerator()
00065 {
00066 }
00067 
00068 void TankColorGenerator::addColor(unsigned r, unsigned g, unsigned b)
00069 {
00070         Vector newColor(
00071                 float(int(float(r) / 2.550f)) / 100.0f, 
00072                 float(int(float(g) / 2.550f)) / 100.0f, 
00073                 float(int(float(b) / 2.550f)) / 100.0f);
00074         
00075         std::vector<Vector*>::iterator coloritor;
00076         for (coloritor = availableColors_.begin();
00077                 coloritor != availableColors_.end();
00078                 coloritor++)
00079         {
00080                 Vector &color = *(*coloritor);
00081                 if (newColor[0] > color[0]) break;
00082                 else if (newColor[0] == color[0])
00083                 {
00084                         if (newColor[1] > color[1]) break;
00085                         else if (newColor[1] == color[1])
00086                         {
00087                                 if (newColor[2] > color[2]) break;
00088                                 else if (newColor[2] == color[2])
00089                                 {
00090                                         break;
00091                                 }
00092                         }
00093                 }
00094         }
00095 
00096         availableColors_.insert(coloritor, new Vector(newColor));
00097 }
00098 
00099 bool TankColorGenerator::colorAvailable(Vector &color,
00100         std::map<unsigned int, Tank *> &tanks,
00101         Tank *currentTank)
00102 {
00103         bool available = true;
00104         std::map<unsigned int, Tank *>::iterator tankitor;
00105         for (tankitor = tanks.begin();
00106                 tankitor != tanks.end();
00107                 tankitor++)
00108         {
00109                 Tank *tank = (*tankitor).second;
00110                 if (tank->getColor() == color)
00111                 {
00112                         if (currentTank != tank)
00113                         {
00114                                 available = false;
00115                                 break;
00116                         }
00117                 }
00118         }
00119 
00120         return available;
00121 }
00122 
00123 std::vector<Vector *> TankColorGenerator::getAvailableColors(
00124         std::map<unsigned int, Tank *> &tanks,
00125         Tank *currentTank)
00126 {
00127         std::vector<Vector*> leftColors;
00128         std::vector<Vector*>::iterator coloritor;
00129         for (coloritor = availableColors_.begin();
00130                 coloritor != availableColors_.end();
00131                 coloritor++)
00132         {
00133                 Vector *color = (*coloritor);
00134                 if (colorAvailable(*color, tanks, currentTank))
00135                 {
00136                         leftColors.push_back(color);
00137                 }
00138         }
00139         return leftColors;
00140 }
00141 
00142 Vector &TankColorGenerator::getNextColor(std::map<unsigned int, Tank *> &tanks)
00143 {
00144         std::vector<Vector *> leftColors = getAvailableColors(tanks);
00145         if (!leftColors.empty())
00146         {
00147                 Vector *color = leftColors[rand() % leftColors.size()];
00148                 return *color;
00149         }
00150 
00151         static Vector defaultColor(0.8f, 0.8f, 0.8f);
00152         return defaultColor;
00153 }
00154 
00155 Vector &TankColorGenerator::getTeamColor(int team)
00156 {
00157         static Vector red(1.0f, 0.0f, 0.0f);
00158         static Vector blue(0.0f, 0.3f, 1.0f);
00159         static Vector green(0.0f, 1.0f, 0.0f);
00160         static Vector yellow(1.0f, 1.0f, 0.0f);
00161         if (team == 1) return red;
00162         else if (team == 2) return blue;
00163         else if (team == 3) return green;
00164         else if (team == 4) return yellow;
00165         return Vector::getNullVector();
00166 }
00167 
00168 const char *TankColorGenerator::getTeamName(int team)
00169 {
00170         if (team == 1) return "Red";
00171         else if (team == 2) return "Blue";
00172         else if (team == 3) return "Green";
00173         else if (team == 4) return "Yellow";
00174         return "None";
00175 }

Generated on Mon Feb 16 15:14:51 2009 for Scorched3D by  doxygen 1.5.3