Highlighting tiles in range x

please format code with </> button * homework policy * asking questions

Hello everybody,

I’ve been working on this code a few days, and I’m out of ideas. My goal is to highlight tiles inside of the movement range. I’m using multiple 2d arrays for this as different layers, but for the example i’ll make them quite a bit smaller.

mapunitplaces[][] = { //7x7 grid displaying where units are
{ 0, 0, 0, 0, 0, 0, 0}
{ 0, 0, 0, 0, 0, 0, 0}
{ 0, 0, 0, 0, 0, 0, 0}
{ 0, 0, 0, 0, 0, 0, 0}
{ 0, 0, 0, 0, 0, 0, 0}
{ 0, 0, 0, 0, 1, 0, 0}
{ 0, 0, 0, 0, 0, 0, 0}
}
// 0 is a not occupied tile, 1 is occupied by friendly units

maphighlights[][] = {
{ 0, 0, 0, 0, 0, 0, 0}
{ 0, 0, 0, 0, 0, 0, 0}
{ 0, 0, 0, 0, 0, 0, 0}
{ 0, 0, 0, 0, 0, 0, 0}
{ 0, 0, 0, 0, 0, 0, 0}
{ 0, 0, 0, 0, 0, 0, 0}
{ 0, 0, 0, 0, 0, 0, 0}
} 
// techincally the same, but with all 0, which turn into 1 if it should be highlighted

mapallones[][] = {
{ 1, 1, 1, 1, 1, 1, 1}
{ 1, 1, 1, 1, 1, 1, 1}
{ 1, 1, 1, 1, 1, 1, 1}
{ 1, 1, 1, 1, 1, 1, 1}
{ 1, 1, 1, 1, 1, 1, 1}
{ 1, 1, 1, 1, 1, 1, 1}
{ 1, 1, 1, 1, 1, 1, 1}
}
// as it says, all the numbers on this 7x7 field are 1. 
// the idea behind this is to make the center (unit's position) 0 and count outwards
// by adding to the previous number, so next to the center is 1, next to that will be 1+1, and so forth

and the code itself:

void mousePressed() {

  //function if a unit is clicked
  float UnitXmin;
  float UnitYmin;
  float UnitXmax;
  float UnitYmax;
  int OrigX;
  int OrigY;
  int tteImin;
  int tteJmin;
  int tteImax;
  int tteJmax;
  int range;

  for (int x = 0; x < map.length; x++) {
    for (int y = 0; y < map[x].length; y++) {

      //check which points mapunitPlaces[x][y] = 1

      if (mapunitPlaces[x][y] == 1) {

        //calculate the left top point of the tile and the right bottom point of the tile

        UnitXmin = (x * scaleX) + Xmovement;
        UnitYmin = (y * scaleY) + Ymovement;
        UnitXmax = (x * scaleX) + (scaleX - 1) + Xmovement;
        UnitYmax = (y * scaleY) + (scaleY - 1) + Ymovement;
        
        //check if mouseX and mouseY are within these borders

        if ((UnitXmin < mouseX) && (mouseX < UnitXmax) && (UnitYmin < mouseY) && (mouseY < UnitYmax)) {
          
          //failsafe to calculate original tile

          OrigX = int((UnitXmin - Xmovement)/scaleX);
          OrigY = int((UnitYmin - Ymovement)/scaleY);

         //check which unit is clicked and set movement range accordingly

          if (mapunitspecifics[OrigX][OrigY] == 1) {
            range = 5;
          }

          //tiles till edge to make sure there's no out of bounds exceptions
          tteImax = (78 - OrigX) - 1; //bottom edge?
          tteJmax = (76 - OrigY) - 1; //right edge?
          tteImin = OrigX - 78; //top edge?
          tteJmin = OrigY - 76; //left edge?

          //set to range / -range to create correct min and max 

          if (tteImax > range) {
            tteImax = range;
          }
          if (tteJmax > range) {
            tteJmax = range;
          }
          if (tteImin < -range) {
            tteImin = -range;
          }
          if (tteJmin < -range) {
            tteJmin = -range;
          }

          for (int i = tteImin; i < tteImax; i++) {
            for (int j = tteJmin; j < tteImax; j++) {
              mapallones[OrigX][OrigY] = 0;
              //for loop to check which ones become 5 together
              if (abs(mapallones[OrigX + i][OrigY + j]) <= range) { //<-- DOESN'T WORK (if i'm right, since here's where it says out of bounds exception)
                mapHighlights[OrigX + i][OrigY + j] = 1;
                //abs(i + j) = 5 where upper edge/-5 > i > bottom edge/5 and left edge/-5 > j > right edge/5
              }
            }
          }
        }
      }
    }
  }
}

