Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Help? (game programming)  (Read 2278 times)

SolarShado

  • Bay Watcher
  • Psi-Blade => Your Back
    • View Profile
Help? (game programming)
« on: November 28, 2008, 08:33:56 pm »

I'm working on a turn-based strategy game, my first. I haven't decided whether to allow diagonal movement, and was trying to write a method to return all tiles within range n of a given point. The version that allows diagonals was easy, but I can't figure out how to get them if diagonals aren't allowed. To clarify:
Diagonals allowed looks like this:
-----
-***-
-*+*-
-***-
-----

Diagonals not allowed looks like this:
-----
--*--
-*+*-
--*--
-----

+ = point , * = in range , - = out of range

I'm writing in Java, so actual Java code would be a big plus, but any tips are welcome.
Logged
Avid (rabid?) Linux user. Preferred flavor: Arch

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Help? (game programming)
« Reply #1 on: November 28, 2008, 08:43:21 pm »

x diffrence + y diffrence
(assuming you want one point to another)
Logged
Eh?
Eh!

duro

  • Bay Watcher
  • On Break
    • View Profile
Re: Help? (game programming)
« Reply #2 on: November 28, 2008, 08:52:00 pm »

No obstacles?

x diffrence + y diffrence
(assuming you want one point to another)

Yepp, given x_o and y_o as your origin of movement and x_t and y_t as the coordinates of the tile to test, then it's just every tile for which
abs(x_o - x_t) + abs(y_o - y_t) <= n
(Not Java, but who gives a shit.)
Logged
If slavery, barbarism and desolation are to be called peace, men can have no worse misfortune.

SolarShado

  • Bay Watcher
  • Psi-Blade => Your Back
    • View Profile
Re: Help? (game programming)
« Reply #3 on: November 28, 2008, 09:35:12 pm »

I've currently got code that calculates how many squares there are within range then runs a loop that many times. I was kinda wanting something i could put in the loop that'd calculate the points from only the origin and it's position in the array that will be returned.

As a highschool math geek, I'm sure there's an easier way but I feel compelled to cling to my loop.
Logged
Avid (rabid?) Linux user. Preferred flavor: Arch

Jonathan S. Fox

  • Bay Watcher
    • View Profile
    • http://www.jonathansfox.com/
Re: Help? (game programming)
« Reply #4 on: November 28, 2008, 10:44:59 pm »

That's doable. I don't know Java, but C++ is not too distant a cousin to Java, so hopefully this won't be too confusing -- and with any luck, it might even be legitimate Java code. ;)

Definitions:
RANGE -- the range you're looking outward
x -- x position of your unit
y -- y position of your unit
abs() -- function that returns the absolute value of the passed variable

Code: [Select]
//
// Loop through all points within range of your unit
//

// Scans from top to bottom along the square containing tiles within range
for(int dy=-RANGE;dy<=RANGE;dy++)
{
   // Scans from left to right along the square containing tiles within range
   for(int dx=-RANGE;dx<=RANGE;dx++)
   {
      // Skips tiles that are out of range if you can't move diagonally
      // (just remove this to support diagonal movement)
      if(abs(dx)+abs(dy)>RANGE)
      {
         continue;
      }

      // Your code for checking the point here
      // (The point within range you are checking this iteration is (x+dx, y+dy))
   }
}

Give this a shot. I haven't tested it, but hopefully you can see what I'm getting at, even if something is wrong. :)

Edit: Fixed off-by-one error in the loop controls.
« Last Edit: November 29, 2008, 06:26:38 pm by Jonathan S. Fox »
Logged

duro

  • Bay Watcher
  • On Break
    • View Profile
Re: Help? (game programming)
« Reply #5 on: November 28, 2008, 10:46:47 pm »

Okay, for n=0 you have 1 tile, and for every n+1 you get 4n tiles more, so you have a progression with n_0=1 and n_(a+1)=n_a+4(a+1), thus giving you n(a) = 1 + 4*( sum i=1..n of i ) and thus n(a) = 1 + 4 * (n * (n + 1) / 2).

int numberOfTiles(const int n) { return 1+2*(n*(n+1)); }

class coord {
  public int x;
  public int y; }


Okay, i assume you iterate in the archetypical C way, like
int tileCount=numberOfTiles(n);
for(int i=0;i<tileCount;i++) {
  coord position=getMeATile(origo,n,i);
  /* do something with position */ }


with

coord getMeATile(const coord origo,const int n,const int i) {
  // lazy iteration, am too tired, have the dumb
  const int tileCount=numberOfTiles(n);
  // origo
  if(i==(tileCount-1)/2) return new coord(origo);
  // not origo...
  coord result=new coord;
  // upper left of origo
  if(i<(tileCount-1)/2) {
    result.x=origo.x;
    result.y=origo.y-n;
    for(int j=0;j<i;j++) {
      if(j+1==(result.y-origo.y+n)*(result.y-origo.y+n)) {
        result.y++;
        result.x=origo.x-n+origo.y-result.y; }
      else result.x++; }
    return result; }
  // else
  coord mirrored=getMeATile(origo,n,tileCount-i-1);
  result.x=2*origo.x-mirrored.x;
  result.y=2*origo.y-mirrored.y;
  return result; }


Totally dumb, untested and stuff, but could work. :P Feel free to bash it.
« Last Edit: November 28, 2008, 10:57:49 pm by duro »
Logged
If slavery, barbarism and desolation are to be called peace, men can have no worse misfortune.

SolarShado

  • Bay Watcher
  • Psi-Blade => Your Back
    • View Profile
Re: Help? (game programming)
« Reply #6 on: November 28, 2008, 10:58:10 pm »

Thanks guys! Not sure i follow your suggestion duro, but I haven't looked at it that close yet. I'm not sure when I'll get back to the project, so it'll probably be a while before it test any of it.
Thanks again!
Logged
Avid (rabid?) Linux user. Preferred flavor: Arch