package flash
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.utils.Dictionary;
public dynamic class Main extends MovieClip
{
public var cData:String;
public var modDataLoader:Class;
public var modSettingsLoader:Class;
public var Label:Class;
public var Spinner:Class;
private var current:int = 1;
private var playRec:Boolean = false;
private var recInd:Number = 0;
private var scripts:Array = new Array();
private var store:Array = new Array();
private var tickVal:Number = 1;
private const maxSpeed:Number = 3;
private const minSpeed:Number = 0;
private const tickSpeed:Number = 1/10;
private var loader;
private var mdl;
private var msl;
private var spinnerControl;
private var modPage:MovieClip;
var gfx;
public function initl(l):void
{
loader = l;
loadAuto();
gfx = l.loadedSWF.uiLayer.graphics;
l.addEnterFramePersist(tick);
l.unloadMod();
}
public function loadAuto():void
{
mdl = new modDataLoader ("Rec/" + current + ".txt",cData, dataLoaded);
mdl.addEventListener("dataNotFound",noDataFound);
}
public function loadPrev():void
{
playRec = true;
current = (current > 0) ? --current : scripts.length-1;
stringToAuto(scripts[current]);
loader.updateStatus("Rec: " + current);
}
public function loadNext():void
{
playRec = true;
current = (current < scripts.length -1) ? ++current : 0;
stringToAuto(scripts[current]);
loader.updateStatus("Rec: " + current);
}
public function toggleAuto():void
{
playRec = !playRec;
loader.updateStatus("Toggle: " + (playRec ? "enabled" : "disabled"));
}
public function speedDown():void
{
tickVal = Math.max(minSpeed, tickVal - tickSpeed);
loader.updateStatus("Speed: " + roundTo(tickVal,2));
}
public function speedUp():void
{
tickVal = Math.min(maxSpeed, tickVal + tickSpeed);
loader.updateStatus("Speed: " + roundTo(tickVal,2));
}
public function noDataFound(e):void
{
if(current != 1)
msl = new modSettingsLoader("AutoScript", settingsLoaded, cData);
current = -1;
}
public function settingsLoaded(e):void
{
var dict:Dictionary = e.settings;
if (dict["NextKey"] != null)
loader.registerFunctionPersist(loadNext, dict["NextKey"]);
if (dict["PrevKey"] != null)
loader.registerFunctionPersist(loadPrev, dict["PrevKey"]);
if (dict["SpeedDown"] != null)
loader.registerFunctionPersist(speedDown, dict["SpeedDown"]);
if (dict["SpeedUp"] != null)
loader.registerFunctionPersist(speedUp, dict["SpeedUp"]);
if (dict["Toggle"] != null)
loader.registerFunctionPersist(toggleAuto, dict["Toggle"] );
var l = new Label(300,70, "AutoScript.\nScripts Loaded: " + scripts.length);
l.textFormatting().align = "left";
l.updateFormatting();
spinnerControl = new Spinner(300,60);
spinnerControl.y = 70;
spinnerControl.lowerBound = 0;
spinnerControl.upperBound = scripts.length-1;
spinnerControl.tickValue = 1;
spinnerControl.addEventListener("spinnerChanged",
function sc(ev){
if(ev.alreadySelected && playRec) return;
current = ev.object;
playRec = true;
stringToAuto(scripts[current]);
loader.updateStatus("Rec: " + current);
drawAutoMouse(loader);
}
);
modPage = new MovieClip();
modPage.addChild(l);
modPage.addChild(spinnerControl);
loader.addPage(modPage, true);
loader.loadedSWF.removeEventListener(MouseEvent.MOUSE_MOVE, loader.mouseMoveHook);
//loader.loadedSWF.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHook);
}
public function roundTo(n:Number, p:int):Number
{
var pow = Math.pow(10,p);
return int(n*pow)/pow;
}
public function tick(l):void
{
if (playRec && store.length > 0 && ! loader.g.gamePaused)
{
if(loader.g.shiftDown || loader.g.controlLocked || loader.g.gamePaused) return;
recInd = (recInd + tickVal) % store.length;
var cInd:int = int(recInd);
var cDiff:Number = recInd - cInd;
var nInd:int = Math.ceil(recInd) % store.length;
var pX:Number = store[cInd].x;
var pY:Number = store[cInd].y;
if(cDiff < 1) //Interpolate the point if speed < 1.
{
var pX2:Number = store[nInd].x;
var pY2:Number = store[nInd].y;
pX = (pX2 - pX)*cDiff + pX;
pY = (pY2 - pY)*cDiff + pY;
}
if (loader.him.handOpen)
loader.loadedSWF.currentMousePos = new Point(loader.g.mirrored?700:0,pY);
else
loader.loadedSWF.currentMousePos = new Point(loader.g.mirrored?(700-pX):pX, pY);
drawAutoMouse(l);
}
else
{
loader.mouseMoveHook(new MouseEvent(MouseEvent.MOUSE_MOVE, true, false, mouseX, mouseY));
}
}
public function dataLoaded(e):void
{
mdl.removeEventListener("dataNotFound", noDataFound);
scripts.push(e.binData.toString());
current++;
loadAuto();
}
public function stringToAuto(s:String):void
{
store = new Array();
recInd = 0;
var dat = loader.standardLines(s);
for (var ind:int = 0; ind < dat.length; ind++)
{
var line = dat[ind];
if (line.length > 0)
store.push(new Point(line.split(";")[0], line.split(";")[1]));
}
drawAutoMouse(loader);
}
public function drawAutoMouse(l):void
{
var len = store.length;
var xPos = 600;
var wid = 700-xPos;
gfx.clear();
gfx.beginFill(0,5/10);
gfx.drawRect(xPos,296,wid,8);
gfx.endFill();
var startX = (store.length > 0) ? xPos + store[0].x*wid/700 : xPos+wid/2;
gfx.moveTo(startX, -30);
gfx.lineStyle(4, 0);
for(var j = 0; j < len; j++){
var i = (j + recInd+len/2)%len;
gfx.lineTo(xPos+store[int(i)].x*wid/700, xPos/Math.max(1,len-1)*j);
}
gfx.moveTo(startX, -30);
gfx.lineStyle(2, 0xFFFFFF);
for(var j = 0; j < len; j++){
var i = (j + recInd+len/2)%len;
gfx.lineTo(xPos+store[int(i)].x*wid/700, xPos/Math.max(1,len-1)*j);
}
}
}
}