/*  :ts=8 bk=0
 *
 * misc.c:	Adjuncts to the Marketroid demo.  This is the most vile
 *		and disgusting thing I've ever written....
 *
 * Leo L. Schwab			8710.9		(415) 456-3960
 */
#include <exec/types.h>
#include <graphics/rastport.h>
#include <graphics/gfxmacros.h>
#include "marketroid.h"

extern struct obcontrol	*addobject();
extern struct object	ob_RJ, ob_dale, ob_jay, ob_hero, ob_bike;
			ob_1K, ob_2K, ob_3K, ob_4K, ob_5K;
extern struct object	ob_M, ob_A, ob_R, ob_K, ob_E, ob_T, ob_O, ob_I, ob_D,
			ob_colon;
extern struct object	ob_boing, ob_1987;
extern struct object	ob_amiga;

extern struct BitMap	*sbm;
extern UWORD		colormap[];
extern int		keyx;
extern char		marketroid1987[], *keytext;
extern void		*vp, *smallrobo, *bigrobo;

char realstory [] = "\015 THE \017REAL\015 STORY\033\
BEHIND THE AMIGA";

char save[] = "SAVE THE LAST REAL HACKERS";

char *strings[] = {
	"A PARODY OF THE ROLLING DEMO OF THE WILLIAMS GAME",
	"\"ROBOTRON: 2084\".",
	NULL,
	"SPECIAL THANKS TO JAMES GRAHAM FOR HIS EXPERT ROBOTRON",
	"KNOWLEDGE, AND TO -=RJ MICAL=- FOR USEFUL INFORMATION.",
	NULL,
	"PERPETRATED BY LEO L. SCHWAB",
	NULL,
	"CLICK LEFT MOUSE BUTTON TO EXIT"
};

struct object *letters[] = {
	&ob_M, &ob_A, &ob_R, &ob_K, &ob_E, &ob_T, &ob_R, &ob_O, &ob_I, &ob_D,
	&ob_colon
};

/*  "Save the last real hackers."  */
save_em (rp)
register struct RastPort *rp;
{
	register struct obcontrol	*hero;
	register int			i;
	struct obcontrol		*a, *b, *c, *d, *e;

	openanim ();
	opencycle (vp, colormap, 16);
	standardcycle ();

	SetRast (rp, 0L);
	SetFont (rp, bigrobo);
	standardscreen (rp);
	SetAPen (rp, 1L);
	Move (rp,
	      160 - TextLength (rp,
				marketroid1987,
				(long) strlen (marketroid1987)) / 2,
	      30L);
	Text (rp, marketroid1987, (long) strlen (marketroid1987));

	Move (rp,
	      160 - TextLength (rp, save, (long) strlen (save)) / 2, 110L);
	Text (rp, save, (long) strlen (save));

	hero = addobject (&ob_hero, 0, LEFT, 250, 60);
	a = addobject (&ob_RJ, 1, RIGHT, 190, 58);
	b = addobject (&ob_dale, 1, RIGHT, 160, 59);
	c = addobject (&ob_jay, 1, RIGHT, 130, 59);
	d = addobject (&ob_dale, 1, RIGHT, 100, 59);
	e = addobject (&ob_jay, 1, RIGHT, 70, 59);
	while (hero->x > a->x) {
		animate ();  WaitTOF ();
	}
	removeobject (a);
	a = addobject (&ob_1K, 1, UP, hero->x, hero->y + 4);
	while (hero->x > b->x) {
		animate ();  WaitTOF ();
	}
	removeobject (b);
	b = addobject (&ob_2K, 1, UP, hero->x, hero->y + 4);
	while (hero->x > c->x) {
		animate ();  WaitTOF ();
	}
	removeobject (c);
	c = addobject (&ob_3K, 1, UP, hero->x, hero->y + 4);
	removeobject (a);
	while (hero->x > d->x) {
		animate ();  WaitTOF ();
	}
	removeobject (d);
	d = addobject (&ob_4K, 1, UP, hero->x, hero->y + 4);
	removeobject (b);
	while (hero->x > e->x) {
		animate ();  WaitTOF ();
	}
	removeobject (e);
	e = addobject (&ob_5K, 1, UP, hero->x, hero->y + 4);
	removeobject (c);

	for (i=0; i<30; i++) {
		animate ();  WaitTOF ();
	}
	removeobject (d);
	for (i=0; i<30; i++) {
		animate ();  WaitTOF ();
	}

	closeanim ();
	closecycle ();
}

