From: Daniel Karbach Date: Mon, 12 Nov 2012 20:42:49 +0000 (+0100) Subject: added Sort function to Inventory X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=89aa9fbc77d72e98549bc3aabd7ced7cd8949bdc;p=l2e.git added Sort function to Inventory also specified the equipability of items in test-data --- diff --git a/src/common/Inventory.cpp b/src/common/Inventory.cpp index d118f21..6a6af81 100644 --- a/src/common/Inventory.cpp +++ b/src/common/Inventory.cpp @@ -7,6 +7,11 @@ #include "Inventory.h" +#include "Item.h" + +#include + + namespace common { Inventory::Inventory() { @@ -61,4 +66,21 @@ bool Inventory::SloteFree(int offset) const { return !ItemAt(offset); } + +void Inventory::Sort() { + std::sort(entries, entries + 96, Entry::Less); +} + +bool Inventory::Entry::Less(const Entry &lhs, const Entry &rhs) { + if (lhs.item) { + if (rhs.item) { + return Item::Less(*lhs.item, *rhs.item); + } else { + return true; + } + } else { + return false; + } +} + } diff --git a/src/common/Inventory.h b/src/common/Inventory.h index f60b9ab..f80368c 100644 --- a/src/common/Inventory.h +++ b/src/common/Inventory.h @@ -28,11 +28,14 @@ public: const Item *ItemAt(int offset) const { return entries[offset].item; } int ItemCountAt(int offset) const { return entries[offset].count; } + void Sort(); + private: struct Entry { Entry() : item(0), count(0) { } const Item *item; Uint8 count; + static bool Less(const Entry &, const Entry &); }; private: diff --git a/src/common/Item.cpp b/src/common/Item.cpp index 2bd337d..259c4b5 100644 --- a/src/common/Item.cpp +++ b/src/common/Item.cpp @@ -35,7 +35,6 @@ Item::Item() , equipability(0) , mostUseful(false) -, equipable(false) , cursed(false) , fruit(false) , scenario(false) @@ -57,7 +56,6 @@ void Item::CreateTypeDescription() { td.AddField("menuicon", FieldDescription(((char *)&i.menuIcon) - ((char *)&i), Sprite::TYPE_ID).SetReferenced().SetDescription("icon that is displayed in menus")); td.AddField("mostUseful", FieldDescription(((char *)&i.mostUseful) - ((char *)&i), Interpreter::BOOLEAN_ID)); - td.AddField("equipable", FieldDescription(((char *)&i.equipable) - ((char *)&i), Interpreter::BOOLEAN_ID)); td.AddField("cursed", FieldDescription(((char *)&i.cursed) - ((char *)&i), Interpreter::BOOLEAN_ID)); td.AddField("fruit", FieldDescription(((char *)&i.fruit) - ((char *)&i), Interpreter::BOOLEAN_ID)); td.AddField("scenario", FieldDescription(((char *)&i.scenario) - ((char *)&i), Interpreter::BOOLEAN_ID)); @@ -67,10 +65,34 @@ void Item::CreateTypeDescription() { td.AddField("targets", FieldDescription(((char *)&i.targettingMode) - ((char *)&i), TargetingMode::TYPE_ID).SetDescription("how target selection is to be performed")); td.AddField("ikari", FieldDescription(((char *)&i.ikari) - ((char *)&i), Ikari::TYPE_ID).SetReferenced().SetDescription("ikari attack of the item (sensible only for equipment)")); td.AddField("attackanimation", FieldDescription(((char *)&i.attackAnimation) - ((char *)&i), Animation::TYPE_ID).SetReferenced().SetDescription("animation that is run when the item is used for attacking")); + td.AddField("equipability", FieldDescription(((char *)&i.equipability) - ((char *)&i), Interpreter::NUMBER_ID).SetDescription("how this item can be equipped")); } void Item::Construct(void *data) { new (data) Item; } + +bool Item::Less(const Item &lhs, const Item &rhs) { + if (lhs.IsMostUseful()) { + return !rhs.IsMostUseful(); + } + if (lhs.IsEquipable()) { + if (rhs.IsMostUseful()) { + return false; + } + if (!rhs.IsEquipable()) { + return true; + } + return lhs.equipability < rhs.equipability; + } + if (lhs.IsFruit()) { + if (rhs.IsMostUseful() || rhs.IsEquipable()) { + return true; + } + return !rhs.IsFruit(); + } + return false; +} + } diff --git a/src/common/Item.h b/src/common/Item.h index 2792355..7e757ff 100644 --- a/src/common/Item.h +++ b/src/common/Item.h @@ -29,7 +29,7 @@ public: const char *Name() const { return name; } bool IsMostUseful() const { return mostUseful; } - bool IsEquipable() const { return equipable; } + bool IsEquipable() const { return equipability; } bool IsCursed() const { return cursed; } bool IsFruit() const { return fruit; } bool IsScenario() const { return scenario; } @@ -78,6 +78,8 @@ public: bool HasBattleAnimation() const { return properties & PROPERTY_HAS_BATTLE_ANIMATION; } bool HasIkariEffect() const { return properties & PROPERTY_HAS_IKARI_EFFECT; } + static bool Less(const Item &, const Item &); + // temporary setters public: void SetName(const char *n) { name = n; } @@ -129,12 +131,11 @@ private: Uint16 properties; TargetingMode targettingMode; - Uint8 equipability; + int equipability; HeroGroup equipableBy; // TODO: turn these back into bits as soon as fields are implemented in the loader bool mostUseful; - bool equipable; bool cursed; bool fruit; bool scenario; diff --git a/test-data/constants.l2h b/test-data/constants.l2h index 5ace629..d761fb8 100644 --- a/test-data/constants.l2h +++ b/test-data/constants.l2h @@ -10,6 +10,14 @@ Number multiple Boolean magical Boolean physical +// Items +Number weapon +Number armor +Number shield +Number helmet +Number ring +Number jewel + // Trigger type Number triggerNorth Number triggerEast diff --git a/test-data/constants.l2s b/test-data/constants.l2s index fa01f2b..11d5db5 100644 --- a/test-data/constants.l2s +++ b/test-data/constants.l2s @@ -10,6 +10,14 @@ export Number single 2 export Boolean magical false export Boolean physical true +// Items +export Number weapon 1 +export Number armor 2 +export Number shield 4 +export Number helmet 8 +export Number ring 16 +export Number jewel 32 + // Trigger type export Number triggerNorth 0 export Number triggerEast 1 diff --git a/test-data/items.l2s b/test-data/items.l2s index 4b49dd6..d80b542 100644 --- a/test-data/items.l2s +++ b/test-data/items.l2s @@ -78,26 +78,31 @@ export Item antidoteItem { targets: TargetingMode { faction: ally, mode: single - } + }, + mostUseful: true } export Item eagleRockItem { name: "Eagle rock", menuicon: jewelIcon, - ikari: diveIkari + ikari: diveIkari, + equipability: jewel } export Item escapeItem { name: "Escape", - battle: false + battle: false, + mostUseful: true } export Item evilJewelItem { name: "Evil jewel", menuicon: jewelIcon, - ikari: gloomyIkari + ikari: gloomyIkari, + equipability: jewel } export Item ghostRingItem { name: "Ghost ring", menuicon: ringIcon, - ikari: destroyIkari + ikari: destroyIkari, + equipability: ring } export Item hiPotionItem { name: "Hi-Potion", @@ -106,32 +111,38 @@ export Item hiPotionItem { targets: TargetingMode { faction: ally, mode: single - } + }, + mostUseful: true } export Item holyCapItem { name: "Holy cap", menuicon: helmetIcon, - ikari: vulnerableIkari + ikari: vulnerableIkari, + equipability: helmet } export Item holyRobeItem { name: "Holy robe", menuicon: armorIcon, - ikari: crisisCureIkari + ikari: crisisCureIkari, + equipability: armor } export Item holyShieldItem { name: "Holy shield", menuicon: shieldIcon, - ikari: lightGuardIkari + ikari: lightGuardIkari, + equipability: shield } export Item krakenRockItem { name: "Kraken rock", menuicon: jewelIcon, - ikari: tenLeggerIkari + ikari: tenLeggerIkari, + equipability: jewel } export Item legendHelmItem { name: "Legend helm", menuicon: helmetIcon, - ikari: boomerangIkari + ikari: boomerangIkari, + equipability: helmet } export Item lizardBlowItem { name: "Lizard blow", @@ -140,7 +151,8 @@ export Item lizardBlowItem { targets: TargetingMode { faction: enemy, mode: single - } + }, + equipability: weapon } export Item magicJarItem { name: "Magic jar", @@ -149,27 +161,32 @@ export Item magicJarItem { targets: TargetingMode { faction: ally, mode: single - } + }, + mostUseful: true } export Item megaShieldItem { name: "Mega shield", menuicon: shieldIcon, - ikari: ironBarrierIkari + ikari: ironBarrierIkari, + equipability: shield } export Item powerPotionItem { name: "Power potion", menuicon: potionIcon, - battle: false + battle: false, + mostUseful: true } export Item powerRingItem { name: "Power ring", menuicon: ringIcon, - ikari: trickIkari + ikari: trickIkari, + equipability: ring } export Item rocketRingItem { name: "Rocket ring", menuicon: ringIcon, - ikari: fakeIkari + ikari: fakeIkari, + equipability: ring } export Item sleepBallItem { name: "Sleep ball", @@ -178,12 +195,14 @@ export Item sleepBallItem { targets: TargetingMode { faction: enemy, mode: single - } + }, + mostUseful: true } export Item sProRingItem { name: "S-pro ring", menuicon: ringIcon, - ikari: courageIkari + ikari: courageIkari, + equipability: ring } export Item zircoAxItem { name: "Zirco ax", @@ -192,28 +211,33 @@ export Item zircoAxItem { targets: TargetingMode { faction: enemy, mode: single - } + }, + equipability: weapon } export Item zircoGlovesItem { name: "Zirco gloves", menuicon: shieldIcon, - ikari: forcefieldIkari + ikari: forcefieldIkari, + equipability: shield } export Item zircoHelmetItem { name: "Zirco helmet", menuicon: helmetIcon, - ikari: slowIkari + ikari: slowIkari, + equipability: helmet } export Item zirconArmorItem { name: "Zircon armor", menuicon: armorIcon, battle: false, - ikari: magicCureIkari + ikari: magicCureIkari, + equipability: armor } export Item zirconPlateItem { name: "Zircon plate", menuicon: armorIcon, - ikari: suddenCureIkari + ikari: suddenCureIkari, + equipability: armor } export Item zircoSwordItem { name: "Zirco sword", @@ -224,7 +248,8 @@ export Item zircoSwordItem { mode: single }, ikari: firestormIkari, - attackanimation: swordAttackAnimation + attackanimation: swordAttackAnimation, + equipability: weapon } export Item zircoWhipItem { name: "Zirco whip", @@ -233,5 +258,6 @@ export Item zircoWhipItem { faction: enemy, mode: single }, - ikari: thundershriekIkari + ikari: thundershriekIkari, + equipability: weapon }