src/CityEconomy.cpp

Go to the documentation of this file.
00001 /* Crown and Cutlass
00002  * City Economy Class
00003  */
00004 
00005 #include <string>
00006 #include <vector>
00007 #include <iostream>
00008 #include <cstdlib>
00009 #include <ctime>
00010 #include "ccmath.h"
00011 #include "Product.h"
00012 #include "Log.h"
00013 #include "CityEconomy.h"
00014 
00015 using namespace std;
00016 
00017 #define PPSPERSIZE 5
00018 
00019 CityEconomy::CityEconomy(const char* CityName, vector<Product*> ProductList, int CitySize) {
00020   unsigned int i;
00021   int EvenlyDividedPPs;
00022   m_Products = ProductList;
00023   m_Name = CityName;
00024   m_Size = CitySize;
00025   m_UnallocatedProductionPoints = CitySize * PPSPERSIZE;
00026   EvenlyDividedPPs = m_UnallocatedProductionPoints / m_Products.size();
00027   for (i = 0; i < m_Products.size(); i++) 
00028     m_Products[i]->SetProductionPoints(EvenlyDividedPPs);
00029 }
00030 
00031 CityEconomy::~CityEconomy() {
00032   unsigned int i;
00033   Product* tempProduct;
00034 
00035   for (i = 0; i < m_Products.size(); i++) {
00036     tempProduct = (Product*)m_Products[i];
00037     delete tempProduct;
00038   }
00039 
00040   m_Products.clear();
00041 
00042 }
00043 
00044 string CityEconomy::GetCityName() {
00045   return m_Name;
00046 }
00047 
00048 
00049 Product* CityEconomy::GetProduct(char* ProductName) {
00050   string compare;
00051   Product* tempProduct = NULL;
00052   unsigned int i;
00053 
00054   compare = ProductName;
00055 
00056   for (i = 0; i < m_Products.size(); i++) {
00057     tempProduct = (Product*)m_Products[i];
00058     if (compare == tempProduct->GetName()) break;
00059   }
00060 
00061   return tempProduct;
00062 
00063 }
00064 
00065 float CityEconomy::GetPrice(char* ProductName) {
00066   string compare;
00067   Product* tempProduct = NULL;
00068   unsigned int i;
00069 
00070   compare = ProductName;
00071 
00072   for (i = 0; i < m_Products.size(); i++) {
00073     tempProduct = (Product*)m_Products[i];
00074     if (compare == tempProduct->GetName()) break;
00075   }
00076 
00077   if (tempProduct == NULL) {
00078     // This should probably throw an exception
00079     return 0;
00080   }
00081 
00082   return tempProduct->GetPrice(m_Size);
00083 
00084 }
00085 
00086 
00087 float CityEconomy::GetPrice(string ProductName) {
00088   string compare;
00089   Product* tempProduct = NULL;
00090   unsigned int i;
00091 
00092   compare = ProductName;
00093 
00094   for (i = 0; i < m_Products.size(); i++) {
00095     tempProduct = (Product*)m_Products[i];
00096     if (compare == tempProduct->GetName()) break;
00097   }
00098 
00099   if (tempProduct == NULL) {
00100     // This should probably throw an exception
00101     return 0;
00102   }
00103 
00104   return tempProduct->GetPrice(m_Size);
00105 
00106 }
00107 
00108 void CityEconomy::Print() {
00109   unsigned int i;
00110   Product* temp;
00111 
00112   cout << m_Name << ":\n";
00113   for (i = 0; i < m_Products.size(); i++) {
00114     temp = (Product*)m_Products[i];
00115     temp->Print();
00116   }
00117 }
00118 
00119 void CityEconomy::UpdateEconomy(int steps) {
00120   AllocateProductionPoints();
00121 }
00122 
00123 vector<Product*> CityEconomy::GetProductList() {
00124   return m_Products;
00125 }
00126 
00127 int CityEconomy::GetSize() {
00128   return m_Size;
00129 }
00130 
00131 void CityEconomy::IncreaseSize() {
00132   if (m_Size >= 10) {
00133     Log::s_log->Message("City has reached its maximum size");
00134     return;
00135   }
00136   m_Size += 1;
00137 }
00138 
00139 void CityEconomy::DecreaseSize() {
00140   if (m_Size <= 1) {
00141     Log::s_log->Message("City has reached its minimum size");
00142     return;
00143   }
00144   m_Size -= 1;
00145 }
00146 
00147 void CityEconomy::AllocateProductionPoints() {
00148   unsigned int i;
00149   int TotalBasketOfGoods = 0;
00150   float Percent;
00151   int PPsToMove;
00152   /*
00153     Not exactly sure how this will work, but get Total value for all the
00154     products. Then get the percentage of the value of the total value.
00155     Then distribute the production points based on those.
00156   */
00157   for (i = 0; i < m_Products.size(); i++) {
00158     TotalBasketOfGoods += m_Products[i]->GetPrice(m_Size);
00159   }
00160   
00161   for (i = 0; i < m_Products.size(); i++) {
00162     Percent = ((float)m_Products[i]->GetPrice(m_Size) / (float)TotalBasketOfGoods);
00163     // We want to take away from the lowest producers
00164     if (Percent < (1/m_Products.size())) {
00165       ++m_UnallocatedProductionPoints;
00166       m_Products[i]->AddProductionPoints(-1);
00167     } else if (m_UnallocatedProductionPoints > 0) {
00168       // and give to the highest producers
00169       PPsToMove = (int) ((float) m_UnallocatedProductionPoints * Percent);
00170       Log::s_log->DebugMessage("Moving %i PPs", PPsToMove);
00171       m_Products[i]->AddProductionPoints(PPsToMove);
00172       m_UnallocatedProductionPoints -= PPsToMove;
00173     }
00174   }
00175 }

Generated on Mon Jan 8 22:34:12 2007 for CrownandCutlass by  doxygen 1.4.7