home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume8 / phoon / phoon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-02-26  |  12.6 KB  |  396 lines

  1. /* phoon - show the phase of the moon
  2.  
  3. ver  date   who remarks
  4. --- ------- --- -------------------------------------------------------------
  5. 01C 26jan87 JP  Added backgrounds for 29 and 18 lines.
  6. 01B 28dec86 JP  Added -l flag, and backgrounds for 19 and 24 lines.
  7. 01A 08nov86 JP  Translated from the ratfor version of 12nov85, which itself
  8.                   was translated from the Pascal version of 05apr79.
  9.  
  10. Copyright (C) 1986 by Jeffrey A. Poskanzer.  Permission to use, copy,
  11. modify, and distribute this software and its documentation for any
  12. purpose and without fee is hereby granted, provided that this copyright
  13. notice appear in all copies and in all supporting documentation.  No
  14. representation is made about the suitability of this software for any
  15. purpose.  It is provided "as is" without express or implied warranty.
  16.  
  17. */
  18.  
  19. static char copyright[] = "\nCopyright (C) 1986 by Jeffrey A. Poskanzer.\n";
  20.  
  21.  
  22. #include <stdio.h>
  23. #include <math.h>
  24. #include "tws.h"
  25.  
  26.  
  27. /* Global defines and declarations. */
  28.  
  29. #define SECSPERMINUTE 60
  30. #define SECSPERHOUR (60 * SECSPERMINUTE)
  31. #define SECSPERDAY (24 * SECSPERHOUR)
  32.  
  33. #define PI 3.14159
  34.  
  35. #define DEFAULTNUMLINES 23
  36. #define MOONSTARTCOL 18
  37. #define QUARTERLITLEN 16
  38. #define QUARTERLITLENPLUSONE 17
  39.  
  40. /* If you change the aspect ratio, the canned backgrounds won't work. */
  41. #define ASPECTRATIO 0.5
  42.  
  43. /* Main program. */
  44.  
  45. main( argc, argv, envp )
  46. int argc;
  47. char *argv[], *envp[];
  48.     {
  49.     struct tws t, *twp;
  50.     char buf[100];
  51.     int numlines, argi, i;
  52.     char *usage = "usage:  %s  [ -l <lines> ]  [ <date/time> ]\n";
  53.  
  54.     /* Parge args. */
  55.     argi = 1;
  56.     /* Check for -l flag. */
  57.     numlines = DEFAULTNUMLINES;
  58.     if ( argc - argi >= 1 )
  59.     {
  60.     if ( argv[argi][0] == '-' )
  61.         {
  62.         if ( argv[argi][1] != 'l' | argv[argi][2] != '\0' )
  63.         {
  64.         fprintf( stderr, usage, argv[0] );
  65.         exit( 1 );
  66.         }
  67.         else
  68.         {
  69.         if ( argc - argi < 2 )
  70.             {
  71.             fprintf( stderr, usage, argv[0] );
  72.             exit( 1 );
  73.             }
  74.         if ( sscanf( argv[argi + 1], "%d", &numlines ) != 1 )
  75.             {
  76.             fprintf( stderr, usage, argv[0] );
  77.             exit( 1 );
  78.             }
  79.         argi += 2;
  80.         }
  81.         }
  82.     }
  83.  
  84.     /* Figure out what date and time to use. */
  85.     if ( argc - argi == 0 )
  86.     {
  87.     /* No arguments present - use the current date and time. */
  88.     twscopy( &t, dtwstime( ) );
  89.     }
  90.     else if ( argc - argi == 1 || argc - argi == 2 || argc - argi == 3 )
  91.     {
  92.     /* One, two, or three args - use them. */
  93.     strcpy( buf, argv[argi] );
  94.     if ( argc - argi > 1 )
  95.         {
  96.         strcat( buf, " " );
  97.         strcat( buf, argv[argi + 1] );
  98.         if ( argc - argi > 2 )
  99.         {
  100.         strcat( buf, " " );
  101.         strcat( buf, argv[argi + 2] );
  102.         }
  103.         }
  104.     twp = dparsetime( buf );
  105.     if ( twp == NULL || twp -> tw_flags & TW_JUNK )
  106.         {
  107.         fprintf( stderr, "illegal date/time: %s\n", buf );
  108.         exit( 1 );
  109.         }
  110.     twscopy( &t, twp );
  111.     }
  112.     else
  113.     {
  114.     /* Too many args! */
  115.     fprintf( stderr, usage, argv[0] );
  116.     exit( 1 );
  117.     }
  118.  
  119.     /* Pseudo-randomly decide what the moon is made of, and print it. */
  120.     if ( twclock( dtwstime( ) ) % 17 == 3 )
  121.     putmoon( &t, numlines, "GREENCHEESE" );
  122.     else
  123.     putmoon( &t, numlines, "@" );
  124.  
  125.     /* All done. */
  126.     exit( 0 );
  127.     }
  128.  
  129.  
  130. putmoon( t, numlines, atfiller )
  131. struct tws *t;
  132. int numlines;
  133. char *atfiller;
  134.     {
  135.     struct tws twsanewmoon;
  136.     long secsynodic = 29*SECSPERDAY + 12*SECSPERHOUR + 44*SECSPERMINUTE + 3;
  137.     long secdiff, secphase;
  138.     int atflrlen, atflridx, lin, col, midlin, qlitidx;
  139.     float angphase, mcap, yrad, xrad, y, xright, xleft;
  140.     int colright, colleft, i;
  141.     char c;
  142.  
  143.     static char background18[18][37] = {
  144.     "             .----------.            ",
  145.     "         .--'   o    .   `--.        ",
  146.     "       .'@  @@@@@@ O   .   . `.      ",
  147.     "     .'@@  @@@@@@@@   @@@@   . `.    ",
  148.     "   .'    . @@@@@@@@  @@@@@@    . `.  ",
  149.     "  / @@ o    @@@@@@.   @@@@    O   @\\ ",
  150.     "  |@@@@               @@@@@@     @@| ",
  151.     " / @@@@@   `.-.    . @@@@@@@@  .  @@\\",
  152.     " | @@@@   --`-'  .  o  @@@@@@@      |",
  153.     " |@ @@                 @@@@@@ @@@   |",
  154.     " \\      @@    @   . ()  @@   @@@@@  /",
  155.     "  |   @      @@@         @@@  @@@  | ",
  156.     "  \\  .   @@  @\\  .      .  @@    o / ",
  157.     "   `.   @@@@  _\\ /     .      o  .'  ",
  158.     "     `.  @@    ()---           .'    ",
  159.     "       `.     / |  .    o    .'      ",
  160.     "         `--./   .       .--'        ",
  161.     "             `----------'            "};
  162.  
  163.     static char background19[19][39] = {
  164.     "              .----------.             ",
  165.     "          .--'   o    .   `--.         ",
  166.     "       .-'@  @@@@@@ O   .   . `-.      ",
  167.     "     .' @@  @@@@@@@@   @@@@   .  `.    ",
  168.     "    /     . @@@@@@@@  @@@@@@     . \\   ",
  169.     "   /@@  o    @@@@@@.   @@@@    O   @\\  ",
  170.     "  /@@@@                @@@@@@     @@@\\ ",
  171.     " . @@@@@   `.-./    . @@@@@@@@  .  @@ .",
  172.     " | @@@@   --`-'  .      @@@@@@@       |",
  173.     " |@ @@        `      o  @@@@@@ @@@@   |",
  174.     " |      @@        o      @@   @@@@@@  |",
  175.     " ` .  @       @@     ()   @@@  @@@@   '",
  176.     "  \\     @@   @@@@        . @@   .  o / ",
  177.     "   \\   @@@@  @@\\  .           o     /  ",
  178.     "    \\ . @@     _\\ /    .      .-.  /   ",
  179.     "     `.    .    ()---        `-' .'    ",
  180.     "       `-.    ./ |  .   o     .-'      ",
  181.     "          `--./   .       .--'         ",
  182.     "              `----------'             "};
  183.  
  184.     static char background23[23][47] = {
  185.     "                 .------------.                ",
  186.     "             .--'  o     . .   `--.            ",
  187.     "          .-'   .    O   .       . `-.         ",
  188.     "       .-'@   @@@@@@@   .  @@@@@      `-.      ",
  189.     "      /@@@  @@@@@@@@@@@   @@@@@@@   .    \\     ",
  190.     "    ./    o @@@@@@@@@@@   @@@@@@@       . \\.   ",
  191.     "   /@@  o   @@@@@@@@@@@.   @@@@@@@   O      \\  ",
  192.     "  /@@@@   .   @@@@@@@o    @@@@@@@@@@     @@@ \\ ",
  193.     "  |@@@@@               . @@@@@@@@@@@@@ o @@@@| ",
  194.     " /@@@@@  O  `.-./  .      @@@@@@@@@@@@    @@  \\",
  195.     " | @@@@    --`-'       o     @@@@@@@@ @@@@    |",
  196.     " |@ @@@        `    o      .  @@   . @@@@@@@  |",
  197.     " |       @@  @         .-.     @@@   @@@@@@@  |",
  198.     " \\  . @        @@@     `-'   . @@@@   @@@@  o /",
  199.     "  |      @@   @@@@@ .           @@   .       | ",
  200.     "  \\     @@@@  @\\@@    /  .  O    .     o   . / ",
  201.     "   \\  o  @@     \\ \\  /         .    .       /  ",
  202.     "    `\\     .    .\\.-.___   .      .   .-. /'   ",
  203.     "      \\           `-'                `-' /     ",
  204.     "       `-.   o   / |     o    O   .   .-'      ",
  205.     "          `-.   /     .       .    .-'         ",
  206.     "             `--.       .      .--'            ",
  207.     "                 `------------'                "};
  208.     
  209.     static char background24[24][49] = {
  210.     "                  .------------.                 ",
  211.     "             .---' o     .  .   `---.            ",
  212.     "          .-'   .    O    .       .  `-.         ",
  213.     "        .'@   @@@@@@@   .   @@@@@       `.       ",
  214.     "      .'@@  @@@@@@@@@@@    @@@@@@@   .    `.     ",
  215.     "     /    o @@@@@@@@@@@    @@@@@@@       .  \\    ",
  216.     "    /@  o   @@@@@@@@@@@.    @@@@@@@   O      \\   ",
  217.     "   /@@@   .   @@@@@@@o     @@@@@@@@@@     @@@ \\  ",
  218.     "  /@@@@@               .  @@@@@@@@@@@@@ o @@@@ \\ ",
  219.     "  |@@@@  O  `.-./  .       @@@@@@@@@@@@    @@  | ",
  220.     " / @@@@    --`-'       o      @@@@@@@@ @@@@     \\",
  221.     " |@ @@@     @  `           .   @@     @@@@@@@   |",
  222.     " |      @           o          @      @@@@@@@   |",
  223.     " \\       @@            .-.      @@@    @@@@  o  /",
  224.     "  | . @        @@@     `-'    . @@@@           | ",
  225.     "  \\      @@   @@@@@ .            @@   .        / ",
  226.     "   \\    @@@@  @\\@@    /  .   O    .     o   . /  ",
  227.     "    \\ o  @@     \\ \\  /          .    .       /   ",
  228.     "     \\     .    .\\.-.___    .      .   .-.  /    ",
  229.     "      `.          `-'                 `-' .'     ",
  230.     "        `.   o   / |      o    O   .    .'       ",
  231.     "          `-.   /      .       .     .-'         ",
  232.     "             `---.       .      .---'            ",
  233.     "                  `------------'                 "};
  234.     static char background29[29][59] = {
  235.     "                      .--------------.                     ",
  236.     "                 .---'  o        .    `---.                ",
  237.     "              .-'    .    O  .         .   `-.             ",
  238.     "           .-'     @@@@@@       .             `-.          ",
  239.     "         .'@@   @@@@@@@@@@@       @@@@@@@   .    `.        ",
  240.     "       .'@@@  @@@@@@@@@@@@@@     @@@@@@@@@         `.      ",
  241.     "      /@@@  o @@@@@@@@@@@@@@     @@@@@@@@@     O     \\     ",
  242.     "     /        @@@@@@@@@@@@@@  @   @@@@@@@@@ @@     .  \\    ",
  243.     "    /@  o      @@@@@@@@@@@   .  @@  @@@@@@@@@@@     @@ \\   ",
  244.     "   /@@@      .   @@@@@@ o       @  @@@@@@@@@@@@@ o @@@@ \\  ",
  245.     "  /@@@@@                  @ .      @@@@@@@@@@@@@@  @@@@@ \\ ",
  246.     "  |@@@@@    O    `.-./  .        .  @@@@@@@@@@@@@   @@@  | ",
  247.     " / @@@@@        --`-'       o        @@@@@@@@@@@ @@@    . \\",
  248.     " |@ @@@@ .  @  @    `    @            @@      . @@@@@@    |",
  249.     " |   @@                         o    @@   .     @@@@@@    |",
  250.     " |  .     @   @ @       o              @@   o   @@@@@@.   |",
  251.     " \\     @    @       @       .-.       @@@@       @@@      /",
  252.     "  |  @    @  @              `-'     . @@@@     .    .    | ",
  253.     "  \\ .  o       @  @@@@  .              @@  .           . / ",
  254.     "   \\      @@@    @@@@@@       .                   o     /  ",
  255.     "    \\    @@@@@   @@\\@@    /        O          .        /   ",
  256.     "     \\ o  @@@       \\ \\  /  __        .   .     .--.  /    ",
  257.     "      \\      .     . \\.-.---                   `--'  /     ",
  258.     "       `.             `-'      .                   .'      ",
  259.     "         `.    o     / | `           O     .     .'        ",
  260.     "           `-.      /  |        o             .-'          ",
  261.     "              `-.          .         .     .-'             ",
  262.     "                 `---.        .       .---'                ",
  263.     "                      `--------------'                     "};
  264.  
  265.     static char qlits[8][16] = {
  266.     "New Moon +     ",
  267.     "First Quarter +",
  268.     "Full Moon +    ",
  269.     "Last Quarter + ",
  270.     "First Quarter -",
  271.     "Full Moon -    ",
  272.     "Last Quarter - ",
  273.     "New Moon -     " };
  274.  
  275.  
  276.     /* Find the length of the atfiller string. */
  277.     atflrlen = strlen( atfiller );
  278.  
  279.     /* Convert a new moon date from a string to a tws. */
  280.     twscopy( &twsanewmoon, dparsetime( "05jan81 23:24:00 PST" ) );
  281.  
  282.     /* Subtract the new moon date from the desired date to get the interval
  283.        since the new moon. */
  284.     secdiff = twsubtract( t, &twsanewmoon );
  285.  
  286.     /* Figure out the phase - the interval since the last new moon. */
  287.     secphase = secdiff % secsynodic;
  288.     if ( secphase < 0L )
  289.     secphase += secsynodic;  /* fucking mathematician language designers */
  290.     angphase = (float) secphase / (float) secsynodic * 2.0 * PI;
  291.     mcap = -cos( angphase );
  292.  
  293.     /* Figure out how big the moon is. */
  294.     yrad = numlines / 2.0;
  295.     xrad = yrad / ASPECTRATIO;
  296.  
  297.     /* Figure out some other random stuff. */
  298.     midlin = numlines / 2;
  299.     qlitidx = angphase / PI * 2.0;
  300.  
  301.     /* Now output the moon, a slice at a time. */
  302.     atflridx = 0;
  303.     for ( lin = 0; lin < numlines; lin = lin + 1 )
  304.     {
  305.     /* Compute the edges of this slice. */
  306.     y = lin + 0.5 - yrad;
  307.     xright = xrad * sqrt( 1.0 - ( y * y ) / ( yrad * yrad ) );
  308.     xleft = -xright;
  309.     if ( angphase >= 0.0 && angphase < PI )
  310.         xleft = mcap * xleft;
  311.     else
  312.         xright = mcap * xright;
  313.     colleft = (int) (xrad + 0.5) + (int) (xleft + 0.5);
  314.     colright = (int) (xrad + 0.5) + (int) (xright + 0.5);
  315.  
  316.     /* Now output the slice. */
  317.     for ( i = 0; i < colleft; i++ )
  318.         putchar( ' ' );
  319.     for ( col = colleft; col <= colright; col = col + 1 )
  320.         {
  321.         switch ( numlines )
  322.         {
  323.         case 18:
  324.             c = background18[lin][col];
  325.             break;
  326.         case 19:
  327.             c = background19[lin][col];
  328.             break;
  329.             case 23:
  330.             c = background23[lin][col];
  331.             break;
  332.             case 24:
  333.             c = background24[lin][col];
  334.             break;
  335.             case 29:
  336.             c = background29[lin][col];
  337.             break;
  338.         default:
  339.             c = '@';
  340.         }
  341.         if ( c != '@' )
  342.         putchar( c );
  343.         else
  344.         {
  345.         putchar( atfiller[atflridx] );
  346.         atflridx = ( atflridx + 1 ) % atflrlen;
  347.         }
  348.         }
  349.  
  350.     /* Output the end-of-line information, if any. */
  351.     if ( lin == midlin - 2 )
  352.         {
  353.         putchar( '\t' );
  354.         putchar( '\t' );
  355.         fputs( qlits[qlitidx], stdout );
  356.         }
  357.     else if ( lin == midlin - 1)
  358.         {
  359.         putchar( '\t' );
  360.         putchar( '\t' );
  361.         putseconds( secphase % (secsynodic / 4) );
  362.         }
  363.     else if ( lin == midlin )
  364.         {
  365.         putchar( '\t' );
  366.         putchar( '\t' );
  367.         fputs( qlits[qlitidx + 4], stdout );
  368.         }
  369.     else if ( lin == midlin + 1 )
  370.         {
  371.         putchar( '\t' );
  372.         putchar( '\t' );
  373.         putseconds( (secsynodic - secphase) % (secsynodic / 4) );
  374.         }
  375.  
  376.     putchar( '\n' );
  377.     }
  378.  
  379.     }
  380.  
  381.  
  382. putseconds( secs )
  383. long secs;
  384.     {
  385.     long days, hours, minutes;
  386.  
  387.     days = secs / SECSPERDAY;
  388.     secs = secs - days * SECSPERDAY;
  389.     hours = secs / SECSPERHOUR;
  390.     secs = secs - hours * SECSPERHOUR;
  391.     minutes = secs / SECSPERMINUTE;
  392.     secs = secs - minutes * SECSPERMINUTE;
  393.  
  394.     printf( "%ld %2ld:%02ld:%02ld", days, hours, minutes, secs );
  395.     }
  396.