DEADSOFTWARE

0c361c52e5ba19bac69a6d399ed6e039851f0716
[d2df-editor.git] / src / editor / f_packmap.pas
1 unit f_packmap;
3 {$INCLUDE ../shared/a_modes.inc}
5 interface
7 uses
8 LCLIntf, LCLType, LMessages, SysUtils, Variants, Classes,
9 Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls, utils;
11 type
12 TPackMapForm = class (TForm)
13 bPack: TButton;
14 SaveDialog: TSaveDialog;
15 Panel1: TPanel;
16 // Сохранить в:
17 LabelSaveTo: TLabel;
18 eWAD: TEdit;
19 bSelectWAD: TButton;
20 // Имя карты:
21 LabelMapName: TLabel;
22 eResource: TEdit;
23 // Текстуры:
24 cbTextrures: TCheckBox;
25 LabelTextures: TLabel;
26 eTSection: TEdit;
27 // Небо:
28 cbSky: TCheckBox;
29 LabelSky: TLabel;
30 eSSection: TEdit;
31 // Музыка:
32 cbMusic: TCheckBox;
33 LabelMusic: TLabel;
34 eMSection: TEdit;
35 // Дополнительно:
36 cbAdd: TCheckBox;
37 cbNonStandart: TCheckBox;
39 procedure bSelectWADClick(Sender: TObject);
40 procedure bPackClick(Sender: TObject);
41 procedure FormCreate(Sender: TObject);
43 private
44 { Private declarations }
45 public
46 { Public declarations }
47 end;
49 var
50 PackMapForm: TPackMapForm;
52 implementation
54 uses
55 BinEditor, WADEDITOR, g_map, MAPREADER, MAPWRITER, MAPSTRUCT,
56 f_main, math, g_language, g_resources;
58 {$R *.lfm}
60 const
61 STANDART_WAD = 'standart.wad';
62 SHRSHADE_WAD = 'shrshade.wad';
65 procedure TPackMapForm.bSelectWADClick(Sender: TObject);
66 begin
67 SaveDialog.Filter := _lc[I_FILE_FILTER_WAD];
69 if SaveDialog.Execute() then
70 eWAD.Text := SaveDialog.FileName;
71 end;
73 function ProcessResource(wad_to, section_to, filename, section, resource: String): Boolean;
74 var
75 data: Pointer;
76 res, len: Integer;
77 begin
78 if filename = '' then
79 g_ProcessResourceStr(OpenedMap, @filename, nil, nil)
80 else
81 filename := EditorDir + 'wads/' + filename;
83 g_ReadResource(filename, section, resource, data, len);
84 if data <> nil then
85 begin
86 (* Write resource only if it does not exists *)
87 g_ExistsResource(wad_to, section_to, resource, res);
88 if res <> 0 then
89 begin
90 g_AddResource(wad_to, section_to, resource, data, len, res);
91 ASSERT(res = 0)
92 end;
93 FreeMem(data);
94 Result := True
95 end
96 else
97 begin
98 //MessageBox(0, PChar(Format(_lc[I_MSG_WAD_ERROR], [ExtractFileName(filename)])), PChar(_lc[I_MSG_ERROR]), MB_OK + MB_ICONERROR);
99 MessageBox(0, PChar(Format(_lc[I_MSG_RES_ERROR], [filename, section, resource])), PChar(_lc[I_MSG_ERROR]), MB_OK + MB_ICONERROR);
100 Result := False
101 end
102 end;
104 procedure TPackMapForm.bPackClick(Sender: TObject);
105 var
106 mr: TMapReader_1;
107 mw: TMapWriter_1;
108 data: Pointer;
109 len: LongWord;
110 textures: TTexturesRec1Array;
111 header: TMapHeaderRec_1;
112 a: Integer;
113 res, tsection, ssection, msection, filename, section, resource: String;
115 begin
116 if eWAD.Text = '' then
117 Exit;
118 if eResource.Text = '' then
119 Exit;
121 tsection := eTSection.Text;
122 ssection := eSSection.Text;
123 msection := eMSection.Text;
125 // Сохраняем карту в память:
126 data := SaveMap('');
127 if data = nil then
128 Exit;
130 // Не перезаписывать WAD, а дополнить:
131 if not cbAdd.Checked then
132 begin
133 if FileExists(eWAD.Text) then
134 begin
135 if FileExists(eWAD.Text + '.bak0') then
136 ASSERT(DeleteFile(eWAD.Text + '.bak0'));
137 ASSERT(RenameFile(eWAD.Text, eWAD.Text + '.bak0'))
138 end
139 end;
141 // Читаем карту из памяти:
142 mr := TMapReader_1.Create();
143 mr.LoadMap(data);
144 FreeMem(data);
146 // Получаем текстуры:
147 textures := mr.GetTextures();
149 // Нужно копировать текстуры:
150 if cbTextrures.Checked and (textures <> nil) then
151 for a := 0 to High(textures) do
152 begin
153 res := win2utf(textures[a].Resource);
154 if IsSpecialTexture(res) then
155 Continue;
157 g_ProcessResourceStr(res, @filename, @section, @resource);
159 // Не записывать стандартные текстуры:
160 if (not cbNonStandart.Checked) or
161 ( (AnsiLowerCase(filename) <> STANDART_WAD) and
162 (AnsiLowerCase(filename) <> SHRSHADE_WAD) ) then
163 begin
164 // Копируем ресурс текстуры:
165 if not f_packmap.ProcessResource(eWAD.Text, tsection, filename, section, resource) then
166 begin
167 mr.Free();
168 Exit;
169 end;
171 // Переименовываем ресурс текстуры:
172 res := utf2win(Format(':%s\%s', [tsection, resource]));
173 ZeroMemory(@textures[a].Resource[0], 64);
174 CopyMemory(@textures[a].Resource[0], @res[1], Min(Length(res), 64));
175 end;
176 end;
178 // Получаем заголовок карты:
179 header := mr.GetMapHeader();
181 // Нужно копировать небо:
182 if cbSky.Checked then
183 begin
184 res := win2utf(header.SkyName);
185 g_ProcessResourceStr(res, @filename, @section, @resource);
187 // Не записывать стандартное небо:
188 if (not cbNonStandart.Checked) or
189 ( (AnsiLowerCase(filename) <> STANDART_WAD) and
190 (AnsiLowerCase(filename) <> SHRSHADE_WAD) ) then
191 begin
192 // Копируем ресурс неба:
193 if not f_packmap.ProcessResource(eWAD.Text, ssection, filename, section, resource) then
194 begin
195 mr.Free();
196 Exit;
197 end;
199 // Переименовываем ресурс неба:
200 res := utf2win(Format(':%s\%s', [ssection, resource]));
201 ZeroMemory(@header.SkyName[0], 64);
202 CopyMemory(@header.SkyName[0], @res[1], Min(Length(res), 64));
203 end;
204 end;
206 // Нужно копировать музыку:
207 if cbMusic.Checked then
208 begin
209 res := win2utf(header.MusicName);
210 g_ProcessResourceStr(res, @filename, @section, @resource);
212 // Не записывать стандартную музыку:
213 if (not cbNonStandart.Checked) or
214 ( (AnsiLowerCase(filename) <> STANDART_WAD) and
215 (AnsiLowerCase(filename) <> SHRSHADE_WAD) ) then
216 begin
217 // Копируем ресурс музыки:
218 if not f_packmap.ProcessResource(eWAD.Text, msection, filename, section, resource) then
219 begin
220 mr.Free();
221 Exit;
222 end;
224 // Переименовываем ресурс музыки:
225 res := utf2win(Format(':%s\%s', [msection, resource]));
226 ZeroMemory(@header.MusicName[0], 64);
227 CopyMemory(@header.MusicName[0], @res[1], Min(Length(res), 64));
228 end;
229 end;
232 // Нужно копировать дополнительные текстуры:
233 if cbTextrures.Checked and (textures <> nil) and
234 (gPanels <> nil) and (gTriggers <> nil) then
235 begin
236 for a := 0 to High(gPanels) do
237 begin
238 ok := False;
240 // Ссылаются ли на эту панель триггеры:
241 for b := 0 to High(gTriggers) do
242 if ( (gTriggers[b].TriggerType in [TRIGGER_OPENDOOR,
243 TRIGGER_CLOSEDOOR, TRIGGER_DOOR, TRIGGER_DOOR5,
244 TRIGGER_CLOSETRAP, TRIGGER_TRAP, TRIGGER_LIFTUP,
245 TRIGGER_LIFTDOWN, TRIGGER_LIFT]) and
246 (gTriggers[b].Data.PanelID = a) ) or
247 (gTriggers[b].TexturePanel = a) then
248 begin
249 ok := True;
250 Break;
251 end;
253 // Есть триггеры на эту панель:
254 if ok and (gPanels[a].TextureName <> '') and
255 (not IsSpecialTexture(gPanels[a].TextureName) and
256 g_Texture_NumNameFindStart(gPanels[a].TextureName) then
257 begin
258 while True do
259 begin
260 r := g_Texture_NumNameFindNext(res);
261 case r of
262 NNF_NAME_FOUND: ;
263 NNF_NAME_EQUALS: Continue;
264 else Break;
265 end;
267 if res = '' then
268 Break;
270 g_ProcessResourceStr(res, @filename, @section, @resource);
272 // Не записывать стандартные дополнительные текстуры:
273 if (not cbNonStandart.Checked) or
274 ( (AnsiLowerCase(filename) <> STANDART_WAD) and
275 (AnsiLowerCase(filename) <> SHRSHADE_WAD) ) then
276 begin
277 // Копируем ресурс дополнительной текстуры:
278 if f_packmap.ProcessResource(eWAD.Text, tsection, filename, section, resource) then
279 begin
281 Нужно проверять есть такая текстура textures и есть ли она вообще?
282 // Переименовываем ресурс текстуры:
283 res := utf2win(Format(':%s\%s', [tsection, resource]));
284 ZeroMemory(@textures[a].Resource[0], 64);
285 CopyMemory(@textures[a].Resource[0], @res[1], Min(Length(res), 64));
289 end;
290 end;
291 end; // while True
292 end;
293 end;
294 end;
297 // Записываем изменения карты:
298 mw := TMapWriter_1.Create();
300 mw.AddHeader(header);
301 mw.AddTextures(textures);
302 mw.AddPanels(mr.GetPanels());
303 mw.AddItems(mr.GetItems());
304 mw.AddAreas(mr.GetAreas());
305 mw.AddMonsters(mr.GetMonsters());
306 mw.AddTriggers(mr.GetTriggers());
308 // Сохраняем карту из памяти под новым именем в WAD-файл:
309 len := mw.SaveMap(data);
310 g_AddResource(eWAD.Text, '', eResource.Text, data, len, a);
311 mw.Free();
312 mr.Free();
313 Close();
315 ASSERT(a = 0); (* saved *)
316 MessageDlg(Format(_lc[I_MSG_PACKED], [eResource.Text, ExtractFileName(eWAD.Text)]), mtInformation, [mbOK], 0);
317 end;
319 procedure TPackMapForm.FormCreate(Sender: TObject);
320 begin
321 SaveDialog.InitialDir := EditorDir;
322 end;
324 end.