Results 1 to 1 of 1

Thread: TReflectionGroundItemArray.SortStacks() (+ compareTile / T2DReflectionGroundItemArra)

  1. #1
    Join Date
    Feb 2007
    Location
    Access Violation at 0x00000000
    Posts
    2,865
    Mentioned
    3 Post(s)
    Quoted
    18 Post(s)

    Default TReflectionGroundItemArray.SortStacks() (+ compareTile / T2DReflectionGroundItemArra)

    I had a need for this in one of my scripts. It will take an array of ground items (such as the one attained with TReflectGroundItemArray.GetAll()) and sort it in a 2D array, by tile.
    So, the result of this function is an array with each item containing all ground items that share the same position in-game.

    Simba Code:
    type
      T2DReflectGroundItemArray = Array of TReflectGroundItemArray

    function compareTile(t1, t2: TTile): Boolean;
    begin
      Result := (t1.X = t2.X) and (t1.Y = t2.Y);
    end;

    function TReflectGroundItemArray.SortStacks(): T2DReflectGroundItemArray;
    var
      tmp: T2DReflectGroundItemArray;
      i, j, k, l: Integer;
      inserted: Boolean;
    begin
      if Length(Self) = 0 then
        Exit;

      SetLength(tmp, 1);
      SetLength(tmp[0], 1);
      tmp[0][0] := Self[0];

      if Length(Self) > 1 then
      begin
        j := High(Self);
        for i := 1 to j do
        begin
          inserted := False;

          l := High(tmp);
          for k := 0 to l do
          begin
            if compareTile(Self[i].Tile, tmp[k][0].Tile) then
            begin
              SetLength(tmp[k], Length(tmp[k]) + 1);
              tmp[k][High(tmp[k])] := Self[i];
              inserted := True;
              Break;
            end;
          end;

          if not inserted then
          begin
            SetLength(tmp, Length(tmp) + 1);
            SetLength(tmp[High(tmp)], 1);
            tmp[High(tmp)][0] := Self[i];
          end;
        end;
      end;

      Result := tmp;
    end;

    Let me know if you see any pesky access violations/obvious mistakes/performance improvements here
    Last edited by Floor66; 02-16-2015 at 08:05 PM.
    Ce ne sont que des gueux


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •