DEADSOFTWARE

no more old-styled map structured; sadly, most triggers aren't working; also, save...
[d2df-sdl.git] / src / shared / xdynrec.pas
index 3e9ba66b1b9f4c3d6608857e7edf95d3842c907d..8ebac36188754ef6349bbb7bbc5147a1a0ccfe7a 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *)
 {$INCLUDE a_modes.inc}
+{.$DEFINE XDYNREC_USE_FIELDHASH} // actually, it is SLOWER with this
 unit xdynrec;
 
 interface
 
 uses
   Classes,
-  xparser, xstreams, utils;
+  xparser, xstreams, utils, hashtable;
 
 
 // ////////////////////////////////////////////////////////////////////////// //
@@ -58,10 +59,10 @@ type
     mIVal2: Integer; // for point and size
     mSVal: AnsiString; // string; for byte and char arrays
     mRVal: TDynRecList; // for list
+    mRHash: THashStrInt; // id -> index in mRVal
     mRecRef: TDynRecord; // for TEBS.TRec
     mMaxDim: Integer; // for byte and char arrays; <0: not an array; 0: impossible value
     mBinOfs: Integer; // offset in binary; <0 - none
-    mRecOfs: Integer; // offset in record; <0 - none
     mSepPosSize: Boolean; // for points and sizes, use separate fields
     mAsT: Boolean; // for points and sizes, use separate fields, names starts with `t`
     mDefined: Boolean;
@@ -83,6 +84,10 @@ type
     // for binary parser
     mRecRefId: AnsiString;
 
+    // for userdata
+    mTagInt: Integer;
+    mTagPtr: Pointer;
+
   private
     procedure cleanup ();
 
@@ -92,6 +97,31 @@ type
     procedure fixDefaultValue (); // this will NOT clone `mDefRecRef`
     function isDefaultValue (): Boolean;
 
+    function getListCount (): Integer; inline;
+    function getListItem (idx: Integer): TDynRecord; inline; overload;
+    function getListItem (const aname: AnsiString): TDynRecord; inline; overload;
+
+    function getRecRefIndex (): Integer;
+
+    procedure setIVal (v: Integer); inline;
+
+  protected
+    // returns `true` for duplicate record id
+    function addListItem (rec: TDynRecord): Boolean; inline;
+
+  public
+    type
+      TListEnumerator = record
+      private
+        mList: TDynRecList;
+        mCurIdx: Integer;
+      public
+        constructor Create (alist: TDynRecList);
+        function MoveNext (): Boolean; inline;
+        function getCurrent (): TDynRecord; inline;
+        property Current: TDynRecord read getCurrent;
+      end;
+
   public
     constructor Create (const aname: AnsiString; atype: TType);
     constructor Create (pr: TTextParser);
@@ -115,25 +145,35 @@ type
 
     procedure setValue (const s: AnsiString);
 
+    function GetEnumerator (): TListEnumerator;
+
   public
     property pasname: AnsiString read mPasName;
     property name: AnsiString read mName;
     property baseType: TType read mType;
+    property negbool: Boolean read mNegBool;
     property defined: Boolean read mDefined write mDefined;
     property internal: Boolean read mInternal write mInternal;
-    property ival: Integer read mIVal;
+    property hasTPrefix: Boolean read mAsT;
+    property separatePasFields: Boolean read mSepPosSize;
+    property binOfs: Integer read mBinOfs;
+    property ival: Integer read mIVal write setIVal;
+    property ival2: Integer read mIVal2;
     property sval: AnsiString read mSVal;
     property hasDefault: Boolean read mHasDefault;
     property defsval: AnsiString read mDefSVal;
     property ebs: TEBS read mEBS;
     property ebstype: TObject read mEBSType;
     property ebstypename: AnsiString read mEBSTypeName; // enum/bitset name
-    property list: TDynRecList read mRVal; // for list
-
-    property x: Integer read mIVal;
-    property w: Integer read mIVal;
-    property y: Integer read mIVal2;
-    property h: Integer read mIVal2;
+    property recref: TDynRecord read mRecRef write mRecRef; //FIXME: writing is a hack!
+    property recrefIndex: Integer read getRecRefIndex; // search for this record in header; -1: not found
+    // for lists
+    property count: Integer read getListCount;
+    property item[idx: Integer]: TDynRecord read getListItem;
+    property items[const aname: AnsiString]: TDynRecord read getListItem; default; // alas, FPC 3+ lost property overloading feature
+    // userdata
+    property tagInt: Integer read mTagInt write mTagInt;
+    property tagPtr: Pointer read mTagPtr write mTagPtr;
   end;
 
 
