[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]  Changing text color based on identifier

Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?
(New message)
Subject: Changing text color based on identifier
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 Deacla   USA  (42 posts)  [Biography] bio
Date Wed 14 Jul 2010 05:25 AM (UTC)  quote  ]
Message
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="MM_Auto_Sac"
   author="Deacla"
   id="dc109a6028175ae32994c209"
   language="Lua"
   save_state="y"
   purpose="Auto Sacs Items"
   requires="4.52"
   version="1.0"
   >
<description trim="y">
<![CDATA[

---------------------------------------------------------------------------
---------------------------++++++++++++++++--------------------------------

MUD: Materia Magica
Requires: serialize.lua

This plugin keeps a list of items that are to be automatically
sacrified when you grab them from a corpse. It also saves this list
when MUSHclient is closed. Command line usage is as follows:

ASI item name	(Adds Item 'item name' to the auto-sac list)
RSI item name	(Removes Item 'item name' to the auto-sac list)
sacson			(Turns on all auto-sac triggers)
sacsoff			(Turns off all auto-sac triggers)
sacshelp		(Displays this command list)

---------------------------++++++++++++++++--------------------------------
---------------------------------------------------------------------------

]]>
</description>
</plugin>

<triggers>
  <trigger
   enabled="y"
   group="AutoSacTriggers"
   match="^You take (.*) from the corpse of (?:.*)\.$"
   name="SacFromCorpse"
   regexp="y"
   send_to="12"
   sequence="90"
  >
  <send>

  if AutoSacItems["%1"] then			-- check item name for match in table
    Send ("drop '" .. "%1" .. "'")
    Send ("sac '" .. "%1" .. "'")
  end -- if

  </send>
 
 </trigger>
</triggers>
<aliases>
  <alias
   match="^ASI (.*)$"
   regexp="y"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>AutoSacItems["%1"] = true
Note ()
ColourTell("silver", "black", "Added '" .. "%1" .. "' to AutoSacItems list.")
Send ("")</send>
  </alias>
  <alias
   match="^RSI (.*)$"
   regexp="y"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>AutoSacItems["%1"] = nil
Note ()
ColourTell("silver", "black", "Removed '" .. "%1" .. "' from the AutoSacItems list.")
Send ("")</send>
  </alias>
  <alias
   match="^sacson$"
   regexp="y"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>EnableTriggerGroup ("AutoSacTriggers", true)
Note ()
ColourTell("silver", "black", "Sacs are now ON!.")
Send ("")</send>
  </alias>
  <alias
   match="^sacsoff$"
   regexp="y"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>EnableTriggerGroup ("AutoSacTriggers", false)
Note ()
ColourTell("silver", "black", "Sacs are now OFF!.")
Send ("")</send>
  </alias>
  <alias
   match="^sacshelp$"
   regexp="y"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>
Note ()
ColourTell("white", "black", "Auto_sac commands:")Note()
ColourTell("silver", "black", "ASI item name")Note("    (Adds Item 'item name' to the auto-sac list)")
ColourTell("silver", "black", "RSI item name")Note("    (Removes Item 'item name' to the auto-sac list)")
ColourTell("silver", "black", "sacson")Note("           (Turns on all auto-sac triggers)")
ColourTell("silver", "black", "sacsoff")Note("          (Turns off all auto-sac triggers)")
Send ()</send>
  </alias>
</aliases>

<script>
<![CDATA[

require "serialize"  -- needed to serialize table to string

AutoSacItems = {}  -- ensure table exists, if not loaded from variable

-- on plugin install, convert variable into Lua table

function OnPluginInstall ()
  assert (loadstring (GetVariable ("AutoSacItems") or "")) ()
end -- function OnPluginInstall

-- on saving state, convert Lua table back into string variable

-- save_simple is for simple tables that do not have cycles (self-reference)
-- or refer to other tables

function OnPluginSaveState ()
  SetVariable ("AutoSacItems", 
               "AutoSacItems = " .. serialize.save_simple (AutoSacItems))
end -- function OnPluginSaveState

]]>

</script>
</muclient>

