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 
29 #include "src/engines/nwn/object.h"
30 #include "src/engines/nwn/module.h"
31 #include "src/engines/nwn/area.h"
35 #include "src/engines/nwn/door.h"
38 
39 namespace Engines {
40 
41 namespace NWN {
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<NWN::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<NWN::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 NWN
170 
171 } // End of namespace Engines
T ABS(T x)
Definition: util.h:69
A container of Neverwinter Nights objects.
std::list< NWN::Object * > ::const_iterator iterator
static Creature * toCreature(Aurora::NWScript::Object *object)
A placeable object in a Neverwinter Nights area.
static Module * toModule(Aurora::NWScript::Object *object)
bool isPC() const
Is the creature a player character?
Definition: creature.cpp:226
static NWN::Object * toObject(::Aurora::NWScript::Object *object)
STL namespace.
An area in Neverwinter Nights, holding all objects and room tiles within, as well as general area pro...
Definition: area.h:63
static Location * toLocation(Aurora::NWScript::EngineType *engineType)
static Door * toDoor(Aurora::NWScript::Object *object)
bool operator()(NWN::Object *a, NWN::Object *b)
The context needed to run a Neverwinter Nights module.
SearchType(const iterator &a, const iterator &b)
The context holding a Neverwinter Nights area.
virtual void getPosition(float &x, float &y, float &z) const
Return the object&#39;s position within its area.
Definition: object.cpp:156
Utility templates and functions.
static Creature * toPC(Aurora::NWScript::Object *object)
::Aurora::NWScript::ObjectSearch * findObjectsByType(ObjectType type) const
Return a search context to iterate over all objects of this type.
Low-level type definitions to handle fixed width types portably.
void removeObject(Object &object)
Remove an object from this container.
Object * get()
Return the current object in the search context.
void addObject(NWN::Object &object)
Add an object to this container.
float getDistance(NWN::Object &a)
ObjectType
Object type, matches the bitfield in nwscript.nss.
Definition: types.h:36
An object in a Neverwinter Nights area.
static Placeable * toPlaceable(Aurora::NWScript::Object *object)
static Area * toArea(Aurora::NWScript::Object *object)
static Waypoint * toWaypoint(Aurora::NWScript::Object *object)
::Aurora::NWScript::Object * getObject(const iterator &t)
A creature in a Neverwinter Nights area.
A location within a Neverwinter Nights area.
A situated object in a Neverwinter Nights area.
void removeObject(NWN::Object &object)
Remove an object from this container.
::Aurora::NWScript::Object * getFirstObjectByType(ObjectType type) const
Return the first object of this type.
A door in a Neverwinter Nights area.
A waypoint in a Neverwinter Nights area.
static Situated * toSituated(Aurora::NWScript::Object *object)
void addObject(Object &object)
Add an object to this container.
ObjectDistanceSort(const NWN::Object &target)