Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ Trouble getting my script working

Trouble getting my script working

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by forral   USA  (79 posts)  Bio
Date Thu 04 Nov 2010 03:13 AM (UTC)
Message
Hi all,

I made a script in MUSH to calculate the accuracy of my weapons as I'm sailing on my boat in MM. I was able to work out the basics of the code but for some reason, only 2 of the weapons are working correctly, and the others don't. This is the error I am getting


[AWAY][Mail][*][SPK](450hull SEdir SLOW SEwind 100%shld 1385, 594) You give the order to fire!
BOOM! Your ship fires ten basic-cannon rounds at Exposed Plague, and six land.
Run-time error
World: Materia Magica - Forral
Immediate execution
[string "Trigger: "]:72: bad argument #1 to 'GetVariable' (string expected, got nil)
stack traceback:
        [C]: in function 'GetVariable'
        [string "Trigger: "]:72: in main chunk
Exposed Plague's shields absorb the full force of the cannon.
Smiling Norba, the Sea Queen tells you, 'Yer gonna regret that! Arr!@#'



My variables are:

ingaverage
ingot
ship_firelauncher_hit
ship_firelauncher_shot
ship_harpoon_hit
ship_harpoon_shot
ship_hballista_hit
ship_hballista_shot
ship_lballista_hit
ship_lballista_shot
ship_hcannon_hit
ship_hcannon_shot
ship_lcannon_hit
ship_lcannon_shot
ship_mcannon_hit
ship_mcannon_shot


Basically, the code captures the following string:
Your ship fires * * at *, and * land.

The syntax of that string is: Your ship fires <amount> <weapon type> at <shipname>, and <amount> land(s).

I have the code to convert the words (ten nine eight, etc) to the numerical values, and to crunch the math, then to display that numerical value as a %.

This is how I have the string formatted:

Your ship fires ([^\s]+) (.+) at (.+)\, and ([^\s]+) land

I can get the heavy and light ballistae accuracy, but for everything else it says:
L CANNON : -1.#IND%
FIRE LAUNCHER : -1.#IND%

Or:
H BALLISTA : 0%
L BALLISTA : 0%

The following is my code for that matched string:

howmany = 0
hits = 0

if "%1" == "ten" then
howmany = 10
elseif "%1" == "nine" then
howmany = 9
elseif "%1" == "eight" then
howmany = 8
elseif "%1" == "seven" then
howmany = 7
elseif "%1" == "six" then
howmany = 6
elseif "%1" == "five" then
howmany = 5
elseif "%1" == "four" then
howmany = 4
elseif "%1" == "three" then
howmany = 3
elseif "%1" == "two" then
howmany = 2
elseif "%1" == "one" then
howmany = 1
elseif "%1" == "zero" then
howmany = 0
end

if "%4" == "ten" then
hits = 10
elseif "%4" == "nine" then
hits = 9
elseif "%4" == "eight" then
hits = 8
elseif "%4" == "seven" then
hits = 7
elseif "%4" == "six" then
hits = 6
elseif "%4" == "five" then
hits = 5
elseif "%4" == "four" then
hits = 4
elseif "%4" == "three" then
hits = 3
elseif "%4" == "two" then
hits = 2
elseif "%4" == "one" then
hits = 1
elseif "%4" == "zero" then
hits = 0
end

if "%2" == "heavy-ballista rounds" then
weapon_shot = "ship_hballista_shot"
weapon_hit = "ship_hballista_shot"
elseif "%2" == "light-ballista rounds" then
weapon_shot = "ship_lballista_shot"
weapon_hit = "ship_lballista_shot"
elseif "%2" == "fire-launcher rounds" then
weapon_shot = "ship_firelauncher_shot"
weapon_hit = "ship_firelauncher_hit"
elseif "%2" == "heavy-cannon rounds" then
weapon_shot = "ship_hcannon_shot"
weapon_hit = "ship_hcannon_hit"
elseif "%2" == "medium-cannon rounds" then
weapon_shot = "ship_mcannon_shot"
weapon_hit = "ship_mcannon_hit"
elseif "%2" == "light-cannon rounds" then
weapon_shot = "ship_lcannon_shot"
weapon_hit = "ship_lcannon_hit"
end

SetVariable(weapon_hit,tonumber(GetVariable(weapon_hit))+hits)
SetVariable(weapon_shot,tonumber(GetVariable(weapon_shot))+howmany)