/*  The main Marketroid credits screen  */
market (rp)
register struct RastPort *rp;
{
	register struct obcontrol	*oc;
	register int			i, n, k;
	UWORD				specialmap[16];

	CopyMem (colormap, specialmap, (long) sizeof (specialmap));
	specialmap[15] = 0xf00;
	specialmap[10] = 0xff0;
	LoadRGB4 (vp, specialmap, 16L);

	openanim ();
	SetRast (rp, 0L);

	for (i=0, n=0; i<11; i++) {
		n += letters[i] -> width;
		if (i != 10)
			n += 3;
	}
	n = 160 - n/2;
	for (i=0; i<11; i++) {
		oc = addobject (letters[i], 1, UP, n, 40);
		oc -> flags |= EXPLODING;
		oc -> frame = 11;
		oc -> dir = -1;
		oc -> delay = 1;
		n += letters[i] -> width + 3;
		for (k=0; k<8; k++) {
			animate ();  WaitTOF ();
			fixexplode ();
		}
	}
	while (oc -> frame > 1) {
		animate ();  WaitTOF ();
		fixexplode ();
	}

	opencycle (vp, specialmap, 16);
	standardcycle ();
	BltBitMap (ob_1987.bitmap, 0L, 0L,
		   sbm, 160L - ob_1987.width/2, 65L,
		   (long) ob_1987.width, (long) ob_1987.height,
		   0xc0L, 0xffL, NULL);
	dotext (realstory, 96, 105);
	for (i=0; i<7; i++) {
		BltBitMap (ob_boing.bitmap, 0L, 0L,
			   sbm, i * 42L, 0L,
			   (long) ob_boing.width, (long) ob_boing.height,
			   0xc0L, i + 1L, NULL);
		Delay (5L);
	}
	for (i=0; i<7; i++) {
		BltBitMap (ob_boing.bitmap, 0L, 0L,
			   sbm, 294L, i * 25L,
			   (long) ob_boing.width, (long) ob_boing.height,
			   0xc0L, i + 1L, NULL);
		Delay (5L);
	}
	for (i=7; i>0; i--) {
		BltBitMap (ob_boing.bitmap, 0L, 0L,
			   sbm, i * 42L, 175L,
			   (long) ob_boing.width, (long) ob_boing.height,
			   0xc0L, 8L - i, NULL);
		Delay (5L);
	}
	for (i=7; i>0; i--) {
		BltBitMap (ob_boing.bitmap, 0L, 0L,
			   sbm, 0L, i * 25L,
			   (long) ob_boing.width, (long) ob_boing.height,
			   0xc0L, 8L - i, NULL);
		Delay (5L);
	}

	waittext ();
	addcycle (CYCLE_SIMPLE, NULL, 0, 1, 7, 4);
	SetAPen (rp, 12L);
	SetFont (rp, smallrobo);
	for (i=0, n=125; i<10; i++, n += 7) {
		if (!strings[i])
			n -= 3;
		else {
/*- - - - - - - - - - -*/
Move (rp,
      160 - TextLength (rp, strings[i], (long) strlen (strings[i])) / 2,
      (long) n);
Text (rp, strings[i], (long) strlen (strings[i]));
/*- - - - - - - - - - -*/
		}
	}
	Delay (500L);

	closeanim ();
	closecycle ();
}

fixexplode ()
{
	extern struct List		guys;
	register struct obcontrol	*oc;

	for (oc = (struct obcontrol *) guys.lh_Head;
	     oc -> node.ln_Succ;
	     oc = (struct obcontrol *) oc -> node.ln_Succ)
		if (oc -> flags & EXPLODING && oc->frame == 1) {
			oc -> flags &= ~EXPLODING;
			oc -> flags |= FREEZE;
		}
}

static UWORD amymap[] = {
	0x000, 0xfff, 0xff0, 0xf80, 0xf00, 0xf0f, 0x0f0, 0x00f,
	0xf00, 0xfb0, 0x7f0, 0x0f5, 0x0df, 0x01f, 0xb0f, 0xf08,
};

