DEADSOFTWARE

migrating from PanelIDs to panel GUIDs; part one
[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);
44 function isZero (): Boolean; inline;
45 end;
47 Char16 = packed array[0..15] of Char;
48 Char32 = packed array[0..31] of Char;
49 Char64 = packed array[0..63] of Char;
50 Char100 = packed array[0..99] of Char;
51 Char256 = packed array[0..255] of Char;
52 Byte128 = packed array[0..127] of Byte;
54 {$INCLUDE mapdef.inc}
56 // various helpers to access map structures
57 type
58 TDynRecordHelper = class helper for TDynRecord
59 private
60 function getFieldWithType (const aname: AnsiString; atype: TDynField.TType): TDynField; inline;
62 function getPanelByIdx (idx: Integer): TDynRecord; inline;
64 function getPanelId (): Integer; inline;
65 //procedure setPanelId (v: Integer); inline;
67 function getTexturePanel (): Integer; inline;
68 //procedure setTexturePanel (v: Integer); inline;
70 function getPanelIndex (pan: TDynRecord): Integer;
72 function getPointField (const aname: AnsiString): TDFPoint; inline;
74 public
75 function panelCount (): Integer; inline;
77 // header
78 function mapName (): AnsiString; inline;
79 function mapAuthor (): AnsiString; inline;
80 function mapDesc (): AnsiString; inline;
81 function musicName (): AnsiString; inline;
82 function skyName (): AnsiString; inline;
84 // panel
85 function X (): Integer; inline;
86 function Y (): Integer; inline;
87 function Width (): Word; inline;
88 function Height (): Word; inline;
89 function TextureNum (): Word; inline;
90 function TextureRec (): TDynRecord; inline;
91 function PanelType (): Word; inline;
92 function Alpha (): Byte; inline;
93 function Flags (): Byte; inline;
95 function moveSpeed (): TDFPoint; inline;
96 function moveStart (): TDFPoint; inline;
97 function moveEnd (): TDFPoint; inline;
99 // texture
100 function Resource (): AnsiString; inline;
101 function Anim (): Boolean; inline;
103 // item
104 function ItemType (): Byte; inline;
105 function Options (): Byte; inline;
107 // monster
108 function MonsterType (): Byte; inline; // type, ubyte
109 function Direction (): Byte; inline; // direction, ubyte
111 // area
112 function AreaType (): Byte; inline; // type, ubyte
113 //function Direction (): Byte; inline; // direction, ubyte
115 // trigger
116 function trigRec (): TDynRecord; inline;
117 function Enabled (): Boolean; inline; // enabled, bool
118 function TriggerType (): Byte; inline; // type, ubyte
119 function ActivateType (): Byte; inline; // activatetype, ubyte
120 function Keys (): Byte; inline; // keys, ubyte
121 //function DATA (): Byte128; inline; // triggerdata, trigdata[128]; // the only special nested structure
123 {$INCLUDE mapdef_help.inc}
124 function trigMonsterId (): Integer; inline;
125 function trigPanelId (): Integer; inline; // panel index in list
127 private
128 // user fields
129 function getUserPanelId (): Integer; inline;
130 procedure setUserPanelId (v: Integer); inline;
132 function getUserTrigRef (): Boolean; inline;
133 procedure setUserTrigRef (v: Boolean); inline;
135 public
136 property panel[idx: Integer]: TDynRecord read getPanelByIdx;
137 property panelIndex[pan: TDynRecord]: Integer read getPanelIndex;
138 // triggers
139 property tgPanelID: Integer read getPanelId {write setPanelId};
140 property tgShotPanelID: Integer read getPanelId {write setPanelId};
141 property TexturePanel: Integer read getTexturePanel {write setTexturePanel}; // texturepanel, int
142 // user fields
143 property userPanelId: Integer read getUserPanelId write setUserPanelId;
144 property userPanelTrigRef: Boolean read getUserTrigRef write setUserTrigRef;
145 end;
147 implementation
149 uses
150 SysUtils, {e_log,} utils, xparser, xstreams;
153 // ////////////////////////////////////////////////////////////////////////// //
154 constructor TDFPoint.Create (ax, ay: LongInt); begin X := ax; Y := ay; end;
155 function TDFPoint.isZero (): Boolean; inline; begin result := (X = 0) and (Y = 0); end;
158 // ////////////////////////////////////////////////////////////////////////// //
159 function TDynRecordHelper.getUserPanelId (): Integer; inline;
160 var
161 fld: TDynField;
162 begin
163 fld := field['userPanelId'];
164 //if (fld = nil) or (fld.baseType <> TDynField.TType.TInt) then result := -1 else result := fld.ival;
165 if (fld = nil) then result := -1 else result := Integer(fld.varvalue);
166 end;
169 procedure TDynRecordHelper.setUserPanelId (v: Integer); inline;
170 begin
171 user['userPanelId'] := v;
172 end;
175 function TDynRecordHelper.getUserTrigRef (): Boolean; inline;
176 var
177 fld: TDynField;
178 begin
179 fld := field['userPanelTrigRef'];
180 if (fld = nil) then result := false else result := Boolean(fld.varvalue);
181 //if (fld = nil) or (fld.baseType <> TDynField.TType.TBool) then result := false else result := (fld.ival <> 0);
182 end;
185 procedure TDynRecordHelper.setUserTrigRef (v: Boolean); inline;
186 begin
187 user['userPanelTrigRef'] := v;
188 end;
191 // ////////////////////////////////////////////////////////////////////////// //
192 function TDynRecordHelper.moveSpeed (): TDFPoint; inline; begin result := getPointField('move_speed'); end;
193 function TDynRecordHelper.moveStart (): TDFPoint; inline; begin result := getPointField('move_start'); end;
194 function TDynRecordHelper.moveEnd (): TDFPoint; inline; begin result := getPointField('move_end'); end;
197 // ////////////////////////////////////////////////////////////////////////// //
198 function TDynRecordHelper.getFieldWithType (const aname: AnsiString; atype: TDynField.TType): TDynField; inline;
199 begin
200 result := field[aname];
201 if (result = nil) then raise Exception.Create(Format('field ''%s'' not found in record ''%s'' of type ''%s''', [aname, name, id]));
202 if (result.baseType <> atype) then raise Exception.Create(Format('field ''%s'' in record ''%s'' of type ''%s'' has invalid data type', [aname, name, id]));
203 end;
206 function TDynRecordHelper.getPointField (const aname: AnsiString): TDFPoint; inline;
207 var
208 fld: TDynField;
209 begin
210 fld := field[aname];
211 if (fld = nil) then raise Exception.Create(Format('field ''%s'' not found in record ''%s'' of type ''%s''', [aname, name, id]));
212 if (fld.baseType <> TPoint) then raise Exception.Create(Format('field ''%s'' in record ''%s'' of type ''%s'' has invalid data type', [aname, name, id]));
213 result := TDFPoint.Create(fld.ival, fld.ival2);
214 end;
217 function TDynRecordHelper.getPanelByIdx (idx: Integer): TDynRecord; inline;
218 var
219 fld: TDynField;
220 begin
221 fld := headerRec['panel'];
222 if (fld <> nil) then result := fld.item[idx] else result := nil;
223 end;
226 function TDynRecordHelper.getPanelIndex (pan: TDynRecord): Integer;
227 var
228 fld: TDynField;
229 f: Integer;
230 begin
231 result := -1;
232 if (pan <> nil) then
233 begin
234 fld := headerRec['panel'];
235 if (fld <> nil) then
236 begin
237 for f := 0 to fld.count-1 do if (fld.item[f] = pan) then begin result := f; exit; end;
238 end;
239 end;
240 end;
243 function TDynRecordHelper.panelCount (): Integer; inline;
244 var
245 fld: TDynField;
246 begin
247 fld := headerRec['panel'];
248 if (fld <> nil) then result := fld.count else result := 0;
249 end;
252 function TDynRecordHelper.TextureNum (): Word; inline;
253 var
254 idx: Integer;
255 fld: TDynField;
256 begin
257 fld := getFieldWithType('texture', TDynField.TType.TUShort);
258 idx := fld.recrefIndex;
259 if (idx < 0) then result := Word(TEXTURE_NONE) else result := Word(idx);
260 end;
263 // ////////////////////////////////////////////////////////////////////////// //
264 // trigger
265 function TDynRecordHelper.trigRec (): TDynRecord; inline;
266 var
267 fld: TDynField;
268 begin
269 fld := getFieldWithType('triggerdata', TDynField.TType.TTrigData);
270 if (fld <> nil) then result := fld.recref else result := nil;
271 end;
274 function TDynRecordHelper.trigMonsterId (): Integer; inline;
275 var
276 fld: TDynField;
277 begin
278 fld := getFieldWithType('monsterid', TDynField.TType.TInt);
279 result := fld.recrefIndex;
280 end;
283 // panel index in list
284 function TDynRecordHelper.trigPanelId (): Integer; inline;
285 var
286 fld: TDynField;
287 begin
288 fld := getFieldWithType('panelid', TDynField.TType.TInt);
289 result := fld.recrefIndex;
290 end;
293 // ////////////////////////////////////////////////////////////////////////// //
294 function TDynRecordHelper.mapName (): AnsiString; inline; begin result := utf2win(getFieldWithType('name', TDynField.TType.TChar).sval); end;
295 function TDynRecordHelper.mapAuthor (): AnsiString; inline; begin result := utf2win(getFieldWithType('author', TDynField.TType.TChar).sval); end;
296 function TDynRecordHelper.mapDesc (): AnsiString; inline; begin result := utf2win(getFieldWithType('description', TDynField.TType.TChar).sval); end;
297 function TDynRecordHelper.musicName (): AnsiString; inline; begin result := utf2win(getFieldWithType('music', TDynField.TType.TChar).sval); end;
298 function TDynRecordHelper.skyName (): AnsiString; inline; begin result := utf2win(getFieldWithType('sky', TDynField.TType.TChar).sval); end;
299 function TDynRecordHelper.X (): Integer; inline; begin result := getFieldWithType('position', TDynField.TType.TPoint).ival; end;
300 function TDynRecordHelper.Y (): Integer; inline; begin result := getFieldWithType('position', TDynField.TType.TPoint).ival2; end;
301 function TDynRecordHelper.Width (): Word; inline; begin result := Word(getFieldWithType('size', TDynField.TType.TSize).ival); end;
302 function TDynRecordHelper.Height (): Word; inline; begin result := Word(getFieldWithType('size', TDynField.TType.TSize).ival2); end;
303 function TDynRecordHelper.PanelType (): Word; inline; begin result := Word(getFieldWithType('type', TDynField.TType.TUShort).ival); end;
304 function TDynRecordHelper.TextureRec (): TDynRecord; inline; begin result := getFieldWithType('texture', TDynField.TType.TUShort).recref; end;
305 function TDynRecordHelper.Alpha (): Byte; inline; begin result := Byte(getFieldWithType('alpha', TDynField.TType.TUByte).ival); end;
306 function TDynRecordHelper.Flags (): Byte; inline; begin result := Byte(getFieldWithType('flags', TDynField.TType.TUByte).ival); end;
307 function TDynRecordHelper.Resource (): AnsiString; inline; begin result := utf2win(getFieldWithType('path', TDynField.TType.TChar).sval); end;
308 function TDynRecordHelper.Anim (): Boolean; inline; begin result := (getFieldWithType('animated', TDynField.TType.TBool).ival <> 0); end;
309 function TDynRecordHelper.ItemType (): Byte; inline; begin result := Byte(getFieldWithType('type', TDynField.TType.TUByte).ival); end;
310 function TDynRecordHelper.Options (): Byte; inline; begin result := Byte(getFieldWithType('options', TDynField.TType.TUByte).ival); end;
311 function TDynRecordHelper.MonsterType (): Byte; inline; begin result := Byte(getFieldWithType('type', TDynField.TType.TUByte).ival); end;
312 function TDynRecordHelper.Direction (): Byte; inline; begin result := Byte(getFieldWithType('direction', TDynField.TType.TUByte).ival); end;
313 function TDynRecordHelper.AreaType (): Byte; inline; begin result := Byte(getFieldWithType('type', TDynField.TType.TUByte).ival); end;
314 function TDynRecordHelper.Enabled (): Boolean; inline; begin result := (getFieldWithType('enabled', TDynField.TType.TBool).ival <> 0); end;
315 function TDynRecordHelper.TriggerType (): Byte; inline; begin result := Byte(getFieldWithType('type', TDynField.TType.TUByte).ival); end;
316 function TDynRecordHelper.ActivateType (): Byte; inline; begin result := Byte(getFieldWithType('activatetype', TDynField.TType.TUByte).ival); end;
317 function TDynRecordHelper.Keys (): Byte; inline; begin result := Byte(getFieldWithType('keys', TDynField.TType.TUByte).ival); end;
319 function TDynRecordHelper.getPanelId (): Integer; inline; begin result := getFieldWithType('panelid', TDynField.TType.TInt).recrefIndex; end;
320 function TDynRecordHelper.getTexturePanel (): Integer; begin result := getFieldWithType('texturepanel', TDynField.TType.TInt).recrefIndex; end;
322 {$INCLUDE mapdef_impl.inc}
325 end.