ScummVM API documentation
current_map.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef ULTIMA8_WORLD_CURRENTMAP_H
23 #define ULTIMA8_WORLD_CURRENTMAP_H
24 
25 #include "ultima/shared/std/containers.h"
26 #include "ultima/ultima8/usecode/intrinsics.h"
27 #include "ultima/ultima8/world/position_info.h"
28 #include "ultima/ultima8/misc/direction.h"
29 #include "ultima/ultima8/misc/point3.h"
30 
31 namespace Ultima {
32 namespace Ultima8 {
33 
34 struct Box;
35 class Map;
36 class Item;
37 class UCList;
38 class TeleportEgg;
39 class EggHatcherProcess;
40 
41 #define MAP_NUM_CHUNKS 64
42 #define MAP_NUM_TARGET_ITEMS 200
43 
44 class CurrentMap {
45  friend class World;
46 public:
47  CurrentMap();
48  ~CurrentMap();
49 
50  void clear();
51  void writeback();
52  void loadMap(Map *map);
53 
56  void setMap(Map *map) {
57  _currentMap = map;
58  }
59 
61  uint32 getNum() const;
62 
63  unsigned int getChunkSize() const {
64  return _mapChunkSize;
65  }
66 
68  void addItem(Item *item);
69 
71  void addItemToEnd(Item *item);
72 
73  void removeItemFromList(Item *item, int32 oldx, int32 oldy);
74  void removeItem(Item *item);
75 
77  void addTargetItem(const Item *item);
79  void removeTargetItem(const Item *item);
81  Item *findBestTargetItem(int32 x, int32 y, int32 z, Direction dir, DirectionMode dirmode);
82 
84  void updateFastArea(const Point3 &from, const Point3 &to);
85 
96  void areaSearch(UCList *itemlist, const uint8 *loopscript,
97  uint32 scriptsize, const Item *item, uint16 range,
98  bool recurse, int32 x = 0, int32 y = 0) const;
99 
100  // Surface search: Search above and below an item.
101  void surfaceSearch(UCList *itemlist, const uint8 *loopscript,
102  uint32 scriptsize, const Item *item, bool above,
103  bool below, bool recurse = false) const;
104 
105  // Collision detection. Returns position information with valid being true
106  // when the target box does not collide with any solid items.
107  // Ignores collisions when overlapping with the start box.
108  PositionInfo getPositionInfo(const Box &target, const Box &start, uint32 shapeflags, ObjId id) const;
109 
110  // Note that this version of getPositionInfo can not take 'flipped' into account!
111  PositionInfo getPositionInfo(int32 x, int32 y, int32 z, uint32 shape, ObjId id) const;
112 
114  bool scanForValidPosition(int32 x, int32 y, int32 z, const Item *item,
115  Direction movedir, bool wantsupport,
116  int32 &tx, int32 &ty, int32 &tz);
117 
118  struct SweepItem {
119  SweepItem(ObjId it, int32 ht, int32 et, bool touch,
120  bool touchfloor, bool block, uint8 dir)
121  : _item(it), _hitTime(ht), _endTime(et), _touching(touch),
122  _touchingFloor(touchfloor), _blocking(block), _dirs(dir) { }
123 
124  ObjId _item; // Item that was hit
125 
126  //
127  // The time values here are 'normalized' fixed point values
128  // They range from 0 for the start of the move to 0x4000 for the end of
129  // The move.
130  //
131  // Linear interpolate between the start and end positions using
132  // hit_time to find where the moving item was when the hit occurs
133  //
134 
135  int32 _hitTime; // if -1, already hitting when sweep started.
136  int32 _endTime; // if 0x4000, still hitting when sweep finished
137 
138  bool _touching; // We are only touching (don't actually overlap)
139  bool _touchingFloor; // touching and directly below the moving item
140 
141  bool _blocking; // This item blocks the moving item
142 
143  uint8 _dirs; // Directions in which the item is being hit.
144  // Bitmask. Bit 0 is x, 1 is y, 2 is z.
145 
146  // Use this func to get the interpolated location of the hit
147  Point3 GetInterpolatedCoords(const Point3 &start, const Point3 &end) const {
148  Point3 pt;
149  pt.x = start.x + ((end.x - start.x) * (_hitTime >= 0 ? _hitTime : 0) + (end.x > start.x ? 0x2000 : -0x2000)) / 0x4000;
150  pt.y = start.y + ((end.y - start.y) * (_hitTime >= 0 ? _hitTime : 0) + (end.y > start.y ? 0x2000 : -0x2000)) / 0x4000;
151  pt.z = start.z + ((end.z - start.z) * (_hitTime >= 0 ? _hitTime : 0) + (end.z > start.z ? 0x2000 : -0x2000)) / 0x4000;
152  return pt;
153  }
154  };
155 
168  bool sweepTest(const Point3 &start, const Point3 &end,
169  const int32 dims[3], uint32 shapeflags,
170  ObjId item, bool solid_only, Std::list<SweepItem> *hit) const;
171 
172  TeleportEgg *findDestination(uint16 id);
173 
174  // Not allowed to modify the list. Remember to use const_iterator
175  const Std::list<Item *> *getItemList(int32 gx, int32 gy) const;
176 
177  bool isChunkFast(int32 cx, int32 cy) const {
178  // CONSTANTS!
179  if (cx < 0 || cy < 0 || cx >= MAP_NUM_CHUNKS || cy >= MAP_NUM_CHUNKS)
180  return false;
181  return (_fast[cy][cx / 32] & (1 << (cx & 31))) != 0;
182  }
183 
184  void setFastAtPoint(const Point3 &pt);
185 
186  // Set the entire map as being 'fast'
187  void setWholeMapFast();
188 
189  void save(Common::WriteStream *ws);
190  bool load(Common::ReadStream *rs, uint32 version);
191 
192  INTRINSIC(I_canExistAt);
193  INTRINSIC(I_canExistAtPoint);
194 
195 private:
196  void loadItems(const Std::list<Item *> &itemlist, bool callCacheIn);
197  void createEggHatcher();
198 
200  static void clipMapChunks(int &minx, int &maxx, int &miny, int &maxy);
201 
202  Map *_currentMap;
203 
204  // item lists. Lots of them :-)
205  // items[x][y]
206  Std::list<Item *> _items[MAP_NUM_CHUNKS][MAP_NUM_CHUNKS];
207 
208  ProcId _eggHatcher;
209 
210  // Fast area bit masks -> fast[ry][rx/32]&(1<<(rx&31));
211  uint32 _fast[MAP_NUM_CHUNKS][MAP_NUM_CHUNKS / 32];
212  int32 _fastXMin, _fastYMin, _fastXMax, _fastYMax;
213 
214  int _mapChunkSize;
215 
218  ObjId _targets[MAP_NUM_TARGET_ITEMS];
219 
220  void setChunkFast(int32 cx, int32 cy);
221  void unsetChunkFast(int32 cx, int32 cy);
222 };
223 
224 } // End of namespace Ultima8
225 } // End of namespace Ultima
226 
227 #endif
void addTargetItem(const Item *item)
Add an item to the list of possible targets (in Crusader)
Definition: stream.h:77
Definition: map.h:32
void removeTargetItem(const Item *item)
Remove an item from the list of possible targets (in Crusader)
Definition: item.h:42
Definition: point3.h:28
bool sweepTest(const Point3 &start, const Point3 &end, const int32 dims[3], uint32 shapeflags, ObjId item, bool solid_only, Std::list< SweepItem > *hit) const
void addItem(Item *item)
Add an item to the beginning of the item list.
Definition: box.h:36
Definition: detection.h:27
Definition: position_info.h:44
Definition: world.h:72
uint32 getNum() const
Get the map number of the CurrentMap.
void areaSearch(UCList *itemlist, const uint8 *loopscript, uint32 scriptsize, const Item *item, uint16 range, bool recurse, int32 x=0, int32 y=0) const
Definition: current_map.h:118
Item * findBestTargetItem(int32 x, int32 y, int32 z, Direction dir, DirectionMode dirmode)
Find the best target item in the given direction from the given start point.
bool scanForValidPosition(int32 x, int32 y, int32 z, const Item *item, Direction movedir, bool wantsupport, int32 &tx, int32 &ty, int32 &tz)
Scan for a valid position for item in directions orthogonal to movedir.
Definition: uc_list.h:41
Definition: containers.h:200
Definition: stream.h:385
void addItemToEnd(Item *item)
Add an item to the end of the item list.
void updateFastArea(const Point3 &from, const Point3 &to)
Update the fast area for the cameras position.
Definition: current_map.h:44
Definition: teleport_egg.h:30
void setMap(Map *map)
Definition: current_map.h:56