Scripting

# Text formatting options

The text formatting option is denoted after the ':' in the variable reference in the text. For instance "{0:Names} kick{0:s} {1} in the head". The formatting options in that string are: Names, s and no formatting option.

All formatting options starting with an uppercase will result in an uppercase string. All examples are written with

* name → you, 'name of character' (same as no formatting option)
* name's → your, Afara's
* s → s, 'nothing'
* he → you, he, she, it
* his → your, his, her or its
* him → you, him, her, it
* was → were, was
* is → are, is
* has → have, has
* does → do, does
* doesn't → don't doesn't
* es → es, 'nothing'
* ies → y, ies
* sir → lady, sir
* himself → yourself, himself, herself, itself
* hisown → your own, his own, her own, its own

### Text colors

* $Em$ → Normal text. Colored text. Normal text.
* $Damage$ → Normal text. Colored text. Normal text.
* $Heal$ → Normal text. Colored text. Normal text.
* $Poison$ → Same as $Heal$?
* $Fire$ → Normal text. Colored text. Normal text.

# Scripting examples

### Prevent leaving

```
room.RoomEvents[RoomEvent.CreatureLeaving] = function(ev)
    if ev.Exit.Direction == "small cave" then
        ev.Canceled = true
        room:Write('{0:Name} tried to enter a small cave but {0:was} blocked by a magical barrier.', ev.Creature)
    end
end

```

### Scripted object interaction

—Find a trapdoor behind the stall when searching section

```
room.RoomEvents[RoomEvent.CreatureObjectInteraction] = function (ev)
    if ev.Verb == 'Search' and ev.Object.Name:Contains('section in the back') then—Make sure the search message is not shown
        ev.Handled = true—Print search message
        room:Write("{0:Name} search{0:es} a section in the back...", ev.Creature)

        -- Open the trapdoor and print mesage
        room.Exits.down:Open()
        room:Write("A trapdoor was found under a layer of dust!");
    end
end

```

### Complete quest objective

```
local littleKid = 'b3cec951-83c0-41df-8209-8f0f9f088826'

npc.RoomEvents[RoomEvent.CreatureEntered] = function(ev)
	if ev.Creature.Id == littleKid then
		npc.Room:Write("{0:Name} starts crying and hugs {1} intensively", npc, ev.Creature)
		npc:SayTo(ev.Creature.FollowTarget, "Thank you! My hero!")

		local leader = ev.Creature.FollowTarget
		ev.Creature:StopFollowing()
		leader.Quests['e8037651-5218-45e4-ae2a-6ca663faf758']?.Objectives['974d91fa-779f-4c41-af71-b88140261343']?.Complete()

		server:ScheduleTask(function()
			npc.Room:Write("{0:Name} walks home with her kid", npc)
			npc:Despawn()
			ev.Creature:Despawn()
		end, 5000)
	end
end

```

### Transfer creature on room enter

—Player Slips or Slips and Falls in the Large Pool!

```
function HandleCreatureEnteredRoom (ev)
   local roll = math.random()
   if roll < 0.2 then
       room:Write("{0:Name} slip{0:s} but regain{0:s} {0:his} balance quickly!", ev.Creature)
   elseif roll < 0.5 then
       room:Write("$Damage${0:Name} slip{0:s} falling into murky pool below!$$", ev.Creature)
       ev.Creature:Move(server:GetRoom("sewers1.73"))
       ev.Creature:Paralyze(2000)
   end
end

room.CreatureEntered:Add(HandleCreatureEnteredRoom)

```

### **Paralyze creature on room enter**

—Here's one with three possible outcomes:

```
-- Player Slips, Gets Splashed or Falls In!
function HandleCreatureEnteredRoom (ev)
   local roll = math.random()
   if roll < 0.3 then
       room:Write("{0:Name} slip{0:s} but regain{0:s} {0:his} balance quickly!", ev.Creature)
   elseif roll < 0.3 then
       room:Write("{0:Name} slip{0:s} and get{0:s} splashed in {0:his} face with nasty water!", ev.Creature)
   elseif roll < 0.4 then
       room:Write("{0:Name} slip{0:s} and fall{0:s} into the swirling pool of nasty water!", ev.Creature)
       -- Pause the player 2 seconds.
       ev.Creature:Paralyze(2000, {
           Complete = function()
               room:Write("{0:Name} pull{0:s} {0:himself} out of the water and onto the bulkhead.", ev.Creature)
           end
       })
   end
end

room.CreatureEntered:Add(HandleCreatureEnteredRoom)

```

