Be a Supporter!

Multi Threading?

  • 348 Views
  • 14 Replies
New Topic Respond to this Topic
ScrumTurd
ScrumTurd
  • Member since: Sep. 10, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Multi Threading? 2012-11-19 09:39:45 Reply

I'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?

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Multi Threading? 2012-11-19 10:32:16 Reply

At 11/19/12 09:39 AM, ScrumTurd wrote: As far as I'm aware, flash does not support multi threading.

It does:
http://gotoandlearn.com/play.php?id=162

4urentertainment
4urentertainment
  • Member since: Aug. 1, 2008
  • Offline.
Forum Stats
Moderator
Level 13
Game Developer
Response to Multi Threading? 2012-11-19 10:33:29 Reply

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.

ScrumTurd
ScrumTurd
  • Member since: Sep. 10, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Multi Threading? 2012-11-19 10:52:38 Reply

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.

ScrumTurd
ScrumTurd
  • Member since: Sep. 10, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Multi Threading? 2012-11-19 11:07:39 Reply

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
}
}
}

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Multi Threading? 2012-11-19 11:12:55 Reply

Please use code tags when you're posting code.

Again, there are workers in As3 that let you run things in "parallel".

ScrumTurd
ScrumTurd
  • Member since: Sep. 10, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Multi Threading? 2012-11-19 11:16:25 Reply

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.

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Multi Threading? 2012-11-19 11:20:43 Reply

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.

ScrumTurd
ScrumTurd
  • Member since: Sep. 10, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Multi Threading? 2012-11-19 11:29:46 Reply

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.

ScrumTurd
ScrumTurd
  • Member since: Sep. 10, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Multi Threading? 2012-11-19 11:37:47 Reply

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"?

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Supporter
Level 05
Game Developer
Response to Multi Threading? 2012-11-19 12:48:33 Reply

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

BBS Signature
ScrumTurd
ScrumTurd
  • Member since: Sep. 10, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Multi Threading? 2012-11-19 13:12:58 Reply

Wahey, sarcasm never gets old.
Except on newgrounds.
When it did.
Several years ago.

egg82
egg82
  • Member since: Jun. 24, 2006
  • Offline.
Forum Stats
Supporter
Level 05
Game Developer
Response to Multi Threading? 2012-11-19 13:21:13 Reply

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

BBS Signature
milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to Multi Threading? 2012-11-19 15:37:18 Reply

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.

ScrumTurd
ScrumTurd
  • Member since: Sep. 10, 2012
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Multi Threading? 2012-11-20 04:03:43 Reply

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.