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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
|
/* 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_defines.h"
#include "ags/shared/ac/game_struct_defines.h"
#include "ags/shared/ac/words_dictionary.h" // TODO: extract string decryption
#include "ags/shared/core/asset_manager.h"
#include "ags/shared/debugging/out.h"
#include "ags/shared/game/custom_properties.h"
#include "ags/shared/game/room_file.h"
#include "ags/shared/game/room_struct.h"
#include "ags/shared/gfx/bitmap.h"
#include "ags/shared/script/cc_common.h"
#include "ags/shared/script/cc_script.h"
#include "ags/shared/util/compress.h"
#include "ags/shared/util/data_ext.h"
#include "ags/shared/util/string_utils.h"
#include "ags/globals.h"
namespace AGS3 {
// default number of hotspots to read from the room file
#define MIN_ROOM_HOTSPOTS 20
#define LEGACY_HOTSPOT_NAME_LEN 30
#define LEGACY_ROOM_PASSWORD_LENGTH 11
#define LEGACY_ROOM_PASSWORD_SALT 60
#define ROOM_MESSAGE_FLAG_DISPLAYNEXT 200
// Reserved room options (each is a byte)
#define ROOM_OPTIONS_RESERVED 4
#define LEGACY_TINT_IS_ENABLED 0x80000000
namespace AGS {
namespace Shared {
HRoomFileError OpenRoomFileFromAsset(const String &filename, RoomDataSource &src) {
// Cleanup source struct
src = RoomDataSource();
// Try to find and open room file
Stream *in = _GP(AssetMgr)->OpenAsset(filename);
if (in == nullptr)
return new RoomFileError(kRoomFileErr_FileOpenFailed, String::FromFormat("Filename: %s.", filename.GetCStr()));
src.Filename = filename;
src.InputStream.reset(in);
return ReadRoomHeader(src);
}
void ReadRoomObject(RoomObjectInfo &obj, Stream *in) {
obj.Sprite = (uint16_t)in->ReadInt16();
obj.X = in->ReadInt16();
obj.Y = in->ReadInt16();
obj.Room = in->ReadInt16();
obj.IsOn = in->ReadInt16() != 0;
}
void WriteRoomObject(const RoomObjectInfo &obj, Stream *out) {
// TODO: expand serialization into 32-bit values at least for the sprite index!!
out->WriteInt16((uint16_t)obj.Sprite);
out->WriteInt16((int16_t)obj.X);
out->WriteInt16((int16_t)obj.Y);
out->WriteInt16((int16_t)obj.Room);
out->WriteInt16(obj.IsOn ? 1 : 0);
}
// Main room data
HError ReadMainBlock(RoomStruct *room, Stream *in, RoomFileVersion data_ver) {
int bpp;
if (data_ver >= kRoomVersion_208)
bpp = in->ReadInt32();
else
bpp = 1;
if (bpp < 1)
bpp = 1;
room->BackgroundBPP = bpp;
room->WalkBehindCount = in->ReadInt16();
if (room->WalkBehindCount > MAX_WALK_BEHINDS)
return new RoomFileError(kRoomFileErr_IncompatibleEngine, String::FromFormat("Too many walk-behinds (in room: %d, max: %d).", room->WalkBehindCount, MAX_WALK_BEHINDS));
// Walk-behinds baselines
for (size_t i = 0; i < room->WalkBehindCount; ++i)
room->WalkBehinds[i].Baseline = in->ReadInt16();
room->HotspotCount = in->ReadInt32();
if (room->HotspotCount == 0)
room->HotspotCount = MIN_ROOM_HOTSPOTS;
if (room->HotspotCount > MAX_ROOM_HOTSPOTS)
return new RoomFileError(kRoomFileErr_IncompatibleEngine, String::FromFormat("Too many hotspots (in room: %d, max: %d).", room->HotspotCount, MAX_ROOM_HOTSPOTS));
// Hotspots walk-to points
for (size_t i = 0; i < room->HotspotCount; ++i) {
room->Hotspots[i].WalkTo.X = in->ReadInt16();
room->Hotspots[i].WalkTo.Y = in->ReadInt16();
}
// Hotspots names and script names
for (size_t i = 0; i < room->HotspotCount; ++i) {
if (data_ver >= kRoomVersion_3415)
room->Hotspots[i].Name = StrUtil::ReadString(in);
else if (data_ver >= kRoomVersion_303a)
room->Hotspots[i].Name = String::FromStream(in);
else
room->Hotspots[i].Name = String::FromStreamCount(in, LEGACY_HOTSPOT_NAME_LEN);
}
if (data_ver >= kRoomVersion_270) {
for (size_t i = 0; i < room->HotspotCount; ++i) {
if (data_ver >= kRoomVersion_3415)
room->Hotspots[i].ScriptName = StrUtil::ReadString(in);
else
room->Hotspots[i].ScriptName = String::FromStreamCount(in, LEGACY_MAX_SCRIPT_NAME_LEN);
}
}
// TODO: remove from format later
size_t polypoint_areas = in->ReadInt32();
if (polypoint_areas > 0)
return new RoomFileError(kRoomFileErr_IncompatibleEngine, "Legacy poly-point areas are no longer supported.");
/* NOTE: implementation hidden in room_file_deprecated.cpp
for (size_t i = 0; i < polypoint_areas; ++i)
wallpoints[i].Read(in);
*/
room->Edges.Top = in->ReadInt16();
room->Edges.Bottom = in->ReadInt16();
room->Edges.Left = in->ReadInt16();
room->Edges.Right = in->ReadInt16();
// Room objects
uint16_t obj_count = in->ReadInt16();
if (obj_count > MAX_ROOM_OBJECTS)
return new RoomFileError(kRoomFileErr_IncompatibleEngine, String::FromFormat("Too many objects (in room: %d, max: %d).", obj_count, MAX_ROOM_OBJECTS));
room->Objects.resize(obj_count);
for (auto &obj : room->Objects)
ReadRoomObject(obj, in);
// Legacy interactions
if (data_ver >= kRoomVersion_253) {
size_t localvar_count = in->ReadInt32();
if (localvar_count > 0) {
room->LocalVariables.resize(localvar_count);
for (size_t i = 0; i < localvar_count; ++i)
room->LocalVariables[i].Read(in);
}
}
if (data_ver >= kRoomVersion_241 && data_ver < kRoomVersion_300a) {
for (size_t i = 0; i < room->HotspotCount; ++i)
room->Hotspots[i].Interaction.reset(Interaction::CreateFromStream(in));
for (auto &obj : room->Objects)
obj.Interaction.reset(Interaction::CreateFromStream(in));
room->Interaction.reset(Interaction::CreateFromStream(in));
}
if (data_ver >= kRoomVersion_255b) {
room->RegionCount = in->ReadInt32();
if (room->RegionCount > MAX_ROOM_REGIONS)
return new RoomFileError(kRoomFileErr_IncompatibleEngine, String::FromFormat("Too many regions (in room: %d, max: %d).", room->RegionCount, MAX_ROOM_REGIONS));
if (data_ver < kRoomVersion_300a) {
for (size_t i = 0; i < room->RegionCount; ++i)
room->Regions[i].Interaction.reset(Interaction::CreateFromStream(in));
}
}
// Event script links
if (data_ver >= kRoomVersion_300a) {
room->EventHandlers.reset(InteractionScripts::CreateFromStream(in));
for (size_t i = 0; i < room->HotspotCount; ++i)
room->Hotspots[i].EventHandlers.reset(InteractionScripts::CreateFromStream(in));
for (auto &obj : room->Objects)
obj.EventHandlers.reset(InteractionScripts::CreateFromStream(in));
for (size_t i = 0; i < room->RegionCount; ++i)
room->Regions[i].EventHandlers.reset(InteractionScripts::CreateFromStream(in));
}
if (data_ver >= kRoomVersion_200_alpha) {
for (auto &obj : room->Objects)
obj.Baseline = in->ReadInt32();
room->Width = in->ReadInt16();
room->Height = in->ReadInt16();
}
if (data_ver >= kRoomVersion_262)
for (auto &obj : room->Objects)
obj.Flags = in->ReadInt16();
if (data_ver >= kRoomVersion_200_final)
room->MaskResolution = in->ReadInt16();
room->WalkAreaCount = MAX_WALK_AREAS;
if (data_ver >= kRoomVersion_240)
room->WalkAreaCount = in->ReadInt32();
if (room->WalkAreaCount > MAX_WALK_AREAS)
return new RoomFileError(kRoomFileErr_IncompatibleEngine, String::FromFormat("Too many walkable areas (in room: %d, max: %d).", room->WalkAreaCount, MAX_WALK_AREAS));
if (data_ver >= kRoomVersion_200_alpha7)
for (size_t i = 0; i < room->WalkAreaCount; ++i)
room->WalkAreas[i].ScalingFar = in->ReadInt16();
if (data_ver >= kRoomVersion_214)
for (size_t i = 0; i < room->WalkAreaCount; ++i)
room->WalkAreas[i].PlayerView = in->ReadInt16();
if (data_ver >= kRoomVersion_251) {
for (size_t i = 0; i < room->WalkAreaCount; ++i)
room->WalkAreas[i].ScalingNear = in->ReadInt16();
for (size_t i = 0; i < room->WalkAreaCount; ++i)
room->WalkAreas[i].Top = in->ReadInt16();
for (size_t i = 0; i < room->WalkAreaCount; ++i)
room->WalkAreas[i].Bottom = in->ReadInt16();
}
in->Seek(LEGACY_ROOM_PASSWORD_LENGTH); // skip password
room->Options.StartupMusic = in->ReadInt8();
room->Options.SaveLoadDisabled = in->ReadInt8() != 0;
room->Options.PlayerCharOff = in->ReadInt8() != 0;
room->Options.PlayerView = in->ReadInt8();
room->Options.MusicVolume = (RoomVolumeMod)in->ReadInt8();
room->Options.Flags = in->ReadInt8();
in->Seek(ROOM_OPTIONS_RESERVED);
room->MessageCount = in->ReadInt16();
if (room->MessageCount > MAX_MESSAGES)
return new RoomFileError(kRoomFileErr_IncompatibleEngine, String::FromFormat("Too many room messages (in room: %d, max: %d).", room->MessageCount, MAX_MESSAGES));
if (data_ver >= kRoomVersion_272)
room->GameID = in->ReadInt32();
if (data_ver >= kRoomVersion_pre114_3) {
for (size_t i = 0; i < room->MessageCount; ++i) {
room->MessageInfos[i].DisplayAs = in->ReadInt8();
room->MessageInfos[i].Flags = in->ReadInt8();
}
}
char buffer[3000];
for (size_t i = 0; i < room->MessageCount; ++i) {
if (data_ver >= kRoomVersion_261)
read_string_decrypt(in, buffer, sizeof(buffer));
else
StrUtil::ReadCStr(buffer, in, sizeof(buffer));
room->Messages[i] = buffer;
}
// Very old format legacy room animations (FullAnimation)
if (data_ver >= kRoomVersion_pre114_6) {
// TODO: remove from format later
size_t fullanim_count = in->ReadInt16();
if (fullanim_count > 0)
return new RoomFileError(kRoomFileErr_IncompatibleEngine, "Room animations are no longer supported.");
/* NOTE: implementation hidden in room_file_deprecated.cpp
in->ReadArray(&fullanims[0], sizeof(FullAnimation), fullanim_count);
*/
}
// Ancient "graphical scripts". We currently don't support them because
// there's no knowledge on how to convert them to modern engine.
if ((data_ver >= kRoomVersion_pre114_4) && (data_ver < kRoomVersion_250a)) {
return new RoomFileError(kRoomFileErr_IncompatibleEngine, "Pre-2.5 graphical scripts are no longer supported.");
/* NOTE: implementation hidden in room_file_deprecated.cpp
ReadPre250Scripts(in);
*/
}
if (data_ver >= kRoomVersion_114) {
// NOTE: this WA value was written for the second time here, for some weird reason
for (size_t i = 0; i < (size_t)MAX_WALK_AREAS; ++i)
room->WalkAreas[i].PlayerView = in->ReadInt16();
}
if (data_ver >= kRoomVersion_255b) {
for (size_t i = 0; i < room->RegionCount; ++i)
room->Regions[i].Light = in->ReadInt16();
for (size_t i = 0; i < room->RegionCount; ++i)
room->Regions[i].Tint = in->ReadInt32();
}
// Primary background (LZW or RLE compressed depending on format)
if (data_ver >= kRoomVersion_pre114_5)
room->BgFrames[0].Graphic.reset(
load_lzw(in, room->BackgroundBPP, &room->Palette));
else
room->BgFrames[0].Graphic.reset(load_rle_bitmap8(in));
// Area masks
if (data_ver >= kRoomVersion_255b)
room->RegionMask.reset(load_rle_bitmap8(in));
else if (data_ver >= kRoomVersion_114)
skip_rle_bitmap8(in); // an old version - clear the 'shadow' area into a blank regions bmp (???)
room->WalkAreaMask.reset(load_rle_bitmap8(in));
room->WalkBehindMask.reset(load_rle_bitmap8(in));
room->HotspotMask.reset(load_rle_bitmap8(in));
return HError::None();
}
// Room script sources (original text)
HError ReadScriptBlock(char *&buf, Stream *in, RoomFileVersion /*data_ver*/) {
size_t len = in->ReadInt32();
buf = new char[len + 1];
in->Read(buf, len);
buf[len] = 0;
for (size_t i = 0; i < len; ++i)
buf[i] += _G(passwencstring)[i % 11];
return HError::None();
}
// Compiled room script
HError ReadCompSc3Block(RoomStruct *room, Stream *in, RoomFileVersion /*data_ver*/) {
room->CompiledScript.reset(ccScript::CreateFromStream(in));
if (room->CompiledScript == nullptr)
return new RoomFileError(kRoomFileErr_ScriptLoadFailed, cc_get_error().ErrorString);
return HError::None();
}
// Room object names
HError ReadObjNamesBlock(RoomStruct *room, Stream *in, RoomFileVersion data_ver) {
size_t name_count = static_cast<uint8_t>(in->ReadInt8());
if (name_count != room->Objects.size())
return new RoomFileError(kRoomFileErr_InconsistentData,
String::FromFormat("In the object names block, expected name count: %zu, got %zu", room->Objects.size(), name_count));
for (auto &obj : room->Objects) {
if (data_ver >= kRoomVersion_3415)
obj.Name = StrUtil::ReadString(in);
else
obj.Name.ReadCount(in, LEGACY_MAXOBJNAMELEN);
}
return HError::None();
}
// Room object script names
HError ReadObjScNamesBlock(RoomStruct *room, Stream *in, RoomFileVersion data_ver) {
size_t name_count = static_cast<uint8_t>(in->ReadInt8());
if (name_count != room->Objects.size())
return new RoomFileError(kRoomFileErr_InconsistentData,
String::FromFormat("In the object script names block, expected name count: %zu, got %zu", room->Objects.size(), name_count));
for (auto &obj : room->Objects) {
if (data_ver >= kRoomVersion_3415)
obj.ScriptName = StrUtil::ReadString(in);
else
obj.ScriptName.ReadCount(in, LEGACY_MAX_SCRIPT_NAME_LEN);
}
return HError::None();
}
// Secondary backgrounds
HError ReadAnimBgBlock(RoomStruct *room, Stream *in, RoomFileVersion data_ver) {
room->BgFrameCount = in->ReadInt8();
if (room->BgFrameCount > MAX_ROOM_BGFRAMES)
return new RoomFileError(kRoomFileErr_IncompatibleEngine, String::FromFormat("Too many room backgrounds (in room: %d, max: %d).", room->BgFrameCount, MAX_ROOM_BGFRAMES));
room->BgAnimSpeed = in->ReadInt8();
if (data_ver >= kRoomVersion_255a) {
for (size_t i = 0; i < room->BgFrameCount; ++i)
room->BgFrames[i].IsPaletteShared = in->ReadInt8() != 0;
}
for (size_t i = 1; i < room->BgFrameCount; ++i) {
room->BgFrames[i].Graphic.reset(
load_lzw(in, room->BackgroundBPP, &room->BgFrames[i].Palette));
}
return HError::None();
}
// Read custom properties
HError ReadPropertiesBlock(RoomStruct *room, Stream *in, RoomFileVersion /*data_ver*/) {
int prop_ver = in->ReadInt32();
if (prop_ver != 1)
return new RoomFileError(kRoomFileErr_PropertiesBlockFormat, String::FromFormat("Expected version %d, got %d", 1, prop_ver));
int errors = 0;
errors += Properties::ReadValues(room->Properties, in);
for (size_t i = 0; i < room->HotspotCount; ++i)
errors += Properties::ReadValues(room->Hotspots[i].Properties, in);
for (auto &obj : room->Objects)
errors += Properties::ReadValues(obj.Properties, in);
if (errors > 0)
return new RoomFileError(kRoomFileErr_InvalidPropertyValues);
return HError::None();
}
HError ReadRoomBlock(RoomStruct *room, Stream *in, RoomFileBlock block, const String &ext_id,
soff_t block_len, RoomFileVersion data_ver) {
//
// First check classic block types, identified with a numeric id
//
switch (block) {
case kRoomFblk_Main:
return ReadMainBlock(room, in, data_ver);
case kRoomFblk_Script:
in->Seek(block_len); // no longer read source script text into RoomStruct
return HError::None();
case kRoomFblk_CompScript3:
return ReadCompSc3Block(room, in, data_ver);
case kRoomFblk_ObjectNames:
return ReadObjNamesBlock(room, in, data_ver);
case kRoomFblk_ObjectScNames:
return ReadObjScNamesBlock(room, in, data_ver);
case kRoomFblk_AnimBg:
return ReadAnimBgBlock(room, in, data_ver);
case kRoomFblk_Properties:
return ReadPropertiesBlock(room, in, data_ver);
case kRoomFblk_CompScript:
case kRoomFblk_CompScript2:
return new RoomFileError(kRoomFileErr_OldBlockNotSupported,
String::FromFormat("Type: %d.", block));
case kRoomFblk_None:
break; // continue to string ids
default:
return new RoomFileError(kRoomFileErr_UnknownBlockType,
String::FromFormat("Type: %d, known range: %d - %d.", block, kRoomFblk_Main, kRoomFblk_ObjectScNames));
}
// Add extensions here checking ext_id, which is an up to 16-chars name
if (ext_id.CompareNoCase("ext_sopts") == 0) {
StrUtil::ReadStringMap(room->StrOptions, in);
return HError::None();
}
return new RoomFileError(kRoomFileErr_UnknownBlockType,
String::FromFormat("Type: %s", ext_id.GetCStr()));
}
// RoomBlockReader reads whole room data, block by block
class RoomBlockReader : public DataExtReader {
public:
RoomBlockReader(RoomStruct *room, RoomFileVersion data_ver, Stream *in)
: DataExtReader(in,
kDataExt_NumID8 | ((data_ver < kRoomVersion_350) ? kDataExt_File32 : kDataExt_File64))
, _room(room)
, _dataVer(data_ver) {
}
// Helper function that extracts legacy room script
HError ReadRoomScript(String &script) {
HError err = FindOne(kRoomFblk_Script);
if (!err)
return err;
char *buf = nullptr;
err = ReadScriptBlock(buf, _in, _dataVer);
script = buf;
delete[] buf;
return err;
}
private:
String GetOldBlockName(int block_id) const override {
return GetRoomBlockName((RoomFileBlock)block_id);
}
HError ReadBlock(int block_id, const String &ext_id,
soff_t block_len, bool &read_next) override {
read_next = true;
return ReadRoomBlock(_room, _in, (RoomFileBlock)block_id, ext_id, block_len, _dataVer);
}
RoomStruct *_room{};
RoomFileVersion _dataVer{};
};
HRoomFileError ReadRoomData(RoomStruct *room, Stream *in, RoomFileVersion data_ver) {
room->DataVersion = data_ver;
RoomBlockReader reader(room, data_ver, in);
HError err = reader.Read();
return err ? HRoomFileError::None() :
new RoomFileError(kRoomFileErr_BlockListFailed, err);
}
HRoomFileError UpdateRoomData(RoomStruct *room, RoomFileVersion data_ver, bool game_is_hires, const std::vector<SpriteInfo> &sprinfos) {
if (data_ver < kRoomVersion_200_final)
room->MaskResolution = room->BgFrames[0].Graphic->GetWidth() > 320 ? kRoomHiRes : kRoomLoRes;
if (data_ver < kRoomVersion_3508) {
// Save legacy resolution if it DOES NOT match game's;
// otherwise it gets promoted to "real resolution"
if (room->MaskResolution == 1 && game_is_hires)
room->SetResolution(kRoomLoRes);
else if (room->MaskResolution > 1 && !game_is_hires)
room->SetResolution(kRoomHiRes);
}
// Old version - copy walkable areas to regions
if (data_ver < kRoomVersion_255b) {
if (!room->RegionMask)
room->RegionMask.reset(BitmapHelper::CreateBitmap(room->WalkAreaMask->GetWidth(), room->WalkAreaMask->GetHeight(), 8));
room->RegionMask->Blit(room->WalkAreaMask.get(), 0, 0, 0, 0, room->RegionMask->GetWidth(), room->RegionMask->GetHeight());
for (size_t i = 0; i < MAX_ROOM_REGIONS; ++i) {
// sic!! walkable areas were storing Light level in this field pre-2.55
room->Regions[i].Light = room->WalkAreas[i].PlayerView;
room->Regions[i].Tint = 255;
}
}
// Fill in dummy interaction objects into unused slots
// TODO: remove this later, need to rework the legacy interaction usage around the engine code to prevent crashes
if (data_ver < kRoomVersion_300a) {
if (!room->Interaction)
room->Interaction.reset(new Interaction());
for (size_t i = 0; i < (size_t)MAX_ROOM_HOTSPOTS; ++i)
if (!room->Hotspots[i].Interaction)
room->Hotspots[i].Interaction.reset(new Interaction());
for (auto &obj : room->Objects)
if (!obj.Interaction)
obj.Interaction.reset(new Interaction());
for (size_t i = 0; i < (size_t)MAX_ROOM_REGIONS; ++i)
if (!room->Regions[i].Interaction)
room->Regions[i].Interaction.reset(new Interaction());
}
// Upgade room object script names
if (data_ver < kRoomVersion_300a) {
for (auto &obj : room->Objects) {
if (obj.ScriptName.GetLength() > 0) {
String jibbledScriptName;
jibbledScriptName.Format("o%s", obj.ScriptName.GetCStr());
jibbledScriptName.MakeLower();
if (jibbledScriptName.GetLength() >= 2)
jibbledScriptName.SetAt(1, toupper(jibbledScriptName[1u]));
obj.ScriptName = jibbledScriptName;
}
}
}
// Pre-3.0.3, multiply up co-ordinates for high-res games to bring them
// to the proper game coordinate system.
// If you change this, also change convert_room_coordinates_to_data_res
// function in the engine
if (data_ver < kRoomVersion_303b && game_is_hires) {
const int mul = HIRES_COORD_MULTIPLIER;
for (auto &obj : room->Objects) {
obj.X *= mul;
obj.Y *= mul;
if (obj.Baseline > 0) {
obj.Baseline *= mul;
}
}
for (size_t i = 0; i < room->HotspotCount; ++i) {
room->Hotspots[i].WalkTo.X *= mul;
room->Hotspots[i].WalkTo.Y *= mul;
}
for (size_t i = 0; i < room->WalkBehindCount; ++i) {
room->WalkBehinds[i].Baseline *= mul;
}
room->Edges.Left *= mul;
room->Edges.Top *= mul;
room->Edges.Bottom *= mul;
room->Edges.Right *= mul;
room->Width *= mul;
room->Height *= mul;
}
// Adjust object Y coordinate by adding sprite's height
// NOTE: this is impossible to do without game sprite information loaded beforehand
// NOTE: this should be done after coordinate conversion above for simplicity
if (data_ver < kRoomVersion_300a) {
for (auto &obj : room->Objects)
obj.Y += sprinfos[obj.Sprite].Height;
}
if (data_ver >= kRoomVersion_251) {
// if they set a contiuously scaled area where the top
// and bottom zoom levels are identical, set it as a normal
// scaled area
for (size_t i = 0; i < room->WalkAreaCount; ++i) {
if (room->WalkAreas[i].ScalingFar == room->WalkAreas[i].ScalingNear)
room->WalkAreas[i].ScalingNear = NOT_VECTOR_SCALED;
}
}
// Convert the old format region tint saturation
if (data_ver < kRoomVersion_3404) {
for (size_t i = 0; i < room->RegionCount; ++i) {
if ((room->Regions[i].Tint & LEGACY_TINT_IS_ENABLED) != 0) {
room->Regions[i].Tint &= ~LEGACY_TINT_IS_ENABLED;
// older versions of the editor had a bug - work around it
int tint_amount = (room->Regions[i].Light > 0 ? room->Regions[i].Light : 50);
room->Regions[i].Tint |= (tint_amount & 0xFF) << 24;
room->Regions[i].Light = 255;
}
}
}
// Older format room messages had flags appended to the message string
// TODO: find out which data versions had these; is it safe to assume this was before kRoomVersion_pre114_3?
for (size_t i = 0; i < room->MessageCount; ++i) {
if (!room->Messages[i].IsEmpty() &&
static_cast<uint8_t>(room->Messages[i].GetLast()) == ROOM_MESSAGE_FLAG_DISPLAYNEXT) {
room->Messages[i].ClipRight(1);
room->MessageInfos[i].Flags |= MSG_DISPLAYNEXT;
}
}
// sync bpalettes[0] with room.pal
memcpy(room->BgFrames[0].Palette, room->Palette, sizeof(RGB) * 256);
return HRoomFileError::None();
}
HRoomFileError ExtractScriptText(String &script, Stream *in, RoomFileVersion data_ver) {
RoomBlockReader reader(nullptr, data_ver, in);
HError err = reader.ReadRoomScript(script);
if (!err)
new RoomFileError(kRoomFileErr_BlockListFailed, err);
return HRoomFileError::None();
}
void WriteInteractionScripts(const InteractionScripts *interactions, Stream *out) {
out->WriteInt32(interactions->ScriptFuncNames.size());
for (size_t i = 0; i < interactions->ScriptFuncNames.size(); ++i)
interactions->ScriptFuncNames[i].Write(out);
}
void WriteMainBlock(const RoomStruct *room, Stream *out) {
out->WriteInt32(room->BackgroundBPP);
out->WriteInt16((int16_t)room->WalkBehindCount);
for (size_t i = 0; i < room->WalkBehindCount; ++i)
out->WriteInt16(room->WalkBehinds[i].Baseline);
out->WriteInt32(room->HotspotCount);
for (size_t i = 0; i < room->HotspotCount; ++i) {
out->WriteInt16(room->Hotspots[i].WalkTo.X);
out->WriteInt16(room->Hotspots[i].WalkTo.Y);
}
for (size_t i = 0; i < room->HotspotCount; ++i)
Shared::StrUtil::WriteString(room->Hotspots[i].Name, out);
for (size_t i = 0; i < room->HotspotCount; ++i)
Shared::StrUtil::WriteString(room->Hotspots[i].ScriptName, out);
out->WriteInt32(0); // legacy poly-point areas
out->WriteInt16(room->Edges.Top);
out->WriteInt16(room->Edges.Bottom);
out->WriteInt16(room->Edges.Left);
out->WriteInt16(room->Edges.Right);
out->WriteInt16((int16_t)room->Objects.size());
for (const auto &obj : room->Objects) {
WriteRoomObject(obj, out);
}
out->WriteInt32(0); // legacy interaction vars
out->WriteInt32(MAX_ROOM_REGIONS);
WriteInteractionScripts(room->EventHandlers.get(), out);
for (size_t i = 0; i < room->HotspotCount; ++i)
WriteInteractionScripts(room->Hotspots[i].EventHandlers.get(), out);
for (const auto &obj : room->Objects)
WriteInteractionScripts(obj.EventHandlers.get(), out);
for (size_t i = 0; i < room->RegionCount; ++i)
WriteInteractionScripts(room->Regions[i].EventHandlers.get(), out);
for (const auto &obj : room->Objects)
out->WriteInt32(obj.Baseline);
out->WriteInt16(room->Width);
out->WriteInt16(room->Height);
for (const auto &obj : room->Objects)
out->WriteInt16(obj.Flags);
out->WriteInt16(room->MaskResolution);
out->WriteInt32(MAX_WALK_AREAS);
for (size_t i = 0; i < (size_t)MAX_WALK_AREAS; ++i)
out->WriteInt16(room->WalkAreas[i].ScalingFar);
for (size_t i = 0; i < (size_t)MAX_WALK_AREAS; ++i)
out->WriteInt16(room->WalkAreas[i].PlayerView);
for (size_t i = 0; i < (size_t)MAX_WALK_AREAS; ++i)
out->WriteInt16(room->WalkAreas[i].ScalingNear);
for (size_t i = 0; i < (size_t)MAX_WALK_AREAS; ++i)
out->WriteInt16(room->WalkAreas[i].Top);
for (size_t i = 0; i < (size_t)MAX_WALK_AREAS; ++i)
out->WriteInt16(room->WalkAreas[i].Bottom);
out->WriteByteCount(0, LEGACY_ROOM_PASSWORD_LENGTH);
out->WriteInt8(room->Options.StartupMusic);
out->WriteInt8(room->Options.SaveLoadDisabled ? 1 : 0);
out->WriteInt8(room->Options.PlayerCharOff ? 1 : 0);
out->WriteInt8(room->Options.PlayerView);
out->WriteInt8(room->Options.MusicVolume);
out->WriteInt8(room->Options.Flags);
out->WriteByteCount(0, ROOM_OPTIONS_RESERVED);
out->WriteInt16((int16_t)room->MessageCount);
out->WriteInt32(room->GameID);
for (size_t i = 0; i < room->MessageCount; ++i) {
out->WriteInt8(room->MessageInfos[i].DisplayAs);
out->WriteInt8(room->MessageInfos[i].Flags);
}
for (size_t i = 0; i < room->MessageCount; ++i)
write_string_encrypt(out, room->Messages[i].GetCStr());
out->WriteInt16(0); // legacy room animations
// NOTE: this WA value was written for the second time here, for some weird reason
for (size_t i = 0; i < (size_t)MAX_WALK_AREAS; ++i)
out->WriteInt16(room->WalkAreas[i].PlayerView);
for (size_t i = 0; i < (size_t)MAX_ROOM_REGIONS; ++i)
out->WriteInt16(room->Regions[i].Light);
for (size_t i = 0; i < (size_t)MAX_ROOM_REGIONS; ++i)
out->WriteInt32(room->Regions[i].Tint);
save_lzw(out, room->BgFrames[0].Graphic.get(), &room->Palette);
save_rle_bitmap8(out, room->RegionMask.get());
save_rle_bitmap8(out, room->WalkAreaMask.get());
save_rle_bitmap8(out, room->WalkBehindMask.get());
save_rle_bitmap8(out, room->HotspotMask.get());
}
void WriteCompSc3Block(const RoomStruct *room, Stream *out) {
room->CompiledScript->Write(out);
}
void WriteObjNamesBlock(const RoomStruct *room, Stream *out) {
out->WriteByte((uint8_t)room->Objects.size());
for (const auto &obj : room->Objects)
Shared::StrUtil::WriteString(obj.Name, out);
}
void WriteObjScNamesBlock(const RoomStruct *room, Stream *out) {
out->WriteByte((uint8_t)room->Objects.size());
for (const auto &obj : room->Objects)
Shared::StrUtil::WriteString(obj.ScriptName, out);
}
void WriteAnimBgBlock(const RoomStruct *room, Stream *out) {
out->WriteByte((int8_t)room->BgFrameCount);
out->WriteByte(room->BgAnimSpeed);
for (size_t i = 0; i < room->BgFrameCount; ++i)
out->WriteInt8(room->BgFrames[i].IsPaletteShared ? 1 : 0);
for (size_t i = 1; i < room->BgFrameCount; ++i)
save_lzw(out, room->BgFrames[i].Graphic.get(), &room->BgFrames[i].Palette);
}
void WritePropertiesBlock(const RoomStruct *room, Stream *out) {
out->WriteInt32(1); // Version 1 of properties block
Properties::WriteValues(room->Properties, out);
for (size_t i = 0; i < room->HotspotCount; ++i)
Properties::WriteValues(room->Hotspots[i].Properties, out);
for (const auto &obj : room->Objects)
Properties::WriteValues(obj.Properties, out);
}
void WriteStrOptions(const RoomStruct *room, Stream *out) {
StrUtil::WriteStringMap(room->StrOptions, out);
}
HRoomFileError WriteRoomData(const RoomStruct *room, Stream *out, RoomFileVersion data_ver) {
if (data_ver < kRoomVersion_Current)
return new RoomFileError(kRoomFileErr_FormatNotSupported, "We no longer support saving room in the older format.");
// Header
out->WriteInt16(data_ver);
// Main data
WriteRoomBlock(room, kRoomFblk_Main, WriteMainBlock, out);
// Compiled script
if (room->CompiledScript)
WriteRoomBlock(room, kRoomFblk_CompScript3, WriteCompSc3Block, out);
// Object names
if (room->Objects.size() > 0) {
WriteRoomBlock(room, kRoomFblk_ObjectNames, WriteObjNamesBlock, out);
WriteRoomBlock(room, kRoomFblk_ObjectScNames, WriteObjScNamesBlock, out);
}
// Secondary background frames
if (room->BgFrameCount > 1)
WriteRoomBlock(room, kRoomFblk_AnimBg, WriteAnimBgBlock, out);
// Custom properties
WriteRoomBlock(room, kRoomFblk_Properties, WritePropertiesBlock, out);
// String options
WriteRoomBlock(room, "ext_sopts", WriteStrOptions, out);
// Write end of room file
out->WriteByte(kRoomFile_EOF);
return HRoomFileError::None();
}
} // namespace Shared
} // namespace AGS
} // namespace AGS3
|