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

feat: Added the Extra movement conditions #211

Merged
merged 2 commits into from
Mar 1, 2025
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
Expand Up @@ -8,7 +8,7 @@
import com.deeme.types.config.ExtraKeyConditions;
import com.deeme.types.config.ExtraKeyConditionsSelectable;
import com.deemeplus.general.configchanger.ExtraConfigChangerConfig;
import com.deemeplus.general.movement.MovementConfig;
import com.deeme.shared.movement.MovementConfig;

import eu.darkbot.api.config.annotations.Configuration;
import eu.darkbot.api.config.annotations.Dropdown;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.deeme.behaviours.defense;

import com.deeme.types.ShipAttacker;
import com.deemeplus.general.movement.ExtraMovementLogic;
import com.deeme.shared.movement.ExtraMovementLogic;
import com.deemeplus.general.configchanger.ExtraCChangerLogic;

import eu.darkbot.api.PluginAPI;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/deeme/modules/PVPModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.deeme.types.VerifierChecker;
import com.deeme.types.backpage.Utils;
import com.deemeplus.general.configchanger.ExtraCChangerLogic;
import com.deemeplus.general.movement.ExtraMovementLogic;
import com.deeme.shared.movement.ExtraMovementLogic;

import eu.darkbot.api.PluginAPI;
import eu.darkbot.api.config.ConfigSetting;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/deeme/modules/SentinelModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.deeme.types.backpage.Utils;
import com.deeme.types.config.SentinelConfig;
import com.deemeplus.general.configchanger.ExtraCChangerLogic;
import com.deemeplus.general.movement.ExtraMovementLogic;
import com.deeme.shared.movement.ExtraMovementLogic;
import com.github.manolo8.darkbot.Main;

import eu.darkbot.api.PluginAPI;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/deeme/modules/pvp/PVPConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.deeme.modules.sentinel.Humanizer;
import com.deeme.types.config.ExtraKeyConditions;
import com.deemeplus.general.configchanger.ExtraConfigChangerConfig;
import com.deemeplus.general.movement.MovementConfig;
import com.deeme.shared.movement.MovementConfig;

import eu.darkbot.api.config.annotations.Configuration;
import eu.darkbot.api.config.annotations.Number;
Expand Down
182 changes: 182 additions & 0 deletions src/main/java/com/deeme/shared/movement/ExtraMovementLogic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package com.deeme.shared.movement;

import java.util.Comparator;
import java.util.Optional;
import java.util.Random;

import eu.darkbot.api.PluginAPI;
import eu.darkbot.api.config.types.Condition;
import eu.darkbot.api.game.entities.Entity;
import eu.darkbot.api.game.entities.Ship;
import eu.darkbot.api.game.group.GroupMember;
import eu.darkbot.api.game.other.Location;
import eu.darkbot.api.game.other.Lockable;
import eu.darkbot.api.managers.GroupAPI;
import eu.darkbot.api.managers.HeroAPI;
import eu.darkbot.api.managers.MovementAPI;
import eu.darkbot.api.utils.Inject;
import eu.darkbot.shared.utils.SafetyFinder;

