 /* Fichier Music.dark.c				*/
/* Module appele par le programme principal	*/
/* pour effectuer un effet defini..appele par	*/
/* loadseg					*/

#include	<exec/types.h>
#include	<exec/memory.h>
#include	<exec/interrupts.h>
#include	<graphics/gfx.h>
#include	<graphics/gfxmacros.h>
#include	<graphics/rastport.h>
#include	<graphics/gfxbase.h>
#include	<clib/exec_protos.h>
#include	<clib/graphics_protos.h>
#include	<clib/intuition_protos.h>
#include	<clib/gadtools_protos.h>
#include	<dos/dos.h>
#include	<devices/audio.h>
#include	<hardware/cia.h>
#include	<clib/asl_protos.h>
#include	<libraries/gadtools.h>
#include	<libraries/asl.h>
#include	"/includes/struct.h"
#include	"/includes/tom_gadget.h"

#include	"none.h"
#include	"left_keyb.h"
#include	"right_keyb.h"
#include	"left_drum.h"
#include	"right_drum.h"
#include 	"guita.h"

BPTR	hdf;
extern	struct	CIA	far ciab;
extern	char	*get_default();
struct	RastPort	*rp,rp_s;
struct	Screen	*s;
extern	struct	appel_proc	*p_data_proc;
struct	Library *myCiaPointer;
struct	Library *AudioDev;
struct	Library *AslBase;
extern	struct	GfxBase *GfxBase;
struct	Library	*GadToolsBase;
/* Pour l'audiodevice */
struct	IOAudio	*allocIOB=0;
struct	IOAudio	*lockIOB=0;
struct	Device	*device=0;
struct	MsgPort	*soundport=0;
UBYTE	allocationmap[]={15};
struct	BitMap	b;
unsigned	int	seed;
struct	FileRequester	*my_req=0;
UBYTE	flg_able;

extern	void	mt_init(),mt_music(),mt_end();
UWORD	v[4];

#define	WIDTH	320
int	HEIGHT;
#define	HEIGHT_S	161
#define	DEPTH	4
#define	POSY_SCENE	102
#define	NB_ANIM		12
#define	FIRST_X		64

#define	T_PLATE_FORME	4
#define	T_POI		18
extern	char	*get_image();
char	*image;
char	*p_text_info=
"      'The Great PoiPoi'\n"
"\n"
"\nBased on an my old demo....The poipoi are\n"
"very nice guys,bumping and playing in rythm.\n"
"\n"
"You can choose a the soundtracker module to\n"
"play by selecting it in the string gadget.\n\n"
"You can select the voice instrument assigned\n"
"to a PoiPoi in the bottom of the screen. If\n"
"the selected voice do nothing, the PoPoi will\n"
"not move!\n(Read docs...)\n\n"
"     V1.1 By T.Landspurg";

char	*adr_data;
extern	void	replay();
UWORD	tab_inst[32];
UWORD	tab_lookup[32];
UWORD	tab_value[6];

WORD ColorMap[] =
{
	0x0000,0x0AAA,0x0070,0x00A0,0x00B0,0x0444,0x0555,0x0655,
	0x0733,0x0E00,0x0A90,0x0888,0x0050,0x0DDD,0x0FE0,0x0FFF
};

/*char	nom[100]="<insert a module name here>";*/
char	nom[100]="DH_Divers:Musiques/Modules-ST/mod.impact";

int	__saveds	press_load();
int	__saveds	press_info();

