/* $Header: rob.c,v 2.0 90/05/05 12:45:40 lachesis Exp $
 * $Log:    rob.c,v $
 * Revision 2.0  90/05/05  12:45:40  lachesis
 * Incorporated ABODE and HAVEN flags (remembering to convert FireFoot's
 * usage of those flags to ours).
 * Added Examine of objects that don't belong to you, added GOD_PRIV.
 *
 * Revision 1.2  90/04/20  14:06:49  lachesis
 * Added @odrop && @drop.
 *
 * Revision 1.1  90/04/14  14:56:50  lachesis
 * Initial revision
 *
 */
#include "copyright.h"

/* rob and kill */

#include "os.h"
#include "db.h"
#include "config.h"
#include "interface.h"
#include "match.h"
#include "externs.h"

void do_rob (dbref player, const char *what)
{
  dbref thing;
  char buf[BUFFER_LEN];

  init_match (player, what, TYPE_PLAYER);
  match_neighbor ();
  match_me ();
  if (Wizard (player)) {
    match_absolute ();
    match_player ();
  }
  thing = match_result ();

  switch (thing) {
  case NOTHING:
    notify (player, "Rob whom?");
    break;
  case AMBIGUOUS:
    notify (player, "I don't know who you mean!");
    break;
  default:
    if (Typeof (thing) != TYPE_PLAYER) {
      notify (player, "Sorry, you can only rob other players.");
    } else if (db[thing].sp.player.pennies < 1) {
      sprintf (buf, "%s is bereft of cookies.", db[thing].name);
      notify (player, buf);
      sprintf (buf,
        "%s tried to rob you, but you have no cookies to take.",
        db[player].name);
      notify (thing, buf);
    } else if (can_doit (player, thing, "Your conscience tells you not to.")) {
      /* steal a penny */
      db[player].sp.player.pennies++;
      db[thing].sp.player.pennies--;
      notify (player, "You stole a cookie.");
      sprintf (buf, "%s stole one of your cookies!", db[player].name);
      notify (thing, buf);
    }
    break;
  }
}

void do_kill (dbref player, const char *what, int cost)
{
  dbref victim;
  char buf[BUFFER_LEN];
#ifdef GENDER
  char xbuf[BUFFER_LEN];
#endif /* GENDER */

  init_match (player, what, TYPE_PLAYER);
  match_neighbor ();
  match_me ();
  if (Wizard (player)) {
    match_player ();
    match_absolute ();
  }
  victim = match_result ();

  switch (victim) {
  case NOTHING:
    notify (player, "I don't see that player here.");
    break;
  case AMBIGUOUS:
    notify (player, "I don't know who you mean!");
    break;
  default:
    if (Typeof (victim) != TYPE_PLAYER) {
      notify (player, "Sorry, you can only kill other players.");
    } else {
      /* go for it */
      /* set cost */
      if (cost < KILL_MIN_COST)
        cost = KILL_MIN_COST;
#ifdef HAVEN
      if (db[db[player].location].flags & HAVEN
        && !controls (player, db[player].location)) {
        notify (player, "You can't kill anyone here!");
        break;
      }
#endif /* HAVEN */

      /* see if it works */
      if (!payfor (player, cost)) {
        notify (player, "You don't have enough cookies.");
      } else if ((OS_RAND () % KILL_BASE_COST) < cost && !Wizard (victim)) {
        /* you killed him */
        if (db[victim].drop_message)
          /* give him the drop message */
          notify (player, db[victim].drop_message);
        else {
          sprintf (buf, "You killed %s!", db[victim].name);
          notify (player, buf);
        }

        /* notify victim */
        sprintf (buf, "%s killed you!", db[player].name);
        notify (victim, buf);

        /* maybe pay off the bonus */
        if (db[victim].sp.player.pennies < MAX_PENNIES) {
          sprintf (buf, "Your insurance policy pays %d cookies.", KILL_BONUS);
          notify (victim, buf);
          db[victim].sp.player.pennies += KILL_BONUS;
        } else {
          notify (victim, "Your insurance policy has been revoked.");
        }

        /* now notify everybody else */
        /* give drop message */
        sprintf (buf, "%s killed %s!", db[player].name, db[victim].name);
        if (db[victim].odrop) {
          (void) strcat (buf, "  ");
#ifdef GENDER
          pronoun_substitute (xbuf, player, db[victim].odrop, 0);
          (void) strcat (buf, xbuf);
#endif /* GENDER */
        }
        notify_except2 (db[db[player].location].contents,
          player, victim, buf);

        /* send him home */
        send_home (victim);
      } else {
        /* notify player and victim only */
        notify (player, "Your murder attempt failed.");
        sprintf (buf, "%s tried to kill you!", db[player].name);
        notify (victim, buf);
      }
      break;
    }
  }
}

void do_give (dbref player, char *recipient, int amount)
{
  dbref who;
  char buf[BUFFER_LEN];

  /* do amount consistency check */
  if (amount < 0 && !Wizard (player)) {
    notify (player, "Try using the \"rob\" command.");
    return;
  } else if (amount == 0) {
    notify (player, "You must specify a positive number of cookies.");
    return;
  }

  /* check recipient */
  init_match (player, recipient, TYPE_PLAYER);
  match_neighbor ();
  match_me ();
  if (Wizard (player)) {
    match_player ();
    match_absolute ();
  }

  switch (who = match_result ()) {
  case NOTHING:
    notify (player, "Give to whom?");
    return;
  case AMBIGUOUS:
    notify (player, "I don't know who you mean!");
    return;
  default:
    if (!Wizard (player)) {
      if (Typeof (who) != TYPE_PLAYER) {
        notify (player, "You can only give to other players.");
        return;
      } else if (db[who].sp.player.pennies + amount > MAX_PENNIES) {
        notify (player, "That player doesn't need that many cookies!");
        return;
      }
    }
    break;
  }

  /* try to do the give */
  if (!payfor (player, amount)) {
    notify (player, "You don't have that many cookies to give!");
  } else {
    /* he can do it */
    switch (Typeof (who)) {
    case TYPE_PLAYER:
      db[who].sp.player.pennies += amount;
      sprintf (buf, "You give %d %s to %s.",
        amount, amount == 1 ? "cookie" : "cookies", db[who].name);
      notify (player, buf);
      sprintf (buf, "%s gives you %d %s.",
        db[player].name, amount, amount == 1 ? "cookie" : "cookies");
      notify (who, buf);
      break;
    case TYPE_THING:
      db[who].sp.thing.value += amount;
      sprintf (buf, "You change the value of %s to %d %s.",
        db[who].name,
        db[who].sp.thing.value,
        db[who].sp.thing.value == 1 ? "cookie" : "cookies");
      notify (player, buf);
      break;
    default:
      notify (player, "You can't give cookies to that!");
      break;
    }

  }
}