00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef _TMW_SERVER_MAPCOMPOSITE_
00023 #define _TMW_SERVER_MAPCOMPOSITE_
00024
00025 #include <string>
00026 #include <vector>
00027
00028 class Actor;
00029 class Being;
00030 class Character;
00031 class Map;
00032 class MapComposite;
00033 class Point;
00034 class Rectangle;
00035 class Script;
00036 class Thing;
00037
00038 struct MapContent;
00039 struct MapZone;
00040
00041 enum PvPRules
00042 {
00043 PVP_NONE,
00044 PVP_FREE
00045
00046 };
00047
00051 typedef std::vector< unsigned > MapRegion;
00052
00056 struct ZoneIterator
00057 {
00058 MapRegion region;
00059 unsigned pos;
00060 MapZone *current;
00061 const MapContent *map;
00062
00063 ZoneIterator(const MapRegion &, const MapContent *);
00064 void operator++();
00065 MapZone *operator*() const { return current; }
00066 operator bool() const { return current; }
00067 };
00068
00072 struct CharacterIterator
00073 {
00074 ZoneIterator iterator;
00075 unsigned short pos;
00076 Character *current;
00077
00078 CharacterIterator(const ZoneIterator &);
00079 void operator++();
00080 Character *operator*() const { return current; }
00081 operator bool() const { return iterator; }
00082 };
00083
00087 struct BeingIterator
00088 {
00089 ZoneIterator iterator;
00090 unsigned short pos;
00091 Being *current;
00092
00093 BeingIterator(const ZoneIterator &);
00094 void operator++();
00095 Being *operator*() const { return current; }
00096 operator bool() const { return iterator; }
00097 };
00098
00102 struct FixedActorIterator
00103 {
00104 ZoneIterator iterator;
00105 unsigned short pos;
00106 Actor *current;
00107
00108 FixedActorIterator(const ZoneIterator &);
00109 void operator++();
00110 Actor *operator*() const { return current; }
00111 operator bool() const { return iterator; }
00112 };
00113
00117 struct ActorIterator
00118 {
00119 ZoneIterator iterator;
00120 unsigned short pos;
00121 Actor *current;
00122
00123 ActorIterator(const ZoneIterator &);
00124 void operator++();
00125 Actor *operator*() const { return current; }
00126 operator bool() const { return iterator; }
00127 };
00128
00132 struct MapZone
00133 {
00134 unsigned short nbCharacters, nbMovingObjects;
00140 std::vector< Actor * > objects;
00141
00147 MapRegion destinations;
00148
00149 MapZone(): nbCharacters(0), nbMovingObjects(0) {}
00150 void insert(Actor *);
00151 void remove(Actor *);
00152 };
00153
00159 struct ObjectBucket
00160 {
00161 static int const int_bitsize = sizeof(unsigned) * 8;
00162 unsigned bitmap[256 / int_bitsize];
00163 short free;
00164 short next_object;
00165 Actor *objects[256];
00166
00167 ObjectBucket();
00168 int allocate();
00169 void deallocate(int);
00170 };
00171
00175 struct MapContent
00176 {
00177 MapContent(Map *);
00178 ~MapContent();
00179
00183 bool allocate(Actor *);
00184
00188 void deallocate(Actor *);
00189
00193 void fillRegion(MapRegion &, const Point &, int) const;
00194
00198 void fillRegion(MapRegion &, const Rectangle &) const;
00199
00203 MapZone &getZone(const Point &pos) const;
00204
00208 std::vector< Thing * > things;
00209
00213 ObjectBucket *buckets[256];
00214
00215 int last_bucket;
00220 MapZone *zones;
00221
00222 unsigned short mapWidth;
00223 unsigned short mapHeight;
00224 };
00225
00229 class MapComposite
00230 {
00231 public:
00235 MapComposite(int id, const std::string &name);
00236
00240 ~MapComposite();
00241
00246 void setMap(Map *);
00247
00251 Map *getMap() const
00252 { return mMap; }
00253
00257 void setScript(Script *s)
00258 { mScript = s; }
00259
00263 Script *getScript() const
00264 { return mScript; }
00265
00269 bool isActive() const
00270 { return mMap; }
00271
00275 int getID() const
00276 { return mID; }
00277
00281 const std::string &getName() const
00282 { return mName; }
00283
00287 bool insert(Thing *);
00288
00292 void remove(Thing *);
00293
00297 void update();
00298
00302 PvPRules getPvP() const { return mPvPRules; }
00303
00307 ZoneIterator getWholeMapIterator() const
00308 { return ZoneIterator(MapRegion(), mContent); }
00309
00313 ZoneIterator getInsideRectangleIterator(const Rectangle &) const;
00314
00318 ZoneIterator getAroundPointIterator(const Point &, int radius) const;
00319
00323 ZoneIterator getAroundActorIterator(Actor *, int radius) const;
00324
00329 ZoneIterator getAroundBeingIterator(Being *, int radius) const;
00330
00334 const std::vector< Thing * > &getEverything() const;
00335
00336 private:
00337 MapComposite(const MapComposite &);
00338
00339 Map *mMap;
00340 MapContent *mContent;
00341 Script *mScript;
00342 std::string mName;
00343 unsigned short mID;
00345 PvPRules mPvPRules;
00346 };
00347
00348 #endif