dbm/
misc/
old-docs/
/* notify.c */

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

#include <stdio.h>

#include "teeny.h"

void            notify_except();

/*
 * Routines for sending strings to players.
 */

void            notify_bad(player)
  int             player;
{
  notify_player(player, "Something truly horrid happened.  Good luck.\r\n");
}

void            do_huh(player)
  int             player;
{
  notify_player(player, "Huh?  (Type \"help\" for help.)\r\n");
}

/*
 * Echoes the string to everyone in the room the player is in, except the
 * player itself
 * 
 */

void            notify_oall(player, str)
  int             player;
  char           *str;

{
  notify_except(player, str, player);
}

/*
 * Echoes the string to everyone in the room the player is in.
 */
void            notify_all(player, str)
  int             player;
  char           *str;
{
  notify_except(player, str, -1);
}

/*
 * Writes a list out to a player, displaying numbers and so forth when
 * appropriate.
 * 
 */

void            notify_list(player, list, buff, siz, dark_ok)
  int             player;	/* Obj # of player. */
  int             list;		/* Obj # of 1st list element */
  char           *buff;
  int             siz;
  int             dark_ok;
{
  int             count;

  /* Loop over the whole list */

  for (count = 0; list != -1 && count < MAXLIST; count++) {

    if (!dark_ok) {
      if (!can_see(player, list)) {	/* Skip this name */
	if (get_int_elt(list, NEXT, &list) == -1) {
	  warning("notify_list", "bad next reference");
	  break;
	}
	continue;
      }
    }
    if ((dark_ok && (list == player)) || list != player) {
      char           *z;
      z = stuff_name(player, list);
      if (strlen(z) > (siz - 3))
	z[siz - 4] = 0;		/* trunicate it */
      (void) strcpy(buff, z);
      (void) strcat(buff, "\r\n");
      notify_player(player, buff);
    }
    if (get_int_elt(list, NEXT, &list) == -1) {
      warning("notify_list", "bad next reference");
      break;
    }
  }
}

/*
 * Notifies everyone in the room where player is, except possibly the
 * specified object number (typically either player or -1)
 * 
 */

void            notify_except(player, str, except)
  int             player;
  char           *str;
  int             except;
{
  int             location, list;

  if (get_int_elt(player, LOC, &location) == -1) {
    warning("notify_except", "bad player location ref");
    return;
  }
  /* Grab the first thing in the contents list */

  if (get_int_elt(location, CONTENTS, &list) == -1) {
    warning("notify_except", "bad contents reference on player loc");
    return;
  }
  /* Trot down the contents list, looking for objects that are alive */

  while (list != -1) {
    if (!exists_object(list)) {
      warning("notify_except", "non existant object in contents list");
      continue;
    }
    if (isalive(list) && list != except) {
      notify_player(list, str);
    }
    if (get_int_elt(list, NEXT, &list) == -1) {
      warning("notify_except", "bad reference to contents list");
      return;
    }
  }
}

/* notify everyone in player's location except two people */
void            notify_except2(player, string, except1, except2)
  int             player;
  char           *string;
  int             except1, except2;
{
  int             loc, list;

  if (get_int_elt(player, LOC, &loc) == -1) {
    warning("notify_except2", "bad loc ref on player");
    return;
  }
  if (get_int_elt(loc, CONTENTS, &list) == -1) {
    warning("notify_except2", "bad contents list on loc");
    return;
  }
  while (list != -1) {
    if (isalive(list) && (list != except1 && list != except2))
      notify_player(list, string);
    if (get_int_elt(list, NEXT, &list) == -1) {
      warning("notify_except2", "bad next ref on object");
      return;
    }
  }
}

void            notify_contents(loc, str)
  int             loc;
  char           *str;
{
  int             list;

  if (get_int_elt(loc, CONTENTS, &list) == -1) {
    warning("notify_contents", "bad contents list");
    return;
  }
  while (list != -1) {
    if (isalive(list))
      notify_player(list, str);
    if (get_int_elt(list, NEXT, &list) == -1) {
      warning("notify_contents", "bad next ref");
      return;
    }
  }
}

void            notify_exits(player, list)
  int             player, list;
{
  char            buffer[1024];
  char           *name, *p;
  int             any = 0;

  (void) strcpy(buffer, "Obvious exits:\r\n");
  p = buffer + 16;

  while (list != -1) {
    if (!isobvious(list)) {
      if (get_int_elt(list, NEXT, &list) == -1) {
	warning("notify_exits", "bad next ref");
	return;
      }
      continue;
    }
    if (!any)
      any = 1;

    if (get_str_elt(list, NAME, &name) == -1) {
      warning("notify_exits", "bad name ref");
      return;
    }
    while (name[0] == ' ' || name[0] == ';')
      name++;

    for (; *name && *name != ';' && (p - buffer) < 1000; *p++ = *name++);
    if ((p - buffer) > 999) {
      *p++ = '\r';
      *p++ = '\n';
      *p = 0;
      break;
    }
    *p++ = ',';
    *p++ = ' ';

    if (get_int_elt(list, NEXT, &list) == -1) {
      warning("notify_exits", "bad next ref");
      return;
    }
  }

  if (any) {
    p -= 2;
    p[0] = '\r';
    p[1] = '\n';
    p[2] = 0;

    notify_player(player, buffer);
  }
}