/**************************************************************
 * FFTacticsMUD : room.cpp                                    *
 **************************************************************
 * (c) 2002 Damien Dailidenas (Trenton). All rights reserved. *
 **************************************************************/

#include "main.h"
#include <strstream>
#include <iomanip>

short distance(const int x1, const int y1, const int x2, const int y2) {
  return (abs)(x2 - x1) + (abs)(y2 - y1);
}

short BATTLE_ROOM::distance(const BATTLE_ROOM *room) {
  return (abs)(room->x - x) + (abs)(room->y - y);
}

short BATTLE_ROOM::distance(const CH *ch) {
  return (abs)(ch->battle_room->x - x) + (abs)(ch->battle_room->y - y);
}

BATTLE_ROOM *BATTLE_ROOM::get_adjacent_room(const short door) {
  long id = 0, size = area->width * area->height;
  
  if(door == DIR_N)       id = this->id - area->width;
  else if(door == DIR_S)  id = this->id + area->width;
  else if(door == DIR_E)  id = this->id + 1;
  else if(door == DIR_W)  id = this->id - 1;
  
  if(id > size)
    id -= size;
  else if(id < 1)
    id += size;
  
  return area->room[id];
}

string BATTLE_ROOM::location() {
  ostrstream ost;
  ost << (y > area->height / 2 ? "S" : y < area->height / 2 ? "N" : " ") << (x > area->width / 2 ? "E" : x < area->width / 2 ? "W" : " ") << x << "." << y << ends;
  return ost.str();
}

OBJ *BATTLE_ROOM::obj(const string name) {
  for(OBJ *obj = objects; obj; obj = obj->next)
    if(find(name, obj->name()))
      return obj;
 
  return NULL;
}

OBJ *BATTLE_ROOM::obj(const short id) {
  short cnt = 0;

  for(OBJ *obj = objects; obj; obj = obj->next)
    if(++cnt == id)
      return obj;
  
  return NULL;
}

void ROOM::echo(const string str, CH *ch = NULL, CH *ch2 = NULL) {
  for(CH *pers = people; pers; pers = pers->next_in_room) {
    if(!pers->desc || (ch && ch == pers) || (ch2 && ch2 == pers))
      continue;

    pers->printf(str);
  }

  return;
}

void BATTLE_ROOM::print_contents(CH *ch) {
  CH *ch2;

  if((ch2=occupied(ch))) {
    ch->printf("%s  %s  Lv.%02d Exp.%02d\n\r", ch2->name.c_str(), !ch2->group || !ch->group || ch2->group != ch->group ? "Enemy" : "{0", ch2->lvl, ch2->exp);
    ch->printf("  Hp %s %03d/%03d  Brave %d\n\r", str_bar(ch2->HP[0], ch2->HP[1], "{^").c_str(), ch2->HP[0], ch2->HP[1], ch2->Br);
    ch->printf("  Mp %s %03d/%03d  Faith %d\n\r", str_bar(ch2->MP[0], ch2->MP[1], "{#").c_str(), ch2->MP[0], ch2->MP[1], ch2->Fa);
    ch->printf("  CT %s %03d/%03d\n\r", str_bar(ch2->CT, 100, "{@").c_str(), MAX(ch2->CT, 100), 100);
    return;
  }

  return;
}

void ROOM::print_contents(CH *ch) {
  if(people)
    for(CH *ch2 = people; ch2; ch2 = ch2->next_in_room)
      if(ch2 != ch)
        ch->printf(ch2->name + " is here.\n\r");

  return;
}

void BATTLE_ROOM::stat(CH *ch) {
  return;
}

CH *ROOM::ch(const string name, CH *ch = NULL) {
  for(CH *ch2 = people; ch2; ch2 = ch2->next_in_room)
    if(find(name, ch2->name) && (!ch || (ch->can_see(ch2))))
      return ch2;

  return NULL;
}

CH *BATTLE_ROOM::occupied(CH *ch) {
  for(CH *ch2 = people; ch2; ch2 = ch2->next_in_room)
    if(ch->can_see(ch2))
      return ch2;

  return NULL;
}

string ROOM::name() {
  if(id == ROOM_OUTSIDE)
    return "Outside";
  else if(id == ROOM_BAR)
    return "Bar";
  else if(id == ROOM_SHOP)
    return "Shop";
  else
    return "Fur Shop";
}

string ROOM::description() {
  if(id == ROOM_OUTSIDE)
    return (string)area_table[area->id].description;
  else
    return "Description goes here.\n\r";
}