Fun With Variables

 

Learning how to use variables properly can be a really effective way to get your mobs to do almost anything that you want them to do. With variables, I have been able to create insanely detailed mobprogs, such as mobs that play blackjack and roulette. Variables are the key to mob intelligence, and this section of the tutorial is designed to show you how to use them.

 

Setting the Variable

 

The mobprog command mpsetvar creates variables and stores values into them. The mpsetvar command can set integers, strings, and expanded character variables like var-actor as the values of a variable. The variable name can be anything you want, but should probably be ten characters or less for simplicitiy’s sake. The target of mpsetvar is normally the mob using it, but it can be used to set the variable for another mob. Here are a few examples of variables being set:

 

mpsetvar me myvictim var-actor

(Variable myvictim is given the value held in var-actor, or $n)

 

mpsetvar me myfriend “guardthree”

(Variable myfriend is given the value guardthree)

 

mpsetvar me counter 1

(Variable counter is given the value 1)

 

mpsetvar me counter $(+(var-counter,1))

(Variable counter is given the value of the previous value of counter + 1)

 

mpsetvar me dice number(1,6)

(Variable dice is given a random value from 1 to 6)

 

mpsetvar me goldcount goldamt($n)

(Variable goldcount is given the value of the gold on hand of $n)

 

mpsetvar me questobj var-object

(Variable questobj is given the value held in var-object or $o)

 

mpsetvar me myweapon “axe”

(Variable myweapon is given the value axe)

 

mpsetvar me average $(/(+(var-diceone,var-dicetwo),2))

(Variable average is given the value of (diceone + dicetwo) / 2)

 

mpsetvar me fincount var-average

(Variable fincount is given the value of variable average)

 

mpsetvar xxxgoblinonexxx hunting var-actor

(Variable hunting of mob xxxgoblinonexxx is set to actor)

 

mpsetvar xxxgoblintwoxxx value 1

(Variable value of mob xxxgoblintwoxxx is set to 1)

 

mpsetvar xxxgoblinonexxx  greenvar var-bluevar

(Variable greenvar of mob xxxgoblinonexxx is set to value of variable bluevar)

 

mpsetvar xxxgoblintwoxxx guarding “orc”

(Variable guarding of mob xxxgoblintwoxxx is set to “orc”)

 

Note that different kinds of values set by the mpsetvar command have different ways of being set. Here are the general rules of mpsetvar values:

 

Integer            mpsetvar [target] variable integer              

- Ex: mpsetvar var 1

String              mpsetvar [target] variable “string”              

- Ex: mpsetvar var “cow”

Character       mpsetvar [target] variable var-character    

- Ex: mpsetvar var var-actor

Variable         mpsetvar [target] variable var-variable      

- Ex: mpsetvar var var-counter

Arithmetical    mpsetvar [target] variable $(functions)      

- Ex: mpsetvar var $(-(var-var,1))

Function         mpsetvar [target] variable function             

- Ex: mpsetvar var number(1,3)

 

The rules can somewhat vary from function to function, but the rules are generally true. Note that when you are dealing with variables and character variables you must use var- and then the name of the variable or character. It is also important to know that 0 is NOT a valid value for a variable! So if you need a variable to store 0, you will have to use 1 for 0, 2 for 1, etc.

 

There really is no limit to the ways you can manipulate variables using the mpsetvar command. You can add different variables together, set variable values to each other, etc. As of yet I have not found a limit to the number of variables a mob can store at one time, although the highest number I have used on a mob at one time is about ten. Remember to never mpadd or mppush mpsetvar. I also recommend only using letters in your variable names; that is, don’t use non-alphabetic characters.

 

You can also have a mob set a variable from the variable of another mob, but only if it is an integer. To do this you make use of the var function. The syntax for doing this is:  mpsetvar variable $(var(“variable”,$n))  This assuming that $n is a mob that has triggered the current mob. For instance you could have a mob poke another mob, triggering the second mob to use the first mobs variable. This unfortunately only works on integers, so its use is somewhat limited. Luckily, there is a better way to do this.

 

The mpsetvar command can be used to set the variables of a different mob than the one using the command. The mob having variables set on it must be in the same room as the mob doing it, or affected using the mpat command. Setting the variables of another mob is pretty much the same as having a mob set its own variables. Using this ability can create truly interesting mobprogs.

 

 

Comparing Variables

 

Comparing variables is fairly straight forward. You can use any of the operators listed in the Mobprog Functions section of this tutorial. For variables that store a numeric value, you would use the numeric operators. The only difference is that you should use = instead of == when doing variable comparisons. See the following examples:

 

if >(var-counter,3)

(Is variable counter greater than three?)

 

if <=(var-plrtotal,var-dlrtotal)

(Is variable plrtotal less than or equal to variable dlrtotal?)

 

if =(var-randcheck,1)

(Is variable randcheck equal to 1?)

 

if != (var-monkey,(+(var-banana,1)))

(Is variable monkey not equal to variable banana + 1?)

 

if <(goldamt($n),var-infocost))

(Is the amount of gold $n has on hand less than variable infocost?)

 

if >=(var-strtest,stat($n,”str”))

(Is variable strtest greater than or equal to $n’s strength stat?)

 

Note again the use of var- in our expansion of variables values. As you can see, this sort of variable comparison is not very difficult. The key is only getting the syntax down correctly. For string and character variable comparisons of variables, you can use the string operators found in the Mobprog Functions section of this tutorial. See the following examples:

 

if =(var-talkingto,var-actor)

(Is variable talkingto the same person as the actor triggering me?)

 

if !=(var-myvictim,var-myfriend)

(Is variable myvictim not the same as variable myfriend?)

 

if  =(var-myweapon,”axe”)

(Is variable myweapon the same as the string axe?)

 

if /(var-barnyard,”cow”)

(Is the string cow contained in whole or part in variable barnyard?)

 

One thing to keep in mind. You cannot compare different types of variable values, so for example, you cannot compare a variable that has been stored with var-actor or any other character variable to a string. So you could not store var-actor in a variable called myvictim, and then later have an if check to see if myvictim was the same as “guard.” They are different – myvictim is a character/mobile and “guard” is a string. Thus the only time you can compare a string to a variable is if the variable has been set with a string itself.

 

 

Variables and Mobprog Functions

 

You can use variables in mobprog functions aside from comparison with operators as well. However, in order to do this, the type of value stored in the variable must be the same as the kind used in the function. For example, the ispc function works with characters, and nothing else. So you could store a character variable like var-actor or var-me into a variable talkingto, and then do a if ispc(var-talkingto). See the usage of each mobprog funtion to see which ones will work with what type of variable value. Here are a few examples:

 

if ispc(var-talkingto)

(Is variable talkingto (character var) a player character?)

 

if =(inroom(var-myvictim),inroom($i))

(Is variable myvictim (character var) in the same room is me?)

 

if >((isaffected(var-myfriend,11)),0)

(Is variable myfriend (character var) affected by poison?)

 

if =(name(var-talkingto),”guard”)

(Is the name of variable (character var) talking to guard?)

 

if !=(objtype(var-myweapon),5)

(Is variable myweapon (object var) a weapon?)

 

if rand(var-check)

(Is the random chance between 1 and 100 less than variable check (integer var)?)

 

if vinroom(var-mobvnum)

(Is a mob ov vnum variable mobvnum (integer var) in the room?)

 

if or(=(class(var-myvictim),3),=(premulticlass(var-myvictim),3))

(Is variable myvictim (character var) a thief either pre or post multiclass?)

 

 

Using and Displaying Variables

 

The next thing you need to know is how to use variables once you have them. Often you will be doing a lot of comparisons with variables, particularly integer variables. You can do other things though as well. You can display variables for players to see, have the mob act upon variables, etc. The way to do this is using $[   ]. Here are a few examples:

 

mpadd mpecho $I says, ‘You know, I’m looking for someone named $[myfriend].’

mpadd backstab $[myvictim]

mpadd mptransfer $[talkingto] 10078

mpadd mpecho $I says, ‘The total is $[counter]

mpadd wield $[myweapon]

 

Expressing variables in this way works for all types of variables, although obviously your mob will find it hard to backstab an integer or wield an actor. Now you have all the knowledge you need to create truly intelligent mobs.

 

 

Putting It All Together

 

Okay. So now we know how to use variables. Now let’s make a set of progs that use variables well. We are going to make a set of mobprogs that allows a mob to pursue its victim and do random nasty stuff to them. First of all, we’ll need to set the variable. Let’s do this in a greet_prog:

 

>greet_prog 100~
  if isqueued($i)

    break

  else

    endif

  endif

  if isnpc($n)

    break

  else

    endif

  endif

  mpsetvar me myvictim var-actor

  mpadd mpecho $I says, ‘You should not have come here, $n. Now you will die.’

~

 

Next, let’s have out mob pursue our fleeing victim:

 

>act_prog p panics, and attempts to flee.~
  if =(var-myvictim,var-actor)

    mpadd hunt $[myvictim]

  else

    endif

  endif

~

 

We will want to make sure that our mob has the hunt skill if we want to do this. We could also add some checks in here. Perhaps only sometimes the mob hunts the pc, and sometimes it summons:

 

>act_prog p panics, and attempts to flee.~
  if !=(var-myvictim,var-actor)

    break

  else

    endif

  endif

  if rand(50)

    mpadd hunt $[myvictim]

  else

    mpadd cast ‘summon’ $[myvictim]

  endif

~

 

As you can see, having a mob have this option is much more interesting than simply giving the mob the zonehunt bit in the mob file. We can make the mob much more intelligent or varying about the way it handles things. We could also add perhaps a check on the mobs hit points… if the mob is hurt badly, it won’t pursue the fleeing player, but instead will go and hide. At any rate, we will now need to have the mob do something when it does hunt after its victim:

 

>entry_prog 100~

  if =(inroom(var-myvictim),inroom($i))

    mpadd backstab $[myvictim]

  else

    endif

  endif

~

 

Or, if we wanted to spice it up a little:

 

>entry_prog 100~

  if !=(inroom(var-myvictim),inroom($i))

    break

  else

    endif

  endif

  mpsetvar me check number(1,5)

  if or(=(var-check,1),=(var-check,2))

    mpadd backstab $[myvictim]

    break

  else

    endif

  endif

  if =(var-check,3)

    mpadd cast ‘fireball’ $[myvictim]

    break

  else

    endif

  endif

  if =(var-check,4)

    mpadd cast ‘blindness’ $[myvictim]

    break

  else

    endif

  endif

  if =(var-check,5)

    mpadd taunt $[myvictim]

    break

  else

    endif

  endif

~

 

What we have done here is now made it so when the mob enters the room with its victim, it does one of four things, with the probablity of it backstabbing the victim a little higher than the rest.

 

Now let’s try setting the variable of another mob. A good example of how this can be useful is to make a ‘scout’ mob that sees a character coming, and have another mob that goes after the character. One mob would thus have the following prog:

 

>greet_prog 100~

  if isqueued($n)

    break

  else

    endif

  endif

  if ispc($n)

    mpsetvar xxxhuntergolemxxx myvictim var-actor

    mpadd mpat xxxhuntergolemxxx mpechoat xxxhuntergolemxxx START HUNTING

    mpadd mpgoto 1252

    mpadd mppurge me

  else

    endif

  endif

~

 

This mob basically sets the second mob, xxxhuntergolemxxx, to have a variable myvictim filled with the value of the first mob’s var-actor (the one who set off the greet_prog). The mob then gives a trigger line to the hunter golem, and goes to a private room and purges itself. Now the second mob:

 

>act_prog p START HUNTING~

  mpadd hunt $[myvictim]

~

>entry_prog 100~

  if =(inroom(var-myvictim),inroom($i))

    mpadd kill $[myvictim]

  else

    mpadd hunt $[myvictim]

  endif

~

>act_prog p panics, and attempts to flee.~
  if !=(var-myvictim,var-actor)

    break

  else

    endif

  endif

  mpadd hunt $[myvictim]

~

 

The hunting golem will now pursue the poor character stored in myvictim, without ever even having encountered the unsuspecting character previously! Pretty snazzy, eh?

 

That about does it for variables. They can be fun to use, and will allow you to create incredibly detailed and intelligent mobs if you so choose.