[Home] [Downloads] [Search] [Help/forum]

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  Trouble getting my script working

Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?
(New message)
Subject: Trouble getting my script working
Name:
Your forum user name.
Register forum user name
Password:
Your forum password.
Forgotten password?
Message:
Message to be posted (in English, please).
Forum codes:
Check this if your message uses 'forum codes' or templates (auto-detected for new posts).
Forum codes Templates

Save this message ...


Subject review (reverse sequence)

Posted by Nick Gammon   Australia  (18,770 posts)  [Biography] bio   Forum Administrator
Date Tue 09 Nov 2010 09:05 PM (UTC)  quote  ]
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
[Go to top] top

Posted by Twisol   USA  (2,229 posts)  [Biography] bio
Date Thu 04 Nov 2010 08:59 PM (UTC)  quote  ]
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
[Go to top] top

Posted by Nick Gammon   Australia  (18,770 posts)  [Biography] bio   Forum Administrator
Date Thu 04 Nov 2010 08:57 PM (UTC)  quote  ]
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
[Go to top] top

Posted by Twisol   USA  (2,229 posts)  [Biography] bio
Date Thu 04 Nov 2010 07:26 PM (UTC)  quote  ]
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
[Go to top] top

Posted by forral   USA  (79 posts)  [Biography] bio
Date Thu 04 Nov 2010 03:17 PM (UTC)  quote  ]

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.
[Go to top] top

Posted by Nick Gammon   Australia  (18,770 posts)  [Biography] bio   Forum Administrator
Date Thu 04 Nov 2010 04:08 AM (UTC)  quote  ]
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
[Go to top] top

Posted by Twisol   USA  (2,229 posts)  [Biography] bio
Date Thu 04 Nov 2010 04:03 AM (UTC)  quote  ]
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
[Go to top] top

Posted by Nick Gammon   Australia  (18,770 posts)  [Biography] bio   Forum Administrator
Date Thu 04 Nov 2010 03:52 AM (UTC)  quote  ]
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
[Go to top] top

Posted by Nick Gammon   Australia  (18,770 posts)  [Biography] bio   Forum Administrator
Date Thu 04 Nov 2010 03:51 AM (UTC)  quote  ]
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
[Go to top] top

Posted by Nick Gammon   Australia  (18,770 posts)  [Biography] bio   Forum Administrator
Date Thu 04 Nov 2010 03:50 AM (UTC)  quote  ]
Message
Apart from this:


local numbers = {
  ten  = 0, 


Try: ten = 10

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Twisol   USA  (2,229 posts)  [Biography] bio
Date Thu 04 Nov 2010 03:49 AM (UTC)  quote  ]

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
[Go to top] top

Posted by Nick Gammon   Australia  (18,770 posts)  [Biography] bio   Forum Administrator
Date Thu 04 Nov 2010 03:47 AM (UTC)  quote  ]

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
[Go to top] top

Posted by forral   USA  (79 posts)  [Biography] bio
Date Thu 04 Nov 2010 03:13 AM (UTC)  quote  ]
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
[Go to top] 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.


2,280 views.

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

[Home]

Written by Nick Gammon - 5K

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Internet Contents Rating Association (ICRA) - 2K]    [Web site powered by FutureQuest.Net]