Author Topic: Teleport.c  (Read 4261 times)

Offline amylase

  • Friend
  • **
  • Posts: 75
    • View Profile
    • gpLand
Teleport.c
« on: December 05, 2006, 08:57:09 pm »
Hi guys, this is my second command created partly by myself, partly by pulling goto.c apart, and also partly by annoying the shit out of people on ds channel with questions. Keep a long story short, teleport contains all the features of goto and in addition also lists locations you may feel like going to. All you need to do is teleport N where N is the number corresponding to a particular location.

At a player level your input can be
   Object eg. Teleport John where John is a player.
   Filename eg. Teleport /domains/campus/room/start.c.
   Index eg. Teleport 50
   Nil parameter ie. just typing teleport, will show the list of locations.

At a creator/maintainer level
   Feel free to add additional paths to domains array. Currently only
   /domains/campus/room/ and /domains/town/room/ are included.
   Simply add in a few more and they will be indexed automatically.

Pending Issues
    1) Quite slow running it for the first time loading all hundreds of rooms.
    2) Future version allows player to add own teleport destinations.
    3) Oh yea, some of the remarks probably sound a bit lame to some of you. They are more for myself than anything else. Feel free to erase all remarks and do anything you please with the code.

Code: [Select]
#include <lib.h>
inherit LIB_DAEMON;

mixed cmd(string str) {
    object ob;     /* object to teleport to, if requested */
    string *fils;  /* list of filenames in directory (path not included)*/
    string foo;   /* short description of room */
    int i=1;  /* teleport sites index */
    int dn; /* total number of domains */
    string intstr; /* path corresponding to numeric index */  
    string *domains; /* directories */
    string *sites;  /* filenames including full path for rooms */
        
   /* Domains is the array that contains list of directories to look for rooms in. Feel free to add in more. */
    domains  = ({ "" });  /* fill in a few blanks to startup */
    domains += ({ "/domains/campus/room/" });
    domains += ({ "/domains/town/room/" });

    sites  = ({ "" });  /* fill in a few blanks to startup*/
    sites  += ({ "" });

    dn = sizeof(domains); /* Domain refers to directory containing rooms. Site refers to specific rooms */

while(dn-- >0){
    fils = get_dir(domains[dn]+"*.c");

    /* Core of teleport.c. Grabs file by file from directory. Feeds filename including path and corresponding index into sites array for later retrieval if required */
    foreach(string fil in fils){
    foo = load_object(domains[dn] + fil)->GetShort();
    sites  += ({ domains[dn] +fil});
    if(!str) message("system", "[" + i++ +"] %^YELLOW%^" + foo + "%^RESET%^ (" + sites[i] +")", this_player());    
} /*endforeach*/
} /*endwhile*/

    /* Teleport X where X can be an index (integer), exact path (string) of room, or an object. If X were integer, intstr is the string of filename (including path) X refers to */
    intstr = sites[to_int(str)+1];
    
    if(!str) return "Teleport where?";  /* no parameter supplied */

    /* Goto object if that happens to be the parameter to go to */
    if((ob = find_living(lower_case(str))) &&
      (!ob->GetInvis() || !archp(ob)) && ob=environment(ob)) {
if(ob == environment(this_player())) {
   message("my_action", "You twitch.", this_player());
   if(hiddenp(this_player())) return 1;
   message("other_action", (string)this_player()->GetName()+
     " twitches.", ob, ({ this_player() }));
   return 1;
}
    }
    if(ob && ob->GetInvis() && creatorp(ob) && !archp(this_player())) ob = 0;
    if(!ob) str = absolute_path((string)this_player()->query_cwd(), str);
    if(ob) {
this_player()->eventMoveLiving(ob, "");
return 1;
    }

    if(file_exists(intstr)) str = intstr;
    if(!file_exists(str)) str += ".c";
    if(!file_exists(str)) {
write("Location not found."); /* Parameter given but no match */
return 1;
    }
    else this_player()->eventMoveLiving(str, ""); /* Actual moving of you to destination. Three ifs above sorts out index and file extension issues */
    return 1;
}

