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

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

void CH::map_area(const bool quiet) {
  BATTLE_ROOM *room=battle_room, *prev_room=NULL;
  BATTLE_AREA *area=battle_room->area;
  ostrstream ost_map;
  bool beginning=true, was_color=false, colour=false, found=false;

  for(int x = 1; x <= area->size(); ++x) {
    colour = false;
    found = false;
    room = area->room[x];

    if(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;
    beginning = false;

    if(x % area->width == 0) {
      ost_map << endl;
      beginning = true;
    }
  }

  ost_map << CLEAR << ends;

  if(!quiet && !npc)
    printf(ost_map.str());

  return;
}     
      
void CH::print_map(const bool quiet) {
  map_area(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));
}