File: settlers.c

package info (click to toggle)
freeciv 1.9.0-2.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 11,004 kB
  • ctags: 6,284
  • sloc: ansic: 65,037; makefile: 634; sh: 418; sed: 93
file content (1395 lines) | stat: -rw-r--r-- 49,994 bytes parent folder | download
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
/********************************************************************** 
 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
   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; either version 2, or (at your option)
   any later version.

   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.
***********************************************************************/
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "game.h"
#include "government.h"
#include "log.h"
#include "map.h"
#include "packets.h"
#include "timing.h"

#include "cityhand.h"
#include "citytools.h"
#include "gotohand.h"
#include "maphand.h"
#include "unitfunc.h"
#include "unithand.h"
#include "unittools.h"

#include "aicity.h"
#include "aiunit.h"

#include "settlers.h"

extern struct move_cost_map warmap;

signed short int minimap[MAP_MAX_WIDTH][MAP_MAX_HEIGHT];
static unsigned short int territory[MAP_MAX_WIDTH][MAP_MAX_HEIGHT];
/* negative: in_city_radius, 0: unassigned, positive: city_des */

/**************************************************************************
...
**************************************************************************/
const char *get_a_name(struct player *pplayer)
{
  static char buf[80];
  static int x=0;
  sprintf(buf, "%s", city_name_suggestion(pplayer)); /* Not a Number -- Syela */
  if (!buf[0]) sprintf(buf, "city%d", x++);
  return buf;
}

/**************************************************************************
...
**************************************************************************/
static int ai_do_build_city(struct player *pplayer, struct unit *punit)
{
  int x,y;
  struct packet_unit_request req;
  struct city *pcity;
  req.unit_id=punit->id;
  strcpy(req.name, get_a_name(pplayer));
  x = punit->x; y = punit->y; /* Trevor Pering points out that punit gets freed */
  handle_unit_build_city(pplayer, &req);        
  pcity=map_get_city(x, y); /* so we need to cache x and y for a very short time */
  if (!pcity)
    return 0;
  for (y=0;y<5;y++)
    for (x=0;x<5;x++) {
      if ((x==0 || x==4) && (y==0 || y==4)) 
        continue;
      light_square(pplayer, x+pcity->x-2, y+pcity->y-2, 0);
    }
  return 1;
}

int amortize(int b, int d)
{
  int num = MORT - 1;
  int denom;
  int s = 1;
  assert(d >= 0);
  if (b < 0) { s = -1; b *= s; }
  while (d && b) {
    denom = 1;
    while (d >= 12 && !(b>>28) && !(denom>>27)) {
      b *= 3;          /* this is a kluge but it is 99.9% accurate and saves time */
      denom *= 5;      /* as long as MORT remains 24! -- Syela */
      d -= 12;
    }
    while (!(b>>25) && d && !(denom>>25)) {
      b *= num;
      denom *= MORT;
      d--;
    }
    if (denom > 1) {
      b = (b + (denom/2)) / denom;
    }
  }
  return(b * s);
}

void generate_minimap(void)
{
  int a, i, j;

  memset(minimap, 0, sizeof(minimap));
  for (a = 0; a < game.nplayers; a++) {
    city_list_iterate(game.players[a].cities, pcity)
      city_map_iterate(i, j) {
        minimap[map_adjust_x(pcity->x+i-2)][map_adjust_y(pcity->y+j-2)]--;
      }
    city_list_iterate_end;
  }
}

void remove_city_from_minimap(int x, int y)
{
  int i, j, n, xx;

  freelog(LOG_DEBUG, "Removing (%d, %d) from minimap.", x, y);
  for (j = -4; j <= 4; j++) {
    if (y+j < 0 || y+j >= map.ysize) continue;
    for (i = -4; i <= 4; i++) {
      xx = map_adjust_x(x+i);
      n = i * i + j * j;
      if (n <= 5) {
        if (minimap[xx][y+j] < 0) minimap[xx][y+j]++;
        else minimap[xx][y+j] = 0;
      } else if (n <= 20) {
        if (minimap[xx][y+j] > 0) minimap[xx][y+j] = 0;
      }
    }
  }
}    

void add_city_to_minimap(int x, int y)
{
  int i, j, n, xx;
  
  freelog(LOG_DEBUG, "Adding (%d, %d) to minimap.", x, y);
  for (j = -4; j <= 4; j++) {
    if (y+j < 0 || y+j >= map.ysize) continue;
    for (i = -4; i <= 4; i++) {
      xx = map_adjust_x(x+i);
      n = i * i + j * j;
      if (n <= 5) {
        if (minimap[xx][y+j] < 0) minimap[xx][y+j]--;
        else minimap[xx][y+j] = -1;
      } else if (n <= 20) {
        if (minimap[xx][y+j] > 0) minimap[xx][y+j] = 0;
      }
    }
  }
}    

void locally_zero_minimap(int x, int y)
{
  int i, j, n, xx;
  for (j = -2; j <= 2; j++) {
    if (y+j < 0 || y+j >= map.ysize) continue;
    for (i = -2; i <= 2; i++) {
      xx = map_adjust_x(x+i);
      n = i * i + j * j;
      if (n <= 5) {
        if (minimap[xx][y+j] > 0) minimap[xx][y+j] = 0;
      }
    }
  }
}  

