File: saibuy.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 (675 lines) | stat: -rw-r--r-- 24,914 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/* $Header: /fridge/cvs/xscorch/sai/saibuy.c,v 1.29 2004/02/26 06:34:54 justins Exp $ */
/*

   xscorch - sai.c            Copyright(c) 2000-2003 Justin David Smith
                              Copyright(c) 2001-2003 Jacob Luna Lundberg
   justins(at)chaos2.org      http://chaos2.org/
   jacob(at)chaos2.org        http://chaos2.org/~jacob

   AI purchasing 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>              /* AI internal header */

#include <sgame/sconfig.h>       /* Need basic configuration */
#include <sgame/seconomy.h>      /* Need current economical state */
#include <sgame/sinventory.h>    /* Need to get player inventory */
#include <sgame/splayer.h>       /* Need player data */
#include <sgame/sweapon.h>       /* Yield calculations */
#include <sgame/swindow.h>       /* Used for status messages */



static inline int _sc_ai_budget(const sc_config *c, const sc_player *p) {
/* sc_ai_budget
   Calculate the amount of money the AI is permitted to spend this turn */

   const sc_economy_config *ec; /* Economy data */
   double percent;              /* Percentage of rounds played */

   /* Any budget constraints? */
   if(c->aicontrol->nobudget) {
      return(p->money * 0.75);
   }

   ec = c->economics;
   if(ec->computersbuy) {
      /* Only buy based on current interest rate, or the default
         conservative budget, whichever is larger */
      if(ec->computersaggressive) {
         percent = SC_AI_AGGRESSIVE_BUDGET;
      } else {
         percent = SC_AI_CONSERVATIVE_BUDGET;
      } /* What rate should we set as the "minimum"? */
      if(ec->interestrate > percent) {
         percent = ec->currentinterest;
      } /* Take maximum of "percent" and current interest rate */

      /* Also factor in the number of rounds remaining :) */
      percent += ((double)(c->curround + 1) / c->numrounds) * (1 - percent);
      if(percent > 1) percent = 1;

      if(SC_AI_DEBUG_BUY) {
         printf("AI_budget:   %s, %s has budget %d\n", sc_ai_name(p->aitype), p->name, (int)(p->money * percent));
      }

      /* That ought to be sufficiently devastating. */
      return(p->money * percent);
   } /* Can computers buy? */

   /* Computers cannot buy. */
   return(0);

}



static bool _sc_ai_buy_last_weapons(const sc_config *c, sc_player *p, int *budget) {
/* sc_ai_buy_last_weapons
   Buy the last weapons on the list that we can afford.  This is a rather
   naive approach, in assuming that weapons at the end of the list must
   surely be better?  */

   sc_weapon_info *info;        /* Weapon information */
   int count = sc_weapon_count(c->weapons, SC_WEAPON_LIMIT_ALL);
   int inner, outer;            /* Iterators */
   bool result = false;

   for(outer = SC_AI_BUY_MAX_OF_WEAPON; outer > 0; --outer) {
      info = sc_weapon_first(c->weapons, SC_WEAPON_LIMIT_ALL | SC_WEAPON_SCAN_REVERSE);
      for(inner = count; inner > 0; --inner) {
         if(sc_inventory_can_buy_weapon(p, info, *budget) &&
            sc_weapon_statistic(c->weapons, info, p, SC_WEAPON_STAT_YIELD) > 0) {
            /* Buy some of this weapon. */
            sc_inventory_buy_weapon(p, info);
            *budget -= info->price;
            if(SC_AI_DEBUG_BUY) {
               printf("   AI_buy:   %s, %s bought %s.  $%d\n", sc_ai_name(p->aitype), p->name, info->name, *budget);
            }
            result = true;
         } /* Can we buy this weapon? */
         info = sc_weapon_next(c->weapons, info, SC_WEAPON_LIMIT_ALL | SC_WEAPON_SCAN_REVERSE);
      } /* Iteration on info lists */
   } /* Iterate multiples of weapons */

   return(result);

}



