Chapter 1. Overview of LPC Programming

Table of Contents
1.1. Your first LPC code block
1.2. Architecture
1.3. Objects

You might have prior experience programming in other languages... then again you might not. This guide does cover how to program in LPC, but this isn't a great guide to how to program in general. The best way to learn how to program is to spend years writing code and reading other peoples' code until you eventually have a deeply nuanced understanding of what you're doing.

No one built Rome in a day. No one learned Calculus in a day. No one learns how to program in a day, either. Seriously--take my word for it.

This manual will walk through the basics of LPC. You can use it to learn how to do new things and you can use it as a reference when you can't remember how to do something you did before.

FIXME - add more whining gibberish here that shows how l337 we are.

1.1. Your first LPC code block

This may not make a lot of sense and we're going to gloss through some things in order to then turn around, repeat everything, and explain further. So bear that in mind as you keep reading.

LPC code is written in files that end in .c or .h. .c files are for code while .h files are header files. This is similar to C and C++ if you're familiar with those languages.

We're going to look at nectar.c which is a file with LPC code in it that implements a crystal glass of nectar which is a form of alcohol on DarkRifts.

Example 1-1. Sample LPC code: nectar.c

    
    #include <mudlib.h>
    
    inherit ALCOHOL;
  5 
    void create() {
      ::create();
      set_author("flake");
      set_id( ({ "glass", "nectar"}) );
 10 
      set_short("A crystal glass of nectar");
      set_long("The deep earthy smells waft from the pure crystal glass "
               "and surround you in an refreshing intoxication.  It is "
               "good to be Immortal.");
 15 
      set("empty_short", "An empty glass");
      set("empty_long", 
          "An empty glass with but a savory hint of the lovely nectar it "
          "once held.");
 20 
      set("alcohol", 2);
    
      set("drink_message", 
          "You drink from the glass and the smooth liquid gently washes "
 25       "down your throat in such a pleasant interlude that lasts for "
          "hours that even Aubrey couldn't annoy you.\n");
    
      set("other_drink_message", 
          "drinks sweetly from the pure crystal glass of nectar.\n");
 30 }
    
    

Let's look at the first line:


#include <mudlib.h>

In this line, we're including the file mudlib.h. mudlib.h defines a series of constants that are used by many of the code files on the mud.

Because the constants are used in so many places, it's much easier to put them in one file and include that file in all the code files that need them. If we ever have to change the values of one of the constants, we can change it in mudlib.h and that affects all those files. The alternative is to hard-code the constants in every file and if we ever have to change one, we have to go change it in every single file. Our mud is in the tens of thousands of files now so centralizing things like constants is a big deal.


inherit ALCOHOL;

This line inherits ALCOHOL. ALCOHOL is a constant defined in mudlib.h which points to the codefile that holds the code that dictates how alcohol behaves. We inherit the behavior here because we're creating a glass of nectar which is a form of alcohol on DarkRifts and we want it to have the properties and functionality of alcohol.

Inheriting code allows us to centralize code that's common between groups of objects into one file. Instead of coding the behavior of alcohol into every code file that defines an alcoholic drink, we can centralize the code into one file and inherit it for every drink. We do the same thing for rooms, containers, monsters, objects, etc....

If you're paying close attention, you'll notice a pattern here. LPC (like many programming languages) has features that allow you to centralize functionality so that it only exists in a few places rather than a lot of places. This makes writing code much easier and it makes maintaining that code much easier as well. Both of these aspects are very important to think about when designing and writing code.


void create() {
  ::create();
  .
  .
  .
}

Here we declare the create function. This function gets called when the object is initially loaded. We define all the object's properties here: author, name, ids, short description, long description, and any other properties the object requires to function.

The first line of the create function here is ::create(). The :: indicates that we're calling the superclass' create function.

Remember that our nectar class inherits from ALCOHOL. That means that ALCOHOL is the superclass or parent class--the class from which the nectar class is derived. So ::create() calls the create function in ALCOHOL which sets all the properties that are common to alcohol objects on our mud.

After doing that, we then define all the properties that are specific to this nectar class. Things like the short description, long description, author, ...


  set_author("flake");

Each object in the mud has an author. This author is credited with various things over time as the object is manipulated. Additionally, the author gets bug reports whenever this object encounters problems.


  set_short("A crystal glass of nectar");
  set_long("The deep earthy smells waft from the pure crystal glass "
           "and surround you in an refreshing intoxication.  It is "
           "good to be Immortal.");

Most objects have a short description which is used when we list a group of objects and a long description which is used when a player looks at the object.


  set("empty_short", "An empty glass");
  set("empty_long", 
      "An empty glass with but a savory hint of the lovely nectar it "
      "once held.");

  set("alcohol", 2);

  set("drink_message", 
      "You drink from the glass and the smooth liquid gently washes "
      "down your throat in such a pleasant interlude that lasts for "
      "hours that even Aubrey couldn't annoy you.\n");

  set("other_drink_message", 
      "drinks sweetly from the pure crystal glass of nectar.\n");

This code defines properties that are used by the ALCOHOL code to define the behavior of this alcoholic drink.

And that's it! That's your first code file.