home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 101_01 / lines.c < prev    next >
Text File  |  1985-11-14  |  10KB  |  478 lines

  1.  
  2. /*
  3.     "Lines"
  4.  
  5.     written by:
  6.         Leor Zolman
  7.         173 Hampshire st. #2
  8.         Cambridge, Massachussetts 02139
  9.  
  10.     This program requires that a 64 x 16 memory mapped
  11.     video board be present in the system. For best
  12.     results, use a Processor Technology VDM-1 addressed
  13.     at CC00 hex.
  14.     The function"Line" will only work for such a memory
  15.     board; if you wish to run this program with a DMA board
  16.     organized other than as 64 x 16, you will have to write
  17.     your own line drawing program to work as follows:
  18.         line (char, x1, y1, x2, y2)
  19.     should draw a line from (x1,y1) to (x2,y2) consisting
  20.     of the character `char';
  21.         x's range from 0 to (# of rows)-1,
  22.         y's range from 0 to (# of columns)-1.
  23.  
  24.     It has been rumored that the use of psychoactive
  25.     chemicals and Tangerine Dream albums in conjunction
  26.     with this program can lead to irreparable brain
  27.     damage...for instance, one individual (who shall
  28.     go nameless), following sufficient exposure to the
  29.     aforementioned combination of sensory stimulants,
  30.     began to exhibit extremely anti-social attitudes
  31.     such as "Gee, after THIS, watching television seems
  32.     a waste of TIME!" and "Wow, I'd rather be writing
  33.     computer programs to do neat things like THIS
  34.     instead of letting my natural ability for creative
  35.     abstraction atrophy through overexposure to the
  36.     inanity of Prime-Time tunnelvision."
  37.         This poor individual has, needless to say,
  38.     now suffered a total emotional and social breakdown.
  39.     Even the THOUGHT of perfectly natural, socially
  40.     accepted TV viewing sends him into fits of depression;
  41.     he's become psychologically addicted to the manip-
  42.     ulation of numbers and other unreal abstractions
  43.     for the purpose of producing things like "compilers"
  44.     which he, due to his tortured and twisted psyche,
  45.     actually considers USEFUL. Poor guy; for him, the
  46.     REALITY of television was just too much to cope with.
  47.  
  48.     I have related this sobering account with the sadistic
  49.     hope that the same thing happens to YOU!!!!
  50.  
  51.     P.S.:
  52.      If only people would listen to Harlan Ellison...
  53. */
  54.  
  55.  
  56. #define VDMBASE 0xcc00
  57. #define ROWS 16
  58. #define COLUMNS 64
  59. #define MODULES 7
  60. #define ESC '\033'
  61. #define HOME 0x1a
  62. #define RANDROW (rand() >> 5) % ROWS
  63. #define RANDCOL (rand() >> 6) % COLUMNS
  64.  
  65. char speed, density, setcnt, erasmod, bkround;
  66. char frontp, nactive, dchar, charcd, charmode;
  67.  
  68. struct module {
  69.     char freq;
  70.     int nfactor;
  71.     char active;
  72.  } mtab[MODULES];
  73.  
  74. char activet[MODULES];
  75. char *modnames[MODULES];
  76. int adj[16];
  77.  
  78. int flag;
  79. main()
  80. {
  81.     char cmod, i, j, k, l, c;
  82.     char x, y, x1, y1, x2, y2;
  83.     char count;
  84.     int limit, d;
  85.  
  86.     init();
  87.     loop:
  88.     if (nactive)
  89.      while (!kbhit()) {
  90.         if(frontp) {
  91.             speed = csw() >> 4;
  92.             density = csw() & 15;
  93.          }
  94.         if(erasmod == 1) clear();
  95.         do
  96.             cmod = rand() % nactive;
  97.          while (mtab[activet[cmod]].freq > rand()&15);
  98.  
  99.         limit = rand() % setcnt;
  100.         for (i=0; i < limit; i++) {
  101.          if (kbhit()) break;
  102.          switch (erasmod) {
  103.            case 2: clear(); break;
  104.            case 3: if(!(rand()%(speed+speed)))clear();
  105.          }
  106.          outp(255, ~activet[cmod]);
  107.  
  108.         switch(activet[cmod]) {
  109.          case 0:    /* lines */
  110.           count = rand()%(density*5);
  111.           do {
  112.             figure();
  113.             dline(0, RANDROW,
  114.                  RANDCOL,
  115.                 (rand(), RANDROW),
  116.                  RANDCOL);
  117.            } while (count--);
  118.         break;
  119.  
  120.         case 1:        /* Connected Random lines */
  121.           count = rand()%(density*5);
  122.           x = RANDROW;
  123.           y = RANDCOL;
  124.           do {
  125.             figure();
  126.             x1 = RANDROW;
  127.             y1 = RANDCOL;
  128.             dline (0,x,y,x1,y1);
  129.             x = x1;    
  130.             y = y1;
  131.            }  while (count--);
  132.         break;
  133.  
  134.  
  135.         case 2: /* rectangles */
  136.           count = rand() % (density<<3);
  137.           do {
  138.             figure();
  139.             x1 = RANDROW;
  140.             y1 = RANDCOL;    
  141.             x2 = RANDROW;
  142.             y2 = RANDCOL;
  143.             dline (0, x1, y1, x1, y2);
  144.             dline (0, x2, y1, x2, y2);
  145.             dline (0, x1, y1, x2, y1);
  146.             dline (0, x1, y2, x2, y2);
  147.            }  while (count--);
  148.         break;
  149.  
  150.  
  151.         case 3:    /* triangles */
  152.           count = rand() % (density<<2);
  153.           do {
  154.             figure();
  155.             x = rand()%ROWS;
  156.             x1 = RANDROW;
  157.             x2 = RANDROW;
  158.             y = RANDCOL;
  159.             y1 = RANDCOL;
  160.             y2 = RANDCOL;
  161.             dline (0, x, y, x1, y1);    
  162.             dline (0,x1,y1,x2,y2);
  163.             dline (0,x2,y2,x,y);
  164.          } while (count--);    
  165.         break;
  166.  
  167.         case 4:    /* ECKS IS */
  168.           count = rand() % (density<<3);
  169.           do {
  170.             figure();
  171.             x = RANDROW;
  172.             x1 = RANDROW;
  173.             y = RANDCOL;
  174.  
  175.             y1 = RANDCOL;
  176.             dline (0,x,y,x1,y1);
  177.             dline (0,x,y1,x1,y);
  178.            } while (count--);
  179.           break;
  180.  
  181.         case 5:    /* vertices */
  182.             count = rand() % density;
  183.            do {
  184.             figure();
  185.             x = RANDROW;
  186.             y = RANDCOL;
  187.             while (rand()&15)
  188.               dline(0,x,y,RANDROW,RANDCOL);
  189.             } while (count--);
  190.             break;
  191.  
  192.         case 6:        /* dart */
  193.             x1 = RANDROW;
  194.             y1 = RANDCOL;
  195.             count = rand() % (density<<2);
  196.             do {    
  197.               d = rand() % 8;
  198.               do {
  199.                 x = x1;
  200.                 y = y1;
  201.                 plot (x,y,rand());
  202.                 x1 += adj[d+d];
  203.                 y1 += adj[d+d+1];
  204.             } while (x1<16 && y1<64);
  205.               x1=x; y1=y;
  206.             } while (count--);
  207.             break;
  208.         
  209.  
  210.         }
  211.        }
  212.     }
  213.     c = getchar();
  214.     flag = 1;
  215.     while (1) {
  216.     putchar(HOME);
  217.     if (!flag) {
  218.         commands();
  219.         printf("\nCommand: ");
  220.      }
  221.     c = flag? c : getchar();
  222.     flag = 0;
  223.       switch (c) {
  224.         case '\n':
  225.           rand(); rand(); rand(); clear(); goto loop;
  226.  
  227.         case 's':
  228.           speed = gethd(
  229.         "new speed factor (0=slow ... F=fast): ");
  230.            break;
  231.  
  232.         case 'd':
  233.           density = gethd(
  234.         "new density factor (0=sparse ... F=dense): ");
  235.            break;
  236.  
  237.         case 'n':
  238.           setcnt = gethd(
  239.         "new maximum set size (0 - F): ");
  240.            break;
  241.  
  242.         case 'f':
  243.           frontp = 1;
  244.           printf("\nOK; the high order 4 input switches");
  245.           printf(" at port 255 now control\n");
  246.           printf(" SPEED, and the low order 4 bits");
  247.           printf(" control DENSITY.\n");
  248.           printf("Type CR to continue...");
  249.           getchar();
  250.           break;
  251.  
  252.         case 'k':
  253.           frontp = 0; break;
  254.  
  255.         case 'q':
  256.           return;
  257.  
  258.         case 'b':
  259.           printf("\nEnter new backround character \
  260. (or ESCAPE for inversion): ");
  261.           if ((bkround = getchar() ) == ESC ) {
  262.             printf("\nOK, now type the actual char: ");
  263.             bkround = getchar() | 0x80;
  264.            }
  265.         break;
  266.  
  267.         case 'r':
  268.           display();
  269.           if ( !(c = getmod() ) || !mtab[c-1].active)
  270.             break;
  271.           mtab[c-1].active = 0;
  272.           --nactive;
  273.           compile();
  274.           break;
  275.  
  276.         case 'm':
  277.           display();
  278.           if ( !(c = getmod() )) break;
  279.           mtab[c-1].freq = gethd(
  280.             "Frequency factor (0 - F): ");
  281.           mtab[c-1].active = 1;
  282.           ++nactive;
  283.           compile();
  284.           break;
  285.  
  286.         case 'e':
  287.           putchar('\n');
  288.           printf("0 = never erase        1 = erase \
  289. on module entry\n");
  290.           printf("2 = at start of sets   \
  291. 3 = randomly\n");
  292.           do erasmod = gethd(
  293.         "Enter new erase mode (0 - 3): ");
  294.             while (erasmod > 3);
  295.            break;
  296.  
  297.         case ' ':
  298.           goto loop;
  299.  
  300.         case 'c':
  301.           printf("\n0 - fixed character           1 - random char per line\n");
  302.           printf("2 - rand char per figure      3 - randomness\n");
  303.           do charmode = gethd("Enter character choosing mode (0-3): ");
  304.             while (charmode > 3);
  305.           if (!charmode) {
  306.            printf("\nType the character (or ESCAPE for inversion): ");
  307.            if ( (dchar = getchar()) == ESC) {
  308.             printf("\nType the actual character: ");
  309.             dchar = getchar() | 0x80;
  310.             }
  311.            break;
  312.            }
  313.           if (charmode == 3) charcd = rand() % 100;
  314.           break;
  315.  
  316.         }
  317.     }
  318. }
  319.  
  320.  
  321. clear()
  322. {
  323.     setmem(VDMBASE, ROWS*COLUMNS, bkround);
  324.     outp ( 0xc8, 0);
  325. }
  326.  
  327.  
  328. compile()
  329. {
  330.     char slot; int i;
  331.     slot = 0;
  332.     for (i=0; i<MODULES; i++)
  333.      if (mtab[i].active) activet[slot++] = i;
  334. }
  335.  
  336.  
  337. gethd(s)
  338. char *s;
  339. {
  340.     char c;
  341.    foo:
  342.     putchar('\n');
  343.     puts(s);
  344.     while ((c = getch() ) == ' ' || c=='\t');
  345.     if (c >= '0' && c <= '9') return c - '0';
  346.     if (c>='a' && c <= 'f') return c - 87;
  347.     goto foo;
  348. }
  349.  
  350. getch()
  351. {
  352.     char c;
  353.     c = getchar();
  354.     if (c >= 'A' && c <= 'Z') return c+32;
  355.     return c;
  356. }
  357.  
  358.  
  359. getmod()
  360. {
  361.     char c;
  362.     printf("\nEnter module letter (a - %c): ",
  363.         MODULES+'a'-1);
  364.     while (( c = getch() ) == ' ' || c=='\t');
  365.     if (c >= 'a' && c <= MODULES+'a'-1) return c-'a'+1;
  366.     return 0;
  367. }
  368.  
  369.  
  370. display()
  371. {
  372.     int i,j,k;
  373.     clrplot();    
  374.     putchar('\n');
  375.     for (i=0; i<MODULES; i++) {
  376.         printf("%c: %c  %s", i+'A',
  377.          mtab[i].active ? hd(mtab[i].freq) : ' ',
  378.          modnames[i]);
  379.  
  380.         for (j=strlen(modnames[i])+7; j<32; j++)
  381.             putchar(' ');
  382.         if (i&1) putchar('\n');
  383.         }
  384.     putchar('\n');
  385. }
  386.  
  387.  
  388. hd(n)
  389. {
  390.     if (n>=0 && n<=9) return n+'0';
  391.     return n+87;
  392. }
  393.  
  394.  
  395. figure()
  396. {
  397.     int delay;
  398.     if (charmode==2) dchar = rand();
  399.     if (15-speed) for (delay=0; delay<((15-speed)<<5);
  400.                 delay++);
  401. }
  402.  
  403.  
  404. dline(c)
  405. char c;
  406. {
  407.     if (charmode==1) dchar= rand();
  408.     else if (charmode == 3) if (!charcd--) {
  409.                   dchar =
  410.                    (rand()%4==1) ? bkround
  411.                      : rand();
  412.                   charcd = rand() % 100;
  413.                 }
  414.     line(dchar);
  415. }
  416.  
  417.  
  418. init()
  419. {
  420.     int i;
  421.     initw(adj,"-1,-1,-1,0,0,1,1,1,-1,0,1,-1,1,-1,0,1");
  422.     speed = 15;
  423.     density = 12;
  424.     setcnt = 15;
  425.     erasmod = 3;
  426.     bkround = ' ';
  427.     frontp = 0;
  428.     nactive = 5;
  429.     dchar = rand();
  430.     charmode = 3;
  431.     mtab[0].freq = 5;
  432.     mtab[0].active = 1;
  433.     mtab[1].active = 1;
  434.     mtab[1].freq = 5;
  435.     mtab[2].active = 1;
  436.     mtab[2].freq = 2;
  437.     mtab[3].active = 1;
  438.     mtab[3].freq = 4;
  439.     mtab[4].active = 1;
  440.     mtab[4].freq = 3;
  441.     modnames[0] ="Random Lines";
  442.     modnames[1] ="Connected Random Lines";
  443.     modnames[2] ="Rectangles";
  444.     modnames[3] ="Triangles";
  445.     modnames[4] ="Ecks Is";
  446.     modnames[5] ="Vertices";
  447.     modnames[6] ="Dart";
  448.     mtab[0].nfactor=3;
  449.     mtab[1].nfactor=3;
  450.     mtab[2].nfactor = 1;
  451.     mtab[3].nfactor = 1;
  452.     mtab[4].nfactor = 2;
  453.     mtab[5].nfactor = 1;
  454.     mtab[6].nfactor = 1;
  455.     setplot(VDMBASE,ROWS,COLUMNS);
  456.     srand(0);
  457.     for (i=5; i<MODULES; i++) mtab[i].active = 0;
  458.     compile();
  459.     clear();
  460. }
  461.  
  462. commands()
  463. {
  464.     printf("LINES commands:\n\n");
  465.     printf("CR:    start LINES\n");
  466.     printf("s:    set speed from console\n");
  467.     printf("d:    set density from console\n");
  468.     printf("n:    set set-duration\n");
  469.     printf("f:    get speed and density from front panel\n");
  470.     printf("k:    get speed & density from console\n");
  471.     printf("b:    set backround character\n");
  472.     printf("e:    set erase mode\n");
  473.     printf("m:    turn modules ON\n");
  474.     printf("r:    turn modules OFF\n");
  475.     printf("c:    set character mode\n");
  476.     printf("q:    quit\n");
  477. }
  478.