static void _sc_ai_sorted_weapon_scores(sc_weapon_config *wc, int *ordering, double *scores) {
/* sc_ai_sorted_weapon_scores
   Impose an ordering on the weapons, based on scores loaded into *scores.  */

   int inner, outer;             /* Iterator variables... */
   int count = sc_weapon_count(wc, SC_WEAPON_LIMIT_ALL);

   /* Bubble-sort the scores */   
   for(outer = 0; outer < count; ++outer) {
      for(inner = 0; inner < outer; ++inner) {
         if(scores[inner] > scores[outer]) {
            scores[inner] += scores[outer];
            scores[outer] = scores[inner] - scores[outer];
            scores[inner] = scores[inner] - scores[outer];
            ordering[inner] += ordering[outer];
            ordering[outer] = ordering[inner] - ordering[outer];
            ordering[inner] = ordering[inner] - ordering[outer];
         }
      }
   }

}



static bool _sc_ai_buy_weapons_from_list(const sc_config *c, sc_player *p, int *budget, const int *ordering) {
/* sc_ai_buy_weapons_from_list
   Buys weapons from a presorted list, beginning at the _end_ of the list.  */

   sc_weapon_info *info;   /* Weapon information. */
   int count = sc_weapon_count(c->weapons, SC_WEAPON_LIMIT_ALL);
   int inner, outer;             /* iterators */
   bool result = false;

   /* Iterate through, beginning with the "best" weapons. */
   for(outer = 0; outer < SC_AI_BUY_MAX_OF_WEAPON; ++outer) {
      for(inner = count - 1; inner >= 0; --inner) {
         info = sc_weapon_lookup(c->weapons, ordering[inner], SC_WEAPON_LIMIT_ALL);
         /* We don't want to buy weapons that won't kill things!
            We also bias slightly in favor of buying weapons we don't own yet. */
         if(sc_inventory_can_buy_weapon(p, info, *budget) &&
            sc_weapon_statistic(c->weapons, info, p, SC_WEAPON_STAT_YIELD) &&
            (outer || (info->inventories[p->index] / info->bundle) < SC_AI_INVENTORY_HIGH)) {
            /* buy some of this weapon. */
            sc_inventory_buy_weapon(p, info);
            *budget -= info->price;
            if(SC_AI_DEBUG_BUY) {
               printf("   AI_buy:   %s, %s bought %s.  $%d\n", sc_ai_name(p->aitype), p->name, info->name, *budget);
            }
            result = true;
         } /* Can we buy this weapon? */
      } /* Buying weapons */
   } /* Iterate multiples of weapons */

   return(result);

}



static bool _sc_ai_buy_weapons_by_score(const sc_config *c, sc_player *p, int *budget) {
/* sc_ai_buy_weapons_by_score
   Buy the weapons yielding the best destructive power.  This is a great
   buying strategy for players that just want to annihilate the entire
   playing field.  Coupled with agressive, this can be quite deadly!  */

   sc_weapon_info *info;
   int count = sc_weapon_count(c->weapons, SC_WEAPON_LIMIT_ALL);
   /* Sorted indices, based on score. */
   int *ordering = (int *)malloc(sizeof(int) * count);
   /* Sorted scores corresponding to ordering */
   double *scores = (double *)malloc(sizeof(double) * count);
   int i; /* Iterator */
   bool result;

   if(ordering == NULL || scores == NULL) {
      fprintf(stderr, "Malloc error in sc_ai_buy_weapons_by_score!\n");
      free(ordering);
      free(scores);
      return(false);
   }

   /* Calculate scores, and set initial ordering */
   info = sc_weapon_first(c->weapons, SC_WEAPON_LIMIT_ALL | SC_WEAPON_SCAN_FORWARD);
   for(i = 0; i < count; ++i) {
      ordering[i] = info->ident;
      scores[i] = sc_weapon_statistic(c->weapons, info, p, SC_WEAPON_STAT_YIELD);
      info = sc_weapon_next(c->weapons, info, SC_WEAPON_LIMIT_ALL | SC_WEAPON_SCAN_FORWARD);
   }

   /* Bubble-sort the scores */   
   _sc_ai_sorted_weapon_scores(c->weapons, ordering, scores);

   /* Iterate through, beginning with the "best" weapons. */
   result = _sc_ai_buy_weapons_from_list(c, p, budget, ordering);

   free(ordering);
   free(scores);
   return(result);

}



static bool _sc_ai_buy_weapons_by_weighted_score(const sc_config *c, sc_player *p, int *budget) {
/* sc_ai_buy_weapons_by_weighted_score
   Buy the weapons yielding the best destructive power per price of
   unit.  This is the "efficient" way to buy, but there are times when
   a good screen-destroying weapon is better than a weapon that will
   take us 100 turns to wipe out the opponent...  */

   sc_weapon_info *info;
   int count = sc_weapon_count(c->weapons, SC_WEAPON_LIMIT_ALL);
   /* Sorted indices, based on score. */
   int *ordering = (int *)malloc(sizeof(int) * count);
   /* Sorted scores corresponding to ordering */
   double *scores = (double *)malloc(sizeof(double) * count);
   int i; /* Iterator */
   bool result;

   if(ordering == NULL || scores == NULL) {
      fprintf(stderr, "Malloc error in sc_ai_buy_weapons_by_weighted_score!\n");
      free(ordering);
      free(scores);
      return(false);
   }

   /* Calculate scores, and set initial ordering */
   info = sc_weapon_first(c->weapons, SC_WEAPON_LIMIT_ALL | SC_WEAPON_SCAN_FORWARD);
   for(i = 0; i < count; ++i) {
      ordering[i] = info->ident;
      scores[i] = sc_weapon_statistic(c->weapons, info, p, SC_WEAPON_STAT_ECON_YIELD);
      info = sc_weapon_next(c->weapons, info, SC_WEAPON_LIMIT_ALL | SC_WEAPON_SCAN_FORWARD);
   }

   /* Bubble-sort the scores */   
   _sc_ai_sorted_weapon_scores(c->weapons, ordering, scores);

   /* Iterate through, beginning with the "best" weapons. */
   result = _sc_ai_buy_weapons_from_list(c, p, budget, ordering);

   free(ordering);
   free(scores);
   return(result);

}



static bool _sc_ai_buy_precision_weapons(const sc_config *c, sc_player *p, int *budget) {
/* sc_ai_buy_precision_weapons
   Buy the weapons yielding high power and precision.  This is for use
   by AI players who know exactly how to aim and exactly what/who they
   want to destroy.  Such a player can take out her neighbor without
   also destroying herself as one might expect of AI players that make
   use of _sc_ai_buy_weapons_by_score(). */

   sc_weapon_info *info;
   int count = sc_weapon_count(c->weapons, SC_WEAPON_LIMIT_ALL);
   /* Sorted indices, based on score. */
   int *ordering = (int *)malloc(sizeof(int) * count);
   /* Sorted scores corresponding to ordering */
   double *scores = (double *)malloc(sizeof(double) * count);
   int i; /* Iterator */
   bool result;

   if(ordering == NULL || scores == NULL) {
      fprintf(stderr, "Malloc error in sc_ai_buy_precision_weapons!\n");
      free(ordering);
      free(scores);
      return(false);
   }

   /* Calculate scores, and set initial ordering */
   info = sc_weapon_first(c->weapons, SC_WEAPON_LIMIT_ALL | SC_WEAPON_SCAN_FORWARD);
   for(i = 0; i < count; ++i) {
      ordering[i] = info->ident;
      scores[i] = sc_weapon_statistic(c->weapons, info, p, SC_WEAPON_STAT_PRECISION_YIELD);
      info = sc_weapon_next(c->weapons, info, SC_WEAPON_LIMIT_ALL | SC_WEAPON_SCAN_FORWARD);
   }

   /* Bubble-sort the scores */   
   _sc_ai_sorted_weapon_scores(c->weapons, ordering, scores);

   /* Iterate through, beginning with the "best" weapons. */
   result = _sc_ai_buy_weapons_from_list(c, p, budget, ordering);

   free(ordering);
   free(scores);
   return(result);

}



