ScummVM API documentation
savegame.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 ULTIMA2_DATA_SAVEGAME_H
23#define ULTIMA2_DATA_SAVEGAME_H
24
25#include "common/serializer.h"
26#include "common/stream.h"
27#include "ultima/ultima2/data/data.h"
28#include "ultima/ultima2/data/tiles.h"
29
30namespace Ultima {
31namespace Ultima2 {
32namespace Data {
33
37struct Savegame {
38 // Identity
39 char _name[MAX_NAME_LENGTH + 1] = {};
40 Sex _sex = SEX_MALE;
41 Race _race = RACE_HUMAN;
42 CharClass _class = CLASS_FIGHTER;
43
44 // Attributes
45 int16 _strength = 0;
46 int16 _agility = 0;
47 int16 _stamina = 0;
48 int16 _charisma = 0;
49 int16 _wisdom = 0;
50 int16 _intelligence = 0;
51
52 // Resources
53 int16 _hp = 0;
54 int16 _food = 0;
55 int16 _foodTurnCtr = 0;
56 int16 _experience = 0;
57 int16 _gold = 0;
58
59 // Position - _mapEra/_mapType select the current mapX??/monX??/tlkX???
60 // file; _mapX/_mapY are the player's position within it
61 byte _mapEra = 0;
62 byte _mapType = 0;
63 byte _mapX = 0;
64 byte _mapY = 0;
65
66 // Overworld position to restore to when walking off the edge of a
67 // village/town/castle/tower/dungeon map back to the surface
68 byte _overworldReturnX = 0;
69 byte _overworldReturnY = 0;
70
71 // Position within a tower or dungeon: the level (0-15) and the compass
72 // direction faced, as an x/y step
73 byte _dungeonLevel = 0;
74 int8 _facingX = 1;
75 int8 _facingY = 0;
76
77 // Tile of the vehicle the player is riding (horse, ship, ...), or 0 on foot
78 byte _mount = 0;
79
80 // Currently readied items
81 WeaponType _readiedWeapon = WEAPON_HANDS;
82 ArmorType _readiedArmor = ARMOR_SKIN;
83 SpellType _readiedSpell = SPELL_NONE;
84
85 // Consumable resources dropped by monster kills
86 int16 _torches = 0;
87 int16 _keys = 0;
88 int16 _thievesTools = 0;
89
90 // Outer space
91 bool _inSpace = false;
92 byte _launchMapX = 0;
93 byte _launchMapY = 0;
94
95 // The world the player is on or orbiting: 0 is Earth, 1-8 the other
96 // planets, 9 planet X and 10 deep space. Saving only works on Earth
97 int16 _orbitTarget = 0;
98
99 // The XENO/YAKO/ZABO coordinates being hyperwarped to, or last reached
100 byte _hyperwarpCoords[3] = {};
101
102 // True while SpaceMap is the active view (flying between worlds, not yet
103 // landed) - lets a save made mid-flight resume back into it. Distinct
104 // from the original's own _inSpace flag below
105 bool _inFlight = false;
106
107 bool _ringQuestFlag = false;
108 bool _enilnoOwned = false;
109 byte _moongateWaypoint = 0;
110
111 // Turns until the moongate hops to its next waypoint, and the terrain
112 // tile it's currently covering
113 byte _moongateTimer = 0;
114 byte _moongateTerrain = 0;
115
116 byte _offerRewardItems[9] = {};
117
118 // Inventory quantities/charges, indexed by the enums in data.h
119 int16 _armorOwned[ARMOR_COUNT] = {};
120 int16 _weaponOwned[WEAPON_COUNT] = {};
121 int16 _spellCharges[SPELL_COUNT] = {};
122 int16 _items[ITEM_COUNT] = {};
123
124 // Turn-based status counters, decremented once per turn in Logic::endOfTurn
125 int16 _legParalysisTurns = 0;
126 int16 _armParalysisTurns = 0;
127 int16 _sleepTurns = 0;
128 int16 _negateTimeTurns = 0;
129 // Torch/Light spell duration in a tower or dungeon
130 int16 _lightTurns = 0;
131
132 // Incremented on every overworld monster spawn attempt; its lowest set
133 // bit selects the monster type rotation in OverworldLogic::trySpawnMonster
134 byte _monsterSpawnCounter = 0;
135
140
157
165
170 bool saveDisabled() const {
171 return _orbitTarget != 0;
172 }
173
177 bool hasCharacter() const {
178 return _name[0] != '\0';
179 }
180
186
190 TileId playerTile() const {
191 return _mount ? (TileId)_mount : (TileId)(TILE_FIGHTER + _class);
192 }
193
199 bool deductHP(int amount) {
200 if (_hp <= amount) {
201 _hp = 0;
202 return false;
203 }
204
205 _hp -= amount + 1;
206 return true;
207 }
208
213 bool deductFood(int amount) {
214 int total = _food * 100 + _foodTurnCtr - amount;
215 if (total < 0)
216 return false;
217
218 _food = total / 100;
219 _foodTurnCtr = total % 100;
220 return true;
221 }
222
229 int computeItemPrice(int itemIndex) const {
230 // The original sums the raw packed-BCD Intelligence/Charisma bytes
231 // with no decimal adjustment, so a combined score of 100+ lands on
232 // an invalid byte and produces too small a discount. The 1989
233 // "Ultima II Upgrade" patch fixes this by decimal-adjusting the sum
234 // and clamping it to the highest valid BCD byte (0x99) instead;
235 // since a properly decimal-adjusted BCD byte's integer value is
236 // just its own toBcd() encoding, that's the same as clamping the
237 // true decimal sum to 99 before re-encoding it here
238 int sum = toBcd(MIN(_intelligence + _charisma, MAX_BCD_BYTE));
239 int bits = 0;
240 while (sum > 0) {
241 sum >>= 1;
242 ++bits;
243 }
244
245 int steps = itemIndex + 8 - bits;
246 int prev = 4, price = 4;
247 for (int i = 0; i < steps; ++i) {
248 int next = (prev + price) % 10000;
249 prev = price;
250 price = next;
251 }
252 return price;
253 }
254};
255
256} // namespace Data
257} // namespace Ultima2
258} // namespace Ultima
259
260#endif
Definition: stream.h:745
Definition: serializer.h:80
Definition: stream.h:77
T MIN(T a, T b)
Definition: util.h:63
Definition: savegame.h:37
bool saveDisabled() const
Definition: savegame.h:170
int computeItemPrice(int itemIndex) const
Definition: savegame.h:229
bool deductHP(int amount)
Definition: savegame.h:199
bool importOriginal(Common::SeekableReadStream &stream)
bool deductFood(int amount)
Definition: savegame.h:213
TileId playerTile() const
Definition: savegame.h:190
void exportOriginal(Common::WriteStream &stream) const
bool hasCharacter() const
Definition: savegame.h:177
void synchronize(Common::Serializer &s)