Author Topic: Blocking an exit  (Read 1199 times)

Offline sorin

  • Acquaintance
  • *
  • Posts: 5
    • View Profile
Blocking an exit
« on: February 12, 2014, 11:59:50 AM »
Hi guys,
After days and days of reading and trying to understand the commands, I'm still stuck.

I want that two guards to block the exit, so only fighters (members of the fighter class) can pass. I came up with this piece of code, which seems to not be working.

int block_player(object ob){
if (ob->GetClass() != "fighter")
ob->SetGuard("up", "The guards blocks your way!");                 
                  } 

              void init(){
    ::init();
call_out((: block_player, this_player() :), 0);
   }


It still blocks everyone no matter what class. Where I'm wrong?

Offline sorin

  • Acquaintance
  • *
  • Posts: 5
    • View Profile
Re: Blocking an exit
« Reply #1 on: February 12, 2014, 12:41:17 PM »
I solved it, thanks to Kalinash

Offline quixadhal

  • BFF
  • ***
  • Posts: 631
    • View Profile
    • WileyMUD
Re: Blocking an exit
« Reply #2 on: February 12, 2014, 03:08:43 PM »
Just to help others who wander in here...

Code: [Select]
#include <lib.h>
#include "../customdefs.h"

inherit LIB_NPC;
string privs;

int AllowPass(object who, object what){
    if(who->GetKeyName() == privs) return 1;
    return ::AllowPass(who, what);
}

int CheckVisitor(object who){
    eventForce("say I'm sorry, "+remove_article(who->GetShort())+
            ". Only "+capitalize(privs)+" is allowed there.");
    return 1;
}

static void create(){
    npc::create();
    privs = query_privs();
    SetKeyName("fighter");
    SetId( ({"human","npc","warrior","","fighter"}) );
    SetShort("a fighter");
    SetLong("This is a sample warrior NPC.");
    SetLevel(10);
    SetRace("human");
    SetClass("fighter");
    SetGender("male");
    SetInventory(([
                MY_ARMOR "/chainmail.c" : "wear chainmail",
                MY_ARMOR "/leather_boot_r.c" : "wear boot",
                MY_ARMOR "/leather_boot_l.c" : "wear boot",
                MY_WEAP "/sword.c" : "wield sword",
                ]));
    SetStat("strength",40);
    SetGuard("west", (: CheckVisitor :));
    SetGuard("east", "The fighter blocks your travel in that direction.");
}
void init(){
    ::init();
}

In particular, the SetGuard() lfun likely causes the player object to call AllowPass() on the guard NPC.  If AllowPass() returns 1, the guard does nothing and movement is allowed.  If it returns 0, the argument to SetGuard() is returned as an error message, and movement is denied.  Since the message uses happy code, that gets evaluated at that time, so you get a nice personalized message.

Someone less lazy can correct me if I got the details wrong. :)