Skip to content

Commit

Permalink
feat(ai): Add steal characteristic simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent4vx committed Dec 9, 2023
1 parent 20fff4d commit cb3dcc1
Show file tree
Hide file tree
Showing 3 changed files with 308 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/java/fr/quatrevieux/araknemu/game/GameModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
import fr.quatrevieux.araknemu.game.fight.ai.simulation.effect.RemovePointsSimulator;
import fr.quatrevieux.araknemu.game.fight.ai.simulation.effect.SetStateSimulator;
import fr.quatrevieux.araknemu.game.fight.ai.simulation.effect.SpellReturnSimulator;
import fr.quatrevieux.araknemu.game.fight.ai.simulation.effect.StealCharacteristicSimulator;
import fr.quatrevieux.araknemu.game.fight.ai.simulation.effect.StealLifeSimulator;
import fr.quatrevieux.araknemu.game.fight.ai.simulation.effect.SwitchPositionOnAttackSimulator;
import fr.quatrevieux.araknemu.game.fight.builder.ChallengeBuilderFactory;
Expand Down Expand Up @@ -935,6 +936,7 @@ private void configureServices(ContainerConfigurator configurator) {
simulator.register(127, new RemovePointsSimulator(Characteristic.MOVEMENT_POINT, Characteristic.RESISTANCE_MOVEMENT_POINT, 200));

// Characteristics boost
simulator.register(110, new AlterCharacteristicSimulator()); // vitality
simulator.register(112, new AlterCharacteristicSimulator(10)); // damage
simulator.register(115, new AlterCharacteristicSimulator(5)); // critical
simulator.register(117, new AlterCharacteristicSimulator(5)); // sight
Expand All @@ -943,6 +945,7 @@ private void configureServices(ContainerConfigurator configurator) {
simulator.register(122, new AlterCharacteristicSimulator(-5)); // Fail malus
simulator.register(123, new AlterCharacteristicSimulator()); // luck
simulator.register(124, new AlterCharacteristicSimulator()); // wisdom
simulator.register(125, new AlterCharacteristicSimulator()); // vitality
simulator.register(126, new AlterCharacteristicSimulator()); // intelligence
simulator.register(138, new AlterCharacteristicSimulator(2)); // percent damage
simulator.register(178, new AlterCharacteristicSimulator(8)); // heal
Expand All @@ -951,12 +954,14 @@ private void configureServices(ContainerConfigurator configurator) {
simulator.register(607, new AlterCharacteristicSimulator()); // Strength not dispellable
simulator.register(608, new AlterCharacteristicSimulator()); // Luck not dispellable
simulator.register(609, new AlterCharacteristicSimulator()); // Agility not dispellable
simulator.register(610, new AlterCharacteristicSimulator()); // Vitality not dispellable
simulator.register(611, new AlterCharacteristicSimulator()); // Intelligence not dispellable

// Characteristics malus
simulator.register(116, new AlterCharacteristicSimulator(-5)); // sight malus
simulator.register(145, new AlterCharacteristicSimulator(-10)); // -damage
simulator.register(152, new AlterCharacteristicSimulator(-1)); // -luck
simulator.register(153, new AlterCharacteristicSimulator(-1)); // -vitality
simulator.register(154, new AlterCharacteristicSimulator(-1)); // -agility
simulator.register(155, new AlterCharacteristicSimulator(-1)); // -intelligence
simulator.register(156, new AlterCharacteristicSimulator(-1)); // -wisdom
Expand All @@ -965,6 +970,14 @@ private void configureServices(ContainerConfigurator configurator) {
simulator.register(179, new AlterCharacteristicSimulator(-8)); // -heal
simulator.register(186, new AlterCharacteristicSimulator(-2)); // -percent damage

// Steal characteristics
simulator.register(267, new StealCharacteristicSimulator()); // vitality
simulator.register(268, new StealCharacteristicSimulator()); // agility
simulator.register(269, new StealCharacteristicSimulator()); // intelligence
simulator.register(271, new StealCharacteristicSimulator()); // strength
simulator.register(320, new StealCharacteristicSimulator(5)); // sight

// Armors
simulator.register(105, new ArmorSimulator());
simulator.register(106, new SpellReturnSimulator(20));
simulator.register(107, new AlterCharacteristicSimulator(5)); // Reflect damage. Considered as simple characteristic boost to simplify the code
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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-2019 Vincent Quatrevieux
*/

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

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 org.checkerframework.checker.index.qual.Positive;

/**
* Simulator for simple steal characteristic effect
*
* @see fr.quatrevieux.araknemu.game.fight.castable.effect.handler.characteristic.StealCharacteristicHandler The simulated effect handler
*/
public final class StealCharacteristicSimulator implements EffectSimulator {
private final @Positive int multiplier;

/**
* Creates without multiplier
*/
public StealCharacteristicSimulator() {
this(1);
}

/**
* Creates with defining a multiplier
*
* @param multiplier The value multiplier. Must be positive
*/
public StealCharacteristicSimulator(@Positive int multiplier) {
this.multiplier = multiplier;
}

@Override
public void simulate(CastSimulation simulation, AI ai, CastScope.EffectScope<? extends FighterData, ? extends BattlefieldCell> effect) {
final FighterData caster = ai.fighter();
final int duration = Formula.capedDuration(effect.effect().duration());

final double value = (effect.effect().max() < effect.effect().min() ? effect.effect().min() : (double) (effect.effect().min() + effect.effect().max()) / 2)
* multiplier
* duration
;

for (FighterData target : effect.targets()) {
if (target.equals(caster)) {
continue;
}

simulation.addBoost(-value, target);
simulation.addBoost(value, caster);
}
}

@Override
public void score(SpellScore score, SpellEffect effect, Characteristics characteristics) {
final int min = effect.min();
final int max = Math.max(effect.max(), min);
final int value = (min + max) * multiplier / 2;

// Only take in account the malus
score.addDebuff(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
/*
* 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-2019 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 org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

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

class StealCharacteristicSimulatorTest extends FightBaseCase {
private Fight fight;
private PlayerFighter fighter;
private PlayerFighter target;
private FighterAI ai;

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

fight = createFight();
fighter = player.fighter();
target = other.fighter();
ai = new FighterAI(fighter, fight, new NullGenerator());
}

@Test
void simulateSimple() {
StealCharacteristicSimulator simulator = new StealCharacteristicSimulator();

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));

assertEquals(10, simulation.selfBoost());
assertEquals(-10, simulation.enemiesBoost());
}

@Test
void shouldIgnoreSelfTarget() {
StealCharacteristicSimulator simulator = new StealCharacteristicSimulator();

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, fighter.cell());

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

assertEquals(0, simulation.selfBoost());
assertEquals(0, simulation.enemiesBoost());
}

@Test
void simulateBuff() {
StealCharacteristicSimulator simulator = new StealCharacteristicSimulator();

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(20, simulation.selfBoost());
assertEquals(-20, simulation.enemiesBoost());

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(50, simulation.selfBoost());
assertEquals(-50, simulation.enemiesBoost());

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(100, simulation.selfBoost());
assertEquals(-100, simulation.enemiesBoost());

Mockito.when(effect.duration()).thenReturn(-1);
simulation = new CastSimulation(spell, fighter, target.cell());
scope = makeCastScope(fighter, spell, effect, target.cell());
simulator.simulate(simulation, ai, scope.effects().get(0));
assertEquals(100, simulation.selfBoost());
assertEquals(-100, simulation.enemiesBoost());
}

@Test
void simulateArea() {
Fight fight = fightBuilder()
.addSelf(fb -> fb.cell(220))
.addEnemy(fb -> fb.player(other).cell(124))
.addEnemy(fb -> fb.cell(125))
.build(true)
;
fight.nextState();

fighter = player.fighter();

ai = new FighterAI(fighter, fight, new NullGenerator());

StealCharacteristicSimulator simulator = new StealCharacteristicSimulator();

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, fight.map().get(124));

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

assertEquals(20, simulation.selfBoost());
assertEquals(-20, simulation.enemiesBoost());
}

@Test
void scorePositive() {
StealCharacteristicSimulator simulator = new StealCharacteristicSimulator();

SpellEffect effect = Mockito.mock(SpellEffect.class);
Spell spell = Mockito.mock(Spell.class);
SpellConstraints constraints = Mockito.mock(SpellConstraints.class);

Mockito.when(effect.min()).thenReturn(2);
Mockito.when(effect.max()).thenReturn(3);
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, fighter.characteristics());

assertEquals(2.0, score.score());
assertEquals(2.0, score.debuff());
}
}

0 comments on commit cb3dcc1

Please sign in to comment.