/***************************************************************************

  Misfitmame Video support for extra games

***************************************************************************/



/*************************************
 *
 *  Bagmanm2 extensions
 *
 *************************************/

void bagmanm2_extend_tile_info(UINT16 *code, UINT8 *color, UINT8 attrib, UINT8 x)
{
	*code |= gfxbank[0] << 9;
}

void bagmanm2_extend_sprite_info(const UINT8 *base, UINT8 *sx, UINT8 *sy, UINT8 *flipx, UINT8 *flipy, UINT16 *code, UINT8 *color)
{
	*code |= (gfxbank[0] << 7) | 0x40;
}



/*************************************
 *
 *  Four Play extensions
 *
 *************************************/

/* gfxbank[4] is used as a cpu bank number, and gfxbank[0] for graphics banking */ 
WRITE8_HANDLER( fourplay_rombank_w )
{
	gfxbank[4] = (gfxbank[4] & (2 - offset)) | (data << offset);

	gfxbank[0] = (gfxbank[4] == 3);	// 1 = true, 0 = false

	memory_set_bank(1, gfxbank[4] );
}



/*************************************
 *
 *  Video Eight extensions
 *
 *************************************/

void videight_extend_tile_info(UINT16 *code, UINT8 *color, UINT8 attrib, UINT8 x)
{
	*code |= (gfxbank[0] << 8);
	*color |= (gfxbank[4] << 3);
}

void videight_extend_sprite_info(const UINT8 *base, UINT8 *sx, UINT8 *sy, UINT8 *flipx, UINT8 *flipy, UINT16 *code, UINT8 *color)
{
	*code |= (gfxbank[0] << 6);
	*color |= (gfxbank[4] << 3);
}


/* This handles the main bankswitching for code and one-bank gfx games */
WRITE8_HANDLER( videight_rombank_w )
{
	UINT8 gfxbanks[] = { 0, 10, 2, 8, 1, 9, 4, 11 };
	if (offset == 2)
	{
		galaxian_stars_enable_w( machine, 0, data );
	}
	else
	{
		gfxbank[4] = (gfxbank[4] & (6 - offset)) | (data << ((offset + 1) >> 1));
		galaxian_gfxbank_w (machine, 0, gfxbanks[gfxbank[4]]);
		memory_set_bank(1, gfxbank[4] );
	}
}

/* This handles those games with multiple gfx banks */
WRITE8_HANDLER( videight_gfxbank_w )
{
	/* Moon Cresta (mooncrgx) */
	if (( data < 2 ) && (gfxbank[4] == 3))
	{
		UINT8 gfxbanks[] = { 8, 12, 8, 14, 8, 13, 8, 15 };
		if (!offset) gfxbank[3] = (gfxbank[3] & 6) | data;
		if (offset == 1) gfxbank[3] = (gfxbank[3] & 5) | (data << 1);
		if (offset == 2) gfxbank[3] = (gfxbank[3] & 3) | (data << 2);
		galaxian_gfxbank_w( machine, 0, gfxbanks[gfxbank[3]]);
	}

	/* Uniwar S */
	if (( data < 2 ) && (gfxbank[4] == 2) && (offset == 2))
	{
		galaxian_gfxbank_w( machine, 0, data+2 );
	}

	/* Pisces (piscesb) */
	if (( data < 2 ) && (gfxbank[4] == 6) && (offset == 2))
	{
		galaxian_gfxbank_w( machine, 0, data+4 );
	}
}



/*************************************
 *
 *  Galbug extensions
 *
 *************************************/


/* The proper palette for the game is created whenever the selected game changes */
void galbug_gfxbank_w (running_machine *machine, UINT8 gbank )
{
	UINT8 *color_prom = memory_region(machine, "proms");
	static const int rgb_resistances[3] = { 1000, 470, 220 };
	double rweights[3], gweights[3], bweights[2];
	int i, j, minval, midval, maxval;
	UINT8 starmap[4];

	compute_resistor_weights(0,	RGB_MAXIMUM, -1.0,
			3, &rgb_resistances[0], rweights, 470, 0,
			3, &rgb_resistances[0], gweights, 470, 0,
			2, &rgb_resistances[1], bweights, 470, 0);

	/* decode the palette first */
	j = gbank << 5;
	for (i = 0; i < 32; i++)
	{
		UINT8 bit0, bit1, bit2, r, g, b;

		/* red component */
		bit0 = BIT(color_prom[j],0);
		bit1 = BIT(color_prom[j],1);
		bit2 = BIT(color_prom[j],2);
		r = combine_3_weights(rweights, bit0, bit1, bit2);

		/* green component */
		bit0 = BIT(color_prom[j],3);
		bit1 = BIT(color_prom[j],4);
		bit2 = BIT(color_prom[j],5);
		g = combine_3_weights(gweights, bit0, bit1, bit2);

		/* blue component */
		bit0 = BIT(color_prom[j],6);
		bit1 = BIT(color_prom[j],7);
		b = combine_2_weights(bweights, bit0, bit1);

		palette_set_color(machine, i, MAKE_RGB(r,g,b));
		j++;
	}

	minval = RGB_MAXIMUM * 130 / 150;
	midval = RGB_MAXIMUM * 130 / 100;
	maxval = RGB_MAXIMUM * 130 / 60;

	/* compute the values for each of 4 possible star values */
	starmap[0] = 0;
	starmap[1] = minval;
	starmap[2] = minval + (255 - minval) * (midval - minval) / (maxval - minval);
	starmap[3] = 255;

	/* generate the colors for the stars */
	for (i = 0; i < 64; i++)
	{
		UINT8 bit0, bit1, r, g, b;

		/* bit 5 = red @ 150 Ohm, bit 4 = red @ 100 Ohm */
		bit0 = BIT(i,5);
		bit1 = BIT(i,4);
		r = starmap[(bit1 << 1) | bit0];

		/* bit 3 = green @ 150 Ohm, bit 2 = green @ 100 Ohm */
		bit0 = BIT(i,3);
		bit1 = BIT(i,2);
		g = starmap[(bit1 << 1) | bit0];

		/* bit 1 = blue @ 150 Ohm, bit 0 = blue @ 100 Ohm */
		bit0 = BIT(i,1);
		bit1 = BIT(i,0);
		b = starmap[(bit1 << 1) | bit0];

		/* set the RGB color */
		star_color[i] = MAKE_RGB(r, g, b);
	}

	/* default bullet colors are white for the first 7, and yellow for the last one */
	for (i = 0; i < 7; i++)
		bullet_color[i] = MAKE_RGB(0xff,0xff,0xff);
	bullet_color[7] = MAKE_RGB(0xff,0xff,0x00);

	galaxian_gfxbank_w ( machine, 0, gbank );
}

PALETTE_INIT( galbug )
{
	/* doesn't do anything, but it prevents PALETTE_INIT(galaxian) from running */
}

