-
Notifications
You must be signed in to change notification settings - Fork 0
/
allygen2.pl
849 lines (756 loc) · 17.6 KB
/
allygen2.pl
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
#!/usr/bin/perl
# allygen: compute regional borders for a SEC-format Traveller data
# file, based on the "influence" of good starports, suitable for
# feeding into sec2pdf as a metadata-file. It can also completely
# ignore the existing allegiance data and create brand new allegiances,
# saving the results to a fresh SEC file.
#
# $Id: allygen,v 1.15 2004/11/09 03:36:15 jgreely Exp $
#
use strict;
use Getopt::Long;
Getopt::Long::Configure("Bundling");
my @Same;
my $NoAllies = "";
my $Ignore = 0;
my $Fudge = 0;
my $Routes = 0;
my $DEBUG = 0;
my $OutFile;
my $DoDOS = 0;
#TODO: add option & code for dumping a new SEC file when -i is used
#
GetOptions("same|s=s" => \@Same,"nonaligned|n=s" => \$NoAllies,
"fudge|f=i" => \$Fudge, "ignore|i" => \$Ignore,"routes|r" => \$Routes,"dos|d" => \$DoDOS,
"DEBUG|D" => \$DEBUG,"output|o=s" => \$OutFile) or die <<EOF;
usage: allygen [-di] [-o out] [-f N] [-n Na,...] [-s Im,...] secfile
-d write output files as DOS text instead of Unix (CRLF)
* -r ignore existing routes and create new ones
-i ignore existing allegiances and create new ones.
-f fudge-factor for scaling influence of systems on others (+-N).
-n treat comma-separated list of allegiance codes as non-aligned
(don't draw borders around them).
-o save border data to out.msec; if -i is also specified, save
a revised version of the input SEC file with new allegiances.
-s treat comma-separated list of allegiance codes as the same
when generating borders.
EOF
my $CR = $DoDOS ? "\r" : "";
my ($SectorFile) = @ARGV;
die "usage: $0 [...] sectorfile\n" unless -r $SectorFile;
if ($OutFile) {
open(Meta,">$OutFile.msec") or die "$0: $OutFile.msec: $!\n";
select(Meta);
}
my @S;
my %A;
my %Aname;
my $mod;
my %Aneighbor;
my @WorldAlly;
my %Port;
my %Pop;
my %TL;
my %Data;
my $I = 0;
# record the "non-aligned" codes
#
my %NoAllies;
foreach (split(/,/,$NoAllies)) {
$NoAllies{$_} = 1;
}
# collapse multiple allegiance codes to one
#
my %Same;
foreach (@Same) {
my ($primary,@rest) = split(/,/);
foreach my $i (@rest) {
$Same{$i} = $primary;
}
}
readsectorfile();
# for each populated system, in order by its starport, population, and
# tech level, claim it as part of the region, and then extend its
# influence outward based on starport quality.
#
foreach (sort bycoolness keys %Port) {
my ($c,$r) = /^(..)(..)$/;
if ($Ignore) {
if (! $S[$c][$r]) {
$I++;
$S[$c][$r] = $I;
$A{$I} = $I;
# $Aname{$I} = sprintf("%s%s",unidigit(int($I/33)),
$Aname{$I} = sprintf("%s%s",unidigit(int(rand($I % 92))),
unidigit_lc(int($I % 33)));
}
$WorldAlly[$c][$r] = $S[$c][$r];
}
my $A = $A{$WorldAlly[$c][$r]};
# ignore non-aligned worlds
#
if ($NoAllies{$WorldAlly[$c][$r]}) {
$S[$c][$r] = 99999;
next;
}
# I claim this planet in the name of Mars
#
$S[$c][$r] = $A;
# what neighbors can I claim?
# (E and X starports have no influence at all)
#
next if $Port{$c.$r} gt "D";
my ($cn,$rn);
# starting at distance 1, up to the limit of my influence,
# claim unclaimed hexes
#
foreach my $dist (1..index("DCBA",$Port{$c.$r})+$Fudge) {
#start in direction 4
($cn,$rn) = neighbor($c,$r,$dist,4);
$S[$cn][$rn] = $A unless $S[$cn][$rn];
#for each direction
for my $dir (0..5) {
#claim $dist hexes in a line
for my $i (1..$dist) {
($cn,$rn) = neighbor($cn,$rn,1,$dir);
# ignore out-sector hexes
next if $cn < 1 or $cn > 32 or $rn < 1
or $rn > 40;
$S[$cn][$rn] = $A unless $S[$cn][$rn];
}
}
}
}
# compute number of hexes in each allegiance
#
my %Count = ();;
foreach my $c (1..32) {
foreach my $r (1..40) {
next if $S[$c][$r] == 99999 or $S[$c][$r] == 0;
$Count{$S[$c][$r]}++;
}
}
# switch empty squares to largest allegiance of adjacent planet
#
foreach my $c (1..32) {
foreach my $r (1..40) {
next if $WorldAlly[$c][$r];
my %tmp = ();
for my $dir (0..5) {
my ($cn,$rn) = neighbor($c,$r,1,$dir);
next if $S[$cn][$rn] == 99999 or $S[$cn][$rn] == 0;
$tmp{$S[$cn][$rn]} = 1 if $WorldAlly[$cn][$rn];
}
next unless keys %tmp;
my $ally = 0;
foreach (sort keys %tmp) {
$ally = $_ if $Count{$_} >= $Count{$ally};
}
$S[$c][$r] = $ally;
$Count{$S[$c][$r]}--;
$Count{$ally}++;
}
}
# repel borders! non-aligned worlds want elbow room!
#
foreach my $c (1..32) {
foreach my $r (1..40) {
next unless $WorldAlly[$c][$r] and $S[$c][$r] == 99999;
for my $dir (0..5) {
my ($cn,$rn) = neighbor($c,$r,1,$dir);
$S[$cn][$rn] = 0 unless $WorldAlly[$cn][$rn];
}
}
}
if ($Routes) {
# routermatic! - generate trade routes and like it
# vary the results based on starports. Yikes! Here we go.
# pair J1 J2 J3 J4
# A-A 1 2 4 5
# A-B 1 3 4 5
# A-C 1 4 6 -
# A-D 1 5 - -
# A-E 2 - - -
# B-B 1 3 4 6
# B-C 2 4 6 -
# B-D 3 6 - -
# B-E 4 - - -
# C-C 3 6 - -
# C-D 4 - - -
# C-E 4 - - -
# D-D 4 - - -
# D-E 5 - - -
# E-E 6 - - -
my $SECTOR_WIDTH=32;
my $SECTOR_HEIGHT=40;
sub even { return ( $_[0] % 2 ) == 0; }
sub odd { return !even($_[0]); }
sub div { return int( $_[0] / $_[1] ); }
sub mod { return int( $_[0] % $_[1] ); }
sub max { my $max = shift; while( @_ ) { my $test = shift; if( $test > $max ) { $max = $test; } } return $max; }
sub dist
{
my($a,$b) = @_;
my $a_x = div($a,100);
my $a_y = mod($a,100);
my $b_x = div($b,100);
my $b_y = mod($b,100);
my $dx = $b_x - $a_x;
my $dy = $b_y - $a_y;
my $adx = abs($dx);
my $ody = $dy + div( $adx, 2 );
if( odd($a_x) && even($b_x) ) { $ody += 1; }
return max($adx - $ody, $ody, $adx);
}
sub reachable
{
my( $hex, $jump ) = @_;
my @results = ();
my $x = div($hex,100);
my $y = mod($hex,100);
for( my $rx = $x - $jump; $rx <= $x + $jump; $rx++ )
{
for( my $ry = $y - $jump; $ry <= $y + $jump; $ry++ )
{
if( $rx >= 1 && $rx <= $SECTOR_WIDTH && $ry >= 1 && $ry <= $SECTOR_HEIGHT )
{
my $candidate = sprintf("%02d%02d", $rx, $ry );
my $dist = dist( $hex, $candidate );
if( $dist > 0 && $dist <= $jump )
{
push( @results, $candidate );
}
}
}
}
return @results;
}
foreach (sort bycoolness keys %Port) {
my ($c,$r) = /^(..)(..)$/;
next if $Port{$c.$r} gt "C";
foreach my $dist (1..index("DCBA",$Port{$c.$r})) {
# for my $dir (0..5) {
# #for each direction
# my ($cn,$rn) = neighbor($c,$r,$dist,$dir);
# # ignore out-sector hexes
# next if $cn < 1 or $cn > 32 or $rn < 1
# or $rn > 40;
#
foreach ( reachable( sprintf("%02d%02d", $c, $r ), $dist ) )
{
my( $cn, $rn ) = /^(..)(..)$/; next if $Port{$cn.$rn} gt "C";
if ($WorldAlly[$cn][$rn]) {
$mod="99";
#if ($dist=1) {
#
## A
#
#if (($Port{$c.$r} gt "A") and ($Port{$cn.$rn} lt "A")) {
#$mod="0"
#};
#if (($Port{$c.$r} lt "A") and ($Port{$cn.$rn} lt "B")) {
#$mod="0"
#};
#if (($Port{$c.$r} lt "A") and ($Port{$cn.$rn} lt "C")) {
#$mod="0"
#};
#if (($Port{$c.$r} lt "A") and ($Port{$cn.$rn} lt "D")) {
#$mod="0"
#};
#if (($Port{$c.$r} lt "A") and ($Port{$cn.$rn} lt "E")) {
#$mod="1"
#};
#
## B
#
#if (($Port{$c.$r} = "B") and ($Port{$cn.$rn} = "A")) {
#$mod="0"
#};
#if (($Port{$c.$r} = "B") and ($Port{$cn.$rn} = "B")) {
#$mod="0"
#};
#if (($Port{$c.$r} = "B") and ($Port{$cn.$rn} = "C")) {
#$mod="1"
#};
#if (($Port{$c.$r} = "B") and ($Port{$cn.$rn} = "D")) {
#$mod="2"
#};
#if (($Port{$c.$r} = "B") and ($Port{$cn.$rn} = "E")) {
#$mod="3
#"
#};
#
## C
#
#if (($Port{$c.$r} = "C") and ($Port{$cn.$rn} = "A")) {
#$mod="0"
#};
#if (($Port{$c.$r} = "C") and ($Port{$cn.$rn} = "B")) {
#$mod="1"
#};
#if (($Port{$c.$r} = "C") and ($Port{$cn.$rn} = "C")) {
#$mod="2"
#};
#if (($Port{$c.$r} = "C") and ($Port{$cn.$rn} = "D")) {
#$mod="3"
#};
#if (($Port{$c.$r} = "C") and ($Port{$cn.$rn} = "E")) {
#$mod="3"
#};
#
## D
#
#if (($Port{$c.$r} = "D") and ($Port{$cn.$rn} = "A")) {
#$mod="1"
#};
#if (($Port{$c.$r} = "D") and ($Port{$cn.$rn} = "B")) {
#$mod="2"
#};
#if (($Port{$c.$r} = "D") and ($Port{$cn.$rn} = "C")) {
#$mod="3"
#};
#if (($Port{$c.$r} = "D") and ($Port{$cn.$rn} = "D")) {
#$mod="3"
#};
#if (($Port{$c.$r} = "D") and ($Port{$cn.$rn} = "E")) {
#$mod="4"
#};
#
## E
#
#if (($Port{$c.$r} = "E") and ($Port{$cn.$rn} = "A")) {
#$mod="1"
#};
#if (($Port{$c.$r} = "E") and ($Port{$cn.$rn} = "B")) {
#$mod="2"
#};
#if (($Port{$c.$r} = "E") and ($Port{$cn.$rn} = "C")) {
#$mod="3"
#};
#if (($Port{$c.$r} = "E") and ($Port{$cn.$rn} = "D")) {
#$mod="4"
#};
#if (($Port{$c.$r} = "E") and ($Port{$cn.$rn} = "E")) {
#$mod="5"
#};
#};
#
#
## Distance 2
#
#if ($dist=2) {
#if (($Port{$c.$r} = "A") and ($Port{$cn.$rn} = "A")) {
#$mod="1"
#};
#if (($Port{$c.$r} = "A") and ($Port{$cn.$rn} = "B")) {
#$mod="2"
#};
#if (($Port{$c.$r} = "A") and ($Port{$cn.$rn} = "C")) {
#$mod="3"
#};
#if (($Port{$c.$r} = "A") and ($Port{$cn.$rn} = "D")) {
#$mod="4"
#};
#
## B
#
#if (($Port{$c.$r} = "B") and ($Port{$cn.$rn} = "A")) {
#$mod="2"
#};
#if (($Port{$c.$r} = "B") and ($Port{$cn.$rn} = "B")) {
#$mod="2"
#};
#if (($Port{$c.$r} = "B") and ($Port{$cn.$rn} = "C")) {
#$mod="3"
#};
#if (($Port{$c.$r} = "B") and ($Port{$cn.$rn} = "D")) {
#$mod="5"
#};
#
#};
#
## C
#
#if (($Port{$c.$r} = "C") and ($Port{$cn.$rn} = "A")) {
#$mod="3"
#};
#if (($Port{$c.$r} = "C") and ($Port{$cn.$rn} = "B")) {
#$mod="3"
#};
#if (($Port{$c.$r} = "C") and ($Port{$cn.$rn} = "C")) {
#$mod="5"
#};
#
## D
#
#if (($Port{$c.$r} = "D") and ($Port{$cn.$rn} = "A")) {
#$mod="1"
#};
#if (($Port{$c.$r} = "D") and ($Port{$cn.$rn} = "B")) {
#$mod="2"
#};
#
## E
#
#};
#
#
#
#
#
#
## Distance 3
#
#if ($dist=3) {
#if (($Port{$c.$r} = "A") and ($Port{$cn.$rn} = "A")) {
#$mod="3"
#};
#if (($Port{$c.$r} = "A") and ($Port{$cn.$rn} = "B")) {
#$mod="3"
#};
#if (($Port{$c.$r} = "A") and ($Port{$cn.$rn} = "C")) {
#$mod="5"
#};
#
## B
#
#if (($Port{$c.$r} = "B") and ($Port{$cn.$rn} = "A")) {
#$mod="3"
#};
#if (($Port{$c.$r} = "B") and ($Port{$cn.$rn} = "B")) {
#$mod="3"
#};
#if (($Port{$c.$r} = "B") and ($Port{$cn.$rn} = "C")) {
#$mod="5"
#};
#
## C
#
#if (($Port{$c.$r} = "C") and ($Port{$cn.$rn} = "A")) {
#$mod="5"
#};
#if (($Port{$c.$r} = "C") and ($Port{$cn.$rn} = "B")) {
#$mod="5"
#};
#
## D
#
## E
#
#};
#
#
#
#
#
## Distance 4
#
#if ($dist=4) {
#if (($Port{$c.$r} = "A") and ($Port{$cn.$rn} = "A")) {
#$mod="4"
#};
#if (($Port{$c.$r} = "A") and ($Port{$cn.$rn} = "B")) 4
#$mod="4"
#};
#
## B
#
#if (($Port{$c.$r} = "B") and ($Port{$cn.$rn} = "A")) {
#$mod="4"
#};
#if (($Port{$c.$r} = "B") and ($Port{$cn.$rn} = "B")) {
#$mod="5"
#};
#
## C
#
## D
#
## E
#
#};
#
#
#
if (((int(rand(100)+1) ) > 92)) {
# $Aneighbor{$I} = sprintf("route %01d %01d %01d %02d%02d %02d%02d",$mod,$Port{$c.$r},$Port{[$cn].[$rn]},$c,$r,$cn,$rn,"$CR\n");
$Aneighbor{$I} = sprintf("route %02d%02d %02d%02d",$c,$r,$cn,$rn,"$CR\n");
print $Aneighbor{$I},"$CR\n";
};
}
}
}
}
}
# the empires strike back! don't let non-aligned worlds
# break an allegiance in two
#
foreach my $c (1..32) {
foreach my $r (1..40) {
next if $WorldAlly[$c][$r];
my %tmp;
for my $dir (0..5) {
my ($cn,$rn) = neighbor($c,$r,1,$dir);
$tmp{$S[$cn][$rn]}++ if $WorldAlly[$cn][$rn]
and $S[$cn][$rn] != 99999;
}
my $ally = (sort { $tmp{$b} <=> $tmp{$a} } keys %tmp)[0];
next unless $tmp{$ally} >= 2;
$S[$c][$r] = $ally;
}
}
# un-align empty squares that are far away from their allied worlds
#
foreach my $c (1..32) {
foreach my $r (1..40) {
next if $WorldAlly[$c][$r];
my $count = 0;
my ($cn,$rn);
foreach my $dist (1..2) {
#start in direction 4
($cn,$rn) = neighbor($c,$r,$dist,4);
#for each direction
for my $dir (0..5) {
for my $i (1..$dist) {
($cn,$rn) = neighbor($cn,$rn,1,$dir);
next if $cn < 0 or $rn < 0;
$count++ if $S[$c][$r] ==
$A{$WorldAlly[$cn][$rn]};
}
}
}
$S[$c][$r] = 0 unless $count;
}
}
# un-align empty hexes that have less than two adjacent friends
# (it's so nice we'll do it twice!)
#
foreach (1..2) {
foreach my $c (1..32) {
foreach my $r (1..40) {
next if $WorldAlly[$c][$r];
my $count = 0;
for my $dir (0..5) {
my ($cn,$rn) = neighbor($c,$r,1,$dir);
# ignore out-sector hexes
next if $cn < 1 or $cn > 32 or $rn < 1
or $rn > 40;
$count++ if $S[$cn][$rn] == $S[$c][$r];
}
$S[$c][$r] = 0 unless $count > 1;
}
}
}
#dump cheesy fills for testing
#
if ($DEBUG) {
foreach my $c (1..32) {
foreach my $r (1..40) {
next if $S[$c][$r] == 99999;
next if $S[$c][$r] == 0;
printf("region %02d%02d %.4f$CR\n",$c,$r,
0.9 - $S[$c][$r] / (1.75*$I)) if $S[$c][$r];
}
}
}
# hoo-boy, here we go: walk the outside edge of each connected
# region in the sector
#
foreach my $c (1..32) {
my $current = 0;
foreach my $r (1..40) {
my $A = $S[$c][$r];
next if abs($current) == abs($A);
$current = $A;
next if $A == 0 or $A == 99999;
next if $A < 0;
my @R = walkregion($c,$r,$A);
print "# Alliance: ",$Aname{$A},"$CR\n",
join(" ","border",@R),"$CR\n" if @R;
}
}
# need to deal with completely-surrounded non-aligned worlds
# (not often needed, unless repel-borders is disabled)
# TODO: not quite good enough for alphacrucis.sec (use -D to see why...)
#
foreach my $c (1..32) {
foreach my $r (1..40) {
next unless $S[$c][$r] == 99999;
my $count = 0;
foreach my $dir (0..5) {
my ($cn,$rn) = neighbor($c,$r,1,$dir);
$count++ if $S[$cn][$rn] > 0;
}
next unless $count == 5;
my @R = walkregion($c,$r,99999);
print "# Non-aligned\n",join(" ","border",@R),"$CR\n" if @R;
}
}
if ($Ignore and $OutFile) {
open(Out,">$OutFile.sec") or die "$0: $OutFile.sec: $!\n";
print Out <<EOF;
SECTOR DATA$CR
$CR
The data in the sector text files is laid out in column format:$CR
$CR
1-14: Name$CR
15-18: HexNbr$CR
20-28: UWP$CR
31: Bases$CR
33-47: Codes & Comments$CR
49: Zone$CR
52-54: PBG$CR
56-57: Allegiance$CR
59-74: Stellar Data$CR
$CR
....+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8$CR
EOF
foreach (sort keys %Port) {
my ($name,$uwp,$bases,$trade,$zone,$pbg,$ally,$stars)
= split(/\t/,$Data{$_});
my ($c,$r) = /^(..)(..)$/;
$ally = $Aname{$WorldAlly[$c][$r]};
printf Out ("%-14s%04d %-9s %1s %-15s %1s %-3s %-2s %s$CR\n",
$name,$_,$uwp,$bases,$trade,$zone,$pbg,$ally,$stars);
}
close(Out);
}
if ($OutFile) {
select(STDOUT);
close(Meta);
}
exit 0;
# reproduce the walk-around-the-edges routine used in sec2pdf
#
sub walkregion {
my ($c,$r,$ally) = @_;
my @R = (sprintf("%02d%02d",$c,$r));
#directions checked in starting hex
my @checked = (1,1,1);
my $checkfirst = 3;
my $checklast = 5;
my ($c1,$r1) = ($c,$r);
my ($cn,$rn);
until ($checked[4] and $checked[5]) {
last if $c == $c1 and $r == $r1 and $checked[$checkfirst];
$checklast = $checkfirst + 5;
my $dir;
for ($checkfirst..$checklast) {
$dir = $_;
if ($c == $c1 and $r == $r1) {
if ($checked[$dir % 6]++) {
($cn,$rn) = (0,0);
last;
}
}
($cn,$rn) = neighbor($c,$r,1,$dir % 6);
next if $cn < 1 or $cn > 32 or $rn < 1 or $rn > 40;
last if abs($S[$cn][$rn]) == $ally;
}
if (abs($S[$cn][$rn]) == $ally) {
$S[$c][$r] = -1 * $ally; #been there!
push(@R,sprintf("%02d%02d",$cn,$rn));
$checkfirst = ($dir + 4) % 6;
($c,$r) = ($cn,$rn);
}else{
#can't find a friend!
last;
}
}
# scan for bogon regions
# - invalid ones made when you leave concentric regions
# - empty ones made when you hit open space inside a region
#
my ($cs,$rs) = $R[0] =~ /^(..)(..)$/;
my $pcount = 0;
foreach (@R) {
my ($ct,$rt) = /^(..)(..)$/;
return () if $ct < $cs;
return () if $ct == $cs and $rt < $rs;
$pcount++ if $WorldAlly[$ct][$rt];
}
return () if @R < 4 and $pcount == 0;
return @R;
}
#location of neighbor at a certain distance, in a certain direction
#directions are numbered starting with 0 (LL) and going clockwise
#
sub neighbor {
my ($c,$r,$dist,$dir) = @_;
my ($c2,$r2);
#expanded for clarity
if ($dir == 0) {
$c2 = $c - $dist;
$r2 = $r + int(($dist + 1 - $c % 2)/2);
}elsif ($dir == 1) {
$c2 = $c - $dist;
$r2 = $r - int(($dist + $c % 2)/2);
}elsif ($dir == 2) {
$c2 = $c;
$r2 = $r - $dist;
}elsif ($dir == 3) {
$c2 = $c + $dist;
$r2 = $r - int(($dist + $c % 2)/2);
}elsif ($dir == 4) {
$c2 = $c + $dist;
$r2 = $r + int(($dist + 1 - $c % 2)/2);
}elsif ($dir == 5) {
$c2 = $c;
$r2 = $r + $dist;
}else{
return undef;
}
return ($c2,$r2);
}
sub bycoolness {
#lower is better
return $Port{$a} cmp $Port{$b} if $Port{$a} ne $Port{$b};
#higher is better
return $TL{$b} <=> $TL{$a} if $TL{$b} != $TL{$a};
#higher is better
return $Pop{$a} <=> $Pop{$b} if $Pop{$a} != $Pop{$b};
}
sub readsectorfile {
open(In,$SectorFile) or die "$0: $SectorFile: $!\n";
#fast parse of traditional .SEC file
while (<In>) {
last if /^\.\.\./;
}
while (<In>) {
next if /^\s+$/;
my ($name,$c,$r,$uwp,$bases,$trade,$zone,$pbg,$ally,$stars) =
/^(.{14})(.{2})(.{2}) (.{9}) (.) (.{15}) (.) (.{3}) (.{2}) (.*)$/;
$name =~ s/\s+$//;
$stars =~ s/\s+$//;
# preserve original data in case we're writing it back out
#
$Data{$c.$r} = join("\t",$name,$uwp,$bases,$trade,$zone,
$pbg,$ally,$stars);
# record allegiance if we're not ignoring it
if (! $Ignore) {
# collapse allegiances that should share borders
$ally = $Same{$ally} if $Same{$ally};
if (! defined $A{$ally}) {
$I++;
$A{$ally} = $I;
$Aname{$I} = $ally;
}
$WorldAlly[$c][$r] = $ally;
}
$Port{$c.$r} = substr($uwp,0,1);
$Pop{$c.$r} = 10 ^ uni2dec(substr($uwp,4,1)) *
substr($pbg,0,1);
$TL{$c.$r} = uni2dec(substr($uwp,8,1));
}
close(In);
}
sub uni2dec {
return undef unless length($_[0]) == 1;
return index("0123456789ABCDEFGHJKLMNPQRSTUVWXY",$_[0]);
}
sub unidigit {
return substr("ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz",$_[0],1);
}
sub unidigit_lc {
return substr("0123456789abcdefghjklmnpqrstuvwxy",$_[0],1);
}