static bool _sc_ai_buy_shield_sappers(const sc_config *c, sc_player *p, int *budget) {
/* sc_ai_buy_shield_sappers
   Buy up Shield Sappers, which have no yield,
   but AIs can use them against shielded players. */

   sc_weapon_info *info;        /* Weapon information */
   int count = sc_weapon_count(c->weapons, SC_WEAPON_LIMIT_ALL);
   int sapbudget = *budget / 4; /* Sappers shouldn't drain the budget */
   int inner, outer;            /* Iterators */
   bool result = false;

/* TEMP - We're not using this for now.  See the comment in the
   shield sappers section of saiturn.c for more information...  */
return(result);

   for(outer = SC_AI_BUY_MAX_OF_WEAPON; outer > 0; --outer) {
      info = sc_weapon_first(c->weapons, SC_WEAPON_LIMIT_ALL | SC_WEAPON_SCAN_REVERSE);
      for(inner = count; inner > 0; --inner) {
         if(sc_inventory_can_buy_weapon(p, info, sapbudget) && SC_WEAPON_IS_SAPPER(info)) {
            /* Buy some of this weapon. */
            sc_inventory_buy_weapon(p, info);
            *budget -= info->price;
            if(SC_AI_DEBUG_BUY) {
               printf("   AI_buy:   %s, %s bought %s.  $%d\n", sc_ai_name(p->aitype), p->name, info->name, *budget);
            }
            result = true;
         } /* Can we buy this weapon? */
         info = sc_weapon_next(c->weapons, info, SC_WEAPON_LIMIT_ALL | SC_WEAPON_SCAN_REVERSE);
      } /* Iteration on info lists */
   } /* Iterate multiples of weapons */

   return(result);

}



static bool _sc_ai_buy_auto_defense(const sc_config *c, sc_player *p, int *budget) {
/* sc_ai_buy_auto_defense
   Buy auto-defense, only if this is a sequential game. */

   sc_accessory_info *info;  /* Item information */
   int count = sc_accessory_count(c->accessories, SC_ACCESSORY_LIMIT_ALL);

   /* Check if this is a sequential game. */
   if(c->options.mode == SC_CONFIG_MODE_SYNCHRONOUS) return(false);

   /* If this is a sequential game, make damn sure we buy autodefense */
   info = sc_accessory_first(c->accessories, SC_ACCESSORY_LIMIT_ALL | SC_ACCESSORY_SCAN_FORWARD);
   for(; count > 0; --count) {
      if(sc_inventory_can_buy_accessory(p, info, *budget)) {
         /* Make sure the damn thing is auto-defence. */
         if(SC_ACCESSORY_IS_AUTO_DEF(info)) {
            /* Buy some of this autodefense */
            sc_inventory_buy_accessory(p, info);
            *budget -= info->price;
            if(SC_AI_DEBUG_BUY) {
               printf("   AI_buy:   %s, %s bought %s.  $%d\n", sc_ai_name(p->aitype), p->name, info->name, *budget);
            }
            return(true);
         } /* Is accessory auto-defense? */
      } /* Can we buy? */
      info = sc_accessory_next(c->accessories, info, SC_ACCESSORY_LIMIT_ALL | SC_ACCESSORY_SCAN_FORWARD);
   } /* Iterate through */

   return(false);

}



