~ubuntu-branches/ubuntu/trusty/manaplus/trusty-proposed

« back to all changes in this revision

Viewing changes to src/resources/avatardb.cpp

  • Committer: Package Import Robot
  • Author(s): Patrick Matthäi, Andrei Karas, Patrick Matthäi
  • Date: 2013-05-27 09:14:03 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20130527091403-4b1jceqok7g2v5on
Tags: 1.3.5.26-1
[ Andrei Karas ]
* Add new files to copyright file.
* Update homepage URL.

[ Patrick Matthäi ]
* New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  The ManaPlus Client
 
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
 
4
 *  Copyright (C) 2009-2010  The Mana Developers
 
5
 *  Copyright (C) 2011-2013  The ManaPlus Developers
 
6
 *
 
7
 *  This file is part of The ManaPlus Client.
 
8
 *
 
9
 *  This program is free software; you can redistribute it and/or modify
 
10
 *  it under the terms of the GNU General Public License as published by
 
11
 *  the Free Software Foundation; either version 2 of the License, or
 
12
 *  any later version.
 
13
 *
 
14
 *  This program is distributed in the hope that it will be useful,
 
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 *  GNU General Public License for more details.
 
18
 *
 
19
 *  You should have received a copy of the GNU General Public License
 
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
 */
 
22
 
 
23
#include "resources/avatardb.h"
 
24
 
 
25
#include "logger.h"
 
26
 
 
27
#include "net/net.h"
 
28
 
 
29
#include "resources/beinginfo.h"
 
30
 
 
31
#include "utils/dtor.h"
 
32
#include "utils/gettext.h"
 
33
 
 
34
#include "configuration.h"
 
35
 
 
36
#include "debug.h"
 
37
 
 
38
namespace
 
39
{
 
40
    BeingInfos mAvatarInfos;
 
41
    bool mLoaded = false;
 
42
}
 
43
 
 
44
void AvatarDB::load()
 
45
{
 
46
    if (mLoaded)
 
47
        unload();
 
48
 
 
49
    XML::Document doc("avatars.xml");
 
50
    const XmlNodePtr rootNode = doc.rootNode();
 
51
 
 
52
    if (!rootNode || !xmlNameEqual(rootNode, "avatars"))
 
53
    {
 
54
        logger->log1("Avatars Database: Error while loading avatars.xml!");
 
55
        mLoaded = true;
 
56
        return;
 
57
    }
 
58
 
 
59
    for_each_xml_child_node(avatarNode, rootNode)
 
60
    {
 
61
        if (!xmlNameEqual(avatarNode, "avatar"))
 
62
            continue;
 
63
 
 
64
        BeingInfo *const currentInfo = new BeingInfo;
 
65
 
 
66
        currentInfo->setName(XML::langProperty(
 
67
            // TRANSLATORS: unknown info name
 
68
            avatarNode, "name", _("unnamed")));
 
69
 
 
70
        currentInfo->setTargetOffsetX(XML::getProperty(avatarNode,
 
71
            "targetOffsetX", 0));
 
72
 
 
73
        currentInfo->setTargetOffsetY(XML::getProperty(avatarNode,
 
74
            "targetOffsetY", 0));
 
75
 
 
76
        currentInfo->setWidth(XML::getProperty(avatarNode,
 
77
            "width", 0));
 
78
        currentInfo->setHeight(XML::getProperty(avatarNode,
 
79
            "height", 0));
 
80
 
 
81
        SpriteDisplay display;
 
82
 
 
83
        // iterate <sprite>s and <sound>s
 
84
        for_each_xml_child_node(spriteNode, avatarNode)
 
85
        {
 
86
            if (xmlNameEqual(spriteNode, "sprite"))
 
87
            {
 
88
                if (!spriteNode->xmlChildrenNode)
 
89
                    continue;
 
90
 
 
91
                SpriteReference *const currentSprite = new SpriteReference;
 
92
                currentSprite->sprite = reinterpret_cast<const char*>(
 
93
                    spriteNode->xmlChildrenNode->content);
 
94
 
 
95
                currentSprite->variant = XML::getProperty(
 
96
                    spriteNode, "variant", 0);
 
97
                display.sprites.push_back(currentSprite);
 
98
            }
 
99
        }
 
100
        currentInfo->setDisplay(display);
 
101
 
 
102
        mAvatarInfos[XML::getProperty(avatarNode, "id", 0)] = currentInfo;
 
103
    }
 
104
 
 
105
    mLoaded = true;
 
106
}
 
107
 
 
108
void AvatarDB::unload()
 
109
{
 
110
    delete_all(mAvatarInfos);
 
111
    mAvatarInfos.clear();
 
112
    mLoaded = false;
 
113
}
 
114
 
 
115
BeingInfo *AvatarDB::get(const int id)
 
116
{
 
117
    BeingInfoIterator i = mAvatarInfos.find(id);
 
118
    if (i == mAvatarInfos.end())
 
119
        return BeingInfo::unknown;
 
120
    else
 
121
        return i->second;
 
122
}