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


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  Serialize

Serialize

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


Pages: 1 2  

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

Amended on Mon 13 Dec 2004 01:18 PM (UTC) by David Berthiaume

Message
Ok, this table thing, I'm really digging it.

Bash           sliver    Street sense   sliver    Spelunking     sliver    
Enhanced damag sliver    Berserk        sliver    Hunt           sliver    
Shield block   sliver    Parry          sliver    Dodge          sliver    
Master mace    sliver    Expert mace    sliver    Mace           sliver    
Mountaineering sliver
Is my current skill list, it's not long at the moment, this I know. However, The way the mud works, it's not in ANY kind of order at all. I was thinking, can serialize save this, and then use another function to alphabitize the list?

And example of a LONG List:
          Spells/Songs
Name           Mastery   Name           Mastery   Name           Mastery
Acid blast     master    Acidproof      advanced  Mana charge    expert    
Anchor gate    advanced  Animate golem  advanced  Animate weapon advanced  
Magic armor    advanced  Bind golem     advanced  Blink          master    
Blindness      master    Blood fiend    skilled   Burning hands  advanced  
Call lightning advanced  Calm           advanced  Cancellation   master    
Chain lightnin expert    Charge weapon  expert    Charm person   expert    
Chill touch    skilled   Cloth form     advanced  Cloud kill     advanced  
Colour spray   expert    Common cold    skilled   Cone of cold   master    
Confusion      master    Continual ligh advanced  Control weathe expert    
Create food    expert    Create ioun    advanced  Create rabbit  master    
Create rose    skilled   Create spring  expert    Cryogenesis    advanced  
Dampener       advanced  Dark sight     advanced  Delayed blast  advanced  
Detect comfort advanced  Detect evil    advanced  Detect good    advanced  
Detect invis   advanced  Detect magic   advanced  Detect undead  advanced  
Dispel magic   advanced  Enduring light advanced  Empower staff  expert    
Encase fibers  master    Enchant accura advanced  Enchant armor  master    
Enchant damage advanced  Enchanted sigh advanced  Endurance      advanced  
Energy drain   expert    Faerie fire    advanced  Faerie fog     expert    
Farsight       advanced  Fear           expert    Fireball       master    
Firebane       master    Fireproof      advanced  Fly            master    
Floating disc  skilled   Frisky animati skilled   Frostbane      skilled   
Gate           master    Golem of ice   skilled   Golem of light advanced  
Ghoulish touch skilled   Giant strength expert    Glyph of encha average   
Globe of force master    Guardian       advanced  Haste          advanced  
Ice sphere     master    Identify       master    Imbue flame    expert    
Imbue frost    expert    Impact extenti skilled   Impart agility master    
Impart intelle master    Impart health  master    Impart strengt master    
Impart wisdom  master    Infravision    skilled   Invisibility   master    
Invisible stal advanced  Kenetic defens expert    Kenetic offens skilled   
Knock          advanced  Know alignment skilled   Levitate       master    
Life to mana   skilled   Lightning bolt master    Locate object  expert    
Locate person  master    Magic mirror   advanced  Magic missile  advanced  
Metal form     skilled   Minor malison  expert    Mass armor     advanced  
Mass fly       advanced  Mass haste     advanced  Mass invis     advanced  
Needle strike  advanced  Nightmares     advanced  Pass door      advanced  
Permanency     expert    Phase          expert    Poison         expert    
Pose old       advanced  Pose youth     skilled   Pose strength  advanced  
Pose weakness  advanced  Prismatic wall master    Random return  skilled   
Recharge       skilled   Refresh        master    Replenishment  advanced  
Resist paralys skilled   Seal gate      master    Searing orb    master    
Shatter proof  skilled   Suppress magic master    Ring of shield master    
Shocking grasp advanced  Sleep          expert    Slow           advanced  
Soften defense advanced  Sonic boom     master    Starburst      fair      
Starve spring  advanced  Stone form     advanced  Stone skin     master    
Talons         average   Teleport       master    Ventriloquate  skilled   
Water breathin skilled   Weaken         expert    Web            skilled   
Woopee         skilled

You try finding a particular spell... Now, I know at the moment it's alphabitized, but it's a random thing.
[Go to top] top

Posted by David Berthiaume   (202 posts)  [Biography] bio
Date Reply #1 on Mon 13 Dec 2004 01:18 PM (UTC)
Message
Update:

So I came up with a potential idea, on how to do this. Atleast get it into a table. I ran into problems with spacing however.

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

