/* Copyright 1989, 1990 by James Aspnes, David Applegate, and Bennet Yee */
/* See the file COPYING for distribution information */
#include "db.h"
#include "globals.h"
#include "externs.h"

/* returns 0 if the move fails, 1 if it succeeds */
datum move (datum thing, datum dest)
{
  struct object *t;
  struct object *d;

  if ((t = object (thing)) == NOTHING || (d = object (dest)) == NOTHING)
    return 0;

  /* short-circuit null moves */
  if (t->location == dest)
    return 1;

  /* check authorization */
  /* thing must be either you, controlled by me,
     or in a location controlled by me */
  if (!(thing == you || controls (me, thing)
      || controls (me, thing)
      || controls (me, t->location)))
    return 0;

  /* dest must either be you, controlled by me, or OPEN */
  if (!(dest == you || oflag_set (d, F_OPEN)
      || controls (me, dest)))
    return 0;

  /* can't move a player into a player */
  if (oflag_set (t, F_PLAYER) && oflag_set (d, F_PLAYER))
    return 0;

  /* move is allowed, do it */
  /* remove t from old location */
  take_from_internal (t->location, CONTENTS_NAME, thing);

  /* add t to new location */
  add_to_internal (dest, CONTENTS_NAME, thing);

  /* set location */
  t->location = dest;

  /* and we win */
  return 1;
}