-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathheritage.lua
1235 lines (1125 loc) · 35.9 KB
/
heritage.lua
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
function isHistorical(unit)
local type = tostring(unit._type);
--print(type);
if ( string.find(type, "historical_figure") ~= nil ) then
do return true end;
end
if ( string.find(type, "unit") ~= nil ) then
do return false end;
end
dfhack.error("unit is not of a valid type: " .. unit);
do return nil end;
end
function getRelation(figure, typeString)
for index,link in pairs(figure.histfig_links) do
local type = tostring(link._type);
if ( string.find(type, typeString) ~= nil ) then
do return link.anon_1 end;
end
end
do return nil end;
end
function merge(list, lessThan, lo, mid, hi)
local temp = {};
local index_L = lo;
local index_R = mid;
local index_temp = 0;
while(true) do
if ( index_L >= mid ) then
if ( index_R < hi ) then
temp[index_temp] = list[index_R];
index_temp = index_temp+1;
index_R = index_R+1;
else
break;
end
else
if ( index_R < hi ) then
if ( lessThan(index_R, index_L) ) then
temp[index_temp] = list[index_R];
index_temp = index_temp+1;
index_R = index_R+1;
else
temp[index_temp] = list[index_L];
index_temp = index_temp+1;
index_L = index_L + 1;
end
else
temp[index_temp] = list[index_L];
index_temp = index_temp+1;
index_L = index_L + 1;
end
end
end
for i=lo,hi-1 do
list[i] = temp[i-lo];
end
end
local doOnce = 0;
function sort(list, lessThan, lo, hi)
if ( lo == hi or lo > hi or lo+1 == hi ) then
do return end;
end
--print("lo="..lo..", hi="..hi);
--[[--see if some of it is already sorted
local firstUnsorted = 0;
local previous = list[lo];
local index = lo+1;
while(true) do
index = index+1;
if ( index >= hi ) then
break;
end
if ( lessThan(index, index-1) ) then
firstUnsorted = index;
if ( doOnce == 0 ) then
print(firstUnsorted);
doOnce = 1;
end
break;
end
end
if ( firstUnsorted - lo > 10 and firstUnsorted - lo >= (hi-lo)/4 ) then
local mid = firstUnsorted-1;
--sort(list, lessThan, lo, mid);
sort(list, lessThan, mid, hi);
merge(list, lessThan, lo, mid, hi);
do return end;
end]]
local mid = math.floor((lo+hi)/2);
sort(list, lessThan, lo, mid);
sort(list, lessThan, mid, hi);
merge(list, lessThan, lo, mid, hi);
end
function getMax(list)
local result = 0;
for key,value in pairs(list) do
if ( key >= result ) then
result = key+1;
end
end
return result;
end
function computeHeritage(nameScheme, outputType, sortOutputBy, doReverseOutputOrder)
local dwarfRace = df.global.ui.race_id;
local allUnits = {};
local age = {};
local count=0;
local idToHistoricalUnit = {};
local idToLocalUnit = {};
local localToHistorical = {};
local historicalToLocal = {};
local ticksPerYear = 403200; --403200 ticks in a year
--compute allUnits, age, count, idToHistoricalUnit
for index,unit in pairs(df.global.world.history.figures) do
if ( unit.race == dwarfRace ) then
allUnits[count] = unit;
age[unit] = unit.born_year*403200 + unit.born_seconds;
count = count+1;
idToHistoricalUnit[unit.id] = unit;
end
end
--compute allUnits, age, count, idToLocalUnit, localToHistorical, historicalToLocal
for index,unit in pairs(df.global.world.units.all) do
if ( unit.race == dwarfRace ) then
allUnits[count] = unit;
age[unit] = unit.relations.birth_year*ticksPerYear + unit.relations.birth_time;
count = count+1;
idToLocalUnit[unit.id] = unit;
if ( unit.hist_figure_id ~= -1 ) then
local historical_me = idToHistoricalUnit[unit.hist_figure_id];
localToHistorical[unit] = historical_me;
historicalToLocal[historical_me] = unit;
end
end
end
print("Sorting...");
--sort units
function lessThan(index1, index2)
if ( index1 == index2 ) then
do return false end;
end
local unit1 = allUnits[index1];
local unit2 = allUnits[index2];
--print(tostring(index1) .. ", " .. tostring(index2));
--print(tostring(unit1) .. ", " .. tostring(unit2));
if ( isHistorical(unit1) == isHistorical(unit2) ) then
if ( age[unit2] < age[unit1] ) then
do return false end;
end
else
if ( isHistorical(unit2) ) then
do return false end;
end
end
return true;
end
sort(allUnits, lessThan, 0, count);
print("Done sorting!");
function getParent(unit, whichParent)
if ( isHistorical(unit) ) then
local id = getRelation(unit, whichParent);
return idToHistoricalUnit[id];
else
--try the parent of your historical version if you have one
if ( unit.hist_figure_id ~= -1 ) then
local hist_me = idToHistoricalUnit[unit.hist_figure_id];
local hist_parent = getParent(hist_me, whichParent);
if ( hist_parent ~= nil ) then
do return hist_parent end;
end
end
local id;
if ( whichParent == "mother" ) then
id = unit.relations.mother_id;
elseif ( whichParent == "father" ) then
id = unit.relations.father_id;
else
dfhack.error("Invalid relation: " .. whichParent);
end
if ( id == -1 ) then
do return nil end;
end
local ancestor = idToLocalUnit[id];
if ( ancestor == nil ) then
do return nil end;
end
if ( ancestor.hist_figure_id == -1 or idToHistoricalUnit[ancestor.hist_figure_id] == nil ) then
do return ancestor end;
end
do return idToHistoricalUnit[ancestor.hist_figure_id] end;
end
end
local childIndexTable = {};
local allNames = {};
local nameAge = {};
function computeChildIndexTable()
--precondition: units are sorted by birth order
local childTable = {};
local childCount = {};
local result = {};
function helper(unit)
local mother = getParent(unit, "mother");
if ( mother == nil ) then
do return end;
end
local children = childTable[mother];
if ( children == nil ) then
children = {};
childCount[mother] = 0;
end
--if you're historical, you might already be in the list!
if ( not isHistorical(unit) ) then
local alternate = idToHistoricalUnit[unit.hist_figure_id];
if ( alternate == nil ) then
--no alternate, fall through to base case
else
--we do have an alternate: copy his childIndex
result[unit] = result[alternate];
do return end;
end
end
local count = childCount[mother];
children[count] = unit;
childCount[mother] = count+1;
childTable[mother] = children;
result[unit] = count;
end
for index,unit in pairs(allUnits) do
helper(unit);
end
return result;
end
childIndexTable = computeChildIndexTable();
local aliveNames = {};
local nameHistogram = {};
local aliveNameHistogram = {};
local localNameHistogram = {};
local localAliveNameHistogram = {};
local nameFounder = {};
local nameLeader = {};
local localNameLeader = {};
local localNameFounder = {};
local outOfNames = false;
local mostRecentName = -1;
function handleNewName(dwarf)
function newNameHelper(name)
if ( name == -1 ) then
do return end;
end
local old = nameAge[name];
if ( old == nil ) then
nameAge[name] = age[dwarf];
nameFounder[name] = dwarf;
--[[print(string.format("Family founder of %-15s: %s",
df.global.world.raws.language.translations[0].words[name].value,
dfhack.TranslateName(dwarf.name)));]]
mostRecentName = name;
else
if ( age[dwarf] < old ) then
nameAge[name] = age[dwarf];
end
end
allNames[name] = 1;
old = nameHistogram[name];
if ( old == nil ) then
nameHistogram[name] = 1;
else
nameHistogram[name] = old+1;
end
local alive=false;
local isLocal;
if ( isHistorical(dwarf) ) then
if ( dwarf.died_year == -1 ) then
alive = true;
end
if ( historicalToLocal[dwarf] ~= nil ) then
isLocal = true;
else
isLocal = false;
end
else
isLocal = true;
if ( not dwarf.flags1.dead ) then
alive = true;
end
end
if ( isLocal ) then
old = localNameHistogram[name];
if ( old == nil ) then
localNameHistogram[name] = 1;
localNameFounder[name] = dwarf;
else
localNameHistogram[name] = old+1;
end
end
if ( alive ) then
old = aliveNameHistogram[name];
if ( old == nil ) then
nameLeader[name] = dwarf;
aliveNameHistogram[name] = 1;
else
aliveNameHistogram[name] = old+1;
end
end
if ( alive and isLocal ) then
aliveNames[name] = 1;
old = localAliveNameHistogram[name];
if ( old == nil ) then
localAliveNameHistogram[name] = 1;
localNameLeader[name] = dwarf;
else
localAliveNameHistogram[name] = old+1;
end
end
end
for i=0,1 do
local name = dwarf.name.words[i];
newNameHelper(name);
end
end
function resolveConflict(dwarf)
local max = getMax(df.global.world.raws.language.words);
local first = math.random(max)-1;
local guess = first;
while (true) do
if ( nameAge[guess] == nil ) then
dwarf.name.words[1] = guess;
break;
end
guess = (guess+1)%max;
if ( guess >= max ) then
dfhack.error("guess >= max! " .. guess .. ", " .. max);
end
if ( outOfNames or guess == first ) then
outOfNames = true;
dwarf.name.words[1] = first;
--TODO: duplicate full names-----------------------------------------------------
--do return end;
break;
end
end
detectAndResolveConflicts(dwarf);
end
function detectAndResolveConflicts(dwarf)
local name = dfhack.TranslateName(dwarf.name);
if ( allNames[name] ~= nil ) then
resolveConflict(dwarf);
do return end;
end
if ( dwarf.name.words[0] == dwarf.name.words[1] ) then
resolveConflict(dwarf);
do return end;
end
if ( dwarf.name.words[1] == -1 ) then
resolveConflict(dwarf);
do return end;
end
if ( dwarf.name.words[0] == -1 ) then
dwarf.name.words[0] = dwarf.name.words[1];
dwarf.name.words[1] = -1;
resolveConflict(dwarf);
local temp = dwarf.name.words[0];
dwarf.name.words[0] = dwarf.name.words[1];
dwarf.name.words[1] = temp;
end
end
function sortName(dwarf)
local name0 = dwarf.name.words[0];
local name1 = dwarf.name.words[1];
if ( name0 == -1 or name1 == -1 ) then
do return end;
end
local age0 = nameAge[name0];
local age1 = nameAge[name1];
if ( age1 ~= nil and (age0 == nil or age1 < age0) ) then
dwarf.name.words[0] = name1;
dwarf.name.words[1] = name0;
elseif ( age1 == age0 ) then
local str1 = df.global.world.raws.language.translations[0].words[name0].value;
local str2 = df.global.world.raws.language.translations[0].words[name1].value;
if ( str2 < str1 ) then
dwarf.name.words[0] = name1;
dwarf.name.words[1] = name0;
end
end
end
helper = function(dwarf)
if ( dwarf.race ~= dwarfRace ) then
do return end;
end
--sortName(dwarf);
if ( not isHistorical(dwarf) ) then
local hist_me = idToHistoricalUnit[dwarf.hist_figure_id];
if ( hist_me ~= nil ) then
dwarf.name.words[0] = hist_me.name.words[0];
dwarf.name.words[1] = hist_me.name.words[1];
do return end;
end
end
--print("id = " .. dwarf.id);
local parent1 = getParent(dwarf, "mother");
local parent2 = getParent(dwarf, "father");
local oldName0 = dwarf.name.words[0];
local oldName1 = dwarf.name.words[1];
local oldNameString = dfhack.TranslateName(dwarf.name);
if ( oldName0 == -1 or oldName1 == -1 ) then
handleNewName(dwarf);
do return end; -- don't mess with weird shit
end
if ( parent1 == nil and parent2 == nil ) then
--print("Generating unique last name for " .. dfhack.TranslateName(dwarf.name));
local seed;
if ( isHistorical(dwarf) ) then
seed = dwarf.id;
else
local hist_me = idToHistoricalUnit[dwarf.hist_figure_id];
if ( hist_me ~= nil ) then
seed = hist_me.id;
else
seed = dwarf.id;
end
end
math.randomseed(seed);
--print("seed = " .. seed);
local max = getMax(df.global.world.raws.language.words);
--print("max = " .. max);
local first = math.random(max)-1;
local guess = first;
while(true) do
if ( nameAge[guess] == nil ) then
dwarf.name.words[0] = guess;
break;
end
guess = (guess+1)%max;
if ( outOfNames or guess == first ) then
outOfNames = true;
dwarf.name.words[0] = first;
--dwarf.name.words[0] = mostRecentName;
--dwarf.name.words[1] = mostRecentName; -- gonna happen anyway
break;
end;
end
first = math.random(max)-1;
if ( first == dwarf.name.words[0] ) then
first = (first+1)%max;
end
guess = first;
local lastNonduplicate;
while(true) do
if ( guess ~= dwarf.name.words[0] and nameAge[guess] == nil ) then
dwarf.name.words[1] = guess;
break;
end
local fullName = dfhack.TranslateName(dwarf.name);
if ( allNames[fullName] == nil ) then
lastNonduplicate = guess;
end
guess = (guess+1)%max;
if ( outOfNames or guess == first ) then
outOfNames = true;
dwarf.name.words[1] = lastNonduplicate;
--dwarf.name.words[1] = mostRecentName;
break;
end;
end
sortName(dwarf);
local newNameString = dfhack.TranslateName(dwarf.name);
if ( oldNameString ~= newNameString ) then
print("No parents: giving new unique last names (if possible):");
print(" old name = " .. oldNameString);
print(" new name = " .. newNameString);
print();
end
--print(" " .. dfhack.TranslateName(dwarf.name));
handleNewName(dwarf);
do return end;
end
if ( parent1 == nil or parent2 == nil ) then
handleNewName(dwarf);
do return end;
end
local nameTable = {};
nameTable[1] = parent1.name.words[0];
nameTable[2] = parent1.name.words[1];
nameTable[3] = parent2.name.words[0];
nameTable[4] = parent2.name.words[1];
for i=1,5 do
for j=2,4 do
local temp1 = nameTable[j-1];
local temp2 = nameTable[j ];
local age1 = nameAge[temp1] or 1000000;
local age2 = nameAge[temp2] or 1000000;
if ( age2 < age1 ) then
nameTable[j-1] = temp2;
nameTable[j ] = temp1;
elseif ( age2 == age1 ) then
--both equal: go by alphabetic
local str1 = df.global.world.raws.language.translations[0].words[temp1].value;
local str2 = df.global.world.raws.language.translations[0].words[temp2].value;
if ( str2 < str1 ) then
nameTable[j-1] = temp2;
nameTable[j ] = temp1;
end
end
end
end
local newName0 = -1;
local newName1 = -1;
local childIndex = childIndexTable[dwarf];
childIndex = childIndex % 6;
--[[if ( childIndex == 0 ) then
newName0 = nameTable[1];
newName1 = nameTable[2];
elseif ( childIndex == 1 ) then
newName0 = nameTable[1];
newName1 = nameTable[3];
elseif ( childIndex == 2 ) then
newName0 = nameTable[1];
newName1 = nameTable[4];
elseif ( childIndex == 3 ) then
newName0 = nameTable[2];
newName1 = nameTable[3];
elseif ( childIndex == 4 ) then
newName0 = nameTable[2];
newName1 = nameTable[4];
elseif ( childIndex == 5 ) then
newName0 = nameTable[3];
newName1 = nameTable[4];
else
dfhack.error("Invalid child index: " .. childIndex);
end--]]
--[[newName0 = nameTable[1];
newName1 = nameTable[2];
if ( newName1 == newName0 ) then
newName1 = nameTable[3];
if ( newName1 == newName0 ) then
newName1 = nameTable[4];
end
end--]]
if ( childIndex == 0 ) then
newName0 = nameTable[1];
newName1 = nameTable[2];
elseif ( childIndex == 1 ) then
newName0 = nameTable[3];
newName1 = nameTable[4];
elseif ( childIndex == 2 ) then
newName0 = nameTable[1];
newName1 = nameTable[3];
elseif ( childIndex == 3 ) then
newName0 = nameTable[2];
newName1 = nameTable[4];
elseif ( childIndex == 4 ) then
newName0 = nameTable[1];
newName1 = nameTable[4];
elseif ( childIndex == 5 ) then
newName0 = nameTable[2];
newName1 = nameTable[3];
else
dfhack.error("Invalid child index: " .. childIndex);
end
local oldNameString = dfhack.TranslateName(dwarf.name);
dwarf.name.words[0] = newName0;
dwarf.name.words[1] = newName1;
if ( nameScheme == 'motherName' ) then
if ( parent1 == nil ) then
parent1 = parent2;
end
if ( parent1 == nil ) then
do return end;
end
dwarf.name.words[0] = parent1.name.words[0];
dwarf.name.words[1] = parent1.name.words[1];
--do return end;
elseif ( nameScheme == 'fatherName' ) then
if ( parent2 == nil ) then
parent2 = parent1;
end
if ( parent2 == nil ) then
do return end;
end
dwarf.name.words[0] = parent2.name.words[0];
dwarf.name.words[1] = parent2.name.words[1];
elseif ( nameScheme == 'eldestParent' ) then
local olderParent;
if ( parent1 == nil and parent2 ~= nil ) then
olderParent = parent2;
elseif ( parent2 == nil and parent1 ~= nil ) then
olderParent = parent1;
else
if ( parent1 == nil or parent2 == nil ) then
dfhack.error('WTF?');
end
if ( age[parent1] == age[parent2] ) then
print('What a coincidence! Parents with the exact same age.');
olderParent = parent1;
elseif ( age[parent1] < age[parent2] ) then
olderParent = parent1;
else
olderParent = parent2;
end
end
dwarf.name.words[0] = olderParent.name.words[0];
dwarf.name.words[1] = olderParent.name.words[1];
end
--resolve conflicts
--choose the same new name in the event of a conflict
local seed;
if ( isHistorical(dwarf) ) then
seed = dwarf.id;
else
local hist_me = idToHistoricalUnit[dwarf.hist_figure_id];
if ( hist_me ~= nil ) then
seed = hist_me.id;
else
seed = dwarf.id;
end
end
--print("seed = " .. seed);
math.randomseed(seed);
if ( nameScheme == 'default' ) then
detectAndResolveConflicts(dwarf);
sortName(dwarf);
--only handleNewName when their name is finalized!
handleNewName(dwarf);
end
local newNameString = dfhack.TranslateName(dwarf.name);
if ( oldNameString ~= newNameString ) then
print("old name = " .. oldNameString);
print("new name = " .. newNameString);
print();
end
end
for index,dwarf in pairs(allUnits) do
helper(dwarf);
end
local influence = {};
function getBoss(unit)
local name0 = unit.name.words[0];
local name1 = unit.name.words[1];
local leader0 = nameLeader[name0];
local leader1 = nameLeader[name1];
if ( leader0 == unit ) then
return leader1;
end
if ( leader1 == unit ) then
return leader0;
end
dfhack.error("WTF?");
end
function computeInfluence(name)
local leader = nameLeader[name];
if ( leader == nil ) then
--influence[] = 0;
return 0;
end
local otherName = leader.name.words[0];
if ( otherName == name ) then
otherName = leader.name.words[1];
end
local inf = influence[leader] or 0;
inf = inf + (aliveNameHistogram[name] or 0);
influence[leader] = inf;
local boss = getBoss(leader);
if ( boss == leader ) then
local count2 = aliveNameHistogram[otherName] or 0;
influence[leader] = inf + count2;
return influence[leader];
end
--he's our boss: add our influence to him, all the way up
while(true) do
local count1 = influence[boss] or 0;
influence[boss] = count1 + aliveNameHistogram[name];
local lastBoss = boss;
boss = getBoss(boss);
if ( boss == lastBoss ) then
break;
end
end
return influence[leader];
end
for name,_ in pairs(allNames) do
computeInfluence(name);
end
--print out the age of each name
function printNameInformation()
local count = 0;
local newList = {};
for name,_ in pairs(allNames) do
newList[count] = name;
count = count+1;
end
function ageLessThan(a,b)
if ( a == b ) then
do return false end;
end
local name1 = newList[a];
local name2 = newList[b];
local age1 = nil;
local age2 = nil;
if ( sortOutputBy == 'alphabetic' ) then
--TODO: ------------------------------------------------------------------------------------this line for correct translations
local str1 = df.global.world.raws.language.translations[0].words[name1].value;
local str2 = df.global.world.raws.language.translations[0].words[name2].value;
if ( doReverseOutputOrder ) then
do return str2 < str1 end;
else
do return str1 < str2 end;
end
elseif ( sortOutputBy == 'aliveUses' ) then
age1 = aliveNameHistogram[name1];
age2 = aliveNameHistogram[name2];
elseif ( sortOutputBy == 'uses' ) then
age1 = nameHistogram[name1];
age2 = nameHistogram[name2];
elseif ( sortOutputBy == 'fortUses' ) then
age1 = localNameHistogram[name1];
age2 = localNameHistogram[name2];
elseif ( sortOutputBy == 'fortAliveUses' ) then
age1 = localAliveNameHistogram[name1];
age2 = localAliveNameHistogram[name2];
elseif ( sortOutputBy == 'nameAge' ) then
age1 = nameAge[name1];
age2 = nameAge[name2];
elseif ( sortOutputBy == 'leaderAge' ) then
if ( nameLeader[name1] == nil ) then
age1 = nil;
else
age1 = age[nameLeader[name1]];
end
if ( nameLeader[name2] == nil ) then
age2 = nil;
else
age2 = age[nameLeader[name2]];
end
elseif ( sortOutputBy == 'fortLeaderAge' ) then
if ( localNameLeader[name1] == nil ) then
age1 = nil;
else
age1 = age[localNameLeader[name1]];
end
if ( localNameLeader[name2] == nil ) then
age2 = nil;
else
age2 = age[localNameLeader[name2]];
end
elseif ( sortOutputBy == 'fortNameAge' ) then
if ( localNameFounder[name1] == nil ) then
age1 = nil;
else
age1 = age[localNameFounder[name1]];
end
if ( localNameFounder[name2] == nil ) then
age2 = nil;
else
age2 = age[localNameFounder[name2]];
end
elseif ( sortOutputBy == 'influence') then
if ( nameLeader[name1] == nil ) then
age1 = nil;
else
age1 = influence[nameLeader[name1]];
end
if ( nameLeader[name2] == nil ) then
age2 = nil;
else
age2 = influence[nameLeader[name2]];
end
else
dfhack.error('Invalid sortOutputBy: "' .. sortOutputBy .. '"');
end
if ( age1 == nil and age2 == nil ) then
do return false end;
end
if ( doReverseOutputOrder ) then
if ( age2 == nil ) then
do return true end;
end
if ( age1 == nil ) then
do return false end;
end
else
--not applicables first
if ( age2 == nil ) then
do return false end;
end
if ( age1 == nil ) then
do return true end;
end
end
if ( doReverseOutputOrder ) then
local temp = age1;
age1 = age2;
age2 = temp;
end
if (age1 < age2) then
do return true end;
elseif (age1 == age2) then
--TODO: other orderings?
--mergesort is stable, so this will be consistent with the current ordering
do return false end;
else
--age1 > age2
do return false end;
end
dfhack.error("This should not be reachable.");
end
sort(newList, ageLessThan, 0, count);
if ( outputType == 'printAllNames' ) then
print("Printing all names:");
elseif (outputType == 'printFortNames' ) then
print("Printing all names represented in this fort at some point in the past or present:");
elseif (outputType == 'printAllAliveNames') then
print("Printing all names with at least one living dwarf:");
elseif (outputType == 'printFortAliveNames') then
print("Printing all names with at least one living dwarf in the current fort:");
else
dfhack.error('Invalid output type: "' .. outputType .. '"');
end
local sortOutputByStr = nil;
if ( sortOutputBy == 'alphabetic' ) then
sortOutputByStr = 'alphabetically';
if ( doReverseOutputOrder ) then
sortOutputByStr = sortOutputByStr .. ', from Z to A';
else
sortOutputByStr = sortOutputByStr .. ', from A to Z';
end
elseif ( sortOutputBy == 'aliveUses' ) then
sortOutputByStr = 'by number of living dwarves with the name';
if ( doReverseOutputOrder ) then
sortOutputByStr = sortOutputByStr .. ', most common names last';
else
sortOutputByStr = sortOutputByStr .. ', most common names first';
end
elseif ( sortOutputBy == 'uses' ) then
sortOutputByStr = 'by number of dwarves living or dead who have had the name';
if ( doReverseOutputOrder ) then
sortOutputByStr = sortOutputByStr .. ', most common names last';
else
sortOutputByStr = sortOutputByStr .. ', most common names first';
end
elseif ( sortOutputBy == 'fortUses' ) then
sortOutputByStr = 'by number of dwarves who have lived in the current fort who have the name, living or dead';
if ( doReverseOutputOrder ) then
sortOutputByStr = sortOutputByStr .. ', most common names last';
else
sortOutputByStr = sortOutputByStr .. ', most common names first';
end
elseif ( sortOutputBy == 'fortAliveUses' ) then
sortOutputByStr = 'by number of living dwarves in the current fort who have the name';
if ( doReverseOutputOrder ) then
sortOutputByStr = sortOutputByStr .. ', most common names last';
else
sortOutputByStr = sortOutputByStr .. ', most common names first';
end
elseif ( sortOutputBy == 'nameAge' ) then
sortOutputByStr = 'by time since the first dwarf to have the name';
if ( doReverseOutputOrder ) then
sortOutputByStr = sortOutputByStr .. ', oldest names last';
else
sortOutputByStr = sortOutputByStr .. ', oldest names first';
end
elseif ( sortOutputBy == 'leaderAge' ) then
sortOutputByStr = 'by age of the eldest dwarf with the name';
if ( doReverseOutputOrder ) then
sortOutputByStr = sortOutputByStr .. ', oldest last';
else
sortOutputByStr = sortOutputByStr .. ', oldest first';
end
elseif ( sortOutputBy == 'fortLeaderAge' ) then
sortOutputByStr = 'by age of the eldest dwarf in the current fort with the name';
if ( doReverseOutputOrder ) then
sortOutputByStr = sortOutputByStr .. ', oldest last';
else
sortOutputByStr = sortOutputByStr .. ', oldest first';
end
elseif ( sortOutputBy == 'fortNameAge' ) then
sortOutputByStr = 'by time since the birth of the first dwarf who lived in the current fort with the name';
if ( doReverseOutputOrder ) then
sortOutputByStr = sortOutputByStr .. ', oldest last';
else
sortOutputByStr = sortOutputByStr .. ', oldest first';
end
--[[elseif ( sortOutputBy == '' ) then
sortOutputByStr = '';
if ( doReverseOutputOrder ) then
sortOutputByStr = sortOutputByStr .. '';
else
sortOutputByStr = sortOutputByStr .. '';
end--]]
elseif ( sortOutputBy == 'influence') then
sortOutputByStr = 'by level of influence';
if ( doReverseOutputOrder ) then
sortOutputByStr = sortOutputByStr .. ', most influential last';
else
sortOutputByStr = sortOutputByStr .. ', most influential first';
end
else
dfhack.error('Invalid sortOutputBy: "' .. sortOutputBy .. '"');
end
if ( sortOutputByStr == nil ) then
dfhack.error('sortOutputByStr is nil');
end
print("Sorting " .. sortOutputByStr);
function helper(i)
local name = newList[i];
--print(name);
if ( name == -1 ) then