File: room_status.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (236 lines) | stat: -rw-r--r-- 7,408 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "ags/shared/ac/common.h"
#include "ags/shared/ac/game_version.h"
#include "ags/engine/ac/room_status.h"
#include "ags/shared/game/custom_properties.h"
#include "ags/engine/game/savegame_components.h"
#include "ags/shared/util/string_utils.h"
#include "ags/globals.h"

namespace AGS3 {

using namespace AGS::Shared;
using namespace AGS::Engine;

void HotspotState::ReadFromSavegame(Shared::Stream *in, int save_ver) {
	Enabled = in->ReadInt8() != 0;
	if (save_ver >= kRoomStatSvgVersion_36016) {
		Name = StrUtil::ReadString(in);
	}
}

void HotspotState::WriteToSavegame(Shared::Stream *out) const {
	out->WriteInt8(Enabled);
	StrUtil::WriteString(Name, out);
}

RoomStatus::RoomStatus() {
	contentFormat = kRoomStatSvgVersion_Current; // set current to avoid fixups
	beenhere = 0;
	numobj = 0;
	tsdatasize = 0;

	memset(&region_enabled, 0, sizeof(region_enabled));
	memset(&walkbehind_base, 0, sizeof(walkbehind_base));
	memset(&interactionVariableValues, 0, sizeof(interactionVariableValues));
}

RoomStatus::~RoomStatus() {}

void RoomStatus::FreeScriptData() {
	tsdata.clear();
	tsdatasize = 0;
}

void RoomStatus::FreeProperties() {
	roomProps.clear();
	for (int i = 0; i < MAX_ROOM_HOTSPOTS; ++i) {
		hsProps[i].clear();
	}
	objProps.clear();
}

void RoomStatus::ReadFromSavegame_v321(Stream *in, GameDataVersion data_ver) {
	FreeScriptData();
	FreeProperties();

	contentFormat = kRoomStatSvgVersion_Initial;
	obj.resize(MAX_ROOM_OBJECTS_v300);
	objProps.resize(MAX_ROOM_OBJECTS_v300);
	intrObject.resize(MAX_ROOM_OBJECTS_v300);

	beenhere = in->ReadInt32();
	numobj = in->ReadInt32();
	// NOTE: legacy format always contained max object slots
	for (auto &o : obj) {
		o.ReadFromSavegame(in, -1 /* legacy save with padding */);
	}

	in->Seek(MAX_LEGACY_ROOM_FLAGS * sizeof(int16_t)); // flagstates (OBSOLETE)
	in->ReadInt16(); // alignment padding to int32
	tsdatasize = static_cast<uint32_t>(in->ReadInt32());
	in->ReadInt32(); // tsdata
	for (int i = 0; i < MAX_ROOM_HOTSPOTS; ++i) {
		intrHotspot[i].ReadFromSavedgame_v321(in);
	}
	for (auto &intr : intrObject) {
		intr.ReadFromSavedgame_v321(in);
	}
	for (int i = 0; i < MAX_ROOM_REGIONS; ++i) {
		intrRegion[i].ReadFromSavedgame_v321(in);
	}
	intrRoom.ReadFromSavedgame_v321(in);
	for (size_t i = 0; i < MAX_ROOM_HOTSPOTS; ++i)
		hotspot[i].Enabled = in->ReadInt8() != 0;
	in->ReadArrayOfInt8((int8_t *)region_enabled, MAX_ROOM_REGIONS);
	in->ReadArrayOfInt16(walkbehind_base, MAX_WALK_BEHINDS);
	in->ReadInt16(); // alignment padding to int32 (66 int8 + 16 int16 = 49 int16 -> 50)
	in->ReadArrayOfInt32(interactionVariableValues, MAX_GLOBAL_VARIABLES);

	if (data_ver >= kGameVersion_340_4) {
		Properties::ReadValues(roomProps, in);
		for (int i = 0; i < MAX_ROOM_HOTSPOTS; ++i) {
			Properties::ReadValues(hsProps[i], in);
		}
		for (auto &props : objProps) {
			Properties::ReadValues(props, in);
		}
	}
}

void RoomStatus::ReadFromSavegame(Stream *in, GameDataVersion data_ver, RoomStatSvgVersion save_ver) {
	FreeScriptData();
	FreeProperties();

	beenhere = in->ReadInt8();
	numobj = static_cast<uint32_t>(in->ReadInt32());
	obj.resize(numobj);
	objProps.resize(numobj);
	intrObject.resize(numobj);
	for (uint32_t i = 0; i < numobj; ++i) {
		obj[i].ReadFromSavegame(in, save_ver);
		Properties::ReadValues(objProps[i], in);
		if (data_ver <= kGameVersion_272)
			SavegameComponents::ReadInteraction272(intrObject[i], in);
	}
	for (int i = 0; i < MAX_ROOM_HOTSPOTS; ++i) {
		hotspot[i].ReadFromSavegame(in, save_ver);
		Properties::ReadValues(hsProps[i], in);
		if (data_ver <= kGameVersion_272)
			SavegameComponents::ReadInteraction272(intrHotspot[i], in);
	}
	for (int i = 0; i < MAX_ROOM_REGIONS; ++i) {
		region_enabled[i] = in->ReadInt8();
		if (data_ver <= kGameVersion_272)
			SavegameComponents::ReadInteraction272(intrRegion[i], in);
	}
	for (int i = 0; i < MAX_WALK_BEHINDS; ++i) {
		walkbehind_base[i] = in->ReadInt32();
	}

	Properties::ReadValues(roomProps, in);
	if (data_ver <= kGameVersion_272) {
		SavegameComponents::ReadInteraction272(intrRoom, in);
		in->ReadArrayOfInt32(interactionVariableValues, MAX_GLOBAL_VARIABLES);
	}

	tsdatasize = static_cast<uint32_t>(in->ReadInt32());
	if (tsdatasize) {
		tsdata.resize(tsdatasize);
		in->Read(tsdata.data(), tsdatasize);
	}

	contentFormat = save_ver;
	if (save_ver >= kRoomStatSvgVersion_36041) {
		contentFormat = (RoomStatSvgVersion)in->ReadInt32();
		in->ReadInt32(); // reserved
		in->ReadInt32();
		in->ReadInt32();
	}
}

void RoomStatus::WriteToSavegame(Stream *out, GameDataVersion data_ver) const {
	out->WriteInt8(beenhere);
	out->WriteInt32(numobj);
	for (uint32_t i = 0; i < numobj; ++i) {
		obj[i].WriteToSavegame(out);
		Properties::WriteValues(objProps[i], out);
		if (data_ver <= kGameVersion_272)
			SavegameComponents::WriteInteraction272(intrObject[i], out);
	}
	for (int i = 0; i < MAX_ROOM_HOTSPOTS; ++i) {
		hotspot[i].WriteToSavegame(out);
		Properties::WriteValues(hsProps[i], out);
		if (data_ver <= kGameVersion_272)
			SavegameComponents::WriteInteraction272(intrHotspot[i], out);
	}
	for (int i = 0; i < MAX_ROOM_REGIONS; ++i) {
		out->WriteInt8(region_enabled[i]);
		if (data_ver <= kGameVersion_272)
			SavegameComponents::WriteInteraction272(intrRegion[i], out);
	}
	for (int i = 0; i < MAX_WALK_BEHINDS; ++i) {
		out->WriteInt32(walkbehind_base[i]);
	}

	Properties::WriteValues(roomProps, out);
	if (data_ver <= kGameVersion_272) {
		SavegameComponents::WriteInteraction272(intrRoom, out);
		out->WriteArrayOfInt32(interactionVariableValues, MAX_GLOBAL_VARIABLES);
	}

	out->WriteInt32(static_cast<int32_t>(tsdatasize));
	if (tsdatasize)
		out->Write(tsdata.data(), tsdatasize);

	// kRoomStatSvgVersion_36041
	out->WriteInt32(contentFormat);
	out->WriteInt32(0); // reserved
	out->WriteInt32(0);
	out->WriteInt32(0);
}

// Replaces all accesses to the roomstats array
RoomStatus *getRoomStatus(int room) {
	if (!_G(room_statuses)[room]) {
		// First access, allocate and initialise the status
		_G(room_statuses)[room].reset(new RoomStatus());
	}
	return _G(room_statuses)[room].get();
}

// Used in places where it is only important to know whether the player
// had previously entered the room. In this case it is not necessary
// to initialise the status because a player can only have been in
// a room if the status is already initialised.
bool isRoomStatusValid(int room) {
	return (_G(room_statuses)[room] != nullptr);
}

void resetRoomStatuses() {
	for (int i = 0; i < MAX_ROOMS; i++) {
		_G(room_statuses)[i].reset();
	}
}

} // namespace AGS3