Programming by exploration guide

From ChatMUD Wiki
Revision as of 00:18, 5 September 2020 by Athlon (talk | contribs) (Created page with "There are a number of moo programming tutorials out there. This one aims to be a bit different from them. For one thing, it is ChatMUD-tailored, which is important to note bec...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

There are a number of moo programming tutorials out there. This one aims to be a bit different from them. For one thing, it is ChatMUD-tailored, which is important to note because ChatMUD's codebase is a fair bit expanded from the one on lamda MOO. For another thing, this tutorial isn't supposed to be easy, and it won't teach you how to make an interesting object by the end. Instead, it will hopefully furnish you with knowledge of many basic concepts, a toolbox to help you remember common coding patterns, and exercises to help satiate your curiosity.

If you have questions, feel free to ask on the dev channel. There is usually at least one or two people awake who can help you puzzle something out. Also see the end of this document for links to some of the other moo programming tutorials previously mentioned, which apply many of the same concepts discussed.

Some Basic Assumptions

We assume that you have created your ChatMUD character and gotten a programmer bit. This can be done by connecting to the progbits channel and sending the message "progbit" to it. We also assume that you know how to create and destroy objects, and that you know how to examine objects to see the possible verb commands you can use with them that way. Also, hopefully you are working on a computer, because programming on a mobile interface is usually quite laborious.

Conventions

If there is a single-line command for you type into the mud, it is proceeded by a > sign. Verb programs start with a level 4 heading which contains the @program line that begins the programming of the verb, and ends with a single . You can copy and paste the text, including the beginning and ending line, to send all of this code to the mud, and it should work as a valid program.

Output from the moo is proceeded by two less than signs. <<

It's Literally this simple: single-line evaluation of literal variables

First let's evaluate some literal variables. The semicolon ; command begins a single-line evaluation. This lets you run simple snipets of code without programming a whole verb and having to delete it. It's also really nice for testing what verbs do, as you'll get the return value right there.

> ;5
<< => 5
<< [used -2 ticks, 0 seconds. Finished in 0 seconds.]

> 5+7
<< => 12
<< [used -2 ticks, 0 seconds. Finished in 0 seconds.]

> ;"hello world"
<< => "hello world"
<< [used -2 ticks, 0 seconds. Finished in 0 seconds.]


Nothing too weird here yet. Notice chatMUD adds 5+7 and instnatly returns 12. Also, notice the double quotes around hello world. Double quotes indicate a string. You must have double quotes around a string, because otherwise ChatMUD doesn't know it's a string. Try evaluating hello world without double quotes around it, to see what error you get. Be very mindful of where your quotation marks are! This is one of the most common pitfalls for any coder, even experienced ones.

>#2
<< => #2  (Sinistral)
<< [used -2 ticks, 0 seconds. Finished in 0 seconds.]

A number sign # followed by an object number will return that object, and ChatMUD is nice enough to tell you what its name is as well. Try evaluating random object numbers to see what you can find. Every time someone uses the @create command to make something, they make a new object, and it gets an object number. Every room, exit, and new player created also takes up an object number. To see the highest object number currently in use, run this command:

> ;max_object();
<< => #21183  ()
<< [used -1 ticks, 0 seconds. Finished in 0 seconds.]

The number will probably be different at any given time as objects are created and destroyed. The evaluation you just saw is a built-in function. ChatMUD has a number of built-in functions that make up the very lowest level and usually have easier ways of doing the same functionality built on top of them. These easier ways have the jargon word of wrappers. You will not be using many of them. For a full list, along with thousands of words of programming information that you probably don't care about, check out the full lamda moo + stunt programmers' manual.

Now let's evaluate some verbs. First, let's make the moo shout at us, by capitalizing any text we send over.

> ;$string_utils:uppercase("I like ChatMUD!");
<< => "I LIKE CHATMUD!"
<< [used 168 ticks, 0 seconds. Finished in 0 seconds.]

Remember, the string you send in must have quotes around it! Now, let's make ChatMUD calculate the greatest common divisor between 24 and 30, which is a problem you may remember from math class at school.

> ;$math_utils:gcd(30,24);
<< => 6
<< [used 21 ticks, 0 seconds. Finished in 0 seconds.]

Pretty neat, huh? These examples might seem contrived. How would anyone know what math_utils was, or that the gcd function was available? The first thing to know is that when you see a colon : symbol, and it's not part of a string, the thing on the left of the colon is an object, and the thing on the right is a verb. The verb is part of the object. Think of the object as a box, with verbs that work like levers to make things happen.

Try evaluating $math_utils now, and you will see that this evaluation is perfectly valid and will return object #20. $math_utils is just a convenient mnemonic to reference object #20, and this mnemonic is what is known as being corrified. Similarly, you can evaluate $string_utils. What object number do you get?

Note that these objects aren't supposed to be tangible things that you can pick up and manipulate, like the objects you may have been filling your area with. They are more of an organizational scheme to group similar sets of functions together. The math_utils object has a number of math algorithms, and the $string_utils object has many verbs to help you manipulate strings. Meanwhile the $object_utils object has a number of useful verbs for probing at the functionality of other objects. To get a full list of all of the utilities packages, type @kids $generic_utils. This command will show you all of the descendants of the generic utilities package. You can even make your own utilities package if you hvae ideas for useful functions for others to try; just make an object that is based off the $generic_utils package and go from there.

Let's explore what is available on the math and the string utilities objects. To do so, type the following:

>help $math_utils

You will get a long list of verbs that are available. Most of them provide adequate help information about how to use them. Type help $math_utils:verbname to see help for that particular verb. Now you can do the same for string utilities, and most of the other utilities packages.

Unfortunately, these help files are not automatically updated, so if a wizard adds a new verb to a utilities package, the help information is generally not updated to reflect the change. You can get an exhaustive list of verbs on a utility package by typing: @verbs <object>. Do this now to the string utilities object, and you will see several new verbs that were not listed in the help file. Try getting help on these new verbs. The results are mixed, but you will often be rewarded with information. If you are not, complain on the dev channel.

Stretch your thinking

The stretch your thinking sections contain some exploration questions to help you solidify what you learned, and stretch your thinking. There are no right or wrong answers here. But feel free to try things out while struggling with them. ChatMUD is built with security in mind, meaning it's very difficult to do permanent damage.

  1. Every object might have verbs on it, not just the utils packages. Use the @verbs command to probe some of the items in the player creations database to see what verbs they have.
  2. Can you add or subtract other kinds of literal values besides numbers? Try adding and subtracting strings. Try adding or subtracting objects.
  3. The multiplication, division, and modulus operators are *, /, and %, respectivly. These operators only work on numbers. They don't work on strings or objects. However, you might notice something weird with the division operator. What happens if you evaluate 32/4? What about 33/4? 31/4? Make a conjecture about why your results come up this way, and try some other division evaluations to see if it checks out.
  4. What happens if you send the wrong type of value to a verb? Well let's find out! Evaluate:
> ;$string_utils:lowercase(35);