@@ -146,25 +186,40 @@ type
     mName: AnsiString;
     mSize: Integer;
     mFields: TDynFieldList;
+    {$IF DEFINED(XDYNREC_USE_FIELDHASH)}
+    mFieldsHash: THashStrInt; // id -> index in mRVal
+    {$ENDIF}
     mTrigTypes: array of AnsiString; // if this is triggerdata, we'll hold list of triggers here
     mHeader: Boolean; // true for header record
     mBinBlock: Integer; // -1: none
     mHeaderRec: TDynRecord; // for "value" records this is header record with data, for "type" records this is header type record
 
+    // for userdata
+    mTagInt: Integer;
+    mTagPtr: Pointer;
+
   private
     procedure parseDef (pr: TTextParser); // parse definition
 
     function findByName (const aname: AnsiString): Integer; inline;
     function hasByName (const aname: AnsiString): Boolean; inline;
     function getFieldByName (const aname: AnsiString): TDynField; inline;
+    function getFieldAt (idx: Integer): TDynField; inline;
+    function getCount (): Integer; inline;
 
     function getIsTrigData (): Boolean; inline;
     function getIsForTrig (const aname: AnsiString): Boolean; inline;
 
+    function getForTrigCount (): Integer; inline;
+    function getForTrigAt (idx: Integer): AnsiString; inline;
+
   protected
     function findRecordByTypeId (const atypename, aid: AnsiString): TDynRecord;
     function findRecordNumByType (const atypename: AnsiString; rc: TDynRecord): Integer;
-    procedure addRecordByType (const atypename: AnsiString; rc: TDynRecord);
+    function addRecordByType (const atypename: AnsiString; rc: TDynRecord): Boolean; // `true`: duplicate record id
+
+    procedure addField (fld: TDynField); inline;
+    function addFieldChecked (fld: TDynField): Boolean; inline; // `true`: duplicate name
 
   public
     constructor Create ();
@@ -195,13 +250,20 @@ type
     property pasname: AnsiString read mPasName;
     property name: AnsiString read mName; // record name
     property size: Integer read mSize; // size in bytes
-    property fields: TDynFieldList read mFields;
+    //property fields: TDynFieldList read mFields;
     property has[const aname: AnsiString]: Boolean read hasByName;
-    property field[const aname: AnsiString]: TDynField read getFieldByName;
+    property count: Integer read getCount;
+    property field[const aname: AnsiString]: TDynField read getFieldByName; default;
+    property fieldAt[idx: Integer]: TDynField read getFieldAt;
     property isTrigData: Boolean read getIsTrigData;
     property isForTrig[const aname: AnsiString]: Boolean read getIsForTrig;
-    property headerType: TDynRecord read mHeaderRec;
+    property forTrigCount: Integer read getForTrigCount;
+    property forTrigAt[idx: Integer]: AnsiString read getForTrigAt;
+    property headerRec: TDynRecord read mHeaderRec;
     property isHeader: Boolean read mHeader;
+    // userdata
+    property tagInt: Integer read mTagInt write mTagInt;
+    property tagPtr: Pointer read mTagPtr write mTagPtr;
   end;
 
   TDynEBS = class
@@ -252,6 +314,9 @@ type
 
     function getHeaderRecType (): TDynRecord; inline;
 
+    function getTrigTypeCount (): Integer; inline;
+    function getTrigTypeAt (idx: Integer): TDynRecord; inline;
+
   public
     constructor Create (pr: TTextParser); // parses data definition
     destructor Destroy (); override;
@@ -261,6 +326,7 @@ type
     function findEBSType (const aname: AnsiString): TDynEBS;
 
     function pasdef (): AnsiString;
+    function pasdefconst (): AnsiString;
 
     // creates new header record
     function parseMap (pr: TTextParser): TDynRecord;
@@ -270,6 +336,8 @@ type
 
   public
     property headerType: TDynRecord read getHeaderRecType;
+    property trigTypeCount: Integer read getTrigTypeCount;
+    property trigType[idx: Integer]: TDynRecord read getTrigTypeAt;
   end;
 
 
@@ -281,7 +349,7 @@ procedure xdynDumpProfiles ();
 implementation
 
 uses
-  SysUtils, e_log, hashtable
+  SysUtils, e_log
   {$IF DEFINED(D2D_DYNREC_PROFILER)},xprofiler{$ENDIF};
 
 
@@ -289,15 +357,47 @@ uses
 function StrEqu (const a, b: AnsiString): Boolean; inline; begin result := (a = b); end;
 
 
+// ////////////////////////////////////////////////////////////////////////// //
+constructor TDynField.TListEnumerator.Create (alist: TDynRecList);
+begin
+  mList := alist;
+  mCurIdx := -1;
+end;
+
+
+function TDynField.TListEnumerator.MoveNext (): Boolean; inline;
+begin
+  Inc(mCurIdx);
+  result := (mList <> nil) and (mCurIdx < mList.count);
+end;
+
+
+function TDynField.TListEnumerator.getCurrent (): TDynRecord; inline;
+begin
+  result := mList[mCurIdx];
+end;
+
+
+function TDynField.GetEnumerator (): TListEnumerator;
+begin
+  result := TListEnumerator.Create(mRVal);
+end;
+
+
 // ////////////////////////////////////////////////////////////////////////// //
 constructor TDynField.Create (const aname: AnsiString; atype: TType);
 begin
   mRVal := nil;
   mRecRef := nil;
+  mRHash := nil;
   cleanup();
   mName := aname;
   mType := atype;
-  if (mType = TType.TList) then mRVal := TDynRecList.Create();
+  if (mType = TType.TList) then
+  begin
+    mRVal := TDynRecList.Create();
+    mRHash := hashNewStrInt();
+  end;
 end;
 
 
@@ -324,10 +424,11 @@ begin
   mSVal := '';
   mRVal.Free();
   mRVal := nil;
+  mRHash.Free();
+  mRHash := nil;
   mRecRef := nil;
   mMaxDim := -1;
   mBinOfs := -1;
-  mRecOfs := -1;
   mSepPosSize := false;
   mAsT := false;
   mHasDefault := false;
@@ -346,7 +447,8 @@ begin
   mAsMonsterId := false;
   mNegBool := false;
   mRecRefId := '';
-  if (mType = TType.TList) then mRVal := TDynRecList.Create();
+  mTagInt := 0;
+  mTagPtr := nil;
 end;
 
 
@@ -365,17 +467,13 @@ begin
   result.mSVal := mSVal;
   if (mRVal <> nil) then
   begin
-    result.mRVal := TDynRecList.Create(mRVal.count);
-    for rec in mRVal do result.mRVal.append(rec.clone());
-  end
-  else
-  begin
-    if (mType = TType.TList) then result.mRVal := TDynRecList.Create() else result.mRVal := nil;
+    if (result.mRVal = nil) then result.mRVal := TDynRecList.Create(mRVal.count);
+    if (result.mRHash = nil) then result.mRHash := hashNewStrInt();
+    for rec in mRVal do result.addListItem(rec.clone());
   end;
   result.mRecRef := mRecRef;
   result.mMaxDim := mMaxDim;
   result.mBinOfs := mBinOfs;
-  result.mRecOfs := mRecOfs;
   result.mSepPosSize := mSepPosSize;
   result.mAsT := mAsT;
   result.mDefined := mDefined;
@@ -394,6 +492,16 @@ begin
   result.mEBSTypeName := mEBSTypeName;
   result.mEBSType := mEBSType;
   result.mRecRefId := mRecRefId;
+  result.mTagInt := mTagInt;
+  result.mTagPtr := mTagPtr;
+end;
+
+
+procedure TDynField.setIVal (v: Integer); inline;
+begin
+  //FIXME: check type
+  mIVal := v;
+  mDefined := true;
 end;
 
 
@@ -511,6 +619,37 @@ begin
 end;
 
 
