Problem with expressing ranges in switch{}

Started by bx83, Sat 17/07/2021 08:22:37

Previous topic - Next topic

bx83

I wrote this function:

Code: ags
int GetRowRange(int y)
{
  Display("provided with %d",y);
  switch (y) {    
    case (y>=0 && y<=50):    return 0; break;//return x/row
    case (y>=51 && y<=100):  return 1; break;
    case (y>=101 && y<=200): return 2; break;
    case (y>=201 && y<=300): return 3; break;
    case (y>=301 && y<=400): return 4; break;
    case (y>=401 && y<=500): return 5; break;
    case (y>=501 && y<=600): return 6; break;
    case (y>=601 && y<=700): return 7; break;
    case (y>=701 && y<=800): return 8; break;
    case (y>=801 && y<=900): return 9; break;
  }
}


I plugged in y=156.
It goes through every case, doesn't pick any, and returns 0 - instead of 2, which is what it should return. Do we have ranges? Am I using it properly?

ps I added redundant break;s to make absolutely sure it stopped after the return statement.

Crimson Wizard

#1
This is not how switches work. They do not test for the condition, they calculate whatever is in "case" and match it with an expression in the switch header;
case is only executed when the tested value equals the one in switch.

https://adventuregamestudio.github.io/ags-manual/ScriptKeywords.html#switch-case-statements

If you want big ranges, use if/else if.

Crimson Wizard

#2
Ah.... Also, I completely missed the fact that you are calculating a row index of Y coordinate.

All the above may perhaps be replaced with this:

Code: ags

int GetRowRange(int y)
{
     return y / 100;
}


It's interesting that you have this in a comment
Quote//return x/row

This is practically a correct formula, assuming "row" means row height

PS. The only thing I don't understand is, why in the original code the first two rows have range of 50, and others range of 100...

bx83

#3
Because:
a) I needed to redo implementation, it's stuffed
b) I have trouble remembering left/right, x/y, row/column etc - I usually need lots of visual clues burnt into my brain to be able to nicely record stuff about these hither/thither binaries. Is 'x' a row or column? My brain just flips back and forth until I draw a picture.


Code: ags
//get the X-AXIS COLUMN
int GetColRange(int x)
{
  Display("x=%d",x);
  if (x>=0 && x<=50)    return 0;
  if (x>=51 && x<=100)  return 1;
  if (x>=101 && x<=150) return 2;
  if (x>=151 && x<=200) return 3;
  if (x>=201 && x<=250) return 4;
  if (x>=251 && x<=300) return 5;
  if (x>=301 && x<=350) return 6;
  if (x>=351 && x<=400) return 7;
  if (x>=401 && x<=450) return 8;
  if (x>=451 && x<=500) return 9;  
}

//get the Y-AXIS ROW
int GetRowRange(int y)
{
  Display("y=%d",y);
  if (y>=0 && y<=50)    return 0;
  if (y>=51 && y<=100)  return 1;
  if (y>=101 && y<=150) return 2;
  if (y>=151 && y<=200) return 3;
  if (y>=201 && y<=250) return 4;
  if (y>=251 && y<=300) return 5;
  if (y>=301 && y<=350) return 6;
  if (y>=351 && y<=400) return 7;
}



Crimson Wizard

Quote from: bx83 on Sat 17/07/2021 13:17:31
b) I have trouble remembering left/right, x/y, row/column etc - I usually need lots of visual clues burnt into my brain to be able to nicely record stuff about these hither/thither binaries. Is 'x' a row or column? My brain just flips back and forth until I draw a picture.

x and y are whatever you decide :) script does not care because it's just same math, so it's all up to you.

But yeah, traditionally X is columns (left -> right) and Y is rows (top -> bottom).

But the math is really primitive. It's just better to put good names to the functions to help tell what they do

Code: ags

#define COL_WIDTH_PIXELS 50
#define ROW_HEIGHT_PIXELS 50


// get column index from X coord
int GetColFromX(int x)
{
     return x / COL_WIDTH_PIXELS;
}

// get row index from Y coord
int GetRowFromY(int y)
{
     return y / ROW_HEIGHT_PIXELS;
}

// get x1 (left side) of a column
int GetColX1(int col)
{
     return col * COL_WIDTH_PIXELS;
}

// get x2 (right side) of a column
int GetColX2(int col)
{
     return col * COL_WIDTH_PIXELS + COL_WIDTH_PIXELS;
}

// get y1 (top side) of a row
int GetRowY1(int row)
{
     return row * COL_HEIGHT_PIXELS;
}

// get y2 (bottom side) of a row
int GetRowY2(int row)
{
     return row * COL_HEIGHT_PIXELS + COL_HEIGHT_PIXELS;
}


That's it really.

By the way, I recall now there's a module by fernewelten for creating rectangular and hexagonal grids in the game, maybe you could also use that if you find making one difficult:
https://www.adventuregamestudio.co.uk/forums/index.php?topic=57931.0

bx83



Or that. Time to redesign the game for the 5th time :/
At least it's solid now and matches the text-books I just looked up...


SMF spam blocked by CleanTalk