Logo
~Sockets~
~Examples~
~Contact~


ItemFactory Class Reference

Item factory; contains/spawns items. More...

#include <ItemFactory.h>

Collaboration diagram for ItemFactory:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ItemFactory (SmallHandler &)
 File .
 ~ItemFactory ()
ITEMCreateRandomItem ()
ITEMCreateGold (long amount)
void Spawn ()
void ShowNamesAt (SmallSocket *p, int x, int y, const std::string &prefix="")
SmallHandlerHandler ()
item_vGetItems (int x, int y)
size_t NumberOfItems ()
void Merge (item_v &, ITEM *)

Private Attributes

SmallHandlerm_handler
std::map< int, std::map< int,
item_v > > 
m_items

Detailed Description

Item factory; contains/spawns items.

Definition at line 76 of file ItemFactory.h.


Constructor & Destructor Documentation

ItemFactory::ItemFactory ( SmallHandler h  ) 

File .

........ ItemFactory.cpp Published .... 2004-05-15 Author ....... grymse@alhem.net

Definition at line 31 of file ItemFactory.cpp.

00031                                         : m_handler(h)
00032 {
00033 }

ItemFactory::~ItemFactory (  ) 

Definition at line 36 of file ItemFactory.cpp.

References m_items.

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 }


Member Function Documentation

ITEM * ItemFactory::CreateRandomItem (  ) 

Definition at line 54 of file ItemFactory.cpp.

References CreateGold().

Referenced by Spawn().

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 }

ITEM * ItemFactory::CreateGold ( long  amount  ) 

Definition at line 79 of file ItemFactory.cpp.

Referenced by CreateRandomItem().

00080 {
00081         return new ITEM("a gold coin","gold coins","",0,0,amount);
00082 }

void ItemFactory::Spawn (  ) 

Definition at line 85 of file ItemFactory.cpp.

References CreateRandomItem(), SmallHandler::Event(), World::GetRandomLocation(), SmallHandler::GetWorld(), Handler(), m_handler, m_items, and Merge().

Referenced by SmallHandler::SecTick().

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 }

void ItemFactory::ShowNamesAt ( SmallSocket p,
int  x,
int  y,
const std::string &  prefix = "" 
)

Definition at line 102 of file ItemFactory.cpp.

References m_items.

Referenced by SmallHandler::ShowCell().

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 }

SmallHandler& ItemFactory::Handler (  )  [inline]

Definition at line 87 of file ItemFactory.h.

References m_handler.

Referenced by Spawn().

00087 { return m_handler; }

item_v& ItemFactory::GetItems ( int  x,
int  y 
) [inline]

Definition at line 88 of file ItemFactory.h.

References m_items.

Referenced by SmallHandler::GetItems().

00088 { return m_items[x][y]; }

size_t ItemFactory::NumberOfItems (  ) 

Definition at line 177 of file ItemFactory.cpp.

References m_items.

Referenced by SmallHandler::ShowStatus().

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 }

void ItemFactory::Merge ( item_v ,
ITEM  
)

Definition at line 193 of file ItemFactory.cpp.

Referenced by SmallHandler::MergeItem(), and Spawn().

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 }


Member Data Documentation

Definition at line 94 of file ItemFactory.h.

Referenced by Handler(), and Spawn().

std::map<int, std::map<int, item_v> > ItemFactory::m_items [private]

Definition at line 95 of file ItemFactory.h.

Referenced by GetItems(), NumberOfItems(), ShowNamesAt(), Spawn(), and ~ItemFactory().


The documentation for this class was generated from the following files:
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