File: sai.c

package info (click to toggle)
xscorch 0.2.0-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 4,636 kB
  • ctags: 3,399
  • sloc: ansic: 31,504; sh: 8,826; makefile: 467; perl: 16
file content (270 lines) | stat: -rw-r--r-- 5,461 bytes parent folder | download | duplicates (2)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* $Header: /fridge/cvs/xscorch/sai/sai.c,v 1.10 2004/02/26 06:34:54 justins Exp $ */
/*
   
   xscorch - sai.c            Copyright(c) 2000-2003 Justin David Smith
   justins(at)chaos2.org      http://chaos2.org/
    
   Main file for AI code
    

   This program is free software; you can redistribute it and/or modify 
   it under the terms of the GNU General Public License as published by 
   the Free Software Foundation, version 2 of the License ONLY. 

   This program is distributed in the hope that it will be useful, 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation, 
   Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/
#include <saiint.h>              /* Main AI internal header */

#include <sgame/sconfig.h>       /* Need config data structure */
#include <sgame/sland.h>         /* Need sc_land_calculate_deltas */
#include <sgame/sphysics.h>      /* Need for debugging code */
#include <sgame/splayer.h>       /* Need player data structure */
#include <sutil/srand.h>         /* Random AI selection */



void sc_ai_init_game(unused const struct _sc_config *c, sc_player *p) {
/* sc_ai_init_game */

   if(p->aitype == SC_AI_RANDOM) {
      do { /* Need a random AI */
         p->ai->realaitype = game_lrand(SC_AI_RANDOM);
      } while(p->ai->realaitype == SC_AI_HUMAN);
   } else {
      p->ai->realaitype = p->aitype;
   } /* Selecting a random AI? */

}



void sc_ai_init_round(unused const struct _sc_config *c, sc_player *p) {
/* sc_ai_init_round */

   p->ai->victim = NULL;

}



sc_ai *sc_ai_new(void) {

   return((sc_ai *)malloc(sizeof(sc_ai)));

}



void sc_ai_free(sc_ai **ai) {

   if(ai == NULL || *ai == NULL) return;
   free(*ai);
   *ai = NULL;

}



sc_ai_controller *sc_ai_controller_new(void) {

   sc_ai_controller *aic;
   
   aic = (sc_ai_controller *)malloc(sizeof(sc_ai_controller));
   if(aic == NULL) return(NULL);
   
   aic->humantargets = false;
   aic->allowoffsets = false;
   aic->alwaysoffset = false;
   aic->enablescan = false;
   aic->nobudget = false;
   
   return(aic);

}



void sc_ai_controller_free(sc_ai_controller **aic) {

   if(aic == NULL || *aic == NULL) return;
   free(*aic);
   *aic = NULL;

}



void sc_ai_trajectory_terminus(const sc_config *c, const sc_trajectory *tr) {

   if(SC_AI_DEBUG_VICTIMS) {
      const sc_player *vp;
      const sc_player *p;
      int dx;
      int dy;

      if(c == NULL || tr == NULL || tr->player == NULL) return;
      p = tr->player;
      if(p->ai == NULL || p->ai->victim == NULL) return;

      /* We hit something; was it near intended victim? */
      vp = p->ai->victim;
      if(vp->index != tr->victim && tr->victim >= 0) {
         printf("trajectory(%d): intended victim %d, hit victim %d\n", 
                p->index, vp->index, tr->victim);
      }
      if(sc_land_calculate_deltas(c->land, &dx, &dy, vp->x, vp->y, tr->curx, tr->cury)) {
         printf("trajectory(%d): was %g pixels away from intended victim %d\n",
                p->index, sqrt(SQR(dx) + SQR(dy)), vp->index);
      }
   }
   
}



/* AI types and codes */
static const char *_sc_player_ai_names[] = {
   "Human",
   "Network",
   "Moron",
   "Shooter",
   "Spreader",
   "Chooser",      
   "Calculater",    
   "Annihilater",
   "Insanity",
   "Unknown",
   NULL
};
static const unsigned int _sc_player_ai_types[] = {
   SC_AI_HUMAN,
   SC_AI_NETWORK,
   SC_AI_MORON,
   SC_AI_SHOOTER,
   SC_AI_SPREADER,
   SC_AI_CHOOSER,
   SC_AI_CALCULATER,
   SC_AI_ANNIHILATER,
   SC_AI_INSANITY,
   SC_AI_RANDOM,
   0
};


const char **sc_ai_names(void) {
   
   return(_sc_player_ai_names);

}


const unsigned int *sc_ai_types(void) {
   
   return(_sc_player_ai_types);

}


const char *sc_ai_name(sc_ai_type ai) {

   int index = 0;

   while(_sc_player_ai_names[index] != NULL) {
      if(_sc_player_ai_types[index] == ai) return(_sc_player_ai_names[index]);
      ++index;
   }
   return("Unknown");

}



/* AI types and codes */
static const char *_sc_player_ai_names_nonet[] = {
   "Human",
   "Moron",
   "Shooter",
   "Spreader",
   "Chooser",      
   "Calculater",    
   "Annihilater",
   "Insanity",
   "Unknown",
   NULL
};
static const unsigned int _sc_player_ai_types_nonet[] = {
   SC_AI_HUMAN,
   SC_AI_MORON,
   SC_AI_SHOOTER,
   SC_AI_SPREADER,
   SC_AI_CHOOSER,
   SC_AI_CALCULATER,
   SC_AI_ANNIHILATER,
   SC_AI_INSANITY,
   SC_AI_RANDOM,
   0
};


const char **sc_ai_names_nonet(void) {
   
   return(_sc_player_ai_names_nonet);

}


const unsigned int *sc_ai_types_nonet(void) {
   
   return(_sc_player_ai_types_nonet);

}



/* AI types and codes */
static const char *_sc_player_ai_names_nohuman[] = {
   "Moron",
   "Shooter",
   "Spreader",
   "Chooser",      
   "Calculater",    
   "Annihilater",
   "Insanity",
   "Unknown",
   NULL
};
static const unsigned int _sc_player_ai_types_nohuman[] = {
   SC_AI_MORON,
   SC_AI_SHOOTER,
   SC_AI_SPREADER,
   SC_AI_CHOOSER,
   SC_AI_CALCULATER,
   SC_AI_ANNIHILATER,
   SC_AI_INSANITY,
   SC_AI_RANDOM,
   0
};


const char **sc_ai_names_nohuman(void) {
   
   return(_sc_player_ai_names_nohuman);

}


const unsigned int *sc_ai_types_nohuman(void) {
   
   return(_sc_player_ai_types_nohuman);

}