int city_desirability(struct player *pplayer, int x, int y)
{ /* this whole funct assumes G_REP^H^H^HDEMOCRACY -- Syela */
  int taken[5][5], food[5][5], shield[5][5], trade[5][5];
  int irrig[5][5], mine[5][5], road[5][5];
  int i, j, f, n = 0;
  int worst, b2, best, ii, jj, val, cur;
  int d = 0;
  int a, i0, j0; /* need some temp variables */
  int temp=0, tmp=0;
  int debug = 0;
  int g = 1;
  struct tile *ptile;
  int con, con2;
  int har, t, sh;
  struct tile_type *ptype;
  struct city *pcity;
  int db;

  if (is_square_threatened(pplayer, x, y))
    return 0;
  
  ptile = map_get_tile(x, y);
  if (ptile->terrain == T_OCEAN) return(0);
  pcity = map_get_city(x, y);
  if (pcity && pcity->size > 7) return(0);
  if (!pcity && minimap[x][y] < 0) return(0);
  if (!pcity && minimap[x][y] > 0) return(minimap[x][y]);

  har = is_terrain_near_tile(x, y, T_OCEAN);

  sh = SHIELD_WEIGHTING * MORT;
  t = TRADE_WEIGHTING * MORT; /* assuming DEMOCRACY now, much easier! - Syela */

  con = ptile->continent;

/* not worth the computations AFAICT -- Syela
   wrapped anyway in case it comes back -- dwp
  if (improvement_variant(B_PYRAMIDS)==0) {     
    city_list_iterate(pplayer->cities, acity) 
      if (city_got_building(acity, B_PYRAMIDS)) g++;
    city_list_iterate_end;
  }
*/

  memset(taken, 0, sizeof(taken));
  memset(food, 0, sizeof(food));
  memset(shield, 0, sizeof(shield));
  memset(trade, 0, sizeof(trade));
  memset(irrig, 0, sizeof(irrig));
  memset(mine, 0, sizeof(mine));
  memset(road, 0, sizeof(road));

  city_map_iterate(i, j) {
    if ((minimap[map_adjust_x(x+i-2)][map_adjust_y(y+j-2)] >= 0 && !pcity) ||
       (pcity && get_worker_city(pcity, i, j) == C_TILE_EMPTY)) {
      ptile = map_get_tile(x+i-2, y+j-2);
      con2 = ptile->continent;
      ptype = get_tile_type(ptile->terrain);
      food[i][j] = (get_tile_food_base(ptile) - 2) * MORT;
      if (i == 2 && j == 2) food[i][j] += 2 * MORT;
      if (ptype->irrigation_result == ptile->terrain && con2 == con) {
        if (ptile->special&S_IRRIGATION || (i == 2 && j == 2))
	  irrig[i][j] = MORT * ptype->irrigation_food_incr;
        else if (is_water_adjacent_to_tile(x+i-2, y+j-2) &&
                 ptile->terrain != T_HILLS)
	  irrig[i][j] = MORT * ptype->irrigation_food_incr - 9; /* KLUGE */
/* all of these kluges are hardcoded amortize calls to save much CPU use -- Syela */
      } else if (ptile->terrain == T_OCEAN && har) food[i][j] += MORT; /* harbor */
      shield[i][j] = get_tile_shield_base(ptile) * sh;
      if (i == 2 && j == 2 && !shield[i][j]) shield[i][j] = sh;
      if (ptile->terrain == T_OCEAN && har) shield[i][j] += sh;
/* above line is not sufficiently tested.  AI was building on shores, but not
as far out in the ocean as possible, which limits growth in the very long
term (after SEWER).  These cities will all eventually have OFFSHORE, and
I need to acknowledge that.  I probably shouldn't treat it as free, but
that's the easiest, and I doubt pathological behavior will result. -- Syela */
      if (ptile->special&S_MINE)
	mine[i][j] = sh * ptype->mining_shield_incr;
      else if (ptile->terrain == T_HILLS && con2 == con)
	mine[i][j] = sh * ptype->mining_shield_incr - 300; /* KLUGE */
      trade[i][j] = get_tile_trade_base(ptile) * t;
      if (ptype->road_trade_incr > 0) {
        if (ptile->special&S_ROAD || (i == 2 && j == 2))
	  road[i][j] = t * ptype->road_trade_incr;
        else if (con2 == con)
          road[i][j] = t * ptype->road_trade_incr - 70; /* KLUGE */ /* actualy exactly 70 1/2 */
      }
      if (trade[i][j]) trade[i][j] += t;
      else if (road[i][j]) road[i][j] += t;
    }
  }

  if (pcity) { /* quick-n-dirty immigration routine -- Syela */
    n = pcity->size;
    best = 0; ii = 0; jj = 0; /* blame -Wall -Werror for these */
    city_map_iterate(i, j) {
      cur = (food[i][j]) * food_weighting(n) + /* ignoring irrig on purpose */
            (shield[i][j] + mine[i][j]) +
            (trade[i][j] + road[i][j]);
      if (cur > best && (i != 2 || j != 2)) { best = cur; ii = i; jj = j; }
    }
    if (!best) return(0);
    val = (shield[ii][jj] + mine[ii][jj]) +
          (food[ii][jj] + irrig[ii][jj]) * FOOD_WEIGHTING + /* seems to be needed */
          (trade[ii][jj] + road[ii][jj]);
    val -= amortize(40 * SHIELD_WEIGHTING + (50 - 20 * g) * FOOD_WEIGHTING, 12);
    /* 12 is arbitrary; need deterrent to represent loss
       of a settlers -- Syela */
    freelog(LOG_DEBUG, "Desire to immigrate to %s = %d -> %d",
		  pcity->name, val, (val * 100) / MORT / 70);
    return(val);
  }

  f = food[2][2] + irrig[2][2];
  if (!f) return(0); /* no starving cities, thank you! -- Syela */
  val = f * FOOD_WEIGHTING + /* this needs to be here, strange as it seems */
          (shield[2][2] + mine[2][2]) +
          (trade[2][2] + road[2][2]);
  taken[2][2]++;
  /* val is mort times the real value */
  /* treating harbor as free to approximate advantage of
     building boats. -- Syela */
  db = get_tile_type(map_get_terrain(x, y))->defense_bonus;
  if (map_get_special(x, y) & S_RIVER)
    db += (db * terrain_control.river_defense_bonus) / 100;
  val += (4 * db - 40) * SHIELD_WEIGHTING;
  /* don't build cities in danger!! FIX! -- Syela */
  val += 8 * MORT; /* one science per city */
  
  if (debug) freelog(LOG_DEBUG, "City value (%d, %d) = %d, har = %d, f = %d",
		     x, y, val, har, f);

  for (n = 1; n <= 20 && f > 0; n++) {
    for (a = 1; a; a--) {
      best = 0; worst = -1; b2 = 0; i0 = 0; j0 = 0; ii = 0; jj = 0;
      city_map_iterate(i, j) {
        cur = (food[i][j]) * food_weighting(n) + /* ignoring irrig on purpose */
              (shield[i][j] + mine[i][j]) +
              (trade[i][j] + road[i][j]);
        if (!taken[i][j]) {
          if (cur > best) { b2 = best; best = cur; ii = i; jj = j; }
          else if (cur > b2) b2 = cur;
        } else if (i != 2 || j != 2) {
          if (cur < worst || worst < 0) { worst = cur; i0 = i; j0 = j; }
        }
      }
      if (!best) break;
      cur = amortize((shield[ii][jj] + mine[ii][jj]) +
            (food[ii][jj] + irrig[ii][jj]) * FOOD_WEIGHTING + /* seems to be needed */
            (trade[ii][jj] + road[ii][jj]), d);
      f += food[ii][jj] + irrig[ii][jj];
      if (cur > 0) val += cur;
      taken[ii][jj]++;
      
      if (debug) {
	freelog(LOG_DEBUG,
		"Value of (%d, %d) = %d food = %d, type = %s, n = %d, d = %d",
		ii, jj, cur, (food[ii][jj] + irrig[ii][jj]),
		get_tile_type(map_get_tile(x + ii - 2,
					   y + jj - 2)->terrain)->terrain_name,
		n, d);
      }

/* I hoped to avoid the following, but it seems I can't take ANY shortcuts
in this unspeakable routine, so here comes even more CPU usage in order to
get this EXACTLY right instead of just reasonably close. -- Syela */
      if (worst < b2 && worst >= 0) {
        cur = amortize((shield[i0][j0] + mine[i0][j0]) +
              (trade[i0][j0] + road[i0][j0]), d);
        f -= (food[i0][j0] + irrig[i0][j0]);
        val -= cur;
        taken[i0][j0]--;
        a++;
	
	if (debug) {
	  freelog(LOG_DEBUG, "REJECTING Value of (%d, %d) = %d"
		  " food = %d, type = %s, n = %d, d = %d",
		  i0, j0, cur, (food[i0][j0] + irrig[i0][j0]),
		  get_tile_type(map_get_tile(x + i0 - 2,
					     y + j0 - 2)->terrain)->terrain_name,
		  n, d);
	}
      }
    }
    if (!best) break;
    if (f > 0) d += (game.foodbox * MORT * n + (f*g) - 1) / (f*g);
    if (n == 4) {
      val -= amortize(40 * SHIELD_WEIGHTING + (50 - 20 * g) * FOOD_WEIGHTING, d); /* lers */
      temp = amortize(40 * SHIELD_WEIGHTING, d); /* temple */
      tmp = val;
    }
  } /* end while */
  if (n > 4) {
    if (val - temp > tmp) val -= temp;
    else val = tmp;
  }
  val -= 110 * SHIELD_WEIGHTING; /* WAG: walls, defenders */
  minimap[x][y] = val;

  if (debug) {
    freelog(LOG_DEBUG, "Total value of (%d, %d) [%d workers] = %d",
	    x, y, n, val);
  }
  return(val);
}

