Skip to content

Commit

Permalink
update v.0.2.0
Browse files Browse the repository at this point in the history
- Fixed some bugs & errs
- Added attack radius out of preferred zone when attack only in preferred zone is enabled
  • Loading branch information
rejchev committed Jan 4, 2025
1 parent 22d4c75 commit ef783f0
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 149 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>ru.rejchev</groupId>
<artifactId>rejmodule</artifactId>
<version>0.1.0</version>
<version>0.2.0</version>

<name>RejModule</name>
<description>RejModule - simple anti push DarkBot Module</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
@FieldDefaults(level = AccessLevel.PUBLIC)
public class AttackComponentConfig {

@Number(min = 0, max = 100_000, step = 10)
int preferred_radius = 1000;

@Option
AttackPriorityRule priorities = new AttackPriorityRule();

Expand Down
18 changes: 13 additions & 5 deletions src/main/java/ru/rejchev/rejmodule/module/ModuleCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Setter;
import lombok.experimental.FieldDefaults;
import lombok.experimental.NonFinal;
import lombok.extern.slf4j.Slf4j;
import ru.rejchev.rejmodule.configurations.CoreModuleConfig;
import ru.rejchev.rejmodule.module.base.IModuleAction;
import ru.rejchev.rejmodule.module.base.IModuleComponent;
Expand Down Expand Up @@ -71,7 +72,10 @@ public void onTickModule() {
if(!isCanWorking())
return;

final ModuleContext ctx = ModuleContext.of(getPluginAPI())
if(isInitialTick())
register();

ModuleContext ctx = ModuleContext.of(getPluginAPI())
.components(getComponents())
.properties(getProperties());

Expand Down Expand Up @@ -142,9 +146,7 @@ public <T extends IModuleComponent> ModuleCore components(Map<Class<T>, IModuleC
return this;
}

private boolean initModule(IModuleContext ctx) {
isInitialTick = false;

private void register() {
register("config", ModuleProperty.of(getConfig(), ModuleCore.BasePriority));

// base components
Expand All @@ -155,9 +157,15 @@ private boolean initModule(IModuleContext ctx) {

// extended components
register(AntiPushComponent.class, AntiPushComponent.instance());
}

private boolean initModule(IModuleContext ctx) {
isInitialTick = false;

for(IActionLoad elem : streamActions(IActionLoad.class).toList()) {
setStatus(elem.onLoad(ctx));

if((status = elem.onLoad(ctx)) != null)
status = elem.getClass().getSimpleName() + "::" + status;

if(getStatus() != null)
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,34 @@ private AntiPushComponent() {

@Override
public ModuleAction preBehaviourAction(IModuleContext ctx) {
return initAction(ctx);
ModuleAction action = ModuleAction.Continue, buf;

for(IAntiPushComponent component : getComponents()) {
if(component != null && (buf = component.preBehaviourAction(ctx)).ordinal() > action.ordinal()) {
if(buf.ordinal() >= ModuleAction.Handled.ordinal())
buf = ModuleAction.Change;

action = buf;
}
}

return action;
}

@Override
public ModuleAction behaviourAction(IModuleContext ctx) {
return initAction(ctx);
ModuleAction action = ModuleAction.Continue, buf;

for(IAntiPushComponent component : getComponents()) {
if(component != null && (buf = component.behaviourAction(ctx)).ordinal() > action.ordinal()) {
if(buf.ordinal() >= ModuleAction.Handled.ordinal())
buf = ModuleAction.Change;

action = buf;
}
}

return action;
}

@Override
Expand All @@ -65,26 +87,12 @@ public String onLoad(IModuleContext ctx) {
continue;

if((status = component.onLoad(ctx)) != null)
return status;
return component.getClass().getSimpleName() + ":: " + status;
}

return null;
}

private ModuleAction initAction(IModuleContext ctx) {
ModuleAction action = ModuleAction.Continue, buf;

for(IAntiPushComponent component : getComponents()) {
if(component != null && (buf = component.preBehaviourAction(ctx)).ordinal() > action.ordinal()) {
if(buf.ordinal() >= ModuleAction.Handled.ordinal())
buf = ModuleAction.Change;

action = buf;
}
}

return action;
}

public <T extends IAntiPushComponent> AntiPushComponent register(T value) {
getComponents().add(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import eu.darkbot.api.game.entities.Npc;
import eu.darkbot.api.game.other.GameMap;
import eu.darkbot.api.game.other.Locatable;
import eu.darkbot.api.game.other.Location;
import eu.darkbot.api.game.other.Lockable;
import eu.darkbot.api.managers.*;
import lombok.AccessLevel;
Expand Down Expand Up @@ -61,6 +62,11 @@ public static BaseAttackComponent instance() {
@Getter(AccessLevel.PRIVATE)
AttackComponentConfig config;

boolean backwards;

@Getter(AccessLevel.PRIVATE)
Locatable lastPreferredZoneLocation;

private BaseAttackComponent() {
super(Signature, BasePriority);
}
Expand All @@ -69,6 +75,9 @@ private BaseAttackComponent() {
public ModuleAction preBehaviourAction(IModuleContext ctx) {
super.preBehaviourAction(ctx);

if(getLastPreferredZoneLocation() == null || getMovementAPI().isInPreferredZone(getHeroAPI()))
lastPreferredZoneLocation = getHeroAPI().getLocationInfo().getCurrent();

updateNpcBlackList();

final NpcWrapper potentialTarget = getPotentialTarget(null);
Expand Down Expand Up @@ -100,6 +109,17 @@ public ModuleAction preBehaviourAction(IModuleContext ctx) {
return ModuleAction.Change;
}

@Override
public ModuleAction behaviourAction(IModuleContext context) {

Npc target;
if((target = getProperty(context, getSignature(), Npc.class)) == null /*|| !getAttackAPI().hasTarget()*/)
return ModuleAction.Continue;

// TODO: user priority
return context.property(BaseMovementComponent.class, getMovementLocation(target), getPriority());
}

@Override
public void postBehaviourAction(final IModuleContext context) {

Expand Down Expand Up @@ -188,8 +208,13 @@ private boolean isInvalidTarget(NpcWrapper target) {
return target == null || !target.isValid();
}

private boolean isInPreferred(Locatable target) {
return getMovementAPI().isInPreferredZone(target) || (!getMovementAPI().isInPreferredZone(target) && getHeroAPI().distanceTo(target) <= 2000.0);
private boolean isInPreferred(Npc target) {

if(getMovementAPI().isInPreferredZone(target))
return true;

return !getMovementAPI().isInPreferredZone(target) && getMovementAPI()
.getDistanceBetween(target, getLastPreferredZoneLocation()) <= getConfig().getPreferred_radius();
}

private void updateNpcBlackList() {
Expand Down Expand Up @@ -238,4 +263,91 @@ private int sumPriorities() {
+ getConfig().getPriorities().getHp()
+ getConfig().getPriorities().getPet();
}


private Location getMovementLocation(Npc target) {
Location targetLoc = target.getLocationInfo().destinationInTime(250);

double distance = getHeroAPI().distanceTo(target);
double angle = targetLoc.angleTo(getHeroAPI());

double radius = 560.0;

if(target.getInfo() != null)
radius = modifyRadius(target, target.getInfo().getRadius());

double speed = target.getSpeed();

double angleDiff;
{
double maxRadFix = radius / 2,
radiusFix = (int) Math.max(Math.min(radius - distance, maxRadFix), -maxRadFix);
distance = (radius += radiusFix);
// Moved distance + speed - distance to chosen radius same angle, divided by radius
angleDiff = Math.max((getHeroAPI().getSpeed() * 0.625) + (Math.max(200, speed) * 0.625)
- getHeroAPI().distanceTo(Location.of(targetLoc, angle, radius)), 0) / radius;
}

Location direction = getBestDir(targetLoc, angle, angleDiff, distance, target);
searchValidLocation(direction, targetLoc, angle, distance);

return direction;
}

private Location getBestDir(Locatable targetLoc, double angle, double angleDiff, double distance, Npc target) {
int maxCircleIterations = 4;
int iteration = 1;
double forwardScore = 0;
double backScore = 0;
do {
forwardScore += score(Locatable.of(targetLoc, angle + (angleDiff * iteration), distance), target);
backScore += score(Locatable.of(targetLoc, angle - (angleDiff * iteration), distance), target);
// Toggle direction if either one of the directions is perfect, or one is 300 better.
if (forwardScore < 0 != backScore < 0 || Math.abs(forwardScore - backScore) > 300) break;
} while (iteration++ < maxCircleIterations);

if (iteration <= maxCircleIterations) backwards = backScore > forwardScore;
return Location.of(targetLoc, angle + angleDiff * (backwards ? -1 : 1), distance);
}

private void searchValidLocation(Location direction, Location targetLoc, double angle, double distance) {
// Search in a spiral around the wanted position
// MAX_LOCATION_SEARCH = 10_000
while (!getMovementAPI().canMove(direction) && distance < 10_000) {
direction.toAngle(targetLoc, angle += backwards ? -0.3 : 0.3, distance += 2);
}

// Found no valid location within in a spiral, settle for whatever
// 10_000 = MAX_LOCATION_SEARCH
if (distance >= 10_000)
direction.toAngle(targetLoc, angle, 500);
}

private double score(Locatable loc, Npc target) {
return (getMovementAPI().canMove(loc) ? 0 : -1000) - getEntitiesAPI().getNpcs().stream() // Consider barrier as bad as 1000 radius units.
.filter(n -> target != n)
.mapToDouble(n -> Math.max(0, n.getInfo().getRadius() - n.distanceTo(loc)))
.sum();
}

private double modifyRadius(Npc target, double radius) {
if (target.getHealth().hpPercent() < 0.25 && target.getInfo().hasExtraFlag(NpcFlag.AGGRESSIVE_FOLLOW))
radius *= 0.75;

if (!getHeroAPI().isAttacking() && target.isMoving() && angleDiff(target.getLocationInfo().getAngle(), getHeroAPI().angleTo(target)) > 1.6)
radius = Math.min(560, radius);

if (getAttackAPI().isCastingAbility())
radius = Math.min(560, radius);

if (!target.isMoving() || target.getHealth().hpPercent() < 0.25)
radius = Math.min(600, radius);

return radius;
}

private static double angleDiff(double alpha, double beta) {
double phi = Math.abs(beta - alpha) % (Math.PI * 2);
return phi > Math.PI ? ((Math.PI * 2) - phi) : phi;
}
}
Loading

0 comments on commit ef783f0

Please sign in to comment.