public class ExtraMovementLogic {
private final PluginAPI api;
private final HeroAPI hero;
private final MovementAPI movement;
private final SafetyFinder safetyFinder;
private final GroupAPI group;

private static final int DISTANCE_TO_USE_VS_MODE = 1500;

private MovementConfig config;
private Random rnd;

public ExtraMovementLogic(PluginAPI api, MovementConfig config) {
this(api, api.requireAPI(HeroAPI.class), api.requireAPI(MovementAPI.class), config);
}

@Inject
public ExtraMovementLogic(PluginAPI api, HeroAPI hero, MovementAPI movement, MovementConfig config) {
this.api = api;
this.hero = hero;
this.movement = movement;
this.group = api.getAPI(GroupAPI.class);
this.safetyFinder = api.requireInstance(SafetyFinder.class);
this.config = config;
this.rnd = new Random();
}

public void tick() {
move();
}

private void move() {
MovementModeEnum movementSelected = getMovementMode();

switch (movementSelected) {
case VS:
vsMove();
break;
case RANDOM:
handleRandom();
break;
case GROUPVSSAFETY:
handleGroupVSSafety();
break;
case GROUPVS:
handleGroupVS();
break;
case VSSAFETY:
default:
handleVSSafety();
break;
}
}

private void handleVSSafety() {
if (!safetyFinder.tick()) {
vsMove();
}
}

private void handleRandom() {
if (!movement.isMoving() || movement.isOutOfMap()) {
movement.moveRandom();
}
}

private void handleGroupVSSafety() {
if (safetyFinder.tick()) {
handleGroupMovement();
}
}

private void handleGroupVS() {
handleGroupMovement();
}

private void handleGroupMovement() {
GroupMember groupMember = getClosestMember();
if (groupMember != null) {
if (groupMember.getLocation().distanceTo(hero) < DISTANCE_TO_USE_VS_MODE) {
vsMove();
} else {
movement.moveTo(groupMember.getLocation());
}
} else {
vsMove();
}
}

private MovementModeEnum getMovementMode() {
if (checkCondition(config.movementCondtion1.condition)) {
return config.movementCondtion1.movementMode;
}
if (checkCondition(config.movementCondtion2.condition)) {
return config.movementCondtion2.movementMode;
}
if (checkCondition(config.movementCondtion3.condition)) {
return config.movementCondtion3.movementMode;
}
if (checkCondition(config.movementCondtion4.condition)) {
return config.movementCondtion4.movementMode;
}
if (checkCondition(config.movementCondtion5.condition)) {
return config.movementCondtion5.movementMode;
}

return config.defaultMovementMode;
}

private boolean checkCondition(Condition condition) {
if (condition == null) {
return false;
}

return condition.get(api).allows();
}

private Optional<Location> getDestination() {
Ship target = hero.getTargetAs(Ship.class);
if (target != null && target.isValid()) {
Comment on lines +137 to +139
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Potential bug in getDestination condition check.

In the else branch, the condition is once again checking 'target' rather than the newly assigned 'other'. This appears to be a copy-paste mistake and may lead to incorrect behavior when hero.getTargetAs(Ship.class) is null but hero.getTarget() returns a valid entity.

return Optional.of(target.getDestination().orElse(target.getLocationInfo().destinationInTime(500)));
} else {
Entity other = hero.getTarget();
if (other != null && other.isValid()) {
return Optional.of(other.getLocationInfo());
}
}

Lockable t = hero.getLocalTarget();
if (t != null && t.isValid()) {
return Optional.of(t.getLocationInfo());
}

return Optional.empty();
}

private void vsMove() {
Location targetLoc = getDestination().orElse(null);

if (targetLoc == null) {
return;
}

double distance = hero.getLocationInfo().distanceTo(targetLoc);
if (distance > 600) {
if (movement.canMove(targetLoc)) {
movement.moveTo(targetLoc);
}
} else if (!movement.isMoving()) {
movement.moveTo(Location.of(targetLoc, rnd.nextInt(360), distance));
}
}

private GroupMember getClosestMember() {
if (group.hasGroup()) {
return group.getMembers().stream()
.filter(member -> !member.isDead() && member.getMapId() == hero.getMap().getId())
.min(Comparator.<GroupMember>comparingDouble(m -> m.getLocation().distanceTo(hero)))
.orElse(null);
}
return null;
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/deeme/shared/movement/MovementConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.deeme.shared.movement;

import eu.darkbot.api.config.annotations.Configuration;
import eu.darkbot.api.config.annotations.Dropdown;
import eu.darkbot.api.config.annotations.Option;

@Configuration("movement_config")
public class MovementConfig {
@Option("movement_config.default")
@Dropdown
public MovementModeEnum defaultMovementMode = MovementModeEnum.VS;

@Option("movement_config.condition")
public MovementWithConditions movementCondtion1 = new MovementWithConditions();
Comment on lines +13 to +14
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick (typo): Typo in property naming: movementCondtion1.

The field name 'movementCondtion1' appears to contain a typo. Standardizing the naming to 'movementCondition1' (and similarly for condition 2-5) would improve readability and reduce potential confusion in the future.

Suggested implementation:

    public MovementWithConditions movementCondition1 = new MovementWithConditions();
    public MovementWithConditions movementCondition2 = new MovementWithConditions();
    public MovementWithConditions movementCondition3 = new MovementWithConditions();
    public MovementWithConditions movementCondition4 = new MovementWithConditions();

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (review_instructions): Typo in variable name 'movementCondtion1' affecting code clarity.

Renaming 'movementCondtion1' to 'movementCondition1' (and similarly for the subsequent fields) would improve readability and maintainability.

Review instructions:

Path patterns: *.java

Instructions:
It is a plugin for the darkbot bot in java, check that the code is clean and efficient.


@Option("movement_config.condition")
public MovementWithConditions movementCondtion2 = new MovementWithConditions();

@Option("movement_config.condition")
public MovementWithConditions movementCondtion3 = new MovementWithConditions();

@Option("movement_config.condition")
public MovementWithConditions movementCondtion4 = new MovementWithConditions();

@Option("movement_config.condition")
public MovementWithConditions movementCondtion5 = new MovementWithConditions();
}
8 changes: 8 additions & 0 deletions src/main/java/com/deeme/shared/movement/MovementModeEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.deeme.shared.movement;

import eu.darkbot.api.config.annotations.Configuration;

@Configuration("defense.movement_mode.list")
public enum MovementModeEnum {
VS, VSSAFETY, RANDOM, GROUPVSSAFETY, GROUPVS;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.deeme.shared.movement;

import eu.darkbot.api.config.annotations.Configuration;
import eu.darkbot.api.config.annotations.Dropdown;
import eu.darkbot.api.config.annotations.Option;
import eu.darkbot.api.config.types.Condition;

@Configuration("movement_conditions")
public class MovementWithConditions {
@Option("defense.movement_mode")
@Dropdown
public MovementModeEnum movementMode = MovementModeEnum.VSSAFETY;

@Option("general.condition")
public Condition condition;
}
2 changes: 1 addition & 1 deletion src/main/java/com/deeme/types/config/SentinelConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.deeme.behaviours.defense.AmmoConfig;
import com.deeme.modules.sentinel.Humanizer;
import com.deemeplus.general.configchanger.ExtraConfigChangerConfig;
import com.deemeplus.general.movement.MovementConfig;
import com.deeme.shared.movement.MovementConfig;
import com.github.manolo8.darkbot.config.PlayerTag;
import com.github.manolo8.darkbot.config.types.Tag;
import com.github.manolo8.darkbot.config.types.TagDefault;
Expand Down
4 changes: 2 additions & 2 deletions 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.8",
"version": "2.3.9",
"minVersion": "1.131",
"supportedVersion": "1.131.2",
"basePackage": "com.deeme",
Expand Down Expand Up @@ -35,6 +35,6 @@
],
"donation": "https://ko-fi.com/deeme",
"discord": "https://discord.gg/GPRTRRZJPw",
"download": "https://github.com/darkbot-reloaded/DmPlugin/releases/download/v2.3.8/DmPlugin.jar",
"download": "https://github.com/darkbot-reloaded/DmPlugin/releases/download/v2.3.9/DmPlugin.jar",
"update": "https://raw.githubusercontent.com/darkbot-reloaded/DmPlugin/master/src/main/resources/plugin.json"
}
Loading