Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List

Cost.cpp

00001 /*
00002 Copyright 2003 - 2005 Elliott Kleinrock, Dan Neely, Kurt W. Over, Damon Domjan
00003 
00004 This file is part of FreeStars, a free clone of the Stars! game.
00005 
00006 FreeStars 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 FreeStars 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 FreeStars; if not, write to the Free Software
00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 The full GPL Copyright notice should be in the file COPYING.txt
00021 
00022 Contact:
00023 Email Elliott at 9jm0tjj02@sneakemail.com
00024 */
00025 
00026 #include "FSServer.h"
00027 
00028 #include "Cost.h"
00029 
00030 #ifdef _DEBUG
00031 #define new DEBUG_NEW
00032 #endif
00033 
00034 
00035 Cost::Cost()
00036 {
00037         Zero();
00038 }
00039 
00040 Cost::Cost(const Cost & c) :
00041         mResources(c.mResources),
00042         mCrew(c.mCrew)
00043 {
00044         mMinerals.insert(mMinerals.begin(), c.mMinerals.begin(), c.mMinerals.end());
00045 }
00046 
00047 void Cost::Zero()
00048 {
00049         mResources = 0;
00050         mCrew = 0;
00051         mMinerals.clear();
00052         mMinerals.insert(mMinerals.begin(), Rules::MaxMinType, 0);
00053 }
00054 
00055 Cost & Cost::operator =(const Cost & c)
00056 {
00057         mResources = c.mResources;
00058         mCrew = c.mCrew;
00059         mMinerals.clear();
00060         mMinerals.insert(mMinerals.begin(), c.mMinerals.begin(), c.mMinerals.end());
00061 
00062         return *this;
00063 }
00064 
00065 Cost & Cost::operator *= (double factor)
00066 {
00067         mResources = long(mResources * factor);
00068         mCrew = long(mCrew * factor);
00069         for (CargoType ct = 0; ct < Rules::MaxMinType; ++ct)
00070                 mMinerals[ct] = long(mMinerals[ct] * factor);
00071 
00072         return *this;
00073 }
00074 
00075 Cost & Cost::operator += (const Cost & c)
00076 {
00077         mResources += c.mResources;
00078         mCrew += c.mCrew;
00079         for (CargoType ct = 0; ct < Rules::MaxMinType; ++ct)
00080                 mMinerals[ct] += c.mMinerals[ct];
00081 
00082         return *this;
00083 }
00084 
00085 Cost & Cost::operator -= (const Cost & c)
00086 {
00087         mResources -= c.mResources;
00088         mCrew -= c.mCrew;
00089         for (CargoType ct = 0; ct < Rules::MaxMinType; ++ct)
00090                 mMinerals[ct] -= c.mMinerals[ct];
00091 
00092         return *this;
00093 }
00094 
00095 long & Cost::operator [](long ct)
00096 {
00097         if (ct == RESOURCES)
00098                 return mResources;
00099         else if (ct == POPULATION)
00100                 return mCrew;
00101         else if (ct >= 0 && ct < Rules::MaxMinType)
00102                 return mMinerals[ct];
00103         else {
00104                 assert(false);
00105                 return mMinerals[ct];
00106         }
00107 }
00108 
00109 long Cost::operator [](long ct) const
00110 {
00111         if (ct == RESOURCES)
00112                 return mResources;
00113         else if (ct == POPULATION)
00114                 return mCrew;
00115         else if (ct >= 0 && ct < Rules::MaxMinType)
00116                 return mMinerals[ct];
00117         else {
00118                 assert(false);
00119                 return 0;
00120         }
00121 }
00122 
00123 bool operator == (const Cost & b, const Cost & c)
00124 {
00125         return c.mResources == b.mResources && c.mCrew == b.mCrew && c.mMinerals == b.mMinerals;
00126 }
00127 
00128 /*
00129 long Cost::GetCost(CargoType ct) const
00130 {
00131         if (ct == RESOURCES)
00132                 return mResources;
00133         else if (ct == POPULATION)
00134                 return mCrew;
00135         else if (ct >= 0 && ct < Rules::MaxMinType)
00136                 return mMinerals[ct];
00137         else {
00138                 assert(false);
00139                 return 0;
00140         }
00141 }
00142 
00143 void Cost::SetCost(CargoType ct, long c)
00144 {
00145         if (ct == RESOURCES)
00146                 mResources = c;
00147         else if (ct == POPULATION)
00148                 mCrew = c;
00149         else if (ct >= 0 && ct < Rules::MaxMinType)
00150                 mMinerals[ct] = c;
00151         else
00152                 assert(false);
00153 }
00154 */
00155 
00156 void Cost::ReadCosts(const TiXmlNode * node)
00157 {
00158         if (node == NULL) {
00159                 mResources = 0;
00160                 mCrew = 0;
00161                 deque<long>::iterator li;
00162                 for (li = mMinerals.begin(); li != mMinerals.end(); ++li)
00163                         *li = 0;
00164         } else {
00165                 mResources = GetLong(node->FirstChild("Resources"));
00166                 mCrew = GetLong(node->FirstChild("Crew"));
00167                 Rules::ParseArray(node, mMinerals, MINERALS);
00168         }
00169 }
00170 
00171 TiXmlNode * Cost::WriteCosts(TiXmlNode * node, const char * name) const
00172 {
00173         deque<long>::const_iterator li;
00174 
00175         li = max_element(mMinerals.begin(), mMinerals.end());
00176         TiXmlElement * child = NULL;
00177         if (*li > 0 || mResources > 0 || mCrew > 0) {
00178                 if (*li > 0)
00179                         child = Rules::WriteArray(name, mMinerals, MINERALS);
00180                 else
00181                         child = new TiXmlElement(name);
00182 
00183                 if (mResources > 0)
00184                         AddLong(child, "Resources", mResources);
00185                 if (mCrew > 0)
00186                         AddLong(child, "Crew", mCrew);
00187 
00188                 node->LinkEndChild(child);
00189         }
00190 
00191         return child;
00192 }

Generated on Mon Aug 8 21:33:42 2005 for Freestars by  doxygen 1.4.2-20050421