static bool _sc_ai_buy_contact_triggers(const sc_config *c, sc_player *p, int *budget) {
/* sc_ai_buy_contact_triggers
   Buy contact triggers if tunneling is allowed. */

   sc_accessory_info *info;  /* Item information */
   int count = sc_accessory_count(c->accessories, SC_ACCESSORY_LIMIT_ALL);

   /* Only buy if tunneling is allowed. */
   if(!c->weapons->tunneling) return(false);

   /* Buy contact triggers if we can. */
   info = sc_accessory_first(c->accessories, SC_ACCESSORY_LIMIT_ALL | SC_ACCESSORY_SCAN_FORWARD);
   for(; count > 0; --count) {
      /* Make sure the damn thing is a contact trigger. */
      if(SC_ACCESSORY_IS_TRIGGER(info)) {
         while(sc_inventory_can_buy_accessory(p, info, *budget)) {
            /* Buy some of these contact triggers */
            sc_inventory_buy_accessory(p, info);
            *budget -= info->price;
            if(SC_AI_DEBUG_BUY) {
               printf("   AI_buy:   %s, %s bought %s.  $%d\n", sc_ai_name(p->aitype), p->name, info->name, *budget);
            }
            return(true);
         } /* Can we buy? */
      } /* Is accessory a contact trigger? */
      info = sc_accessory_next(c->accessories, info, SC_ACCESSORY_LIMIT_ALL | SC_ACCESSORY_SCAN_FORWARD);
   } /* Iterate through */

   return(false);

}



static bool _sc_ai_buy_last_shields(const sc_config *c, sc_player *p, int *budget) {
/* sc_ai_buy_last_shields
   Buy the last shields on the list that we can afford.  This is a rather
   naive approach, in assuming that shields at the end of the list must
   surely be better?  */

   sc_accessory_info *info;  /* Shield information */
   int count = sc_accessory_count(c->accessories, SC_ACCESSORY_LIMIT_ALL);

   /* Buy some shielding */
   info = sc_accessory_first(c->accessories, SC_ACCESSORY_LIMIT_ALL | SC_ACCESSORY_SCAN_REVERSE);
   for(; count > 0; --count) {
      if(sc_inventory_can_buy_accessory(p, info, *budget)) {
         /* Make sure the damn thing is a shield. */
         if(SC_ACCESSORY_IS_SHIELD(info)) {
            /* Buy some of this shield */
            sc_inventory_buy_accessory(p, info);
            *budget -= info->price;
            if(SC_AI_DEBUG_BUY) {
               printf("   AI_buy:   %s, %s bought %s.  $%d\n", sc_ai_name(p->aitype), p->name, info->name, *budget);
            }
            return(true);  /* That's all, for now */
         } /* Is accessory a shield? */
      } /* Can we buy? */
      info = sc_accessory_next(c->accessories, info, SC_ACCESSORY_LIMIT_ALL | SC_ACCESSORY_SCAN_REVERSE);
   } /* Iterate up the accessory list. */

   return(false);

}



static void _sc_ai_sorted_accessory_scores(sc_accessory_config *ac, int *ordering, int *scores) {
/* sc_ai_sorted_accessory_scores
   Impose an ordering on the accessorys, based on scores loaded into *scores.  */

   int inner, outer;             /* Iterator variables... */
   int count = sc_accessory_count(ac, SC_ACCESSORY_LIMIT_ALL);

   /* Bubble-sort the scores */   
   for(outer = 0; outer < count; ++outer) {
      for(inner = 0; inner < outer; ++inner) {
         if(scores[inner] > scores[outer]) {
            scores[inner] += scores[outer];
            scores[outer] = scores[inner] - scores[outer];
            scores[inner] = scores[inner] - scores[outer];
            ordering[inner] += ordering[outer];
            ordering[outer] = ordering[inner] - ordering[outer];
            ordering[inner] = ordering[inner] - ordering[outer];
         }
      }
   }

}



