Skip to content
Open
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
286 changes: 213 additions & 73 deletions src/main/java/com/github/manolo8/darkbot/config/Config.java

Large diffs are not rendered by default.

31 changes: 25 additions & 6 deletions src/main/java/com/github/manolo8/darkbot/config/NpcInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -26,8 +27,11 @@ public class NpcInfo implements eu.darkbot.api.config.types.NpcInfo {
public boolean kill;
@Option("config.loot.npc_table.attack_key")
public Character attackKey;
// @Option("config.loot.npc_table.attack_formation")
@Option.Ignore
private Character attackFormation;
@Option("config.loot.npc_table.attack_formation")
public Character attackFormation;
public SelectableItem.Formation attackFormationNew;
public ExtraNpcInfo extra = new ExtraNpcInfo();

public transient String name;
Expand All @@ -54,12 +58,12 @@ public void set(Double radius, Integer priority, Boolean kill, Character attackK
set(radius, priority, kill, attackKey, null, extra);
}

public void set(Double radius, Integer priority, Boolean kill, Character attackKey, Character attackFormation, ExtraNpcInfo extra) {
public void set(Double radius, Integer priority, Boolean kill, Character attackKey, SelectableItem.Formation attackFormation, ExtraNpcInfo extra) {
if (radius != null) this.radius = radius;
if (priority != null) this.priority = priority;
if (kill != null) this.kill = kill;
if (attackKey != null) this.attackKey = attackKey;
if (attackFormation != null) this.attackFormation = attackFormation;
if (attackFormation != null) this.attackFormationNew = attackFormation;
if (extra != null) this.extra = extra;
}

Expand All @@ -68,7 +72,7 @@ public void copyOf(NpcInfo other) {
this.priority = other.priority;
this.kill = other.kill;
this.attackKey = other.attackKey;
this.attackFormation = other.attackFormation;
this.attackFormationNew = other.attackFormationNew;
this.extra.flags = new HashSet<>(other.extra.flags);
}

Expand Down Expand Up @@ -109,7 +113,21 @@ public Optional<SelectableItem.Laser> getAmmo() {

@Override
Comment thread
Pablete1234 marked this conversation as resolved.
public Optional<SelectableItem.Formation> getFormation() {
return getHeroItems().getItem(attackFormation, ItemCategory.DRONE_FORMATIONS, SelectableItem.Formation.class);
if (attackFormation != null) {
// Try to find associated formation and update state if found
getHeroItems().getItem(attackFormation, ItemCategory.DRONE_FORMATIONS, SelectableItem.Formation.class)
.map(formation -> {
attackFormation = null;
attackFormationNew = formation;
return formation;
});
}
return Optional.ofNullable(attackFormationNew);
// If we have a new formation, use it and clear the old one
}

public void setAttackFormation(SelectableItem.Formation formation) {
this.attackFormationNew = formation;
}

private static HeroItemsAPI getHeroItems() {
Expand Down Expand Up @@ -168,7 +186,8 @@ public static String getId(Enum<?> flag) {
public static class ExtraNpcInfo {
private Set<String> flags = new HashSet<>();

public ExtraNpcInfo() {}
public ExtraNpcInfo() {
}

public ExtraNpcInfo(NpcExtraFlag... flags) {
for (NpcExtraFlag flag : flags) set(flag, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.github.manolo8.darkbot.core.objects.slotbars.CategoryBar;
import com.github.manolo8.darkbot.core.objects.slotbars.Item;
import eu.darkbot.api.game.items.ItemCategory;
import eu.darkbot.api.game.items.SelectableItem;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;
Expand Down Expand Up @@ -34,4 +35,10 @@ public static Optional<Item> findAssociatedItem(@Nullable ItemCategory category,
.findAny();
}

public static Character findAssociatedItem(@Nullable ItemCategory category, SelectableItem selectableItem) {
Comment thread
Pablete1234 marked this conversation as resolved.
Main main = Main.INSTANCE;

return main.facadeManager.slotBars.getKeyBind(selectableItem);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,12 @@ public boolean attackMode(Npc target) {
Config.ShipConfig config = this.main.config.GENERAL.OFFENSIVE;

boolean otherConfig = target.npcInfo.extra.has(NpcExtra.OPPOSITE_CONFIG);
return setMode(
otherConfig ? ((config.CONFIG % 2) + 1) : config.CONFIG,
target.npcInfo.attackFormation != null ?
target.npcInfo.attackFormation : config.FORMATION);

HeroAPI.Configuration newConfig = otherConfig ? config.getOppositeConfiguration() : config.getConfiguration();

SelectableItem.Formation formation = target.npcInfo.getFormation().orElse(config.getFormation());

return setMode(new ShipMode.ShipModeImpl(newConfig, formation));
}

public boolean runMode() {
Expand All @@ -215,7 +217,7 @@ public boolean roamMode() {

@Deprecated
public boolean setMode(Config.ShipConfig config) {
return setMode(config.CONFIG, config.FORMATION);
return setMode(config.getShipMode());
}

@Deprecated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public EditorProvider(PluginAPI api,

defaultEditors.put(ShipMode.class, ShipModeEditor.class);
defaultEditors.put(Config.ShipConfig.class, ShipModeEditor.class);
defaultEditors.put(ShipMode.ShipModeImpl.class, ShipModeEditor.class);

defaultEditors.put(ImageWrapper.class, ImagePicker.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,102 @@

import com.github.manolo8.darkbot.config.Config;
import com.github.manolo8.darkbot.gui.AdvancedConfig;
import com.github.manolo8.darkbot.gui.tree.utils.SizedLabel;
import com.github.manolo8.darkbot.gui.utils.UIUtils;
import eu.darkbot.api.config.ConfigSetting;
import eu.darkbot.api.config.types.ShipMode;
import eu.darkbot.api.config.util.OptionEditor;
import eu.darkbot.api.game.items.SelectableItem;
import eu.darkbot.api.managers.HeroAPI;

import javax.swing.*;
import java.awt.*;
import net.miginfocom.swing.MigLayout;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JPanel;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.ItemEvent;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ShipModeEditor extends JPanel implements OptionEditor<ShipMode> {

private HeroAPI.Configuration config;
private HeroAPI.Configuration newConfig;

private final List<ConfigButton> configButtons = Arrays.stream(HeroAPI.Configuration.values())
.filter(c -> c != HeroAPI.Configuration.UNKNOWN)
.map(ConfigButton::new).collect(Collectors.toList());
.map(ConfigButton::new)
.collect(Collectors.toList());

private SelectableItem.Formation newFormation;

private final CharacterEditor formationField = new CharacterEditor();
private final JComboBox<Object> comboBox;

public ShipModeEditor() {
super(new FlowLayout(FlowLayout.LEFT, 0, 0));
super(new MigLayout("gap 0, ins 0", "[][]5px[]", "[17px]"));

this.newConfig = HeroAPI.Configuration.FIRST;
this.newFormation = SelectableItem.Formation.STANDARD;

setOpaque(false);

for (ConfigButton configButton : configButtons) {
add(configButton);
configButton.addKeyListener(formationField); // Relay key presses to formation
}
add(new SizedLabel(" Formation "));
add(formationField);
configButtons.forEach(button -> add(button, "wmax 17, hmax 17"));

// Build combo box items: "None" + all formations
Object[] items = new Object[SelectableItem.Formation.values().length + 1];
items[0] = "None";
System.arraycopy(SelectableItem.Formation.values(), 0, items, 1, SelectableItem.Formation.values().length);

comboBox = new JComboBox<>(items);
comboBox.setRenderer(new FormationRenderer());

// FIX: Add focus listener to show popup immediately
comboBox.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
Comment thread
Pablete1234 marked this conversation as resolved.
comboBox.showPopup();
}
});

comboBox.addItemListener(item -> {
if (item.getStateChange() == ItemEvent.SELECTED) {
Object selected = item.getItem();
if (selected instanceof SelectableItem.Formation) {
setFormation((SelectableItem.Formation) selected);
} else {
setFormation(null); // "None" selected
}
}
});
add(comboBox, "hmax 17");
}


@Override
public JComponent getEditorComponent(ConfigSetting<ShipMode> mode) {
ShipMode value = mode.getValue();

public JComponent getEditorComponent(ConfigSetting<ShipMode> shipConfig) {
ShipMode value = shipConfig.getValue();
setConfig(value.getConfiguration());

if (value instanceof Config.ShipConfig) {
formationField.setValue(((Config.ShipConfig) value).FORMATION);
} else {
// TODO: show a formation selection dropdown?
// we cannot use old & new formats interchangeably here, because
// converting key to formation or formation to key can only be done while the
// bot is running. If running in no-op mode it's impossible to convert.
// Old format must stay old format not to break plugins, and new format can't be
// expressed with old format because it is unknown before runtime.
// The only real solution is to have different editors for old & new format,
// one that is key based, and one that is formation based.
formationField.setValue(null);
}

setFormation(value.getFormation());
return this;
}

private void setConfig(HeroAPI.Configuration config) {
this.config = config;
this.newConfig = config;
configButtons.forEach(ConfigButton::repaint);
}

private void setFormation(SelectableItem.Formation newFormation) {
this.newFormation = newFormation;
this.comboBox.setSelectedItem(newFormation == null ? "None" : newFormation);
}
Comment thread
Pablete1234 marked this conversation as resolved.

@Override
public ShipMode getEditorValue() {
return new Config.ShipConfig(config.ordinal(), formationField.getEditorValue());
return new Config.ShipConfig(newConfig, newFormation);
}

@Override
Expand All @@ -94,8 +124,28 @@ private class ConfigButton extends JButton {

@Override
public boolean isDefaultButton() {
return ShipModeEditor.this.config == config;
return ShipModeEditor.this.newConfig == config;
}
}

}
private static class FormationRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList<?> list, Object value, int index,
boolean isSelected, boolean cellHasFocus) {
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

if (value instanceof SelectableItem.Formation) {
SelectableItem.Formation formation = (SelectableItem.Formation) value;
setIcon(UIUtils.getFormationIcon(formation.name().toLowerCase()));
setText(formation.toString());
} else {
// "None" or any other value
setIcon(UIUtils.getFormationIcon("default"));
setText(String.valueOf(value));
}

setIconTextGap(3);
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import com.github.manolo8.darkbot.config.NpcInfo;
import com.github.manolo8.darkbot.gui.utils.MultiTableRowSorter;
import com.github.manolo8.darkbot.gui.utils.table.ExtraNpcInfoEditor;
import com.github.manolo8.darkbot.gui.utils.table.FormationNpcInfoEditor;
import eu.darkbot.api.config.ConfigSetting;
import eu.darkbot.api.config.annotations.Table;
import eu.darkbot.api.game.items.SelectableItem;
import eu.darkbot.api.managers.ConfigAPI;
import eu.darkbot.api.managers.StarSystemAPI;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -76,6 +78,7 @@ public void handle(JTable jTable, JScrollPane jScrollPane,
new RowSorter.SortKey(2, SortOrder.ASCENDING),
new RowSorter.SortKey(0, SortOrder.DESCENDING)));

jTable.setDefaultEditor(SelectableItem.Formation.class, new FormationNpcInfoEditor());
jTable.setDefaultEditor(NpcInfo.ExtraNpcInfo.class, new ExtraNpcInfoEditor());
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/github/manolo8/darkbot/gui/utils/UIUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ public static Icon getIcon(String name) {
return getSvgIcon(url, 16, 16);
}

public static Icon getFormationIcon(String name) {
URL url = UIUtils.class.getResource("/formations/" + name + ".svg");
if (url != null) return getSvgIcon(url);

url = Objects.requireNonNull(UIUtils.class.getResource("/missing.svg"));
return getSvgIcon(url);
}

private static FlatSVGIcon getSvgIcon(URL url) {
return new FlatSVGIcon(url);
}

private static FlatSVGIcon getSvgIcon(URL url, int width, int height) {
return new FlatSVGIcon(url).derive(width, height);
}
Expand Down
Loading
Loading