DEADSOFTWARE

Map: Use the animation flags in the map texture list only as a hint, not a prescription
[d2df-editor.git] / src / editor / g_map.pas
index a93a75af45ac2396fb8f48f8849aeb24d50824eb..5b2cc5f54ce11899881a12d1d89592b90b20adfc 100644 (file)
@@ -233,7 +233,7 @@ function  IsSpecialTexture(TextureName: String): Boolean;
 function  SpecialTextureID(TextureName: String): DWORD;
 
 procedure ClearMap();
-function  SaveMap(Res: String): Pointer;
+function  SaveMap(Res, ArchiveFormat: String): Pointer;
 function  LoadMap(Res: String): Boolean;
 function  LoadMapOld(_FileName: String): Boolean;
 procedure DrawMap();
@@ -1051,9 +1051,9 @@ begin
         Result := TEXTURE_SPECIAL_ACID2;
 end;
 
-function SaveMap(Res: String): Pointer;
+function SaveMap(Res, ArchiveFormat: String): Pointer;
 var
-  WAD: TWADEditor_1;
+  WAD: TWADEditor;
   MapWriter: TMapWriter_1;
   textures: TTexturesRec1Array;
   panels: TPanelsRec1Array;
@@ -1086,11 +1086,22 @@ begin
 // Открываем WAD, если надо:
   if Res <> '' then
   begin
-    WAD := TWADEditor_1.Create();
     g_ProcessResourceStr(Res, FileName, SectionName, ResName);
-    if not WAD.ReadFile(FileName) then
-      WAD.FreeWAD();
 
+    if ArchiveFormat = '' then
+    begin
+      // format not specified -> try open automatically and append to it (or create new default)
+      WAD := gWADEditorFactory.OpenFile(FileName);
+      if WAD = nil then
+        WAD := gWADEditorFactory.CreateDefaultEditor();
+    end
+    else
+    begin
+      // format specified -> append using exactly this format (overwrite if not compatible)
+      WAD := gWADEditorFactory.CreateEditor(ArchiveFormat);
+      if WAD.ReadFile(FileName) = False then
+        WAD.FreeWAD();
+    end;
     WAD.CreateImage();
   end;
 
@@ -1381,6 +1392,24 @@ begin
   end;
 end;
 
+function map_CreateAnimTexture(TextureName, Resource: String): Boolean;
+var
+  Data: Pointer = nil;
+  FrameLen: Integer = 0;
+  Width: Word = 0;
+  Height: Word = 0;
+begin
+  Result := GetFrame(Resource, Data, FrameLen, Width, Height);
+  if not Result then // Кадры
+    e_WriteLog(Format('GetFrame() error, res=%s', [TextureName]), MSG_WARNING)
+  else
+  begin
+    Result := g_CreateTextureMemorySize(Data, FrameLen, TextureName, 0, 0, Width, Height, 1);
+    if not Result then // Сама текстура
+      e_WriteLog(Format('g_CreateTextureMemorySize() error, res=%s', [TextureName]), MSG_WARNING);
+  end;
+end;
+
 function LoadMap(Res: String): Boolean;
 var
   WAD: TWADEditor_1;
@@ -1398,23 +1427,18 @@ var
   area: TArea;
   trigger: TTrigger;
   a: Integer;
-  Data: Pointer;
-  Width, Height, m: Word;
+  m: Word;
   FileName, SectionName, ResName, _fn: String;
   TextureRes, ustr: String;
   pData: Pointer;
-  Len, FrameLen: Integer;
-  Error: Boolean;
+  Len: Integer;
+  Success: Boolean;
   NoTextureID: DWORD;
   NW, NH: Word;
 begin
   Result := False;
   pData := nil;
   Len := 0;
-  Data := nil;
-  FrameLen := 0;
-  Width := 0;
-  Height := 0;
   NoTextureID := 0;
   NW := 0;
   NH := 0;
@@ -1464,50 +1488,43 @@ begin
       Application.ProcessMessages();
       ustr := win2utf(textures[a].Resource);
 
-      if IsSpecialTexture(ustr) then
+      Success := True;
+      if not IsSpecialTexture(ustr) then
       begin
-        AddTexture(ustr, False);
-        Continue;
-      end;
-
-      g_ProcessResourceStr(ustr, @_fn, nil, nil);
-
-      if _fn = '' then
-        TextureRes := FileName + ustr
-      else
-        TextureRes := WadsDir + DirectorySeparator + ustr;
+        g_ProcessResourceStr(ustr, @_fn, nil, nil);
 
-      Error := False;
+        if _fn = ''
+          then TextureRes := FileName + ustr
+          else TextureRes := WadsDir + DirectorySeparator + ustr;
 
-      if not ByteBool(textures[a].Anim) then
+        if not Boolean(textures[a].Anim) then
         begin // Обычная текстура
           if not g_CreateTextureWAD(ustr, TextureRes) then
           begin
-            e_WriteLog(Format('g_CreateTextureWAD() error, res=%s',
-                              [ustr]), MSG_WARNING);
-            Error := True;
+            e_WriteLog(Format('g_CreateTextureWAD() error, res=%s', [ustr]), MSG_WARNING);
+            Success := map_CreateAnimTexture(ustr, TextureRes);
+            if Success then
+            begin
+              textures[a].Anim := Byte(ByteBool(True));
+              e_WriteLog(Format('    wrong (outdated?) anim flag hint - texture #%d is actually animated: %s', [a, ustr]), MSG_WARNING);
+            end;
           end;
-
-          AddTexture(ustr, Error);
         end
-      else // Anim
+        else
         begin // Анимированная текстура
-          if not GetFrame(TextureRes, Data, FrameLen, Width, Height) then
-          begin // Кадры
-            e_WriteLog(Format('GetFrame() error, res=%s',
-                              [ustr]), MSG_WARNING);
-            Error := True;
-          end;
-
-          if not g_CreateTextureMemorySize(Data, FrameLen, ustr, 0, 0, Width, Height, 1) then
-          begin // Сама текстура
-            e_WriteLog(Format('g_CreateTextureMemorySize() error, res=%s',
-                              [ustr]), MSG_WARNING);
-            Error := True;
+          if not map_CreateAnimTexture(ustr, TextureRes) then
+          begin
+            Success := g_CreateTextureWAD(ustr, TextureRes);
+            if Success then
+            begin
+              textures[a].Anim := Byte(ByteBool(False));
+              e_WriteLog(Format('    wrong (outdated?) anim flag hint - texture #%d is actually static: %s', [a, ustr]), MSG_WARNING);
+            end;
           end;
-
-          AddTexture(ustr, Error);
         end;
+      end;
+
+      AddTexture(ustr, not Success);
     end;
   end;