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#ifndef FS_TOWN_H_3BE21D2293B44AA4A3D22D25BE1B9350
21#define FS_TOWN_H_3BE21D2293B44AA4A3D22D25BE1B9350
22
23#include "position.h"
24
25class Town
26{
27 public:
28 explicit Town(uint32_t id) : id(id) {}
29
30 const Position& getTemplePosition() const {
31 return templePosition;
32 }
33 const std::string& getName() const {
34 return name;
35 }
36
37 void setTemplePos(Position pos) {
38 templePosition = pos;
39 }
40 void setName(std::string name) {
41 this->name = std::move(name);
42 }
43 uint32_t getID() const {
44 return id;
45 }
46
47 private:
48 uint32_t id;
49 std::string name;
50 Position templePosition;
51};
52
53using TownMap = std::map<uint32_t, Town*>;
54
55class Towns
56{
57 public:
58 Towns() = default;
59 ~Towns() {
60 for (const auto& it : townMap) {
61 delete it.second;
62 }
63 }
64
65 // non-copyable
66 Towns(const Towns&) = delete;
67 Towns& operator=(const Towns&) = delete;
68
69 bool addTown(uint32_t townId, Town* town) {
70 return townMap.emplace(townId, town).second;
71 }
72
73 Town* getTown(const std::string& townName) const {
74 for (const auto& it : townMap) {
75 if (strcasecmp(townName.c_str(), it.second->getName().c_str()) == 0) {
76 return it.second;
77 }
78 }
79 return nullptr;
80 }
81
82 Town* getTown(uint32_t townId) const {
83 auto it = townMap.find(townId);
84 if (it == townMap.end()) {
85 return nullptr;
86 }
87 return it->second;
88 }
89
90 const TownMap& getTowns() const {
91 return townMap;
92 }
93
94 private:
95 TownMap townMap;
96};
97
98#endif
99