void help() {
    message("help",
      "Syntax: <Teleport [living thing|file|number]>\n\n"
      "This command will move you to where the living thing is if it can "
      "be found, otherwise it will search for the file named or file that corresponds to the index number and try to "
      "move you into that placele.\n\nSee also: home, move, trans, expel.",
      this_player()
    );
}

« Last Edit: May 12, 2007, 07:19:38 am by amylase »

Offline cratylus

  • Your favorite and best
  • Administrator
  • ***
  • Posts: 1024
  • Cratylus@Dead Souls <ds> np
    • View Profile
    • About Cratylus
Teleport.c
« Reply #1 on: December 06, 2006, 05:48:26 am »

:)



Thanks for the contribution, Amylase!



-Crat



Offline detah

  • BFF
  • ***
  • Posts: 190
  • Ruler of 2D
    • View Profile
Re: Teleport.c
« Reply #2 on: May 10, 2007, 09:10:48 am »
A, could you please post this again using the code tags.
When I copy paste this, it has no hard returns, so its all in one line.

Offline cratylus

  • Your favorite and best
  • Administrator
  • ***
  • Posts: 1024
  • Cratylus@Dead Souls <ds> np
    • View Profile
    • About Cratylus
Re: Teleport.c
« Reply #3 on: May 10, 2007, 09:15:16 am »
perform a liveupgrade.

Type: help liveupgrade

-Crat

Offline daelaskai

  • BFF
  • ***
  • Posts: 174
    • View Profile
Re: Teleport.c
« Reply #4 on: May 10, 2007, 01:23:29 pm »
I like what you've done here; however, I have a suggestion for you.  In your code, it looks like you have the option of adding individual files to the list rather than every room in a specific directory, however, I do not see where your code actually takes that input and uses it.  I would suggest adding this above where you process the domain files:


Code: [Select]
    foreach(string fil in sites)
    {
        foo = load_object(fil)->GetShort();
        sites  += ({ fil });
        if(!str) message("system", "[" + i++ +"] %^YELLOW%^" + foo + "%^RESET%^", this_player());
    }

I also changed one line that was causing me some trouble (I mentioned this on another thread).  I changed:

Code: [Select]
if(!str) message("system", "[" + i++ +"] %^YELLOW%^" + foo + "%^RESET%^ (" + sites +")", this_player());

to this:

Code: [Select]
if(!str) message("system", "[" + i++ +"] %^YELLOW%^" + foo + "%^RESET%^", this_player());

The complete code would look like this:

Code: [Select]
#include <lib.h>
inherit LIB_DAEMON;

