Skip to content

Commit

Permalink
[fight] #27 Add reflect damage
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent4vx committed Dec 4, 2021
1 parent 9efdb1e commit e61b0dc
Show file tree
Hide file tree
Showing 26 changed files with 1,009 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import fr.quatrevieux.araknemu.game.fight.castable.CastScope;
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage.Damage;
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage.ReflectedDamage;
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;
import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter;

Expand Down Expand Up @@ -139,7 +140,18 @@ public default void onBuffDamage(Buff buff, Buff poison, Damage value) {
* @param buff The active buff
* @param value Altered life value. Negative for a damage, positive for a heal
*
* @see fr.quatrevieux.araknemu.game.fight.fighter.FighterLife#alter(fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter, int)
* @see fr.quatrevieux.araknemu.game.fight.fighter.FighterLife#alter(fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter, int)
*/
public default void onLifeAltered(Buff buff, int value) {}

/**
* Damage has been reflected by the cast target
*
* The target can be changed using {@link ReflectedDamage#changeTarget(PassiveFighter)}
* Or modified using {@link ReflectedDamage#multiply(int)}
*
* @param buff The active buff
* @param damage The reflected damage
*/
public default void onReflectedDamage(Buff buff, ReflectedDamage damage) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import fr.quatrevieux.araknemu.game.fight.castable.CastScope;
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage.Damage;
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage.ReflectedDamage;
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;
import fr.quatrevieux.araknemu.game.fight.fighter.Fighter;
import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter;
Expand Down Expand Up @@ -123,6 +124,13 @@ public void onLifeAltered(int value) {
}
}

@Override
public void onReflectedDamage(ReflectedDamage damage) {
for (Buff buff : buffs) {
buff.hook().onReflectedDamage(buff, damage);
}
}

