Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.runelite.client.plugins.microbot.aiofighter.loot;

import lombok.extern.slf4j.Slf4j;
import net.runelite.client.plugins.grounditems.GroundItem;
import net.runelite.client.plugins.microbot.Microbot;
import net.runelite.client.plugins.microbot.Script;
import net.runelite.client.plugins.microbot.aiofighter.AIOFighterConfig;
Expand All @@ -9,205 +10,108 @@
import net.runelite.client.plugins.microbot.aiofighter.enums.State;
import net.runelite.client.plugins.microbot.util.grounditem.LootingParameters;
import net.runelite.client.plugins.microbot.util.grounditem.Rs2GroundItem;
import net.runelite.client.plugins.microbot.util.grounditem.Rs2LootEngine;
import net.runelite.client.plugins.microbot.util.inventory.Rs2Inventory;
import net.runelite.client.plugins.microbot.util.player.Rs2Player;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;

@Slf4j
public class LootScript extends Script {
int minFreeSlots = 0;

public LootScript() {
private static final int DEFAULT_MIN_STACK_EXCLUSIVE_ARROWS = 9; // allow 2+
private static final int DEFAULT_MIN_STACK_EXCLUSIVE_RUNES = 1; // allow 2+

}
private int minFreeSlots = 0;

public LootScript() {}

public boolean run(AIOFighterConfig config) {

mainScheduledFuture = scheduledExecutorService.scheduleWithFixedDelay(() -> {
try {
minFreeSlots = config.bank() ? config.minFreeSlots() : 0;

if (!super.run()) return;
if (!Microbot.isLoggedIn()) return;
if (!config.toggleLootItems()) return;
if (AIOFighterPlugin.getState().equals(State.BANKING) || AIOFighterPlugin.getState().equals(State.WALKING)) return;
if (((Rs2Inventory.isFull() || Rs2Inventory.getEmptySlots() <= minFreeSlots) && !config.eatFoodForSpace()) || (Rs2Player.isInCombat() && !config.toggleForceLoot() && !AIOFighterPlugin.isWaitingForLoot())) {
return;
}

if (config.toggleWaitForLoot()) {
//TODO: currently disabled as it had merge conflicts with ducks changes.
}

final State st = AIOFighterPlugin.getState();
if (st == State.BANKING || st == State.WALKING) return;

if (config.looterStyle().equals(DefaultLooterStyle.MIXED) || config.looterStyle().equals(DefaultLooterStyle.ITEM_LIST)) {
lootItemsOnName(config);
if (((Rs2Inventory.isFull() || Rs2Inventory.getEmptySlots() <= minFreeSlots) && !config.eatFoodForSpace())
|| (Rs2Player.isInCombat() && !config.toggleForceLoot())) {
return;
}


if (config.looterStyle().equals(DefaultLooterStyle.GE_PRICE_RANGE) || config.looterStyle().equals(DefaultLooterStyle.MIXED)) {
lootItemsByValue(config);
LootingParameters params = new LootingParameters(
config.minPriceOfItemsToLoot(),
config.maxPriceOfItemsToLoot(),
config.attackRadius(),
/* minQuantity */ 1,
/* minInvSlots */ minFreeSlots,
config.toggleDelayedLooting(),
config.toggleOnlyLootMyItems()
);
params.setEatFoodForSpace(config.eatFoodForSpace());

Rs2LootEngine.Builder builder = Rs2LootEngine.with(params)
.withLootAction(Rs2GroundItem::coreLoot);

// custom filter
if (config.looterStyle() == DefaultLooterStyle.ITEM_LIST || config.looterStyle() == DefaultLooterStyle.MIXED) {
addCustomNames(builder, config.listOfItemsToLoot());
}

lootBones(config);
lootAshes(config);
lootRunes(config);
lootCoins(config);
lootUntradeableItems(config);
lootArrows(config);

} catch(Exception ex) {
Microbot.log("Looterscript: " + ex.getMessage());
}

}, 0, 200, TimeUnit.MILLISECONDS);
return true;
}
if (config.looterStyle() == DefaultLooterStyle.GE_PRICE_RANGE || config.looterStyle() == DefaultLooterStyle.MIXED) builder.addByValue();
if (config.toggleBuryBones()) builder.addBones();
if (config.toggleScatter()) builder.addAshes();
if (config.toggleLootCoins()) builder.addCoins();
if (config.toggleLootUntradables()) builder.addUntradables();
if (config.toggleLootArrows()) builder.addArrows(DEFAULT_MIN_STACK_EXCLUSIVE_ARROWS);
if (config.toggleLootRunes()) builder.addRunes(DEFAULT_MIN_STACK_EXCLUSIVE_RUNES);

private void lootArrows(AIOFighterConfig config) {
if (config.toggleLootArrows()) {
LootingParameters arrowParams = new LootingParameters(
config.attackRadius(),
1,
10,
minFreeSlots,
config.toggleDelayedLooting(),
config.toggleOnlyLootMyItems(),
"arrow"
);
arrowParams.setEatFoodForSpace(config.eatFoodForSpace());
if (Rs2GroundItem.lootItemsBasedOnNames(arrowParams)) {
Microbot.pauseAllScripts.compareAndSet(true, false);
}
}
}

private void lootBones(AIOFighterConfig config) {
if (config.toggleBuryBones()) {
LootingParameters bonesParams = new LootingParameters(
config.attackRadius(),
1,
1,
minFreeSlots,
config.toggleDelayedLooting(),
config.toggleOnlyLootMyItems(),
"bones"
);
bonesParams.setEatFoodForSpace(config.eatFoodForSpace());
if (Rs2GroundItem.lootItemsBasedOnNames(bonesParams)) {
Microbot.pauseAllScripts.compareAndSet(true, false);
}
}
}

private void lootAshes(AIOFighterConfig config) {
if (config.toggleScatter()) {
LootingParameters ashesParams = new LootingParameters(
config.attackRadius(),
1,
1,
minFreeSlots,
config.toggleDelayedLooting(),
config.toggleOnlyLootMyItems(),
" ashes"
);
ashesParams.setEatFoodForSpace(config.eatFoodForSpace());
if (Rs2GroundItem.lootItemsBasedOnNames(ashesParams)) {
Microbot.pauseAllScripts.compareAndSet(true, false);
}
}
}
// Execute one combined, distance-sorted looting pass
builder.loot();

// loot runes
private void lootRunes(AIOFighterConfig config) {
if (config.toggleLootRunes()) {
LootingParameters runesParams = new LootingParameters(
config.attackRadius(),
1,
1,
minFreeSlots,
config.toggleDelayedLooting(),
config.toggleOnlyLootMyItems(),
" rune"
);
runesParams.setEatFoodForSpace(config.eatFoodForSpace());
if (Rs2GroundItem.lootItemsBasedOnNames(runesParams)) {
Microbot.pauseAllScripts.compareAndSet(true, false);
} catch (Exception ex) {
Microbot.log("LootScript: " + ex.getMessage());
}
}
}
}, 0, 200, TimeUnit.MILLISECONDS);

// loot coins
private void lootCoins(AIOFighterConfig config) {
if (config.toggleLootCoins()) {
LootingParameters coinsParams = new LootingParameters(
config.attackRadius(),
1,
1,
minFreeSlots,
config.toggleDelayedLooting(),
config.toggleOnlyLootMyItems(),
"coins"
);
coinsParams.setEatFoodForSpace(config.eatFoodForSpace());
if (Rs2GroundItem.lootCoins(coinsParams)) {
Microbot.pauseAllScripts.compareAndSet(true, false);
}
}
return true;
}

// loot untradeable items
private void lootUntradeableItems(AIOFighterConfig config) {
if (config.toggleLootUntradables()) {
LootingParameters untradeableItemsParams = new LootingParameters(
config.attackRadius(),
1,
1,
minFreeSlots,
config.toggleDelayedLooting(),
config.toggleOnlyLootMyItems(),
"untradeable"
);
untradeableItemsParams.setEatFoodForSpace(config.eatFoodForSpace());
if (Rs2GroundItem.lootUntradables(untradeableItemsParams)) {
Microbot.pauseAllScripts.compareAndSet(true, false);
/**
* Adds a custom "by names" intent sourced from the config's comma-separated list.
* (We use a custom predicate so we don't depend on params.getNames()).
*/
private void addCustomNames(Rs2LootEngine.Builder builder, String csvNames) {
if (csvNames == null) return;
final Set<String> needles = new HashSet<>();
Arrays.stream(csvNames.split(","))
.map(s -> s == null ? "" : s.trim().toLowerCase())
.filter(s -> !s.isEmpty())
.forEach(needles::add);

if (needles.isEmpty()) return;

Predicate<GroundItem> byNames = gi -> {
final String n = gi.getName() == null ? "" : gi.getName().trim().toLowerCase();
for (String needle : needles) {
if (n.contains(needle)) return true;
}
}
}
return false;
};

private void lootItemsByValue(AIOFighterConfig config) {
LootingParameters valueParams = new LootingParameters(
config.minPriceOfItemsToLoot(),
config.maxPriceOfItemsToLoot(),
config.attackRadius(),
1,
minFreeSlots,
config.toggleDelayedLooting(),
config.toggleOnlyLootMyItems()
);
valueParams.setEatFoodForSpace(config.eatFoodForSpace());
if (Rs2GroundItem.lootItemBasedOnValue(valueParams)) {
Microbot.pauseAllScripts.compareAndSet(true, false);
}
builder.addCustom("names", byNames, /*ignoredLower*/ null);
}

private void lootItemsOnName(AIOFighterConfig config) {
LootingParameters valueParams = new LootingParameters(
config.attackRadius(),
1,
1,
minFreeSlots,
config.toggleDelayedLooting(),
config.toggleOnlyLootMyItems(),
config.listOfItemsToLoot().trim().split(",")
);
valueParams.setEatFoodForSpace(config.eatFoodForSpace());
if (Rs2GroundItem.lootItemsBasedOnNames(valueParams)) {
Microbot.pauseAllScripts.compareAndSet(true, false);
}
}

@Override
public void shutdown() {
super.shutdown();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
public class Rs2GroundItem {
private static final int DESPAWN_DELAY_THRESHOLD_TICKS = 150;

private static boolean runWhilePaused(BooleanSupplier booleanSupplier) {
public static boolean runWhilePaused(BooleanSupplier booleanSupplier) {
final boolean paused = Microbot.pauseAllScripts.getAndSet(true);
final boolean success = booleanSupplier.getAsBoolean();
if (!paused && !Microbot.pauseAllScripts.compareAndSet(true, false)) {
Expand Down Expand Up @@ -262,7 +262,7 @@ public static boolean waitForGroundItemDespawn(Runnable actionWhileWaiting,Groun
return groundItem != getGroundItems().get(groundItem.getLocation(), groundItem.getId());
}

private static boolean coreLoot(GroundItem groundItem) {
public static boolean coreLoot(GroundItem groundItem) {
int quantity = Math.min(groundItem.isStackable() ? 1 : groundItem.getQuantity(),
Rs2Inventory.emptySlotCount());

Expand Down Expand Up @@ -552,7 +552,7 @@ public static Table<WorldPoint, Integer, GroundItem> getGroundItems() {
return GroundItemsPlugin.getCollectedGroundItems();
}

private static boolean canTakeGroundItem(GroundItem groundItem) {
public static boolean canTakeGroundItem(GroundItem groundItem) {
int maxQuantity = groundItem.isStackable() ? 1 : groundItem.getQuantity();
int availableSlots = Rs2Inventory.emptySlotCount();
int quantity = Math.min(maxQuantity, availableSlots);
Expand Down
Loading