Advertisement
Guest User

Untitled

a guest
May 20th, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.22 KB | None | 0 0
  1. {
  2.     Check that Edge Linked triangles in navmeshes are pointing to each other
  3. }
  4. unit NavMeshCheckExternal;
  5.  
  6. var
  7.   arEdge: array [0..2] of string;
  8.  
  9. //===========================================================================
  10. function Initialize: integer;
  11. begin
  12.   if wbSimpleRecords then begin
  13.     MessageDlg('Simple records must be unchecked in xEdit options', mtInformation, [mbOk], 0);
  14.     Result := 1;
  15.     Exit;
  16.   end;
  17.   arEdge[0] := '0-1';
  18.   arEdge[1] := '1-2';
  19.   arEdge[2] := '2-0';
  20. end;
  21.  
  22. //===========================================================================
  23. // check that tri1 in navmesh navm1 points to tri2 in navmesh navm2
  24. function CheckConnection(navm1, navm2: IInterface; tri1, tri2: integer): Boolean;
  25. var
  26.   tris, tri, EdgeLinks, EdgeLink: IInterface;
  27.   TriLimit, j, flags, EdgeLinkLimit, EdgeLinkIndex: integer;
  28. begin
  29.   if Signature(navm1) <> 'NAVM' then begin
  30.     AddMessage('Not NAVM!');
  31.     AddMessage(Format('Referenced triangle %d in %s', [tri1, Name(navm1)]));
  32.     Exit;
  33.   end;
  34.   tris := ElementByPath(navm1, 'NVNM\Triangles');
  35.   TriLimit := Pred(ElementCount(tris));
  36.   EdgeLinks := ElementByPath(navm1, 'NVNM\Edge Links');
  37.   EdgeLinkLimit := Pred(ElementCount(EdgeLinks));
  38.   if tri1 > TriLimit then begin
  39.     AddMessage(Format('Referenced triangle (%d > %d) in %s', [tri1, TriLimit, Name(navm1)]));
  40.     Exit;
  41.   end;
  42.   tri := ElementByIndex(tris, tri1);
  43.   flags := GetElementNativeValues(tri, 'Flags');
  44.   if flags and 7 = 0 then begin
  45.     AddMessage('No Edge Links');
  46.     AddMessage(Format('Referenced triangle %d in %s', [tri1, Name(navm1)]));
  47.     Exit;
  48.   end;
  49.  
  50.   for j := 0 to 2 do
  51.     if flags and (1 shl j) > 0 then begin
  52.       EdgeLinkIndex := GetElementNativeValues(tri, 'Edge ' + arEdge[j]);
  53.       if EdgeLinkIndex > EdgeLinkLimit then begin
  54.         AddMessage(Format('Bad Edge %s link (%d > %d)!', [arEdge[j], EdgeLinkIndex, EdgeLinkLimit]));
  55.         AddMessage(Format('Referenced triangle %d in %s', [tri1, Name(navm1)]));
  56.       end
  57.       else begin
  58.         EdgeLink := ElementByIndex(EdgeLinks, EdgeLinkIndex);
  59.         if (FormID(LinksTo(ElementByName(EdgeLink, 'Mesh'))) = FormID(navm2)) and (GetElementNativeValues(EdgeLink, 'Triangle') = tri2) then begin
  60.           Result := True;
  61.           Exit;
  62.         end;
  63.       end;
  64.     end;
  65.  
  66.   AddMessage('No Edge Link match!');
  67.   AddMessage(Format('Referenced triangle %d in %s', [tri1, Name(navm1)]));
  68. end;
  69.  
  70.  
  71. //===========================================================================
  72. function Process(e: IInterface): integer;
  73. var
  74.   tris, tri, EdgeLinks, EdgeLink: IInterface;
  75.   i, j, flags, EdgeLinkLimit, EdgeLinkIndex, EdgeLinkFound, EdgeLinkCount, errors: integer;
  76. begin
  77.   if Signature(e) <> 'NAVM' then
  78.     Exit;
  79.  
  80.   e := WinningOverride(e);
  81.   tris := ElementByPath(e, 'NVNM\Triangles');
  82.   EdgeLinks := ElementByPath(e, 'NVNM\Edge Links');
  83.   EdgeLinkCount := ElementCount(EdgeLinks);
  84.   EdgeLinkLimit := Pred(ElementCount(EdgeLinks));
  85.   EdgeLinkFound := 0;
  86.  
  87.   // iterate through triangles
  88.   for i := 0 to Pred(ElementCount(tris)) do begin
  89.     errors := 0;
  90.     tri := ElementByIndex(tris, i);
  91.     flags := GetElementNativeValues(tri, 'Flags');
  92.     for j := 0 to 2 do
  93.       // triangle has EdgeLinks?
  94.       if flags and (1 shl j) > 0 then begin
  95.         EdgeLinkIndex := GetElementNativeValues(tri, 'Edge ' + arEdge[j]);
  96.         if EdgeLinkIndex > EdgeLinkLimit then
  97.           AddMessage(Format('Bad Edge %s link (%d > %d)!', [arEdge[j], EdgeLinkIndex, EdgeLinkLimit]))
  98.         else begin
  99.           Inc(EdgeLinkFound);
  100.           EdgeLink := ElementByIndex(EdgeLinks, EdgeLinkIndex);
  101.           if not CheckConnection(WinningOverride(LinksTo(ElementByName(EdgeLink, 'Mesh'))), e, GetElementNativeValues(EdgeLink, 'Triangle'), i) then begin
  102.             AddMessage(Format('via Edge %s link %d', [arEdge[j], EdgeLinkIndex]));
  103.             Inc(errors);
  104.           end;
  105.         end;
  106.       end;
  107.  
  108.     if errors > 0 then
  109.       AddMessage(Format('^^^ Error triangle %d in %s', [i, Name(e)]));
  110.   end;
  111.    
  112.   if (EdgeLinkFound > 0) and (EdgeLinkFound > EdgeLinkCount) then begin
  113.     AddMessage(Format('Too many Edges Linked (%d > %d) in %s', [EdgeLinkFound, EdgeLinkCount, Name(e)]));
  114.     //Result := 1;
  115.     //Exit;
  116.   end;
  117. end;
  118.  
  119. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement