Skip to content

Commit

Permalink
Fix antihostile and other changes (#1416)
Browse files Browse the repository at this point in the history
AntiHostile.js
-keep thread running for subsequent hostiles and quit if bot fails to go to town

class attacks, Town.js
-add repair check for barb/amazons before attack

Misc.js
-add year/date timestamp for item and error logs

Default.dbj
-add check for gameaction tag length
-skip repairing for indestructible items

Town.js
-add force repair param on town visit
-pass param to doChores
  • Loading branch information
imbalanced authored and noah- committed Apr 5, 2019
1 parent 55a1b30 commit c64eba1
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 48 deletions.
6 changes: 5 additions & 1 deletion d2bs/kolbot/default.dbj
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ function main() {
}

if (mode === 1638) { // getProfile
GameAction.init(JSON.parse(msg).Tag);
msg = JSON.parse(msg);

if (msg.Tag) {
GameAction.init(msg.Tag);
}
}
};

Expand Down
8 changes: 5 additions & 3 deletions d2bs/kolbot/libs/common/Attacks/Amazon.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ var ClassAttack = {
lightFuryTick: 0,

doAttack: function (unit, preattack) {
if (Config.MercWatch && Town.needMerc()) {
Town.visitTown();
var needRepair = Town.needRepair();

if ((Config.MercWatch && Town.needMerc()) || needRepair.length > 0) {
Town.visitTown(!!needRepair.length);
}

if (preattack && Config.AttackSkill[0] > 0 && Attack.checkResist(unit, Config.AttackSkill[0]) && (!me.getState(121) || !Skill.isTimed(Config.AttackSkill[0]))) {
Expand Down Expand Up @@ -102,7 +104,7 @@ var ClassAttack = {
needRepair = Town.needRepair();

if (needRepair && needRepair.length > 0) { // Repair check, mainly to restock arrows
Town.visitTown();
Town.visitTown(true);
}

this.lightFuryTick = 0;
Expand Down
8 changes: 5 additions & 3 deletions d2bs/kolbot/libs/common/Attacks/Barbarian.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

var ClassAttack = {
doAttack: function (unit, preattack) {
if (Config.MercWatch && Town.needMerc()) {
Town.visitTown();
var needRepair = Town.needRepair();

if ((Config.MercWatch && Town.needMerc()) || needRepair.length > 0) {
Town.visitTown(!!needRepair.length);
}

if (preattack && Config.AttackSkill[0] > 0 && Attack.checkResist(unit, Attack.getSkillElement(Config.AttackSkill[0])) && (!me.getState(121) || !Skill.isTimed(Config.AttackSkill[0]))) {
Expand Down Expand Up @@ -59,7 +61,7 @@ var ClassAttack = {
needRepair = Town.needRepair();

if (needRepair && needRepair.length > 0) { // Repair check
Town.visitTown();
Town.visitTown(true);
}

if (pickit) {
Expand Down
15 changes: 5 additions & 10 deletions d2bs/kolbot/libs/common/Misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1290,10 +1290,7 @@ var Misc = {

var desc,
date = new Date(),
h = date.getHours(),
m = date.getMinutes(),
s = date.getSeconds(),
dateString = "[" + (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s) + "]";
dateString = "[" + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(0,-5).replace(/-/g, '/').replace('T', ' ') + "]";

switch (action) {
case "Sold":
Expand Down Expand Up @@ -1847,23 +1844,21 @@ MainLoop:

// Report script errors to logs/ScriptErrorLog.txt
errorReport: function (error, script) {
var i, h, m, s, date, msg, oogmsg, filemsg, source, stack,
var i, date, dateString, msg, oogmsg, filemsg, source, stack,
stackLog = "";

date = new Date();
h = date.getHours();
m = date.getMinutes();
s = date.getSeconds();
dateString = "[" + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(0,-5).replace(/-/g, '/').replace('T', ' ') + "]";

if (typeof error === "string") {
msg = error;
oogmsg = error.replace(/ÿc[0-9!"+<:;.*]/gi, "");
filemsg = "[" + (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s) + "] <" + me.profile + "> " + error.replace(/ÿc[0-9!"+<:;.*]/gi, "") + "\n";
filemsg = dateString + " <" + me.profile + "> " + error.replace(/ÿc[0-9!"+<:;.*]/gi, "") + "\n";
} else {
source = error.fileName.substring(error.fileName.lastIndexOf("\\") + 1, error.fileName.length);
msg = "ÿc1Error in ÿc0" + script + " ÿc1(" + source + " line ÿc1" + error.lineNumber + "): ÿc1" + error.message;
oogmsg = " Error in " + script + " (" + source + " #" + error.lineNumber + ") " + error.message + " (Area: " + me.area + ", Ping:" + me.ping + ", Game: " + me.gamename + ")";
filemsg = "[" + (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":" + (s < 10 ? "0" + s : s) + "] <" + me.profile + "> " + msg.replace(/ÿc[0-9!"+<:;.*]/gi, "") + "\n";
filemsg = dateString + " <" + me.profile + "> " + msg.replace(/ÿc[0-9!"+<:;.*]/gi, "") + "\n";

if (error.hasOwnProperty("stack")) {
stack = error.stack;
Expand Down
54 changes: 30 additions & 24 deletions d2bs/kolbot/libs/common/Town.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var Town = {
],

// Do town chores
doChores: function () {
doChores: function (repair = false) {
if (!me.inTown) {
this.goToTown();
}
Expand All @@ -89,7 +89,7 @@ var Town = {
this.clearInventory();
Item.autoEquip();
this.buyKeys();
this.repair();
this.repair(repair);
this.gamble();
this.reviveMerc();
Cubing.doCubing();
Expand Down Expand Up @@ -1255,13 +1255,17 @@ CursorLoop:
return false;
},

repair: function () {
repair: function (force = false) {
var i, quiver, myQuiver, npc, repairAction, bowCheck;

this.cubeRepair();

repairAction = this.needRepair();

if (force && repairAction.indexOf("repair") === -1) {
repairAction.push("repair");
}

if (!repairAction || !repairAction.length) {
return true;
}
Expand Down Expand Up @@ -1367,28 +1371,30 @@ CursorLoop:
if (item) {
do {
if (!item.getFlag(0x400000)) { // Skip ethereal items
switch (item.itemType) {
// Quantity check
case 42: // Throwing knives
case 43: // Throwing axes
case 44: // Javelins
case 87: // Amazon javelins
quantity = item.getStat(70);

if (typeof quantity === "number" && quantity * 100 / (getBaseStat("items", item.classid, "maxstack") + item.getStat(254)) <= repairPercent) { // Stat 254 = increased stack size
itemList.push(copyUnit(item));
}
if (!item.getStat(152)) { // Skip indestructible items
switch (item.itemType) {
// Quantity check
case 42: // Throwing knives
case 43: // Throwing axes
case 44: // Javelins
case 87: // Amazon javelins
quantity = item.getStat(70);

if (typeof quantity === "number" && quantity * 100 / (getBaseStat("items", item.classid, "maxstack") + item.getStat(254)) <= repairPercent) { // Stat 254 = increased stack size
itemList.push(copyUnit(item));
}

break;
// Durability check
default:
durability = item.getStat(72);
break;
// Durability check
default:
durability = item.getStat(72);

if (typeof durability === "number" && durability * 100 / item.getStat(73) <= repairPercent) {
itemList.push(copyUnit(item));
}
if (typeof durability === "number" && durability * 100 / item.getStat(73) <= repairPercent) {
itemList.push(copyUnit(item));
}

break;
break;
}
}

if (chargedItems) {
Expand Down Expand Up @@ -2168,7 +2174,7 @@ MainLoop:
return true;
},

visitTown: function () {
visitTown: function (repair = false) {
if (me.inTown) {
this.doChores();
this.move("stash");
Expand All @@ -2185,7 +2191,7 @@ MainLoop:
return false;
}

this.doChores();
this.doChores(repair);

if (me.act !== preAct) {
this.goToTown(preAct);
Expand Down
48 changes: 41 additions & 7 deletions d2bs/kolbot/tools/AntiHostile.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ include("common/Town.js");

function main() {
// Variables and functions
var player, attackCount, prevPos, check, missile,
var player, attackCount, prevPos, check, missile, outside,
charClass = ["Amazon", "Sorceress", "Necromancer", "Paladin", "Barbarian", "Druid", "Assassin"],
hostiles = [];

Expand Down Expand Up @@ -194,20 +194,37 @@ function main() {

if (hostiles.length > 0 && (Config.HostileAction === 0 || (Config.HostileAction === 1 && me.inTown))) {
if (Config.TownOnHostile) {
print("ÿc1Hostility detected, going to town.");
this.pause();
Town.goToTown();

if (!me.inTown) {
outside = true;
}

try {
Town.goToTown();
} catch (e) {
print(e + " Failed to go to town. Quitting.");
scriptBroadcast("quit"); // quit if failed to go to town
}

while (hostiles.length > 0) {
delay(500);
}

Pather.usePortal(null, me.name);
if (outside) {
outside = false;
Pather.usePortal(null, me.name);
}

this.resume();
} else {
scriptBroadcast("quit");
}

return;
delay(500);

continue;
}

// Mode 3 - Spam entrance (still experimental)
Expand Down Expand Up @@ -289,20 +306,37 @@ function main() {
// Mode 1 - Quit if hostile player is nearby
if (Config.HostileAction === 1) {
if (Config.TownOnHostile) {
print("ÿc1Hostile player nearby, going to town.");
this.pause();
Town.goToTown();

if (!me.inTown) {
outside = true;
}

try {
Town.goToTown();
} catch (e) {
print(e + " Failed to go to town. Quitting.");
scriptBroadcast("quit"); // quit if failed to go to town
}

while (hostiles.length > 0) {
delay(500);
}

Pather.usePortal(null, me.name);
if (outside) {
outside = false;
Pather.usePortal(null, me.name);
}

this.resume();
} else {
scriptBroadcast("quit");
}

return;
delay(500);

continue;
}

// Kill the hostile player
Expand Down

0 comments on commit c64eba1

Please sign in to comment.