Loop logic: Noise dissapation
Posted: Thu Jan 20, 2011 12:00 pm
EDIT: I re-read my post, and essentially it ended up being long and confusing. What I'm wondering, is if there's a way to set up a loop structure that has dynamic limits, and omits previous itteration's changes?
Let's say I have x and y coordinates set up as values on a really large grid, and I have a function noise($x, $y, $power).
when noise($x, $y, $power) is called, ideally it should assign $power to tile_noise_level where tile_x and tile_y are equal to $x and $y...
and then loop, but this time grab all adjacent tiles, meaning from $x-1, $y-1 to $x+1, $y+1 (excluding $x, $y as it's already been established) and apply ($power--;) to tile_noise_level to those...
and then loop from $x-2, $y-2 to $x+2, $y+2....
as long as ($power > 0).
I thought of somehow creating a collection of loops for as long as ($power > 0) :
Code: Select all
[0,0]
Code: Select all
[-1,-1] [0,-1] [+1,-1]
[-1,0] [0,0] [+1,0]
[-1,+1] [0,+1] [+1, +1]
Code: Select all
[-2,-2]...
...[+2,+2]
But I can't quite seem to wrap my head around how to do that dynamically with loops. And, that method also hits ground zero each itteration.
I guess what I'm looking for is an efficiant way to apply a reduced $power each itteration to outside surrounding tiles.
This is what I have now and I'll explain why I think it could be better:
Code: Select all
function noise($x, $y, $range){
$counter_x = $x - $range;
$counter_y = $y - $range;
while($counter_x =< $x + $range){
while($counter_y =< $y + $range){
$current_string = "SELECT noise_level FROM map WHERE x ='" . $counter_x . "' AND y ='" . $counter_y . "'";
$current_result = mysql_query($current_string);
$current_data = mysql_fetch_array($current_result);
$query_string = "UPDATE map SET noise_level ='" . $current_data["noise_level"] + 1 . "' WHERE x ='" . $coutner_x . "' AND y = '" . $counter_y . "'";
$query_result = mysql_query($query_string);
$counter_y++;
}
$counter_y = $y;
$counter_x++;
$range--;
}
}Any suggestions would be awesome, I've been on this for the past 3 hours so I've probably lost my ability to look at it objectivly. Any critisism is appreciated!