Script to detect number keypad input

Runes of Magic/Radiant Arcana (http://www.runesofmagic.com)
Post Reply
Message
Author
noobbotter
Posts: 527
Joined: Fri Aug 31, 2012 1:15 pm

Script to detect number keypad input

#1 Post by noobbotter » Wed Mar 13, 2013 11:02 pm

I'm working on a script that I can use while running one character (who will be a party leader), with a different script for party member, where I can go questing with the lead and the follower bot will follow, accept quests I accept, turn in quests, etc..

I've got the basic communications figured out (I think... not able to test it yet) but I need to start out by figuring out how I can have a script detect when I press the keypad # 1, #2, #3, etc..., just like the createpath.lua file does.

Because that file does it, I thought I'd use it as a template to get started but when I try to start it, I get the following error: "11:52pm - scripts/rom/classes/pawn.lua:491: attempt to call global 'GetIdName' (a nil value)"

I'm not sure what function I'm calling that's trying to use that, or if it's possible to do the things in this lua file that I want to do. Here's what I have so far (I copied the createpath.lua file and tried to edit it as best I could):

Code: Select all

include("database.lua");
include("addresses.lua");
include("classes/player.lua");
include("classes/camera.lua");
include("classes/waypoint.lua");
include("classes/waypointlist.lua");
include("classes/waypointlist_wander.lua");
include("classes/node.lua");
include("settings.lua");
include("functions.lua");
include("macros.lua");
print("load settings");
settings.load();
database.load();

-- ********************************************************************
-- Change the parameters below to your need                           *
-- ********************************************************************
-- if you want to create waypoint files with special waypoint types
-- like type=TRAVEL, than you can change the global variables
-- below to your need, see the following example
-- p_wp_gtype = " type=\"TRAVEL\"";	-- global type for whole file
-- p_wp_type = " type=\"TRAVEL\"";	-- type for normal waypoints
-- p_hp_type = " type=\"TRAVEL\"";	-- type for harvest waypoints
p_wp_gtype = "";	-- global type for whole file: e.g. TRAVEL
p_wp_type = "";		-- type for normal waypoints
p_hp_type = "";		-- type for harvest waypoints
p_harvest_command = "player:harvest();";
p_merchant_command = "player:merchant(\"%s\");";
p_targetNPC_command = "player:target_NPC(\"%s\");";
p_choiceOption_command = "sendMacro(\"ChoiceOption(%d);\");";
p_mouseClickL_command = "player:mouseclickL(%d, %d, %d, %d);";
-- ********************************************************************
-- End of Change parameter changes                                    *
-- ********************************************************************


setStartKey(settings.hotkeys.START_BOT.key);
setStopKey(settings.hotkeys.STOP_BOT.key);

mntKey = key.VK_NUMPAD1;	-- tell bots to mount
folKey = key.VK_NUMPAD2;	-- tell bots to follow
aqKey = key.VK_NUMPAD3;		-- tell bots to Accept Quest
cqKey = key.VK_NUMPAD4;		-- tell bots to Complete Quest



-- read arguments / forced profile perhaps
local forcedProfile = nil;
for i = 2,#args do

	local foundpos = string.find(args[i], ":", 1, true);
	if( foundpos ) then
		local var = string.sub(args[i], 1, foundpos-1);
		local val = string.sub(args[i], foundpos+1);

		if( var == "profile" ) then
			forcedProfile = val;
		else
			-- invalid option
			local msg = sprintf(language[61], args[i]);
			error(msg, 0 );
		end
	end

	-- check the options
	if(not foundpos  and  args[i] ~= "update" ) then
		local msg = sprintf(language[61], args[i]);
		error(msg, 0 );
	end;

end

local wpList = {};
print("loading pointers");
local playerPtr = memoryReadIntPtr(getProc(), addresses.staticbase_char, addresses.charPtr_offset);
player = CPlayer(playerPtr);
player:update();

-- convert player name to profile name and check if profile exist
local load_profile_name;	-- name of profile to load
if( forcedProfile ) then
	load_profile_name = convertProfileName(forcedProfile);
else
	load_profile_name = convertProfileName(player.Name);
end

attach(getWin());
settings.loadProfile(load_profile_name);

--======================= function to run when accepting a quest===============================--
function waitForQuestAccept() 
	sendpartychat("getquest");
	eventParty();
	repeat
		yrest(100);
	until msg == "ready"
	yrest(1000);
	eventParty("stop");
	RoMScript("SendSystemChat('Safe to get the quest now.');");
	EventMonitorStart("newquestid", "ADDNEW_QUESTBOOK");
	local QuestStarted = false;
	repeat
		yrest(500); -- check every half second
		local questid = EventMonitorCheck("newquestid", "1");
		if questid then
			quest = questlog:getQuest(questid);
			QuestStarted = true;
			acceptquest = quest.Name; -- now I have the quest name of the quest I just accepted. 
			break
		end;
	until QuestStarted == true
	sendpartychat(acceptquest); 
	EventMonitorStop("newquestid");
end


function main()
	print("starting main function");
	local running = true;
	while(running) do

		local hf_x, hf_y, hf_wide, hf_high = windowRect( getWin());
		cprintf(cli.turquoise, language[42], hf_wide, hf_high, hf_x, hf_y );	-- RoM windows size
		cprintf(cli.green, language[501]);	-- RoM waypoint creator\n
		printf("Mount up	" .. getKeyName(mntKey) .. "		-- tells bot to get mounted");
		printf("Follow		" .. getKeyName(folKey) .. "		-- tells bot to follow leader");
		printf("Accept Quest	" .. getKeyName(aqKey) .. "	-- tells bot to accept quest");
		printf("Complete Quest	" .. getKeyName(cqKey) .. "	-- tells bot to Complete Quest");
		
		attach(getWin())
		
		local hf_key_pressed, hf_key;
		while(true) do

			hf_key_pressed = false;

			if( keyPressed(mntKey) ) then	-- mount key pressed
				hf_key_pressed = true;
				hf_key = "MNT";
			end;
			if( keyPressed(folKey) ) then	-- follow key pressed
				hf_key_pressed = true;
				hf_key = "FOL";
			end;
			if( keyPressed(aqKey) ) then	-- Accept Quest key pressed
				hf_key_pressed = true;
				hf_key = "AQ";
			end;
			if( keyPressed(cqKey ) ) then	-- Complete Quest key pressed
				hf_key_pressed = true;
				hf_key = "CQ";
			end;
						
			if( hf_key_pressed == false and 	-- key released, do the work
				hf_key ) then					-- and key not empty

				player:update();

				
				
				if( hf_key == "MNT" ) then			-- mount command
					print("pressed key 1 to mount");
					sendpartychat("mount");
					if not player.Mounted then
						if inventory:itemTotalCount(207204) >= 15 then
							if (useGoodie) then
								useGoodie("riding");
							end;
						end;
						player:mount();
					end;
				elseif(	hf_key == "FOL") then			-- follow command
					print("pressed key 2 to follow");
					sendpartychat("follow");
				elseif( hf_key == "AQ" ) then	
					RoMScript("SendWarningMsg('Wait for acknowledgement before accepting quest.');");
					print("pressed key 3 to accept quest"); 	-- Accept quest command
					waitForQuestAccept();
				elseif( hf_key == "CQ" ) then 			-- Complete quest command
					
				end;


				hf_key = nil;	-- clear last pressed key
			end;

			yrest(10);
		end; -- End of: while(true)
	end; -- End of: while(running)
end

startMacro(main, true);

If someone can point me in the right direction, that would be great. Thanks.

User avatar
lisa
Posts: 8332
Joined: Tue Nov 09, 2010 11:46 pm
Location: Australia

Re: Script to detect number keypad input

#2 Post by lisa » Wed Mar 13, 2013 11:44 pm

I would probably look at doing this as a WP with all the code in the onload, rather than creating a .lua and then adding in all the usual bot stuff to it.

you can use this as an example.

Code: Select all

<?xml version="1.0" encoding="utf-8"?><waypoints type="TRAVEL">
	<onLoad><![CDATA[
	--=== key usage ===--
	--=== fly  		NUMPAD1 ===--
	--=== flyoff  	NUMPAD2 ===--
	--=== speed  	NUMPAD4 ===--
	--=== speedoff  NUMPAD5 ===--
	
cprintf(cli.blue, "Press numpad 1 to fly\n")
cprintf(cli.blue, "Press numpad 2 to not fly\n")
cprintf(cli.green, "Press numpad 4 to run fast\n")
cprintf(cli.green, "Press numpad 5 to walk normal speed\n")
cprintf(cli.red, "Press Ctrl + L to exit\n")
	
	
	local delay = 1 -- time between key presses.
	local time = os.time()
	while(true) do
		yrest(10)
		if keyPressed(key.VK_NUMPAD1) and (os.time() - time > delay ) then
			fly()
			time = os.time()
		end
		if keyPressed(key.VK_NUMPAD2) and (os.time() - time > delay ) then
			flyoff()
			time = os.time()
		end
		if keyPressed(key.VK_NUMPAD4) and (os.time() - time > delay ) then
			keepSpeed()
			time = os.time()
		end		
		if keyPressed(key.VK_NUMPAD5) and (os.time() - time > delay ) then
			speed("off")
			time = os.time()
		end	
		if keyPressed(key.VK_NUMPAD6) and (os.time() - time > delay ) then
			speed(70)
			time = os.time()
		end	
	end
	]]></onLoad>
</waypoints>
Nothing to fancy and when a key is pressed it does the code for it, the time delay is because without it you would press a key once and it would do the code 20 times, so this means you have a 1 second gap between key press code.
Remember no matter you do in life to always have a little fun while you are at it ;)

