Skip to content

Commit

Permalink
add support for potion order reordering (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
hex-agon authored Oct 14, 2024
1 parent cf4433c commit 5e6de02
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public interface MasteringMixologyConfig extends Config {
)
String HIGHLIGHTS = "Highlights";

@ConfigItem(
keyName = "potionOrderSorting",
name = "Order sorting",
description = "Determines how potion orders are sorted in the interface",
position = 1
)
default PotionOrderSorting potionOrderSorting() {
return PotionOrderSorting.VANILLA;
}

@ConfigItem(
keyName = "highlightLevers",
name = "Highlight levers",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class MasteringMixologyPlugin extends Plugin {

private static final Logger LOGGER = LoggerFactory.getLogger(MasteringMixologyPlugin.class);

private static final int PROC_MASTERING_MIXOLOGY_BUILD_POTION_ORDER = 7063;
private static final int PROC_MASTERING_MIXOLOGY_BUILD_POTION_ORDERS = 7063;
private static final int PROC_MASTERING_MIXOLOGY_BUILD_REAGENTS = 7064;

private static final int VARBIT_POTION_ORDER_1 = 11315;
Expand Down Expand Up @@ -163,7 +163,6 @@ public void onWidgetLoaded(WidgetLoaded event) {
if (event.getGroupId() != COMPONENT_POTION_ORDERS_GROUP_ID) {
return;
}

initialize();
}

Expand All @@ -183,6 +182,10 @@ public void onConfigChanged(ConfigChanged event) {
return;
}

if (event.getKey().equals("potionOrderSorting")) {
clientThread.invokeLater(this::updatePotionOrders);
}

if (!config.highlightStations()) {
unHighlightAllStations();
}
Expand Down Expand Up @@ -372,34 +375,73 @@ public void onGraphicsObjectCreated(GraphicsObjectCreated event) {

@Subscribe
public void onScriptPostFired(ScriptPostFired event) {
if (event.getScriptId() != PROC_MASTERING_MIXOLOGY_BUILD_REAGENTS) {
var scriptId = event.getScriptId();
if (scriptId != PROC_MASTERING_MIXOLOGY_BUILD_POTION_ORDERS && scriptId != PROC_MASTERING_MIXOLOGY_BUILD_REAGENTS) {
return;
}
var baseWidget = client.getWidget(COMPONENT_POTION_ORDERS);

if (baseWidget == null) {
return;
}
var textComponents = findTextComponents(baseWidget);
if (scriptId == PROC_MASTERING_MIXOLOGY_BUILD_POTION_ORDERS) {
updatePotionOrdersComponent(baseWidget);
} else {
appendResins(baseWidget);
}
}

if (textComponents.size() < 4) {
private void updatePotionOrdersComponent(Widget baseWidget) {
// https://github.com/Joshua-F/cs2-scripts/blob/7cc261be62a40a6390de3e1f770259038660af10/scripts/%5Bproc%2Cscript7063%5D.cs2#L26
var children = baseWidget.getChildren();

if (children == null) {
return;
}

for (var order : potionOrders) {
// The first text widget is always the interface title 'Potion Orders'
appendPotionRecipe(textComponents.get(order.idx()), order.idx(), order.fulfilled());
}
for (int i = 0; i < potionOrders.size(); i++) {
var order = potionOrders.get(i);

var orderGraphic = children[order.idx() * 2 + 1];
var orderText = children[order.idx() * 2 + 2];

// If anyone still has orders they don't have the herblore level to deliver there's an extra RECTANGLE component which
// causes the idx calculations to select the wrong components
if (orderGraphic.getType() != WidgetType.GRAPHIC || orderText.getType() != WidgetType.TEXT) {
continue;
}
var builder = new StringBuilder(orderText.getText());

if (order.fulfilled()) {
builder.append(" (<col=00ff00>done!</col>)");
} else {
builder.append(" (").append(order.potionType().recipe()).append(")");
}
orderText.setText(builder.toString());

if (config.displayResin()) {
var parentWidth = baseWidget.getWidth();
var dx = parentWidth / 3;
int x = dx / 2;
if (i != order.idx()) {
// update component position
var y = 20 + (i * 26) + 3;
orderGraphic.setOriginalY(y);
orderText.setOriginalY(y);

addResinText(baseWidget.createChild(-1, WidgetType.TEXT), x, VARP_MOX_RESIN, MOX);
addResinText(baseWidget.createChild(-1, WidgetType.TEXT), x + dx, VARP_AGA_RESIN, AGA);
addResinText(baseWidget.createChild(-1, WidgetType.TEXT), x + dx * 2, VARP_LYE_RESIN, LYE);
orderGraphic.revalidate();
orderText.revalidate();
}
}
}

private void appendResins(Widget baseWidget) {
if (!config.displayResin()) {
return;
}
var parentWidth = baseWidget.getWidth();
var dx = parentWidth / 3;
int x = dx / 2;

addResinText(baseWidget.createChild(-1, WidgetType.TEXT), x, VARP_MOX_RESIN, MOX);
addResinText(baseWidget.createChild(-1, WidgetType.TEXT), x + dx, VARP_AGA_RESIN, AGA);
addResinText(baseWidget.createChild(-1, WidgetType.TEXT), x + dx * 2, VARP_LYE_RESIN, LYE);
}

private void initialize() {
Expand Down Expand Up @@ -483,6 +525,14 @@ private void updatePotionOrders() {
LOGGER.debug("Updating potion orders");
potionOrders = getPotionOrders();

var potionOrderSorting = config.potionOrderSorting();

if (potionOrderSorting != PotionOrderSorting.VANILLA) {
LOGGER.debug("Orders pre-sort: {}", potionOrders);
potionOrders.sort(potionOrderSorting.comparator());
LOGGER.debug("Sorted orders: {}", potionOrders);
}

// Trigger a fake varbit update to force run the clientscript proc
var varbitType = client.getVarbit(VARBIT_POTION_ORDER_1);

Expand All @@ -491,49 +541,20 @@ private void updatePotionOrders() {
}
}

private List<Widget> findTextComponents(Widget parent) {
var children = parent.getDynamicChildren();
var textComponents = new ArrayList<Widget>();

for (var child : children) {
if (child.getType() != WidgetType.TEXT) {
continue;
}
textComponents.add(child);
}
return textComponents;
}

private void appendPotionRecipe(Widget component, int orderIdx, boolean fulfilled) {
var potionType = getPotionType(orderIdx);

if (potionType == null) {
return;
}
var builder = new StringBuilder(component.getText());

if (fulfilled) {
builder.append(" (<col=00ff00>done!</col>)");
} else {
builder.append(" (").append(potionType.recipe()).append(")");
}
component.setText(builder.toString());
}

private void addResinText(Widget widget, int x, int varp, PotionComponent component) {
var amount = client.getVarpValue(varp);
var color = ColorUtil.fromHex(component.color()).getRGB();

widget.setText(amount + "")
.setTextColor(color)
.setOriginalWidth(20)
.setOriginalHeight(15)
.setFontId(FontID.QUILL_8)
.setOriginalY(0)
.setOriginalX(x)
.setYPositionMode(WidgetPositionMode.ABSOLUTE_BOTTOM)
.setXTextAlignment(WidgetTextAlignment.CENTER)
.setYTextAlignment(WidgetTextAlignment.CENTER);
.setTextColor(color)
.setOriginalWidth(20)
.setOriginalHeight(15)
.setFontId(FontID.QUILL_8)
.setOriginalY(0)
.setOriginalX(x)
.setYPositionMode(WidgetPositionMode.ABSOLUTE_BOTTOM)
.setXTextAlignment(WidgetTextAlignment.CENTER)
.setYTextAlignment(WidgetTextAlignment.CENTER);

widget.revalidate();
LOGGER.debug("adding resin text {} at {} with color {}", amount, x, color);
Expand Down Expand Up @@ -571,7 +592,7 @@ private void tryHighlightNextStation() {
private List<PotionOrder> getPotionOrders() {
var potionOrders = new ArrayList<PotionOrder>(3);

for (int orderIdx = 1; orderIdx <= 3; orderIdx++) {
for (int orderIdx = 0; orderIdx < 3; orderIdx++) {
var potionType = getPotionType(orderIdx);
var potionModifier = getPotionModifier(orderIdx);

Expand All @@ -584,23 +605,23 @@ private List<PotionOrder> getPotionOrders() {
}

private PotionType getPotionType(int orderIdx) {
if (orderIdx == 1) {
if (orderIdx == 0) {
return PotionType.fromIdx(client.getVarbitValue(VARBIT_POTION_ORDER_1) - 1);
} else if (orderIdx == 2) {
} else if (orderIdx == 1) {
return PotionType.fromIdx(client.getVarbitValue(VARBIT_POTION_ORDER_2) - 1);
} else if (orderIdx == 3) {
} else if (orderIdx == 2) {
return PotionType.fromIdx(client.getVarbitValue(VARBIT_POTION_ORDER_3) - 1);
} else {
return null;
}
}

private PotionModifier getPotionModifier(int orderIdx) {
if (orderIdx == 1) {
if (orderIdx == 0) {
return PotionModifier.from(client.getVarbitValue(VARBIT_POTION_MODIFIER_1) - 1);
} else if (orderIdx == 2) {
} else if (orderIdx == 1) {
return PotionModifier.from(client.getVarbitValue(VARBIT_POTION_MODIFIER_2) - 1);
} else if (orderIdx == 3) {
} else if (orderIdx == 2) {
return PotionModifier.from(client.getVarbitValue(VARBIT_POTION_MODIFIER_3) - 1);
} else {
return null;
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/work/fking/masteringmixology/PotionOrderSorting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package work.fking.masteringmixology;

import java.util.Comparator;

public enum PotionOrderSorting {
VANILLA("Vanilla (random)", null),
BY_STATION("By station", Comparator.comparing(order -> order.potionModifier().ordinal()));

private final String name;
private final Comparator<PotionOrder> comparator;

PotionOrderSorting(String name, Comparator<PotionOrder> comparator) {
this.name = name;
this.comparator = comparator;
}

public Comparator<PotionOrder> comparator() {
return comparator;
}

@Override
public String toString() {
return name;
}
}

0 comments on commit 5e6de02

Please sign in to comment.