void TranBucketMan::Flush( Bool doDraw)  // = TRUE
{ 
  if (!doDraw)
	{
		BucketMan::Flush(FALSE);
		return;
	}

  Vid::SetZWriteState( FALSE);
  Bool alpha = Vid::SetAlphaState( TRUE);

	// figure out how many buckets actually have vertices in them
	// so that we don't have to look at any empty buckets

	NList<Bucket>::Iterator li(&bucketList); 
  Bucket * list[444];
  S32 count = 0;
  while (Bucket * bucket = li++)
  {
    if (bucket->vCount)
    {
      list[count] = bucket;
      count++;

      ASSERT( count <= 444);
    }
#ifdef DOLASTBUCKET
    if (bucket == lastUsedBucket)
    {
      break;
    }
#endif
  }

	// for each bucket that is not empty...
	for (count--; count >= 0; count--)
	{
		Bucket *max = list[count];
  
		// find the bucket with the maximum z value
    S32 i, current = count;
		for (i = count - 1; i >= 0; i--)
		{
			if (list[i]->tag > max->tag)
			{
				max = list[i];
        current = i;
			}
/*
			else if (list[i]->tag == max->tag)
      {
        if (list[i]->tag1 > max->tag1)
        {
	  			max = list[i];
          current = i;
        }
      }
*/
		}

    if (!Vid::caps.noTransort && !(max->flags & RS_NOSORT) && primitive.primitive_type != PT_LINELIST)
    {
      max->Sort();
    }
		// flush the bucket with the maximum z value
    if ((max->flags & RS_DST_MASK) == RS_DST_ONE)
    {
      Vid::SetFogColorD3D( 0);

  		FlushBucket( *max);

      Vid::SetFogColorD3D( Vid::renderState.fogColor);
    }
    else
    {
  		FlushBucket( *max);
    }

    max->Reset();

    // move end of list to current position 
    //
    list[current] = list[count];
	}


  Vid::SetAlphaState( alpha);
  Vid::SetZWriteState( TRUE);

  curMem  = memBlock;
  curSize = memSize;

  currentBucket = NULL;
  lastUsedBucket = NULL;
}
void TranBucketMan::FlushTex( const Bitmap * texture, Bool doDraw)  // = TRUE
{ 
  if (!doDraw)
	{
		BucketMan::Flush(FALSE);
		return;
	}

  Vid::SetZWriteState( FALSE);
  Bool alpha = Vid::SetAlphaState( TRUE);

#if 1

  NList<Bucket>::Iterator li(&bucketList); 
  for (!li; *li; li++)
	{
    Bucket *bucket = *li;

    if (bucket->texture_count && bucket->textureStages[0].texture == texture && bucket->vCount)
    {

		  // flush the bucket with the maximum z value
      if ((bucket->flags & RS_DST_MASK) == RS_DST_ONE)
      {
        Vid::SetFogColorD3D( 0);

  		  FlushBucket( *bucket);

        Vid::SetFogColorD3D( Vid::renderState.fogColor);
      }
      else
      {
  		  FlushBucket( *bucket);
      }
    }
    bucket->Reset();
	}

  currentBucket = NULL;

#else

	// figure out how many buckets actually have vertices in them
	// so that we don't have to look at any empty buckets
	NList<Bucket>::Iterator li(&bucketList); 
  Bucket *list[222];
  S32 count = 0;
#if 0
  S32 ic = 0;
	NList<Bucket>::Node *lnode = NULL;
  NList<Bucket>::Node *llnode = NULL;
#endif
  for (!li; *li; li++)
  {

		if ((*li)->vCount)
		{
      list[count] = *li;
      count++;
		}
	}

	// for each bucket that is not empty...
	for (count--; count >= 0; count--)
	{
		Bucket *max = list[count];
  
		// find the bucket with the maximum z value
    S32 i, current = count;
		for (i = count - 1; i >= 0; i--)
		{
			if (list[i]->tag > max->tag)
			{
				max = list[i];
        current = i;
			}
		}

    if (!Vid::caps.notransort && !(max->flags & RS_NOSORT))
    {
      max->Sort();
    }


		// flush the bucket with the maximum z value
    if ((max->flags & RS_DST_MASK) == RS_DST_ONE)
    {
      Vid::SetFogColorD3D( 0);

  		FlushBucket( *max);

      Vid::SetFogColorD3D( Vid::renderState.fogColor);
    }
    else
    {
  		FlushBucket( *max);
    }
    max->Reset();

    // move end of list to current position 
    //
    list[current] = list[count];
	}
#endif

  Vid::SetAlphaState( alpha);
  Vid::SetZWriteState( TRUE);

  curMem  = memBlock;
  curSize = memSize;
  currentBucket = NULL;
}