Complex Mobprogs

 

The main purpose of this section of the tutorial is to show you how you should put together more complicated mobprogs, namely, by showing you the difference between nested progs and non-nested complicated progs.

 

A nested prog basically means that a mobprog has nested if statements. Nested means that they are inside of one another. Here is an example:

 

if ispc($n)

  if <(stat($i,”con”),16)

    if ==(inroom($i),10034)

      mpadd mpecho I am working!

    endif

  endif

endif

 

This nesting basically checks to see if $n is a pc, and if so, is the con stat of $i less than 16, and if so, is $i in room 10034. Quite frankly, nesting can get ugly quickly. Simple nested if statements usually aren’t a problem, but more complicated ones are hard to debug and can even fail just because they are nested so much. Now consider the following:

 

if and(ispc($n),<(stat($i,”con”),16),==(inroom($i),10034))

  mpadd mpecho I am working!

endif

 

Much cleaner isn’t it? And it does the same thing. Using the and, or, and not functions can really clean up your if checks. You can even combine them like so:

 

if or(=(var-checkone,2),and(=(var-checktwo,3),=(var-checkthree,4)),ispc($n))

This means, if checkone is 2, OR checktwo is 3 AND checkthree is 4, OR ispc($n).

 

Now let’s consider another example. Here is a nested mobprog:

 

if ispc($n)

  if ==(inroom($i),2450)

    mpadd mpecho $I says, ‘Go away!’

  else

    if ==(inroom($i),2451)

      mpadd mpecho $I says, ‘Welcome to my home, $n.’

    else

        if rand(20)

          mpadd kill $n

        else

          mpadd hug $n

        endif

    endif

  endif

else

  if ischarmed($n)

    if rand(50)

      mpadd mppurge $n

    else

      mpadd growl $n

    endif

  else

    mpadd bow $n

  endif

endif

 

This is a heavily nested mobprog! Is it so bad to have nesting? Yes and no. Most of the time, it doesn’t make much difference. However, when you have complicated mobprogs like this, with lots of else and endif all over the place, things can be screwed up fairly easily, and debugging progs tangled up like this can be a mess. But now look at this:

 

if and(isnpc($n),!(ischarmed($n)))

  mpadd bow $n

  break

endif

if and(isnpc($n),ischarmed($n))

  if rand(40)

    mpadd mppurge $n

    break

  else

    mpadd growl $n

    break

  endif

endif

if ==(inroom($i),2450)

  mpadd mpecho $I says, ‘Go away!’

  break

endif

if ==(inroom($i),2451)

  mpadd mpecho $I says, ‘Welcome to my home, $n.’

  break

endif

if rand(20)

  mpadd kill $n

else

  mpadd hug $n

endif

 

At first glance you may realize that this mobprog is actually longer than the first, and that it, too, contains a little bit of nesting. The fact of the matter is however, that this second mobprog has a much greater chance of actually working, and you can see step by step how everything is behaving. Using the break command can really help in these situations. Having a little nesting is okay, having a lot can be tedious and may not work. If you look at everything the right way, and know how to use all your operators and functions, you can take even the most difficult prog and make it not be nested. In truth, I could have easily not nested the if rand(40) in the above example, but I wanted to show you that a little bit of nesting doesn’t usually hurt anything. Ultimately it is up to you whether or not you want to nest your if statements, but I would encourage you to avoid it. Once you begin writing larger and larger mobprogs, not having to untangle a nested mess can really be of great value.

 

If you do find your mobprogs are getting really big and tedious, consider creating ‘subroutines’ of sorts. You can have a mob trigger itself in a number of ways. A good example is a casino mob. It has subroutines for collecting a bet, starting a game, allowing a player to take a turn, having the dealer take a turn, and so on, and then ending the game. Each of these is a separate mobprog triggered by the mob. The mob may perform some social that triggers itself, use a secret password kind of speech_prog or act_prog somewhere else to trigger itself, etc. This is another good way to untangle hairy mobprogs or mobprogs that (gasp!) are getting a little too close to the 16000 character limit per mobprog…