Skip to content

Commit

Permalink
feat(ai): Add fixed steal life simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent4vx committed Dec 16, 2023
1 parent f966e07 commit a581c6f
Show file tree
Hide file tree
Showing 3 changed files with 281 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/fr/quatrevieux/araknemu/game/GameModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
import fr.quatrevieux.araknemu.game.fight.ai.simulation.effect.DamageSimulator;
import fr.quatrevieux.araknemu.game.fight.ai.simulation.effect.FixedCasterDamage;
import fr.quatrevieux.araknemu.game.fight.ai.simulation.effect.FixedDamageSimulator;
import fr.quatrevieux.araknemu.game.fight.ai.simulation.effect.FixedStealLifeSimulator;
import fr.quatrevieux.araknemu.game.fight.ai.simulation.effect.GivePercentLifeSimulator;
import fr.quatrevieux.araknemu.game.fight.ai.simulation.effect.HealOrMultiplyDamageSimulator;
import fr.quatrevieux.araknemu.game.fight.ai.simulation.effect.HealSimulator;
Expand Down Expand Up @@ -925,6 +926,7 @@ private void configureServices(ContainerConfigurator configurator) {
simulator.register(99, new DamageSimulator(Element.FIRE));
simulator.register(100, new DamageSimulator(Element.NEUTRAL));

simulator.register(82, new FixedStealLifeSimulator());
simulator.register(131, new DamageOnActionPointUseSimulator());
simulator.register(144, new FixedDamageSimulator());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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-2023 Vincent Quatrevieux
*/

package fr.quatrevieux.araknemu.game.fight.ai.simulation.effect;

import fr.arakne.utils.value.Interval;
import fr.quatrevieux.araknemu.game.fight.ai.AI;
import fr.quatrevieux.araknemu.game.fight.ai.simulation.CastSimulation;
import fr.quatrevieux.araknemu.game.fight.ai.simulation.SpellScore;
import fr.quatrevieux.araknemu.game.fight.ai.simulation.effect.util.Formula;
import fr.quatrevieux.araknemu.game.fight.castable.CastScope;
import fr.quatrevieux.araknemu.game.fight.fighter.FighterData;
import fr.quatrevieux.araknemu.game.fight.map.BattlefieldCell;
import fr.quatrevieux.araknemu.game.spell.effect.SpellEffect;
import fr.quatrevieux.araknemu.game.world.creature.characteristics.Characteristics;
import fr.quatrevieux.araknemu.util.Asserter;
import org.checkerframework.checker.index.qual.GTENegativeOne;

import java.util.Collection;

/**
* Simulator for fixed steal life effect
*
* @see fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage.FixedStealLifeHandler The simulated effect
*/
public final class FixedStealLifeSimulator implements EffectSimulator {
@Override
public void simulate(CastSimulation simulation, AI ai, CastScope.EffectScope<? extends FighterData, ? extends BattlefieldCell> effect) {
final SpellEffect spellEffect = effect.effect();
final Interval damage = damage(spellEffect);
final FighterData caster = simulation.caster();

if (spellEffect.duration() == 0) {
simulateDamage(simulation, caster, damage, effect.targets());
} else {
simulatePoison(simulation, caster, damage, spellEffect.duration(), effect.targets());
}
}

@Override
public void score(SpellScore score, SpellEffect effect, Characteristics characteristics) {
score.addDamage(Asserter.castNonNegative((int) damage(effect).average()));
}

private Interval damage(SpellEffect effect) {
return Interval.of(effect.min(), Math.max(effect.max(), effect.min()));
}

private void simulatePoison(CastSimulation simulation, FighterData caster, Interval damage, @GTENegativeOne int duration, Collection<? extends FighterData> targets) {
final int capedDuration = Formula.capedDuration(duration);

for (FighterData target : targets) {
simulation.addPoison(damage, capedDuration, target);
simulation.addHealBuff(damage, capedDuration, caster);
}
}

private void simulateDamage(CastSimulation simulation, FighterData caster, Interval damage, Collection<? extends FighterData> targets) {
for (FighterData target : targets) {
simulation.addDamage(damage, target);
simulation.addHeal(damage, caster);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*
* 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-2023 Vincent Quatrevieux
*/

package fr.quatrevieux.araknemu.game.fight.ai.simulation.effect;

import fr.quatrevieux.araknemu.data.value.EffectArea;
import fr.quatrevieux.araknemu.game.fight.Fight;
import fr.quatrevieux.araknemu.game.fight.FightBaseCase;
import fr.quatrevieux.araknemu.game.fight.ai.FighterAI;
import fr.quatrevieux.araknemu.game.fight.ai.action.logic.NullGenerator;
import fr.quatrevieux.araknemu.game.fight.ai.simulation.CastSimulation;
import fr.quatrevieux.araknemu.game.fight.ai.simulation.SpellScore;
import fr.quatrevieux.araknemu.game.fight.castable.CastScope;
import fr.quatrevieux.araknemu.game.fight.fighter.Fighter;
import fr.quatrevieux.araknemu.game.fight.fighter.player.PlayerFighter;
import fr.quatrevieux.araknemu.game.fight.map.FightCell;
import fr.quatrevieux.araknemu.game.spell.Spell;
import fr.quatrevieux.araknemu.game.spell.SpellConstraints;
import fr.quatrevieux.araknemu.game.spell.effect.SpellEffect;
import fr.quatrevieux.araknemu.game.spell.effect.area.CellArea;
import fr.quatrevieux.araknemu.game.spell.effect.area.CircleArea;
import fr.quatrevieux.araknemu.game.spell.effect.target.SpellEffectTarget;
import fr.quatrevieux.araknemu.game.world.creature.characteristics.DefaultCharacteristics;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import static org.junit.jupiter.api.Assertions.*;

class FixedStealLifeSimulatorTest extends FightBaseCase {
private Fight fight;
private PlayerFighter fighter;
private Fighter target;
private FighterAI ai;
private FixedStealLifeSimulator simulator;

@Override
@BeforeEach
public void setUp() throws Exception {
super.setUp();

fight = createFight();
fighter = player.fighter();
target = other.fighter();

target.init();
fighter.init();

target.life().alterMax(target, 1000);
fighter.life().alter(fighter, -50);

ai = new FighterAI(fighter, fight, new NullGenerator());
simulator = new FixedStealLifeSimulator();
}

@Test
void simulateSimple() {
assertEquals(-10, simulate().enemiesLife());
assertEquals(10, simulate().selfLife());
}

@Test
void simulateBuff() {
SpellEffect effect = Mockito.mock(SpellEffect.class);
Spell spell = Mockito.mock(Spell.class);
SpellConstraints constraints = Mockito.mock(SpellConstraints.class);

Mockito.when(effect.min()).thenReturn(10);
Mockito.when(effect.area()).thenReturn(new CellArea());
Mockito.when(effect.target()).thenReturn(SpellEffectTarget.DEFAULT);
Mockito.when(effect.duration()).thenReturn(2);
Mockito.when(spell.constraints()).thenReturn(constraints);
Mockito.when(constraints.freeCell()).thenReturn(false);

CastSimulation simulation = new CastSimulation(spell, fighter, target.cell());

CastScope<Fighter, FightCell> scope = makeCastScope(fighter, spell, effect, target.cell());
simulator.simulate(simulation, ai, scope.effects().get(0));

assertEquals(-15.0, simulation.enemiesLife());
assertEquals(10.0, simulation.selfLife());
assertEquals(7.5, simulation.selfBoost());

Mockito.when(effect.duration()).thenReturn(5);
simulation = new CastSimulation(spell, fighter, target.cell());
scope = makeCastScope(fighter, spell, effect, target.cell());
simulator.simulate(simulation, ai, scope.effects().get(0));
assertEquals(-37.5, simulation.enemiesLife());
assertEquals(10, simulation.selfLife());
assertEquals(30, simulation.selfBoost());

Mockito.when(effect.duration()).thenReturn(20);
simulation = new CastSimulation(spell, fighter, target.cell());
scope = makeCastScope(fighter, spell, effect, target.cell());
simulator.simulate(simulation, ai, scope.effects().get(0));
assertEquals(-75.0, simulation.enemiesLife());
assertEquals(10.0, simulation.selfLife());
assertEquals(67.5, simulation.selfBoost());
}

@Test
void simulateInfiniteBuff() {
SpellEffect effect = Mockito.mock(SpellEffect.class);
Spell spell = Mockito.mock(Spell.class);
SpellConstraints constraints = Mockito.mock(SpellConstraints.class);

Mockito.when(effect.min()).thenReturn(10);
Mockito.when(effect.area()).thenReturn(new CellArea());
Mockito.when(effect.target()).thenReturn(SpellEffectTarget.DEFAULT);
Mockito.when(effect.duration()).thenReturn(-1);
Mockito.when(spell.constraints()).thenReturn(constraints);
Mockito.when(constraints.freeCell()).thenReturn(false);

CastSimulation simulation = new CastSimulation(spell, fighter, target.cell());

CastScope<Fighter, FightCell> scope = makeCastScope(fighter, spell, effect, target.cell());
simulator.simulate(simulation, ai, scope.effects().get(0));

assertEquals(-75.0, simulation.enemiesLife());
assertEquals(10.0, simulation.selfLife());
assertEquals(67.5, simulation.selfBoost());
}

@Test
void simulateArea() {
SpellEffect effect = Mockito.mock(SpellEffect.class);
Spell spell = Mockito.mock(Spell.class);
SpellConstraints constraints = Mockito.mock(SpellConstraints.class);

Mockito.when(effect.min()).thenReturn(10);
Mockito.when(effect.area()).thenReturn(new CircleArea(new EffectArea(EffectArea.Type.CIRCLE, 10)));
Mockito.when(effect.target()).thenReturn(SpellEffectTarget.DEFAULT);
Mockito.when(spell.constraints()).thenReturn(constraints);
Mockito.when(constraints.freeCell()).thenReturn(false);

CastSimulation simulation = new CastSimulation(spell, fighter, other.fighter().cell());

CastScope<Fighter, FightCell> scope = makeCastScope(fighter, spell, effect, other.fighter().cell());
simulator.simulate(simulation, ai, scope.effects().get(0));

assertEquals(10, simulation.selfLife());
assertEquals(-10, simulation.enemiesLife());
}

@Test
void score() {
SpellEffect effect = Mockito.mock(SpellEffect.class);
Spell spell = Mockito.mock(Spell.class);
SpellConstraints constraints = Mockito.mock(SpellConstraints.class);

Mockito.when(effect.min()).thenReturn(10);
Mockito.when(effect.max()).thenReturn(15);
Mockito.when(effect.area()).thenReturn(new CellArea());
Mockito.when(effect.target()).thenReturn(SpellEffectTarget.DEFAULT);
Mockito.when(spell.constraints()).thenReturn(constraints);
Mockito.when(constraints.freeCell()).thenReturn(false);

SpellScore score = new SpellScore();
simulator.score(score, effect, new DefaultCharacteristics());

assertEquals(12, score.score());
assertEquals(12, score.damage());
}

private CastSimulation simulate() {
SpellEffect effect = Mockito.mock(SpellEffect.class);
Spell spell = Mockito.mock(Spell.class);
SpellConstraints constraints = Mockito.mock(SpellConstraints.class);

Mockito.when(effect.min()).thenReturn(10);
Mockito.when(effect.area()).thenReturn(new CellArea());
Mockito.when(effect.target()).thenReturn(SpellEffectTarget.DEFAULT);
Mockito.when(spell.constraints()).thenReturn(constraints);
Mockito.when(constraints.freeCell()).thenReturn(false);

CastSimulation simulation = new CastSimulation(spell, fighter, target.cell());

CastScope<Fighter, FightCell> scope = makeCastScope(fighter, spell, effect, target.cell());
simulator.simulate(simulation, ai, scope.effects().get(0));

return simulation;
}
}

0 comments on commit a581c6f

Please sign in to comment.