1992/666.r

001/*           CROBOT              */
002 
003/*            666.r              */
004 
005/*      di Antonio Montese       */
006/*           13/09/92            */
007 
008 
009int corner, d, ssc, esc, dir1, dir2;
010 
011main()
012 
013{
014    int range, s, i;
015    while (1) {
016        i = 3;
017        newcorner();                      /* Cambia angolo nel campo di battaglia */
018        d = damage();
019        s = ssc;
020        while (!(d - damage()) && i)                         /* Ripete per 3 volte  */ {                                                 /* lo scan dei 90 gradi*/
021            range = scan(s, 3);                                 /* a meno che          */
022            while (!(d - damage()) && range && range <= 700)     /* non viene colpito.  */ {
023                cannon(s, range);                               /* Se trova un nemico  */
024                if (range = scan(dir1 + 3, 3))
025                    s = dir1 = dir1 + 8;       /* gli spara seguendo  */
026                    /* i suoi movimenti.   */
027                else if (range = scan(dir2 + 357, 3))
028                    s = dir2 = dir2 + 352;
029            }
030            s += 6;                                            /* Step dello SCAN     */
031            if (s > esc) {
032                s = ssc;
033                i -= 1;
034            }
035        }
036    }
037}
038 
039 
040newcorner()
041{
042    int xp, yp;
043    int rotazione, range;
044    int OK, oldcorner, vers, dirs, offs;
045    OK = 0;
046    oldcorner = corner;
047    vers = 60;
048    offs = 6;
049    while (!OK) {
050        corner = (corner + 3) % 4;        /* Sceglie l'angolo successivo          */
051        /* L'angolo scelto sara' accettato solo */
052        /* se nella posizione di destinazione   */
053        /* non ci sono nemici gia' appostati.   */
054        /* Cio' viene indicato dal flag OK      */
055        if ( corner == 0 ) {
056            yp = 12;                                /* Coordinate dell'angolo        */
057            xp = 12;
058            rotazione = angolo_rotazione(xp, yp);
059            ssc = 350;                             /* Angolo iniziale SCAN          */
060            esc = 460;                             /* Angolo finale SCAN            */
061            if (!(scan(rotazione, 6))) {
062                OK = 1;                               /* viene settato il flag OK      */
063                if ((oldcorner == 3) || (oldcorner == 2)) {
064                    vers = 300;                      /* A seconda dell'angolo di          */
065                    offs = 354;                      /* provrnienza, vengono aggiornati   */
066                }                               /* i parametri da usare nello SCAN   */
067                if (oldcorner == 2) {                               /* e la posizione di destinazione    */
068                    rotazione -= 7;                  /* nel caso di spostamento diagonale.*/
069                    yp += 200;                       /* Cio' assicura maggiore precisione */
070                    ssc = 260;                       /* e potenza di fuoco                */
071                }
072            }
073        }
074 
075 
076        if ( corner == 1 ) {
077            yp = 988;
078            xp = 12;
079            rotazione = angolo_rotazione(xp, yp);
080            ssc = 260;
081            esc = 370;
082            if (!(scan(rotazione, 6))) {
083                OK = 1;
084                if ((oldcorner == 0) || (oldcorner == 3)) {
085                    vers = 300;
086                    offs = 354;
087                }
088                if (oldcorner == 3) {
089                    rotazione -= 7;
090                    xp += 200;
091                    ssc = 175;
092                }
093            }
094        }
095 
096 
097        if ( corner == 2 ) {
098            yp = 988;
099            xp = 988;
100            rotazione = angolo_rotazione(xp, yp);
101            ssc = 170;
102            esc = 280;
103            if (!(scan(rotazione, 6))) {
104                OK = 1;
105                if ((oldcorner == 1) || (oldcorner == 0)) {
106                    vers = 300;
107                    offs = 354;
108                }
109                if (oldcorner == 0) {
110                    rotazione -= 7;
111                    yp -= 200;
112                    ssc = 80;
113                }
114            }
115        }
116 
117 
118        if ( corner == 3 ) {
119            yp = 12;
120            xp = 988;
121            rotazione = angolo_rotazione(xp, yp);
122            ssc = 80;
123            esc = 190;
124            if (!(scan(rotazione + 358, 6))) {
125                OK = 1;
126                if ((oldcorner == 2) || (oldcorner == 1)) {
127                    vers = 300;
128                    offs = 354;
129                }
130                if (oldcorner == 1) {
131                    rotazione -= 7;
132                    xp -= 200;
133                    ssc = 0;
134                }
135            }
136        }
137    }
138 
139    dirs = rotazione + vers;                /* Direzione dello SCAN.                */
140    drive(rotazione, 100);               /* Si muove alla massima velocita       */
141    dir1 = dir2 = dirs;                     /* verso l'angolo prescelto.            */
142 
143    while (distanza(loc_x(), loc_y(), xp, yp) > 100 && speed()) {                                                    /* Durante lo         */
144        range = scan(dirs, 2);                                 /* spostamento        */
145        if (range && range <= 700)
146            cannon(dirs + 359, range);  /* intercetta i nemici*/
147        if (range = scan(dir1 + offs, 3))
148            dirs = dir1 = dir1 + offs;   /* e li segue sparando*/
149        if (range = scan(dir2, 4))
150            dirs = dir2;
151    }
152 
153    drive(rotazione, 10);                                      /* Vicino al     */
154    while (distanza(loc_x(), loc_y(), xp, yp) > 13 && speed())
155        /* bordo rallenta*/
156    drive(rotazione, 0);                                       /* e poi si ferma*/
157}
158 
159 
160/* formula di pitagora */
161distanza(x1, y1, x2, y2)
162int x1;
163int y1;
164int x2;
165int y2;
166{
167    int x, y;
168 
169    x = x1 - x2;
170    y = y1 - y2;
171    d = sqrt((x * x) + (y * y));
172    return(d);
173}
174 
175 
176/* Calcola l'angolo di rotazione */
177 
178angolo_rotazione(xx, yy)
179int xx, yy;
180{
181    int d;
182    int x, y;
183    int scale;
184    int curx, cury;
185 
186    scale = 100000;                      /* Scala per funzioni trigonometriche */
187    curx = loc_x();                      /* Restituisce la posizione corrente  */
188    cury = loc_y();
189    x = curx - xx;
190    y = cury - yy;
191 
192    if (x == 0) {                          /* x = 0, allora va a Nord o a Sud  */
193        if (yy > cury)
194            d = 90;                            /* Nord                             */
195 
196        else
197            d = 270;                           /* Sud                              */
198    } else {
199        if (yy < cury) {
200            if (xx > curx)
201                d = 360 + atan((scale * y) / x);      /* Sud-Est, quadrante 4        */
202            else
203                d = 180 + atan((scale * y) / x);      /* Sud-Ovest, quadrante 3      */
204        } else {
205            if (xx > curx)
206                d = atan((scale * y) / x);            /* Nord-Est, quadrante 1       */
207            else
208                d = 180 + atan((scale * y) / x);      /* Nord-Ovest, quadrante 2     */
209        }
210    }
211    return (d);
212}