const char *_VERSION="\0$VER: Flut! 4.0a (23.03.96)";
#include <cnet/empty.h>
#include <dos/dos.h>
#include "Flut.h"

#define mainansi	0
#define titelansi	1
#define winneransi	2
#define loseransi	3
#define helpansi	4
#define layoutansi	5
#define joinansi	6
#define hallansi	7
#define wallansi	8
#define messansi	9
#define rankansi	10
#define chefansi	11
#define testansi	12
#define wallspfad	13
#define flutserver	14

FILE	*fp;

struct tm*datum_zeit;

void	send_message(long com);

void	flute(BYTE xpos, BYTE ypos);
void	fuelle(BYTE xpos, BYTE ypos);
void	print_fuellgrad(BYTE xt, BYTE yt, BYTE akt_usert, BYTE fuellgrad);

void	print_players(void);
void	print_games(void);
void	print_brett(void);
void	print_status(void);
void	print_action(void);
void	print_titel(void);
void	print_clock(void);
void	print_ranks(void);
void	print_hall(void);

void	fluter(long fluterid);
void	operator(void);
void	selectorjoin(void);
void	joiner(void);
void	returner(void);
void	pointeradd(void);
void	print_changes(void);
void	count_eimer(void);
void	killgame(void);
void	scrollx(UBYTE line,char *text);
void	scrolly(char *text);
void	mymain(void);
void	select(void);
void	get_wall(void);
void	cursor(void);
void	make_path(byte pathnr);

char	line_a[500],*p,line_b[500],*p2,
		batzen[7]="°°²²ÛÛ",
		leer[2]=" ",
		taker[23],
		key[3],
		last_tag[3],
		today_tag[3],
		next_tag[3],
		next_hh_mm[6],
		last_faktor_char[8],
		username[6][21],
		wall[12][59],
		routepfad[21]=	"mail:users/cr.fluter";

char	path[15][21]={	"ansis/main",	"ansis/titel.  ",	"ansis/winner.  ",	"ansis/loser.  ",
						"ansis/help. ",	"ansis/layout",		"ansis/join",		"ansis/hall",
						"ansis/wall",	"ansis/mess",		"ansis/rank",		"ansis/chef",
						"ansis/test",	"walls/  ",			"flut-server"},
		pathstart[108],
		pathwork[108];

UBYTE	x,y,i,j,k,
		xtab,ytab,
		altx,alty,
		akt_user,
		stacker,
		infoline=11,
		colorcycle,
		farbe,
		olmyeah,
		anz_angefasst,
		anz_eimer[3][6],
		max_eimer,
		groesster,
		zweiter,
		gesamt_voll,
		titelmax,
		winnermax,
		losermax;

BYTE	moremode,
		starts,
		x_flut[128],
		y_flut[128],
		game=-1,
		game_print=0,
		game_neu,
		game_sysop;

int		wertung_a,wertung_b;

ULONG	today,
		rechen,
		nearest;

BPTR	lock;

BOOL	turn[60],join[60],
		rankyes,
		ansidesign=TRUE,
		ready,
		reading,
		go;

FLOAT	last_faktor;

struct tabellenstruct
{
	LONG	wert,
			ptr;
}tab[12];

main(int argc,char **argv)
{
	PreMain(argc,argv);

	if(argc!=5)
	{
		PutText("n1Arguments for Flut!n1-------------------n2Number of Titelansisn1Number of Winneransisn1Number of Loseransisn2");
	}
	else
	{
		titelmax=atoi(argv[2]);
		winnermax=atoi(argv[3]);
		losermax=atoi(argv[4]);

		srand(time(NULL));
	
		if(z->user1.TermLength<30 || z->user1.TermWidth<80 || z->user1.Colors==0 || z->user1.ANSI<2)
		{
			PutText("n1Requirements for Flut!n1----------------------n2Ansi 8/16 Coloursn1IBM-Fontn180 Columnsn130 Linesn2");
		}
		else
		{
			lock=GetProgramDir();
			NameFromLock(lock,pathstart,108);

			make_path(mainansi);
			ReadFile(pathwork,0);

			make_path(flutserver);
			sprintf(line_a,"run %s",pathwork);

			while(!flutport && starts<5)
			{
				Forbid();
				if(flutport=FindPort(FLUT_PORT))
				{
					if(ptr=(struct messagestruct *)AllocVec(sizeof(struct messagestruct),MEMF_PUBLIC))
					{
						if(flutreplyport=CreatePort(NULL,0))
						{
							strcpy(ptr->pname,z->user1.Handle);
							send_message(MSG_INIT);
							Permit();
							mymain();
							send_message(MSG_QUIT);
							DeletePort(flutreplyport);
						}
						FreeVec(ptr);
					}
				}
				else
				{
					Permit();
					system(line_a);
					starts++;
					Delay(25);
				}
			}
			PutText("[30;1H");
		}
	}
	PostMain();
}

void send_message(long com)
{
	ptr->msg.mn_Node.ln_Pri=0;
	ptr->msg.mn_Node.ln_Name=NULL;
	ptr->msg.mn_Node.ln_Type=NT_MESSAGE;
	ptr->msg.mn_ReplyPort=flutreplyport;
	ptr->msg.mn_Length=sizeof(struct messagestruct);
	ptr->com=com;
	PutMsg(flutport,(struct messagestruct *)ptr);
	WaitPort(flutreplyport);
	while(ptrreply=(struct Message *)GetMsg(flutreplyport))
	{
	}
}

void make_path(byte pathnr)
{
	strcpy(pathwork,pathstart);
	AddPart(pathwork,path[pathnr],108);
}

void scrollx(UBYTE line, char *text)
{
	char	string[20],*pstring;
	pstring=text;

	if(colorcycle==7)
	{
		colorcycle=1;
	}
	else
	{
		colorcycle++;
	}

	sprintf(string,"[0m[%d;1H[3PC%d",line,colorcycle);
	PutText(string);

	while(*pstring)
	{
		if(*pstring==25)
		{
			sprintf(string,"[%d;1H[P[78C%4.4s",line,pstring);
			pstring+=4;
		}
		else
		{
			sprintf(string,"[%d;1H[P[78C%1.1s",line,pstring);
			pstring++;
		}
		PutText(string);
	}
}