+function TDynField.getListCount (): Integer; inline;
+begin
+  if (mRVal <> nil) then result := mRVal.count else result := 0;
+end;
+
+
+function TDynField.getListItem (idx: Integer): TDynRecord; inline; overload;
+begin
+  if (mRVal <> nil) and (idx >= 0) and (idx < mRVal.count) then result := mRVal[idx] else result := nil;
+end;
+
+
+function TDynField.getListItem (const aname: AnsiString): TDynRecord; inline; overload;
+var
+  idx: Integer;
+begin
+  if (mRVal <> nil) and mRHash.get(aname, idx) then result := mRVal[idx] else result := nil;
+end;
+
+
+function TDynField.addListItem (rec: TDynRecord): Boolean; inline;
+begin
+  result := false;
+  if (mRVal <> nil) then
+  begin
+    mRVal.append(rec);
+    if (Length(rec.mId) > 0) then result := mRHash.put(rec.mId, mRVal.count-1);
+  end;
+end;
+
+
 class function TDynField.getTypeName (t: TType): AnsiString;
 begin
   case t of
@@ -537,7 +676,7 @@ begin
   result := mPasName+' is '+quoteStr(mName)+' type ';
   result += getTypeName(mType);
   if (mMaxDim >= 0) then result += Format('[%d]', [mMaxDim]);
-  if (mRecOfs >= 0) then result += Format(' offset %d', [mRecOfs]);
+  if (mBinOfs >= 0) then result += Format(' offset %d', [mBinOfs]);
   case mEBS of
     TEBS.TNone: begin end;
     TEBS.TRec: result += ' '+mEBSTypeName;
@@ -761,7 +900,6 @@ begin
   self.mAsMonsterId := asmonid;
   self.mMaxDim := lmaxdim;
   self.mBinOfs := fldofs;
-  self.mRecOfs := fldofs;
   self.mSepPosSize := (asxy or aswh);
   self.mAsT := ast;
   self.mOmitDef := omitdef;
@@ -769,6 +907,13 @@ begin
 end;
 
 
+function TDynField.getRecRefIndex (): Integer;
+begin
+  if (mRecRef = nil) then begin result := -1; exit; end;
+  result := mOwner.findRecordNumByType(mEBSTypeName, mRecRef);
+end;
+
+
 procedure TDynField.writeBinTo (st: TStream);
 var
   s: AnsiString;
@@ -1069,6 +1214,7 @@ begin
   raise Exception.Create(Format('cannot parse field ''%s'' yet', [mName]));
 end;
 
+
 procedure TDynField.parseBinValue (st: TStream);
 var
   rec, rc: TDynRecord;
@@ -1338,7 +1484,11 @@ begin
           rc.parseValue(pr);
           mRecRef := rc;
           mDefined := true;
-          mOwner.addRecordByType(mEBSTypeName, rc);
+          if mOwner.addRecordByType(mEBSTypeName, rc) then
+          begin
+            //raise Exception.Create(Format('record type ''%s'' for field ''%s'' not found', [mEBSTypeName, mName]));
+            e_LogWritefln('duplicate record with id ''%s'' for field ''%s'' in record ''%s''', [rc.mId, mName, mOwner.mName]);
+          end;
           pr.eatTT(pr.TTSemi); // hack: allow (but don't require) semicolon after inline records
           exit;
         end;
@@ -1500,10 +1650,15 @@ begin
   mName := '';
   mSize := 0;
   mFields := TDynFieldList.Create();
+  {$IF DEFINED(XDYNREC_USE_FIELDHASH)}
+  mFieldsHash := hashNewStrInt();
+  {$ENDIF}
   mTrigTypes := nil;
   mHeader := false;
   mHeaderRec := nil;
   mBinBlock := -1;
+  mTagInt := 0;
+  mTagPtr := nil;
   parseDef(pr);
 end;
 
@@ -1513,9 +1668,14 @@ begin
   mName := '';
   mSize := 0;
   mFields := TDynFieldList.Create();
+  {$IF DEFINED(XDYNREC_USE_FIELDHASH)}
+  mFieldsHash := hashNewStrInt();
+  {$ENDIF}
   mTrigTypes := nil;
   mHeader := false;
   mHeaderRec := nil;
+  mTagInt := 0;
+  mTagPtr := nil;
 end;
 
 
@@ -1524,14 +1684,47 @@ begin
   mName := '';
   mFields.Free();
   mFields := nil;
