/*
* [U]NITED [G]AMING [C]HEATS Client Hook
*
* Copyright (c) LIQUID     2001-2002
* Copyright (c) ZOOLABUS   2001-2002
* 
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
* 
* GNU license doesn't cover Engine directory.
* Content of Engine directory is copyrighted (c) 1999, 2000, by Valve LLC
* and it is licensed under Valve's proprietary license (see original HL SDK).
*/

#pragma warning (disable:4786) // vc++ <map> bugfix
#include <vector>
#include "playeritems.h"
#include "misc/parsemsg.h"
#include "stringfinder.h"
#include <map>
#include <fstream>

//========================================================================================
typedef map<int,int> MapIntInt;
struct PrivateData{
	MapIntInt     findByID;
	StringFinder  findByName;
};

#define INDEXFIND_BY_ID   (((PrivateData*)my_data)->findByID)
#define INDEXFIND_BY_NAME (((PrivateData*)my_data)->findByName)

//========================================================================================
PlayerItems::PlayerItems()
{
	my_data = new PrivateData;
	slot1Index=-1;
	slot2Index=-1;
	currentSlot=-1;
}

//========================================================================================
PlayerItems::~PlayerItems()
{
    delete (PrivateData*)my_data;
}

//========================================================================================
bool PlayerItems::canSwitch()
{
	if(currentSlot==0){
		if(slot2Index!=-1){
			HlWeaponInfo& Pistol = weaponList[slot2Index];
			return (Pistol.iClip>0);
		}
	}
	return false;
}

//========================================================================================
void PlayerItems::msgWeaponList(int iSize, void* pbuf)
{
	// read
	HlWeaponInfo Weapon;
	BEGIN_READ( pbuf, iSize );
	
	char* name = READ_STRING();
	if(strstr(name,"weapon_")==name) { name += 7;} 
	strcpy( Weapon.szName, name );
	
	Weapon.iAmmoType = (int)READ_CHAR();	
	
	Weapon.iMax1 = READ_BYTE();
	if (Weapon.iMax1 == 255)
		Weapon.iMax1 = -1;

	Weapon.iAmmo2Type = READ_CHAR();
	Weapon.iMax2 = READ_BYTE();
	if (Weapon.iMax2 == 255)
		Weapon.iMax2 = -1;

	Weapon.iSlot = READ_CHAR();
	Weapon.iSlotPos = READ_CHAR();
	Weapon.iId = READ_CHAR();
	Weapon.iFlags = READ_BYTE();
	Weapon.iClip = 0;

	// store
	MapIntInt::iterator foundpos = INDEXFIND_BY_ID.find( Weapon.iId );
	typedef MapIntInt::value_type Entry;
	if(foundpos==(INDEXFIND_BY_ID.end()))
	{
		int index = weaponList.size();
		weaponList.push_back(Weapon);
		INDEXFIND_BY_ID.insert(Entry(Weapon.iId,index));
		INDEXFIND_BY_NAME.add(Weapon.szName,index);
	} else {
		int index = foundpos->second;
		weaponList[index]=Weapon;
	}
}

//========================================================================================
void PlayerItems::msgCurWeapon (int iStat, int iId, int iClip )
{
	if(iStat){ currentSlot = -1; }

	MapIntInt::iterator foundpos = INDEXFIND_BY_ID.find( iId );
	if(foundpos==(INDEXFIND_BY_ID.end())) { return; }
	int index = foundpos->second;

	HlWeaponInfo& Weapon = weaponList[index];
	Weapon.iClip = iClip;
	if(Weapon.iSlot==0){ slot1Index = index; }
	if(Weapon.iSlot==1){ slot2Index = index; }

	if(iStat){ currentSlot = Weapon.iSlot; }
}

//========================================================================================
bool PlayerItems::hasWeapon(const char* name)
{
	bool found = INDEXFIND_BY_NAME.find(name);
	if(!found){ return false; }

	int index = INDEXFIND_BY_NAME.num;
	return (slot1Index==index || slot2Index==index);
}

//========================================================================================
char* PlayerItems::getNamebyId(int id)
{
	MapIntInt::iterator foundpos = INDEXFIND_BY_ID.find( id );
	if(foundpos==(INDEXFIND_BY_ID.end())) { return "none"; }
	int index = foundpos->second;
	return weaponList[index].szName;

}
