1992/random.r

001/*
002 
003   ROBOT         :Random.R
004    
005   PROGRAMMATORE :Mattia Rossi
006*/
007 
008 
009 
010 
011/* Il robot ha un comportamento casuale al massimo: si muove a caso per tutto
012   il campo di battaglia cercando bersagli da colpire.
013   La routine di fuoco e' minimizzata al massimo per permettere un alto
014   volume di fuoco                                                           */
015 
016 
017int dir, direz  ;
018 
019 
020main()
021{
022    dir = 0;
023 
024    while ( 1 ) {
025 
026 
027        direz = plot_course ( rand(1000) , rand(1000) ); /* sceglie una direzione
028                                                                    a caso               */
029 
030        drive ( direz , 100 );                            /* vi si dirige alla
031                                                                    massima velocita'    */
032 
033 
034        while (loc_x() > 150 && loc_y() > 150 && loc_y() < 850 && loc_x() < 850 ) {
035 
036 
037            drive (direz, 100);
038            cerca()          ;                       /* Routine di ricerca e
039                                                                    di fuoco            */
040 
041        }
042 
043        drive(direz, 50);                                 /* Quando e' vicino al
044                                    margine riduce la sua
045                                    velocita' per poter
046                                            girare              */
047 
048    }
049 
050}
051 
052 
053 
054 
055 
056 
057/* La routine di ricerca e' un semplice scan circolare del campo di battaglia
058   Viene eseguita un numero finito di volte e poi sospesa per poter controlla_
059   re la posizione del robot ed evitare che vada a sbattere                  */
060 
061 
062 
063 
064cerca()
065{
066    int range , cycle , cycle1;
067 
068    cycle = 0;
069 
070 
071    while (cycle < 2 ) {
072 
073 
074        cycle1 = 0;
075 
076 
077        /* Cerca un Robor, Angolo di 10 gradi */
078 
079 
080        if (range = scan(dir, 10)) {
081 
082 
083            cannon(dir + 3 , 7 * range / 8 ); /* spara! ( Jazz insegna )*/
084 
085            dir  -= 3 ;
086 
087 
088            /* Esegue uno scan pi� preciso */
089 
090 
091            while (cycle1 < 10 ) {
092 
093 
094                range = scan(dir , 3);   /* cerca in una direzione, angolo di 10 gradi */
095 
096                if (range > 70 )         /* piu' vicino mi faccio male anch'io         */ {
097 
098 
099                    cannon(dir , 7 * range / 8 ); /* spara! */
100 
101                    dir  -= 8 ;
102 
103 
104                }
105 
106                dir += 5 ;
107                cycle1 +=  1  ;
108 
109            }
110 
111        }
112        dir += 10;
113        cycle += 1 ;
114    }
115 
116 
117}
118 
119 
120/* plot course function, return degree heading to */
121/* reach destination x, y; uses atan() trig function */
122plot_course(xx, yy)
123int xx, yy;
124{
125    int d;
126    int x, y;
127    int scale;
128    int curx, cury;
129 
130    scale = 100000;  /* scale for trig functions */
131    curx = loc_x();  /* get current location */
132    cury = loc_y();
133    x = curx - xx;
134    y = cury - yy;
135 
136    /* atan only returns -90 to +90, so figure out how to use */
137    /* the atan() value */
138 
139    if (x == 0) {      /* x is zero, we either move due north or south */
140        if (yy > cury)
141            d = 90;        /* north */
142        else
143            d = 270;       /* south */
144    } else {
145        if (yy < cury) {
146            if (xx > curx)
147                d = 360 + atan((scale * y) / x);  /* south-east, quadrant 4 */
148            else
149                d = 180 + atan((scale * y) / x);  /* south-west, quadrant 3 */
150        } else {
151            if (xx > curx)
152                d = atan((scale * y) / x);        /* north-east, quadrant 1 */
153            else
154                d = 180 + atan((scale * y) / x);  /* north-west, quadrant 2 */
155        }
156    }
157    return (d);
158}