As far as I’m aware, the problem is in the last bit. I’ve checked the formula and it should work, but the actual notation in code is the problem here. The plan was to take the position and highlight tiles untill the program runs into the limit of the formula. this should give a diamond kind of shape (or a 2d array like below.

maphighlights[][] = {
{ 0, 0, 0, 0, 1, 0, 0}
{ 0, 0, 0, 1, 1, 1, 0}
{ 0, 0, 1, 1, 1, 1, 1}
{ 0, 1, 1, 1, 1, 1, 1}
{ 1, 1, 1, 1, 1, 1, 1}
{ 1, 1, 1, 1, 0, 1, 1}
{ 1, 1, 1, 1, 1, 1, 1}
}  
//the 0 in the middle of the 1's is the unit's position, but if it gets 
//highlighted as well but everything works, i'll take that xD

Currently i’m getting an out of bouds exception at the point shown in the code, so my guess is I have to write it down completely different
If there’s any questions, feel free to ask, and thanks in advance!

here is an error

must be tteJmax

Thanks! that indeed did something.
the problem still stands though, after changing I to J, the results for highlighting are:

maphighlights[][] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}
{1, 1, 1, 1, 1, 1 /*<- unit's position*/, 1, 1, 1, 1, 0}
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
//edge of the real map
}  

At least things get hightlighted, but it’s not yet a diamond (also 3 tiles above, 1 below and 4 right of the unit, while expecting 5 above, 2 below and 5 right).

I can’t help you with that

No problem, got most of the answer completed. The current code is:

void mousePressed() {

  //function if a unit is clicked
  float UnitXmin;
  float UnitYmin;
  float UnitXmax;
  float UnitYmax;
  int OrigX;
  int OrigY;
  int tteImin;
  int tteJmin;
  int tteImax;
  int tteJmax;

  for (int x = 0; x < map.length; x++) {
    for (int y = 0; y < map[x].length; y++) {
      //check if mouseX & mouseY align with the point where mapunitPlaces[x][y] = 1
      if (mapunitPlaces[x][y] == 1) {
        UnitXmin = (x * scaleX) + Xmovement;
        UnitYmin = (y * scaleY) + Ymovement;
        UnitXmax = (x * scaleX) + (scaleX - 1) + Xmovement;
        UnitYmax = (y * scaleY) + (scaleY - 1) + Ymovement;

        if ((UnitXmin < mouseX) && (mouseX < UnitXmax) && (UnitYmin < mouseY) && (mouseY < UnitYmax)) {
          //check which unit is clicked and according to that check movement range, health, etc

          OrigX = int((UnitXmin - Xmovement)/scaleX);
          OrigY = int((UnitYmin - Ymovement)/scaleY);

          if (mapunitspecifics[OrigX][OrigY] == 1) {
            //movement range = 5
            range = 5;
          }

          //tiles till edge
          tteImax = 78 - OrigX; //bottom edge?
          tteJmax = 76 - OrigY; //right edge?
          tteImin = OrigX - 78; //top edge?
          tteJmin = OrigY - 76; //left edge?

          if (tteImax > range) {
            tteImax = range + 1;
          }
          if (tteJmax > range) {
            tteJmax = range + 1;
          }
          if (tteImin < -range) {
            tteImin = -range;
          }
          if (tteJmin < -range) {
            tteJmin = -range;
          }

          for (int i = tteImin; i < tteImax; i++) {
            for (int j = tteJmin; j < tteJmax; j++) {

              if ((abs(i + j) <= range) && (abs(i*cos(135) + j) <= 5)) {
                mapHighlights[OrigX + i][OrigY + j] = 1;
              }
            }
          }
        }
      }
    }
  }
}

This seems to work for units that aren’t near the edges. However, when there’s a unit near the edge, it cuts of a few tiles, resulting in the following:

mapHighlights[][] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
{0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0}
{0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0}
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}
{1, 1, 1, 1, 1, 1 /*unit position*/, 1, 1, 1, 1, 1}
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0}
{0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0}
}

I managed to solve the problem by adding 1 to the right and bottom (still keeping the edge into consideration), but now near the edge this happens… Basically the top gets cut off, and I currently have no idea why.