Logo
~Sockets~
~Examples~
~Contact~


ItemFactory.cpp

Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2004  Anders Hedstrom
00008 
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 */
00023 //#include <stdio.h>
00024 #include "SmallHandler.h"
00025 #include "SmallSocket.h"
00026 #include "ItemFactory.h"
00027 
00028 
00029 
00030 
00031 ItemFactory::ItemFactory(SmallHandler& h) : m_handler(h)
00032 {
00033 }
00034 
00035 
00036 ItemFactory::~ItemFactory()
00037 {
00038         for (std::map<int, std::map<int, item_v> >::iterator it = m_items.begin(); it != m_items.end(); it++)
00039         {
00040                 std::map<int, item_v>& ref = (*it).second;
00041                 for (std::map<int, item_v>::iterator it = ref.begin(); it != ref.end(); it++)
00042                 {
00043                         item_v& ref = (*it).second;
00044                         for (item_v::iterator it = ref.begin(); it != ref.end(); it++)
00045                         {
00046                                 ITEM *i = *it;
00047                                 delete i;
00048                         }
00049                 }
00050         }
00051 }
00052 
00053 
00054 ITEM *ItemFactory::CreateRandomItem()
00055 {
00056         switch (random() % 20)
00057         {
00058         case 0:
00059                 return CreateGold( random() % 100 + 100 );
00060         case 1:
00061                 return new ITEM("a sword","swords","right hand");
00062         case 2:
00063                 return new ITEM("a dagger","daggers","right hand");
00064         case 3:
00065                 return new ITEM("a shield","shields","wrong hand");
00066         case 4:
00067                 return new ITEM("a shoulder cap","shoulder caps","left shoulder");
00068         case 5:
00069                 return new ITEM("a shoulder cap","shoulder caps","right shoulder");
00070         case 6:
00071                 return new ITEM("a helmet","helmets","head");
00072         default:
00073                 return new ITEM("a bucket of spam","buckets of spam","",0,0,1);
00074         }
00075         return NULL;
00076 }
00077 
00078 
00079 ITEM *ItemFactory::CreateGold(long amount)
00080 {
00081         return new ITEM("a gold coin","gold coins","",0,0,amount);
00082 }
00083 
00084 
00085 void ItemFactory::Spawn()
00086 {
00087         int x;
00088         int y;
00089         std::string loc;
00090 
00091         m_handler.GetWorld().GetRandomLocation(x,y,loc);
00092 
00093         ITEM *p = CreateRandomItem();
00094         {
00095                 std::string str = "Suddenly, " + p -> GetDescription() + " appears\n";
00096                 Handler().Event(x,y,str);
00097         }
00098         Merge(m_items[x][y],p);
00099 }
00100 
00101 
00102 void ItemFactory::ShowNamesAt(SmallSocket *p,int x,int y,const std::string& prefix)
00103 {
00104         item_v& ref = m_items[x][y];
00105         for (item_v::iterator it = ref.begin(); it != ref.end(); it++)
00106         {
00107                 ITEM *i = *it;
00108                 p -> Send("  " + prefix + i -> GetDescription() + "\n");
00109         }
00110 }
00111 
00112 
00113 ITEM::ITEM(const std::string& n,const std::string& p,const std::string& w) 
00114 :m_vecno(0)
00115 ,m_wield_position(w),m_physical(20),m_magical(20),m_amount(1) 
00116 {
00117         std::string cols[10]; // nrgybmcw
00118         for (size_t i = 0; i < 10; i++)
00119         {
00120                 int nr = random() % 8;
00121                 if (!nr)
00122                 {
00123                         nr = random() % 15 + 1;
00124                 }
00125                 cols[i] = "&";
00126                 cols[i] += "nrgybmcwRGYBMCWL"[nr];
00127         }
00128         size_t col = 0;
00129         for (size_t i = 0; i < n.size(); i++)
00130         {
00131                 if (n[i] == ' ')
00132                 {
00133                         m_name += " ";
00134                         m_name += cols[col++];
00135                 }
00136                 else
00137                 {
00138                         m_name += n.substr(i,1); //n[i];
00139                 }
00140         }
00141         col = 0;
00142         m_plural += cols[col++];
00143         for (size_t i = 0; i < p.size(); i++)
00144         {
00145                 if (p[i] == ' ')
00146                 {
00147                         m_plural += " ";
00148                         m_plural += cols[col++];
00149                 }
00150                 else
00151                 {
00152                         m_plural += p.substr(i,1); //p[i];
00153                 }
00154         }
00155         if (!random() % 100)
00156         {
00157                 m_physical += random() % 80 + 1;
00158                 if (!random() % 100)
00159                 {
00160                         m_magical += random() % 80 + 1;
00161                 }
00162         }
00163         m_name += "&n";
00164         m_plural += "&n";
00165 }
00166 
00167 
00168 ITEM::ITEM(const std::string& n,const std::string& p,const std::string& w,short ph,short ma,long am) 
00169 :m_vecno(0)
00170 ,m_name(n)
00171 ,m_plural(p)
00172 ,m_wield_position(w),m_physical(ph),m_magical(ma),m_amount(am)
00173 {
00174 }
00175 
00176 
00177 size_t ItemFactory::NumberOfItems()
00178 {
00179         size_t q = 0;
00180         for (std::map<int, std::map<int, item_v> >::iterator it = m_items.begin(); it != m_items.end(); it++)
00181         {
00182                 std::map<int, item_v>& ref = (*it).second;
00183                 for (std::map<int, item_v>::iterator it = ref.begin(); it != ref.end(); it++)
00184                 {
00185                         item_v& ref = (*it).second;
00186                         q += ref.size();
00187                 }
00188         }
00189         return q;
00190 }
00191 
00192 
00193 void ItemFactory::Merge(item_v& ref,ITEM *p)
00194 {
00195         size_t max = 0;
00196         for (item_v::iterator it = ref.begin(); it != ref.end(); it++)
00197         {
00198                 ITEM *i = *it;
00199                 max = i -> m_vecno > max ? i -> m_vecno : max;
00200         }
00201         for (item_v::iterator it = ref.begin(); it != ref.end(); it++)
00202         {
00203                 ITEM *i = *it;
00204                 std::string iname = i -> m_name;
00205                 std::string iplural = i -> m_plural;
00206                 if (iname == p -> m_name &&
00207                     iplural == p -> m_plural &&
00208                     i -> m_wield_position == p -> m_wield_position &&
00209                     i -> m_physical == p -> m_physical &&
00210                     i -> m_magical == p -> m_magical)
00211                 {
00212                         i -> m_amount += p -> m_amount;
00213                         delete p;
00214                         return;
00215                 }
00216         }
00217         p -> m_vecno = max + 1;
00218         ref.push_back(p);
00219 }
00220 
00221 
Page, code, and content Copyright (C) 2006 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4