Skip to content

Commit 1573110

Browse files
committed
FALLOUT2: Preliminary implementation of mapsaving
1 parent d9a1d50 commit 1573110

File tree

1 file changed

+95
-53
lines changed

1 file changed

+95
-53
lines changed

engines/fallout2/map.cpp

Lines changed: 95 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
// #include <stdio.h>
44
// #include <string.h>
5-
65
// #include <vector>
76

8-
#include "common/array.h"
7+
#include "fallout2/fallout2.h"
98

109
#include "fallout2/animation.h"
1110
#include "fallout2/art.h"
@@ -45,14 +44,18 @@
4544
#include "fallout2/window_manager_private.h"
4645
#include "fallout2/worldmap.h"
4746

47+
#include "common/array.h"
48+
#include "common/savefile.h"
49+
50+
4851
namespace Fallout2 {
4952

5053
static char *mapBuildPath(char *name);
5154
static int mapLoad(File *stream);
5255
static int _map_age_dead_critters();
5356
static void _map_fix_critter_combat_data();
5457
static int _map_save();
55-
static int _map_save_file(File *stream);
58+
static int _map_save_file(Common::OutSaveFile *stream);
5659
static void mapMakeMapsDirectory();
5760
static void isoWindowRefreshRect(Rect *rect);
5861
static void isoWindowRefreshRectGame(Rect *rect);
@@ -67,7 +70,7 @@ static void _map_place_dude_and_mouse();
6770
static void square_init();
6871
static void _square_reset();
6972
static int _square_load(File *stream, int a2);
70-
static int mapHeaderWrite(MapHeader *ptr, File *stream);
73+
static int mapHeaderWrite(MapHeader *ptr, Common::OutSaveFile *stream);
7174
static int mapHeaderRead(MapHeader *ptr, File *stream);
7275

7376
// 0x50B058
@@ -688,7 +691,12 @@ static char *mapBuildPath(char *name) {
688691

689692
if (*name != '\\') {
690693
// NOTE: Uppercased from "maps".
691-
snprintf(map_path, sizeof(map_path), "MAPS\\%s", name);
694+
Common::String newMapName(g_engine->getTargetName());
695+
if (Common::String(name).contains(".SAV"))
696+
snprintf(map_path, sizeof(map_path), "%s_maps_%s", newMapName.c_str(), name);
697+
else
698+
snprintf(map_path, sizeof(map_path), "MAPS\\%s", name);
699+
debug("Built map path: %s", map_path);
692700
return map_path;
693701
}
694702
return name;
@@ -740,19 +748,21 @@ int mapLoadByName(char *fileName) {
740748
strncpy(extension, ".SAV", 5);
741749

742750
const char *filePath = mapBuildPath(fileName);
751+
// Check if .SAV map exists
743752

744-
File *stream = fileOpen(filePath, "rb");
753+
// File *stream = fileOpen(filePath, "rb"); TODO saveload
745754

746755
strncpy(extension, ".MAP", 5);
747756

748-
if (stream != NULL) {
749-
fileClose(stream);
750-
rc = mapLoadSaved(fileName);
751-
wmMapMusicStart();
752-
}
757+
// if (stream != NULL) {
758+
// fileClose(stream);
759+
// rc = mapLoadSaved(fileName);
760+
// wmMapMusicStart();
761+
// }
753762
}
754763

755764
if (rc == -1) {
765+
debug(".SAV map not found, looking for .MAP");
756766
const char *filePath = mapBuildPath(fileName);
757767
File *stream = fileOpen(filePath, "rb");
758768
if (stream != NULL) {
@@ -1201,6 +1211,8 @@ int mapHandleTransition() {
12011211
return 0;
12021212
}
12031213

1214+
debug("Doing map transition!");
1215+
12041216
gameMouseObjectsHide();
12051217

12061218
gameMouseSetCursor(MOUSE_CURSOR_NONE);
@@ -1278,10 +1290,15 @@ static int _map_save() {
12781290
int rc = -1;
12791291
if (gMapHeader.name[0] != '\0') {
12801292
char *mapFileName = mapBuildPath(gMapHeader.name);
1281-
File *stream = fileOpen(mapFileName, "wb");
1293+
Common::SaveFileManager *saveMan = g_system->getSavefileManager();
1294+
1295+
Common::OutSaveFile *stream = saveMan->openForSaving(Common::String(mapFileName), false);
1296+
// File *stream = fileOpen(mapFileName, "wb");
12821297
if (stream != NULL) {
12831298
rc = _map_save_file(stream);
1284-
fileClose(stream);
1299+
stream->finalize();
1300+
delete stream;
1301+
// fileClose(stream);
12851302
} else {
12861303
snprintf(temp, sizeof(temp), "Unable to open %s to write!", gMapHeader.name);
12871304
debugPrint(temp);
@@ -1299,7 +1316,7 @@ static int _map_save() {
12991316
}
13001317

13011318
// 0x483980
1302-
static int _map_save_file(File *stream) {
1319+
static int _map_save_file(Common::OutSaveFile *stream) {
13031320
if (stream == NULL) {
13041321
return -1;
13051322
}
@@ -1350,22 +1367,28 @@ static int _map_save_file(File *stream) {
13501367
mapHeaderWrite(&gMapHeader, stream);
13511368

13521369
if (gMapHeader.globalVariablesCount != 0) {
1353-
fileWriteInt32List(stream, gMapGlobalVars, gMapHeader.globalVariablesCount);
1370+
for (int i = 0; i < gMapHeader.globalVariablesCount; i++)
1371+
stream->writeSint32BE(gMapGlobalVars[i]);
1372+
//fileWriteInt32List(stream, gMapGlobalVars, gMapHeader.globalVariablesCount);
13541373
}
13551374

13561375
if (gMapHeader.localVariablesCount != 0) {
1357-
fileWriteInt32List(stream, gMapLocalVars, gMapHeader.localVariablesCount);
1376+
for (int i = 0; i < gMapHeader.localVariablesCount; i++)
1377+
stream->writeSint32BE(gMapLocalVars[i]);
1378+
//fileWriteInt32List(stream, gMapLocalVars, gMapHeader.localVariablesCount);
13581379
}
13591380

13601381
for (int elevation = 0; elevation < ELEVATION_COUNT; elevation++) {
13611382
if ((gMapHeader.flags & _map_data_elev_flags[elevation]) == 0) {
1362-
_db_fwriteLongCount(stream, _square[elevation]->field_0, SQUARE_GRID_SIZE);
1383+
for (int i = 0; i < SQUARE_GRID_SIZE; i++)
1384+
stream->writeSint32BE(_square[elevation]->field_0[i]);
1385+
//_db_fwriteLongCount(stream, _square[elevation]->field_0, SQUARE_GRID_SIZE);
13631386
}
13641387
}
13651388

13661389
char err[80];
13671390

1368-
if (scriptSaveAll(stream) == -1) {
1391+
/* if (scriptSaveAll(stream) == -1) { TODO saveload
13691392
snprintf(err, sizeof(err), "Error saving scripts in %s", gMapHeader.name);
13701393
_win_msg(err, 80, 80, _colorTable[31744]);
13711394
}
@@ -1374,7 +1397,7 @@ static int _map_save_file(File *stream) {
13741397
snprintf(err, sizeof(err), "Error saving objects in %s", gMapHeader.name);
13751398
_win_msg(err, 80, 80, _colorTable[31744]);
13761399
}
1377-
1400+
*/
13781401
scriptsEnable();
13791402

13801403
return 0;
@@ -1385,6 +1408,7 @@ int _map_save_in_game(bool a1) {
13851408
if (gMapHeader.name[0] == '\0') {
13861409
return 0;
13871410
}
1411+
debug("Attempting map_save_in_game! a1 = %d", a1);
13881412

13891413
animationStop();
13901414
_partyMemberSaveProtos();
@@ -1409,25 +1433,25 @@ int _map_save_in_game(bool a1) {
14091433

14101434
char name[16];
14111435

1412-
/* if (a1 && !wmMapIsSaveable()) { TODO world_map
1436+
if (a1 && !wmMapIsSaveable()) {
14131437
debugPrint("\nNot saving RANDOM encounter map.");
14141438

1415-
strcpy(name, gMapHeader.name);
1439+
strncpy(name, gMapHeader.name, sizeof(name) - 1);
14161440
_strmfe(gMapHeader.name, name, "SAV");
14171441
_MapDirEraseFile_("MAPS\\", gMapHeader.name);
1418-
strcpy(gMapHeader.name, name);
1442+
strncpy(gMapHeader.name, name, sizeof(gMapHeader.name) - 1);
14191443
} else {
14201444
debugPrint("\n Saving \".SAV\" map.");
14211445

1422-
strcpy(name, gMapHeader.name);
1446+
strncpy(name, gMapHeader.name, sizeof(name) - 1);
14231447
_strmfe(gMapHeader.name, name, "SAV");
14241448
if (_map_save() == -1) {
14251449
return -1;
14261450
}
14271451

1428-
strcpy(gMapHeader.name, name);
1452+
strncpy(gMapHeader.name, name, sizeof(gMapHeader.name) - 1);
14291453

1430-
automapSaveCurrent();
1454+
// automapSaveCurrent(); // TODO automap
14311455

14321456
if (a1) {
14331457
gMapHeader.name[0] = '\0';
@@ -1436,7 +1460,7 @@ int _map_save_in_game(bool a1) {
14361460
_square_reset();
14371461
gameTimeScheduleUpdateEvent();
14381462
}
1439-
} */
1463+
}
14401464

14411465
return 0;
14421466
}
@@ -1680,33 +1704,51 @@ static int _square_load(File *stream, int flags) {
16801704
}
16811705

16821706
// 0x4843B8
1683-
static int mapHeaderWrite(MapHeader *ptr, File *stream) {
1684-
if (fileWriteInt32(stream, ptr->version) == -1)
1685-
return -1;
1686-
if (fileWriteFixedLengthString(stream, ptr->name, 16) == -1)
1687-
return -1;
1688-
if (fileWriteInt32(stream, ptr->enteringTile) == -1)
1689-
return -1;
1690-
if (fileWriteInt32(stream, ptr->enteringElevation) == -1)
1691-
return -1;
1692-
if (fileWriteInt32(stream, ptr->enteringRotation) == -1)
1693-
return -1;
1694-
if (fileWriteInt32(stream, ptr->localVariablesCount) == -1)
1695-
return -1;
1696-
if (fileWriteInt32(stream, ptr->scriptIndex) == -1)
1697-
return -1;
1698-
if (fileWriteInt32(stream, ptr->flags) == -1)
1699-
return -1;
1700-
if (fileWriteInt32(stream, ptr->darkness) == -1)
1701-
return -1;
1702-
if (fileWriteInt32(stream, ptr->globalVariablesCount) == -1)
1703-
return -1;
1704-
if (fileWriteInt32(stream, ptr->field_34) == -1)
1705-
return -1;
1706-
if (fileWriteInt32(stream, ptr->lastVisitTime) == -1)
1707-
return -1;
1708-
if (fileWriteInt32List(stream, ptr->field_3C, 44) == -1)
1709-
return -1;
1707+
static int mapHeaderWrite(MapHeader *ptr, Common::OutSaveFile *stream) {
1708+
int i;
1709+
1710+
stream->writeSint32BE(ptr->version);
1711+
for (i = 0; i < 16; i++)
1712+
stream->writeByte(ptr->name[i]);
1713+
stream->writeSint32BE(ptr->enteringTile);
1714+
stream->writeSint32BE(ptr->enteringElevation);
1715+
stream->writeSint32BE(ptr->enteringRotation);
1716+
stream->writeSint32BE(ptr->localVariablesCount);
1717+
stream->writeSint32BE(ptr->scriptIndex);
1718+
stream->writeSint32BE(ptr->flags);
1719+
stream->writeSint32BE(ptr->darkness);
1720+
stream->writeSint32BE(ptr->globalVariablesCount);
1721+
stream->writeSint32BE(ptr->field_34);
1722+
stream->writeSint32BE(ptr->lastVisitTime);
1723+
for (i = 0; i < 44; i++)
1724+
stream->writeByte(ptr->field_3C[i]);
1725+
1726+
/* if (fileWriteInt32(stream, ptr->version) == -1)
1727+
return -1;
1728+
if (fileWriteFixedLengthString(stream, ptr->name, 16) == -1)
1729+
return -1;
1730+
if (fileWriteInt32(stream, ptr->enteringTile) == -1)
1731+
return -1;
1732+
if (fileWriteInt32(stream, ptr->enteringElevation) == -1)
1733+
return -1;
1734+
if (fileWriteInt32(stream, ptr->enteringRotation) == -1)
1735+
return -1;
1736+
if (fileWriteInt32(stream, ptr->localVariablesCount) == -1)
1737+
return -1;
1738+
if (fileWriteInt32(stream, ptr->scriptIndex) == -1)
1739+
return -1;
1740+
if (fileWriteInt32(stream, ptr->flags) == -1)
1741+
return -1;
1742+
if (fileWriteInt32(stream, ptr->darkness) == -1)
1743+
return -1;
1744+
if (fileWriteInt32(stream, ptr->globalVariablesCount) == -1)
1745+
return -1;
1746+
if (fileWriteInt32(stream, ptr->field_34) == -1)
1747+
return -1;
1748+
if (fileWriteInt32(stream, ptr->lastVisitTime) == -1)
1749+
return -1;
1750+
if (fileWriteInt32List(stream, ptr->field_3C, 44) == -1)
1751+
return -1;*/
17101752

17111753
return 0;
17121754
}

0 commit comments

Comments
 (0)