aminet/blindschl.r
001 | /*Sirius Cybernetic |
002 | Felix Buebl & Michael Balser |
003 | This robot does |
004 | a) move randomly |
005 | b) search vor the enemy (360 degree scan!) |
006 | c) try to watch() it where it is moving to (calculate it's heading) |
007 | d) fire at it |
008 |
009 | */ |
010 |
011 | /* the robot's own data: */ |
012 | int degree, turn; |
013 | /* enemies has the following meaning: |
014 | 0: Robot is searching for an enemie |
015 | ++1: Robot has found # enemies |
016 | */ |
017 | int enemies; |
018 | /* this "array" stores the coords where the current enemie has been last */ |
019 | int x1, y1, x2, y2; |
020 |
021 |
022 | main() |
023 | { |
024 | /* initialize */ |
025 | enemies = turn = degree = x1 = y1 = y2 = 0; |
026 | x2 = 1234; |
027 |
028 | while (666 != 42) /* for ever */ |
029 | { |
030 | move(); /* is invoked EVERY loop! */ |
031 |
032 | if (!enemies) |
033 | search(); |
034 | else |
035 | { |
036 | if (x2 == 1234) |
037 | watch(); |
038 | else |
039 | terminate(); |
040 | } |
041 | } |
042 | } |
043 |
044 | move () |
045 | { |
046 | /* IF we are close to a wall: Retreat 8c) */ |
047 |
048 |
049 | if (loc_x() > 700) |
050 | { |
051 | degree = 160 + rand (40); |
052 | drive (degree, 100); |
053 | return ; |
054 | } |
055 |
056 | if (loc_x() < 300) |
057 | { |
058 | degree = -20 + rand (40); |
059 | drive (degree, 100); |
060 | return ; |
061 | } |
062 |
063 | if (loc_y() > 700) |
064 | { |
065 | degree = 250 + rand (40); |
066 | drive (degree, 100); |
067 | return ; |
068 | } |
069 |
070 | if (loc_y() < 300) |
071 | { |
072 | degree = 70 + rand (40); |
073 | drive (degree, 100); |
074 | return ; |
075 | } |
076 |
077 | /* if the ran into some enemie, retreat */ |
078 |
079 | if (!speed()) |
080 | { |
081 | degree -= 180; |
082 | drive (degree, 100); |
083 | return ; |
084 | } |
085 |
086 | /* if we are not close to a wall, PERHAPS change movent */ |
087 |
088 |
089 |
090 | /* if we want to turn, do it */ |
091 |
092 | if (turn && speed() >= 50 ) |
093 | drive (degree, 42); |
094 |
095 | if (turn && speed() < 50) |
096 | { |
097 | degree += turn; |
098 | turn = 0; |
099 | drive (degree, 100); |
100 | } |
101 |
102 | if (!turn && x2 != 1234) |
103 | { |
104 | drive (degree, 100); |
105 | } |
106 |
107 |
108 | if ( rand (21) == 1 && !turn ) |
109 | { |
110 | if (degree > 180) |
111 | turn = (-1)* rand (10)*6 + 30; |
112 | else |
113 | turn = rand (10)*6 -30; |
114 | } |
115 | return ; |
116 | } |
117 |
118 |
119 | search () |
120 | { |
121 | int i, range, dir; |
122 |
123 | /* do a partially 360 degrees scan first in order to find a enemie */ |
124 | i=18; |
125 |
126 | while (i > 0) |
127 | { |
128 | if (range = scan ( i*20, 10) ) |
129 | if (range < 850 ) |
130 | { |
131 | dir = i*20; |
132 | enemies = 1; |
133 | i=0; /*break*/ |
134 | } |
135 | --i; |
136 | } |
137 |
138 | if (!enemies) |
139 | return ; |
140 |
141 | /* now get the exact position */ |
142 |
143 | i= -6; |
144 | while ( i<= 6) |
145 | { |
146 | if ( range = scan ( dir + i*3, 1)) |
147 | { |
148 | dir += i*3; |
149 | i = 42; /* break */ |
150 | } |
151 | ++i; |
152 | } |
153 | /* evaluate the coords */ |
154 |
155 | x1 = loc_x() + cos (dir)*range/100000; |
156 | y1 = loc_y() + sin (dir)*range/100000; |
157 | x2 = 1234; |
158 | return ; |
159 |
160 | } |
161 |
162 | watch() |
163 | { |
164 | int scandir, newrange, i; |
165 | int xh,yh; |
166 |
167 | xh=loc_x()-x1; |
168 | yh=loc_y()-y1; |
169 |
170 | if (xh>0) |
171 | scandir = atan (yh*100000/xh) + 180; /* +-90 */ |
172 | else |
173 | scandir = atan (yh*100000/xh); |
174 |
175 | if (scandir < 0) |
176 | scandir += 360; |
177 |
178 | i=0; |
179 | while ((! (newrange = scan (scandir+i*10, 5)) ) && i< 3 ) |
180 | { |
181 | if (i <= 0) |
182 | i = -i+1; |
183 | else |
184 | i *= -1; |
185 |
186 | } |
187 |
188 | if (!newrange) |
189 | { |
190 | enemies = 0; |
191 | return ; |
192 | } |
193 | scandir += i*10; |
194 |
195 | i=0; |
196 | while (( ! (newrange = scan (scandir + i*3, 1)) ) && i < 2) |
197 | { |
198 | if ( i <= 0) |
199 | i = -i +1; |
200 | else |
201 | i *= -1; |
202 | } |
203 | scandir += i*3; |
204 | /* |
205 | if ( sqrt ((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)) > 123) |
206 | { |
207 | enemies = 0; |
208 | x2 = 1234; |
209 | } |
210 | */ |
211 | x2 = loc_x() + cos (scandir)*newrange/100000; |
212 | y2 = loc_y() + sin (scandir)*newrange/100000; |
213 | drive (degree, 0); |
214 |
215 | return ; |
216 | } |
217 |
218 | terminate() |
219 | { |
220 | int xt, yt, xh, yh, shootdir, shootrange; |
221 |
222 | xt = x2 + x2-x1; |
223 | yt = y2 + y2-y1; |
224 |
225 | if (xt-loc_x() < 0) |
226 | shootdir = atan ((yt-loc_y())*100000/(xt-loc_x())) + 180; /* +-90 */ |
227 | else |
228 | shootdir = atan ((yt-loc_y())*100000/(xt-loc_x())); |
229 |
230 | if (shootdir < 0) |
231 | shootdir += 360; |
232 |
233 | xh=xt-loc_x(); |
234 | yh=yt-loc_y(); |
235 | if ( (shootrange = sqrt (xh*xh + yh*yh)) > 721) |
236 | { |
237 | y1 = y2; |
238 | x1 = x2; |
239 | return ; |
240 | } |
241 |
242 |
243 | cannon (shootdir, shootrange); |
244 | y1 = y2; |
245 | x1 = x2; |
246 | x2 = 1234; |
247 |
248 | return ; |
249 | } |