Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basemodule listObservableTiles performance #3

Open
AODtorusan opened this issue Aug 9, 2013 · 4 comments
Open

Basemodule listObservableTiles performance #3

AODtorusan opened this issue Aug 9, 2013 · 4 comments

Comments

@AODtorusan
Copy link

The method listObservableTiles in basemodule, checks each tile if it is within the viewrange. The performance can be significantly increased simply listing the tiles within a fixed range as follows:

visible = [ ]
for each -N ≤ Δu ≤ N:
    for each max(-N, -Δu-N) ≤ Δy ≤ min(N, -Δu+N)
        Δv = -Δu-Δy
        visible.append(origin + (Δu, Δv) )

Where N is the view range.

Adapted from http://www.redblobgames.com/grids/hexagons/#range

@lhunath
Copy link
Member

lhunath commented Aug 13, 2013

are you sure dy shouldn't be dv?

@AODtorusan
Copy link
Author

No, it should be correct. dy is part of the cubic coordinates, the others are x = u and z = v (y=-u-v).

I inlined these conversions as you don't use cubic coordinates, see the link.

@lhunath
Copy link
Member

lhunath commented Aug 13, 2013

Any chance you can compress that by including the dv into the second loop?

@AODtorusan
Copy link
Author

I think you can also do the following way:

visible = [ ]
for each -N ≤ Δu ≤ N:
    for each max(-Δu, 0) - N ≤ Δv ≤ min(-Δu, 0) + N
        visible.append(origin + (Δu, Δv) )

I quickly check it in mathematica with the following code (and it seems to work):

For[\[Delta]u = -n, \[Delta]u <= n, \[Delta]u++,
 For[\[Delta]v = Max[-\[Delta]u, 0] - n, \[Delta]v <= 
   Min[-\[Delta]u, 0] + n, \[Delta]v++, {
   x = \[Delta]u;
   y = -\[Delta]u - \[Delta]v;
   z = \[Delta]v;
   r = (Abs[x] + Abs[y] + Abs[z])/2;
   (*Print \[Delta]u, \[Delta]v, distance from (0,0) *)
   Print[{\[Delta]u, \[Delta]y, r}]
   }]
 ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants