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 ➜ Help getting a script working

Help getting a 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 05 May 2011 03:41 PM (UTC)

Amended on Thu 05 May 2011 04:28 PM (UTC) by forral

Message
*Edit* I remembered a question I forgot in the original post

Hi all,

I've been wanting to do this script for a while, but I guess my mind didn't properly grasp the idea until recently.

This is, in a nutshell, what I'm trying to accomplish, and the accompanying code.

The script is based on a sailing feature that this mud has, where a player sinks pirate ships, salvages gold-ingots, and sells them for in-game gold.

Original idea:
Quote:
-Get # of ingots (@ingots)
-multiply # of ingots by respective ingot values (495 519 544 569 594 618) (@prices)
-include the corresponding ingot threshold values
-output corresponding values into the output of the screen (using the Note( ) command?)


This is the code I have so far:

price495 = tonumber(GetVariable("ingot")) * 495
price519 = tonumber(GetVariable("ingot")) * 519
price544 = tonumber(GetVariable("ingot")) * 544
price569 = tonumber(GetVariable("ingot")) * 569
price594 = tonumber(GetVariable("ingot")) * 594
price618 = tonumber(GetVariable("ingot")) * 618

Note("Ingots at 495" .. " " .. math.floor(price495))
Note("Ingots at 519" .. " " .. math.floor(price519))
Note("Ingots at 544" .. " " .. math.floor(price544))
Note("Ingots at 569" .. " " .. math.floor(price569))
Note("Ingots at 594" .. " " .. math.floor(price594))
Note("Ingots at 618" .. " " .. math.floor(price618))


The variables price495.....price618, and ingot have all been created, and as it currently stands this works perfectly. If, say, I had 100 ingots it displays to my output window:


Ingots at 495: 49500
Ingots at 519: 51900
Ingots at 544: 54400
Ingots at 569: 56900
Ingots at 594: 59400
Ingots at 618: 61800


I only need to expand/edit a few functionalities, which I need help doing.

1) I want to take the value that price495 = tonumber(GetVariable("ingot")) * 495 calculates and have that actually set to the correct variable (in this case, 495). I was trying to use the SetVariable command, which I've used just fine in another sailing script, but I was not sure how to post math functions inside that command. I also tried

local tmp = GetVariable("ingot")
tmp = tmp * 495
SetVariable("price495",tmp)

which works fine but the code becomes a bit convoluted at the end.

2) The # of ingots you can sell at each selling spot varies based on thresholds (which I have painstakingly gathered over my years spent sailing). For instance, at the port for the main towne of Rune, the trader will buy perhaps only 2000 ingots at 618 per, and then another 2000 at 594 per, another 2000 at 569 per, but then another 4000 at 544 per, etc. These values vary for each port, and increase as you go down (You could sell maybe 20,000 ingots at 318 gold per, for instance).

My issue becomes how do I post the various thresholds, for the various ports, in an easy to read fashion, and what code can I use to accomplish this? My scripting knowledge is limited, and the code I used above is from a script I made quite some time ago and as you can see the code I used is rather primitive, and I'm sure it could be cleaned up using lua math functions and a host of other things.

3) I would eventually like to make this into a plugin, but I've never made a plugin before. I know there is a plugin wizard but I'm somewhat confused as to what codebits I put where. Could someone guide me on this?

4) I also want to show the difference between prices. So, if I can sell 100 ingots at 594 per ingot for 59,400 gold, and 100 at 569 for 56,900 gold, have it show that price difference of 2,500 gold in a new line in the output.

I apologize for the length of my post, but it seemed prudent to list all the steps I've already tried as well as what exactly it is I'm trying to accomplish so you guys can best help me.

Thanks in advance,
Forral

*EDIT* Just want to note, I'm using an alias for all this code...that is, I have an alias named "price" and that is what all this code is going into. I have it sent to "script", and it outputs to my window.

*EDIT again!!* I got the functionality for the change between prices working...the code I added (for 495 to 519) is:
change = tonumber(GetVariable("price519")) - tonumber(GetVariable("price495"))

Note("Change 495 to 519:" .. " " .. change)


I'm sure this can be cleaned up and made alot prettier, but this works so far!
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #1 on Thu 05 May 2011 10:36 PM (UTC)
Message
My initial reaction is to stop using lots and lots of variables and make a table. So instead of:


