summaryrefslogtreecommitdiff
path: root/src/state.cpp
blob: 87e85015e1ffe27b860e881bcc7aa5d7a7963083 (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
/*
 *  The Mana World Server
 *  Copyright 2004 The Mana World Development Team
 *
 *  This file is part of The Mana World.
 *
 *  The Mana World 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 World 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 World; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  $Id$
 */

#include "state.h"

#include "gamehandler.h"
#include "map.h"
#include "mapmanager.h"
#include "messageout.h"
#include "storage.h"

#include "utils/logger.h"

State::State()
{
}

State::~State()
{
    for (std::map<unsigned int, MapComposite>::iterator i = maps.begin();
         i != maps.end();
         i++)
    {
        delete i->second.map;
    }
}

void
State::update()
{
    // update game state (update AI, etc.)
    for (std::map<unsigned int, MapComposite>::iterator m = maps.begin(),
         m_end = maps.end(); m != m_end; ++m)
    {
        typedef std::vector< utils::CountedPtr<MovingObject> > Movings;
        Movings movings;

        for (Objects::iterator o = m->second.objects.begin(),
             o_end = m->second.objects.end(); o != o_end; ++o)
        {
            (*o)->update();
            int t = (*o)->getType();
            if (t == OBJECT_NPC || t == OBJECT_PLAYER || t == OBJECT_MONSTER) {
                utils::CountedPtr<MovingObject> ptr(*o);
                ptr->move();
                movings.push_back(ptr);
            }
        }

        Players &players = m->second.players;
        for (Players::iterator p = players.begin(),
             p_end = players.end(); p != p_end; ++p)
        {
            std::pair<unsigned, unsigned> ps = (*p)->getXY();
            std::pair<unsigned, unsigned> pn = (*p)->getNextPosition();
            MessageOut msg;
            msg.writeShort(GPMSG_BEINGS_MOVE);

            for (Movings::iterator o = movings.begin(),
                 o_end = movings.end(); o != o_end; ++o)
            {
                std::pair<unsigned, unsigned> os = (*o)->getXY();
                std::pair<unsigned, unsigned> on = (*o)->getNextPosition();

                bool were = areAround(ps.first, ps.second, os.first, os.second);
                bool will = areAround(pn.first, pn.second, on.first, on.second);
                bool has_moved = os.first != on.first || os.second != on.second;
                if (!(will && has_moved) && !(were != will))
                    continue;

                std::pair<unsigned, unsigned> od = (*o)->getDestination();
                msg.writeLong((*o)->getID());
                msg.writeShort(on.first);
                msg.writeShort(on.second);
                msg.writeShort(od.first);
                msg.writeShort(od.second);
            }

            if (msg.getLength() > 2)
                gameHandler->sendTo(*p, msg);
        }

        for (Movings::iterator o = movings.begin(),
             o_end = movings.end(); o != o_end; ++o)
        {
            std::pair<unsigned, unsigned> pos = (*o)->getNextPosition();
            (*o)->setXY(pos.first, pos.second);
        }
    }
}

void
State::addObject(ObjectPtr objectPtr)
{
    unsigned mapId = objectPtr->getMapId();
    if (!loadMap(mapId)) return;
    maps[mapId].objects.push_back(objectPtr);
    if (objectPtr->getType() != OBJECT_PLAYER) return;
    Players &players = maps[mapId].players;
    PlayerPtr playerPtr(objectPtr);

    /* Currently when a player is added, all existing players are notified
     * about this. This will need to be modified so that players only know
     * about players close to them.
     */
    MessageOut msg(GPMSG_BEING_ENTER);
    msg.writeByte(OBJECT_PLAYER);
    msg.writeLong(playerPtr->getID());
    msg.writeString(playerPtr->getName());
    msg.writeByte(playerPtr->getHairStyle());
    msg.writeByte(playerPtr->getHairColor());
    msg.writeByte(playerPtr->getGender());

    for (Players::iterator p = players.begin(),
         p_end = players.end(); p != p_end; ++p)
    {
        gameHandler->sendTo(*p, msg);
    }

    // Add the new player to the list
    players.push_back(playerPtr);

    /* Since the player doesn't know yet where on the world he is after
     * connecting to the map server, we send him an initial change map message.
     */
    Storage &store = Storage::instance("tmw");
    MessageOut mapChangeMessage(GPMSG_PLAYER_MAP_CHANGE);
    mapChangeMessage.writeString(store.getMapNameFromId(mapId));
    mapChangeMessage.writeShort(playerPtr->getX());
    mapChangeMessage.writeShort(playerPtr->getY());
    mapChangeMessage.writeByte(0);
    gameHandler->sendTo(playerPtr, mapChangeMessage);
}

void
State::removeObject(ObjectPtr objectPtr)
{
    unsigned mapId = objectPtr->getMapId();
    std::map<unsigned, MapComposite>::iterator m = maps.find(mapId);
    if (m == maps.end()) return;
    Objects &objects = m->second.objects;
    for (Objects::iterator o = objects.begin(),
         o_end = objects.end(); o != o_end; ++o) {
        if (o->get() == objectPtr.get()) {
            objects.erase(o);
            break;
        }
    }
    if (objectPtr->getType() != OBJECT_PLAYER) return;
    PlayerPtr playerPtr(objectPtr);
    Players &players = maps[mapId].players;

    /* Also see note add addObject. All other players are notified about this
     * player leaving, but not all of them need to know.
     */
    MessageOut msg(GPMSG_BEING_LEAVE);
    msg.writeByte(OBJECT_PLAYER);
    msg.writeLong(playerPtr->getID());

    Players::iterator p_end = players.end(), j = p_end;
    for (Players::iterator p = players.begin(); p != p_end; ++p)
    {
        if (p->get() == playerPtr.get())
            j = p;
        else
            gameHandler->sendTo(*p, msg);
    }
    if (j != players.end()) players.erase(j);
}

void
State::informPlayer(PlayerPtr playerPtr)
{
    unsigned mapId = playerPtr->getMapId();
    std::map<unsigned, MapComposite>::iterator m = maps.find(mapId);
    if (m == maps.end()) return;
    Players &players = m->second.players;

    /* Here the player is informed about all the other players on the map.
     * However, the player should only be told about other players within
     * visual range. See also notes at addObject and removeObject.
     */
    for (Players::iterator p = players.begin(),
         p_end = players.end(); p != p_end; ++p)
    {
        MessageOut msg(GPMSG_BEING_ENTER);
        msg.writeByte(OBJECT_PLAYER);
        msg.writeLong((*p)->getID());
        msg.writeString((*p)->getName());
        msg.writeByte((*p)->getHairStyle());
        msg.writeByte((*p)->getHairColor());
        msg.writeByte((*p)->getGender());
        gameHandler->sendTo(playerPtr, msg);
    }
}

bool
State::loadMap(const unsigned int mapId)
{
    if (maps.find(mapId) != maps.end()) return true;
    Map *tmp = MapManager::instance().loadMap(mapId);
    if (!tmp) return false;
    maps[mapId].map = tmp;

    // will need to load extra map related resources here also

    return true; // We let true for testing on beings
}