home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d161 / lgz.lha / LGZ / LGZ.c < prev    next >
C/C++ Source or Header  |  1988-10-02  |  21KB  |  860 lines

  1. /*Let's Get Ziviliced!
  2.   - a terrain generator & editor for the game LGZ.
  3.   by Malik Hyldtoft (who made the game)
  4.   &  Lars R. Clausen (who is writing the program)
  5.   The terrain generator algoritm is from Chris Gray's Terrain, thanks, Chris.
  6.   Filerequester from Peter Da Silva, thanks. For further notes on 
  7.   the requester, see STDFile.c */
  8.  
  9. /*Data.pre includes all*/
  10.  
  11. void *OpenLibrary();
  12. void *OpenScreen();
  13.  
  14. int errno;
  15. struct bufferstruct {
  16.        int heigth,growth;
  17.        };
  18. /*
  19. **   It starts with OpenThings(), after that I initialize colors,
  20. **   draw etc.
  21. **   It closes with CloseThings() and exit(0).
  22. **   Thanks go to P.S.Kivolowitz for making a simple program we
  23. **   could learn and steal screen and window structures from.
  24. */
  25.  
  26. struct IntuitionBase *IntuitionBase;
  27. struct GfxBase *GfxBase;
  28. struct DOSBase *DOSBase;
  29.  
  30. int is_cli = 0;
  31.  
  32. struct Window *w;                  /* window structure returned by exec */
  33.  
  34. /*
  35. ** mask is a bit mask of things that I should close or deallocate
  36. ** when the program terminates for  any reason.  after opening or
  37. ** allocating some resource set the appropriate bit in mask.
  38. */
  39.  
  40. unsigned int mask = 0;
  41.  
  42. #define  INTUITION   0x00000001
  43. #define  GRAPHICS    0x00000002
  44. #define  SCREEN      0x00000004
  45. #define  WINDOW      0x00000008
  46. #define  DOSBASE     0x00000010
  47.  
  48. OpenThings(argc)      /* Open libraries, screen, window etc. */
  49. {    
  50.     if (argc) is_cli = 1;
  51.  
  52.     if (!(GfxBase = (struct GfxBase *)
  53.            OpenLibrary("graphics.library",0L)))
  54.     {
  55.        if (is_cli) printf("no graphics library!!!\n");
  56.        CloseThings();
  57.        exit(1);
  58.     }
  59.     mask |= GRAPHICS;
  60.  
  61.         if (!(IntuitionBase = (struct IntuitionBase *)
  62.            OpenLibrary("intuition.library", 0L)))
  63.         {
  64.        if (is_cli) printf("no intuition here!!\n");
  65.        CloseThings();
  66.        exit(1);
  67.     }
  68.     mask |= INTUITION;
  69.     
  70.     if (!(DOSBase = (struct DOSBase *)
  71.        OpenLibrary("dos.library", 0L)))
  72.     {
  73.        if (is_cli) printf("DOS closed for now...\n");
  74.        CloseThings();
  75.        exit(2);
  76.     }
  77.     mask |= DOSBASE;
  78.  
  79.         if (!(GfxBase->DisplayFlags & PAL)) ns.ViewModes |= LACE;
  80.     if ((stdscreen = (struct Screen *) 
  81.            OpenScreen(&ns)) == (struct Screen *) NULL) 
  82.         {
  83.        if (is_cli) printf("could not open the screen!\n");
  84.        CloseThings();
  85.        exit(3);
  86.     }
  87.     
  88.     mask |= SCREEN;
  89.         nw.Screen = stdscreen;
  90.  
  91.     if ((w = (struct Window *)
  92.            OpenWindow(&nw)) == (struct Window *) NULL) 
  93.            {
  94.        if (is_cli) printf("could not open the window!\n");
  95.        CloseThings();
  96.        exit(4);
  97.     }
  98.     mask |= WINDOW;
  99.     SetMenuStrip(w,MyMenu);
  100.     ShowTitle(stdscreen,FALSE);
  101.     MySetDMRequester();
  102. }
  103.  
  104. CloseThings()   /* Closes all the stuff opened above */
  105. {
  106.    if (w) ClearMenuStrip(w);
  107.     if (w) ClearDMRequest(w);
  108.     if (mask & WINDOW)    CloseWindow(w);
  109.     if (mask & SCREEN)    CloseScreen(stdscreen);
  110.     if (mask & DOSBASE)   CloseLibrary(DOSBase);
  111.     if (mask & GRAPHICS)  CloseLibrary(GfxBase);
  112.     /*OpenWorkBench();*/
  113.     if (mask & INTUITION) CloseLibrary(IntuitionBase);
  114. }
  115.  
  116. Abort()   /* Get the hell out of here */
  117. {
  118.   CloseThings();
  119.   exit(100);
  120. }
  121.  
  122. InitColorMap()   /* Stolen from Kivolowitz */
  123. {
  124.     static short map_values[NMAP] = {
  125.        /*   0-3 */  0x000A , 0x0FFF , 0x0000 , 0x0EC1 ,
  126.        /*   4-7 */  0x0F12 , 0x000C , 0x0050 , 0x0060 ,
  127.        /*  8-11 */  0x0070 , 0x0080 , 0x0090 , 0x00A0 ,
  128.        /* 12-15 */  0x00B0 , 0x00C0 , 0x00D0 , 0x0FB1 ,
  129.        /* 16-19 */  0x0D80 , 0x0C80 , 0x0C70 , 0x0B70 ,
  130.        /* 20-23 */  0x0A70 , 0x0A60 , 0x0960 , 0x0850 ,
  131.        /* 24-27 */  0x0777 , 0x0888 , 0x0999 , 0x0AAA ,
  132.        /* 28-31 */  0x0BBB , 0x0CCC , 0x0DDD , 0x0EEE
  133.                         };
  134.     LoadRGB4(&stdscreen->ViewPort , map_values , (long)NMAP);
  135. }
  136.  
  137. #define SIZE    6 
  138. #define COUNT    1 << SIZE 
  139. #define COLOURS    32 
  140.  
  141. /* These values for reference only, the real thing in DMReq */
  142. int GRange[6] = {64,32,16,6,3,1};  /* Parameters for growth */
  143. int HRange[6] = {64,32,16,6,3,1};  /* Parameters for height */
  144. short Cell[COUNT][COUNT][2]; 
  145. int VegOnOff = 1;
  146.  
  147. set(l,c,size,height,nr)  /* Set a single cell */
  148. unsigned int l,c,size,nr;
  149. int height;
  150. {
  151.    unsigned int rang;
  152.  
  153.    if (nr == 0) rang = HRange[size];
  154.    else rang = GRange[size];
  155.    height += rand()/(32767/rang) - (rang+1)/2;
  156.    Cell[l][c][nr] = height;
  157. }
  158.  
  159. Grow(nr)   /* Grow the terrain, either for height or for vegetation */
  160. int nr;
  161. {
  162.    unsigned int step,nextstep,l1,l2,c1 = 0;
  163.    register unsigned int l,c,i,c2 = 0;
  164.     
  165.     /* User may have entered new HRange/GRange values from DMReq */
  166.     for (i=0; i<6; i++) {
  167.         HRange[i] = IntGadgetStuff[i].LongInt;
  168.         GRange[i] = IntGadgetStuff[i+6].LongInt;
  169.         }
  170.    Cell[0][0][nr] = 0;
  171.    step = COUNT;
  172.    for (i=0; i<SIZE; i++)
  173.    {
  174.       nextstep = step / 2;
  175.       for (l=0; l<COUNT; l+=step)
  176.       {
  177.          l1 = l+nextstep;
  178.          l2 = l+step;
  179.          if (l2 == COUNT) l2 = 0;
  180.          for (c=0; c<COUNT; c+=step)
  181.          {
  182.             c1 = c+nextstep;
  183.             c2 = c+step;
  184.             if (c2 == COUNT) c2 = 0;
  185.             set(l,c1,i,(Cell[l][c][nr]+Cell[l][c2][nr])/2,nr);
  186.             set(l1,c,i,(Cell[l][c][nr]+Cell[l2][c2][nr])/2,nr);
  187.             set(l1,c1,i,(Cell[l][c][nr]+Cell[l][c2][nr]+
  188.                          Cell[l2][c][nr]+Cell[l2][c2][nr])/4,nr);
  189.          }
  190.       }
  191.       step = nextstep;
  192.    }
  193. }
  194.  
  195. DrawMap()   /* Draw it */
  196. {
  197.    register long x,y,h,g = 0;
  198.  
  199.    for (x=0; x<COUNT; x++)
  200.       for (y=0; y<COUNT; y++)
  201.       {
  202.          h = Cell[x][y][0];
  203.          g = Cell[x][y][1];
  204.          if (h<5)
  205.             h = 5;
  206.          else
  207.             if (h >= COLOURS)
  208.                h = COLOURS-1;
  209.          SetAPen(w->RPort,h);
  210.          RectFill(w->RPort,x*4,y*3+10,x*4+3,y*3+12);
  211.          if ((Cell[x][y][0]>5) && (Cell[x][y][0]<15) && (VegOnOff == TRUE))
  212.          {
  213.             SetAPen(w->RPort,15L);
  214.             if ((g>-10) && (g<13))
  215.             {
  216.                WritePixel(w->RPort,x*4+1,y*3+11);
  217.                WritePixel(w->RPort,x*4+2,y*3+11);
  218.             }   
  219.             else
  220.                if (g<=-10)
  221.                   RectFill(w->RPort,x*4+1,y*3+10,x*4+2,y*3+12);
  222.          }
  223.       }
  224. }
  225.  
  226. LoadMap()   /* Load formerly saved maps */
  227. {
  228. FILE *fp,*fopen();
  229. char filename[MAXFILENAME];
  230. int error=1;
  231.  
  232.    if (stdfile("Load Map","df1:LGZ/LGZ.map",NULL,filename)==1)
  233.    {
  234.    if ((fp = fopen(filename,"r")) == NULL)
  235.       printf("Couldn't open %s\n",filename);
  236.    else
  237.    {
  238.       error=fread(&Cell[0][0][0],16384,1,fp);
  239.       if (error == 0)
  240.          {
  241.          printf("Error in reading map from file\n");
  242.          if feof(fp) printf("Unexpected end of file.\n");
  243.             else printf("Error number: %d\n",errno);
  244.          }
  245.       fclose(fp);
  246.       }
  247.    }
  248. }
  249.  
  250. SaveMap()  /* Save the map */
  251. {
  252. FILE *fp,*fopen();
  253. char filename[30];
  254. int error=1l;
  255.  
  256.    if (stdfile("Save Map","df1:LGZ/",NULL,filename)==1)
  257.    {
  258.       if ((fp = fopen(filename,"w")) == NULL)
  259.          printf("Couldn't open %s\n",filename);
  260.       else
  261.       {
  262.          error=fwrite(&Cell[0][0][0],16384,1,fp);
  263.          if (error == 0)
  264.          {
  265.             printf("Error in writing map to file\n");
  266.             printf("Error number: %d\n",errno);
  267.          }
  268.          else
  269.             printf("File written!\n");
  270.          fclose(fp);
  271.       }
  272.    }
  273. }
  274.  
  275. int ProjectHandler(n) /* Handle the Project menu */
  276. USHORT n;
  277.    {
  278.    int x,y = 0;
  279.  
  280.    switch(ITEMNUM(n))
  281.    {
  282.       case 0: {  /* Clear */
  283.          if (AutoRequest(w,&ClearText,&YesText,&NoText,NULL,NULL,200L,60L)==TRUE)
  284.          {
  285.             ClearTerrain();
  286.             SetAPen(w->RPort,5L);
  287.             RectFill(w->RPort,0L,10L,256L,240L);
  288.          }
  289.          break;
  290.          }
  291.       case 1: {  /* Open */
  292.          LoadMap();
  293.          DrawMap();
  294.          break;
  295.          }
  296.       case 2: {  /* Save */
  297.          SaveMap();
  298.          break;
  299.          }
  300.       case 3: {  /* Info */
  301.          break;
  302.          }
  303.       case 4: {  /* Quit */
  304.          if (AutoRequest(w,&QuitText,&YesText,&NoText,NULL,NULL,200L,60L)==TRUE)
  305.             {
  306.             CloseThings();
  307.             exit(0);
  308.             }
  309.          }
  310.       }
  311.    }
  312.  
  313. ClearTerrain()  /* Clear it all */
  314. {
  315.    register int x,y;
  316.  
  317.    for (x=0; x<COUNT; x++)
  318.       for (y=0; y<COUNT; y++)
  319.          Cell[x][y][0] = Cell[x][y][1] = 0;
  320. }
  321.  
  322. EditHandler(n)  /* Handle the Edit menu */
  323. USHORT n;
  324. {
  325.    switch (ITEMNUM(n))
  326.    {
  327.       case 0: /* Edit pixels */
  328.       {
  329.          Edit();
  330.          break;
  331.       }
  332.       case 1: /* Sink map */
  333.       {
  334.          Sink();
  335.          DrawMap();
  336.          break;
  337.       }
  338.       case 2: /* Lift map */
  339.       {
  340.          Lift();
  341.          DrawMap();
  342.          break;
  343.       }
  344.       case 3: /* Compress map ( divide by two, idea from Bjarne Fich) */
  345.       {
  346.          Compress();
  347.          DrawMap();
  348.          break;
  349.       }
  350.       case 4: /* Scroll Map */
  351.       {
  352.          ScrollHandler(n);
  353.          break;
  354.       }
  355.    }
  356.    
  357. }
  358.  
  359. Edit()  /* Edit pixelwise */
  360. {
  361.    long x=0,type=2,height=7,a=0,b=0,hg=0,ac,bc,h,g,outflag=0;
  362.    struct Gadget *numgad;
  363.    struct IntuiMessage *msg;
  364.    struct RastPort *rp;
  365.  
  366.    rp = w->RPort;
  367.    SetAPen(rp,1L);
  368.    RectFill(rp,279L,29L,291L,166L);
  369.    for (x=5; x<COLOURS; x++)
  370.    {
  371.       SetAPen(rp,x);
  372.       RectFill(rp,280L,(36-x)*5+5,290L,(36-x)*5+10);
  373.    }
  374.    RemoveGadget(w,&ReadyGadget);
  375.    AddGadget(w,&ExitGadget,-1);
  376.    AddGadget(w,&WoodsGadget,-1);
  377.    AddGadget(w,&DesertGadget,-1);
  378.    AddGadget(w,&PlainsGadget,-1);
  379.    AddGadget(w,&MapGad,-1);
  380.    RefreshGadgets(&ExitGadget,w,NULL);
  381.    do
  382.    {
  383.       WaitPort(w->UserPort);
  384.       msg = (struct IntuiMessage *) GetMsg(w->UserPort);
  385.       if (((msg->Class == MOUSEBUTTONS) && (msg->Code == SELECTDOWN)) ||
  386.            (msg->Class == GADGETDOWN) || ((msg->Class == MOUSEMOVE) &&
  387.            (msg->MouseX<257)))
  388.       {
  389.          a = msg->MouseX; b = msg->MouseY;
  390.          if ((a > 279) && (a < 291) &&
  391.              (b > 30)  && (b < 166))
  392.          {
  393.             DrawBorder(rp,&TypeOffBorder,70*type+17,208L);
  394.             SetAPen(rp,1L);
  395.             RectFill(rp,279L,29L,291L,166L);
  396.             for (x=5; x<COLOURS; x++)
  397.             {
  398.                SetAPen(rp,x);
  399.                RectFill(rp,280L,(36-x)*5+5,290L,(36-x)*5+10);
  400.             }
  401.             height = 37-((b-1)/5);
  402.             hg = 0L;
  403.             DrawBorder(rp,&ColBorder,279L,(36-height)*5+5);   
  404.          }
  405.          if ((a < 256L) && (b>9L) && (b<201L))
  406.          {
  407.             ac = a/4; bc = (b-9)/3;
  408.             if (hg<1L)
  409.                Cell[ac][bc][0] = height;
  410.             else
  411.                Cell[ac][bc][1] = (2-type)*30;
  412.             g = Cell[ac][bc][1]; h = Cell[ac][bc][0];
  413.             if (h>31) h = 31;
  414.             if (h<5) h = 5;
  415.             SetAPen(rp,h);
  416.             RectFill(w->RPort,ac*4,bc*3+10,ac*4+3,bc*3+12);
  417.             if ((h>5L) && (h<15L) && (VegOnOff = TRUE))
  418.             {
  419.                SetAPen(rp,15L);
  420.                if ((g>-10) && (g<13))
  421.                {
  422.                   WritePixel(rp,ac*4+1,bc*3+11);
  423.                   WritePixel(rp,ac*4+2,bc*3+11);
  424.                }   
  425.                else
  426.                   if (g<=-10)
  427.                      RectFill(rp,ac*4+1,bc*3+10,ac*4+2,bc*3+12);
  428.             }
  429.          }
  430.          if ((msg->Class == GADGETDOWN) && (a>256L))
  431.          {
  432.             numgad = msg->IAddress;
  433.             type = numgad->UserData;
  434.             if (type < 4)
  435.             {
  436.                if (type!=1) WoodsGadget.Flags&=0xFF7F;
  437.                else WoodsGadget.Flags|=0x0080;
  438.                if (type!=2) PlainsGadget.Flags&=0xFF7F;
  439.                else PlainsGadget.Flags|=0x0080;
  440.                if (type!=3) DesertGadget.Flags&=0xFF7F;
  441.                else DesertGadget.Flags|=0x0080;
  442.                RefreshGadgets(&ExitGadget,w,NULL);
  443.                SetAPen(rp,1L);
  444.                RectFill(rp,279L,29L,291L,166L);
  445.                for (x=5; x<COLOURS; x++)
  446.                {
  447.                   SetAPen(rp,x);
  448.                   RectFill(rp,280L,(36-x)*5+5,290L,(36-x)*5+10);
  449.                }
  450.                if (VegOnOff == 0)
  451.                {
  452.                   VegOnOff = 1;
  453.                   DrawMap();
  454.                }
  455.                hg = 1;
  456.             }
  457.             if (type == 4)
  458.                outflag = 1;
  459.          }
  460.       ReplyMsg(msg);
  461.       }
  462.    }
  463.    while (outflag == 0);
  464.    RemoveGadget(w,&ExitGadget);
  465.    RemoveGadget(w,&WoodsGadget);
  466.    RemoveGadget(w,&PlainsGadget);
  467.    RemoveGadget(w,&DesertGadget);
  468.    RemoveGadget(w,&MapGad);
  469.    SetAPen(rp,0L);
  470.    RectFill(rp,259L,29L,320L,226L);
  471.    AddGadget(w,&ReadyGadget,-1);
  472. }
  473.  
  474. Sink()  /* Sink Map */
  475. {
  476.    register int x,y = 0;
  477.  
  478.    for (x=0; x<COUNT; x++)
  479.       for (y=0; y<COUNT; y++)
  480.          Cell[x][y][0]--;
  481. }
  482.  
  483. Lift()  /* Lift Map */
  484. {
  485.    register int x,y = 0;
  486.  
  487.    for (x=0; x<COUNT; x++)
  488.       for (y=0; y<COUNT; y++)
  489.          Cell[x][y][0]++;
  490. }
  491.  
  492. Compress() /* Compress map */
  493. {
  494.    register int x,y =0;
  495.    
  496.    for (x=0; x<COUNT; x++)
  497.       for (y=0; y<COUNT; y++)
  498.          Cell[x][y][0] = (Cell[x][y][0]-6)/2+6;
  499. }
  500.  
  501. ScrollHandler(n) /* Handle scroll submenuitems */
  502. USHORT n;
  503. {
  504.    register int x,y,th,tg =0;
  505.    #define SCOUNT 64
  506.  
  507.    switch (SUBNUM(n))
  508.    {
  509.       case 0:/*Up*/
  510.       {
  511.          for (x=0; x<SCOUNT; x++)
  512.          {
  513.             th = Cell[x][0][0];
  514.             tg = Cell[x][0][1];
  515.             for (y=0; y<SCOUNT-1; y++)
  516.             {
  517.                Cell[x][y][0] = Cell[x][y+1][0];
  518.                Cell[x][y][1] = Cell[x][y+1][1];
  519.             }
  520.             Cell[x][SCOUNT-1][0] = th;
  521.             Cell[x][SCOUNT-1][1] = tg;
  522.          }
  523.          ScrollRaster(w->RPort,0L,3L,0L,10L,255L,201L);
  524.          DrawMapLine(1,1);
  525.          break;
  526.       }
  527.       case 1:/*Down*/
  528.       {
  529.          for (x=0; x<SCOUNT; x++)
  530.          {
  531.             th = Cell[x][SCOUNT-1][0];
  532.             tg = Cell[x][SCOUNT-1][1];
  533.             for (y=SCOUNT-1; y>0; y--)
  534.             {
  535.                Cell[x][y][0] = Cell[x][y-1][0];
  536.                Cell[x][y][1] = Cell[x][y-1][1];
  537.             }
  538.             Cell[x][0][0] = th;
  539.             Cell[x][0][1] = tg;
  540.          }
  541.          ScrollRaster(w->RPort,0L,-3L,0L,10L,255L,201L);
  542.          DrawMapLine(1,0);
  543.          break;
  544.       }
  545.       case 2:/*Left*/
  546.       {
  547.          for (x=0; x<SCOUNT; x++)
  548.          {
  549.             th = Cell[0][x][0];
  550.             tg = Cell[0][x][1];
  551.             for (y=0; y<SCOUNT-1; y++)
  552.             {
  553.                Cell[y][x][0] = Cell[y+1][x][0];
  554.                Cell[y][x][1] = Cell[y+1][x][1];
  555.             }
  556.             Cell[SCOUNT-1][x][0] = th;
  557.             Cell[SCOUNT-1][x][1] = tg;
  558.          }
  559.          ScrollRaster(w->RPort,4L,0L,0L,10L,255L,201L);
  560.          DrawMapLine(0,1);
  561.          break;
  562.       }
  563.       case 3:/*Right*/
  564.       {
  565.          for (x=0; x<SCOUNT; x++)
  566.          {
  567.             th = Cell[SCOUNT-1][x][0];
  568.             tg = Cell[SCOUNT-1][x][1];
  569.             for (y=SCOUNT-1; y>0; y--)
  570.             {
  571.                Cell[y][x][0] = Cell[y-1][x][0];
  572.                Cell[y][x][1] = Cell[y-1][x][1];
  573.             }
  574.             Cell[0][x][0] = th;
  575.             Cell[0][x][1] = tg;
  576.          }
  577.          ScrollRaster(w->RPort,-4L,0L,0L,10L,255L,201L);
  578.          DrawMapLine(0,0);
  579.          break;
  580.       }
  581.    }
  582. }
  583.  
  584. DrawMapLine(way,nr) /* Used by scroll */
  585. int way,nr;
  586. {
  587.    register long x,y,h,g = 0;
  588.  
  589.    if (way==0)
  590.    {
  591.       x = 63*nr;
  592.       for (y=0; y<COUNT; y++)
  593.       {
  594.          h = Cell[x][y][0];
  595.          g = Cell[x][y][1];
  596.          if (h<5)
  597.             h = 5;
  598.          else
  599.             if (h >= COLOURS)
  600.                h = COLOURS-1;
  601.          SetAPen(w->RPort,h);
  602.          RectFill(w->RPort,x*4,y*3+10,x*4+3,y*3+12);
  603.          if ((Cell[x][y][0]>5) && (Cell[x][y][0]<15) && (VegOnOff = TRUE))
  604.          {
  605.             SetAPen(w->RPort,15L);
  606.             if ((g>-10) && (g<13))
  607.             {
  608.                WritePixel(w->RPort,x*4+1,y*3+11);
  609.                WritePixel(w->RPort,x*4+2,y*3+11);
  610.             }   
  611.             else
  612.                if (g<=-10)
  613.                   RectFill(w->RPort,x*4+1,y*3+10,x*4+2,y*3+12);
  614.          }
  615.       }
  616.    }
  617.    else
  618.    {
  619.       y = 63*nr;
  620.       for (x=0; x<COUNT; x++)
  621.       {
  622.          h = Cell[x][y][0];
  623.          g = Cell[x][y][1];
  624.          if (h<5)
  625.             h = 5;
  626.          else
  627.             if (h >= COLOURS)
  628.                h = COLOURS-1;
  629.          SetAPen(w->RPort,h);
  630.          RectFill(w->RPort,x*4,y*3+10,x*4+3,y*3+12);
  631.          if ((Cell[x][y][0]>5) && (Cell[x][y][0]<15) && (VegOnOff == TRUE))
  632.          {
  633.             SetAPen(w->RPort,15L);
  634.             if ((g>-10) && (g<13))
  635.             {
  636.                WritePixel(w->RPort,x*4+1,y*3+11);
  637.                WritePixel(w->RPort,x*4+2,y*3+11);
  638.             }   
  639.             else
  640.                if (g<=-10)
  641.                   RectFill(w->RPort,x*4+1,y*3+10,x*4+2,y*3+12);
  642.          }
  643.       }
  644.    }
  645. }
  646.  
  647. NewMapHandler(n) /* Handle NewMap menu */
  648. USHORT n;
  649. {
  650.    switch (ITEMNUM(n))
  651.    {
  652.       case 0: /* Grow Height, Growth or both */
  653.       {
  654.          GrowHandler(n);
  655.          DrawMap();
  656.          break;
  657.       }
  658.       case 1: /* On/Off Vegetation */
  659.       {
  660.          VegOnOff = !(VegOnOff);
  661.          DrawMap();
  662.          break;
  663.       }
  664.       case 2: /* Change growth parameters - now with requester */
  665.       {
  666.          ChangePara();
  667.          break;
  668.       }
  669.       case 3: /* Make terrain more smooth - also from Bjarne Fich */
  670.               /* Doesn't work yet */
  671.       {
  672.          LevelItOut();
  673.          break;
  674.       }
  675.       case 4: /* 3-dimesional map - not implemented yet */
  676.       {
  677.          Draw3DMap();
  678.          DrawMap();
  679.          break;
  680.       }
  681.    }
  682. }
  683.  
  684. GrowHandler(nr) /* Grow submenuitems */
  685. int nr;
  686. {
  687.    switch (SUBNUM(nr))
  688.    {
  689.       case 0: /* Both */
  690.       {
  691.          Grow(0);
  692.          Grow(1);
  693.          break;
  694.       }
  695.       case 1: /* Height */
  696.       {
  697.          Grow(0);
  698.          break;
  699.       }
  700.       case 2: /* Growth */
  701.       {
  702.          Grow(1);
  703.          VegOnOff = TRUE;
  704.          break;
  705.       }
  706.    }
  707. }
  708.  
  709. MySetDMRequester()
  710. {
  711.    InitRequester(&DMReq);
  712.    DMReq.LeftEdge = 50;
  713.    DMReq.TopEdge = 30;
  714.    DMReq.Width = 150;
  715.    DMReq.Height = 130;
  716.     DMReq.RelLeft = -50;
  717.     DMReq.RelTop = -50;
  718.    DMReq.ReqGadget = &DMIntGadget[11]; /* All done */
  719.    DMReq.ReqText = &DMReqText; /* Current Parameters */
  720.    DMReq.BackFill = 3; /* To be adjusted */
  721.    DMReq.ReqBorder = &DMReqBorder;
  722.    DMReq.Flags |= POINTREL;
  723.     SetDMRequest(w,&DMReq);
  724. }
  725.  
  726. ChangePara()
  727. {    
  728.     int i;
  729.     
  730.     Request(&DMReq,w);
  731.     WaitPort(w->UserPort);
  732.     /* Seems Intuition does not keep the LongInt variabla ?!?!? */
  733.     for (i=0; i<12; i++) IntGadgetStuff[i].LongInt = atoi(DMBuffer[i]);
  734.     /* Here needs to enter some code to distinguish 
  735.        'USE' and 'Cancel!' Gadget in DMReq */
  736. }
  737.  
  738. LevelItOut()
  739. {
  740.    register long x,y,z;
  741.    long px,mx,py,my;
  742.    short TCell[COUNT][COUNT];
  743.  
  744.    for (x=0;x<COUNT;x++) 
  745.    {
  746.       for (y=0;y<COUNT;y++)
  747.       {
  748.          px=x+1;
  749.          if (px>=COUNT) px=0;
  750.          mx=x-1;
  751.          if (mx<0) mx=COUNT-1;
  752.          py=y+1;
  753.          if (py>=COUNT) py=0;
  754.          my=y-1;
  755.          if (my<0) my=COUNT-1;
  756.          z=Cell[mx][my][0];
  757.          z+=Cell[mx][py][0];
  758.          z+=Cell[px][my][0];
  759.          z+=Cell[px][py][0];
  760.          z+=Cell[mx][y][0]*2;
  761.          z+=Cell[x][my][0]*2;
  762.          z+=Cell[px][y][0]*2;
  763.          z+=Cell[x][py][0]*2;
  764.          z+=Cell[x][y][0]*8;
  765.          TCell[x][y]=(z+10)/20;
  766.       }
  767.    }
  768.    for (x=0;x<COUNT;x++)
  769.    {
  770.       for (y=0;y<COUNT;y++)
  771.       {
  772.          Cell[x][y][0]=TCell[x][y];
  773.          printf("%d,%d,%d,%d\n",x,y,Cell[x][y][0],TCell[x][y]);
  774.       }
  775.    }
  776.    DrawMap();
  777. }
  778.  
  779. Draw3DMap() /* To be made - real soon now */
  780. {
  781. }
  782.  
  783. MenuHandler(number) /* Greater Menuhandler */
  784. USHORT number;
  785. {
  786.    if (number != MENUNULL){
  787.       switch(MENUNUM(number)){
  788.          case 0: {
  789.             ProjectHandler(number);
  790.             break;
  791.             }
  792.          case 1: {
  793.             EditHandler(number);
  794.             break;
  795.             }
  796.          case 2: {
  797.             NewMapHandler(number);
  798.             break;
  799.             }
  800.          default: printf("Error in menu selection. Please debug!\n");
  801.          }
  802.       }
  803. }
  804.  
  805. main(argc)
  806. {
  807.    ULONG seconds,micros;
  808.    struct IntuiMessage *msg;
  809.    int x,y;
  810.    
  811.    OpenThings(argc);
  812.    InitColorMap();
  813.    SetAPen(w->RPort,0L);
  814.    RectFill(w->RPort,259L,9L,310L,20L);
  815.    CurrentTime(&seconds,µs);
  816.    srand(seconds);
  817.    Grow(0);
  818.    Grow(1);
  819.    DrawMap();
  820.    RefreshGadgets(&ReadyGadget,w,NULL);
  821.    for (;;) 
  822.    {
  823.       WaitPort(w->UserPort);
  824.       msg = (struct IntuiMessage *) GetMsg(w->UserPort);
  825.       if (msg != NULL)
  826.          switch (msg->Class) 
  827.          {
  828.             case MENUPICK : 
  829.             {
  830.                SetAPen(w->RPort,0L);
  831.                RectFill(w->RPort,259L,9L,310L,20L);
  832.                MenuHandler(msg->Code);
  833.                RefreshGadgets(&ReadyGadget,w,NULL);
  834.                break;
  835.             }
  836.             case CLOSEWINDOW : 
  837.             {
  838.                if (AutoRequest(w,&QuitText,&YesText,&NoText,
  839.               NULL,NULL,200L,60L)==TRUE) {
  840.                   printf("We're closing now. Goodbye (wave hands)\n");
  841.                   CloseThings();
  842.                   exit(0); /* The way out */
  843.            }
  844.            else break;
  845.             }
  846.             case GADGETDOWN :
  847.             {  /* Only thing needed yet is to recognize the Cancel! gadget */
  848.            for (x=0; x<12; x++) 
  849.                IntGadgetStuff[x].LongInt = atoi(DMBuffer[x]);
  850.            break;
  851.             }
  852.             case MOUSEBUTTONS :
  853.             {
  854.                break;
  855.             }
  856.          }
  857.    }
  858.    if (0 == 1) Abort();  /* Just to confuse - never gets here */
  859. }
  860.