About Us
Products
Purchase
Downloads
Support
Forums
Contact Us
Site
 Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
Progonoi
Magician


Joined: 28 Jan 2007
Posts: 430

PostPosted: Sat Aug 16, 2008 1:50 pm   

Old ZMud "function" conversion for CMud.
 
Here's the deal, this is the piece of code that worked under ZMud but I don't really have no idea what to do with it under CMud.

Code:

<var name="roomlists" id="905">%exec( "#loopdb @zoneDB {#if ((%int( %item( %db( @zonedb, %key), 4))-@{stages.@mystage}<=%int( @MyCurrentLevel)) and (%int( %item( %db( @zonedb, %key), 5))-@{stages.@mystage}>=%int( @MyCurrentLevel))) {%key}}")</var>


It loops through db, makes calculations if minimum and maximum levels of zones within this db are in comparison OK with my
current level, in case they are, keys in question are returned in a form of string list.
(Stages is a string list containing values such as: 1|2|4|6, mystage can be either 1, 2,3 or 4)

I tried with #func and #varfunc but to no avail. Chances are I'm just not understanding how they work yet and can't wrap my brain around it.

Thanks in advance.

Prog
Reply with quote
Tech
GURU


Joined: 18 Oct 2000
Posts: 2733
Location: Atlanta, USA

PostPosted: Sat Aug 16, 2008 4:11 pm   
 
Try this

Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<cmud>
  <func name="roomlists" copy="yes">
    <value>$return = ""
#loopdb @zoneDB {#if ((%int( %item( %db( @zonedb, %key), 4))-@{stages.@mystage}<=%int( @MyCurrentLevel)) and (%int( %item( %db( @zonedb, %key), 5))-@{stages.@mystage}>=%int( @MyCurrentLevel))) {$return=%key}}
#RETURN $return</value>
  </func>
</cmud>


You don't need to use %exec since we have proper functions, and since your commands were then quoted strings, it was hard to determine what worked and what didn't. I don't have the necessarily variables to test, but it should be a good starting point for you.
_________________
Asati di tempari!
Reply with quote
Progonoi
Magician


Joined: 28 Jan 2007
Posts: 430

PostPosted: Sat Aug 16, 2008 6:20 pm   
 
Well, this one did compile the way it should, but.

How can I see the output? With old code, it was sufficent to do "#show @roomlists" and I could saw the list that was formed.

I'm betting its again something I haven't learned to do yet.

Just in case, thats the piece of code I use for testing atm.

Code:

<class name="testing" id="1820">
  <var name="testdb" type="Record" id="1822">Area 1="150|155"|Area 2="145|150"</var>
  <var name="testmystage" type="Integer" id="1823">1</var>
  <var name="teststages" type="StringList" id="1824">0|2|3|3|4|4|5</var>
  <func name="func" id="1825">
    <value>$return = ""
#loopdb @testdb {#if ((%int( %item( %db( @testdb, %key), 1))-@{teststages.@testmystage}<=%int( @MyCurrentLevel)) and (%int( %item( %db( @testdb, %key), 2))-@{teststages.@testmystage}>=%int( @MyCurrentLevel))) {$return=%key}}
#RETURN $return</value>
  </func>
</class>


EDIT: Oh and MyCurrentLevel has the value of 155.

Btw, I actually start to understand the way new functions work, too. I just didn't come out with local var and correct #return setup on my own. Setting all this wrongly was I suppose primary reason I couldn't figure it out.

Thanks a lot for help.

Prog
Reply with quote
Tech
GURU


Joined: 18 Oct 2000
Posts: 2733
Location: Atlanta, USA

PostPosted: Sun Aug 17, 2008 5:27 am   
 
Simply doing a #SHOW @roomlist() should do the trick. And function are awesome, experience the power Very Happy
_________________
Asati di tempari!
Reply with quote
Progonoi
Magician


Joined: 28 Jan 2007
Posts: 430

PostPosted: Sun Aug 17, 2008 12:08 pm   
 
Ahh, using () after the name...

Well, anyway, I have a feeling that my logic may be wrong because even doing #show @func() doesn't show any result though it should.
Same math logic yield a result under ZMud.

Code:

<class name="testing" id="1820">
  <var name="testdb" type="Record" id="1822">Area 1="200|201"|Area 2="200|201"</var>
  <var name="testmystage" type="Integer" id="1823">1</var>
  <var name="teststages" type="StringList" id="1824">0|2|3|3|4|4|5</var>
  <func name="func" id="1828">
    <value>$return=""
#loopdb @testdb {#if ((%int( %item( %db( @testdb, %key), 1))-@{teststages.@testmystage}<=%int( @MyCurrentLevel)) and (%int( %item( %db( @testdb, %key), 2))-@{teststages.@testmystage}>=%int( @MyCurrentLevel))) {$return=%key}}
#RETURN $return</value>
  </func>
</class>


Basically it should be "#if 200-0 < 201 (mycurrentlevel) and 201-0 >= 201, return keys."

Maybe there's something minor I just can't seem to catch under CMud...


Prog
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5182

PostPosted: Sun Aug 17, 2008 5:49 pm   
 
First based on your second post stating that the variable MyCurrentLevel holds "155", I can unequivocably say that your #IF will never be true. This means $return will always remain defined to "".

Second the logic used in the #IF seems overly processor intensive. You use of "%db(@testdb,%key)" makes no sense when %val would contain the same value, eliminate a fair bit of unnecessary code, and be much faster. I expect that the use of %int could also be eliminated as CMud is much smarter about this then zMud ever was.

Finally, the #LOOPDB will run through the entire variable leaving $return set to the last %key that matched the conditions in the #IF. It would be faster to use a #BREAK to end the loop when the first matching condition is found. This command is a rather recent addition to CMud, and the change in behavior is something you might consider.
_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
Progonoi
Magician


Joined: 28 Jan 2007
Posts: 430

PostPosted: Sun Aug 17, 2008 7:10 pm   
 
Sorry, it was my bad not to give new MyCurrentLevel value with new code paste. Its 201. So unless I'm completely off, it should work.
Sorry again for the confusion.

I have to think about other stuff you brought up and get back to you, though.


Prog
_________________
The Proud new owner of CMud.

--------------------------------
Intel Core i5-650 3,2GHz
4 DD3 RAM
GTX 460 768MB
Win 7 Home Premium 64x
--------------------------------
Reply with quote
Progonoi
Magician


Joined: 28 Jan 2007
Posts: 430

PostPosted: Sun Aug 17, 2008 7:39 pm   
 
Well, I removed the %ints but there's still nothing.

I won't start to think about speed until I get it to work, but #break idea seems a very nice one indeed.

However, I don't understand what you mean by your %val talk. Testdb holds two values. Both of them are used for evaluating if keys are OK to use or not.

If First value of a given key is 200 and teststages minus myteststage gives the value of 0 (%item(@teststages, @mystage), where mystage is 1 which is first item in teststages list). Ergo, 200 is < than 201. Second value of a given key is 201, teststages minus myteststage also gives 0 so it remains to 201 (just as 200 did). And 201 is >= than 201.

Seriously, maybe I'm missing something here, but this code has always worked 100% under ZMud however badly it had the need to be tailored before hand with %int etc for ZMud to understand.

Code:

<class name="testing" id="1820">
  <var name="testdb" type="Record" id="1822">Area 1="200|201"|Area 2="200|201"</var>
  <var name="testmystage" type="Integer" id="1823">1</var>
  <var name="teststages" type="StringList" id="1824">0|2|3|3|4|4|5</var>
  <func name="func" id="1828">
    <value>$return=""
#loopdb @testdb {#if ((%item( %db( @testdb, %key), 1)-@{teststages.@testmystage}<=@MyCurrentLevel) and (%item( %db( @testdb, %key), 2)-@{teststages.@testmystage}>=@MyCurrentLevel)) {$return=%key}}
#RETURN $return</value>
  </func>
  <var name="MyCurrentLevel" id="1829">201</var>
</class>


I'm totally sure that I'm missing something trivial here, I just can't seem to catch it at all.

Prog
Reply with quote
Progonoi
Magician


Joined: 28 Jan 2007
Posts: 430

PostPosted: Sun Aug 17, 2008 8:43 pm   
 
Well, I figured I'll just write a separate post otherwise one would become huge with all the edits.

I think I may have figured it out but I'm still not too sure. As Vji mentioned, $return captures the last match. That it does, but its not really what I need. At the risk of sounding like an old record - back in ZMud, this code was able to come up with a string list in case there were more than one match. Maybe thats where #break comes in and parses the eventual list, I don't know. Haven't looked into #break yet.

