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 ➜ Lua ➜ Is there an easier way?

Is there an easier way?

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


Posted by Garrion   (21 posts)  Bio
Date Tue 08 Dec 2009 03:47 AM (UTC)
Message
Hi all,

I have made a plugin for my mud that shows my xp rate in a graph form. This was done more as a learning experience than anything but you can see what it looks like here: http://discworld.imaginary-realities.com/garrion/Graph.jpg

The issue I have is my code is very messy and I was wondering if there was an easier way to deal with my tables than what I have done. The code for the tables is below.

This code is in a function that fires every minute to update the graph:


	-- This moves the graph points along one tick when the timer fires
	t59 = t58
	t58 = t57
	t57 = t56
	t56 = t55
	t55 = t54
	t54 = t53
	t53 = t52
	t52 = t51
	t51 = t50
	t50 = t49
	t49 = t48
	t48 = t47
	t47 = t46
	t46 = t45
	t45 = t44
	t44 = t43
	t43 = t42
	t42 = t41
	t41 = t40
	t40 = t39
	t39 = t38
	t38 = t37
	t37 = t36
	t36 = t35
	t35 = t34
	t34 = t33
	t33 = t32
	t32 = t31
	t31 = t30
	t30 = t29
	t29 = t28
	t28 = t27
	t27 = t26
	t26 = t25
	t25 = t24
	t24 = t23
	t23 = t22
	t22 = t21
	t21 = t20
	t20 = t19
	t19 = t18
	t18 = t17
	t17 = t16
	t16 = t15
	t15 = t14
	t14 = t13
	t13 = t12
	t12 = t11
	t11 = t10
	t10 = t9
	t9 = t8
	t8 = t7
	t7 = t6
	t6 = t5
	t5 = t4
	t4 = t3
	t3 = t2
	t2 = t1
	t1 = t0


Hopefully you can see what I am talking about here. I didn't want to post the whole plugin and spam up the screen.
Top

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #1 on Tue 08 Dec 2009 04:10 AM (UTC)

Amended on Tue 08 Dec 2009 04:14 AM (UTC) by Nick Gammon

Message
I don't see a table there, but yes there is an easier way.

You have 60 points, right? So you need a table, eg.


points = {}


Now each time you add to it, and remove the oldest entry if it is full. eg.


-- if full, remove the earliest entry
if #points >= 60 then
  table.remove (points, 1)
end -- if

-- add new entry to the end
table.insert (points, n)  -- n is the new value


Now the table has points in the range 1 (the first, not zero) to #points (the number of items in the table).

For example, to display it:


for k, v in ipairs (points) do
  print (v)  -- v is the value of this point
end -- for


The ipairs function is designed to iterate through a table from 1 to the end of it, provided the keys are numeric, start at 1, and don't have gaps.

- Nick Gammon

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

Posted by Garrion   (21 posts)  Bio
Date Reply #2 on Tue 08 Dec 2009 04:21 AM (UTC)
Message
Sorry. I knew I wouldn't make myself clear. Each of the points is a table that is holding a number. These numbers are used to plot a line on the miniwindow so it gives a line graph.

The table t0 is updated constantly with my current xp rate. The code above moves the values of all the table one table back (eg. t1=10). so the old value of t0 is now in t1 and so on.

Would the fix you gave me work in this way? If so how would I refer to the point values in the WindowLine () as a position?
Top

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #3 on Tue 08 Dec 2009 04:26 AM (UTC)
Message
I think my code does what you want. The table will consist of point values, and the latest one is the most recent. Where I did the table.insert is inserting the most recent value (t0 value in your terminology).

- Nick Gammon

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

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #4 on Tue 08 Dec 2009 04:30 AM (UTC)

Amended on Tue 08 Dec 2009 04:36 AM (UTC) by Twisol

Message
What you want is a "sliding window" of points over time, am I right? What Nick suggested would be perfect, and you absolutely don't need to maintain each point separately as you seem to have been doing. You contain all of the current points in another table (Nick's 'points', or perhaps 'graph'), and treat it as a queue (like the ones at amusement parks). Each time you add a point, it steps onto the back of the queue. Once you hit the maximum number of points the queue should hold - sixty, in this case - then the front point gets removed when a new point is added to the back.

For clarity, the only thing I would change in Nick's example is the example display loop:


for _, point in ipairs(points) do
  print(point.value)
end -- for


I hope that clarifies things!

EDIT: One last point (no pun intended), with this technique, your old t59 would equate to the new points[60]. Lua's tables are one-based rather than zero-based.

EDIT 2: There's actually no need to make each point a table on its own, if each one just contains a single number. Just insert the numbers instead of the tables. If that was the case (and if that's what Nick intended), my version of the loop is incorrect, versus Nick's.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #5 on Tue 08 Dec 2009 04:37 AM (UTC)
Message
I think his points are individual values (not x and y). The x is the time component, and the y is the value.

So in other words, point 1 is displayed at x = 1, y = n where n is the value of points [1]. The value n would be scaled to fit the graph (eg. if xp / hour is in the range 1 to 10000, and the graph is 60 pixels high you would divide by 10000 and multiply by 60).


- Nick Gammon

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

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #6 on Tue 08 Dec 2009 04:41 AM (UTC)
Message
Yeah... I hoped I had edited before anyone noticed. >_>

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
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,969 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.