/**************************************************************************
...
**************************************************************************/
void ai_manage_settler(struct player *pplayer, struct unit *punit)
{
  punit->ai.control = 1;
  if (punit->ai.ai_role == AIUNIT_NONE) /* if BUILD_CITY must remain BUILD_CITY */
    punit->ai.ai_role = AIUNIT_AUTO_SETTLER;
/* gonna handle city-building in the auto-settler routine -- Syela */
  return;
}

/*************************************************************************
  returns dy according to the wrap rules
**************************************************************************/
int make_dy(int y1, int y2)
{
  int dy=y2-y1;
  if (dy<0) dy=-dy;
  return dy;
}

/*************************************************************************
  returns dx according to the wrap rules
**************************************************************************/
int make_dx(int x1, int x2)
{
  int tmp;
  x1=map_adjust_x(x1);
  x2=map_adjust_x(x2);
  if(x1>x2)
    tmp=x1, x1=x2, x2=tmp;

  return MIN(x2-x1, map.xsize-x2+x1);
}

/**************************************************************************
 return 1 if there is already a unit on this square or one destined for it 
 (via goto)
**************************************************************************/
int is_already_assigned(struct unit *myunit, struct player *pplayer, int x, int y)
{ 
  x=map_adjust_x(x);
  y=map_adjust_y(y);
  if (same_pos(myunit->x, myunit->y, x, y) || 
      same_pos(myunit->goto_dest_x, myunit->goto_dest_y, x, y)) {
/* I'm still not sure this is exactly right -- Syela */
    unit_list_iterate(map_get_tile(x, y)->units, punit)
      if (myunit==punit) continue;
      if (punit->owner!=pplayer->player_no)
        return 1;
      if (unit_flag(punit->type, F_SETTLERS) && unit_flag(myunit->type, F_SETTLERS))
        return 1;
    unit_list_iterate_end;
    return 0;
  }
  return(map_get_tile(x, y)->assigned & (1<<pplayer->player_no));
}

#ifdef UNUSED
int old_is_already_assigned(struct unit *myunit, struct player *pplayer, int x, int y)
{
  x=map_adjust_x(x);
  y=map_adjust_y(y);
#ifdef RIDICULOUS
  if (same_pos(myunit->x, myunit->y, x, y))
    return 0;
#endif
  unit_list_iterate(map_get_tile(x, y)->units, punit) {
    if (myunit==punit) continue;
    if (punit->owner!=pplayer->player_no)
      return 1;
    if (unit_flag(punit->type, F_SETTLERS) && unit_flag(myunit->type, F_SETTLERS))
      return 1;
  }
  unit_list_iterate_end;
#ifndef RIDICULOUS
  if (same_pos(myunit->x, myunit->y, x, y))
    return 0;
#endif
  unit_list_iterate(pplayer->units, punit) {
    if (myunit==punit) continue;
    if (punit->owner!=pplayer->player_no)
      return 1;
    if (same_pos(punit->goto_dest_x, punit->goto_dest_y, x, y) && unit_flag(punit->type, F_SETTLERS) && unit_flag(myunit->type,F_SETTLERS) && punit->activity==ACTIVITY_GOTO)
      return 1;
    if (same_pos(punit->goto_dest_x, punit->goto_dest_y, x, y) && !unit_flag(myunit->type, F_SETTLERS) && punit->activity==ACTIVITY_GOTO)
      return 1;
  }
  unit_list_iterate_end;
  return 0;
}
#endif

/* all of the benefit and ai_calc routines rewritten by Syela */
/* to conform with city_tile_value and related calculations elsewhere */
/* all of these functions are VERY CPU-inefficient and are being cached */
/* I don't really want to rewrite them and possibly screw them up. */
/* The cache should keep the CPU increase linear instead of quadratic. -- Syela */

int ai_calc_pollution(struct city *pcity, struct player *pplayer, int i, int j)
{
  int x, y, m;
  x = pcity->x + i - 2; y = pcity->y + j - 2;
  if (!(map_get_special(x, y) & S_POLLUTION)) return(-1);
  map_clear_special(x, y, S_POLLUTION);
  m = city_tile_value(pcity, i, j, 0, 0);
  map_set_special(x, y, S_POLLUTION);
  return(m);
}

static int is_wet(struct player *pplayer, int x, int y)
{
  if (!map_get_known(x, y, pplayer) && !pplayer->ai.control) return 0;
  if (map_get_terrain(x,y) == T_OCEAN || map_get_terrain(x,y) == T_RIVER ||
      map_get_special(x,y)&S_RIVER || map_get_special(x,y)&S_IRRIGATION) return 1;
  return 0;
}

int ai_calc_irrigate(struct city *pcity, struct player *pplayer, int i, int j)
{
  int m, x = pcity->x + i - 2, y = pcity->y + j - 2;
  struct tile *ptile = map_get_tile(x, y);
  enum tile_terrain_type t = ptile->terrain;
  struct tile_type *type = get_tile_type(t);
  int s = ptile->special;

  if (ptile->terrain != type->irrigation_result &&
      type->irrigation_result != T_LAST) { /* EXPERIMENTAL 980905 -- Syela */
    ptile->terrain = type->irrigation_result;
    map_clear_special(x, y, S_MINE);
    m = city_tile_value(pcity, i, j, 0, 0);
    ptile->terrain = t;
    ptile->special = s;
    return(m);
  } else if((ptile->terrain==type->irrigation_result &&
     !(ptile->special&S_IRRIGATION) &&
     !(ptile->special&S_MINE) && !(ptile->city) &&
     (is_wet(pplayer,x,y) || is_wet(pplayer,x,y-1) || is_wet(pplayer,x,y+1) ||
     is_wet(pplayer,x-1,y) || is_wet(pplayer,x+1,y)))) {
    map_set_special(x, y, S_IRRIGATION);
    m = city_tile_value(pcity, i, j, 0, 0);
    map_clear_special(x, y, S_IRRIGATION);
    return(m);
  } else if((ptile->terrain==type->irrigation_result &&
     (ptile->special&S_IRRIGATION) && !(ptile->special&S_FARMLAND) &&
     player_knows_improvement_tech(pplayer, B_SUPERMARKET) &&
     !(ptile->special&S_MINE) && !(ptile->city) &&
     (is_wet(pplayer,x,y) || is_wet(pplayer,x,y-1) || is_wet(pplayer,x,y+1) ||
     is_wet(pplayer,x-1,y) || is_wet(pplayer,x+1,y)))) {
    map_set_special(x, y, S_FARMLAND);
    m = city_tile_value(pcity, i, j, 0, 0);
    map_clear_special(x, y, S_FARMLAND);
    return(m);
  } else return(-1);
}

