#if defined(macintosh)
#include <types.h>
#else
#include <sys/types.h>
#endif
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "emlen.h"

#ifndef NEW_WORLD
bool 
is_boat_empty (SINGLE_OBJECT * boat)
{
  int cnt;
  if (!boat->boat)
    return TRUE;
  for (cnt = 0; cnt < 8; cnt++)
    {
      if (boat->boat->in_boat[cnt] != NULL)
	return FALSE;
    }
  return TRUE;
}

bool 
is_in_same_boat (CHAR_DATA * ch, CHAR_DATA * victim)
{
  int cnt;
  if (!victim->ced || !ch->ced)
    return FALSE;
  if (!IS_IN_BOAT (victim) || !IS_IN_BOAT (ch))
    return FALSE;
  if (!ch->ced->in_boat->boat)
    return FALSE;
  for (cnt = 0; cnt < 8; cnt++)
    {
      if (ch->ced->in_boat->boat->in_boat[cnt] == victim)
	return TRUE;
    }
  return FALSE;
}

void 
char_from_boat (CHAR_DATA * ch, SINGLE_OBJECT * boat)
{
  int cnt;
  if (!ch->ced)
    return;
  if (!boat->boat)
    return;
  for (cnt = 0; cnt < 8; cnt++)
    {
      if (boat->boat->in_boat[cnt] == ch)
	boat->boat->in_boat[cnt] = NULL;
    }
  if (boat->boat->navigator == ch)
    boat->boat->navigator = NULL;
  ch->ced->in_boat = NULL;
  return;
}

void 
char_to_boat (CHAR_DATA * ch, SINGLE_OBJECT * boat)
{
  int cnt;
  if (!boat->boat)
    return;
  check_ced (ch);
  for (cnt = 0; cnt < 8; cnt++)
    {
      if (boat->boat->in_boat[cnt] == NULL)
	{
	  boat->boat->in_boat[cnt] = ch;
	  break;
	}
    }
  ch->ced->in_boat = boat;
  return;
}

bool 
has_boat_space (CHAR_DATA * ch, SINGLE_OBJECT * boat)
{
  int whities;
  int darkies;
  int cnt;
  darkies = 0;
  whities = 0;
  if (!boat->boat)
    return FALSE;
  for (cnt = 0; cnt < 8; cnt++)
    {
      if (boat->boat->in_boat[cnt] != NULL)
	{
	  if (IS_EVIL (boat->boat->in_boat[cnt]))
	    darkies++;
	  if (!IS_EVIL (boat->boat->in_boat[cnt]))
	    whities++;
	}
    }
  if (IS_EVIL (ch) && darkies > 3)
    return FALSE;
  if (!IS_EVIL (ch) && whities > 3)
    return FALSE;
  if (darkies + whities > 7)
    return FALSE;
  if (RIDING (ch) != NULL)
    do_dismount (ch, "");
  return TRUE;
}

void 
send_to_boat (CHAR_DATA * ch, SINGLE_OBJECT * boat, char *to_send)
{
  CHAR_DATA *to_char;
  check_room_more (ch->in_room);
  for (to_char = ch->in_room->more->people; to_char != NULL; to_char = to_char->next_in_room)
    {
      if (to_char->ced && to_char->ced->in_boat != boat)
	continue;
      send_to_char (to_send, to_char);
    }
  return;
}

void 
do_disembark (CHAR_DATA * ch, char *argy)
{
  SINGLE_OBJECT *boat;
  char buffr[500];
  DEFINE_COMMAND ("disembark", do_disembark, POSITION_STANDING, 0, LOG_NORMAL, "Allows you to disembark from a sea-faring vessel.")
#ifdef NEW_WORLD
    do_leave (ch, argy);
  return;
#endif
  if (!ch->ced)
    return;
  if (ch->ced->in_boat == NULL)
    {
      send_to_char ("You aren't in a boat!\n\r", ch);
      return;
    }
  if (ch->in_room->sector_type == SECT_WATER_NOSWIM)
    {
      send_to_char ("You are still in deep, deep water... You can't disembark!\n\r", ch);
      return;
    }
  if (FIGHTING (ch) != NULL)
    {
      if (number_range (1, 2) == 1)
	{
	  send_to_char ("Due to combat, you were unable to disembark the vessel!\n\r", ch);
	  return;
	}
      stop_fighting (ch, TRUE);
    }
  boat = ch->ced->in_boat;
  char_from_boat (ch, ch->ced->in_boat);
  send_to_char ("You disembark from the boat...\n\r", ch);
  sprintf (buffr, "%s disembarks...\n\r", NAME (ch));
  send_to_boat (ch, boat, buffr);
  return;
}



void 
do_board (CHAR_DATA * ch, char *argy)
{
  int cnt;
  SINGLE_OBJECT *obj;
  SINGLE_OBJECT *boat_to_board;
  SINGLE_OBJECT *boat_in_now;
  char which_boat;
  char output[500];
  DEFINE_COMMAND ("board", do_board, POSITION_STANDING, 0, LOG_NORMAL, "Allows you to enter a sea faring vessel.")

    check_ced (ch);
  boat_to_board = NULL;
  boat_in_now = ch->ced->in_boat;
  if (argy[0] >= '1' && argy[0] <= '9')
    {
      which_boat = argy[0] - '1' + 1;
      argy += 2;
    }
  else
    which_boat = 1;
  cnt = 0;
  check_room_more (ch->in_room);
  for (obj = ch->in_room->more->contents; obj != NULL; obj = obj->next_content)
    {
      if (obj == boat_in_now)
	continue;
      if (obj->pIndexData->item_type == ITEM_BOAT && (is_name (argy, obj->pIndexData->name) || argy == "" || argy == NULL || argy[0] == '\0' || argy[0] == '\n'))
	cnt++;
      if (cnt == which_boat)
	{
	  boat_to_board = obj;
	  break;
	}
    }
  if (boat_to_board == NULL)
    {
      if (boat_in_now != NULL)
	{
	  send_to_char ("You are already in a boat, and there is no other boat here!\n\r", ch);
	  return;
	}
      send_to_char ("There is no boat here to board...\n\r", ch);
      return;
    }
  if (!has_boat_space (ch, boat_to_board))
    {
      send_to_char ("There isn't enough room on that vessel to board it!\n\r", ch);
      return;
    }
  if (ch->position == POSITION_BASHED)
    {
      send_to_char ("You are bashed to the ground!\n\r", ch);
      return;
    }
  if (ch->position == POSITION_GROUNDFIGHTING)
    {
      send_to_char ("You are groundfighting!\n\r", ch);
      return;
    }
  if (ch->position == POSITION_FIGHTING && FIGHTING (ch) != NULL)
    {
      if (number_range (1, 2) == 1)
	{
	  send_to_char ("You couldn't manage to get away during combat to board that vessel!\n\r", ch);
	  return;
	}
      send_to_char ("You've managed to board the vessel successfully during combat!\n\r", ch);
      if (FIGHTING (ch) != NULL)
	{
	  send_to_char ("Your opponent has managed to escape and board another vessel!!\n\r", FIGHTING (ch));
	}
      stop_fighting (ch, TRUE);
    }
  if (boat_in_now != NULL)
    {
      sprintf (output, "%s leaves the boat and boards %s!\n\r",
	       NAME (ch),
	       STR (boat_to_board, short_descr));
      char_from_boat (ch, boat_in_now);
      send_to_boat (ch, boat_in_now, output);
    }
  sprintf (output, "%s has boarded the vessel!\n\r", NAME (ch));
  send_to_boat (ch, boat_to_board, output);
  char_to_boat (ch, boat_to_board);
  send_to_char ("You board the vessel!\n\r", ch);
  return;
}
#endif

void 
disengage (CHAR_DATA * ch, char *argy)
{
  DEFINE_COMMAND ("disengage", disengage, POSITION_FIGHTING, 0, LOG_NORMAL, "Allows you to disengage from battle if someone is not directly hitting you.")

    if (FIGHTING (ch) == NULL)
    {
      send_to_char ("You aren't fighting anything or anyone!\n\r", ch);
      return;
    }
  if (ch->fgt && ch->fgt->fighting && ch->fgt->fighting->fgt &&
      ch->fgt->fighting->fgt->fighting == ch)
    {
      send_to_char ("Your opponent is attacking YOU! You can't disengage!\n\r", ch);
      return;
    }
  if (ch->fgt)
    ch->fgt->fighting = NULL;
  ch->position = POSITION_STANDING;
  WAIT_STATE (ch, 4 * PULSE_VIOLENCE);
  return;
}