Help debugging this plugin?

Posted by forral on Sun 02 Dec 2012 12:51 AM — 9 posts, 32,901 views.

USA #0
Greetings,

I'm having issues trying to figure out where in this plugin my issues are arising. The issue is that when I press "2" on my keypad (which corresponds to south), the plugin sends the "up" command, and tries to open a non-existent door in the "up" direction. This spams repeatedly until I manually enter the direction.

The only workaround I've seen is for me to manually type (using letters) the directions I want, and the script will sometimes kick in.

To clarify, the plugin I'm using is a simple auto door-opening plugin. I've looked over the code from top to bottom and can see the section that is causing me issues, but can't find fault in any of the code.

Can anybody help?

As the code for the plugin isn't that long, I'll just post it below:


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Thursday, January 26, 2012, 5:34 PM -->
<!-- MuClient version 4.79 -->

<!-- Plugin "MM_Auto_Open_Doors" generated by Plugin Wizard -->

<muclient>
<plugin
   name="MM_Auto_Open_Doors"
   author="Forral"
   id="130ffba77eaf284e58979da5"
   language="Lua"
   purpose="Automatically opens doors"
   save_state="y"
   date_written="2012-01-26 17:33:26"
   requires="4.00"
   version="1.0"
   >

</plugin>


<!--  Get our standard constants -->

<include name="\MUSHClient\worlds\plugins\constants.lua"/>

<!--  Triggers  -->

<triggers>
  <trigger
   enabled="y"
   group="script-autodoor"
   keep_evaluating="y"
   match="^(.+)\: It\'s locked\.$"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>
if GetVariable("lock") then
  Note("Unlocking: " .. lastdir)
  Send("cast 'knock'"  .. lastdir)
end
  </send>
  </trigger>
  <trigger
   enabled="y"
   group="script-autodoor"
   keep_evaluating="y"
   match="^(?:The)? (.*?) (is|are) closed\.$"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>
if GetInfo(87) ~= nil then

local lastcommand = string.lower(GetInfo(87))
--local lastdir = "NONE" -- need this available elsewhere to bash
lastdir = "NONE"

local directions = {"n", "s", "e", "w", "u", "d", "nw", "ne", "sw", "se"}

local fulldirections = {
 n = "NORTH",
 s = "SOUTH",
 e = "EAST",
 w = "WEST",
 ne = "NORTHEAST",
 se = "SOUTHEAST",
 nw = "NORTHWEST",
 sw = "SOUTHWEST",
 u = "UP",
 d = "DOWN"
}

for k, v in pairs(directions) do
  if string.match(lastcommand, v) then
    lastdir = fulldirections[v]
  end
end

Note("Auto-opening: " .. lastdir)
Send("OPEN " .. lastdir)
Send(lastdir)

else
  Note("Autodoor: Could not retrieve the last command sent to the MUD!")
end
  </send>
  </trigger>
</triggers>

<!--  Variables  -->

<variables>
  <variable name="lock">y</variable>
</variables>

</muclient>
Australia Forum Administrator #1
That's pretty confusing stuff. Can't you send "open n" to the MUD?
USA Global Moderator #2
fyi:


lastdir = "NONE"
for k, v in pairs(directions) do
  if string.match(lastcommand, v) then
    lastdir = fulldirections[v]
  end
end


is roughly equivalent to

lastdir = fulldirections[lastcommand] or "NONE"

but your way is less safe, because a command could be "now" and it would string.match "n" and "w".


Quote:
the plugin sends the "up" command

I think you have a problem with some other setting somewhere. If GetInfo(87) is "up" then you sent "up". You should print the value of GetInfo(87) before you do anything with it. And enable the "Echo My Input in:" setting under the Alt+0 config screen. And/or enable your game's command echo feature if it has one.
Amended on Sun 02 Dec 2012 08:22 PM by Fiendish
USA #3
I should note I didn't write this plugin, I just put in my character name when I used the plugin wizard after pulling it from a website. Credits due to the author, but I've long forgotten who it was/where I got it.

That being said, I have no idea why the code is so convoluted, and if Nick Gammon is confused then all hope is lost :P

I don't know how to get the value of GetInfo(87) Fiendish, but will look into it.

Here's a bit of the output from my screen when I encounter a locked door to my south. Note, the 1st line of my prompt is just a persistent sextant, so ignore it.


<3475hp 3065sp 2245st> [ 0 * -40 * 0 ]
south
The door is closed.
Auto-opening: UP
OPEN UP
UP

[801, 545]
<3475hp 3065sp 2245st> 
There is no obvious exit up here.

[801, 545]
<3475hp 3065sp 2245st> 
You cannot move in that direction.

[801, 545]
<3475hp 3065sp 2245st> 
USA Global Moderator #4
forral said:
I don't know how to get the value of GetInfo(87) Fiendish, but will look into it.


Erm, it will help to read the code you posted, then. It happens a couple times in there.
Australia Forum Administrator #5
forral said:


That being said, I have no idea why the code is so convoluted, and if Nick Gammon is confused then all hope is lost :P


What happens is, people take snippets of code and blend them together without fully understanding what each piece does. I can read the code, but am confused about why someone would do that (what this code does) in the first place.

For example, why have two tables of directions?


local directions = {"n", "s", "e", "w", "u", "d", "nw", "ne", "sw", "se"}

local fulldirections = {
 n = "NORTH",
 s = "SOUTH",
 e = "EAST",
 w = "WEST",
 ne = "NORTHEAST",
 se = "SOUTHEAST",
 nw = "NORTHWEST",
 sw = "SOUTHWEST",
 u = "UP",
 d = "DOWN"
}

USA Global Moderator #6
Anyway, the problem is that when you use Numpad-2 it sends the full word "south", not "s", and string.match("south","u") returns a positive result.

This points to both Nick's and my comments. Your problem is that this plugin is going in so many circles that you can't follow what it's doing.

I think maybe Nick wonders why you aren't just doing

local last_command = GetInfo(87)
Send("open " .. last_command)
Send(last_command)
USA #7
Thank you guys for the insight.

Like I said, I didn't write this code so I really don't understand the logic behind it. I'm actually learning to code (through CodeAcademy and others) in Python and Javascript, with Ruby to come soon, so I will more likely than not re-write some of these scripts to my own liking in the future.

That said, I think I may have inadvertently modified something in that script because I loaded an original version from back in July of this year and it worked fine, so I'll experiment with that.

I think I may also have a conflict as I have another plugin that acts on my keypad, so that may be an issue.

Thanks again.
USA Global Moderator #8
Eh, all you have to do is change your numpad macros to send short forms of directions instead of full words and it would work as originally posted.