struct tom_gadget my_gadg[]={
	{"_Module",	STRING, 100, 10,  250,13,0,100,0,0,nom},
	{"_Sel",	BUTTON ,380, 10,  50,13, 0,0,0,0,(char *)&press_load},
/*
	{"Drummer (l)",	INTEGER,100, 35,  50,13,5,2,0,0,0},
	{"Drummer (r)",	INTEGER,100, 50,  50,13,5,2,0,0,0},
	{"Left  Keyb",	INTEGER,250, 35,  50,13,2,2,0,0,0},
	{"Right Keyb",	INTEGER,250, 50,  50,13,7,2,0,0,0},
	{"guitar",	INTEGER,100, 65,  50,13,1,2,0,0,0},
*/
	{"Voice  1:",	SLIDER	,150,55, 190,8, 0,0,5,0,0},
	{"Voice  2:",	SLIDER	,150,65, 190,8, 0,0,5,0,0},
	{"Voice  3:",	SLIDER	,150,75, 190,8, 0,0,5,0,0},
	{"Voice  4:",	SLIDER	,150,85, 190,8, 0,0,5,0,0},
	{"Voice  5:",	SLIDER	,150,95,190,8, 0,0,5,0,0},
	{"Voice  6:",	SLIDER	,150,105,190,8, 0,0,5,0,0},
	{"Voice  7:",	SLIDER	,150,115,190,8, 0,0,5,0,0},
	{"Voice  8:",	SLIDER	,150,125,190,8, 0,0,5,0,0},
	{"Voice  9:",	SLIDER	,150,135,190,8, 0,0,5,0,0},
	{"Voice 10:",	SLIDER	,150,145,190,8, 0,0,5,0,0},
	{"Voice 11:",	SLIDER	,150,155,190,8, 0,0,5,0,0},
	{"Voice 12:",	SLIDER	,150,165,190,8, 0,0,5,0,0},
	{"Voice 13:",	SLIDER	,150,175,190,8, 0,0,5,0,0},
	{"Voice 14:",	SLIDER	,150,185,190,8, 0,0,5,0,0},
	{"Voice 15:",	SLIDER	,150,195,190,8, 0,0,5,0,0},
	{"Voice 16:",	SLIDER	,150,205,190,8, 0,0,5,0,0},
	{""	,	IMAGE	,150,25,33,26,  0,0,0,0,(char *)&none_iffImage},
	{""	,	IMAGE	,150+32,25,33,26,  0,0,0,0,(char *)&left_drum_iffImage},
	{""	,	IMAGE	,150+64,25,33,26,  0,0,0,0,(char *)&right_drum_iffImage},
	{""	,	IMAGE	,150+96,25,33,26,  0,0,0,0,(char *)&left_keyb_iffImage},
	{""	,	IMAGE	,150+128,25,33,26,  0,0,0,0,(char *)&right_keyb_iffImage},
	{""	,	IMAGE	,150+160,25,33,26,  0,0,0,0,(char *)&guita_iffImage},
	{"Inst_r",	BUTTON ,380, 40,  50,13, 0,0,0,0,(char *)&press_info},
	{0,		END_LISTE,  0,  0,   0, 0, 0,0,0,0,0}
};
struct	Interrupt *p_interrupt;

#define	SIZE	100000

#define	OK	1
#define	NOK	0

#define	FREQU_PAL	1773447/125
#define FREQU_NTSC	1789773/125
int	frequ=FREQU_PAL;

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

int	init_audio()
{
	if(((allocIOB=(struct IOAudio *)AllocMem(sizeof(struct IOAudio),MEMF_PUBLIC|MEMF_CLEAR))==0)||
	   (( lockIOB=(struct IOAudio *)AllocMem(sizeof(struct IOAudio),MEMF_PUBLIC|MEMF_CLEAR))==0)){
		return(NOK);
	}
	if(OpenDevice(AUDIONAME,0,allocIOB,0)!=0){
		return(NOK);
	}
	device=allocIOB->ioa_Request.io_Device;
	allocIOB->ioa_Request.io_Message.mn_Node.ln_Pri=127;
	if((soundport=(struct MsgPort *)CreatePort("music.dark",0))==0){
		return(NOK);
	}

	allocIOB->ioa_Request.io_Message.mn_ReplyPort=soundport;
	allocIOB->ioa_Request.io_Command=ADCMD_ALLOCATE;
	allocIOB->ioa_Request.io_Flags=ADIOF_NOWAIT;
	allocIOB->ioa_Data=allocationmap;
	allocIOB->ioa_Length=sizeof(allocationmap);
	BeginIO(allocIOB);
	if(WaitIO(allocIOB)){
/*		SimpleRequest("Audio device busy");*/
		return(NOK);
	}
	lockIOB->ioa_Request.io_Message.mn_ReplyPort=soundport;
	lockIOB->ioa_Request.io_Device=device;
	lockIOB->ioa_Request.io_Unit=allocIOB->ioa_Request.io_Unit;
	lockIOB->ioa_Request.io_Command=ADCMD_LOCK;
	lockIOB->ioa_AllocKey=allocIOB->ioa_AllocKey;
	SendIO(lockIOB);
	if(CheckIO(lockIOB)){
/*		SimpleRequest("Canal deja pris!");*/
		return(NOK);
	}

	return(OK);
}
void	free_audio()
{
	if(lockIOB->ioa_Request.io_Device!=0){
		if(CheckIO(lockIOB)){
/*			SimpleRequest("Channel stolen");*/
		}
		allocIOB->ioa_Request.io_Command=ADCMD_FREE;
		DoIO(allocIOB);
	}
	if(soundport!=0)DeletePort(soundport);
	if(device!=0)CloseDevice(allocIOB);
	if(lockIOB!=0)FreeMem(lockIOB,sizeof(struct IOAudio));
	if(allocIOB!=0)FreeMem(allocIOB,sizeof(struct IOAudio));

}

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

