Intercept_prog to allow several different conditions

For builders to discuss and ask building questions.
Post Reply
User avatar
Kelemvor
Sword Grand Master
Sword Grand Master
Posts: 2295
Joined: Mon Apr 11, 2005 6:14 pm
Location: The Fugue Plain within the Crystal Spire

Intercept_prog to allow several different conditions

Post by Kelemvor » Sat Jun 30, 2007 4:52 pm

I wasn't able to work this out from the lessons alone, but how would I set an NPC to stop some races and classes from entering, but allow others and do it all in a single prog?

Basically, if race is not X or Y or class is not 'this' then they can not enter

would it be :

Code: Select all

> intercept_prog North~
 if race($n) \= X
  and
 if race($n) \= Y
  and
 if class($n) \= this
  sayto $n I am sorry, but you can not go there.
 else
  mpunintercept
 endif
~
|
is there anywhere which shows the construction of mob and object progs in more detail than the examples on the builders lesson page?
...never send to know for whom the bell tolls,
it tolls for thee.
Dalvyn
Sword Grand Master
Sword Grand Master
Posts: 4708
Joined: Tue Jul 15, 2003 9:26 pm
Location: House of Wonder, Waterdeep

Post by Dalvyn » Sun Jul 01, 2007 2:39 am

Two things to keep in mind :
  • We do not have any "and" connector (connector being a thing that connects two conditions in this case). We do have an "or" connector, but no "and". That means that we sometimes have to do things in rather strange ways.
  • Races checks do not fully work currently. "Positive" race checks correctly check for subraces as far as I know. By "positive" check, I mean a check with "==", as in "race($n) == Dwarf". This condition (race($n) == Dwarf) will be satisfied if $n is a dwarf, or a shield dwarf, or a gold dwarf, or belongs to any subrace of the dwarven race. [As a side note, duergar is NOT coded as a subrace, so we can tell apart the good dwarves and the bad ones. For example, Mithril Hall is restricted to race($n) == Dwarf people ... that explains why duergars are not coded as dwarves.]
    Now... since I pointed out that subraces work for "positive" race checks, you might have guessed that they do not work for "negative" race checks. Side note : negative equality [that is, difference] is written !=, not \=. The condition "race($n) != Dwarf" should be satisfied for everybody BUT dwarves, shield dwarves, gold dwarves, and so on. Currently, the code makes it "false" for dwarves, but true for everybody else (including people belonging to dwarven subraces).
That being said ... there's no lesson to learn how to write programs with more complex combinations of conditions. The only piece of advice I can think of is to try and express what you mean by using only "or". Also, note that in most natural languages (i.e., English, French, and so on), the meanings of "or" and "and" are sometimes mixed up (that happens very often in negative sentences), so one needs to be extra careful.

Here, what you mean (if I understood correctly) is :

IF (race is X) or (race is Y) or (class is Z),
- you cannot enter
ELSE/OTHERWISE
- you can enter

That would "stop some races (X and Y) and classes (Z) from entering, but allow others" in.

An "or" expression is true/satisfied as soon as one of its parts is true. In this case, the bold expression above is true as soon as one of its three parts is true, that is, as soon as $n is of race X, or of race Y, or of class Z. In those cases, $n will be prevented from entering. In all other cases, they can go in.

Turned in code, that yields:

Code: Select all

>intercept_prog north~
if race($n) == Dwarf
or race($n) == Gnome
or class($n) == Rogue
  sayto $n I cannot let you in!
else
  sayto $n Sure, go in!
  mpunintercept
endif
~
Just a last comment to point out the mistake in the prog above, with all the "and" connectors. Let's assume for a minute that we can use "and" in programmes and see what the following programme is going to do.

Code: Select all

!! This is not correct FK code !!
>intercept_prog north~
if race($n) != Dwarf
and race($n) != Gnome
and class($n) != Rogue
  sayto $n I cannot let you in!
else
  sayto $n Sure, go in!
  mpunintercept
endif
~
This programme says that: if the big condition is true, then $n is prevented from entering; otherwise, he can go in. Now, what is the condition ? The condition is (something) and (something) and (something), which is satisfied only when the three "something"s are true, that is ... the condition is satisfied only when
- $n is not a dwarf, and
- $n is not a gnome, and
- $n is not a rogue.

