/[adonthell]/adonthell/src/rpg/equipment.cc
ViewVC logotype

Diff of /adonthell/src/rpg/equipment.cc

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by ksterker, Mon Aug 23 06:33:47 2004 UTC revision 1.4 by ksterker, Mon Oct 18 07:40:23 2004 UTC
# Line 36  using rpg::slot; Line 36  using rpg::slot;
36  using rpg::item;  using rpg::item;
37    
38  /**  /**
39   * Mapping from itm categories to equipment slots.   * Mapping from %item categories to equipment slots.
40   */   */
41  std::hash_map<std::string, rpg::slot_list,  std::hash_map<std::string, rpg::slot_list,
42      std::hash<std::string> > equipment::SlotCategoryMap;      std::hash<std::string> > equipment::SlotCategoryMap;
43    
44    /**
45     * Predefined %equipment for certain %character types
46     */
47    std::hash_map<std::string, std::vector<std::string>,
48        std::hash<std::string> > equipment::EquipmentDefs;
49    
50  // retrieve a list of slots the item would fit into      // retrieve a list of slots the item would fit into    
51  const rpg::slot_list equipment::fits (item *itm) const  const rpg::slot_list equipment::fits (item *itm) const
52  {  {
# Line 173  void equipment::add_mapping (const std:: Line 179  void equipment::add_mapping (const std::
179          SlotCategoryMap[category] = slots;          SlotCategoryMap[category] = slots;
180      }      }
181  }  }
182    
183    // define equipment for certain type of characters
184    void equipment::add_definition (const std::string & type, const std::vector<std::string> & slots)
185    {
186        EquipmentDefs[type] = slots;
187    }
188    
189    // create equipment storage for given type of characters
190    rpg::inventory *equipment::create (const std::string & type)
191    {
192        rpg::inventory *inv = new rpg::inventory ();
193        std::hash_map<std::string, std::vector<std::string>,
194            std::hash<std::string> >::iterator t = EquipmentDefs.find (type);
195        
196        // does given type exist?
197        if (t != EquipmentDefs.end ())
198        {
199            const std::vector<std::string> &slots = (*t).second;
200            for (std::vector<std::string>::const_iterator i = slots.begin(); i != slots.end (); i++)
201            {
202                // fill inventory with predefined slots
203                inv->add_slot (*i, true);
204            }
205        }
206        else
207        {
208            fprintf (stderr, "*** equipment::create: character type '%s' undefined!\n", type.c_str ());
209        }
210        
211        return inv;
212    }
213    
214    // save to disk
215    void equipment::put_state (base::flat & out)
216    {
217        base::flat record, slots;
218        
219        // save mappings
220        std::hash_map<std::string, rpg::slot_list,
221            std::hash<std::string> >::const_iterator s = SlotCategoryMap.begin ();
222        for (; s != SlotCategoryMap.end (); s++)
223        {
224            slots.put_string ("emp", (*s).first);
225            for (rpg::slot_list::const_iterator i = (*s).second.begin (); i != (*s).second.end (); i++)
226            {
227                slots.put_string ("", *i);
228            }
229            record.put_flat ("", slots);
230            slots.clear ();
231        }
232        
233        // save equipment definitions
234        std::hash_map<std::string, std::vector<std::string>,
235            std::hash<std::string> >::const_iterator t = EquipmentDefs.begin ();
236        for (; t != EquipmentDefs.end (); t++)
237        {
238            slots.put_string ("edf", (*t).first);
239            for (std::vector<std::string>::const_iterator i = (*t).second.begin (); i != (*t).second.end (); i++)
240            {
241                slots.put_string ("", *i);
242            }
243            record.put_flat ("", slots);
244            slots.clear ();
245        }
246        
247        out.put_flat ("eqp", record);
248    }
249    
250    // load from disk
251    bool equipment::get_state (base::flat & in)
252    {
253        base::flat list, record = in.get_flat ("eqp");
254        
255        while (in.next ((void **) &list) != -1)
256        {
257            char *val, *name;
258            if (list.next ((void **) &val, (int*) NULL, &name) != base::flat::T_STRING)
259                continue;
260            
261            // load mapping
262            if (strncmp (name, "emp", 3) == 0)
263            {  
264                std::string category = val;
265                while (list.next ((void **) &val) == base::flat::T_STRING)
266                {
267                    add_mapping (val, category);
268                }
269                continue;
270            }
271    
272            // load equipment def
273            if (strncmp (name, "edf", 3) == 0)
274            {
275                std::string type = val;
276                std::vector<std::string> slots;
277                while (list.next ((void **) &val) == base::flat::T_STRING)
278                {
279                    slots.push_back (std::string (val));
280                }
281                add_definition (type, slots);
282            }
283        }
284        
285        return in.success ();
286    }

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.4

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26