I don't understand why I cannot get the script working, but I know from previous programming experience that it is something trivial, like a comma or period somewhere.


My other question is:
I'm wanting to record the # of gold-ingots recovered per trip (a trip being leaving docks and returning). My question is how do I enable the variable when I leave docks (I have strings that I can match when I leave) and then disable that same variable when I dock again?



I appreciate any help in this matter, and apologize for the length of my post. I figure more information is better than being vague.

Thanks,
Forral
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #1 on Thu 04 Nov 2010 03:47 AM (UTC)

Amended on Thu 04 Nov 2010 03:58 AM (UTC) by Nick Gammon

Message
OK, well let's clean up the code a bit. When you see lots of "if" statements you can usually do things in a neater way:


<triggers>
  <trigger
   enabled="y"
   match="Your ship fires (\w+) (.+) at (.+)\, and (\w+) land"
   regexp="y"
   send_to="12"
   sequence="110"
  >
  <send>

local words_to_numbers = {
  zero = 0,
  one = 1,
  two = 2,
  three = 3,
  four = 4,
  five = 5,
  six = 6,
  seven = 7,
  eight = 8,
  nine = 9,
  ten = 10
  }

local var_name = {

  ["heavy-ballista rounds"] = { 
    weapon_shot = "ship_hballista_shot",
    weapon_hit = "ship_hballista_shot"
    },

    ["light-ballista rounds"] = {
      weapon_shot = "ship_lballista_shot",
      weapon_hit = "ship_lballista_shot"
    },

    ["fire-launcher rounds"] = {
      weapon_shot = "ship_firelauncher_shot",
      weapon_hit = "ship_firelauncher_hit"
    },
    
    ["heavy-cannon rounds"] = {
      weapon_shot = "ship_hcannon_shot",
      weapon_hit = "ship_hcannon_hit"
    },
    
    ["medium-cannon rounds"] = {
      weapon_shot = "ship_mcannon_shot",
      weapon_hit = "ship_mcannon_hit"
    },
    
    ["light-cannon rounds"] = {
      weapon_shot = "ship_lcannon_shot",
      weapon_hit = "ship_lcannon_hit"
    },
}


local howmany = assert (words_to_numbers ["%1"], "Bad number fired: %1")
local hits = assert (words_to_numbers ["%4"], "Bad number hit: %4")
local t = assert (var_name ["%2"], "No match for: %2")

SetVariable (t.weapon_hit, tonumber (GetVariable (t.weapon_hit)) + hits)
SetVariable (t.weapon_shot, tonumber (GetVariable (t.weapon_shot)) + howmany) 

</send>
  </trigger>
</triggers>


The above uses a table-lookup to convert words to numbers, and another one to work out which variable to set. Running that on your test line of:


BOOM! Your ship fires ten basic-cannon rounds at Exposed Plague, and six land.


I get this:


Run-time error
World: SmaugFUSS
Immediate execution
[string "Trigger: "]:49: No match for: basic-cannon rounds
stack traceback:
        [C]: in function 'assert'
        [string "Trigger: "]:49: in main chunk


So there you are. You didn't allow for "basic-cannon rounds".



- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #2 on Thu 04 Nov 2010 03:49 AM (UTC)

Amended on Thu 04 Nov 2010 08:58 PM (UTC) by Twisol

