Difference between revisions of "Liqwiid Wars/Algorithm"
Steaky1212 (talk | contribs) (←Created page with '==Algorithm== For those wondering the algorithm I am using to get the gradient can be defined as such... new_grid[x, y] = abs(position[0] - x) + abs(position[1] - y); 4 3 2 ...') |
Steaky1212 (talk | contribs) m (update algorithm) |
||
Line 1: | Line 1: | ||
+ | === Flood Fill Algorithm == | ||
+ | I have tried changing hte algorithm as the one previously used doesnt take walls into account. | ||
+ | I am now trying different algorithms including a modified flood(/seed/bucket) fill. | ||
+ | |||
+ | [CODE] | ||
+ | void bucket_fill(int x, int y, int value) | ||
+ | { | ||
+ | if (grid[x, y] == 0 || (grid[x,y]-value) > 0) | ||
+ | { | ||
+ | grid[x, y] = value; | ||
+ | if (x < map_size-1) | ||
+ | bucket_fill(x + 1, y, value+1); | ||
+ | if(x>0) | ||
+ | bucket_fill(x - 1, y, value+1); | ||
+ | if (y < map_size-1) | ||
+ | bucket_fill(x, y+1, value+1); | ||
+ | if(y>0) | ||
+ | bucket_fill(x, y-1, value+1); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | [/CODE] | ||
+ | This is gives the right result when used on a small map, but larger maps give stack overflows. I will try and use a queue instead, as this should be smaller. | ||
+ | |||
==Algorithm== | ==Algorithm== | ||
For those wondering the algorithm I am using to get the gradient can be defined as such... | For those wondering the algorithm I am using to get the gradient can be defined as such... |
Revision as of 15:57, 7 July 2009
= Flood Fill Algorithm
I have tried changing hte algorithm as the one previously used doesnt take walls into account. I am now trying different algorithms including a modified flood(/seed/bucket) fill.
[CODE] void bucket_fill(int x, int y, int value)
{ if (grid[x, y] == 0 || (grid[x,y]-value) > 0) { grid[x, y] = value; if (x < map_size-1) bucket_fill(x + 1, y, value+1); if(x>0) bucket_fill(x - 1, y, value+1); if (y < map_size-1) bucket_fill(x, y+1, value+1); if(y>0) bucket_fill(x, y-1, value+1);
} }
[/CODE] This is gives the right result when used on a small map, but larger maps give stack overflows. I will try and use a queue instead, as this should be smaller.
Algorithm
For those wondering the algorithm I am using to get the gradient can be defined as such...
new_grid[x, y] = abs(position[0] - x) + abs(position[1] - y);
4 3 2 3 4 5
3 2 1 2 w 4
2 1 X 1 w 3
3 2 1 2 w 4
4 3 2 3 4 5
However if a wall exists, any fighters on the right of the wall cant "escape" due to them ALWAYS going to an area of equal or lower potential.
Using...
new_grid[x, y] = max(abs(position[0] - x), abs(position[1] - y));
would give
2 2 2 2 2 3
2 1 1 1 w 3
2 1 X 1 w 3
2 1 1 1 w 3
2 2 2 2 2 3
With the wall in the same position the problem no longer arises. However "max" gives additional overheads, and the second option will give rise to this situation
2 2 2 w 2 3
2 1 1 1 w 3
2 1 X 1 2 w
2 1 1 1 2 3
2 2 2 2 2 3
where any army trapped in the "2" space above the wall is trapped there, until the cursor moves.
Either I am worrying about it too much, as the cursor wont stay in the same position for an amount of time (especially given the fact the IR will shake) added to the fact this will not affect a huge amount of fighters
-or-
I need to program some disobedience into the fighters such that very occasionally they will disregard orders and go in the wrong direction