As you can see, very pretty format for the skills.

I got the gsub just fine, that wasn't nearly as hard as I thought it was.

<triggers>
<trigger
custom_colour="2"
keep_evaluating="y"
match="^(.*)\s+(sliver|slight|fair|basic|skilled|advanced|expert|master)\s+(.*)\s+(sliver|slight|fair|basic|skilled|advanced|expert|master)"
regexp="y"
repeat="y"
send_to="12"
sequence="100"
>
<send>s = string.gsub("%1", " ", "_")
Note (s)
st = string.gsub("%3", " ", "_")
Note (st)



--Note (SetVariable ("%1", "%2"))
--Note (SetVariable ("%3", "%4"))
</send>
</trigger>
<trigger
custom_colour="2"
keep_evaluating="y"
match="(.*)\s+(sliver|slight|fair|basic|skilled|advanced|expert|master)\s+(.*)\s+(sliver|slight|fair|basic|skilled|advanced|expert|master)\s+(.*)\s+(sliver|slight|fair|basic|skilled|advanced|expert|master)"
regexp="y"
send_to="12"
sequence="99"
>
<send>str = string.gsub("%5", " ", "_")
Note (str)
--Note (SetVariable (s, "%6"))</send>
</trigger>
</triggers>

THE BIGGEST PROBLEM!!! Is the frigg'n trigger itself. I can't figure out how to make it perfect. I'm stumped.

Once I get the trigger worked out and I make 50 million variables one for each skill/prof. Then I can continue further with the script.

Once I can make the variable, and it's contents working properly, I think it'll be fairly easy to set up a table, then from there, I'm sure it can't be too difficult to alphabitize it, then run it back through the gsub and reverse the process, putting it back up on the screen in the proper order. That's of course assuming I can figure out the rest of this.

What I still need to figure out would be how to make 2 tables at once, and get the code to recognize Table a.1.blah and table b.1.blag are what needs to be replacing whatever...

Triger matches *
Runs the script(not sure on how to compare a wildcard to a subtable in a table... That's the tricky part I think)
[Go to top] top

Posted by David Berthiaume   (202 posts)  [Biography] bio
Date Reply #2 on Mon 13 Dec 2004 01:46 PM (UTC)
Message
All righty, man, I should research BEFORE I make long postings... As taken from section 19.2 from the handbook
if Table a has the array (10, 20, 30) then the following line would insert at table.a.1

table.insert (a, 1, 15)

The new table a would be: (15, 10, 20, 30)

Basically, you can move the elements within the array, simply by inserting into the spot that you wish.

table.insert (a, 3, 45)

Table a's array would be: (15, 10, 45, 30, 30)

Conversely, table.remove will remove said element from the array. syntax would be: table.remove (a, 1)

You can use insert without position, that would simple add the element to the end of the array

table.insert (a, 500)

Once, I get the trigger sorted out, I won't even need to create variable's to accomplish what I want, I can go straight from wildcards straight into the table array.

Which I'm about to go test out. as soon as I post this.


[Go to top] top

Posted by David Berthiaume   (202 posts)  [Biography] bio
Date Reply #3 on Mon 13 Dec 2004 03:08 PM (UTC)
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

Posted by Shadowfyr   USA  (1,786 posts)  [Biography] bio
Date Reply #4 on Mon 13 Dec 2004 05:44 PM (UTC)
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.
[Go to top] top

Posted by David Berthiaume   (202 posts)  [Biography] bio
Date Reply #5 on Mon 13 Dec 2004 08:13 PM (UTC)
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 Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #6 on Mon 13 Dec 2004 08:27 PM (UTC)

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 Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #7 on Mon 13 Dec 2004 08:41 PM (UTC)
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  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #8 on Mon 13 Dec 2004 08:46 PM (UTC)
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 David Berthiaume   (202 posts)  [Biography] bio
Date Reply #9 on Mon 13 Dec 2004 09:07 PM (UTC)
Message
googled: Lua Script remove extra spaces

You came up first!
[Go to top] top

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

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 Reply #11 on Mon 13 Dec 2004 10:06 PM (UTC)

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 Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #12 on Mon 13 Dec 2004 11:04 PM (UTC)
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 Reply #13 on Mon 13 Dec 2004 11:18 PM (UTC)
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  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #14 on Mon 13 Dec 2004 11:26 PM (UTC)
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

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.


46,746 views.

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

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

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

[Best viewed with any browser - 2K]    [Hosted at HostDash]