static bool _sc_ai_buy_shields_from_list(const sc_config *c, sc_player *p, int *budget, const int *ordering) {
/* sc_ai_buy_shields_from_list
   Buy shields from the ordered list given; keep trying to
   buy until we find a set we can afford, then stop there.  */

   sc_accessory_info *info;  /* Accessory information. */
   int count = sc_accessory_count(c->accessories, SC_ACCESSORY_LIMIT_ALL);

   /* Iterate through, beginning with the "best" shields. */
   for(--count; count >= 0; --count) {
      /* Make sure the damn thing is a shield. */
      info = sc_accessory_lookup(c->accessories, ordering[count], SC_ACCESSORY_LIMIT_ALL);
      if(SC_ACCESSORY_IS_SHIELD(info)) {
         /* Can we afford this set of shields? */
         if(sc_inventory_can_buy_accessory(p, info, *budget)) {
            /* buy some of this shielding. */
            sc_inventory_buy_accessory(p, info);
            *budget -= info->price;
            if(SC_AI_DEBUG_BUY) {
               printf("   AI_buy:   %s, %s bought %s.  $%d\n", sc_ai_name(p->aitype), p->name, info->name, *budget);
            }
            return(true);  /* That's all, for now */
         } /* Can we buy these shields? */
      } /* Is this accessory a shield? */
   } /* Iterate through the shields */

   return(false);

}



static bool _sc_ai_buy_best_shields(const sc_config *c, sc_player *p, int *budget) {
/* sc_ai_buy_best_shields
   Buy the best shields on the list that we can afford.  We will
   want to sort the shields out before deciding which to buy.  */

   sc_accessory_info *info;
   int count = sc_accessory_count(c->accessories, SC_ACCESSORY_LIMIT_ALL);
   /* Sorted indices, based on score. */
   int *ordering = (int *)malloc(sizeof(int) * count);
   /* Sorted scores corresponding to ordering */
   int *scores = (int *)malloc(sizeof(int) * count);
   int i; /* Iterator */
   bool result;

   if(ordering == NULL || scores == NULL) {
      fprintf(stderr, "Malloc error in sc_ai_buy_best_shields!\n");
      free(ordering);
      free(scores);
      return(false);
   }

   /* Calculate scores, and set initial ordering */
   info = sc_accessory_first(c->accessories, SC_ACCESSORY_LIMIT_ALL | SC_ACCESSORY_SCAN_FORWARD);
   for(i = 0; i < count; ++i) {
      ordering[i] = info->ident;
      /* TEMP HACK - This should call a function in sgame/saccessory.c like the weapon version does. -JL */
      /* TEMP NOTE ABOUT MY TEMP HACK - Actually maybe sgame/sshield.c would make more sense, heh... */
      scores[i] = info->shield;  /* = sc_accessory_yield(c->accessories, info); */
      info = sc_accessory_next(c->accessories, info, SC_ACCESSORY_LIMIT_ALL | SC_ACCESSORY_SCAN_FORWARD);
   }

   /* TEMP NOTE NO. 2 - Ideally we should decide what's best based on type as well...
                        For example, a spreader probably prefers standard shielding.  ? */
   /* Bubble-sort the scores */
   _sc_ai_sorted_accessory_scores(c->accessories, ordering, scores);

   /* Iterate through, beginning with the "best" shields. */
   result = _sc_ai_buy_shields_from_list(c, p, budget, ordering);

   free(ordering);
   free(scores);
   return(result);

}



static bool _sc_ai_buy_batteries(const sc_config *c, sc_player *p, int *budget) {
/* sc_ai_buy_batteries
   Buys some batteries for the AI.  */

   sc_accessory_info *info;
   int count = sc_accessory_count(c->accessories, SC_ACCESSORY_LIMIT_ALL);

   /* Buy batteries if we can. */
   info = sc_accessory_first(c->accessories, SC_ACCESSORY_LIMIT_ALL | SC_ACCESSORY_SCAN_FORWARD);
   for(; count > 0; --count) {
      /* Make sure the damn thing is a battery. */
      if(SC_ACCESSORY_IS_BATTERY(info)) {
         while(sc_inventory_can_buy_accessory(p, info, *budget) && sc_player_battery_count(c, p) < SC_AI_MAX_BATTERIES) {
            /* Buy some of these here batteries */
            sc_inventory_buy_accessory(p, info);
            *budget -= info->price;
            if(SC_AI_DEBUG_BUY) {
               printf("   AI_buy:   %s, %s bought %s.  $%d\n", sc_ai_name(p->aitype), p->name, info->name, *budget);
            }
            return(true);
         } /* Can we buy? */
      } /* Is accessory a battery? */
      info = sc_accessory_next(c->accessories, info, SC_ACCESSORY_LIMIT_ALL | SC_ACCESSORY_SCAN_FORWARD);
   } /* Iterate through */

   return(false);

}



