summaryrefslogtreecommitdiff
path: root/src/net/ea/playerrecv.cpp
blob: edc60490745903d763f1bb954d00475b2677b17b (plain) (blame)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 *  The ManaPlus Client
 *  Copyright (C) 2004-2009  The Mana World Development Team
 *  Copyright (C) 2009-2010  The Mana Developers
 *  Copyright (C) 2011-2017  The ManaPlus Developers
 *
 *  This file is part of The ManaPlus Client.
 *
 *  This program 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.
 *
 *  This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "net/ea/playerrecv.h"

#include "game.h"
#include "notifymanager.h"
#include "settings.h"
#include "soundmanager.h"

#include "being/localplayer.h"
#include "being/playerinfo.h"

#include "const/net/nostat.h"

#include "enums/resources/notifytypes.h"

#include "gui/viewport.h"

#include "gui/windows/statuswindow.h"

#include "input/inputmanager.h"

#include "resources/map/map.h"

#include "net/playerhandler.h"

#include "utils/stdmove.h"
#include "utils/stringutils.h"

#include "debug.h"

// Max. distance we are willing to scroll after a teleport;
// everything beyond will reset the port hard.
static const int MAP_TELEPORT_SCROLL_DISTANCE = 8;

namespace Ea
{

void PlayerRecv::processPlayerWarp(Net::MessageIn &msg)
{
    BLOCK_START("PlayerRecv::processPlayerWarp")
    std::string mapPath = msg.readString(16, "map name");
    int x = msg.readInt16("x");
    int y = msg.readInt16("y");

    logger->log("Warping to %s (%d, %d)", mapPath.c_str(), x, y);

    if (localPlayer == nullptr)
        logger->log1("SMSG_PLAYER_WARP localPlayer null");

    /*
      * We must clear the local player's target *before* the call
      * to changeMap, as it deletes all beings.
      */
    if (localPlayer != nullptr)
        localPlayer->stopAttack();

    Game *const game = Game::instance();
    if (game == nullptr)
    {
        BLOCK_END("PlayerRecv::processPlayerWarp")
        return;
    }

    const std::string &currentMapName = game->getCurrentMapName();
    const bool sameMap = (currentMapName == mapPath);

    // Switch the actual map, deleting the previous one if necessary
    mapPath = mapPath.substr(0, mapPath.rfind('.'));
    game->changeMap(mapPath);

    int scrollOffsetX = 0;
    int scrollOffsetY = 0;

    if (localPlayer != nullptr)
    {
        const Map *const map = game->getCurrentMap();
        if (map != nullptr)
        {
            if (x >= map->getWidth())
                x = map->getWidth() - 1;
            if (y >= map->getHeight())
                y = map->getHeight() - 1;
            if (x < 0)
                x = 0;
            if (y < 0)
                y = 0;
            /* Scroll if neccessary */
            if (!sameMap
                || (abs(x - localPlayer->getTileX())
                > MAP_TELEPORT_SCROLL_DISTANCE)
                || (abs(y - localPlayer->getTileY())
                > MAP_TELEPORT_SCROLL_DISTANCE))
            {
                scrollOffsetX = (x - localPlayer->getTileX())
                    * map->getTileWidth();
                scrollOffsetY = (y - localPlayer->getTileY())
                    * map->getTileHeight();
            }
        }

        localPlayer->setAction(BeingAction::STAND, 0);
        localPlayer->setTileCoords(x, y);
        localPlayer->navigateClean();
        localPlayer->reset();
    }

    logger->log("Adjust scrolling by %d:%d", scrollOffsetX, scrollOffsetY);

    if (viewport != nullptr)
    {
        viewport->returnCamera();
        viewport->scrollBy(scrollOffsetX, scrollOffsetY);
    }
    BLOCK_END("PlayerRecv::processPlayerWarp")
}

void PlayerRecv::processPlayerStatUpdate1(Net::MessageIn &msg)
{
    BLOCK_START("PlayerRecv::processPlayerStatUpdate1")
    const int type = msg.readInt16("type");
    const int value = msg.readInt32("value");
    if (localPlayer == nullptr)
    {
        BLOCK_END("PlayerRecv::processPlayerStatUpdate1")
        return;
    }

    playerHandler->setStat(msg, type, value, NoStat, Notify_true);
    BLOCK_END("PlayerRecv::processPlayerStatUpdate1")
}

void PlayerRecv::processPlayerStatUpdate2(Net::MessageIn &msg)
{
    BLOCK_START("PlayerRecv::processPlayerStatUpdate2")
    const int type = msg.readInt16("type");
    const int value = msg.readInt32("value");
    playerHandler->setStat(msg, type, value, NoStat, Notify_true);
    BLOCK_END("PlayerRecv::processPlayerStatUpdate2")
}

void PlayerRecv::processPlayerStatUpdate3(Net::MessageIn &msg)
{
    BLOCK_START("PlayerRecv::processPlayerStatUpdate3")
    const int type = msg.readInt32("type");
    const int base = msg.readInt32("base");
    const int bonus = msg.readInt32("bonus");

    playerHandler->setStat(msg, type, base, bonus, Notify_false);
    BLOCK_END("PlayerRecv::processPlayerStatUpdate3")
}

void PlayerRecv::processPlayerStatUpdate4(Net::MessageIn &msg)
{
    BLOCK_START("PlayerRecv::processPlayerStatUpdate4")
    const uint16_t type = msg.readInt16("type");
    const uint8_t ok = msg.readUInt8("flag");
    const int value = msg.readUInt8("value");

    if (ok != 1)
    {
        const int oldValue = PlayerInfo::getStatBase(
            static_cast<AttributesT>(type));
        const int points = PlayerInfo::getAttribute(
            Attributes::PLAYER_CHAR_POINTS) + oldValue - value;
        PlayerInfo::setAttribute(Attributes::PLAYER_CHAR_POINTS, points);
        NotifyManager::notify(NotifyTypes::SKILL_RAISE_ERROR);
    }

    playerHandler->setStat(msg, type, value, NoStat, Notify_true);
    BLOCK_END("PlayerRecv::processPlayerStatUpdate4")
}

void PlayerRecv::processPlayerStatUpdate6(Net::MessageIn &msg)
{
    BLOCK_START("PlayerRecv::processPlayerStatUpdate6")
    const int type = msg.readInt16("type");
    const int value = msg.readUInt8("value");
    if (statusWindow != nullptr)
        playerHandler->setStat(msg, type, value, NoStat, Notify_true);
    BLOCK_END("PlayerRecv::processPlayerStatUpdate6")
}

void PlayerRecv::processPlayerArrowMessage(Net::MessageIn &msg)
{
    BLOCK_START("PlayerRecv::processPlayerArrowMessage")
    const int type = msg.readInt16("type");
    switch (type)
    {
        case 0:
            NotifyManager::notify(NotifyTypes::ARROWS_EQUIP_NEEDED);
            break;
        case 3:
            // arrows equiped
            break;
        default:
            UNIMPLEMENTEDPACKETFIELD(type);
            break;
    }
    BLOCK_END("PlayerRecv::processPlayerArrowMessage")
}

void PlayerRecv::processMapMusic(Net::MessageIn &msg)
{
    const int size = msg.readInt16("len") - 4;
    const std::string music = msg.readString(size, "name");
    soundManager.playMusic(music,
        SkipError_false);

    Map *const map = viewport->getMap();
    if (map != nullptr)
        map->setMusicFile(music);
}

void PlayerRecv::processMapMask(Net::MessageIn &msg)
{
    const int mask = msg.readInt32("mask");
    msg.readInt32("unused");
    Map *const map = Game::instance()->getCurrentMap();
    if (map != nullptr)
        map->setMask(mask);
}

void PlayerRecv::processPlayerClientCommand(Net::MessageIn &msg)
{
    const int sz = msg.readInt16("len") - 4;
    std::string command = msg.readString(sz, "command");
    std::string cmd;
    std::string args;

    if (settings.awayMode ||
        settings.pseudoAwayMode ||
        !settings.enableRemoteCommands)
    {
        return;
    }

    if (!parse2Str(command, cmd, args))
    {
        cmd = STD_MOVE(command);
        args.clear();
    }
    inputManager.executeRemoteChatCommand(cmd, args, nullptr);
}

}  // namespace Ea