| 1 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  * This file is part of Araknemu. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Araknemu is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * it under the terms of the GNU Lesser General Public License as published by | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Araknemu is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * GNU Lesser General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * You should have received a copy of the GNU Lesser General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * along with Araknemu.  If not, see <https://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Copyright (c) 2017-2022 Vincent Quatrevieux | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | package fr.quatrevieux.araknemu.game.fight.ai.action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | import fr.quatrevieux.araknemu.game.fight.ai.AI; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | import fr.quatrevieux.araknemu.game.fight.ai.action.util.CastSpell; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | import fr.quatrevieux.araknemu.game.fight.ai.simulation.CastSimulation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | import fr.quatrevieux.araknemu.game.fight.ai.simulation.Simulator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | import fr.quatrevieux.araknemu.game.fight.turn.action.Action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | import fr.quatrevieux.araknemu.game.fight.turn.action.factory.ActionsFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | import java.util.Optional; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * Try to heal allies | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * Select spells which heal allies or self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * All cells are tested for select the most effective target for each spells | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | public final class Heal<F extends ActiveFighter> implements ActionGenerator<F>, CastSpell.SimulationSelector { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     private final CastSpell<F> generator; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     @SuppressWarnings({"assignment", "argument"}) | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 1 |  |     public Heal(Simulator simulator) { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 | 1 |  |         this.generator = new CastSpell<>(simulator, this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     @Override | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public void initialize(AI<F> ai) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |         generator.initialize(ai); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     @Override | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public Optional<Action> generate(AI<F> ai, ActionsFactory<F> actions) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |         return generator.generate(ai, actions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     @Override | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public boolean valid(CastSimulation simulation) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |         if ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |             simulation.alliesLife() + simulation.selfLife() <= 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |             || simulation.killedAllies() > 0.1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |             || simulation.suicideProbability() > 0.1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         // Apply more heal on enemies than allies | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 1 |  |         return simulation.alliesLife() + simulation.selfLife() > simulation.enemiesLife(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     @Override | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     public double score(CastSimulation simulation) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 1 |  |         final double score = healScore(simulation) + boostScore(simulation); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |         return score / simulation.actionPointsCost(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     private double healScore(CastSimulation simulation) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |         return simulation.alliesLife() + simulation.selfLife() - simulation.enemiesLife(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     private double boostScore(CastSimulation simulation) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 1 |  |         return (simulation.alliesBoost() + simulation.selfBoost() - simulation.enemiesBoost()) / 10; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  |