void scrolly(char *text)
{
	PutText("[14;1H[M[30;1H");
	PutText(text);
}

void gettime(void)
{
	time(&today);
	datum_zeit=localtime(&today);
	strftime(today_tag,3,"%d",datum_zeit);
}

void print_games(void)
{
	gettime();
	PutText("[3;1H[0m");
	nearest=0;
	for(y=0;y<51;y+=10)
	{
		p=stpcpy(line_a,"\n[16C");
		for(x=0;x<10;x++)
		{
			join[y+x]=FALSE;
			turn[y+x]=FALSE;
			if(ptr->all->game[y+x].anzahl==0)
			{
				join[y+x]=TRUE;
				farbe=36; /* empty */
			}
			else
			{
				farbe=35; /* out */
				for(i=0;i<6;i++)
				{
					if(ptr->all->game[y+x].player[i].userid==z->id)
					{
						if(ptr->all->game[y+x].player[ptr->all->game[y+x].anzahl-1].userid==0)
						{
							farbe=34; /* joined */
						}
						else
						{
							datum_zeit=localtime(&ptr->all->game[y+x].player[i].last_move);
							strftime(last_tag,3,"%d",datum_zeit);
							if(today-ptr->all->game[y+x].player[i].last_move>43200 && strcmp(today_tag,last_tag)!=0 && y+x<50)
							{
								turn[y+x]=TRUE;
								farbe=31; /* turn */
							}
							else
							{
								turn[y+x]=FALSE;
								farbe=33; /* in */
								if(y+x<50)
								{
									if(nearest>ptr->all->game[y+x].player[i].last_move+43200 || nearest==0)
									{
										nearest=ptr->all->game[y+x].player[i].last_move+43200;
									}
								}
							}
							for(j=0;j<ptr->all->game[y+x].y_size;j++)
							{
								for(k=0;k<ptr->all->game[y+x].x_size;k++)
								{
									if(ptr->all->game[y+x].fuellgrad[k][j]==0 || (ptr->all->game[y+x].farbe[k][j]==i+1 && ptr->all->game[y+x].fuellgrad[k][j]<3))
									{
										goto found;
									}
								}
							}
							turn[y+x]=FALSE;
							farbe=37;
							found:
						}
					}
				}
				if(farbe==35 && ptr->all->game[y+x].player[ptr->all->game[y+x].anzahl-1].userid==0)
				{
					join[y+x]=TRUE;
					farbe=32; /* wait */
				}
			}
			sprintf(line_b," [%2.2dm%2.2d",farbe,y+x+1);
			p=stpcpy(p,line_b);

		}
		if(y<50)
		{
			PutText(line_a);
		}
	}
	PutText("[9;17H [2C [2C [2C [2C [2C [2C [2C [2C [2C [37m");

	for(y=0;y<51;y+=10)
	{
		for(x=0;x<10;x++)
		{
			if(ptr->all->game[y+x].belegt==TRUE)
			{
				sprintf(line_a,"[%d;%dH>",y/10+4,x*3+17);
				PutText(line_a);
			}
		}
	}
}

void print_text(byte pathnr)
{
	make_path(pathnr);
	if(fp=fopen(pathwork,"r"))
	{
		while (fgets(line_a,sizeof(line_a),fp))
		{
			line_a[strlen(line_a)-1]=0;
			scrolly(line_a);
		}
		fclose(fp);
	}
}

void count_eimer(void)
{
	for(y=0;y<6;y++)
	{
		anz_eimer[0][y]=0;
		anz_eimer[1][y]=0;
		anz_eimer[2][y]=0;
	}

	anz_angefasst=0;
	for(y=0;y<ptr->all->game[game].y_size;y++)
	{
		for(x=0;x<ptr->all->game[game].x_size;x++)
		{
			if(ptr->all->game[game].fuellgrad[x][y]>0)
			{
				anz_eimer[ptr->all->game[game].fuellgrad[x][y]-1][ptr->all->game[game].farbe[x][y]-1]++;
				anz_angefasst++;
			}
		}
	}
}

void print_players(void)
{
	count_eimer();

	for(y=0;y<6;y++)
	{
		if(ptr->all->game[game].player[y].userid==0)
		{
			if(y<ptr->all->game[game].anzahl)
			{
				sprintf(line_a,"[%d;48HC%1x???????? ??? ??? ??? ???? ?????",y+4,9+y);
			}
			else
			{
				sprintf(line_a,"[%d;48HC%1x-------- --- --- --- ---- -----",y+4,9+y);
			}
		}
		else
		{
			if(username[y][0]==0)
			{
				ReadAccount(ptr->all->game[game].player[y].userid,&z->user2);
				strcpy(username[y],z->user2.Handle);
			}
			last_faktor=(today - ptr->all->game[game].player[y].last_move)/86400.0;

			if(last_faktor < 10)
			{
				if(last_faktor>2.99 && last_faktor<3.0)
				{
					strcpy(last_faktor_char,"2.99");
				}
				else
				{
					sprintf(last_faktor_char,"%1.2f",last_faktor);
				}
			}
			else
			{
				sprintf(last_faktor_char,">%3.0f",last_faktor);
			}

			rechen=ptr->all->game[game].player[y].last_move;

			datum_zeit=localtime(&rechen);
			strftime(next_tag,3,"%d",datum_zeit);

			rechen+=43200;

			if(ptr->all->game[game].player[ptr->all->game[game].anzahl-1].userid==0)
			{
				sprintf(line_a,"[%d;48HC%1x%-8.8s% 4d% 4d% 4d ----  Wait",y+4,9+y,username[y],anz_eimer[0][y],anz_eimer[1][y],anz_eimer[2][y]);
			}
			else
			{
				if(game>49)
				{
					sprintf(line_a,"[%d;48HC%1x%-8.8s% 4d% 4d% 4d ----   OLG",y+4,9+y,username[y],anz_eimer[0][y],anz_eimer[1][y],anz_eimer[2][y],last_faktor_char);
				}
				else
				{
					if(today>rechen && strcmp(today_tag,next_tag)!=0)
					{
						sprintf(line_a,"[%d;48HC%1x%-8.8s% 4d% 4d% 4d %s   Now",y+4,9+y,username[y],anz_eimer[0][y],anz_eimer[1][y],anz_eimer[2][y],last_faktor_char);
					}
					else
					{
						datum_zeit=localtime(&rechen);
						strftime(next_hh_mm,6,"%H:%M",datum_zeit);

						if(atoi(next_hh_mm)>11)
						{
							sprintf(line_a,"[%d;48HC%1x%-8.8s% 4d% 4d% 4d %s 00:00",y+4,9+y,username[y],anz_eimer[0][y],anz_eimer[1][y],anz_eimer[2][y],last_faktor_char);
						}
						else
						{
							sprintf(line_a,"[%d;48HC%1x%-8.8s% 4d% 4d% 4d %s %s",y+4,9+y,username[y],anz_eimer[0][y],anz_eimer[1][y],anz_eimer[2][y],last_faktor_char,next_hh_mm);
						}
					}
				}
			}
		}
		PutText(line_a);
		if(ptr->all->game[game].player[y].userid==z->id)
		{
			sprintf(line_a,"[37m[%d;56H<",y+4);
			PutText(line_a);
		}
	}
}