mixed cmd(string str) {
    object ob;     /* object to teleport to, if requested */
    string *fils;  /* list of filenames in directory (path not included)*/
    string foo;   /* short description of room */
    int i=1;  /* teleport sites index */
    int dn; /* total number of domains */
    string intstr; /* path corresponding to numeric index */   
    string *domains; /* directories */
    string *sites;  /* filenames including full path for rooms */
       
   /* Domains is the array that contains list of directories to look for rooms in. Feel free to add in more. */
    domains  = ({ "" });  /* fill in a few blanks to startup */
    domains += ({ "/domains/campus/room/" });
    domains += ({ "/domains/town/room/" });

    sites  = ({ "" });  /* fill in a few blanks to startup*/
    sites  += ({ "" });

    foreach(string fil in sites)
    {
        foo = load_object(fil)->GetShort();
        sites  += ({ fil });
        if(!str) message("system", "[" + i++ +"] %^YELLOW%^" + foo + "%^RESET%^", this_player());
    }

    dn = sizeof(domains); /* Domain refers to directory containing rooms. Site refers to specific rooms */

while(dn-- >0){
    fils = get_dir(domains[dn]+"*.c");

    /* Core of teleport.c. Grabs file by file from directory. Feeds filename including path and corresponding index into sites array for later retrieval if required */
    foreach(string fil in fils){
    foo = load_object(domains[dn] + fil)->GetShort();
    sites  += ({ domains[dn] +fil});
    if(!str) message("system", "[" + i++ +"] %^YELLOW%^" + foo + "%^RESET%^", this_player());   
   } /*endforeach*/
} /*endwhile*/

    /* Teleport X where X can be an index (integer), exact path (string) of room, or an object. If X were integer, intstr is the string of filename (including path) X refers to */
    intstr = sites[to_int(str)+1];
   
    if(!str) return "Teleport where?";  /* no parameter supplied */

    /* Goto object if that happens to be the parameter to go to */
    if((ob = find_living(lower_case(str))) &&
      (!ob->GetInvis() || !archp(ob)) && ob=environment(ob)) {
   if(ob == environment(this_player())) {
       message("my_action", "You twitch.", this_player());
       if(hiddenp(this_player())) return 1;
       message("other_action", (string)this_player()->GetName()+
         " twitches.", ob, ({ this_player() }));
       return 1;
   }
    }
    if(ob && ob->GetInvis() && creatorp(ob) && !archp(this_player())) ob = 0;
    if(!ob) str = absolute_path((string)this_player()->query_cwd(), str);
    if(ob) {
   this_player()->eventMoveLiving(ob, "");
   return 1;
    }

    if(file_exists(intstr)) str = intstr;
    if(!file_exists(str)) str += ".c";
    if(!file_exists(str)) {
   write("Location not found."); /* Parameter given but no match */
   return 1;
    }
    else this_player()->eventMoveLiving(str, ""); /* Actual moving of you to destination. Three ifs above sorts out index and file extension issues */
    return 1;
}

void help() {
    message("help",
      "Syntax: <Teleport [living thing|file|number]>\n\n"
      "This command will move you to where the living thing is if it can "
      "be found, otherwise it will search for the file named or file that corresponds to the index number and try to "
      "move you into that placele.\n\nSee also: home, move, trans, expel.",
      this_player()
    );
}

Daelas

Offline cratylus

  • Your favorite and best
  • Administrator
  • ***
  • Posts: 1024
  • Cratylus@Dead Souls <ds> np
    • View Profile
    • About Cratylus
Re: Teleport.c
« Reply #5 on: May 10, 2007, 05:47:09 pm »
I dunno why I posted that thing about liveupgrade.

It made sense at the time, but I just can't remember now what it was.

Anyway, it's a good idea in general.

-Crat

Offline amylase

  • Friend
  • **
  • Posts: 75
    • View Profile
    • gpLand
Re: Teleport.c
« Reply #6 on: May 11, 2007, 02:45:12 am »
Thanks for the comments guys. Thanks for improving on the code Daelas.  However when I attempted to execute your version of teleport.c, this error message is returned within the game:
Quote
2007.05.12-18.25
*call_other() couldn't find object
Object: /secure/sefun/sefun at /secure/sefun/load_object.c:14

'<function>' at /secure/save/creators/a/amylase (<function>) at /:0
'cmdAll' at /secure/save/creators/a/amylase (/lib/command.c) at line 160
'cmd' at /secure/cmds/creators/teleport at line 25
'load_object' at /secure/sefun/sefun at /secure/sefun/load_object.c:14
'CATCH' at /secure/sefun/sefun at /secure/sefun/load_object.c:14
Trace written to /log/catch
---
2007.05.12-18.25
*Bad argument 1 to call_other()
Expected: string or array or object Got: 0.
Object: /secure/cmds/creators/teleport at line 25

'<function>' at /secure/save/creators/a/amylase (<function>) at /:0
'cmdAll' at /secure/save/creators/a/amylase (/lib/command.c) at line 160
'cmd' at /secure/cmds/creators/teleport at line 25
Trace written to /log/runtime

What platform are you running DS2 on Daelas? I run mine on Windows XP. Could that make a difference?

Detah, have a go with both my old code as well as Daelas' new code, see if you have any problem with either. The following is my old code with code tags for correct carriag returns.

