Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » zMUD General Discussion
Theragil
Apprentice


Joined: 13 Feb 2004
Posts: 157
Location: USA

PostPosted: Tue Feb 24, 2004 10:45 pm   

Complex nested multiple if-conditions
 
Is there a better way to make a more readable, manageable, editable construction when you have a lot of if-then constructs inside other if-then constructs? What I want is something like the if-elseif-else blocks in some languages. For instance, in a particular alias I want to pick the highest-priority affliction to cure. A big part of it looks like this:

#if (@pStupidity <> 0) {cureaffliction Stupidity} {#if (@pParalysis <> 0) {cureaffliction Paralysis} {#if (@pRecklessness <> 0) {cureaffliction Recklessness} {#if (@pMasochism <> 0) {cureaffliction Masochism} {#if (@pAsthma <> 0) {cureaffliction Asthma} {#if (@pEpilepsy <> 0) {cureaffliction Epilepsy} {#if (@pDizziness <> 0) {cureaffliction Dizziness} {#if (@pShyness = 1) {cureaffliction Shyness} {#if (@pAddiction = 1) {cureaffliction Addiction} {#if (@iGoldenseal > 0) {eatherbdec goldenseal} {#if (@iBloodroot > 0) {eatherbdec bloodroot} {#if (@iLobelia > 0) {eatherbdec lobelia} {#if (@iBellwort > 0) {eatherbdec bellwort} {#if (@iAsh > 0) {eatherbdec ash} {#if (@iGinseng > 0) {eatherbdec ginseng} {#if (@iKelp > 0) {eatherbdec kelp} {#if (@iBayberry > 0) {eatherbdec bayberry}} {#if (@iHawthorn > 0) {eatherbdec hawthorn}}}}}}}}}}}}}}}}}

Only that's the tip of the iceberg -- the complete version should have about four times as many cases as this. And I can only cure the first (in priority order) affliction that I encounter, even though coming in, I may have many.

Here's a klugey way that would work:

pAlreadyCuredSomething = 0
#if (@pAlreadyCuredSomething = 0 and @pStupidity <> 0) {cureaffliction Stupidity}
#if (@pAlreadyCuredSomething = 0 and @pParalysis <> 0) {cureaffliction Paralysis}
#if (@pAlreadyCuredSomething = 0 and @pRecklessness <> 0) {cureaffliction Recklessness}
etc. (in which cureaffliction also sets pAlreadyCuredSomething = 1).

There must be a better way.
Reply with quote
Darker
GURU


Joined: 24 Sep 2000
Posts: 1237
Location: USA

PostPosted: Tue Feb 24, 2004 11:23 pm   
 
With a little help from the aptly named: Helpfile...
quote:

ABORT

Syntax: #AB (flag)


Aborts further parsing of the current command line. Commands after this on the same line are discarded. Aborts processing of current program block.

If the flag is present and non-zero, the entire script is aborted instead of just the current program block.
ABORT example

get all corpse;#ABORT;split
OK, this is somewhat contrived, but the command ‘get all corpse’ is sent, then the #ABORT stops further processing so that the ‘split’ command is ignored.



Soooo, try it like this:

#if (@pStupidity <> 0) {cureaffliction Stupidity;#abort 1}
#if (@pParalysis <> 0) {cureaffliction Paralysis;#abort 1}
#if (@pRecklessness <> 0) {cureaffliction Recklessness;#abort 1}
#if (@pMasochism <> 0) {cureaffliction Masochism;#abort 1}
#if (@pAsthma <> 0) {cureaffliction Asthma;#abort 1}
#if (@pEpilepsy <> 0) {cureaffliction Epilepsy;#abort 1}
#if (@pDizziness <> 0) {cureaffliction Dizziness;#abort 1}
#if (@pShyness = 1) {cureaffliction Shyness;#abort 1}
#if (@pAddiction = 1) {cureaffliction Addiction;#abort 1}
#if (@iGoldenseal > 0) {eatherbdec goldenseal;#abort 1}
#if (@iBloodroot > 0) {eatherbdec bloodroot;#abort 1}
#if (@iLobelia > 0) {eatherbdec lobelia;#abort 1}
#if (@iBellwort > 0) {eatherbdec bellwort;#abort 1}
#if (@iAsh > 0) {eatherbdec ash;#abort 1}
#if (@iGinseng > 0) {eatherbdec ginseng;#abort 1}
#if (@iKelp > 0) {eatherbdec kelp;#abort 1}
#if (@iBayberry > 0) {eatherbdec bayberry;#abort 1
#if (@iHawthorn > 0) {eatherbdec hawthorn;#abort 1}
Reply with quote
Talahaski
Enchanter


Joined: 10 Oct 2000
Posts: 656
Location: USA

PostPosted: Tue Feb 24, 2004 11:27 pm   
 
complicated If-then-else can get very ugly in zmud. sometimes the use of the Case statement can help, other times I try to find a better solution instead of using if conditions. Would be nice if the case statement was more flexible.
Reply with quote
Talahaski
Enchanter


Joined: 10 Oct 2000
Posts: 656
Location: USA

PostPosted: Tue Feb 24, 2004 11:29 pm   
 
Hey, good solution Darker! I never thought to use abort like that.
Reply with quote
Theragil
Apprentice


Joined: 13 Feb 2004
Posts: 157
Location: USA

PostPosted: Wed Feb 25, 2004 3:15 pm   
 
Thank you, Darker. I looked for a command like this back when I was a complete zMUD newbie and didn't find one, so I didn't go back looking again. This will really make things a lot better.
Reply with quote
Theragil
Apprentice


Joined: 13 Feb 2004
Posts: 157
Location: USA

PostPosted: Thu Feb 26, 2004 12:33 am   
 
Ran into a problem with this already. If I have alias1 call alias2 and alias2 uses this approach, the #abort 1 causes alias1 to also get aborted. That's a pretty serious side-effect for what is essentially an attempt to fiddle with code structure, not function. Is there any command that aborts the alias in which it appears, but not all the calling aliases, triggers, etc.?
Reply with quote
Darker
GURU


Joined: 24 Sep 2000
Posts: 1237
Location: USA

PostPosted: Thu Feb 26, 2004 2:04 am   
 
Just use #abort then, not #abort 1
Reply with quote
Theragil
Apprentice


Joined: 13 Feb 2004
Posts: 157
Location: USA

PostPosted: Thu Feb 26, 2004 6:37 pm   
 
Just #abort only aborts the #if block it's found in, so in the example above, it will not stop the rest of the if commands from executing. An #abort at the end of a {} block doesn't do anything.
Reply with quote
Darker
GURU


Joined: 24 Sep 2000
Posts: 1237
Location: USA

PostPosted: Thu Feb 26, 2004 8:26 pm   
 
Hmm, no good answer comes to mind. I think Talahaski's CASE idea is as good as I can think of.
Reply with quote
Serentus
Apprentice


Joined: 28 Sep 2001
Posts: 103
Location: USA

PostPosted: Fri Feb 27, 2004 6:41 am   
 
This might help. Try putting the afflictions in a stringlist and using a #FORALL to go through the list.
#VAR Afflictions Stupidity|Paralysis|Recklessness....
then in you alias you have something like this

#VAR Cured {0}
#FORALL @Afflictions {#IF (@Cured=0) {#IF (@p%i<>0) {CureAffliction %i};#VAR Cured {1}}}

This you may be able to put an #ABORT 1 in the false condtion of the @Cured=0, but i'm not sure how it would affect you other scripts. This way will be slightly slower than the nested #ifs.

You can also try something like this

#ALIAS Cure1 {#if (@pStupidity <> 0) {cureaffliction Stupidity} {#if (@pParalysis <> 0) {cureaffliction Paralysis} {#if (@pRecklessness <> 0) {cureaffliction Recklessness} {#if (@pMasochism <> 0) {cureaffliction Masochism} {#if (@pAsthma <> 0) {cureaffliction Asthma} {}}}}}}

#ALIAS Cure2 {#if (@pEpilepsy <> 0) {cureaffliction Epilepsy} {#if (@pDizziness <> 0) {cureaffliction Dizziness} {#if (@pShyness = 1) {cureaffliction Shyness} {#if (@pAddiction = 1) {cureaffliction Addiction} {#if (@iGoldenseal > 0) {eatherbdec goldenseal} {#if (@iBloodroot > 0) {eatherbdec bloodroot} {#if (@iLobelia > 0) {eatherbdec lobelia} {#if (@iBellwort > 0) {eatherbdec bellwort} {Cure3}}}}}}}

#ALIAS Cure3 {#if (@iAsh > 0) {eatherbdec ash} {#if (@iGinseng > 0) {eatherbdec ginseng} {#if (@iKelp > 0) {eatherbdec kelp} {#if (@iBayberry > 0) {eatherbdec bayberry}} {#if (@iHawthorn > 0) {eatherbdec hawthorn}}}}}}}

It's a bit more managable and readable. Here Cure1 goes through about 5 nested #ifs and if none have been true it calls Cure2 and which checks a few more and if none of those are true it calls Cure3....and so on.
Reply with quote
Theragil
Apprentice


Joined: 13 Feb 2004
Posts: 157
Location: USA

PostPosted: Fri Feb 27, 2004 5:28 pm   
 
Interesting ideas. I'll fiddle about with them. Thanks for the suggestions.
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » zMUD 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