void print_brett(void)
{
	UBYTE colorcool;
	char fuellung[14];

	if(game==-1)
	{
		x=rand()%titelmax;
		sprintf(&path[titelansi][12],"%2.2d",x);
		print_text(titelansi);
		scrollx(29,"C1Welcome to Flut! C9v4C7   C2Org Kongoman C3Prg Qwert C5Ansis Miracle C6Docu Fate/1oo%   ");
	}
	else
	{
		if(join[game] && ptr->all->game[game].anzahl==0)
		{
			print_text(joinansi);
		}
		else
		{
			PutText("C7@2");

			y=0;
			while(y<ytab)
			{
				y++;
				scrolly("");
			}
			for(y=0;y<=ptr->all->game[game].y_size;y++)
			{
				if(ansidesign || y<ptr->all->game[game].y_size)
				{
					if(xtab==0)
					{
						p=&line_a[0];
						p2=&line_b[0];
					}
					else
					{
						sprintf(line_a,"[%2.2dC",xtab);
						strcpy(line_b,line_a);
						p=&line_a[5];
						p2=&line_b[5];
					}
				}
				for(x=0;x<ptr->all->game[game].x_size;x++)
				{
					if(ansidesign)
					{
						if(x==0)
						{
							if(y==0)
							{
								p=stpcpy(p,"ÚÄÄ");
							}
							else
							{
								if(y==ptr->all->game[game].y_size)
								{
									p=stpcpy(p,"ÀÄÄ");
								}
								else
								{
									p=stpcpy(p,"ÃÄÄ");
								}
							}
						}
						else
						{
							if(y==0)
							{
								p=stpcpy(p,"ÂÄÄ");
							}
							else
							{
								if(y==ptr->all->game[game].y_size)
								{
									p=stpcpy(p,"ÁÄÄ");
								}
								else
								{
									p=stpcpy(p,"ÅÄÄ");
								}
							}
						}
						p2=stpcpy(p2,"³  ");
					}
					else
					{
						p=stpcpy(p," _  ");
						p2=stpcpy(p2,"(_) ");
					}
				}

				if(ansidesign)
				{
					if(y==0)
					{
						p=stpcpy(p,"¿ ");
					}
					else
					{
						if(y==ptr->all->game[game].y_size)
						{
							p=stpcpy(p,"Ù ");
						}
						else
						{
							p=stpcpy(p,"´ ");
						}
					}
					p2=stpcpy(p2,"³ ");
				}
				else
				{
					if(y<ptr->all->game[game].y_size)
					{
						p2--;
						p2=stpcpy(p2,"[4m");
					}
				}

				if(y<ptr->all->game[game].y_size)
				{
					*(p-1)=0;
					colorcool=0;

					for(x=0;x<ptr->all->game[game].x_size;x++)
					{
						if(ptr->all->game[game].fuellgrad[x][y]>0)
						{
							if(colorcool==ptr->all->game[game].farbe[x][y])
							{
								if(ansidesign)
								{
									sprintf(fuellung,"[30;%dH%2.2s",x*3+2+xtab,batzen+(ptr->all->game[game].fuellgrad[x][y]-1)*2);
								}
								else
								{
									sprintf(fuellung,"[30;%dH%d",x*4+2+xtab,ptr->all->game[game].fuellgrad[x][y]);
								}
							}
							else
							{
								if(ansidesign)
								{
									sprintf(fuellung,"[30;%dHC%1x%2.2s",x*3+2+xtab,ptr->all->game[game].farbe[x][y]+8,batzen+(ptr->all->game[game].fuellgrad[x][y]-1)*2);
								}
								else
								{
									sprintf(fuellung,"[30;%dHC%1x%d",x*4+2+xtab,ptr->all->game[game].farbe[x][y]+8,ptr->all->game[game].fuellgrad[x][y]);
								}
							}
							p2=stpcpy(p2,fuellung);
							colorcool=ptr->all->game[game].farbe[x][y];
						}
					}
					scrolly(line_a);
					scrolly(line_b);
					PutText("[0m");
				}
			}
			if(ansidesign)
			{
				scrolly(line_a);
				y=1;
			}
			else
			{
				y=0;
			}

			while(y<ytab+1)
			{
				y++;
				scrolly("");
			}
		}
	}
}

void tabulator(void)
{
	xtab=(80-4*ptr->all->game[game].x_size)/2;
	ytab=8-ptr->all->game[game].y_size;
	if(ansidesign)
	{
		xtab+=ptr->all->game[game].x_size/2;
	}
}

