home *** CD-ROM | disk | FTP | other *** search
/ Desktop Works 1995 - 1996 / desktopworks1995-1996.iso / scrnsave / win_maze / maze3d.cpp < prev    next >
C/C++ Source or Header  |  1996-01-01  |  28KB  |  850 lines

  1. #include <owl\owlpch.h>
  2. #include <owl\applicat.h>
  3. #include <owl\framewin.h>
  4. #include <owl\dc.h>
  5. #include <string.h>
  6. #include <math.h>
  7. #include <stdlib.h>
  8. #include <time.h>
  9. #include "oracle.h"
  10. #include "cell.h"
  11. #include "hexmaze.h"
  12. #include "sqrmaze.h"
  13. #include "plot3d.h"
  14. #include "maze3d.rh"
  15.  
  16. #ifndef TRUE
  17. #define TRUE -1
  18. #endif
  19. #ifndef FALSE
  20. #define FALSE 0
  21. #endif
  22.  
  23. static int    hex_external_to_plot(double,double);
  24. static double hex_f(double,double);
  25. static int    hex_red(double,double);
  26. static int    sqr_external_to_plot(double,double);
  27. static double sqr_f(double,double);
  28. static int    sqr_red(double,double);
  29.  
  30. // maze constants
  31. #define PIXELS_PER_HEXAGONAL_ROOM 60               
  32. #define PIXELS_PER_SQUARE_ROOM    40                           
  33. #define RESOLUTION                 4  // larger values takes more time (and
  34.                                       // virtual memory) but produce a better 
  35.                                       // image.
  36.  
  37. hexmaze *hexmaze_ptr; // pointer to generator for maze with hexagonal rooms
  38. sqrmaze *sqrmaze_ptr; // pointer to generator for maze with square rooms
  39.  
  40. class MazeWindow : public TFrameWindow
  41.   {
  42.     public:
  43.  
  44.       MazeWindow(TWindow* parent,const char far* title);
  45.  
  46.       void CleanupWindow();
  47.  
  48.       BOOL IdleAction(long IdleCount);
  49.         // So that the program remains responsive, the plotting is done here.
  50.  
  51.       void Paint(TDC &dc,BOOL erase,TRect &dirty);
  52.         // This method is invoked when part of the display has been made
  53.         // "dirty" (for example, when another window has been moved from
  54.         // atop the display).
  55.  
  56.     protected:
  57.  
  58.       void CeActionClear(TCommandEnabler &ce);
  59.         // Determines whether the action "Clear" may be selected.
  60.  
  61.       void CeActionSolve(TCommandEnabler &ce);
  62.         // Determines whether the action "Solve" may be selected.
  63.  
  64.       void CeStyleSquare(TCommandEnabler &ce);
  65.         // Determines whether a check mark should appear to the left of the 
  66.         // style "Square rooms".
  67.  
  68.       void CeStyleHexagon(TCommandEnabler &ce);
  69.         // Determines whether a check mark should appear to the left of the
  70.         // style "Hexagonal rooms".
  71.  
  72.       void CmActionNew();
  73.         // Processes a request for a new maze.
  74.  
  75.       void CmActionSolve();
  76.         // Processes a request that a maze be solved.
  77.  
  78.       void CmActionClear();
  79.         // Processes a request that the solution be cleared from a maze.
  80.  
  81.       void CmHelpAbout();
  82.         // Processes a request for information about the author.
  83.  
  84.       void CmHelpUsage();
  85.         // Processes a request for information about using this program.
  86.  
  87.       void CmStyleSquare();
  88.         // Processes a request for square rooms.
  89.  
  90.       void CmStyleHexagon();
  91.         // Processes a request for hexagonal rooms.
  92.  
  93.       void EvHScroll(UINT code,UINT pos,HWND wnd);
  94.         // Processes a change in the horizontal scroll bar -- a request for
  95.         // a new angle of rotation.
  96.  
  97.       void EvSize(UINT sizeType,TSize &size);
  98.         // Processes resizing of the window -- a request for a different size
  99.         // maze.
  100.  
  101.       void EvVScroll(UINT code,UINT pos,HWND wnd);
  102.         // Processes a change in the vertical scroll bar -- a request for a
  103.         // new angle of tilt.
  104.  
  105.     private:
  106.   
  107.       void   DisplayMessage(const string& msg);
  108.         // Pops up a message to the user.
  109.  
  110.       double light_x;
  111.       double light_y;
  112.       double light_z;
  113.         // Vector to the major source of light.
  114.  
  115.       int    num_columns;
  116.         // Number of columns in the current maze.
  117.  
  118.       int    num_rows;
  119.         // Number of rows in the current maze.
  120.  
  121.       int    only_plot_solution;
  122.         // If TRUE, only plot the outline to the solution of the maze.
  123.  
  124.       plot3d *plot3d_ptr;
  125.         // Pointer to an instance of the 3D plotting class.
  126.  
  127.       TRect  region_to_paint;
  128.         // The part of the maze to be plotted.
  129.  
  130.       int    rotation;
  131.         // The number of degrees the maze is rotated about a line
  132.         // perpendicular to its base.
  133.  
  134.       char   seed [9];
  135.         // Random number seed (derived from the system clock) used to
  136.         // generate the maze.
  137.  
  138.       int    solve;
  139.         // TRUE if the maze is to be solved.
  140.  
  141.       int    solved;
  142.         // TRUE if the solution is currently displayed.
  143.  
  144.       char   state;
  145.         // State of the plotting; one of the following:
  146.         //     'B' -- beginning
  147.         //     'M' -- maze being generated
  148.         //     'S' -- preparing plot
  149.         //     'P' -- plotting
  150.         //     'F' -- failure
  151.         //     'D' -- done
  152.  
  153.       char   style;
  154.         // 'H' for hexagonal rooms; 'S' for square rooms.
  155.  
  156.       int    tilt;
  157.         // The number of degrees the maze is tilted towards the viewer.
  158.   
  159.       DECLARE_RESPONSE_TABLE(MazeWindow);
  160.        // Associates user commands with methods in this program.
  161.   };
  162.  
  163. DEFINE_RESPONSE_TABLE1(MazeWindow,TFrameWindow)
  164.   EV_COMMAND(CM_ACTION_NEW,CmActionNew),
  165.   EV_COMMAND(CM_ACTION_SOLVE,CmActionSolve),
  166.   EV_COMMAND(CM_ACTION_CLEAR,CmActionClear),
  167.   EV_COMMAND(CM_HELP_ABOUT,CmHelpAbout),
  168.   EV_COMMAND(CM_HELP_USAGE,CmHelpUsage),
  169.   EV_COMMAND(CM_STYLE_SQUARE,CmStyleSquare),
  170.   EV_COMMAND(CM_STYLE_HEXAGON,CmStyleHexagon),
  171.   EV_COMMAND_ENABLE(CM_ACTION_SOLVE,CeActionSolve),
  172.   EV_COMMAND_ENABLE(CM_ACTION_CLEAR,CeActionClear),
  173.   EV_COMMAND_ENABLE(CM_STYLE_SQUARE,CeStyleSquare),
  174.   EV_COMMAND_ENABLE(CM_STYLE_HEXAGON,CeStyleHexagon),
  175.   EV_WM_HSCROLL,
  176.   EV_WM_VSCROLL,
  177.   EV_WM_SIZE,
  178. END_RESPONSE_TABLE;
  179.  
  180. MazeWindow::MazeWindow(
  181.   TWindow        *parent,
  182.   const char far *title) : TFrameWindow(parent, title)
  183.     {
  184.       Attr.Style |= WS_VSCROLL | WS_HSCROLL;
  185.         // Scroll bars are used to specify tilt and rotation.
  186.       AssignMenu("MAZE_MENU");
  187.       state='B';             // beginning plot
  188.       style='S';             // square rooms
  189.       rotation=0;            // (degrees)
  190.       tilt=30;               // (degrees)
  191.       light_x=(double) 1.5;  // vector to light source
  192.       light_y=(double) -1.0;
  193.       light_z=(double) 2.6;
  194.       solve=FALSE;           // initially, don't show the solution
  195.       plot3d_ptr=new plot3d((TFrameWindow *) this); // 3D plotter
  196.     }
  197.  
  198. void MazeWindow::CleanupWindow()
  199.   {
  200.     delete plot3d_ptr; // destroy 3D plotter
  201.     TFrameWindow::CleanupWindow();
  202.   }
  203.  
  204. void MazeWindow::DisplayMessage(const string &msg)
  205. // Pops up a message to the user.
  206.   {
  207.     MessageBox(msg.c_str(),GetApplication()->GetName(),
  208.      MB_OK | MB_ICONEXCLAMATION);
  209.     return;
  210.   }
  211.  
  212. BOOL MazeWindow::IdleAction(long IdleCount)
  213. // So that the program remains responsive, the plotting is done here.
  214.   {
  215.     switch (state)
  216.       // State of the plotting; one of the following:
  217.       //     'B' -- beginning
  218.       //     'R' -- restarting
  219.       //     'M' -- maze being generated
  220.       //     'S' -- preparing plot
  221.       //     'P' -- plotting
  222.       //     'F' -- failure
  223.       //     'D' -- done
  224.       {
  225.         case 'B': // begin
  226.           {
  227.             solved=FALSE;
  228.               // The solution is not (or soon won't be) completely displayed.
  229.             GetClientRect(region_to_paint);
  230.               // The whole maze is being plotted.
  231.             SetScrollRange(SB_VERT,0,90);   
  232.               // The maze may be tilted between 0 and 90 degrees.
  233.             SetScrollRange(SB_HORZ,0,360);
  234.               // The maze may be rotated between 0 and 360 degrees.
  235.             SetScrollPos(SB_VERT,90-tilt);
  236.               // Display the current tilt on the vertical scroll bar.
  237.             SetScrollPos(SB_HORZ,rotation);
  238.               // Display the current rotation on the horizontal scroll bar.
  239.             only_plot_solution=FALSE;
  240.               // Plot the base, walls, etc.
  241.  
  242.             // Pick a maze at random. 
  243.             time_t quotient;
  244.             time_t remainder;
  245.             time_t start_time;
  246.             time(&start_time);
  247.             for (int i=0; i < 8; i++)
  248.               {
  249.                 quotient=start_time/10;
  250.                 remainder=start_time