static inline void _sc_ai_buy_status(const sc_config *c, const sc_player *p) {
/* sc_ai_buy_status */

   if(SC_CONFIG_GFX_FAST(c)) return;
   sc_status_player_message(c->window, p, "AI player is buying ...");
   sc_window_idle(c->window);

}



sc_ai_result sc_ai_player_buy(const sc_config *c, sc_player *p) {
/* sc_ai_player_buy
   Buy some weapons and accessories.  */

   int budget;    /* Amount of money allocated to spend */

   budget = _sc_ai_budget(c, p);

   switch(p->ai->realaitype) {
      case SC_AI_HUMAN:
         return(SC_AI_NO_ACTION);
         break;

      case SC_AI_NETWORK:
      case SC_AI_RANDOM:
         /* No-ops */
         break;

      case SC_AI_MORON:
         _sc_ai_buy_status(c, p);
         _sc_ai_buy_contact_triggers(c, p, &budget);
         _sc_ai_buy_auto_defense(c, p, &budget);
         _sc_ai_buy_last_shields(c, p, &budget);
         _sc_ai_buy_last_weapons(c, p, &budget);
         break;

      case SC_AI_SHOOTER:
         _sc_ai_buy_status(c, p);
         _sc_ai_buy_contact_triggers(c, p, &budget);
         _sc_ai_buy_auto_defense(c, p, &budget);
         _sc_ai_buy_best_shields(c, p, &budget);
         _sc_ai_buy_shield_sappers(c, p, &budget);
         _sc_ai_buy_weapons_by_weighted_score(c, p, &budget);
         break;

      case SC_AI_SPREADER:
         _sc_ai_buy_status(c, p);
         _sc_ai_buy_contact_triggers(c, p, &budget);
         _sc_ai_buy_auto_defense(c, p, &budget);
         _sc_ai_buy_best_shields(c, p, &budget);
         _sc_ai_buy_weapons_by_score(c, p, &budget);
         _sc_ai_buy_shield_sappers(c, p, &budget);
         break;

      case SC_AI_CHOOSER:
         _sc_ai_buy_status(c, p);
         _sc_ai_buy_contact_triggers(c, p, &budget);
         _sc_ai_buy_auto_defense(c, p, &budget);
         _sc_ai_buy_best_shields(c, p, &budget);
         _sc_ai_buy_shield_sappers(c, p, &budget);
         _sc_ai_buy_precision_weapons(c, p, &budget);
         _sc_ai_buy_batteries(c, p, &budget);
         break;

      case SC_AI_CALCULATER:
         _sc_ai_buy_status(c, p);
         _sc_ai_buy_contact_triggers(c, p, &budget);
         _sc_ai_buy_auto_defense(c, p, &budget);
         _sc_ai_buy_best_shields(c, p, &budget);
         _sc_ai_buy_shield_sappers(c, p, &budget);
         _sc_ai_buy_precision_weapons(c, p, &budget);
         _sc_ai_buy_batteries(c, p, &budget);
         break;

      case SC_AI_ANNIHILATER:
      case SC_AI_INSANITY:
         _sc_ai_buy_status(c, p);
         budget = p->money * 0.85;
         _sc_ai_buy_contact_triggers(c, p, &budget);
         _sc_ai_buy_weapons_by_score(c, p, &budget);
         budget = p->money;
         _sc_ai_buy_auto_defense(c, p, &budget);
         _sc_ai_buy_best_shields(c, p, &budget);
         _sc_ai_buy_batteries(c, p, &budget);
         _sc_ai_buy_shield_sappers(c, p, &budget);
         break;

   }

   return(SC_AI_CONTINUE);

}