However, the code below works the way it should (the fact that only one match is returned excluded, but thats not the fault of the code).

Code:

<class name="test2" id="1830">
  <func name="func2" id="1831">
    <value>#local $return $testLevel $mystage
$return=""
$mystage=1
$testLevel=10
#loopdb @testdb2 {#if ($testLevel>(%item(%db(@testdb2,%key),1)-%item(@stages,@mystage)) and ($testLevel>=%item(%db(@testdb2,%key),2)-%item(@stages,@mystage))) {$return=%key}}
#RETURN $return</value>
  </func>
  <var name="testdb2" type="Record" id="1835">Area 1="5|10"|Area 2="5|10"</var>
  <var name="stages" type="StringList" id="1836">0|1|2|4</var>
</class>


All I did now was switch current level (testLevel local variable here) around inside if. Evaluation used to come after the math, not before.
And now it seems to work the way it should. Ironically, I recall that this layout didn't work under ZMud and was the reason why I ended up using the setup I had.

Anyone who can explain this to me is much appreciated.

Anyway, just figured I'd let you guys know where I'm at.

EDIT: Went through #break help and tried it out. If I understand correctly, it works as a loop stopper. So if I'd put break after $return=%key, return would show only first match. Its not what I need. I need both matches to be returned. Is there something about #break I'm missing?


Prog
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5182

PostPosted: Sun Aug 17, 2008 11:04 pm   
 
Quote:
Went through #break help and tried it out. If I understand correctly, it works as a loop stopper. So if I'd put break after $return=%key, return would show only first match. Its not what I need. I need both matches to be returned. Is there something about #break I'm missing?
Yes, that is the behavior of #BREAK. "$return=%key" will only ever show 1 match, so there is nothing about #BREAK you are missing here.

Your desire to have multiple matches was not covered in your original request, its script, the original zMud script, nor any statement until your most recent post. Is there something you are not telling us?

Because nearly everything you are requesting seems to be a text parsing/replacement issue, it is is likely I can write it as a single %subregex, but knowing the exact details are a determining factor as to whether that is appropiate Those same details also are required to determine whether the use lower level items might be more appropiate.
_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
Tech
GURU


Joined: 18 Oct 2000
Posts: 2733
Location: Atlanta, USA

PostPosted: Mon Aug 18, 2008 12:49 am   
 
I also didn't get that this was suppose to return multiple values but should be easy enough to correct.

Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<cmud>
  <func name="roomlists" copy="yes">
    <value>#local $return $testLevel $mystage
$return=""
$mystage=1
$testLevel=10
#loopdb @testdb2 {#if ($testLevel>(%item(%val,1)-%item(@stages,@mystage)) and ($testLevel>=%item(%val,2)-%item(@stages,@mystage))) {#additem $return %key}}
#RETURN $return</value>
  </func>
</cmud>


Instead of assigning $return to be %key I'm adding it to the stringlist instead and that should return you the string list your looking for.

What Vij is saying about the %val is that in the #LOOPDB command you have a variable %val that is the value for the key you working with for that iteration of the loop, i.e. %val is equivalent to %db(@testdb2,%key) while your looping on @testdb2. So there's no need to have CMUD and you're computer do the extra work to look up what it already has. Very Happy

Try that and see how it works.
_________________
Asati di tempari!
Reply with quote
Progonoi
Magician


Joined: 28 Jan 2007
Posts: 430

PostPosted: Mon Aug 18, 2008 1:25 am   
 
Very Happy Excellent! Now that you put that into code, I realize what Vji was saying. Interesting that I didn't understand nor followed that logic when this script was originally written for ZMud.

I'm going to try this snipplet tomorrow as I don't really have much time other than to reply at this point. However, with the latest code I've given out here, I did try additem. However I tried %additem if that means anything. $return=%additem(%key,$return), and this one didn't work.

Anyway, I'm going to get back to you tomorrow with outcome of the new code suggestion, much appreciated.

Prog
_________________
The Proud new owner of CMud.

--------------------------------
Intel Core i5-650 3,2GHz
4 DD3 RAM
GTX 460 768MB
Win 7 Home Premium 64x
--------------------------------
Reply with quote
Progonoi
Magician


Joined: 28 Jan 2007
Posts: 430

