summaryrefslogtreecommitdiffstats
path: root/src/game-server/combatcomponent.cpp
blob: 38c7716e35069ed09c743f12bfb06c7af8749ba2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 *  The Mana Server
 *  Copyright (C) 2013 The Mana Developers
 *
 *  This file is part of The Mana Server.
 *
 *  The Mana Server is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  The Mana Server 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with The Mana Server.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "combatcomponent.h"

#include "game-server/being.h"
#include "game-server/mapcomposite.h"

#include "utils/logger.h"

CombatComponent::CombatComponent(Entity &being):
    mTarget(nullptr),
    mCurrentAttack(nullptr)
{
    being.getComponent<BeingComponent>()->signal_died.connect(sigc::mem_fun(
            this, &CombatComponent::diedOrRemoved));
    being.signal_removed.connect(sigc::mem_fun(this,
                                  &CombatComponent::diedOrRemoved));
}

CombatComponent::~CombatComponent()
{
}

void CombatComponent::update(Entity &entity)
{
    auto *beingComponent = entity.getComponent<BeingComponent>();

    if (beingComponent->getAction() != ATTACK || !mTarget)
            return;

    std::vector<Attack *> attacksReady;
    mAttacks.getUsuableAttacks(&attacksReady);

    if (Attack *triggerableAttack = mAttacks.getTriggerableAttack())
    {
        processAttack(entity, *triggerableAttack);
        mAttacks.markAttackAsTriggered();
    }

    // Deal with the ATTACK action.
    if (attacksReady.empty())
        return;

    Attack *highestPriorityAttack = 0;
    // Performs all ready attacks.
    for (std::vector<Attack *>::const_iterator it = attacksReady.begin(),
         it_end = attacksReady.end(); it != it_end; ++it)
    {
        const Point &attackerPosition =
                entity.getComponent<ActorComponent>()->getPosition();
        const Point &targetPosition =
                mTarget->getComponent<ActorComponent>()->getPosition();

        // check if target is in range using the pythagorean theorem
        int distx = attackerPosition.x - targetPosition.x;
        int disty = attackerPosition.y - targetPosition.y;
        int distSquare = (distx * distx + disty * disty);
        AttackInfo *info = (*it)->getAttackInfo();
        int maxDist = info->getDamage().range +
                entity.getComponent<ActorComponent>()->getSize();

        if (distSquare <= maxDist * maxDist &&
                (!highestPriorityAttack ||
                 highestPriorityAttack->getAttackInfo()->getPriority()
                 < info->getPriority()))
        {
            highestPriorityAttack = *it;
        }
    }
    if (highestPriorityAttack)
    {
        mAttacks.startAttack(highestPriorityAttack);
        mCurrentAttack = highestPriorityAttack;
        const Point &point =
                entity.getComponent<ActorComponent>()->getPosition();
        beingComponent->setDestination(entity, point);
        // TODO: Turn into direction of enemy
        entity.getComponent<ActorComponent>()->raiseUpdateFlags(
                UPDATEFLAG_ATTACK);
    }
}

/**
 * Takes a damage structure, computes the real damage based on the
 * stats, deducts the result from the hitpoints and adds the result to
 * the HitsTaken list.
 */
int CombatComponent::damage(Entity &target,
                            Entity *source,
                            const Damage &damage)
{
    auto *beingComponent = target.getComponent<BeingComponent>();

    int HPloss = damage.base;
    if (damage.delta)
        HPloss += rand() * (damage.delta + 1) / RAND_MAX;

    // TODO magical attacks and associated elemental modifiers
    switch (damage.type)
    {
        case DAMAGE_PHYSICAL:
        {
            const double dodge =
                    beingComponent->getModifiedAttribute(ATTR_DODGE);
            if (!damage.trueStrike && rand()%((int)dodge + 1) >
                    rand()%(damage.cth + 1))
            {
                HPloss = 0;
                // TODO Process triggers for a dodged physical attack here.
                // If there is an attacker included, also process triggers for the attacker (failed physical strike)
            }
            else
            {
                const double defense =
                        beingComponent->getModifiedAttribute(ATTR_DEFENSE);
                HPloss = HPloss * (1.0 - (0.0159375f * defense) /
                        (1.0 + 0.017 * defense)) +
                        (rand()%((HPloss / 16) + 1));
                // TODO Process triggers for receiving damage here.
                // If there is an attacker included, also process triggers for the attacker (successful physical strike)
            }
            break;
        }
        case DAMAGE_MAGICAL:
#if 0
            beingComponent.getModifiedAttribute(BASE_ELEM_BEGIN + damage.element);
#else
            LOG_WARN("Attempt to use magical type damage! This has not been"
                      "implemented yet and should not be used!");
            HPloss = 0;
#endif
            break;
        case DAMAGE_DIRECT:
            break;
        default:
            LOG_WARN("Unknown damage type '" << damage.type << "'!");
            break;
    }

    if (HPloss > 0)
    {
        mHitsTaken.push_back(HPloss);
        const Attribute *HP = beingComponent->getAttribute(ATTR_HP);
        LOG_DEBUG("Being "
                  << target.getComponent<ActorComponent>()->getPublicID()
                  << " suffered " << HPloss
                  << " damage. HP: "
                  << HP->getModifiedAttribute() << "/"
                  << beingComponent->getModifiedAttribute(ATTR_MAX_HP));
        beingComponent->setAttribute(target, ATTR_HP, HP->getBase() - HPloss);
        // No HP regen after being hit if this is set.
        // TODO: Reenable this once the attributes are available as a component
        // A bit too fuzzy to implement at the moment
        //mHealthRegenerationTimeout.setSoft(
        //            Configuration::getValue("game_hpRegenBreakAfterHit", 0));
    }
    else
    {
        HPloss = 0;
    }

    signal_damaged.emit(source, damage, HPloss);
    return HPloss;
}

/**
 * Performs an attack
 */
void CombatComponent::processAttack(Entity &source, Attack &attack)
{
    performAttack(source, attack.getAttackInfo()->getDamage());
}

/**
 * Adds an attack to the available attacks
 */
void CombatComponent::addAttack(AttackInfo *attackInfo)
{
    mAttacks.add(this, attackInfo);
}

/**
 * Removes an attack from the available attacks
 */
void CombatComponent::removeAttack(AttackInfo *attackInfo)
{
    mAttacks.remove(this, attackInfo);
}

/**
 * Performs an attack.
 */
int CombatComponent::performAttack(Entity &source, const Damage &dmg)
{
    // check target legality
    if (!mTarget
            || mTarget == &source
            || mTarget->getComponent<BeingComponent>()->getAction() == DEAD
            || !mTarget->canFight())
        return -1;

    if (source.getMap()->getPvP() == PVP_NONE
            && mTarget->getType() == OBJECT_CHARACTER
            && source.getType() == OBJECT_CHARACTER)
        return -1;

    return mTarget->getComponent<CombatComponent>()->damage(*mTarget,
                                                              &source, dmg);
}