#include "empire.h"

void check_blocking_base(_unit *u)
{
  _loc d;
  if (friendly_unit_maker_here(u->tilex, u->tiley, u->color) == true)
  {
		if (u->type != LANDER)
    {  //unless the unit's a lander, move off the unit-producing building
			d = a_free_space(u, u->tilex, u->tiley);
      if (d.x != -1)
      {
        action[0].type = action_move;
        action[0].loc = d;
				action[0].destination = d;
      }
    }
    else
    {  //if it's a lander, move to a reachable friendly beach or stay at port
      d = reachable_friendly_beach(u->color, u->number, u->tilex, u->tiley, u->move);
      if (d.x != -1)
      {
        action[0].type = action_move;
        action[0].loc = d;
				action[0].destination = d;
      }
    }
  }
}

void check_low_health(_unit *u)
{
  _loc d;
  if (action[0].type != action_standby)
  {  //if it's attacking something, never mind low health
    return;
  }
  if ((u->health < 7) || (u->gas < 15) || ((u->ammo < 3) && (u->attacktype != -1)))
  {  //if the unit has taken serious damage or is low on gas or ammo (and can attack)
    d = closest_free_building(u->color, u->number, u->movetype, u->move, u->basetype, u->tilex, u->tiley);
    if (d.x != -1)  //if there is a free friendly building, head for it
    {
      if ((d.x != u->tilex) || (d.y != u->tiley))
      {  //if it isn't already sitting on the best place
        action[0].type = action_move;
        action[0].loc = d;
      }
    }
    if (u->health < 5)
    {  //if the unit is very low on health, look for a merge instead
      d = unhealthy_unit_nearby(u, u->tilex, u->tiley);
      if (d.x != -1)
      {
        action[0].type = action_move;
        action[0].loc = d;
        action[1].type = action_merge;
        action[1].loc = d;
      }
    }
  }
}