+  {$IF DEFINED(XDYNREC_USE_FIELDHASH)}
+  mFieldsHash.Free();
+  mFieldsHash := nil;
+  {$ENDIF}
   mTrigTypes := nil;
   mHeaderRec := nil;
+  mTagInt := 0;
+  mTagPtr := nil;
   inherited;
 end;
 
 
+procedure TDynRecord.addField (fld: TDynField); inline;
+begin
+  if (fld = nil) then raise Exception.Create('cannot append nil field to record');
+  mFields.append(fld);
+  {$IF DEFINED(XDYNREC_USE_FIELDHASH)}
+  if (Length(fld.mName) > 0) then mFieldsHash.put(fld.mName, mFields.count-1);
+  {$ENDIF}
+end;
+
+
+function TDynRecord.addFieldChecked (fld: TDynField): Boolean; inline; // `true`: duplicate name
+begin
+  result := false;
+  if (fld = nil) then raise Exception.Create('cannot append nil field to record');
+  {$IF not DEFINED(XDYNREC_USE_FIELDHASH)}
+  if (Length(fld.mName) > 0) then result := hasByName(fld.mName);
+  {$ENDIF}
+  mFields.append(fld);
+  {$IF DEFINED(XDYNREC_USE_FIELDHASH)}
+  if (Length(fld.mName) > 0) then result := mFieldsHash.put(fld.mName, mFields.count-1);
+  {$ENDIF}
+end;
+
+
 function TDynRecord.findByName (const aname: AnsiString): Integer; inline;
 begin
+  {$IF DEFINED(XDYNREC_USE_FIELDHASH)}
+  if not mFieldsHash.get(aname, result) then result := -1;
+  {$ELSE}
   result := 0;
   while (result < mFields.count) do
   begin
@@ -1539,6 +1732,7 @@ begin
     Inc(result);
   end;
   result := -1;
+  {$ENDIF}
 end;
 
 
@@ -1557,6 +1751,18 @@ begin
 end;
 
 
+function TDynRecord.getFieldAt (idx: Integer): TDynField; inline;
+begin
+  if (idx >= 0) and (idx < mFields.count) then result := mFields[idx] else result := nil;
+end;
+
+
+function TDynRecord.getCount (): Integer; inline;
+begin
+  result := mFields.count;
+end;
+
+
 function TDynRecord.getIsTrigData (): Boolean; inline;
 begin
   result := (Length(mTrigTypes) > 0);
@@ -1573,6 +1779,18 @@ begin
 end;
 
 
+function TDynRecord.getForTrigCount (): Integer; inline;
+begin
+  result := Length(mTrigTypes);
+end;
+
+
+function TDynRecord.getForTrigAt (idx: Integer): AnsiString; inline;
+begin
+  if (idx >= 0) and (idx < Length(mTrigTypes)) then result := mTrigTypes[idx] else result := '';
+end;
+
+
 function TDynRecord.clone (): TDynRecord;
 var
   fld: TDynField;
@@ -1587,20 +1805,22 @@ begin
   if (mFields.count > 0) then
   begin
     result.mFields.capacity := mFields.count;
-    for fld in mFields do result.mFields.append(fld.clone(result));
+    for fld in mFields do result.addField(fld.clone(result));
   end;
   SetLength(result.mTrigTypes, Length(mTrigTypes));
   for f := 0 to High(mTrigTypes) do result.mTrigTypes[f] := mTrigTypes[f];
   result.mHeader := mHeader;
   result.mBinBlock := mBinBlock;
   result.mHeaderRec := mHeaderRec;
+  result.mTagInt := mTagInt;
+  result.mTagPtr := mTagPtr;
 end;
 
 
 function TDynRecord.findRecordByTypeId (const atypename, aid: AnsiString): TDynRecord;
 var
   fld: TDynField;
-  rec: TDynRecord;
+  idx: Integer;
 begin
   result := nil;
   if (Length(aid) = 0) then exit;
@@ -1611,10 +1831,7 @@ begin
   // find by id
   if (fld.mRVal <> nil) then
   begin
-    for rec in fld.mRVal do
-    begin
-      if StrEqu(rec.mId, aid) then begin result := rec; exit; end;
-    end;
+    if fld.mRHash.get(aid, idx) then begin result := fld.mRVal[idx]; exit; end;
   end;
   // alas
 end;
