User File #28439378095979691

Upload All User Files

#28439378095979691 - Auto Analog Input v1.9

AutoInput.lua
Game: Unknown Game ( N64, see all files )
707 downloads
Uploaded 1/24/2016 6:10 PM by TASeditor (see all 188)
Not finished. Be careful.
-- Auto analog input script written by TASeditor
-- Main window
-- This function runs after the user clicked on the Start button.
function Start()

	if PauseFlag == false
	then if StartFlag == false
		 then if forms.ischecked(PosCheck) and not forms.ischecked(AngCheck)
			  then CalcAngle();
				   FollowAngle = forms.gettext(AngFollow);
				   --savestate.saveslot(0);
				   StartFlag = true;
				   forms.settext(StatLabel, "Started");
				   -- if forms.ischecked(AdvCheck)
				   -- then AdvMode = true;
				   -- else AdvMode = false;
				   -- end;
			  elseif not forms.ischecked(PosCheck) and forms.ischecked(AngCheck)
				  then FollowAngle = forms.gettext(AngFollow);
					   savestate.saveslot(0);
					   StartFlag = true;
					   forms.settext(StatLabel, "Started");
				  elseif forms.ischecked(PosCheck) and forms.ischecked(AngCheck)
					  then forms.settext(StatLabel, "Error: Uncheck one checkbox");
					  
			  end;
		 end;
	end;
	
end;

-- This function runs after the user clicked on the Pause button.
function Pause()

	if StartFlag == true
	then if PauseFlag == false
		 then PauseFlag = true;
			  forms.settext(StatLabel, "Paused");
			  forms.settext(PauseButton, "Continue");
			  client.pause();
			  Xinput["P1 X Axis"] = 0;
			  Yinput["P1 Y Axis"] = 0;
			  joypad.setanalog(Xinput);
			  joypad.setanalog(Yinput);
		 else PauseFlag = false
			  forms.settext(StatLabel, "Started");
			  forms.settext(PauseButton, "Pause");
		 end;
	end;

end;

-- This function runs after the user clicked on the Stop button.
function Stop()

	if StartFlag == true
	then StartFlag = false;
		 PauseFlag = false;
		 forms.settext(StatLabel, "Stopped");
		 forms.settext(PauseButton, "Pause");
		 client.pause();
		 Xinput["P1 X Axis"] = 0;
		 Yinput["P1 Y Axis"] = 0;
		 joypad.setanalog(Xinput);
		 joypad.setanalog(Yinput);
		 X = 0;
		 Y = 0;
	end;

end;

-- This function runs after the user clicked the "+" button.
function Add()

	--AngF = math.floor((tonumber(forms.gettext(AngFollow)) + (tonumber(forms.gettext(AngAdd))*32768)/180) % 65536 + 0.5);
	--forms.settext(AngFollow, AngF);
	--BestDiff = 99999;
	console.writeline("not implemented");

end;

function CalcAngle()

	DeltaX = forms.gettext(XPosGoto) - memory.readfloat(XPosAddr, true);
	DeltaY = forms.gettext(YPosGoto) - memory.readfloat(YPosAddr, true);
	Distance = math.sqrt(DeltaX^2 + DeltaY^2);
	
	if FloatBool == true
	then Alpha = math.acos(DeltaX / Distance) * 180 / math.pi;
	else Alpha = math.floor(math.acos(DeltaX / Distance) * 32768/math.pi);
	end;
	forms.settext(AngFollow, Alpha);
		
end;

