1 /*
2  * inventory.h
3  * Copyright (C) 2009-2018 Joachim de Groot <jdegroot@web.de>
4  *
5  * NLarn is free software: you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * NLarn is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef __INVENTORY_H_
20 #define __INVENTORY_H_
21 
22 #include <glib.h>
23 
24 #include "items.h"
25 
26 /* forward declarations */
27 struct _inventory;
28 
29 typedef gint (*inv_callback_bool) (struct _inventory *inv, item *item);
30 typedef void (*inv_callback_void) (struct _inventory *inv, item *item);
31 
32 typedef struct _inventory
33 {
34     inv_callback_bool pre_add;
35     inv_callback_void post_add;
36     inv_callback_bool pre_del;
37     inv_callback_void post_del;
38     gconstpointer owner;
39     GPtrArray *content;
40 } inventory;
41 
42 /* function definitions */
43 
44 inventory *inv_new(gconstpointer owner);
45 void inv_destroy(inventory *inv, gboolean special);
46 
47 cJSON *inv_serialize(inventory *inv);
48 inventory *inv_deserialize(cJSON *iser);
49 
50 void inv_callbacks_set(inventory *inv, inv_callback_bool pre_add,
51                        inv_callback_void post_add, inv_callback_bool pre_del,
52                        inv_callback_void post_del);
53 
54 /**
55  * Function to add an item to an inventory.
56  *
57  * This function calls the pre_add callback to determine if adding the item
58  * to the inventory is possible at all. If this callback returns FALSE,
59  * the item in not added to the inventory and control is returned to the
60  * calling function.
61  *
62  * After putting the item into the inventory, the post_add callback is
63  * called if it is defined.
64  *
65  * @param the inventory the item has to be added to
66  * @param the item which has to be added
67  * @return FALSE on failure, the new length of the inventory upon success
68  */
69 int inv_add(inventory **inv, item *it);
70 
71 item *inv_get(inventory *inv, guint idx);
72 
73 /**
74  * Function to remove an item from an inventory by its index.
75  *
76  * If the pre_del callback is set, it is used to determine if the action is
77  * valid. If the post_del callback is set, it is called after removing the
78  * item.
79  *
80  * If the inventories owner attribute is not set, empty inventories get
81  * destroyed.
82  *
83  * @param the inventory from which the item shall be removed
84  * @param the index of the item in the inventory
85  * @return an pointer to the removed item
86  *
87  */
88 item *inv_del(inventory **inv, guint idx);
89 
90 /**
91  * Function to remove an item from an inventory.
92  *
93  * If the pre_del callback is set, it is used to determine if the action is
94  * valid. If the post_del callback is set, it is called after removing the
95  * item.
96  *
97  * If the inventories owner attribute is not set, empty inventories get
98  * destroyed.
99  *
100  * @param the inventory from which the item shall be removed
101  * @param the item to be removed
102  * @return TRUE upon success, FALSE on failure.
103  *
104  */
105 int inv_del_element(inventory **inv, item *it);
106 
107 /**
108  * Function to remove an item from an inventory by its oid.
109  *
110  * The inventory's callback functions are ignored.
111  * If the inventories owner attribute is not set, empty inventories get
112  * destroyed.
113  *
114  * @param the inventory from which the item shall be removed
115  * @param the oid of the item to be removed
116  * @return TRUE if the oid was removed, FALSE if the oid has not been found.
117  *
118  */
119 int inv_del_oid(inventory **inv, gpointer oid);
120 
121 /**
122  * Erode all items in an inventory.
123  *
124  * @param pointer to the address of the inventory to erode
125  * @param the erosion type affecting the inventory
126  * @param TRUE if the player can see the inventory, FALSE otherwise
127  * @param a filter function to restrict the eroded items
128  *
129  */
130 void inv_erode(inventory **inv, item_erosion_type iet,
131 		gboolean visible, int (*ifilter)(item *));
132 
133 /**
134  * Function to determine the count of items in an inventory.
135  *
136  * @param the inventory to be counted
137  * @return the count of items in the inventory
138  *
139  */
140 guint inv_length(inventory *inv);
141 
142 /**
143  * Function to sort the items in an inventory.
144  *
145  * @param the inventory to be sorted
146  * @param the function used to compare the items
147  * @param additional data for the compare function
148  *
149  */
150 void inv_sort(inventory *inv, GCompareDataFunc compare_func, gpointer user_data);
151 
152 /**
153  * Function to determine the weight of all items in an inventory.
154  *
155  * @param the inventory
156  * @return the weight of the inventory in grams
157  *
158  */
159 int inv_weight(inventory *inv);
160 
161 /**
162  * Count an filtered inventory.
163  *
164  * @param the inventory to look in
165  * @param the filter function
166  * @return the number of items for which the filter function returned TRUE
167  *
168  */
169 guint inv_length_filtered(inventory *inv, int (*filter)(item *));
170 
171 item *inv_get_filtered(inventory *inv, guint idx, int (*filter)(item *));
172 
173 #endif
174