void selectorjoin(void)
{
	altx=(ptr->all->game[game].x_size-1)/2;
	alty=(ptr->all->game[game].y_size-1)/2;

	for(y=0;y<6;y++)
	{
		for(x=0;x<21;x++)
		{
			username[y][x]=0;
		}
	}

	if(ptr->all->game[game].anzahl>0)
	{
		max_eimer=ptr->all->game[game].x_size*ptr->all->game[game].y_size;

		tabulator();

		wertung_a=ptr->all->game[game].x_size*ptr->all->game[game].y_size*ptr->all->game[game].anzahl/6;
		wertung_b=ptr->all->game[game].x_size*ptr->all->game[game].y_size/(-6);

		sprintf(line_a,"[0m[7;9H%3.3d/%2.2d",wertung_a,wertung_b);
		PutText(line_a);
	}

	akt_user=0;
	while(ptr->all->game[game].player[akt_user].userid!=z->id && akt_user<6)
	{
		akt_user++;
	}
	akt_user++;

	if(game>49)
	{
		ptr->olgpointer=ptr->all->olg[game-50].pointer;
	}
}

void print_status(void)
{
	if(game>49) 
	{
		PutText("[13;55H    ");
	}
	else
	{
		if(ptr->all->game[game].player[akt_user-1].last_wall_lese=='x' && akt_user<7)
		{
			PutText("[13;55HC9WC1allC7");
		}
		else
		{
			PutText("[13;55HCFWC7all");
		}
	}

	if(game_print==0 || (join[game] && ptr->all->game[game].player[0].userid==0))
	{
		PutText("C7[7;9H-      [8;9H-     [9;9H-  ");
	}
	else
	{
		if(ptr->all->game[game].status==8)
		{
			PutText("[8;9HC1X-Flut");
		}
		else
		{
			PutText("[8;9HC7Normal");
		}
		if(ptr->all->game[game].border==0)
		{
			PutText("[9;9HC1No ");
		}
		else
		{
			PutText("[9;9HC7Yes");
		}
	}
	if(game_print>0)
	{
		if(join[game])
		{
			PutText("[13;1HCFJC7oin game");
		}
		else
		{
			if(turn[game])
			{
				sprintf(taker,"[13;1HC%1xTC%1dake turn",akt_user+8,akt_user);
				PutText(taker);
			}
			else
			{
				PutText("[13;1H         ");
			}
		}
	}
	else
	{
		PutText("[13;1H         ");
	}
}

void select()
{
	if(game_neu<0 || game_neu>60)
	{
		sprintf(line_a,"uups game %d?",game_neu);
		scrollx(infoline,line_a);
	}
	else
	{
		if(game_neu==game_print)
		{
			sprintf(line_a,"this IS game %d!",game_neu);
			scrollx(infoline,line_a);
		}
		else
		{
			game_neu--;

			ptr->game_neu=game_neu;
			ptr->game_alt=game;

			send_message(MSG_GAME);

			if(ptr->result)
			{
				game=game_neu;
				game_print=game+1;

				selectorjoin();
				print_players();
				print_games();
				print_status();
				print_brett();
			}
			else
			{
				if(game_neu>49)
				{
					scrollx(infoline,"somebody creates a new battlefield - please wait");
				}
				else
				{
					scrollx(infoline,"sorry - in use!");
				}
			}
		}
	}
	sprintf(line_a,"[6;9HC7%2.2d",game_print);
	PutText(line_a);
}

void print_clock (void)
{
	time(&today);
	datum_zeit=localtime(&today);
	strftime(line_a,12,"Time %H:%M ",datum_zeit);

	p=&line_a[11];
	p=stpcpy(p,"NextTurn ");
	if(nearest==0)
	{
		stpcpy(p,"??:??");
	}
	else
	{
		datum_zeit=localtime(&nearest);
		strftime(next_hh_mm,6,"%H:%M",datum_zeit);
		if(atoi(next_hh_mm)>11)
		{
			stpcpy(p,"00:00");
		}
		else
		{
			stpcpy(p,next_hh_mm);
		}
	}
	scrollx(infoline,line_a);
}

void pointeradd(void)
{
	if(ptr->pointer==9?(ptr->pointer=0):(ptr->pointer++));
}

void print_changes(void)
{
	if(z->OLMWaiting!=olmyeah)
	{
		olmyeah=z->OLMWaiting;
		if(olmyeah==1)
		{
			scrollx(infoline,"1 OLM waiting");
		}
		else
		{
			sprintf(line_a,"%d OLM's waiting",olmyeah);
			scrollx(infoline,line_a);
		}
	}

	while(ptr->pointer!=ptr->all->pointer)
	{
		scrollx(infoline,ptr->all->mails[ptr->pointer]);
		pointeradd();
	}

	if(game>49 && !reading)
	{
		while(ptr->olgpointer!=ptr->all->olg[game-50].pointer)
		{
			ptr->olgpointer++;
			print_fuellgrad(ptr->all->olg[game-50].fluter[ptr->olgpointer].xpos,ptr->all->olg[game-50].fluter[ptr->olgpointer].ypos,ptr->all->olg[game-50].fluter[ptr->olgpointer].akt_user,ptr->all->olg[game-50].fluter[ptr->olgpointer].fuellgrad);
			PutText("U0");
			print_players();
		}

		if(akt_user<7)
		{
			if(akt_user==ptr->all->olg[game-50].akt_user && !turn[game])
			{
				print_players();
				sprintf(taker,"[13;1HC%1xTC%1dake turn",akt_user+8,akt_user);
				PutText(taker);
				turn[game]=TRUE;
			}

			if(ptr->all->olg[game-50].status[akt_user-1]==1)
			{
				ptr->all->olg[game-50].status[akt_user-1]=0;
				x=rand()%winnermax;
				sprintf(&path[winneransi][13],"%2.2d",x);
				print_text(winneransi);
				returner();
				print_brett();
			}
			if(ptr->all->olg[game-50].status[akt_user-1]==2)
			{
				ptr->all->olg[game-50].status[akt_user-1]=0;
				x=rand()%losermax;
				sprintf(&path[loseransi][12],"%2.2d",x);
				print_text(loseransi);
				returner();
				print_brett();
			}
		}
	}
}

void open_wall(BOOL display)
{
	sprintf(&path[wallspfad][6],"%2.2d",game_print);
	i=0;
	make_path(wallspfad);
	if(fp=fopen(pathwork,"r"))
	{
		while (fgets(wall[i],sizeof(wall[0]),fp))
		{
			if(display)
			{
				PutText(wall[i]);
			}
			i++;
		}
		fclose(fp);
	}
}

