Skip to content

Commit

Permalink
feat: Sentinel module: Added that humanizer also takes effect when se…
Browse files Browse the repository at this point in the history
…arching for the target (#192)
  • Loading branch information
dm94 authored Oct 4, 2024
1 parent 8507527 commit 1e01653
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
31 changes: 24 additions & 7 deletions src/main/java/com/deeme/modules/SentinelModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.Random;

import javax.swing.JComponent;
import javax.swing.JLabel;
Expand Down Expand Up @@ -89,6 +90,8 @@ public class SentinelModule implements Module, Configurable<SentinelConfig>, Ins
private int groupLeaderID = 0;

private long randomWaitTime = 0;
private Random rnd;
private Entity oldTarget;

private Location lastSentinelLocation = null;

Expand Down Expand Up @@ -162,6 +165,8 @@ public SentinelModule(Main main, PluginAPI api, AuthAPI auth, SafetyFinder safet
this.runConfigInCircle = configApi.requireConfig("loot.run_config_in_circle");
this.configRun = configApi.requireConfig("general.run");
this.configRoam = configApi.requireConfig("general.roam");
this.rnd = new Random();
this.oldTarget = null;

setup();
}
Expand Down Expand Up @@ -288,6 +293,7 @@ private boolean followByPortals() {
if (!sConfig.followByPortals || lastSentinelLocation == null) {
return false;
}

Portal portal = getNearestPortal(lastSentinelLocation);
if (portal != null) {
if (group.hasGroup() && masterID != 0) {
Expand Down Expand Up @@ -322,6 +328,14 @@ private GameMap getWorkingMap() {
}

private boolean isAttacking() {
if (this.randomWaitTime > System.currentTimeMillis()) {
return this.oldTarget != null;
}

if (sConfig.humanizer.addRandomTime) {
this.randomWaitTime = System.currentTimeMillis() + (rnd.nextInt(sConfig.humanizer.maxRandomTime) * 1000);
}

Entity target = getSentinelTarget();

if (target == null) {
Expand All @@ -337,34 +351,37 @@ private boolean isAttacking() {
}

if (target == null && sConfig.autoAttack.autoAttackEnemies) {
target = shipAttacker.getEnemy(sConfig.autoAttack.rangeForEnemies);
target = this.shipAttacker.getEnemy(sConfig.autoAttack.rangeForEnemies);
}
if (target == null && sConfig.autoAttack.defendFromNPCs) {
target = SharedFunctions.getAttacker(heroapi, npcs, heroapi);
}

if (target != null) {
setMode(extraConfigChangerLogic.getShipMode());
if (target instanceof Npc) {
isNpc = true;
this.isNpc = true;
attacker.setTarget((Npc) target);
attacker.tryLockAndAttack();
} else {
isNpc = false;
this.isNpc = false;
shipAttacker.setTarget((Ship) target);
shipAttacker.tryLockAndAttack();
}
}

this.oldTarget = target;

return target != null;
}

private Entity getSentinelTarget() {
if (randomWaitTime > System.currentTimeMillis()) {
return null;
}

Entity target = null;
if (sentinel.getTarget() != null) {
if (this.oldTarget != null && this.oldTarget.getId() == sentinel.getTarget().getId()) {
return this.oldTarget;
}

if (sConfig.autoAttack.helpAttackPlayers || sConfig.autoAttack.helpAttackEnemyPlayers) {
target = players.stream()
.filter(s -> (sentinel.getTarget().getId() == s.getId())
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/deeme/types/ShipAttacker.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class ShipAttacker {
private Random rnd;

private int lastAttacked;
private static final int MIN_CLICK_DELAY = 250;

private boolean firstAttack;
private long isAttacking;
Expand Down Expand Up @@ -110,9 +111,10 @@ private void tryLockTarget() {
lastAttacked = target.getId();
firstAttack = true;

clickDelay = System.currentTimeMillis();
if (humanizerConfig.addRandomTime) {
clickDelay = System.currentTimeMillis() + (rnd.nextInt(humanizerConfig.maxRandomTime) * 1000);
} else {
clickDelay = System.currentTimeMillis() + MIN_CLICK_DELAY;
}
}

Expand All @@ -124,7 +126,7 @@ private void tryLockTarget() {
if (System.currentTimeMillis() > clickDelay) {
heroapi.setLocalTarget(target);
target.trySelect(false);
clickDelay = System.currentTimeMillis();
clickDelay = System.currentTimeMillis() + MIN_CLICK_DELAY;
}
} else {
movement.moveTo(target.getDestination().orElse(target.getLocationInfo()));
Expand Down Expand Up @@ -285,11 +287,10 @@ public Ship getEnemy(int maxDistance, List<Integer> playersToIgnore, boolean ign
&& (ableToAttack || s.isAttacking())
&& !s.hasEffect(290)
&& !(s instanceof Pet)
&& !(ignoreInvisible && s.isInvisible())
&& !inGroup(s.getId())
&& !(ignoreInvisible && s.isInvisible()))
.filter(s -> !inGroup(s.getId())
&& movement.canMove(s)
&& s.getLocationInfo().distanceTo(heroapi) <= maxDistance)

.sorted(Comparator.comparingDouble(s -> s.getLocationInfo().distanceTo(heroapi))).findAny()
.orElse(null);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "DmPlugin",
"author": "Dm94Dani",
"version": "2.3.3 beta 2",
"version": "2.3.3 beta 4",
"minVersion": "1.131",
"supportedVersion": "1.131.2",
"basePackage": "com.deeme",
Expand Down

0 comments on commit 1e01653

Please sign in to comment.