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 
31 #include "src/engines/nwn2/area.h"
35 #include "src/engines/nwn2/door.h"
38 
39 namespace Engines {
40 
41 namespace NWN2 {
42 
44  target.getPosition(xt, yt, zt);
45 }
46 
48  return getDistance(*a) < getDistance(*b);
49 }
50 
52  float x, y, z;
53  a.getPosition(x, y, z);
54 
55  return ABS(x - xt) + ABS(y - yt) + ABS(z - zt);
56 }
57 
58 
59 class SearchType : public ::Aurora::NWScript::SearchRange< std::list<NWN2::Object *> > {
60 public:
61  SearchType(const iterator &a, const iterator &b) : ::Aurora::NWScript::SearchRange<type>(std::make_pair(a, b)) { }
62  SearchType(const type &l) : ::Aurora::NWScript::SearchRange<type>(std::make_pair(l.begin(), l.end())) { }
64 
65  ::Aurora::NWScript::Object *getObject(const iterator &t) { return *t; }
66 };
67 
68 
70 }
71 
73 }
74 
76  lock();
77 
78  _objects.clear();
79 
81 
82  unlock();
83 }
84 
86  lock();
87 
89 
90  _objects[object.getType()].push_back(&object);
91 
92  unlock();
93 }
94 
96  lock();
97 
98  _objects[object.getType()].remove(&object);
99 
101 
102  unlock();
103 }
104 
106  ObjectMap::const_iterator l = _objects.find(type);
107  if (l == _objects.end())
108  return 0;
109 
110  SearchType ctx(l->second);
111 
112  return ctx.get();
113 }
114 
116  static const ObjectList kEmptyObjectList;
117 
118  ObjectMap::const_iterator l = _objects.find(type);
119  if (l == _objects.end())
120  return new SearchType(kEmptyObjectList.begin(), kEmptyObjectList.end());
121 
122  return new SearchType(l->second);
123 }
124 
126  return dynamic_cast<NWN2::Object *>(object);
127 }
128 
130  return dynamic_cast<Module *>(object);
131 }
132 
134  return dynamic_cast<Area *>(object);
135 }
136 
138  return dynamic_cast<Waypoint *>(object);
139 }
140 
142  return dynamic_cast<Situated *>(object);
143 }
144 
146  return dynamic_cast<Placeable *>(object);
147 }
148 
150  return dynamic_cast<Door *>(object);
151 }
152 
154  return dynamic_cast<Creature *>(object);
155 }
156 
158  Creature *pc = dynamic_cast<Creature *>(object);
159  if (!pc || !pc->isPC())
160  return 0;
161 
162  return pc;
163 }
164 
166  return dynamic_cast<Location *>(engineType);
167 }
168 
169 } // End of namespace NWN2
170 
171 } // End of namespace Engines
T ABS(T x)
Definition: util.h:69
static NWN2::Object * toObject(::Aurora::NWScript::Object *object)
std::list< NWN2::Object * > ::const_iterator iterator
An area in Neverwinter Nights 2, holding all objects, room tiles and terrain within, as well as general area properties like the current background music and ambient sounds.
Definition: area.h:65
The context holding a Neverwinter Nights 2 area.
A location within a Neverwinter Nights 2 area.
STL namespace.
static Creature * toCreature(Aurora::NWScript::Object *object)
float getDistance(NWN2::Object &a)
An object within a NWN2 area.
Definition: object.h:58
The context needed to run a Neverwinter Nights 2 module.
A creature in a Neverwinter Nights 2 area.
A container of Neverwinter Nights 2 objects.
void removeObject(NWN2::Object &object)
Remove an object from this container.
Utility templates and functions.
SearchType(const iterator &a, const iterator &b)
static Location * toLocation(Aurora::NWScript::EngineType *engineType)
static Situated * toSituated(Aurora::NWScript::Object *object)
static Module * toModule(Aurora::NWScript::Object *object)
::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 addObject(NWN2::Object &object)
Add an object to this container.
bool operator()(NWN2::Object *a, NWN2::Object *b)
Low-level type definitions to handle fixed width types portably.
::Aurora::NWScript::Object * getFirstObjectByType(ObjectType type) const
Return the first object of this type.
void removeObject(Object &object)
Remove an object from this container.
Object * get()
Return the current object in the search context.
bool isPC() const
Is the creature a player character?
Definition: creature.cpp:201
::Aurora::NWScript::Object * getObject(const iterator &t)
A situated object in a Neverwinter Nights 2 area.
A door in a Neverwinter Nights 2 area.
static Door * toDoor(Aurora::NWScript::Object *object)
A placeable object in a Neverwinter Nights 2 area.
virtual void getPosition(float &x, float &y, float &z) const
Return the object&#39;s position within its area.
Definition: object.cpp:142
An object in a Neverwinter Nights 2 area.
ObjectType
Object type, matches the bitfield in nwscript.nss.
Definition: types.h:35
ObjectDistanceSort(const NWN2::Object &target)
static Area * toArea(Aurora::NWScript::Object *object)
static Creature * toPC(Aurora::NWScript::Object *object)
void addObject(Object &object)
Add an object to this container.
A waypoint in a Neverwinter Nights 2 area.
static Waypoint * toWaypoint(Aurora::NWScript::Object *object)