--

working way to hard to play
[Go to top] top

Posted by Deacla   USA  (42 posts)  [Biography] bio
Date Tue 13 Jul 2010 11:41 PM (UTC)  quote  ]
Message
*Smacks forehead*
Doh! It's always something simple. Just had to add that one flag. I'll post the full plugin once I've fleshed out the alias's.

--

working way to hard to play
[Go to top] top

Posted by Nick Gammon   Australia  (18,797 posts)  [Biography] bio   Forum Administrator
Date Tue 13 Jul 2010 11:16 PM (UTC)  quote  ]
Message
Deacla said:

Does OnPluginInstall load every time the world opens or just the first time the plugin is installed?


After the plugin is installed, OnPluginInstall is called.

Did you set save_state="y" in the plugin header? Check the save state file (RH-double-click on the plugin in the plugin list).

- Nick Gammon

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

Posted by Deacla   USA  (42 posts)  [Biography] bio
Date Tue 13 Jul 2010 11:08 PM (UTC)  quote  ]
Message
Well my first goal is ease of use, and second portability. So I went back to the plugin and dropped the script file. It works while the world is open just like before. but the serialization is either not saving or its loading incorrectly. Does OnPluginInstall load every time the world opens or just the first time the plugin is installed? and now the code.
</plugin>

<triggers>
  <trigger
   enabled="y"
   match="^You take (.*) from the corpse of (?:.*)\.$"
   regexp="y"
   send_to="12"
   sequence="90"
  >
  <send>

  Note("%1") 							-- make a note to see what was captured

  if AutoSacItems["%1"] then			-- check item name for match in table
    Send ("drop '" .. "%1" .. "'")
    Send ("sac '" .. "%1" .. "'")
  end -- if

  </send>
 
 </trigger>
</triggers>
<aliases>
  <alias
   match="AddSacItem (.*)"
   regexp="y"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>AutoSacItems["%1"] = true
Note ("Added '" .. "%1" .. "' to AutoSacItems list.")</send>
  </alias>
</aliases>

<script>
<![CDATA[

require "serialize"  -- needed to serialize table to string

AutoSacItems = {}  -- ensure table exists, if not loaded from variable

-- on plugin install, convert variable into Lua table

function OnPluginInstall ()
  assert (loadstring (GetVariable ("AutoSacItems") or "")) ()
end -- function OnPluginInstall

-- on saving state, convert Lua table back into string variable

-- save_simple is for simple tables that do not have cycles (self-reference)
-- or refer to other tables

function OnPluginSaveState ()
  SetVariable ("AutoSacItems", 
               "AutoSacItems = " .. serialize.save_simple (AutoSacItems))
end -- function OnPluginSaveState

]]>

</script>

I copy/pasted the last bit from your serialization topic and plugged in my own variable name. My plan is to expand on this functionality once the core is working, then passing it on all the other MM'ers on MUSH..

--

working way to hard to play
[Go to top] top

Posted by Nick Gammon   Australia  (18,797 posts)  [Biography] bio   Forum Administrator
Date Tue 13 Jul 2010 09:09 PM (UTC)  quote  ]
Message
Read this about serialization:

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


In a plugin it is simple (there is example code in that thread). Basically serialize.save will convert a Lua table into a string, which you can then put in a MUSHclient variable and have that saved. You can also do it in the main script file as there is a world file callback for "world save". But plugins are safer because the plugin state file is always saved. Plus plugins are easier to share between worlds or other players.

To get it back next time outside a plugin, use the "world open" script callback. In a plugin use the OnPluginInstall function callback.

- Nick Gammon

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

Posted by Deacla   USA  (42 posts)  [Biography] bio
Date Tue 13 Jul 2010 08:38 PM (UTC)  quote  ]
Message
So going off of what Nick was saying, I ditched the plugin and am now trying to get this idea to work using the 'edit trigger' and 'edit alias' dialog boxes. If i use the little 'copy' buttons it gives me this:

<triggers>
  <trigger
   enabled="y"
   group="sac_items"
   match="^You take (.*?) from the corpse of (?:.*)\.$"
   regexp="y"
   send_to="12"
   sequence="99"
  >
  <send>
