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

Update BattleOrders.js #418

Open
wants to merge 8 commits into
base: restructure
Choose a base branch
from
Open
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
91 changes: 59 additions & 32 deletions d2bs/kolbot/libs/scripts/BattleOrders.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const BattleOrders = new Runnable(
/** @type {Set<string>} */
const totalBoed = new Set();
/** @type {Set<string>} */
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,
Expand All @@ -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();
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -92,34 +92,48 @@ 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
// remove those from the getters list who are not in game
function removeMissing () {
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)));
}

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
if (!BattleOrders.gaveBo) {
nearPlayers = Misc.getNearbyPlayerCount();
while (nearPlayers !== boGetters.size) {
if (getTickCount() - tick >= Time.seconds(30)) {
log("Begin");

break;
}

me.overhead(
"Waiting " + Math.round(((tick + Time.seconds(30)) - getTickCount()) / 1000)
+ " for all players to show up"
);
nearPlayers = Misc.getNearbyPlayerCount();
delay(1000);
// check nearby players
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
while (new Set(playersToBo.map(p => p.name.toLowerCase())).size !== boGetters.size) {
if (getTickCount() - tick >= Time.seconds(30)) {
log("Begin");

break;
}

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
playersToBo = getUnits(sdk.unittype.Player)
.filter(p => boGetters.has(p.name.toLowerCase()) && p.distance < 20);

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();

Expand All @@ -140,13 +154,22 @@ const BattleOrders = new Runnable(
}

totalBoed.add(p.name.toLowerCase());
boGetters.delete(p.name.toLowerCase());
console.debug("Bo-ed " + p.name);
boed = true;
}
});

/*
if (boed) {
delay(5000);
delay(5000); // Giving the barb a coffee break?
} */

if (boGetters.size === 0) {
return {
success: boed,
count: playersToBo.length
};
}

return {
Expand Down Expand Up @@ -209,7 +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) {
Expand All @@ -220,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;
}
Expand All @@ -234,7 +257,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) {
Expand All @@ -249,7 +272,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) {
Expand Down Expand Up @@ -299,10 +322,14 @@ const BattleOrders = new Runnable(
}
}

return true;
} catch (e) {
console.error(e);
} finally {
BattleOrders.gaveBo = false;
removeEventListener("chatmsg", chatEvent);
}

return true;
},
{
startArea: sdk.areas.CatacombsLvl2
Expand Down
Loading