-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathInventory.py
302 lines (253 loc) · 11.5 KB
/
Inventory.py
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
#MineBot
#GPL and all that
# - espes
from __future__ import division
import random
import logging
from collections import defaultdict
from Utility import *
from constants import *
from packets import *
class InventoryHandler(object):
def __init__(self, protocol):
self.protocol = protocol
self.currentWindowId = 0
self.windows = {0: PlayerInventory(self, 0)}
def closeWindow(self, window=None):
if window is None:
windowId = self.currentWindowId
else:
windowId = window.windowId
assert windowId == self.currentWindowId
self.protocol.sendPacked(PACKET_WINDOWCLOSE, windowId)
self.onWindowClose(windowId)
@property
def currentWindow(self):
return self.windows.get(self.currentWindowId)
#These will be called from reactor thread, so it's ok to block untill
#one of these fires
def onTransaction(self, windowId, actionNumber, accepted):
if windowId not in self.windows:
return
self.windows[windowId].onTransaction(actionNumber, accepted)
def onWindowOpen(self, windowId, windowType, windowTitle, numSlots):
if windowType == INVENTORYTYPE_WORKBENCH:
inventory = WorkBenchInventory(self, windowId)
else:
logging.info("fixme %d" % windowType)
return
self.windows[windowId] = inventory
self.currentWindowId = windowId
def onWindowClose(self, windowId):
if windowId == 0:
#these items will be dropped
for i in range(5):
if i in self.windows[0].items:
del self.windows[0].items[i]
return
if windowId in self.windows:
del self.windows[windowId]
if self.currentWindowId == windowId:
self.currentWindowId = 0
def onWindowItems(self, windowId, items):
self.windows[windowId].items = items
def onSetSlot(self, windowId, slot, item):
if windowId == -1 and slot == -1:
self.currentWindow.inHand = item
return
if item is None:
if slot in self.windows[windowId].items:
del self.windows[windowId].items[slot]
return
self.windows[windowId].items[slot] = item
logging.debug(" --> %r" % self.windows[windowId].items)
class Inventory(object):
def __init__(self, handler, windowId, playerSlotsRange):
self.handler = handler
self.windowId = windowId
self.playerSlotsRange = playerSlotsRange
self.inHand = None
self.items = {}
self.transactionResult = defaultdict(lambda: None)
def findPlayerItemId(self, itemId):
for slot, item in self.items.items():
if slot not in self.playerSlotsRange: continue
if item.itemId == itemId:
return slot
return None
def countPlayerItemId(self, itemId):
count = 0
for slot, item in self.items.items():
if slot not in self.playerSlotsRange: continue
if item.itemId == itemId:
count += item.count
return count
def findPlayerEmptySlot(self):
for i in self.playerSlotsRange:
if self.items.get(i) is None:
return i
return None
def command_click(self, slot, rightClick=0, shiftClick=0):
assert self.handler.currentWindowId == self.windowId
transactionId = random.randrange(1, 0x7fff)
logging.debug("click %r %r %r %r %r" % (slot, rightClick,
self.windowId, transactionId, self.items.get(slot)))
self.handler.protocol.sendPacked(PACKET_WINDOWCLICK, self.windowId, slot,
rightClick, transactionId, shiftClick, self.items.get(slot))
while self.transactionResult[transactionId] is None:
yield True
result = self.transactionResult[transactionId]
del self.transactionResult[transactionId]
if result:
if slot == -999:
#drop
self.inHand = None
else:
#update inventory
if self.inHand is not None and slot in self.items:
if self.inHand.itemId == self.items[slot].itemId:
maxStack = gamelogic.maxStack(self.inHand.itemId)
placeAmount = max(maxStack-self.items[slot].count, 0)
if rightClick:
placeAmount = min(placeAmount, 1)
placeAmount = min(placeAmount, self.inHand.count)
self.inHand.count -= placeAmount
self.items[slot].count += placeAmount
else:
#swap them
self.inHand, self.items[slot] = self.items[slot], self.inHand
elif self.inHand is not None:
#in hand but empty slot
if rightClick and self.inHand.count > 1:
self.items[slot] = Item(self.inHand.itemId, 1, 0)
self.inHand.count -= 1
else:
self.items[slot] = self.inHand
self.inHand = None
elif slot in self.items:
if rightClick and self.items[slot].count > 1:
takeCount = int((self.items[slot].count+1)/2)
self.items[slot].count -= takeCount
self.inHand = Item(self.items[slot].itemId, takeCount, 0)
else:
self.inHand = self.items[slot]
del self.items[slot]
else:
#both are empty, don't need to do anything
pass
if self.inHand is not None and self.inHand.count == 0:
self.inHand = None
else:
logging.error("transaction failed")
yield result
def command_swapSlots(self, src, dst):
assert self.handler.currentWindowId == self.windowId
for v in self.command_click(src): yield v
for v in self.command_click(dst): yield v
if self.inHand is not None:
for v in self.command_click(src): yield v
def command_fillSlotsWithPlayerItem(self, itemId, targetSlots):
#print bool(targetSlots)
while targetSlots:
srcSlot = self.findPlayerItemId(itemId)
if srcSlot is None:
raise Exception, "insuffient items to fill"
for v in self.command_click(srcSlot): yield v
logging.debug("fill item %r" % self.inHand)
for i in xrange(min(self.inHand.count, len(targetSlots))):
slot = targetSlots.pop(0)
assert slot not in self.items
for v in self.command_click(slot, True): yield v
if self.inHand is not None:
#put it back
for v in self.command_click(srcSlot): yield v
def command_purge(self):
for slot, item in self.items.items():
for v in self.command_click(slot): yield v
for v in self.command_click(-999): yield v
def command_drop(self, itemId, count=1, dropAll=False):
while count > 0 or dropAll:
srcSlot = self.findPlayerItemId(itemId)
if srcSlot is None: return
if self.items[srcSlot].count <= count or dropAll:
count -= self.items[srcSlot].count
for v in self.command_click(srcSlot): yield v
for v in self.command_click(-999): yield v
else:
for i in xrange(count):
count -= 1
for v in self.command_click(srcSlot, True): yield v
for v in self.command_click(-999): yield v
def command_moveToPlayerInventory(self, sourceSlot):
for v in self.command_click(sourceSlot): yield v
while self.inHand is not None:
for slot, item in self.items.items():
if slot not in self.playerSlotsRange:
continue
if self.inHand.itemId == item.itemId:
for v in self.command_click(slot): yield v
break
else:
emptySlot = self.findPlayerEmptySlot()
if emptySlot is None:
raise Exception, "no empty slot for item"
for v in self.command_click(emptySlot): yield v
assert self.inHand is None
def onTransaction(self, actionNumber, accepted):
self.transactionResult[actionNumber] = accepted
class CraftingInventory(Inventory):
def __init__(self, handler, windowId, playerSlotsRange,
craftSlotsRange, craftOutSlot, craftDim):
Inventory.__init__(self, handler, windowId, playerSlotsRange)
self.craftItemsRange = craftSlotsRange
self.craftOutSlot = craftOutSlot
self.craftW, self.craftH = craftDim
def command_fillRecipe(self, recipe):
for i in self.craftItemsRange:
if i in self.items:
for v in self.command_moveToPlayerInventory(i): yield v
recipeW, recipeH = recipe.dimensions
craftSlots = defaultdict(list)
for i, r in enumerate(recipe.recipe):
if r is None: continue
(itemId, _), count = r
cx, cy = i%recipeW, i//recipeW
craftSlots[itemId].append(self.craftItemsRange[cy*self.craftW+cx])
for itemId, slots in craftSlots.iteritems():
logging.debug("fill %r %r" % (itemId, slots))
for v in self.command_fillSlotsWithPlayerItem(itemId, slots):
yield v
(producedItemId, _), producedCount = recipe.provides
self.items[self.craftOutSlot] = Item(producedItemId, producedCount, 0)
for i in self.craftItemsRange:
if i in self.items:
self.items[i].count -= 1
if self.items[i].count <= 0:
del self.items[i]
else:
for v in self.command_moveToPlayerInventory(i): yield v
yield True
class WorkBenchInventory(CraftingInventory):
def __init__(self, handler, windowId):
CraftingInventory.__init__(self, handler, windowId, range(10, 46), range(1, 10), 0, (3, 3))
class PlayerInventory(CraftingInventory):
def __init__(self, handler, windowId):
CraftingInventory.__init__(self, handler, windowId, range(9, 45), range(1, 5), 0, (2, 2))
self.equippableSlots = range(36, 45)
self.equippedSlot = None
@property
def equippedItem(self):
return self.items.get(self.equippedSlot)
def command_equipItem(self, itemId):
assert self.handler.currentWindowId == self.windowId
for slot, item in self.items.items():
if item.itemId == itemId:
if slot in self.equippableSlots:
self.handler.protocol.sendPacked(PACKET_ITEMSWITCH, self.equippableSlots.index(slot))
self.equippedSlot = slot
else:
for v in self.command_swapSlots(slot, self.equippableSlots[0]): yield v
self.handler.protocol.sendPacked(PACKET_ITEMSWITCH, 0)
self.equippedSlot = self.equippableSlots[0]
return
raise Exception, "No item %d" % itemId