int ai_calc_mine(struct city *pcity, struct player *pplayer, int i, int j)
{
  int m, x = pcity->x + i - 2, y = pcity->y + j - 2;
  struct tile *ptile = map_get_tile(x, y);
#if 0
  enum tile_terrain_type t = ptile->terrain;
  struct tile_type *type = get_tile_type(t);
  int s = ptile->special;

  if (ptile->terrain != type->mining_result &&
      type->mining_result != T_LAST) { /* EXPERIMENTAL 980905 -- Syela */
    ptile->terrain = type->mining_result;
    map_clear_special(x, y, S_FARMLAND);
    map_clear_special(x, y, S_IRRIGATION);
    m = city_tile_value(pcity, i, j, 0, 0);
    ptile->terrain = t;
    ptile->special = s;
    return(m);
  } else 
#endif
  if ((ptile->terrain == T_HILLS || ptile->terrain == T_MOUNTAINS) &&
      !(ptile->special&S_IRRIGATION) && !(ptile->special&S_MINE)) {
    map_set_special(x, y, S_MINE);
    m = city_tile_value(pcity, i, j, 0, 0);
    map_clear_special(x, y, S_MINE);
    return(m);
  } else return(-1);
}

int ai_calc_transform(struct city *pcity, struct player *pplayer, int i, int j)
{
  int m, x = pcity->x + i - 2, y = pcity->y + j - 2;
  struct tile *ptile = map_get_tile(x, y);

  enum tile_terrain_type t = ptile->terrain;
  struct tile_type *type = get_tile_type(t);
  int s = ptile->special;
  enum tile_terrain_type r = type->transform_result;
  
  if ((t == T_ARCTIC || t == T_DESERT || t == T_JUNGLE || t == T_SWAMP  || 
       t == T_TUNDRA || t == T_MOUNTAINS) && r != T_LAST) {
    ptile->terrain = r;

    if (get_tile_type(r)->mining_result != r) 
      map_clear_special(x, y, S_MINE);

    if (get_tile_type(r)->irrigation_result != r) {
      map_clear_special(x, y, S_FARMLAND);
      map_clear_special(x, y, S_IRRIGATION);
    }

    m = city_tile_value(pcity, i, j, 0, 0);
    ptile->terrain = t;
    ptile->special = s;
    return(m);
  } else return(-1);
}

static int road_bonus(int x, int y, int spc)
{
  int m = 0, k;
  int rd[12], te[12];
  int ii[12] = { -1, 0, 1, -1, 1, -1, 0, 1, 0, -2, 2, 0 };
  int jj[12] = { -1, -1, -1, 0, 0, 1, 1, 1, -2, 0, 0, 2 };
  struct tile *ptile;
  for (k = 0; k < 12; k++) {
    ptile = map_get_tile(x + ii[k], y + jj[k]);
    rd[k] = ptile->special&spc;
    te[k] = (ptile->terrain == T_MOUNTAINS || ptile->terrain == T_OCEAN);
    if (!rd[k]) {
      unit_list_iterate(ptile->units, punit)
        if (punit->activity == ACTIVITY_ROAD || punit->activity == ACTIVITY_RAILROAD)
          rd[k] = spc;
      unit_list_iterate_end;
    }
  }

  if (rd[0] && !rd[1] && !rd[3] && (!rd[2] || !rd[8]) &&
      (!te[2] || !te[4] || !te[7] || !te[6] || !te[5])) m++;
  if (rd[2] && !rd[1] && !rd[4] && (!rd[7] || !rd[10]) &&
      (!te[0] || !te[3] || !te[7] || !te[6] || !te[5])) m++;
  if (rd[5] && !rd[6] && !rd[3] && (!rd[5] || !rd[11]) &&
      (!te[2] || !te[4] || !te[7] || !te[1] || !te[0])) m++;
  if (rd[7] && !rd[6] && !rd[4] && (!rd[0] || !rd[9]) &&
      (!te[2] || !te[3] || !te[0] || !te[1] || !te[5])) m++;

  if (rd[1] && !rd[4] && !rd[3] && (!te[5] || !te[6] || !te[7])) m++;
  if (rd[3] && !rd[1] && !rd[6] && (!te[2] || !te[4] || !te[7])) m++;
  if (rd[4] && !rd[1] && !rd[6] && (!te[0] || !te[3] || !te[5])) m++;
  if (rd[6] && !rd[4] && !rd[3] && (!te[0] || !te[1] || !te[2])) m++;
  return(m);
}

int ai_calc_road(struct city *pcity, struct player *pplayer, int i, int j)
{
  int x, y, m;
  struct tile *ptile;
  x = pcity->x + i - 2; y = pcity->y + j - 2;
  ptile = map_get_tile(x, y);
  if (ptile->terrain != T_OCEAN &&
      (((ptile->terrain != T_RIVER) && !(ptile->special&S_RIVER)) ||
       player_knows_techs_with_flag(pplayer, TF_BRIDGE)) &&
      !(ptile->special&S_ROAD)) {
    ptile->special|=S_ROAD; /* have to do this to avoid reset_move_costs -- Syela */
    m = city_tile_value(pcity, i, j, 0, 0);
    ptile->special&=~S_ROAD;
    return(m);
  } else return(-1);
}

int ai_calc_railroad(struct city *pcity, struct player *pplayer, int i, int j)
{
  int x, y, m;
  struct tile *ptile;
  enum tile_special_type spe_sav;
  
  x = pcity->x + i - 2; y = pcity->y + j - 2;
  ptile = map_get_tile(x, y);
  if (ptile->terrain != T_OCEAN &&
      player_knows_techs_with_flag(pplayer, TF_RAILROAD) &&
      !(ptile->special&S_RAILROAD)) {
    spe_sav = ptile->special;
    ptile->special|=(S_ROAD | S_RAILROAD);
    m = city_tile_value(pcity, i, j, 0, 0);
    ptile->special = spe_sav;
    return(m);
  } else return(-1);
  /* bonuses for adjacent railroad tiles */
}

/*************************************************************************
  return how good this square is for a new city.
**************************************************************************/
int is_ok_city_spot(int x, int y)
{
  int dx, dy, i;

  switch (map_get_terrain(x,y)) {
  case T_OCEAN:
  case T_UNKNOWN:
  case T_MOUNTAINS:
  case T_FOREST:
  case T_HILLS:
  case T_ARCTIC:
  case T_JUNGLE:
  case T_SWAMP:
  case T_TUNDRA:
  case T_LAST:
    return 0;
  case T_DESERT:
    if
    (
     !((map_get_tile(x, y))->special&S_SPECIAL_1)
    &&
     !((map_get_tile(x, y))->special&S_SPECIAL_2)
    )
      return 0;
  case T_GRASSLAND:
  case T_PLAINS:
  case T_RIVER:
    break;
  default:
    break;
  }
  for (i = 0; i < game.nplayers; i++) {
    city_list_iterate(game.players[i].cities, pcity) {
      if (map_distance(x, y, pcity->x, pcity->y)<=8) {
        dx=make_dx(pcity->x, x);
        dy=make_dy(pcity->y, y);
        if (dx<=5 && dy<5)
          return 0;
        if (dx<5 && dy<=5)
          return 0;
      }
    }
    city_list_iterate_end;
  }
  return 1;
}

/*************************************************************************
  return the city if any that is in range of this square.
**************************************************************************/
int in_city_radius(int x, int y)
{
  int i, j;
  city_map_iterate(i, j) {
    if (map_get_city(x+i-2, y+j-2)) return 1;
  }
  return 0;
}

/**************************************************************************
  simply puts the settler unit into goto
**************************************************************************/
int auto_settler_do_goto(struct player *pplayer, struct unit *punit, int x, int y)
{
  punit->goto_dest_x=map_adjust_x(x);
  punit->goto_dest_y=map_adjust_y(y);
  set_unit_activity(punit, ACTIVITY_GOTO);
  send_unit_info(0, punit, 0);
  do_unit_goto(pplayer, punit);
  return 1;
}

