summaryrefslogtreecommitdiff
path: root/src/state.cpp
blob: dc21e76665b156f523dd4cbe99dc3f66a3f08406 (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
/*
 *  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 <cassert>

#include "state.h"

#include "gamehandler.h"
#include "map.h"
#include "mapmanager.h"
#include "messageout.h"
#include "point.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)
        {
            Point ps = (*p)->getXY();
            Point pn = (*p)->getNextPosition();
            bool po = !(*p)->isNew(); // Is p old?
            MessageOut msg(GPMSG_BEINGS_MOVE);

            for (Movings::iterator o = movings.begin(),
                 o_end = movings.end(); o != o_end; ++o)
            {
                Point os = (*o)->getXY();
                Point on = (*o)->getNextPosition();
                bool oo = po && !(*o)->isNew(); // Are p and o both old?

                /* Look whether p and o "were" around the last time and whether
                   they "will" be around the next time. */
                bool wereInRange = ps.inRangeOf(os) && oo;
                bool willBeInRange = pn.inRangeOf(on);

                if (!wereInRange)
                {
                    // o was outside p's range.
                    if (!willBeInRange)
                    {
                        // Nothing to report: o will not be inside p's range.
                        continue;
                    }

                    int type = (*o)->getType();
                    MessageOut msg2(GPMSG_BEING_ENTER);
                    msg2.writeByte(type);
                    msg2.writeLong((*o)->getID());
                    switch (type) {
                    case OBJECT_PLAYER:
                    {
                        PlayerPtr q(*o);
                        msg2.writeString(q->getName());
                        msg2.writeByte(q->getHairStyle());
                        msg2.writeByte(q->getHairColor());
                        msg2.writeByte(q->getGender());
                    } break;
                    default:
                        assert(false); // TODO
                    }
                    gameHandler->sendTo(*p, msg2);
                }
                else if (!willBeInRange)
                {
                    // o is no longer visible from p.
                    MessageOut msg2(GPMSG_BEING_LEAVE);
                    msg2.writeByte((*o)->getType());
                    msg2.writeLong((*o)->getID());
                    gameHandler->sendTo(*p, msg2);
                    continue;
                }
                else if (os.x == on.x && os.y == on.y)
                {
                    // o does not move, nothing to report.
                    continue;
                }

                /* At this point, either o has entered p's range, either o is
                   moving inside p's range. Report o's movements. */
                Point od = (*o)->getDestination();
                msg.writeLong((*o)->getID());
                msg.writeShort(on.x);
                msg.writeShort(on.y);
                msg.writeShort(od.x);
                msg.writeShort(od.y);
            }

            // Don't send a packet if nothing happed in p's range.
            if (msg.getLength() > 2)
                gameHandler->sendTo(*p, msg);
        }

        for (Movings::iterator o = movings.begin(),
             o_end = movings.end(); o != o_end; ++o)
        {
            (*o)->setXY((*o)->getNextPosition());
            (*o)->setNew(false);
        }
    }
}

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

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;

    MessageOut msg(GPMSG_BEING_LEAVE);
    msg.writeByte(OBJECT_PLAYER);
    msg.writeLong(objectPtr->getID());

    Players &players = maps[mapId].players;
    Players::iterator p_end = players.end(), j = p_end;
    for (Players::iterator p = players.begin(); p != p_end; ++p)
    {
        if (p->get() == objectPtr.get())
        {
            j = p;
        }
        else if (objectPtr->getXY().inRangeOf((*p)->getXY()))
        {
            gameHandler->sendTo(*p, msg);
        }
    }

    if (j != p_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;

    /* 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);
}

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
}