Otclient  14/8/2020
towns.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "towns.h"
24 
26 
27 Town::Town(uint32 tid, const std::string& name, const Position& pos)
28  : m_id(tid), m_name(name)
29 {
30  if(pos.isValid())
31  m_pos = pos;
32 }
33 
35 {
36  m_nullTown = TownPtr(new Town);
37 }
38 
39 void TownManager::addTown(const TownPtr &town)
40 {
41  if(findTown(town->getId()) == m_towns.end())
42  m_towns.push_back(town);
43 }
44 
46 {
47  auto it = findTown(townId);
48  if(it != m_towns.end())
49  m_towns.erase(it);
50 }
51 
53 {
54  auto it = std::find_if(m_towns.begin(), m_towns.end(),
55  [=] (const TownPtr& town) -> bool { return town->getId() == townId; });
56  if(it != m_towns.end())
57  return *it;
58  return m_nullTown;
59 }
60 
61 const TownPtr& TownManager::getTownByName(std::string name)
62 {
63  auto it = std::find_if(m_towns.begin(), m_towns.end(),
64  [=] (const TownPtr& town) -> bool { return town->getName() == name; } );
65  if(it != m_towns.end())
66  return *it;
67  return m_nullTown;
68 }
69 
70 TownList::iterator TownManager::findTown(uint32 townId)
71 {
72  return std::find_if(m_towns.begin(), m_towns.end(),
73  [=] (const TownPtr& town) -> bool { return town->getId() == townId; });
74 }
75 
77 {
78  m_towns.sort([] (const TownPtr& lhs, const TownPtr& rhs) { return lhs->getName() < rhs->getName(); });
79 }
80 
TownPtr
stdext::shared_object_ptr< Town > TownPtr
Definition: declarations.h:76
TownManager
Definition: towns.h:49
TownManager::removeTown
void removeTown(uint32 townId)
Definition: towns.cpp:45
Town::Town
Town()
Definition: towns.h:32
TownManager::findTown
TownList::iterator findTown(uint32 townId)
Definition: towns.cpp:70
Position::isValid
bool isValid() const
Definition: position.h:182
uint32
uint32_t uint32
Definition: types.h:35
TownManager::TownManager
TownManager()
Definition: towns.cpp:34
TownManager::sort
void sort()
Definition: towns.cpp:76
towns.h
TownManager::getTownByName
const TownPtr & getTownByName(std::string name)
Definition: towns.cpp:61
Town::getId
uint32 getId()
Definition: towns.h:39
Position
Definition: position.h:33
TownManager::getTown
const TownPtr & getTown(uint32 townId)
Definition: towns.cpp:52
Town
Definition: towns.h:29
g_towns
TownManager g_towns
Definition: towns.cpp:25
stdext::shared_object_ptr< Town >
TownManager::addTown
void addTown(const TownPtr &town)
Definition: towns.cpp:39
Town::getName
std::string getName()
Definition: towns.h:40