-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[AI] Implements missing effects simulators #308
Merged
vincent4vx
merged 22 commits into
Arakne:master
from
vincent4vx:feature-ai-implements-missing-effects
Dec 26, 2023
Merged
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
72b8b06
feat(ai): Handle invocation effect
vincent4vx c371ee7
feat(ai): Handle double effect
vincent4vx 0c2c7c6
feat(ai): Handle move back simulator
vincent4vx d9b528e
feat(ai): Handle sacrifice simulator
vincent4vx 8ade684
fix(ai): Register sacrifice simulator
vincent4vx 8fe44a4
feat(ai): Handle heal or multiply damage simulator
vincent4vx fd73c80
feat(ai): Handle attraction effect
vincent4vx 3009136
feat(ai): Add giver percent life simulator
vincent4vx 20fff4d
feat(ai): Add return spell simulator
vincent4vx cb3dcc1
feat(ai): Add steal characteristic simulator
vincent4vx c95204c
feat(ai): Add damage on action point use simulator
vincent4vx f966e07
feat(ai): Add fixed damage simulator
vincent4vx a581c6f
feat(ai): Add fixed steal life simulator
vincent4vx 686f832
feat(ai): Add percent life damage simulator
vincent4vx a5c9be3
ci(checkerframework): add a cast to NonNegative
vincent4vx 318a0d3
feat(ai): Add percent life lost damage simulator
vincent4vx 291ab52
feat(ai): Add fixed heal simulator
vincent4vx ee11cab
feat(ai): Add kill simulator
vincent4vx 3aae063
feat(ai): Add invisibility simulator
vincent4vx 7efe968
feat(ai): Add skip turn simulator
vincent4vx 0a55f13
feat(ai): Add multiply damage simulator
vincent4vx 8f1886e
feat(ai): Add add characteristic on damage simulator
vincent4vx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/main/java/fr/quatrevieux/araknemu/game/fight/ai/action/Invoke.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* 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.action; | ||
|
||
import fr.quatrevieux.araknemu.game.fight.ai.AI; | ||
import fr.quatrevieux.araknemu.game.fight.ai.action.util.CastSpell; | ||
import fr.quatrevieux.araknemu.game.fight.ai.simulation.CastSimulation; | ||
import fr.quatrevieux.araknemu.game.fight.ai.simulation.Simulator; | ||
import fr.quatrevieux.araknemu.game.fight.fighter.FighterData; | ||
import fr.quatrevieux.araknemu.game.fight.team.Team; | ||
import fr.quatrevieux.araknemu.game.fight.turn.action.Action; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
import org.checkerframework.checker.nullness.util.NullnessUtil; | ||
|
||
import java.util.Iterator; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Invoke a creature | ||
* | ||
* This generator will prioritize healing when allies are low life. | ||
* It will also prioritize killing enemies. | ||
*/ | ||
public final class Invoke implements ActionGenerator, CastSpell.SimulationSelector { | ||
private final CastSpell cast; | ||
private @Nullable AI ai; | ||
private double alliesLifeRatio = 1.0; | ||
|
||
@SuppressWarnings({"assignment", "argument"}) | ||
public Invoke(Simulator simulator) { | ||
this.cast = new CastSpell(simulator, this); | ||
} | ||
|
||
@Override | ||
public void initialize(AI ai) { | ||
this.ai = ai; | ||
this.alliesLifeRatio = computeAlliesLifeRatio(ai); | ||
} | ||
|
||
@Override | ||
public <A extends Action> Optional<A> generate(AI ai, AiActionFactory<A> actions) { | ||
return cast.generate(ai, actions); | ||
} | ||
|
||
@Override | ||
public boolean valid(CastSimulation simulation) { | ||
return simulation.invocation() > 0 | ||
&& simulation.killedAllies() == 0 | ||
&& simulation.suicideProbability() == 0 | ||
; | ||
} | ||
|
||
@Override | ||
public double score(CastSimulation simulation) { | ||
double score = simulation.invocation(); | ||
|
||
// Can kill an enemy : increase invocation score by the max life of the enemy | ||
if (simulation.killedEnemies() >= 0.99) { | ||
score += NullnessUtil.castNonNull(ai).enemy().map(fighter -> fighter.life().max()).orElse(0); | ||
} | ||
|
||
// Prioritize healing when allies are low life | ||
score += (2 - alliesLifeRatio) * (simulation.alliesLife() + simulation.selfLife()); | ||
|
||
// Add direct damage to enemies | ||
score -= simulation.enemiesLife(); | ||
|
||
return score / simulation.actionPointsCost(); | ||
} | ||
|
||
private double computeAlliesLifeRatio(AI ai) { | ||
final Team<?> team = ai.fighter().team(); | ||
|
||
double totalCurrentLife = 0; | ||
double totalMaxLife = 0; | ||
|
||
for (Iterator<? extends FighterData> it = ai.fighters().iterator(); it.hasNext();) { | ||
final FighterData fighter = it.next(); | ||
|
||
if (fighter.team().equals(team)) { | ||
totalCurrentLife += fighter.life().current(); | ||
totalMaxLife += fighter.life().max(); | ||
} | ||
} | ||
|
||
if (totalMaxLife == 0) { | ||
return 1.0; | ||
} | ||
|
||
return totalCurrentLife / totalMaxLife; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,7 @@ public final class CastSimulation { | |
private double enemiesBoost; | ||
private double alliesBoost; | ||
private double selfBoost; | ||
private double invocation; | ||
|
||
private double killedAllies; | ||
private double killedEnemies; | ||
|
@@ -231,6 +232,13 @@ public double selfBoost() { | |
return selfBoost; | ||
} | ||
|
||
/** | ||
* The score of the invoked creature | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Score ? |
||
*/ | ||
public double invocation() { | ||
return invocation; | ||
} | ||
|
||
/** | ||
* Add a boost to the target | ||
* | ||
|
@@ -246,6 +254,15 @@ public double boost() { | |
}, target); | ||
} | ||
|
||
/** | ||
* Add an invocation score | ||
* | ||
* @param score The score of the invoked creature | ||
*/ | ||
public void addInvocation(double score) { | ||
invocation += score; | ||
} | ||
|
||
/** | ||
* Get the simulated spell caster | ||
*/ | ||
|
@@ -302,6 +319,7 @@ public void merge(CastSimulation simulation, double percent) { | |
suicide += simulation.suicide * percent / 100d; | ||
|
||
actionPointsModifier += simulation.actionPointsModifier * percent / 100d; | ||
invocation += simulation.invocation * percent / 100d; | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never test ? (Code coverage report)