void get_wall(void)
{
	PutText("[13;55HCFWC7all");
	print_text(wallansi);

	PutText("[47m[17;1H");

	open_wall(TRUE);

	sprintf(line_a,"[29;17H[31;47m%-8.8s:[30m ",z->user1.Handle);
	PutText(line_a);

	if(EnterLine(32,4096,""))
	{
		make_path(wallspfad);
		if(fp=fopen(pathwork,"w"))
		{
			for(y=0;y<ptr->all->game[game].anzahl;y++)
			{
				ptr->all->game[game].player[y].last_wall_lese='x';
			}

			sprintf(line_a,"[16C[31m%-8.8s:[30m %s\n",z->user1.Handle,z->InBuffer);

			for(j=0;j<=i-1;j++)
			{
				if(j!=0 || i!=12)
				{
					fputs(wall[j],fp);
				}
			}
			fputs(line_a,fp);

			fclose(fp);
		}
	}
	if(akt_user<7)
	{
		ptr->all->game[game].player[akt_user-1].last_wall_lese=' ';
	}

	PutText("[40m");
	print_brett();
}

void cursor(void)
{
	if(ansidesign)
	{
		sprintf(line_a,"C7[%d;%dH³[%d;%dHC%1x<",alty*2+ytab+15,altx*3+xtab+4,y*2+ytab+15,x*3+xtab+4,akt_user+8);
	}
	else
	{
		sprintf(line_a,"[%d;%dH [%d;%dHC%1x<",alty*2+ytab+15,altx*4+xtab+4,y*2+ytab+15,x*4+xtab+4,akt_user+8);
	}
	PutText(line_a);
}

void print_fuellgrad(BYTE xt, BYTE yt, BYTE akt_usert, BYTE fuellgrad)
{
	if(ansidesign)
	{
		sprintf(line_a,"C%1x[%d;%dH%2.2s",akt_usert+8,yt*2+ytab+15,xt*3+xtab+2,batzen+(fuellgrad-1)*2);
	}
	else
	{
		sprintf(line_a,"U1C%1x[%d;%dH%d",akt_usert+8,yt*2+ytab+15,xt*4+xtab+2,fuellgrad);
	}
	PutText(line_a);
}

void flute(BYTE xt, BYTE yt)
{
	if(ptr->all->game[game].border==0)
	{
		if(xt<0) xt=ptr->all->game[game].x_size-1;
		if(yt<0) yt=ptr->all->game[game].y_size-1;
		if(xt==ptr->all->game[game].x_size) xt=0;
		if(yt==ptr->all->game[game].y_size) yt=0;
	}
	if(xt>=0 && xt<ptr->all->game[game].x_size && yt>=0 && yt<ptr->all->game[game].y_size && ptr->all->game[game].fuellgrad[xt][yt]<3)
	{
		ptr->all->game[game].fuellgrad[xt][yt]++;
		ptr->all->game[game].farbe[xt][yt]=akt_user;
		if(ptr->all->game[game].fuellgrad[xt][yt]==3)
		{
			x_flut[stacker]=xt;
			y_flut[stacker]=yt;
			stacker++;
		}

		print_fuellgrad(xt,yt,akt_user,ptr->all->game[game].fuellgrad[xt][yt]);

		if(game>49)
		{
			ptr->all->olg[game-50].fluter[ptr->all->olg[game-50].pointer+1].xpos=xt;
			ptr->all->olg[game-50].fluter[ptr->all->olg[game-50].pointer+1].ypos=yt;
			ptr->all->olg[game-50].fluter[ptr->all->olg[game-50].pointer+1].akt_user=akt_user;
			ptr->all->olg[game-50].fluter[ptr->all->olg[game-50].pointer+1].fuellgrad=ptr->all->game[game].fuellgrad[xt][yt];
			ptr->all->olg[game-50].pointer++;
		}
	}
}

void fuelle(BYTE xt, BYTE yt)
{
	ptr->all->game[game].farbe[xt][yt]=akt_user;
	ptr->all->game[game].fuellgrad[xt][yt]++;

	print_fuellgrad(xt,yt,akt_user,ptr->all->game[game].fuellgrad[xt][yt]);

	if(game>49)
	{
		ptr->all->olg[game-50].fluter[ptr->all->olg[game-50].pointer+1].xpos=xt;
		ptr->all->olg[game-50].fluter[ptr->all->olg[game-50].pointer+1].ypos=yt;
		ptr->all->olg[game-50].fluter[ptr->all->olg[game-50].pointer+1].akt_user=akt_user;
		ptr->all->olg[game-50].fluter[ptr->all->olg[game-50].pointer+1].fuellgrad=ptr->all->game[game].fuellgrad[xt][yt];
		ptr->all->olg[game-50].pointer++;
	}

	ptr->punkte=0;
	if(ptr->all->game[game].fuellgrad[xt][yt]==3)
	{
		x_flut[0]=xt;
		y_flut[0]=yt;
		stacker=1;

		do
		{
			stacker--;
			ptr->punkte++;;
			xt=x_flut[stacker];
			yt=y_flut[stacker];	

			if(ptr->all->game[game].status==4)
			{
				flute(xt-1,yt);
				flute(xt+1,yt);
				flute(xt,yt-1);
				flute(xt,yt+1);
			}
			else
			{
				flute(xt-1,yt-1);
				flute(xt+1,yt+1);
				flute(xt+1,yt-1);
				flute(xt-1,yt+1);
			}
		}
		while(stacker>0);
	}
}

void returner()
{
	key[0]=0;
	do
	{
		key[0]=WaitForInput(500000);
		print_changes();
	}
	while (key[0]==0 && z->TimeLeft!=0);
}

void killgame(void)
{
	for(i=0;i<ptr->all->game[game].x_size;i++)
	{
		for(j=0;j<ptr->all->game[game].y_size;j++)
		{
			ptr->all->game[game].farbe[i][j]=0;
			ptr->all->game[game].fuellgrad[i][j]=0;
		}
	}

	for(i=0;i<6;i++)
	{
		ptr->all->game[game].player[i].userid=0;
	}

	ptr->all->game[game].anzahl=0;
	ptr->all->game[game].x_size=0;
	ptr->all->game[game].y_size=0;
	join[game]=TRUE;
}

