-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInventory.gd
351 lines (279 loc) · 9.55 KB
/
Inventory.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# extends Node
extends Node
# Inventory Variables
var InvMaxCapacity = 0
var InvCurrentUse = 0
var InvItems = []
var KeyInvItems = []
var IsInvOverencumbered = false #TOOD Move to Character
var CapacityOverencumbered = 0 #TODO Move to Character
const VALID_ITEM_ATTRS = ['GUID', 'ParentClass', 'Purpose', 'ItemClass', 'ID',
'PrimaryRole', 'SecondaryRole', "TertiaryRole"
'IsStackable', 'MaxStackSize', "InvSize", "Name", "Description",
"FreeTags", "ResellValue", "IsKeyItem"]
const INV_MULTIPLIER = 7
const MAX_STACK_SIZE = 20
# Inner Classes
# TODO
func _ready():
# Called every time the node is added to the scene.
# Initialization here
pass
func CreateInv(Str):
'''
Preps an Inventory Object for usage.
Keyword Arguments:
Str -- Integer representation of Strength. (Required)
'''
if typeof(Str) != TYPE_INT:
return false
if Str < 0:
return false
InvMaxCapacity = INV_MULTIPLIER * Str
return InvMaxCapacity
func AddItemToInv(ItemObj):
'''
Adds a defined item to the inventory. Items are defined as dictionary items
taking upon the following format.
{
"ParentClass": Equippable",
"Purpose": "Equippable",
"ItemClass": "ItemWeapon",
"GUID": "0000-0000-0000-0000",
"ID": "YNPShortSword",
"PrimaryRole": "Weapon",
"IsStackable": false,
"InvSize": "10",
"Name": "National Police Shortsword",
"Description": "The standard issue short-sword, commonly used for
ceremonial practices.",
"FreeTags": ["weapon", "sword"],
"ResellValue": 10.00,
"IsKeyItem": false
}
The dictionary is checked for all of the above attributes.
All attributes excluding ItemFreeTags, MaxStackSize, TertiaryRole, and
ItemResellValue are required
If the item is not sent or the dictionary is empty, the method will
return "ItemNotSet".
If the item is not a Dictionary, the method will return "ItemNotDict".
If a required attribute is missing, it is Rejected from the Inventory
with the string error "MissingDictionaryAttributes" returned.
If an attribute that does not match the potentials above is found, the item is
rejected from the Inventory and the string error "MalformedDictionaryAttr" is
returned.
If the Item Inventory Size exceeds the MaxCapacity, the item is rejected
and the method will return error "ItemLargerThanMaxInvSize". This is not a
factor if IsKeyItem is set to true, as Key Items are persistent and do not
affect inventory size.
If the item will run the Inventory Capacity greater than the current
maximum capacity, the item is rejected and the method will return error
"ItemWillForceOvercapacity".
If the item is missing any defined attribute, the item will be rejected and
state the error in a dictionary:
{
"ItemRejected": true
"AttributeRejected": {ATTRIBUTE_KEY}
"ReasonForRejection": {REASON_FOR_REJECTION}
}
The complete list of rejections can be seen in the comments below (these will
be pulled into the docs in the future).
If the item is valid, the item is added and the method returns the following
dictionary:
TODO
Keyword Attributes:
ItemObj -- Dictionary entry with the Item in question. (Required)
'''
# Check validity based on the objects above, and with what is defined in the
# specification
if typeof(ItemObj) != TYPE_DICTIONARY:
return "ItemNotDict"
if typeof(ItemObj) == TYPE_NIL:
return "ItemNotSet"
if ItemObj.empty():
return "ItemNotSet"
var passesDictValidation = CheckItemEntry(ItemObj)
if passesDictValidation != TYPE_NIL:
return passesDictValidation
if ItemObj.InvSize > InvMaxCapacity and ItemObj.IsKeyItem == false:
return "ItemLargerThanMaxInvSize"
if (InvCurrentInvUse + ItemObj.InvSize) > and ItemObj.IsKeyItem == false:
return "ItemWillForceOvercapacity"
# Check validity of items in the Dictionary
# Add Item to Dictionary
# TODO Adjust Inventory Size
InvItems.push_back(ItemObj)
func CheckItemEntry(ItemObj):
"""
Delegate call for AddItemToInv(). This call handles the verification of
the Item dictionary item.
"""
for k in VALID_ITEM_ATTRS:
if ItemObj.has(k) == false:
return "MissingDictionaryAttributes"
# Set/Increment Inventory Size Methods
func SetInventoryMax(InvMax):
'''
Sets the Maximum Inventory as passed in InvMax. If InvMax is zero, or
negative, or not an integer, it will return false.
If successful, the method returns the newly set Inventory Max.
Keywors arguments
InvMax -- Integer that represents the new Inventory Max Size. (Required)
'''
if typeof(InvMax) != TYPE_INT:
return false
if InvMax <= 0:
return false
InvMaxCapacity = InvMax
return InvMaxCapacity
func IncrementInventoryMax(Delta):
'''
Increases the Inventory Size by the aforementioned delta passed in a keyword
argument. Delta must be a positive, non-zero integer or else the method
returns false. If successful, the new Inventory Size will be returned.
Keyword arguments:
Delta -- Integer size for which to increase the Inventory.
'''
if typeof(Delta) != TYPE_INT:
return false
if Delta <= 0:
return false
InvMaxCapacity = InvMaxCapacity + Delta
return InvMaxCapacity
function DecrementInventoryMax(Delta):
'''
Decreases the Inventory Size by the aforementioned delta passed in a keyword
argument. Delta must be a positive, non-zero integer or else the method will
return false. If successful, the new Inventory Size will be returned.
If the Delta will cause the Max Inventory to reach zero or a negative value,
the method will also return false.
Keyword arguments:
Delta -- Integer size for which to decrease the Inventory.
'''
if typeof(Delta) != TYPE_INT:
return false
if Delta <= 0:
return false
if (InvMaxCapacity - Delta) <= 0:
return false
InvMaxCapacity = InvMaxCapacity - Delta
return InvMaxCapacity
# Get Inventory Info Methods
func GetInventoryMax():
'''
Returns the Maximum size of the inventory.
'''
return InvMaxCapacity
func GetCurrentInventorySize():
'''
Returns the size of the inventory being used.
'''
return InvCurrentUse
func GetInventoryItems():
'''
Returns the Dictionary for all Items currently being used by the Dictionary.
'''
return InvItems
func RemoveItemFromInventory(Item, ReasonForRemoval):
'''
Removes an item from the inventory. The item is flushed from the Inventory
list when its key is found.
If its not found, the method returns an error of "ItemNotFound". If the field
Item is not defined with a valid Item Dictionary entry or pointer to a Object
instance in memory, the method returns false.
ReasonForRemoval is not used, and is not required. If A non-string is sent to
it, the method returns error "InvalidTypeReasonForRemoval".
Keyword arguments:
Item -- Dictionary Instance of Item or a Pointer to an Object reference in
memory space, for the item to be removed. (Required)
ReasonForRemoval -- A string representing a one word summary for its removal.
Currently not used.
'''
if typeof(Item) != TYPE_NIL:
return false
if (typeof(Item) != TYPE_DICTIONARY) or (typeof(Item) != TYPE_OBJECT):
return false
if (typeof(ReasonForRemoval) != TYPE_STRING) or
(typeof(ReasonForRemoval) != TYPE_NIL):
return "InvalidTypeReasonForRemoval"
# Remove Item From Inventory
var ItemLocation = InvItems.find(Item)
if ItemLocation == -1:
return "ItemNotFound"
else:
InvCurrentUse = InvCurrentUse - InvItems[ItemLocation].InvSize
InvItems.erase(ItemLocation)
return "ItemRemoved"
func StackInventoryItems(Item, ItemToAdd, MaxStackSize):
'''
Stacks items on top of one another, unless the size of the stack limit
reaches the maximum.
'''
if typeof(Item) != TYPE_DICTIONARY or typeof(Item) != TYPE_OBJECT:
return "InvalidItemType"
if typeof(ItemToAdd) != TYPE_DICTIONARY or typeof(ItemToAdd) != TYPE_OBJECT:
return "InvalidItemToAddType"
if Item.IsStackable == false:
return "ItemNotStackable"
if ItemToAdd.IsStackable == false:
return "ItemToAddNotStackable"
#TODO Stack Consolidation Logic
func UnstackInventoryItem(Item):
'''
Removes one item from a stacked inventory item.
'''
#TODO
func BreakItemStack(Item):
'''
Breaks all the items from a Stack.
'''
#TODO
func SearchInventory(SearchType, SearchRequest):
'''
Searches the Inventory for a specific item(s). This method serves as a
delegate for multiples based on the mechanism declared in SearchType. This
method can also return zero, or null, when no results exist.
If SearchType is not set, is a type that is not a String, or of the kind
outlined below, the method returns "InvalidSearchType"
The available search mechanisms part of the method and its child methods are:
* guid
* id
* name
* freetags
* category
* stackable
* keyitem
* purpose
Keyword Arguments:
SearchType -- The kind of Search mechanism to use. (Required)
SearchRequest -- The actual item you are requesting. If set to nothing,
Search will return all possible items based on the mechanism set. If set to
an Asterisk (*), it functions s imilar to if set as blank. Partial String
searches are not yet supported.
'''
# Validation
if typeof(SearchType) == TYPE_NIL:
return "InvalidSearchType"
if SearchType == "guid":
Results = SearchInvByGuid(SearchRequest)
return Results
elif SearchType == "id"
Results = SearchInvById(SearchRequest)
return Results
elif SearchType == "freetags"
Results = SearchInvByFreetags(SearchRequest)
return Results
elif SearchType == "category"
Results = SearchInvByCategory(SearchRequest)
return Results
elif SearchType == "stackable"
Results = SearchInvByStackable(SearchRequest)
return Results
elif SearchType == "keyitem"
Results = SearchInvByKeyItem(SearchRequest)
return Results
elif SearchType == "purpose":
Results = SearchInvByPurpose(SearchRequest)
return Results
else:
return "InvalidSearchType"