More Abilities

This commit is contained in:
Kam committed 2026-01-26 04:42:23 -05:00
1 parent de043f4a25
commit 651f2d0d84
24 files changed
+560 -220

No files matched your search

+90 -58
View File
@@ -18,9 +18,7 @@ import noppes.npcs.scripted.NpcAPI;
import noppes.npcs.scripted.event.AbilityEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
/**
@@ -47,9 +45,14 @@ public class DataAbilities {
public boolean enabled = false;
/**
* Minimum ticks between ability selections (global cooldown)
* Minimum cooldown ticks between abilities (global minimum)
*/
public int globalCooldown = 20;
public int minCooldown = 80;
/**
* Maximum cooldown ticks between abilities (global maximum)
*/
public int maxCooldown = 200;
// ═══════════════════════════════════════════════════════════════════
// RUNTIME STATE (not saved)
@@ -61,14 +64,9 @@ public class DataAbilities {
private transient Ability currentAbility;
/**
* Per-ability cooldown timers (ability ID -> world time when cooldown ends)
* World time when NPC cooldown ends (can select next ability)
*/
private transient Map<String, Long> cooldowns = new HashMap<>();
/**
* Ticks until next ability can be selected (global cooldown timer)
*/
private transient int globalCooldownTimer = 0;
private transient long cooldownEndTime = 0;
/**
* Last target used for ability execution
@@ -101,11 +99,6 @@ public class DataAbilities {
return;
}
// Tick global cooldown
if (globalCooldownTimer > 0) {
globalCooldownTimer--;
}
// Tick current ability if executing
if (currentAbility != null && currentAbility.isExecuting()) {
tickCurrentAbility();
@@ -192,24 +185,33 @@ public class DataAbilities {
npc.motionZ = 0;
}
}
// Lock look direction at target during ACTIVE phase only
if (shouldLockLookDirection() && target != null && target.isEntityAlive()) {
npc.getLookHelper().setLookPositionWithEntity(target, 30.0F, 30.0F);
}
}
}
/**
* Called when an ability completes.
* Calculates cooldown as: random(minCooldown, maxCooldown) + ability's cooldown offset
*/
private void onAbilityComplete() {
if (currentAbility != null) {
// Stop any ability animation
stopAbilityAnimation();
// Start per-ability cooldown
startCooldown(currentAbility);
// Calculate cooldown: random(min, max) + ability offset
int baseCooldown = minCooldown;
if (maxCooldown > minCooldown) {
baseCooldown = minCooldown + random.nextInt(maxCooldown - minCooldown + 1);
}
int totalCooldown = baseCooldown + currentAbility.getCooldownTicks();
cooldownEndTime = npc.worldObj.getTotalWorldTime() + totalCooldown;
currentAbility = null;
lastTarget = null;
// Start global cooldown
globalCooldownTimer = globalCooldown;
}
}
@@ -247,7 +249,8 @@ public class DataAbilities {
if (currentAbility != null && currentAbility.isExecuting()) {
return false;
}
if (globalCooldownTimer > 0) {
// Check if still on cooldown
if (npc.worldObj.getTotalWorldTime() < cooldownEndTime) {
return false;
}
return true;
@@ -293,11 +296,6 @@ public class DataAbilities {
return false;
}
// Check cooldown
if (isOnCooldown(ability)) {
return false;
}
// Check if already executing
if (ability.isExecuting()) {
return false;
@@ -358,8 +356,11 @@ public class DataAbilities {
/**
* Play an animation on the NPC by ID.
* Public so abilities can trigger animations directly if needed.
*
* @param animationId The global animation ID to play, or -1 for none
*/
private void playAbilityAnimation(int animationId) {
public void playAbilityAnimation(int animationId) {
if (animationId < 0) return;
if (AnimationController.Instance == null) return;
@@ -407,37 +408,25 @@ public class DataAbilities {
// ═══════════════════════════════════════════════════════════════════
/**
* Check if an ability is on cooldown.
* Check if NPC is on cooldown (cannot use any ability).
*/
public boolean isOnCooldown(Ability ability) {
Long endTime = cooldowns.get(ability.getId());
if (endTime == null) {
return false;
}
return npc.worldObj.getTotalWorldTime() < endTime;
public boolean isOnCooldown() {
return npc.worldObj.getTotalWorldTime() < cooldownEndTime;
}
/**
* Start cooldown for an ability.
* Get remaining cooldown ticks.
*/
private void startCooldown(Ability ability) {
long endTime = npc.worldObj.getTotalWorldTime() + ability.getCooldownTicks();
cooldowns.put(ability.getId(), endTime);
public long getRemainingCooldown() {
long remaining = cooldownEndTime - npc.worldObj.getTotalWorldTime();
return remaining > 0 ? remaining : 0;
}
/**
* Reset cooldown for a specific ability.
* Reset cooldown (allow immediate ability use).
*/
public void resetCooldown(String abilityId) {
cooldowns.remove(abilityId);
}
/**
* Reset all cooldowns.
*/
public void resetAllCooldowns() {
cooldowns.clear();
globalCooldownTimer = 0;
public void resetCooldown() {
cooldownEndTime = 0;
}
// ═══════════════════════════════════════════════════════════════════
@@ -489,9 +478,18 @@ public class DataAbilities {
/**
* Record a hit for the hit count condition.
* Includes periodic cleanup to prevent memory leak.
*/
private void recordHit() {
recentHitTimes.add(npc.worldObj.getTotalWorldTime());
long currentTime = npc.worldObj.getTotalWorldTime();
recentHitTimes.add(currentTime);
// Cleanup old entries periodically to prevent unbounded growth
// Keep only hits from last 5 minutes (6000 ticks) max
if (recentHitTimes.size() > 50) {
long cutoff = currentTime - 6000;
recentHitTimes.removeIf(time -> time < cutoff);
}
}
/**
@@ -517,6 +515,8 @@ public class DataAbilities {
/**
* Interrupt the currently executing ability.
* The ability will transition to RECOVERY phase (dazed state) and continue ticking.
* When RECOVERY completes, the ability will move to IDLE and onAbilityComplete() is called.
*/
public void interruptCurrentAbility(DamageSource source, float damage) {
if (currentAbility != null) {
@@ -532,9 +532,10 @@ public class DataAbilities {
NpcAPI.EVENT_BUS.post(interruptEvent);
currentAbility.onInterrupt(npc, source, damage);
currentAbility.interrupt();
currentAbility = null;
lastTarget = null;
currentAbility.interrupt(); // This now transitions to RECOVERY, not IDLE
// Don't clear currentAbility - let it tick through RECOVERY phase
// When RECOVERY ends and phase becomes IDLE, onAbilityComplete() will be called
}
}
@@ -605,7 +606,7 @@ public class DataAbilities {
*/
public void reset() {
stopCurrentAbility();
resetAllCooldowns();
resetCooldown();
// Reset execution state on all abilities
for (Ability ability : abilities) {
@@ -707,6 +708,24 @@ public class DataAbilities {
}
}
/**
* Check if NPC's look direction should be locked to target.
* Only locks during ACTIVE phase when lockMovement is true.
* This allows the NPC to freely track target during WINDUP and RECOVERY.
*/
public boolean shouldLockLookDirection() {
if (currentAbility == null || !currentAbility.isExecuting()) {
return false;
}
// Only lock look direction during ACTIVE phase
if (currentAbility.getPhase() != AbilityPhase.ACTIVE) {
return false;
}
return currentAbility.isLockMovement();
}
/**
* Check if NPC should skip normal attacks during ability execution.
*/
@@ -724,7 +743,8 @@ public class DataAbilities {
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
compound.setBoolean("AbilitiesEnabled", enabled);
compound.setInteger("AbilityGlobalCooldown", globalCooldown);
compound.setInteger("AbilityMinCooldown", minCooldown);
compound.setInteger("AbilityMaxCooldown", maxCooldown);
NBTTagList abilityList = new NBTTagList();
for (Ability ability : abilities) {
@@ -737,8 +757,20 @@ public class DataAbilities {
public void readFromNBT(NBTTagCompound compound) {
enabled = compound.getBoolean("AbilitiesEnabled");
globalCooldown = compound.hasKey("AbilityGlobalCooldown") ?
compound.getInteger("AbilityGlobalCooldown") : 20;
// Support old globalCooldown for backwards compatibility
if (compound.hasKey("AbilityMinCooldown")) {
minCooldown = compound.getInteger("AbilityMinCooldown");
maxCooldown = compound.getInteger("AbilityMaxCooldown");
} else if (compound.hasKey("AbilityGlobalCooldown")) {
// Migrate old single cooldown to min/max range
int oldCooldown = compound.getInteger("AbilityGlobalCooldown");
minCooldown = oldCooldown;
maxCooldown = oldCooldown * 2; // Give some range
} else {
minCooldown = 20;
maxCooldown = 60;
}
abilities.clear();
NBTTagList abilityList = compound.getTagList("Abilities", 10);
@@ -51,7 +51,8 @@ public class GuiNPCAbilities extends GuiNPCInterface2 implements IScrollData, IC
// Settings
private boolean abilitiesEnabled = false;
private int globalCooldown = 20;
private int minCooldown = 20;
private int maxCooldown = 60;
private String search = "";
private int selectedAbilityIndex = -1;
@@ -72,12 +73,18 @@ public class GuiNPCAbilities extends GuiNPCInterface2 implements IScrollData, IC
addLabel(new GuiNpcLabel(100, "gui.enabled", guiLeft + 5, y + 5));
addButton(new GuiNpcButton(100, guiLeft + 55, y, 50, 20, new String[]{"gui.no", "gui.yes"}, abilitiesEnabled ? 1 : 0));
// Global cooldown (spaced out since combat only is removed)
addLabel(new GuiNpcLabel(101, "ability.globalCooldown", guiLeft + 140, y + 5));
GuiNpcTextField cooldownField = new GuiNpcTextField(101, this, fontRendererObj, guiLeft + 230, y, 50, 20, String.valueOf(globalCooldown));
cooldownField.setIntegersOnly();
cooldownField.setMinMaxDefault(0, 10000, 20);
addTextField(cooldownField);
// Cooldown range: Min Cooldown and Max Cooldown
addLabel(new GuiNpcLabel(101, "ability.minCooldown", guiLeft + 115, y + 5));
GuiNpcTextField minField = new GuiNpcTextField(101, this, fontRendererObj, guiLeft + 185, y, 40, 20, String.valueOf(minCooldown));
minField.setIntegersOnly();
minField.setMinMaxDefault(0, 10000, 20);
addTextField(minField);
addLabel(new GuiNpcLabel(102, "ability.maxCooldown", guiLeft + 235, y + 5));
GuiNpcTextField maxField = new GuiNpcTextField(102, this, fontRendererObj, guiLeft + 305, y, 40, 20, String.valueOf(maxCooldown));
maxField.setIntegersOnly();
maxField.setMinMaxDefault(0, 10000, 60);
addTextField(maxField);
y += 28;
@@ -254,7 +261,19 @@ public class GuiNPCAbilities extends GuiNPCInterface2 implements IScrollData, IC
@Override
public void setGuiData(NBTTagCompound compound) {
abilitiesEnabled = compound.getBoolean("AbilitiesEnabled");
globalCooldown = compound.hasKey("AbilityGlobalCooldown") ? compound.getInteger("AbilityGlobalCooldown") : 20;
// Support old globalCooldown for backwards compatibility
if (compound.hasKey("AbilityMinCooldown")) {
minCooldown = compound.getInteger("AbilityMinCooldown");
maxCooldown = compound.getInteger("AbilityMaxCooldown");
} else if (compound.hasKey("AbilityGlobalCooldown")) {
int oldCooldown = compound.getInteger("AbilityGlobalCooldown");
minCooldown = oldCooldown;
maxCooldown = oldCooldown * 2;
} else {
minCooldown = 20;
maxCooldown = 60;
}
npcAbilities.clear();
NBTTagList abilityList = compound.getTagList("Abilities", 10);
@@ -352,7 +371,8 @@ public class GuiNPCAbilities extends GuiNPCInterface2 implements IScrollData, IC
public void save() {
NBTTagCompound compound = new NBTTagCompound();
compound.setBoolean("AbilitiesEnabled", abilitiesEnabled);
compound.setInteger("AbilityGlobalCooldown", globalCooldown);
compound.setInteger("AbilityMinCooldown", minCooldown);
compound.setInteger("AbilityMaxCooldown", maxCooldown);
NBTTagList abilityList = new NBTTagList();
for (Ability ability : npcAbilities) {
@@ -421,7 +441,20 @@ public class GuiNPCAbilities extends GuiNPCInterface2 implements IScrollData, IC
@Override
public void unFocused(GuiNpcTextField textField) {
if (textField.id == 101) {
globalCooldown = textField.getInteger();
minCooldown = textField.getInteger();
// Ensure min <= max
if (minCooldown > maxCooldown) {
maxCooldown = minCooldown;
initGui();
}
save();
} else if (textField.id == 102) {
maxCooldown = textField.getInteger();
// Ensure min <= max
if (maxCooldown < minCooldown) {
minCooldown = maxCooldown;
initGui();
}
save();
}
}
@@ -15,13 +15,18 @@ import noppes.npcs.client.gui.util.SubGuiInterface;
public class SubGuiAbilityLaserShot extends SubGuiAbilityConfig {
private final AbilityLaserShot laser;
private int editingTypeColorId = 0;
private int editingVisualColorId = 0;
public SubGuiAbilityLaserShot(AbilityLaserShot ability, IAbilityConfigCallback callback) {
super(ability, callback);
this.laser = ability;
}
@Override
protected boolean hasVisualSettings() {
return true;
}
@Override
protected void initTypeTab(int startY) {
int y = startY;
@@ -72,21 +77,40 @@ public class SubGuiAbilityLaserShot extends SubGuiAbilityConfig {
addLabel(new GuiNpcLabel(109, "ability.knockbackUp", col2LabelX, y + 5));
addTextField(createFloatField(109, col2FieldX, y, 50, laser.getKnockbackUp()));
}
y += 24;
@Override
protected void initVisualTab(int startY) {
int y = startY;
int labelX = guiLeft + 8;
int fieldX = guiLeft + 85;
int col2LabelX = guiLeft + 180;
int col2FieldX = guiLeft + 260;
// Row 6: Inner Color + Outer Color
addLabel(new GuiNpcLabel(120, "ability.innerColor", labelX, y + 5));
// Row 1: Inner Color + Outer Color
addLabel(new GuiNpcLabel(200, "ability.innerColor", labelX, y + 5));
String innerHex = String.format("%06X", laser.getInnerColor() & 0xFFFFFF);
GuiNpcButton innerColorBtn = new GuiNpcButton(120, fieldX, y, 50, 20, innerHex);
GuiNpcButton innerColorBtn = new GuiNpcButton(200, fieldX, y, 55, 20, innerHex);
innerColorBtn.setTextColor(laser.getInnerColor() & 0xFFFFFF);
addButton(innerColorBtn);
addLabel(new GuiNpcLabel(121, "ability.outerColor", col2LabelX, y + 5));
addLabel(new GuiNpcLabel(201, "ability.outerColor", col2LabelX, y + 5));
String outerHex = String.format("%06X", laser.getOuterColor() & 0xFFFFFF);
GuiNpcButton outerColorBtn = new GuiNpcButton(121, col2FieldX, y, 50, 20, outerHex);
GuiNpcButton outerColorBtn = new GuiNpcButton(201, col2FieldX, y, 55, 20, outerHex);
outerColorBtn.setTextColor(laser.getOuterColor() & 0xFFFFFF);
outerColorBtn.setEnabled(laser.isOuterColorEnabled());
addButton(outerColorBtn);
y += 24;
// Row 2: Outer Color Enabled + Outer Color Width
addLabel(new GuiNpcLabel(202, "ability.outerEnabled", labelX, y + 5));
addButton(new GuiNpcButton(202, fieldX, y, 50, 20, new String[]{"gui.no", "gui.yes"}, laser.isOuterColorEnabled() ? 1 : 0));
addLabel(new GuiNpcLabel(203, "ability.outerWidth", col2LabelX, y + 5));
GuiNpcTextField widthField = createFloatField(203, col2FieldX, y, 55, laser.getOuterColorWidth());
widthField.setEnabled(laser.isOuterColorEnabled());
addTextField(widthField);
}
@Override
@@ -96,34 +120,54 @@ public class SubGuiAbilityLaserShot extends SubGuiAbilityConfig {
case 106:
laser.setExplosive(value == 1);
break;
case 120:
editingTypeColorId = 120;
}
}
@Override
protected void handleVisualButton(int id, GuiNpcButton button) {
int value = button.getValue();
switch (id) {
case 200:
editingVisualColorId = 200;
setSubGui(new SubGuiColorSelector(laser.getInnerColor()));
break;
case 121:
editingTypeColorId = 121;
case 201:
editingVisualColorId = 201;
setSubGui(new SubGuiColorSelector(laser.getOuterColor()));
break;
case 202:
laser.setOuterColorEnabled(value == 1);
initGui();
break;
}
}
@Override
public void subGuiClosed(SubGuiInterface subgui) {
if (subgui instanceof SubGuiColorSelector && editingTypeColorId != 0) {
if (subgui instanceof SubGuiColorSelector && editingVisualColorId != 0) {
SubGuiColorSelector colorSelector = (SubGuiColorSelector) subgui;
int rgb = colorSelector.color & 0x00FFFFFF;
if (editingTypeColorId == 120) {
if (editingVisualColorId == 200) {
laser.setInnerColor(rgb);
} else if (editingTypeColorId == 121) {
} else if (editingVisualColorId == 201) {
laser.setOuterColor(rgb);
}
editingTypeColorId = 0;
editingVisualColorId = 0;
initGui();
} else {
super.subGuiClosed(subgui);
}
}
@Override
protected void handleVisualTextField(int id, GuiNpcTextField field) {
switch (id) {
case 203:
laser.setOuterColorWidth(parseFloat(field, laser.getOuterColorWidth()));
break;
}
}
@Override
protected void handleTypeTextField(int id, GuiNpcTextField field) {
switch (id) {