-- This function creates the main window.
function WindowForm()

	Window = forms.newform(300, 500, "Auto analog input");

	PosCheck = forms.checkbox(Window, "Go to position:", 5, 20); 
	forms.label(Window, "X =", 110, 10, 30, 15);
	XPosGoto = forms.textbox(Window, "0", 120, 20, nil, 140, 5);
	forms.label(Window, "Y =", 110, 40, 30, 15);
	YPosGoto = forms.textbox(Window, "0", 120, 20, nil, 140, 35);
	
	AngCheck = forms.checkbox(Window, "Follow angle:", 5, 75);
	forms.label(Window, "a =", 110, 80, 30, 15);
	AngFollow = forms.textbox(Window, "0", 120, 20, nil, 140, 75);
	
	AngAdd = forms.textbox(Window, "0", 80, 20, nil, 110, 110);
	forms.label(Window, "�", 190, 110, 10, 15);
	forms.button(Window, "+", Add, 205, 110, 20,23);
	
	forms.label(Window, "Status:", 5, 150, 40, 15);
	StatLabel = forms.label(Window, "Stopped", 45, 150, 200, 15);
	
	forms.button(Window, "Start", Start, 5, 180);
	PauseButton = forms.button(Window, "Pause", Pause, 105, 180);
	forms.button(Window, "Stop", Stop, 205, 180);
	
	forms.label(Window, "Information:", 5, 220, 70, 15);
	
	forms.label(Window, "Current angle:", 5, 240, 80, 15);
	CurrAngLabel = forms.label(Window, "", 85, 240, 40, 15);
	forms.label(Window, "Difference:", 130, 240, 60, 15);
	DiffCurrAngLabel = forms.label(Window, "", 200, 240, 40, 15);
	
	forms.label(Window, "Best angle:", 5, 260, 60, 15);
	BestAngLabel = forms.label(Window, "", 85, 260, 40, 15);
	forms.label(Window, "Difference:", 130, 260, 60, 15);
	DiffBestAngLabel = forms.label(Window, "", 200, 260, 40, 15);
	
	--OptCheck = forms.checkbox(Window, "Optimize", 5, 280);

end;

 -- Address window
-- This function checks wheter the user has typed in the memory addresses or not.
-- It doesn't check if the typed address is the correct one.
-- The "0x" should not be deleted.
function Check()
	
	-- Reading the addresses as a string.
	XPosAddr = forms.gettext(XPosAddrTxt);
	YPosAddr = forms.gettext(YPosAddrTxt);
	MovAngAddr = forms.gettext(MovAngAddrTxt);
	CamAngAddr = forms.gettext(CamAngAddrTxt);
	FloatBool = forms.ischecked(FloatCheck);
	
	if XPosAddr ~= "0x" and YPosAddr ~= "0x" and MovAngAddr ~= "0x" and CamAngAddr ~= "0x"
	then -- Converting the string into an integer number.
		 XPosAddr = tonumber(XPosAddr); 
	     YPosAddr = tonumber(YPosAddr);
	     MovAngAddr = tonumber(MovAngAddr);
		 CamAngAddr = tonumber(CamAngAddr);
		 
		 -- Writes the addresses into a text file.
		 -- The user doesn't have to type in the addresses everytime.
		 AddrFile = io.open(ROMname, "a");
		 AddrFile:write(XPosAddr, "\n", YPosAddr, "\n", MovAngAddr, "\n", CamAngAddr, "\n", tostring(FloatBool));
		 AddrFile:close();
		 
		 -- Closes the form where the user typed in the addresses.
		 forms.destroy(Addr);
		 WindowForm();
	end;
	
end;

-- This function creates the form where the user needs to type in memory addresses.
function AddrForm()

	Addr = forms.newform(320, 230, "Memory addresses");
	
	forms.label(Addr, "Type in horizontal position addresses:", 5, 5, 280, 20);
	forms.label(Addr, "X =",5, 30, 30, 15);
	XPosAddrTxt = forms.textbox(Addr, "0x", 70, 20, nil, 40, 25);
	forms.label(Addr, "Y =",120, 30, 30, 15); 
	YPosAddrTxt = forms.textbox(Addr, "0x", 70, 20, nil, 155, 25);
	
	forms.label(Addr, "Type in horizontal movement angle address:", 5, 55, 350, 20);
	forms.label(Addr, "a =", 5, 80, 30, 15); 
	MovAngAddrTxt = forms.textbox(Addr, "0x", 70, 20, nil, 40, 75);
	FloatCheck = forms.checkbox(Addr, "Float?", 120, 75);
	
	forms.label(Addr, "Type in horizontal camera angle address:", 5, 100, 340, 20);
	forms.label(Addr, "b =", 5, 125, 30, 15);
	CamAngAddrTxt = forms.textbox(Addr, "0x", 70, 20, nil, 40, 120);
	
	forms.button(Addr, "Done", Check, 5, 160);