In other words, to be prevented from entering, one needs to be "not a dwarf" and "not a gnome" and "not a rogue" all at once. This is actually the opposite of what you wanted.

A dwarf character, for example, fails to satisfy the condition "not a dwarf". That is the first "something" of the big expression... and since this "something" is false, the whole expression is false. Therefore, the "otherwise/else" part is triggered ... and the dwarf is allowed in.

Actually, the only way to be prevented from entering is to be a non-dwarf, non-gnome, and non-rogue. So, you are actually keeping out the people you wanted in, and allowing in those you wanted to stay outside.

In a first time, it's better to try and stay completely away from negations in complex conditions. Logic errors in complex conditions involving negations are very common, even in code written by very experienced programmers.

I hope that helps. If you have further questions, please post again!
Image
Jharthyne
Sword Apprentice
Sword Apprentice
Posts: 60
Joined: Tue Sep 16, 2003 7:36 am
Location: Waterdeep
Contact:

Post by Jharthyne » Sun Jul 01, 2007 4:30 am

To allow dwarves, gnomes and thieves to enter:

Code: Select all

>intercept_prog north~
if race($n) == Dwarf
or race($n) == Gnome
or class($n) == Rogue
  sayto $n Sure, go in!
  mpunintercept
else
  sayto $n I cannot let you in!
endif
~
To allow anyone BUT dwarves, gnomes and thieves:

Code: Select all

>intercept_prog north~
if race($n) == Dwarf
or race($n) == Gnome
or class($n) == Rogue
  sayto $n I cannot let you in!
else
  sayto $n Sure, go in!
  mpunintercept
endif
~
Basically:
NOT X AND NOT Y
is the same as
NOT (X OR Y)

I think I am just adding to the confusion...
User avatar
Kelemvor
Sword Grand Master
Sword Grand Master
Posts: 2295
Joined: Mon Apr 11, 2005 6:14 pm
Location: The Fugue Plain within the Crystal Spire

Post by Kelemvor » Sun Jul 01, 2007 7:43 am

Thanks for the explanations guys, I realised how badly I'd asked the question when Dal thought I wanted to keep the 'X' 'Y' and 'them' out, whereas in fact I wanted to keep all out except them! :)

I'll keep trying my hand at the various mob and object progs, but any I can't explicitly check against the lessons I'll just leave until a hard coder can review the area.

One I would like double-checked now though...
I noticed I'd used a very different means of setting up an Inn and sleeping room than that in the lessons. I think mine would work, but can you confirm.

Code: Select all

>greet_prog 50~
 sayto $n For 2 silvers you can rest up
 sayto $n in the cellars long as you don't
 sayto $n mind the rats.
~
|
>bribe_prog 20~
 mpechoat $n The bartender hustles you down the cellar steps.
 mpechoaround $n The bartender hustles $N down the cellar steps.
 mptransfer $n QQ16
 mpat QQ16 mpforce $n look
 mpjunk coin
~
|

Code: Select all

>greet_prog 100 up~
 mpechoat $n As you reach the bottom of the stairs the guard bars your way
 mpechoaround $n As $N reaches the bottom of the stairs the guard bars the way.
 sayto $n You ain't coming down 'ere less'n ye've paid ti rest up
 mpforce $n up
~
|
Basically, if they walk into the sleeping room they're forced back up again, if they pay the barkeeper they bypass the need to walk down and can stay
...never send to know for whom the bell tolls,
it tolls for thee.
User avatar
Japcil
Sword Grand Master
Sword Grand Master
Posts: 1143
Joined: Fri Jun 17, 2005 5:32 pm
Location: Golden Oaks
Contact:

Post by Japcil » Sun Jul 01, 2007 7:54 am

Instead of mpjunk coin, have it add the coin to the area's economy. "mpdeposit 20" would be the line of code I believe.
Image
Dalvyn
Sword Grand Master
Sword Grand Master
Posts: 4708
Joined: Tue Jul 15, 2003 9:26 pm
Location: House of Wonder, Waterdeep

Post by Dalvyn » Sun Jul 01, 2007 5:09 pm

Yes, those programs should work.

A few observations though:

1. You transfer only the character, which might leave minions (e.g., familiars) behind. I would use "mptransfer $n QQ16 pet" instead - the only difference is that it will transfer the pets along with $n.

2. When you transfer $n, people who are already in the room won't see $n coming in. I would add, after your "mpat ..." line the following : "mpat QQ16 mpechoaround $n @N comes in from above."

3. Mpdeposit does not replace mpjunk, as far as I know (though a test could prove me wrong). Mpdeposit simply adds to the area economy (I do not think that it's worth doing it for just 20 copper).
Image
User avatar
Raona
Staff
Staff
Posts: 4944
Joined: Fri Aug 19, 2005 3:40 pm
Location: Waterdeep - Halls of Justice
Contact:

Post by Raona » Sun Jul 29, 2007 8:56 pm

Playing the helpfile role, if I read my other messages correctly, AND now IS available to builders, correct? (For future reference of all!)
Dalvyn
Sword Grand Master
Sword Grand Master
Posts: 4708
Joined: Tue Jul 15, 2003 9:26 pm
Location: House of Wonder, Waterdeep

Post by Dalvyn » Sun Jul 29, 2007 9:45 pm

Yes, AND is not available... though I'm not sure how well AND and ORs would mix. I'll ask in the other thread.
Image
User avatar
Kelemvor
Sword Grand Master
Sword Grand Master
Posts: 2295
Joined: Mon Apr 11, 2005 6:14 pm
Location: The Fugue Plain within the Crystal Spire

Post by Kelemvor » Wed Jan 23, 2008 10:30 pm

I need to refine a program which originally was intended to keep evil alignments out and refuse them training, thus...

Code: Select all

>intercept_prog train~
if isevil($n)
  sayto $n I do not know how you gained entry here,
  sayto $n but I will have no truck with the dark
  sayto $n of heart
else
  mpunintercept
endif
~
What I want to add is that if the character is of a particular deity, they are also denied entry or training.
Now from the above, I know I can simply add

Code: Select all

>intercept_prog train~
if isevil($n)
[color=red]or if deity($n) == talos
or if deity($n) == umberlee
or if deity($n) == malar[/color]
  sayto $n I do not know how you gained entry here,
  sayto $n but I will have no truck with the dark
  sayto $n of heart
else
  mpunintercept
endif
~
and achieve the effect. However, the response doesn't now make complete sense if they aren't evil.

If I wish it to check for alignment separately from deity and generate either the 'dark of heart' message for an evil or 'the dark of heart' message plus 'rude comment about the Gods of Fury' how would I do that?

Code: Select all

>intercept_prog train~
if isevil($n)
  sayto $n I do not know how you gained entry here, but
  sayto $n I will have no truck with the dark of heart
else
  if deity($n) == talos
  or if deity($n) == malar
  or if deity($n) == umberlee
    sayto $n Child of Fury!
    sayto $n Your very presence offends me.
  else
    mpunintercept
  endif
endif
~
Would that work?
...never send to know for whom the bell tolls,
it tolls for thee.
Dalvyn
Sword Grand Master
Sword Grand Master
Posts: 4708
Joined: Tue Jul 15, 2003 9:26 pm
Location: House of Wonder, Waterdeep

Post by Dalvyn » Wed Jan 23, 2008 10:53 pm

That's nearly 100% correct.

But when you use "or" (or "and"), you do not need to repeat the "if", so that would be:

Code: Select all

>intercept_prog train~
if isevil($n)
  sayto $n I do not know how you gained entry here, but
  sayto $n I will have no truck with the dark of heart
else
  if deity($n) == talos
  or deity($n) == malar
  or deity($n) == umberlee
    sayto $n Child of Fury!
    sayto $n Your very presence offends me.
  else
    mpunintercept
  endif
endif
~
Image
User avatar
Kelemvor
Sword Grand Master
Sword Grand Master
Posts: 2295
Joined: Mon Apr 11, 2005 6:14 pm
Location: The Fugue Plain within the Crystal Spire

Post by Kelemvor » Thu Jan 24, 2008 12:10 am

I could almost get the hang of these progs...

(in another year or two :) )
...never send to know for whom the bell tolls,
it tolls for thee.
Post Reply