### **Damage creature on enter room**

—Here's one where the creature takes damage:

```
-- Creature Enters, May get Splashed by Boiling Water!
function HandleCreatureEnteredRoom (ev)
    if math.random() < 0.5 then
        if ev.Creature:CanTakeDamage(ForceType.Fire) then
            damage = 10
            room:WriteDamage("{0:Name} slip{0:s}, sliding right under the scalding hot water for {1} damage!", ev.Creature, damage)
            ev.Creature:TakeDamage(damage, ForceType.Fire)
        elseif math.random() < 0.9 then
            room:Write("{0:Name} slip{0:s}, barely avoiding the scalding hot water!", ev.Creature)
        end
    end
end

room.CreatureEntered:Add(HandleCreatureEnteredRoom)

```

### Searching a Room Object to Reveal an Exit

—find a hole in the wall

```
function HandleCreatureObjectInteraction (ev)
   if ev.Verb == "Search" and ev.Object.Name:Contains('trash') then
   	ev.Handled = true
       room:Write("{0:Name} search{0:es} the pile of trash...", ev.Creature)

       ev.Creature:Paralyze(1500, {
           Complete = function()
               if ev.Creature:RollSkillBinary(CreatureAttribute.SearchSkill, 3, 0.75) then—Opens the door to the west
                   room.Exits.west:Open()
                   room:Write("A large hole has been found behind the trash in the western wall!");

               else
                   local ran = math.random(0, 3)
                   if ran == 0 then
                       room:Write("{0:Name} find{0:s} nothing but manure.", ev.Creature)
                   elseif ran == 1 then
                       room:Write("{0:Name} pull{0:s} out a piece of something disgusting.", ev.Creature)
                   else
                       room:Write("{0:Names} hand get{0:s} bitten by something!", ev.Creature)
                       if ev.Creature:CanTakeDamage() then
                           ev.Creature:TakeDamage(5)
                       end
                   end
               end
           end
       })
    end
end

room.CreatureObjectInteraction:Add(HandleCreatureObjectInteraction)

```

### Creature Searches Object and is Trapped

—Search Treasures to Find a Very Rare Item OR Get Trapped!

```
function LockRoom (ev)
   if ev.Verb == "Search" and ev.Object.Name:Contains('treasures') then
       ev.Handled = true
       room:Write("{0:Name} search{0:es} the many treasures...", ev.Creature)

       if (not ev.Creature:HasItem('0818e982-cca4-4c61-9162-2b0f48f9e433')) and
       	ev.Creature:RollSkillBinary(CreatureAttribute.SearchSkill, 3, 0.7) then
           local key = ev.Creature.SpawnItem('0818e982-cca4-4c61-9162-2b0f48f9e433')
           room:Write('{0:Name} search{0:es} {1} and find{0:s} {2}!', ev.Creature, ev.Object, key)
       else
           local ran = math.random(0, 1)
           if ran == 0 then
               room:Write("{0:Name} can't decide on what {0:he} want{0:s}...", ev.Creature)

           else
               room:Write("{0:Name} grab{0:s} a suspicious golden idol -- **CLiCK**", ev.Creature)
               -- script waits 2 sec
               server:ScheduleTask(function()
                   room:Write("A series of clicks and pulleys are heard around the room... IT's A TRAP!", ev.Creature)

                   -- script waits 1 sec
                   server:ScheduleTask(function()
                       room:Write("Suddenly a solid metal slab falls over the hole in the wall and automatically locks with heavy bolts!", evCreature)
                       -- remove or lock the exit east.   Player will have to wait for someone to help them or recall.
                       -- This will simulate a death trap.  But really, it will force the player to socialize with SOMEONE, or to heed the advice
                       -- (I'll have a warning to keep a scroll of recall with you, to low level players going in.)
                       room.Exits.east:Lock()
                   end, 1000)
               end, 2000)
           end
       end
   end
end

room.CreatureObjectInteraction:Add(LockRoom)

```

### Searching a Room Object for a Quest Item

—Searching the Dead Body

