home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / logo / part03 / turtle.c < prev   
Encoding:
C/C++ Source or Header  |  1987-06-23  |  9.6 KB  |  603 lines

  1.  
  2. #include "logo.h"
  3.  
  4. #ifndef NOTURTLE
  5.  
  6. #include <math.h>
  7.  
  8. extern char *getenv();
  9. int turtdes; /* file descriptor for open turtle */
  10. int color;    /* pen color */
  11. int pendown = 0; /* nonzero with pen down */
  12. int penerase = 0; /* 0=pd, 1=pe, 2=px, pendown must be nonzero */
  13. int shown = 1;    /* nonzero if turtle is visible */
  14. int textmode = 0;    /* not turtle off */
  15. NUMBER yscrunch;    /* scale factor for y */
  16. struct display *mydpy;
  17.  
  18. #ifdef ATARI
  19. #include "atari.i"
  20. #endif
  21.  
  22. #ifdef GIGI
  23. #include "gigi.i"
  24. #endif
  25.  
  26. #ifdef ADM
  27. #include "admtek.i"
  28. #include "adm.i"
  29. #endif
  30.  
  31. #ifdef TEK
  32. #ifndef ADM
  33. #include "admtek.i"
  34. #endif
  35. #include "tek.i"
  36. #endif
  37.  
  38. #ifdef SUN
  39. #include "sun.i"
  40. #endif
  41.  
  42. NUMBER ncheck(arg)
  43. struct object *arg;
  44. {
  45.     NUMBER val;
  46.  
  47.     arg = numconv(arg,"Turtle command");
  48.     arg = dubconv(arg);
  49.     val = arg->obdub;
  50.     mfree(arg);
  51.     return(val);
  52. }
  53.  
  54. dpyinit() {
  55.     char *ttytype;
  56.  
  57.     ttytype = getenv("TERM");
  58. #ifdef GIGI
  59.     if (!strcmp(ttytype,"gigi"))
  60.         mydpy = &gigi;
  61.     else
  62. #endif
  63. #ifdef ATARI
  64.     if (!strcmp(ttytype,"atari"))
  65.         mydpy = &bwatari;
  66.     else
  67. #endif
  68. #ifdef ADM
  69.     if (!strncmp(ttytype,"adm",3))
  70.         mydpy = &adm;
  71.     else
  72. #endif
  73. #ifdef TEK
  74.     if (!strncmp(ttytype,"tek",3))
  75.         mydpy = &tek;
  76.     else
  77. #endif
  78. #ifdef SUN
  79.     if (1 || !strcmp(ttytype,"sun"))    /* Sun is always a sun */
  80.         mydpy = &sun;
  81.     else
  82. #endif
  83.     {
  84.         printf("I don't recognize your terminal type!\n");
  85.         errhand();
  86.     }
  87.     pendown = 1; penerase = 0; shown = 1;
  88.     textmode = 0;
  89.     mydpy->turtx = mydpy->turty = mydpy->turth = 0.0;
  90.     printf(mydpy->init);
  91.     if (!(mydpy->cleared)) {
  92.         printf(mydpy->clear);
  93.         (*mydpy->state)('c');
  94.         mydpy->cleared++;
  95.         yscrunch = mydpy->stdscrunch;
  96.     }
  97.     turtdes = -1;
  98.     (*mydpy->infn)();
  99.     (*mydpy->drawturt)(0);
  100. }
  101.  
  102. struct object *getturtle(arg)
  103. register struct object *arg;
  104. {
  105.     int lsflag[2];    /* BH 1/4/81 */
  106.     register char *temp,*argc;
  107.     char c[100];
  108.     char astr[20];
  109.  
  110.     if (stringp(arg)) argc = arg->obstr;
  111.     else argc = "";
  112.     if (!strcmp(argc,"off")) {
  113. #ifdef FLOOR
  114.         if (turtdes>0) {
  115.             close (turtdes);
  116.             printf("Please\007 unplug the turtle\007 and put it\007 away!\n");
  117.         }
  118. #endif /* FLOOR */
  119.         if (turtdes<0) {
  120.             printf(mydpy->finish);
  121.             (*mydpy->outfn)();
  122.         }
  123.         turtdes = 0;
  124.         mfree(arg);
  125.         return((struct object *)(-1));
  126.     }
  127.     if (!strcmp(argc,"dpy")||!strcmp(argc,"display")) {
  128.  
  129. #ifdef FLOOR
  130.         if (turtdes>0) {
  131.             close (turtdes);
  132.             printf("Please\007 unplug the turtle\007 and put it\007 away!\n");
  133.         }
  134. #endif /* FLOOR */
  135.  
  136.         dpyinit();
  137.         mfree(arg);
  138.         return ((struct object *)(-1));
  139.     }
  140. #ifdef FLOOR
  141.     if (intp(arg)) {
  142.         sprintf(astr,FIXFMT,arg->obint);
  143.         argc = astr;
  144.     }
  145.     temp = c;
  146.     cpystr(temp,"/dev/turtle",argc,NULL);
  147.     if (turtdes>0) close(turtdes);
  148.     if((turtdes = open(c,2)) < 0) {
  149.         turtdes = 0;
  150.         pf1("Turtle %l not available.\n",arg);
  151.     } else printf("Please put the turtle away when you're done!\n");
  152.     mfree(arg);
  153.     return ((struct object *)(-1));
  154. #else
  155.     ungood("Turtle",arg);
  156. #endif /* FLOOR */
  157. }
  158.  
  159. dpysxy(newx,newy)
  160. NUMBER newx,newy;
  161. {
  162.     if ((newx < mydpy->xlow) || (newx > mydpy->xhigh) ||
  163.         (newy < mydpy->ylow) || (newy > mydpy->yhigh)) {
  164.             puts("Out of bounds!");
  165.             errhand();
  166.     }
  167.     if (shown) (*mydpy->drawturt)(1);
  168.     if (fabs(newx) < 0.01) newx = 0.0;
  169.     if (fabs(newy) < 0.01) newy = 0.0;
  170.     if (pendown)
  171.         (*mydpy->drawfrom)(mydpy->turtx,yscrunch*mydpy->turty);
  172.     mydpy->turtx = newx;
  173.     mydpy->turty = newy;
  174.     if (pendown)
  175.         (*mydpy->drawto)(newx,yscrunch*newy);
  176.     (*mydpy->state)('G');
  177.     if (shown) (*mydpy->drawturt)(0);
  178. }
  179.  
  180. dpyforw(dist)
  181. NUMBER dist;
  182. {
  183.     NUMBER newx,newy,deltax,deltay;
  184.  
  185.     tcheck();
  186.     (*mydpy->txtchk)();
  187.     deltax = dist * sin((mydpy->turth)*3.141592654/180.0);
  188.     if (fabs(deltax) < 1.0e-5) deltax = 0.0;
  189.     deltay = dist * cos((mydpy->turth)*3.141592654/180.0);
  190.     if (fabs(deltay) < 1.0e-5) deltay = 0.0;
  191.     newx = mydpy->turtx + deltax;
  192.     newy = mydpy->turty + deltay;
  193.     dpysxy(newx,newy);
  194. }
  195.  
  196. struct object *forward(arg)
  197. register struct object *arg;
  198. {
  199.     NUMBER dist;
  200.  
  201.     dist = ncheck(arg);
  202. #ifdef FLOOR
  203.     if (turtdes > 0) {
  204.         if (dist < 0.0)
  205.             moveturtle('b',-6*(int)dist);
  206.         else
  207.             moveturtle('f',6*(int)dist);
  208.         return ((struct object *)(-1));
  209.     }
  210. #endif /* FLOOR */
  211.     dpyforw(dist);
  212.     return ((struct object *)(-1));
  213. }
  214.  
  215. struct object *back(arg)
  216. register struct object *arg;
  217. {
  218.     NUMBER dist;
  219.  
  220.     dist = ncheck(arg);
  221. #ifdef FLOOR
  222.     if (turtdes > 0) {
  223.         if (dist < 0.0)
  224.             moveturtle('f',-6*(int)dist);
  225.         else
  226.             moveturtle('b',6*(int)dist);
  227.         return ((struct object *)(-1));
  228.     }
  229. #endif /* FLOOR */
  230.     dpyforw(-dist);
  231.     return ((struct object *)(-1));
  232. }
  233.  
  234. dpysh(angle)
  235. NUMBER angle;
  236. {
  237.     (*mydpy->txtchk)();
  238.     if (shown) (*mydpy->drawturt)(1);
  239.     mydpy->turth = angle;
  240.     while (mydpy->turth+11.0 < 0.0) mydpy->turth += 360.0;
  241.     while (mydpy->turth+11.0 >= 360.0) mydpy->turth -= 360.0;
  242.     if (shown) (*mydpy->drawturt)(0);
  243.     (*mydpy->turnturt)();
  244. }
  245.  
  246. dpyturn(angle)
  247. NUMBER angle;
  248. {
  249.     tcheck();
  250.     dpysh(mydpy->turth + angle);
  251. }
  252.  
  253. struct object *left(arg)
  254. register struct object *arg;
  255. {
  256.     NUMBER dist;
  257.  
  258.     dist = ncheck(arg);
  259. #ifdef FLOOR
  260.     if (turtdes > 0) {
  261.         if (dist < 0.0)
  262.             moveturtle('r',(-2*(int)dist)/5);
  263.         else
  264.             moveturtle('l',(2*(int)dist)/5);
  265.         return ((struct object *)(-1));
  266.     }
  267. #endif /* FLOOR */
  268.     dpyturn(-dist);
  269.     return ((struct object *)(-1));
  270. }
  271.  
  272. struct object *right(arg)
  273. register struct object *arg;
  274. {
  275.     NUMBER dist;
  276.  
  277.     dist = ncheck(arg);
  278. #ifdef FLOOR
  279.     if (turtdes > 0) {
  280.         if (dist < 0.0)
  281.             moveturtle('l',(-2*(int)dist)/5);
  282.         else
  283.             moveturtle('r',(2*(int)dist)/5);
  284.         return ((struct object *)(-1));
  285.     }
  286. #endif /* FLOOR */
  287.     dpyturn(dist);
  288.     return ((struct object *)(-1));
  289. }
  290.  
  291. #ifdef FLOOR
  292. fcheck() {
  293.     if (turtdes <= 0) {
  294.         puts("You don't have a floor turtle!");
  295.         errhand();
  296.     }
  297. }
  298.  
  299. struct object *hitoot(arg)
  300. register struct object *arg;
  301. {
  302.     NUMBER dist;
  303.  
  304.     fcheck();
  305.     dist = ncheck(arg);
  306.     moveturtle('H',(15*(int)dist)/2);
  307.     return ((struct object *)(-1));
  308. }
  309.  
  310. struct object *lotoot(arg)
  311. register struct object *arg;
  312. {
  313.     NUMBER dist;
  314.  
  315.     fcheck();
  316.     dist = ncheck(arg);
  317.     moveturtle('L',(15*(int)dist)/2);
  318.     return ((struct object *)(-1));
  319. }
  320.  
  321. moveturtle(where,arg)
  322. register int arg;
  323. {
  324.     char buff[2];
  325.  
  326.     buff[0] = where;
  327.     while (arg >= 0400) {
  328.         buff[1] = 0377;
  329.         write(turtdes,buff,2);
  330.         arg -= 0377;
  331.     }
  332.     buff[1] = arg;
  333.     write(turtdes,buff,2);
  334. }
  335.  
  336. lampon() {
  337.     int i;
  338.  
  339.     fcheck();
  340.     i = 'B';
  341.     write(turtdes,&i,2);
  342. }
  343.  
  344. lampoff() {
  345.     int i;
  346.  
  347.     fcheck();
  348.     i = 'B'+0400;
  349.     write(turtdes,&i,2);
  350. }
  351.  
  352. struct object *touchsense(which)
  353. {
  354.     char x;
  355.  
  356.     fcheck();
  357.     read (turtdes,&x,1);
  358.     if ( (0200>>which) & x) return (true());
  359.     else return (false());
  360. }
  361.  
  362. struct object *ftouch() {
  363.     return(touchsense(0));
  364. }
  365.  
  366. struct object *btouch() {
  367.     return(touchsense(1));
  368. }
  369.  
  370. struct object *ltouch() {
  371.     return(touchsense(2));
  372. }
  373.  
  374. struct object *rtouch() {
  375.     return(touchsense(3));
  376. }
  377. #endif
  378.  
  379. int tcheck() {
  380.     if (turtdes > 0) {
  381.         puts("You don't have a display turtle!");
  382.         errhand();
  383.     }
  384.     if (turtdes == 0) dpyinit();    /* free turtle "display */
  385. }
  386.  
  387. NUMBER posangle(angle)
  388. NUMBER angle;
  389. {
  390.     if (angle < 0.0) return(angle+360.0);
  391.     return(angle);
  392. }
  393.  
  394. struct object *pencolor(pen)
  395. struct object *pen;
  396. {
  397.     NUMBER dpen;
  398.  
  399.     tcheck();
  400.     (*mydpy->txtchk)();
  401.     dpen = ncheck(pen);
  402.     (*mydpy->penc)((int)dpen);
  403.     color = dpen;
  404.     return ((struct object *)(-1));
  405. }
  406.  
  407. int setcolor(pen,colorlist)
  408. struct object *pen,*colorlist;
  409. {
  410.     NUMBER number;
  411.     register int ipen;
  412.  
  413.     tcheck();
  414.     (*mydpy->txtchk)();
  415.     number = ncheck(pen);
  416.     ipen = number;
  417.     (*mydpy->setc)(ipen,colorlist);
  418. }
  419.  
  420. int setxy(strx,stry)
  421. struct object *strx,*stry;
  422. {
  423.     NUMBER x,y;
  424.  
  425.     tcheck();
  426.     (*mydpy->txtchk)();
  427.     x = ncheck(strx);
  428.     y = ncheck(stry);
  429.     dpysxy(x,y);
  430. }
  431.  
  432. struct object *setheading(arg)
  433. struct object *arg;
  434. {
  435.     NUMBER heading;
  436.  
  437.     tcheck();
  438.     (*mydpy->txtchk)();
  439.     heading = ncheck(arg);
  440.     dpysh(heading);
  441.     return ((struct object *)(-1));
  442. }
  443.  
  444. struct object *xcor()
  445. {
  446.     tcheck();
  447.     return(localize(objdub(mydpy->turtx)));
  448. }
  449.  
  450. struct object *ycor()
  451. {
  452.     tcheck();
  453.     return(localize(objdub(mydpy->turty)));
  454. }
  455.  
  456. struct object *heading()
  457. {
  458.     tcheck();
  459.     return(localize(objdub(posangle(mydpy->turth))));
  460. }
  461.  
  462. struct object *getpen()
  463. {
  464.     tcheck();
  465.     return(localize(objint(color)));
  466. }
  467.  
  468. struct object *setscrunch(new)
  469. struct object *new;
  470. {
  471.     tcheck();
  472.     yscrunch = ncheck(new);
  473.     return ((struct object *)(-1));
  474. }
  475.  
  476. struct object *scrunch() {
  477.     tcheck();
  478.     return(localize(objdub(yscrunch)));
  479. }
  480.  
  481. penup() {
  482. #ifdef FLOOR
  483.     int i;
  484.  
  485.     if (turtdes>0) {
  486.         i = 'P'+0400;
  487.         write(turtdes,&i,2);
  488.         return;
  489.     }
  490. #endif FLOOR
  491.     tcheck();
  492.     pendown = 0;
  493.     (*mydpy->state)('U');
  494. }
  495.  
  496. cmpendown() {
  497. #ifdef FLOOR
  498.     int i;
  499.  
  500.     if (turtdes>0) {
  501.         i = 'P';
  502.         write(turtdes,&i,2);
  503.         return;
  504.     }
  505. #endif FLOOR
  506.     tcheck();
  507.     pendown = 1;
  508.     penerase = 0;
  509.     (*mydpy->state)('D');
  510. }
  511.  
  512. cmpenerase() {
  513.     tcheck();
  514.     pendown = penerase = 1;
  515.     (*mydpy->state)('E');
  516. }
  517.  
  518. penreverse() {
  519.     tcheck();
  520.     pendown = 1;
  521.     penerase = 2;
  522.     (*mydpy->state)('R');
  523. }
  524.  
  525. clearscreen() {
  526.     tcheck();
  527.     (*mydpy->txtchk)();
  528.     printf(mydpy->clear);
  529.     mydpy->turtx = mydpy->turty = mydpy->turth = 0.0;
  530.     (*mydpy->state)('c');
  531.     if (shown) (*mydpy->drawturt)(0);
  532. }
  533.  
  534. wipeclean() {
  535.     tcheck();
  536.     (*mydpy->txtchk)();
  537.     printf(mydpy->clear);
  538.     (*mydpy->state)('w');
  539.     if (shown) (*mydpy->drawturt)(0);
  540. }
  541.  
  542. fullscreen() {
  543.     tcheck();
  544.     (*mydpy->state)('f');
  545.     textmode = 0;
  546. }
  547.  
  548. splitscreen() {
  549.     tcheck();
  550.     (*mydpy->state)('s');
  551.     textmode = 0;
  552. }
  553.  
  554. textscreen() {
  555.     tcheck();
  556.     (*mydpy->state)('t');
  557.     textmode++;
  558. }
  559.  
  560. showturtle() {
  561.     tcheck();
  562.     (*mydpy->txtchk)();
  563.     if (!shown) (*mydpy->drawturt)(0);
  564.     shown = 1;
  565.     (*mydpy->state)('S');
  566. }
  567.  
  568. hideturtle() {
  569.     tcheck();
  570.     (*mydpy->txtchk)();
  571.     if (shown) (*mydpy->drawturt)(1);
  572.     shown = 0;
  573.     (*mydpy->state)('H');
  574. }
  575.  
  576. struct object *penmode() {
  577.     static char *pens[] = {"pendown","penerase","penreverse"};
  578.  
  579.     tcheck();
  580.     if (pendown) return(localize(objcpstr(pens[penerase])));
  581.     return(localize(objcpstr("penup")));
  582. }
  583.  
  584. struct object *shownp() {
  585.     tcheck();
  586.     return(torf(shown));
  587. }
  588.  
  589. struct object *towardsxy(x,y)
  590. struct object *x,*y;
  591. {
  592.     NUMBER dx,dy;
  593.  
  594.     tcheck();
  595.     dx = ncheck(x);
  596.     dy = ncheck(y);
  597.     return(localize(objdub(posangle((double)180.0*
  598.         atan2(dx-(mydpy->turtx),dy-(mydpy->turty))/3.141592654))));
  599. }
  600.  
  601. #endif
  602.  
  603.