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

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

void CH::map_area(const short diameter, const bool quiet) {
  short x, y;
  BATTLE_ROOM *room=battle_room, *prev_room=NULL;
  ostrstream ost_map;
  bool beginning=true, was_color=false, colour=false, found=false;

  for(y = 1; y <= diameter / 2; ++y)
    room = room->get_adjacent_room(DIR_N);

  for(x = 1; x <= diameter / 2; ++x)
    room = room->get_adjacent_room(DIR_W);

  for(y = 1; y <= diameter; ++y) {
    for(x = 1; x <= diameter; ++x) {
      colour = false;
      found = false;
      
      if((((abs)(room->x - battle_room->x) > diameter/2+1 || (abs)(room->y - battle_room->y) > diameter/2+1))) {
        ost_map << "  "; 
        colour = true;
      }
      else if(this->battle_room == room) {
        if(temp_action.action && room != orig_room) {
          string str_sector = sector_table[room->sector].print;
          ost_map << (temp_action.action == do_act ? "{2" : "{^") << "{z" << str_sector.substr(2, 2) << "{0";
        }
        else
          ost_map << "{^" << name.substr(0, 2) << "{0";
      }
      else if(!room->occupied(this) && action.target && room == action.target) {
	string str_sector = sector_table[room->sector].print;
        ost_map << "{!{z" << str_sector.substr(2, 2) << "{0";
        colour = true;
      }
      else if(orig_room && room == orig_room) {
        ost_map << "{0" << name.substr(0, 2);
        colour = true;
      }
      else if(!room->occupied(this) && temp_action.action && orig_room->distance(room) <= temp_action.range) {
	string str_sector = sector_table[room->sector].print;
        ost_map << (temp_action.action == do_act ? "{1" : "{b") << str_sector.substr(2, 2);
        colour = true;
      }
      else {
        found = was_color || beginning || prev_room->occupied(this) || room->sector != prev_room->sector || prev_room == this->battle_room;
        ost_map << print_sector(prev_room, room, found);
      }
  
      was_color = colour;
      prev_room = room;
      room = room->get_adjacent_room(DIR_E);
      beginning = false;
    }

    ost_map << CLEAR;

    if(y == 1) {
      ost_map << " Location: " << battle_room->area->name << " " << battle_room->location();

      if(admin)
        ost_map << " [" << area->id << "][" << battle_room->id << "]";
    }
    else if(y == 2)
      ost_map << " Weather: " << battle_room->area->str_weather();

    ost_map << ENDL;
    room = room->get_adjacent_room(DIR_S);
  
    for(x = 1; x <= diameter; ++x)
      room = room->get_adjacent_room(DIR_W);

    beginning = true;
  }   
  
  ost_map << CLEAR << ends;
     
  if(!quiet && !npc)
    printf(ost_map.str());

  return;
}     
      
void CH::print_map(const bool quiet) {
  map_area(MAX(battle_room->area->width, 21), quiet);

  if(!quiet)
    WAIT_STATE(this, 1);
          
  return;
}
        
string CH::print_sector(BATTLE_ROOM *prev_room, BATTLE_ROOM *room, const bool color) {
  CH *ch;
      
  if((ch = room->occupied(this)) && can_see(ch))
    return (ch->group && group && ch->group == group ? "{B" : "{!") + ch->name.substr(0, 2);

  string str_sector = sector_table[room->sector].print;
  return (color ? str_sector : str_sector.substr(2, 2));
}