/* $Header: /belch_a/users/rearl/tinymuck/src/RCS/rob.c,v 1.5 90/09/16 04:42:51 rearl Exp $ */

/*
 * $Log:	rob.c,v $
 * Revision 1.5  90/09/16  04:42:51  rearl
 * Preparation code added for disk-based MUCK.
 * 
 * Revision 1.4  90/08/15  03:10:13  rearl
 * Fiddled with do_kill() and put in pronoun subs for it.
 * 
 * Revision 1.3  90/08/05  03:19:55  rearl
 * Redid matching routines.
 * 
 * Revision 1.2  90/08/02  02:16:20  rearl
 * Odrop and x killed y! messages now come before y has left.
 * Pronoun substitution added for player odrop.
 * 
 * Revision 1.1  90/07/19  23:04:05  casie
 * Initial revision
 * 
 *
 */

#include "copyright.h"
#include "config.h"

/* rob and kill */

#include "db.h"
#include "params.h"
#include "interface.h"
#include "match.h"
#include "externs.h"


void do_kill(dbref player, const char *what, int Cost)
{
  dbref victim;
  char buf[BUFFER_LEN];
  struct match_data md;
  
  init_match(player, what, TYPE_PLAYER, &md);
  match_neighbor(&md);
  match_me(&md);
  if(Arch(player)) {
    match_player(&md);
    match_absolute(&md);
  }
  victim = match_result(&md);
  
  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 && Typeof(victim)!=TYPE_THING) {
      notify(player, "Sorry, you can only kill other players and objects.");
    } else {
      /* go for it */
      /* set Cost */
      if(Cost < KILL_MIN_COST) Cost = KILL_MIN_COST;

#ifdef HAVEN
      if (Haven(DBFETCH(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 pennies.");
      } else if((random() % KILL_BASE_COST) < Cost
		&& !controls(victim, DBFETCH(victim)->location)) {
	/* you killed him */
	if(get_attr(victim,"Kill"))
	  /* give him the drop message */
	  notify(player, get_attr(victim,"Kill"));
	else
	  {
	    sprintf(buf, "You killed %s!", NAME(victim));
	    notify(player, buf);
	  }
	
	/* now notify everybody else */
	if (get_attr(victim,"Okill"))
	  sprintf(buf, "%s %s", NAME(victim),
		  pronoun_substitute(player,
				     get_attr(victim,"Okill"),
				     victim));
	else {
	  sprintf(buf, "%s killed %s!", NAME(player), NAME(victim));
	}
	notify_except(DBFETCH(DBFETCH(player)->location)->contents,
		      player, buf);
	
	/* maybe pay off the bonus */
#if (KILL_BONUS) != 0
	if(DBFETCH(victim)->sp.player.pennies < MAX_PENNIES) {
	  sprintf(buf, "Your insurance policy pays %d pennies.",
		  KILL_BONUS);
	  notify(victim, buf);
	  DBFETCH(victim)->sp.player.pennies += KILL_BONUS;
	  DBDIRTY(victim);
	  DBDIRTY(OWNER(victim));
	} else {
	  notify(victim, "Your insurance policy has been revoked.");
	}
#endif /* KILL_BONUS */
	/* send him home */
	send_home(victim);
	halt_long(victim);
	if(get_attr(victim, "Akill"))
	  trigobj(victim, get_attr(victim, "Akill"), player);
      } else {
	/* notify player and victim only */
	notify(player, "Your murder attempt failed.");
	sprintf(buf, "%s tried to kill you!", NAME(player));
	notify(victim, buf);
      }
      break;
    }
  }
}

void do_give(dbref player, const char *recipient, int amount)
{
  dbref who;
  char buf[BUFFER_LEN];
  struct match_data md;
  
  /* do amount consistency check */
  if(amount < 0 && !(Arch(player))) {
    notify(player, "Try using the \"rob\" command.");
    return;
  }

  /* check recipient */
  init_match(player, recipient, TYPE_PLAYER, &md);
  match_neighbor(&md);
  match_me(&md);
  match_possession(&md);
  match_here(&md);
  if(Arch(player)) {
    match_player(&md);
    match_absolute(&md);
  }
  
  switch(who = match_result(&md)) {
  case NOTHING:
    notify(player, "Give to whom?");
    return;
  case AMBIGUOUS:
    notify(player, "I don't know who you mean!");
    return;
  default:
    if(!Arch(player)) {
      if((Typeof(who) != TYPE_PLAYER) && (Typeof(who) != TYPE_THING)) {
	notify(player, "You can only give to other players.");
	return;
      } else if(DBFETCH(who)->sp.player.pennies + amount > MAX_PENNIES) {
	notify(player, "That player doesn't need that many pennies!");
	return;
      }
    }
    break;
  }

  /* try to do the give */
  if(!payfor(player, amount)) {
    notify(player, "You don't have that many pennies to give!");
  } else {
    /* he can do it */
    switch(Typeof(who)) {
    case TYPE_PLAYER:
      DBFETCH(who)->sp.player.pennies += amount;
      sprintf(buf, "You give %d %s to %s.",
	      amount,
	      amount == 1 ? "penny" : "pennies",
	      NAME(who));
      notify(player, buf);
      sprintf(buf, "%s gives you %d %s.",
	      NAME(player),
	      amount,
	      amount == 1 ? "penny" : "pennies");
      notify(who, buf);
      break;
    case TYPE_THING:
      if(!get_attr(who,"Cost")) {
	if(!Arch(player) &&
	   (DBFETCH(who)->sp.thing.value + amount > MAX_OBJECT_DEPOSIT)) {
	  sprintf(buf,
		  "Object cannot exceed the maximum value of %d pennies.",
		  MAX_OBJECT_DEPOSIT);
	  notify(player, buf);
	}
	else {
	  DBFETCH(who)->sp.thing.value += amount;
	  sprintf(buf, "You change the value of %s to %d %s.",
		  NAME(who),
		  DBFETCH(who)->sp.thing.value,
		  DBFETCH(who)->sp.thing.value == 1 ? "penny" : "pennies");
	  notify(player, buf);
	}
	return;
      }
      if(amount < atoi(get_attr(who,"Cost"))) {
	notify(player, "Feeling poor today?");
	return;
      }
      sprintf(buf,"You get %d in change.", amount-atoi(get_attr
						      (who,"Cost")));
      notify(player,buf);
      DBFETCH(player)->sp.player.pennies += /*refund the change */
	amount - atoi(get_attr(who, "Cost"));
      DBFETCH(who)->sp.player.pennies += atoi(get_attr(who,"Cost"));

      if(get_attr(who, "Pay"))
	notify(player,get_attr(who,"Pay"));
      if(get_attr(who, "Opay")) {
	dbref where = DBFETCH(player)->location;
	sprintf(buf,"%s %s", NAME(who), get_attr(who, "Opay"));
	notify_except(DBFETCH(where)->contents, player,
		      pronoun_substitute(player, buf, who));
      }
      if(get_attr(who, "Apay"))
	trigobj(who, get_attr(who, "Apay"), player);
      break;
    default:
      notify(player, "You can't give pennies to that!");
      break;
    }
    DBDIRTY(who);
  }
}