int find_boat(struct player *pplayer, int *x, int *y, int cap)
{ /* this function uses the current warmap, whatever it may hold */
/* unit is no longer an arg!  we just trust the map! -- Syela */
  int best = 22, id = 0; /* arbitrary maximum distance, I will admit! */
  unit_list_iterate(pplayer->units, aunit)
    if (is_ground_units_transport(aunit)) {
      if (warmap.cost[aunit->x][aunit->y] < best &&
          (warmap.cost[aunit->x][aunit->y] == 0 ||
          is_transporter_with_free_space(pplayer, aunit->x, aunit->y) >= cap)) {
        id = aunit->id;
        best = warmap.cost[aunit->x][aunit->y];
        *x = aunit->x;
        *y = aunit->y;
      }
    }
  unit_list_iterate_end;
  if (id) return(id);
#ifdef ALLOW_VIRTUAL_BOATS
  city_list_iterate(pplayer->cities, pcity)
    if (pcity->is_building_unit &&
        unit_types[pcity->currently_building].transport_capacity &&
        !unit_flag(pcity->currently_building, F_CARRIER) &&
	!unit_flag(pcity->currently_building, F_SUBMARINE)) {
      if (warmap.cost[pcity->x][pcity->y] < best) {
        id = pcity->id;
        best = warmap.cost[pcity->x][pcity->y];
        *x = pcity->x;
        *y = pcity->y;
      }
    }
  city_list_iterate_end;
#endif
  return(id);
}

struct unit *other_passengers(struct unit *punit)
{
  unit_list_iterate(map_get_tile(punit->x, punit->y)->units, aunit)
    if (is_ground_unit(aunit) && aunit != punit) return aunit;
  unit_list_iterate_end;
  return 0;
}

