/*********************************************************************//**
*	Ammo definition.
*	Jednoducha databaze nazvu a indexu naboju
*
*	author: Michal Jirous
*	date: 24.11.2008
*	file: ammo_def.cpp
**********************************************************************/

#include "ammo_def.h"
#include "mathematic.h"
#include <map>

using namespace std;

map<string,unsigned int> ammoTranslationMap;

/** @param name Jmeno naboje.
*	@return Typ naboje. */
unsigned int getAmmoId( std::string name )
{
	map<string, unsigned int>::iterator iter = ammoTranslationMap.find( name );
	if( iter != ammoTranslationMap.end() )
		return iter->second;
	return NO_AMMO;
}

/*
*	BASE AMMO
*/
AmmoElement::AmmoElement()
{
	amount = 0;
	pLimits = NULL;
}

/** @param type Ciselny index naboje. */
AmmoElement::AmmoElement( unsigned int type )
{
	setType( type );
}

/** @param type Ciselny index naboje. */
void AmmoElement::setType( unsigned int type )
{
	type %= AMMO_COUNT;	//kontrola spravnosti indexu

	amount = 0;
	ammo_names[type];

	pLimits = gamevarsLibrary::getVariable( max_ammos[type] );
	ammoTranslationMap.insert( make_pair( ammo_names[type], type ) );
}

/** @param new_amount Nova hodnota mnozstvi naboju. */
void AmmoElement::setAmount( unsigned int new_amount )
{
	if( pLimits )
	{
		unsigned int maximum = (unsigned int)gamevarsLibrary::getiData( pLimits );

		if( new_amount > maximum )
			new_amount = maximum;
	}
	amount = new_amount;
}

/** @return Mnozstvi nahoju. */
unsigned int AmmoElement::getAmount()
{
	return amount;
}

/** @param add_amount Pocet pridavanych naboju. */
void AmmoElement::addAmmo( unsigned int add_amount )
{
	setAmount( amount + add_amount );
}

/** @param remove_amount Odebirane mnozstvi naboju. */
void AmmoElement::removeAmmo( unsigned int remove_amount )
{
	if( remove_amount > amount )
		setAmount( 0 );
	else
		setAmount( amount - remove_amount );
}




