/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/naJp
*/
package {
import flash.text.TextField;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
// write as3 code here..
var i:int;
var k:int;
var a:xZone;
var p:xPort;
vecZone = new Vector.<xZone>(0, false);
for (i = 0; i < 4; i++)
{
for (k = 0; k < 4; k++)
{
a = new xZone();
a.cx =64+ k * 64 + Math.random()*16;
a.cy =64+ i * 64 + Math.random() *16;
vecZone.push(a);
}//nextk
}//nexti
for (i = 0; i < 4; i++)
{
for (k = 0; k < 4; k++)
{
a = vecZone[k + (i*4)];
if (k == 1 && i == 1) { continue; }
if (k != 3)
{
p = new xPort();
p.dest = vecZone[k+1 + (i*4)];
a.vecPort.push(p);
p.nx = 1;
p = new xPort();
p.dest = a;
p.nx = -1;
vecZone[k+1 + (i*4)].vecPort.push(p);
}
if (i != 3)
{
p = new xPort();
p.dest = vecZone[k+ (i*4)+4];
a.vecPort.push(p);
p.ny = 1;
p = new xPort();
p.dest = a;
vecZone[k + (i*4)+4].vecPort.push(p);
p.ny = -1;
}
}//nextk
}//nexti
deb = new TextField();
deb.width = 320;
deb.height = 240;
deb.mouseEnabled = false;
addChild(deb);
// deb.text = "reach";
onEnter(null);
// stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var vecZone:Vector.<xZone>;
public function onEnter(e:Event):void
{
graphics.clear();
graphics.lineStyle(2, 0);
var i:int;
var num:int;
var a:xZone;
var np:int;
var vp:Vector.<xPort>;
var p:xPort;
var k:int;
num = vecZone.length;
for (i = 0; i < num; i++)
{
a = vecZone[i];
graphics.drawCircle(a.cx, a.cy, 8);
vp = a.vecPort;
np = vp.length;
for (k = 0; k < np; k++)
{
p = vp[k];
if (p.dest == null) {continue; }
graphics.moveTo(a.cx, a.cy);
graphics.lineTo(p.dest.cx, p.dest.cy);
}//nextk
}//nexti
curFrame += 1;
graphics.lineStyle(2, 0xFF0000);
var test:xStack;
test = new xStack();
test.next = null;
test.port = null;
prevent = 0;
// deb.text = "reach2";
//ok now it visits all nodes, now i just need
//to prevent the infinite loop somehow
//(for now its just using an int (prevent) to count how many times the func is called)
checkZone(vecZone[0], 0, test);
deb.text = " "+prevent;
}//onenter
public var curFrame:int = 0;
public var prevent:int = 0;
public var deb:TextField;
public function checkZone(xz:xZone, d:int, st:xStack):void
{
//if (d > 16) { return; } //debug -- trying to preven infinite loop
prevent += 1;
if (prevent >= 512) { return; }
if (prevent >= 256) { return; }
if (prevent >= 128) { return; }
//if (prevent >= 32) { return; }
//if the first node is drawn more than once
graphics.drawCircle(xz.cx, xz.cy, 4+d); //4 +prevent); // 16+d);
//if (xz.frame == curFrame && xz.d < d) { return; }
xz.frame = curFrame;
// xz.d = d;
var i:int;
var num:int;
var p:xPort;
var vec:Vector.<xPort>;
var k:xStack;
var dot:Number;
vec = xz.vecPort;
num = vec.length;
for ( i = 0; i < num; i++)
{
p = vec[i];
//if (p.visit == curFrame) { continue; }
//p.visit = curFrame;
if (p.dest == null) { continue; }
k = st;
for (; k!=null; k = k.next)
{
if (k.port == p) { break;}
}
if (k!=null) { continue; } //continue i loop
//if (k.port !=null && k.port==p) { continue; }
//if (k) { return; }
if (st.port != null )
{
dot = (p.nx * st.port.nx) + (p.ny *st.port.ny);
//deb.text = "d " + dot;
if (dot > 0) { continue; }
}
var ns:xStack;
ns = new xStack();
ns.next = st;
ns.port = p;
checkZone(p.dest, d+1, ns);
}//nexti
}//checkzone
}//classend
}
internal class xZone
{
public var cx:Number = 0;
public var cy:Number = 0;
public var vecPort:Vector.<xPort> = new Vector.<xPort>(0, false);
public var d:int = 0;
public var frame:int = 0;
}//xzone
internal class xPort
{
public var dest:xZone = null;
public var visit:int = 0;
public var nx:Number = 0;
public var ny:Number = 0;
}//xport
internal class xStack
{
public var next:xStack = null;
public var port:xPort = null;
public var d:int = 0;
}//xstack