ScummVM API documentation
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 ULTIMA_SHARED_CORE_MAP_H
23 #define ULTIMA_SHARED_CORE_MAP_H
24 
25 #include "common/array.h"
26 #include "common/ptr.h"
27 #include "common/serializer.h"
28 #include "ultima/shared/core/rect.h"
29 #include "ultima/shared/gfx/dungeon_surface.h"
30 
31 namespace Ultima {
32 namespace Shared {
33 
34 #define REGISTER_WIDGET(NAME) if (name == #NAME) return new Widgets::NAME(_game, (Ultima1Map::MapBase *)map)
35 #define DECLARE_WIDGET(NAME) const char *getClassName() const override { return #NAME; }
36 
37 enum Direction {
38  DIR_NONE = 0,
39  DIR_LEFT = 1, DIR_RIGHT = 2, DIR_UP = 3, DIR_DOWN = 4,
40  DIR_WEST = 1, DIR_EAST = 2, DIR_NORTH = 3, DIR_SOUTH = 4
41 };
42 
43 typedef byte MapCell;
44 typedef int MapId;
45 
46 class Game;
47 class MapWidget;
48 
49 typedef Common::SharedPtr<MapWidget> MapWidgetPtr;
50 
54 class MapTile {
55 public:
56  int _tileId; // Tile Id
57  int _tileNum; // Tile number to display. Normally equals Tile Id, but can differ in rare cases
58  int _widgetNum; // Widget number, if any
59  MapWidget *_widget; // Widget pointer
60  int _itemNum; // Item number, if any
61  // Dungeon tile flags
62  bool _isDoor, _isSecretDoor;
63  bool _isLadderUp, _isLadderDown;
64  bool _isWall, _isHallway, _isBeams;
65 public:
69  MapTile() : _tileNum(-1), _tileId(-1), _widgetNum(-1), _widget(nullptr), _itemNum(-1),
70  _isDoor(false), _isSecretDoor(false), _isLadderUp(false), _isLadderDown(false), _isWall(false),
71  _isHallway(false), _isBeams(false) {}
72 
76  virtual ~MapTile() {}
77 
81  virtual void clear();
82 
86  bool isDoor() const { return _isDoor; }
87 
91  bool isWallOrSecretDoor() const { return _isWall || _isSecretDoor; }
92 
96  bool isWallOrDoorway() const { return _isWall || _isDoor || _isSecretDoor; }
97 
101  bool isSolid() const { return !(_isHallway || _isLadderUp || _isLadderDown || _isBeams); }
102 };
103 
107 class Map {
112  struct ViewportPosition {
113  Point _topLeft; // Top, left tile position for viewport
114  Point _size; // Size of the viewport. Just in case we ever allow it to change
115  MapId _mapId; // Maze the viewport is for. Used to detect when the map changes
116 
120  ViewportPosition() : _topLeft(-1, -1), _mapId(-1) {}
121 
125  bool isValid() const { return _mapId != -1; }
126 
130  void synchronize(Common::Serializer &s) {
131  s.syncAsUint16LE(_topLeft.x);
132  s.syncAsUint16LE(_topLeft.y);
133  }
134 
138  void reset() { _mapId = -1; }
139  };
140 
144  struct MapCellsRow {
145  public:
147  public:
148  byte &operator[](int idx) { return _data[idx]; }
149  byte operator[](int idx) const { return _data[idx]; }
150  };
151 public:
155  class MapBase {
156  private:
157  Map *_map; // Map manager reference
158  protected:
159  MapId _mapId; // The map Id
160  uint _mapIndex; // Index of map within the group of same maps
161  uint _mapStyle; // Map style category for towns & castles
162  ViewportPosition _viewportPos; // Viewport position
163  protected:
167  void setDimensions(const Point &size);
168  public:
169  Point _size; // X, Y size of the map
170  Point _tilesPerOrigTile; // For enhanced modes, number of tiles per original game tile
171  Common::String _name; // Name of map, if applicable
172  MapWidget *_playerWidget; // Current means of transport, even if on foot
173  Common::Array<MapWidgetPtr> _widgets; // Party, monsteres, transports, etc.
174  Common::Array<MapCellsRow> _data; // Data for the map
175  public:
179  MapBase(Game *game, Map *map) : _map(map), _playerWidget(nullptr), _mapId(0), _mapIndex(0),
180  _mapStyle(0) {}
181 
185  virtual ~MapBase() {}
186 
190  virtual void synchronize(Common::Serializer &s);
191 
195  void addWidget(MapWidget *widget);
196 
200  void removeWidget(MapWidget *widget);
201 
205  virtual void clear();
206 
210  virtual void getTileAt(const Point &pt, MapTile *tile);
211 
215  void resetViewport();
216 
220  virtual Point getViewportPosition(const Point &viewportSize);
221 
225  virtual void load(MapId mapId);
226 
232  virtual bool changeLevel(int delta) { return true; }
233 
237  virtual uint getLevel() const { return 0; }
238 
242  virtual bool isMapWrapped() const { return false; }
243 
247  size_t width() const { return _size.x; }
248 
252  size_t height() const { return _size.y; }
253 
257  Point getPosition() const;
258 
262  void setPosition(const Point &pt);
263 
267  Direction getDirection() const;
268 
272  void setDirection(Direction dir);
273 
277  Point getDirectionDelta() const;
278 
282  virtual Point getDeltaPosition(const Point &delta);
283 
287  MapId getMapId() const { return _mapId; }
288 
292  uint getMapIndex() const { return _mapIndex; }
293 
297  virtual void shiftViewport(const Point &delta);
298 
302  virtual void update();
303  };
304 protected:
305  MapBase *_mapArea;
306 public:
310  Map() : _mapArea(nullptr) {}
311 
315  virtual ~Map() {}
316 
320  virtual void load(MapId mapId);
321 
325  virtual void clear();
326 
330  virtual void synchronize(Common::Serializer &s);
331 
335  virtual MapWidget *createWidget(Map::MapBase *map, const Common::String &name) = 0;
336 
340  void getTileAt(const Point &pt, MapTile *tile) {
341  assert(_mapArea);
342  return _mapArea->getTileAt(pt, tile);
343  }
344 
348  Point getViewportPosition(const Point &viewportSize) {
349  assert(_mapArea);
350  return _mapArea->getViewportPosition(viewportSize);
351  }
352 
356  size_t width() const {
357  assert(_mapArea);
358  return _mapArea->width();
359  }
360 
364  size_t height() const {
365  assert(_mapArea);
366  return _mapArea->height();
367  }
368 
372  Point getPosition() const {
373  assert(_mapArea);
374  return _mapArea->getPosition();
375  }
376 
380  void setPosition(const Point &pt) {
381  assert(_mapArea);
382  _mapArea->setPosition(pt);
383  }
384 
388  Direction getDirection() const {
389  assert(_mapArea);
390  return _mapArea->getDirection();
391  }
392 
396  void setDirection(Direction dir) {
397  assert(_mapArea);
398  _mapArea->setDirection(dir);
399  }
400 
405  assert(_mapArea);
406  return _mapArea->getDirectionDelta();
407  }
408 
412  Point getDeltaPosition(const Point &delta) {
413  assert(_mapArea);
414  return _mapArea->getDeltaPosition(delta);
415  }
416 
420  void shiftViewport(const Point &delta) {
421  assert(_mapArea);
422  _mapArea->shiftViewport(delta);
423  }
424 
430  assert(_mapArea);
431  return _mapArea->_tilesPerOrigTile;
432  }
433 
438  assert(_mapArea);
439  return _mapArea->_name;
440  }
441 
446  assert(_mapArea);
447  return _mapArea->_playerWidget;
448  }
449 
454  bool changeLevel(int delta) {
455  assert(_mapArea);
456  return _mapArea->changeLevel(delta);
457  }
458 
462  uint getLevel() const {
463  assert(_mapArea);
464  return _mapArea->getLevel();
465  }
466 
470  bool isMapWrapped() const {
471  assert(_mapArea);
472  return _mapArea->isMapWrapped();
473  }
474 
478  void update() {
479  assert(_mapArea);
480  return _mapArea->update();
481  }
482 };
483 
487 class MapWidget {
488 protected:
489  Game *_game; // Game reference
490  Map::MapBase *_map; // Map reference
491 public:
492  Point _position; // Position within the map
493  Direction _direction; // Direction
494  Common::String _name; // Name of widget
495 public:
499  MapWidget(Game *game, Map::MapBase *map) : _game(game), _map(map) {}
500  MapWidget(Game *game, Map::MapBase *map, const Point &pt, Direction dir = DIR_NONE) : _game(game), _map(map), _position(pt), _direction(dir) {}
501  MapWidget(Game *game, Map::MapBase *map, const Common::String &name, const Point &pt, Direction dir = DIR_NONE) :
502  _game(game), _map(map), _name(name), _position(pt), _direction(dir) {}
503 
507  virtual ~MapWidget() {}
508 
512  virtual const char *getClassName() const { return nullptr; }
513 
517  virtual void synchronize(Common::Serializer &s);
518 
524  void addInfoMsg(const Common::String &text, bool newLine = true);
525 
529  virtual uint getTileNum() const { return 0; }
530 
534  virtual bool isBlocking() const { return false; }
535 
541  virtual void update(bool isPreUpdate) {}
542 
543  enum CanMove { UNSET = 0, YES = 1, NO = 2 };
544 
548  virtual CanMove canMoveTo(const Point &destPos);
549 
556  virtual void moveTo(const Point &destPos, Direction dir = DIR_NONE);
557 };
558 
559 } // End of namespace Shared
560 } // End of namespace Ultima
561 
562 #endif
Direction getDirection() const
Definition: map.h:388
bool isWallOrSecretDoor() const
Definition: map.h:91
MapWidget(Game *game, Map::MapBase *map)
Definition: map.h:499
virtual uint getTileNum() const
Definition: map.h:529
Definition: str.h:59
virtual const char * getClassName() const
Definition: map.h:512
virtual void update(bool isPreUpdate)
Definition: map.h:541
bool isWallOrDoorway() const
Definition: map.h:96
Definition: game.h:42
Definition: map.h:107
virtual Point getViewportPosition(const Point &viewportSize)
Definition: map.h:54
Point getViewportPosition(const Point &viewportSize)
Definition: map.h:348
void getTileAt(const Point &pt, MapTile *tile)
Definition: map.h:340
Common::String getName() const
Definition: map.h:437
size_t height() const
Definition: map.h:252
void setDirection(Direction dir)
MapTile()
Definition: map.h:69
Point getDirectionDelta() const
Definition: map.h:404
Definition: map.h:487
virtual void clear()
bool changeLevel(int delta)
Definition: map.h:454
virtual ~MapBase()
Definition: map.h:185
virtual ~MapWidget()
Definition: map.h:507
virtual bool changeLevel(int delta)
Definition: map.h:232
virtual ~MapTile()
Definition: map.h:76
bool isMapWrapped() const
Definition: map.h:470
Definition: serializer.h:79
Direction getDirection() const
virtual void shiftViewport(const Point &delta)
uint getMapIndex() const
Definition: map.h:292
Definition: detection.h:27
virtual uint getLevel() const
Definition: map.h:237
void shiftViewport(const Point &delta)
Definition: map.h:420
Point getTilesPerOrigTile() const
Definition: map.h:429
MapId getMapId() const
Definition: map.h:287
virtual bool isBlocking() const
Definition: map.h:534
size_t width() const
Definition: map.h:247
void update()
Definition: map.h:478
void setPosition(const Point &pt)
Definition: map.h:380
void setDirection(Direction dir)
Definition: map.h:396
bool isSolid() const
Definition: map.h:101
Definition: rect.h:45
MapBase(Game *game, Map *map)
Definition: map.h:179
uint getLevel() const
Definition: map.h:462
virtual void getTileAt(const Point &pt, MapTile *tile)
size_t height() const
Definition: map.h:364
int16 x
Definition: rect.h:46
Definition: map.h:155
Map()
Definition: map.h:310
virtual ~Map()
Definition: map.h:315
MapWidget * getPlayerWidget() const
Definition: map.h:445
int16 y
Definition: rect.h:47
Point getDeltaPosition(const Point &delta)
Definition: map.h:412
Point getPosition() const
Definition: map.h:372
bool isDoor() const
Definition: map.h:86
Definition: ptr.h:159
virtual Point getDeltaPosition(const Point &delta)
size_t width() const
Definition: map.h:356
virtual bool isMapWrapped() const
Definition: map.h:242
void setPosition(const Point &pt)