if AutoSacItems["%1"] then
  Send ("drop '" .. "%1" .. "'")
  Send ("sac '" .. "%1" .. "'")
end
</send>
  </trigger>
</triggers>

The trigger now works great outside of a plugin.

<aliases>
  <alias
   match="AddSacItem *"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>AutoSacItems["%1"] = true
Note ("Added '" .. "%1" .. "' to AutoSacItems list.")</send>
  </alias>
</aliases>

This alias works great too as does another alias 'DelSacItem *'. I am able to add/remove items from the AutoSacItems table while the world is open. my problem now is getting the save state code to work and where do I put it so the table will still be available when i next load the world?

--

working way to hard to play
[Go to top] top

Posted by Twisol   USA  (2,230 posts)  [Biography] bio
Date Tue 13 Jul 2010 10:06 AM (UTC)  quote  ]
Message
Whoops, I totally missed that "Plugin" bit in the error... Good catch.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Nick Gammon   Australia  (18,797 posts)  [Biography] bio   Forum Administrator
Date Tue 13 Jul 2010 10:00 AM (UTC)  quote  ]
Message
Deacla said:

Run-time error
Plugin: MM_Auto_Sac (called from world: Materia Magica)


Plugins are in a different script space from the main world script file.

I would look up about serialization in Lua, that is the way to go with saving variables from one session to another.

- Nick Gammon

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

Posted by Twisol   USA  (2,230 posts)  [Biography] bio
Date Tue 13 Jul 2010 09:53 AM (UTC)  quote  ]
Message
Hm. That's odd. Make sure nothing is overwriting it, like an older alias or trigger. Also make sure the table isn't declared with the 'local' keyword.

World Events is for scripting callbacks, like when the world is connecting or disconnecting and such. It's unnecessary for merely having the file executed when the world starts.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Deacla   USA  (42 posts)  [Biography] bio
Date Tue 13 Jul 2010 09:49 AM (UTC)  quote  ]
Message
sacs.lua is located at C:\Program Files\MUSHclient\worlds\plugins\sacs.lua
I created it using the 'new' button in configuration -> scripts. The file name is there in the 'Script File' box. I access it via CTRL+Shift+H, edit it, and when i save it asks if i want to reload it. Should i also put that filename elsewhere? Like in the 'World Events' somewhere?

--

working way to hard to play
[Go to top] top

Posted by Twisol   USA  (2,230 posts)  [Biography] bio
Date Tue 13 Jul 2010 09:34 AM (UTC)  quote  ]

Amended on Tue 13 Jul 2010 09:37 AM (UTC) by Twisol

Message
I'm guessing sacs.lua is never even executed. Where is sacs.lua located? What file is shown in the "script file" field at Game -> Configure -> Scripting? The script file is a file you associate with a world, and is run when the world is loaded or when the file itself is manually reloaded. You need to put your Sac_Items definition in that specific file.

EDIT: A bit unrelated, but you're using "%1" there when it seems like you'd want "%<item_name>" instead.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Deacla   USA  (42 posts)  [Biography] bio
Date Tue 13 Jul 2010 09:27 AM (UTC)  quote  ]
Message
Okay i'm trying to solve a different issue using the same basic concept. Following your outline i put this in a script named sacs.lua:

Sac_Items = {
  ["an iron cleaver"] = true
}

I don't really know how this works but it compiled fine. then I have this trigger in a separate plugin file:

<triggers>
  <trigger
   enabled="y"
   match="^You take (?P<item_name>.*) from the corpse of (?:.*)\.$"
   regexp="y"
   send_to="12"
   sequence="90"
  >
  <send>

if Sac_Items["%1"] then
  Send ("drop '" .. "%1" .. "'")
  Send ("sac '" .. "%1" .. "'")
end

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

But I keep getting this error:

Run-time error
Plugin: MM_Auto_Sac (called from world: Materia Magica)
Immediate execution
[string "Trigger: "]:3: attempt to index field 'Sac_Items' (a nil value)
stack traceback:
        [string "Trigger: "]:3: in main chunk