Code: [Select]
/*
Hi guys, this is my second command created partly by myself, partly by pulling goto.c apart, and also partly by annoying the shit out of people on ds channel with questions. Keep a long story short, teleport contains all the features of goto and in addition also lists locations you may feel like going to. All you need to do is teleport N where N is the number corresponding to a particular location.

At a player level your input can be
   Object eg. Teleport John where John is a player.
   Filename eg. Teleport /domains/campus/room/start.c.
   Index eg. Teleport 50
   Nil parameter ie. just typing teleport, will show the list of locations.

At a creator/maintainer level
   Feel free to add additional paths to domains array. Currently only
   /domains/campus/room/ and /domains/town/room/ are included.
   Simply add in a few more and they will be indexed automatically.

Pending Issues
    1) Quite slow running it for the first time loading all hundreds of rooms.
    2) Future version allows player to add own teleport destinations.
    3) Oh yea, some of the remarks probably sound a bit lame to some of you. They are more for myself than anything else. Feel free to erase all remarks and do anything you please with the code.
*/

#include <lib.h>
inherit LIB_DAEMON;

mixed cmd(string str) {
    object ob;     /* object to teleport to, if requested */
    string *fils;  /* list of filenames in directory (path not included)*/
    string foo;   /* short description of room */
    int i=1;  /* teleport sites index */
    int dn; /* total number of domains */
    string intstr; /* path corresponding to numeric index */   
    string *domains; /* directories */
    string *sites;  /* filenames including full path for rooms */
       
   /* Domains is the array that contains list of directories to look for rooms in. Feel free to add in more. */
    domains  = ({ "" });  /* fill in a few blanks to startup */
    domains += ({ "/domains/campus/room/" });
    domains += ({ "/domains/town/room/" });

    sites  = ({ "" });  /* fill in a few blanks to startup*/
    sites  += ({ "" });

    dn = sizeof(domains); /* Domain refers to directory containing rooms. Site refers to specific rooms */

while(dn-- >0){
    fils = get_dir(domains[dn]+"*.c");

    /* Core of teleport.c. Grabs file by file from directory. Feeds filename including path and corresponding index into sites array for later retrieval if required */
    foreach(string fil in fils){
    foo = load_object(domains[dn] + fil)->GetShort();
    sites  += ({ domains[dn] +fil});
    if(!str) message("system", "[" + i++ +"] %^YELLOW%^" + foo + "%^RESET%^ (" + sites[i] +")", this_player());   
} /*endforeach*/
} /*endwhile*/

    /* Teleport X where X can be an index (integer), exact path (string) of room, or an object. If X were integer, intstr is the string of filename (including path) X refers to */
    intstr = sites[to_int(str)+1];
   
    if(!str) return "Teleport where?";  /* no parameter supplied */

    /* Goto object if that happens to be the parameter to go to */
    if((ob = find_living(lower_case(str))) &&
      (!ob->GetInvis() || !archp(ob)) && ob=environment(ob)) {
if(ob == environment(this_player())) {
    message("my_action", "You twitch.", this_player());
    if(hiddenp(this_player())) return 1;
    message("other_action", (string)this_player()->GetName()+
      " twitches.", ob, ({ this_player() }));
    return 1;
}
    }
    if(ob && ob->GetInvis() && creatorp(ob) && !archp(this_player())) ob = 0;
    if(!ob) str = absolute_path((string)this_player()->query_cwd(), str);
    if(ob) {
this_player()->eventMoveLiving(ob, "");
return 1;
    }

    if(file_exists(intstr)) str = intstr;
    if(!file_exists(str)) str += ".c";
    if(!file_exists(str)) {
write("Location not found."); /* Parameter given but no match */
return 1;
    }
    else this_player()->eventMoveLiving(str, ""); /* Actual moving of you to destination. Three ifs above sorts out index and file extension issues */
    return 1;
}

void help() {
    message("help",
      "Syntax: <Teleport [living thing|file|number]>\n\n"
      "This command will move you to where the living thing is if it can "
      "be found, otherwise it will search for the file named or file that corresponds to the index number and try to "
      "move you into that placele.\n\nSee also: home, move, trans, expel.",
      this_player()
    );
}
« Last Edit: May 12, 2007, 07:20:35 am by amylase »

