//=======================================================================
void CWeaponAttachmentManager::CreatePlayerProjectedAttachments()
{

	Vec3 c4FrontPos(-0.0105f,0.2233f,1.297f),c4BackPos(0.00281f,-0.2493f,1.325f);
	Quat c4FrontRot(-0.0368f,-0.0278f,0.0783f,-0.9958f),c4BackRot(1,0,0,0);
	//Creates on init c4 face attachments
	if (ICharacterInstance* pCharInstance = m_pOwner->GetEntity()->GetCharacter(0))
	{
		IAttachmentManager* pAttachmentManager = pCharInstance->GetIAttachmentManager(); 
		IAttachment *pAttachment = NULL;
		pAttachment = pAttachmentManager->GetInterfaceByName("c4_front");
		if(!pAttachment)
		{
			//Attachment doesn't exist, create it
			pAttachment= pAttachmentManager->CreateAttachment("c4_front",CA_FACE,0);
			if(pAttachment)
			{
				pAttachment->SetAttAbsoluteDefault( QuatT(c4FrontRot,c4FrontPos) );
				pAttachment->ProjectAttachment();
			}
		}
		pAttachment = NULL;
		pAttachment = pAttachmentManager->GetInterfaceByName("c4_back");
		if(!pAttachment)
		{
			//Attachment doesn't exist, create it
			pAttachment= pAttachmentManager->CreateAttachment("c4_back",CA_FACE,0);
			if(pAttachment)
			{
				pAttachment->SetAttAbsoluteDefault( QuatT(c4BackRot,c4BackPos) );
				pAttachment->ProjectAttachment();
			}
		}
	}
}
//======================================================================
void CWeaponAttachmentManager::CreatePlayerBoneAttachments()
{
		
	if (ICharacterInstance* pCharInstance = m_pOwner->GetEntity()->GetCharacter(0))
	{
		IAttachmentManager* pAttachmentManager = pCharInstance->GetIAttachmentManager(); 
		IAttachment *pAttachment = NULL;
		
		for(int i=0; i<MAX_WEAPON_ATTACHMENTS; i++)
		{
			pAttachment = pAttachmentManager->GetInterfaceByName(gAttachmentTable[i]);
			if(!pAttachment)
			{
				//Attachment doesn't exist, create it
				pAttachment = pAttachmentManager->CreateAttachment(gAttachmentTable[i],CA_BONE,gBoneTable[i]);
				if(pAttachment)
				{
					m_boneAttachmentMap.insert(TBoneAttachmentMap::value_type(gAttachmentTable[i],0));
					if(pAttachment && !gOffsetTable[i].IsZero())
					{
						pAttachment->SetAttAbsoluteDefault( QuatT(gRotationTable[i],gOffsetTable[i]) );
						pAttachment->ProjectAttachment();
					}
				}
			}
		}

	}

}
//======================================================================
void CWeaponAttachmentManager::CreatePlayerBoneAttachments()
{
	if (ICharacterInstance *pCharInstance = m_pOwner->GetEntity()->GetCharacter(0))
	{
		IAttachmentManager *pAttachmentManager = pCharInstance->GetIAttachmentManager();

		if(pAttachmentManager == NULL)
		{
			return;
		}

		const XmlNodeRef rootNode = gEnv->pSystem->LoadXmlFromFile( WEAPON_ATTACHMENTS_FILE );

		if (!rootNode || strcmpi(rootNode->getTag(), "WeaponAttachments"))
		{
			CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_WARNING, "Could not load Weapon Attachments data. Invalid XML file '%s'! ", WEAPON_ATTACHMENTS_FILE);
			return;
		}

		IAttachment *pAttachment = NULL;
		const int childCount = rootNode->getChildCount();

		for (int i = 0; i < childCount; ++i)
		{
			XmlNodeRef weaponAttachmentNode = rootNode->getChild(i);

			if(weaponAttachmentNode == (IXmlNode *)NULL)
			{
				continue;
			}

			const char *attachmentName = "";
			weaponAttachmentNode->getAttr("name", &attachmentName);

			if(!strcmp(attachmentName, ""))
			{
				CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_WARNING, "Empty Weapon Attachment name in file: %s! Skipping Weapon Attachment.", WEAPON_ATTACHMENTS_FILE);
				continue;
			}

			pAttachment = pAttachmentManager->GetInterfaceByName(attachmentName);

			if(pAttachment)
			{
				continue;
			}

			//Attachment doesn't exist, create it
			const int weaponAttachmentCount = weaponAttachmentNode->getChildCount();
			const char *boneName = "";
			Vec3 attachmentOffset(ZERO);
			Ang3 attachmentRotation(ZERO);

			for (int a = 0; a < weaponAttachmentCount; ++a)
			{
				const XmlNodeRef childNode = weaponAttachmentNode->getChild(a);

				if(childNode == (IXmlNode *)NULL)
				{
					continue;
				}

				if(!strcmp(childNode->getTag(), "Bone"))
				{
					childNode->getAttr("name", &boneName);
				}
				else if(!strcmp(childNode->getTag(), "Offset"))
				{
					childNode->getAttr("x", attachmentOffset.x);
					childNode->getAttr("y", attachmentOffset.y);
					childNode->getAttr("z", attachmentOffset.z);
				}
				else if(!strcmp(childNode->getTag(), "Rotation"))
				{
					float value = 0.0f;
					childNode->getAttr("x", value);
					attachmentRotation.x = DEG2RAD(value);
					childNode->getAttr("y", value);
					attachmentRotation.y = DEG2RAD(value);
					childNode->getAttr("z", value);
					attachmentRotation.z = DEG2RAD(value);
				}
			}

			const char *attachmentType = "";
			weaponAttachmentNode->getAttr("type", &attachmentType);

			if(!strcmp(attachmentType, ""))
			{
				CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_WARNING, "No Weapon Attachment type assigned! Skipping Weapon Attachment: %s", attachmentName);
				continue;
			}

			const bool isBoneAttachment = !strcmp(attachmentType, "Bone");
			const bool isFaceAttachment = !strcmp(attachmentType, "Face");

			//Bone attachment needs the bone name
			if (!strcmp(boneName, "") && isBoneAttachment)
			{
				CryWarning(VALIDATOR_MODULE_GAME, VALIDATOR_WARNING, "Bone Attachment with no bone name assigned! Skipping Weapon Attachment: %s", attachmentName);
				continue;
			}

			if(isBoneAttachment)
			{
				pAttachment = pAttachmentManager->CreateAttachment(attachmentName, CA_BONE, boneName);
			}
			else if(isFaceAttachment)
			{
				pAttachment = pAttachmentManager->CreateAttachment(attachmentName, CA_FACE, 0);
			}

			if(pAttachment)
			{
				if(isBoneAttachment)
				{
					m_boneAttachmentMap.insert(TBoneAttachmentMap::value_type(attachmentName, 0));
				}

				if(pAttachment && !attachmentOffset.IsZero())
				{
					QuatT attachmentQuat(IDENTITY);
					attachmentQuat.SetRotationXYZ(attachmentRotation, attachmentOffset);
					pAttachment->SetAttAbsoluteDefault( attachmentQuat );
					pAttachment->ProjectAttachment();
				}
			}
		}
	}
}