File: follower.c

package info (click to toggle)
netmaze 0.81+jpg0.82-13
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,204 kB
  • ctags: 915
  • sloc: ansic: 8,277; tcl: 1,223; makefile: 66; sh: 26
file content (231 lines) | stat: -rw-r--r-- 3,884 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* 
 * follower bot
 * written and copyrights: roderick@ksu.ksu.edu (Mike Roderick)
 */

#include <string.h>
#include "netmaze.h"
#include <math.h>

#define PI 3.141592

extern struct shared_struct *sm;

/*******************
 * the init_robot is called one time right after the start
 */

void init_robot(void)
{
  strcpy(sm->ownname,"Follower2");
  strcpy(sm->owncomment,"Look Behind You");
}

void start_robot(void)
{
  printf("Here We Go Again\n");
  printf("The number of players in this game is:  %d\n",sm->anzplayers);
}

/*******************
 * the own_action is called every 'beat'
 *
 * this bot really is a dummy, I use this guy
 * to test my program with 31 'players'.
 */

int own_action(void)
{
  int ret=0;
  int chase;
  static int prtcntr = 0;
  int me;
  int angle;
  int mangle;
  int mag,dir;
  int updown;
  int shot;

  me = sm->shownumber;

  if(sm->playfeld[me].fitness < 800)
    updown = JOY_DOWN;
  else
    updown = JOY_UP;

  mangle = sm->playfeld[me].winkel;        /* get my angle */

  chase = whos_close(me);                  /* who should I chase */

  if (chase == -1)
    return 0;

  angle = what_ang(chase , me);            /* what is the target's agnle */

   if((sm->playfeld[me].fitness<800)&&sm->playfeld[me].team!=sm->playfeld[chase].team)
    updown = JOY_DOWN;
  else
    updown = JOY_UP; 

  if (!(abs(angle-mangle)<6) || sm->playfeld[me].team == sm->playfeld[chase].team)
    shot = 0;                  /* hold your fire */
  else
    shot = JOY_BUTTON;         /* shoot them */

  dir = angle - mangle;
  mag = abs(dir);
  
  if (((dir < 0) && (mag < 128)) || ((dir > 0) && (mag >= 128)))
    {
      ret = JOY_LEFT | updown | shot;
    }
  else if (((dir > 0) && (mag < 128)) || ((dir < 0) && (mag >=128)))
    {
      ret = JOY_RIGHT | updown | shot;
    }
  else
    {
      ret = updown | shot;
    }
/*
  if ( sm->playfeld[me].fitness < 800 )
    updown = JOY_DOWN;
  else
    updown = JOY_UP;

  if (!(( angle - mangle < 6 ) && ( angle - mangle > -6 )))
    shot = 0;
  else
    shot = JOY_BUTTON;

  if ( angle > mangle )
    {
      ret = JOY_RIGHT | updown | shot;
    }
  else if ( angle < mangle )
    {
      ret = JOY_LEFT | updown | shot;
    }
  else
    {
      ret = updown | JOY_BUTTON;
    }
*/
  if(prtcntr++ > 10&&0)
    {
      prtcntr = 0;
      printf("chasing opponent: %d at angle: %d My angle: %d\n",chase,angle,sm->playfeld[me].winkel);
    }

  return ret;
}


int whos_close(int me)
{
  int i;
  int dist=999999999;
  int temp;
  int close=-1;


  for( i = 0 ; i < (sm->anzplayers) ; i++ )
    {
      if( i == me )
	continue;
      temp = get_dist( i , me );
      if((dist > temp) && (sm->playfeld[i].alive) && (sm->playfeld[i].team != sm->playfeld[me].team))
	{
	  dist = temp;
	  close = i;
	}
/*      else if (dist == -1)
	{
	  dist = 999999999;
	  close = i;
	}
*/
    }
  return close;
}

int get_dist(int a, int b)
{
  int tmp1;
  int tmp2;
  int difx;
  int dify;
  int dist;
  double dx,dy,di;

  tmp1 = sm->playfeld[a].x;
  tmp2 = sm->playfeld[b].x;
  difx = tmp1 - tmp2;
  tmp1 = sm->playfeld[a].y;
  tmp2 = sm->playfeld[b].y;
  dify = tmp1 - tmp2;
  dx   = (double) difx;
  dy   = (double) dify;
  dx  *= dx;
  dy  *= dy;
  di   = sqrt(dx+dy);
  dist = (int) di;
  return dist;
}

int what_ang(int target , int me)
{
  int ang;
  int difx;
  int dify;
  double an;
  double dx;
  double dy;
  int tmp1;
  int tmp2;
  double tmp3;

  tmp1 = sm->playfeld[me].x;
  tmp2 = sm->playfeld[target].x;
  difx = tmp1 - tmp2;
  dx   = (double) difx;
  if (dx == 0.0)
    dx = 1.0;
  tmp1 = sm->playfeld[me].y;
  tmp2 = sm->playfeld[target].y;
  dify = tmp1 - tmp2;
  dy   = (double) dify;
  tmp3 = dy/dx;
  an   = -atan(tmp3);
  an   = (an/(2.0*PI))*256.0;
  ang  = (int) an;
  if ( difx < 0 )
    ang += 63;
  else
    ang += 191;
  if ( ang > 255 )
    ang -= 256;
  return ang;
}