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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
/* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
/*
* Based on
* WebVenture (c) 2010, Sean Kasun
* https://github.com/mrkite/webventure, http://seancode.com/webventure/
*
* Used with explicit permission from the author
*/
#include "macventure/world.h"
#include "macventure/macventure.h"
#include "common/file.h"
namespace MacVenture {
World::World(MacVentureEngine *engine, Common::MacResManager *resMan) {
_resourceManager = resMan;
_engine = engine;
_saveGame = NULL;
_gameText = NULL;
startNewGame();
_objectConstants = new Container(_engine->getFilePath(kObjectPathID));
calculateObjectRelations();
_gameText = new Container(_engine->getFilePath(kTextPathID));
}
World::~World() {
if (_saveGame)
delete _saveGame;
if (_objectConstants)
delete _objectConstants;
if (_gameText)
delete _gameText;
}
void World::startNewGame() {
if (_saveGame)
delete _saveGame;
if ((_startGameFileName = _engine->getStartGameFileName()) == "")
error("WORLD: Could not load initial game configuration");
Common::File saveGameFile;
if (!saveGameFile.open(_startGameFileName))
error("WORLD: Could not load initial game configuration");
debugC(2, kMVDebugMain, "Loading save game state from %s", _startGameFileName.c_str());
Common::SeekableReadStream *saveGameRes = saveGameFile.readStream(saveGameFile.size());
_saveGame = new SaveGame(_engine, saveGameRes);
calculateObjectRelations();
delete saveGameRes;
saveGameFile.close();
}
uint32 World::getObjAttr(ObjID objID, uint32 attrID) {
uint res;
uint32 index = _engine->getGlobalSettings()._attrIndices[attrID];
// HACK, but if I try to initialize it in the else clause, it goes out of scope and segfaults
Common::SeekableReadStream *objStream = _objectConstants->getItem(objID);
if (!(index & 0x80)) { // It's not a constant
res = _saveGame->getAttr(objID, index);
} else {
index &= 0x7F;
if (objStream->size() == 0) {
return 0;
}
// Look for the right attribute inside the object
objStream->skip(index * 2);
res = objStream->readByte() << 8;
res |= objStream->readByte();
}
res &= _engine->getGlobalSettings()._attrMasks[attrID];
res >>= _engine->getGlobalSettings()._attrShifts[attrID];
if (res & 0x8000)
res = -((int)((res ^ 0xffff) + 1));
debugC(5, kMVDebugMain, "Attribute %x from object %x is %x", attrID, objID, res);
delete objStream;
return res;
}
void World::setObjAttr(ObjID objID, uint32 attrID, Attribute value) {
if (attrID == kAttrPosX || attrID == kAttrPosY) {
// Round to scale
// Intentionally empty, we don't seem to require this functionality
}
if (attrID == kAttrParentObject)
setParent(objID, value);
if (attrID < kAttrOtherDoor)
_engine->enqueueObject(kUpdateObject, objID);
uint32 idx = _engine->getGlobalSettings()._attrIndices[attrID];
value <<= _engine->getGlobalSettings()._attrShifts[attrID];
value &= _engine->getGlobalSettings()._attrMasks[attrID];
Attribute oldVal = _saveGame->getAttr(objID, idx);
oldVal &= ~_engine->getGlobalSettings()._attrMasks[attrID];
_saveGame->setAttr(idx, objID, (value | oldVal));
_engine->gameChanged();
}
bool MacVenture::World::isObjActive(ObjID obj) {
ObjID destObj = _engine->getDestObject();
Common::Point p = _engine->getDeltaPoint();
ControlAction selectedControl = _engine->getSelectedControl();
if (!getAncestor(obj)) {
return false; // If our ancestor is the garbage (obj 0), we're inactive
}
if (_engine->getInvolvedObjects() >= 2 && // If (we need > 1 objs for the command) &&
destObj > 0 && // we have a destination object &&
!getAncestor(destObj)) { // but that destination object is in the garbage
return false;
}
if (selectedControl != kMoveObject) {
return true; // We only need one
}
// Handle move object
if (!isObjDraggable(obj)) {
return false; // We can't move it
}
if (getObjAttr(1, kAttrParentObject) != destObj) {
return true; // if the target is not the player's parent, we can go
}
Common::Rect rect(kScreenWidth, kScreenHeight);
rect.top -= getObjAttr(obj, kAttrPosY) + p.y;
rect.left -= getObjAttr(obj, kAttrPosX) + p.x;
return intersects(obj, rect);
}
ObjID World::getAncestor(ObjID objID) {
ObjID root = getObjAttr(1, kAttrParentObject);
while (objID != 0 && objID != 1 && objID != root) {
objID = getObjAttr(objID, kAttrParentObject);
}
return objID;
}
Common::Array<ObjID> World::getFamily(ObjID objID, bool recursive) {
Common::Array<ObjID> res;
res.push_back(objID);
res.push_back(getChildren(objID, recursive));
return res;
}
Common::Array<ObjID> World::getChildren(ObjID objID, bool recursive) {
Common::Array<ObjID> res;
ObjID child = _relations[objID * 2];
while (child) {
res.push_back(child);
if (!recursive)
res.push_back(getChildren(child, false));
child = _relations[child * 2 + 1];
}
return res;
}
Attribute World::getGlobal(uint32 attrID) {
return _saveGame->getGlobals()[attrID];
}
void World::setGlobal(uint32 attrID, Attribute value) {
_saveGame->setGlobal(attrID, value);
}
void World::updateObj(ObjID objID) {
WindowReference win;
if (getObjAttr(1, kAttrParentObject) == objID) {
win = kMainGameWindow;
} else {
win = _engine->getObjWindow(objID);
}
if (win) {
_engine->focusObjWin(objID);
_engine->runObjQueue();
_engine->updateWindow(win);
}
}
void World::captureChildren(ObjID objID) {
warning("Capture children unimplemented!");
}
void World::releaseChildren(ObjID objID) {
warning("Release children unimplemented!");
}
Common::String World::getText(ObjID objID, ObjID source, ObjID target) {
if (objID & 0x8000) {
return _engine->getUserInput();
}
TextAsset text = TextAsset(_engine, objID, source, target, _gameText, _engine->isOldText(), _engine->getDecodingHuffman());
return *text.decode();
}
bool World::isObjDraggable(ObjID objID) {
return (getObjAttr(objID, kAttrInvisible) == 0 &&
getObjAttr(objID, kAttrUnclickable) == 0 &&
getObjAttr(objID, kAttrUndraggable) == 0);
}
bool World::intersects(ObjID objID, Common::Rect rect) {
return _engine->getObjBounds(objID).intersects(rect);
}
void World::calculateObjectRelations() {
_relations.clear();
ObjID val, next;
uint32 numObjs = _engine->getGlobalSettings()._numObjects;
const AttributeGroup &parents = *_saveGame->getGroup(0);
for (uint i = 0; i < numObjs * 2; i++) {
_relations.push_back(0);
}
for (uint i = numObjs - 1; i > 0; i--) {
val = parents[i];
next = _relations[val * 2];
if (next) {
_relations[i * 2 + 1] = next;
}
_relations[val * 2] = i;
}
}
void World::setParent(ObjID child, ObjID newParent) {
ObjID old = _saveGame->getAttr(child, kAttrParentObject);
if (newParent == child)
return;
ObjID oldNdx = old * 2;
old = _relations[oldNdx];
while (old != child) {
oldNdx = (old * 2) + 1;
old = _relations[oldNdx];
}
_relations[oldNdx] = _relations[(old * 2) + 1];
oldNdx = newParent * 2;
old = _relations[oldNdx];
while (old && old <= child) {
oldNdx = (old * 2) + 1;
old = _relations[oldNdx];
}
_relations[child * 2 + 1] = old;
_relations[oldNdx] = child;
}
void World::loadGameFrom(Common::InSaveFile *file) {
if (_saveGame) {
delete _saveGame;
}
_saveGame = new SaveGame(_engine, file);
calculateObjectRelations();
}
void World::saveGameInto(Common::OutSaveFile *file) {
_saveGame->saveInto(file);
}
// SaveGame
SaveGame::SaveGame(MacVentureEngine *engine, Common::SeekableReadStream *res) {
_groups = Common::Array<AttributeGroup>();
loadGroups(engine, res);
_globals = Common::Array<uint16>();
loadGlobals(engine, res);
_text = Common::String();
loadText(engine, res);
}
SaveGame::~SaveGame() {
}
Attribute SaveGame::getAttr(ObjID objID, uint32 attrID) {
return _groups[attrID][objID];
}
void SaveGame::setAttr(uint32 attrID, ObjID objID, Attribute value) {
_groups[attrID][objID] = value;
}
const Common::Array<AttributeGroup> &MacVenture::SaveGame::getGroups() {
return _groups;
}
const AttributeGroup *SaveGame::getGroup(uint32 groupID) {
assert(groupID < _groups.size());
return &(_groups[groupID]);
}
void SaveGame::setGlobal(uint32 attrID, Attribute value) {
_globals[attrID] = value;
}
const Common::Array<uint16> &SaveGame::getGlobals() {
return _globals;
}
const Common::String &SaveGame::getText() {
return _text;
}
void SaveGame::saveInto(Common::OutSaveFile *file) {
warning("Saving the game not yet tested!");
// Save attibutes
Common::Array<AttributeGroup>::const_iterator itg;
for (itg = _groups.begin(); itg != _groups.end(); itg++) {
Common::Array<Attribute>::const_iterator ita;
for (ita = itg->begin(); ita != itg->end(); ita++) {
file->writeUint16BE((*ita));
}
}
// Save globals
Common::Array<uint16>::const_iterator global;
for (global = _globals.begin(); global != _globals.end(); global++) {
file->writeUint16BE((*global));
}
// Save text
// TODO: Insert text from GUI console
_text = "Hello";
file->write(_text.c_str(), _text.size());
}
void SaveGame::loadGroups(MacVentureEngine *engine, Common::SeekableReadStream *res) {
GlobalSettings settings = engine->getGlobalSettings();
for (int i = 0; i < settings._numGroups; ++i) {
AttributeGroup g;
for (int j = 0; j < settings._numObjects; ++j) {
g.push_back(res->readUint16BE());
}
_groups.push_back(g);
}
}
void SaveGame::loadGlobals(MacVentureEngine *engine, Common::SeekableReadStream *res) {
GlobalSettings settings = engine->getGlobalSettings();
for (int i = 0; i < settings._numGlobals; ++i) {
_globals.push_back(res->readUint16BE());
}
}
void SaveGame::loadText(MacVentureEngine *engine, Common::SeekableReadStream *res) {
// TODO: Load console text. For now, the GUI doesn't even look at this.
_text = "Placeholder Console Text";
}
} // End of namespace MacVenture
|