[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]  Serialize

Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?
(New message)
Subject: Serialize
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)

Pages: 1 2  

Posted by David Berthiaume   (202 posts)  [Biography] bio
Date Tue 14 Dec 2004 12:25 AM (UTC)  quote  ]

Amended on Tue 14 Dec 2004 12:26 AM (UTC) by David Berthiaume

Message
Gotcha gotcha... I understand now. I misunderstood what it was you were doing, then I thought I understood, but I understood incorrectly, but it worked out because it did what I wanted it to do anyways.

I think this part of the script is just about finished. All that's left is to finish off the replace text script we're working on in the other thread, then we can run this script through that one, and re-order our skill/spell lists.
[Go to top] top

Posted by Nick Gammon   Australia  (18,800 posts)  [Biography] bio   Forum Administrator
Date Tue 14 Dec 2004 12:07 AM (UTC)  quote  ]
Message
I was illustrating ideas. Not saying to put the whole heap in a single trigger.

The trigger, which is called for every line, should simply gather information, eg.


skills = {}
function do_skill_trigger (name, line, wildcards)

-- make a regexp the same as the trigger, excluding the {1,3} at the end
re = rex.new (string.gsub (GetTriggerInfo (name, 1), "{1,3}", ""))

-- save name and mastery
re:gmatch (line, function (m, t) skills [t.name] = t.mastery end )

end -- function


The rest is done elsewhere, once you have all the lines. For example, you could make a trigger that matches something at the end of the skills list, whatever that is. Or just make an alias to do it when you need the sorted list.


- Nick Gammon

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

Posted by David Berthiaume   (202 posts)  [Biography] bio
Date Mon 13 Dec 2004 11:31 PM (UTC)  quote  ]
Message
Ok, a little mis-communication going on here. Let me clarify
skills = {}
function do_skill_trigger (name, line, wildcards)

-- make a regexp the same as the trigger, excluding the {1,3} at the end
re = rex.new (string.gsub (GetTriggerInfo (name, 1), "{1,3}", ""))

-- save name and mastery
re:gmatch (line, function (m, t) skills [t.name] = t.mastery end )
skills_sorted = {}
for k in pairs (skills) do
  table.insert (skills_sorted, k)
end
table.sort (skills_sorted)
for _, v in skills_sorted do
  print (string.format ("%-20s", v), skills [v])
end


end -- function


The last for statement.
for _, v in skills_sorted do
  print (string.format ("%-20s", v), skills [v])
end


That prints like crazy amounts of lines... I don't know where to put that bit of code, I put it in the scriptfile, because that's where I figured it went...

I was under the impression that as you walked through each step you built on the existing function do_skills_trigger.

I think what you're saying is that, I don't have to have that in the do_skills_trigger, I can put that in an alias, or a different trigger.
[Go to top] top

Posted by Nick Gammon   Australia  (18,800 posts)  [Biography] bio   Forum Administrator
Date Mon 13 Dec 2004 11:26 PM (UTC)  quote  ]
Message
I don't quite understand the problem here. The trigger will run for every line, you clearly don't want the printing there. You could make it happen on an alias, so you type the alias whenever you want to see the sorted list.

- Nick Gammon

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

Posted by David Berthiaume   (202 posts)  [Biography] bio
Date Mon 13 Dec 2004 11:18 PM (UTC)  quote  ]
Message
...
...
Put it wherever you want... I know it's important, I just don't want it printing out at me... It makes fthings very spammy. It evaluates each time the trigger is run, so it alphabitizes everything 50 million times.
[Go to top] top

Posted by Nick Gammon   Australia  (18,800 posts)  [Biography] bio   Forum Administrator
Date Mon 13 Dec 2004 11:04 PM (UTC)  quote  ]
Message
Quote:

That line is giving me serious issues. It's evaluating every time the trigger goes off, which isn't a problem, but I don't want it to print it out to me every time.


Put it whereever you want it to happen.

Quote:

loadstring (GetVariable ("replace")) ()


I noticed that myself. Looks like I was wrong about how to use loadstring. I have amended the post about it. Change it to:


loadstring (GetVariable ("replace"))


- Nick Gammon

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

Posted by David Berthiaume   (202 posts)  [Biography] bio
Date Mon 13 Dec 2004 10:06 PM (UTC)  quote  ]

Amended on Mon 13 Dec 2004 10:09 PM (UTC) by David Berthiaume

Message
Error number: 0
Event: Run-time error
Description: [string "Script file"]:4: attempt to call a nil value
Called by: Function/Sub: connect called by world connect
Reason: connecting to world