void turner(void)
{
	if(game_print>0 && turn[game])
	{
		x=altx;
		y=alty;

		cursor();

		ready=FALSE;

		key[0]=0;
		do
		{
			key[0]=WaitForInput(500000);
			print_changes();
			switch (key[0])
			{
				case 'A':
				{
					altx=x;
					alty=y;
					if(y>0?(y--):(y=ptr->all->game[game].y_size-1));
					cursor();
					break;
				}
				case 'B':
				{
					altx=x;
					alty=y;
					if(y<(ptr->all->game[game].y_size-1)?(y++):(y=0));
					cursor();
					break;
				}
				case 'C':
				{
					altx=x;
					alty=y;
					if(x<(ptr->all->game[game].x_size-1)?(x++):(x=0));
					cursor();
					break;
				}
				case 'D':
				{
					altx=x;
					alty=y;
					if(x>0?(x--):(x=ptr->all->game[game].x_size-1));
					cursor();
					break;
				}
				case 13:
				{
					if((ptr->all->game[game].farbe[x][y]!=akt_user && ptr->all->game[game].fuellgrad[x][y]!=0) || ptr->all->game[game].fuellgrad[x][y]>2)
					{
						if(ptr->all->game[game].farbe[x][y]!=akt_user && ptr->all->game[game].fuellgrad[x][y]!=0)
						{
							scrollx(infoline,"oh no - select your own ones!");
						}
						else
						{
							if(ptr->all->game[game].fuellgrad[x][y]>2)
							{
								scrollx(infoline,"oh no - this one is full!");
							}
						}
					}
					else
					{
						ready=TRUE;
					}
				}
			}
		}
		while (!ready && z->TimeLeft!=0);

		if(ansidesign)
		{
			sprintf(line_a,"C7[%d;%dH³",y*2+ytab+15,x*3+xtab+4);
		}
		else
		{
			sprintf(line_a,"[%d;%dH ",y*2+ytab+15,x*4+xtab+4);
		}
		PutText(line_a);

		if(ready)
		{
			altx=x;
			alty=y;

			fuelle(x,y);

			if(game>49)
			{
				ptr->olgpointer=ptr->all->olg[game-50].pointer;
			}


			PutText("[0m");

			if(today-ptr->all->game[game].player[akt_user-1].last_move>=86400*3)
			{
				ptr->all->game[game].player[akt_user-1].last_move=today-86400;
			}
			else
			{
				ptr->all->game[game].player[akt_user-1].last_move=today;
			}
			
			print_players();

			count_eimer();

			gesamt_voll=0;
			groesster=0;

			for(y=0;y<ptr->all->game[game].anzahl;y++)
			{
				ptr->status[y]=0;
				gesamt_voll+=anz_eimer[2][y];
				if(anz_eimer[2][y]>=groesster)
				{
					zweiter=groesster;
					groesster=anz_eimer[2][y];
				}
				else
				{
					if(anz_eimer[2][y]>zweiter)
					{
						zweiter=anz_eimer[2][y];
					}
				}
			}

			for(y=0;y<6;y++)
			{
				if(y<ptr->all->game[game].anzahl)
				{
					ptr->name[y]=&username[y][0];
				}
				else
				{
					ptr->name[y]=&leer[0];
				}
			}

			if(zweiter+max_eimer-gesamt_voll<groesster || max_eimer==gesamt_voll)
			{
				for(y=0;y<6;y++)
				{
					if(anz_eimer[2][y]==groesster)
					{
						ptr->status[y]=1;
					}
					else
					{
						ptr->status[y]=2;
					}
				}
			}

			if(game>49)
			{
				PutText("[13;1H         ");
				turn[game]=FALSE;
			}

			ptr->akt_user=akt_user;
			send_message(MSG_SCORES);

			print_players();

			if(zweiter+max_eimer-gesamt_voll<groesster || max_eimer==gesamt_voll)
			{
				turn[game]=FALSE;
				if(game>49)
				{
					ptr->all->olg[game-50].status[akt_user-1]=0;
					ptr->all->olg[game-50].akt_user=0;
				}
				if(anz_eimer[2][akt_user-1]==groesster)
				{
					x=rand()%winnermax;
					sprintf(&path[winneransi][13],"%2.2d",x);
					print_text(winneransi);
				}
				else
				{
					x=rand()%losermax;
					sprintf(&path[loseransi][12],"%2.2d",x);
					print_text(loseransi);
				}
				returner();

				if(ptr->result)
				{
					print_hall();
				}
				else
				{
					print_brett();
				}

				if(game<50)
				{
					open_wall(FALSE);
					make_path(wallspfad);
					if(fp=fopen(pathwork,"w"))
					{
						for(j=0;j<=i-1;j++)
						{
							if(j!=0 || i!=12)
							{
								fputs(wall[j],fp);
							}
						}
						fputs("[16C[30m------------------------------------------\n",fp);
						fclose(fp);
					}
					killgame();
				}
			}
			else
			{
				if(game>49)
				{
					do
					{
						if(ptr->all->olg[game-50].akt_user==ptr->all->game[game].anzahl)
						{
							ptr->all->olg[game-50].akt_user=1;
						}
						else
						{
							ptr->all->olg[game-50].akt_user++;
						}
					}
					while(anz_angefasst==max_eimer && anz_eimer[0][ptr->all->olg[game-50].akt_user-1]+anz_eimer[1][ptr->all->olg[game-50].akt_user-1]==0);
				}
				if(ptr->result)
				{
					print_hall();
				}
			}
			if(game<50)
			{
				print_games();
			}
			print_status();

		}
	}
}