wiki here http://www.solarstrike.net/wiki/index.php?title=Manual

noobbotter
Posts: 527
Joined: Fri Aug 31, 2012 1:15 pm

Re: Script to detect number keypad input

#3 Post by noobbotter » Thu Mar 14, 2013 5:38 am

That's exactly what I needed to know. Thanks Lisa! I'll give it a try tonight.

noobbotter
Posts: 527
Joined: Fri Aug 31, 2012 1:15 pm

Re: Script to detect number keypad input

#4 Post by noobbotter » Thu Mar 14, 2013 8:12 am

Quick question, I've been curious about this but never asked. In an onload section, what exactly does having the code inside the <![CDATA[ tag do? What are the differences in the type of code you can put inside the <![CDATA[ tag versus what you can do without it? Thanks.

User avatar
Ego95
Posts: 564
Joined: Tue Feb 28, 2012 12:38 pm
Contact:

Re: Script to detect number keypad input

#5 Post by Ego95 » Thu Mar 14, 2013 3:45 pm

Quick question, I've been curious about this but never asked. In an onload section, what exactly does having the code inside the <![CDATA[ tag do? What are the differences in the type of code you can put inside the <![CDATA[ tag versus what you can do without it? Thanks.
That's one thing I want to know too. I thought it's just for changing the colour :D
I would probably look at doing this as a WP with all the code in the onload
When I do this I always get the error, that I don't have any waypoints. I've tried it with a weapon leveling script which was working for the poster, but not for me.