@@ -1623,7 +1840,7 @@ end;
 function TDynRecord.findRecordNumByType (const atypename: AnsiString; rc: TDynRecord): Integer;
 var
   fld: TDynField;
-  f: Integer;
+  idx: Integer;
 begin
   result := -1;
   // find record data
@@ -1633,16 +1850,16 @@ begin
   // find by ref
   if (fld.mRVal <> nil) then
   begin
-    for f := 0 to fld.mRVal.count-1 do
+    for idx := 0 to fld.mRVal.count-1 do
     begin
-      if (fld.mRVal[f] = rc) then begin result := f; exit; end;
+      if (fld.mRVal[idx] = rc) then begin result := idx; exit; end;
     end;
   end;
   // alas
 end;
 
 
-procedure TDynRecord.addRecordByType (const atypename: AnsiString; rc: TDynRecord);
+function TDynRecord.addRecordByType (const atypename: AnsiString; rc: TDynRecord): Boolean;
 var
   fld: TDynField;
 begin
@@ -1653,12 +1870,16 @@ begin
     // first record
     fld := TDynField.Create(atypename, TDynField.TType.TList);
     fld.mOwner := mHeaderRec;
-    mHeaderRec.mFields.append(fld);
+    mHeaderRec.addField(fld);
   end;
   if (fld.mType <> fld.TType.TList) then raise Exception.Create(Format('cannot append record of type ''%s'' due to name conflict with ordinary field', [atypename]));
   // append
-  if (fld.mRVal = nil) then fld.mRVal := TDynRecList.Create();
-  fld.mRVal.append(rc);
+  if (fld.mRVal = nil) then
+  begin
+    fld.mRVal := TDynRecList.Create();
+    fld.mRHash := hashNewStrInt();
+  end;
+  result := fld.addListItem(rc);
 end;
 
 
@@ -1765,10 +1986,14 @@ begin
   while (pr.tokType <> pr.TTEnd) do
   begin
     fld := TDynField.Create(pr);
-    if hasByName(fld.name) then begin fld.Free(); raise Exception.Create(Format('duplicate field ''%s''', [fld.name])); end;
+    //if hasByName(fld.name) then begin fld.Free(); raise Exception.Create(Format('duplicate field ''%s''', [fld.name])); end;
     // append
     fld.mOwner := self;
-    mFields.append(fld);
+    if addFieldChecked(fld) then
+    begin
+      fld.Free();
+      raise Exception.Create(Format('duplicate field ''%s''', [fld.name]));
+    end;
     // done with field
   end;
   pr.expectTT(pr.TTEnd);
@@ -1926,7 +2151,7 @@ begin
           // create list for this type
           fld := TDynField.Create(rec.mName, TDynField.TType.TList);
           fld.mOwner := self;
-          mFields.append(fld);
+          addField(fld);
           if (bsize > 0) then
           begin
             GetMem(buf, bsize);
@@ -1938,7 +2163,7 @@ begin
               rec.mHeaderRec := self;
               rec.parseBinValue(mst);
               rec.mId := Format('%s%d', [rec.mName, f]);
-              fld.mRVal.append(rec);
+              fld.addListItem(rec);
               //writeln('parsed ''', rec.mId, '''...');
             end;
           end;
@@ -2169,8 +2394,6 @@ var
   {$IF DEFINED(D2D_DYNREC_PROFILER)}
   stt, stall: UInt64;
   {$ENDIF}
-  ids: THashStrInt = nil;
-  idtmp: AnsiString = '';
 begin
   if (mOwner = nil) then raise Exception.Create(Format('can''t parse record ''%s'' value without owner', [mName]));
 
@@ -2185,98 +2408,85 @@ begin
   else
   begin
     assert(mHeaderRec = self);
-    ids := hashNewStrInt();
   end;
 