So I'm obviously not defining my variable correctly or passing the data at all...

--

working way to hard to play
[Go to top] top

Posted by Twisol   USA  (2,230 posts)  [Biography] bio
Date Mon 12 Jul 2010 09:41 AM (UTC)  quote  ]

Amended on Mon 12 Jul 2010 09:43 AM (UTC) by Twisol

Message
Put those table definitions in your script file instead of in the trigger itself. If it's in the trigger, it'll be executed every time, and hence recreated every time. If you put it in the script file (Game -> Edit Script File, or create one if you haven't yet in Game -> Configure -> Scripting), then it'll only be created when the world is loaded or when you explicitly reload it (Game -> Reload Script File).

With that done, you can create aliases that use Clannies["name"] = true and Clannies["name"] = nil to add and remove the names. After that you'll want some way to save the state of the tables between sessions, but one step at a time. :)



As a side-note, I'm working on a highlighting library script that should make it much easier to colour parts of lines. It's an interesting task to tackle and I've already hit some road-bumps, but the core mechanism is hardly any different from what I showcased above.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
[Go to top] top

Posted by Deacla   USA  (42 posts)  [Biography] bio
Date Mon 12 Jul 2010 09:36 AM (UTC)  quote  ]
Message
I got this to work. but it's kind of clunky if these lists are going to change.

<triggers>
  <trigger
   enabled="y"
   match="^(.*?) tells you '(.*?)'$"
   omit_from_output="y"
   regexp="y"
   send_to="14"
   sequence="90"
  >
  <send>

Administrators_Color = "white"
Clannies_Color = "magenta"
Allies_Color = "blue"
Friends_Color = "green"
Others_Color = "yellow"

Administrators = {
  ["Duende"] = true,
  ["Vassago"] =	true,
  ["add name here"] = true,
}

Clannies = {
  ["Deacla"] = true,
  ["Tuck"] = true,
  ["add name here"] = true,
}

Allies = {
  ["Alyce"] = true,
  ["Deathsmasher"] = true,
  ["add name here"] = true,
}

Friends = {
  ["add friends name"] = true,
  ["add friends name"] = true,
  ["add friends name"] = true,
}




if Administrators["%1"] then
  for _,v in ipairs(TriggerStyleRuns) do
    ColourTell(Administrators_Color, RGBColourToName(v.backcolour), v.text)
  end
elseif Clannies["%1"] then
  for _,v in ipairs(TriggerStyleRuns) do
    ColourTell(Clannies_Color, RGBColourToName(v.backcolour), v.text)
  end
elseif Allies["%1"] then
  for _,v in ipairs(TriggerStyleRuns) do
    ColourTell(Allies_Color, RGBColourToName(v.backcolour), v.text)
  end
elseif Friends["%1"] then
  for _,v in ipairs(TriggerStyleRuns) do
    ColourTell(Friends_Color, RGBColourToName(v.backcolour), v.text)
  end
else
  for _,v in ipairs(TriggerStyleRuns) do
    ColourTell(Others_Color, RGBColourToName(v.backcolour), v.text)
  end
end


Note() -- finish the line

</send>
 
 </trigger>

</triggers>

it would be great if you could add a name to the tables with aliases like 'AddClannie Billy' would add Billy to the Clannie table until you used 'RemoveClannie Billy' to remove him , but i'm not that good yet.

--

working way to hard to play
[Go to top] top

Posted by WillFa   USA  (517 posts)  [Biography] bio
Date Sun 11 Jul 2010 11:27 PM (UTC)  quote  ]
Message
make 4 triggers like.

<triggers>
<trigger
custom_colour="7"
enabled="y"
expand_variables="y"
match="^(!@Admins) tells you \: \'*'$"
regexp="y"
sequence="100"
>
</trigger>
</triggers>


And then make Variables like Admins (in the above example) in the format:

Joe|Jim|Bob|Nick

Give Admins a sequence number lower than 100 if you want them to be more important in your coloring...
[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.


3,926 views.

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

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