int SetCIAInt()
{
	int code_ret;

	if( (GfxBase->DisplayFlags & PAL) !=0){
		frequ=FREQU_PAL;
	}else if( (GfxBase->DisplayFlags & NTSC) !=0){
		frequ=FREQU_NTSC;
	}	
	myCiaPointer=(struct Library *)OpenResource("ciab.resource");
	if(myCiaPointer==NULL){
		code_ret=FALSE;
	}else{
		code_ret=TRUE;;
	}
	return(code_ret);
}

int	__saveds	press_info(pg,p_t,ctx)
struct	Gadget	*pg;
struct	tom_gadget	*p_t;
struct	contexe	*ctx;
{
	char	buffer[50*16];
	char	adr_data[30*16+10];
	char	name[4];
	int	i;
	char	*pc;

	static struct EasyStruct ES={
		sizeof(struct EasyStruct),
		0,"Info :",
		"","Thanks for being so good with me",};

	SPrintF(buffer,"Intruments:\n");

	hdf=Open(my_gadg[0].p_data,MODE_OLDFILE);
	pc=0;
	if(hdf!=0){
		Read(hdf,adr_data,sizeof(adr_data));
		Seek(hdf,0x438,OFFSET_BEGINNING);
		Read(hdf,name,4);
		Close(hdf);
		if(strncmp(name,"M.K.",4)==0){
			pc=adr_data;
		}
	}
	if(pc==0){
		pc=get_default();
		SPrintF(buffer,"%s...Not a Soundtracker module...Show default\n",buffer);
	}
	pc+=20;
	for(i=0;i<8;i++){
			for(i=0;i<8;i++){
				SPrintF(buffer,"%s[%2ld]:%-30s  ",buffer,i+1,pc);
				SPrintF(buffer,"%s[%2ld]:%-30s\n",buffer,2*i+1,pc+30*8);
				pc+=30;
	    		}
	}
	ES.es_TextFormat=buffer;
	EasyRequest(ctx->Wnd, &ES, NULL);
	return FALSE;
}

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

unsigned        int     my_rand()
{
        
        seed=25173*seed+138490;
        return(seed);
}

/**************************************************/
/* Initialisation de notre handler d'interruption */
/**************************************************/

int	setup()
{

   p_interrupt=(struct Interrupt *)AllocMem(sizeof(struct Interrupt),MEMF_PUBLIC);

   if(p_interrupt == NULL){
	return(NOK);
   }
   p_interrupt->is_Node.ln_Type=NT_INTERRUPT;
   p_interrupt->is_Node.ln_Pri=0;
   p_interrupt->is_Node.ln_Name="SuperDark replay Interrupt";
   p_interrupt->is_Data=0;
   p_interrupt->is_Code=replay;

   if(AddICRVector(myCiaPointer,CIAICRB_TA,p_interrupt)!=0){
	return(NOK);
   }else{
	flg_able=TRUE;
	ciab.ciatalo=(UBYTE)(frequ & 0xff);
	ciab.ciatahi=(UBYTE)((frequ>>8) & 0xff);
	ciab.ciacra|=1;
   }
   return(OK);
}

/******************************************/
/* Arret  de notre handler d'interruption */
/******************************************/

void stop()
{
	if(flg_able){
		AbleICR(myCiaPointer,(1<<CIAICRB_TA) );
	   	RemICRVector(myCiaPointer,CIAICRB_TA,p_interrupt);
	}
	if(p_interrupt!=0)FreeMem(p_interrupt,sizeof(struct Interrupt));
/*
   exec_safe(tab_replay[num_replay].p_end);
*/
}
/********************************************************** aff_poipoi ***/

