/* Lupengo			*
 * by Carlo Borreo		*
 * email: borreo@softhome.net	*
 * License: GPL			*
 * Started writing: 10-Aug-1987	*
 * First released:  01-May-2000 */

_main()
{
main();
}

extern struct NewWindow nw;
extern int demoplayers;

char *picsarr;
char chip ptrshape[16];
int screen[BX1+3][BY1+3];
struct Window *w;
struct Screen *s;
struct RastPort *rp;
int joyx, joyy, button, lives, level, eggs, score, nextwin;
int players, writingdemo, trisdone, playspeed, listos;
int countdown, trainer, flash, players;
jmp_buf jmpsave;
int enemyx[ENEMIES+1], enemyy[ENEMIES+1], eggx[MAXEGG+1];
int eggy[MAXEGG+1], slp[ENEMIES+1];
int myx[2], myy[2], living[2], agony[2];
int black[4];
struct topitem top[TOPS+1][2];

extern struct Library *IntuitionBase, *GfxBase, *DiskfontBase, *SysBase;
struct DosLibrary *DOSBase;
struct TextAttr ta	={ "ruby.font", 8, 0, 0 };
struct TextAttr bigta	={ "big.font", 20, 0, 0 };
struct TextFont *font, *bigfont;

struct NewScreen ns=
{
0,0,640,200,DEPTH,
0,1,
HIRES,
CUSTOMSCREEN,
&ta,
NULL,
NULL,
NULL,
};

struct BitMap bm[NPICS];

struct coord { short x; unsigned char y; } titletab[]={
30,1,
29,4,
29,6,
28,7,
27,9,
27,10,
26,13,
24,15,
24,17,
23,20,
22,22,
22,24,
21,25,
21,27,
20,29,
20,31,
20,33,
19,35,
17,38,
16,39,
14,41,
13,43,
12,44,
10,47,
9,49,
7,51,
6,52,
5,54,
4,57,
3,59,
2,60,
2,61,
2,63,
3,65,
4,65,
6,66,
10,67,
12,68,
14,68,
16,68,
17,69,
21,69,
23,69,
28,69,
32,69,
36,68,
41,68,
45,68,
52,67,
55,67,
61,67,
66,67,
71,67,
74,67,
74,66,
74,66,
74,66,
75,66,
109,30,
109,32,
109,34,
109,37,
109,40,
109,42,
109,45,
109,49,
109,51,
109,54,
109,57,
110,59,
111,61,
113,62,
116,64,
118,65,
121,66,
124,67,
129,68,
132,68,
136,68,
139,67,
143,66,
147,64,
151,62,
154,60,
156,58,
158,54,
158,52,
159,49,
159,45,
160,42,
160,39,
159,36,
159,33,
159,30,
159,29,
159,29,
159,29,
159,28,
195,24,
195,24,
195,24,
195,27,
195,30,
196,33,
197,37,
198,41,
198,46,
199,49,
199,52,
199,56,
199,57,
199,59,
199,60,
199,62,
200,64,
200,66,
200,67,
200,68,
200,68,
200,68,
200,68,
204,20,
205,20,
209,20,
221,22,
229,23,
235,25,
240,28,
243,31,
243,34,
239,37,
233,41,
228,43,
222,45,
218,46,
214,46,
211,46,
208,46,
207,46,
206,46,
205,46,
205,47,
205,46,
278,19,
278,19,
278,21,
277,25,
276,30,
276,33,
276,36,
276,40,
275,43,
275,46,
275,50,
275,53,
276,56,
276,59,
276,61,
277,63,
278,63,
279,64,
280,64,
281,64,
284,65,
289,65,
292,65,
297,65,
301,66,
304,66,
308,66,
313,66,
314,66,
314,66,
314,66,
281,40,
282,40,
285,40,
292,40,
296,40,
297,40,
300,40,
302,40,
305,40,
306,40,
309,40,
310,40,
310,40,
278,15,
278,15,
287,15,
294,16,
297,17,
300,17,
303,17,
305,17,
309,17,
312,17,
314,17,
315,17,
315,17,
315,17,
356,17,
356,17,
356,17,
356,21,
356,23,
356,27,
357,32,
357,35,
357,38,
357,41,
357,44,
357,48,
357,51,
356,54,
356,58,
355,59,
355,61,
355,63,
355,63,
355,64,
355,64,
355,64,
355,65,
355,65,
355,15,
356,16,
358,18,
362,20,
364,22,
369,25,
375,29,
379,33,
383,38,
388,44,
390,46,
391,48,
392,48,
393,50,
394,52,
395,53,
397,55,
398,57,
399,59,
400,61,
401,62,
402,63,
402,64,
402,64,
405,12,
405,14,
406,16,
407,19,
407,21,
408,24,
409,27,
410,29,
410,33,
411,36,
411,39,
411,44,
411,48,
410,51,
409,55,
407,57,
406,61,
405,63,
405,64,
404,65,
505,13,
503,12,
501,11,
498,11,
493,10,
486,11,
479,12,
472,14,
468,16,
464,19,
460,22,
455,27,
451,30,
450,34,
449,37,
449,39,
448,43,
449,45,
450,48,
452,52,
454,54,
457,57,
460,59,
464,60,
471,63,
477,64,
482,64,
488,63,
494,62,
499,60,
504,58,
508,55,
510,52,
510,49,
510,48,
510,46,
510,44,
510,44,
510,44,
510,44,
510,46,
510,51,
510,55,
511,59,
511,64,
512,67,
512,72,
512,74,
512,75,
512,76,
512,76,
512,76,
512,76,
518,72,
584,11,
581,9,
580,9,
577,9,
572,9,
568,10,
564,12,
560,14,
556,18,
553,21,
549,28,
548,31,
547,35,
547,38,
547,41,
547,43,
547,48,
549,51,
550,53,
551,55,
553,56,
556,58,
557,59,
561,61,
564,62,
566,62,
569,62,
574,62,
580,61,
585,60,
588,59,
592,57,
596,55,
600,52,
604,49,
605,46,
606,43,
607,39,
607,34,
607,29,
606,26,
604,23,
602,20,
600,17,
598,16,
595,14,
0,0
};

short colortable[]={
 0x0444, 0x0fff, 0x0002, 0x0f80, 0x000f, 0x0f0f, 0x00ff, 0x0ff0,
 0x0620, 0x0e50, 0x09f1, 0x0eb0, 0x0000, 0x0999, 0x00f0, 0x0f02
};

short rotcolortable[]={
 0x0444, 0x0fff, 0x0002, 0x0f80, 0x000f, 0x0f0f, 0x00ff, 0x0ff0,
 0x0620, 0x0e50, 0x09f1, 0x0eb0, 0x0000, 0x0999, 0x00f0, 0x0f02
};

char *copyright[]= {
	"© 1987-2000 Carlo Borreo Email:borreo@softhome.net License:GPL",
	"This is the last Amiga version I release, just let me know if",
	"you want to update it. I am currently working on a Linux version,",
	"available at http://lupengo.sourceforget.net",
	NULL
	};

main()
{
register int i,j,current,n;
BPTR fh;

DOSBase = (struct DOSLibrary *)OpenLibrary("dos.library",0);
IntuitionBase = OpenLibrary("intuition.library",0);
GfxBase = OpenLibrary("graphics.library",0);
check(DiskfontBase = OpenLibrary("diskfont.library",0), "Diskfont.lib");
check(font   =(struct TextFont *)OpenDiskFont(&ta   ), "OpenDiskFont"   );
check(bigfont=(struct TextFont *)OpenDiskFont(&bigta), "BigOpenDiskFont");
check(s=nw.Screen=OpenScreen(&ns),"OpenScreen");
LoadRGB4(& (s->ViewPort) ,colortable,NCOLS);
check(w = OpenWindow(&nw),"OpenWindow");
check(picsarr=AllocMem(PICSARRSIZE,MEMF_CHIP), "Memory");
check(fh=Open(picsname, MODE_OLDFILE), "No pics file");
check(Read(fh, picsarr, PICSARRSIZE)==PICSARRSIZE, "Read pics");
Close(fh);
for (i=0; i<NPICS; i++) {
	InitBitMap(&(bm[i]),DEPTH,36,20);
	for (j=0; j<DEPTH; j++)
		bm[i].Planes[j]=picsarr+i*PICDIM+j*(PICDIM/DEPTH);
	}
rp=w->RPort;
showhighs(-1,0); /* Read top file */
SetPointer(w, (short *)ptrshape, 0, 0, 0, 0);
opensound();

setjmp(jmpsave);

players=0;
toplay("-");
SetAPen(rp,0);
RectFill(rp,0,0,640,256);
for (i=0; titletab[i].x; i++) {
	BltBitMapRastPort(&(bm[CIRCLE]),0,0,rp,
		(int)titletab[i].x, (int)titletab[i].y,
		36,20,0xee);
	}

n=printarray(options,90,11);
printarray(copyright,n,9);

n=74;
BltBitMapRastPort(&(bm[CARLONID]),0,0,rp,n       ,130,36,20,0xc0);
BltBitMapRastPort(&(bm[PIGD    ]),0,0,rp,640-n-36,130,36,20,0xc0);

while ( (current=testkey(0)) == 0) {
	i		 =rotcolortable[ 5];
	rotcolortable[ 5]=rotcolortable[ 7];
	rotcolortable[ 7]=rotcolortable[14];
	rotcolortable[14]=rotcolortable[15];
	rotcolortable[15]=i;
	LoadRGB4(& (s->ViewPort) ,rotcolortable,NCOLS);
	Delay(10);
	}
LoadRGB4(& (s->ViewPort) ,colortable,NCOLS);

switch (current) {
	case 1:
	case 2: demoplayers=players=current;
		newgame();		break;
	case 3: playdemo();		break;
	case 4: showhighs(0,0);		break;
	case 5: showtable();		break;
	case 6: clall(0);		break;
	}
longjmp(jmpsave, 0);
}