Message
([EDIT]: Okay, Nick ninja'd me pretty hard. That said, I like my refactoring better. *stubborn pride*)

It seems likely that weapon_shot/weapon_hit aren't getting set. Maybe you're missing a potential vaue of %2? Also, to clean up the code, you may want to try this:

local numbers = {
  ten  = 10, nine = 9, eight = 8, seven = 7, six = 6,
  five = 5, four = 4, three = 3, two   = 2, one = 1,
  zero = 0,
}

local rounds = {
  ["heavy-ballista rounds"] = "hballista",
  ["light-ballista rounds"] = "lballista",
  ["fire-launcher rounds"]  = "firelauncher",
  ["heavy-cannon rounds"]   = "hcannon",
  ["medium-cannon rounds"]  = "mcannon",
  ["light-cannon rounds"]   = "lcannon",
}

local howmany = numbers["%1"]
local hits = numbers["%4"]

local weapon = "ship_" .. rounds["%2"]
local hits_var, shots_var = weapon .. "_hit, weapon .. "_shot"

SetVariable(hits_var, tonumber(GetVariable(hits_var)) + hits)
SetVariable(shots_var, tonumber(GetVariable(shots_var)) + howmany)


[EDIT]: Fixed typo, thanks Nick.
[EDIT]: Fixed forum codes in code tags.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #3 on Thu 04 Nov 2010 03:50 AM (UTC)
Message
Apart from this:


local numbers = {
  ten  = 0, 


Try: ten = 10

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #4 on Thu 04 Nov 2010 03:51 AM (UTC)
Message
This is neater, I agree:


local rounds = {
  ["heavy-ballista rounds"] = "hballista",
  ["light-ballista rounds"] = "lballista",
  ["fire-launcher rounds"]  = "firelauncher",
  ["heavy-cannon rounds"]   = "hcannon",
  ["medium-cannon rounds"]  = "mcannon",
  ["light-cannon rounds"]   = "lcannon",
}


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #5 on Thu 04 Nov 2010 03:52 AM (UTC)
Message
My asserts pointed to the real problem, it is nice to check doubtful input, as well.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #6 on Thu 04 Nov 2010 04:03 AM (UTC)
Message
Nick Gammon said:
Apart from this:


local numbers = {
  ten  = 0, 


Try: ten = 10

When I try to edit my post to fix that, it says "This post is currently being edited by Nick Gammon, please try again later."

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #7 on Thu 04 Nov 2010 04:08 AM (UTC)
Message
I must have hit Edit by mistake, and then it thought I was editing it. I've done that again and saved, so it should be OK now. Sorry.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by forral   USA  (79 posts)  Bio
Date Reply #8 on Thu 04 Nov 2010 03:17 PM (UTC)

Amended on Thu 04 Nov 2010 03:41 PM (UTC) by forral

Message
For sure I can always count on such a quick response time whenever I post, thanks guys :)

I will adjust my code accordingly and post my results, thanks for helping me shorten it, too.

Was there any response to the 2nd question I had, about what codebit to use to ENABLE a variable at a set point, then disable it again? In my case, when I leave docks I want my "trip" variable to be enabled, and when I dock it'll disable that variable, so I can do the math of ingots per trip.
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #9 on Thu 04 Nov 2010 07:26 PM (UTC)
Message
forral said:
Was there any response to the 2nd question I had, about what codebit to use to ENABLE a variable at a set point, then disable it again? In my case, when I leave docks I want my "trip" variable to be enabled, and when I dock it'll disable that variable, so I can do the math of ingots per trip.

Variables only contain values; they have no concept of being enabled or disabled. I assume you want to freeze the value of a variable unless you're not docked, though. In that case, you can create the triggers that update your "trip" variable, and simply use EnableTriggerGroup() to enable and disable those triggers from within your dock triggers.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #10 on Thu 04 Nov 2010 08:57 PM (UTC)
Message
Twisol said:

When I try to edit my post to fix that, it says "This post is currently being edited by Nick Gammon, please try again later."


Fixed a bug in the forum. Now when I edit a post (probably to grab the original contents if the poster did not use forum codes on code snippets) then my editing action is cancelled next time I save or update.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #11 on Thu 04 Nov 2010 08:59 PM (UTC)
Message
Nick Gammon said:
(... if the poster did not use forum codes on code snippets)

I see what you did there.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #12 on Tue 09 Nov 2010 09:05 PM (UTC)
Message
forral said:


if "%1" == "ten" then
howmany = 10
elseif "%1" == "nine" then
howmany = 9
elseif "%1" == "eight" then
howmany = 8
elseif "%1" == "seven" then
howmany = 7
elseif "%1" == "six" then
howmany = 6
elseif "%1" == "five" then
howmany = 5
elseif "%1" == "four" then
howmany = 4
elseif "%1" == "three" then
howmany = 3
elseif "%1" == "two" then
howmany = 2
elseif "%1" == "one" then
howmany = 1
elseif "%1" == "zero" then
howmany = 0
end


Also see this thread:

http://www.gammon.com.au/forum/bbshowpost.php?id=10155&page=4

In that there is code for handling the more general case of words to numbers. So if you used that it could handle "Your ship fires one thousand seven hundred thirty five basic-cannon rounds at Exposed Plague, and three hundred and twenty two land"

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


33,333 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.