Unit stances for specific item mining

This commit is contained in:
Anuken
2025-04-25 15:44:49 -04:00
parent ca19d525c4
commit 847a201597
11 changed files with 113 additions and 26 deletions

View File

@@ -0,0 +1,33 @@
package mindustry.ai;
import arc.*;
import arc.scene.style.*;
import arc.struct.*;
import arc.util.*;
import mindustry.type.*;
public class ItemUnitStance extends UnitStance{
private static ObjectMap<Item, ItemUnitStance> itemToStance = new ObjectMap<>();
public final Item item;
public ItemUnitStance(Item item){
super("item-" + item.name, "item-" + item.name, null);
this.item = item;
itemToStance.put(item, this);
}
public static @Nullable ItemUnitStance getByItem(Item item){
return itemToStance.get(item);
}
@Override
public String localized(){
return Core.bundle.format("stance.mine", item.localizedName);
}
@Override
public TextureRegionDrawable getIcon(){
return new TextureRegionDrawable(item.uiIcon);
}
}

View File

@@ -26,6 +26,8 @@ public class UnitCommand extends MappableContent{
public boolean resetTarget = true;
/** */
public boolean exactArrival = false;
/** If true, this command refreshes the list of stances when selected TODO: do not use, this will likely be removed later!*/
public boolean refreshOnSelect = false;
/** Key to press for this command. */
public @Nullable Binding keybind = null;
@@ -76,7 +78,9 @@ public class UnitCommand extends MappableContent{
ai.onlyAssist = true;
return ai;
});
mineCommand = new UnitCommand("mine", "production", Binding.unit_command_mine, u -> new MinerAI());
mineCommand = new UnitCommand("mine", "production", Binding.unit_command_mine, u -> new MinerAI()){{
refreshOnSelect = true;
}};
boostCommand = new UnitCommand("boost", "up", Binding.unit_command_boost, u -> new BoostAI()){{
switchToMove = false;
drawTarget = true;

View File

@@ -3,12 +3,14 @@ package mindustry.ai;
import arc.*;
import arc.scene.style.*;
import arc.util.*;
import mindustry.*;
import mindustry.ctype.*;
import mindustry.gen.*;
import mindustry.input.*;
import mindustry.type.*;
public class UnitStance extends MappableContent{
public static UnitStance stop, shoot, holdFire, pursueTarget, patrol, ram;
public static UnitStance stop, shoot, holdFire, pursueTarget, patrol, ram, mineAuto;
/** Name of UI icon (from Icon class). */
public String icon;
@@ -30,7 +32,7 @@ public class UnitStance extends MappableContent{
}
public char getEmoji() {
return (char) Iconc.codes.get(icon, Iconc.cancel);
return (char)Iconc.codes.get(icon, Iconc.cancel);
}
@Override
@@ -50,5 +52,11 @@ public class UnitStance extends MappableContent{
pursueTarget = new UnitStance("pursuetarget", "right", Binding.unit_stance_pursue_target);
patrol = new UnitStance("patrol", "refresh", Binding.unit_stance_patrol);
ram = new UnitStance("ram", "rightOpen", Binding.unit_stance_ram);
mineAuto = new UnitStance("mineauto", "settings", null);
//Only vanilla items are supported for now
for(Item item : Vars.content.items()){
new ItemUnitStance(item);
}
}
}

View File

@@ -77,6 +77,11 @@ public class CommandAI extends AIController{
//this should not be possible
if(stance == UnitStance.stop) stance = UnitStance.shoot;
//fix incorrect stance when mining
if(command == UnitCommand.mineCommand && stance != UnitStance.mineAuto && !(stance instanceof ItemUnitStance)){
stance = UnitStance.mineAuto;
}
//pursue the target if relevant
if(stance == UnitStance.pursueTarget && target != null && attackTarget == null && targetPos == null){
commandTarget(target, false);

View File

@@ -1,5 +1,6 @@
package mindustry.ai.types;
import mindustry.ai.*;
import mindustry.entities.units.*;
import mindustry.gen.*;
import mindustry.type.*;
@@ -22,8 +23,12 @@ public class MinerAI extends AIController{
unit.mineTile(null);
}
Item autoItem = unit.controller() instanceof CommandAI ai && ai.stance instanceof ItemUnitStance stance ? stance.item : null;
if(mining){
if(timer.get(timerTarget2, 60 * 4) || targetItem == null){
if(autoItem != null){
targetItem = autoItem;
}else if(timer.get(timerTarget2, 60 * 4) || targetItem == null){
targetItem = unit.type.mineItems.min(i -> indexer.hasOre(i) && unit.canMine(i), i -> core.items.get(i));
}