Beispiel #1
0
main(void)
{
    char door=0,room[SQUARE][SQUARE],str[128];
    unsigned char ch,val;
    int file,v=0,w=0,x=0,y=0,z=1,weapons[10],weapons2[10],armor[10],armor2[10],
        magic[10],magic2[10],num_monster,num_gold,fountain[10],stairs,staff;

    xp_randomize();
    unlink("tbdmap.dab");
    if((file=open("tbdmap.dab",O_RDWR|O_BINARY|O_CREAT,S_IWRITE|S_IREAD))==-1) {
        printf("Error opening map file\r\n");
        exit(0); }

for (w=0;w<LEVELS;w++) {
    printf("\r\n\r\nCreating The Beast's Domain - Dungeon Level #%d\r\n\r\n",
           w+1);
    lseek(file,w*SQUARE*SQUARE,SEEK_SET);
    for (x=0;x<SQUARE*SQUARE;x++) {
        for (y=0;y<7;y++) {         /* number of possible doors in each room */
            if (xp_random(4)) {
                switch(y) {
                    case 0:
                        if (x>SQUARE-1) door|=N;
                        break;
                    case 1:
                        if (x<(SQUARE*SQUARE)-SQUARE) door|=S;
                        break;
                    case 2:
                        if ((x%SQUARE)<SQUARE-1) door|=E;
                        break;
                    case 3:
                        if ((x%SQUARE)>0) door|=W;
                        break;
                    case 4:
                        if (x>SQUARE-1 && (x%SQUARE)>0) door|=NW;
                        break;
                    case 5:
                        if (x>SQUARE-1 && (x%SQUARE)<SQUARE-1) door|=NE;
                        break;
                    case 6:
                        if (x<(SQUARE*SQUARE)-SQUARE && (x%SQUARE)>0) door|=SW;
                        break;
                    case 7:
                        if (x<(SQUARE*SQUARE)-SQUARE && (x%SQUARE)<SQUARE-1)
                            door|=SE;
                        break;
                }
            }
        }
        if (!door) {
            if(w!=7) {
                if (x>SQUARE-1) door|=N;
                else if (x<(SQUARE*SQUARE)-SQUARE) door|=S;
                else if ((x%SQUARE)<SQUARE-1) door|=E;
                else if ((x%SQUARE)>0) door|=W;
    /*          else { door|=N; door|=S; door|=E; door|=W; } */
            }
        }
        if((w==0 || w==1 || w==5 || w==6) && (x>=0 && x<=SQUARE-1))
            door|=N;
        if(w==7 && x==SQUARE/2) door|=N;
        if(w==7 && x!=SQUARE/2) door&=~N;
        if((w==1 || w==2 || w==4 || w==5) && (x>=(SQUARE*SQUARE)-SQUARE
            && x<=(SQUARE*SQUARE)))
            door|=S;
        if(w==8 && x==((SQUARE*SQUARE)-(SQUARE/2))) door|=S;
        if(w==8 && x!=((SQUARE*SQUARE)-(SQUARE/2))) door&=~S;
        if((w==3 || w==4 || w==6) && ((x%SQUARE)==SQUARE-1))
            door|=E;
        if((w==2 || w==3 || w==7) && ((x%SQUARE)==0))
            door|=W;
        if(w==0) door&=~W;
        if(w==7) door&=~E;

        write(file,&door,1);
        printf("%d",z++); if (z>SQUARE) { z=1; printf("\r\n"); } door=0;
    }
    lseek(file,w*SQUARE*SQUARE,SEEK_SET);
    for (y=0;y<SQUARE;y++)
        for (x=0;x<SQUARE;x++) read(file,&room[y][x],1);
    printf("\r\nChecking for room throughways...\r\n");
    for (y=0;y<SQUARE;y++) {
            printf("\r\n");
        for (x=0;x<SQUARE;x++) {
            printf("%d",x+1);
            if (y>0 && (room[y][x]&N))
                if (!(room[y-1][x]&S)) room[y-1][x]|=S;
            if (y<SQUARE-1 && (room[y][x]&S))
                if (!(room[y+1][x]&N)) room[y+1][x]|=N;
            if (x>0 && (room[y][x]&W))
                if (!(room[y][x-1]&E)) room[y][x-1]|=E;
            if (x<SQUARE-1 && (room[y][x]&E))
                if (!(room[y][x+1]&W)) room[y][x+1]|=W;
            if (y>0 && x>0 && (room[y][x]&NW))
                if (!(room[y-1][x-1]&SE)) room[y-1][x-1]|=SE;
            if (y>0 && x<SQUARE-1 && (room[y][x]&NE))
                if (!(room[y-1][x+1]&SW)) room[y-1][x+1]|=SW;
            if (y<SQUARE-1 && x>0 && (room[y][x]&SW))
                if (!(room[y+1][x-1]&NE)) room[y+1][x-1]|=NE;
            if (y<SQUARE-1 && x<SQUARE-1 && (room[y][x]&SE))
                if (!(room[y+1][x+1]&NW)) room[y+1][x+1]|=NW;
        }
    }
    lseek(file,w*SQUARE*SQUARE,SEEK_SET);
    for (y=0;y<SQUARE;y++)
        for (x=0;x<SQUARE;x++) write(file,&room[y][x],1);
}
    close(file);

    for (x=0;x<LEVELS;x++) {
        do {
            weapons[x]=(rand()%(SQUARE*SQUARE));
            armor[x]=(rand()%(SQUARE*SQUARE));
            magic[x]=(rand()%(SQUARE*SQUARE));
            fountain[x]=(rand()%(SQUARE*SQUARE));
        } while (weapons[x]==armor[x] || weapons[x]==magic[x] || armor[x]==
                 magic[x] || fountain[x]==magic[x] || fountain[x]==armor[x] ||
                 fountain[x]==weapons[x]);
        do {
            weapons2[x]=(rand()%(SQUARE*SQUARE));
            armor2[x]=(rand()%(SQUARE*SQUARE));
            magic2[x]=(rand()%(SQUARE*SQUARE));
        } while(weapons2[x]==armor2[x] || weapons2[x]==magic2[x] || armor2[x]==
                magic2[x]);
    }
#if 0
    clrscr();
#endif
    unlink("tbdobj.dab");
    if((file=open("tbdobj.dab",O_RDWR|O_BINARY|O_CREAT,S_IWRITE|S_IREAD))==-1) {
        printf("Error opening object data file\r\n");
        exit(0); }

    stairs=xp_random(SQUARE*SQUARE);
    do { staff=xp_random(SQUARE*SQUARE); } while(staff==stairs);
for (w=0;w<LEVELS;w++) {
    if(w<8) {
        printf("\r\n\r\nWeapons Shop #1 in Room #%d\r\n",weapons[w]+1);
        printf("Weapons Shop #2 in Room #%d\r\n",weapons2[w]+1);
        printf("Armor Shop #1 in Room   #%d\r\n",armor[w]+1);
        printf("Armor Shop #2 in Room   #%d\r\n",armor2[w]+1);
        printf("Magic Shop #1 in Room   #%d\r\n",magic[w]+1);
        printf("Magic Shop #2 in Room   #%d\r\n",magic2[w]+1);
        printf("Magic Fountain in Rm #%d\r\n",fountain[w]+1);
        printf("\r\n\r\nStocking The Beast's Domain - Dungeon Level #%d\r\n\r\n",
               w+1);
    }
    for (x=0;x<SQUARE*SQUARE;x++) {
        num_gold=num_monster=0;
        if(fountain[w]==x && w<8) {
            val=0;
            for(y=0;y<55;y++) {
                if((y>14 && y<18) || (y>24 && y<30) || (y>36 && y<40))
                    ch=NUM_BLOCKAGE+4;
                else ch=0;
                write(file,&ch,1); write(file,&val,1); }
        } else if((weapons[w]==x || weapons2[w]==x) && w<8) {
            ch=NUM_TRADINGPOST; val=0;
            write(file,&ch,1); write(file,&val,1);
            for (y=0;y<3;y++) {
                for(v=0;v<18;v++) {
                    if(w<5 && w>1) ch=y+NUM_WEAPON+1;
                    else ch=y+4+NUM_WEAPON;
                    write(file,&ch,1); write(file,&val,1);
                }
            }
        } else if((armor[w]==x || armor2[w]==x) && w<8) {
            ch=NUM_TRADINGPOST+1; val=0;
            write(file,&ch,1); write(file,&val,1);
            for (y=0;y<3;y++) {
                for(v=0;v<18;v++) {
                    if(w<5 && w>1) ch=y+NUM_ARMOR;
                    else ch=y+3+NUM_ARMOR;
                    write(file,&ch,1); write(file,&val,1);
                }
            }
        } else if((magic[w]==x || magic2[w]==x) && w<8) {
            ch=NUM_TRADINGPOST+2; val=0;
            write(file,&ch,1); write(file,&val,1);
            for (y=0;y<9;y++) {
                for(v=0;v<6;v++) {
                    ch=y+NUM_SPECIAL;
                    write(file,&ch,1); write(file,&val,1);
                }
            }
        } else {
            for (y=0;y<55;y++) {
                door=ch=val=0;
                if(y!=0 && y!=5 && y!=10 && y!=22 && y!=32 && y!=44 && y!=49 &&
                   y!=54) {
                    if(rand()%10==1) {
                        if(w<5 && w>1) ch=NUM_BLOCKAGE+1;
                        else if(w==1 || w==5) ch=NUM_BLOCKAGE+5;
                        else if(w<8) ch=NUM_BLOCKAGE;
                        else if(w==8 || w==9) ch=NUM_MONSTER+14+rand()%8; }
                } else door=1;
                if(rand()%20==1 && !ch) {
                    ch=(rand()%58)+1;
                    if(ch==NUM_BLOCKAGE) {
                        if(w<5 && w>1) ch=NUM_BLOCKAGE+1;
                        else if(w==1 || w==5) ch=NUM_BLOCKAGE+5;
                        else if(w<8) ch=NUM_BLOCKAGE;
                        else if(w==8 || w==9) ch=NUM_MONSTER+14+rand()%8; } }
                if(object[ch].type!=STAIRS && object[ch].type!=BLOCKAGE &&
                   object[ch].type!=GOLD && ch!=0) {
                    if(object[ch].type==WEAPON) ch=NUM_WEAPON;
                    else if(w<5 && w>1) ch=NUM_MONSTER+rand()%7;
                    else if(w==1 || w==5) ch=NUM_MONSTER+7+rand()%7;
                    else ch=NUM_MONSTER+14+rand()%8; }
                if(object[ch].type==TRADINGPOST && !door) {
                    if(w<5 && w>1) ch=NUM_BLOCKAGE+1;
                    else if(w==1 || w==5) ch=NUM_BLOCKAGE+5;
                    else if(w<8) ch=NUM_BLOCKAGE;
                    else if(w==8 || w==9) ch=NUM_MONSTER+14+rand()%8; }
                if(object[ch].type==TRADINGPOST) {
                    if(w<5 && w>1) ch=NUM_MONSTER+rand()%7;
                    else if(w==1 || w==5) ch=NUM_MONSTER+7+rand()%7;
                    else ch=NUM_MONSTER+14+rand()%8; }
                if(object[ch].type==STAIRS) {
                    if(w<5 && w>1) ch=NUM_MONSTER+rand()%7;
                    else if(w==1 || w==5) ch=NUM_MONSTER+7+rand()%7;
                    else ch=NUM_MONSTER+14+rand()%8; 
                }
                if(object[ch].type==GOLD) {
                    ++num_gold;
                    if(num_gold>1) { ch=0; val=0; }
                    else val=((rand()%4)+1); }
                if(object[ch].type==MONSTER) {
                    ++num_monster;
                    if(num_monster>2 && w<8) { ch=0; val=0; }
                    if(num_monster>3 && w==8) { ch=0; val=0; }
                    if(num_monster>4 && w==9) { ch=0; val=0; }
                    else val=(object[ch].misc*10)+10; }
                if(object[ch].type==BLOCKAGE) {
                    if(w<8 && door) { ch=0; val=0; }
                    if(w<8 && !door) {
                        if(w<5 && w>1) ch=NUM_BLOCKAGE+1;
                        else if(w==1 || w==5) ch=NUM_BLOCKAGE+5;
                        else if(w<8) ch=NUM_BLOCKAGE; } }
                if(y==14 || y==18 || y==36 || y==40) {
                    if(w==8) { ch=NUM_BLOCKAGE+2; val=0; }
                    if(w==9) { ch=NUM_BLOCKAGE+3; val=0; } }
                if(w==9 && staff==x && y==28) { ch=NUM_MONSTER+22;
                    val=(object[ch].misc*10)+10; }
                if(w==8 && stairs==x && y==28) ch=NUM_STAIRS+1;
                if(w==9 && stairs==x && y==28) ch=NUM_STAIRS;
                write(file,&ch,1); write(file,&val,1);
            }
        }
        printf("%d",z++); if(z>SQUARE) { z=1; printf("\r\n"); }
    }
}
close(file);
printf("\r\n\r\nDone!\7");
return 0;
}
Beispiel #2
0
int main()
{
	char	str[128];
	char	compiler[128];
	char	fpath[MAX_PATH+1];
	char*	path = ".";
	char*	glob_pattern = "*wrap*";
	int		i;
	int		ch;
	uint	u;
	time_t	t;
	glob_t	g;
	DIR*	dir;
	DIRENT*	dirent;
	thread_data_t thread_data;
	int	fd;
	int	fd2;
	int	canrelock=0;
	clock_t	ticks;

	/* Show platform details */
	DESCRIBE_COMPILER(compiler);
	printf("%-15s: %s\n","Platform",PLATFORM_DESC);
	printf("%-15s: %s\n","Version",os_version(str));
	printf("%-15s: %s\n","Compiler"	,compiler);
	printf("%-15s: %d\n","Random Number",xp_random(1000));

	for(i=0;i<3;i++) {
		if(_beginthread(
			  sopen_child_thread	/* entry point */
			 ,0  					/* stack size (0=auto) */
			 ,(void*)i				/* data */
			 )==(unsigned long)-1)
			printf("_beginthread failed\n");
		else
			SLEEP(1);
	}
	printf("Waiting for all sopen_child_threads to close...\n");
	SLEEP(5000);	/* wait for all threads to quit */

	/* Exclusive sopen test */
	printf("\nsopen() test\n");
	if((fd=sopen(LOCK_FNAME,O_RDWR|O_CREAT,SH_DENYRW,S_IREAD|S_IWRITE))==-1) {
		perror(LOCK_FNAME);
		return(errno);
	}
	printf("%s is opened with an exclusive (read/write) lock\n",LOCK_FNAME);
	getkey();
	if(_beginthread(
		  sopen_test_thread	/* entry point */
		 ,0  				/* stack size (0=auto) */
		 ,NULL				/* data */
		 )==(unsigned long)-1)
		printf("_beginthread failed\n");
	else
		SLEEP(1000);
	close(fd);

	/* sopen()/lock test */
	printf("\nlock() test\n");
	if((fd=sopen(LOCK_FNAME,O_RDWR|O_CREAT,SH_DENYNO,S_IREAD|S_IWRITE))==-1) {
		perror(LOCK_FNAME);
		return(errno);
	}
	write(fd,"lock testing\n",LOCK_LEN);
	if(lock(fd,LOCK_OFFSET,LOCK_LEN)==0)
		printf("lock() succeeds\n");
	else
		printf("!FAILURE: lock() non-functional (or file already locked)\n");
	if(lock(fd,LOCK_OFFSET,LOCK_LEN)==0)  {
		printf("!FAILURE: Subsequent lock of region was allowed (will skip some tests)\n");
		canrelock=1;
	}
		
	if(_beginthread(
		  lock_test_thread	/* entry point */
		 ,0  				/* stack size (0=auto) */
		 ,NULL				/* data */
		 )==(unsigned long)-1)
		printf("_beginthread failed\n");
	else
		SLEEP(1000);
	if(canrelock)
		printf("?? Skipping some tests due to inability to detect own locks\n");
	else  {
		if(lock(fd,LOCK_OFFSET,LOCK_LEN))
			printf("Locks in first thread survive open()/close() in other thread\n");
		else
			printf("!FAILURE: lock() in first thread lost by open()/close() in other thread\n");
		if(lock(fd,LOCK_OFFSET+LOCK_LEN+1,LOCK_LEN))
			printf("!FAILURE: file locking\n");
		else
			printf("Record locking\n");
	}
	if((fd2=sopen(LOCK_FNAME,O_RDWR,SH_DENYRW))==-1) {
		printf("Cannot reopen SH_DENYRW while lock is held\n");
		close(fd2);
	}
	else  {
		printf("!FAILURE: can reopen SH_DENYRW while lock is held\n");
	}
	if(unlock(fd,LOCK_OFFSET,LOCK_LEN))
		printf("!FAILURE: unlock() non-functional\n");
	if(lock(fd,LOCK_OFFSET+LOCK_LEN+1,LOCK_LEN))
		printf("Cannot re-lock after non-overlapping unlock()\n");
	else
		printf("!FAILURE: can re-lock after non-overlappping unlock()\n");
	if(lock(fd,LOCK_OFFSET,LOCK_LEN))
		printf("!FAILURE: cannot re-lock unlocked area\n");
	close(fd);

	/* getch test */
	printf("\ngetch() test (ESC to continue)\n");
	do {
		ch=getch();
		printf("getch() returned %d\n",ch);
	} while(ch!=ESC);

	/* kbhit test */
	printf("\nkbhit() test (any key to continue)\n");
	while(!kbhit()) {
		printf(".");
		fflush(stdout);
		SLEEP(500);
	}
	getch();	/* remove character from keyboard buffer */

	/* BEEP test */
	printf("\nBEEP() test\n");
	getkey();
	for(i=750;i>250;i-=5)
		BEEP(i,15);
	for(;i<1000;i+=5)
		BEEP(i,15);

	/* SLEEP test */
	printf("\nSLEEP(5 second) test\n");
	getkey();
	t=time(NULL);
	printf("sleeping... ");
	fflush(stdout);
	ticks=msclock();
	SLEEP(5000);
	printf("slept %ld seconds (%ld according to msclock)\n",time(NULL)-t,(msclock()-ticks)/MSCLOCKS_PER_SEC);

	/* Thread SLEEP test */
	printf("\nThread SLEEP(5 second) test\n");
	getkey();
	i=0;
	if(_beginthread(
		  sleep_test_thread	/* entry point */
		 ,0					/* stack size (0=auto) */
		 ,&i				/* data */
		 )==(unsigned long)-1)
		printf("_beginthread failed\n");
	else {
		SLEEP(1);			/* yield to child thread */
		while(i==0) {
			printf(".");
			fflush(stdout);
			SLEEP(1000);
		}
	}

	/* glob test */
	printf("\nglob(%s) test\n",glob_pattern);
	getkey();
	i=glob(glob_pattern,GLOB_MARK,NULL,&g);
	if(i==0) {
		for(u=0;u<g.gl_pathc;u++)
			printf("%s\n",g.gl_pathv[u]);
		globfree(&g);
	} else
		printf("glob(%s) returned %d\n",glob_pattern,i);

	/* opendir (and other directory functions) test */
	printf("\nopendir(%s) test\n",path);
	getkey();
	printf("\nDirectory of %s\n\n",FULLPATH(fpath,path,sizeof(fpath)));
	dir=opendir(path);
	while(dir!=NULL && (dirent=readdir(dir))!=NULL) {
		t=fdate(dirent->d_name);
		printf("%.24s %10lu  %06o  %s%c\n"
			,ctime(&t)
			,flength(dirent->d_name)
			,getfattr(dirent->d_name)
			,dirent->d_name
			,isdir(dirent->d_name) ? '/':0
			);
	}
	if(dir!=NULL)
		closedir(dir);
	printf("\nFree disk space: %lu kbytes\n",getfreediskspace(path,1024));

	/* Thread (and inter-process communication) test */
	printf("\nSemaphore test\n");
	getkey();
	if(sem_init(&thread_data.parent_sem
		,0 /* shared between processes */
		,0 /* initial count */
		)) {
		printf("sem_init failed\n");
	}
	if(sem_init(&thread_data.child_sem
		,0	/* shared between processes */
		,0	/* initial count */
		)) {
		printf("sem_init failed\n");
	}
	if(_beginthread(
		  sem_test_thread	/* entry point */
		 ,0					/* stack size (0=auto) */
		 ,&thread_data		/* data */
		 )==(unsigned long)-1)
		printf("_beginthread failed\n");
	else {
		sem_wait(&thread_data.child_sem);	/* wait for thread to begin */
		for(i=0;i<10;i++) {
			printf("<parent>");
			sem_post(&thread_data.parent_sem);
			sem_wait(&thread_data.child_sem);
		}
		sem_wait(&thread_data.child_sem);	/* wait for thread to end */
	}
	sem_destroy(&thread_data.parent_sem);
	sem_destroy(&thread_data.child_sem);

	printf("\nSemaphore blocking test\n");
	getkey();
	sem_init(&thread_data.parent_sem
		,0 /* shared between processes */
		,0 /* initial count */
		);
	sem_init(&thread_data.child_sem
		,0	/* shared between processes */
		,0	/* initial count */
		);
	if(_beginthread(
		  sem_test_thread_block	/* entry point */
		 ,0					/* stack size (0=auto) */
		 ,&thread_data		/* data */
		 )==(unsigned long)-1)
		printf("_beginthread failed\n");
	else {
		sem_wait(&thread_data.child_sem);	/* wait for thread to begin */
		for(i=0;i<10;i++) {
			printf("<parent>");
			SLEEP(5000);
			sem_post(&thread_data.parent_sem);
			sem_wait(&thread_data.child_sem);
		}
		sem_wait(&thread_data.child_sem);	/* wait for thread to end */
	}
	printf("\nsem_trywait_block test...");
	t=time(NULL);
	sem_trywait_block(&thread_data.parent_sem,5000);
	printf("\ntimed-out after %ld seconds (should be 5 seconds)\n",time(NULL)-t);
	sem_destroy(&thread_data.parent_sem);
	sem_destroy(&thread_data.child_sem);
	printf("\nendian check...");
	memcpy(&i,"\x01\x02\x03\x04",4);
	if(LE_LONG(i)==67305985) {
		printf("OK!\n");
	}
	else {
		printf("FAILED!\n");
	}
	return 0;
}