Skip to content

Commit c031c72

Browse files
authored
Merge pull request RobertSkalko#4 from TUsama/new-spell-fix
carefully handle decimal tick
2 parents 2d32d85 + 7976e70 commit c031c72

4 files changed

Lines changed: 57 additions & 18 deletions

File tree

src/main/java/com/robertx22/mine_and_slash/database/data/spells/components/Spell.java

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -157,28 +157,29 @@ public WeaponTypes getWeapon(LivingEntity en) {
157157
}
158158

159159
public final void onCastingTick(SpellCastContext ctx) {
160-
int timesToCast = (int) ctx.spell.getConfig().times_to_cast;
161-
if (timesToCast > 1) {
162-
int castTimeTicks = (int) getCastTimeTicks(ctx);
160+
161+
int timesToCast = ctx.spell.getConfig().times_to_cast;
162+
if (timesToCast > 1){
163+
SpellCastInfo castInfo = getCastInfo(ctx);
164+
163165
// if i didnt do this then cast time reduction would reduce amount of spell hits.
164-
int castEveryXTicks = castTimeTicks / timesToCast;
165-
if (timesToCast > 1) {
166-
if (castEveryXTicks < 1) {
167-
castEveryXTicks = 1;
168-
}
169-
}
170-
if (ctx.ticksInUse > 0 && ctx.ticksInUse % castEveryXTicks == 0) {
166+
167+
168+
if (ctx.ticksInUse > 0 && castInfo.castInThisTick(ctx.ticksInUse)) {
171169
this.cast(ctx);
172170
}
171+
173172
} else if (timesToCast < 1) {
174173
ExileLog.get().warn("Times to cast spell is: " + timesToCast + " . this seems like a bug.");
175174
}
175+
176+
ctx.castedThisTick = true;
176177
}
177178

178179
public void cast(SpellCastContext ctx) {
179180
LivingEntity caster = ctx.caster;
180-
ctx.castedThisTick = true;
181-
/*
181+
182+
/*
182183
if (MMORPG.RUN_DEV_TOOLS_REMOVE_WHEN_DONE && this.config.swing_arm) {
183184
// caster.swingTime = -1; // this makes sure hand swings
184185
// caster.swing(InteractionHand.MAIN_HAND);
@@ -195,9 +196,32 @@ public final int getChargeCooldownTicks(SpellCastContext ctx) {
195196
return (int) ctx.event.data.getNumber(EventData.CHARGE_COOLDOWN_TICKS).number;
196197
}
197198

198-
public final int getCastTimeTicks(SpellCastContext ctx) {
199+
public SpellCastInfo getCastInfo(SpellCastContext ctx) {
199200
// if it casts 5 times a cast, it should take at least 5 ticks to cast it
200-
return MathHelper.clamp((int) ctx.event.data.getNumber(EventData.CAST_TICKS).number, config.times_to_cast, 10000);
201+
// since Robert don't want spell can be cast multiple times in a single tick, then we have to handle the decimal cast tick carefully
202+
float clamp = MathHelper.clamp(ctx.event.data.getNumber(EventData.CAST_TICKS).number, config.times_to_cast * 1f, 10000f);
203+
int timesToCast = ctx.spell.getConfig().times_to_cast;
204+
float singleTimeCost = clamp / timesToCast;
205+
int[] points = new int[timesToCast];
206+
//means a single cast cost decimal tick
207+
if (singleTimeCost % 1 != 0) {
208+
for (int i = 0; i < timesToCast; i++) {
209+
float v = singleTimeCost * (i + 1);
210+
//means the result time is integer at some situation, like 2.5 * 4 ticks.
211+
if (v % 1 == 0){
212+
points[i] = ((int) v);
213+
} else {
214+
points[i] = ((int) Math.ceil(singleTimeCost * (i + 1)));
215+
}
216+
217+
}
218+
} else {
219+
for (int i = 0; i < timesToCast; i++) {
220+
points[i] = ((int) singleTimeCost) * (i + 1);
221+
}
222+
223+
}
224+
return new SpellCastInfo(points[points.length - 1], points);
201225
}
202226

203227
@Override
@@ -272,12 +296,12 @@ public final List<Component> GetTooltipString(StatRangeInfo info) {
272296
list.add(Words.COOLDOWN.locName(getCooldownTicks(ctx) / 20).withStyle(ChatFormatting.YELLOW));
273297
}
274298

275-
int casttime = getCastTimeTicks(ctx);
299+
int casttime = getCastInfo(ctx).castTime();
276300

277301
if (casttime == 0) {
278302
list.add(Words.INSTANT_CAST.locName().withStyle(ChatFormatting.GREEN));
279303
} else {
280-
list.add(Words.CAST_TIME.locName(casttime / 20).withStyle(ChatFormatting.GREEN));
304+
list.add(Words.CAST_TIME.locName(Math.round(casttime / 20.0f * 100f) / 100f).withStyle(ChatFormatting.GREEN));
281305
}
282306

283307
Set<String> radiuses = new LinkedHashSet<>();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.robertx22.mine_and_slash.database.data.spells.components;
2+
3+
import java.util.TreeSet;
4+
5+
public record SpellCastInfo(int castTime, int[] castPoint) {
6+
7+
//for quick search
8+
public boolean castInThisTick( int target) {
9+
for (int n : castPoint) {
10+
if (n == target) return true;
11+
}
12+
return false;
13+
}
14+
}

src/main/java/com/robertx22/mine_and_slash/saveclasses/spells/SpellCastingData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import com.robertx22.mine_and_slash.database.data.spells.entities.CalculatedSpellData;
1414
import com.robertx22.mine_and_slash.database.data.spells.spell_classes.CastingWeapon;
1515
import com.robertx22.mine_and_slash.database.data.spells.spell_classes.bases.SpellCastContext;
16-
import com.robertx22.mine_and_slash.database.data.spells.spell_classes.bases.SpellPredicates;
1716
import com.robertx22.mine_and_slash.database.data.stats.types.LearnSpellStat;
1817
import com.robertx22.mine_and_slash.database.data.stats.types.MaxAllSpellLevels;
1918
import com.robertx22.mine_and_slash.database.data.stats.types.MaxSpellLevel;
@@ -427,7 +426,8 @@ public List<String> getSpellsOnCooldown(LivingEntity en) {
427426
public void setToCast(SpellCastContext ctx) {
428427

429428
this.calcSpell = ctx.calcData;
430-
this.castTickLeft = ctx.spell.getCastTimeTicks(ctx);
429+
430+
this.castTickLeft = ctx.spell.getCastInfo(ctx).castTime();
431431
this.spellTotalCastTicks = this.castTickLeft;
432432
this.castTicksDone = 0;
433433
this.casting = true;

src/main/java/com/robertx22/mine_and_slash/uncommon/effectdatas/rework/action/DecreaseNumberByPercentEffect.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public DecreaseNumberByPercentEffect(String num) {
2121
@Override
2222
public void activate(EffectEvent event, EffectSides statSource, StatData data, Stat stat) {
2323
event.data.getNumber(num_id).number -= event.data.getOriginalNumber(num_id).number * data.getValue() / 100F;
24+
event.data.getNumber(num_id).number = Math.round(event.data.getNumber(num_id).number * 100.0f) / 100.0f;
2425
}
2526

2627
@Override

0 commit comments

Comments
 (0)