Aardwolf MUD Home Page Link

Location: Home / Lua Coding / Lua Mud Howtos

This page contains snippets of code to frequently asked questions when working with the Lua / Mud system. As builders become familiar with the code, useful hints and answers will be added to this page.

How to test for good, evil, neutral?

The old mobprog system had 3 commands for isgood, isevil and isneutral. The Lua based system allows you to be more specific and test the character alignment number. To get the equivalents of the old commands use:

if ch.align >= 875 then .... (good alignment)
if ch.align <= -875 then .... (evil alignment)
if ch.align > -875 and ch.align < 875 then .... (neutral)

How to test for a non-player?

The 'isplayer(CH)' function tests if a character variable is a player. The old mobprog system had no concept of not. In Lua, you would just wrap a 'not' around it:
if not(isplayer(ch)) then
   say("I don't deal with mobs, shoo!")

How to do equivalent of 'mob force mob oload aylor-321' etc?

In the old mobprog system, 'mob force' forces the target to execute a MUD command. This works fine in the Lua force also. The exception is 'mob force mob ...' - In the old system this causes the mob being forced to execute an arbitrary line of mobprog code within its own space. Because Lua is a completely separate environment integrated back in to the mud, you cannot simply force a mob to execute a lua statement.

The workaround for this is more flexible, but a little more complex. Using the v2 mobprog example below, the mob loads a new mob, then forces it to load and wield a sword:

--- Create a new mob : forge worker.
mob mload aylor-20
--- Force it to load an aylorian sword on itself
mob force worker mob oload aylor-321
--- Force it to wield it. No 'mob force mob' here - regular MUD command.
mob force wield wield sword

Because the Lua call allows you to change the actors (including self), and because mload returns a CH variable for the mob created, you can combine these to achieve the same results.

You would split this into two progs, then call the second prog changing the 'self' actor to the mob that was just loaded:

--- Create a new mob
local newmob = mload("aylor-20")

--- Call the second prog, as the new mob. Imagine the new prog is aylor-50.
--- We're only changing self to newmob in the call so put nil in the other
--- variable positions. Think of this as "execute aylor-50 as the newmob"

--- Aylor-50 prog: now being executed as the new mob is simply:
mdo("wield sword")

How to restrict a wandering mob by sector

This mostly came up because of mobs wandering on the continents - being able to restrict a fish to river only sectors or a mountain lion to the mountains etc

The solution for this involves using the new 'lastroom' character property along with an entry trigger. If the mob tries to enter a sector type you don't want it to be in, transfer it back to its last room.

--- Fish mob, can only wander to another river or a lake.
--- At the point an entry trigger fires, the mob is already in the room
--- it is attempting to move to.

local lastroom = self.lastroom
if lastroom ~= nil then
   if self.room.sector ~= SECT_RIVER and self.room.sector ~= SECT_LAKE then

The trigger for this is:

Current Triggers
No   Trigger Type Program       Old Vnum Phrase/Percentage
==   ============ ============= ======== ==================================
 1 - Entry        test-47              0 100