Skip to content

Commit

Permalink
Update to api v0.9.7, implement fake NPC api
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablete1234 committed Oct 19, 2024
1 parent 377b65b commit b7de0bf
Show file tree
Hide file tree
Showing 14 changed files with 288 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ configurations {
}

dependencies {
val apiVersion = "0.9.5"
val apiVersion = "0.9.7"
val flatLafVersion = "3.4"

// use this if you want to use local(mavenLocal) darkbot API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import eu.darkbot.api.extensions.Task;
import eu.darkbot.api.game.other.GameMap;
import eu.darkbot.api.managers.GameResourcesAPI;
import eu.darkbot.impl.utils.TranslationMatcherImpl;
import eu.darkbot.util.XmlUtils;
import eu.darkbot.util.http.Http;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -85,6 +86,12 @@ public Optional<String> findTranslation(@NotNull String key) {
return Optional.ofNullable(getTranslation(key));
}

@Override
public Optional<TranslationMatcher> getTranslationMatcher(@NotNull String key, String... replacements) {
return Optional.ofNullable(getLanguage())
.flatMap(lang -> findTranslation(key).map(t -> (TranslationMatcher) new TranslationMatcherImpl(lang, t, replacements)));
}

public CompletableFuture<Image> getBackgroundImage(GameMap gameMap) {
Path minimapPath = MINIMAPS_PATH.resolve(gameMap.getId() + "-700.jpg");
CompletableFuture<Image> future = new CompletableFuture<>();
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/github/manolo8/darkbot/config/BoxInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class BoxInfo implements eu.darkbot.api.config.types.BoxInfo {
@Option(key = "config.collect.box_table.priority")
public int priority;

public transient String name;

@Override
public boolean shouldCollect() {
return collect;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public BoxInfo getOrCreateBoxInfo(String name) {
BoxInfo info = boxes.get(name);
if (info == null) {
info = new BoxInfo();
info.name = name;

if (!name.isEmpty()) {
boxes.put(name, info);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public <T> ConfigSetting.Parent<T> getConfigRoot() {
return (ConfigSetting.Parent<T>) configuration;
}

@Override
public String getCurrentProfile() {
return loader.getConfigName();
}

@Override
public List<String> getConfigProfiles() {
return loader.getAvailableConfigs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ private Config loadConfig(Path configFile, Path backupFile) {
}
// set name of npc on each config load
config.LOOT.NPC_INFOS.forEach((k, v) -> v.name = k);
config.COLLECT.BOX_INFOS.forEach((k, v) -> v.name = k);

return config;
}
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/com/github/manolo8/darkbot/core/entities/Box.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
import com.github.manolo8.darkbot.config.BoxInfo;
import com.github.manolo8.darkbot.config.ConfigEntity;
import com.github.manolo8.darkbot.core.api.Capability;
import com.github.manolo8.darkbot.core.entities.fake.FakeEntities;
import com.github.manolo8.darkbot.core.entities.fake.FakeExtension;
import com.github.manolo8.darkbot.utils.Offsets;
import eu.darkbot.api.game.entities.FakeEntity;
import eu.darkbot.util.Timer;
import lombok.Getter;
import org.jetbrains.annotations.Nullable;

import java.time.Instant;
Expand All @@ -25,6 +29,9 @@ public class Box extends Entity implements eu.darkbot.api.game.entities.Box {

public BoxInfo boxInfo;

private Box(int id) {
super(id);
}

public Box(int id, long address) {
super(id);
Expand Down Expand Up @@ -135,4 +142,42 @@ public Beacon(int id, long address) {
super(id, address);
}
}

@Getter
public static class Fake extends Box implements FakeEntity.FakeBox, FakeExtension {
private final FakeExtension.Data fakeData = new FakeExtension.Data(this);

public Fake(BoxInfo box) {
super(FakeEntities.allocateFakeId());
super.type = box.name;
super.boxInfo = box;
}

@Override
public boolean isInvalid(long mapAddress) {
return fakeData.isInvalid();
}

@Override
public boolean tryCollect() {
return fakeData.trySelect(false);
}

@Override
public boolean trySelect(boolean tryAttack) {
return fakeData.trySelect(tryAttack);
}

public String getHash() {
return type + locationInfo.getCurrent();
}

@Override
public void update() {
}

@Override
public void update(long address) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import com.github.manolo8.darkbot.Main;
import com.github.manolo8.darkbot.config.NpcInfo;
import com.github.manolo8.darkbot.core.entities.fake.FakeEntities;
import com.github.manolo8.darkbot.core.utils.Location;

/**
* Represents a Pet ping FakeNpc, this is a singleton and any other fake npc should use {@link Npc.Fake} instead
*/
public class FakeNpc extends Npc {

private long pingAlive = 0;

public FakeNpc(Main main) {
super(Integer.MIN_VALUE);
super(FakeEntities.allocateFakeId());
super.removed = true;
super.address = -1;
super.main = main;
Expand Down
40 changes: 39 additions & 1 deletion src/main/java/com/github/manolo8/darkbot/core/entities/Mine.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.github.manolo8.darkbot.core.entities;

import com.github.manolo8.darkbot.core.entities.fake.FakeEntities;
import com.github.manolo8.darkbot.core.entities.fake.FakeExtension;
import com.github.manolo8.darkbot.core.itf.Obstacle;
import com.github.manolo8.darkbot.core.utils.pathfinder.AreaImpl;
import com.github.manolo8.darkbot.core.utils.pathfinder.CircleImpl;
import eu.darkbot.api.game.entities.FakeEntity;
import lombok.Getter;

import static com.github.manolo8.darkbot.Main.API;

Expand All @@ -12,7 +16,11 @@ public class Mine extends Entity implements Obstacle, eu.darkbot.api.game.entiti

public int typeId;

private final CircleImpl area = new CircleImpl(0, 0, 200);
protected final CircleImpl area = new CircleImpl(0, 0, 200);

private Mine(int id) {
super(id);
}

public Mine(int id, long address) {
super(id);
Expand Down Expand Up @@ -58,4 +66,34 @@ public String toString() {
public int getTypeId() {
return typeId;
}

@Getter
public static class Fake extends Mine implements FakeEntity.FakeMine, FakeExtension {
private final FakeExtension.Data fakeData = new FakeExtension.Data(this);

public Fake(int typeId) {
super(FakeEntities.allocateFakeId());
super.typeId = typeId;
}

@Override
public boolean isInvalid(long mapAddress) {
return fakeData.isInvalid();
}

@Override
public boolean trySelect(boolean tryAttack) {
return fakeData.trySelect(tryAttack);
}

@Override
public void update() {
if (locationInfo.isMoving())
area.set(locationInfo.now, typeId == FROZEN_LAB_MINE ? 500 : 200);
}

@Override
public void update(long address) {
}
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/github/manolo8/darkbot/core/entities/Npc.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import com.github.manolo8.darkbot.config.ConfigEntity;
import com.github.manolo8.darkbot.config.NpcInfo;
import com.github.manolo8.darkbot.core.entities.fake.FakeEntities;
import com.github.manolo8.darkbot.core.entities.fake.FakeExtension;
import com.github.manolo8.darkbot.core.manager.EffectManager;
import eu.darkbot.api.game.entities.FakeEntity;
import lombok.Getter;

import java.util.Objects;

Expand Down Expand Up @@ -53,4 +57,34 @@ public eu.darkbot.api.config.types.NpcInfo getInfo() {
public int getShipId() {
return getNpcId();
}

@Getter
public static class Fake extends Npc implements FakeEntity.FakeNpc, FakeExtension {
private final FakeExtension.Data fakeData = new FakeExtension.Data(this);
public Fake(NpcInfo npcInfo) {
super(FakeEntities.allocateFakeId());
this.npcInfo = npcInfo;
this.npcId = npcInfo.npcId;
this.playerInfo.username = npcInfo.name;
}

@Override
public boolean isInvalid(long mapAddress) {
return fakeData.isInvalid();
}

@Override
public boolean trySelect(boolean tryAttack) {
return fakeData.trySelect(tryAttack);
}

@Override
public void update() {
}

@Override
public void update(long address) {
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.manolo8.darkbot.core.entities.fake;

public class FakeEntities {
private static int CURR_ID = Integer.MIN_VALUE;

public static int allocateFakeId() {
return CURR_ID++;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.github.manolo8.darkbot.core.entities.fake;

import com.github.manolo8.darkbot.Main;
import com.github.manolo8.darkbot.core.entities.Entity;
import com.github.manolo8.darkbot.core.objects.LocationInfo;
import eu.darkbot.api.game.entities.FakeEntity;
import eu.darkbot.api.game.other.Location;
import eu.darkbot.util.Timer;

public interface FakeExtension extends FakeEntity {

FakeExtension.Data getFakeData();

@Override
default void setRemoveOnSelect(boolean remove) {
getFakeData().removeOnSelect = remove;
}

@Override
default void setRemoveDistance(long removeDistance) {
getFakeData().removeDistance = removeDistance;
}

@Override
default void setLocation(Location location) {
((LocationInfo) getLocationInfo()).updatePosition(location.x(), location.y());
}

@Override
default void setTimeout(long keepAlive) {
getFakeData().setTimeout(keepAlive);
}

class Data {
private final Entity entity;
private Timer timeout;
private long removeDistance;
private boolean removeOnSelect;

public Data(Entity entity) {
this.entity = entity;
this.entity.main = Main.INSTANCE;
this.entity.removed = false;
}

private void setTimeout(long keepAlive) {
if (keepAlive != -1) {
timeout = Timer.get(keepAlive);
timeout.activate();
}
else timeout = null;
}

public boolean isInvalid() {
if (timeout != null && timeout.isInactive()) return true;
return removeDistance == -1 || entity.main.hero.distanceTo(entity) < removeDistance;
}

public boolean trySelect(boolean tryAttack) {
if (removeOnSelect) entity.removed();
return false;
}
}

}
Loading

0 comments on commit b7de0bf

Please sign in to comment.