Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merc autoequip #129

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
270 changes: 264 additions & 6 deletions d2bs/kolbot/libs/common/Misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ var Skill = {
break;
}

MainLoop:
MainLoop:
for (n = 0; n < 3; n += 1) {
if (typeof x === "object") {
clickMap(clickType, shift, x);
Expand Down Expand Up @@ -458,7 +458,11 @@ MainLoop:

var Item = {
hasTier: function (item) {
return Config.AutoEquip && NTIP.GetTier(item) > 0;
return Config.AutoEquip && (NTIP.GetTier(item) > 0 || NTIP.GetMercTier(item) > 0);
},

hasMercTier: function (item) {
return Config.AutoEquip && NTIP.GetMercTier(item) > 0;
},

canEquip: function (item) {
Expand All @@ -477,6 +481,29 @@ var Item = {
return true;
},

canEquipMerc: function (item, bodyLoc) {
if (item.type !== 4) { // Not an item
return false;
}

if (!me.getMerc()) { // dont have merc or he is dead
return false;
}

if (!item.getFlag(0x10)) { // Unid item
return false;
}

var merc = me.getMerc();
var curr = this.getEquippedItemMerc(bodyLoc);

if (item.getStat(92) > merc.getStat(12) || item.dexreq > merc.getStat(2) - curr.dex || item.strreq > merc.getStat(0) - curr.str) { // Higher requirements
return false;
}

return true;
},

// Equips an item and throws away the old equipped item
equip: function (item, bodyLoc) {
if (!this.canEquip(item)) {
Expand Down Expand Up @@ -522,6 +549,50 @@ var Item = {
return false;
},

// Equips an item and throws away the old equipped item
equipMerc: function (item, bodyLoc) {
if (!this.canEquipMerc(item, bodyLoc)) {
return false;
}

// Already equipped in the right slot
if (item.mode === 1 && Number(this.getBodyLoc(item)) === bodyLoc) {
return true;
}

var i, cursorItem;

if (item.location === 7) {
if (!Town.openStash()) {
return false;
}
}

for (i = 0; i < 3; i += 1) {
if (item.toCursor()) {
clickItem(4, bodyLoc);
delay(me.ping * 2 + 500);

if (Number(this.getBodyLoc(item)) === bodyLoc) {
if (getCursorType() === 3) {

cursorItem = getUnit(100);

if (cursorItem) {
if (!Storage.Inventory.CanFit(cursorItem) || !Storage.Inventory.MoveTo(cursorItem)) {
cursorItem.drop();
}
}
}

return true;
}
}
}

return false;
},

getEquippedItem: function (bodyLoc) {
var item = me.getItem();

Expand All @@ -543,6 +614,34 @@ var Item = {
};
},

getEquippedItemMerc: function (bodyLoc) {
var merc = me.getMerc();
var item = merc.getItem();

if (item) {
do {
if (item.bodylocation === bodyLoc && item.location === 1) {
return {
classid: item.classid,
tier: NTIP.GetMercTier(item),
name: item.name,
str: item.getStatEx(0),
dex: item.getStatEx(2)
};
}
} while (item.getNext());
}

// Don't have anything equipped in there
return {
classid: -1,
tier: -1,
name: "none",
str: 0,
dex: 0
};
},

getBodyLoc: function (item) {
var bodyLoc;

Expand Down Expand Up @@ -625,29 +724,103 @@ var Item = {
return bodyLoc;
},

getBodyLocMerc: function (item) {
var bodyLocMerc;

switch (item.itemType) {
case 2: // Shield
bodyLocMerc = 5;

break;
case 3: // Armor
bodyLocMerc = 3;

break;

case 37: // Helm
case 75: // Circlet
bodyLocMerc = 1;

break;
case 27: // Bow
case 30: // Sword
case 33: // Spear
case 34: // Polearm
case 44: // Javelin
bodyLocMerc = 4;

break;
default:
return false;
}

if (typeof bodyLocMerc === "number") {
bodyLocMerc = [bodyLocMerc];
}

return bodyLocMerc;
},

// Returns false if item is of lower tier than the item equipped.
autoEquipCheck: function (item) {
if (!Config.AutoEquip) {
return true;
}

// Prevent hoarding of merc items if the merc isn't alive
if (this.hasMercTier(item) && !me.getMerc() && NTIP.GetMercTier(item) < 100) {
return false;
}

var i,
tier = NTIP.GetTier(item),
bodyLoc = this.getBodyLoc(item);


if (tier > 0 && bodyLoc) {
for (i = 0; i < bodyLoc.length; i += 1) {
// Low tier items shouldn't be kept if they can't be equipped
if (tier > this.getEquippedItem(bodyLoc[i]).tier && (this.canEquip(item) || !item.getFlag(0x10))) {
var oldTier = this.getEquippedItem(bodyLoc[i]).tier;

if (tier > oldTier && (this.canEquip(item) || !item.getFlag(0x10))) {
return true;
}
}
}

var mercTier, mercBodyLoc;

if (me.getMerc()) {
mercTier = NTIP.GetMercTier(item);
mercBodyLoc = this.getBodyLocMerc(item);

if (mercTier > 0 && mercBodyLoc) {
for (i = 0; i < mercBodyLoc.length; i += 1) {
// Low tier items shouldn't be kept if they can't be equipped
var oldTierMerc = this.getEquippedItemMerc(mercBodyLoc[i]).tier;

if (mercTier > oldTierMerc && (this.canEquipMerc(item) || !item.getFlag(0x10))) {
//print("\xFFc8MercAutoEquip :: New merc item: " + color + item.name + " (new: " + mercTier + ", old: " + oldTierMerc + ")");

return true;
}
}
}
}

// Sell/ignore low tier items, keep high tier
if (tier > 0 && tier < 100) {
return false;
}

if (me.getMerc()) {
mercTier = NTIP.GetMercTier(item);

if (mercTier > 0 && mercTier < 100) {
return false;
}
}

return true;
},

Expand All @@ -664,7 +837,7 @@ var Item = {
return false;
}

function sortEq(a, b) {
function sortEq (a, b) {
if (Item.canEquip(a)) {
return -1;
}
Expand Down Expand Up @@ -724,6 +897,91 @@ var Item = {
items.shift();
}

return true;
},

// returns true if the item should be kept+logged, false if not
autoEquipMerc: function () {
if (!Config.AutoEquip || !me.getMerc()) {
return true;
}

var i, j, tier, bodyLoc, tome, gid, classid,
items = me.findItems(-1, 0);

if (!items) {
print("No Items");

return false;
}

function sortEq (a, b) {
if (Item.canEquipMerc(a) && Item.canEquipMerc(b)) {
return NTIP.GetMercTier(b) - NTIP.GetMercTier(a);
}

if (Item.canEquipMerc(a)) {
return -1;
}

if (Item.canEquipMerc(b)) {
return 1;
}

return 0;
}

me.cancel();

// Remove items without tier
for (i = 0; i < items.length; i += 1) {
if (NTIP.GetMercTier(items[i]) === 0) {
items.splice(i, 1);
i -= 1;
}
}

while (items.length > 0) {
items.sort(sortEq);

tier = NTIP.GetMercTier(items[0]);
bodyLoc = this.getBodyLocMerc(items[0]);

if (tier > 0 && bodyLoc) {
for (j = 0; j < bodyLoc.length; j += 1) {
if ([3, 7].indexOf(items[0].location) > -1 && tier > this.getEquippedItemMerc(bodyLoc[j]).tier) { // khalim's will adjustment
if (!items[0].getFlag(0x10)) { // unid
tome = me.findItem(519, 0, 3);

if (tome && tome.getStat(70) > 0) {
if (items[0].location === 7) {
Town.openStash();
}

Town.identifyItem(items[0], tome);
}
}

if (this.equipMerc(items[0], bodyLoc[j])) {
// gid changes when equipped to merc
var merc = me.getMerc();
var mercItems = merc.getItems();

for (var k = 0; k < mercItems.length; k += 1) {
if (mercItems[k].bodylocation === Number(bodyLoc)) {
Misc.logItem("Merc Equipped", mercItems[k]);
}
}
}

break;
}
}
}

items.shift();
}

return true;
}
};
Expand Down Expand Up @@ -1843,7 +2101,7 @@ MainLoop:
var i,
contents = "";

MainLoop:
MainLoop:
for (i = 0; i < 30; i += 1) {
try {
switch (mode) {
Expand Down Expand Up @@ -2341,7 +2599,7 @@ var Packet = {
return false;
}

CursorLoop:
CursorLoop:
for (i = 0; i < 3; i += 1) {
sendPacket(1, 0x27, 4, unit.gid, 4, tome.gid);

Expand Down
Loading