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


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Pulling along set of directions

Pulling along set of directions

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


Posted by Thalir   (13 posts)  [Biography] bio
Date Wed 15 Aug 2007 12:36 AM (UTC)
Message
For example, I have a set of different directions running from my home to city A, b, C:
n, e, s, w, u, d <--- in this format

If I pull a cart north, there are two messages:
i) You pull a cart north <---- When I start
ii) You have pulled a cart north <---- Once I've done pulling and am in the next room.

How might I tackle this, to trigger cart pulling along the set of directions I provide, plus recognize I've moved into a new room and use the next available direction?

Would it be possible to store these dirs in a text file and read from them?

I'm guessing the solution is to somehow use the key order by putting the dirs in a lua table.
=================================
In summary:
-How to pull a cart, following the directions?
-How to switch the set of directions (cities)?
-And a way to stop and start, but still know which dir I'm up to?
================================
I'd also like to express my utmost gratitude for all those helpful people replying to my previous posts, I am very glad mushclient has such great technical support.


[Go to top] top

Posted by Shaun Biggs   USA  (644 posts)  [Biography] bio
Date Reply #1 on Wed 15 Aug 2007 01:43 AM (UTC)
Message
I would just create a table with the directions listed in order. cartdirs={"n", "e", "s", "w", "u", "d"} Then have a regex trigger match="^You pull a cart (north|south|east|west|up|down)$"

Then just have a function like this:

function nextpull()
  if #cartdirs > 0 then
    Send( "pull cart "..cartdirs[1] )
    table.remove( cartdirs, 1 )
  end
end


To make it start or resume, just have an alias to enable the trigger and send the first direction. To pause it, just disable the trigger.

It is much easier to fight for one's ideals than to live up to them.
[Go to top] top

Posted by Thalir   (13 posts)  [Biography] bio
Date Reply #2 on Wed 15 Aug 2007 02:17 AM (UTC)
Message
Thanks, I'd like to ask, what does
#cartdirs > 0
mean? Is that a check to see if the table is empty
[Go to top] top

Posted by Shaun Biggs   USA  (644 posts)  [Biography] bio
Date Reply #3 on Wed 15 Aug 2007 02:24 AM (UTC)
Message
You are correct. #varname tells you how many elements are in the table. if #cartdirs == 0, then the table is empty.

It is much easier to fight for one's ideals than to live up to them.
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #4 on Wed 15 Aug 2007 04:53 AM (UTC)
Message
As a minor note, the #table syntax only works for arrays i.e. tables with only numeric keys. And strictly speaking, the # operator also only returns the highest integer key in the contiguous range starting from 1. (At least, for sparse arrays.)

E.g.

[david@thebalrog:~]$ cat test.lua
t = {}
t.a = 1
t.b = 1
print(#t)
t = {}
t[1] = 1
t[2] = 1
print(#t)
t[10] = 1
print(#t)
[david@thebalrog:~]$ lua test.lua
0
2
2


This can be tricky, because e.g.

[david@thebalrog:~]$ cat test.lua
t = {}
t[10] = 1
print(#t)
[david@thebalrog:~]$ lua test.lua
0


The proper way to check if a table is empty, "proper" meaning one that will always work, is to see if next returns nil for that table. If next(t) == nil, then the table is empty; if next(t) ~= nil, the table has something in it.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Shaun Biggs   USA  (644 posts)  [Biography] bio
Date Reply #5 on Wed 15 Aug 2007 05:24 AM (UTC)
Message
True, but in this case, the array has to be sequential and starting with an index of 1 for the function to work, so #t will do the job better than next(t) will. It is important to make the distinction between the two though, so thanks for clarifying.

It is much easier to fight for one's ideals than to live up to them.
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #6 on Wed 15 Aug 2007 05:46 AM (UTC)
Message
Main reason I pointed it out was that I've been bitten in the neck a few times by this. It's interesting to follow Lua list traffic a bit because this is something of a point of contention.

For instance, if you set indices 1, 2 and 4, you get #t == 4, but if you set 1, 2 and 10, you get #t == 2... some people think the # operator isn't quite right because it doesn't always do what you think it should do, but then sometimes it does something you thought it wasn't supposed to do, and all that stuff.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Shaun Biggs   USA  (644 posts)  [Biography] bio
Date Reply #7 on Wed 15 Aug 2007 06:16 AM (UTC)
Message
You can also use table.setn(t, 20) to set the size of the table to 20 elements. At that point, you can have 2 elements filled, and #t would return 20. And even then table.remove(t) will remove the item in #t (even if there was nothing there to begin with), so #t will then return 19. Point being... be careful with data set construction and proper removal of elements.

It is much easier to fight for one's ideals than to live up to them.
[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.


18,799 views.

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]