//=============================================================================
//
// calculate the maximum reachable percentage through assembly with the filled resources
//
float CraftingSession::_calcAverageMalleability()
{
    FilledResources::iterator	filledResIt;
    ManufactureSlots*			manSlots	= mManufacturingSchematic->getManufactureSlots();
    ManufactureSlots::iterator	manIt		= manSlots->begin();
    CraftWeights::iterator		weightIt;

    ManufactureSlot*			manSlot;
    Resource*					resource;
    uint16						resAtt		= 0;
    uint8						slotCount	= 0;

    while(manIt != manSlots->end())
    {
        manSlot		= (*manIt);

        // skip if its a sub component slot
        if(manSlot->mDraftSlot->getType() != 4)
        {
            ++manIt;
            continue;
        }

        // we limit it so that only the same resource can go into one slot, so grab only the first entry
        filledResIt		= manSlot->mFilledResources.begin();

        if(manSlot->mFilledResources.empty())
        {
            //in case we can leave resource slots optionally emptymanSlot->mFilledResources
            ++manIt;
            continue;
        }

        resource		= gResourceManager->getResourceById((*filledResIt).first);

        resAtt			+= resource->getAttribute(ResAttr_MA);

        ++slotCount;
        ++manIt;
    }

    return static_cast<float>(resAtt/slotCount);
}
bool MessageLib::sendDeltasMSCO_7(ManufacturingSchematic* manSchem,PlayerObject* playerObject)
{
	if(!(playerObject->isConnected()))
		return(false);

	Message*							message;
	ManufactureSlots*					manSlots			= manSchem->getManufactureSlots();
	ManufactureSlots::iterator			manSlotIt			= manSlots->begin();

	uint32								slotDataByteCount	= 0;
	//uint32							caByteCount			= 0;
	uint16								elementIndex		= 0;

	// get total size
	while(manSlotIt != manSlots->end())
	{
		slotDataByteCount += 57 + (*manSlotIt)->mFilledResources.size() * 12 + (*manSlotIt)->mDraftSlot->getComponentFile().getLength() + (*manSlotIt)->mDraftSlot->getComponentName().getLength();
		++manSlotIt;
	}
	slotDataByteCount += 3;

	//while(caIt != craftAtts->end())
	//{
	//	caByteCount += 8 ;
	//	++caIt;
	//}

	mMessageFactory->StartMessage();
	mMessageFactory->addUint32(opDeltasMessage);
	mMessageFactory->addUint64(manSchem->getId());
	mMessageFactory->addUint32(opMSCO);
	mMessageFactory->addUint8(7);

	mMessageFactory->addUint32(103 + slotDataByteCount + 3);
	mMessageFactory->addUint16(11);

	// slot description
	mMessageFactory->addUint16(0);
	mMessageFactory->addUint32(manSlots->size());

	mMessageFactory->addUint32(++manSchem->mUpdateCounter[0]);

	manSlotIt = manSlots->begin();

	mMessageFactory->addUint8(3);		//1 for add - 3 for redo
	//mMessageFactory->addUint16(elementIndex);
	mMessageFactory->addUint16(manSlots->size());

	while(manSlotIt != manSlots->end())
	{
		mMessageFactory->addString((*manSlotIt)->mDraftSlot->getComponentFile());
		mMessageFactory->addUint32(0);
		mMessageFactory->addString((*manSlotIt)->mDraftSlot->getComponentName());



		++elementIndex;
		++manSlotIt;
	}

	//
	// SlotContent filled
	// 0 nothing
	// 1 ??
	// 2 item
	// 4 resource
	mMessageFactory->addUint16(1);
	mMessageFactory->addUint32(manSlots->size() + 1);

	mMessageFactory->addUint32(++manSchem->mUpdateCounter[1]);

	manSlotIt		= manSlots->begin();
	elementIndex	= 0;

	while(manSlotIt != manSlots->end())
	{
		mMessageFactory->addUint8(1);
		mMessageFactory->addUint16(elementIndex);

		mMessageFactory->addUint32(0);//(*manSlotIt)->getmFilledIndicator());

		++elementIndex;
		++manSlotIt;
	}

	// the updated slot
	manSlotIt		= manSlots->begin();
	elementIndex	= 0;

	while(manSlotIt != manSlots->end())
	{
		if((*manSlotIt)->mFilled > 0)
			break;

		++elementIndex;
		++manSlotIt;
	}

	mMessageFactory->addUint8(2);
	mMessageFactory->addUint16(elementIndex);

	mMessageFactory->addUint32(static_cast<uint32>((*manSlotIt)->getFilledType()));


	//
	// resources filled
	//
	mMessageFactory->addUint16(2);
	mMessageFactory->addUint32(manSlots->size() + 1);

	mMessageFactory->addUint32(++manSchem->mUpdateCounter[2]);
	//printf("\nsendDeltasMSCO_7 counter 2 : %u",ManSchem->mUpdateCounter[2]);

	manSlotIt		= manSlots->begin();
	elementIndex	= 0;

	while(manSlotIt != manSlots->end())
	{
		mMessageFactory->addUint8(1);
		mMessageFactory->addUint16(elementIndex);

		mMessageFactory->addUint32(0);

		++elementIndex;
		++manSlotIt;
	}

	// the updated slot
	manSlotIt		= manSlots->begin();
	elementIndex	= 0;
	FilledResources::iterator filledResIt;

	while(manSlotIt != manSlots->end())
	{
		filledResIt = (*manSlotIt)->mFilledResources.begin();

		if((*manSlotIt)->mFilledResources.size())
			break;

		++elementIndex;
		++manSlotIt;
	}

	mMessageFactory->addUint8(2);
	mMessageFactory->addUint16(elementIndex);

	mMessageFactory->addUint32((*manSlotIt)->mFilledResources.size());

	while(filledResIt != (*manSlotIt)->mFilledResources.end())
	{
		mMessageFactory->addUint64((*filledResIt).first);
		++filledResIt;
	}



	//
	// resources filled amounts, all lists initially 0
	//
	mMessageFactory->addUint16(3);
	mMessageFactory->addUint32(manSlots->size() + 1);


	mMessageFactory->addUint32(++manSchem->mUpdateCounter[3]);

	manSlotIt		= manSlots->begin();
	elementIndex	= 0;

	while(manSlotIt != manSlots->end())
	{
		mMessageFactory->addUint8(1);
		mMessageFactory->addUint16(elementIndex);

		mMessageFactory->addUint32(0);

		++elementIndex;
		++manSlotIt;
	}

	// the updated slot
	manSlotIt		= manSlots->begin();
	elementIndex	= 0;

	while(manSlotIt != manSlots->end())
	{
		filledResIt = (*manSlotIt)->mFilledResources.begin();

		if((*manSlotIt)->mFilled>0)
			break;

		++elementIndex;
		++manSlotIt;
	}

	mMessageFactory->addUint8(2);
	mMessageFactory->addUint16(elementIndex);

	mMessageFactory->addUint32((*manSlotIt)->mFilledResources.size());

	filledResIt = (*manSlotIt)->mFilledResources.begin();
	while(filledResIt != (*manSlotIt)->mFilledResources.end())
	{
		mMessageFactory->addUint32((*filledResIt).second);
		++filledResIt;
	}

	// slotqualitylist
	mMessageFactory->addUint16(4);
	mMessageFactory->addUint32(manSlots->size());

	mMessageFactory->addUint32(manSchem->mUpdateCounter[4]);

	manSlotIt		= manSlots->begin();
	elementIndex	= 0;

	while(manSlotIt != manSlots->end())
	{
		mMessageFactory->addUint8(1);
		mMessageFactory->addUint16(elementIndex);

		mMessageFactory->addUint32((*manSlotIt)->mUnknown1);

		++manSlotIt;
		++elementIndex;
	}

	// cleanslotlist
	mMessageFactory->addUint16(5);
	mMessageFactory->addUint32(manSlots->size() + 1);

	mMessageFactory->addUint32(++manSchem->mUpdateCounter[5]);

	manSlotIt		= manSlots->begin();
	elementIndex	= 0;

	while(manSlotIt != manSlots->end())
	{
		mMessageFactory->addUint8(1);
		mMessageFactory->addUint16(elementIndex);

		mMessageFactory->addUint32((*manSlotIt)->mUnknown2);

		++manSlotIt;
		++elementIndex;
	}

	// the updated slot
	manSlotIt		= manSlots->begin();
	elementIndex	= 0;

	while(manSlotIt != manSlots->end())
	{
		if((*manSlotIt)->mFilled > 0)
			break;

		++manSlotIt;
		++elementIndex;
	}

	mMessageFactory->addUint8(2);
	mMessageFactory->addUint16(elementIndex);

	mMessageFactory->addUint32(0);


	// slotindexlist
	mMessageFactory->addUint16(6);
	mMessageFactory->addUint32(manSlots->size() * 2);

	mMessageFactory->addUint32(manSchem->mUpdateCounter[6]);

	manSlotIt		= manSlots->begin();
	elementIndex	= 0;

	while(manSlotIt != manSlots->end())
	{
		mMessageFactory->addUint8(1);
		mMessageFactory->addUint16(elementIndex);

		mMessageFactory->addUint32((*manSlotIt)->mUnknown2);

		mMessageFactory->addUint8(2);
		mMessageFactory->addUint16(elementIndex);

		mMessageFactory->addUint32(elementIndex);

		++elementIndex;
		++manSlotIt;
	}

	// unknown flag
	mMessageFactory->addUint16(7);
	mMessageFactory->addUint8(manSchem->getCounter());

	// ready flag
	mMessageFactory->addUint16(20);
	mMessageFactory->addUint8(1);
	mMessageFactory->addUint32(0);

	/*mMessageFactory->addUint64(0);
	mMessageFactory->addUint64(0);
	mMessageFactory->addUint32(0);*/

	message = mMessageFactory->EndMessage();

	(playerObject->getClient())->SendChannelA(message,playerObject->getAccountId(),CR_Client,5);

	return(true);
}
bool MessageLib::sendBaselinesMSCO_7(ManufacturingSchematic* manSchem,PlayerObject* playerObject)
{

	if(!(playerObject->isConnected()))
		return(false);

	Message*							message;
	Message*							finalMessage;

	ManufactureSlots*					manSlots			= manSchem->getManufactureSlots();
	ManufactureSlots::iterator			manSlotIt			= manSlots->begin();

	//ExperimentationProperties*			exProp				= manSchem->getExperimentationProperties();
	//ExperimentationProperties::iterator	epIt				= exProp->begin();

	ExperimentationPropertiesStore::iterator	epStoreIt	= manSchem->expPropStore.begin();

	CustomizationList*					custList			= manSchem->getCustomizationList();
	CustomizationList::iterator			custIt				= custList->begin();

	//start with the messagebody

	mMessageFactory->StartMessage();

	//object count
	mMessageFactory->addUint16(21);

	// slot description
	//SlotNameList
	//		-1-
	mMessageFactory->addUint32(manSlots->size());
	mMessageFactory->addUint32(manSchem->mUpdateCounter[0]);

	manSlotIt = manSlots->begin();

	while(manSlotIt != manSlots->end())
	{
		mMessageFactory->addString((*manSlotIt)->mDraftSlot->getComponentFile());
		mMessageFactory->addUint32(0);
		mMessageFactory->addString((*manSlotIt)->mDraftSlot->getComponentName());

		++manSlotIt;
	}

	// slots filled
	//SlotContentsList
	//		-2-
	mMessageFactory->addUint32(manSlots->size());
	mMessageFactory->addUint32(manSchem->mUpdateCounter[1]);

	manSlotIt = manSlots->begin();

	while(manSlotIt != manSlots->end())
	{
		mMessageFactory->addUint32(0);

		++manSlotIt;
	}

	// resources filled, all lists initially 0
	// IngridientList
	//		-3-
	mMessageFactory->addUint32(manSlots->size());
	mMessageFactory->addUint32(manSchem->mUpdateCounter[2]);
	manSlotIt = manSlots->begin();

	while(manSlotIt != manSlots->end())
	{
		mMessageFactory->addUint32(0);

		++manSlotIt;
	}

	// resources filled amounts, all lists initially 0
	// QuantityList
	//		-4-
	mMessageFactory->addUint32(manSlots->size());
	mMessageFactory->addUint32(manSchem->mUpdateCounter[3]);

	manSlotIt = manSlots->begin();

	while(manSlotIt != manSlots->end())
	{
		mMessageFactory->addUint32(0);

		++manSlotIt;
	}

	// SlotQualityList
	// is not used!!!!!!!!!!!!!! as quality is calculated clientside!
	//		-5-
	mMessageFactory->addUint32(manSlots->size());
	mMessageFactory->addUint32(manSchem->mUpdateCounter[4]);

	manSlotIt = manSlots->begin();

	while(manSlotIt != manSlots->end())
	{
		mMessageFactory->addUint32(0);

		++manSlotIt;
	}

	// CleanSlotList
	//
	//		-6-
	mMessageFactory->addUint32(manSlots->size());
	mMessageFactory->addUint32(manSchem->mUpdateCounter[5]);

	manSlotIt = manSlots->begin();

	while(manSlotIt != manSlots->end())
	{
		mMessageFactory->addUint32((*manSlotIt)->mUnknown2);

		++manSlotIt;
	}

	// SlotIndexList
	//
	//		-7-
	manSchem->mUpdateCounter[6] *= 2;

	mMessageFactory->addUint32(manSlots->size());
	mMessageFactory->addUint32(manSchem->mUpdateCounter[6]);

	manSlotIt = manSlots->begin();

	uint32 index = 0;
	while(manSlotIt != manSlots->end())
	{
		mMessageFactory->addUint32(index);
		index++;

		++manSlotIt;
	}

	// IngredientsCounter
	//flowcontrol - 4 times number of slots for every update
	manSchem->setCounter(manSlots->size() * 4);
	mMessageFactory->addUint8(manSlots->size() * 4);

	// experimentation
	///8
	//we need to send them so that they are shown on assembly!!!
	mMessageFactory->addUint32(manSchem->expPropStore.size());
	mMessageFactory->addUint32(manSchem->mUpdateCounter[8]);

	epStoreIt			= manSchem->expPropStore.begin();

	while(epStoreIt != manSchem->expPropStore.end())
	{
		mMessageFactory->addString(BString("crafting"));
		mMessageFactory->addUint32(0);
		mMessageFactory->addString((*epStoreIt).second->mExpAttributeName);
		++epStoreIt;
	}

	//9
	//CurrentExperimentationValueList

	mMessageFactory->addUint32(manSchem->expPropStore.size());

	//annoyingly the craftattributeslist is still zero at this time so we need to reinitialize them !!!!
	manSchem->mUpdateCounter[9] = manSchem->expPropStore.size();
	mMessageFactory->addUint32(manSchem->mUpdateCounter[9]);

	epStoreIt			= manSchem->expPropStore.begin();

	while(epStoreIt != manSchem->expPropStore.end())
	{
		mMessageFactory->addFloat(0);//(*epStoreIt)->mExpAttributeValue);
		(*epStoreIt).second->mExpAttributeValue = 0;
		(*epStoreIt).second->mExpAttributeValueOld = 0;

		++epStoreIt;
	}

	// 10
	//exp offset leave as 0

	mMessageFactory->addUint32(manSchem->expPropStore.size());
	//annoyingly the craftattributeslist is still zero at this time so we need to reinitialize them !!!!
	manSchem->mUpdateCounter[10] = manSchem->expPropStore.size();
	mMessageFactory->addUint32(manSchem->mUpdateCounter[10]);

	epStoreIt			= manSchem->expPropStore.begin();

	while(epStoreIt != manSchem->expPropStore.end())
	{

		mMessageFactory->addUint32(0);//(*epStoreIt)->mExpUnknown);

		++epStoreIt;
	}

	//11
	// bluebar / exp bar value

	mMessageFactory->addUint32(manSchem->expPropStore.size());
	//annoyingly the craftattributeslist is still zero at this time so we need to reinitialize them !!!!
	manSchem->mUpdateCounter[11] = manSchem->expPropStore.size();
	mMessageFactory->addUint32(manSchem->mUpdateCounter[11]);

	epStoreIt			= manSchem->expPropStore.begin();

	while(epStoreIt != manSchem->expPropStore.end())
	{
		(*epStoreIt).second->mBlueBarSizeOld = 1.0;
		(*epStoreIt).second->mBlueBarSize = 1.0;
		mMessageFactory->addFloat((*epStoreIt).second->mBlueBarSize);//

		++epStoreIt;
	}

	//12 (c)
	// max experimentation
	mMessageFactory->addUint32(manSchem->expPropStore.size());
	//annoyingly the craftattributeslist is still zero at this time so we need to reinitialize them !!!!
	manSchem->mUpdateCounter[12] = manSchem->expPropStore.size();
	mMessageFactory->addUint32(manSchem->mUpdateCounter[12]);

	epStoreIt			= manSchem->expPropStore.begin();

	while(epStoreIt != manSchem->expPropStore.end())
	{

		(*epStoreIt).second->mMaxExpValue = 0;
		(*epStoreIt).second->mMaxExpValueOld = 0;
		mMessageFactory->addFloat(0);//(*epStoreIt)->mMaxExpValue);

		++epStoreIt;
	}

	// 13 customization attribute name (d)
	uint32 custSize = custList->size();
	mMessageFactory->addUint32(custSize);
	mMessageFactory->addUint32(custSize);

	custIt = custList->begin();
	while(custIt != custList->end())
	{
		mMessageFactory->addString((*custIt)->attribute);
		++custIt;
	}


	// 14 customization default color
	mMessageFactory->addUint32(custSize);
	mMessageFactory->addUint32(custSize);

	custIt = custList->begin();
	while(custIt != custList->end())
	{
		mMessageFactory->addUint32((*custIt)->defaultValue);
		++custIt;
	}

	// 15 palette start
	mMessageFactory->addUint32(custSize);
	mMessageFactory->addUint32(custSize);

	custIt = custList->begin();
	while(custIt != custList->end())
	{
		mMessageFactory->addUint32(0);
		++custIt;
	}


	// 16 palette end
	mMessageFactory->addUint32(custSize);
	mMessageFactory->addUint32(custSize);


	custIt = custList->begin();
	while(custIt != custList->end())
	{
		mMessageFactory->addUint32((*custIt)->paletteSize);
		++custIt;
	}

	// 17 customization counter
	mMessageFactory->addUint8(static_cast<uint8>(custSize));

	mMessageFactory->addUint64(0);
	mMessageFactory->addUint32(0);
	mMessageFactory->addUint8(0);
	mMessageFactory->addUint8(1);

	message = mMessageFactory->EndMessage();


	mMessageFactory->StartMessage();
	mMessageFactory->addUint32(opBaselinesMessage);
	mMessageFactory->addUint64(manSchem->getId());
	mMessageFactory->addUint32(opMSCO);
	mMessageFactory->addUint8(7);

	mMessageFactory->addUint32(message->getSize());
	mMessageFactory->addData(message->getData(),message->getSize());

	finalMessage = mMessageFactory->EndMessage();
	message->setPendingDelete(true);

	(playerObject->getClient())->SendChannelA(finalMessage,playerObject->getAccountId(),CR_Client,5);

	return(true);
}