#include "ro.hpp"


/*classe RO
___________________*/

//constructor
ro::ro()
{
	_sp=0;
	_index=0;
}

/*stack fifo*/

//stack push
void ro::push(bot * lui)
{
	if(_sp<20 && lui)
		_swarm[_sp++] = lui;
}

//stack pop
bot * ro::pop()
{
	if (_sp)
		return _swarm[--_sp];
	return 0;
}

/*array operation*/

//array get  desired index
bot * ro::get(int index)
{
    if (index < _sp)
        return _swarm[index];
    return 0;
}

//array remove desired index
bot * ro::remove(int i)
{
    bot * it = _swarm[i];
    _swarm[i] = _swarm[_sp-1];
    _swarm[_sp-1] = it;
    return pop();
}

//array get size
int ro::size()
{
    return _sp;
}

/*iterator loop*/

//iterator peek current position
bot * ro::peek()
{
    if (_index<_sp)
        return _swarm[_index];
    return _swarm[0];
}

//iterator remove current position
bot * ro::peekRemove()
{
    if (_index<_sp)
        return remove(_index);
    return 0;
}

//iterator next
bot * ro::next()
{
	if (_index<_sp)
		return _swarm[_index++];
	return _swarm[0];
}

//iterator test if end
bool ro::end()
{
	return _index>=_sp;
}

//iterator reset
void ro::reset()
{
	_index = 0;
}

/*TRANSBOT
___________________*/

//bouge tout les bots en meme temps
void trans::relMove(int vx,int vy)
{
	//bouge tout les bots de la liste de vx,vy 
	for(int i=0;i<_sp;i++)
		_swarm[i]->relMove(vx,vy);
}

//avec la classe shape,modifie les coordonnés des bots pour avoir une forme
void trans::reshape(shape & form)
{
	//shape est une sorte de tableau de coordonné
    for (int i=0;i<_sp;i++)
        _swarm[i]->absMove(form.col(i), form.row(i));
}

//normalisation de la numérotation des bots
void trans::unite()
{
	//tous les bots de la liste doivent être numéroté dans l'ordre
	for (int i=0; i<_sp; i++)
	{
		if (i<10)
			_swarm[i]->type = i + 49;	//char 1 2 3 4 5 6 7 8 9
		else
			_swarm[i]->type = i + 55;	//char A B C D E F G H I J K
		lastType = _swarm[i]->type;
		if (i == _sp/2)
		    eye = _swarm[i]->type;
		else if (i == (_sp/2)-1)
		    teeth = _swarm[i]->type;
	}
}

/*nautbot
___________________*/

//test de collision entre les bots épparpillés
bool naut::peekCollideWithRel(int vx, int vy)
{
	//si un seul bot partage les memes coordonnés avec les modifications
    for (int i=0;i<_sp;i++)
        if (_index != i && _swarm[_index]->collideWithRel(_swarm[i],vx,vy))
            return true;
    return false;
}

//normalisation de la numérotation
void naut::unite()
{
	//s'assure que tous les bots de la liste sont  des "0"
	for (int i=0; i<_sp; i++)
		_swarm[i]->type = '0';
}

