crobs/xecutner.r
001 | int dir, dis; |
002 | main () |
003 | { |
004 | int dam, res, num; |
005 |
006 | dam = damage (); |
007 | first (); |
008 |
009 | while (1) |
010 | { |
011 | if (dam + 5 < damage ()) |
012 | { |
013 | dam = damage (); |
014 | flee (); |
015 | first (); |
016 | } |
017 | else if (dis == 0) |
018 | { |
019 | first (); |
020 | } |
021 | else |
022 | { |
023 | if (dis < 700 && dis > 0) |
024 | { |
025 | cannon (dir, dis); |
026 | } |
027 | if (dis > 700) |
028 | { |
029 | drive (dir, 75); |
030 | } |
031 | else if (dis > 100) |
032 | { |
033 | drive (dir, 30); |
034 | } |
035 | else if (dis > 20) |
036 | { |
037 | drive (dir, 10); |
038 | } |
039 | else |
040 | { |
041 | drive (dir, 0); |
042 | } |
043 |
044 | res = 5; |
045 | while (res > 0) |
046 | { |
047 | dir -= res; |
048 | dis = scan (dir, res); |
049 | if (dis == 0) |
050 | { |
051 | dir += res * 2; |
052 | } |
053 | res /= 2; |
054 | } |
055 | } |
056 | } |
057 | } |
058 |
059 | first () /*return the angle to the 'closest' robot*/ |
060 | { |
061 | int resolution; |
062 |
063 | resolution = 10; |
064 |
065 | dis = scan (dir, resolution); |
066 | while (dis == 0) |
067 | { |
068 | dir += resolution * 2; |
069 | dis = scan (dir, resolution); |
070 | } |
071 |
072 | while (resolution > 1) |
073 | { |
074 | resolution /= 2; |
075 | dir -= resolution; |
076 | dis = scan (dir, resolution); |
077 | if (dis == 0) |
078 | { |
079 | dir += resolution * 2; |
080 | } |
081 | } |
082 | } |
083 |
084 | flee () /*move to a random location on the screen*/ |
085 | { |
086 | int dir,x,y; |
087 |
088 | x = rand (970) + 15; |
089 | y = rand (970) + 15; |
090 |
091 | dir = plot_course (x,y); |
092 | drive (dir, 100); |
093 |
094 | while (distance (loc_x(), loc_y(), x, y) > 150 && speed () > 0) |
095 | ; |
096 | drive (dir, 0); |
097 | } |
098 |
099 | int |
100 | distance (x1, y1, x2, y2) /*the distance between the two points*/ |
101 | int x1,y1, x2, y2; |
102 | { |
103 | int x,y; |
104 |
105 | x = x1-x2; |
106 | y = y1-y2; |
107 | return ( sqrt ((x*x) + (y*y))); |
108 | } |
109 |
110 | int |
111 | plot_course (xx,yy) /*the angle to get to the new point*/ |
112 | int xx,yy; |
113 | { |
114 | int d; |
115 | int x,y; |
116 | int scale; |
117 | int curx,cury; |
118 |
119 | scale = 100000; |
120 | curx = loc_x (); |
121 | cury = loc_y (); |
122 | x = curx - xx; |
123 | y = cury - yy; |
124 |
125 | if (x==0) |
126 | { |
127 | if (yy > cury) |
128 | { |
129 | d = 90; |
130 | } |
131 | else |
132 | { |
133 | d = 270; |
134 | } |
135 | } |
136 | else |
137 | { |
138 | if (yy < cury) |
139 | { |
140 | if (xx > curx) |
141 | { |
142 | d = 360 - atan ((scale * y) / x); |
143 | } |
144 | else |
145 | { |
146 | d = 180 + atan ((scale * y) / x); |
147 | } |
148 | } |
149 | else |
150 | { |
151 | if (xx > curx) |
152 | { |
153 | d = atan ((scale * y) / x); |
154 | } |
155 | else |
156 | { |
157 | d = 180 + atan ((scale * y) / x); |
158 | } |
159 | } |
160 | } |
161 | return (d); |
162 | } |