Results 1 to 9 of 9

Thread: Multi-dimensional arrays...

  1. #1
    Join Date
    Feb 2012
    Posts
    170
    Mentioned
    0 Post(s)
    Quoted
    6 Post(s)

    Default Multi-dimensional arrays...

    I'm trying to make an array of boxes so I can build myself a grid of 45x45 pixel boxes starting at a certain point. Now I'm having issues with the array. I tried using pascal tutorials for multi dimensional arrays, but they need to bring in a library and whatnot and it didn't go over very well with simba. When I decided to peek at the tutorials here all I found was to make an array of an array, but I don't know how to load info into an array of an array, or how I would call a certain box. (I'm going to need to be making alot of checks between boxes.).

    If anyone could help me get this grid working that would be awesome

  2. #2
    Join Date
    Mar 2012
    Location
    Grambling, LA
    Posts
    70
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by xdarkshadowx View Post
    I'm trying to make an array of boxes so I can build myself a grid of 45x45 pixel boxes starting at a certain point. Now I'm having issues with the array. I tried using pascal tutorials for multi dimensional arrays, but they need to bring in a library and whatnot and it didn't go over very well with simba. When I decided to peek at the tutorials here all I found was to make an array of an array, but I don't know how to load info into an array of an array, or how I would call a certain box. (I'm going to need to be making alot of checks between boxes.).

    If anyone could help me get this grid working that would be awesome
    Sounds interesting. If I understand you right, you might use simple arrays such as:
    Simba Code:
    var
        boxArray : Array[0..45] of TBox;
        myBox : TBox;
    And to call each box you would just say like
    Simba Code:
    myBox := boxArray[i];

  3. #3
    Join Date
    Feb 2006
    Location
    Helsinki, Finland
    Posts
    1,395
    Mentioned
    30 Post(s)
    Quoted
    107 Post(s)

    Default

    You could always use grid functions that I wrote for MSSL:

    Simba Code:
    type
      TCell = record
        row, column: Integer;
      end;
      TBoxGrid = record
        startPoint: TPoint;
        size, rows, columns, boxWidth, boxHeight, rowSpace, columnSpace: Integer;
        grid: array of record
          cell: TCell;
          bx: TBox;
        end;
      end;
      TPointGrid = record
        startPoint: TPoint;
        size, rows, columns, rowSpace, columnSpace: Integer;
        grid: array of record
          cell: TCell;
          pt: TPoint;
        end;
      end;

    const
      COMPASS_DIRECTION_NORTH_EAST = 0;
      COMPASS_DIRECTION_SOUTH_EAST = 1;
      COMPASS_DIRECTION_SOUTH_WEST = 2;
      COMPASS_DIRECTION_NORTH_WEST = 3;
      COMPASS_DIRECTION_WEST = 4;
      COMPASS_DIRECTION_NORTH = 5;
      COMPASS_DIRECTION_EAST = 6;
      COMPASS_DIRECTION_SOUTH = 7;

    function Box(X1, Y1, X2, Y2: Integer): TBox;
    begin
      Result.X1 := X1;
      Result.Y1 := Y1;
      Result.X2 := X2;
      Result.Y2 := Y2;
    end;

    function grid_Build_BoxGrid(var bg: TBoxGrid; startX, startY, boxWidth, boxHeight, rows, columns, rowSpace, columnSpace: Integer): Boolean;
    var
      id, r, c: Integer;
    begin
      if (startX < 0) or (startY < 0) or (boxWidth < 1) or (boxHeight < 1) or (rows < 1) or (columns < 1) or (rowSpace < 0) or (columnSpace < 0) then
        Exit;
      bg.size := (rows * columns);
      bg.rows := rows;
      bg.columns := columns;
      bg.startPoint := Point(startX, startY);
      bg.boxWidth := boxWidth;
      bg.boxHeight := boxHeight;
      bg.rowSpace := rowSpace;
      bg.columnSpace := columnSpace;
      SetLength(bg.grid, bg.size);
      for r := 0 to (rows - 1) do
        for c := 0 to (columns - 1) do
        begin
          id := (r * columns) + c;
          bg.grid[id].cell.row := r;
          bg.grid[id].cell.column := c;
          bg.grid[id].bx := Box((startX + ((id mod columns) * (boxWidth + (columnSpace + 1)))),
                                (startY + ((id div columns) * (boxHeight + (rowSpace + 1)))),
                                ((startX + ((id mod columns) * (boxWidth + (columnSpace + 1)))) + boxWidth),
                                ((startY + ((id div columns) * (boxHeight + (rowSpace + 1)))) + boxHeight));
        end;
      Result := True;
    end;

    function grid_Add_BoxGrid_Row(var bg: TBoxGrid): Boolean;
    var
      id, r, c: Integer;
    begin
      if (bg.startPoint.X < 0) or (bg.startPoint.Y < 0) or (bg.boxWidth < 1) or (bg.boxHeight < 1) or (bg.rowSpace < 0) or (bg.columnSpace < 0) then
        Exit;
      SetLength(bg, 0);
      Inc(bg.rows);
      bg.size := (bg.rows * bg.columns);
      SetLength(bg.grid, bg.size);
      for r := 0 to (bg.rows - 1) do
        for c := 0 to (bg.columns - 1) do
        begin
          id := (r * bg.columns) + c;
          bg.grid[id].cell.row := r;
          bg.grid[id].cell.column := c;
          bg.grid[id].bx := Box((bg.startPoint.X + ((id mod bg.columns) * (bg.boxWidth + (bg.columnSpace + 1)))),
                                (bg.startPoint.Y + ((id div bg.columns) * (bg.boxHeight + (bg.rowSpace + 1)))),
                                ((bg.startPoint.X + ((id mod bg.columns) * (bg.boxWidth + (bg.columnSpace + 1)))) + bg.boxWidth),
                                ((bg.startPoint.Y + ((id div bg.columns) * (bg.boxHeight + (bg.rowSpace + 1)))) + bg.boxHeight));
        end;
      Result := True;
    end;

    function grid_Add_BoxGrid_Column(var bg: TBoxGrid): Boolean;
    var
      id, r, c: Integer;
    begin
      if (bg.startPoint.X < 0) or (bg.startPoint.Y < 0) or (bg.boxWidth < 1) or (bg.boxHeight < 1) or (bg.rowSpace < 0) or (bg.columnSpace < 0) then
        Exit;
      SetLength(bg, 0);
      Inc(bg.columns);
      bg.size := (bg.rows * bg.columns);
      SetLength(bg.grid, bg.size);
      for r := 0 to (bg.rows - 1) do
        for c := 0 to (bg.columns - 1) do
        begin
          id := (r * bg.columns) + c;
          bg.grid[id].cell.row := r;
          bg.grid[id].cell.column := c;
          bg.grid[id].bx := Box((bg.startPoint.X + ((id mod bg.columns) * (bg.boxWidth + (bg.columnSpace + 1)))),
                                (bg.startPoint.Y + ((id div bg.columns) * (bg.boxHeight + (bg.rowSpace + 1)))),
                                ((bg.startPoint.X + ((id mod bg.columns) * (bg.boxWidth + (bg.columnSpace + 1)))) + bg.boxWidth),
                                ((bg.startPoint.Y + ((id div bg.columns) * (bg.boxHeight + (bg.rowSpace + 1)))) + bg.boxHeight));
        end;
      Result := True;
    end;

    function grid_Jump_BoxGrid_CellByDistance(bg: TBoxGrid; cell: TCell; rowMoveDist, columnMoveDist: Integer): Boolean;
    begin
      Result := InRange(0, bg.rows, (cell.row + rowMoveDist)) and InRange(0, bg.columns, (cell.column + columnMoveDist));
      if not Result then
        Exit;
      cell.row := (cell.row + rowMoveDist);
      cell.column := (cell.column + columnMoveDist);
    end;

    function grid_Get_BoxGrid_Boxes(bg: TBoxGrid): TBoxArray;
    var
      i: Integer;
    begin
      if bg.size < 1 then
        Exit;
      SetLength(Result, bg.size);
      for i := 0 to (bg.size - 1) do
        Result[i] := bg.grid[i].bx;
    end;

    function grid_Convert_BoxGrid_CellToID(bg: TBoxGrid; cell: TCell): Integer;
    begin
      Result := -1;
      if InRange(cell.row, 0, (bg.rows - 1)) and InRange(cell.column, 0, (bg.columns - 1)) then
        Result := ((cell.row * bg.columns) + cell.column);
    end;

    function grid_Convert_BoxGrid_IDToCell(bg: TBoxGrid; ID: Integer): TCell;
    begin
      if InRange(ID, 0, (bg.size - 1)) then
        for Result.row := 0 to (bg.rows - 1) do
          for Result.column := 0 to (bg.columns - 1) do
            if ID = ((Result.row * bg.columns) + Result.column) then
              Exit;
      Result.row := -1;
      Result.column := -1;
    end;

    function grid_Convert_BoxGrid_PointToID(bg: TBoxGrid; p: TPoint): Integer;
    var
      i: Integer;
    begin
      Result := -1;
      if bg.size > 0 then
        for i := 0 to (bg.size - 1) do
          if PointInBox(p, bg.grid[i].bx) then
          begin
            Result := i;
            Exit;
          end;
    end;

    function grid_Convert_BoxGrid_IDToBox(bg: TBoxGrid; ID: Integer): TBox;
    begin
      Result := Box(-1, -1, -1, -1);
      if InRange(ID, 0, (bg.size - 1)) and (bg.size > 0) then
        Result := bg.grid[ID].bx;
    end;

    function grid_Jump_BoxGrid_CellByCompassDirection(bg: TBoxGrid; var cell: TCell; direction, distance: Integer): Boolean;
    begin
      if (distance < 1) or (distance > bg.columns) or (distance > bg.rows) or not InRange(direction, 0, 7) then
        Exit;
      case direction of
        COMPASS_DIRECTION_NORTH_EAST: begin
                                        if (0 > (cell.row - distance)) or ((bg.columns - 1) < (cell.column + distance)) then
                                          Exit;
                                        cell.row := (cell.row - distance);
                                        cell.column := (cell.column + distance);
                                      end;
        COMPASS_DIRECTION_SOUTH_EAST: begin
                                        if ((bg.rows - 1) < (cell.row + distance)) or ((bg.columns - 1) < (cell.column + distance)) then
                                          Exit;
                                        cell.row := (cell.row + distance);
                                        cell.column := (cell.column + distance);
                                      end;
        COMPASS_DIRECTION_SOUTH_WEST: begin
                                        if ((bg.rows - 1) < (cell.row + distance)) or (0 > (cell.column - distance)) then
                                          Exit;
                                        cell.row := (cell.row + distance);
                                        cell.column := (cell.column - distance);
                                      end;
        COMPASS_DIRECTION_NORTH_WEST: begin
                                        if (0 > (cell.row - distance)) or (0 > (cell.column - distance)) then
                                          Exit;
                                        cell.row := (cell.row - distance);
                                        cell.column := (cell.column - distance);
                                      end;
        COMPASS_DIRECTION_WEST: begin
                                  if (0 > (cell.column - distance)) then
                                    Exit;
                                  cell.column := (cell.column - distance);
                                end;
        COMPASS_DIRECTION_NORTH: begin
                                   if (0 > (cell.row - distance)) then
                                     Exit;
                                   cell.row := (cell.row - distance);
                                 end;
        COMPASS_DIRECTION_EAST: begin
                                  if ((bg.columns - 1) < (cell.column + distance)) then
                                    Exit;
                                  cell.column := (cell.column + distance);
                                end;
        COMPASS_DIRECTION_SOUTH: begin
                                   if ((bg.rows - 1) < (cell.row + distance)) then
                                     Exit;
                                   cell.row := (cell.row + distance);
                                 end;
      end;
      Result := True;
    end;

    function grid_Build_PointGrid(var pg: TPointGrid; startX, startY, rows, columns, rowSpace, columnSpace: Integer): Boolean;
    var
      id, r, c: Integer;
    begin
      if (startX < 0) or (startY < 0) or (rows < 1) or (columns < 1) or (rowSpace < 0) or (columnSpace < 0) then
        Exit;
      pg.size := (rows * columns);
      pg.rows := rows;
      pg.columns := columns;
      pg.startPoint := Point(startX, startY);
      pg.rowSpace := rowSpace;
      pg.columnSpace := columnSpace;
      SetLength(pg.grid, pg.size);
      for r := 0 to (rows - 1) do
        for c := 0 to (columns - 1) do
        begin
          id := (r * columns) + c;
          pg.grid[id].cell.row := r;
          pg.grid[id].cell.column := c;
          pg.grid[id].pt := Point((startX + ((id mod columns) * (columnSpace + 1))),
                                  (startY + ((id div columns) * (rowSpace + 1))));
        end;
      Result := True;
    end;

    function grid_Add_Point_Row(var pg: TPointGrid): Boolean;
    var
      id, r, c: Integer;
    begin
      if (pg.startPoint.X < 0) or (pg.startPoint.Y < 0) or (pg.rowSpace < 0) or (pg.columnSpace < 0) then
        Exit;
      SetLength(pg, 0);
      Inc(pg.rows);
      pg.size := (pg.rows * pg.columns);
      SetLength(pg.grid, pg.size);
      for r := 0 to (pg.rows - 1) do
        for c := 0 to (pg.columns - 1) do
        begin
          id := (r * pg.columns) + c;
          pg.grid[id].cell.row := r;
          pg.grid[id].cell.column := c;
          pg.grid[id].pt := Point((pg.startPoint.X + ((id mod pg.columns) * (pg.columnSpace + 1))),
                                  (pg.startPoint.Y + ((id div pg.columns) * (pg.rowSpace + 1))));
        end;
      Result := True;
    end;

    function grid_Add_PointGrid_Column(var pg: TPointGrid): Boolean;
    var
      id, r, c: Integer;
    begin
      if (pg.startPoint.X < 0) or (pg.startPoint.Y < 0) or (pg.rowSpace < 0) or (pg.columnSpace < 0) then
        Exit;
      SetLength(pg, 0);
      Inc(pg.columns);
      pg.size := (pg.rows * pg.columns);
      SetLength(pg.grid, pg.size);
      for r := 0 to (pg.rows - 1) do
        for c := 0 to (pg.columns - 1) do
        begin
          id := (r * pg.columns) + c;
          pg.grid[id].cell.row := r;
          pg.grid[id].cell.column := c;
          pg.grid[id].pt := Point((pg.startPoint.X + ((id mod pg.columns) * (pg.columnSpace + 1))),
                                  (pg.startPoint.Y + ((id div pg.columns) * (pg.rowSpace + 1))));
        end;
      Result := True;
    end;

    function grid_Jump_PointGrid_CellByDistance(pg: TPointGrid; cell: TCell; rowMoveDist, columnMoveDist: Integer): Boolean;
    begin
      Result := InRange(0, pg.rows, (cell.row + rowMoveDist)) and InRange(0, pg.columns, (cell.column + columnMoveDist));
      if not Result then
        Exit;
      cell.row := (cell.row + rowMoveDist);
      cell.column := (cell.column + columnMoveDist);
    end;

    function grid_Jump_PointGrid_CellByCompassDirection(pg: TPointGrid; var cell: TCell; direction, distance: Integer): Boolean;
    begin
      if (distance < 1) or (distance > pg.columns) or (distance > pg.rows) or not InRange(direction, 0, 7) then
        Exit;
      case direction of
        COMPASS_DIRECTION_NORTH_EAST: begin
                                        if (0 > (cell.row - distance)) or ((pg.columns - 1) < (cell.column + distance)) then
                                          Exit;
                                        cell.row := (cell.row - distance);
                                        cell.column := (cell.column + distance);
                                      end;
        COMPASS_DIRECTION_SOUTH_EAST: begin
                                        if ((pg.rows - 1) < (cell.row + distance)) or ((pg.columns - 1) < (cell.column + distance)) then
                                          Exit;
                                        cell.row := (cell.row + distance);
                                        cell.column := (cell.column + distance);
                                      end;
        COMPASS_DIRECTION_SOUTH_WEST: begin
                                        if ((pg.rows - 1) < (cell.row + distance)) or (0 > (cell.column - distance)) then
                                          Exit;
                                        cell.row := (cell.row + distance);
                                        cell.column := (cell.column - distance);
                                      end;
        COMPASS_DIRECTION_NORTH_WEST: begin
                                        if (0 > (cell.row - distance)) or (0 > (cell.column - distance)) then
                                          Exit;
                                        cell.row := (cell.row - distance);
                                        cell.column := (cell.column - distance);
                                      end;
        COMPASS_DIRECTION_WEST: begin
                                  if (0 > (cell.column - distance)) then
                                    Exit;
                                  cell.column := (cell.column - distance);
                                end;
        COMPASS_DIRECTION_NORTH: begin
                                   if (0 > (cell.row - distance)) then
                                     Exit;
                                   cell.row := (cell.row - distance);
                                 end;
        COMPASS_DIRECTION_EAST: begin
                                  if ((pg.columns - 1) < (cell.column + distance)) then
                                    Exit;
                                  cell.column := (cell.column + distance);
                                end;
        COMPASS_DIRECTION_SOUTH: begin
                                   if ((pg.rows - 1) < (cell.row + distance)) then
                                     Exit;
                                   cell.row := (cell.row + distance);
                                 end;
      end;
      Result := True;
    end;

    function grid_Get_PointGrid_Points(pg: TPointGrid): TPointArray;
    var
      i: Integer;
    begin
      if pg.size < 1 then
        Exit;
      SetLength(Result, pg.size);
      for i := 0 to (pg.size - 1) do
        Result[i] := pg.grid[i].pt;
    end;

    function grid_Convert_PointGrid_CellToID(pg: TPointGrid; cell: TCell): Integer;
    begin
      Result := -1;
      if InRange(cell.row, 0, (pg.rows - 1)) and InRange(cell.column, 0, (pg.columns - 1)) then
        Result := ((cell.row * pg.columns) + cell.column);
    end;

    function grid_Convert_PointGrid_IDToCell(pg: TPointGrid; ID: Integer): TCell;
    begin
      if InRange(ID, 0, (pg.size - 1)) then
        for Result.row := 0 to (pg.rows - 1) do
          for Result.column := 0 to (pg.columns - 1) do
            if ID = ((Result.row * pg.columns) + Result.column) then
              Exit;
      Result.row := -1;
      Result.column := -1;
    end;

    function grid_Convert_PointGrid_PointToID(pg: TPointGrid; p: TPoint): Integer;
    var
      i: Integer;
    begin
      Result := -1;
      if pg.size > 0 then
        for i := 0 to (pg.size - 1) do
          if p = pg.grid[i].pt then
          begin
            Result := i;
            Exit;
          end;
    end;

    function grid_Convert_PointGrid_IDToPoint(pg: TPointGrid; ID: Integer): TPoint;
    begin
      Result := Point(-1, -1);
      if InRange(ID, 0, (pg.size - 1)) and (pg.size > 0) then
        Result := pg.grid[ID].pt;
    end;

    function grid_Convert_PointGrid_PointToCell(pg: TPointGrid; p: TPoint): TCell;
    begin
      if pg.size > 0 then
        for Result.row := 0 to (pg.rows - 1) do
          for Result.column := 0 to (pg.columns - 1) do
            if p = pg.grid[(Result.row * pg.columns) + Result.column].pt then
              Exit;
      Result.row := -1;
      Result.column := -1;
    end;

    function grid_Convert_PointGrid_CellToPoint(pg: TPointGrid; cell: TCell): TPoint;
    var
      ID: Integer;
    begin
      Result := Point(-1, -1);
      if InRange(ID, 0, (pg.size - 1)) and (pg.size > 0) then
        Result := pg.grid[(cell.row * pg.columns) + cell.column].pt;
    end;

    -Jani
    Last edited by Janilabo; 04-28-2012 at 06:36 AM.

  4. #4
    Join Date
    Feb 2012
    Posts
    170
    Mentioned
    0 Post(s)
    Quoted
    6 Post(s)

    Default

    var
    boxArray : Array[0..72] of TBox; // 6 x 12 boxes is 72 boxes I'll need if I understand this declaration correctly.
    myBox : TBox; // how would this help? setting a new box equal to one part of my array of boxes? sorry I'm lost..

    and then I need to loop through all the declarations of the boxes, so something I believe
    x and y are integers.
    like
    for x:= 1 to 6 do
    begin
    for y:=1 to 12 do
    begin
    with boxArray[(I don't know how to number them.. because I want it to be gridded (x,y). but in a 1D array I'm going to have them all in a line so box at 1x and 2y would end up being box[7]... which is terribly hard to use later on.] do
    begin
    X1 := 95 + (x * 45);
    X2 := 140 + (x * 45);
    Y1 := 70 + (y * 45);
    Y2 := 115 + (y * 45);
    end; //The grid is kinda off center in the middle of the screen.

  5. #5
    Join Date
    Feb 2012
    Posts
    170
    Mentioned
    0 Post(s)
    Quoted
    6 Post(s)

    Default

    Would it just be simpler if I made an array for each of the x collumns? then I'd have 6 arrays and I could name them based on there x coord and there y coord would just be their number. meh..

  6. #6
    Join Date
    Feb 2006
    Location
    Helsinki, Finland
    Posts
    1,395
    Mentioned
    30 Post(s)
    Quoted
    107 Post(s)

    Default

    Added missing Cell(row, column: Integer): TCell; function, and made example with row, column search...
    Look below.

    Simba Code:
    type
      TCell = record
        row, column: Integer;
      end;
      TBoxGrid = record
        startPoint: TPoint;
        size, rows, columns, boxWidth, boxHeight, rowSpace, columnSpace: Integer;
        grid: array of record
          cell: TCell;
          bx: TBox;
        end;
      end;
      TPointGrid = record
        startPoint: TPoint;
        size, rows, columns, rowSpace, columnSpace: Integer;
        grid: array of record
          cell: TCell;
          pt: TPoint;
        end;
      end;

    const
      COMPASS_DIRECTION_NORTH_EAST = 0;
      COMPASS_DIRECTION_SOUTH_EAST = 1;
      COMPASS_DIRECTION_SOUTH_WEST = 2;
      COMPASS_DIRECTION_NORTH_WEST = 3;
      COMPASS_DIRECTION_WEST = 4;
      COMPASS_DIRECTION_NORTH = 5;
      COMPASS_DIRECTION_EAST = 6;
      COMPASS_DIRECTION_SOUTH = 7;

    function Box(X1, Y1, X2, Y2: Integer): TBox;
    begin
      Result.X1 := X1;
      Result.Y1 := Y1;
      Result.X2 := X2;
      Result.Y2 := Y2;
    end;

    function Cell(row, column: Integer): TCell;
    begin
      Result.row := row;
      Result.column := column;
    end;

    function grid_Build_BoxGrid(var bg: TBoxGrid; startX, startY, boxWidth, boxHeight, rows, columns, rowSpace, columnSpace: Integer): Boolean;
    var
      id, r, c: Integer;
    begin
      if (startX < 0) or (startY < 0) or (boxWidth < 1) or (boxHeight < 1) or (rows < 1) or (columns < 1) or (rowSpace < 0) or (columnSpace < 0) then
        Exit;
      bg.size := (rows * columns);
      bg.rows := rows;
      bg.columns := columns;
      bg.startPoint := Point(startX, startY);
      bg.boxWidth := boxWidth;
      bg.boxHeight := boxHeight;
      bg.rowSpace := rowSpace;
      bg.columnSpace := columnSpace;
      SetLength(bg.grid, bg.size);
      for r := 0 to (rows - 1) do
        for c := 0 to (columns - 1) do
        begin
          id := (r * columns) + c;
          bg.grid[id].cell.row := r;
          bg.grid[id].cell.column := c;
          bg.grid[id].bx := Box((startX + ((id mod columns) * (boxWidth + (columnSpace + 1)))),
                                (startY + ((id div columns) * (boxHeight + (rowSpace + 1)))),
                                ((startX + ((id mod columns) * (boxWidth + (columnSpace + 1)))) + boxWidth),
                                ((startY + ((id div columns) * (boxHeight + (rowSpace + 1)))) + boxHeight));
        end;
      Result := True;
    end;

    function grid_Add_BoxGrid_Row(var bg: TBoxGrid): Boolean;
    var
      id, r, c: Integer;
    begin
      if (bg.startPoint.X < 0) or (bg.startPoint.Y < 0) or (bg.boxWidth < 1) or (bg.boxHeight < 1) or (bg.rowSpace < 0) or (bg.columnSpace < 0) then
        Exit;
      SetLength(bg, 0);
      Inc(bg.rows);
      bg.size := (bg.rows * bg.columns);
      SetLength(bg.grid, bg.size);
      for r := 0 to (bg.rows - 1) do
        for c := 0 to (bg.columns - 1) do
        begin
          id := (r * bg.columns) + c;
          bg.grid[id].cell.row := r;
          bg.grid[id].cell.column := c;
          bg.grid[id].bx := Box((bg.startPoint.X + ((id mod bg.columns) * (bg.boxWidth + (bg.columnSpace + 1)))),
                                (bg.startPoint.Y + ((id div bg.columns) * (bg.boxHeight + (bg.rowSpace + 1)))),
                                ((bg.startPoint.X + ((id mod bg.columns) * (bg.boxWidth + (bg.columnSpace + 1)))) + bg.boxWidth),
                                ((bg.startPoint.Y + ((id div bg.columns) * (bg.boxHeight + (bg.rowSpace + 1)))) + bg.boxHeight));
        end;
      Result := True;
    end;

    function grid_Add_BoxGrid_Column(var bg: TBoxGrid): Boolean;
    var
      id, r, c: Integer;
    begin
      if (bg.startPoint.X < 0) or (bg.startPoint.Y < 0) or (bg.boxWidth < 1) or (bg.boxHeight < 1) or (bg.rowSpace < 0) or (bg.columnSpace < 0) then
        Exit;
      SetLength(bg, 0);
      Inc(bg.columns);
      bg.size := (bg.rows * bg.columns);
      SetLength(bg.grid, bg.size);
      for r := 0 to (bg.rows - 1) do
        for c := 0 to (bg.columns - 1) do
        begin
          id := (r * bg.columns) + c;
          bg.grid[id].cell.row := r;
          bg.grid[id].cell.column := c;
          bg.grid[id].bx := Box((bg.startPoint.X + ((id mod bg.columns) * (bg.boxWidth + (bg.columnSpace + 1)))),
                                (bg.startPoint.Y + ((id div bg.columns) * (bg.boxHeight + (bg.rowSpace + 1)))),
                                ((bg.startPoint.X + ((id mod bg.columns) * (bg.boxWidth + (bg.columnSpace + 1)))) + bg.boxWidth),
                                ((bg.startPoint.Y + ((id div bg.columns) * (bg.boxHeight + (bg.rowSpace + 1)))) + bg.boxHeight));
        end;
      Result := True;
    end;

    function grid_Jump_BoxGrid_CellByDistance(bg: TBoxGrid; cell: TCell; rowMoveDist, columnMoveDist: Integer): Boolean;
    begin
      Result := InRange(0, bg.rows, (cell.row + rowMoveDist)) and InRange(0, bg.columns, (cell.column + columnMoveDist));
      if not Result then
        Exit;
      cell.row := (cell.row + rowMoveDist);
      cell.column := (cell.column + columnMoveDist);
    end;

    function grid_Get_BoxGrid_Boxes(bg: TBoxGrid): TBoxArray;
    var
      i: Integer;
    begin
      if bg.size < 1 then
        Exit;
      SetLength(Result, bg.size);
      for i := 0 to (bg.size - 1) do
        Result[i] := bg.grid[i].bx;
    end;

    function grid_Convert_BoxGrid_CellToID(bg: TBoxGrid; cell: TCell): Integer;
    begin
      Result := -1;
      if InRange(cell.row, 0, (bg.rows - 1)) and InRange(cell.column, 0, (bg.columns - 1)) then
        Result := ((cell.row * bg.columns) + cell.column);
    end;

    function grid_Convert_BoxGrid_IDToCell(bg: TBoxGrid; ID: Integer): TCell;
    begin
      if InRange(ID, 0, (bg.size - 1)) then
        for Result.row := 0 to (bg.rows - 1) do
          for Result.column := 0 to (bg.columns - 1) do
            if ID = ((Result.row * bg.columns) + Result.column) then
              Exit;
      Result.row := -1;
      Result.column := -1;
    end;

    function grid_Convert_BoxGrid_PointToID(bg: TBoxGrid; p: TPoint): Integer;
    var
      i: Integer;
    begin
      Result := -1;
      if bg.size > 0 then
        for i := 0 to (bg.size - 1) do
          if PointInBox(p, bg.grid[i].bx) then
          begin
            Result := i;
            Exit;
          end;
    end;

    function grid_Convert_BoxGrid_IDToBox(bg: TBoxGrid; ID: Integer): TBox;
    begin
      Result := Box(-1, -1, -1, -1);
      if InRange(ID, 0, (bg.size - 1)) and (bg.size > 0) then
        Result := bg.grid[ID].bx;
    end;

    function grid_Jump_BoxGrid_CellByCompassDirection(bg: TBoxGrid; var cell: TCell; direction, distance: Integer): Boolean;
    begin
      if (distance < 1) or (distance > bg.columns) or (distance > bg.rows) or not InRange(direction, 0, 7) then
        Exit;
      case direction of
        COMPASS_DIRECTION_NORTH_EAST: begin
                                        if (0 > (cell.row - distance)) or ((bg.columns - 1) < (cell.column + distance)) then
                                          Exit;
                                        cell.row := (cell.row - distance);
                                        cell.column := (cell.column + distance);
                                      end;
        COMPASS_DIRECTION_SOUTH_EAST: begin
                                        if ((bg.rows - 1) < (cell.row + distance)) or ((bg.columns - 1) < (cell.column + distance)) then
                                          Exit;
                                        cell.row := (cell.row + distance);
                                        cell.column := (cell.column + distance);
                                      end;
        COMPASS_DIRECTION_SOUTH_WEST: begin
                                        if ((bg.rows - 1) < (cell.row + distance)) or (0 > (cell.column - distance)) then
                                          Exit;
                                        cell.row := (cell.row + distance);
                                        cell.column := (cell.column - distance);
                                      end;
        COMPASS_DIRECTION_NORTH_WEST: begin
                                        if (0 > (cell.row - distance)) or (0 > (cell.column - distance)) then
                                          Exit;
                                        cell.row := (cell.row - distance);
                                        cell.column := (cell.column - distance);
                                      end;
        COMPASS_DIRECTION_WEST: begin
                                  if (0 > (cell.column - distance)) then
                                    Exit;
                                  cell.column := (cell.column - distance);
                                end;
        COMPASS_DIRECTION_NORTH: begin
                                   if (0 > (cell.row - distance)) then
                                     Exit;
                                   cell.row := (cell.row - distance);
                                 end;
        COMPASS_DIRECTION_EAST: begin
                                  if ((bg.columns - 1) < (cell.column + distance)) then
                                    Exit;
                                  cell.column := (cell.column + distance);
                                end;
        COMPASS_DIRECTION_SOUTH: begin
                                   if ((bg.rows - 1) < (cell.row + distance)) then
                                     Exit;
                                   cell.row := (cell.row + distance);
                                 end;
      end;
      Result := True;
    end;

    function grid_Build_PointGrid(var pg: TPointGrid; startX, startY, rows, columns, rowSpace, columnSpace: Integer): Boolean;
    var
      id, r, c: Integer;
    begin
      if (startX < 0) or (startY < 0) or (rows < 1) or (columns < 1) or (rowSpace < 0) or (columnSpace < 0) then
        Exit;
      pg.size := (rows * columns);
      pg.rows := rows;
      pg.columns := columns;
      pg.startPoint := Point(startX, startY);
      pg.rowSpace := rowSpace;
      pg.columnSpace := columnSpace;
      SetLength(pg.grid, pg.size);
      for r := 0 to (rows - 1) do
        for c := 0 to (columns - 1) do
        begin
          id := (r * columns) + c;
          pg.grid[id].cell.row := r;
          pg.grid[id].cell.column := c;
          pg.grid[id].pt := Point((startX + ((id mod columns) * (columnSpace + 1))),
                                  (startY + ((id div columns) * (rowSpace + 1))));
        end;
      Result := True;
    end;

    function grid_Add_Point_Row(var pg: TPointGrid): Boolean;
    var
      id, r, c: Integer;
    begin
      if (pg.startPoint.X < 0) or (pg.startPoint.Y < 0) or (pg.rowSpace < 0) or (pg.columnSpace < 0) then
        Exit;
      SetLength(pg, 0);
      Inc(pg.rows);
      pg.size := (pg.rows * pg.columns);
      SetLength(pg.grid, pg.size);
      for r := 0 to (pg.rows - 1) do
        for c := 0 to (pg.columns - 1) do
        begin
          id := (r * pg.columns) + c;
          pg.grid[id].cell.row := r;
          pg.grid[id].cell.column := c;
          pg.grid[id].pt := Point((pg.startPoint.X + ((id mod pg.columns) * (pg.columnSpace + 1))),
                                  (pg.startPoint.Y + ((id div pg.columns) * (pg.rowSpace + 1))));
        end;
      Result := True;
    end;

    function grid_Add_PointGrid_Column(var pg: TPointGrid): Boolean;
    var
      id, r, c: Integer;
    begin
      if (pg.startPoint.X < 0) or (pg.startPoint.Y < 0) or (pg.rowSpace < 0) or (pg.columnSpace < 0) then
        Exit;
      SetLength(pg, 0);
      Inc(pg.columns);
      pg.size := (pg.rows * pg.columns);
      SetLength(pg.grid, pg.size);
      for r := 0 to (pg.rows - 1) do
        for c := 0 to (pg.columns - 1) do
        begin
          id := (r * pg.columns) + c;
          pg.grid[id].cell.row := r;
          pg.grid[id].cell.column := c;
          pg.grid[id].pt := Point((pg.startPoint.X + ((id mod pg.columns) * (pg.columnSpace + 1))),
                                  (pg.startPoint.Y + ((id div pg.columns) * (pg.rowSpace + 1))));
        end;
      Result := True;
    end;

    function grid_Jump_PointGrid_CellByDistance(pg: TPointGrid; cell: TCell; rowMoveDist, columnMoveDist: Integer): Boolean;
    begin
      Result := InRange(0, pg.rows, (cell.row + rowMoveDist)) and InRange(0, pg.columns, (cell.column + columnMoveDist));
      if not Result then
        Exit;
      cell.row := (cell.row + rowMoveDist);
      cell.column := (cell.column + columnMoveDist);
    end;

    function grid_Jump_PointGrid_CellByCompassDirection(pg: TPointGrid; var cell: TCell; direction, distance: Integer): Boolean;
    begin
      if (distance < 1) or (distance > pg.columns) or (distance > pg.rows) or not InRange(direction, 0, 7) then
        Exit;
      case direction of
        COMPASS_DIRECTION_NORTH_EAST: begin
                                        if (0 > (cell.row - distance)) or ((pg.columns - 1) < (cell.column + distance)) then
                                          Exit;
                                        cell.row := (cell.row - distance);
                                        cell.column := (cell.column + distance);
                                      end;
        COMPASS_DIRECTION_SOUTH_EAST: begin
                                        if ((pg.rows - 1) < (cell.row + distance)) or ((pg.columns - 1) < (cell.column + distance)) then
                                          Exit;
                                        cell.row := (cell.row + distance);
                                        cell.column := (cell.column + distance);
                                      end;
        COMPASS_DIRECTION_SOUTH_WEST: begin
                                        if ((pg.rows - 1) < (cell.row + distance)) or (0 > (cell.column - distance)) then
                                          Exit;
                                        cell.row := (cell.row + distance);
                                        cell.column := (cell.column - distance);
                                      end;
        COMPASS_DIRECTION_NORTH_WEST: begin
                                        if (0 > (cell.row - distance)) or (0 > (cell.column - distance)) then
                                          Exit;
                                        cell.row := (cell.row - distance);
                                        cell.column := (cell.column - distance);
                                      end;
        COMPASS_DIRECTION_WEST: begin
                                  if (0 > (cell.column - distance)) then
                                    Exit;
                                  cell.column := (cell.column - distance);
                                end;
        COMPASS_DIRECTION_NORTH: begin
                                   if (0 > (cell.row - distance)) then
                                     Exit;
                                   cell.row := (cell.row - distance);
                                 end;
        COMPASS_DIRECTION_EAST: begin
                                  if ((pg.columns - 1) < (cell.column + distance)) then
                                    Exit;
                                  cell.column := (cell.column + distance);
                                end;
        COMPASS_DIRECTION_SOUTH: begin
                                   if ((pg.rows - 1) < (cell.row + distance)) then
                                     Exit;
                                   cell.row := (cell.row + distance);
                                 end;
      end;
      Result := True;
    end;

    function grid_Get_PointGrid_Points(pg: TPointGrid): TPointArray;
    var
      i: Integer;
    begin
      if pg.size < 1 then
        Exit;
      SetLength(Result, pg.size);
      for i := 0 to (pg.size - 1) do
        Result[i] := pg.grid[i].pt;
    end;

    function grid_Convert_PointGrid_CellToID(pg: TPointGrid; cell: TCell): Integer;
    begin
      Result := -1;
      if InRange(cell.row, 0, (pg.rows - 1)) and InRange(cell.column, 0, (pg.columns - 1)) then
        Result := ((cell.row * pg.columns) + cell.column);
    end;

    function grid_Convert_PointGrid_IDToCell(pg: TPointGrid; ID: Integer): TCell;
    begin
      if InRange(ID, 0, (pg.size - 1)) then
        for Result.row := 0 to (pg.rows - 1) do
          for Result.column := 0 to (pg.columns - 1) do
            if ID = ((Result.row * pg.columns) + Result.column) then
              Exit;
      Result.row := -1;
      Result.column := -1;
    end;

    function grid_Convert_PointGrid_PointToID(pg: TPointGrid; p: TPoint): Integer;
    var
      i: Integer;
    begin
      Result := -1;
      if pg.size > 0 then
        for i := 0 to (pg.size - 1) do
          if p = pg.grid[i].pt then
          begin
            Result := i;
            Exit;
          end;
    end;

    function grid_Convert_PointGrid_IDToPoint(pg: TPointGrid; ID: Integer): TPoint;
    begin
      Result := Point(-1, -1);
      if InRange(ID, 0, (pg.size - 1)) and (pg.size > 0) then
        Result := pg.grid[ID].pt;
    end;

    function grid_Convert_PointGrid_PointToCell(pg: TPointGrid; p: TPoint): TCell;
    begin
      if pg.size > 0 then
        for Result.row := 0 to (pg.rows - 1) do
          for Result.column := 0 to (pg.columns - 1) do
            if p = pg.grid[(Result.row * pg.columns) + Result.column].pt then
              Exit;
      Result.row := -1;
      Result.column := -1;
    end;

    function grid_Convert_PointGrid_CellToPoint(pg: TPointGrid; cell: TCell): TPoint;
    var
      ID: Integer;
    begin
      Result := Point(-1, -1);
      if InRange(ID, 0, (pg.size - 1)) and (pg.size > 0) then
        Result := pg.grid[(cell.row * pg.columns) + cell.column].pt;
    end;

    var
      i, r, c: Integer;
      boxGrid: TBoxGrid;

    const
      START_X = 95;
      START_Y = 70;
      BOX_WIDTH = 45;
      BOX_HEIGHT = 45;
      ROWS = 12;
      COLUMNS = 6;
      ROW_SPACE = 1;
      COLUMN_SPACE = 1;

    begin
      ClearDebug;
      grid_Build_BoxGrid(boxGrid,
                         START_X,
                         START_Y,
                         BOX_WIDTH,
                         BOX_HEIGHT,
                         ROWS,
                         COLUMNS,
                         ROW_SPACE,
                         COLUMN_SPACE);
      for r := 0 to (boxGrid.rows - 1) do
        for c := 0 to (boxGrid.columns - 1) do
        begin
          i := grid_Convert_BoxGrid_CellToID(boxGrid, Cell(r, c));
          WriteLn('boxGrid.grid[' + ToStr(i) + '].bx: ' + ToStr(boxGrid.grid[i].bx));
          WriteLn('boxGrid.grid[' + ToStr(i) + '].cell.Row: ' + ToStr(boxGrid.grid[i].cell.row));
          WriteLn('boxGrid.grid[' + ToStr(i) + '].cell.Column: ' + ToStr(boxGrid.grid[i].cell.column));
          if i < (boxGrid.size - 1) then
            WriteLn('');
        end;
    end.
    Last edited by Janilabo; 04-28-2012 at 08:33 AM. Reason: Fixed the codes to use the correct start Y. +MERGE

  7. #7
    Join Date
    Feb 2012
    Posts
    170
    Mentioned
    0 Post(s)
    Quoted
    6 Post(s)

    Default

    Oh god.. haha. I only have basic knowledge scripting with java. I don't know how types work. I guess I have to go learn that first... Thanks bro. I'll probably have more questions after I start understanding what all that you posted means.

  8. #8
    Join Date
    Mar 2012
    Location
    Grambling, LA
    Posts
    70
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by xdarkshadowx View Post
    Oh god.. haha. I only have basic knowledge scripting with java. I don't know how types work. I guess I have to go learn that first... Thanks bro. I'll probably have more questions after I start understanding what all that you posted means.
    And, I am also new to Simba, so it would take me some time to understand and utilize Janilabo, a senior scripter's script which seems to have a lots of functionalities added for our convenience.

    Yes, a basic example of one solution to your problem would be to use Record. e.g.
    Simba Code:
    program new;
    type
      boxArrayType = record
        boxArray:Array[1..6] of TBox;
      end;
    var
      boxArrayX : boxArrayType;
      boxArrayY : Array[1..12] of boxArrayType;
      x, y, x1, y1, x2, y2: Integer;
    begin
      for x:=1 to 6 do
          with boxArrayY[y] do
              for y:=1 to 12 do
                  with boxArrayX.boxArray[x] do
                  begin
                      X1 := 95 + (x * 45);
                      X2 := 140 + (x * 45);
                      Y1 := 70 + (y * 45);
                      Y2 := 115 + (y * 45);
                  end;
          end;
    end.
    Last edited by johnbrown8976; 04-28-2012 at 07:14 AM.

  9. #9
    Join Date
    Feb 2006
    Location
    Helsinki, Finland
    Posts
    1,395
    Mentioned
    30 Post(s)
    Quoted
    107 Post(s)

    Default

    Sorry buddy, I just noticed you used X1, X2, Y1, Y2.. So I set a wrong start Y for you, to the code.. I fixed that.
    Is it correct now? If its not.. Then the row_space / column_space need to be changed..

    Yeah sorry about the code, its for sure not the easiest one to read.. But I wanted to make the types to keep info of the cell aswell. +It's very flexible in a way, so thats why its a bit confusing to understand... Keep reading, as it's not rocket science at least..

    Here is a little simplified version, maybe it helps?

    Simba Code:
    const
      START_X = 95;
      START_Y = 70;
      ROWS = 12;
      COLUMNS = 6;
      WIDTH = 45;
      HEIGHT = 45;
      ROW_SPACE = 0;
      COLUMN_SPACE = 0;

    var
      BoxArr2D: array of TBoxArray;
      r, c: Integer;

    function BuildBoxArr2D: array of TBoxArray;
    var
      id, r, c: Integer;
    begin
      if (ROWS < 0) or (COLUMNS < 0) then
        Exit;
      SetLength(Result, ROWS);
      for r := 0 to (ROWS - 1) do
      begin
        SetLength(Result[r], COLUMNS);
        for c := 0 to (COLUMNS - 1) do
        begin
          id := ((r * COLUMNS) + c);
          Result[r][c].X1 := START_X + ((id mod COLUMNS) * (WIDTH + (COLUMN_SPACE + 1)));
          Result[r][c].Y1 := START_Y + ((id div COLUMNS) * (HEIGHT + (ROW_SPACE + 1)));
          Result[r][c].X2 := (START_X + ((id mod COLUMNS) * (WIDTH + (COLUMN_SPACE + 1))) + WIDTH);
          Result[r][c].Y2 := (START_Y + ((id div COLUMNS) * (HEIGHT + (ROW_SPACE + 1))) + HEIGHT);
        end;
      end;
    end;

    {
    function RowColToID(row, column: Integer): Integer;
    begin
      if InRange(row, 0, (ROWS - 1)) and InRange(column, 0, (COLUMNS - 1)) then
        Result := ((row * COLUMNS) + column);
    end;

    procedure IDToRowCol(ID: Integer; var row, column: Integer);
    begin
      if InRange(ID, 0, ((ROWS * COLUMNS) - 1)) then
        for row := 0 to (ROWS - 1) do
          for column := 0 to (COLUMNS - 1) do
            if ID = ((row * COLUMNS) + column) then
              Exit;
    end;
    }


    begin
      ClearDebug;
      boxArr2D := BuildBoxArr2D;
      for r := 0 to (ROWS - 1) do
        for c := 0 to (COLUMNS - 1) do
          WriteLn('boxArr2D[' + ToStr(r) + '][' + ToStr(c) + ']: ' + ToStr(boxArr2D[r][c]));
      SetLength(boxArr2D, 0);
    end.
    Last edited by Janilabo; 04-28-2012 at 08:28 AM.

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
  •