
                             LPC Basics
                     Written by Descartes of Borg
                            23 april 1993


                              第一章
                          基本 LPC 程式

第一章的标题其实定义的不好,因为根本不是在LPC里写程式,事实上一个写LPC的设计师,写的是 "
object". 那么,这有甚么不同呢? 目前最大的不同就是在执行 "program file" 和 "object
file" 是相反的. "program file" 和 "object file" 只是 is simply any single
file with code in it.在一个像 BASIC 或是 C 的程式,一定会有一定的起始点和终结点来
执行程式.

看看这两种程式的例子:

C                         | BASIC
void main() {             | PRINT "Hello mud."
  printf("Hello mud.\n"); | END
}                         |

在 BASIC 里,除了一些较新的版本,否则程式是被翻译成零与一,也就是当你在执行这个程式时,编
译器才会把程式转换成电脑所懂的语言(零与一). 相反的,C 是一个已编译的程式(compiled
program), 也就是在你执行某段程式前,必须要把它写成一个电恼懂得的语言.很显然的,一个已编
译的程式在执行的速度上会比较快,因为它省去了一段编译的程序.

当在执行一个 BASIC 程式,执行的顺序是从第一行开始而一直执行下去 (除非遇到 goto,
gosub 等指令).在 C 里,一个程式是从一个称为"main ()" 的 "function" 开始执行的,直
到遇到整段程式的最后一行,或是遇到一个"return"指令,或是遇到一个"call to exit ()" 的
指令. 在一个程式里,main() 通常会"call"一个附属程式. As you see, programs have
a place where they clearly begin when run and end and are done with
until run again.  可是在 MUDs 里整个程式是用一个 C 语言所写的程式作为驱动程式.所有
的 LPC 档是都是为了配合执行这个驱动程式而写成的"物品"(objects).
一个简单的 LPC 写的"房间"(room)大概就是如下的程式:

void create() {
    set_property("light", 2);
    set("short", "Descartes' simple workroom");
    set("long", "You do feel that Descartes makes dull workrooms.\n");
    set_exits( ({ "d/standard/square" }), ({ "square" }) );
}

就像 C 语言的 main(),还有一些驱动程式里里的 creat(),或是 reset() 等等,整个程式的
执行都是从是从整个程式的起点执行起的.可是前面所提到的是它们唯一的共同点.在一般的物品程式
里,不论 create() 还是 reset() 都不需有要跳回主程式的"出口"(exits).在 C 语言里,
main() 的功能是让整个程式能够执行. 可是如果有 LPC 物品(LPC objets),程式并不会执行,
取而代之的是一些功能(funcions)被呼叫出来.举个例子来说,我想知道我东边的房间的长叙述
(long description),可是那间房间还没被使用过,(换句话说,房间还没存到记忆体里).当玩家
下一个往右边看的指令,程式会向现在的"房间"询问东边的房间长叙述是啥.因为它还没被存到记忆
体里,(也就是说驱动程式还不知道它的存在),它会先被load到记忆体里.而玩家所在的这个房间会
跟驱动程式询问它的长叙述,而驱动程式发现它自己没有这样的资料,它会把在前一个房间所想要知道
的东西(被存在一个档案里)找出来.在找到之后会把它load到记忆体里.在这时候我们会有一个空房
间.为了能让制造的人(creators)能设定一些预设质(starting values),驱动程式会把
create() 或 在compat mode 里的reset() 呼叫出来.一当驱动程式能够熟悉整个房间,它可
以呼叫 query_long() (也就是在 compact mode 里的 long() )来看整个房间的长叙述
然后再把这些资料传回原来的房间.不过,如果这个房间如果以经被用过(也就是曾经在驱动程式的记
忆体里)它只要把这房间的 query_long() 呼叫出来再把这些资料传到第一个房间去.而就不需要
再呼叫create() 的必要.
因此,在　LPC 里,create() 和 reset() 的用途是来设定每一个物品(object)的预定质.如果
这些物品(objects)不需要预定质,则不需要这一个功能(function). And
similarly, the object is not done with once create() (or reset()) are
finished being executed.

现在我们已经谈到达整章讨论的重点.一个完整的 LPC 物品(LPC object)包涵哪些呢?这样说吧,
一个 LPC 物品是几个功能(functions)被集合到一个档案(file)里.而这些功能在档案里的的顺
序事实上是无关紧要的.换句话说:

void init() { add_action("smile", "smile"); }

void create() { return; }

int smile(string str) { return 0; }

IS EXACTLY THE SAME AS:

void create() { return; }

int smile(string str) { return 0; }

void init() { add_action("smile", "smile"); }

ALSO THE OBJECT:

void nonsense() {}

i a  valid object, but probably would not interact properly with other
objects on your mud (it would be invisible, weightless, etc.)

CHAPTER SUMMARY:
There is no beginning point or ending point of LPC code, as LPC is used
in creating objects, not programs.  LPC objects consist of one or more
functions whose order in the code is irrelevant.  These functions are
used by the driver and other objects in the game to allow the object
to interact with other objects in the game.

A NOTE ON THE FUNCTIONS reset() AND create():
As its name implies, the function reset() is used to reset the room.
In all native muds, this is the sole function of reset().  By resetting
the room, I mean that every so often, the driver calls this function

in objects so that certain things like regenerating monsters, resetting
doors, etc. can happen.
In compat muds or LPMuds of a release earlier than 3.0, reset() also
acts in the same manner as the create() function.  In other words,
on creation
In native muds:
    the driver calls create()
In compat muds:
    the driver calls reset() (It also passes an argument of 0, but that
                              will be discussed when we get to
functions)

And at reset time:
In native muds:
    the driver calls reset()
In compat muds:
    the driver calls reset() (Here the reset number is passed)

Since an argument is passed to reset() in compat muds, you can tell the
difference between when the function is being used in the sense of
create()
and when it is being used as reset().  But this all is for later
discussion.