PostPosted: Mon Aug 18, 2008 1:08 pm   
 
I'm very happy to announce that it worked! Cool Weird, that %additem didn't work, though. But anyway, now I can get complete list of matches and thats how it was originally designed.

I suppose at this point I need to explain a little what I was actually trying to do as there has already been quite a bit of confusion and things
left unsaid.

In a nutshell, this function goes through a huge db that contains keys as zone names and values which contain all sorts of information about the given zone (and values are in a from of value1|value2|value3 etc). If conditions are met considering my current level, a list is formed which contains matching keys (as in zones) in relation to my current level.

Then I can go on and do things like %item(%item(@func(),2),2) and so on.

I'm not very good at explaining things to be completely honest but maybe this will shine at least some light why I needed the #loopdb output be firm complete match not only first or last.

Thanks again for all the help, Tech and Vji!

Prog
_________________
The Proud new owner of CMud.

--------------------------------
Intel Core i5-650 3,2GHz
4 DD3 RAM
GTX 460 768MB
Win 7 Home Premium 64x
--------------------------------
Reply with quote
Progonoi
Magician


Joined: 28 Jan 2007
Posts: 430

PostPosted: Mon Aug 18, 2008 1:49 pm   
 
Pretty much a follow-up because same sort of problem and didn't see much point in starting a new thread.

Under ZMud, I have this:

Code:

#var ChangeKey {%item( @roomlists, %if( %ismember( %lower( @ChangeKey), %lower( @roomlists))=%numitems( @roomlists), 1, %eval( %ismember( %lower( @ChangeKey), %lower( @roomlists))+1)))}


Lets change this to names we got used to during this thread...

Code:

#var ChangeKey {%item( @func(), %if( %ismember( %lower( @ChangeKey), %lower( @func()))=%numitems( @func()), 1, %eval( %ismember( %lower( @ChangeKey), %lower( @func()))+1)))}


With my dumbness still with CMud, it does seem to me like another function. Because I can't put it under anything else Variable's menu throws at me.

When I entered it directly from command line, it expanded and took the value of first item in @func().

In case its a function, how should I write it down? I mean, there's no loop here which would result in a direct "return"...


I'm really excited about all this, hopefully I'm not too much trouble Wink

Prog
Reply with quote
Tech
GURU


Joined: 18 Oct 2000
Posts: 2733
Location: Atlanta, USA

PostPosted: Mon Aug 18, 2008 4:07 pm   
 
Yes, it is a function so you should be creating as function.

If you were doing it from a command line you'd use #VARFUNC to keep the zMUD version or better yet, use #FUNCTION to create a proper function.

As for what it's returning you're actually returning the result of the %item so you could do something like.

Code:
#varfunc ChangeKey {%item( @func(), %if( %ismember( %lower( @ChangeKey), %lower( @func()))=%numitems( @func()), 1, %eval( %ismember( %lower( @ChangeKey), %lower( @func()))+1)))}
or
#function ChangeKey {#RETURN %item( @func(), %if( %ismember( %lower( @ChangeKey), %lower( @func()))=%numitems( @func()), 1, %eval( %ismember( %lower( @ChangeKey), %lower( @func()))+1)))}


Since you now have access to proper functions it's probably a good time to rewrite the function to remove all the nested %if calls to make it easier to understand and more legible.

I'm not in front of CMUD right now so I can't do that rewrite, but you should be able to get that done. If you haven't by the time I get some free time, I'll take a crack at it myself.
_________________
Asati di tempari!
Reply with quote
Progonoi
Magician


Joined: 28 Jan 2007
Posts: 430

PostPosted: Mon Aug 18, 2008 5:26 pm   
 
I'm unsure if its expected behavior or not but when I enter the #varfunc above into command line and then do #show @changekey(),

I get ERROR: Argument still in stack, followed by current value of @func() being spammed directly into screen until MUD disconnects me.

The second version, using function directly doesn't seem to return any result at all when tried with #show @changekey().

I'm trying to take a crack at how to re-write the logic to make more sense, though.


Thanks for suggestions.

EDIT: Let it be said, that the original ZMud code is supposed to be a "looping" one, meaning if @func() has two values - Area 1 and Area 2, then first time I'd use @changekey() I'd get Area 1, second time Area 2 and third time Area 1 again.

Prog
Reply with quote
Progonoi
Magician


Joined: 28 Jan 2007
Posts: 430

PostPosted: Mon Aug 18, 2008 9:57 pm   
 
Man, I really start to love functions Cool

However, even though I think I may have figured out the logic how all this would work under new function, I still can't get it correctly.

Code:

  <func name="changekey" id="1840">
    <value>#local $return
$return=""
#if (%null(@changekey)) {$return=%item(@func(),1);#RETURN $return} {$return=%item(@func(),%if(%ismember(%lower(@changekey()),%lower(@func()))=%numitems(@func()),1,%eval(%ismember(%lower(@changekey()),%lower(@func()))+1)));#RETURN $return}</value>
  </func>


Made a simple if which would return first item from @func() in case changekey is empty, in case it isn't second portion of the #if call would come to play and make the %if loop.

But the result is that #show @changekey() is always giving me first item from @func(), regardless.

I'm sure its some major logic flaw but I just can't seem to wrap my head around it.

Here's all the code involved.

Code:

<class name="testing" id="1820">
  <var name="testdb" type="Record" id="1822">Area 1="200|201"|Area 2="200|201"</var>
  <var name="testmystage" type="Integer" id="1823">1</var>
  <var name="teststages" type="StringList" id="1824">0|2|3|3|4|4|5</var>
  <func name="func" id="1828">
    <value>#local $return
$return=""
#loopdb @testdb {#if (@MyCurrentLevel>(%item(%val,1)-@{teststages.@testmystage} and @MyCurrentLevel>=%item(%val,2)-@{teststages.@testmystage})) {#additem $return %key}}
#RETURN $return</value>
  </func>
  <var name="MyCurrentLevel" id="1829">201</var>
  <func name="changekey" id="1840">
    <value>#local $return
$return=""
#if (%null(@changekey)) {$return=%item(@func(),1);#RETURN $return} {$return=%item(@func(),%if(%ismember(%lower(@changekey()),%lower(@func()))=%numitems(@func()),1,%eval(%ismember(%lower(@changekey()),%lower(@func()))+1)));#RETURN $return}</value>
  </func>
</class>




Prog
Reply with quote
Tech
GURU


Joined: 18 Oct 2000
Posts: 2733
Location: Atlanta, USA

PostPosted: Tue Aug 19, 2008 3:03 am   
 
Ok... I've figured out why you're always getting the same result. In your if statement you reference the @changeKey, base on what you've given so far it's not an active variable so CMUD (I'm guessing) treats as a literal that always evaluates and thus you always exit the loop early.

Now if it's a reference to the changeKey fuction, then you've got another problem, infinite loop. Can you post more of the original zMUD code so we can get a better understanding of what the changeKey function is supposed to do. Right now we're trying build something (could be a clock or car) by just reconstructing small pieces and that can make it difficult to do the reconstruction properly.
_________________
Asati di tempari!
Reply with quote
Progonoi
Magician


Joined: 28 Jan 2007
Posts: 430

PostPosted: Tue Aug 19, 2008 12:59 pm   
 
Well, to be completely honest, there's really nothing additional to give nor have I hold anything important back.

First I had to solve the old Roomlists function's problem, which I did

Old code:
Code:

<var name="roomlists" id="905">%exec( "#loopdb @zoneDB {#if ((%int( %item( %db( @zonedb, %key), 4))-@{stages.@mystage}<=%int( @MyCurrentLevel)) and (%int( %item( %db( @zonedb, %key), 5))-@{stages.@mystage}>=%int( @MyCurrentLevel))) {%key}}")
</var>


New, improved code:
Code:

#local $return
$return=""
#loopdb @testdb {#if (@MyCurrentLevel>(%item(%val,1)-@{teststages.@testmystage} and @MyCurrentLevel>=%item(%val,2)-@{teststages.@testmystage})) {#additem $return %key}}
#RETURN $return


This code goes through a DB which consists of keys that have multiple values. The DB is looped through, checked each first and second value of a given key against my current level and
when math conditions are met, the outcome is given in a form of a string list.

Next I need a piece of code that'd at any given point take one of the items from that string list above. It is needed that the same code contains both the means to
extract and parse that info and also return it. That way I could in the future do something like %item(%db(@zonedb,@changekey),4) etc. Additionally, the extraction of
first code's string list items has to be a looping one - When the above string list contains 2 items, changekey code should first take First item, then Second, then First again.

Old Code
Code:

#var ChangeKey {%item( @roomlists, %if( %ismember( %lower( @ChangeKey), %lower( @roomlists))=%numitems( @roomlists), 1, %eval( %ismember( %lower( @ChangeKey), %lower( @roomlists))+1)))} 


Honestly I don't know if I can explain this any better :(

Prog
Reply with quote
Tech
GURU


Joined: 18 Oct 2000
Posts: 2733
Location: Atlanta, USA

PostPosted: Tue Aug 19, 2008 11:34 pm   
 
Ok... I just ran the original code snippet in zMUD and you had an infinite loop there too... if you look at the script debugger while it's executing you'll see that. Maybe it's because of the test data or how you're using it you never noticed it before, but it's definitely there. I guess it's a credit to zMUD that it didn't bring the system to a halt.

With that said let's re-write for CMUD. We now know it's now our understanding that was flawed, but the code.

Now we need to know a few things... can the @roomlists() function return a string list greater than 2? If so, how do you know which entry from the list you want? Does it matter if you always take the first one? What do you do with the result of @ChangeKey()?

It looks like the function originally tried to check if you were getting the last member of the list, return to the first member of the list. So I'm guessing what it really want's to do is process the next member of the list each time it's called, then back to the first one. If that's not the functionality you want let me know. The original logic is really flawed, so you'll need to tell me how you want it to work today, and not how it worked before.
_________________
Asati di tempari!
Reply with quote
Progonoi
Magician


Joined: 28 Jan 2007
Posts: 430

PostPosted: Wed Aug 20, 2008 12:25 am   
 
Original code ran perfectly fine in ZMud, I know what I'm talking as all this is ultimately part of a seriously huge code that I had working for little over a year.
When I started, I had Arminas to help me out as I knew close to nothing about Zscripting, but by now I've honestly forgotten which pieces of subcode belong
to his imagination and which to mine.

In my last message, I tried to explain what the pieces of code are doing. I really don't know any better way to explain, other send you the whole thing which
under CMud is little over 2,5K lines of code.


Sorry if I come across harsh and I most likely do and apologize in advance, but as I got the first part (basically the code that started this thread) working perfectly
fine, I figured this one shouldn't be that hard either. At least I thought so. Maybe I'm just unable to explain properly.

That being said.

Yes, @roomlists() function can return a string list thats item count is infinite, basically. There's no restriction there.

Yes, I always want the first entry. When the time comes and I don't want it anymore, Changekey is designed to take next entry which isn't the current one used.

As I said in my last post, result of @changekey() is used for extracting data from zonedb. In a nutshell, zonedb consists of keys as zones and values as different
sort of data (i.e: minimum_zone_level|maximum_zone_level|mob_list|repop_message - Obviously, this sort of huge value list has some values as stand alone string
lists or db records: such as mob_list for example).

Once @changekey() obtains a value, I can do stuff like %item(%db(@zonedb,@changekey()), 2) and so on to form fresh data containers for new zone.

My belief is that original code checked if %ismember(@changekey(),@roomlists()) is a valid one, that means it would return 1. If it is one and equals with
%numitems(@roomlists()), it would take first entry, otherwise it would take %eval(%ismember(@changekey(),@roomlists()))+1.

At least thats how I used to explain it to myself.

I can't dispute that the original *logic* isn't flawed. Perhaps it is, perhaps it isn't. That really isn't the point to be honest. The point is that I should be able to
re-write this code under new set of rules to make it work. If in process I'd make the logic more clearer, well... Even better :)

Lastly I want to say that I really appreciate your effort to crack this, Tech. But maybe there's some gap of understand I can't fill and therefore must
continue alone although I'm unsure if I can because most of the new stuff in CMud I'd really like to use is still pretty much beyond my comprehension.

Thanks again.


Prog
Reply with quote
Vijilante
SubAdmin


Joined: 18 Nov 2001
Posts: 5182

PostPosted: Wed Aug 20, 2008 2:28 am   
 
Why in the world would you need a function to increment through items of lists, when that is what adding 1 to a local variable and using it as the key for %item does? A generic function that works with any list instead of a specific one...Oh, wait that is %push, %pop, %ismember, %item, %delitem, %replaceitem, %additem. To do the increment #FORALL @list {some code;#IF (something) {#BREAK}; some more forall code}

I think Tech is right, without seeing the code that calls these functions we really can't make sense of the functions.
_________________
The only good questions are the ones we have never answered before.
Search the Forums
Reply with quote
Tech
GURU


Joined: 18 Oct 2000
Posts: 2733
Location: Atlanta, USA

PostPosted: Wed Aug 20, 2008 6:08 am   
 
Ok Prognoi,

This should do what you want.

Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<cmud>
  <class name="testing" copy="yes">
    <var name="testdb" type="Record" copy="yes">Area 1="200|201"|Area 2="200|201"</var>
    <var name="testmystage" type="Integer" copy="yes">1</var>
    <var name="teststages" type="StringList" copy="yes">0|2|3|3|4|4|5</var>
    <func name="roomlists" copy="yes">
      <value>#local $return
$return=""
#loopdb @testdb {#if (@MyCurrentLevel>(%item(%val,1)-@{teststages.@testmystage} and @MyCurrentLevel>=%item(%val,2)-@{teststages.@testmystage})) {#additem $return %key}}
#RETURN $return</value>
    </func>
    <var name="MyCurrentLevel" copy="yes">201</var>
    <func name="changekey" copy="yes">
      <value>#local $return
$return=""

// Rather than constantly calling the function we'll call it once and store it
$list = @roomlists( )

#IF (%null( @oldKey)) {$return = %pop( $list )}  {
  $index = %ismember( @oldKey, $list)
  #IF ($index == %numitems( $list)) {$return = %pop( $list )} {
    $index = $index + 1
    $return = %item( $list, $index)
  }
}
oldKey=$return

#RETURN $return
</value>
    </func>
    <var name="oldKey" usedef="true" copy="yes">Area 2</var>
  </class>
</cmud>


You're last explanation is what I was looking for... i.e. what is supposed to be done with the result.

I'm a programmer, so if it (dis)pleases you may ignore the rest of this paragraph as purist rhetoric. I know you contend that the code 'worked' in zMUD so we should be able to make it work, but in truth the code did not really 'work' in zMUD. There were serious flaws in it, but they were such that you were still able to derive you desired function without being affected by the error. A kludge in zMUD somehow allowed an infinite recursive loop to process at some point, which CMUD in it's stricter parsing and more powerful scripting environment did not allow. With great power comes great responsibility.

Now that we know you want to increment through the list it was relatively easy to do what you wanted. The old (zMUD) code assumed calling changeKey() again would get you the old value, so what we really needed was something to keep track of the old value, hence the oldKey variable. I set it as useDefault, assuming you would never want to persist between sessions, you can uncheck if you do want it persisted. You gain speed advantages (almost to that of a local variable by doing so).

I also assumed the result of @roomlists() won't change in the @changeKey function so we store that. The %ismember function is case insensitive so I got rid of the unneeded %lower calls. The rest of it should be fairly easy to follow, but let me know if you have any questions.
_________________
Asati di tempari!
Reply with quote
Progonoi
Magician


Joined: 28 Jan 2007
Posts: 430

PostPosted: Wed Aug 20, 2008 12:59 pm   
 
Before I actually reply, I feel the need to clear the air here. My ultimate motive wasn't in fact pressing on using old code however bad it was. Eventually I just
wanted to get the point of the code across and come up with something more up-to-date in process. Its true though that some sort of internal processes or
whatever CMud does go way beyond my head and therefore I just couldn't understand why the old code wouldn't work. In fact I still don't to be honest.

However, I have nothing against stricter parsing environment. In fact, when I first saw that this code acts weird under CMud (when I tried to enter it via cmd line and
got infinite loop for example), I knew that ultimately it had to be re-written. Seeing now that it can be, while obtaining the same logic principle and amids the
more strict rules still seems to have a lot more understandable logic, pleases me a great deal.

So yeah, I just tried to new code and it works flawlessly, I owe you one, Tech. Ironically I still have beat myself some because looking at the code I may have been
close but missed several key elements to make it work the way it should.

I did, however, not make oldKey default. There's no real need for that in my code and while not doing it gains a speed perspective, well, even more so.
Basically @roomlists() value change over time while I advance in levels so there's no real need at all to keep some random value as default, @changekey() gains
its value in time just like everything else.

Anyway, again great deal of thanks, really appreciated.


Prog
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net