DEADSOFTWARE

FPC3.2.0 compat patch by deaddoomer
[d2df-sdl.git] / src / shared / MAPDEF.pas
index ad160252dcc5877460d99f413785999aa8df791f..5316d010f6a7875df557254d7ea4df71b3d35136 100644 (file)
@@ -1,9 +1,8 @@
-(* Copyright (C)  DooM 2D:Forever Developers
+(* Copyright (C)  Doom 2D: Forever Developers
  *
  * 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.
+ * the Free Software Foundation, version 3 of the License ONLY.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -44,32 +43,56 @@ type
     function isZero (): Boolean; inline;
   end;
 
-  Char16     = packed array[0..15] of Char;
-  Char32     = packed array[0..31] of Char;
-  Char64     = packed array[0..63] of Char;
-  Char100    = packed array[0..99] of Char;
-  Char256    = packed array[0..255] of Char;
-  Byte128    = packed array[0..127] of Byte;
+  TDFSize = packed record
+  public
+    w, h: LongInt;
+
+  public
+    constructor Create (aw, ah: LongInt);
+
+    function isZero (): Boolean; inline;
+    function isValid (): Boolean; inline;
+  end;
+
+  TDFColor = packed record
+  public
+    r, g, b, a: Byte; // a: 0 is transparent, 255 is opaque
+
+  public
+    constructor Create (ar, ag, ab: LongInt; aa: LongInt=0);
+
+    function isTransparent (): Boolean; inline;
+    function isOpaque (): Boolean; inline;
+    function isBlack (): Boolean; inline;
+    function isWhite (): Boolean; inline;
+  end;
 
 {$INCLUDE mapdef.inc}
 
 // various helpers to access map structures
 type
+  TDynFieldHelper = class helper for TDynField
+  public
+    function getRGBA (): TDFColor; inline;
+    procedure setRGBA (const v: TDFColor); inline;
+
+  public
+    property rgba: TDFColor read getRGBA write setRGBA; // for `TColor`
+  end;
+
   TDynRecordHelper = class helper for TDynRecord
   private
     function getFieldWithType (const aname: AnsiString; atype: TDynField.TType): TDynField; inline;
 
     function getPanelByIdx (idx: Integer): TDynRecord; inline;
 
-    function getPanelId (): Integer; inline;
-    //procedure setPanelId (v: Integer); inline;
-
     function getTexturePanel (): Integer; inline;
-    //procedure setTexturePanel (v: Integer); inline;
+    function getTexturePanelRec (): TDynRecord; inline;
 
     function getPanelIndex (pan: TDynRecord): Integer;
 
     function getPointField (const aname: AnsiString): TDFPoint; inline;
+    function getSizeField (const aname: AnsiString): TDFSize; inline;
 
   public
     function panelCount (): Integer; inline;
@@ -96,6 +119,14 @@ type
     function moveStart (): TDFPoint; inline;
     function moveEnd (): TDFPoint; inline;
 
+    function moveOnce (): Boolean; inline;
+
+    function sizeSpeed (): TDFSize; inline;
+    function sizeEnd (): TDFSize; inline;
+
+    function endPosTrig (): Integer; inline;
+    function endSizeTrig (): Integer; inline;
+
     // texture
     function Resource (): AnsiString; inline;
     function Anim (): Boolean; inline;
@@ -113,7 +144,7 @@ type
     //function Direction (): Byte; inline; // direction, ubyte
 
     // trigger
-    function trigRec (): TDynRecord; inline;
+    function trigRec (): TDynRecord; {inline;}
     function Enabled (): Boolean; inline; // enabled, bool
     function TriggerType (): Byte; inline; // type, ubyte
     function ActivateType (): Byte; inline; // activatetype, ubyte
@@ -123,6 +154,7 @@ type
     {$INCLUDE mapdef_help.inc}
     function trigMonsterId (): Integer; inline;
     function trigPanelId (): Integer; inline; // panel index in list
+    function trigPanelRec (): TDynRecord; inline;
 
   private
     // user fields
@@ -136,9 +168,10 @@ type
     property panel[idx: Integer]: TDynRecord read getPanelByIdx;
     property panelIndex[pan: TDynRecord]: Integer read getPanelIndex;
     // triggers
-    property tgPanelID: Integer read getPanelId {write setPanelId};
-    property tgShotPanelID: Integer read getPanelId {write setPanelId};
-    property TexturePanel: Integer read getTexturePanel {write setTexturePanel}; // texturepanel, int
+    property tgPanelId: Integer read trigPanelId;
+    property tgPanelRec: TDynRecord read trigPanelRec;
+    property TexturePanelId: Integer read getTexturePanel; // texturepanel, int
+    property TexturePanelRec: TDynRecord read getTexturePanelRec;
     // user fields
     property userPanelId: Integer read getUserPanelId write setUserPanelId;
     property userPanelTrigRef: Boolean read getUserTrigRef write setUserTrigRef;
@@ -155,6 +188,28 @@ constructor TDFPoint.Create (ax, ay: LongInt); begin X := ax; Y := ay; end;
 function TDFPoint.isZero (): Boolean; inline; begin result := (X = 0) and (Y = 0); end;
 
 
+constructor TDFSize.Create (aw, ah: LongInt); begin w := aw; h := ah; end;
+function TDFSize.isZero (): Boolean; inline; begin result := (w = 0) and (h = 0); end;
+function TDFSize.isValid (): Boolean; inline; begin result := (w > 0) and (h > 0); end;
+
+constructor TDFColor.Create (ar, ag, ab: LongInt; aa: LongInt=0);
+begin
+  if (ar < 0) then r := 0 else if (ar > 255) then r := 255 else r := Byte(ar);
+  if (ag < 0) then g := 0 else if (ag > 255) then g := 255 else g := Byte(ag);
+  if (ab < 0) then b := 0 else if (ab > 255) then b := 255 else b := Byte(ab);
+  if (aa < 0) then a := 0 else if (aa > 255) then a := 255 else a := Byte(aa);
+end;
+function TDFColor.isTransparent (): Boolean; inline; begin result := (a = 0); end;
+function TDFColor.isOpaque (): Boolean; inline; begin result := (a = 255); end;
+function TDFColor.isBlack (): Boolean; inline; begin result := (r = 0) and (g = 0) and (b = 0); end;
+function TDFColor.isWhite (): Boolean; inline; begin result := (r = 255) and (g = 255) and (b = 255); end;
+
+
+// ////////////////////////////////////////////////////////////////////////// //
+function TDynFieldHelper.getRGBA (): TDFColor; inline; begin result := TDFColor.Create(red, green, blue, alpha); end;
+procedure TDynFieldHelper.setRGBA (const v: TDFColor); inline; begin red := v.r; green := v.g; blue := v.b; alpha := v.a; end;
+
+
 // ////////////////////////////////////////////////////////////////////////// //
 function TDynRecordHelper.getUserPanelId (): Integer; inline;
 var
@@ -162,7 +217,7 @@ var
 begin
   fld := field['userPanelId'];
   //if (fld = nil) or (fld.baseType <> TDynField.TType.TInt) then result := -1 else result := fld.ival;
-  if (fld = nil) then result := -1 else result := Integer(fld.varvalue);
+  if (fld = nil) then result := -1 else result := Integer(fld.value);
 end;
 
 
@@ -177,7 +232,7 @@ var
   fld: TDynField;
 begin
   fld := field['userPanelTrigRef'];
-  if (fld = nil) then result := false else result := Boolean(fld.varvalue);
+  if (fld = nil) then result := false else result := Boolean(fld.value);
   //if (fld = nil) or (fld.baseType <> TDynField.TType.TBool) then result := false else result := (fld.ival <> 0);
 end;
 
@@ -193,13 +248,35 @@ function TDynRecordHelper.moveSpeed (): TDFPoint; inline; begin result := getPoi
 function TDynRecordHelper.moveStart (): TDFPoint; inline; begin result := getPointField('move_start'); end;
 function TDynRecordHelper.moveEnd (): TDFPoint; inline; begin result := getPointField('move_end'); end;
 
+function TDynRecordHelper.sizeSpeed (): TDFSize; inline; begin result := getSizeField('size_speed'); end;
+function TDynRecordHelper.sizeEnd (): TDFSize; inline; begin result := getSizeField('size_end'); end;
+
+function TDynRecordHelper.moveOnce (): Boolean; inline; begin result := (getFieldWithType('move_once', TDynField.TType.TBool).ival <> 0); end;
+
+
+function TDynRecordHelper.endPosTrig (): Integer; inline;
+var
+  fld: TDynField;
+begin
+  fld := getFieldWithType('end_pos_trigger', TDynField.TType.TInt);
+  result := fld.recrefIndex;
+end;
+
+function TDynRecordHelper.endSizeTrig (): Integer; inline;
+var
+  fld: TDynField;
+begin
+  fld := getFieldWithType('end_size_trigger', TDynField.TType.TInt);
+  result := fld.recrefIndex;
+end;
+
 
 // ////////////////////////////////////////////////////////////////////////// //
 function TDynRecordHelper.getFieldWithType (const aname: AnsiString; atype: TDynField.TType): TDynField; inline;
 begin
   result := field[aname];
-  if (result = nil) then raise Exception.Create(Format('field ''%s'' not found in record ''%s'' of type ''%s''', [aname, name, id]));
-  if (result.baseType <> atype) then raise Exception.Create(Format('field ''%s'' in record ''%s'' of type ''%s'' has invalid data type', [aname, name, id]));
+  if (result = nil) then raise Exception.Create(Format('field ''%s'' not found in record ''%s'' of type ''%s''', [aname, typeName, id]));
+  if (result.baseType <> atype) then raise Exception.Create(Format('field ''%s'' in record ''%s'' of type ''%s'' has invalid data type', [aname, typeName, id]));
 end;
 
 
@@ -208,18 +285,29 @@ var
   fld: TDynField;
 begin
   fld := field[aname];
-  if (fld = nil) then raise Exception.Create(Format('field ''%s'' not found in record ''%s'' of type ''%s''', [aname, name, id]));
-  if (fld.baseType <> TPoint) then raise Exception.Create(Format('field ''%s'' in record ''%s'' of type ''%s'' has invalid data type', [aname, name, id]));
+  if (fld = nil) then raise Exception.Create(Format('field ''%s'' not found in record ''%s'' of type ''%s''', [aname, typeName, id]));
+  if (fld.baseType <> fld.TType.TPoint) then raise Exception.Create(Format('field ''%s'' in record ''%s'' of type ''%s'' has invalid data type', [aname, typeName, id]));
   result := TDFPoint.Create(fld.ival, fld.ival2);
 end;
 
 
+function TDynRecordHelper.getSizeField (const aname: AnsiString): TDFSize; inline;
+var
+  fld: TDynField;
+begin
+  fld := field[aname];
+  if (fld = nil) then raise Exception.Create(Format('field ''%s'' not found in record ''%s'' of type ''%s''', [aname, typeName, id]));
+  if (fld.baseType <> fld.TType.TSize) and (fld.baseType <> fld.TType.TPoint) then raise Exception.Create(Format('field ''%s'' in record ''%s'' of type ''%s'' has invalid data type', [aname, typeName, id]));
+  result := TDFSize.Create(fld.ival, fld.ival2);
+end;
+
+
 function TDynRecordHelper.getPanelByIdx (idx: Integer): TDynRecord; inline;
 var
   fld: TDynField;
 begin
   fld := headerRec['panel'];
-  if (fld <> nil) then result := fld.item[idx] else result := nil;
+  if (fld <> nil) then result := fld.itemAt[idx] else result := nil;
 end;
 
 
@@ -234,7 +322,7 @@ begin
     fld := headerRec['panel'];
     if (fld <> nil) then
     begin
-      for f := 0 to fld.count-1 do if (fld.item[f] = pan) then begin result := f; exit; end;
+      for f := 0 to fld.count-1 do if (fld.itemAt[f] = pan) then begin result := f; exit; end;
     end;
   end;
 end;
@@ -262,7 +350,7 @@ end;
 
 // ////////////////////////////////////////////////////////////////////////// //
 // trigger
-function TDynRecordHelper.trigRec (): TDynRecord; inline;
+function TDynRecordHelper.trigRec (): TDynRecord; {inline;}
 var
   fld: TDynField;
 begin
@@ -270,22 +358,66 @@ begin
   if (fld <> nil) then result := fld.recref else result := nil;
 end;
 
-
 function TDynRecordHelper.trigMonsterId (): Integer; inline;
 var
   fld: TDynField;
 begin
-  fld := getFieldWithType('monsterid', TDynField.TType.TInt);
+  result := -1;
+  fld := field['monsterid'];
+  if (fld = nil) then exit;
+  if (fld.baseType <> TDynField.TType.TInt) then exit;
+  if (fld.recref = nil) then exit;
   result := fld.recrefIndex;
 end;
 
+function TDynRecordHelper.trigPanelRec (): TDynRecord; inline;
+var
+  fld: TDynField;
+begin
+  result := nil;
+  fld := field['panelid'];
+  if (fld = nil) then exit;
+  if (fld.baseType <> TDynField.TType.TInt) then exit;
+  result := fld.recref;
+  if (result <> nil) and (result.typeName <> 'panel') then result := nil;
+end;
 
 // panel index in list
 function TDynRecordHelper.trigPanelId (): Integer; inline;
 var
   fld: TDynField;
 begin
-  fld := getFieldWithType('panelid', TDynField.TType.TInt);
+  result := -1;
+  fld := field['panelid'];
+  if (fld = nil) then exit;
+  if (fld.baseType <> TDynField.TType.TInt) then exit;
+  if (fld.recref = nil) then exit;
+  if (fld.recref.typeName <> 'panel') then exit;
+  result := fld.recrefIndex;
+end;
+
+function TDynRecordHelper.getTexturePanelRec (): TDynRecord;
+var
+  fld: TDynField;
+begin
+  result := nil;
+  fld := field['texture_panel'];
+  if (fld = nil) then exit;
+  if (fld.baseType <> TDynField.TType.TInt) then exit;
+  result := fld.recref;
+  if (result <> nil) and (result.typeName <> 'panel') then result := nil;
+end;
+
+function TDynRecordHelper.getTexturePanel (): Integer;
+var
+  fld: TDynField;
+begin
+  result := -1;
+  fld := field['texture_panel'];
+  if (fld = nil) then exit;
+  if (fld.baseType <> TDynField.TType.TInt) then exit;
+  if (fld.recref = nil) then exit;
+  if (fld.recref.typeName <> 'panel') then exit;
   result := fld.recrefIndex;
 end;
 
@@ -313,12 +445,9 @@ function TDynRecordHelper.Direction (): Byte; inline; begin result := Byte(getFi
 function TDynRecordHelper.AreaType (): Byte; inline; begin result := Byte(getFieldWithType('type', TDynField.TType.TUByte).ival); end;
 function TDynRecordHelper.Enabled (): Boolean; inline; begin result := (getFieldWithType('enabled', TDynField.TType.TBool).ival <> 0); end;
 function TDynRecordHelper.TriggerType (): Byte; inline; begin result := Byte(getFieldWithType('type', TDynField.TType.TUByte).ival); end;
-function TDynRecordHelper.ActivateType (): Byte; inline; begin result := Byte(getFieldWithType('activatetype', TDynField.TType.TUByte).ival); end;
+function TDynRecordHelper.ActivateType (): Byte; inline; begin result := Byte(getFieldWithType('activate_type', TDynField.TType.TUByte).ival); end;
 function TDynRecordHelper.Keys (): Byte; inline; begin result := Byte(getFieldWithType('keys', TDynField.TType.TUByte).ival); end;
 
-function TDynRecordHelper.getPanelId (): Integer; inline; begin result := getFieldWithType('panelid', TDynField.TType.TInt).recrefIndex; end;
-function TDynRecordHelper.getTexturePanel (): Integer; begin result := getFieldWithType('texturepanel', TDynField.TType.TInt).recrefIndex; end;
-
 {$INCLUDE mapdef_impl.inc}