[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Adonthell-commits] CVS: adonthell/src dialog.cc,1.33,1.34 inventory.cc
From: |
Kai Sterker <address@hidden> |
Subject: |
[Adonthell-commits] CVS: adonthell/src dialog.cc,1.33,1.34 inventory.cc,1.5,1.6 inventory.h,1.9,1.10 item_base.cc,1.7,1.8 item_base.h,1.7,1.8 item_storage.cc,1.2,1.3 item_storage.h,1.1,1.2 mapcharacter.cc,1.48,1.49 py_object.h,1.11,1.12 slot.cc,1.3,1.4 |
Date: |
Mon, 17 Feb 2003 14:31:24 -0500 |
Update of /cvsroot/adonthell/adonthell/src
In directory subversions:/tmp/cvs-serv5425/src
Modified Files:
dialog.cc inventory.cc inventory.h item_base.cc item_base.h
item_storage.cc item_storage.h mapcharacter.cc py_object.h
slot.cc
Log Message:
ADDED -DSWIG_GLOBAL flag
ADDED item combination code
FIXED item/inventory bugs
Index: dialog.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/dialog.cc,v
retrieving revision 1.33
retrieving revision 1.34
diff -C2 -r1.33 -r1.34
*** dialog.cc 29 Sep 2002 16:08:34 -0000 1.33
--- dialog.cc 17 Feb 2003 19:31:21 -0000 1.34
***************
*** 326,329 ****
--- 326,330 ----
end = strcspn (start, "}");
+ mid = NULL;
str = new char[end];
***************
*** 334,342 ****
// run the string
! result = PyObject_CallMethod (dialogue.get_instance (), str, NULL);
! #ifdef PY_DEBUG
! python::show_traceback ();
! #endif
! mid = NULL;
if (result)
--- 335,339 ----
// run the string
! result = PyObject_CallMethod (dialogue.get_instance (false), str,
NULL);
if (result)
Index: inventory.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/inventory.cc,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -r1.5 -r1.6
*** inventory.cc 12 Feb 2003 15:58:55 -0000 1.5
--- inventory.cc 17 Feb 2003 19:31:21 -0000 1.6
***************
*** 27,30 ****
--- 27,32 ----
inventory::inventory (const bool & limited, const u_int16 & size)
{
+ QueryType = 0;
+ QueryKey = "";
Limited = limited;
grow (size);
***************
*** 126,129 ****
--- 128,243 ----
// return how many items didn't fit
return remaining;
+ }
+
+ // combine two (stacks of) items
+ bool inventory::combine (slot *target, slot *agent)
+ {
+ slot *tmp = new slot (NULL);
+ u_int32 count = 0;
+ item_base *result, *item;
+
+ // combine agent with all items in target slot
+ while (target->count () > 0 && agent->count () > 0)
+ {
+ // get result of combination
+ item = target->get_item ();
+ result = item->combine (agent->get_item ());
+
+ // combination successful?
+ if (!result)
+ {
+ // if it wasn't the first try, we screwed up
+ if (count > 0)
+ {
+ fprintf (stderr, "*** error:: inventory::combine: not all
items could be combined!\n");
+
+ // try to recover already created items
+ break;
+ }
+
+ // otherwise we can happily tell that the items weren't combinable
+ delete tmp;
+ return false;
+ }
+
+ // if new item was created, destroy old one
+ if (item != result) item->destroy ();
+
+ // this shouldn't fail if items are designed properly
+ if (tmp->add (result) != 0)
+ {
+ fprintf (stderr, "*** error: inventory::combine: result not
fitting into temporary slot!\n");
+
+ // cleanup
+ result->destroy ();
+ break;
+ }
+
+ count++;
+ }
+
+ // if target slot is empty, add resulting items to target slot
+ if (target->count () == 0) target->add (tmp->get_item (), count);
+
+ // otherwise add result to a free slot (or existing stack)
+ else add (tmp->get_item (), count);
+
+ // do items remain in temporary slot? (this is not supposed to happen)
+ if (tmp->count () != 0)
+ {
+ // FIXME: place remaining items on the ground
+ // for now just utter warning
+ fprintf (stderr, "*** warning: inventory::combine: %i newly created
items do not fit\n", tmp->count ());
+ }
+
+ // cleanup
+ delete tmp;
+
+ return true;
+ }
+
+ // find first matching item
+ item_base *inventory::find (const string & key, const u_int8 & match)
+ {
+ // init query
+ QueryKey = key;
+ QueryType = match;
+ I = Slots.begin ();
+
+ // actually start searching
+ return find_next ();
+ }
+
+ // find more matching items
+ item_base *inventory::find_next ()
+ {
+ item_base *item;
+
+ for (; I != Slots.end (); I++)
+ {
+ // get item from slot
+ if ((*I)->count () > 0) item = (*I)->get_item ();
+ else continue;
+
+ // compare
+ switch (QueryType)
+ {
+ // match name
+ case inventory::MATCH_NAME:
+ if (item->name () == QueryKey) return item;
+ else break;
+
+ // match category
+ case inventory::MATCH_CATEGORY:
+ if (item->is_a (QueryKey)) return item;
+ else break;
+
+ // wrong query type
+ default: return NULL;
+ }
+ }
+
+ // reached end of inventory
+ return NULL;
}
Index: inventory.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/inventory.h,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -r1.9 -r1.10
*** inventory.h 11 Feb 2003 21:00:35 -0000 1.9
--- inventory.h 17 Feb 2003 19:31:21 -0000 1.10
***************
*** 110,114 ****
* items will be returned.
*
! * Not that there is no explicit remove method, as item removal will
* happen automatically.
*
--- 110,114 ----
* items will be returned.
*
! * Note that there is no explicit remove method, as item removal will
* happen automatically.
*
***************
*** 118,121 ****
--- 118,174 ----
*/
u_int32 add (item_base *item, const u_int32 & count = 1);
+
+ /**
+ * Combine item(s) in the target %slot with the item(s) from the
+ * agent %slot. The combination's outcome will depend on the items
+ * involved. The target may be changed or transformed into a totally
+ * different item. The agent could remain unchanged or even be
+ * destroyed in the process. If no combination for target and agent
+ * is defined, nothing will happen.
+ *
+ * Note that target and agent need not be from the same inventory.
+ * The items resulting from the combination will always be added
+ * to this inventory (which should be that of the target).
+ *
+ * @param target %Slot containing the target items.
+ * @param agent %Slot containing the items to combine with target.
+ * @return \b true if combination succeeded, \b false otherwise.
+ */
+ bool combine (slot *target, slot *agent);
+ //@}
+
+ /**
+ * @name Query Methods
+ */
+ //@{
+ /**
+ * Values representing the different query types. It is possible
+ * to find items either by their name or by their category.
+ */
+ enum
+ {
+ MATCH_NAME = 1,
+ MATCH_CATEGORY = 2
+ };
+
+ /**
+ * Find the first item that matches key. Depending on the
+ * match parameter, items are either retrieved by name or by
+ * category.
+ * @param key The key that items must match.
+ * @param match The type of query being performed.
+ * @return First item matching key, or \c NULL if no match found.
+ */
+ item_base *find (const string & key, const u_int8 & match);
+
+ /**
+ * Continue a query started by inventory::find. This will return
+ * subsequent items matching the parameters used by the last query.
+ * Note that this will not retrieve items in the same stack as the
+ * one returned by inventory::find. That is, only the first item
+ * of each matching stack is retrieved.
+ * @return More matching items, or \c NULL if no more match found.
+ */
+ item_base *find_next ();
//@}
***************
*** 156,159 ****
--- 209,222 ----
*/
bool Limited;
+
+ /**
+ * Key of last query.
+ */
+ string QueryKey;
+
+ /**
+ * Type of last query.
+ */
+ u_int8 QueryType;
#endif // SWIG
};
Index: item_base.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/item_base.cc,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -r1.7 -r1.8
*** item_base.cc 12 Feb 2003 15:58:55 -0000 1.7
--- item_base.cc 17 Feb 2003 19:31:21 -0000 1.8
***************
*** 27,30 ****
--- 27,36 ----
#include "character_base.h"
+ struct swig_type_info;
+ extern "C" {
+ swig_type_info *SWIG_TypeQuery (const char*);
+ int SWIG_ConvertPtr (PyObject*, void**, swig_type_info*, int);
+ }
+
// ctor
item_base::item_base (const std::string & item) : py_object ()
***************
*** 75,78 ****
--- 81,110 ----
}
+ // combine two items
+ item_base *item_base::combine (item_base *item)
+ {
+ // can't be combined
+ if (!has_attribute ("combine")) return NULL;
+
+ item_base *result = NULL;
+
+ // pass item
+ PyObject *args = PyTuple_New (1);
+ PyTuple_SetItem (args, 0, python::pass_instance (item, "item_base"));
+
+ // call method
+ PyObject *retval = call_method_ret ("combine", args);
+
+ // try to retrieve result
+ SWIG_ConvertPtr (retval, (void **) &result,
+ SWIG_TypeQuery ("_p_item_base"), 1);
+
+ // cleanzp
+ Py_XDECREF (retval);
+ Py_DECREF (args);
+
+ return result;
+ }
+
// save a single item to file
bool item_base::put_state (const std::string & file) const
***************
*** 83,87 ****
if (!out.is_open ())
{
! fprintf (stderr, "*** item_base::save: cannot write '%s'\n",
file.c_str ());
return false;
}
--- 115,119 ----
if (!out.is_open ())
{
! fprintf (stderr, "*** item_base::put_state: cannot write '%s'\n",
file.c_str ());
return false;
}
***************
*** 130,134 ****
if (!in.is_open ())
{
! fprintf (stderr, "*** item_base::load: cannot read '%s'\n",
file.c_str ());
return false;
}
--- 162,166 ----
if (!in.is_open ())
{
! fprintf (stderr, "*** item_base::get_state: cannot read '%s'\n",
file.c_str ());
return false;
}
Index: item_base.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/item_base.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -r1.7 -r1.8
*** item_base.h 12 Feb 2003 15:58:55 -0000 1.7
--- item_base.h 17 Feb 2003 19:31:21 -0000 1.8
***************
*** 81,85 ****
*/
~item_base ();
!
/**
* @name Item Actions
--- 81,99 ----
*/
~item_base ();
!
! /**
! * Removes an item from its slot and deletes it if necessary. This
! * method should be preferred over deleting an item directly, as it
! * takes care of both mutable and immutable items.
! */
! void destroy ()
! {
! if (!Mutable)
! {
! if (Slot) Slot->remove (this);
! }
! else delete this;
! }
!
/**
* @name Item Actions
***************
*** 99,103 ****
* @return item resulting from combination, or \c NULL if combination
fails.
*/
! // item_base *combine (item_base * item);
//@}
--- 113,117 ----
* @return item resulting from combination, or \c NULL if combination
fails.
*/
! item_base *combine (item_base * item);
//@}
Index: item_storage.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/item_storage.cc,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -r1.2 -r1.3
*** item_storage.cc 12 Feb 2003 15:58:55 -0000 1.2
--- item_storage.cc 17 Feb 2003 19:31:21 -0000 1.3
***************
*** 53,65 ****
{
std::hash_map<std::string, item_base*>::iterator i = Items.find (name);
// no such item
if (i == Items.end ())
{
fprintf (stderr, "*** error: item_storage::get: no item called
'%s'\n", name.c_str ());
return NULL;
}
! else return (*i).second;
}
--- 53,80 ----
{
std::hash_map<std::string, item_base*>::iterator i = Items.find (name);
+ item_base *item;
// no such item
if (i == Items.end ())
{
+ // assume that it is a mutable item ...
+ item = new item_base (true);
+
+ // ... and return it if that is the case
+ if (item->get_state (name) && item->is_mutable ()) return item;
+
fprintf (stderr, "*** error: item_storage::get: no item called
'%s'\n", name.c_str ());
+
+ delete item;
return NULL;
}
! // retrieve item
! item = (*i).second;
!
! // make sure that items from the storage have no inventory assigned
! item->set_slot (NULL);
!
! return item;
}
Index: item_storage.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/item_storage.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** item_storage.h 11 Feb 2003 19:59:26 -0000 1.1
--- item_storage.h 17 Feb 2003 19:31:21 -0000 1.2
***************
*** 55,63 ****
* Add an item to the %storage. The item needs to be immutable,
* otherwise it won't be added. It should also have a unique name.
*/
static void add (item_base *item);
/**
! * Retrieve an item from the %storage.
* @param name name of the item to retrieve.
* @return item with given name, or \c NULL if no such item exists.
--- 55,67 ----
* Add an item to the %storage. The item needs to be immutable,
* otherwise it won't be added. It should also have a unique name.
+ * @param item The item to add to the storage
*/
static void add (item_base *item);
/**
! * Retrieve immutable item with given name from the %storage.
! * Instead of an immutable item's name, one may also pass the file
! * name of a mutable item. In that case, the mutable item will be
! * instanciated and returned.
* @param name name of the item to retrieve.
* @return item with given name, or \c NULL if no such item exists.
Index: mapcharacter.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/mapcharacter.cc,v
retrieving revision 1.48
retrieving revision 1.49
diff -C2 -r1.48 -r1.49
*** mapcharacter.cc 21 Aug 2002 15:25:45 -0000 1.48
--- mapcharacter.cc 17 Feb 2003 19:31:21 -0000 1.49
***************
*** 208,212 ****
// get the events
! py_callback::instance = schedule.get_instance ();
return event_list::get_state (file);
}
--- 208,212 ----
// get the events
! py_callback::instance = schedule.get_instance (false);
return event_list::get_state (file);
}
***************
*** 571,575 ****
void mapcharacter::time_callback_string (string delay, string cb, PyObject
*args)
{
! PyObject *instance = schedule.get_instance ();
// check that we have a valid instance that contains our callback
--- 571,575 ----
void mapcharacter::time_callback_string (string delay, string cb, PyObject
*args)
{
! PyObject *instance = schedule.get_instance (false);
// check that we have a valid instance that contains our callback
Index: py_object.h
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/py_object.h,v
retrieving revision 1.11
retrieving revision 1.12
diff -C2 -r1.11 -r1.12
*** py_object.h 18 Jan 2003 23:22:59 -0000 1.11
--- py_object.h 17 Feb 2003 19:31:21 -0000 1.12
***************
*** 198,212 ****
//@{
/**
! * Direct access to the instance object
*
! * @return the Python class instance
*/
! PyObject *get_instance () const
{
return Instance;
}
/**
! * Returns the class name of this object.
*
* @return class name of this object.
--- 198,217 ----
//@{
/**
! * Direct access to the instance object. The default behaviour is to
! * increase the instance's reference count, so that this method can
! * be safely called from Python scripts.
*
! * @param incref whether to increase the reference count.
! * @return the Python class instance.
*/
! PyObject *get_instance (const bool & incref = true) const
{
+ if (incref) Py_XINCREF (Instance);
return Instance;
}
/**
! * Returns the class name of this object. This is the name of the
! * wrapped Python class.
*
* @return class name of this object.
***************
*** 218,222 ****
/**
! * Returns the file name of this object.
*
* @return fiöe name of this object.
--- 223,228 ----
/**
! * Returns the file name of this object. This is the name of the
! * Python module containing the wrapped class.
*
* @return fiöe name of this object.
Index: slot.cc
===================================================================
RCS file: /cvsroot/adonthell/adonthell/src/slot.cc,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -r1.3 -r1.4
*** slot.cc 12 Feb 2003 15:58:55 -0000 1.3
--- slot.cc 17 Feb 2003 19:31:21 -0000 1.4
***************
*** 84,88 ****
}
! // clculate how many items will fit into the slot
u_int32 fitting = item->max_stack () - Count;
fitting = (fitting > count) ? count : fitting;
--- 84,88 ----
}
! // calculate how many items will fit into the slot
u_int32 fitting = item->max_stack () - Count;
fitting = (fitting > count) ? count : fitting;
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Adonthell-commits] CVS: adonthell/src dialog.cc,1.33,1.34 inventory.cc,1.5,1.6 inventory.h,1.9,1.10 item_base.cc,1.7,1.8 item_base.h,1.7,1.8 item_storage.cc,1.2,1.3 item_storage.h,1.1,1.2 mapcharacter.cc,1.48,1.49 py_object.h,1.11,1.12 slot.cc,1.3,1.4,
Kai Sterker <address@hidden> <=
- Prev by Date:
[Adonthell-commits] CVS: adonthell configure.in,1.84,1.85
- Next by Date:
[Adonthell-commits] CVS: adonthell/src game.cc,1.27,1.28 item_base.h,1.8,1.9 main.cc,1.57,1.58 prefs.cc,1.31,1.32
- Previous by thread:
[Adonthell-commits] CVS: adonthell configure.in,1.84,1.85
- Next by thread:
[Adonthell-commits] CVS: adonthell/src game.cc,1.27,1.28 item_base.h,1.8,1.9 main.cc,1.57,1.58 prefs.cc,1.31,1.32
- Index(es):