Author Topic: Extended Workroom  (Read 1786 times)

Offline brainfrz

  • Acquaintance
  • *
  • Posts: 15
    • View Profile
Extended Workroom
« on: January 23, 2009, 09:40:52 am »
What I am trying to do is make it possible to have multiple rooms in your workroom rather than just a single room. This could be so you'd have different rooms to work on different things, or maybe just have different rooms so you could just feel more at home. The problem is, I'm trying to make the privacy field extend throughout the entire "house". Also, as a sidefeature, I'm making the privacy field take users that are allowed and let them in even when the field is up. Both things share the same problem: I don't know all the connected rooms beforehand, and I need to reload all the connected rooms, as well as change some variables in them. Namely, `string *ban' and `string *trust' must be changed in each room, loaded or not. Here's what I have currently:
Code: ("workroom_base.c") [Select]
/*
 *  The idea behind this base_file is to allow for creators to have a larger
 *  workroom than just a single room. This "house" will still have a privacy
 *  field, and will also have a list of people who are "banned" from your house,
 *  and a list of those who
 */

#define ALLOW_FILE __DIR__".allow.list"
#define BAN_FILE   __DIR__".ban.list"

#include <rooms.h>

inherit LIB_ROOM;

int privacy = 0, sizeTrust, sizeBan;
string *trust, *ban, owner, desc = "";

void create_workroom() { }
void init_workroom()   { }
int DumpList();
int DumpAllowList();
int DumpBanList();
string SetPrivacy(int i);
string SetDesc(string str);
string GetDesc();

string DescPrivacy()
{
    if( privacy )
        return desc + "\n\
%^YELLOW%^The walls seem to be encased in a yellowish shield.%^RESET%^";
    return desc;
}
string SetDesc( string str )
{
    return desc = str;
}
string GetDesc()
{
    return desc;
}

static void create()
{
    trust = explode(read_file(ALLOW_FILE), "\n");
    ban   = explode(read_file(BAN_FILE), "\n");
    sizeTrust = sizeof(trust);
    owner = trust[0];
    room::create();
    SetShort("Within a House");
    SetClimate("indoors");
    SetAmbientLight(30);
    create_workroom();
    SetDesc(this_object()->GetDesc());
    SetLong((: DescPrivacy :));
}
void init()
{
    ::init();
    init_workroom();
    add_action("mod_privacy", ({ "privacy", "priv" }));
    add_action("eventAllow",  "allow");
    add_action("eventReject", "reject");
    add_action("eventBan",    "ban");
}

int CanReceive( object sneak )
{
    int ret;
    object *living_stack;
    if( member_array(capitalize(sneak->GetName()), ban) == -1 )
        return 0;
    if( !privacy )
        return ::CanReceive(sneak);
    living_stack = get_livings(sneak);
    if( !living_stack || !arrayp(living_stack) )
        living_stack = ({ sneak });
    foreach( object ob in living_stack )
    {
        if( !archp(ob) && member_array(ob->GetName(), trust) == -1 )
        {
            message("info", "You bounce off the privacy field.", ob);
            message("info", ob->GetName()+" bounced off the privacy field.",
                    this_object());
            if( !environment(ob) )
                ob->eventMoveLiving(ROOM_START);
            return 0;
        }
    }
    ret = ::CanReceive(sneak);
    if( ret )
        tell_room(this_object(), "\n\n%^BOLD%^%^RED%^PRIVACY WARNING: %^RESET%^%^RED%^"+identify(sneak)+
                "%^BOLD%^ has entered the room.%^RESET%^\n\n", sneak);
    return ret;
}

int set_privacy( int i )
{
    object *npcs = filter(deep_inventory(this_object()),
            (: living($1) && !interactive($1) :));
    privacy = i;
    SetProperty("no peer", i);
    if( sizeof(npcs) )
    {
        foreach( object npc in npcs )
        {
            tell_room(this_object(), "Ejecting "+identify(npc), npc);
            npc->eventMove(ROOM_FURNACE);
        }
    }
    SetLong((: DescPrivacy :));
    return privacy;
}