```
local lastSearch = server.Now
function HandleCreatureObjectInteraction (ev)
if ev.Verb == "Search" and ev.Object.Name:Contains('body') then
    ev.Handled = true
    room:Write("{0:Name} search{0:es} a dead body...", ev.Creature)

```

 -- NOT IMPLEMENTED: check if player has quest and finished quest: f49b76d5-434f-4d6f-811d-3af8403a9250
 if lastSearch:AddSeconds(10) > server.Now then
 room:Write("{0:Name} can't find anything, as if it's already been picked clean.", ev.Creature)

 elseif (not ev.Creature:HasItem('0818e982-cca4-4c61-9162-2b0f48f9e433')) and ev.Creature:RollSkillBinary(CreatureAttribute.SearchSkill, 3, 0.75) then
 lastSearch = server.Now

 local key = ev.Creature:SpawnItem('0818e982-cca4-4c61-9162-2b0f48f9e433')
 room:Write('{0:Name} search{0:es} {1} and find{0:s} {2}!', ev.Creature, ev.Object, key)

 else
 lastSearch = server.Now

```
     		local ran = math.random(0, 3)
           if ran == 0 then
               room:Write("{0:Name} STOP{0:s} as the skull falls off, and rolls away!", ev.Creature)

           elseif ran == 1 then
               room:Write("{0:Name} pull{0:s} out a hand covered in rancid goo.", ev.Creature)

           elseif ran == 2 then
               room:Write("{0:Names} hand get{0:s} bitten by something!", ev.Creature)
               if ev.Creature:CanTakeDamage() then
                   ev.Creature:TakeDamage(5)
               end

           else
               room:Write("{0:Name} pull{0:s} {0:his} hand back in shock!", ev.Creature)
               -- small rat
               room:Spawn("309d0cf8-5963-4db1-9e9e-d86b288cfb1f"):Attack(ev.Creature)
           end
       end
   end
end

room.CreatureObjectInteraction:Add(HandleCreatureObjectInteraction)

```

### Searching Multiple Room Objects in a Whole Zone

—Rat's Nest Zone Script (in room)

```
room.CreatureObjectInteraction:Add(room.Zone.Share.CreateRatNestHandler(room))

--Zone Script!
zone.Share.CreateRatNestHandler = function(room)
   local lastSearch = server.Now

   return function(ev)
       if ev.Verb == "Search" and ev.Object.Name:Contains('rat') then
       	ev.Handled = true
           room:Write("{0:Name} search{0:es} the trashy rat's nest...", ev.Creature)

           if lastSearch:AddSeconds(10) > server.Now then
               room:Write("{0:Name} end{0:s} up empty handed as someone just searched this pile of trash...", ev.Creature)

           else
               lastSearch = server.Now
             	local ran = math.random(0, 5)
               if ran == 0 then
                   room:Write("{0:Name} find{0:s} a wad of slimy gunk.", ev.Creature)
               elseif ran == 1 then
                   room:Write("{0:Name} pull{0:s} out a holy boot.", ev.Creature)
               elseif ran == 2 then
                   room:Write("{0:Names} hand get{0:s} bitten by something!", ev.Creature)
                   if ev.Creature:CanTakeDamage() then
                       ev.Creature:TakeDamage(5)
                   end
               elseif ran == 3 then
                   room:Write("{0:Name} pull{0:s} {0:his} hand back in surprise!", ev.Creature)
                   -- small rat
                   room:Spawn("309d0cf8-5963-4db1-9e9e-d86b288cfb1f"):Attack(ev.Creature)
               else
                   room:Write("{0:Name} pull{0:s} {0:his} hand back in shock!", ev.Creature)
                   -- mama rat
                   room:Spawn("9e10185a-596d-448c-878e-055898498f46"):Attack(ev.Creature)
                   -- small rats
                   room:Spawn("309d0cf8-5963-4db1-9e9e-d86b288cfb1f"):Attack(ev.Creature)
                   room:Spawn("309d0cf8-5963-4db1-9e9e-d86b288cfb1f"):Attack(ev.Creature)
               end
           end
       end
   end
end

```

## Spawn additional enemies at 50% health

```
local npcSpawnBlocked = false
function HandleCreatureHealthChanged(ev)
  if not npcSpawnBlocked and npc.HealthRatio < 0.5 then
    npc.Room:Write("{0:Name} yells, 'GUARDS! GUARDS!'", npc)

    for crea in iter(npc.Room.Creatures) do
      if crea.IsPlayer then
        local spawnedCreature = npc.Room:Spawn("gobnest-goblin-guard")
        npc.Room:Write("{0:Name} comes to rescue his king", spawnedCreature)
        spawnedCreature:Attack(crea)
      end
    end

    npc.Room:Write("{0:Name} says, 'KILL THEM! BIG PAIN!'", npc)

    npcSpawnBlocked = true
  elseif npc.HealthRatio > 0.99 then
    npcSpawnBlocked = false
  end
end

npc.Events[Event.HealthChanged] = HandleCreatureHealthChanged

```