int ci=0,loopexit=0;
void sellaction(int s){	int n, i, amt, value=0, totgold=0;	P_ITEM pRestock = NULL;	P_ITEM pNoRestock = NULL;	P_ITEM pSellCont = NULL;	if (buffer[s][8]!=0)	{		n=calcCharFromSer(buffer[s][3], buffer[s][4], buffer[s][5], buffer[s][6]);		if (n<0 || n>=cmem) return;		int ci=0,loopexit=0;		P_ITEM pi;		vector<SERIAL> vecContainer = contsp.getData(chars[n].serial);		for ( ci = 0; ci < vecContainer.size(); ci++)		{			pi = FindItemBySerial(vecContainer[ci]);			if (pi->layer==0x1A) pRestock = pi;				// Buy Restock container			else if (pi->layer==0x1B) pNoRestock = pi;		// Buy no restock container			else if (pi->layer==0x1C) pSellCont = pi;		// Sell container		}		// Pre Calculate Total Amount of selling items to STOPS if the items if greater than SELLMAXITEM - Magius(CHE)		int maxsell=0;		i=buffer[s][8];		if (i>256) return;		for (i=0;i<buffer[s][8];i++)		{			amt=ShortFromCharPtr(buffer[s]+9+(6*i)+4);			maxsell+=amt;		}		if (maxsell>SrvParms->sellmaxitem)		{			char tmpmsg[256];			sprintf(tmpmsg,"Sorry %s but i can buy only %i items at time!",chars[currchar[s]].name,SrvParms->sellmaxitem);			npctalkall(n, tmpmsg,0);			return;		}		for (i=0;i<buffer[s][8];i++)		{			P_ITEM pSell=FindItemBySerPtr(buffer[s]+9+(6*i));	// the item to sell			if (!pSell) continue;			amt=ShortFromCharPtr(buffer[s]+9+(6*i)+4);						// player may have taken items out of his bp while the sell menu was up ;-)			if (pSell->amount<amt)			{				npctalkall(n, "Cheating scum! Leave now, before I call the guards!",0);				return;			}			// Search the buy restock Container			P_ITEM join = NULL;			ci=0;			P_ITEM pi;			vector<SERIAL> vecContainer = contsp.getData(pRestock->serial);			for ( ci = 0; ci < vecContainer.size(); ci++)			{				pi = FindItemBySerial(vecContainer[ci]);				if (items_match(pi,pSell))					join = pi;			}			// Search the sell Container to determine the price			ci=0;			vecContainer.clear();			vecContainer = contsp.getData(pSellCont->serial);			for ( ci = 0; ci < vecContainer.size(); ci++)			{				pi = FindItemBySerial(vecContainer[ci]);				if (items_match(pi,pSell))				{					value=pi->value;					value=calcValue(pSell, value);					if (SrvParms->trade_system==1)						value=calcGoodValue(s, pSell, value, 1); // Fixed for adv trade --- by Magius(CHE) 					break;	// let's take the first match				}			}			totgold+=(amt*value);	// add to the bill			if (join != NULL)	// The item goes to the container with restockable items			{				join->amount+=amt;				join->restock-=amt;				pSell->ReduceAmount(amt);			}			else			{				pSell->SetContSerial(pNoRestock->serial);				SndRemoveitem(pSell->serial);				if (pSell->amount!=amt)					Commands->DupeItem(s, pSell, pSell->amount-amt);			}		}		addgold(s, totgold);		goldsfx(s, totgold);	// Dupois, SFX for gold movement	// Added Oct 08, 1998	}	char clearmsg[9];	clearmsg[0]=0x3B;	clearmsg[1]=0x00;	clearmsg[2]=0x08;	clearmsg[3]=buffer[s][3];	clearmsg[4]=buffer[s][4];	clearmsg[5]=buffer[s][5];	clearmsg[6]=buffer[s][6];	clearmsg[7]=0x00;	Xsend(s, clearmsg, 8);}