end;

-- Reads out the memory addresses from the file, if there's content in the file.
-- The memory addresses are saved in decimal numbers.
-- The file is in the main BizHawk folder and is called "<romname>.txt".
-- The main window will open.

XPosAddr = nil;
YPosAddr = nil;
MovAngAddr = nil;
CamAngAddr = nil;
FloatBool = nil;
AddrFile = nil;

ROMname = gameinfo.getromname()..".txt";
AddrFile = io.open(ROMname, "r");

if AddrFile ~= nil
then XPosAddr = tonumber(AddrFile:read("*line"));
     YPosAddr = tonumber(AddrFile:read("*line"));
     MovAngAddr = tonumber(AddrFile:read("*line"));
	 CamAngAddr = tonumber(AddrFile:read("*line"));
	 FloatBool = (AddrFile:read("*line"));
	 WindowForm(); 
	 AddrFile:close();
end;
 
-- If there's no content in the file a window will open, where the user types in the memory addresses once.
if XPosAddr == nil and YPosAddr == nil and MovAngAddr == nil and CamAngAddr == nil and FloatBool == nil
then AddrForm();
	 -- Prevents crash.
	 XPosAddr = 0;
	 YPosAddr = 0;
	 MovAngAddr = 0;
	 CamAngAddr = 0;
	 FloatBool = false;
end



--**************************************************************************************************--
--Brute force script																				--
--**************************************************************************************************--

Xinput = {};
Yinput = {};

StartFlag = false;
PauseFlag = false;
X = 0; Y = 0;
CamAngle = 0;
InputAngle = 0;
Radius = 127;

-- function Rotate()

	-- if Direction == "right"
	-- then X = X + 1;
		 -- if X >= 127
		 -- then Direction = "down";
			  -- X = 127;
		 -- end;
	-- elseif Direction == "down"
		-- then Y = Y - 1;
			-- if Y <= -128
			-- then Direction = "left";
				 -- Y = -128;
			-- end;
		-- elseif Direction == "left"
			-- then X = X - 1;
				-- if X <= -128
				-- then Direction = "up"
					 -- X = -128;
				-- end;
			-- elseif Direction == "up"
				-- then Y = Y + 1;
					-- if Y >= 127
					-- then Direction = "right";
						 -- Y = 127;
					-- end;
				-- end;

-- end

function CreateInput()

	if FloatBool == "true"
	then InputAngle = ((FollowAngle - CamAngle - 90) % 360)*math.pi/180; --console.writeline("Flt InA"..InputAngle);
	else InputAngle = ((FollowAngle - CamAngle - 49152) % 65536)*math.pi/32768; --console.writeline("Int InA"..InputAngle);
	end; 
	
	X = math.floor(math.cos(InputAngle)*Radius+0.5); --console.writeline("X"..X);
	Y = math.floor(math.sin(InputAngle)*Radius+0.5); --console.writeline("Y"..Y);
	
	InputAngleInt = math.atan2(Y, X); --console.writeline(InputAngle.." "..InputAngleInt);
	
end;

function BruteForce()

	XPosition = memory.readfloat(XPosAddr, true);
	YPosition = memory.readfloat(YPosAddr, true);
	if FloatBool == "true"
	then MovAngle = memory.readfloat(MovAngAddr, true);
		 CamAngle = memory.readfloat(CamAngAddr, true);
	else MovAngle = memory.read_u16_be(MovAngAddr);
		 CamAngle = memory.read_u16_be(CamAngAddr);
	end;
	
	CreateInput();


	
	Xinput["P1 X Axis"] = X;
	Yinput["P1 Y Axis"] = Y;
	joypad.setanalog(Xinput);
	joypad.setanalog(Yinput);

	
	
end;

while true do
	
	if StartFlag and not PauseFlag and not emu.islagged()
	then BruteForce();
	end;
	
	emu.frameadvance();
end;