Offline daelaskai

  • BFF
  • ***
  • Posts: 174
    • View Profile
Re: Teleport.c
« Reply #7 on: May 12, 2007, 08:04:09 am »
You are correct.  I missed a little section.  The corrected code that works on DS-2.4.2 on Windows XP

Code: [Select]
#include <lib.h>
inherit LIB_DAEMON;

mixed cmd(string str) {
    object ob;     /* object to teleport to, if requested */
    string *fils;  /* list of filenames in directory (path not included)*/
    string foo;   /* short description of room */
    int i=1;  /* teleport sites index */
    int dn; /* total number of domains */
    string intstr; /* path corresponding to numeric index */   
    string *domains; /* directories */
    string *sites;  /* filenames including full path for rooms */
       
   /* Domains is the array that contains list of directories to look for rooms in. Feel free to add in more. */
    domains  = ({ "" });  /* fill in a few blanks to startup */
    domains += ({ "/domains/campus/room/" });
    domains += ({ "/domains/town/room/" });

    sites  = ({ });  /* fill in a few blanks to startup*/
    sites  += ({ "/domains/default/room/start" });

    foreach(string fil in sites)
    {
        foo = load_object(fil)->GetShort();
        sites  += ({ fil });
        if(!str) message("system", "[" + i++ +"] %^YELLOW%^" + foo + "%^RESET%^", this_player());
    }

    dn = sizeof(domains); /* Domain refers to directory containing rooms. Site refers to specific rooms */

while(dn-- >0){
    fils = get_dir(domains[dn]+"*.c");

    /* Core of teleport.c. Grabs file by file from directory. Feeds filename including path and corresponding index into sites array for later retrieval if required */
    foreach(string fil in fils){
    foo = load_object(domains[dn] + fil)->GetShort();
    sites  += ({ domains[dn] +fil});
    if(!str) message("system", "[" + i++ +"] %^YELLOW%^" + foo + "%^RESET%^", this_player());   
   } /*endforeach*/
} /*endwhile*/

    /* Teleport X where X can be an index (integer), exact path (string) of room, or an object. If X were integer, intstr is the string of filename (including path) X refers to */
    intstr = sites[to_int(str)+1];
   
    if(!str) return "Teleport where?";  /* no parameter supplied */

    /* Goto object if that happens to be the parameter to go to */
    if((ob = find_living(lower_case(str))) &&
      (!ob->GetInvis() || !archp(ob)) && ob=environment(ob)) {
   if(ob == environment(this_player())) {
       message("my_action", "You twitch.", this_player());
       if(hiddenp(this_player())) return 1;
       message("other_action", (string)this_player()->GetName()+
         " twitches.", ob, ({ this_player() }));
       return 1;
   }
    }
    if(ob && ob->GetInvis() && creatorp(ob) && !archp(this_player())) ob = 0;
    if(!ob) str = absolute_path((string)this_player()->query_cwd(), str);
    if(ob) {
   this_player()->eventMoveLiving(ob, "");
   return 1;
    }

    if(file_exists(intstr)) str = intstr;
    if(!file_exists(str)) str += ".c";
    if(!file_exists(str)) {
   write("Location not found."); /* Parameter given but no match */
   return 1;
    }
    else this_player()->eventMoveLiving(str, ""); /* Actual moving of you to destination. Three ifs above sorts out index and file extension issues */
    return 1;
}

void help() {
    message("help",
      "Syntax: <Teleport [living thing|file|number]>\n\n"
      "This command will move you to where the living thing is if it can "
      "be found, otherwise it will search for the file named or file that corresponds to the index number and try to "
      "move you into that placele.\n\nSee also: home, move, trans, expel.",
      this_player()
    );
}

The problem was here:

Code: [Select]
    sites  = ({ "" });  /* fill in a few blanks to startup*/
    sites  += ({ "" });

The ({ "" }) was causing the code to give an error since it was looking for ""->GetShort() which makes no sense.  I took these out and the code works fine.  It also works when adding individual rooms  such as /domains/default/room/start as you can see above.

Hope it helps.

Daelas