int mod_privacy( string str )
{
    int toggle;
    if( !archp(this_player()) &&
            this_player()->GetName() != owner )
    {
        write("You close your eyes, but nothing changes.");
        say(this_player()->GetName()+" is trying to muck around with the
privacy field system.");
        return 1;
    }
    if( lower_case(str) == "list" )
        return DumpList();
    if( !privacy )
    {
        write("You close your eyes, and the walls shimmer briefly.");
        say(this_player()->GetName()+" closes "+possessive(this_player())+
            " eyes, and the walls shimmer briefly.");
        foreach( object room in children(file_name()) )
        {
            message("env", "The walls shimmer briefly before stabalizing their "
                    "yellowish hue.", room);
            room->set_privacy(1);
        }
        return 1;
    }
    else
    {
        set_privacy(0);
        write("You close your eyes, and a gentle draft passes through the
room.");
        say(this_player()->GetName()+" closes "+possessive(this_player())+
            " eyes, and a gentle draft passes through the room.");
        return 1;
    }
}

int eventReject( string who )
{
    if( !who )
        return notify_fail("Who do you want to reject?");
    who = lower_case(who);
    if( who == "list" )
        return notify_fail("Use 'allow list' for that.\n");
    who = capitalize(who);
    if( member_array(who, trust) == -1 )
        return notify_fail(who+" is not on the list!");
    write_file(ALLOW_FILE, implode(trust-({ who }), "\n"), 1);
    write("You will no longer allow "+who+" through the privacy field.");
    return 1;
}
int eventAllow( string who )
{
    if( !who )
        return notify_fail("Who do you want to allow?");
    who = lower_case(who);
    if( who == "list" )
        return DumpAllowList();
    who = capitalize(who);
    if( member_array(who, trust) != -1 )
        return notify_fail(who+" is already on the list!");
    if( member_array(who, ban) != -1 )
    {
        write("You have unbanned "+who+", but not added "+objective(who)+
              " to the trusted list.");
        write_file(BAN_FILE, implode(ban-({ who }), "\n"), 1);
    }
    else
    {
        write("You now trust "+who+" and will let "+objective(who)+
              "through the privacy field.");
        write_file(ALLOW_FILE, "\n"+who);
    }
    return 1;
}
int eventBan( string who )
{
    if( !who )
        return notify_fail("Who do you want to ban?");
    who = lower_case(who);
    if( who == "list" )
        return DumpBanList();
    who = capitalize(who);
    if( member_array(who, ban) != -1 )
        return notify_fail(who+" is already banned!");
   
}

int DumpAllowList()
{
    int sizeTrust = sizeof(trust);
    string list;
    if( sizeTrust == 1 )    list = trust[0];
    if( sizeTrust == 2 )    list = trust[0]+" and "+trust[1];
    if( sizeTrust >= 3 )    list = implode(trust[0 .. <2], ", ")+", and "+trust[<1];
    write("Trusted players: "+list);
    return 1;
}
int DumpBanList()
{
    string list;
    if( !sizeBan )
    {
        write("No one is currently banned.");
        return 1;
    }
    if( sizeBan == 1 )  list = ban[0];
    if( sizeBan == 2 )  list = ban[0]+" and "+ban[1];
    if( sizeBan >= 3 )  list = implode(ban[0 .. <2], ", ")+", and "+ban[<1];
    write("Banned players: "+list);
    return 1;
}
int DumpList()
{
    DumpAllowList();
    DumpBanList();
    return 1;
}
How would I be able to load those objects that aren't loaded and then call it in each of the files.
« Last Edit: January 23, 2009, 09:43:20 am by brainfrz »

Offline Raudhrskal

  • BFF
  • ***
  • Posts: 214
  • The MUD community needs YOUR help!
    • View Profile
Re: Extended Workroom
« Reply #1 on: January 23, 2009, 12:20:46 pm »
Ok, just sit and wait, Uncle 'Skal will hack it for ya...

Code: (/daemon/workprivacy.c) [Select]
/* /daemon/workprivacy.c
 * Raudhrskal, 2009-01-23
 */

#include <lib.h>
#include <dirs.h>

inherit LIB_DAEMON;

private mapping Forcefields;
private mapping Bans;
private mapping Allows;

#define SAVE_WORKPRIVACY DIR_SAVE "/workprivacy"

static void create()
  { daemon::create();
    SetNoClean(1);
    restore_object(SAVE_WORKPRIVACY);
    if(!mapp(Forcefields)) Forcefields = ([]);
    if(!mapp(Bans)) Bans = ([]);
    if(!mapp(Allows)) Allows = ([]);
  }

mapping GetForcefieldsMap() { return copy(Forcefields); }
mapping GetBansMap() { return copy(Bans); }
mapping GetAllowsMap() { return copy(Allows); }

int GetForcefield(string location)
  { if(!location) return 0;
    if(Forcefields[location]) return 1;
    else return 0;
  }

string *GetBans(string location)
  { if(arrayp(Bans[location])) return copy(Bans[location]);
    else return ({});
  }

string *GetAllows(string location)
  { if(arrayp(Allows[location])) return copy(Allows[location]);
    else return ({});
  }

void SetForcefield(string location, int enabled)
  { if(enabled) Forcefields[location] = 1;
    else Forcefields[location] = 0;
    unguarded( (: save_object(SAVE_WORKPRIVACY) :) );
  }

int IsBanned(string location, string name)
  { if(!location) return 0;
    if(!arrayp(Bans[location])) return 0;
    return member_array(convert_name(name), Bans[location]) != -1;
  }

void SetBanned(string location, string name, int flag)
  { if(!arrayp(Bans[location])) Bans[location] = ({});
    if(flag) Bans[location] += ({ convert_name(name) });
    else Bans[location] -= ({ convert_name(name) });
    unguarded( (: save_object(SAVE_WORKPRIVACY) :) );
  }

int IsAllowed(string location, string name)
  { if(!location) return 0;
    if(!arrayp(Allows[location])) return 0;
    return member_array(convert_name(name), Allows[location]) != -1;
  }

void SetAllowed(string location, string name, int flag)
  { if(!arrayp(Allows[location])) Allows[location] = ({});
    if(flag) Allows[location] += ({ convert_name(name) });
    else Allows[location] -= ({ convert_name(name) });
    unguarded( (: save_object(SAVE_WORKPRIVACY) :) );
  }

Code: (/lib/std/workplace_room.c) [Select]
#include <lib.h>
#include <rooms.h>

inherit LIB_ROOM;

#define WORKPRIVACY_D "/daemon/workprivacy"

string location;
string owner;

int validate_forcefield()
  { if((!archp(this_player())) && (convert_name(this_player()->GetName()) != owner))
      { write("You close your eyes, but nothing changes.");
say(this_player()->GetName()+" is trying to muck around with the privacy field system.");
return 0;
      }
    return 1;
  }

int check_forcefield() { return WORKPRIVACY_D->GetForcefield(location); }

int eventAllow(string str)
  { if(!validate_forcefield()) return 1;
    if(str)
      { write("You allow "+str+" to pass through the force field.");
WORKPRIVACY_D->SetAllowed(location, str, 1);
      }
    else
       write("Currently allowed:\n\n" + implode(WORKPRIVACY_D->GetAllows(location), "\n") );
    return 1;
  }

int eventDisallow(string str)
  { if(!validate_forcefield()) return 1;
    if(str)
      { write(str+" is no longer allowed to pass through the force field.");
WORKPRIVACY_D->SetAllowed(location, str, 0);
      }
    else
       write("Currently allowed:\n\n" + implode(WORKPRIVACY_D->GetAllows(location), "\n") );
    return 1;
  }

int eventBan(string str)
  { if(!validate_forcefield()) return 1;
    if(str)
      { write("You completely ban "+str+" from this area.");
WORKPRIVACY_D->SetBanned(location, str, 1);
      }
    else
       write("Currently banned:\n\n" + implode(WORKPRIVACY_D->GetBans(location), "\n") );
    return 1;
  }

int eventUnban(string str)
  { if(!validate_forcefield()) return 1;
    if(str)
      { write(str+" is no longer banned from this area.");
WORKPRIVACY_D->SetBanned(location, str, 0);
      }
    else
       write("Currently banned:\n\n" + implode(WORKPRIVACY_D->GetBans(location), "\n") );
    return 1;
  }

int eventForcefield(string str)
  { object *npcs;
    if(!validate_forcefield()) return 1;
    switch(str)
      {
     case "1":
     case "on":
     case "enabled":
WORKPRIVACY_D->SetForcefield(location, 1);
write("You enable the forcefield.");
npcs = filter(deep_inventory(this_object()),
      (: living($1) && !interactive($1) :) );
if(sizeof(npcs))
  { foreach(object npc in npcs)
      { tell_room(this_object(),"Ejecting "+identify(npc),npc);
npc->eventMove(ROOM_FURNACE);
      }
  }
break;
     case "0":
     case "off":
     case "disabled":
WORKPRIVACY_D->SetForcefield(location, 0);
write("You disable the forcefield.");
break;
     default:
write("Use one of 1, \"on\" or \"enabled\" to enable the forcefield,\n"
      "Use one of 0, \"off\" or \"disabled\" to disable it.\n");
      }
    return 1;
  }

int CanReceive(object sneak) {
    int ret;
    object *living_stack;
    if(WORKPRIVACY_D->IsBanned(location, sneak->GetName())) return 0;
    if(!(WORKPRIVACY_D->check_forcefield())) return ::CanReceive(sneak);
    living_stack = get_livings(sneak);
    if(!living_stack || !arrayp(living_stack)) living_stack = ({ sneak });
    foreach(object ob in living_stack){
      if(!archp(ob) && ob->GetKeyName() != lower_case(owner)
&& !(WORKPRIVACY_D->IsAllowed(location, sneak->GetName()))){
    message("info","You bounce off the privacy field.", ob);
            message("info",ob->GetName()+" bounced off the privacy field.",
              this_object());
    if(!environment(ob)) ob->eventMoveLiving(ROOM_START);
    return 0;
}
    }
    ret = ::CanReceive(sneak);
    if(ret){
        tell_room(this_object(), "\n\nPRIVACY WARNING: "+identify(sneak)+
                " has entered the room.\n\n", sneak);
    }
    return ret;
}

static void create() {
    room::create();
    //i take both from the path, change it as ya wish.
    if(!sscanf(base_name(this_object()), "/realms/%s/", location)) location = 0;
    owner = location;
    //any other are-global stuff you want, e.g.
    // SetNoModify(0);
}

void init() {
    ::init();
    SetProperty("no peer", check_forcefield());
    add_action("eventForcefield","privacy");
    add_action("eventForcefield","priv");
    add_action("eventAllow","allow");
    add_action("eventDisallow","disallow");
    add_action("eventBan","ban");
    add_action("eventUnban","unban");
}


Code: (example_room.c) [Select]
#include <lib.h>

inherit "/lib/std/workplace_room";

string LongDesc()
  { string ret = "Long description here.";
    if(check_forcefield()) ret += "\nThe privacy forcefield is enabled.";
    return ret;
  }

static void create()
  { ::create();
    SetClimate("indoors");
    SetAmbientLight(40);
    SetShort("ShortDesc here");
    SetLong((: LongDesc :));
    //SetItems();
    //SetInventory();
    //SetExits();
    //etc...
  }

void init()
  { ::init();
  }


Not tested much. YMMV.

PS.
Well, the WORKPRIVACY_D define could be moved to <daemon.h>...
And <lib.h> might make use of #define LIB_WORKPLACE_ROOM ...
I think, therefore i may be wrong.
Please note that if you met a Raudhrskal in a place that's not related to muds, it wasn't me. *sigh*... back when I started there was zero hits on google for that name...