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!