-  try
-    //writeln('parsing record <', mName, '>');
-    if not beginEaten then pr.expectTT(pr.TTBegin);
-    while (pr.tokType <> pr.TTEnd) do
-    begin
-      if (pr.tokType <> pr.TTId) then raise Exception.Create('identifier expected');
-      //writeln('<', mName, '.', pr.tokStr, '>');
+  //writeln('parsing record <', mName, '>');
+  if not beginEaten then pr.expectTT(pr.TTBegin);
+  while (pr.tokType <> pr.TTEnd) do
+  begin
+    if (pr.tokType <> pr.TTId) then raise Exception.Create('identifier expected');
+    //writeln('<', mName, '.', pr.tokStr, '>');
 
-      // records
-      if mHeader then
+    // records
+    if mHeader then
+    begin
+      // add records with this type (if any)
+      {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
+      trc := mOwner.findRecType(pr.tokStr);
+      {$IF DEFINED(D2D_DYNREC_PROFILER)}profFindRecType := curTimeMicro()-stt;{$ENDIF}
+      if (trc <> nil) then
       begin
-        // add records with this type (if any)
         {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
-        trc := mOwner.findRecType(pr.tokStr);
-        {$IF DEFINED(D2D_DYNREC_PROFILER)}profFindRecType := curTimeMicro()-stt;{$ENDIF}
-        if (trc <> nil) then
-        begin
-          {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
-          rec := trc.clone();
-          {$IF DEFINED(D2D_DYNREC_PROFILER)}profCloneRec := curTimeMicro()-stt;{$ENDIF}
-          rec.mHeaderRec := mHeaderRec;
-          try
-            pr.skipToken();
-            rec.parseValue(pr);
-            if (Length(rec.mId) > 0) then
+        rec := trc.clone();
+        {$IF DEFINED(D2D_DYNREC_PROFILER)}profCloneRec := curTimeMicro()-stt;{$ENDIF}
+        rec.mHeaderRec := mHeaderRec;
+        try
+          pr.skipToken();
+          rec.parseValue(pr);
+          (*
+          if (Length(rec.mId) > 0) then
+          begin
+            {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
+            fld := field[pr.tokStr];
+            {$IF DEFINED(D2D_DYNREC_PROFILER)}profFieldSearching := curTimeMicro()-stt;{$ENDIF}
+            (*
+            if (fld <> nil) and (fld.mRVal <> nil) then
             begin
               {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
-              fld := field[pr.tokStr];
-              {$IF DEFINED(D2D_DYNREC_PROFILER)}profFieldSearching := curTimeMicro()-stt;{$ENDIF}
-              {$iF FALSE}
-                if (fld <> nil) and (fld.mRVal <> nil) then
-                begin
-                  {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
-                  for rv in fld.mRVal do
-                  begin
-                    if (Length(rv.mId) > 0) and StrEqu(rv.mId, rec.mId) then raise Exception.Create(Format('duplicate thing ''%s'' in record ''%s''', [fld.mName, mName]));
-                  end;
-                  {$IF DEFINED(D2D_DYNREC_PROFILER)}profListDupChecking := curTimeMicro()-stt;{$ENDIF}
-                end;
-              {$ELSE}
-                if (fld <> nil) and (fld.mRVal <> nil) then
-                begin
-                  {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
-                  idtmp := trc.mName+':'+rec.mId;
-                  if ids.put(idtmp, 1) then raise Exception.Create(Format('duplicate thing ''%s'' in record ''%s''', [fld.mName, mName]));
-                  {$IF DEFINED(D2D_DYNREC_PROFILER)}profListDupChecking := curTimeMicro()-stt;{$ENDIF}
-                end;
-              {$ENDIF}
+              //idtmp := trc.mName+':'+rec.mId;
+              //if ids.put(idtmp, 1) then raise Exception.Create(Format('duplicate thing ''%s'' in record ''%s''', [fld.mName, mName]));
+              if fld.mRHash.has(rec.mId) then raise Exception.Create(Format('duplicate thing ''%s'' in record ''%s''', [fld.mName, mName]));
+              {$IF DEFINED(D2D_DYNREC_PROFILER)}profListDupChecking := curTimeMicro()-stt;{$ENDIF}
             end;
-            {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
-            addRecordByType(rec.mName, rec);
-            {$IF DEFINED(D2D_DYNREC_PROFILER)}profAddRecByType := curTimeMicro()-stt;{$ENDIF}
-            rec := nil;
-          finally
-            rec.Free();
           end;
-          continue;
+          *)
+          {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
+          addRecordByType(rec.mName, rec);
+          {$IF DEFINED(D2D_DYNREC_PROFILER)}profAddRecByType := curTimeMicro()-stt;{$ENDIF}
+          rec := nil;
+        finally
+          rec.Free();
         end;
-      end;
-
-      // fields
-      {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
-      fld := field[pr.tokStr];
-      {$IF DEFINED(D2D_DYNREC_PROFILER)}profFieldSearching := curTimeMicro()-stt;{$ENDIF}
-      if (fld <> nil) then
-      begin
-        if fld.defined then raise Exception.Create(Format('duplicate field ''%s'' in record ''%s''', [fld.mName, mName]));
-        if fld.internal then raise Exception.Create(Format('internal field ''%s'' in record ''%s''', [fld.mName, mName]));
-        pr.skipToken();
-        {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
-        fld.parseValue(pr);
-        {$IF DEFINED(D2D_DYNREC_PROFILER)}profFieldValParsing := curTimeMicro()-stt;{$ENDIF}
         continue;
       end;
-
-      // something is wrong
-      raise Exception.Create(Format('unknown field ''%s'' in record ''%s''', [pr.tokStr, mName]));
     end;
-    pr.expectTT(pr.TTEnd);
-    // fix field defaults
+
+    // fields
     {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
-    for fld in mFields do fld.fixDefaultValue();
-    {$IF DEFINED(D2D_DYNREC_PROFILER)}profFixDefaults := curTimeMicro()-stt;{$ENDIF}
-    //writeln('done parsing record <', mName, '>');
-    //{$IF DEFINED(D2D_DYNREC_PROFILER)}writeln('stall: ', curTimeMicro()-stall);{$ENDIF}
-    {$IF DEFINED(D2D_DYNREC_PROFILER)}profRecValParse := curTimeMicro()-stall;{$ENDIF}
-  finally
-    ids.Free();
+    fld := field[pr.tokStr];
+    {$IF DEFINED(D2D_DYNREC_PROFILER)}profFieldSearching := curTimeMicro()-stt;{$ENDIF}
+    if (fld <> nil) then
+    begin
+      if fld.defined then raise Exception.Create(Format('duplicate field ''%s'' in record ''%s''', [fld.mName, mName]));
+      if fld.internal then raise Exception.Create(Format('internal field ''%s'' in record ''%s''', [fld.mName, mName]));
+      pr.skipToken();
+      {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
+      fld.parseValue(pr);
+      {$IF DEFINED(D2D_DYNREC_PROFILER)}profFieldValParsing := curTimeMicro()-stt;{$ENDIF}
+      continue;
+    end;
+
+    // something is wrong
+    raise Exception.Create(Format('unknown field ''%s'' in record ''%s''', [pr.tokStr, mName]));
   end;
+  pr.expectTT(pr.TTEnd);
+  // fix field defaults
+  {$IF DEFINED(D2D_DYNREC_PROFILER)}stt := curTimeMicro();{$ENDIF}
+  for fld in mFields do fld.fixDefaultValue();
+  {$IF DEFINED(D2D_DYNREC_PROFILER)}profFixDefaults := curTimeMicro()-stt;{$ENDIF}
+  //writeln('done parsing record <', mName, '>');
+  //{$IF DEFINED(D2D_DYNREC_PROFILER)}writeln('stall: ', curTimeMicro()-stall);{$ENDIF}
+  {$IF DEFINED(D2D_DYNREC_PROFILER)}profRecValParse := curTimeMicro()-stall;{$ENDIF}
 end;
 
 
@@ -2730,4 +2940,19 @@ begin
 end;
 
 
+function TDynMapDef.pasdefconst (): AnsiString;
+var
+  ebs: TDynEBS;
+begin
+  result := '';
+  result += '// ////////////////////////////////////////////////////////////////////////// //'#10;
+  result += '// enums and bitsets'#10;
+  for ebs in ebsTypes do result += #10+ebs.pasdef();
+end;
+
+
+function TDynMapDef.getTrigTypeCount (): Integer; inline; begin result := trigTypes.count; end;
+function TDynMapDef.getTrigTypeAt (idx: Integer): TDynRecord; inline; begin if (idx >= 0) and (idx < trigTypes.count) then result := trigTypes[idx] else result := nil; end;
+
+
 end.