/********************************************************************
  find some work for the settler
*********************************************************************/
int auto_settler_findwork(struct player *pplayer, struct unit *punit)
{
  struct city *mycity = map_get_city(punit->x, punit->y);
  int nav_known       = (get_invention(pplayer, game.rtech.nav) == TECH_KNOWN);
  int ucont           = map_get_continent(punit->x, punit->y);
  int mv_rate         = unit_types[punit->type].move_rate;
  int player_num      = pplayer->player_no;
  int save_id         = punit->id;              /* in case unit dies */
  
  int gx,gy;			/* x,y of target (goto) square */
  int mv_turns;			/* estimated turns to move to target square */
  int oldv;			/* current value of consideration tile */
  int newv;			/* upgraded value of consideration tile */
  int best_newv = 0;		/* newv of best target so far, all cities */
  int best_oldv = 9999;		/* oldv of best target so far; compared if
				   newv==best_newv; not initialized to zero,
				   so that newv=0 activities are not chosen */
  int best_act = 0;		/* ACTIVITY_ of best target so far */
  int food_upkeep;		/* upkeep food value for single settler  */
  int food_cost;		/* estimated food cost to produce settler */
  
  int boatid, bx, by;		/* as returned by find_boat */
  struct unit *ferryboat;	/* if non-null, boatid boat at unit's x,y */
  
  int x, y, i, j;
  int a, b=0, d=0;
  int worst;
  int save_newv;		/* for debugging only */
  
  struct ai_choice choice;	/* for nav want only */

  choice.type = 1;
  choice.want = 0;		/* will change as needed */
  choice.choice = get_role_unit(L_FERRYBOAT, -1);
  /* was U_CARAVEL (first non-trireme boat?) but gets set
   * properly later anyway */

  if (punit->id) food_cost = 30;
  else {
    if (mycity->size == 1) food_cost = 20;
    else food_cost = 40 * (mycity->size - 1) / mycity->size;
    if (city_got_effect(mycity, B_GRANARY)) food_cost -= 20;
  }
  food_upkeep = utype_food_cost(get_unit_type(punit->type),
				get_gov_pplayer(pplayer));
  if (punit->id && !punit->homecity) food_upkeep = 0; /* thanks, Peter */

  /** First find the best square to upgrade,
   ** results in: gx, gy, best_oldv, best_newv, best_act */
  
  gx=-1;
  gy=-1;
  /* iterating over the whole map is just ridiculous.  let's only look at
     our own cities.  The old method wasted billions of CPU cycles and led to
     AI settlers improving enemy cities.  arguably should include city_spot  */
  generate_warmap(mycity, punit);
  city_list_iterate(pplayer->cities, pcity)
    worst = worst_worker_tile_value(pcity);
    /* try to work near the city */
    city_map_iterate_outwards(i, j)
      if (get_worker_city(pcity, i, j) == C_TILE_UNAVAILABLE) continue;
      x = map_adjust_x(pcity->x + i - 2);
      y = pcity->y + j - 2;	/* No adjust! */
      if ((y >= 0 && y < map.ysize)
	  && map_get_continent(x, y) == ucont
	  && warmap.cost[x][y] <= THRESHOLD * mv_rate
	  && (territory[x][y]&(1<<player_num))
				/* pretty good, hope it's enough! -- Syela */
          && !is_already_assigned(punit, pplayer, x, y)) {
	/* calling is_already_assigned once instead of four times
	   for obvious reasons;  structure is much the same as it once
	   was but subroutines are not -- Syela	*/
	mv_turns = (warmap.cost[x][y]) / mv_rate;
        oldv = city_tile_value(pcity, i, j, 0, 0);
        if (worst > oldv && (i != 2 || j != 2)) oldv = worst;
	/* perhaps I should just subtract worst rather than oldv but
	   I prefer this -- Syela */

/* Christopher Neufeld provided a rather crufty patch to make settlers
needlessly idle less often.  I didn't want to apply it, but the ideas
for the -1 return values in ai_calc_*, the !irrigation in calc_mine,
and the prioritization of useless (b <= 0) activities are his. -- Syela
*/

	newv = pcity->ai.irrigate[i][j];
        if (newv >= oldv) { /* worth evaluating */
          b = MAX((newv - oldv)*64, MORT);
          d = (map_build_irrigation_time(x, y) * 3 + mv_rate - 1) / mv_rate
	    + mv_turns;
          a = amortize(b, d);
          newv = ((a * b) / (MAX(1, b - a)))/64;
        } else newv = 0;
        if ((newv > best_newv || (newv == best_newv && oldv > best_oldv))
	    && ai_fuzzy(pplayer, 1)) {
	  freelog(LOG_DEBUG,
		  "Replacing (%d, %d) = %d with (%d, %d) I=%d d=%d, b=%d",
		  gx, gy, best_newv, x, y, newv, d, b);
	  best_act = ACTIVITY_IRRIGATE;
	  best_newv=newv; gx=x; gy=y; best_oldv=oldv;
        }

	newv = pcity->ai.transform[i][j];
        if (newv >= oldv) { /* worth evaluating */
          b = MAX((newv - oldv)*64, MORT);
          d = (map_transform_time(x, y) * 3 + mv_rate - 1) / mv_rate
	    + mv_turns;
          a = amortize(b, d);
          newv = ((a * b) / (MAX(1, b - a)))/64;
        } else newv = 0;
        if ((newv > best_newv || (newv == best_newv && oldv > best_oldv))
	    && ai_fuzzy(pplayer, 1)) {
	  freelog(LOG_DEBUG,
		  "Replacing (%d, %d) = %d with (%d, %d) I=%d d=%d, b=%d",
		  gx, gy, best_newv, x, y, newv, d, b);
	  best_act = ACTIVITY_TRANSFORM;
	  best_newv=newv; gx=x; gy=y; best_oldv=oldv;
        }

	newv = pcity->ai.mine[i][j];
        if (newv >= oldv) {
          b = MAX((newv - oldv)*64, MORT);
          d = (map_build_mine_time(x, y) * 3 + mv_rate - 1) / mv_rate
	    + mv_turns;
          a = amortize(b, d);
          newv = ((a * b) / (MAX(1, b - a)))/64;
        } else newv = 0;
	if ((newv > best_newv || (newv == best_newv && oldv > best_oldv))
	    && ai_fuzzy(pplayer, 1)) {
	  freelog(LOG_DEBUG,
		  "Replacing (%d, %d) = %d with (%d, %d) M=%d d=%d, b=%d",
		  gx, gy, best_newv, x, y, newv, d, b);
	  best_act = ACTIVITY_MINE;
	  best_newv=newv; gx=x; gy=y; best_oldv=oldv;
        }

        if (!(map_get_tile(x,y)->special&S_ROAD)) {
  	  newv = pcity->ai.road[i][j];
          if (newv >= 0) {    
            newv = MAX(newv, oldv) + road_bonus(x, y, S_ROAD) * 8;
	    /* guessing about the weighting based on unit upkeeps;
	       (* 11) was too high! -- Syela */
            b = MAX((newv - oldv)*64, MORT);
            d = (map_build_road_time(x, y) * 3 + 3 + mv_rate - 1) / mv_rate
	      + mv_turns; /* uniquely weird! */
            a = amortize(b, d);
            newv = ((a * b) / (MAX(1, b - a)))/64;
          } else newv = 0;
          if ((newv > best_newv || (newv == best_newv && oldv > best_oldv))
	      && ai_fuzzy(pplayer, 1)) {
	    freelog(LOG_DEBUG, "Replacing (%d, %d) = %d"
		    " with (%d, %d) R=%d d=%d, b=%d",
		    gx, gy, best_newv, x, y, newv, d, b);
	    best_act = ACTIVITY_ROAD;
	    best_newv=newv; gx=x; gy=y; best_oldv=oldv;
          }

	  newv = pcity->ai.railroad[i][j];
          if (newv >= 0) {    
            newv = MAX(newv, oldv) + road_bonus(x, y, S_RAILROAD) * 4;
            b = MAX((newv - oldv)*64, MORT);
            d = (3 * 3 + 3 * map_build_road_time(x,y) + 3 + mv_rate - 1)
	         / mv_rate + mv_turns;
            a = amortize(b, d);
            newv = ((a * b) / (MAX(1, b - a)))/64;
          } else newv = 0;
          if ((newv > best_newv || (newv == best_newv && oldv > best_oldv))
	      && ai_fuzzy(pplayer, 1)) {
  	    best_act = ACTIVITY_ROAD;
	    best_newv=newv; gx=x; gy=y; best_oldv=oldv;
          }
        } else {
	  newv = pcity->ai.railroad[i][j];
          if (newv >= 0) {    
            newv = MAX(newv, oldv) + road_bonus(x, y, S_RAILROAD) * 4;
            b = MAX((newv - oldv)*64, MORT);
            d = (3 * 3 + mv_rate - 1) / mv_rate + mv_turns;
            a = amortize(b, d);
            newv = ((a * b) / (MAX(1, b - a)))/64;
          } else newv = 0;
          if ((newv > best_newv || (newv == best_newv && oldv > best_oldv))
	      && ai_fuzzy(pplayer, 1)) {
  	    best_act = ACTIVITY_RAILROAD;
	    best_newv=newv; gx=x; gy=y; best_oldv=oldv;
          }
        } /* end S_ROAD else */

	newv = pcity->ai.detox[i][j];
        if (newv >= 0) {
          newv = MAX(newv, oldv) + pplayer->ai.warmth;
          b = MAX((newv - oldv)*64, MORT);
          d = (3 * 3 + mv_turns + mv_rate - 1) / mv_rate + mv_turns;
          a = amortize(b, d);
          newv = ((a * b) / (MAX(1, b - a)))/64;
        } else newv = 0;
        if (newv > best_newv || (newv == best_newv && oldv > best_oldv)) {
	  best_act = ACTIVITY_POLLUTION;
	  best_newv=newv; gx=x; gy=y; best_oldv=oldv;
        }
      } /* end if we are a legal destination */
    city_map_iterate_outwards_end;
  city_list_iterate_end;

  /** Found the best square to upgrade, have gx, gy, best_newv, best_act **/

  best_newv = (best_newv - food_upkeep * FOOD_WEIGHTING) * 100 /
                                                     (40 + food_cost);
  if (best_newv < 0)
    best_newv = 0; /* Bad Things happen without this line! :( -- Syela */

  if (best_newv > 0) {
    freelog(LOG_DEBUG,
	    "Settler %d@(%d,%d) wants to %d at (%d,%d) with desire %d",
	    punit->id, punit->x, punit->y, best_act, gx, gy, best_newv);
  }
  save_newv = best_newv;
  
  boatid = find_boat(pplayer, &bx, &by, 1); /* might need 2 for body */
  ferryboat = unit_list_find(&(map_get_tile(punit->x, punit->y)->units), boatid);
  if (ferryboat)
    really_generate_warmap(mycity, ferryboat, SEA_MOVING);

  /** Decide whether to build a new city:
   ** if so, modify: gx, gy, best_newv, best_act */
  
  if (pplayer->ai.control && 
		ai_fuzzy(pplayer,1)) { /* don't want to make cities otherwise */
    if (punit->ai.ai_role == AIUNIT_BUILD_CITY) {
      remove_city_from_minimap(punit->goto_dest_x, punit->goto_dest_y);
    }
    punit->ai.ai_role = AIUNIT_AUTO_SETTLER; /* here and not before! -- Syela */
    for (j = -11; j <= 11; j++) { /* hope this is far enough -- Syela */
      y = punit->y + j;
      if (y < 0 || y >= map.ysize) continue;
      for (i = -11; i <= 11; i++) {
        int near = (MAX((MAX(i, -i)),(MAX(j, -j))));
        int w_virtual = 0;	/* I'm no entirely sure what this is --dwp */
	int mv_cost;
        x = map_adjust_x(punit->x + i);
        if (!is_already_assigned(punit, pplayer, x, y)
	    && map_get_terrain(x, y) != T_OCEAN
	    && (territory[x][y]&(1<<player_num))
				/* pretty good, hope it's enough! -- Syela */
            && (near < 8 || map_get_continent(x, y) != ucont)) {

	  /* potential target, calculate mv_cost: */
          if (ferryboat) {
	    /* already aboard ship, can use real warmap */
            if (!is_terrain_near_tile(x, y, T_OCEAN)) {
	      mv_cost = 9999;
	    } else {
	      mv_cost = warmap.seacost[x][y] * mv_rate /
		    unit_types[ferryboat->type].move_rate;
	    }
          } else if (!goto_is_sane(pplayer, punit, x, y, 1) ||
		     warmap.cost[x][y] > THRESHOLD * mv_rate) {
				/* for Rome->Carthage */
	    if (!is_terrain_near_tile(x, y, T_OCEAN)) {
	      mv_cost = 9999;
	    } else if (boatid) {
              if (!punit->id && mycity->id == boatid) w_virtual = 1;
              mv_cost = warmap.cost[bx][by] + real_map_distance(bx, by, x, y)
		   + mv_rate; 
            } else if (punit->id ||
		       !is_terrain_near_tile(mycity->x, mycity->y, T_OCEAN)) {
	      mv_cost = 9999;
	    } else {
              mv_cost = warmap.seacost[x][y] * mv_rate / 9;
	      /* this should be fresh; the only thing that could have
		 munged the seacost is the ferryboat code in
		 k_s_w/f_s_t_k, but only if find_boat succeeded */
              w_virtual = 1;
            }
          } else {
	    mv_cost = warmap.cost[x][y];
	  }
          d = mv_cost / mv_rate;
	  /* without this, the computer will go 6-7 tiles from X to
	     build a city at Y */
          d *= 2;
	  /* and then build its NEXT city halfway between X and Y. -- Syela */
          b = city_desirability(pplayer, x, y) * ai_fuzzy(pplayer,1);
          newv = amortize(b, d);
          
          b = (food_upkeep * FOOD_WEIGHTING) * MORT;
          if (map_get_continent(x, y) != ucont) b += SHIELD_WEIGHTING * MORT;
          newv -= (b - amortize(b, d));
	  /* deal with danger Real Soon Now! -- Syela */
	  /* newv is now the value over mort turns */
          newv = (newv * 100) / MORT / ((w_virtual ? 80 : 40) + food_cost);

          if (best_newv && map_get_city(x, y)) newv = 0;
	  
/* I added a line to discourage settling in existing cities, but it was
inadequate.  It may be true that in the short-term building infrastructure
on tiles that won't be worked for ages is not as useful as helping other
cities grow and then building engineers later, but this is short-sighted.
There was also a problem with workers suddenly coming onto unimproved tiles,
either through city growth or de-elvisization, settlers being built and
improving those tiles, and then immigrating shortly thereafter. -- Syela
*/
 	  if (newv>0 && pplayer->ai.expand!=100) {
 	    newv = (newv * pplayer->ai.expand) / 100;
 	  }
 
	  if (w_virtual) {
	    freelog(LOG_DEBUG, "%s: best_newv = %d, w_virtual = 1, newv = %d",
		    mycity->name, best_newv, newv);
	  }
	  
          if (map_get_continent(x, y) != ucont && !nav_known && near >= 8) {
            if (newv > choice.want && !punit->id) choice.want = newv;
	    freelog(LOG_DEBUG,
		    "%s@(%d, %d) city_des (%d, %d) = %d, newv = %d, d = %d", 
		    (punit->id ? unit_types[punit->type].name : mycity->name), 
		    punit->x, punit->y, x, y, b, newv, d);
          } else if (newv > best_newv) {
	    best_act = ACTIVITY_UNKNOWN; /* flag value */
	    best_newv = newv;
            if (w_virtual) {
              gx = -1; gy = -1;
            } else {
	      gx = x; gy = y;
            }
          }
        }
      }
    }
  }

  choice.want -= best_newv;
  if (choice.want > 0) ai_choose_ferryboat(pplayer, mycity, &choice);

#ifdef DEBUG
  if (best_newv != save_newv) {
    freelog(LOG_DEBUG,
	    "Settler %d@(%d,%d) wants to %d at (%d,%d) with desire %d",
	    punit->id, punit->x, punit->y, best_act, gx, gy, best_newv);
  }
  if (map_get_terrain(punit->x, punit->y) == T_OCEAN) {
    freelog(LOG_DEBUG, "Punit %d@(%d,%d) wants to %d at (%d,%d) with desire %d",
	    punit->id, punit->x, punit->y, best_act, gx, gy, best_newv);
  }
#endif
  
  /* I had the return here, but it led to stupidity where several engineers
     would be built to solve one problem.  Moving the return down will solve
     this. -- Syela */

  if (gx!=-1 && gy!=-1) {
      map_get_tile(gx, gy)->assigned =
        map_get_tile(gx, gy)->assigned | 1<<pplayer->player_no;
  } else if (pplayer->ai.control) { /* settler has no purpose */
    /* possibly add Gilligan's Island here */
    ;
  }

  /** If this is a virtual unit for assessing settler want: **/
  if (!punit->id) {
    /* has to be before we try to send_unit_info()! -- Syela */
    freelog(LOG_DEBUG,
	    "%s (%d, %d) settler-want = %d, task = %d, target = (%d, %d)",
	    mycity->name, mycity->x, mycity->y, best_newv,
	    best_act, gx, gy);
    if (gx < 0 && gy < 0) {
      return(0 - best_newv);
    } else {
      return(best_newv);
    }
  }

  /** We've now worked out what to do; go to it! **/
  if (gx!=-1 && gy!=-1) {
    if (best_act == ACTIVITY_UNKNOWN /* flag */) {
      punit->ai.ai_role = AIUNIT_BUILD_CITY;
      add_city_to_minimap(gx, gy);
    } else {
      punit->ai.ai_role = AIUNIT_AUTO_SETTLER;
    }
    if (!same_pos(gx, gy, punit->x, punit->y)) {
      if (!goto_is_sane(pplayer, punit, gx, gy, 1)
	  || (ferryboat
	      && goto_is_sane(pplayer, ferryboat, gx, gy, 1)
	      && (!is_tiles_adjacent(punit->x, punit->y, gx, gy)
		 || !could_unit_move_to_tile(punit, punit->x, punit->y,
					     gx, gy)))) {
        punit->ai.ferryboat = find_boat(pplayer, &x, &y, 1); /* might need 2 */
	freelog(LOG_DEBUG, "%d@(%d, %d): Looking for BOAT.",
		      punit->id, punit->x, punit->y);
        if (!same_pos(x, y, punit->x, punit->y)) {
          auto_settler_do_goto(pplayer, punit, x, y);
          if (!unit_list_find(&pplayer->units, save_id)) return(0); /* died */
        }
        ferryboat = unit_list_find(&(map_get_tile(punit->x, punit->y)->units),
				   punit->ai.ferryboat);
        punit->goto_dest_x = gx;
        punit->goto_dest_y = gy;
        if (ferryboat && (!ferryboat->ai.passenger
			  || ferryboat->ai.passenger == punit->id)) {
	  freelog(LOG_DEBUG,
		  "We have FOUND BOAT, %d ABOARD %d@(%d,%d)->(%d,%d).",
		  punit->id, ferryboat->id, punit->x, punit->y, gx, gy);
          set_unit_activity(punit, ACTIVITY_SENTRY);
				/* kinda cheating -- Syela */
          ferryboat->ai.passenger = punit->id;
          auto_settler_do_goto(pplayer, ferryboat, gx, gy);
          set_unit_activity(punit, ACTIVITY_IDLE);
        } /* need to zero pass & ferryboat at some point. */
      }
      if (goto_is_sane(pplayer, punit, gx, gy, 1)
	  && punit->moves_left
	  && ((!ferryboat)
	      || (is_tiles_adjacent(punit->x, punit->y, gx, gy)
		  && could_unit_move_to_tile(punit, punit->x, punit->y,
					     gx, gy)))) {
        auto_settler_do_goto(pplayer, punit, gx, gy);
        if (!unit_list_find(&pplayer->units, save_id)) return(0); /* died */
        punit->ai.ferryboat = 0;
      }
    }
  } else punit->ai.control=0;

  if (punit->ai.control
      && punit->moves_left
      && punit->activity == ACTIVITY_IDLE) {
    /* if players can build with 0 moves left, so should the AI */
    if (same_pos(gx, gy, punit->x, punit->y)) {
      if (best_act == ACTIVITY_UNKNOWN) {
        remove_city_from_minimap(gx, gy); /* yeah, I know. -- Syela */
        ai_do_build_city(pplayer, punit);
        return(0);
      }
      set_unit_activity(punit, best_act);
      send_unit_info(0, punit, 0);
      return(0);
    }
  }
  return(0);
}