price495 = tonumber(GetVariable("ingot")) * 495
price519 = tonumber(GetVariable("ingot")) * 519
price544 = tonumber(GetVariable("ingot")) * 544
price569 = tonumber(GetVariable("ingot")) * 569
price594 = tonumber(GetVariable("ingot")) * 594
price618 = tonumber(GetVariable("ingot")) * 618


You do:


price = {}  -- make table
price [495] = tonumber(GetVariable("ingot")) * 495
price [519] = tonumber(GetVariable("ingot")) * 519
price [544] = tonumber(GetVariable("ingot")) * 544
price [569] = tonumber(GetVariable("ingot")) * 569
price [594] = tonumber(GetVariable("ingot")) * 594
price [618] = tonumber(GetVariable("ingot")) * 618


But now, of course, you can spot an efficiency:

Let's say the table has been made once, like this:


price = {}  -- make table
price [495] = 0
price [519] = 0
price [544] = 0
price [569] = 0
price [594] = 0
price [618] = 0


Now all you have to do to update prices is:


local ingot = tonumber(GetVariable("ingot")) 
for k, v in pairs (price) do
  v = ingot * k
end -- for


That takes each entry (where k is the key, like 495, and v is the value (the price)) and recomputes it by multiplying the current ingot price by the key (like you did earlier on).

And printing is then easier:


for k, v in pairs (price) do
  Note("Ingots at " .. k .. " " .. math.floor(v)) 
end -- for


They might not come out in order, but there is an easy way around that.

- Nick Gammon

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

Posted by forral   USA  (79 posts)  Bio
Date Reply #2 on Thu 05 May 2011 11:59 PM (UTC)
Message
Nick Gammon said:

My initial reaction is to stop using lots and lots of variables and make a table. So instead of:


price495 = tonumber(GetVariable("ingot")) * 495
price519 = tonumber(GetVariable("ingot")) * 519
price544 = tonumber(GetVariable("ingot")) * 544
price569 = tonumber(GetVariable("ingot")) * 569
price594 = tonumber(GetVariable("ingot")) * 594
price618 = tonumber(GetVariable("ingot")) * 618


You do:


price = {}  -- make table
price [495] = tonumber(GetVariable("ingot")) * 495
price [519] = tonumber(GetVariable("ingot")) * 519
price [544] = tonumber(GetVariable("ingot")) * 544
price [569] = tonumber(GetVariable("ingot")) * 569
price [594] = tonumber(GetVariable("ingot")) * 594
price [618] = tonumber(GetVariable("ingot")) * 618


But now, of course, you can spot an efficiency:

Let's say the table has been made once, like this:


price = {}  -- make table
price [495] = 0
price [519] = 0
price [544] = 0
price [569] = 0
price [594] = 0
price [618] = 0


Now all you have to do to update prices is:


local ingot = tonumber(GetVariable("ingot")) 
for k, v in pairs (price) do
  v = ingot * k
end -- for


That takes each entry (where k is the key, like 495, and v is the value (the price)) and recomputes it by multiplying the current ingot price by the key (like you did earlier on).

And printing is then easier:


for k, v in pairs (price) do
  Note("Ingots at " .. k .. " " .. math.floor(v)) 
end -- for


They might not come out in order, but there is an easy way around that.



Thank you Nick. I will test this out and try to understand it and incorporate it into my code, but I'm afraid my code has expanded to quite a bit more than my original post. As it stands, I have one alias for the prices (my original code), and a 2nd alias that lists all the change in prices (so, 618 to 569, 618 to 544, or backwards 544 to 618 to show how much profit I'm losing in selling at a lower price). As it stands, the output of that screen is quite spammy but I am sure as I work on this I will get better and better code.

Could you or someone else advise me how to make this into a plugin so others can help me test/debug it/provide suggestions?
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #3 on Fri 06 May 2011 12:24 AM (UTC)
Message
You don't need to quote my entire post you know. Your reply is just under it anyway.

To convert to a plugin (once it is all working) use the Plugin Wizard. There is a bit of walk-through making plugins here:

Template:post=8411 Please see the forum thread: http://gammon.com.au/forum/?id=8411.

- Nick Gammon

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

Posted by forral   USA  (79 posts)  Bio
Date Reply #4 on Sat 07 May 2011 04:24 AM (UTC)
Message
Thanks Nick

I'll try out this code once my work schedule dies down, and I'll post back if I have any questions or issues.

You are awesome as usual :)

Thanks again,
-Forral
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.


16,349 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.