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

refactor: Refactor the sentinel logic #201

Merged
merged 2 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
275 changes: 166 additions & 109 deletions src/main/java/com/deeme/modules/SentinelModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public class SentinelModule implements Module, Configurable<SentinelConfig>, Ins

private Location lastSentinelLocation = null;

private static final double MAX_DISTANCE_LIMIT = 10000;
private static final int MIN_SENTINEL_HEALTH = 1000;
private static final double SPEED_MULTIPLIER = 0.625;

private JLabel label = new JLabel("<html><b>Sentinel Module</b> <br>" +
"It's important that the main ship is in a group <br>" +
"Following priority: Master ID > Tag > Group Leader <br> " +
Expand Down Expand Up @@ -216,64 +220,76 @@ public JComponent instructionsComponent() {
public void onTickModule() {
pet.setEnabled(true);
if ((sConfig.ignoreSecurity || safety.tick()) && (!sConfig.collectorActive || collectorModule.canRefresh())) {
if (shipAround()) {
if (hasSentinel()) {
updateLastMap();
if (isAttacking()) {
currentStatus = State.HELPING_MASTER;
if (isNpc) {
npcMove();
if (sConfig.specialItems.npcEnabled) {
useSpecialItems();
}
} else {
shipAttacker.tryAttackOrFix();
extraMovementLogic.tick();
useSpecialItems();
}

if (sConfig.aggressiveFollow
&& heroapi.distanceTo(sentinel.getLocationInfo().getCurrent()) > sConfig.rangeToLider) {
moveToMaster();
}
attackLogic();
} else if (sentinel.isValid()) {
currentStatus = State.FOLLOWING_MASTER;
setMode(configRoam.getValue());
if (heroapi.distanceTo(sentinel.getLocationInfo().getCurrent()) > sConfig.rangeToLider) {
moveToMaster();
} else if (sConfig.collectorActive) {
collectorModule.findBox();
if (collectorModule.currentBox != null && sentinel.getLocationInfo()
.distanceTo(collectorModule.currentBox) < sConfig.rangeToLider) {
collectorModule.tryCollectNearestBox();
}
}
followSameMapLogic();
} else {
sentinel = null;
}
} else if (!followByPortals()) {
groupLeaderID = 0;
if (group.hasGroup()) {
goToGroup();
} else {
if (lastMap != heroapi.getMap().getId()) {
maximumWaitingTime = System.currentTimeMillis() + 60000;
}
acceptGroupSentinel();
if (lastMap != heroapi.getMap().getId() && currentStatus != State.WAIT_GROUP_LOADING
&& currentStatus != State.WAIT) {
currentStatus = State.WAIT_GROUP_LOADING;
maximumWaitingTime = System.currentTimeMillis() + 60000;
} else if (maximumWaitingTime <= System.currentTimeMillis()) {
currentStatus = State.WAIT;
GameMap map = getWorkingMap();
if (map != null && !portals.isEmpty() && map != starSystem.getCurrentMap()) {
currentStatus = State.TRAVELING_TO_WORKING_MAP;
this.bot.setModule(new MapModule(api, true))
.setTarget(map);
} else if (sConfig.collectorActive) {
collectorModule.onTickModule();
}
}
noSentinelLogic();
}
}
}

private void attackLogic() {
if (isNpc) {
npcMove();
if (sConfig.specialItems.npcEnabled) {
useSpecialItems();
}
} else {
shipAttacker.tryAttackOrFix();
extraMovementLogic.tick();
useSpecialItems();
}

if (sConfig.aggressiveFollow
&& heroapi.distanceTo(sentinel.getLocationInfo().getCurrent()) > sConfig.rangeToLider) {
moveToMaster();
}
}

private void followSameMapLogic() {
setMode(configRoam.getValue());
if (heroapi.distanceTo(sentinel.getLocationInfo().getCurrent()) > sConfig.rangeToLider) {
moveToMaster();
} else if (sConfig.collectorActive) {
collectorModule.findBox();
if (collectorModule.currentBox != null && sentinel.getLocationInfo()
.distanceTo(collectorModule.currentBox) < sConfig.rangeToLider) {
collectorModule.tryCollectNearestBox();
}
}
}

private void noSentinelLogic() {
groupLeaderID = 0;
if (group.hasGroup()) {
goToGroup();
} else {
if (lastMap != heroapi.getMap().getId()) {
maximumWaitingTime = System.currentTimeMillis() + 60000;
}
acceptGroupSentinel();
if (lastMap != heroapi.getMap().getId() && currentStatus != State.WAIT_GROUP_LOADING
&& currentStatus != State.WAIT) {
currentStatus = State.WAIT_GROUP_LOADING;
maximumWaitingTime = System.currentTimeMillis() + 60000;
} else if (maximumWaitingTime <= System.currentTimeMillis()) {
currentStatus = State.WAIT;
GameMap map = getWorkingMap();
if (map != null && !portals.isEmpty() && map != starSystem.getCurrentMap()) {
currentStatus = State.TRAVELING_TO_WORKING_MAP;
this.bot.setModule(new MapModule(api, true))
.setTarget(map);
} else if (sConfig.collectorActive) {
collectorModule.onTickModule();
}
}
}
Expand Down Expand Up @@ -334,19 +350,17 @@ private boolean isAttacking() {

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

Entity target = getSentinelTarget();

if (target == null) {
if (shipAttacker.getTarget() != null) {
target = shipAttacker.getTarget();
} else if (attacker.getTarget() != null) {
target = attacker.getTarget();
}
target = getTargetFromAttackerModules();
}

if (target != null && (!target.isValid() || target.getId() == heroapi.getId())) {
if (target != null && (!target.isValid() || target.getId() == heroapi.getId() || isOurPet(target))) {
target = null;
}

Expand All @@ -357,24 +371,48 @@ private boolean isAttacking() {
target = SharedFunctions.getAttacker(heroapi, npcs, heroapi);
}

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

this.oldTarget = target;

return target != null;
}

private void changeTarget(Entity target) {
if (target == null) {
return;
}

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

private boolean isOurPet(Entity target) {
if (target == null) {
return false;
}

return heroapi.getPet().isPresent() && heroapi.getPet().get().getId() == target.getId();
}

private Entity getTargetFromAttackerModules() {
if (shipAttacker.getTarget() != null) {
return shipAttacker.getTarget();
} else if (attacker.getTarget() != null) {
return attacker.getTarget();
}

return null;
}

private Entity getSentinelTarget() {
Entity target = null;
if (sentinel.getTarget() != null) {
Expand All @@ -400,20 +438,23 @@ private Entity getSentinelTarget() {
return target;
}

private boolean shipAround() {
private boolean isSentinelValid() {
return sentinel != null && sentinel.isValid()
&& sentinel.getHealth() != null && sentinel.getHealth().getHp() > MIN_SENTINEL_HEALTH;
}

private boolean hasSentinel() {
dm94 marked this conversation as resolved.
Show resolved Hide resolved
if (players == null || players.isEmpty()) {
return false;
}

sentinel = players.stream()
.filter(ship -> ship.isValid() && ship.getId() != heroapi.getId())
.filter(ship -> (ship.getId() == masterID ||
(ship.getId() == sConfig.MASTER_ID) ||
(sConfig.SENTINEL_TAG != null && main.config.PLAYER_INFOS != null &&
sConfig.SENTINEL_TAG.hasTag(main.config.PLAYER_INFOS.get(ship.getId())))
||
(sConfig.followGroupLeader && ship.getId() == groupLeaderID)))
.findAny().orElse(null);
if (isSentinelValid()) {
sentinel = players.stream().filter(ship -> ship.getId() == sentinel.getId()).findFirst().orElse(null);
}

if (sentinel == null) {
sentinel = players.stream().filter(this::isPotentialSentinel).findFirst().orElse(null);
}

if (sentinel != null) {
lastSentinelLocation = sentinel.getLocationInfo().getCurrent();
Expand All @@ -422,35 +463,51 @@ private boolean shipAround() {
return sentinel != null;
}

private boolean isPotentialSentinel(Player ship) {
return ship != null && ship.isValid() && ship.getId() != heroapi.getId() &&
(ship.getId() == masterID || ship.getId() == sConfig.MASTER_ID ||
isTaggedSentinel(ship) || (sConfig.followGroupLeader && ship.getId() == groupLeaderID));
}

private boolean isTaggedSentinel(Player ship) {
return ship != null && sConfig.SENTINEL_TAG != null &&
main.config.PLAYER_INFOS != null &&
sConfig.SENTINEL_TAG.hasTag(main.config.PLAYER_INFOS.get(ship.getId()));
}

private void goToGroup() {
for (eu.darkbot.api.game.group.GroupMember m : group.getMembers()) {
if (!m.isDead() && m.getId() != heroapi.getId()
&& ((sConfig.MASTER_ID != 0 && m.getId() == sConfig.MASTER_ID) ||
(sConfig.SENTINEL_TAG != null
&& sConfig.SENTINEL_TAG.has(main.config.PLAYER_INFOS.get(m.getId())))
||
(sConfig.followGroupLeader && m.isLeader()))) {
if (m.isLeader()) {
groupLeaderID = m.getId();
}
masterID = m.getId();
group.getMembers().stream()
.filter(this::isGroupMemberValid)
.forEach(m -> {
if (m.isLeader()) {
groupLeaderID = m.getId();
}
masterID = m.getId();

if (heroapi.getMap() != null && m.getMapId() == heroapi.getMap().getId()) {
lastSentinelLocation = m.getLocation();
}
if (m.getMapId() == heroapi.getMap().getId()) {
movement.moveTo(m.getLocation());
currentStatus = State.FOLLOWING_MASTER;
} else {
GameMap map = starSystem.findMap(m.getMapId()).orElse(null);
if (map != null) {
this.bot.setModule(api.requireInstance(MapModule.class))
.setTarget(map);
currentStatus = State.TRAVELLING_TO_MASTER;
if (heroapi.getMap() != null && m.getMapId() == heroapi.getMap().getId()) {
lastSentinelLocation = m.getLocation();
}
}
}
}
if (m.getMapId() == heroapi.getMap().getId()) {
movement.moveTo(m.getLocation());
currentStatus = State.FOLLOWING_MASTER;
} else {
GameMap map = starSystem.findMap(m.getMapId()).orElse(null);
if (map != null) {
this.bot.setModule(api.requireInstance(MapModule.class))
.setTarget(map);
currentStatus = State.TRAVELLING_TO_MASTER;
}
}
});
}

private boolean isGroupMemberValid(eu.darkbot.api.game.group.GroupMember m) {
return !m.isDead() && m.getId() != heroapi.getId()
&& ((sConfig.MASTER_ID != 0 && m.getId() == sConfig.MASTER_ID) ||
(sConfig.SENTINEL_TAG != null
&& sConfig.SENTINEL_TAG.has(main.config.PLAYER_INFOS.get(m.getId())))
||
(sConfig.followGroupLeader && m.isLeader()));
}

private void acceptGroupSentinel() {
Expand Down Expand Up @@ -483,7 +540,7 @@ private void setMode(ShipMode config) {

private double getRadius(Lockable target) {
if (!(target instanceof Npc)) {
return 550;
return 550.0 + this.rnd.nextInt(60);
}
return attacker.modifyRadius(((Npc) target).getInfo().getRadius());
}
Expand Down Expand Up @@ -521,14 +578,14 @@ private void npcMove() {
double maxRadFix = radius / 2;
double radiusFix = (int) Math.max(Math.min(radius - distance, maxRadFix), -maxRadFix);
distance = (radius += radiusFix);
angleDiff = Math.max((heroapi.getSpeed() * 0.625) + (Math.max(200, speed) * 0.625)
angleDiff = Math.max((heroapi.getSpeed() * SPEED_MULTIPLIER) + (Math.max(200, speed) * SPEED_MULTIPLIER)
- heroapi.distanceTo(Location.of(targetLoc, angle, radius)), 0) / radius;
}
direction = getBestDir(targetLoc, angle, angleDiff, distance);

while (!movement.canMove(direction) && distance < 10000)
while (!movement.canMove(direction) && distance < MAX_DISTANCE_LIMIT)
direction.toAngle(targetLoc, angle += backwards ? -0.3 : 0.3, distance += 2);
dm94 marked this conversation as resolved.
Show resolved Hide resolved
if (distance >= 10000)
if (distance >= MAX_DISTANCE_LIMIT)
direction.toAngle(targetLoc, angle, 500);

setNPCConfig(direction);
Expand Down Expand Up @@ -571,8 +628,8 @@ private void setNPCConfig(Location direction) {
Npc target = attacker.getTargetAs(Npc.class);
if (target == null || !target.isValid()) {
setMode(configRoam.getValue());
} else if (runConfigInCircle.getValue()
&& target.getHealth().hpPercent() < 0.25
} else if (Boolean.TRUE.equals(runConfigInCircle.getValue()
&& target.getHealth().hpPercent() < 0.25)
&& heroapi.getLocationInfo().getCurrent().distanceTo(direction) > target.getInfo().getRadius() * 2) {
setMode(configRun.getValue());
} else if (heroapi.getLocationInfo().getCurrent().distanceTo(direction) > target.getInfo().getRadius() * 3) {
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.5",
"version": "2.3.6 beta 1",
"minVersion": "1.131",
"supportedVersion": "1.131.2",
"basePackage": "com.deeme",
Expand Down
Loading