home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / games / tinymud2.zip / SET.C < prev    next >
C/C++ Source or Header  |  1990-09-02  |  17KB  |  638 lines

  1. #include "copyright.h"
  2.  
  3. /* commands which set parameters */
  4. #include <stdio.h>
  5. #include <ctype.h>
  6.  
  7. #include "db.h"
  8. #include "config.h"
  9. #include "match.h"
  10. #include "interface.h"
  11. #include "externs.h"
  12.  
  13. #ifdef COMPRESS
  14. #define alloc_compressed(x) alloc_string(compress(x))
  15. #else /* COMPRESS */
  16. #define alloc_compressed(x) alloc_string(x)
  17. #endif /* COMPRESS */
  18.  
  19. #define FREE_STRING(X) X = ((X) ? (free ((void *) (X)), NULL) : NULL)
  20.  
  21. static dbref match_controlled(dbref player, const char *name)
  22. {
  23.     dbref match;
  24.  
  25.     init_match(player, name, NOTYPE);
  26.     match_everything();
  27.  
  28.     match = noisy_match_result();
  29.     if(match != NOTHING && !controls(player, match)) {
  30.     notify(player, "Permission denied.");
  31.     return NOTHING;
  32.     } else {
  33.     return match;
  34.     }
  35. }
  36.  
  37. void do_name(dbref player, const char *name, char *newname)
  38. {
  39.     dbref thing;
  40.     char *password;
  41.  
  42.     if((thing = match_controlled(player, name)) != NOTHING) {
  43.     /* check for bad name */
  44.     if(*newname == '\0') {
  45.         notify(player, "Give it what new name?");
  46.         return;
  47.     }
  48.  
  49.     /* check for renaming a player */
  50.     if(Typeof(thing) == TYPE_PLAYER) {
  51.         /* split off password */
  52.         for(password = newname;
  53.         *password && !isspace(*password);
  54.         password++);
  55.         /* eat whitespace */
  56.         if(*password) {
  57.         *password++ = '\0'; /* terminate name */
  58.         while(*password && isspace(*password)) password++;
  59.         }
  60.  
  61.         /* check for null password */
  62.         if(!*password) {
  63.         notify(player,
  64.                "You must specify a password to change a player name.");
  65.         notify(player, "E.g.: name player = newname password");
  66.         return;
  67.         } else if(strcmp (db[thing].name, "guest") == 0) {
  68.         notify(player,
  69.                "You are only a guest here...no name changing.");
  70.         return;
  71.         } else if(!db[thing].password) {
  72.         /* If original has no password, set one */
  73.         db[thing].password = alloc_string(password);
  74.         } else if(strcmp(password, db[thing].password)) {
  75.         notify(player, "Incorrect password.");
  76.         return;
  77.         } else if(string_compare(newname, db[thing].name) &&
  78.               !ok_player_name(newname)) {
  79.         notify(player, "You can't give a player that name.");
  80.         return;
  81.         }
  82.         /* everything ok, notify */
  83.         writelog("NAME CHANGE: %s(#%d) to %s\n",
  84.             db[thing].name, thing, newname);
  85. #ifdef PLAYER_LIST
  86.         delete_player(thing);
  87.         free((void *) db[thing].name);
  88.         db[thing].name = alloc_string(newname);
  89.         add_player(thing);
  90.         notify(player, "Name set.");
  91.         return;
  92. #endif PLAYER_LIST        
  93.     } else {
  94.         if(!ok_name(newname)) {
  95.         notify(player, "That is not a reasonable name.");
  96.         return;
  97.         }
  98.     }
  99.  
  100.     /* everything ok, change the name */
  101.     if(db[thing].name) {
  102.         free((void *) db[thing].name);
  103.     }
  104.     db[thing].name = alloc_string(newname);
  105.     notify(player, "Name set.");
  106.     }
  107. }
  108.  
  109. void do_describe(dbref player, const char *name, const char *description)
  110. {
  111.     dbref thing;
  112.  
  113.     if((thing = match_controlled(player, name)) != NOTHING) {
  114.     if(db[thing].description) {
  115.         free((void *) db[thing].description);
  116.     }
  117.     db[thing].description = alloc_compressed(description);
  118.     notify(player, "Description set.");
  119.     }
  120. }
  121.  
  122. void do_fail(dbref player, const char *name, const char *message)
  123. {
  124.     dbref thing;
  125.  
  126.     if((thing = match_controlled(player, name)) != NOTHING) {
  127.     if(db[thing].fail_message) {
  128.         free((void *) db[thing].fail_message);
  129.     }
  130.     db[thing].fail_message = alloc_compressed(message);
  131.     notify(player, "Message set.");
  132.     }
  133. }
  134.  
  135. void do_success(dbref player, const char *name, const char *message)
  136. {
  137.     dbref thing;
  138.  
  139.     if((thing = match_controlled(player, name)) != NOTHING) {
  140.     if(db[thing].succ_message) {
  141.         free((void *) db[thing].succ_message);
  142.     }
  143.     db[thing].succ_message = alloc_compressed(message);
  144.     notify(player, "Message set.");
  145.     }
  146. }
  147.  
  148. void do_osuccess(dbref player, const char *name, const char *message)
  149. {
  150.     dbref thing;
  151.  
  152.     if((thing = match_controlled(player, name)) != NOTHING) {
  153.     if(db[thing].osuccess) {
  154.         free((void *) db[thing].osuccess);
  155.     }
  156.     db[thing].osuccess = alloc_compressed(message);
  157.     notify(player, "Message set.");
  158.     }
  159. }
  160.  
  161. void do_ofail(dbref player, const char *name, const char *message)
  162. {
  163.     dbref thing;
  164.  
  165.     if((thing = match_controlled(player, name)) != NOTHING) {
  166.     if(db[thing].ofail) {
  167.         free((void *) db[thing].ofail);
  168.     }
  169.     db[thing].ofail = alloc_compressed(message);
  170.     notify(player, "Message set.");
  171.     }
  172. }
  173.  
  174. void do_lock(dbref player, const char *name, const char *keyname)
  175. {
  176.     dbref thing;
  177.     struct boolexp *key;
  178.  
  179.     init_match(player, name, NOTYPE);
  180.     match_everything();
  181.  
  182.     switch(thing = match_result()) {
  183.       case NOTHING:
  184.     notify(player, "I don't see what you want to lock!");
  185.     return;
  186.       case AMBIGUOUS:
  187.     notify(player, "I don't know which one you want to lock!");
  188.     return;
  189.       default:
  190.     if(!controls(player, thing)) {
  191.         notify(player, "You can't lock that!");
  192.         return;
  193.     }
  194.     break;
  195.     }
  196.  
  197.     key = parse_boolexp(player, keyname);
  198.     if(key == TRUE_BOOLEXP) {
  199.     notify(player, "I don't understand that key.");
  200.     } else {
  201.     /* everything ok, do it */
  202.     free_boolexp(db[thing].key);
  203.     db[thing].key = key;
  204.     notify(player, "Locked.");
  205.     }
  206. }
  207.  
  208. void do_unlock(dbref player, const char *name)
  209. {
  210.     dbref thing;
  211.  
  212.     if((thing = match_controlled(player, name)) != NOTHING) {
  213.     free_boolexp(db[thing].key);
  214.     db[thing].key = TRUE_BOOLEXP;
  215.     notify(player, "Unlocked.");
  216.     }
  217. }
  218.  
  219. void do_unlink(dbref player, const char *name)
  220. {
  221.     dbref exit;
  222.  
  223.     init_match(player, name, TYPE_EXIT);
  224.     match_exit();
  225.     match_here();
  226.     if(Wizard(player)) {
  227.     match_absolute();
  228.     }
  229.  
  230.     switch(exit = match_result()) {
  231.       case NOTHING:
  232.     notify(player, "Unlink what?");
  233.     break;
  234.       case AMBIGUOUS:
  235.     notify(player, "I don't know which one you mean!");
  236.     break;
  237.       default:
  238.     if(!controls(player, exit)) {
  239.         notify(player, "Permission denied.");
  240.     } else {
  241.         switch(Typeof(exit)) {
  242.           case TYPE_EXIT:
  243.         db[exit].location = NOTHING;
  244.         notify(player, "Unlinked.");
  245.         break;
  246.           case TYPE_ROOM:
  247.         db[exit].location = NOTHING;
  248.         notify(player, "Dropto removed.");
  249.         break;
  250.           default:
  251.         notify(player, "You can't unlink that!");
  252.         break;
  253.         }
  254.     }
  255.     }
  256. }
  257.  
  258. void do_chown(dbref player, const char *name, const char *newobj)
  259. {
  260.     dbref thing;
  261.     dbref owner;
  262.     
  263.     init_match(player, name, NOTYPE);
  264.     match_everything();
  265.  
  266.     if((thing = noisy_match_result()) == NOTHING) {
  267.     return;
  268.     } else if((owner = lookup_player(newobj)) == NOTHING &&
  269.           string_compare(newobj,"me")) {
  270.     notify(player, "I couldn't find that player.");
  271.     } else if(Typeof(thing) == TYPE_PLAYER) {
  272.     notify(player, "Players always own themselves.");
  273.     } else if (!Wizard(player) &&
  274.            ((string_compare(newobj,"me") &&
  275.          owner != player) || !(db[thing].flags & UNWANTED))) {
  276.     notify(player, "Permission denied.");
  277.     } else if (!Wizard(player) && !could_doit(player,thing)) {
  278.     notify(player, "That item is locked.");
  279.     } else {
  280.     if (!string_compare(newobj,"me"))
  281.         owner = player;
  282.     db[thing].owner = owner;
  283.     notify(player, "Owner changed.");
  284.     }
  285. }
  286.  
  287. void do_set(dbref player, const char *name, const char *flag)
  288. {
  289.     dbref thing;
  290.     const char *p;
  291.     object_flag_type f;
  292.  
  293.     /* find thing */
  294.     if((thing = match_controlled(player, name)) == NOTHING) return;
  295.  
  296.     /* move p past NOT_TOKEN if present */
  297.     for(p = flag; *p && (*p == NOT_TOKEN || isspace(*p)); p++);
  298.  
  299.     /* identify flag */
  300.     if(*p == '\0') {
  301.     notify(player, "You must specify a flag to set.");
  302.     return;
  303.     } else if(string_prefix("LINK_OK", p)) {
  304.     f = LINK_OK;
  305.     } else if(string_prefix("DARK", p)) {
  306.     f = DARK;
  307.     } else if(string_prefix("STICKY", p)) {
  308.     f = STICKY;
  309.     } else if(string_prefix("WIZARD", p) ||
  310.               string_prefix("TINKER", p)) {
  311.     f = WIZARD;
  312.     } else if(string_prefix("TEMPLE", p) ||
  313.               string_prefix("JUNKPILE", p)) {
  314.     f = TEMPLE;
  315.     } else if(string_prefix("HAVEN", p)) {
  316.     f = HAVEN;
  317.     } else if(string_prefix("ABODE", p)) {
  318.     f = ABODE;
  319.     } else if(string_prefix("UNWANTED", p)) {
  320.     f = UNWANTED;
  321. #ifdef ROBOT_MODE
  322.     } else if(string_prefix("ROBOT", p) ||
  323.           string_prefix("BOT", p)) {
  324.     if (Typeof(thing) == TYPE_PLAYER) {
  325.         if (Wizard(player)) {
  326.         writelog ("ROBOT: success %s(%d) %s %s(%d)\n",
  327.               db[player].name, player,
  328.               *flag == NOT_TOKEN ? "reset" : "set",
  329.               db[thing].name, thing);
  330.         } else {
  331.         writelog ("ROBOT: failed %s(%d) %s %s(%d)\n",
  332.               db[player].name, player,
  333.               *flag == NOT_TOKEN ? "reset" : "set",
  334.               db[thing].name, thing);
  335.         notify(player,
  336. #ifndef TINKER
  337.                "Only a Wizard can designate a player robotic.");
  338. #else TINKER
  339.                "Only a Tinker can designate a player robotic.");
  340. #endif TINKER
  341.         return;
  342.         }
  343.     }
  344.     f = ROBOT;
  345. #endif ROBOT_MODE
  346.     } else if(string_prefix("TABULAR_WHO", p)) {
  347.     f = TABULAR_WHO;
  348.     } else if(string_prefix("REVERSED_WHO", p)) {
  349.     f = REVERSED_WHO;
  350. #ifdef GENDER
  351.     } else if(string_prefix("MALE", p) || string_prefix("FEMALE", p) ||
  352.               string_prefix("NEUTER", p)   || string_prefix("UNASSIGNED", p)) {
  353.         if (Typeof(thing) != TYPE_PLAYER) {
  354.         notify(player, "Sorry, only players have gender.");
  355.         return;
  356.         }
  357.         if (thing != player && !Wizard(player)) {
  358.         notify(player, "You can only give yourself a sex-change.");
  359.         return;
  360.         }
  361.         db[thing].flags &= ~GENDER_MASK;
  362.     if(string_prefix("UNASSIGNED", p) || *flag == NOT_TOKEN) {
  363.         db[thing].flags |= (GENDER_UNASSIGNED << GENDER_SHIFT);
  364.         notify(player, "Gender set to unassigned.");
  365.     } else if (string_prefix("MALE", p)) {
  366.         db[thing].flags |= (GENDER_MALE << GENDER_SHIFT);
  367.         notify(player, "Gender set to male.");
  368.         } else if(string_prefix("FEMALE", p)) {
  369.         db[thing].flags |= (GENDER_FEMALE << GENDER_SHIFT);
  370.         notify(player, "Gender set to female.");
  371.         } else if(string_prefix("NEUTER", p)) {
  372.         db[thing].flags |= (GENDER_NEUTER << GENDER_SHIFT);
  373.         notify(player, "Gender set to neuter.");
  374.     }
  375.         return;
  376. #endif /* GENDER */
  377. #ifdef RESTRICTED_BUILDING
  378.     } else if(string_prefix("BUILDER", p) ||) {
  379.               string_prefix("CONSTRUCTOR", p)
  380.     f = BUILDER;
  381. #endif                /* RESTRICTED_BUILDING */
  382.     } else {
  383.     notify(player, "I don't recognize that flag.");
  384.     return;
  385.     }
  386.  
  387.     /* check for restricted flag */
  388.     if(!Wizard(player)
  389.        && (f == TEMPLE
  390. #ifdef RESTRICTED_BUILDING
  391.        || f == BUILDER
  392. #endif /* RESTRICTED_BUILDING */
  393.        || f == DARK &&
  394.        (Typeof(thing) != TYPE_ROOM && Typeof(thing) != TYPE_THING))) {
  395.     notify(player, "Permission denied.");
  396.     return;
  397.     }
  398.  
  399. #ifdef GOD_PRIV
  400.     if (!God(player) && f == DARK && Typeof(thing) == TYPE_PLAYER) {
  401.     notify(player, "Permission denied.");
  402.     return;
  403.     }
  404. #endif GOD_PRIV
  405.  
  406.     if (!Wizard(player) && f == DARK && Typeof(thing) == TYPE_THING &&
  407.         db[thing].location != player) {
  408.     notify(player, "You must be holding an object to set it DARK.");
  409.     return;
  410.     }
  411.  
  412. #ifdef GOD_PRIV
  413.     if(!God(player) && (f == WIZARD)) {
  414. #else
  415.     if (!Wizard(player) && (f == WIZARD)) {
  416. #endif GOD_PRIV
  417.     writelog ("WIZARD: failed %s(%d) %s %s(%d)\n",
  418.           db[player].name, player,
  419.           *flag == NOT_TOKEN ? "reset" : "set",
  420.           db[thing].name, thing);
  421.  
  422.     notify(player, "Permission denied.");
  423.     return;
  424.     }
  425.  
  426.     if (f == WIZARD) {
  427.     writelog ("WIZARD: success %s(%d) %s %s(%d)\n",
  428.           db[player].name, player,
  429.           *flag == NOT_TOKEN ? "reset" : "set",
  430.           db[thing].name, thing);
  431.     }
  432.  
  433. #ifdef GOD_PRIV
  434.     /* check for unGODding */
  435.     if (f == WIZARD && God(thing)) {
  436.     notify(player, "Gods can not me made mortal.");
  437.     return;
  438.     }
  439. #else
  440.     if (f == WIZARD && thing == player) {
  441.     notify(player, "You cannot make yourself mortal!");
  442.     return;
  443.     }
  444. #endif GOD_PRIV
  445.  
  446.     if ( f==HAVEN && !(Typeof(thing) == TYPE_ROOM ||
  447.                Typeof(thing) == TYPE_PLAYER)) {
  448.     notify(player, "Only rooms or players can be HAVEN.");
  449.     return;
  450.     }
  451.  
  452.     if (f==UNWANTED && Typeof(thing)==TYPE_PLAYER) {
  453.     notify(player, "You should always want yourself.");
  454.     return;
  455.     }
  456.  
  457.     if ((f==TABULAR_WHO || f==REVERSED_WHO) &&
  458.     Typeof(thing) != TYPE_PLAYER) {
  459.     notify(player, "Player preference flags can only be set on players.");
  460.     return;
  461.     }
  462.     
  463.     /* else everything is ok, do the set */
  464.     if(*flag == NOT_TOKEN) {
  465.     /* reset the flag */
  466.     db[thing].flags &= ~f;
  467.     notify(player, "Flag reset.");
  468.     } else {
  469.     /* set the flag */
  470.     db[thing].flags |= f;
  471.     notify(player, "Flag set.");
  472.     }
  473. }
  474.  
  475. #ifdef RECYCLE
  476. /****************************************************************
  477.  * do_recycle: Allow any player to give an item away...the item
  478.  * is @chowned to the "Recycler" player, and if it's a thing,
  479.  * it is @linked to the home of the "Recycler" as well.
  480.  ****************************************************************/
  481.  
  482. void do_recycle(dbref player, const char *name)
  483. {
  484.     dbref thing, recip, i;
  485.     char buf[BUFFER_LEN];
  486.  
  487.     if (!name || !*name) {
  488.     notify(player, "You must specify an object to @recycle.");
  489.     return;
  490.     }
  491.  
  492.     if((thing = match_controlled(player, name)) == NOTHING) return;
  493.     
  494.     if(Typeof(thing) == TYPE_PLAYER) {
  495.     notify(player, "You can't @recycle players.");
  496.     return;
  497.     }
  498.  
  499.     if((recip = lookup_player(RECYCLER)) == NOTHING) {
  500.     notify(player, "There is no current Recycler.");
  501.     return;
  502.     }
  503.     
  504.     /* Okay - do it */
  505.     db[thing].owner = recip;
  506.  
  507.     /* If its a thing, link it to the Recyclers home */
  508.     if (Typeof(thing) == TYPE_THING) {
  509.     db[thing].exits = db[recip].exits;
  510.     moveto (thing, db[thing].exits);
  511.     }
  512.  
  513.     /* Clear its strings */
  514.     FREE_STRING (db[thing].name);
  515.     FREE_STRING (db[thing].description);
  516.     FREE_STRING (db[thing].fail_message);
  517.     FREE_STRING (db[thing].ofail);
  518.     FREE_STRING (db[thing].succ_message);
  519.     FREE_STRING (db[thing].osuccess);
  520.     sprintf (buf, "junk-%d", thing);
  521.     db[thing].name = alloc_string(buf);
  522.  
  523.     /* unlock it */
  524.     free_boolexp(db[thing].key);
  525.     db[thing].key = TRUE_BOOLEXP;
  526.  
  527.     /* Make it worthless */
  528.     db[thing].pennies = 1;
  529.  
  530.     /* Make it up for grabs */
  531.     db[thing].flags |= UNWANTED;
  532.  
  533.     /* Tell player we did it */    
  534.     notify(player, "Object recycled.");
  535. }
  536.  
  537. /****************************************************************
  538.  * do_count: count things...
  539.  *    players:    Number of objects owned, carried
  540.  *    rooms:        number of exits from & to, number contents
  541.  ****************************************************************/
  542.  
  543. void do_count(dbref player, const char *name)
  544. {
  545.     dbref thing, i, exit;
  546.     char buf[BUFFER_LEN];
  547.     int owned=0, contents=0, from=0, to=0, objects=0, rooms=0, exits=0;
  548.     int rowned=0, rplayers=0, robjects=0, rrooms=0, rexits=0;
  549.  
  550.     if (!name || !*name) {
  551.     notify(player, "You must specify an object to @count.");
  552.     return;
  553.     }
  554.  
  555.     if((thing = match_controlled(player, name)) == NOTHING) return;
  556.  
  557.     if(Typeof(thing) != TYPE_PLAYER && Typeof(thing) != TYPE_ROOM) {
  558.     notify(player, "You can only @count people or rooms.\n");
  559.     return;
  560.     }
  561.  
  562.     if(!payfor(player, FIND_COST)) {
  563.     notify(player, "You don't have enough pennies.");
  564.     return;
  565.     }
  566.     
  567.     switch (Typeof(thing)) {
  568.       case TYPE_PLAYER:
  569.     for (i=0; i<db_top; i++) {
  570.         if (db[i].location == thing) contents++;
  571.         if (db[i].owner == thing) {
  572.         switch (Typeof(i)) {
  573.           case TYPE_THING:    objects++; break;
  574.           case TYPE_EXIT:    exits++; break;
  575.           case TYPE_ROOM:    rooms++; break;
  576.         }
  577.         owned++;
  578.         }
  579.     }
  580.     sprintf(buf,
  581.          "%s owns %d objects (%d things, %d rooms, %d exits), %d carried.",
  582.         unparse_object(player, thing),
  583.             owned, objects, rooms, exits, contents);
  584.     notify(player, buf);
  585.     break;
  586.  
  587.       case TYPE_ROOM:
  588.     for (i=0; i<db_top; i++) {
  589.         if (db[i].location == thing) {
  590.         if (Typeof(i) == TYPE_EXIT) {
  591.             to++;
  592.         } else {
  593.             contents++;
  594.         }
  595.         }
  596.     }
  597.     DOLIST(exit, db[thing].exits) {
  598.         from++;
  599.     }
  600.     sprintf(buf, "%s has %d entrances, %d exits, %d objects.",
  601.         unparse_object(player, thing), to, from, contents);
  602.     notify(player, buf);
  603.     
  604.     if (!Wizard (player)) break;
  605.  
  606.     for (i=0; i<db_top; i++) {
  607.         if (db[db[i].owner].location == thing) {
  608.             switch (Typeof(i)) {
  609.           case TYPE_EXIT: rexits++; break;
  610.             case TYPE_ROOM: rrooms++; break;
  611.             case TYPE_THING: robjects++; break;
  612.           case TYPE_PLAYER: rplayers++; break;
  613.         }
  614.         rowned++;
  615.         }
  616.     }
  617.  
  618.     if (rplayers == 0) {
  619.         sprintf (buf, "There are no players in %s.\n",
  620.         unparse_object (player, thing));
  621.     } else {
  622.         sprintf(buf,
  623.             "The %d player%s in %s own%s %d %s: %d %s, %d %s, %d %s\n.",
  624.         rplayers, (rplayers == 1) ? "" : "s",
  625.         unparse_object(player, thing), (rplayers == 1) ? "s" : "",
  626.         rowned, "objects", robjects, "things", rrooms, "rooms",
  627.         rexits, "exits");
  628.     }
  629.  
  630.     notify(player, buf);
  631.  
  632.     break;
  633.     }
  634.  
  635. }
  636. #endif RECYCLE
  637.  
  638.