onBuffTerminated(Buff)   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
dl 0
loc 7
ccs 3
cts 3
cp 1
crap 3
rs 10
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.castable.effect.handler.misc;
21
22
import fr.quatrevieux.araknemu.game.fight.Fight;
23
import fr.quatrevieux.araknemu.game.fight.castable.CastScope;
24
import fr.quatrevieux.araknemu.game.fight.castable.FightCastScope;
25
import fr.quatrevieux.araknemu.game.fight.castable.Castable;
26
import fr.quatrevieux.araknemu.game.fight.castable.effect.EffectsUtils;
27
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.Buff;
28
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.BuffHook;
29
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.EffectHandler;
30
import fr.quatrevieux.araknemu.game.fight.fighter.Fighter;
31
import fr.quatrevieux.araknemu.game.fight.fighter.FighterData;
32
import fr.quatrevieux.araknemu.game.spell.effect.SpellEffect;
33
import fr.quatrevieux.araknemu.network.game.out.fight.CellShown;
34
35
/**
36
 * Handle invisibility effect
37
 *
38
 * This effect only works has buff : direct effect is not supported
39
 * The hidden flag will be changed on buff start and termination
40
 *
41
 * When the fighter cast a spell or close combat attack, he will lose its invisibility if he performs direct damage.
42
 * For other spells (or close combat) effects, the cast cell (i.e. current cell of the caster) will be shown to all fighters.
43
 *
44
 * Note: If two (or more) invisibility buffs are effective on a fighter, the invisibility state will be terminated
45
 * with the last (i.e. longest) buff.
46
 *
47
 * @see FighterData#hidden() Check the hidden state
48
 * @see Fighter#setHidden(FighterData, boolean) Called by the handler for change hidden state
49
 */
50
public final class InvisibilityHandler implements EffectHandler, BuffHook {
51
    private final Fight fight;
52
53 1
    public InvisibilityHandler(Fight fight) {
54 1
        this.fight = fight;
55 1
    }
56
57
    @Override
58
    public void handle(FightCastScope cast, FightCastScope.EffectScope effect) {
59 1
        throw new UnsupportedOperationException("Invisibility effect must be used as a buff");
60
    }
61
62
    @Override
63
    public void buff(FightCastScope cast, FightCastScope.EffectScope effect) {
64 1
        final SpellEffect spellEffect = effect.effect();
65 1
        final Fighter caster = cast.caster();
66 1
        final Castable action = cast.action();
67
68 1
        for (Fighter target : effect.targets()) {
69 1
            target.buffs().add(new Buff(spellEffect, action, caster, target, this));
70 1
        }
71 1
    }
72
73
    @Override
74
    public void onBuffStarted(Buff buff) {
75 1
        buff.target().setHidden(buff.caster(), true);
76 1
    }
77
78
    @Override
79
    public void onBuffTerminated(Buff buff) {
80 1
        final Fighter target = buff.target();
81
82
        // Set visible only if not yet visible and there is no other active invisibility buff
83 1
        if (target.hidden() && !hasOtherInvisibilityBuff(target)) {
84 1
            target.setHidden(buff.caster(), false);
85
        }
86 1
    }
87
88
    @Override
89
    public void onCast(Buff buff, FightCastScope cast) {
90 1
        final Fighter target = buff.target();
91
92 1
        if (!target.hidden()) {
93 1
            return;
94
        }
95
96
        // The fighter cast a damage effect : he loses its invisibility
97 1
        if (hasDirectDamageEffect(cast)) {
98 1
            target.setHidden(target, false);
99 1
            return;
100
        }
101
102
        // Show cell only on the first buff
103 1
        if (isFirstInvisibilityBuff(target, buff)) {
104 1
            fight.send(new CellShown(target, target.cell().id()));
105
        }
106 1
    }
107
108
    /**
109
     * Check if another active invisibility buff is present
110
     */
111
    private boolean hasOtherInvisibilityBuff(FighterData fighter) {
112 1
        for (Buff activeBuff : fighter.buffs()) {
113
            // Another invisibility buff exists : do not set target visible
114 1
            if (activeBuff.valid() && activeBuff.hook() == this) {
115 1
                return true;
116
            }
117 1
        }
118
119 1
        return false;
120
    }
121
122
    /**
123
     * Does the given cast will perform direct damage or not
124
     */
125
    private boolean hasDirectDamageEffect(FightCastScope cast) {
126 1
        for (CastScope.EffectScope<Fighter> effect : cast.effects()) {
127 1
            final SpellEffect spellEffect = effect.effect();
128
129 1
            if (spellEffect.duration() == 0 && EffectsUtils.isDamageEffect(spellEffect.effect())) {
130 1
                return true;
131
            }
132 1
        }
133
134 1
        return false;
135
    }
136
137
    /**
138
     * Check if the given buff is the first of the buff list
139
     */
140
    private boolean isFirstInvisibilityBuff(FighterData fighter, Buff currentBuff) {
141 1
        for (Buff otherBuff : fighter.buffs()) {
142 1
            if (otherBuff.hook() == this) {
143
                // Current buff is not the first active invisibility buff
144 1
                return otherBuff == currentBuff;
145
            }
146 1
        }
147
148
        // Should not occur
149 1
        return true;
150
    }
151
}
152