xoreos  0.0.5
objectcontainer.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "src/common/types.h"
26 #include "src/common/util.h"
27 
37 
38 namespace Engines {
39 
40 namespace KotOR2 {
41 
43  target.getPosition(xt, yt, zt);
44 }
45 
47  return getDistance(*a) < getDistance(*b);
48 }
49 
51  float x, y, z;
52  a.getPosition(x, y, z);
53 
54  return ABS(x - xt) + ABS(y - yt) + ABS(z - zt);
55 }
56 
57 
58 class SearchType : public ::Aurora::NWScript::SearchRange< std::list<KotOR2::Object *> > {
59 public:
60  SearchType(const iterator &a, const iterator &b) : ::Aurora::NWScript::SearchRange<type>(std::make_pair(a, b)) { }
61  SearchType(const type &l) : ::Aurora::NWScript::SearchRange<type>(std::make_pair(l.begin(), l.end())) { }
63 
64  ::Aurora::NWScript::Object *getObject(const iterator &t) { return *t; }
65 };
66 
67 
69 }
70 
72 }
73 
75  lock();
76 
77  _objects.clear();
78 
80 
81  unlock();
82 }
83 
85  lock();
86 
88 
89  _objects[object.getType()].push_back(&object);
90 
91  unlock();
92 }
93 
95  lock();
96 
97  _objects[object.getType()].remove(&object);
98 
100 
101  unlock();
102 }
103 
105  ObjectMap::const_iterator l = _objects.find(type);
106  if (l == _objects.end())
107  return 0;
108 
109  SearchType ctx(l->second);
110 
111  return ctx.get();
112 }
113 
115  static const ObjectList kEmptyObjectList;
116 
117  ObjectMap::const_iterator l = _objects.find(type);
118  if (l == _objects.end())
119  return new SearchType(kEmptyObjectList.begin(), kEmptyObjectList.end());
120 
121  return new SearchType(l->second);
122 }
123 
125  return dynamic_cast<KotOR2::Object *>(object);
126 }
127 
129  return dynamic_cast<Module *>(object);
130 }
131 
133  return dynamic_cast<Area *>(object);
134 }
135 
137  return dynamic_cast<Waypoint *>(object);
138 }
139 
141  return dynamic_cast<Situated *>(object);
142 }
143 
145  return dynamic_cast<Placeable *>(object);
146 }
147 
149  return dynamic_cast<Door *>(object);
150 }
151 
153  return dynamic_cast<Creature *>(object);
154 }
155 
157  Creature *pc = dynamic_cast<Creature *>(object);
158  if (!pc || !pc->isPC())
159  return 0;
160 
161  return pc;
162 }
163 
165  Creature *partyMember = dynamic_cast<Creature *>(object);
166  if (!partyMember || !partyMember->isPartyMember())
167  return 0;
168 
169  return partyMember;
170 }
171 
172 } // End of namespace KotOR2
173 
174 } // End of namespace Engines
void addObject(KotOR2::Object &object)
Add an object to this container.
T ABS(T x)
Definition: util.h:69
A creature in a Star Wars: Knights of the Old Republic II - The Sith Lords area.
bool isPartyMember() const
Definition: creature.cpp:85
virtual void getPosition(float &x, float &y, float &z) const
Return the object&#39;s position within its area.
Definition: object.cpp:107
std::list< KotOR2::Object * > ::const_iterator iterator
A container of Star Wars: Knights of the Old Republic II - The Sith Lords objects.
float getDistance(KotOR2::Object &a)
bool operator()(KotOR2::Object *a, KotOR2::Object *b)
static Creature * toPartyMember(Aurora::NWScript::Object *object)
STL namespace.
The context holding a Star Wars: Knights of the Old Republic II - The Sith Lords area.
::Aurora::NWScript::ObjectSearch * findObjectsByType(ObjectType type) const
Return a search context to iterate over all objects of this type.
static Placeable * toPlaceable(Aurora::NWScript::Object *object)
void removeObject(KotOR2::Object &object)
Remove an object from this container.
A placeable in a Star Wars: Knights of the Old Republic II - The Sith Lords area. ...
A door in a Star Wars: Knights of the Old Republic II - The Sith Lords area.
An object in a Star Wars: Knights of the Old Republic II - The Sith Lords area.
Utility templates and functions.
static Creature * toPC(Aurora::NWScript::Object *object)
Low-level type definitions to handle fixed width types portably.
ObjectDistanceSort(const KotOR2::Object &target)
void removeObject(Object &object)
Remove an object from this container.
Object * get()
Return the current object in the search context.
::Aurora::NWScript::Object * getFirstObjectByType(ObjectType type) const
Return the first object of this type.
The context needed to run a Star Wars: Knights of the Old Republic II - The Sith Lords module...
static Door * toDoor(Aurora::NWScript::Object *object)
static Waypoint * toWaypoint(Aurora::NWScript::Object *object)
A situated object in a Star Wars: Knights of the Old Republic II - The Sith Lords area...
bool isPC() const
Is the creature a player character?
Definition: creature.cpp:81
ObjectType
Object type, matches the bitfield in nwscript.nss.
Definition: types.h:33
static KotOR2::Object * toObject(::Aurora::NWScript::Object *object)
A waypoint in a Star Wars: Knights of the Old Republic II - The Sith Lords area.
static Area * toArea(Aurora::NWScript::Object *object)
static Situated * toSituated(Aurora::NWScript::Object *object)
::Aurora::NWScript::Object * getObject(const iterator &t)
An area in Star Wars: Knights of the Old Republic II - The Sith Lords, holding all objects and rooms ...
Definition: area.h:64
static Module * toModule(Aurora::NWScript::Object *object)
void addObject(Object &object)
Add an object to this container.
static Creature * toCreature(Aurora::NWScript::Object *object)
SearchType(const iterator &a, const iterator &b)