function connect ()
Note ("Connected to World!")
  loadstring (GetVariable ("replace")) ()
  loadstring (GetVariable ("skills")) ()
  loadstring (GetVariable ("spells")) ()
end

function disconnect ()
Note ("Disconnected from World.")
  SetVariable ("replace", serialize ("text"))  --> serialize mobs table
  SetVariable ("skills", serialize ("skills")) --> serialize skills table
  SetVariable ("spells", serialize ("spells")) --> serialize spells table
end


Ok, well I was wrong, I thought it went away, it didn't, I put the updated error in place of the old error
[Go to top] top

Posted by David Berthiaume   (202 posts)  [Biography] bio
Date Mon 13 Dec 2004 09:35 PM (UTC)  quote  ]

Amended on Mon 13 Dec 2004 09:52 PM (UTC) by David Berthiaume

Message
Quote:
But for the sake of the exercise, let's look at sorting it. See next post.


I followed you quite well up untill that point.

I'm not sure where those code snipets go, in the "do_skill_trigger"?

Edit: I had a thought, and tested it out, it worked perfectly.

Re-edit: Ok I got it working 99% perfect...

print (string.format ("%-20s", v), skills [v])

That line is giving me serious issues. It's evaluating every time the trigger goes off, which isn't a problem, but I don't want it to print it out to me every time.
[Go to top] top

Posted by David Berthiaume   (202 posts)  [Biography] bio
Date Mon 13 Dec 2004 09:07 PM (UTC)  quote  ]
Message
googled: Lua Script remove extra spaces

You came up first!
[Go to top] top

Posted by Nick Gammon   Australia  (18,800 posts)  [Biography] bio   Forum Administrator
Date Mon 13 Dec 2004 08:46 PM (UTC)  quote  ]
Message
BTW - what did you Google on to find my post? I only started learning Lua about a month ago, bit early to be an expert. ;)

- Nick Gammon

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

Posted by Nick Gammon   Australia  (18,800 posts)  [Biography] bio   Forum Administrator
Date Mon 13 Dec 2004 08:41 PM (UTC)  quote  ]
Message
Given the skills table, to sort them into sequence we can use the Lua sort function, however that works on a numerically keyed table. Let's make that first (based on the names of the skills):


skills_sorted = {}
for k in pairs (skills) do
  table.insert (skills_sorted, k)
end
table.foreach (skills_sorted, print)

-- Output:

1 Dancing
2 Advanced staff
3 Path finding
4 Pottery
5 Drink mixing
6 Link
7 Scribe
8 Tanning
9 Hand to hand
10 Golem craft
11 Scrolls
12 Jewelry craft
13 Drinking
14 Draco biology
15 Butcher
16 Wands
17 Sewing
18 Smithing
19 Staves
20 Metallurgy
21 Dagger
22 Staff
23 Smooth talk
24 Haggle
25 Swimming
26 Peek
27 Weapon craft
28 Skinning
29 Lore
30 Alchemy
31 Dodge
32 Dual wield
33 Powder
34 Armor craft
35 Expert staff
36 Teaching
37 Brew
38 Leather workin
39 Sharpening
40 Riding
41 Spellcraft


We now have a second table, which is the names of each skill.

Next, we'll sort it:


table.sort (skills_sorted)

table.foreach (skills_sorted, print)

-- Output:

1 Advanced staff
2 Alchemy
3 Armor craft
4 Brew
5 Butcher
6 Dagger
7 Dancing
8 Dodge
9 Draco biology
10 Drink mixing
11 Drinking
12 Dual wield
13 Expert staff
14 Golem craft
15 Haggle
16 Hand to hand
17 Jewelry craft
18 Leather workin
19 Link
20 Lore
21 Metallurgy
22 Path finding
23 Peek
24 Pottery
25 Powder
26 Riding
27 Scribe
28 Scrolls
29 Sewing
30 Sharpening
31 Skinning
32 Smithing
33 Smooth talk
34 Spellcraft
35 Staff
36 Staves
37 Swimming
38 Tanning
39 Teaching
40 Wands
41 Weapon craft


You can probably see where this is heading. One table has the names in sequence, the other has the values keyed by the names. Let's put it all together:


for _, v in skills_sorted do
  print (string.format ("%-20s", v), skills [v])
end

-- Output:

Advanced staff       skilled
Alchemy              skilled
Armor craft          fair
Brew                 advanced
Butcher              advanced
Dagger               skilled
Dancing              master
Dodge                master
Draco biology        skilled
Drink mixing         skilled
Drinking             skilled
Dual wield           skilled
Expert staff         skilled
Golem craft          fair
Haggle               expert
Hand to hand         skilled
Jewelry craft        skilled
Leather workin       skilled
Link                 skilled
Lore                 fair
Metallurgy           skilled
Path finding         master
Peek                 skilled
Pottery              fair
Powder               skilled
Riding               expert
Scribe               fair
Scrolls              skilled
Sewing               advanced
Sharpening           sliver
Skinning             advanced
Smithing             master
Smooth talk          expert
Spellcraft           master
Staff                skilled
Staves               skilled
Swimming             skilled
Tanning              skilled
Teaching             skilled
Wands                skilled
Weapon craft         skilled


- Nick Gammon

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

Posted by Nick Gammon   Australia  (18,800 posts)  [Biography] bio   Forum Administrator
Date Mon 13 Dec 2004 08:27 PM (UTC)  quote  ]

Amended on Mon 13 Dec 2004 09:11 PM (UTC) by Nick Gammon

Message
OK, let's do one thing at a time here. The first thing is to match the skills and put them into an associative array.

A single trigger should do it. I'll try to avoid the repetition you had in yours. I am using {1,3} at the end to indicate "between one and three of the previous thing".

I am also using named wildcards, for example (?P<name>.+?) is the name of the skill.

I'll not use "repeat on same line" becaues I plan to rescan the line in the script. I want the whole line so I'll do it in a script file, not "send to script". Here is the trigger:



<triggers>
<trigger
custom_colour="2"
enabled="y"
match="((?P&lt;name&gt;.+?) +(?P&lt;mastery&gt;sliver|slight|fair|basic|average|skilled|advanced|expert|master)( +){0,1}){1,3}"
name="skill_trigger"
regexp="y"
script="do_skill_trigger"
sequence="100"
>
</trigger>
</triggers>



Now the script is deceptively short. I am assuming that you will empty out the skills table in advance. For example, a trigger matching on "Name Mastery Name Mastery Name Mastery" could be used to empty the existing skills.

This is all we need to save all skills and their levels into a table:


skills = {}

function do_skill_trigger (name, line, wildcards)

-- make a regexp the same as the trigger, excluding the {1,3} at the end
re = rex.new (string.gsub (GetTriggerInfo (name, 1), "{1,3}", ""))

-- save name and mastery
re:gmatch (line, function (m, t) skills [t.name] = t.mastery end )

end -- function



Don't believe me? Here is the output:


/tprint (skills)

Dancing=master
Advanced staff=skilled
Path finding=master
Pottery=fair
Drink mixing=skilled
Link=skilled
Scribe=fair
Tanning=skilled
Hand to hand=skilled
Golem craft=fair
Scrolls=skilled
Jewelry craft=skilled
Drinking=skilled
Draco biology=skilled
Butcher=advanced
Wands=skilled
Sewing=advanced
Smithing=master
Staves=skilled
Metallurgy=skilled
Dagger=skilled
Staff=skilled
Smooth talk=expert
Haggle=expert
Swimming=skilled
Peek=skilled
Weapon craft=skilled
Skinning=advanced
Lore=fair
Alchemy=skilled
Dodge=master
Dual wield=skilled
Powder=skilled
Armor craft=fair
Expert staff=skilled
Teaching=skilled
Brew=advanced
Leather workin=skilled
Sharpening=sliver
Riding=expert
Spellcraft=master


However we have a problem - they aren't in alphabetic order. Not that it necessarily matters, to find a particular one you could make an alias to show it. eg.


print (skills ['Spellcraft'])  --> master


But for the sake of the exercise, let's look at sorting it. See next post.

- Nick Gammon

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

Posted by David Berthiaume   (202 posts)  [Biography] bio
Date Mon 13 Dec 2004 08:13 PM (UTC)  quote  ]
Message
The code isn't the issue right now, I think I more or less got that all figured out, my problem is making a trigger that will go through the list and match:

Acid blast     master    Acidproof      advanced  Mana charge    expert


See, it's all about the spacing. I can get it to easily match everything apropriately:

Pottery       =fair
Haggle        =expert
   Staff         =skilled


So when I: /tprint (skill) it comes up with the huge list of all the skills, but the spacing is whacked out crazy.

Now I gsub'd " " to "_", which made it slightly easier, because you can't "/print skill.Haggle ", it won't let you. so you end up with:

Haggle________=expert

and many different combinations of underscore type names.