void initialize_infrastructure_cache(struct city *pcity)
{
  int i, j;
  struct player *pplayer = &game.players[pcity->owner];
  city_map_iterate(i, j) {
    pcity->ai.detox[i][j] = ai_calc_pollution(pcity, pplayer, i, j);
    pcity->ai.mine[i][j] = ai_calc_mine(pcity, pplayer, i, j);
    pcity->ai.irrigate[i][j] = ai_calc_irrigate(pcity, pplayer, i, j);
    pcity->ai.transform[i][j] = ai_calc_transform(pcity, pplayer, i, j);
    pcity->ai.road[i][j] = ai_calc_road(pcity, pplayer, i, j);
/* gonna handle road_bo dynamically for now since it can change
as punits arrive at adjacent tiles and start laying road -- Syela */
    pcity->ai.railroad[i][j] = ai_calc_railroad(pcity, pplayer, i, j);
  }
}

/************************************************************************** 
  run through all the players settlers and let those on ai.control work 
  automagically
**************************************************************************/
void auto_settlers_player(struct player *pplayer) 
{
  static struct timer *t = NULL;      /* alloc once, never free */

  t = renew_timer_start(t, TIMER_CPU, TIMER_DEBUG);

  city_list_iterate(pplayer->cities, pcity)
    initialize_infrastructure_cache(pcity); /* saves oodles of time -- Syela */
  city_list_iterate_end;
  pplayer->ai.warmth = WARMING_FACTOR * total_player_citizens(pplayer) * 10 *
                       (game.globalwarming + game.heating) / (map.xsize *
                        map.ysize * map.landpercent * 2); /* threat of warming */
  freelog(LOG_DEBUG, "Warmth = %d, game.globalwarming=%d",
	  pplayer->ai.warmth, game.globalwarming);
  unit_list_iterate(pplayer->units, punit) {
    freelog(LOG_DEBUG, "%s's settler at (%d, %d)",
	    pplayer->name, punit->x, punit->y); 
    if (punit->ai.control && unit_flag(punit->type, F_SETTLERS)) {
      freelog(LOG_DEBUG, "Is ai controlled.");
      if(punit->activity == ACTIVITY_SENTRY)
	set_unit_activity(punit, ACTIVITY_IDLE);
      if (punit->activity == ACTIVITY_GOTO && punit->moves_left)
        set_unit_activity(punit, ACTIVITY_IDLE);
      if (punit->activity == ACTIVITY_IDLE)
	auto_settler_findwork(pplayer, punit);
      freelog(LOG_DEBUG, "Has been processed.");
    }
  }
  unit_list_iterate_end;
  if (timer_in_use(t)) {
    freelog(LOG_VERBOSE, "%s's autosettlers consumed %g milliseconds.",
 	    pplayer->name, 1000.0*read_timer_seconds(t));
  }
}