void joiner(void)
{
	akt_user=0;
	while(ptr->all->game[game].player[akt_user].userid!=0 && ptr->all->game[game].anzahl>0)
	{
		if(game>49)
		{
			ptr->all->game[game].player[akt_user].last_move=today;
		}
		else
		{
			ptr->all->game[game].player[akt_user].last_move=today-86400;
		}
		akt_user++;
	}
	ptr->all->game[game].player[akt_user].userid=z->id;

	if(game>49)
	{
		ptr->all->game[game].player[akt_user].last_move=today;
	}
	else
	{
		ptr->all->game[game].player[akt_user].last_move=today-86400;
	}

	akt_user++;

	join[game]=FALSE;

	if(ptr->all->game[game].anzahl==0)
	{
		print_text(layoutansi);

		ptr->all->game[game].status=4;
		ptr->all->game[game].border=1;
		ptr->all->game[game].x_size=12;
		ptr->all->game[game].y_size=6;
		ptr->all->game[game].anzahl=4;

		key[0]=0;
		do
		{
			key[0]=WaitForInput(500000);

			print_changes();

			if(key[0]>49 && key[0]<55)
			{
				ptr->all->game[game].anzahl=key[0]-48;
				key[0]='?';
			}

			switch (key[0])
			{
				case 'g':
				{
					if(ptr->all->game[game].status==8)
					{
						ptr->all->game[game].status=4;
						PutText("[0;34;42m[22;27HNormal");
					}
					else
					{
						ptr->all->game[game].status=8;
						PutText("[0;34;42m[22;27HX-Flut");
					}
					break;
				}
				case 'b':
				{
					if(ptr->all->game[game].border==0)
					{
						ptr->all->game[game].border=1;
						PutText("[0;35;42m[23;53HYes");
					}
					else
					{
						ptr->all->game[game].border=0;
						PutText("[0;35;42m[23;53HNo ");
					}
					break;
				}
				case 'A':
				{
					if(ptr->all->game[game].y_size<8) ptr->all->game[game].y_size++;
					sprintf(line_a,"[1;36;42m[18;42H%2.2d",ptr->all->game[game].y_size);
					PutText(line_a);
					break;
				}
				case 'B':
				{
					if(ptr->all->game[game].y_size>4) ptr->all->game[game].y_size--;
					sprintf(line_a,"[1;36;42m[18;42H%2.2d",ptr->all->game[game].y_size);
					PutText(line_a);
					break;
				}
				case 'C':
				{
					if(ptr->all->game[game].x_size<20) ptr->all->game[game].x_size++;
					sprintf(line_a,"[1;36;42m[18;37H%2.2d",ptr->all->game[game].x_size);
					PutText(line_a);
					break;
				}
				case 'D':
				{
					if(ptr->all->game[game].x_size>4) ptr->all->game[game].x_size--;
					sprintf(line_a,"[1;36;42m[18;37H%2.2d",ptr->all->game[game].x_size);
					PutText(line_a);
					break;
				}
				case '?':
				{
					sprintf(line_a,"[1;33;42m[25;30H%d",ptr->all->game[game].anzahl);
					PutText(line_a);
					break;
				}
			}
		}
		while (key[0]!=13 && z->TimeLeft!=0);

		PutText("[0m");

		for(y=0;y<ptr->all->game[game].anzahl;y++)
		{
			if(game>49)
			{
				ptr->all->olg[game-50].status[y]=0;
			}
			ptr->all->game[game].player[y].last_wall_lese=' ';
		}

		if(game>49)
		{
			ptr->all->olg[game-50].akt_user=1;
			ptr->all->olg[game-50].joined=1;
			ptr->all->olg[game-50].pointer=0;

			sprintf(line_a,"I've started OLG #%d (%d players)",game+1,ptr->all->game[game].anzahl);
			ptr->mess=&line_a[0];
			send_message(MSG_MESS);
		}

		selectorjoin();
		print_players();
		print_games();
		print_status();
		print_brett();
	}
	else
	{
		print_players();
		print_games();
		print_status();
	}
}

void print_ranks(void)
{
	print_text(rankansi);
	PutText("CFZ1[1;1H");
	for(x=0;x<2;x++)
	{
		for(i=0;i<12;i++)
		{
			tab[i].wert=0;
			tab[i].ptr=0;
		}
		i=0;
		while(ptr->all->user[i].userid>0)
		{
			for(j=0;j<10;j++)
			{
				if(ptr->all->user[i].points[x]>=tab[j].wert || tab[j].ptr==0)
				{
					for(k=9;k>=j;k--)
					{
						tab[k+1].wert=tab[k].wert;
						tab[k+1].ptr=tab[k].ptr;
					}
					tab[j].wert=ptr->all->user[i].points[x];
					tab[j].ptr=i+1;
					j=9;
				}
			}
			if(ptr->all->user[i].userid==z->id)
			{
				tab[11].wert=ptr->all->user[i].points[x];
				tab[11].ptr=i+1;
			}
			i++;
		}
		rankyes=FALSE;
		for(i=0;i<10;i++)
		{
			if(tab[i].ptr==0)
			{
				sprintf(line_a,"[%d;%dHno one...",16+i,9+x*36);
			}
			else
			{
				if(ptr->all->user[tab[i].ptr-1].userid==z->id)
				{
					rankyes=TRUE;
				}
				if(ReadAccount(ptr->all->user[tab[i].ptr-1].userid,&z->user2))
				{
					sprintf(line_a,"[%d;%dH%-20.20s %5d",16+i,9+x*36,z->user2.Handle,tab[i].wert);
				}
			}
			PutText(line_a);
		}
		if(tab[11].ptr!=0 && !rankyes)
		{
			sprintf(line_a,"[27;%dH%-20.20s %5d",9+x*36,z->user1.Handle,tab[11].wert);
			PutText(line_a);
		}
		PutText("Z4");
	}
	PutText("[0m");
	returner();
	print_brett();
}

void print_hall(void)
{
	print_text(hallansi);
	for(x=0;x<2;x++)
	{
		PutText("Z7C0[19;1H");
		for(y=0;y<10;y++)
		{
			PutText(ptr->all->fame.hall[x][y]);
		}
	}
	PutText("Z0");
	returner();
	print_brett();
}

void fluter(long fluterid)
{
	if(ReadAccount(fluterid,&z->user2))
	{
		if(z->user2.Handle[0]!='!');
		{
			sprintf(line_a,"%-10d%-10d%s\n",fluterid,z->user2.IDNumber,z->user2.Handle);
			fputs(line_a,fp);
		}
	}
}