As for going through the elements within the array(which might I add I'm using a dictionary type array, not a numeric array<or table, which ever you want to call it>)

Numeric would be insert(skill, 1, "%1"
that would give you Skill 1 = Haggle________

Which won't work for what I'm doing. But that's besides the point. Unless I can get the trigger straightened out, I can't do anything with it.

I tried NUMEROUS combinations of Regexp trigger, using various different things, I couldn't get it to work properly.
[Go to top] top

Posted by Shadowfyr   USA  (1,774 posts)  [Biography] bio
Date Mon 13 Dec 2004 05:44 PM (UTC)  quote  ]
Message
You might consider looking up 'binary searches' and adapting that to do the insertion from the start. It is the fastest way to search a table that exists, so is also the fastest method to insert someting. I can't remember the exact definition, but in the 'worst' case, it is something like N/2+1 tests. Of course, designing a binary search and adapting it to do insertions are two different animals. ;) I have tried several times and only got it right once. (unfortunately, I lost the code for it, thus the subsequent failed attempts...) Adapted for the purpose of finding your insertion point you could do away with the need to sort the table at all, since it would then insert directly to the right place. Of course, re-running a sort each time could be done too, since the size of the table is pretty small anyway. Its just an idea.

Umm, the basic idea behind the binary is this:

Table:
0 ----
1 Adam
2 Beth
3 Keith
4 Sam
5 Xavier

Make S = 0, E = 5, T = cint(S + (E - S) / 2 + .5). If you are looking for 'Sam', then:

S=0,E=5,T=3 - test...
'Sam' > 'Keith" S=T, T=cint(3 + (5 - 3) / 2 + .5) - test...
'Sam' = 'Sam' - Exit with T=4

For insertions:

Insert="Aardvark",S=1,E=5,T=3 - test...
'Aardvark' < 'Keith' E=3,T=2 - test...
'Aardvark' < 'Beth' E=2, T=1 - test...
'Aardvark' < 'Adam' E=1, T=0 - Exit
Insert at T + 1

The only difference is that you bail if T=0. In any other case T should be 'insertion point - 1', so you would always insert to the table at T+1, once to correct place was found. I think... I wouldn't bet on it though, I have screwed this up so many times... ;) lol For searches, if you don't find it by the time T=0 or S = E, then it isn't in the table. Though with searches, you don't need to use '0' as a start, that just makes insertion easier, since otherwise you have to have a special rule that says, "if the insertion point is 2, then check the value against what is in 1, just to make sure we didn't mean 'insert at 1'." Which is I think where I continually messed up.

main {
__if (Schrodinger_Cat is Alive or version >= "XP"){
____if version = "Vista" then Performance /= Number_of_Cores;
____call Functional_Code();}
__else
____call Crash_Windows();}
[Go to top] top

Posted by David Berthiaume   (202 posts)  [Biography] bio
Date Mon 13 Dec 2004 03:08 PM (UTC)  quote  ]
Message
Well, that didn't work, so I googled, and I found a posting by Nick... Was number one on the google too... Anyways, I got that part working, I can now get most of my skills into the array, as skill name = proficency.

Unfortunately, There is NASTY formatting... Lemme show you:

Weapon craft  =skilled
Advanced staff=skilled
   Brew          =advanced
      Sharpening    =sliver
Swimming      =skilled
Jewelry craft =skilled
Tanning       =skilled
Draco biology =skilled
Leather workin=skilled
Scribe        =fair
Dual wield    =skilled
Butcher       =advanced
Dancing       =master
      Sewing        =advanced
Scrolls       =skilled
Powder        =skilled
Alchemy       =skilled
   Staves        =skilled
Lore          =fair
  Skinning      =advanced
Drinking      =skilled
      Riding        =expert
   Drink mixing  =skilled
Dodge         =master
Armor craft   =fair
   Golem craft   =fair
Spellcraft    =master
   Smooth talk   =expert
      Path finding  =master
   Metallurgy    =skilled
Dagger        =skilled
Smithing      =master
Peek          =skilled
    Link          =skilled
Pottery       =fair
Haggle        =expert
   Staff         =skilled
Hand to hand  =skilled
Expert staff  =skilled


Yeppers, I made a mess of it with that trigger, but I couldn't figure it out... who knew I'd figure out the code, but not the trigger to make it work. Usually it's the other way around.

I'll tell ya, I'm plum stumped on that trigger.
[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.


6,223 views.

This is page 1, subject is 2 pages long: 1 2  [Next page]

[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]