@Override
public void refresh() {
removeIf(buff -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import fr.quatrevieux.araknemu.game.fight.castable.CastScope;
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage.Damage;
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage.ReflectedDamage;
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;
import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter;

Expand Down Expand Up @@ -60,6 +61,11 @@ public interface Buffs extends Iterable<Buff> {
*/
public void onLifeAltered(int value);

/**
* @see BuffHook#onReflectedDamage(Buff, ReflectedDamage)
*/
public void onReflectedDamage(ReflectedDamage damage);

/**
* @see BuffHook#onStartTurn(Buff)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.BuffHook;
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.EffectHandler;
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage.Damage;
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage.MultipliableDamage;
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage.ReflectedDamage;
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;
import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter;

/**
* Suffered damage will be healed, or multiplied
*
* Note: this buff is only applied on direct damage (see: https://forums.jeuxonline.info/sujet/969716/chance-d-ecaflip)
*/
public final class HealOrMultiplyDamageHandler implements EffectHandler, BuffHook {
private final RandomUtil random = new RandomUtil();
Expand All @@ -46,13 +51,25 @@ public void buff(CastScope cast, CastScope.EffectScope effect) {
}

@Override
public void onDamage(Buff buff, Damage value) {
public void onDirectDamage(Buff buff, ActiveFighter caster, Damage value) {
apply(buff, value);
}

@Override
public void onReflectedDamage(Buff buff, ReflectedDamage damage) {
apply(buff, damage);
}

/**
* Modify the damage multiplier depending on the chance
*/
private void apply(Buff buff, MultipliableDamage damage) {
final boolean heal = random.bool(buff.effect().special());

if (heal) {
value.multiply(-buff.effect().max());
damage.multiply(-buff.effect().max());
} else {
value.multiply(buff.effect().min());
damage.multiply(buff.effect().min());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* This file is part of Araknemu.
*
* Araknemu is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Araknemu is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Araknemu. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright (c) 2017-2021 Vincent Quatrevieux
*/

package fr.quatrevieux.araknemu.game.fight.castable.effect.handler.armor;

import fr.quatrevieux.araknemu.data.constant.Characteristic;
import fr.quatrevieux.araknemu.game.fight.castable.CastScope;
import fr.quatrevieux.araknemu.game.fight.castable.effect.EffectValue;
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.Buff;
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.BuffHook;
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.EffectHandler;
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage.Damage;
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;
import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter;

/**
* Reflect suffered damage
* Unlike item effect, this one is modified by wisdom
*
* Reflected damage cannot exceed half of cast damage, and is lowered by resistance
*
* @see fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage.ReflectedDamage For the formula
*/
public final class ReflectDamageHandler implements EffectHandler, BuffHook {
@Override
public void handle(CastScope cast, CastScope.EffectScope effect) {
throw new UnsupportedOperationException("ReflectDamageHandler can only be used as buff");
}

@Override
public void buff(CastScope cast, CastScope.EffectScope effect) {
for (PassiveFighter target : effect.targets()) {
target.buffs().add(new Buff(effect.effect(), cast.action(), cast.caster(), target, this));
}
}

@Override
public void onDirectDamage(Buff buff, ActiveFighter caster, Damage value) {
// Ignore self damage
if (!caster.equals(buff.target())) {
value.reflect(
new EffectValue(buff.effect())
.percent(buff.target().characteristics().get(Characteristic.WISDOM))
.value()
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
* Formula :
* (value * percent / 100 - fixed - reduce) * multiply
*/
public final class Damage {
public final class Damage implements MultipliableDamage {
private final int value;
private final Element element;

private int multiply = 1;
private int fixed = 0;
private int percent = 100;
private int reduce = 0;
private int returned = 0;

public Damage(int value, Element element) {
this.value = value;
Expand All @@ -48,9 +49,7 @@ public Element element() {
return element;
}

/**
* Compute the value
*/
@Override
public int value() {
final int base = (value * percent / 100 - fixed - reduce);

Expand All @@ -67,10 +66,10 @@ public int value() {
public Damage percent(int percent) {
if (percent > this.percent) {
this.percent = 0;
} else {
this.percent -= percent;
}

this.percent -= percent;

return this;
}

Expand All @@ -83,9 +82,7 @@ public Damage fixed(int fixed) {
return this;
}

/**
* Multiply suffered damage
*/
@Override
public Damage multiply(int factor) {
this.multiply = factor;

Expand All @@ -101,10 +98,26 @@ public Damage reduce(int value) {
return this;
}

/**
* Add reflected damage
*/
public Damage reflect(int value) {
this.returned += value;

return this;
}

/**
* Get the damage reduction value from armor buff effects
*/
public int reducedDamage() {
return reduce;
}

/**
* How much damage has been reflected by the target ?
*/
public int reflectedDamage() {
return returned;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ public DamageApplier(Element element, Fight fight) {
*/
public int apply(ActiveFighter caster, SpellEffect effect, PassiveFighter target) {
final Damage damage = computeDamage(caster, effect, target);

target.buffs().onDirectDamage(caster, damage);

if (!caster.equals(target)) {
damage.reflect(target.characteristics().get(Characteristic.COUNTER_DAMAGE));
}

return applyDamage(caster, damage, target);
}

Expand Down Expand Up @@ -112,8 +117,32 @@ private int applyDamage(ActiveFighter caster, Damage damage, PassiveFighter targ
fight.send(ActionEffect.reducedDamage(target, damage.reducedDamage()));
}

// @todo returned damage
final int lifeChange = target.life().alter(caster, -damage.value());

if (lifeChange < 0 && !target.equals(caster) && damage.reflectedDamage() > 0) {
applyReflectedDamage(target, caster, damage);
}

return lifeChange;
}

return target.life().alter(caster, -damage.value());
/**
* Apply returned damage on the original caster
*
* Notes:
* - do not handle target change chain
* - use resistance on returned damage ?
*
* @param caster The original spell caster
* @param damage The applied damage
*/
private void applyReflectedDamage(PassiveFighter castTarget, ActiveFighter caster, Damage damage) {
final ReflectedDamage returnedDamage = new ReflectedDamage(damage, caster);
caster.buffs().onReflectedDamage(returnedDamage);

if (returnedDamage.baseValue() > 0) {
fight.send(ActionEffect.reflectedDamage(castTarget, returnedDamage.baseValue()));
returnedDamage.target().life().alter(castTarget, -returnedDamage.value());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* This file is part of Araknemu.
*
* Araknemu is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Araknemu is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Araknemu. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright (c) 2017-2021 Vincent Quatrevieux
*/

package fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage;

import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter;

/**
* Base type for damage object which can be multiplied
*/
public interface MultipliableDamage {
/**
* Compute the final damage value, with multiplier applied
*
* @return The damage value. If negative, the damage is transformed to heal.
* If positive the number of life point to remove
*
* @see fr.quatrevieux.araknemu.game.fight.fighter.FighterLife#alter(PassiveFighter, int)
*/
public int value();

/**
* Multiply the applied damage
*
* @param factor The multiplication factor. Can be negative for change damage to heal
*
* @return The current instance
*/
public MultipliableDamage multiply(int factor);
}
Loading

0 comments on commit e61b0dc

Please sign in to comment.