void operator(void)
{
	print_text(chefansi);

	key[0]=0;
	do
	{
		key[0]=WaitForInput(500000);

		print_changes();

		switch (key[0])
		{
			case 'd':
			{
				if(game>-1)
				{
					killgame();

					selectorjoin();
					print_players();
					print_games();
					print_status();
				}
				break;
			}
			case 't':
			{
				print_text(testansi);
				returner();
				break;
			}
			case 'c':
			{
				PutText("[21;33H");
				if(EnterLine(2,4096,""))
				{
					game_sysop=atoi(z->InBuffer);
				}
				else
				{
					game_sysop=0;
				}
				if(game_sysop>=0)
				{
					if(game_sysop==0)
					{
						if(fp=fopen(routepfad,"w"))
						{
							y=0;
							while(ptr->all->user[y].userid!=0)
							{
								fluter(ptr->all->user[y].userid);
								y++;
							}
						}
					}
					else
					{
						game_sysop--;
						if(fp=fopen(routepfad,"w"))
						{
							for(y=0;y<ptr->all->game[game_sysop].anzahl;y++)
							{
								if(ptr->all->game[game_sysop].player[y].userid!=0)
								{
									fluter(ptr->all->game[game_sysop].player[y].userid);
								}
							}
							fclose(fp);
						}
					}
				}
				break;
			}
		}
	}
	while (key[0]==0 && z->TimeLeft!=0);

	print_brett();
}


void mymain(void)
{
	SetDoing("Flut! v4");

	moremode=z->user1.MoreMode;
	z->user1.MoreMode=0;

	print_players();
	print_games();
	print_status();
	print_brett();

	if(z->SysMaint==1234567890)
	{
		scrollx(infoline,"Hi sysop - you've got an extra menu - try 'o' to invoke it           ");
	}

	key[0]=0;
	do
	{
		key[0]=WaitForInput(500000);

		print_changes();

		switch (key[0])
		{
			case '+':
			{
				if(game_print<60?(game_neu=game_print+1):(game_neu=1));
				select();
				break;
			}
			case '-':
			{
				if(game_print>1?(game_neu=game_print-1):(game_neu=60));
				select();
				break;
			}
			case 'a':
			{
				game_neu=game_print;
				do
				{
					if(game_neu<60?(game_neu++):(game_neu=0));
				}
				while((!turn[game_neu-1] || game_neu==0) && game_neu!=game_print);

				if(game_neu!=game)
				{
					select();
				}
				break;
			}
			case 'c':
			{
				print_clock();
				break;
			}
			case 'm':
			{
				reading=TRUE;
				print_text(messansi);
				PutText("[21;12H[30;47m");
				if(EnterLine(56,4096,""))
				{
					ptr->mess=&z->InBuffer[0];
					PutText("[0m");
					print_changes();
					send_message(MSG_MESS);
				}
				else
				{
					PutText("[0m");
				}
				print_brett();
				reading=FALSE;
				break;
			}
			case 'i':
			{
				reading=TRUE;
				go=TRUE;
				path[helpansi][11]='a';

				print_text(helpansi);

				do
				{
					key[0]=WaitForInput(500000);
					print_changes();

					if(key[0]!=0)
					{
						if(key[0]>='a' && key[0] <= 'o')
						{
							if(path[helpansi][11]!=key[0])
							{
								path[helpansi][11]=key[0];
								print_text(helpansi);
							}
						}
						else
						{
							if(key[0]==13)
							{
								if(path[helpansi][11]=='o')
								{
									path[helpansi][11]='a';
								}
								else
								{
									path[helpansi][11]++;
								}
								print_text(helpansi);
							}
							else
							{
								go=FALSE;
							}
						}
					}
				}
				while (go && z->TimeLeft!=0);

				print_brett();
				reading=FALSE;
				break;
			}
			case 's':
			{
				PutText("[6;9H  [2D[0m");
				if(EnterLine(2,4096,""))
				{
					game_neu=atoi(z->InBuffer);
				}
				else
				{
					game_neu=game_print;
				}
				select();
				break;
			}
			case 'w':
			{
				if(game<50)
				{
					get_wall();
				}
				break;
			}
			case 't':
			{
				turner();
				break;
			}
			case 'n':
			{
				reading=TRUE;
				scrolly("");
				scrolly("CBZ4Date     Z0 Z4GameZ0 Z4Points Z0 Z4C9Winner(s)CF/CALoser(s)                                 Z0");
				for(y=0;y<15;y++)
				{
					strcpy(line_a,ptr->all->fame.news[y]);
					x=0;
					while(line_a[x]!=10 && line_a[x]!=0)
					{
						x++;
					}
					line_a[x]=0;
					scrolly(line_a);
				}
				PutText("[1;1H");
				returner();
				print_brett();
				reading=FALSE;
				break;
			}
			case 'd':
			{
				if(ansidesign)
				{
					scrollx(infoline,"AnsiDesign disabled");
				}
				else
				{
					scrollx(infoline,"AnsiDesign enabled");
				}
				ansidesign=!ansidesign;
				tabulator();
				if(game_print>0)
				{
					print_brett();
				}
				break;
			}
			case 'o':
			{
				if(z->SysMaint==1234567890)
				{
					reading=TRUE;
					operator();
					reading=FALSE;
				}
				break;
			}
			case 'h':
			{
				reading=TRUE;
				print_hall();
				reading=FALSE;
				break;
			}
			case 'r':
			{
				reading=TRUE;
				print_ranks();
				reading=FALSE;
				break;
			}
			case 'p':
			{
				break;
			}
			case 'j':
			{
				if(game_print>0 && game_print<61 && join[game])
				{
					reading=TRUE;
					joiner();
					reading=FALSE;
				}
				break;
			}
		}
	}
	while (key[0]!=113 && z->TimeLeft!=0);

	ptr->timeleft=z->TimeLeft;

	ptr->game_alt=game;
	ptr->game_neu=-1;
	send_message(MSG_GAME);

	z->user1.MoreMode=moremode;
}
