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

  1. #include "copyright.h"
  2.  
  3. /* Predicates for testing various conditions */
  4.  
  5. #include <ctype.h>
  6.  
  7. #include "db.h"
  8. #include "interface.h"
  9. #include "config.h"
  10. #include "externs.h"
  11.  
  12. void pronoun_substitute(char *result, dbref player, const char *str);
  13.  
  14. int can_link_to(dbref who, object_flag_type what, dbref where)
  15. {
  16.     return(where >= 0 &&
  17.        where < db_top &&
  18.        Typeof(where) == TYPE_ROOM &&
  19.        (controls(who, where) ||
  20.         (what == NOTYPE &&
  21.          (Flag(where,LINK_OK|ABODE))) ||
  22.         (what == TYPE_ROOM &&
  23.          (Flag(where,ABODE))) ||
  24.         (what == TYPE_EXIT &&
  25.          (Flag(where,LINK_OK))) ||
  26.         ((what == TYPE_PLAYER || what == TYPE_THING) &&
  27. #ifdef ROBOT_MODE
  28.          (what != TYPE_PLAYER || !Robot(who) || !Robot(where)) &&
  29. #endif ROBOT_MODE
  30.          Flag(where,ABODE))));
  31.     }
  32.  
  33. /*
  34.  * Check whether a player can perform an action...robotic players are
  35.  * now implicitly barred from performing actions on things with the
  36.  * robot flag set.    5/18/90 - Fuzzy
  37.  */
  38.  
  39. int could_doit(dbref player, dbref thing)
  40. {
  41.     if(Typeof(thing) != TYPE_ROOM && db[thing].location == NOTHING) return 0;
  42.  
  43. #ifdef ROBOT_MODE
  44.     if(Typeof(thing) != TYPE_PLAYER && Robot(player) && Robot(thing)) return 0;
  45.     if(Typeof(thing) == TYPE_EXIT && Robot(player) &&
  46.         db[thing].location >= 0 && Robot(db[thing].location)) return 0;
  47. #endif ROBOT_MODE
  48.  
  49.     return(eval_boolexp (player, db[thing].key));
  50. }
  51.  
  52. int can_doit(dbref player, dbref thing, const char *default_fail_msg)
  53. {
  54.     dbref loc;
  55.     char buf[BUFFER_LEN];
  56.  
  57.     if((loc = getloc(player)) == NOTHING) return 0;
  58.  
  59.     if(!could_doit(player, thing)) {
  60.     /* can't do it */
  61.     if(db[thing].fail_message) {
  62.         notify(player, db[thing].fail_message);
  63.     } else if(default_fail_msg) {
  64.         notify(player, default_fail_msg);
  65.     }
  66.     
  67.     if(db[thing].ofail && !Dark(player)) {
  68. #ifdef GENDER
  69.         pronoun_substitute(buf, player, db[thing].ofail);
  70. #else        
  71.         sprintf(buf, "%s %s", db[player].name, db[thing].ofail);
  72. #endif /* GENDER */
  73.         notify_except(db[loc].contents, player, buf);
  74.     }
  75.  
  76.     return 0;
  77.     } else {
  78.     /* can do it */
  79.     if(db[thing].succ_message) {
  80.         notify(player, db[thing].succ_message);
  81.     }
  82.  
  83.     if(db[thing].osuccess && !Dark(player)) {
  84. #ifdef GENDER
  85.         pronoun_substitute(buf, player, db[thing].osuccess);
  86. #else
  87.         sprintf(buf, "%s %s", db[player].name, db[thing].osuccess);
  88. #endif /* GENDER */
  89.         notify_except(db[loc].contents, player, buf);
  90.     }
  91.  
  92.     return 1;
  93.     }
  94. }
  95.  
  96. int can_see(dbref player, dbref thing, int can_see_loc)
  97. {
  98.     if(player == thing || Typeof(thing) == TYPE_EXIT) {
  99.     return 0;
  100.     } else if(can_see_loc) {
  101.     return(!Dark(thing) || controls(player, thing));
  102.     } else {
  103.     /* can't see loc */
  104.     return(controls(player, thing));
  105.     }
  106. }
  107.  
  108. int controls(dbref who, dbref what)
  109. {
  110.     /* Wizard controls everything */
  111.     /* owners control their stuff */
  112.     return(what >= 0
  113.        && what < db_top
  114.        && (Wizard(who)
  115.            || who == db[what].owner));
  116. }
  117.  
  118. int can_link(dbref who, dbref what)
  119. {
  120.     return((Typeof(what) == TYPE_EXIT && db[what].location == NOTHING)
  121.        || controls(who, what));
  122. }
  123.  
  124. int payfor(dbref who, int cost)
  125. {
  126.     if(Wizard(who)) {
  127.     return 1;
  128.     } else if(db[who].pennies >= cost) {
  129.     db[who].pennies -= cost;
  130.     return 1;
  131.     } else {
  132.     return 0;
  133.     }
  134. }
  135.  
  136. int word_start (const char *str, const char let)
  137. {
  138.     int chk;
  139.  
  140.     for (chk = 1; *str; str++) {
  141.     if (chk && *str == let) return 1;
  142.     chk = *str == ' ';
  143.     }
  144.     return 0;
  145. }
  146.  
  147.  
  148. int ok_name(const char *name)
  149. {
  150.     return (name
  151.         && *name
  152.         && *name != LOOKUP_TOKEN
  153.         && *name != NUMBER_TOKEN
  154.         && !index(name, ARG_DELIMITER)
  155.         && !index(name, AND_TOKEN)
  156.         && !index(name, OR_TOKEN)
  157.         && !word_start(name, NOT_TOKEN)
  158. #ifdef NOFAKES
  159.         && string_compare(name, "A")
  160.         && string_compare(name, "An")
  161.         && string_compare(name, "The")
  162.         && string_compare(name, "You")
  163.         && string_compare(name, "Your")
  164.         && string_compare(name, "Going")
  165.             && string_compare(name, "Huh?")
  166. #endif NOFAKES
  167.         && string_compare(name, "me")        
  168.         && string_compare(name, "home")
  169.         && string_compare(name, "here"));
  170. }
  171.  
  172. int ok_player_name(const char *name)
  173. {
  174.     const char *scan;
  175.  
  176.     if(!ok_name(name) || strlen(name) > PLAYER_NAME_LIMIT) return 0;
  177.  
  178.     for(scan = name; *scan; scan++) {
  179.     if(!(isprint(*scan) && !isspace(*scan))) { /* was isgraph(*scan) */
  180.         return 0;
  181.     }
  182.     }
  183.  
  184.     /* lookup name to avoid conflicts */
  185.     return (lookup_player(name) == NOTHING);
  186. }
  187.  
  188. int ok_password(const char *password)
  189. {
  190.     const char *scan;
  191.  
  192.     if(*password == '\0') return 0;
  193.  
  194.     for(scan = password; *scan; scan++) {
  195.     if(!(isprint(*scan) && !isspace(*scan))) {
  196.         return 0;
  197.     }
  198.     }
  199.  
  200.     return 1;
  201. }
  202.  
  203. #ifdef GENDER
  204. /*
  205.  * pronoun_substitute()
  206.  *
  207.  * %-type substitutions for pronouns
  208.  *
  209.  * %s/%S for subjective pronouns (he/she/it, He/She/It)
  210.  * %o/%O for objective pronouns (him/her/it, Him/Her/It)
  211.  * %p/%P for possessive pronouns (his/her/its, His/Her/Its)
  212.  * %n    for the player's name.
  213.  */
  214. void pronoun_substitute(char *result, dbref player, const char *str)
  215. {
  216.     char c;
  217.  
  218.     const static char *subjective[4] = { "", "it", "she", "he" };
  219.     const static char *possessive[4] = { "", "its", "her", "his" };
  220.     const static char *objective[4] = { "", "it", "her", "him" };
  221.  
  222. #ifdef COMPRESS
  223.     str = uncompress(str);
  224. #endif /* COMPRESS */
  225.     strcpy(result, db[player].name);
  226.     result += strlen(result);
  227.     *result++ = ' ';
  228.     while (*str) {
  229.     if(*str == '%') {
  230.         *result = '\0';
  231.         c = *(++str);
  232.         if (Genderof(player) == GENDER_UNASSIGNED) {
  233.         switch(c) {
  234.           case 'n':
  235.           case 'N':
  236.           case 'o':
  237.           case 'O':
  238.           case 's':
  239.           case 'S':
  240.             strcat(result, db[player].name);
  241.             break;
  242.           case 'p':
  243.           case 'P':
  244.             strcat(result, db[player].name);
  245.             strcat(result, "'s");
  246.             break;
  247.           default:
  248.             result[0] = *str;
  249.             result[1] = 0;
  250.             break;
  251.         }
  252.         str++;
  253.         result += strlen(result);
  254.         } else {
  255.         switch (c) {
  256.           case 's':
  257.           case 'S':
  258.             strcat(result, subjective[Genderof(player)]);
  259.             break;
  260.           case 'p':
  261.           case 'P':
  262.             strcat(result, possessive[Genderof(player)]);
  263.             break;
  264.           case 'o':
  265.           case 'O':
  266.             strcat(result, objective[Genderof(player)]);
  267.             break;
  268.           case 'n':
  269.           case 'N':
  270.             strcat(result, db[player].name);
  271.             break;
  272.           default:
  273.             *result = *str;
  274.             result[1] = '\0';
  275.             break;
  276.         } 
  277.         if(isupper(c) && islower(*result)) {
  278.             *result = toupper(*result);
  279.         }
  280.         
  281.         result += strlen(result);
  282.         str++;
  283.         }
  284.     } else {
  285.         *result++ = *str++;
  286.     }
  287.     }
  288.     *result = '\0';
  289.  
  290. #endif /* GENDER */
  291.