void	aff_poipoi()
{
	UWORD	i;
	UWORD	old[4];
	UWORD	new[4];
	UWORD	poi[4];
	WORD	speed[4];
	UWORD	num_anim[4];
	UWORD	num_bonjour[4];
	UWORD	posx,posy;
	UWORD	temp,tx;
	
	UWORD	anim_bat_g;
	UWORD	anim_bat_d;
	UWORD	anim_keyb_g;
	UWORD	anim_keyb_d;
	UWORD	anim_guitare;
	UWORD	anim_tourne;
	
#define	num_batteur_g	1
#define	num_batteur_d	2
#define	num_keyb_g	3
#define	num_keyb_d	4
#define	num_guitare	5


	for(i=0;i<4;i++){
		old[i]=0;
		new[i]=0;
		speed[i]=0;
		num_anim[i]=0;
		num_bonjour[i]=0;
		poi[i]=30;
	}
	anim_bat_d=0;
	anim_bat_g=0;
	anim_keyb_g=0;
	anim_keyb_d=0;
	anim_guitare=0;
	anim_tourne=0;

	SetAPen(rp,0);
	while(tst_end()==FALSE){

		WaitTOF();

		for(i=0;i<4;i++){
		   tx=FIRST_X+i*48;
		   new[i]=160-v[i]/2;
		   RectFill(rp,tx,poi[i]/2,tx+32,poi[i]/2+T_POI-1);

		   poi[i]-=speed[i];
		   speed[i]--;

		   /* Animation du poipoi */

		   /********************************************************/
		   /* On demarre l'anim si le poipoi a une vitesse sup a 6 */
		   /* lorsqu'il passe a 0				   */
		   /********************************************************/

		   if(num_anim[i]==0){
			if((speed[i]>6)&&(poi[i]/2<100)){
			   num_anim[i]=1;
			}
		   }else{
			if(num_anim[i])num_anim[i]++;
			if(num_anim[i]==NB_ANIM*2){
				num_anim[i]=0;
			}
		   }
		   /************************************/
		   /* Si le poipoi "touche" le plateau */
		   /************************************/
		   if(poi[i]/2+T_POI>=new[i]){

			/* Si la vitesse est assez importante pour le*/
			/* faire rebondir			     */

			if(old[i]-new[i]>20){
				speed[i]=5+(poi[i]/2+T_POI-new[i])/5;
				num_bonjour[i]=0;
			}else{
				speed[i]=0;
				num_anim[i]=0;
			}
			poi[i]=2*(new[i]-T_POI+1);
		   }
		   posx=num_anim[i]>>1;
		   posy=0;
		   if(posx>=10){
			posx-=10;
			posy=T_POI;
		   }
		   posx=posx*32;
		   /* Si on est pose */
		   if(speed[i]==0){
			if(num_bonjour[i]){
				num_bonjour[i]--;
				temp=num_bonjour[i]>>4;
				posy=18;
				posx=64+temp*32;
			}else{
				if((my_rand()&0xFF)>254){
					num_bonjour[i]=(7)<<4;
				}
			}
		   }
  	           ClipBlit(&rp_s,posx,posy,rp,tx,poi[i]/2,32,T_POI,0xc0);
		}
		/**************************************/
		/* On s'occupe des support maintenant */
		/**************************************/
		for(i=0;i<4;i++){
		   tx=FIRST_X+i*48;
		   if(new[i]!=old[i]){
			RectFill(rp,tx,old[i],tx+32,old[i]+T_PLATE_FORME);
			ClipBlit(	&rp_s,0,55,
					rp,tx,new[i],32,T_PLATE_FORME,0xc0);
			if(new[i]<old[i]) {
				ClipBlit(	&rp_s,288,17,
						rp,tx+8,T_PLATE_FORME+new[i],16,old[i]-new[i]+1,0xc0);
			/* Si le plateau redescend trop vitre, il se peut qu'il */
			/* reste un trace d'un precedent "tube"			*/
			}else if(new[i]>old[i]+T_PLATE_FORME){
				RectFill(rp,tx,old[i]+T_PLATE_FORME,
					    tx+16,new[i]);
			}
			old[i]=new[i];
		   }
		}
		/* Animation batteur cote gauche */
		if(tab_value[num_batteur_g]>90){
			anim_bat_g=2*2;
		}
		if(anim_bat_g){
			anim_bat_g--;
			ClipBlit(	&rp_s,128+(anim_bat_g/2)*16,58,
					rp,96,HEIGHT-36,16,14,0xc0);
		}

		/* Animation batteur cote droit */
		if(tab_value[num_batteur_d]>90){
			anim_bat_d=3*2;
		}
		if(anim_bat_d){
			anim_bat_d--;
			ClipBlit(	&rp_s,80+(anim_bat_d/2)*16,58,
					rp,112,HEIGHT-39,16,14,0xc0);
		}
		/* Animation keyb cote gauche */
		if(tab_value[num_keyb_g]>90){
			anim_keyb_g=3*2;
		}
		
		if(anim_keyb_g){
			anim_keyb_g--;
			ClipBlit(	&rp_s,80+(anim_keyb_g/2)*16,72,
					rp,32,HEIGHT-20,16,5,0xc0);
		}

		/* Animation keyb cote gauche */
		if(tab_value[num_keyb_d]>90){
			anim_keyb_d=3*2;
		}
		if(anim_keyb_d){
			anim_keyb_d--;
			ClipBlit(	&rp_s,80+(anim_keyb_d/2)*16,77,
					rp,192,HEIGHT-18,16,6,0xc0);
			ClipBlit(	&rp_s,80+(anim_keyb_d/2)*16,83,
					rp,208,HEIGHT-18,16,6,0xc0);
		}

		/* Animation guitare gauche */
		if(tab_value[num_guitare]>90){
			anim_guitare=3*2;
		}
		if(anim_guitare){
			anim_guitare--;
			ClipBlit(	&rp_s,128+(anim_guitare>>1)*32,72,
					rp,144,HEIGHT-15,32,12,0xc0);
		}

		/* Animation tourne */
		if(anim_tourne){
			anim_tourne--;
			temp=anim_tourne>>4;
			if(temp>=3){
				temp=5-temp;
			}
			ClipBlit(	&rp_s,80+(temp)*8,89,
					rp,208,HEIGHT-21,8,9,0xc0);
		}else{
			if((my_rand()&0xFF)>253){
				anim_tourne=(2*3)<<4;
			}
		}
	}

}

