| 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-2019 Vincent Quatrevieux | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | package fr.quatrevieux.araknemu.game.fight.turn; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | import fr.quatrevieux.araknemu.game.fight.Fight; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | import fr.quatrevieux.araknemu.game.fight.fighter.Fighter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | import fr.quatrevieux.araknemu.game.fight.turn.event.NextTurnInitiated; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | import fr.quatrevieux.araknemu.game.fight.turn.event.TurnListChanged; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | import fr.quatrevieux.araknemu.game.fight.turn.order.FighterOrderStrategy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | import org.checkerframework.checker.nullness.qual.Nullable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | import org.checkerframework.checker.nullness.util.NullnessUtil; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | import java.util.List; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | import java.util.NoSuchElementException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | import java.util.Optional; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | import java.util.concurrent.atomic.AtomicBoolean; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * Handle fight turns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | public final class FightTurnList { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     private final Fight fight; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     private final List<Fighter> fighters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     private Fighter current; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     private @Nullable FightTurn turn; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     private int index; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |     private final AtomicBoolean active = new AtomicBoolean(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |     public FightTurnList(Fight fight, FighterOrderStrategy orderStrategy) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |         this.fight = fight; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |         this.fighters = orderStrategy.compute(fight.teams()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |         if (fighters.isEmpty()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |             throw new IllegalStateException("Cannot initialise turn list without fighters"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |         current = fighters.get(0); // Always init the first fighter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * Get all fighters ordered by their turn order | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     public List<Fighter> fighters() { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 1 |  |         return fighters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * Remove a fighter from turn list | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param fighter Fighter to remove | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @see TurnListChanged Event triggered after the list is updated | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public void remove(Fighter fighter) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |         final int index = fighters.indexOf(fighter); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |         if (index == -1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |             throw new NoSuchElementException("Fighter " + fighter.id() + " is not found on the turn list"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |         fighters.remove(index); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         // The removed fighter is the current fighter or before on the list | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         // so removing it will shift the list to the left relatively to the cursor (current) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         // which cause that the next fighter will be skipped | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         // See: https://github.com/Arakne/Araknemu/issues/127 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |         if (index <= this.index) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             // If current is negative, move cursor to the end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |             if (--this.index < 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |                 this.index += fighters.size(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |         fight.dispatch(new TurnListChanged(this)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * Add a fighter after the current one | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @param fighter Fighter to add | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @see TurnListChanged Event triggered after the list is updated | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     public void add(Fighter fighter) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 1 |  |         fighters.add(index + 1, fighter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 1 |  |         fight.dispatch(new TurnListChanged(this)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * Get the current turn | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @todo nullable instead of optional ? | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     public Optional<FightTurn> current() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 1 |  |         return Optional.ofNullable(turn); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * Get the current turn fighter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     public Fighter currentFighter() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 1 |  |         return current; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * Start the turn system | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     public void start() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 1 |  |         if (active.getAndSet(true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 1 |  |             throw new IllegalStateException("TurnList already started"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 1 |  |         index = -1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 1 |  |         next(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * Stop the turn system | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     public void stop() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 1 |  |         if (!active.getAndSet(false)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 1 |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 1 |  |         if (turn != null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 1 |  |             turn.stop(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 1 |  |             turn = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * Stop the current turn and start the next | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * @todo test start with return false | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     void next() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         // next is called internally by turn system, and fighters if used only with active turn system | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         // so fighters is always non-null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 1 |  |         final List<Fighter> fighters = NullnessUtil.castNonNull(this.fighters); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 1 |  |         turn = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 1 |  |         fight.dispatch(new NextTurnInitiated()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 1 |  |         while (active.get()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 1 |  |             if (++index >= fighters.size()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 1 |  |                 index = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 1 |  |             if (fighters.get(index).dead()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 1 |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 1 |  |             current = fighters.get(index); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 1 |  |             turn = new FightTurn(current, fight, fight.type().turnDuration()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 1 |  |             if (turn.start()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 1 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 1 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 180 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 181 |  |  |  |