Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

HelperFunctions.cpp

Go to the documentation of this file.
00001 /*
00002 CS Senior Project 2003
00003 Team : Leftfield
00004 Project : ModernWarfare
00005 Members :
00006 - Russ Christensen              <rchriste@cs.utah.edu>
00007 - Todd Smith                    <tcsmith@cs.utah.edu>
00008 - Usit Duongsaa                 <duongsaa@cs.utah.edu>
00009 Copyright 2003 Russ Christensen, Usit Duongsaa, and Todd Smith. All rights reserved.
00010 
00011 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00012 
00013 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
00014 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
00015 THIS SOFTWARE IS PROVIDED BY RUSS CHRISTENSEN, USIT DUONGSAA, AND TODD SMITH ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RUSS, USIT, TODD OR OTHER CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00016 */
00017 
00022 #include "..\AEngine\AEngine.h"
00023 #include "HelperFunctions.h"
00024 #include <math.h>
00025 #include "GameState.h"
00026 
00027 namespace Helper {
00028         FLOAT abs(FLOAT num) {
00029                 if(num < 0) return -1 * num;
00030                 else return num;
00031         }
00032 
00033         FLOAT distXY(Vec3D &a, Vec3D &b) 
00034         {
00035                 return sqrtf( powf(abs(a.x - b.x), 2) + powf(abs(a.y - b.y), 2));
00036         }
00037 
00038         string serialize(Vec3D vec)
00039         {
00040                 stringstream sstr;
00041                 sstr << vec.x << " " << vec.y << " " << vec.z;
00042                 return sstr.str();
00043         }
00044 
00045         Vec3D unserializeVec3D(stringstream & sstr)
00046         {
00047                 Vec3D vec;
00048                 sstr >> vec.x >> vec.y >> vec.z;
00049                 if(sstr.bad()) throw Error("invalid data received");
00050                 return vec;
00051         }
00052     
00053         FLOAT radiansToDegrees(FLOAT rad)
00054         {
00055                 const FLOAT PI(3.15159f);
00056                 FLOAT degrees(rad/PI*180);
00057                 return degrees;
00058         }
00059         
00060         string toLower(string str)
00061         {
00062                 for(unsigned int i=0; i < str.size(); ++i)
00063                 {
00064                         str[i] = tolower(str[i]);
00065                 }
00066                 return str;
00067         }
00068 
00069         string eatEarlyWhiteSpace(string input)
00070         {
00071                 while(input[0] == ' ') input = input.substr(1);
00072                 return input;
00073         }
00074 
00076         Vec3D playerNumberToPlayerColor(int num)
00077         {
00078                 Vec3D colors[8] = {Colors::red, Colors::blue, Colors::green, Colors::yellow,  
00079                Colors::cyan, Colors::white, Colors::black, Colors::magenta};
00080                 return num < 8 ? colors[num] : Vec3D(0,0,0);
00081         }
00082 
00083         int totalCitiesForPlayer(Vec3D playerColor)
00084         {
00085                 int total = 0;
00086                 for(vector<City*>::iterator iter(GameState::cities().begin()); iter != GameState::cities().end(); ++iter)
00087                 {
00088                         if ((*iter)->color() == playerColor) ++total;
00089                 }
00090                 return total;
00091         }
00092 
00094         Player* playerNumberToPlayerPointer(int num)
00095         {
00096                 Vec3D playerColor(playerNumberToPlayerColor(num));
00097                 for(vector<Player*>::iterator iter(GameState::players().begin()); iter != GameState::players().end(); ++iter)
00098                 {
00099                         if ((*iter)->color() == playerColor) return *iter;
00100                 }
00101                 return 0;
00102         }
00103 
00105         int playerColorToPlayerNumber(Vec3D color)
00106         {
00107                 if (color == Colors::red) return 0;
00108                 else if (color == Colors::blue) return 1;
00109                 else if (color == Colors::green) return 2;
00110                 else if (color == Colors::yellow) return 3;
00111                 else if (color == Colors::cyan) return 4;
00112                 else if (color == Colors::white) return 5;
00113                 else if (color == Colors::black) return 6;
00114                 else if (color == Colors::magenta) return 7;
00115                 else {
00116                         sys_console() << "ERROR! invalid color given" << endl;
00117                         return 0;
00118                 }
00119         }
00120 
00122         string playerColorToPlayerName(Vec3D color)
00123         {
00124                 unsigned int num(playerColorToPlayerNumber(color));
00125                 return net_getPlayerNameUsingNum(num);          
00126         }
00127 
00129         void printPlayerNameToScreen(Vec3D playerColor, Vec2D location, FLOAT scale)
00130         {
00131                 overlay_text() << playerColorToPlayerName(playerColor);
00132                 overlay_textOut(location, scale, playerColor);
00133         }
00134 
00135         int totalUnitsForPlayer(Vec3D playerColor)
00136         {
00137                 int totalUnits = 0;
00138                 for(vector<MilitaryUnit*>::iterator iter(GameState::units().begin()); iter != GameState::units().end(); ++iter)
00139                 {
00140                         if ((*iter)->color() == playerColor) ++totalUnits;
00141                 }
00142                 for(vector<BattleEntry*>::iterator iter(GameState::battles().begin()); iter != GameState::battles().end(); ++iter)
00143                 {
00144                         totalUnits += (*iter)->countGroupsBelongingToPlayer(playerColor);
00145                 }
00146                 return totalUnits;
00147         }
00148 
00149         int totalUnitsBeingBuiltForPlayer(Vec3D playerColor)
00150         {
00151                 int total = 0;
00152                 for(vector<City*>::iterator iter(GameState::cities().begin()); iter != GameState::cities().end(); ++iter)
00153                 {
00154                         if ((*iter)->color() == playerColor) total += static_cast<int>((*iter)->trainees().size());
00155                 }
00156                 return total;
00157         }
00158 
00159         int maintenanceMultiplier(Vec3D playerColor, int amount)
00160         {
00161                 FLOAT tmp;
00162                 int totalUnits(totalUnitsForPlayer(playerColor));
00163                 if (totalUnits < 15) return amount;
00164                 else if (totalUnits < 25) tmp = amount * 0.5f;
00165                 else tmp = amount * 0.1f;
00166                 int result(static_cast<int>(tmp));
00167                 if (result > 0) return result;
00168                 else return 1;
00169         }
00170 }

Generated on Wed Apr 23 05:50:15 2003 for Modern Warfare by doxygen1.3-rc2