DEADSOFTWARE

restarting the same map will not reload textures (yay, quickload!); don't spam log...
[d2df-sdl.git] / src / shared / MAPDEF.pas
1 (* Copyright (C) DooM 2D:Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE a_modes.inc}
17 {$M+}
18 unit MAPDEF;
20 interface
22 uses
23 xdynrec;
26 const
27 MAP_SIGNATURE = 'MAP';
30 const
31 TEXTURE_NAME_WATER = '_water_0';
32 TEXTURE_NAME_ACID1 = '_water_1';
33 TEXTURE_NAME_ACID2 = '_water_2';
36 type
37 TDFPoint = packed record
38 public
39 X, Y: LongInt;
41 public
42 constructor Create (ax, ay: LongInt);
43 end;
45 Char16 = packed array[0..15] of Char;
46 Char32 = packed array[0..31] of Char;
47 Char64 = packed array[0..63] of Char;
48 Char100 = packed array[0..99] of Char;
49 Char256 = packed array[0..255] of Char;
50 Byte128 = packed array[0..127] of Byte;
52 {$INCLUDE mapdef.inc}
54 // various helpers to access map structures
55 type
56 TDynRecordHelper = class helper for TDynRecord
57 private
58 function getFieldWithType (const aname: AnsiString; atype: TDynField.TType): TDynField; inline;
60 function getPanelByIdx (idx: Integer): TDynRecord; inline;
62 function getPanelId (): Integer; inline;
63 //procedure setPanelId (v: Integer); inline;
65 function getTexturePanel (): Integer; inline;
66 //procedure setTexturePanel (v: Integer); inline;
68 function getPanelIndex (pan: TDynRecord): Integer;
70 function getPointField (const aname: AnsiString): TDFPoint; inline;
72 public
73 function panelCount (): Integer; inline;
75 // header
76 function mapName (): AnsiString; inline;
77 function mapAuthor (): AnsiString; inline;
78 function mapDesc (): AnsiString; inline;
79 function musicName (): AnsiString; inline;
80 function skyName (): AnsiString; inline;
82 // panel
83 function X (): Integer; inline;
84 function Y (): Integer; inline;
85 function Width (): Word; inline;
86 function Height (): Word; inline;
87 function TextureNum (): Word; inline;
88 function TextureRec (): TDynRecord; inline;
89 function PanelType (): Word; inline;
90 function Alpha (): Byte; inline;
91 function Flags (): Byte; inline;
93 // texture
94 function Resource (): AnsiString; inline;
95 function Anim (): Boolean; inline;
97 // item
98 function ItemType (): Byte; inline;
99 function Options (): Byte; inline;
101 // monster
102 function MonsterType (): Byte; inline; // type, ubyte
103 function Direction (): Byte; inline; // direction, ubyte
105 // area
106 function AreaType (): Byte; inline; // type, ubyte
107 //function Direction (): Byte; inline; // direction, ubyte
109 // trigger
110 function trigRec (): TDynRecord; inline;
111 function Enabled (): Boolean; inline; // enabled, bool
112 function TriggerType (): Byte; inline; // type, ubyte
113 function ActivateType (): Byte; inline; // activatetype, ubyte
114 function Keys (): Byte; inline; // keys, ubyte
115 //function DATA (): Byte128; inline; // triggerdata, trigdata[128]; // the only special nested structure
117 {$INCLUDE mapdef_help.inc}
118 function trigMonsterId (): Integer; inline;
120 private
121 // user fields
122 function getUserPanelId (): Integer; inline;
123 procedure setUserPanelId (v: Integer); inline;
125 function getUserTrigRef (): Boolean; inline;
126 procedure setUserTrigRef (v: Boolean); inline;
128 public
129 property panel[idx: Integer]: TDynRecord read getPanelByIdx;
130 property panelIndex[pan: TDynRecord]: Integer read getPanelIndex;
131 // triggers
132 property tgPanelID: Integer read getPanelId {write setPanelId};
133 property tgShotPanelID: Integer read getPanelId {write setPanelId};
134 property TexturePanel: Integer read getTexturePanel {write setTexturePanel}; // texturepanel, int
135 // user fields
136 property userPanelId: Integer read getUserPanelId write setUserPanelId;
137 property userPanelTrigRef: Boolean read getUserTrigRef write setUserTrigRef;
138 end;
140 implementation
142 uses
143 SysUtils, {e_log,} utils, xparser, xstreams;
146 // ////////////////////////////////////////////////////////////////////////// //
147 constructor TDFPoint.Create (ax, ay: LongInt); begin X := ax; Y := ay; end;
150 // ////////////////////////////////////////////////////////////////////////// //
151 function TDynRecordHelper.getUserPanelId (): Integer; inline;
152 var
153 fld: TDynField;
154 begin
155 fld := field['userPanelId'];
156 if (fld = nil) or (fld.baseType <> TDynField.TType.TInt) then result := -1 else result := fld.ival;
157 end;
160 procedure TDynRecordHelper.setUserPanelId (v: Integer); inline;
161 begin
162 setUserField('userPanelId', Integer(v));
163 end;
166 function TDynRecordHelper.getUserTrigRef (): Boolean; inline;
167 var
168 fld: TDynField;
169 begin
170 fld := field['userPanelTrigRef'];
171 if (fld = nil) or (fld.baseType <> TDynField.TType.TBool) then result := false else result := (fld.ival <> 0);
172 end;
175 procedure TDynRecordHelper.setUserTrigRef (v: Boolean); inline;
176 begin
177 setUserField('userPanelTrigRef', v);
178 end;
181 // ////////////////////////////////////////////////////////////////////////// //
182 function TDynRecordHelper.getFieldWithType (const aname: AnsiString; atype: TDynField.TType): TDynField; inline;
183 begin
184 result := field[aname];
185 if (result = nil) then raise Exception.Create(Format('field ''%s'' not found in record ''%s'' of type ''%s''', [aname, name, id]));
186 if (result.baseType <> atype) then raise Exception.Create(Format('field ''%s'' in record ''%s'' of type ''%s'' has invalid data type', [aname, name, id]));
187 end;
190 function TDynRecordHelper.getPointField (const aname: AnsiString): TDFPoint; inline;
191 var
192 fld: TDynField;
193 begin
194 fld := field[aname];
195 if (fld = nil) then raise Exception.Create(Format('field ''%s'' not found in record ''%s'' of type ''%s''', [aname, name, id]));
196 if (fld.baseType <> TPoint) then raise Exception.Create(Format('field ''%s'' in record ''%s'' of type ''%s'' has invalid data type', [aname, name, id]));
197 result := TDFPoint.Create(fld.ival, fld.ival2);
198 end;
201 function TDynRecordHelper.getPanelByIdx (idx: Integer): TDynRecord; inline;
202 var
203 fld: TDynField;
204 begin
205 fld := headerRec['panel'];
206 if (fld <> nil) then result := fld.item[idx] else result := nil;
207 end;
210 function TDynRecordHelper.getPanelIndex (pan: TDynRecord): Integer;
211 var
212 fld: TDynField;
213 f: Integer;
214 begin
215 result := -1;
216 if (pan <> nil) then
217 begin
218 fld := headerRec['panel'];
219 if (fld <> nil) then
220 begin
221 for f := 0 to fld.count-1 do if (fld.item[f] = pan) then begin result := f; exit; end;
222 end;
223 end;
224 end;
227 function TDynRecordHelper.panelCount (): Integer; inline;
228 var
229 fld: TDynField;
230 begin
231 fld := headerRec['panel'];
232 if (fld <> nil) then result := fld.count else result := 0;
233 end;
236 function TDynRecordHelper.TextureNum (): Word; inline;
237 var
238 idx: Integer;
239 fld: TDynField;
240 begin
241 fld := getFieldWithType('texture', TDynField.TType.TUShort);
242 idx := fld.recrefIndex;
243 if (idx < 0) then result := Word(TEXTURE_NONE) else result := Word(idx);
244 end;
247 // ////////////////////////////////////////////////////////////////////////// //
248 // trigger
249 function TDynRecordHelper.trigRec (): TDynRecord; inline;
250 var
251 fld: TDynField;
252 begin
253 fld := getFieldWithType('triggerdata', TDynField.TType.TTrigData);
254 if (fld <> nil) then result := fld.recref else result := nil;
255 end;
258 function TDynRecordHelper.trigMonsterId (): Integer; inline;
259 var
260 fld: TDynField;
261 begin
262 fld := getFieldWithType('monsterid', TDynField.TType.TInt);
263 result := fld.recrefIndex;
264 end;
267 // ////////////////////////////////////////////////////////////////////////// //
268 function TDynRecordHelper.mapName (): AnsiString; inline; begin result := utf2win(getFieldWithType('name', TDynField.TType.TChar).sval); end;
269 function TDynRecordHelper.mapAuthor (): AnsiString; inline; begin result := utf2win(getFieldWithType('author', TDynField.TType.TChar).sval); end;
270 function TDynRecordHelper.mapDesc (): AnsiString; inline; begin result := utf2win(getFieldWithType('description', TDynField.TType.TChar).sval); end;
271 function TDynRecordHelper.musicName (): AnsiString; inline; begin result := utf2win(getFieldWithType('music', TDynField.TType.TChar).sval); end;
272 function TDynRecordHelper.skyName (): AnsiString; inline; begin result := utf2win(getFieldWithType('sky', TDynField.TType.TChar).sval); end;
273 function TDynRecordHelper.X (): Integer; inline; begin result := getFieldWithType('position', TDynField.TType.TPoint).ival; end;
274 function TDynRecordHelper.Y (): Integer; inline; begin result := getFieldWithType('position', TDynField.TType.TPoint).ival2; end;
275 function TDynRecordHelper.Width (): Word; inline; begin result := Word(getFieldWithType('size', TDynField.TType.TSize).ival); end;
276 function TDynRecordHelper.Height (): Word; inline; begin result := Word(getFieldWithType('size', TDynField.TType.TSize).ival2); end;
277 function TDynRecordHelper.PanelType (): Word; inline; begin result := Word(getFieldWithType('type', TDynField.TType.TUShort).ival); end;
278 function TDynRecordHelper.TextureRec (): TDynRecord; inline; begin result := getFieldWithType('texture', TDynField.TType.TUShort).recref; end;
279 function TDynRecordHelper.Alpha (): Byte; inline; begin result := Byte(getFieldWithType('alpha', TDynField.TType.TUByte).ival); end;
280 function TDynRecordHelper.Flags (): Byte; inline; begin result := Byte(getFieldWithType('flags', TDynField.TType.TUByte).ival); end;
281 function TDynRecordHelper.Resource (): AnsiString; inline; begin result := utf2win(getFieldWithType('path', TDynField.TType.TChar).sval); end;
282 function TDynRecordHelper.Anim (): Boolean; inline; begin result := (getFieldWithType('animated', TDynField.TType.TBool).ival <> 0); end;
283 function TDynRecordHelper.ItemType (): Byte; inline; begin result := Byte(getFieldWithType('type', TDynField.TType.TUByte).ival); end;
284 function TDynRecordHelper.Options (): Byte; inline; begin result := Byte(getFieldWithType('options', TDynField.TType.TUByte).ival); end;
285 function TDynRecordHelper.MonsterType (): Byte; inline; begin result := Byte(getFieldWithType('type', TDynField.TType.TUByte).ival); end;
286 function TDynRecordHelper.Direction (): Byte; inline; begin result := Byte(getFieldWithType('direction', TDynField.TType.TUByte).ival); end;
287 function TDynRecordHelper.AreaType (): Byte; inline; begin result := Byte(getFieldWithType('type', TDynField.TType.TUByte).ival); end;
288 function TDynRecordHelper.Enabled (): Boolean; inline; begin result := (getFieldWithType('enabled', TDynField.TType.TBool).ival <> 0); end;
289 function TDynRecordHelper.TriggerType (): Byte; inline; begin result := Byte(getFieldWithType('type', TDynField.TType.TUByte).ival); end;
290 function TDynRecordHelper.ActivateType (): Byte; inline; begin result := Byte(getFieldWithType('activatetype', TDynField.TType.TUByte).ival); end;
291 function TDynRecordHelper.Keys (): Byte; inline; begin result := Byte(getFieldWithType('keys', TDynField.TType.TUByte).ival); end;
293 function TDynRecordHelper.getPanelId (): Integer; inline; begin result := getFieldWithType('panelid', TDynField.TType.TInt).recrefIndex; end;
294 function TDynRecordHelper.getTexturePanel (): Integer; begin result := getFieldWithType('texturepanel', TDynField.TType.TInt).recrefIndex; end;
296 {$INCLUDE mapdef_impl.inc}
299 end.