From 23ba4222f235c2f94ec632b631df219a849fbefd Mon Sep 17 00:00:00 2001 From: Antiwarden <125026215+Antiwarden@users.noreply.github.com> Date: Sat, 12 Oct 2024 08:43:20 +0200 Subject: [PATCH 1/6] Update BattleOrders.js Adjusts the giveBo() function in BattleOrders.js to ensure that the script waits only for players who are explicitly defined as getters in the configuration, regardless of any additional players present. --- d2bs/kolbot/libs/scripts/BattleOrders.js | 172 +++++++++++++---------- 1 file changed, 97 insertions(+), 75 deletions(-) diff --git a/d2bs/kolbot/libs/scripts/BattleOrders.js b/d2bs/kolbot/libs/scripts/BattleOrders.js index 763cb4cf6..abb995061 100644 --- a/d2bs/kolbot/libs/scripts/BattleOrders.js +++ b/d2bs/kolbot/libs/scripts/BattleOrders.js @@ -56,7 +56,7 @@ const BattleOrders = new Runnable( continue; } else { console.error(e); - // emptry game, don't wait + // empty game, don't wait return true; } } @@ -92,16 +92,18 @@ const BattleOrders = new Runnable( return false; // Not late; wait. } - // bo is AoE, lets build a list of all players near us so we can know who we boed function giveBO () { - // more players might be showing up, give a moment and lets wait until the nearby player count is static - let nearPlayers = 0; let tick = getTickCount(); - - // if we haven't already given a bo, lets wait to see if more players show up + + // check nearby players + const playersToBo = getUnits(sdk.unittype.Player) + .filter(p => boGetters.has(p.name.toLowerCase()) && p.distance < 20); + + // wait for players from boGetters list only, ignoring extra players if (!BattleOrders.gaveBo) { - nearPlayers = Misc.getNearbyPlayerCount(); - while (nearPlayers !== boGetters.size) { + let boGettersNearby = new Set(playersToBo.map(p => p.name.toLowerCase())); + + while (boGettersNearby.size !== boGetters.size) { if (getTickCount() - tick >= Time.seconds(30)) { log("Begin"); @@ -110,16 +112,23 @@ const BattleOrders = new Runnable( me.overhead( "Waiting " + Math.round(((tick + Time.seconds(30)) - getTickCount()) / 1000) - + " for all players to show up" + + " seconds for all BO getters." ); - nearPlayers = Misc.getNearbyPlayerCount(); + + // update nearby players to check only the ones in the getters list + boGettersNearby = new Set( + getUnits(sdk.unittype.Player) + .filter(p => boGetters.has(p.name.toLowerCase()) && p.distance < 20) + .map(p => p.name.toLowerCase()) + ); + delay(1000); } } let boed = false; - const playersToBo = getUnits(sdk.unittype.Player) - .filter(p => boGetters.has(p.name.toLowerCase()) && p.distance < 20); + + // cast BO on the relevant players playersToBo.forEach(p => { tick = getTickCount(); @@ -206,79 +215,79 @@ const BattleOrders = new Runnable( } switch (Config.BattleOrders.Mode) { - case boMode.Give: - // check if anyone is near us - nearPlayer = Game.getPlayer(); - - if (nearPlayer) { - do { - if (nearPlayer.name !== me.name) { - let nearPlayerName = nearPlayer.name.toLowerCase(); - // there is a player near us and they are in the list of players to bo and in my party - if (boGetters.has(nearPlayerName) - && !totalBoed.has(nearPlayerName) - && Misc.inMyParty(nearPlayerName)) { - let result = giveBO(); - if (result.success) { - if (result.count === boGetters.size - || totalBoed.size === boGetters.size) { - // we bo-ed everyone we are set to, don't wait around any longer - break MainLoop; + case boMode.Give: + // check if anyone is near us + nearPlayer = Game.getPlayer(); + + if (nearPlayer) { + do { + if (nearPlayer.name !== me.name) { + let nearPlayerName = nearPlayer.name.toLowerCase(); + // there is a player near us and they are in the list of players to bo and in my party + if (boGetters.has(nearPlayerName) + && !totalBoed.has(nearPlayerName) + && Misc.inMyParty(nearPlayerName)) { + let result = giveBO(); + if (result.success) { + if (result.count === boGetters.size + || totalBoed.size === boGetters.size) { + // we bo-ed everyone we are set to, don't wait around any longer + break MainLoop; + } + // reset fail tick + tick = getTickCount(); + // shorten waiting time since we've already started giving out bo's + BattleOrders.gaveBo = true; } - // reset fail tick - tick = getTickCount(); - // shorten waiting time since we've already started giving out bo's - BattleOrders.gaveBo = true; + } + } else { + me.overhead( + "Waiting " + Math.round(((tick + failTimer) - getTickCount()) / 1000) + + " Seconds for other players" + ); + + if (getTickCount() - tick >= failTimer) { + log("ÿc1Give BO timeout fail."); + log("Failed to bo: " + getFailedToBO().join(", ")); + Config.BattleOrders.QuitOnFailure && scriptBroadcast("quit"); + + break MainLoop; } } - } else { - me.overhead( - "Waiting " + Math.round(((tick + failTimer) - getTickCount()) / 1000) - + " Seconds for other players" - ); - - if (getTickCount() - tick >= failTimer) { - log("ÿc1Give BO timeout fail."); - log("Failed to bo: " + getFailedToBO().join(", ")); - Config.BattleOrders.QuitOnFailure && scriptBroadcast("quit"); - - break MainLoop; - } - } - } while (nearPlayer.getNext()); - } else { - me.overhead( - "Waiting " + Math.round(((tick + failTimer) - getTickCount()) / 1000) - + " Seconds for other players" - ); + } while (nearPlayer.getNext()); + } else { + me.overhead( + "Waiting " + Math.round(((tick + failTimer) - getTickCount()) / 1000) + + " Seconds for other players" + ); - if (getTickCount() - tick >= failTimer) { - log("ÿc1Give BO timeout fail."); - log("Failed to bo: " + getFailedToBO().join(", ")); - Config.BattleOrders.QuitOnFailure && scriptBroadcast("quit"); + if (getTickCount() - tick >= failTimer) { + log("ÿc1Give BO timeout fail."); + log("Failed to bo: " + getFailedToBO().join(", ")); + Config.BattleOrders.QuitOnFailure && scriptBroadcast("quit"); - break MainLoop; + break MainLoop; + } } - } - break; - case boMode.Receive: - if (me.getState(sdk.states.BattleOrders)) { - log("Got bo-ed"); - say("got-bo"); - delay(1000); + break; + case boMode.Receive: + if (me.getState(sdk.states.BattleOrders)) { + log("Got bo-ed"); + say("got-bo"); + delay(1000); - break MainLoop; - } + break MainLoop; + } - if (getTickCount() - tick >= failTimer) { - log("ÿc1BO timeout fail."); - Config.BattleOrders.QuitOnFailure && scriptBroadcast("quit"); + if (getTickCount() - tick >= failTimer) { + log("ÿc1BO timeout fail."); + Config.BattleOrders.QuitOnFailure && scriptBroadcast("quit"); - break MainLoop; - } + break MainLoop; + } - break; + break; } delay(500); @@ -299,10 +308,23 @@ const BattleOrders = new Runnable( } } - return true; + if (Config.BattleOrders.Getters.length > 0) { + const failedToBo = getFailedToBO(); + if (failedToBo.length > 0) { + log("Failed to bo: " + failedToBo.join(", ")); + } else { + log("All designated players received BO."); + } + } + + } catch (e) { + console.error(e); } finally { + BattleOrders.gaveBo = false; removeEventListener("chatmsg", chatEvent); } + + return true; }, { startArea: sdk.areas.CatacombsLvl2 From 781d02311aa6b43ddc593e9fbed2d4bff1c15631 Mon Sep 17 00:00:00 2001 From: theBGuy <60308670+theBGuy@users.noreply.github.com> Date: Sat, 12 Oct 2024 17:03:37 -0400 Subject: [PATCH 2/6] Formatted --- d2bs/kolbot/libs/scripts/BattleOrders.js | 110 +++++++++++------------ 1 file changed, 55 insertions(+), 55 deletions(-) diff --git a/d2bs/kolbot/libs/scripts/BattleOrders.js b/d2bs/kolbot/libs/scripts/BattleOrders.js index abb995061..a0f327a20 100644 --- a/d2bs/kolbot/libs/scripts/BattleOrders.js +++ b/d2bs/kolbot/libs/scripts/BattleOrders.js @@ -215,79 +215,79 @@ const BattleOrders = new Runnable( } switch (Config.BattleOrders.Mode) { - case boMode.Give: - // check if anyone is near us - nearPlayer = Game.getPlayer(); - - if (nearPlayer) { - do { - if (nearPlayer.name !== me.name) { - let nearPlayerName = nearPlayer.name.toLowerCase(); - // there is a player near us and they are in the list of players to bo and in my party - if (boGetters.has(nearPlayerName) + case boMode.Give: + // check if anyone is near us + nearPlayer = Game.getPlayer(); + + if (nearPlayer) { + do { + if (nearPlayer.name !== me.name) { + let nearPlayerName = nearPlayer.name.toLowerCase(); + // there is a player near us and they are in the list of players to bo and in my party + if (boGetters.has(nearPlayerName) && !totalBoed.has(nearPlayerName) && Misc.inMyParty(nearPlayerName)) { - let result = giveBO(); - if (result.success) { - if (result.count === boGetters.size + let result = giveBO(); + if (result.success) { + if (result.count === boGetters.size || totalBoed.size === boGetters.size) { - // we bo-ed everyone we are set to, don't wait around any longer - break MainLoop; - } - // reset fail tick - tick = getTickCount(); - // shorten waiting time since we've already started giving out bo's - BattleOrders.gaveBo = true; + // we bo-ed everyone we are set to, don't wait around any longer + break MainLoop; } + // reset fail tick + tick = getTickCount(); + // shorten waiting time since we've already started giving out bo's + BattleOrders.gaveBo = true; } - } else { - me.overhead( - "Waiting " + Math.round(((tick + failTimer) - getTickCount()) / 1000) + } + } else { + me.overhead( + "Waiting " + Math.round(((tick + failTimer) - getTickCount()) / 1000) + " Seconds for other players" - ); + ); - if (getTickCount() - tick >= failTimer) { - log("ÿc1Give BO timeout fail."); - log("Failed to bo: " + getFailedToBO().join(", ")); - Config.BattleOrders.QuitOnFailure && scriptBroadcast("quit"); + if (getTickCount() - tick >= failTimer) { + log("ÿc1Give BO timeout fail."); + log("Failed to bo: " + getFailedToBO().join(", ")); + Config.BattleOrders.QuitOnFailure && scriptBroadcast("quit"); - break MainLoop; - } + break MainLoop; } - } while (nearPlayer.getNext()); - } else { - me.overhead( - "Waiting " + Math.round(((tick + failTimer) - getTickCount()) / 1000) + } + } while (nearPlayer.getNext()); + } else { + me.overhead( + "Waiting " + Math.round(((tick + failTimer) - getTickCount()) / 1000) + " Seconds for other players" - ); + ); - if (getTickCount() - tick >= failTimer) { - log("ÿc1Give BO timeout fail."); - log("Failed to bo: " + getFailedToBO().join(", ")); - Config.BattleOrders.QuitOnFailure && scriptBroadcast("quit"); + if (getTickCount() - tick >= failTimer) { + log("ÿc1Give BO timeout fail."); + log("Failed to bo: " + getFailedToBO().join(", ")); + Config.BattleOrders.QuitOnFailure && scriptBroadcast("quit"); - break MainLoop; - } + break MainLoop; } + } - break; - case boMode.Receive: - if (me.getState(sdk.states.BattleOrders)) { - log("Got bo-ed"); - say("got-bo"); - delay(1000); + break; + case boMode.Receive: + if (me.getState(sdk.states.BattleOrders)) { + log("Got bo-ed"); + say("got-bo"); + delay(1000); - break MainLoop; - } + break MainLoop; + } - if (getTickCount() - tick >= failTimer) { - log("ÿc1BO timeout fail."); - Config.BattleOrders.QuitOnFailure && scriptBroadcast("quit"); + if (getTickCount() - tick >= failTimer) { + log("ÿc1BO timeout fail."); + Config.BattleOrders.QuitOnFailure && scriptBroadcast("quit"); - break MainLoop; - } + break MainLoop; + } - break; + break; } delay(500); From 619973c369ff8adff0b4dc82f5e24e1abb6669ee Mon Sep 17 00:00:00 2001 From: Antiwarden <125026215+Antiwarden@users.noreply.github.com> Date: Sun, 22 Dec 2024 11:05:01 +0100 Subject: [PATCH 3/6] Keep track of boGetters If some of our getters are missing in game, remove them from the boGetters list and proceed. --- d2bs/kolbot/libs/scripts/BattleOrders.js | 77 ++++++++++++------------ 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/d2bs/kolbot/libs/scripts/BattleOrders.js b/d2bs/kolbot/libs/scripts/BattleOrders.js index a0f327a20..2c65fbe7c 100644 --- a/d2bs/kolbot/libs/scripts/BattleOrders.js +++ b/d2bs/kolbot/libs/scripts/BattleOrders.js @@ -13,7 +13,7 @@ const BattleOrders = new Runnable( /** @type {Set} */ const totalBoed = new Set(); /** @type {Set} */ - const boGetters = new Set(Config.BattleOrders.Getters.map(name => name.toLowerCase())); + let boGetters = new Set(Config.BattleOrders.Getters.map(name => name.toLowerCase())); const boMode = { Give: 0, @@ -39,13 +39,13 @@ const BattleOrders = new Runnable( } catch (e) { if (Config.BattleOrders.Wait) { let counter = 0; - console.log("Waiting " + Config.BattleOrders.Wait + " seconds for other players..."); + console.log("Waiting " + Config.BattleOrders.Wait + " seconds for other boGetters..."); Misc.poll(() => { counter++; me.overhead( "Waiting " + Math.round(((tick + Time.seconds(Config.BattleOrders.Wait)) - getTickCount()) / 1000) - + " Seconds for other players" + + " seconds for other getters" ); if (counter % 5 === 0) { return checkForPlayers(); @@ -92,38 +92,37 @@ const BattleOrders = new Runnable( return false; // Not late; wait. } + // if some getters are not in the game, remove them from the getters list + function removeMissing () { + const activePlayers = new Set(getUnits(sdk.unittype.Player).map(p => p.name.toLowerCase())); + boGetters = new Set([...boGetters].filter(name => activePlayers.has(name))); + } + function giveBO () { let tick = getTickCount(); // check nearby players - const playersToBo = getUnits(sdk.unittype.Player) + let playersToBo = getUnits(sdk.unittype.Player) .filter(p => boGetters.has(p.name.toLowerCase()) && p.distance < 20); // wait for players from boGetters list only, ignoring extra players - if (!BattleOrders.gaveBo) { - let boGettersNearby = new Set(playersToBo.map(p => p.name.toLowerCase())); - - while (boGettersNearby.size !== boGetters.size) { - if (getTickCount() - tick >= Time.seconds(30)) { - log("Begin"); + while (new Set(playersToBo.map(p => p.name.toLowerCase())).size !== boGetters.size) { + if (getTickCount() - tick >= Time.seconds(30)) { + log("Begin"); - break; - } + break; + } - me.overhead( - "Waiting " + Math.round(((tick + Time.seconds(30)) - getTickCount()) / 1000) - + " seconds for all BO getters." - ); + me.overhead( + "Waiting " + Math.round(((tick + Time.seconds(30)) - getTickCount()) / 1000) + + " seconds for all getters to show up" + ); - // update nearby players to check only the ones in the getters list - boGettersNearby = new Set( - getUnits(sdk.unittype.Player) - .filter(p => boGetters.has(p.name.toLowerCase()) && p.distance < 20) - .map(p => p.name.toLowerCase()) - ); + // update nearby players to check only the ones in the getters list + playersToBo = getUnits(sdk.unittype.Player) + .filter(p => boGetters.has(p.name.toLowerCase()) && p.distance < 20); - delay(1000); - } + delay(1000); } let boed = false; @@ -154,9 +153,10 @@ const BattleOrders = new Runnable( } }); + /* if (boed) { - delay(5000); - } + delay(5000); // Giving the barb a coffee break? + } */ return { success: boed, @@ -210,6 +210,12 @@ const BattleOrders = new Runnable( MainLoop: while (true) { + removeMissing(); // remove missing players from the list of getters + /* + if (me.classid === sdk.player.class.Barbarian) { + console.debug("Getters in game: " + [...boGetters].join(", ")); + } */ + if (Config.BattleOrders.SkipIfTardy && tardy()) { break; } @@ -225,12 +231,12 @@ const BattleOrders = new Runnable( let nearPlayerName = nearPlayer.name.toLowerCase(); // there is a player near us and they are in the list of players to bo and in my party if (boGetters.has(nearPlayerName) - && !totalBoed.has(nearPlayerName) - && Misc.inMyParty(nearPlayerName)) { + && !totalBoed.has(nearPlayerName) + && Misc.inMyParty(nearPlayerName)) { let result = giveBO(); if (result.success) { if (result.count === boGetters.size - || totalBoed.size === boGetters.size) { + || totalBoed.size === boGetters.size) { // we bo-ed everyone we are set to, don't wait around any longer break MainLoop; } @@ -243,7 +249,7 @@ const BattleOrders = new Runnable( } else { me.overhead( "Waiting " + Math.round(((tick + failTimer) - getTickCount()) / 1000) - + " Seconds for other players" + + " seconds for other getters" ); if (getTickCount() - tick >= failTimer) { @@ -258,7 +264,7 @@ const BattleOrders = new Runnable( } else { me.overhead( "Waiting " + Math.round(((tick + failTimer) - getTickCount()) / 1000) - + " Seconds for other players" + + " seconds for other getters" ); if (getTickCount() - tick >= failTimer) { @@ -308,15 +314,6 @@ const BattleOrders = new Runnable( } } - if (Config.BattleOrders.Getters.length > 0) { - const failedToBo = getFailedToBO(); - if (failedToBo.length > 0) { - log("Failed to bo: " + failedToBo.join(", ")); - } else { - log("All designated players received BO."); - } - } - } catch (e) { console.error(e); } finally { From 48a98f3ed39f03d7035ddb31ca5a0693d9241929 Mon Sep 17 00:00:00 2001 From: Antiwarden <125026215+Antiwarden@users.noreply.github.com> Date: Sun, 22 Dec 2024 12:06:37 +0100 Subject: [PATCH 4/6] Fix removeMissing() Last change dropped all getters from the list who were not present at the bo spot. Now, it removes only those from the list who are not in the game. --- d2bs/kolbot/libs/scripts/BattleOrders.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/d2bs/kolbot/libs/scripts/BattleOrders.js b/d2bs/kolbot/libs/scripts/BattleOrders.js index 2c65fbe7c..111981759 100644 --- a/d2bs/kolbot/libs/scripts/BattleOrders.js +++ b/d2bs/kolbot/libs/scripts/BattleOrders.js @@ -94,7 +94,13 @@ const BattleOrders = new Runnable( // if some getters are not in the game, remove them from the getters list function removeMissing () { - const activePlayers = new Set(getUnits(sdk.unittype.Player).map(p => p.name.toLowerCase())); + const activePlayers = new Set(); + let player = getParty(); + if (player) { + do { + activePlayers.add(player.name.toLowerCase()); + } while (player.getNext()); + } boGetters = new Set([...boGetters].filter(name => activePlayers.has(name))); } From d9bc3547244bea092db8a0ee54242f990df4d58e Mon Sep 17 00:00:00 2001 From: Antiwarden <125026215+Antiwarden@users.noreply.github.com> Date: Sun, 22 Dec 2024 22:13:57 +0100 Subject: [PATCH 5/6] Move removeMissing() call inside boMode.Give case --- d2bs/kolbot/libs/scripts/BattleOrders.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/d2bs/kolbot/libs/scripts/BattleOrders.js b/d2bs/kolbot/libs/scripts/BattleOrders.js index 111981759..b24b55f38 100644 --- a/d2bs/kolbot/libs/scripts/BattleOrders.js +++ b/d2bs/kolbot/libs/scripts/BattleOrders.js @@ -92,7 +92,7 @@ const BattleOrders = new Runnable( return false; // Not late; wait. } - // if some getters are not in the game, remove them from the getters list + // remove those from the getters list who are not in game function removeMissing () { const activePlayers = new Set(); let player = getParty(); @@ -216,12 +216,6 @@ const BattleOrders = new Runnable( MainLoop: while (true) { - removeMissing(); // remove missing players from the list of getters - /* - if (me.classid === sdk.player.class.Barbarian) { - console.debug("Getters in game: " + [...boGetters].join(", ")); - } */ - if (Config.BattleOrders.SkipIfTardy && tardy()) { break; } @@ -231,6 +225,9 @@ const BattleOrders = new Runnable( // check if anyone is near us nearPlayer = Game.getPlayer(); + removeMissing(); // remove missing getters from the list + //console.debug("Getters in game: " + [...boGetters].join(", ")); + if (nearPlayer) { do { if (nearPlayer.name !== me.name) { From f100955a2f25c0751542fbd99750e3e261dfc477 Mon Sep 17 00:00:00 2001 From: Antiwarden <125026215+Antiwarden@users.noreply.github.com> Date: Fri, 27 Dec 2024 16:25:15 +0100 Subject: [PATCH 6/6] Fix boGiver waiting issue with late-arriving getters Fixes an issue where the boGiver would continue to wait even after successfully casting bo on a late-arriving getter after the wait timer had expired. - remove getters from the list after they receive bo - giveBO() function returns immediately once all getters have received bo - stop the main loop once all getters have received bo --- d2bs/kolbot/libs/scripts/BattleOrders.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/d2bs/kolbot/libs/scripts/BattleOrders.js b/d2bs/kolbot/libs/scripts/BattleOrders.js index b24b55f38..153f0e9c1 100644 --- a/d2bs/kolbot/libs/scripts/BattleOrders.js +++ b/d2bs/kolbot/libs/scripts/BattleOrders.js @@ -154,6 +154,7 @@ const BattleOrders = new Runnable( } totalBoed.add(p.name.toLowerCase()); + boGetters.delete(p.name.toLowerCase()); console.debug("Bo-ed " + p.name); boed = true; } @@ -164,6 +165,13 @@ const BattleOrders = new Runnable( delay(5000); // Giving the barb a coffee break? } */ + if (boGetters.size === 0) { + return { + success: boed, + count: playersToBo.length + }; + } + return { success: boed, count: playersToBo.length @@ -224,10 +232,8 @@ const BattleOrders = new Runnable( case boMode.Give: // check if anyone is near us nearPlayer = Game.getPlayer(); - removeMissing(); // remove missing getters from the list //console.debug("Getters in game: " + [...boGetters].join(", ")); - if (nearPlayer) { do { if (nearPlayer.name !== me.name) { @@ -238,8 +244,7 @@ const BattleOrders = new Runnable( && Misc.inMyParty(nearPlayerName)) { let result = giveBO(); if (result.success) { - if (result.count === boGetters.size - || totalBoed.size === boGetters.size) { + if (boGetters.size === 0) { // we bo-ed everyone we are set to, don't wait around any longer break MainLoop; }