Monster Racer Rush
Select between 5 monster racers, upgrade your monster skill and win the competition!
4.23 / 5.00 3,881 ViewsBuild and Base
Build most powerful forces, unleash hordes of monster and control your soldiers!
3.93 / 5.00 4,634 ViewsI'm using a recursive function to generate fractal terrain and I want to draw some maps that take longer than 15 seconds to generate.
No I won't be doing this in any final product so there's no need to chew me out over the lag time.
As far as I'm aware, flash does not support multi threading.
What I want to do is stop the recursive function after say 1 second, store it's current progress and resume again at the next frame.
Any ideas how I might do this?
At 11/19/12 09:39 AM, ScrumTurd wrote: As far as I'm aware, flash does not support multi threading.
At 11/19/12 09:39 AM, ScrumTurd wrote: As far as I'm aware, flash does not support multi threading.
Right now, you're right. Although Adobe are apparently planning on supporting multi-threading in AS4 if I'm not mistaken.
What I want to do is stop the recursive function after say 1 second, store it's current progress and resume again at the next frame.
That's what I would do, and you basically summed it up. How you save its "state" just depends on what that means to your function.
So if I had a function that adds 50k tiles to make a map, but that loop times out, then I would store the number of tiles I placed and calculate when to start the next loop so it can pan out on multiple frames until I finish all my 50k tiles, adding maybe 100 per frame.
So maybe post your recursive function and we may be able to help? Or a simplified version if it's too long/complicated.
So maybe post your recursive function and we may be able to help? Or a simplified version if it's too long/complicated.
private function diamond(x0,y0,x1,y1,x2,y2,x3,y3,len,max):void {
iterations++;
if(len < max){
var cx = ((x1 - x0) * .5) + x0;
var cy = ((y2 - y1) * .5) + y1;
var arr = [mapMatrix[y0][x0],
mapMatrix[y1][x1],
mapMatrix[y2][x2],
mapMatrix[y3][x3]];
mapMatrix[cy][cx] = average(arr) + rng(0,(max-len));
var tx = ((x1 - x0) * .5) + x0;
var ty = y1;
arr = [mapMatrix[y0][x0],
mapMatrix[y1][x1]];
mapMatrix[ty][tx] = average(arr) + rng(0,(max-len));
var lx = x0;
var ly = ((y2 - y0) * .5) + y0;
arr = [mapMatrix[y0][x0],
mapMatrix[y2][x2]];
mapMatrix[ly][lx] = average(arr) + rng(0,(max-len));
var bx = ((x1 - x0) * .5) + x0;
var by = y2;
arr = [mapMatrix[y2][x2],
mapMatrix[y3][x3]];
mapMatrix[by][bx] = average(arr) + rng(0,(max-len));
var rx = x1;
var ry = ((y2 - y0) * .5) + y0;
arr = [mapMatrix[y1][x1],
mapMatrix[y3][x3]];
mapMatrix[ry][rx] = average(arr) + rng(0,5);
len++;
if(iterations < 5000){
diamond(x0,y0,tx,ty,lx,ly,cx,cy,len,max);
diamond(tx,ty,x1,y1,cx,cy,rx,ry,len,max);
diamond(lx,ly,cx,cy,x2,y2,bx,by,len,max);
diamond(cx,cy,rx,ry,bx,by,x3,y3,len,max);
} else {
// store progress, break loop
}
}
}
I'm using the diamond square algorithm.
Except I put both the diamond and square into a single function
cx & cy = centre co-ordinate
tx & ty = top
lx & ly = left
bx & by = bottom
rx & ry = right
I think the relevant part is at the bottom so you can ignore the nasty details.
Iterations is stored in the class so it will accurately reflect how many passes have been made.
Sorry about the formatting, I don't know how to get that nice formatting that people put their code in.
Maybe this is better?
private function diamond(x0,y0,x1,y1,x2,y2,x3,y3,len,max):void {
iterations++;
if(len < max){
// calculate height of centre
// "" top co-ordinate
// left
// bottom
// right
len++;
if(iterations < 5000){
diamond(x0,y0,tx,ty,lx,ly,cx,cy,len,max);
diamond(tx,ty,x1,y1,cx,cy,rx,ry,len,max);
diamond(lx,ly,cx,cy,x2,y2,bx,by,len,max);
diamond(cx,cy,rx,ry,bx,by,x3,y3,len,max);
} else {
// store progress, break loop
}
}
}
Please use code tags when you're posting code.
Again, there are workers in As3 that let you run things in "parallel".
Please use code tags when you're posting code.
Please explain how, when I have clearly stated that I don't know.
Again, there are workers in As3 that let you run things in "parallel".
Please explain how, when I have clearly stated that I don't know.
At 11/19/12 11:16 AM, ScrumTurd wrote:Please use code tags when you're posting code.Please explain how, when I have clearly stated that I don't know.
It wasn't so clear actually, because of the mess.
Just look at the left of the text field that lets you enter your post
Again, there are workers in As3 that let you run things in "parallel".Please explain how, when I have clearly stated that I don't know.
That's why I posted a link.
Just look at the left of the text field that lets you enter your post
strong
em
ins
code
Right, got it.
That's why I posted a link.
Yeah, I don't think I'm gonna bother with all that.
I prefer 4urentertainment's solution.
Right, here we are:
private var iterations:uint = 0;
private function diamond(x0,y0,x1,y1,x2,y2,x3,y3,len,max):void {
iterations++;
if(len < max){
// calculate heights
len++;
if(iterations < 500){
diamond(x0,y0,tx,ty,lx,ly,cx,cy,len,max);
diamond(tx,ty,x1,y1,cx,cy,rx,ry,len,max);
diamond(lx,ly,cx,cy,x2,y2,bx,by,len,max);
diamond(cx,cy,rx,ry,bx,by,x3,y3,len,max);
} else {
// what do I do here?
}
}
}
Or more specifically...
How do I calculate the exact point to resume at using only the amount of iterations?
And would this negate all of the unfinished "parent iterations"?
yepp, i'm so using this.
if (!(SystemUtils.version[0] >= 11 && SystemUtils.version[1] >= 4)) {
messageText = Text.makeText("Lorimier", 0x000000, 83);
messageText.text = "Flash Player 11.4 or greater is needed";
messageText = Text.makePretty(messageText);
messageText.x = stage.stageWidth / 2 - messageText.textWidth / 2;
messageText.y = stage.stageHeight / 2 - messageText.textHeight / 2;
addChild(messageText);
return;
} Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P
Wahey, sarcasm never gets old.
Except on newgrounds.
When it did.
Several years ago.
At 11/19/12 01:12 PM, ScrumTurd wrote: Wahey, sarcasm never gets old.
Except on newgrounds.
When it did.
Several years ago.
what?
Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P
At 11/19/12 11:37 AM, ScrumTurd wrote: How do I calculate the exact point to resume at using only the amount of iterations?
And would this negate all of the unfinished "parent iterations"?
Turn the recursion into a regular loop, that should make it easier.
Sorry Egg, I thought you were takin the piss.
Cheers Milchreis, I think I'll store the new co-ordinates in an array and iterate a certain length of that array each frame.