def _sortOffer(self, offer): categoryLimitChanges = [] stackLimitChanges = [] setAccumulators = [] stackables = [] locatable = [] accumulators = [] doIds = [] for i in offer: a = i[0] if InventoryId.isLimitChange(a): changing = InventoryId.getChangeCategoryOrType(a) if InventoryId.isCategory(changing): categoryLimitChanges.append(i) elif InventoryId.isStackable(changing): stackLimitChanges.append(i) elif InventoryId.isAccumulator(changing): setAccumulators.append(i) else: print '=============EXCEPTION1 RAISED: %s' % a raise Exception, 'undefined trade category' elif InventoryId.isStackable(a): stackables.append(i) elif isLocatable(a): locatable.append(i) elif InventoryId.isAccumulator(a): accumulators.append(i) elif InventoryId.isCategory(a): doIds.append(i) else: print '=============EXCEPTION2 RAISED: %s' % a raise Exception, 'undefined trade type' offer = categoryLimitChanges + stackLimitChanges + setAccumulators + stackables + locatable + accumulators + doIds
def canAddLocatables(self, items, equippable = True, tempAdd = False): results = [] if not tempAdd: prevTmpAdds = self.tempAdds self.tempAdds = [] for currItem in items: location = Locations.NON_LOCATION itemType = None numValues = len(currItem) currLoc = currItem.getLocation() if isLocatable(currItem.getCat()): if currLoc == Locations.NON_LOCATION or currLoc == None: location = self.findAvailableLocation(currItem.getCat(), itemId = currItem.getType(), count = currItem.getCount(), equippable = equippable) elif self.locationAvailable(currLoc, itemType = currItem.getType()) or currItem.getCount(default = 1) == 0: location = currLoc itemType = currItem.getCat() if self.locationIsValid(location, itemType, itemType = currItem.getType(), includeEquip = equippable) and location not in results: results.append(location) self.tempAdds.append(location) continue results.append(Locations.NON_LOCATION) if not tempAdd: self.tempAdds = prevTmpAdds return results
def check1(tradeItems, limitChanges, stacks, locatable, accumulators, doIds, giving): listType = type([]) possibleTypes = (type(1), type(1L), listType) for i in tradeItems: for v in i: itemType = type(v) if itemType not in possibleTypes: raise AITradeException, 'element is the wrong type' if v > 4294967295L: raise itemType != listType and AITradeException, 'element is larger than unsigned long' else: t = i[TypeIndex] if InventoryId.isLimitChange(t): a = limitChanges.setdefault(t, [t, 0]) a[1] += i[QuantityIndex] if a[1] > 65535L: raise AITradeException, 'element is larger than unsigned short' elif InventoryId.isStackable(t): a = stacks.setdefault(t, [t, 0]) a[1] += i[QuantityIndex] if a[1] > 65535L: raise AITradeException, 'element is larger than unsigned short' elif isLocatable(t): inv = simbase.air.doId2do.get(self.inventoryId) theInvItem = inv and InvItem(i) theInvItemType = theInvItem.getType() if giving: equippableInfo = inv.getItemRequirements( theInvItemType, self.giving) if equippableInfo == None or filter( lambda x: equippableInfo[x][1] == False, equippableInfo): if theInvItemType and theInvItemType not in self.unequippables: self.unequippables.append(theInvItemType) if isStackableType(theInvItem.getCat()): for currLocatable in locatable: prevInvItem = InvItem(currLocatable) if prevInvItem.compare(theInvItem, excludeLoc=True): adjustedInvItem = prevInvItem.adjustCount( theInvItem.getCount()) locatable.remove(currLocatable) i = list(adjustedInvItem) break else: raise AITradeException, 'inventory not present' locatable.append(i) elif InventoryId.isAccumulator(t): a = accumulators.setdefault(t, [t, 0]) a[1] += i[QuantityIndex] if a[1] > 4294967295L: raise AITradeException, 'element is larger than unsigned long' elif InventoryId.isDoId(t): doIds[i[DoIdIndex]] = i return
def getStackQuantity(self, stackType): itemClass = None if isLocatable(stackType): itemClass = ItemGlobals.getClass(stackType) if itemClass: return self.getItemQuantity(itemClass, stackType) else: return self.getItemQuantity(stackType) return
def canRemoveLocatable(self, item, tempRemove=False): numValues = len(item) if numValues > self.ITEM_CAT_IDX and isLocatable(item.getCat()): currItem = self._locatableItems.get(item.getLocation()) if currItem and currItem.compare(item): if tempRemove: self.tempRems.append(item) return True return False
def getStackQuantity(self, stackType): itemClass = None if isLocatable(stackType): itemClass = ItemGlobals.getClass(stackType) if itemClass: return self.getItemQuantity(itemClass, stackType) else: return self.getItemQuantity(stackType)
def canRemoveLocatable(self, item, tempRemove = False): numValues = len(item) if numValues > self.ITEM_CAT_IDX and isLocatable(item.getCat()): currItem = self._locatableItems.get(item.getLocation()) if currItem and currItem.compare(item): if tempRemove: self.tempRems.append(item) return True return False
def getItemQuantity(self, itemCat, itemId=None): if isLocatable(itemCat): findFields = [(self.ITEM_CAT_IDX, itemCat)] if itemId: findFields.append((self.ITEM_ID_IDX, itemId)) itemCounts = self._findLocatablesWithValues(findFields, countsOnly=True) if itemId: return itemCounts.get(itemId, 0) else: return sum(itemCounts.values()) else: return DistributedInventoryBase.getStackQuantity(self, itemCat)
def getItemQuantity(self, itemCat, itemId = None): if isLocatable(itemCat): findFields = [ (self.ITEM_CAT_IDX, itemCat)] if itemId: findFields.append((self.ITEM_ID_IDX, itemId)) itemCounts = self._findLocatablesWithValues(findFields, countsOnly = True) if itemId: return itemCounts.get(itemId, 0) else: return sum(itemCounts.values()) else: return DistributedInventoryBase.getStackQuantity(self, itemCat)
def filterAdds(self, takingList): itemCats = { } nonLocatables = [] for currItem in takingList: itemCat = currItem.getCat() if not isLocatable(itemCat): nonLocatables.append(currItem) continue if itemCats.has_key(itemCat): itemCats[itemCat][1].append(currItem) continue itemCats[itemCat] = [ 0, [ currItem]] possibleLocs = self.getPossibleLocations(itemCat, currItem.getType(), includeEqup = True, expanded = True) for currLoc in possibleLocs: if self.locationAvailable(currLoc, currItem.getType()): itemCats[itemCat][0] += 1 continue def cmp(item1, item2): goldCost1 = ItemGlobals.getGoldCost(item1[1]) if not goldCost1: goldCost1 = item1[1] goldCost2 = ItemGlobals.getGoldCost(item2[1]) if not goldCost2: goldCost2 = item2[1] return int(goldCost2 - goldCost1) chosen = [] for (currCat, currCatInfo) in itemCats.items(): available = currCatInfo[0] items = currCatInfo[1] if available < len(items): items.sort(cmp) chosen.extend(items[:available]) continue chosen.extend(items) chosen.extend(nonLocatables) return chosen
def _sortOffer(self, offer): categoryLimitChanges = [] stackLimitChanges = [] setAccumulators = [] stackables = [] locatable = [] accumulators = [] doIds = [] for i in offer: a = i[0] if InventoryId.isLimitChange(a): changing = InventoryId.getChangeCategoryOrType(a) if InventoryId.isCategory(changing): categoryLimitChanges.append(i) elif InventoryId.isStackable(changing): stackLimitChanges.append(i) elif InventoryId.isAccumulator(changing): setAccumulators.append(i) else: print "=============EXCEPTION1 RAISED: %s" % a raise Exception, "undefined trade category" InventoryId.isCategory(changing) if InventoryId.isStackable(a): stackables.append(i) continue if isLocatable(a): locatable.append(i) continue if InventoryId.isAccumulator(a): accumulators.append(i) continue if InventoryId.isCategory(a): doIds.append(i) continue print "=============EXCEPTION2 RAISED: %s" % a raise Exception, "undefined trade type" offer = ( categoryLimitChanges + stackLimitChanges + setAccumulators + stackables + locatable + accumulators + doIds )
def getItemsInCategory(self, category): if isLocatable(category): return self._findLocatablesWithValues([(self.ITEM_CAT_IDX, category)]) else: return DistributedInventoryBase.getStacksInCategory(self, category)
def check1(tradeItems, limitChanges, stacks, locatable, accumulators, doIds, giving): listType = type([]) possibleTypes = (type(1), type(0x1L), listType) for i in tradeItems: for v in i: itemType = type(v) if itemType not in possibleTypes: raise AITradeException, "element is the wrong type" if v > 0xFFFFFFFFL and itemType != listType: raise AITradeException, "element is larger than unsigned long" continue else: t = i[TypeIndex] if InventoryId.isLimitChange(t): a = limitChanges.setdefault(t, [t, 0]) a[1] += i[QuantityIndex] if a[1] > 0xFFFFL: raise AITradeException, "element is larger than unsigned short" if InventoryId.isStackable(t): a = stacks.setdefault(t, [t, 0]) a[1] += i[QuantityIndex] if a[1] > 0xFFFFL: raise AITradeException, "element is larger than unsigned short" a[1] > 0xFFFFL if isLocatable(t): inv = simbase.air.doId2do.get(self.inventoryId) if inv: theInvItem = InvItem(i) theInvItemType = theInvItem.getType() if giving: equippableInfo = inv.getItemRequirements(theInvItemType, self.giving) if ( ( equippableInfo == None or filter(lambda x: equippableInfo[x][1] == False, equippableInfo) ) and theInvItemType and theInvItemType not in self.unequippables ): self.unequippables.append(theInvItemType) if isStackableType(theInvItem.getCat()): for currLocatable in locatable: prevInvItem = InvItem(currLocatable) if prevInvItem.compare(theInvItem, excludeLoc=True): adjustedInvItem = prevInvItem.adjustCount(theInvItem.getCount()) locatable.remove(currLocatable) i = list(adjustedInvItem) break continue else: raise AITradeException, "inventory not present" locatable.append(i) continue if InventoryId.isAccumulator(t): a = accumulators.setdefault(t, [t, 0]) a[1] += i[QuantityIndex] if a[1] > 0xFFFFFFFFL: raise AITradeException, "element is larger than unsigned long" a[1] > 0xFFFFFFFFL if InventoryId.isDoId(t): doIds[i[DoIdIndex]] = i continue
def getItemsInCategory(self, category): if isLocatable(category): return self._findLocatablesWithValues([ (self.ITEM_CAT_IDX, category)]) else: return DistributedInventoryBase.getStacksInCategory(self, category)