AlterEgo95

User avatar
lisa
Posts: 8332
Joined: Tue Nov 09, 2010 11:46 pm
Location: Australia

Re: Script to detect number keypad input

#6 Post by lisa » Thu Mar 14, 2013 10:25 pm

noobbotter wrote:Quick question, I've been curious about this but never asked. In an onload section, what exactly does having the code inside the <![CDATA[ tag do? What are the differences in the type of code you can put inside the <![CDATA[ tag versus what you can do without it? Thanks.
Honestly it has something to do with the type of data, xml and lua are very different, so I am guessing the CDATA is telling MM that the stuff inside the tag isn't in XML format but is probably LUA format.
I never really looked into it exactly I just add it in because it works when I do ;)
AlterEgo95 wrote:When I do this I always get the error, that I don't have any waypoints. I've tried it with a weapon leveling script which was working for the poster, but not for me.
You need to keep it in a never ending loop, if it ever leaves the loop it will look for waypoints and if there arn't any then it will error.

Code: Select all

while(true) do

end
So aslong as you don't do a break or return it should stay in that loop.
Remember no matter you do in life to always have a little fun while you are at it ;)

wiki here http://www.solarstrike.net/wiki/index.php?title=Manual

noobbotter
Posts: 527
Joined: Fri Aug 31, 2012 1:15 pm

Re: Script to detect number keypad input

#7 Post by noobbotter » Thu Mar 14, 2013 11:23 pm

Yeah, I noticed that tonight when I tried it. In my onload, I have a while(true) do and all the code is inside that.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 3 guests