ScummVM API documentation
space_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 ULTIMA1_DATA_SPACE_MAP_H
23#define ULTIMA1_DATA_SPACE_MAP_H
24
25#include "common/scummsys.h"
26#include "common/random.h"
27#include "common/serializer.h"
28
29namespace Ultima {
30namespace Ultima1 {
31namespace Data {
32
33// The galaxy is a 7x7 grid of sectors
34constexpr int SPACE_SECTOR_COUNT = 7;
35
36// The station sector, where the player arrives and can dock/land
37constexpr int SPACE_STATION_X = 3;
38constexpr int SPACE_STATION_Y = 3;
39
40// Up to 4 ships can be present in a sector at once (parked fighters, the
41// player's own ship, spawned attackers)
42constexpr int SPACE_SHIPS_PER_SECTOR = 4;
43
44// The overhead sector view's playable area - a ship drifting past this
45// seamlessly wraps around to the opposite edge, Asteroids-style. Matches
46// the drawable viewport setupView itself clears each frame (its own inset
47// frame within the outer game border, not the outer border itself), so a
48// ship's sprite is only ever actually invisible right as it wraps rather
49// than jumping there from comfortably mid-screen
50constexpr int SPACE_SECTOR_MIN_X = 16, SPACE_SECTOR_MAX_X = 303;
51constexpr int SPACE_SECTOR_MIN_Y = 16, SPACE_SECTOR_MAX_Y = 143;
52constexpr int SPACE_SECTOR_WRAP_WIDTH = SPACE_SECTOR_MAX_X - SPACE_SECTOR_MIN_X + 1;
53constexpr int SPACE_SECTOR_WRAP_HEIGHT = SPACE_SECTOR_MAX_Y - SPACE_SECTOR_MIN_Y + 1;
54
55// The station's fixed screen position/size within its own sector (3,3) -
56// drawStationGraphic's (x,y) and the raw bitmap's dimensions
57constexpr int SPACE_STATION_SCREEN_X = 60, SPACE_STATION_SCREEN_Y = 75;
58constexpr int SPACE_STATION_WIDTH = 21, SPACE_STATION_HEIGHT = 18;
59
60// The star/heat hazard and docking/encounter anchor bitmaps' dimensions, and
61// the raw bitmaps of all three sector graphics (row-major, 0 is transparent).
62// The collision checks need the same pixels the overhead view draws
63constexpr int SPACE_STAR_WIDTH = 19, SPACE_STAR_HEIGHT = 18;
64constexpr int SPACE_PLANET_WIDTH = 20, SPACE_PLANET_HEIGHT = 20;
65extern const int8 SPACE_STAR_BITMAP[SPACE_STAR_HEIGHT][SPACE_STAR_WIDTH];
66extern const int8 SPACE_PLANET_BITMAP[SPACE_PLANET_HEIGHT][SPACE_PLANET_WIDTH];
67extern const int8 SPACE_STATION_BITMAP[SPACE_STATION_HEIGHT][SPACE_STATION_WIDTH];
68
69// Direction names shown in the cockpit view, indexed by Data::Direction
70// (1-4): the arrow keys read as Left / Right / Climb / Dive rather than the
71// overhead view's West / East / North / South
72extern const char *SPACE_COCKPIT_DIRECTION_NAMES[5];
73
74// Ship types (SHIP_* in the disassembly)
75enum SpaceShipType {
76 SHIP_NONE = -1,
77 SHIP_SHUTTLE = 0, // the only type that can land back on Sosaria
78 SHIP_LARGE_FIGHTER = 1, // "Fighter 1" - high fuel
79 SHIP_SMALL_FIGHTER = 2 // "Fighter 2" - heavy shields
80};
81
82// A ship icon's facing in the overhead sector view (distinct from the
83// cockpit's Data::Direction) - also the order of drawShipSprite's own
84// per-facing jump table
85enum SpaceShipFacing {
86 FACING_LEFT = 0,
87 FACING_RIGHT = 1,
88 FACING_UP = 2,
89 FACING_DOWN = 3
90};
91
92// Unit vector for a given facing, indexed by SpaceShipFacing
93extern const int16 SPACE_FACING_DX[4];
94extern const int16 SPACE_FACING_DY[4];
95
96// The 4 station/encounter docking edges (EDGE_NAMES), and the [edge][shipType]
97// offsets from a sector's anchor for a ship of that type parked at that edge
98// (checkOverlapX/Y's own DELTA_X/DELTA_Y tables)
99constexpr int SPACE_DOCK_EDGE_COUNT = 4;
100extern const int16 SPACE_DOCK_DELTA_X[SPACE_DOCK_EDGE_COUNT][3];
101extern const int16 SPACE_DOCK_DELTA_Y[SPACE_DOCK_EDGE_COUNT][3];
102
103// The facing a ship parked at a given edge should have - always facing
104// inward toward the station/anchor (checkOverlapFacing)
105extern const SpaceShipFacing SPACE_DOCK_EDGE_FACING[SPACE_DOCK_EDGE_COUNT];
106
107// Edge names used in docking prompts/messages (EDGE_NAMES)
108extern const char *SPACE_DOCK_EDGE_NAMES[SPACE_DOCK_EDGE_COUNT];
109
110// Number of stages a cockpit-view combat encounter's target progresses
111// through as it closes in (targetApproachStage, 0..6)
112constexpr int SPACE_TARGET_STAGE_COUNT = 7;
113
114// Per-stage hit-test window for the Fire command (handleFireCommand): a
115// shot only registers as a hit if (viewCenterX - targetX) falls between
116// SPACE_TARGET_HIT_MIN_X[stage] and SPACE_TARGET_HIT_MAX_X[stage], and
117// likewise for Y - the window widens on X and tightens toward zero on Y
118// as the target closes in
119extern const int16 SPACE_TARGET_HIT_MIN_X[SPACE_TARGET_STAGE_COUNT];
120extern const int16 SPACE_TARGET_HIT_MAX_X[SPACE_TARGET_STAGE_COUNT];
121extern const int16 SPACE_TARGET_HIT_MIN_Y[SPACE_TARGET_STAGE_COUNT];
122extern const int16 SPACE_TARGET_HIT_MAX_Y[SPACE_TARGET_STAGE_COUNT];
123
127int16 shipFuelCapacity(int shipType);
128int16 shipShieldCapacity(int shipType);
129
134 int16 _x = 0, _y = 0;
135 int16 _shipType = SHIP_NONE;
136 int16 _fuel = 0;
137 int16 _shield = 0;
138 int16 _facing = 0;
139
140 void synchronize(Common::Serializer &s);
141};
142
147 // Hostile ships that will attack in this sector (0-3)
148 int16 _enemyCount = 0;
149 // A star / heat hazard - flying within 25 units destroys the ship.
150 // (0, 0) means none in this sector
151 int16 _hazardX = 0, _hazardY = 0;
152 // The docking / encounter anchor that any ships in the sector cluster
153 // around (the space station itself in sector 3,3). (0, 0) means the
154 // sector has no ships
155 int16 _anchorX = 0, _anchorY = 0;
156
157 SpaceMapShip _ships[SPACE_SHIPS_PER_SECTOR];
158
159 void synchronize(Common::Serializer &s);
160
166 int dockedEdge(int shipIndex) const;
167
172 int shipAtEdge(int edge) const;
173};
174
180struct SpaceMap {
181private:
182 uint16 _randomSeed = 0;
183
188 int getRandomNumber(int minVal, int maxVal);
189
193 void setupSector(int x, int y);
194
195public:
196 // Indexed [x][y], matching the original's column-major layout
197 SpaceMapCell _sectors[SPACE_SECTOR_COUNT][SPACE_SECTOR_COUNT];
198
202 void setup();
203
210};
211
212// The highest cockpit flight speed, and how many screen refreshes (60 a
213// second) a cockpit frame waits for at each speed - so frames come faster as
214// the ship does. The engine ticks 20 times a second, 3 refreshes each
215constexpr int SPACE_COCKPIT_MAX_SPEED = 8;
216constexpr int SPACE_COCKPIT_VBLANKS_PER_TICK = 3;
217extern const int SPACE_COCKPIT_FRAME_DELAY[SPACE_COCKPIT_MAX_SPEED + 1];
218
219// The cockpit view's warp starfield holds this many stars
220constexpr int SPACE_STAR_COUNT = 40;
221
227 // The viewport centre the stars radiate from and the aiming crosshair
228 // sits at (viewCenterX/Y), and the pan velocity applied to it each
229 // frame while steering (viewPanDeltaX/Y)
230 int16 _centerX = 0x9f, _centerY = 0x4f;
231 int16 _panX = 0, _panY = 0;
232
233 // Each star is an offset from the centre that accelerates outward
234 int16 _starX[SPACE_STAR_COUNT] = {};
235 int16 _starY[SPACE_STAR_COUNT] = {};
236
237 // Where each star sat on screen after the last advance(), and whether it
238 // was in view rather than just respawned - what the hyperjump needs to
239 // leave trails behind them or erase them again
240 int16 _prevX[SPACE_STAR_COUNT] = {};
241 int16 _prevY[SPACE_STAR_COUNT] = {};
242 bool _visible[SPACE_STAR_COUNT] = {};
243
245
249 void reset();
250
256 void reseed(uint32 seed);
257
264 void advance();
265
269 static bool withinView(int x, int y) {
270 return x > 20 && x < 300 && y > 20 && y < 140;
271 }
272
273private:
275
276 void spawnStar(int index);
277};
278
279} // namespace Data
280} // namespace Ultima1
281} // namespace Ultima
282
283#endif
Definition: random.h:44
Definition: serializer.h:80
Definition: space_map.h:146
int dockedEdge(int shipIndex) const
Definition: space_map.h:180
void synchronize(Common::Serializer &s)
Definition: space_map.h:133
Definition: space_map.h:226
static bool withinView(int x, int y)
Definition: space_map.h:269