src/powers.c

Go to the documentation of this file.
00001 /*
00002  * powers.c - power manipulation routines 
00003  */
00004 
00005 #include "copyright.h"
00006 #include "config.h"
00007 
00008 #include "db.h"
00009 #include "mudconf.h"
00010 #include "externs.h"
00011 #include "command.h"
00012 #include "powers.h"
00013 #include "alloc.h"
00014 
00018 int ph_any(dbref target, dbref player, POWER power, int fpowers, int reset)
00019 {
00020         if(fpowers & POWER_EXT) {
00021                 if(reset)
00022                         s_Powers2(target, Powers2(target) & ~power);
00023                 else
00024                         s_Powers2(target, Powers2(target) | power);
00025         } else {
00026                 if(reset)
00027                         s_Powers(target, Powers(target) & ~power);
00028                 else
00029                         s_Powers(target, Powers(target) | power);
00030         }
00031         return 1;
00032 }
00033 
00037 int ph_god(dbref target, dbref player, POWER power, int fpowers, int reset)
00038 {
00039         if(!God(player))
00040                 return 0;
00041         return (ph_any(target, player, power, fpowers, reset));
00042 }
00043 
00047 int ph_wiz(dbref target, dbref player, POWER power, int fpowers, int reset)
00048 {
00049         if(!Wizard(player) & !God(player))
00050                 return 0;
00051         return (ph_any(target, player, power, fpowers, reset));
00052 }
00053 
00057 int ph_wizroy(dbref target, dbref player, POWER power, int fpowers, int reset)
00058 {
00059         if(!WizRoy(player) & !God(player))
00060                 return 0;
00061         return (ph_any(target, player, power, fpowers, reset));
00062 }
00063 
00067 int ph_inherit(dbref target, dbref player, POWER power, int fpowers,
00068                            int reset)
00069 {
00070         if(!Inherits(player))
00071                 return 0;
00072         return (ph_any(target, player, power, fpowers, reset));
00073 }
00074 
00075 POWERENT gen_powers[] = {
00076         {(char *) "quota", POW_CHG_QUOTAS, 0, 0, ph_wiz},
00077         {(char *) "chown_anything", POW_CHOWN_ANY, 0, 0, ph_wiz},
00078         {(char *) "announce", POW_ANNOUNCE, 0, 0, ph_wiz},
00079         {(char *) "boot", POW_BOOT, 0, 0, ph_wiz},
00080         {(char *) "halt", POW_HALT, 0, 0, ph_wiz},
00081         {(char *) "control_all", POW_CONTROL_ALL, 0, 0, ph_god},
00082         {(char *) "expanded_who", POW_WIZARD_WHO, 0, 0, ph_wiz},
00083         {(char *) "see_all", POW_EXAM_ALL, 0, 0, ph_wiz},
00084         {(char *) "prog", POW_PROG, 0, 0, ph_wiz},
00085         {(char *) "find_unfindable", POW_FIND_UNFIND, 0, 0, ph_wiz},
00086         {(char *) "free_money", POW_FREE_MONEY, 0, 0, ph_wiz},
00087         {(char *) "free_quota", POW_FREE_QUOTA, 0, 0, ph_wiz},
00088         {(char *) "hide", POW_HIDE, 0, 0, ph_wiz},
00089         {(char *) "idle", POW_IDLE, 0, 0, ph_wiz},
00090         {(char *) "search", POW_SEARCH, 0, 0, ph_wiz},
00091         {(char *) "long_fingers", POW_LONGFINGERS, 0, 0, ph_wiz},
00092         {(char *) "comm_all", POW_COMM_ALL, 0, 0, ph_wiz},
00093         {(char *) "see_queue", POW_SEE_QUEUE, 0, 0, ph_wiz},
00094         {(char *) "see_hidden", POW_SEE_HIDDEN, 0, 0, ph_wiz},
00095         {(char *) "monitor", POW_MONITOR, 0, 0, ph_wiz},
00096         {(char *) "poll", POW_POLL, 0, 0, ph_wiz},
00097         {(char *) "no_destroy", POW_NO_DESTROY, 0, 0, ph_wiz},
00098         {(char *) "guest", POW_GUEST, 0, 0, ph_god},
00099         {(char *) "stat_any", POW_STAT_ANY, 0, 0, ph_wiz},
00100         {(char *) "steal_money", POW_STEAL, 0, 0, ph_wiz},
00101         {(char *) "tel_anywhere", POW_TEL_ANYWHR, 0, 0, ph_wiz},
00102         {(char *) "tel_anything", POW_TEL_UNRST, 0, 0, ph_wiz},
00103         {(char *) "unkillable", POW_UNKILLABLE, 0, 0, ph_wiz},
00104         {(char *) "pass_locks", POW_PASS_LOCKS, 0, 0, ph_wiz},
00105         {(char *) "builder", POW_BUILDER, POWER_EXT, 0, ph_wiz},
00106 /* BattletechMUX Powers */
00107         {(char *) "mech", POW_MECH, POWER_EXT, 0, ph_wiz},
00108         {(char *) "security", POW_SECURITY, POWER_EXT, 0, ph_wiz},
00109         {(char *) "mechrep", POW_MECHREP, POWER_EXT, 0, ph_wiz},
00110         {(char *) "map", POW_MAP, POWER_EXT, 0, ph_wiz},
00111         {(char *) "tech", POW_TECH, POWER_EXT, 0, ph_wiz},
00112         {(char *) "template", POW_TEMPLATE, POWER_EXT, 0, ph_wiz},
00113         {NULL, 0, 0, 0, 0}
00114 };
00115 
00119 void init_powertab(void)
00120 {
00121         POWERENT *fp;
00122         char *nbuf, *np, *bp;
00123 
00124         hashinit(&mudstate.powers_htab, 15 * HASH_FACTOR);
00125         nbuf = alloc_sbuf("init_powertab");
00126         for(fp = gen_powers; fp->powername; fp++) {
00127                 for(np = nbuf, bp = (char *) fp->powername; *bp; np++, bp++)
00128                         *np = ToLower(*bp);
00129                 *np = '\0';
00130                 hashadd(nbuf, (int *) fp, &mudstate.powers_htab);
00131         }
00132         free_sbuf(nbuf);
00133 }
00134 
00138 void display_powertab(dbref player)
00139 {
00140         char *buf, *bp;
00141         POWERENT *fp;
00142 
00143         bp = buf = alloc_lbuf("display_powertab");
00144         safe_str((char *) "Powers:", buf, &bp);
00145         for(fp = gen_powers; fp->powername; fp++) {
00146                 if((fp->listperm & CA_WIZARD) && !Wizard(player))
00147                         continue;
00148                 if((fp->listperm & CA_GOD) && !God(player))
00149                         continue;
00150                 safe_chr(' ', buf, &bp);
00151                 safe_str((char *) fp->powername, buf, &bp);
00152         }
00153         *bp = '\0';
00154         notify(player, buf);
00155         free_lbuf(buf);
00156 }
00157 
00158 POWERENT *find_power(dbref thing, char *powername)
00159 {
00160         char *cp;
00161 
00162         /*
00163          * Make sure the power name is valid 
00164          */
00165 
00166         for(cp = powername; *cp; cp++)
00167                 *cp = ToLower(*cp);
00168         return (POWERENT *) hashfind(powername, &mudstate.powers_htab);
00169 }
00170 
00171 int decode_power(dbref player, char *powername, POWERSET * pset)
00172 {
00173         POWERENT *pent;
00174 
00175         pset->word1 = 0;
00176         pset->word2 = 0;
00177 
00178         pent = (POWERENT *) hashfind(powername, &mudstate.powers_htab);
00179         if(!pent) {
00180                 notify_printf(player, "%s: Power not found.", powername);
00181                 return 0;
00182         }
00183         if(pent->powerpower & POWER_EXT)
00184                 pset->word2 = pent->powervalue;
00185         else
00186                 pset->word1 = pent->powervalue;
00187 
00188         return 1;
00189 }
00190 
00191 /*
00192  * Set or clear a specified power on an object. 
00193  */
00194 void power_set(dbref target, dbref player, char *power, int key)
00195 {
00196         POWERENT *fp;
00197         int negate, result;
00198 
00199         /*
00200          * Trim spaces, and handle the negation character 
00201          */
00202 
00203         negate = 0;
00204         while (*power && isspace(*power))
00205                 power++;
00206         if(*power == '!') {
00207                 negate = 1;
00208                 power++;
00209         }
00210         while (*power && isspace(*power))
00211                 power++;
00212 
00213         /*
00214          * Make sure a power name was specified 
00215          */
00216 
00217         if(*power == '\0') {
00218                 if(negate)
00219                         notify(player, "You must specify a power to clear.");
00220                 else
00221                         notify(player, "You must specify a power to set.");
00222                 return;
00223         }
00224         fp = find_power(target, power);
00225         if(fp == NULL) {
00226                 notify(player, "I don't understand that power.");
00227                 return;
00228         }
00229         /*
00230          * Invoke the power handler, and print feedback 
00231          */
00232 
00233         result =
00234                 fp->handler(target, player, fp->powervalue, fp->powerpower, negate);
00235         if(!result)
00236                 notify(player, "Permission denied.");
00237         else if(!(key & SET_QUIET) && !Quiet(player))
00238                 notify_printf(player, "%s - %s %s", Name(target), fp->powername,
00239                                           negate ? "removed." : "granted.");
00240         return;
00241 }
00242 
00246 int has_power(dbref player, dbref it, char *powername)
00247 {
00248         POWERENT *fp;
00249         POWER fv;
00250 
00251         fp = find_power(it, powername);
00252         if(fp == NULL)
00253                 return 0;
00254 
00255         if(fp->powerpower & POWER_EXT)
00256                 fv = Powers2(it);
00257         else
00258                 fv = Powers(it);
00259 
00260         if(fv & fp->powervalue) {
00261                 if((fp->listperm & CA_WIZARD) && !Wizard(player))
00262                         return 0;
00263                 if((fp->listperm & CA_GOD) && !God(player))
00264                         return 0;
00265                 return 1;
00266         }
00267         return 0;
00268 }
00269 
00273 char *power_description(dbref player, dbref target)
00274 {
00275         char *buff, *bp;
00276         POWERENT *fp;
00277         int otype;
00278         POWER fv;
00279 
00280         /*
00281          * Allocate the return buffer 
00282          */
00283 
00284         otype = Typeof(target);
00285         bp = buff = alloc_mbuf("power_description");
00286 
00287         /*
00288          * Store the header strings and object type 
00289          */
00290 
00291         safe_mb_str((char *) "Powers:", buff, &bp);
00292 
00293         for(fp = gen_powers; fp->powername; fp++) {
00294                 if(fp->powerpower & POWER_EXT)
00295                         fv = Powers2(target);
00296                 else
00297                         fv = Powers(target);
00298                 if(fv & fp->powervalue) {
00299                         if((fp->listperm & CA_WIZARD) && !Wizard(player))
00300                                 continue;
00301                         if((fp->listperm & CA_GOD) && !God(player))
00302                                 continue;
00303                         safe_mb_chr(' ', buff, &bp);
00304                         safe_mb_str((char *) fp->powername, buff, &bp);
00305                 }
00306         }
00307 
00308         /*
00309          * Terminate the string, and return the buffer to the caller 
00310          */
00311 
00312         *bp = '\0';
00313         return buff;
00314 }
00315 
00319 void decompile_powers(dbref player, dbref thing, char *thingname)
00320 {
00321         POWER f1, f2;
00322         POWERENT *fp;
00323         char new[LBUF_SIZE];
00324 
00325         /*
00326          * Report generic powers 
00327          */
00328 
00329         f1 = Powers(thing);
00330         f2 = Powers2(thing);
00331 
00332         for(fp = gen_powers; fp->powername; fp++) {
00333 
00334                 /*
00335                  * Skip if we shouldn't decompile this power 
00336                  */
00337 
00338                 if(fp->listperm & CA_NO_DECOMP)
00339                         continue;
00340 
00341                 /*
00342                  * Skip if this power is not set 
00343                  */
00344 
00345                 if(fp->powerpower & POWER_EXT) {
00346                         if(!(f2 & fp->powervalue))
00347                                 continue;
00348                 } else {
00349                         if(!(f1 & fp->powervalue))
00350                                 continue;
00351                 }
00352 
00353                 /*
00354                  * Skip if we can't see this power 
00355                  */
00356 
00357                 if(!check_access(player, fp->listperm))
00358                         continue;
00359 
00360                 /*
00361                  * We made it this far, report this power 
00362                  */
00363                 strncpy(new, thingname, LBUF_SIZE-1);
00364                 notify_printf(player, "@power %s=%s", strip_ansi_r(new,thingname,strlen(thingname)),
00365                                           fp->powername);
00366         }
00367 }

Generated on Mon May 28 04:25:25 2007 for BattletechMUX by  doxygen 1.4.7