Defence list for Achaea

Share your scripts and packages with other Mudlet users.
Post Reply
goose
Posts: 13
Joined: Sun Jun 14, 2009 2:51 pm

Defence list for Achaea

Post by goose »

I was messing around, seeing how far along Mudlet has gotten and ended up with this. Seems to work okay and someone might be interested in it, so I thought I'd post it.

It's basically an alias, ^def$ which sends "defences" and displays the result in a slightly more appealing (for me) format, like so:
screen.jpg
screen.jpg (43.79 KiB) Viewed 4309 times
Any class defs/blessings/etc. you'll have to put in yourself. Just add the defence name to the "def" table, and make a trigger that does defDef("weathering") on the message you see when you normally type DEF.

I'm no Mudlet expert obviously, so there's probably loads of things I did the wrong/hard way, but so far it seems to work for me. I also couldn't quite figure out the export button and had to manually paste together a bunch of things, so it might not work at all.
Attachments
def_display.xml
(27.01 KiB) Downloaded 604 times

Xixity
Posts: 6
Joined: Sun May 24, 2009 3:38 pm

Re: Defence list for Achaea

Post by Xixity »

This is great actually and it works well enough for me. Anyone know of a way to organize them though?

User avatar
Vadi
Posts: 4759
Joined: Sat Mar 14, 2009 3:13 pm

Re: Defence list for Achaea

Post by Vadi »

It's a bit complicated to do. Lua doesn't care about the order of this type here (where it's key = value), so the way it saves them is rather random.

It is possible to make it go in an orderly fashion, someone suggested to try "so instead of "for k,v in pairs(t) do ..." you can do "for i,k in ipairs(keys) do local v = t[k]; if v ~= nil then ..."" but I haven't figured that out yet (didn't seem to be working for me).

Caled
Posts: 401
Joined: Thu Apr 09, 2009 4:45 am

Re: Defence list for Achaea

Post by Caled »

Vadi wrote:It's a bit complicated to do. Lua doesn't care about the order of this type here (where it's key = value), so the way it saves them is rather random.

It is possible to make it go in an orderly fashion, someone suggested to try "so instead of "for k,v in pairs(t) do ..." you can do "for i,k in ipairs(keys) do local v = t[k]; if v ~= nil then ..."" but I haven't figured that out yet (didn't seem to be working for me).
As I understand it, the ipairs solution won't work, because it requires a numbered list, which doesn't give you the option of saying whether a def is present/absent. You would need two lists, the one you have already, and a numbered list to hold the order.

Thylacine
Posts: 28
Joined: Sun May 10, 2009 5:04 am

Re: Defence list for Achaea

Post by Thylacine »

Caled wrote:
Vadi wrote:It's a bit complicated to do. Lua doesn't care about the order of this type here (where it's key = value), so the way it saves them is rather random.

It is possible to make it go in an orderly fashion, someone suggested to try "so instead of "for k,v in pairs(t) do ..." you can do "for i,k in ipairs(keys) do local v = t[k]; if v ~= nil then ..."" but I haven't figured that out yet (didn't seem to be working for me).
As I understand it, the ipairs solution won't work, because it requires a numbered list, which doesn't give you the option of saying whether a def is present/absent. You would need two lists, the one you have already, and a numbered list to hold the order.
That's what that code does, caled. Table keys is a standard ipairs-compatible indexed array.

For example,

Code: Select all

--Of course with 2 keys, they might get ordered properly anyway
t = {}
t["bar"] = "randomThing1"
t["foo"] = "randomThing2"

--foo always before bar (lame, I know)
keys = {"foo", "bar"}

--Now print, as an example
for index,key in ipairs(keys) do 
    local val = t[key]; 
    if val ~= nil then 
        echo(key .. "=" .. t[key] .. "\n")
    end
end
That way, foo is always before bar as "foo" is assigned to the first index in an array, which can be safely read using ipairs(). Problem is potential overhead, might not be much but, with larger tables, it could be a problem...

You could mash those together into one table if you wanted, although it'd be even slower (more work) and would be rather messy to access -- you'd almost need a getKey()/getValue option as I don't know if lua supports operator overloading...

I'm not sure, you might be able to use a linked list of some sort. Although randomly accessing elements wouldn't be so simple...

isuka
Posts: 1
Joined: Fri Jun 26, 2009 5:29 am

Re: Defence list for Achaea

Post by isuka »

Vadi wrote:It's a bit complicated to do. Lua doesn't care about the order of this type here (where it's key = value), so the way it saves them is rather random.

It is possible to make it go in an orderly fashion, someone suggested to try "so instead of "for k,v in pairs(t) do ..." you can do "for i,k in ipairs(keys) do local v = t[k]; if v ~= nil then ..."" but I haven't figured that out yet (didn't seem to be working for me).

Code: Select all

if not php then php = {} end

function php:Table(...)
    local newTable,keys,values={},{},{}
    newTable.pairs=function(self) -- pairs iterator
        local count=0
        return function()
            count=count+1
            return keys[count],values[keys[count]]
        end
    end
    newTable.count=function(self) -- count return
    	return table.getn(keys)
    end
    setmetatable(newTable,{
        __newindex=function(self,key,value)
            if not self[key] then table.insert(keys,key)
            elseif value==nil then -- Handle item delete
                local count=1
                while keys[count]~=key do count = count + 1 end
                table.remove(keys,count)
            end
            values[key]=value -- replace/create
        end,
        __index=function(self,key) return values[key] end
    })
    return newTable
end
Usage:

Code: Select all

local testArry = php:Table()
testArry["test1"] = "another test"
testArry["test2"] = "one more"
for k,v in testArry:pairs() do print(k,v) end
This is existing code that I've slightly modified to include counters (array:count()). The idea is to make lua tables work like php tables, which maintain the order elements were saved in without needing to use numeric keys.

User avatar
Vadi
Posts: 4759
Joined: Sat Mar 14, 2009 3:13 pm

Re: Defence list for Achaea

Post by Vadi »

That's a neat tip. Thanks!

Post Reply