Skip to content

Commit

Permalink
Fake entities API & micro patches
Browse files Browse the repository at this point in the history
  • Loading branch information
m9w committed Jan 18, 2024
1 parent b9496f9 commit 641065f
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/main/java/com/github/manolo8/darkbot/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ public static void main(String[] args) throws IOException {
UIManager.put("Table.cellFocusColor", new Color(0, 0, 0, 160));

// set icon here to use it in login popup, java check popup etc.
Popups.setDefaultIcon(MainGui.ICON);
try {
Popups.setDefaultIcon(MainGui.ICON);
} catch (Exception e) {
e.printStackTrace();
}

// Not recommended to keep for production
//FlatInspector.install("ctrl shift alt X");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ public static class Performance {

public @Option @Visibility(Level.INTERMEDIATE) Other OTHER = new Other();
public static class Other {
public @Option boolean SHOW_INSTRUCTIONS = true;
public @Option boolean DISABLE_MASTER_PASSWORD = false;
public @Option boolean ALWAYS_SHOW_CAPTCHA = false;
public @Option @Number(min = 10, max = 300) int ZONE_RESOLUTION = 30;
Expand Down
63 changes: 63 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,7 +3,9 @@
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.manager.HeroManager;
import com.github.manolo8.darkbot.utils.Offsets;
import eu.darkbot.api.game.other.Location;
import eu.darkbot.util.Timer;
import org.jetbrains.annotations.Nullable;

Expand All @@ -26,6 +28,10 @@ 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);
this.update(address);
Expand Down Expand Up @@ -135,4 +141,61 @@ public Beacon(int id, long address) {
super(id, address);
}
}

public static class FakeBox extends Box implements eu.darkbot.api.game.entities.FakeEntity.FakeBox {
private static int CURR_ID = Integer.MIN_VALUE;
private Timer timeout;
private long removeDistance;
private boolean isRemoveWhenAttemptSelect;

public FakeBox(String boxName, Location loc, long removeDistance, long keepAlive, boolean isRemoveWhenAttemptSelect) {
super(CURR_ID++);
super.locationInfo.updatePosition(loc.x(), loc.y());
super.main = HeroManager.instance.main;
super.type = boxName;
super.boxInfo = ConfigEntity.INSTANCE.getOrCreateBoxInfo(type);
super.removed = false;
setRemoveDistance(removeDistance);
setTimeout(keepAlive);
setRemoveWhenAttemptSelect(isRemoveWhenAttemptSelect);
}

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

public void setRemoveDistance(long removeDistance) {
this.removeDistance = removeDistance;
}

public void setRemoveWhenAttemptSelect(boolean removeWhenAttemptSelect) {
isRemoveWhenAttemptSelect = removeWhenAttemptSelect;
}

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

public boolean tryCollect() {
return trySelect(false);
}

public boolean isInvalid(long mapAddress) {
if (timeout != null && timeout.isInactive()) return false;
return HeroManager.instance.distanceTo(this) < removeDistance;
}

public boolean trySelect(boolean tryAttack) {
if (isRemoveWhenAttemptSelect) removed();
return false;
}

public void update() {}

public void update(long address) {}
}
}
45 changes: 45 additions & 0 deletions src/main/java/com/github/manolo8/darkbot/core/entities/Mine.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.github.manolo8.darkbot.core.entities;

import com.github.manolo8.darkbot.core.itf.Obstacle;
import com.github.manolo8.darkbot.core.manager.HeroManager;
import com.github.manolo8.darkbot.core.utils.pathfinder.AreaImpl;
import com.github.manolo8.darkbot.core.utils.pathfinder.CircleImpl;
import eu.darkbot.api.game.other.Location;
import eu.darkbot.util.Timer;

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

Expand All @@ -14,6 +17,10 @@ public class Mine extends Entity implements Obstacle, eu.darkbot.api.game.entiti

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

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