/*  Amiga.  No bullshit, just excellence.  */
amy (rp)
register struct RastPort *rp;
{
	register int	x, ax, i;
	long		bbx, bsx, bwide;
	long		abx, asx, awide;

	LoadRGB4 (vp, amymap, 16L);
	SetRast (rp, 0L);
	SetDrPt (rp, 0xaaaaL);
	for (i=98; i>=0; i--) {
		SetAPen (rp, 7L - (i & 7) + 8);
		rp -> Flags |= FRST_DOT;
		Move (rp, (long) i, (long) i);
		Draw (rp, 318L - i, (long) i);
		Draw (rp, 318L - i, 198L - i);
		Draw (rp, (long) i, 198L - i);
		Draw (rp, (long) i, (long) i);
	}
	SetDrPt (rp, -1L);
	SetAPen (rp, 0L);
	for (i=98; i>=20; i--) {
		Move (rp, (long) i, (long) i);
		Draw (rp, 318L - i, (long) i);
		Draw (rp, 318L - i, 198L - i);
		Draw (rp, (long) i, 198L - i);
		Draw (rp, (long) i, (long) i);
	}

	i = 0;
	for (x=-ob_bike.width+20, ax=x-190; x<300; x++) {
		if (x<20) {
			bbx = 20 - x;
			bwide = ob_bike.width - bbx;
			bsx = 20;
		} else if (x + ob_bike.width > 299) {
			bbx = 0;
			bwide = 299 - x;
			bsx = x;
		} else {
			bbx = 0;
			bwide = ob_bike.width;
			bsx = x;
		}
		if (ax<20) {
			abx = 20 - ax;
			awide = ob_amiga.width - abx;
			asx = 20;
		} else {
			abx = 0;
			awide = ob_amiga.width;
			asx = ax;
		}
		i = ob_bike.animseq[x / 4 % ob_bike.nframes];
		if (bwide > 0)
			BltBitMap (ob_bike.bitmap, bbx, (long) i,
				   sbm, bsx, 90L,
				   bwide, (long) ob_bike.height,
				   0xc0L, 0xffL, NULL);
		if (awide > 0 && ax < 90)
			BltBitMap (ob_amiga.bitmap, abx, 0L,
				   sbm, asx, 84L,
				   awide, (long) ob_amiga.height,
				   0xc0L, 1L, NULL);
		WaitBOVP (vp);
		if (bwide > 0)
			BltBitMap (ob_bike.bitmap, 0L, 0L,
				   sbm, bsx, 90L,
				   bwide, (long) ob_bike.height,
				   0L, 0xffL, NULL);
		if (awide > 0 && ax < 89)
			BltBitMap (ob_amiga.bitmap, 0L, 0L,
				   sbm, asx, 84L,
				   awide, (long) ob_amiga.height,
				   0L, 1L, NULL);
		if (ax < 100)
			ax++;
	}

	opencycle (vp, amymap, 16);
	addcycle (CYCLE_SIMPLE, NULL, 0, 8, 15, 6);
	dotext (keytext, keyx, 140);
	for (i=0; i<20; i++) {
		SetAPen (rp, (i & 7) + 8L);
		x = 95 + i;
		Move (rp, 35L + i, (long) x);
		Draw (rp, 42L + i, (long) x);
		Move (rp, 45L + i, (long) x);
		Draw (rp, 52L + i, (long) x);
		Delay (1L);
	}
	for (i=0; i<60; i++) {
		SetAPen (rp, (i & 7) + 8L);
		x = 115 - i;
		Move (rp, 55L + i, (long) x);
		Draw (rp, 62L + i, (long) x);
		Move (rp, 65L + i, (long) x);
		Draw (rp, 72L + i, (long) x);
		Delay (1L);
	}
	waittext ();
	Delay (400L);

	closecycle ();
}


/*  Dedication, followed by "powerup"  */
char *dedication[] = {
	"Dedicated to the wonderful people",
	"from Amiga Los Gatos."
};

coverpage (rp)
register struct RastPort *rp;
{
	register int	i, n, k;

	SetAPen (rp, 1L);
	for (i=0; i<2; i++) {
		Move (rp,
		      160 - TextLength (rp,
					dedication[i],
					(long) strlen (dedication[i])) / 2,
		      80L + i*10);
		Text (rp, dedication[i], (long) strlen (dedication[i]));
	}
	Delay (400L);

	SetAPen (rp, 15L);
	for (i=0; i<2; i++) {
		for (n=0; n<320; n++)
			for (rp->Mask = 1; rp->Mask < 16; rp->Mask <<= 1) {
				SetDrPt (rp, rnd (0x7ffe) + (rnd (2) << 15));
				Move (rp, (long) n, 0L);
				Draw (rp, (long) n, 199L);
			}
		Delay (100L);
	}
	rp -> Mask = 0xff;
	SetRast (rp, 0L);
	SetAPen (rp, 1L);
	SetDrPt (rp, -1L);
	SetFont (rp, bigrobo);
	Move (rp, 68L, 60L);
	Text (rp, "INITIAL TESTS INDICATE:", 23L);
	Move (rp, 116L, 90L);
	Text (rp, "OPERATIONAL", 11L);
	Delay (200L);
}