/********************************************************** press_load() ***/

int	__saveds	press_load(pg,p_t,ctx)
struct	Gadget	*pg;
struct	tom_gadget	*p_t;
struct	contexe	*ctx;
{
	UWORD	res,len_dir;

	if(my_req!=0){
		res=(int)RequestFile(my_req);
		len_dir=strlen(my_req->rf_Dir);
		if((my_req->rf_Dir[len_dir-1]!='/')&&(my_req->rf_Dir[len_dir-1]!=':')){
			SPrintF(my_gadg[0].p_data,
				"%s/%s",
				my_req->rf_Dir,
				my_req->rf_File);
		}else{
			SPrintF(my_gadg[0].p_data,
				"%s%s",
				my_req->rf_Dir,
				my_req->rf_File);
		}
	}
	if(GadToolsBase!=0){
	    GT_SetGadgetAttrs(  my_gadg[0].p_gadg,ctx->Wnd,NULL,
				GTST_String,my_gadg[0].p_data,
				TAG_END);
	    GT_RefreshWindow( ctx->Wnd, 0l );
	}else{
	    RefreshGadgets( ctx->Wnd->FirstGadget,ctx->Wnd, 0l );
	}
	return 0;
}
/********************************************************** dark ()***/

void	dark()
{
	UWORD	d,i;
	int	flg_error;
	int	etape;
	int	flg_err;
	struct	FileInfoBlock	*fib;
	BPTR	lt;
	int	size;
	char	*name;
	UWORD	width,height;
	ULONG	did;

	lt=0;
	fib=0;
	adr_data=0;
	allocIOB=0;
	lockIOB=0;
	device=0;
	soundport=0;
	p_interrupt=0;
	flg_able=FALSE;

	if(GfxBase->Flags&NTSC){
		HEIGHT=200;
	}else{
		HEIGHT=256;
	}
	d=5;
	width=0;
	height=0;
	if(find_res((SHORT)TRUE,&d,&width,&height,&did)){
		if(d<4)return;
	}else{
		return;
	}
	if(height<HEIGHT)height=HEIGHT;

	InitBitMap(&b,DEPTH,WIDTH,HEIGHT_S);
	InitRastPort(&rp_s);
	rp_s.BitMap=&b;
	image=get_image();
	for(i=0;i<DEPTH;i++){
		b.Planes[i]=image+i*HEIGHT_S*WIDTH/8;
	}
	flg_err=FALSE;
	etape=0;
	while((etape<10)&&(flg_err==FALSE)){
		switch(etape){
			case 0:
				if (s = OpenScreenTags (NULL,
							  SA_Width, (ULONG)width,
							  SA_Height, (ULONG)height,
							  SA_Depth, (ULONG) DEPTH,
							  SA_DisplayID, did,
							  SA_ShowTitle, FALSE,
							  SA_Quiet, TRUE,
/*							  SA_Behind, TRUE,*/
/*							  SA_BitMap, bm,*/
							  TAG_DONE)){

					rp = &(s->RastPort);
					SetAPen(rp,0);
					SetRast(rp,0);
					ClipBlit(&rp_s,0,POSY_SCENE,
						rp,0,HEIGHT-(HEIGHT_S-POSY_SCENE),WIDTH,HEIGHT_S-POSY_SCENE-1,0xc0);
					if(GfxBase->Flags&NTSC){
						SetAPen(rp,0);
						RectFill(rp,
							0,
							HEIGHT-(HEIGHT_S-POSY_SCENE),
							WIDTH-1,
							HEIGHT-(HEIGHT_S-POSY_SCENE)+55);
					}
					d=1<<(s->BitMap.Depth);
					LoadRGB4(&s->ViewPort,ColorMap,d);
					FreeSprite (0);
					GfxBase->SpriteReserved|=1;
				}else{
					flg_err=TRUE;
				}
			break;
			case 1:
				/* Cree la table qui sera utilisee par replay.s */
				for(i=0;i<16;i++){
					tab_lookup[i]=my_gadg[i+2].value;
				}
				if(init_audio()!=OK){
					flg_err=TRUE;
				}
			break;
			case 2:
				flg_error=SetCIAInt();
				break;
			case 3:
				if(setup()!=OK){
					flg_error=TRUE;
				}
				break;
			case 4:
				size=0;
				name=my_gadg[0].p_data;
				if((fib=(struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock),0))!=0){
					lt=Lock(name,ACCESS_READ);
					if(lt!=0){
						if(Examine(lt,fib)!=0){
							size=fib->fib_Size;
						}
					}
				}
				/* Si le module n'a pas ete trouve, on saute 	  */
				/* deux etapes -> on jouera le module par defaut! */
				if(!size){
					etape+=2;
					SetAPen(rp,15);
					My_Print(rp,30,HEIGHT-(HEIGHT_S-POSY_SCENE)-20,"Error with selected module");
					My_Print(rp,30,HEIGHT-(HEIGHT_S-POSY_SCENE)-10,"  --- Using default ---");
				}
				break;
			case 5:
				adr_data=AllocMem(size,MEMF_CHIP|MEMF_CLEAR);
				if(adr_data!=0){
					hdf=Open(name,MODE_OLDFILE);
					if(hdf!=0){
						Read(hdf,adr_data,size);
						Close(hdf);
					}
				}else{
					flg_err=TRUE;
				}
			break;
			/* Test if the module is really a stk module */
			case 6:
				name=&adr_data[0x438];
				if(strncmp(name,"M.K.",4)){
					FreeMem(adr_data,size);
					adr_data=0;
					SetAPen(rp,15);
					My_Print(rp,20,HEIGHT-(HEIGHT_S-POSY_SCENE)-20,"   Error with selected module");
					My_Print(rp,20,HEIGHT-(HEIGHT_S-POSY_SCENE)-10,"-- Not a soundtracker module --");
				}
				break;
			case 7:
				exec_safe(mt_init);
				aff_poipoi();
				break;
			default:
				break;
		}
		etape++;
	}
	stop();

	free_audio();

	if(adr_data!=0)FreeMem(adr_data,size);
	if(lt)UnLock(lt);
	if(fib)FreeMem(fib,sizeof(struct FileInfoBlock));
	exec_safe(mt_end);
	if(s)DCloseScreen(s);
}

void	proc_init()
{
        seed=VBeamPos();
	AslBase=(struct Library *)OpenLibrary(AslName,0);
	if(AslBase!=0){
		my_req=AllocFileRequest();
	}
	GadToolsBase=(struct Library *)OpenLibrary("gadtools.library",0);
}
void	proc_save()
{
}

void	proc_end()
{
	if(AslBase)CloseLibrary(AslBase);
	if(my_req!=0){
			FreeFileRequest(my_req);
	}
	if(GadToolsBase)CloseLibrary(GadToolsBase);
}