static void assign_settlers_player(struct player *pplayer)
{
  short i = 1<<pplayer->player_no;
  struct tile *ptile;
  unit_list_iterate(pplayer->units, punit)
    if (unit_flag(punit->type, F_SETTLERS)) {
      if (punit->activity == ACTIVITY_GOTO) {
        ptile = map_get_tile(punit->goto_dest_x, punit->goto_dest_y);
        ptile->assigned = ptile->assigned | i; /* assigned for us only */
      } else {
        ptile = map_get_tile(punit->x, punit->y);
        ptile->assigned = 32767; /* assigned for everyone */
      }
    } else {
      ptile = map_get_tile(punit->x, punit->y);
      ptile->assigned = ptile->assigned | (32767 ^ i); /* assigned for everyone else */
    }
  unit_list_iterate_end;
}

static void assign_settlers(void)
{
  int i, x, y;
  for (x = 0; x < map.xsize; x++)
    for (y = 0; y < map.ysize; y++)
      map_get_tile(x, y)->assigned = 0;

  for (i = 0; i < game.nplayers; i++) {
    assign_settlers_player(&game.players[i]);
  }
}

static void assign_region(int x, int y, int n, int d, int s)
{
  int i, j;
  for (j = MAX(0, y - d); j <= MIN(map.ysize - 1, y + d); j++) {
    for (i = x - d; i <= x + d; i++) {
      if (!s || is_terrain_near_tile(i, j, T_OCEAN))
        territory[map_adjust_x(i)][j] &= (1<<n);
    }
  }
}

static void assign_territory_player(struct player *pplayer)
{
  int n = pplayer->player_no;
  unit_list_iterate(pplayer->units, punit)
    if (unit_types[punit->type].attack_strength) {
/* I could argue that phalanxes aren't really a threat, but ... */
      if (is_sailing_unit(punit)) {
        assign_region(punit->x, punit->y, n, 1 + unit_types[punit->type].move_rate / 3, 1);
      } else if (is_ground_unit(punit)) {
        assign_region(punit->x, punit->y, n, 1 + unit_types[punit->type].move_rate /
             (unit_flag(punit->type, F_IGTER) ? 1 : 3), 0);
/* I realize this is not the most accurate, but I don't want to iterate
road networks 100 times/turn, and I can't justifiably abort when I encounter
already assigned territory.  If anyone has a reasonable alternative that won't
noticeably slow the game, feel free to replace this else{}  -- Syela */
      } else {
        assign_region(punit->x, punit->y, n, 1 + unit_types[punit->type].move_rate / 3, 0);
      } 
    }
  unit_list_iterate_end;
  city_list_iterate(pplayer->cities, pcity)
    assign_region(pcity->x, pcity->y, n, 3, 0);
  city_list_iterate_end;
}

static void assign_territory(void)
{ /* this funct is supposed to keep settlers out of enemy territory -- Syela */
  int i, x, y;
  for (x = 0; x < map.xsize; x++)
    for (y = 0; y < map.ysize; y++)
      territory[x][y] = 65535;

  for (i = 0; i < game.nplayers; i++) {
    assign_territory_player(&game.players[i]);
  }
/* an actual territorial assessment a la AI algorithms for go might be
appropriate here.  I'm not sure it's necessary, so it's not here yet. -- Syela */
}  

/**************************************************************************
  Do the auto_settler stuff for all the players. 
**************************************************************************/
void auto_settlers(void)
{
  int i;
  assign_settlers();
  assign_territory();
  for (i = 0; i < game.nplayers; i++) {
    auto_settlers_player(&game.players[i]);
  }
}

void contemplate_settling(struct player *pplayer, struct city *pcity)
{
  struct unit virtualunit;
  int want;
/* used to use old crappy formulas for settler want, but now using actual want! */
  memset(&virtualunit, 0, sizeof(struct unit));
  virtualunit.id = 0;
  virtualunit.owner = pplayer->player_no;
  virtualunit.x = pcity->x;
  virtualunit.y = pcity->y;
  virtualunit.type = best_role_unit(pcity, F_SETTLERS);
  virtualunit.moves_left = unit_types[virtualunit.type].move_rate;
  virtualunit.hp = unit_types[virtualunit.type].hp;  
  want = auto_settler_findwork(pplayer, &virtualunit);
  unit_list_iterate(pplayer->units, qpass)
    if (qpass->ai.ferryboat == pcity->id) want = -199;
  unit_list_iterate_end;
  pcity->ai.settler_want = want;
}