public Mine(int id, long address) {
super(id);
this.update(address);
Expand Down Expand Up @@ -58,4 +65,42 @@ public String toString() {
public int getTypeId() {
return typeId;
}

public static class FakeMine extends Mine implements eu.darkbot.api.game.entities.FakeEntity.FakeMine {
private static int CURR_ID = Integer.MIN_VALUE;
private Timer timeout;
private long removeDistance;

public FakeMine(int typeId, Location loc, long removeDistance, long keepAlive) {
super(CURR_ID++);
super.locationInfo.updatePosition(loc.x(), loc.y());
super.main = HeroManager.instance.main;
super.typeId = typeId;
super.area.set(locationInfo.now, typeId == FROZEN_LAB_MINE ? 500 : 200);
super.removed = false;
setTimeout(keepAlive);
setRemoveDistance(removeDistance);
}

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

public void setRemoveDistance(long removeDistance) {
this.removeDistance = removeDistance;
}

public boolean isInvalid(long mapAddress) {
if (timeout != null && timeout.isInactive()) return true;
return removeDistance == -1 || HeroManager.instance.distanceTo(this) < removeDistance;
}

public void update() {}

public void update(long address) {}
}
}
59 changes: 59 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 @@ -3,6 +3,10 @@
import com.github.manolo8.darkbot.config.ConfigEntity;
import com.github.manolo8.darkbot.config.NpcInfo;
import com.github.manolo8.darkbot.core.manager.EffectManager;
import com.github.manolo8.darkbot.core.manager.HeroManager;
import eu.darkbot.api.game.entities.FakeEntity;
import eu.darkbot.api.game.other.Location;
import eu.darkbot.util.Timer;

import java.util.Objects;

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

public static class FakeNpc extends Npc implements FakeEntity.FakeShip {
private static int CURR_ID = Integer.MIN_VALUE;
private Timer timeout;
private long removeDistance;
private boolean isRemoveWhenAttemptSelect;

public FakeNpc(String npcName, Location loc, long removeDistance, long keepAlive, boolean isRemoveWhenAttemptSelect) {
super(CURR_ID++);
this.npcInfo = ConfigEntity.INSTANCE.getOrCreateNpcInfo(npcName);
setLocation(loc);
setRemoveDistance(removeDistance);
setTimeout(keepAlive);
setRemoveWhenAttemptSelect(isRemoveWhenAttemptSelect);
}

@Override
public void setRemoveWhenAttemptSelect(boolean removeWhenAttemptSelect) {
isRemoveWhenAttemptSelect = removeWhenAttemptSelect;
}

@Override
public void setLocation(Location loc) {
locationInfo.updatePosition(loc.x(), loc.y());
}

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

@Override
public void setRemoveDistance(long distance) {
removeDistance = distance;
}


public boolean trySelect(boolean tryAttack) {
if (isRemoveWhenAttemptSelect) removed();
return false;
}

public boolean isInvalid(long mapAddress) {
if (timeout != null && timeout.isInactive()) return false;
return HeroManager.instance.distanceTo(this) < removeDistance;
}

public void update() {}

public void update(long address) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public GuiManager(Main main, PluginAPI pluginAPI, RepairManager repairManager) {
this.assembly = register("assembly");

register("ggBuilder", GateSpinnerGui.class);
register("refinement_count");

this.commandCenter = register("command_center");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.github.manolo8.darkbot.core.objects.swf.ObjArray;
import com.github.manolo8.darkbot.core.utils.factory.EntityFactory;
import com.github.manolo8.darkbot.core.utils.factory.EntityRegistry;
import eu.darkbot.api.game.entities.FakeEntity;
import eu.darkbot.api.game.entities.Mist;
import eu.darkbot.api.game.entities.Station;
import eu.darkbot.api.managers.EntitiesAPI;
Expand Down Expand Up @@ -128,6 +129,24 @@ private void onEntityCreate(Entity entity) {
this.eventBroker.sendEvent(new EntityCreateEvent(entity));
}

public FakeEntity.FakeMine createFakeMine(int typeId, eu.darkbot.api.game.other.Location loc, long removeDistance, long keepAlive) {
return register(new Mine.FakeMine(typeId, loc, removeDistance, keepAlive), mines);
}

public FakeEntity.FakeBox createFakeBox(String boxName, eu.darkbot.api.game.other.Location loc, long removeDistance, long keepAlive, boolean removeIfAttemptSelect) {
return register(new Box.FakeBox(boxName, loc, removeDistance, keepAlive, removeIfAttemptSelect), boxes);
}

public FakeEntity.FakeShip createFakeNpc(String npcName, eu.darkbot.api.game.other.Location loc, long removeDistance, long keepAlive, boolean removeIfAttemptSelect) {
return register(new Npc.FakeNpc(npcName, loc, removeDistance, keepAlive, removeIfAttemptSelect), npcs);
}

private <T extends Entity> T register(T entity, Collection<? super T> collection) {
collection.add(entity);
onEntityCreate(entity);
return entity;
}

@SuppressWarnings("unchecked")
private <T extends Entity> List<T> register(EntityFactory... types) {
List<T> list = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.manolo8.darkbot.extensions.features.decorators;

import com.github.manolo8.darkbot.config.ConfigEntity;
import com.github.manolo8.darkbot.extensions.features.FeatureDefinition;
import com.github.manolo8.darkbot.gui.utils.Popups;
import com.github.manolo8.darkbot.utils.I18n;
Expand All @@ -13,7 +14,10 @@ public class InstructionProviderDecorator extends FeatureDecorator<InstructionPr

@Override
protected void load(FeatureDefinition<InstructionProvider> fd, InstructionProvider obj) {
if (obj instanceof Module && !(obj instanceof TemporalModule)) showInstructions(obj, fd.getName());
if (obj instanceof Module
&& !(obj instanceof TemporalModule)
&& ConfigEntity.INSTANCE.getConfig().BOT_SETTINGS.OTHER.SHOW_INSTRUCTIONS
) showInstructions(obj, fd.getName());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ private static Predicate<String> toMatcher(String pattern) {
super(urls);
}

@SuppressWarnings("unused")
public Class<?> defineClass(String name, byte[] bytes) throws ClassNotFoundException {
if (PROTECTED.stream().anyMatch(p -> p.test(name)))
throw new ClassNotFoundException(name + " is a protected class");
return super.defineClass(name, bytes, 0 ,bytes.length);
}

@Override
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if (PROTECTED.stream().anyMatch(p -> p.test(name)))
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/lang/strings_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ config.bot_settings.other.zone_resolution.desc=Amount of map subdivisions when s
config.bot_settings.other.min_tick=Minimum tick time
config.bot_settings.other.dev_stuff=Developer stuff shown
config.bot_settings.other.dev_stuff.desc=Enabling this WILL make your bot use more cpu.
config.bot_settings.other.show_instructions=Show instructions
config.bot_settings.other.show_instructions.desc=Enabling showing instructions for modules after loading it
config.bot_settings.other.cross_app_events_router_address=Cross-application router
config.bot_settings.other.cross_app_events_router_address.desc=Address of router for cross-application communication (empty - disabled)

# Misc
misc.editor.checkbox_list.selected={0} selected
Expand Down

0 comments on commit 641065f

Please sign in to comment.