printarray(char *strings[],int y,int height)
{
int i,max=0,n;

for (i=0; strings[i]; i++) {
	n=iprint(-1, 100, i*height+y, strings[i], 0);
	if (max<n) max=n;
	}
for (i=0; strings[i]; i++) 
	iprint(1, (640-max)/2, i*height+y, strings[i], 0);
return i*height+y ;
}

clall(retcode)
{
closesound();
closedemo();
if (picsarr) FreeMem(picsarr, PICSARRSIZE);
if (bigfont) CloseFont(bigfont);
if (font) CloseFont(font);
if (w) CloseWindow(w);
if (s) CloseScreen(s);
Exit(retcode);
}

startscreen()
{
register unsigned short i,j;
register int ax,ay,bx,by;
int x, y;
static int dx[4]= { 2,0,-2,0, };
static int dy[4]= { 0,2,0,-2, };
cls();
toplay("+Music");
FOR (i,BX0,BX1)
	FOR (j,BY0,BY1)
		screen[i][j]=(INFIELD(i,j) ? WALL : BORDER);
vertreport(level,BX1+1,LITFLAG,BIGFLAG);
ax=ay=4;
screen[4][4]=4;
loop1:
i=j=rnd(0,3);
loop2:
testkey(1);
bx=ax+dx[j];
by=ay+dy[j];
if (READSCREEN(bx,by)==WALL) {
	screen[bx][by]=j;
	screen[ax+dx[j]/2][ay+dy[j]/2]=BLANK;
	ax=bx;
	ay=by;
	goto loop1;
	}
j=((j+1) & 3);
if (j!=i) goto loop2;
j=READSCREEN(ax,ay);
screen[ax][ay]=BLANK;
if (j<4) { ax=ax-dx[j]; ay=ay-dy[j]; goto loop1; }
FOR (i,BX0,BX1) {
	testkey(1);
	FOR (j,BY0,BY1)
		putscreen(i,j,screen[i][j]);
	}
FOR (i,1,3) freeloc(&x, &y, TRIS, WALL);
trisdone=0;
FOR (i,1,ENEMIES) enemyx[i]=1;
eggs=2+level;
if (eggs>MAXEGG) eggs=MAXEGG;
playspeed=8-level;
if (playspeed<1) playspeed=1;
FOR (i,1,eggs) freeloc(&(eggx[i]),&(eggy[i]),EGGWALL,WALL);
listos=countdown=0;
addscore(0);
updatelives();
FOR (i,0,3) {
	black[i]=0;
	FOR (x,bx0[i],bx1[i])
		FOR (y,by0[i],by1[i])
			optputscreen(x,y,BORDER);
	}
FOR (i,1,ENEMIES) {
	if (enemyx[i]) freeloc(& (enemyx[i]), & (enemyy[i]), LUPONEL, BLANK);
	slp[i]=0;
	}
flasheggs();
FOR (i,0,1)
	if (living[i]) freeloc(& myx[i], & myy[i], PLTOFADE(i), BLANK);
agony[0]=agony[1]=0;
}

vertreport(v, pos, pic1, pic5)
{
int y=BY0;

while (v>=5) { v-=5; putscreen(pos,y++,pic5); }
while (v>0)  { v-- ; putscreen(pos,y++,pic1); }
putscreen(pos,y++,BLANK);
}

putscreen(x,y,shape)
{
BltBitMapRastPort(&(bm[shape]),0,0,rp,x*36+DELTAX,y*20,36,20,0xc0);
screen[x][y]=shape;
}

optputscreen(x,y,shape)
{
if (screen[x][y] != shape)
	putscreen(x, y, shape);
}
