GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/net/eathena/mercenaryrecv.cpp Lines: 1 109 0.9 %
Date: 2017-11-29 Branches: 0 96 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2017  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "net/eathena/mercenaryrecv.h"
22
23
#include "actormanager.h"
24
#include "notifymanager.h"
25
26
#include "being/localplayer.h"
27
#include "being/mercenaryinfo.h"
28
#include "being/playerinfo.h"
29
30
#include "enums/resources/notifytypes.h"
31
32
#include "gui/windows/skilldialog.h"
33
34
#include "net/messagein.h"
35
36
#include "net/eathena/sp.h"
37
38
#include "utils/checkutils.h"
39
40
#include "debug.h"
41
42
namespace EAthena
43
{
44
45
#define setMercStat(sp, stat) \
46
        case sp: \
47
            PlayerInfo::setStatBase(stat, \
48
                val); \
49
            break;
50
51
void MercenaryRecv::processMercenaryUpdate(Net::MessageIn &msg)
52
{
53
    const int sp = msg.readInt16("type");
54
    const int val = msg.readInt32("value");
55
    switch (sp)
56
    {
57
        setMercStat(Sp::ATK1, Attributes::MERC_ATK);
58
        setMercStat(Sp::MATK1, Attributes::MERC_MATK);
59
        setMercStat(Sp::HIT, Attributes::MERC_HIT);
60
        setMercStat(Sp::CRITICAL, Attributes::MERC_CRIT);
61
        setMercStat(Sp::DEF1, Attributes::MERC_DEF);
62
        setMercStat(Sp::MDEF1, Attributes::MERC_MDEF);
63
        setMercStat(Sp::MERCFLEE, Attributes::MERC_FLEE);
64
        setMercStat(Sp::ASPD, Attributes::MERC_ATTACK_DELAY);
65
        setMercStat(Sp::HP, Attributes::MERC_HP);
66
        setMercStat(Sp::MAXHP, Attributes::MERC_MAX_HP);
67
        setMercStat(Sp::SP, Attributes::MERC_MP);
68
        setMercStat(Sp::MAXSP, Attributes::MERC_MAX_MP);
69
        setMercStat(Sp::MERCKILLS, Attributes::MERC_KILLS);
70
        setMercStat(Sp::MERCFAITH, Attributes::MERC_FAITH);
71
        default:
72
            reportAlways("Unknown mercenary stat %d",
73
                sp);
74
            break;
75
    }
76
}
77
78
void MercenaryRecv::processMercenaryInfo(Net::MessageIn &msg)
79
{
80
    // +++ need create if need mercenary being and update stats
81
    Being *const dstBeing = actorManager->findBeing(
82
        msg.readBeingId("being id"));
83
    PlayerInfo::setStatBase(Attributes::MERC_ATK,
84
        msg.readInt16("atk"));
85
    PlayerInfo::setStatBase(Attributes::MERC_MATK,
86
        msg.readInt16("matk"));
87
    PlayerInfo::setStatBase(Attributes::MERC_HIT,
88
        msg.readInt16("hit"));
89
    PlayerInfo::setStatBase(Attributes::MERC_CRIT,
90
        msg.readInt16("crit/10"));
91
    PlayerInfo::setStatBase(Attributes::MERC_DEF,
92
        msg.readInt16("def"));
93
    PlayerInfo::setStatBase(Attributes::MERC_MDEF,
94
        msg.readInt16("mdef"));
95
    PlayerInfo::setStatBase(Attributes::MERC_FLEE,
96
        msg.readInt16("flee"));
97
    PlayerInfo::setStatBase(Attributes::MERC_ATTACK_DELAY,
98
        msg.readInt16("attack speed"));
99
    const std::string name = msg.readString(24, "name");
100
    const int level = msg.readInt16("level");
101
    PlayerInfo::setStatBase(Attributes::MERC_LEVEL,
102
        level);
103
    PlayerInfo::setStatBase(Attributes::MERC_HP,
104
        msg.readInt32("hp"));
105
    PlayerInfo::setStatBase(Attributes::MERC_MAX_HP,
106
        msg.readInt32("max hp"));
107
    PlayerInfo::setStatBase(Attributes::MERC_MP,
108
        msg.readInt32("sp"));
109
    PlayerInfo::setStatBase(Attributes::MERC_MAX_MP,
110
        msg.readInt32("max sp"));
111
    PlayerInfo::setStatBase(Attributes::MERC_EXPIRE,
112
        msg.readInt32("expire time"));
113
    PlayerInfo::setStatBase(Attributes::MERC_FAITH,
114
        msg.readInt16("faith"));
115
    PlayerInfo::setStatBase(Attributes::MERC_CALLS,
116
        msg.readInt32("calls"));
117
    PlayerInfo::setStatBase(Attributes::MERC_KILLS,
118
        msg.readInt32("kills"));
119
    const int range = msg.readInt16("attack range");
120
    PlayerInfo::setStatBase(Attributes::MERC_ATTACK_RANGE,
121
        range);
122
    PlayerInfo::updateAttrs();
123
124
    if ((dstBeing != nullptr) && (localPlayer != nullptr))
125
    {
126
        MercenaryInfo *const mercenary = new MercenaryInfo;
127
        mercenary->id = dstBeing->getId();
128
        mercenary->name = name;
129
        mercenary->level = level;
130
        mercenary->range = range;
131
        PlayerInfo::setMercenary(mercenary);
132
        PlayerInfo::setMercenaryBeing(dstBeing);
133
    }
134
}
135
136
void MercenaryRecv::processMercenarySkills(Net::MessageIn &msg)
137
{
138
    if (skillDialog != nullptr)
139
        skillDialog->hideSkills(SkillOwner::Mercenary);
140
    const int count = (msg.readInt16("len") - 4) / 37;
141
    for (int f = 0; f < count; f ++)
142
    {
143
        const int skillId = msg.readInt16("skill id");
144
        const SkillType::SkillType inf = static_cast<SkillType::SkillType>(
145
            msg.readInt32("inf"));
146
        const int level = msg.readInt16("skill level");
147
        const int sp = msg.readInt16("sp");
148
        const int range = msg.readInt16("range");
149
        const std::string name = msg.readString(24, "skill name");
150
        const Modifiable up = fromBool(msg.readUInt8("up flag"), Modifiable);
151
        PlayerInfo::setSkillLevel(skillId, level);
152
        if (skillDialog != nullptr)
153
        {
154
            if (!skillDialog->updateSkill(skillId, range, up, inf, sp))
155
            {
156
                skillDialog->addSkill(SkillOwner::Mercenary,
157
                    skillId, name, level, range, up, inf, sp);
158
            }
159
        }
160
    }
161
    if (skillDialog != nullptr)
162
        skillDialog->updateModels();
163
}
164
165
void MercenaryRecv::handleMercenaryMessage(const int cmd)
166
{
167
    PlayerInfo::setMercenary(nullptr);
168
    if (skillDialog != nullptr)
169
    {
170
        skillDialog->hideSkills(SkillOwner::Mercenary);
171
        skillDialog->updateModels();
172
    }
173
174
    switch (cmd)
175
    {
176
        case 0:
177
            NotifyManager::notify(NotifyTypes::MERCENARY_EXPIRED);
178
            break;
179
        case 1:
180
            NotifyManager::notify(NotifyTypes::MERCENARY_KILLED);
181
            break;
182
        case 2:
183
            NotifyManager::notify(NotifyTypes::MERCENARY_FIRED);
184
            break;
185
        case 3:
186
            NotifyManager::notify(NotifyTypes::MERCENARY_RUN);
187
            break;
188
        default:
189
            NotifyManager::notify(NotifyTypes::MERCENARY_UNKNOWN);
190
            break;
191
    }
192
}
193
194
4
}  // namespace EAthena