1/**
2 * The Forgotten Server - a free and open-source MMORPG server emulator
3 * Copyright (C) 2019 Mark Samman <mark.samman@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#include "otpch.h"
21
22#include "mounts.h"
23
24#include "pugicast.h"
25#include "tools.h"
26
27bool Mounts::reload()
28{
29 mounts.clear();
30 return loadFromXml();
31}
32
33bool Mounts::loadFromXml()
34{
35 pugi::xml_document doc;
36 pugi::xml_parse_result result = doc.load_file("data/XML/mounts.xml");
37 if (!result) {
38 printXMLError("Error - Mounts::loadFromXml", "data/XML/mounts.xml", result);
39 return false;
40 }
41
42 for (auto mountNode : doc.child("mounts").children()) {
43 mounts.emplace_back(
44 static_cast<uint8_t>(pugi::cast<uint16_t>(mountNode.attribute("id").value())),
45 pugi::cast<uint16_t>(mountNode.attribute("clientid").value()),
46 mountNode.attribute("name").as_string(),
47 pugi::cast<int32_t>(mountNode.attribute("speed").value()),
48 mountNode.attribute("premium").as_bool()
49 );
50 }
51 mounts.shrink_to_fit();
52 return true;
53}
54
55Mount* Mounts::getMountByID(uint8_t id)
56{
57 auto it = std::find_if(mounts.begin(), mounts.end(), [id](const Mount& mount) {
58 return mount.id == id;
59 });
60
61 return it != mounts.end() ? &*it : nullptr;
62}
63
64Mount* Mounts::getMountByName(const std::string& name) {
65 auto mountName = name.c_str();
66 for (auto& it : mounts) {
67 if (strcasecmp(mountName, it.name.c_str()) == 0) {
68 return ⁢
69 }
70 }
71
72 return nullptr;
73}
74
75Mount* Mounts::getMountByClientID(uint16_t clientId)
76{
77 auto it = std::find_if(mounts.begin(), mounts.end(), [clientId](const Mount& mount) {
78 return mount.clientId == clientId;
79 });
80
81 return it != mounts.end() ? &*it : nullptr;
82}
83