home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #define INCL_WIN
- #define INCL_GPI
- #define INCL_PM
- #define INCL_DOS
- #include <os2.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include <math.h>
- #include "3dmaze.h"
-
- #define ERROR_COLOR CLR_RED
- #define RESOLUTION 4
- #define NUM_COLORS 16
- #define PIXELS_PER_ROOM 30
- #define TWICE_PIXELS_PER_ROOM 60 /* 2*PIXELS_PER_ROOM */
- #define WM_PLOT_DONE WM_USER
-
- /* screen constants */
- #define WIDTH_OF_SCREEN 8.0
- #define HEIGHT_OF_SCREEN 6.0 /* same units as WIDTH_OF_SCREEN */
-
- typedef struct
- {
- unsigned char red;
- unsigned char green;
- unsigned char blue;
- } rgb_rec;
-
- typedef struct stack_rec_record
- {
- char index_1;
- char index_2;
- } stack_rec;
-
- typedef struct
- {
- double x;
- double y;
- double z;
- } vertex_rec;
-
- static void adjust_perspective(int,int,float *,float *,
- float *,double,double,double,double,double);
- MRESULT EXPENTRY ClientWndProc(HWND,ULONG,MPARAM,MPARAM);
- static void DestroyMaze(float **,float **,float **,
- int **,int **,unsigned char **,
- unsigned char **,stack_rec **);
- static void evaluate_and_transform(double,double,double,double,int,int,
- double,double,float *,float *,float *,double *,double *,
- double *,double *,double *,vertex_rec *,int *,int *,
- unsigned char *);
- static double f(double,double);
- static void generate_maze(void);
- static void hash(int *,int *,int *,int *,int *,int *,int *,int *);
- static void increment(int *,int *,int *,int *,int *,int *,int *,int *);
- MRESULT EXPENTRY HelpProc(HWND,ULONG,MPARAM,MPARAM);
- int main(void);
- static int memory_allocated(long,float **,float **,
- float **,int **,int **,unsigned char **,
- unsigned char **,int,stack_rec **);
- static int num_rooms_in_solution(void);
- VOID APIENTRY plot(ULONG);
- static void shade(int,int,float *,float *,float *,
- unsigned char *,vertex_rec *);
- VOID APIENTRY SizeMaze(ULONG);
- static void sort_back_to_front(long,float *,int *,int *);
-
-
- static double aspect_ratio;
- static unsigned char *base_z;
- static unsigned char *color;
- static int delta_x [4] [24];
- static int delta_y [4] [24];
- HPS hPS;
- static short horizontal_scroll_position;
- static HWND hwndHscroll;
- static HWND hwndVscroll;
- static int iFatalError;
- static int iMaxX;
- static int iMaxY;
- static vertex_rec light;
- static int max_x;
- static int max_x_less_1;
- static int max_x_plus_1;
- static int max_y;
- static int max_y_less_1;
- static int max_y_plus_1;
- static int num_columns;
- static long num_primes;
- static int num_rooms_in_maze;
- static int num_rows;
- static int num_x_divisions;
- static int num_y_divisions;
- static char **page;
- HEV plot_active;
- static int plot_command;
- HWND plot_hwnd;
- static int plot_kill;
- static ULONG plot_post_count;
- TID plot_thread_id;
- static ULONG plotting;
- static int r_n [8];
- static int r_n_seed [7];
- static int resized;
- static int response;
- static double rotation;
- HEV SizeMaze_active;
- static int SizeMaze_kill;
- static ULONG SizeMaze_post_count;
- TID SizeMaze_thread_id;
- static ULONG sizing;
- static int solve_draw_or_clear;
- static stack_rec *stack;
- static int tem_int;
- static double tilt;
- static short vertical_scroll_position;
- static int *x_division_index;
- static double x_max;
- static int x_maze;
- static double x_min;
- static int x_next;
- static float *x_prime;
- static double x_prime_max;
- static int x_wall;
- static int *y_division_index;
- static double y_max;
- static int y_maze;
- static double y_min;
- static int y_next;
- static float *y_prime;
- static double y_prime_max;
- static double y_prime_min;
- static int y_wall;
- static float *z_prime;
- static double z_prime_max;
- static double z_prime_min;
-
- int main(void)
- {
- ULONG ctldata;
- HAB hAB;
- HMQ hmq;
- HWND hwndClient;
- HWND hwndFrame;
- QMSG qmsg;
- static CHAR szClientClass [] = "3D Maze";
-
- DosCreateEventSem((PSZ) NULL,(PHEV) &plot_active,(ULONG) 0,(BOOL32) 1);
- DosCreateEventSem((PSZ) NULL,(PHEV) &SizeMaze_active,(ULONG) 0,(BOOL32) 1);
- hAB=WinInitialize((ULONG) 0);
- hmq=WinCreateMsgQueue(hAB,0l);
- WinRegisterClass(hAB,(PSZ) szClientClass,(PFNWP) ClientWndProc,
- (ULONG) CS_SIZEREDRAW,(ULONG) 0);
- ctldata=(FCF_STANDARD | FCF_HORZSCROLL | FCF_VERTSCROLL);
- hwndFrame=WinCreateStdWindow((HWND) HWND_DESKTOP,(ULONG) WS_VISIBLE,
- &ctldata,(PSZ) szClientClass,(PSZ) NULL,(ULONG) 0,(HMODULE) NULL,
- (ULONG) ID_MAINMENU,(PHWND) &hwndClient);
- WinShowWindow((HWND) hwndFrame,(BOOL) TRUE);
- while (WinGetMsg((HAB) hAB,(PQMSG) &qmsg,(HWND) NULL,(ULONG) 0,(ULONG) 0))
- WinDispatchMsg((HAB) hAB,(PQMSG) &qmsg);
- WinDestroyWindow((HWND) hwndFrame);
- WinDestroyMsgQueue((HMQ) hmq);
- WinTerminate((HAB) hAB);
- return(0);
- }
-
- MRESULT EXPENTRY ClientWndProc(HWND hwnd,ULONG msg,MPARAM mp1,MPARAM mp2)
- {
- DATETIME dateSeed;
- HWND hwndMenu;
-
- switch (msg)
- {
- case WM_CREATE:
- iFatalError=FALSE;
- DosGetDateTime(&dateSeed);
- r_n_seed[0]=dateSeed.year%10;
- r_n_seed[1]=dateSeed.month%10;
- r_n_seed[2]=dateSeed.day%10;
- r_n_seed[3]=dateSeed.hours%10;
- r_n_seed[4]=dateSeed.minutes%10;
- r_n_seed[5]=dateSeed.seconds%10;
- r_n_seed[6]=dateSeed.hundredths%10;
- hwndHscroll=WinWindowFromID(WinQueryWindow(hwnd,QW_PARENT),
- FID_HORZSCROLL);
- WinSendMsg(hwndHscroll,SBM_SETSCROLLBAR,MPFROM2SHORT(0,0),
- MPFROM2SHORT(0,360));
- horizontal_scroll_position=0;
- rotation=(double) (((short) 180)-horizontal_scroll_position);
- hwndVscroll=WinWindowFromID(WinQueryWindow(hwnd,QW_PARENT),
- FID_VERTSCROLL);
- WinSendMsg(hwndVscroll,SBM_SETSCROLLBAR,MPFROM2SHORT(60,0),
- MPFROM2SHORT(0,90));
- vertical_scroll_position=60;
- tilt=(double) (((short) 90)-vertical_scroll_position);
- solve_draw_or_clear=(int) 'D';
- iMaxX=0;
- iMaxY=0;
- resized=FALSE;
- plot_command=FALSE;
- aspect_ratio=(HEIGHT_OF_SCREEN/WIDTH_OF_SCREEN)
- /(((double) WinQuerySysValue(HWND_DESKTOP,SV_CYSCREEN))
- /((double) WinQuerySysValue(HWND_DESKTOP,SV_CXSCREEN)));
- color=(unsigned char *) NULL;
- base_z=(unsigned char *) NULL;
- x_division_index=(int *) NULL;
- x_prime=(float *) NULL;
- y_division_index=(int *) NULL;
- y_prime=(float *) NULL;
- z_prime=(float *) NULL;
- page=(char **) NULL;
- stack=(stack_rec *) NULL;
- break;
- case WM_HSCROLL:
- switch (SHORT2FROMMP(mp2))
- {
- case SB_LINELEFT:
- horizontal_scroll_position--;
- break;
- case SB_LINERIGHT:
- horizontal_scroll_position++;
- break;
- case SB_PAGELEFT:
- horizontal_scroll_position-=10;
- break;
- case SB_PAGERIGHT:
- horizontal_scroll_position+=10;
- break;
- case SB_SLIDERPOSITION:
- horizontal_scroll_position=SHORT1FROMMP(mp2);
- case SB_ENDSCROLL:
- plot_command=TRUE;
- DosQueryEventSem((HEV) plot_active,&plotting);
- if (plotting == (ULONG) 0)
- plot_kill=TRUE;
- DosQueryEventSem((HEV) SizeMaze_active,&sizing);
- if (sizing == (ULONG) 0)
- {
- SizeMaze_kill=TRUE;
- DosWaitEventSem((HEV) SizeMaze_active,
- (ULONG) SEM_INDEFINITE_WAIT);
- }
- if (plot_kill)
- {
- DosWaitEventSem((HEV) plot_active,
- (ULONG) SEM_INDEFINITE_WAIT);
- WinEndPaint(hPS);
- }
- rotation=(double) (((short) 180)-horizontal_scroll_position);
- if ((iMaxX >= TWICE_PIXELS_PER_ROOM)
- && (iMaxY >= TWICE_PIXELS_PER_ROOM))
- {
- DosResetEventSem((HEV) SizeMaze_active,
- &SizeMaze_post_count);
- SizeMaze_kill=FALSE;
- DosCreateThread(&SizeMaze_thread_id,SizeMaze,(ULONG) 0,
- (ULONG) 0,16384);
- }
- switch (solve_draw_or_clear)
- {
- case 'C':
- solve_draw_or_clear='D';
- break;
- case 'S':
- solve_draw_or_clear='B';
- break;
- default:
- break;
- }
- WinInvalidateRect(hwnd,(PRECTL) NULL,(BOOL) FALSE);
- break;
- default:
- break;
- }
- horizontal_scroll_position=max(0,min(horizontal_scroll_position,360));
- if (horizontal_scroll_position
- != SHORT1FROMMR(WinSendMsg(hwndHscroll,SBM_QUERYPOS,NULL,NULL)))
- WinSendMsg(hwndHscroll,SBM_SETPOS,
- MPFROMSHORT(horizontal_scroll_position),NULL);
- break;
- case WM_VSCROLL:
- switch (SHORT2FROMMP(mp2))
- {
- case SB_LINEUP:
- vertical_scroll_position--;
- break;
- case SB_LINEDOWN:
- vertical_scroll_position++;
- break;
- case SB_PAGEUP:
- vertical_scroll_position-=10;
- break;
- case SB_PAGEDOWN:
- vertical_scroll_position+=10;
- break;
- case SB_SLIDERPOSITION:
- vertical_scroll_position=SHORT1FROMMP(mp2);
- case SB_ENDSCROLL:
- plot_command=TRUE;
- DosQueryEventSem((HEV) plot_active,&plotting);
- if (plotting == (ULONG) 0)
- plot_kill=TRUE;
- DosQueryEventSem((HEV) SizeMaze_active,&sizing);
- if (sizing == (ULONG) 0)
- {
- SizeMaze_kill=TRUE;
- DosWaitEventSem((HEV) SizeMaze_active,
- (ULONG) SEM_INDEFINITE_WAIT);
- }
- if (plot_kill)
- {
- DosWaitEventSem((HEV) plot_active,
- (ULONG) SEM_INDEFINITE_WAIT);
- WinEndPaint(hPS);
- }
- tilt=(double) (((short) 90)-vertical_scroll_position);
- if ((iMaxX >= TWICE_PIXELS_PER_ROOM)
- && (iMaxY >= TWICE_PIXELS_PER_ROOM))
- {
- DosResetEventSem((HEV) SizeMaze_active,
- &SizeMaze_post_count);
- SizeMaze_kill=FALSE;
- DosCreateThread(&SizeMaze_thread_id,SizeMaze,(ULONG) 0,
- (ULONG) 0,16384);
- }
- switch (solve_draw_or_clear)
- {
- case 'C':
- solve_draw_or_clear='D';
- break;
- case 'S':
- solve_draw_or_clear='B';
- break;
- default:
- break;
- }
- WinInvalidateRect(hwnd,(PRECTL) NULL,(BOOL) FALSE);
- break;
- default:
- break;
- }
- vertical_scroll_position=max(0,min(vertical_scroll_position,90));
- if (vertical_scroll_position
- != SHORT1FROMMR(WinSendMsg(hwndVscroll,SBM_QUERYPOS,NULL,NULL)))
- WinSendMsg(hwndVscroll,SBM_SETPOS,
- MPFROMSHORT(vertical_scroll_position),NULL);
- break;
- case WM_COMMAND:
- hwndMenu=WinWindowFromID(WinQueryWindow((HWND) hwnd,(LONG) QW_PARENT),
- (ULONG) FID_MENU);
- switch (COMMANDMSG(&msg)->cmd)
- {
- case IDM_CLEAR:
- plot_command=TRUE;
- if (iFatalError)
- WinInvalidateRect(hwnd,(PRECTL) NULL,(BOOL) FALSE);
- else
- {
- DosQueryEventSem((HEV) SizeMaze_active,&sizing);
- DosQueryEventSem((HEV) plot_active,&plotting);
- if ((sizing == (ULONG) 0)
- || (plotting == (ULONG) 0))
- DosBeep((ULONG) 60,(ULONG) 333);
- else
- {
- solve_draw_or_clear=(int) 'C';
- WinInvalidateRect(hwnd,(PRECTL) NULL,(BOOL) FALSE);
- }
- }
- break;
- case IDM_NEW:
- plot_command=TRUE;
- DosQueryEventSem((HEV) plot_active,&plotting);
- if (plotting == (ULONG) 0)
- plot_kill=TRUE;
- DosQueryEventSem((HEV) SizeMaze_active,&sizing);
- if (sizing == (ULONG) 0)
- {
- SizeMaze_kill=TRUE;
- DosWaitEventSem((HEV) SizeMaze_active,
- (ULONG) SEM_INDEFINITE_WAIT);
- }
- if (plot_kill)
- {
- DosWaitEventSem((HEV) plot_active,
- (ULONG) SEM_INDEFINITE_WAIT);
- WinEndPaint(hPS);
- }
- if ((iMaxX >= TWICE_PIXELS_PER_ROOM)
- && (iMaxY >= TWICE_PIXELS_PER_ROOM))
- {
- DosGetDateTime(&dateSeed);
- r_n_seed[0]=dateSeed.year%10;
- r_n_seed[1]=dateSeed.month%10;
- r_n_seed[2]=dateSeed.day%10;
- r_n_seed[3]=dateSeed.hours%10;
- r_n_seed[4]=dateSeed.minutes%10;
- r_n_seed[5]=dateSeed.seconds%10;
- r_n_seed[6]=dateSeed.hundredths%10;
- DosResetEventSem((HEV) SizeMaze_active,
- &SizeMaze_post_count);
- SizeMaze_kill=FALSE;
- DosCreateThread(&SizeMaze_thread_id,SizeMaze,(ULONG) 0,
- (ULONG) 0,16384);
- }
- solve_draw_or_clear=(int) 'D';
- WinInvalidateRect(hwnd,(PRECTL) NULL,(BOOL) FALSE);
- break;
- case IDM_SOLVE:
- plot_command=TRUE;
- if (iFatalError)
- WinInvalidateRect(hwnd,(PRECTL) NULL,(BOOL) FALSE);
- else
- {
- DosQueryEventSem((HEV) SizeMaze_active,&sizing);
- DosQueryEventSem((HEV) plot_active,&plotting);
- if ((sizing == (ULONG) 0)
- || (plotting == (ULONG) 0))
- DosBeep(60,333);
- else
- {
- if ((iMaxX >= TWICE_PIXELS_PER_ROOM)
- && (iMaxY >= TWICE_PIXELS_PER_ROOM))
- solve_draw_or_clear=(int) 'S';
- WinInvalidateRect(hwnd,(PRECTL) NULL,(BOOL) FALSE);
- }
- }
- break;
- case IDM_HELP:
- WinDlgBox(HWND_DESKTOP,hwnd,(PFNWP) HelpProc,(HMODULE) 0,
- (ULONG) IDD_HELPBOX,(PVOID) NULL);
- break;
- default:
- break;
- }
- break;
- case WM_SIZE:
- DosQueryEventSem((HEV) plot_active,&plotting);
- if (plotting == (ULONG) 0)
- plot_kill=TRUE;
- DosQueryEventSem((HEV) SizeMaze_active,&sizing);
- if (sizing == (ULONG) 0)
- {
- SizeMaze_kill=TRUE;
- DosWaitEventSem((HEV) SizeMaze_active,
- (ULONG) SEM_INDEFINITE_WAIT);
- }
- if (plot_kill)
- {
- DosWaitEventSem((HEV) plot_active,(ULONG) SEM_INDEFINITE_WAIT);
- WinEndPaint(hPS);
- }
- resized=TRUE;
- solve_draw_or_clear=(int) 'D';
- iMaxX=SHORT1FROMMP(mp2)-1;
- iMaxY=SHORT2FROMMP(mp2)-1;
- if ((iMaxX >= TWICE_PIXELS_PER_ROOM)
- && (iMaxY >= TWICE_PIXELS_PER_ROOM))
- {
- DosGetDateTime(&dateSeed);
- r_n_seed[0]=dateSeed.year%10;
- r_n_seed[1]=dateSeed.month%10;
- r_n_seed[2]=dateSeed.day%10;
- r_n_seed[3]=dateSeed.hours%10;
- r_n_seed[4]=dateSeed.minutes%10;
- r_n_seed[5]=dateSeed.seconds%10;
- r_n_seed[6]=dateSeed.hundredths%10;
- DosResetEventSem((HEV) SizeMaze_active,&SizeMaze_post_count);
- SizeMaze_kill=FALSE;
- DosCreateThread(&SizeMaze_thread_id,SizeMaze,(ULONG) 0,(ULONG) 0,
- 16384);
- }
- break;
- case WM_ERASEBACKGROUND:
- return((MRESULT) TRUE);
- break;
- case WM_PAINT:
- DosQueryEventSem((HEV) plot_active,&plotting);
- if (plotting == (ULONG) 0)
- {
- plot_kill=TRUE;
- DosWaitEventSem((HEV) plot_active,(ULONG) SEM_INDEFINITE_WAIT);
- WinEndPaint(hPS);
- WinInvalidateRect(hwnd,(PRECTL) NULL,(BOOL) FALSE);
- }
- plot_hwnd=hwnd;
- if ((iMaxX >= TWICE_PIXELS_PER_ROOM)
- && (iMaxY >= TWICE_PIXELS_PER_ROOM))
- {
- DosResetEventSem((HEV) plot_active,&plot_post_count);
- hPS=WinBeginPaint(hwnd,(HPS) NULL,(PRECTL) NULL);
- plot_kill=FALSE;
- if ((! resized) && (! plot_command))
- switch (solve_draw_or_clear)
- {
- case 'C':
- solve_draw_or_clear='D';
- break;
- case 'S':
- solve_draw_or_clear='B';
- break;
- default:
- break;
- }
- DosCreateThread(&plot_thread_id,plot,(ULONG) 0,(ULONG) 0,16384);
- }
- resized=FALSE;
- plot_command=FALSE;
- break;
- case WM_PLOT_DONE:
- WinEndPaint(hPS);
- break;
- case WM_DESTROY:
- DosQueryEventSem((HEV) plot_active,&plotting);
- if (plotting == (ULONG) 0)
- plot_kill=TRUE;
- DosQueryEventSem((HEV) SizeMaze_active,&sizing);
- if (sizing == (ULONG) 0)
- {
- SizeMaze_kill=TRUE;
- DosWaitEventSem((HEV) SizeMaze_active,
- (ULONG) SEM_INDEFINITE_WAIT);
- }
- if (plot_kill)
- {
- DosWaitEventSem((HEV) plot_active,(ULONG) SEM_INDEFINITE_WAIT);
- WinEndPaint(hPS);
- }
- DestroyMaze(&x_prime,&y_prime,&z_prime,&x_division_index,
- &y_division_index,&color,&base_z,&stack);
- break;
- default:
- return(WinDefWindowProc(hwnd,msg,mp1,mp2));
- break;
- }
- return((MRESULT) 0);
- }
-
- MRESULT EXPENTRY HelpProc(HWND hwnd,ULONG msg,MPARAM mp1,MPARAM mp2)
- {
- switch (msg)
- {
- case WM_COMMAND:
- switch (COMMANDMSG(&msg)->cmd)
- {
- case DID_OK:
- case DID_CANCEL:
- WinDismissDlg(hwnd,(ULONG) TRUE);
- break;
- default:
- break;
- }
- break;
- default:
- return(WinDefDlgProc(hwnd,msg,mp1,mp2));
- }
- return((MRESULT) 0);
- }
-
- static void SizeMaze(arg)
- ULONG arg;
- {
- num_columns=iMaxX/PIXELS_PER_ROOM;
- num_rows=iMaxY/PIXELS_PER_ROOM;
- max_x=2*num_columns;
- max_y=2*num_rows;
- num_rooms_in_maze=num_rows*num_columns;
- num_x_divisions=RESOLUTION*(max_y+1)+2-RESOLUTION/2;
- if (RESOLUTION%2 == 0)
- num_x_divisions++;
- num_y_divisions=RESOLUTION*(max_x+1)+2-RESOLUTION/2;
- if (RESOLUTION%2 == 0)
- num_y_divisions++;
- num_primes=(long) num_x_divisions;
- num_primes*=((long) num_y_divisions);
- DestroyMaze(&x_prime,&y_prime,&z_prime,&x_division_index,
- &y_division_index,&color,&base_z,&stack);
- max_x_plus_1=max_x+1;
- max_x_less_1=max_x-1;
- max_y_plus_1=max_y+1;
- max_y_less_1=max_y-1;
- if (! SizeMaze_kill)
- {
- if (memory_allocated(num_primes,&x_prime,&y_prime,
- &z_prime,&x_division_index,&y_division_index,&color,&base_z,
- num_rooms_in_maze,&stack))
- {
- if (! SizeMaze_kill)
- generate_maze();
- x_min=1.0;
- x_max=(double) num_x_divisions;
- y_min=1.0;
- y_max=(double) num_y_divisions;
- light.x=1.5;
- light.y=-1.0;
- light.z=2.6;
- if (! SizeMaze_kill)
- evaluate_and_transform(x_min,x_max,y_min,y_max,num_x_divisions,
- num_y_divisions,rotation,tilt,x_prime,y_prime,z_prime,
- &x_prime_max,&y_prime_min,&y_prime_max,&z_prime_min,
- &z_prime_max,&light,x_division_index,y_division_index,base_z);
- if (! SizeMaze_kill)
- shade(num_x_divisions,num_y_divisions,x_prime,y_prime,z_prime,
- color,&light);
- if (! SizeMaze_kill)
- adjust_perspective(num_x_divisions,num_y_divisions,x_prime,
- y_prime,z_prime,x_prime_max,y_prime_min,y_prime_max,
- z_prime_min,z_prime_max);
- if (! SizeMaze_kill)
- sort_back_to_front(num_primes,x_prime,x_division_index,
- y_division_index);
- }
- else
- iFatalError=TRUE;
- }
- DosEnterCritSec();
- DosPostEventSem((HEV) SizeMaze_active);
- return;
- }
-
- static int memory_allocated(num_primes,x_prime,y_prime,z_prime,x_division_index,
- y_division_index,color,base_z,num_rooms_in_maze,stack)
- long num_primes;
- float **x_prime;
- float **y_prime;
- float **z_prime;
- int **x_division_index;
- int **y_division_index;
- unsigned char **color;
- unsigned char **base_z;
- int num_rooms_in_maze;
- stack_rec **stack;
- {
- static int result;
- register int y;
-
- if (result=((page=(char **) malloc(max_y_plus_1*sizeof(char *))) != NULL))
- {
- for (y=0; ((result) && (y < max_y_plus_1)); y++)
- result=((page[y]=malloc(max_x_plus_1*sizeof(char))) != NULL);
- if (! result)
- {
- while (y > 0)
- free((void *) page[--y]);
- free((void *) page);
- page=(char **) NULL;
- }
- }
- if (result)
- {
- if (! (result=((*x_prime=(float *) malloc(num_primes*sizeof(float)))
- != NULL)))
- {
- for (y=0; y < max_y_plus_1; y++)
- free((void *) page[y]);
- free((void *) page);
- page=(char **) NULL;
- }
- }
- if (result)
- {
- if (! (result=((*y_prime
- =(float *) malloc(num_primes*sizeof(float))) != NULL)))
- {
- free((void *) *x_prime);
- *x_prime=(float *) NULL;
- for (y=0; y < max_y_plus_1; y++)
- free((void *) page[y]);
- free((void *) page);
- page=(char **) NULL;
- }
- }
- if (result)
- {
- if (! (result=((*z_prime
- =(float *) malloc(num_primes*sizeof(float))) != NULL)))
- {
- free((void *) *y_prime);
- *y_prime=(float *) NULL;
- free((void *) *x_prime);
- *x_prime=(float *) NULL;
- for (y=0; y < max_y_plus_1; y++)
- free((void *) page[y]);
- free((void *) page);
- page=(char **) NULL;
- }
- }
- if (result)
- {
- if (! (result=((*x_division_index
- =(int *) malloc(num_primes*sizeof(int))) != NULL)))
- {
- free((void *) *z_prime);
- *z_prime=(float *) NULL;
- free((void *) *y_prime);
- *y_prime=(float *) NULL;
- free((void *) *x_prime);
- *x_prime=(float *) NULL;
- for (y=0; y < max_y_plus_1; y++)
- free((void *) page[y]);
- free((void *) page);
- page=(char **) NULL;
- }
- }
- if (result)
- {
- if (! (result=((*y_division_index
- =(int *) malloc(num_primes*sizeof(int))) != NULL)))
- {
- free((void *) *x_division_index);
- *x_division_index=(int *) NULL;
- free((void *) *z_prime);
- *z_prime=(float *) NULL;
- free((void *) *y_prime);
- *y_prime=(float *) NULL;
- free((void *) *x_prime);
- *x_prime=(float *) NULL;
- for (y=0; y < max_y_plus_1; y++)
- free((void *) page[y]);
- free((void *) page);
- page=(char **) NULL;
- }
- }
- if (result)
- {
- if (! (result=((*color=(unsigned char *)
- malloc(num_primes*sizeof(unsigned char))) != NULL)))
- {
- free((void *) *y_division_index);
- *y_division_index=(int *) NULL;
- free((void *) *x_division_index);
- *x_division_index=(int *) NULL;
- free((void *) *z_prime);
- *z_prime=(float *) NULL;
- free((void *) *y_prime);
- *y_prime=(float *) NULL;
- free((void *) *x_prime);
- *x_prime=(float *) NULL;
- for (y=0; y < max_y_plus_1; y++)
- free((void *) page[y]);
- free((void *) page);
- page=(char **) NULL;
- }
- }
- if (result)
- {
- if (! (result=((*base_z=(unsigned char *)
- malloc(num_primes*sizeof(unsigned char))) != NULL)))
- {
- free((void *) *color);
- *color=(unsigned char *) NULL;
- free((void *) *y_division_index);
- *y_division_index=(int *) NULL;
- free((void *) *x_division_index);
- *x_division_index=(int *) NULL;
- free((void *) *z_prime);
- *z_prime=(float *) NULL;
- free((void *) *y_prime);
- *y_prime=(float *) NULL;
- free((void *) *x_prime);
- *x_prime=(float *) NULL;
- for (y=0; y < max_y_plus_1; y++)
- free((void *) page[y]);
- free((void *) page);
- page=(char **) NULL;
- }
- }
- if (result)
- {
- if (! (result=((*stack
- =(stack_rec *) malloc(num_rooms_in_maze*sizeof(stack_rec)))
- != NULL)))
- {
- free((void *) *base_z);
- *base_z=(unsigned char *) NULL;
- free((void *) *color);
- *color=(unsigned char *) NULL;
- free((void *) *y_division_index);
- *y_division_index=(int *) NULL;
- free((void *) *x_division_index);
- *x_division_index=(int *) NULL;
- free((void *) *z_prime);
- *z_prime=(float *) NULL;
- free((void *) *y_prime);
- *y_prime=(float *) NULL;
- free((void *) *x_prime);
- *x_prime=(float *) NULL;
- for (y=0; y < max_y_plus_1; y++)
- free((void *) page[y]);
- free((void *) page);
- page=(char **) NULL;
- }
- }
- return(result);
- }
-
- static int num_rooms_in_solution()
- {
- int direction_x;
- int direction_y;
- int initial_direction_x;
- int initial_direction_y;
- int passage_found;
- int result;
- int tem;
- int x;
- int x_next;
- int y;
- int y_next;
-
- result=1;
- x=1;
- y=1;
- page[y][x]='S';
- initial_direction_x=0;
- initial_direction_y=1;
- do
- {
- passage_found=FALSE;
- direction_x=-initial_direction_y;
- direction_y=initial_direction_x;
- while (! passage_found)
- {
- x_next=x+direction_x;
- y_next=y+direction_y;
- if (page[y_next][x_next] != 'W')
- passage_found=TRUE;
- else
- {
- tem=direction_x;
- direction_x=direction_y;
- direction_y=-tem;
- }
- };
- if (page[y_next][x_next] == ' ')
- {
- result++;
- page[y_next][x_next]='S';
- x=x_next+direction_x;
- y=y_next+direction_y;
- page[y][x]='S';
- }
- else
- {
- result--;
- page[y][x]=' ';
- page[y_next][x_next]=' ';
- x=x_next+direction_x;
- y=y_next+direction_y;
- };
- initial_direction_x=direction_x;
- initial_direction_y=direction_y;
- }
- while ((x != max_x_less_1) || (y != max_y_less_1));
- return(result);
- }
-
- static void generate_maze()
- {
- static int counter_0;
- static int counter_1;
- static int counter_2;
- static int counter_3;
- static int counter_4;
- static int counter_5;
- static int counter_6;
- static int counter_7;
- static int delta_index_1a;
- static int delta_index_1b;
- static int delta_index_1c;
- static int delta_index_1d;
- static int delta_index_2;
- static int digit;
- static int digit_num;
- static int passage_found;
- register int r_n_index_1;
- register int r_n_index_2;
- static int search_complete;
- static int stack_head;
- static int sum;
- static int tem_int;
- static int x;
- static int x_next;
- static int y;
- static int y_next;
-
- r_n[0]=r_n_seed[0];
- r_n[1]=r_n_seed[1];
- r_n[2]=r_n_seed[2];
- r_n[3]=r_n_seed[3];
- r_n[4]=r_n_seed[4];
- r_n[5]=r_n_seed[5];
- r_n[6]=r_n_seed[6];
- r_n[7]=0;
- counter_0=r_n[0];
- counter_1=r_n[1];
- counter_2=r_n[2];
- counter_3=r_n[3];
- counter_4=r_n[4];
- counter_5=r_n[5];
- counter_6=r_n[6];
- counter_7=r_n[7];
- hash(&counter_0,&counter_1,&counter_2,&counter_3,&counter_4,&counter_5,
- &counter_6,&counter_7);
- delta_x[0][0]=-1;
- delta_y[0][0]=0;
- delta_x[1][0]=0;
- delta_y[1][0]=1;
- delta_x[2][0]=1;
- delta_y[2][0]=0;
- delta_x[3][0]=0;
- delta_y[3][0]=-1;
- delta_index_2=0;
- for (delta_index_1a=0; delta_index_1a < 4; ++delta_index_1a)
- for (delta_index_1b=0; delta_index_1b < 4; ++delta_index_1b)
- if (delta_index_1a != delta_index_1b)
- for (delta_index_1c=0; delta_index_1c < 4; ++delta_index_1c)
- if ((delta_index_1a != delta_index_1c)
- && (delta_index_1b != delta_index_1c))
- for (delta_index_1d=0; delta_index_1d < 4; ++delta_index_1d)
- if ((delta_index_1a != delta_index_1d)
- && (delta_index_1b != delta_index_1d)
- && (delta_index_1c != delta_index_1d))
- {
- delta_x[delta_index_1a][delta_index_2]=delta_x[0][0];
- delta_y[delta_index_1a][delta_index_2]=delta_y[0][0];
- delta_x[delta_index_1b][delta_index_2]=delta_x[1][0];
- delta_y[delta_index_1b][delta_index_2]=delta_y[1][0];
- delta_x[delta_index_1c][delta_index_2]=delta_x[2][0];
- delta_y[delta_index_1c][delta_index_2]=delta_y[2][0];
- delta_x[delta_index_1d][delta_index_2]=delta_x[3][0];
- delta_y[delta_index_1d][delta_index_2]=delta_y[3][0];
- delta_index_2++;
- };
- do
- {
- for (y=0; y <= max_y; ++y)
- for (x=0; x <= max_x; ++x)
- page[y][x]='W';
- r_n[0]=counter_0+1;
- r_n[1]=counter_1+1;
- r_n[2]=counter_2+1;
- r_n[3]=counter_3+1;
- r_n[4]=counter_4+1;
- r_n[5]=counter_5+1;
- r_n[6]=counter_6+1;
- r_n[7]=counter_7+1;
- sum=0;
- for (digit_num=1; digit_num <= 3; ++digit_num)
- {
- digit=r_n[0];
- r_n_index_1=0;
- for (r_n_index_2=1; r_n_index_2 < 8; ++r_n_index_2)
- {
- tem_int=r_n[r_n_index_2];
- r_n[r_n_index_1]=tem_int;
- digit+=tem_int;
- if (digit >= 29)
- digit-=29;
- r_n_index_1=r_n_index_2;
- }
- r_n[7]=digit;
- sum=29*sum+digit;
- }
- x=2*(sum % num_columns)+1;
- sum=0;
- for (digit_num=1; digit_num <= 3; ++digit_num)
- {
- digit=r_n[0];
- r_n_index_1=0;
- for (r_n_index_2=1; r_n_index_2 < 8; ++r_n_index_2)
- {
- tem_int=r_n[r_n_index_2];
- r_n[r_n_index_1]=tem_int;
- digit+=tem_int;
- if (digit >= 29)
- digit-=29;
- r_n_index_1=r_n_index_2;
- }
- r_n[7]=digit;
- sum=29*sum+digit;
- }
- y=2*(sum % num_rows)+1;
- page[y][x]=' ';
- stack_head=-1;
- do
- {
- delta_index_1a=0;
- do
- {
- delta_index_2=r_n[0];
- r_n_index_1=0;
- r_n_index_2=1;
- while (r_n_index_2 < 8)
- {
- tem_int=r_n[r_n_index_2];
- r_n[r_n_index_1]=tem_int;
- delta_index_2+=tem_int;
- if (delta_index_2 >= 29)
- delta_index_2-=29;
- r_n_index_1=r_n_index_2;
- r_n_index_2++;
- }
- r_n[7]=delta_index_2;
- }
- while (delta_index_2 >= 24);
- passage_found=FALSE;
- search_complete=FALSE;
- while (! search_complete)
- {
- while ((delta_index_1a < 4) && (! passage_found))
- {
- x_next=x+2*delta_x[delta_index_1a][delta_index_2];
- if (x_next <= 0)
- delta_index_1a++;
- else
- if (x_next >= max_x)
- delta_index_1a++;
- else
- {
- y_next=y+2*delta_y[delta_index_1a][delta_index_2];
- if (y_next <= 0)
- delta_index_1a++;
- else
- if (y_next >= max_y)
- delta_index_1a++;
- else
- if (page[y_next][x_next] == 'W')
- passage_found=TRUE;
- else
- delta_index_1a++;
- }
- }
- if (! passage_found)
- {
- delta_index_1a=(int) (stack[stack_head].index_1);
- delta_index_2=(int) (stack[stack_head].index_2);
- x-=2*delta_x[delta_index_1a][delta_index_2];
- y-=2*delta_y[delta_index_1a][delta_index_2];
- stack_head--;
- delta_index_1a++;
- }
- search_complete=((passage_found) || (stack_head == -1));
- }
- if (passage_found)
- {
- stack_head++;
- stack[stack_head].index_1=(char) delta_index_1a;
- stack[stack_head].index_2=(char) delta_index_2;
- page[y_next][x_next]=' ';
- if (x == x_next)
- page[(y+y_next)/2][x_next]=' ';
- else
- page[y_next][(x+x_next)/2]=' ';
- x=x_next;
- y=y_next;
- }
- }
- while (stack_head != -1);
- page[0][1]=' ';
- page[max_y][max_x-1]=' ';
- increment(&counter_0,&counter_1,&counter_2,&counter_3,&counter_4,
- &counter_5,&counter_6,&counter_7);
- }
- while ((! SizeMaze_kill)
- && (3*num_rooms_in_solution() < num_rooms_in_maze));
- return;
- }
-
- static int substitution_high [100] =
- { 4,1,2,8,8,9,9,6,5,7,2,1,2,9,8,8,6,3,5,1,9,5,4,4,9,8,6,0,8,0,
- 6,0,2,4,1,9,2,0,7,4,7,3,0,0,2,6,8,9,4,0,8,3,2,3,2,5,2,4,6,9,
- 7,9,1,3,5,7,1,1,4,5,8,1,6,0,5,7,8,2,3,3,7,3,5,1,7,5,4,0,3,6,
- 3,7,7,1,9,4,0,5,6,6
- };
- static int substitution_low [100] =
- { 1,2,2,1,5,5,4,6,4,6,4,4,5,6,6,3,0,9,6,5,7,2,0,9,3,4,2,3,9,1,
- 9,9,9,3,8,9,3,4,1,5,0,5,2,7,0,8,8,0,4,5,0,3,6,8,1,7,8,8,7,1,
- 3,2,7,7,1,8,0,3,7,5,2,6,4,0,9,9,7,7,4,6,2,0,0,1,7,3,6,6,1,1,
- 2,4,5,9,8,2,8,8,3,5
- };
- static void hash(counter_0,counter_1,counter_2,counter_3,counter_4,counter_5,
- counter_6,counter_7)
- int *counter_0;
- int *counter_1;
- int *counter_2;
- int *counter_3;
- int *counter_4;
- int *counter_5;
- int *counter_6;
- int *counter_7;
- {
- register int iteration;
- static int seed_0;
- static int seed_1;
- static int seed_2;
- static int seed_3;
- static int seed_4;
- static int seed_5;
- static int seed_6;
- static int seed_7;
- register int substitution_index;
- static int tem_0;
- static int tem_1;
- static int tem_2;
-
- seed_0=(*counter_0);
- seed_1=(*counter_1);
- seed_2=(*counter_2);
- seed_3=(*counter_3);
- seed_4=(*counter_4);
- seed_5=(*counter_5);
- seed_6=(*counter_6);
- seed_7=(*counter_7);
- for (iteration=1; iteration <= 8; iteration++)
- {
- substitution_index=10*seed_1+seed_0;
- tem_0=substitution_low[substitution_index];
- tem_1=substitution_high[substitution_index];
- substitution_index=10*seed_3+seed_2;
- seed_0=substitution_low[substitution_index];
- tem_2=substitution_high[substitution_index];
- substitution_index=10*seed_5+seed_4;
- seed_2=substitution_low[substitution_index];
- seed_1=substitution_high[substitution_index];
- substitution_index=10*seed_7+seed_6;
- seed_5=substitution_low[substitution_index];
- seed_7=substitution_high[substitution_index];
- seed_3=tem_0;
- seed_6=tem_1;
- seed_4=tem_2;
- }
- (*counter_0)=seed_0;
- (*counter_1)=seed_1;
- (*counter_2)=seed_2;
- (*counter_3)=seed_3;
- (*counter_4)=seed_4;
- (*counter_5)=seed_5;
- (*counter_6)=seed_6;
- (*counter_7)=seed_7;
- return;
- }
-
- static void increment(counter_0,counter_1,counter_2,counter_3,counter_4,
- counter_5,counter_6,counter_7)
- int *counter_0;
- int *counter_1;
- int *counter_2;
- int *counter_3;
- int *counter_4;
- int *counter_5;
- int *counter_6;
- int *counter_7;
- {
- register tem;
-
- tem=(*counter_0)+1;
- if (tem <= 9)
- (*counter_0)=tem;
- else
- {
- (*counter_0)=0;
- tem=(*counter_1)+1;
- if (tem <= 9)
- (*counter_1)=tem;
- else
- {
- (*counter_1)=0;
- tem=(*counter_2)+1;
- if (tem <= 9)
- (*counter_2)=tem;
- else
- {
- (*counter_2)=0;
- tem=(*counter_3)+1;
- if (tem <= 9)
- (*counter_3)=tem;
- else
- {
- (*counter_3)=0;
- tem=(*counter_4)+1;
- if (tem <= 9)
- (*counter_4)=tem;
- else
- {
- (*counter_4)=0;
- tem=(*counter_5)+1;
- if (tem <= 9)
- (*counter_5)=tem;
- else
- {
- (*counter_5)=0;
- tem=(*counter_6)+1;
- if (tem <= 9)
- (*counter_6)=tem;
- else
- {
- (*counter_6)=0;
- tem=(*counter_7)+1;
- if (tem <= 9)
- (*counter_7)=tem;
- else
- (*counter_7)=0;
- }
- }
- }
- }
- }
- }
- }
- return;
- }
-
- static void evaluate_and_transform(x_min,x_max,y_min,y_max,num_x_divisions,
- num_y_divisions,rotation,tilt,x_prime,y_prime,z_prime,x_prime_max,y_prime_min,
- y_prime_max,z_prime_min,z_prime_max,light,x_division_index,y_division_index,
- base_z)
- double x_min;
- double x_max;
- double y_min;
- double y_max;
- int num_x_divisions;
- int num_y_divisions;
- double rotation;
- double tilt;
- float *x_prime;
- float *y_prime;
- float *z_prime;
- double *x_prime_max;
- double *y_prime_min;
- double *y_prime_max;
- double *z_prime_min;
- double *z_prime_max;
- vertex_rec *light;
- int *x_division_index;
- int *y_division_index;
- unsigned char *base_z;
- {
- static double cos_rotation;
- static double cos_tilt;
- static double magnitude;
- static long prime_num;
- static double radians;
- static double radians_per_degree;
- static double sin_rotation;
- static double sin_tilt;
- static double x;
- static double x_delta;
- register int x_division_num;
- static double x_rotated;
- static double y;
- static double y_delta;
- register int y_division_num;
- static double z;
-
- radians_per_degree=atan(1.0)/45.0;
- radians=tilt*radians_per_degree;
- cos_tilt=cos(radians);
- sin_tilt=sin(radians);
- radians=rotation*radians_per_degree;
- cos_rotation=cos(radians);
- sin_rotation=sin(radians);
- z=fabs(f(x_min,y_min));
- x_rotated=x_min*cos_rotation+y_min*sin_rotation;
- *y_prime_min=-x_min*sin_rotation+y_min*cos_rotation;
- *z_prime_min=-x_rotated*sin_tilt+z*cos_tilt;
- *y_prime_max=*y_prime_min;
- *z_prime_max=*z_prime_min;
- *x_prime_max=x_rotated*cos_tilt+z*sin_tilt;
- x_delta=(double) (num_x_divisions-1);
- x_delta=(x_max-x_min)/x_delta;
- y_delta=(double) (num_y_divisions-1);
- y_delta=(y_max-y_min)/y_delta;
- x=x_min;
- prime_num=0l;
- for (x_division_num=0;
- ((! SizeMaze_kill) && (x_division_num < num_x_divisions));
- x_division_num++)
- {
- y=y_min;
- for (y_division_num=0;
- ((! SizeMaze_kill) && (y_division_num < num_y_divisions));
- y_division_num++)
- {
- z=f(x,y);
- if (z > 0.0)
- base_z[prime_num]=(unsigned char) 1;
- else
- if (z < 0.0)
- base_z[prime_num]=(unsigned char) 2;
- else
- base_z[prime_num]=(unsigned char) 0;
- z=fabs(z);
- x_division_index[prime_num]=x_division_num;
- y_division_index[prime_num]=y_division_num;
- x_rotated=x*cos_rotation+y*sin_rotation;
- y_prime[prime_num]=(float) (-x*sin_rotation+y*cos_rotation);
- x_prime[prime_num]=(float) (x_rotated*cos_tilt+z*sin_tilt);
- z_prime[prime_num]=(float) (-x_rotated*sin_tilt+z*cos_tilt);
- if (((double) (x_prime[prime_num])) > *x_prime_max)
- *x_prime_max=(double) (x_prime[prime_num]);
- if (((double) (y_prime[prime_num])) < *y_prime_min)
- *y_prime_min=(double) (y_prime[prime_num]);
- if (((double) (y_prime[prime_num])) > *y_prime_max)
- *y_prime_max=(double) (y_prime[prime_num]);
- if (((double) (z_prime[prime_num])) < *z_prime_min)
- *z_prime_min=(double) (z_prime[prime_num]);
- if (((double) (z_prime[prime_num])) > *z_prime_max)
- *z_prime_max=(double) (z_prime[prime_num]);
- y+=y_delta;
- prime_num++;
- }
- x+=x_delta;
- }
- if ((! SizeMaze_kill))
- {
- magnitude=(*light).x*(*light).x;
- magnitude+=((*light).y*(*light).y);
- magnitude+=((*light).z*(*light).z);
- magnitude=sqrt(magnitude);
- (*light).x/=magnitude;
- (*light).y/=magnitude;
- (*light).z/=magnitude;
- }
- return;
- }
-
- static void shade(num_x_divisions,num_y_divisions,x_prime,y_prime,z_prime,color,
- light)
- int num_x_divisions;
- int num_y_divisions;
- float *x_prime;
- float *y_prime;
- float *z_prime;
- unsigned char *color;
- vertex_rec *light;
- {
- static double magnitude;
- static vertex_rec normal;
- static long prime_num;
- static vertex_rec vertex [4];
- register int x_division_num;
- register int y_division_num;
-
- prime_num=0l;
- for (x_division_num=0;
- ((! SizeMaze_kill) && (x_division_num < num_x_divisions));
- x_division_num++)
- {
- for (y_division_num=0;
- ((! SizeMaze_kill) && (y_division_num < num_y_divisions));
- y_division_num++)
- {
- vertex[0].x=(double) (x_prime[prime_num]);
- vertex[0].y=(double) (y_prime[prime_num]);
- vertex[0].z=(double) (z_prime[prime_num]);
- if (x_division_num < (num_x_divisions-1))
- if (y_division_num < (num_y_divisions-1))
- {
- prime_num+=((long) num_y_divisions);
- vertex[1].x=(double) (x_prime[prime_num]);
- vertex[1].y=(double) (y_prime[prime_num]);
- vertex[1].z=(double) (z_prime[prime_num]);
- prime_num++;
- vertex[2].x=(double) (x_prime[prime_num]);
- vertex[2].y=(double) (y_prime[prime_num]);
- vertex[2].z=(double) (z_prime[prime_num]);
- prime_num-=((long) num_y_divisions);
- vertex[3].x=(double) (x_prime[prime_num]);
- vertex[3].y=(double) (y_prime[prime_num]);
- vertex[3].z=(double) (z_prime[prime_num]);
- prime_num--;
- }
- else
- {
- prime_num--;
- vertex[1].x=(double) (x_prime[prime_num]);
- vertex[1].y=(double) (y_prime[prime_num]);
- vertex[1].z=(double) (z_prime[prime_num]);
- prime_num+=((long) num_y_divisions);
- vertex[2].x=(double) (x_prime[prime_num]);
- vertex[2].y=(double) (y_prime[prime_num]);
- vertex[2].z=(double) (z_prime[prime_num]);
- prime_num++;
- vertex[3].x=(double) (x_prime[prime_num]);
- vertex[3].y=(double) (y_prime[prime_num]);
- vertex[3].z=(double) (z_prime[prime_num]);
- prime_num-=((long) num_y_divisions);
- }
- else
- if (y_division_num < (num_y_divisions-1))
- {
- prime_num++;
- vertex[1].x=(double) (x_prime[prime_num]);
- vertex[1].y=(double) (y_prime[prime_num]);
- vertex[1].z=(double) (z_prime[prime_num]);
- prime_num-=((long) num_y_divisions);
- vertex[2].x=(double) (x_prime[prime_num]);
- vertex[2].y=(double) (y_prime[prime_num]);
- vertex[2].z=(double) (z_prime[prime_num]);
- prime_num--;
- vertex[3].x=(double) (x_prime[prime_num]);
- vertex[3].y=(double) (y_prime[prime_num]);
- vertex[3].z=(double) (z_prime[prime_num]);
- prime_num+=((long) num_y_divisions);
- }
- else
- {
- prime_num-=((long) num_y_divisions);
- vertex[1].x=(double) (x_prime[prime_num]);
- vertex[1].y=(double) (y_prime[prime_num]);
- vertex[1].z=(double) (z_prime[prime_num]);
- prime_num--;
- vertex[2].x=(double) (x_prime[prime_num]);
- vertex[2].y=(double) (y_prime[prime_num]);
- vertex[2].z=(double) (z_prime[prime_num]);
- prime_num+=((long) num_y_divisions);
- vertex[3].x=(double) (x_prime[prime_num]);
- vertex[3].y=(double) (y_prime[prime_num]);
- vertex[3].z=(double) (z_prime[prime_num]);
- prime_num++;
- }
- normal.x
- =(vertex[1].y-vertex[0].y)*(vertex[3].z-vertex[0].z)
- -(vertex[3].y-vertex[0].y)*(vertex[1].z-vertex[0].z)
- +(vertex[2].y-vertex[1].y)*(vertex[0].z-vertex[1].z)
- -(vertex[0].y-vertex[1].y)*(vertex[2].z-vertex[1].z)
- +(vertex[3].y-vertex[2].y)*(vertex[1].z-vertex[2].z)
- -(vertex[1].y-vertex[2].y)*(vertex[3].z-vertex[2].z)
- +(vertex[0].y-vertex[3].y)*(vertex[2].z-vertex[3].z)
- -(vertex[2].y-vertex[3].y)*(vertex[0].z-vertex[3].z);
- normal.y
- =(vertex[3].x-vertex[0].x)*(vertex[1].z-vertex[0].z)
- -(vertex[1].x-vertex[0].x)*(vertex[3].z-vertex[0].z)
- +(vertex[0].x-vertex[1].x)*(vertex[2].z-vertex[1].z)
- -(vertex[2].x-vertex[1].x)*(vertex[0].z-vertex[1].z)
- +(vertex[1].x-vertex[2].x)*(vertex[3].z-vertex[2].z)
- -(vertex[3].x-vertex[2].x)*(vertex[1].z-vertex[2].z)
- +(vertex[2].x-vertex[3].x)*(vertex[0].z-vertex[3].z)
- -(vertex[0].x-vertex[3].x)*(vertex[2].z-vertex[3].z);
- normal.z
- =(vertex[1].x-vertex[0].x)*(vertex[3].y-vertex[0].y)
- -(vertex[3].x-vertex[0].x)*(vertex[1].y-vertex[0].y)
- +(vertex[2].x-vertex[1].x)*(vertex[0].y-vertex[1].y)
- -(vertex[0].x-vertex[1].x)*(vertex[2].y-vertex[1].y)
- +(vertex[3].x-vertex[2].x)*(vertex[1].y-vertex[2].y)
- -(vertex[1].x-vertex[2].x)*(vertex[3].y-vertex[2].y)
- +(vertex[0].x-vertex[3].x)*(vertex[2].y-vertex[3].y)
- -(vertex[2].x-vertex[3].x)*(vertex[0].y-vertex[3].y);
- if (normal.x < 0.0)
- color[prime_num]=NUM_COLORS;
- else
- {
- magnitude
- =sqrt(normal.x*normal.x+normal.y*normal.y+normal.z*normal.z);
- if (magnitude == 0.0)
- color[prime_num]=(unsigned char) 0;
- else
- {
- color[prime_num]
- =(unsigned char) ((((float) (NUM_COLORS-1))/2.0)*(1.0
- +((*light).x*normal.x+(*light).y*normal.y
- +(*light).z*normal.z)/magnitude));
- if (color[prime_num] >= (unsigned char) (NUM_COLORS-1))
- color[prime_num]=(unsigned char) (NUM_COLORS-2);
- }
- }
- prime_num++;
- }
- }
- return;
- }
-
- static void adjust_perspective(num_x_divisions,num_y_divisions,x_prime,y_prime,
- z_prime,x_prime_max,y_prime_min,y_prime_max,z_prime_min,z_prime_max)
- int num_x_divisions;
- int num_y_divisions;
- float *x_prime;
- float *y_prime;
- float *z_prime;
- double x_prime_max;
- double y_prime_min;
- double y_prime_max;
- double z_prime_min;
- double z_prime_max;
- {
- static long prime_num;
- static vertex_rec vertex [4];
- register int x_division_num;
- static double x_eye;
- static double y_center;
- register int y_division_num;
- static double z_center;
-
- if ((y_prime_max-y_prime_min) > (z_prime_max-z_prime_min))
- x_eye=1.1*(y_prime_max-y_prime_min)+x_prime_max;
- else
- x_eye=1.1*(z_prime_max-z_prime_min)+x_prime_max;
- if (((y_prime_max-y_prime_min) > (z_prime_max-z_prime_min))
- || (z_prime_max != z_prime_min))
- {
- y_center=(y_prime_max+y_prime_min)/2.0;
- z_center=(z_prime_max+z_prime_min)/2.0;
- prime_num=0l;
- for (x_division_num=0;
- ((! SizeMaze_kill) && (x_division_num < num_x_divisions));
- x_division_num++)
- {
- for (y_division_num=0;
- ((! SizeMaze_kill) && (y_division_num < num_y_divisions));
- y_division_num++)
- {
- vertex[0].x=(double) (x_prime[prime_num]);
- vertex[0].y=(double) (y_prime[prime_num]);
- vertex[0].z=(double) (z_prime[prime_num]);
- if (x_division_num < (num_x_divisions-1))
- if (y_division_num < (num_y_divisions-1))
- {
- prime_num+=((long) num_y_divisions);
- vertex[1].x=(double) (x_prime[prime_num]);
- vertex[1].y=(double) (y_prime[prime_num]);
- vertex[1].z=(double) (z_prime[prime_num]);
- prime_num++;
- vertex[2].x=(double) (x_prime[prime_num]);
- vertex[2].y=(double) (y_prime[prime_num]);
- vertex[2].z=(double) (z_prime[prime_num]);
- prime_num-=((long) num_y_divisions);
- vertex[3].x=(double) (x_prime[prime_num]);
- vertex[3].y=(double) (y_prime[prime_num]);
- vertex[3].z=(double) (z_prime[prime_num]);
- prime_num--;
- }
- else
- {
- prime_num--;
- vertex[1].x=(double) (x_prime[prime_num]);
- vertex[1].y=(double) (y_prime[prime_num]);
- vertex[1].z=(double) (z_prime[prime_num]);
- prime_num+=((long) num_y_divisions);
- vertex[2].x=(double) (x_prime[prime_num]);
- vertex[2].y=(double) (y_prime[prime_num]);
- vertex[2].z=(double) (z_prime[prime_num]);
- prime_num++;
- vertex[3].x=(double) (x_prime[prime_num]);
- vertex[3].y=(double) (y_prime[prime_num]);
- vertex[3].z=(double) (z_prime[prime_num]);
- prime_num-=((long) num_y_divisions);
- }
- else
- if (y_division_num < (num_y_divisions-1))
- {
- prime_num++;
- vertex[1].x=(double) (x_prime[prime_num]);
- vertex[1].y=(double) (y_prime[prime_num]);
- vertex[1].z=(double) (z_prime[prime_num]);
- prime_num-=((long) num_y_divisions);
- vertex[2].x=(double) (x_prime[prime_num]);
- vertex[2].y=(double) (y_prime[prime_num]);
- vertex[2].z=(double) (z_prime[prime_num]);
- prime_num--;
- vertex[3].x=(double) (x_prime[prime_num]);
- vertex[3].y=(double) (y_prime[prime_num]);
- vertex[3].z=(double) (z_prime[prime_num]);
- prime_num+=((long) num_y_divisions);
- }
- else
- {
- prime_num-=((long) num_y_divisions);
- vertex[1].x=(double) (x_prime[prime_num]);
- vertex[1].y=(double) (y_prime[prime_num]);
- vertex[1].z=(double) (z_prime[prime_num]);
- prime_num--;
- vertex[2].x=(double) (x_prime[prime_num]);
- vertex[2].y=(double) (y_prime[prime_num]);
- vertex[2].z=(double) (z_prime[prime_num]);
- prime_num+=((long) num_y_divisions);
- vertex[3].x=(double) (x_prime[prime_num]);
- vertex[3].y=(double) (y_prime[prime_num]);
- vertex[3].z=(double) (z_prime[prime_num]);
- prime_num++;
- }
- y_prime[prime_num]=(float) y_center
- +(vertex[0].y-y_center)*(x_eye-x_prime_max)
- /(x_eye-vertex[0].x);
- z_prime[prime_num]=(float) z_center
- +(vertex[0].z-z_center)*(x_eye-x_prime_max)
- /(x_eye-vertex[0].x);
- x_prime[prime_num]=(float)
- (-(vertex[0].x+vertex[1].x+vertex[2].x+vertex[3].x)/4.0);
- prime_num++;
- }
- }
- }
- return;
- }
-
- static void sort_back_to_front(num_primes,x_prime,x_division_index,
- y_division_index)
- long num_primes;
- float *x_prime;
- int *x_division_index;
- int *y_division_index;
- {
- static int finished;
- static long key_index_1;
- static long key_index_2;
- static long left;
- static long right;
- static float t1;
- register int t2;
- register int t3;
-
- left=num_primes/((long) 2);
- right=num_primes-1l;
- t1=x_prime[0];
- t2=x_division_index[0];
- t3=y_division_index[0];
- while ((! SizeMaze_kill) && (right > 0l))
- {
- if (left > 0l)
- {
- left--;
- t1=x_prime[left];
- t2=x_division_index[left];
- t3=y_division_index[left];
- }
- else
- {
- t1=x_prime[right];
- t2=x_division_index[right];
- t3=y_division_index[right];
- x_prime[right]=x_prime[0];
- x_division_index[right]=x_division_index[0];
- y_division_index[right]=y_division_index[0];
- right--;
- }
- if (right > 0l)
- {
- finished=FALSE;
- key_index_2=left;
- while (! finished)
- {
- key_index_1=key_index_2;
- key_index_2+=key_index_2;
- key_index_2++;
- if (key_index_2 > right)
- finished=TRUE;
- else
- {
- if (key_index_2 != right)
- {
- if (x_prime[key_index_2] > x_prime[key_index_2+1])
- key_index_2++;
- }
- if (t1 <= x_prime[key_index_2])
- finished=TRUE;
- else
- {
- x_prime[key_index_1]=x_prime[key_index_2];
- x_division_index[key_index_1]
- =x_division_index[key_index_2];
- y_division_index[key_index_1]
- =y_division_index[key_index_2];
- }
- }
- }
- x_prime[key_index_1]=t1;
- x_division_index[key_index_1]=t2;
- y_division_index[key_index_1]=t3;
- }
- }
- x_prime[0]=t1;
- x_division_index[0]=t2;
- y_division_index[0]=t3;
- return;
- }
-
- static void plot(arg)
- ULONG arg;
- {
- static POINTL box [4];
- static int box_num;
- static short color_num;
- static double pixels_per_unit;
- static long prime_num;
- POINTL ptlPosition;
- static long rgb [NUM_COLORS];
- static long rgb_increment;
- static int solution;
- static long tint;
- static vertex_rec vertex [4];
- static int x_division_num;
- static long x_prime_num;
- static int y_division_num;
- static double y_offset;
- static double y_out_max;
- static double z_offset;
- static double z_out_max;
-
- DosWaitEventSem((HEV) SizeMaze_active,(ULONG) SEM_INDEFINITE_WAIT);
- if ((! plot_kill) && (iFatalError))
- {
- GpiSetColor(hPS,ERROR_COLOR);
- ptlPosition.x=0;
- ptlPosition.y=0;
- GpiMove(hPS,&ptlPosition);
- ptlPosition.x=iMaxX;
- ptlPosition.y=iMaxY;
- GpiBox(hPS,DRO_FILL,&ptlPosition,(long) 0,(long) 0);
- DosBeep(60,333);
- }
- else
- {
- if (! plot_kill)
- {
- if ((solve_draw_or_clear == ((int) 'D'))
- || (solve_draw_or_clear == ((int) 'B')))
- GpiErase(hPS);
- rgb_increment=0x111111;
- tint=0l;
- for (color_num=0; color_num < (NUM_COLORS-1); color_num++)
- {
- rgb[color_num]=tint;
- tint+=rgb_increment;
- }
- rgb[NUM_COLORS-1]=0xff0000;
- GpiCreateLogColorTable(hPS,LCOL_RESET,LCOLF_CONSECRGB,0L,16L,
- &rgb[0]);
- }
- if (! plot_kill)
- {
- y_out_max=(double) iMaxX;
- z_out_max=(double) iMaxY;
- if (aspect_ratio*z_out_max*(y_prime_max-y_prime_min)
- > y_out_max*(z_prime_max-z_prime_min))
- {
- pixels_per_unit
- =y_out_max/(aspect_ratio*(y_prime_max-y_prime_min));
- y_offset=0.0;
- z_offset
- =(z_out_max-pixels_per_unit*(z_prime_max-z_prime_min))/2.0;
- }
- else
- if (aspect_ratio*z_out_max*(y_prime_max-y_prime_min)
- < y_out_max*(z_prime_max-z_prime_min))
- {
- pixels_per_unit=z_out_max/(z_prime_max-z_prime_min);
- y_offset=(y_out_max
- -aspect_ratio*pixels_per_unit*(y_prime_max-y_prime_min))/2.0;
- z_offset=0.0;
- }
- else
- {
- pixels_per_unit=1.0;
- y_offset=y_out_max/2.0;
- z_offset=z_out_max/2.0;
- }
- }
- for (x_prime_num=0l; ((! plot_kill) && (x_prime_num < num_primes));
- x_prime_num++)
- {
- x_division_num=x_division_index[x_prime_num];
- if (x_division_num < (num_x_divisions-1))
- {
- y_division_num=y_division_index[x_prime_num];
- if (y_division_num < (num_y_divisions-1))
- {
- prime_num
- =((long) num_y_divisions)*((long) x_division_num)
- +((long) y_division_num);
- color_num=(short) (color[prime_num]);
- if (color_num < NUM_COLORS)
- {
- vertex[0].y=(double) (y_prime[prime_num]);
- vertex[0].z=(double) (z_prime[prime_num]);
- solution=(base_z[prime_num] == (unsigned char) 2);
- prime_num+=((long) num_y_divisions);
- vertex[1].y=(double) (y_prime[prime_num]);
- vertex[1].z=(double) (z_prime[prime_num]);
- if (solution)
- solution=(base_z[prime_num] == (unsigned char) 2);
- prime_num++;
- vertex[2].y=(double) (y_prime[prime_num]);
- vertex[2].z=(double) (z_prime[prime_num]);
- if (solution)
- solution=(base_z[prime_num] == (unsigned char) 2);
- prime_num-=((long) num_y_divisions);
- vertex[3].y=(double) (y_prime[prime_num]);
- vertex[3].z=(double) (z_prime[prime_num]);
- if (solution)
- solution=(base_z[prime_num] == (unsigned char) 2);
- if ((solve_draw_or_clear == ((int) 'D'))
- || (solve_draw_or_clear == ((int) 'B'))
- || ((solve_draw_or_clear == ((int) 'S'))
- && (solution))
- || ((solve_draw_or_clear == ((int) 'C'))
- && (solution)))
- {
- if ((solve_draw_or_clear == ((int) 'S'))
- || ((solve_draw_or_clear == ((int) 'B'))
- && (solution)))
- color_num=(short) (NUM_COLORS-1);
- for (box_num=0; box_num < 4; box_num++)
- {
- box[box_num].x=(long) (y_offset
- +pixels_per_unit*aspect_ratio
- *(vertex[box_num].y-y_prime_min));
- box[box_num].y=(long) (z_offset
- +pixels_per_unit
- *(vertex[box_num].z-z_prime_min));
- }
- GpiBeginPath(hPS,1L);
- ptlPosition.x=box[3].x;
- ptlPosition.y=box[3].y;
- GpiMove(hPS,&ptlPosition);
- GpiPolyLine(hPS,4l,box);
- GpiEndPath(hPS);
- GpiSetColor(hPS,color_num);
- GpiFillPath(hPS,1l,FPATH_ALTERNATE);
- }
- }
- }
- }
- }
- }
- DosEnterCritSec();
- if (! plot_kill)
- WinPostMsg(plot_hwnd,(ULONG) WM_PLOT_DONE,(MPARAM) NULL,
- (MPARAM) NULL);
- DosPostEventSem((HEV) plot_active);
- return;
- }
-
- static void DestroyMaze(x_prime,y_prime,z_prime,x_division_index,
- y_division_index,color,base_z,stack)
- float **x_prime;
- float **y_prime;
- float **z_prime;
- int **x_division_index;
- int **y_division_index;
- unsigned char **color;
- unsigned char **base_z;
- stack_rec **stack;
- {
- static int y;
-
- if (*stack != (stack_rec *) NULL)
- {
- free((void *) *stack);
- *stack=(stack_rec *) NULL;
- }
- if (*base_z != (unsigned char *) NULL)
- {
- free((void *) *base_z);
- *base_z=(unsigned char *) NULL;
- }
- if (*color != (unsigned char *) NULL)
- {
- free((void *) *color);
- *color=(unsigned char *) NULL;
- }
- if (*y_division_index != (int *) NULL)
- {
- free((void *) *y_division_index);
- *y_division_index=(int *) NULL;
- }
- if (*x_division_index != (int *) NULL)
- {
- free((void *) *x_division_index);
- *x_division_index=(int *) NULL;
- }
- if (*z_prime != (float *) NULL)
- {
- free((void *) *z_prime);
- *z_prime=(float *) NULL;
- }
- if (*y_prime != (float *) NULL)
- {
- free((void *) *y_prime);
- *y_prime=(float *) NULL;
- }
- if (*x_prime != (float *) NULL)
- {
- free((void *) *x_prime);
- *x_prime=(float *) NULL;
- }
- if (page != (char **) NULL)
- {
- for (y=0; y < max_y_plus_1; y++)
- free((void *) (page[y]));
- free((void *) page);
- page=(char **) NULL;
- }
- return;
- }
-
- static double f(x,y)
- double x;
- double y;
- {
- static int solution;
- register int x_next;
- static int x_offset;
- static int x_out;
- static int y_next;
- register int y_offset;
- static int y_out;
- static double z;
-
- y_next=(int) x;
- y_out=y_next+RESOLUTION;
- y_out-=2;
- y_out/=RESOLUTION;
- y_out--;
- if ((y_out%2 == 0)
- && (2*((y_next+RESOLUTION-2)-(RESOLUTION*(y_out+1)))
- > RESOLUTION))
- y_out++;
- if (y_out < 0)
- z=0.0;
- else
- if (y_out > max_y)
- z=0.0;
- else
- {
- x_next=(int) y;
- x_out=x_next+RESOLUTION;
- x_out-=2;
- x_out/=RESOLUTION;
- x_out--;
- if ((x_out%2 == 0)
- && (2*((x_next+RESOLUTION-2)-(RESOLUTION*(x_out+1)))
- > RESOLUTION))
- x_out++;
- if (x_out < 0)
- z=0.0;
- else
- if (x_out > max_x)
- z=0.0;
- else
- if (page[y_out][x_out] == 'W')
- {
- solution=FALSE;
- for (x_offset=-1; x_offset <= 1; x_offset++)
- for (y_offset=-1; y_offset <= 1; y_offset++)
- if (x_offset != 0)
- {
- x_next=x_out+x_offset;
- y_next=y_out+y_offset;
- if ((x_next >= 0)
- && (x_next <= max_x)
- && (y_next >= 0)
- && (y_next <= max_y)
- && (page[y_next][x_next] == 'S'))
- solution=TRUE;
- }
- else
- {
- if (y_offset != 0)
- {
- x_next=x_out+x_offset;
- y_next=y_out+y_offset;
- if ((x_next >= 0)
- && (x_next <= max_x)
- && (y_next >= 0)
- && (y_next <= max_y)
- && (page[y_next][x_next] == 'S'))
- solution=TRUE;
- }
- };
- z=4.0*((double) RESOLUTION);
- if (solution)
- z=-z;
- }
- else
- z=0.0;
- }
- return(z);
- }
-
-