-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstrategy.lua
More file actions
1320 lines (1307 loc) · 65.2 KB
/
Copy pathstrategy.lua
File metadata and controls
1320 lines (1307 loc) · 65.2 KB
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
--制定公主蜂和雄蜂的选择策略
local M = {}
local component = require("component")
local robot = require("robot")
local mutations = require("mutations")
local upgrade_me = component.upgrade_me--[[@as table]]
local doUntil = require("doUntil")
local device = require("device")
local bot = require("bot")
local beeData = require("beeData")
local chromosomeList = {"species", "speed", "lifespan", "fertility", "flowering", "flowerProvider", "territory", "effect", "temperatureTolerance", "humidityTolerance", "nocturnal", "tolerantFlyer", "caveDwelling"}
function M.mutate(princessSlot, droneSlot, targetSpecies, mutation)--单步突变
--参与配对的杂交雄蜂被归为三类:亲代1纯合基因雄蜂(11型)、亲代2纯合基因雄蜂(22型)、双亲杂合基因雄蜂(12型)
--突变过程存在种族基因存在丢失的可能,此时将返回nil(附带退回的备选雄蜂槽位),待上级函数重新获取可用母本后继续。
--由于退出时不丢弃已有雄蜂,且已有雄蜂均打上了该品种基因突变标签,故可在下次突变调用中继承已有的雄蜂基因池。
--1.校验输入
if bot.inventory[princessSlot].type ~= "beePrincess" or bot.inventory[droneSlot].type ~= "beeDrone" or not targetSpecies or not mutation then
error(string.format("错误的调用strategy.mutate(%d, %d, %s)",princessSlot, droneSlot, mutation.name))
end
for _, chromosome in pairs(chromosomeList) do
local p1, p2 = bot.inventory[princessSlot][chromosome][1], bot.inventory[princessSlot][chromosome][2]
local d1, d2 = bot.inventory[droneSlot][chromosome][1], bot.inventory[droneSlot][chromosome][2]
if chromosome == "species" then
local function isValid(gene)
return gene == mutation.parents[1] or gene == mutation.parents[2]
end
if not (isValid(p1) and isValid(p2) and isValid(d1) and isValid(d2)) then
error(string.format("错误的调用strategy.mutate(%d, %d, %s),参与突变的公主蜂和雄蜂含有非亲本的species基因",princessSlot, droneSlot, mutation.name))
end
local hasP1 = p1 == mutation.parents[1] or p2 == mutation.parents[1] or d1 == mutation.parents[1] or d2 == mutation.parents[1]
local hasP2 = p1 == mutation.parents[2] or p2 == mutation.parents[2] or d1 == mutation.parents[2] or d2 == mutation.parents[2]
if not (hasP1 and hasP2) then
error(string.format("错误的调用strategy.mutate(%d, %d, %s),参与突变的公主蜂和雄蜂无法凑齐突变所需品种",princessSlot, droneSlot, mutation.name))
end
elseif chromosome ~= "speed" and chromosome ~= "lifespan" and chromosome ~= "effect" then
if p1 ~= p2 or d1 ~= d2 or p1 ~= d1 then
error(string.format("错误的调用strategy.mutate(%d, %d, %s),参与突变的公主蜂和雄蜂在 %s 基因上不为同种纯合",princessSlot, droneSlot, mutation.name, chromosome))
end
end
end
local allele1Genes, allele2Genes = {}, {}
for _, chromosome in pairs(chromosomeList) do
allele1Genes[chromosome] = bot.inventory[princessSlot][chromosome][1]
allele2Genes[chromosome] = bot.inventory[droneSlot][chromosome][2]
end
local previousLabel = bot.inventoryLabel
bot.inventoryLabel = "mutate:"..targetSpecies
bot.inventory[droneSlot].inventoryLabel = bot.inventoryLabel
local targetBeeSlots = {}
if mutation.dimension then
--2.执行突变(手动突变分支)
device.destruct()
print(mutation.name.."蜂突变仅在维度 "..mutation.dimension.." 发生,请手动前往指定维度突变,将发生突变的蜜蜂与公主蜂放回物品栏")
while true do
local input
while input ~= "Y" and input ~= "y" do
io.write("确认突变完成并检查物品栏[Y/n]:")
input = io.read()
if input == "N" or input == "n" then
error("已取消突变")
end
end
princessSlot = nil
targetBeeSlots = {}
for _,slot in pairs(bot.getItemsWithLabel(bot.inventoryLabel)) do
if bot.inventory[slot].name == "Forestry:beePrincessGE" then
if princessSlot then
princessSlot = "错误:出现了两只公主蜂"
break
end
princessSlot = slot
if bot.inventory[slot].species[1] == targetSpecies or bot.inventory[slot].species[2] == targetSpecies then
table.insert(targetBeeSlots, 1, slot)
end
end
if bot.inventory[slot].name == "Forestry:beeDroneGE" and (bot.inventory[slot].species[1] == targetSpecies or bot.inventory[slot].species[2] == targetSpecies) then
table.insert(targetBeeSlots, slot)
end
end
if next(targetBeeSlots) and type(princessSlot) == "number" then
break
else
if type(princessSlot) == "string" then
print("错误:出现了两只公主蜂")
elseif type(princessSlot) == "number" then
print("未找到突变成功的蜜蜂")
else
print("未找到公主蜂")
end
end
end
else
--2.执行突变(自动突变分支)
if mutation.foundation and not bot.checkItem({name = mutation.foundation.name, damage = mutation.foundation.damage}) then
doUntil(function ()
return bot.checkItem({name = mutation.foundation.name, damage = mutation.foundation.damage})
end, "缺少突变所需的基石:"..mutation.foundation.label)
end
local function nextGeneration(droneSlot)--追踪公主蜂
device.nextGeneration(princessSlot, droneSlot, mutation)
princessSlot = nil
for _,slot in pairs(bot.getItemsWithLabel(bot.inventoryLabel)) do
if bot.inventory[slot].type == "beePrincess" then
if princessSlot then
error("错误的调用strategy.mutate().nextGeneration,突变过程中出现了两只公主蜂")
end
princessSlot = slot
end
end
if not princessSlot then
error("错误的调用strategy.mutate().nextGeneration,突变过程中未找到公主蜂")
end
end
nextGeneration(droneSlot)
droneSlot = nil
local allele11, allele12, allele22 = {}, {}, {}
while true do
targetBeeSlots = {}
allele11, allele12, allele22 = {}, {}, {}
for _,slot in pairs(bot.getItemsWithLabel(bot.inventoryLabel)) do
if bot.inventory[slot].type == "beeDrone" then
local d1, d2 = bot.inventory[slot].species[1], bot.inventory[slot].species[2]
if d1 == targetSpecies or d2 == targetSpecies then
table.insert(targetBeeSlots, slot)
elseif (d1 == mutation.parents[1] and d2 == mutation.parents[2]) or (d1 == mutation.parents[2] and d2 == mutation.parents[1]) then
table.insert(allele12, slot)
elseif d1 == mutation.parents[1] and d2 == mutation.parents[1] then
table.insert(allele11, slot)
elseif d1 == mutation.parents[2] and d2 == mutation.parents[2] then
table.insert(allele22, slot)
else
robot.select(slot)
robot.dropUp()
end
end
end
--丢弃杂蜂
for _, allele in pairs({allele11, allele12, allele22}) do
for i=#allele,4,-1 do
robot.select(allele[i])
robot.dropUp()
table.remove(allele, i)
end
end
if bot.inventory[princessSlot].species[1] == targetSpecies or bot.inventory[princessSlot].species[2] == targetSpecies then
table.insert(targetBeeSlots, 1, princessSlot)
end
if #targetBeeSlots > 0 then
break
end
if p1 == p2 then
if p1 == mutation.parents[1] then
local droneSlot = allele22[1] or allele12[1]
if droneSlot then
nextGeneration(droneSlot)
else
bot.inventoryLabel = previousLabel
return nil, princessSlot
end
elseif p1 == mutation.parents[2] then
local droneSlot = allele11[1] or allele12[1]
if droneSlot then
nextGeneration(droneSlot)
else
bot.inventoryLabel = previousLabel
return nil, princessSlot
end
else
bot.inventoryLabel = previousLabel
return nil, princessSlot, allele22[1] or allele12[1]
end
elseif (p1 == mutation.parents[1] and p2 == mutation.parents[2]) or (p1 == mutation.parents[2] and p2 == mutation.parents[1]) then
local lack11 = #allele11 == 1 and robot.count(allele11[1]) == 1
local lack22 = #allele22 == 1 and robot.count(allele22[1]) == 1
local droneSlot
if #allele11 == 0 or #allele22 == 0 then
droneSlot = allele12[1] or allele11[1] or allele22[1]
elseif lack11 and not lack22 then
droneSlot = allele11[1] or allele12[1] or allele22[1]
elseif lack22 and not lack11 then
droneSlot = allele22[1] or allele12[1] or allele11[1]
else
droneSlot = allele12[1] or allele11[1] or allele22[1]
end
if droneSlot then
nextGeneration(droneSlot)
else
bot.inventoryLabel = previousLabel
return nil, princessSlot
end
else
bot.inventoryLabel = previousLabel
return nil, princessSlot, allele12[1] or allele22[1] or allele11[1]
end
end
end
--3.丢弃杂蜂并返回
for _,slot in pairs(bot.getItemsWithLabel(bot.inventoryLabel)) do
local isTarget = false
for _, tSlot in pairs(targetBeeSlots) do
if slot == tSlot then
isTarget = true
break
end
end
if not isTarget and slot ~= princessSlot then
robot.select(slot)
if bot.inventory[slot].type == "beeDrone" then
local isPure1, isPure2 = true, true
for _, chromosome in pairs(chromosomeList) do
if bot.inventory[slot][chromosome][1] ~= allele1Genes[chromosome] or bot.inventory[slot][chromosome][2] ~= allele1Genes[chromosome] then
isPure1 = false
end
if bot.inventory[slot][chromosome][1] ~= allele2Genes[chromosome] or bot.inventory[slot][chromosome][2] ~= allele2Genes[chromosome] then
isPure2 = false
end
end
if isPure1 or isPure2 then
upgrade_me.sendItems()
else
robot.dropUp()
end
else
robot.dropUp()
end
end
end
bot.inventoryLabel = previousLabel
bot.inventory[princessSlot].inventoryLabel = previousLabel
for _, slot in pairs(targetBeeSlots) do
bot.inventory[slot].inventoryLabel = previousLabel
end
return targetBeeSlots, princessSlot
end
function M.purify(princessSlot, droneSlot, targetGenes, assistantDroneSlot, labelSuffix)--纯化
--目标基因分为以下四类:生育基因、突变产生的值得保留的新基因(Ⅱ类基因)、样板蜂已有的基因(Ⅲ类基因)、突变产生与样板已有重合的基因(不纳入计算因素)
--提纯过程存在基因丢失的概率,若导致Ⅱ类基因丢失,则应当返回nil,待上级函数调用冲洗函数将公主蜂洗成母本基因,并调用突变函数进行新一轮的突变获取新的Ⅱ类基因后,再调用此函数继续提纯。
--由于返回时不丢弃已有雄蜂,且已有雄蜂均打上了该品种基因提纯标签,故可在下一轮调用中继承已有的提纯进度。
local previousLabel = bot.inventoryLabel
bot.inventoryLabel = "purify"..targetGenes.species..(labelSuffix or "")
bot.inventory[princessSlot].inventoryLabel = bot.inventoryLabel
if droneSlot and droneSlot ~= assistantDroneSlot then
bot.inventory[droneSlot].inventoryLabel = bot.inventoryLabel
end
--1.校验输入,对基因进行分类
local newGenes, templateGenes = {}, {}
if bot.inventory[assistantDroneSlot].fertility[1] ~= 4 or bot.inventory[assistantDroneSlot].fertility[2] ~= 4 then
error("错误的调用strategy("..tostring(princessSlot)..","..tostring(droneSlot)..","..tostring(assistantDroneSlot)..","..tostring(targetGenes.species).."),样板雄蜂的生育基因必须为纯合4x")
end
for _, chromosome in pairs(chromosomeList) do
local gene = bot.inventory[princessSlot][chromosome]
--校验目标基因是否存在
if gene[1] ~= targetGenes[chromosome] and gene[2] ~= targetGenes[chromosome] and bot.inventory[droneSlot][chromosome][1] ~= targetGenes[chromosome] and bot.inventory[droneSlot][chromosome][2] ~= targetGenes[chromosome]
--and bot.inventory[assistantDroneSlot][chromosome][1] ~= targetGenes[chromosome] and bot.inventory[assistantDroneSlot][chromosome][2] ~= targetGenes[chromosome] or bot.inventory[assistantDroneSlot][chromosome][1] ~= bot.inventory[assistantDroneSlot][chromosome][2] then
and bot.inventory[assistantDroneSlot][chromosome][1] ~= targetGenes[chromosome] and bot.inventory[assistantDroneSlot][chromosome][2] ~= targetGenes[chromosome] then--初始凛冬雄蜂不纯合的临时解决方案,就这么先跑着吧,哪天出问题了再改
error("错误的调用strategy.purify("..tostring(princessSlot)..","..tostring(droneSlot)..","..tostring(chromosome).."="..tostring(targetGenes[chromosome])..")")
--分类
elseif chromosome ~= "fertility" and not(gene[1] == targetGenes[chromosome] and gene[2] == targetGenes[chromosome] and bot.inventory[droneSlot][chromosome][1] == targetGenes[chromosome]
and bot.inventory[droneSlot][chromosome][2] == targetGenes[chromosome] and bot.inventory[assistantDroneSlot][chromosome][1] == targetGenes[chromosome]) then
if bot.inventory[assistantDroneSlot][chromosome][1] == targetGenes[chromosome] then
table.insert(templateGenes, chromosome)
else
table.insert(newGenes, chromosome)
end
end
end
local function getDrones(highFertilityOnly, includeAssistant)
local result = {}
for _,slot in pairs(bot.getItemsWithLabel(bot.inventoryLabel)) do
if bot.inventory[slot].type == "beeDrone" and (not highFertilityOnly or bot.inventory[slot].fertility[1] == 4 and bot.inventory[slot].fertility[2] == 4) then
table.insert(result, slot)
end
end
if includeAssistant then
local isPresent = false
for _, slot in pairs(result) do
if slot == assistantDroneSlot then isPresent = true break end
end
if not isPresent then
table.insert(result, assistantDroneSlot)
end
end
return result
end
local function nextGeneration(droneSlot)--追踪公主蜂
device.nextGeneration(princessSlot, droneSlot)
princessSlot = nil
for _,slot in pairs(bot.getItemsWithLabel(bot.inventoryLabel)) do
if bot.inventory[slot].type == "beePrincess" then
if princessSlot then
error("错误的调用strategy.purify().nextGeneration,提纯过程中出现了两只公主蜂")
end
princessSlot = slot
end
end
if not princessSlot then
error("错误的调用strategy.purify().nextGeneration,提纯过程中未找到公主蜂")
end
end
--2.提纯生育基因,终止条件是所有生育为纯合4x的雄蜂携带全部Ⅱ类基因且公主蜂生育基因为纯合4x。
local newGenesWithHighFertility
local function checkNewGenesWithHighFertility()
--检查
newGenesWithHighFertility = {}
for _,slot in pairs(bot.getItemsWithLabel(bot.inventoryLabel)) do
if (bot.inventory[slot].type == "beeDrone" or bot.inventory[slot].type == "beePrincess") and bot.inventory[slot].fertility[1] == 4 and bot.inventory[slot].fertility[2] == 4 then
for _,chromosome in pairs(newGenes) do
if bot.inventory[slot][chromosome][1] == targetGenes[chromosome] or bot.inventory[slot][chromosome][2] == targetGenes[chromosome] then
newGenesWithHighFertility[chromosome] = true
end
end
end
end
for _,chromosome in pairs(newGenes) do
if not newGenesWithHighFertility[chromosome] then
return
end
end
if bot.inventory[princessSlot].fertility[1] == 4 and bot.inventory[princessSlot].fertility[2] == 4 then
newGenesWithHighFertility = "All"
end
--丢弃不包含Ⅱ类基因的雄蜂
for _,slot in pairs(bot.getItemsWithLabel(bot.inventoryLabel)) do
if bot.inventory[slot].type == "beeDrone" and (bot.inventory[slot].fertility[1] ~= 4 or bot.inventory[slot].fertility[2] ~= 4) then
local shouldDrop = true
for _,chromosome in pairs(newGenes) do
if bot.inventory[slot][chromosome][1] == targetGenes[chromosome] or bot.inventory[slot][chromosome][2] == targetGenes[chromosome] then
shouldDrop = false
break
end
end
if shouldDrop then
robot.select(slot)
robot.dropUp()
while bot.inventory[slot] do
os.sleep(0)
end
end
end
end
end
::FERTILITY::
checkNewGenesWithHighFertility()
while newGenesWithHighFertility ~= "All" do
local weights = {}
--若公主蜂携带不在newGenesWithHighFertility表内的Ⅱ类基因,则使用样板雄蜂与公主蜂杂交
for _,chromosome in pairs(newGenes) do
if not newGenesWithHighFertility[chromosome] and (bot.inventory[princessSlot][chromosome][1] == targetGenes[chromosome] or bot.inventory[princessSlot][chromosome][2] == targetGenes[chromosome]) then
nextGeneration(assistantDroneSlot)
goto CONTINUE
end
end
--如果发生了基因丢失,直接返回nil
for _,chromosome in pairs(newGenes) do
local isLost = true
if bot.inventory[princessSlot][chromosome][1] == targetGenes[chromosome] or bot.inventory[princessSlot][chromosome][2] == targetGenes[chromosome] then
isLost = false
else
for _,slot in pairs(getDrones()) do
if bot.inventory[slot][chromosome][1] == targetGenes[chromosome] or bot.inventory[slot][chromosome][2] == targetGenes[chromosome] then
isLost = false
break
end
end
end
if isLost then
bot.inventoryLabel = previousLabel
return nil, princessSlot
end
end
--若公主蜂生育基因不为纯合4x,则使用样板雄蜂与公主蜂杂交
if bot.inventory[princessSlot].fertility[1] ~= 4 or bot.inventory[princessSlot].fertility[2] ~= 4 then
nextGeneration(assistantDroneSlot)
goto CONTINUE
end
--选择包含最多不在newGenesWithHighFertility表内的Ⅱ类基因的雄蜂与公主蜂杂交
for _,slot in pairs(getDrones()) do
weights[slot] = 0
for _,chromosome in pairs(newGenes) do
if newGenesWithHighFertility[chromosome] then
if bot.inventory[slot][chromosome][1] == targetGenes[chromosome] then
weights[slot] = weights[slot] + 1
end
if bot.inventory[slot][chromosome][2] == targetGenes[chromosome] then
weights[slot] = weights[slot] + 1
end
elseif bot.inventory[slot][chromosome][1] == targetGenes[chromosome] or bot.inventory[slot][chromosome][2] == targetGenes[chromosome] then
if bot.inventory[slot][chromosome][1] == bot.inventory[slot][chromosome][2] then
weights[slot] = weights[slot] + 95
else
weights[slot] = weights[slot] + 64
end
end
end
if bot.inventory[slot].fertility[1] == 4 or bot.inventory[slot].fertility[2] == 4 then
weights[slot] = weights[slot] + 16
end
end
droneSlot = nil
for slot,weight in pairs(weights) do
if droneSlot then
if weight > weights[droneSlot] then
droneSlot = slot
end
else
droneSlot = slot
end
end
nextGeneration(droneSlot)
::CONTINUE::
checkNewGenesWithHighFertility()
end
--3.提纯其余所有基因
local genes = {}
for _,chromosome in pairs(newGenes) do
table.insert(genes, chromosome)
end
for _,chromosome in pairs(templateGenes) do
table.insert(genes, chromosome)
end
local function dropDrones()
--处理生育非纯合4x雄蜂
local lackGenes = {}
for _,chromosome in pairs(genes) do
local amount = (bot.inventory[princessSlot][chromosome][1] == targetGenes[chromosome] and 1 or 0) + (bot.inventory[princessSlot][chromosome][2] == targetGenes[chromosome] and 1 or 0)
for _,slot in pairs(getDrones(true)) do
amount = amount + (bot.inventory[slot][chromosome][1] == targetGenes[chromosome] and 1 or 0) + (bot.inventory[slot][chromosome][2] == targetGenes[chromosome] and 1 or 0)
if amount >= 2 then
break
end
end
if amount < 2 then
table.insert(lackGenes, chromosome)
end
end
for _,slot in pairs(getDrones()) do
local shouldDrop = true
if bot.inventory[slot].fertility[1] == 4 and bot.inventory[slot].fertility[2] == 4 then
shouldDrop = false
else
for _,chromosome in pairs(lackGenes) do
if bot.inventory[slot][chromosome][1] == targetGenes[chromosome] or bot.inventory[slot][chromosome][2] == targetGenes[chromosome] then
shouldDrop = false
break
end
end
end
if shouldDrop then
robot.select(slot)
robot.dropUp()
while bot.inventory[slot] do
os.sleep(0)
end
end
end
--处理生育纯合4x雄蜂
for i = #genes, 1, -1 do
local pureChromosome = genes[i]
if bot.inventory[princessSlot][pureChromosome][1] == targetGenes[pureChromosome] and bot.inventory[princessSlot][pureChromosome][2] == targetGenes[pureChromosome] then
local isPureEnough = true
for _,chromosome in pairs(genes) do
local amount = (bot.inventory[princessSlot][chromosome][1] == targetGenes[chromosome] and 1 or 0) + (bot.inventory[princessSlot][chromosome][2] == targetGenes[chromosome] and 1 or 0)
for _,slot in pairs(getDrones(true)) do
if bot.inventory[slot][pureChromosome][1] == targetGenes[pureChromosome] and bot.inventory[slot][pureChromosome][2] == targetGenes[pureChromosome] then
amount = amount + (bot.inventory[slot][chromosome][1] == targetGenes[chromosome] and 1 or 0) + (bot.inventory[slot][chromosome][2] == targetGenes[chromosome] and 1 or 0)
end
if amount >= 3 then
break
end
end
if amount < 3 then
isPureEnough = false
break
end
end
if isPureEnough then
for _,slot in pairs(getDrones()) do
if bot.inventory[slot][pureChromosome][1] ~= targetGenes[pureChromosome] or bot.inventory[slot][pureChromosome][2] ~= targetGenes[pureChromosome] then
robot.select(slot)
robot.dropUp()
while bot.inventory[slot] do
os.sleep(0)
end
end
end
table.remove(genes, i)
end
end
end
--处理生育纯合4x雄蜂
while true do
if not next(genes) then break end
local abundantGenes = {}
for _,chromosome in pairs(genes) do
local amount = (bot.inventory[princessSlot][chromosome][1] == targetGenes[chromosome] and 1 or 0) + (bot.inventory[princessSlot][chromosome][2] == targetGenes[chromosome] and 1 or 0)
for _,slot in pairs(getDrones(true)) do
amount = amount + (bot.inventory[slot][chromosome][1] == targetGenes[chromosome] and 1 or 0) + (bot.inventory[slot][chromosome][2] == targetGenes[chromosome] and 1 or 0)
if amount >= 7 then
abundantGenes[chromosome] = true
break
end
end
end
local weights = {}
for _,slot in pairs(getDrones()) do
local hasRareGene = false
for _,chromosome in pairs(genes) do
if not abundantGenes[chromosome] and (bot.inventory[slot][chromosome][1] == targetGenes[chromosome] or bot.inventory[slot][chromosome][2] == targetGenes[chromosome]) then
hasRareGene = true
break
end
end
if not hasRareGene then
weights[slot] = 0
for _,chromosome in pairs(genes) do
if bot.inventory[slot][chromosome][1] == targetGenes[chromosome] and bot.inventory[slot][chromosome][2] == targetGenes[chromosome] then
weights[slot] = weights[slot] + 3
elseif bot.inventory[slot][chromosome][1] == targetGenes[chromosome] or bot.inventory[slot][chromosome][2] == targetGenes[chromosome] then
weights[slot] = weights[slot] + 1
end
end
end
end
if next(weights) then
local minWeightSlot
for slot, weight in pairs(weights) do
if minWeightSlot then
if weight < weights[minWeightSlot] then
minWeightSlot = slot
end
else
minWeightSlot = slot
end
end
robot.select(minWeightSlot)
robot.dropUp()
while bot.inventory[minWeightSlot] do
os.sleep(0)
end
else
break
end
end
end
while true do
::CONTINUE2::
--若存在纯合目标基因公主蜂与纯合目标基因雄蜂,跳出循环
local hasPurePrincess = true
for _,chromosome in pairs(newGenes) do
if bot.inventory[princessSlot][chromosome][1] ~= targetGenes[chromosome] or bot.inventory[princessSlot][chromosome][2] ~= targetGenes[chromosome] then
hasPurePrincess = false
break
end
end
if hasPurePrincess then
for _,chromosome in pairs(templateGenes) do
if bot.inventory[princessSlot][chromosome][1] ~= targetGenes[chromosome] or bot.inventory[princessSlot][chromosome][2] ~= targetGenes[chromosome] then
hasPurePrincess = false
break
end
end
end
if hasPurePrincess then
local hasPureDrone = false
for _,slot in pairs(getDrones(true, true)) do
if bot.inventory[slot] and bot.inventory[slot].type == "beeDrone" and bot.inventory[slot].fertility[1] == 4 and bot.inventory[slot].fertility[2] == 4 then
local isPure = true
for _,chromosome in pairs(newGenes) do
if bot.inventory[slot][chromosome][1] ~= targetGenes[chromosome] or bot.inventory[slot][chromosome][2] ~= targetGenes[chromosome] then
isPure = false
break
end
end
if isPure then
for _,chromosome in pairs(templateGenes) do
if bot.inventory[slot][chromosome][1] ~= targetGenes[chromosome] or bot.inventory[slot][chromosome][2] ~= targetGenes[chromosome] then
isPure = false
break
end
end
end
if isPure then
droneSlot = slot
hasPureDrone = true
break
end
end
end
if hasPureDrone then
break
end
end
dropDrones()
--统计每个基因的纯合和杂合数量
local statistics = {}
for _,chromosome in pairs(genes) do
statistics[chromosome] = {0, 0}
end
for _,slot in pairs(getDrones(true, #newGenes==0)) do
for _,chromosome in pairs(genes) do
if bot.inventory[slot][chromosome][1] == targetGenes[chromosome] or bot.inventory[slot][chromosome][2] == targetGenes[chromosome] then
if bot.inventory[slot][chromosome][1] == bot.inventory[slot][chromosome][2] then
statistics[chromosome][1] = statistics[chromosome][1] + bot.inventory[slot].size
else
statistics[chromosome][2] = statistics[chromosome][2] + bot.inventory[slot].size
end
end
end
end
--若发生Ⅱ类基因丢失,退回到生育基因提纯阶段
for _,chromosome in pairs(newGenes) do
if bot.inventory[princessSlot][chromosome][1] ~= targetGenes[chromosome] and bot.inventory[princessSlot][chromosome][2] ~= targetGenes[chromosome] and statistics[chromosome] and statistics[chromosome][1] == 0 and statistics[chromosome][2] == 0 then
goto FERTILITY
end
end
--若雄蜂中无Ⅲ类基因,则与样板雄蜂杂交
for _,count in pairs(statistics) do
if count[1] == 0 and count[2] == 0 then
nextGeneration(assistantDroneSlot)
goto CONTINUE2
end
end
--根据公主蜂对应基因的基因型与雄蜂基因统计结果计算权重
local weights = {}
for _,slot in pairs(getDrones(true, #newGenes==0)) do
weights[slot] = {0, 0, 0, 0, 0, 0}
for _,chromosome in pairs(genes) do
local count = (bot.inventory[slot][chromosome][1] == targetGenes[chromosome] and 1 or 0) + (bot.inventory[slot][chromosome][2] == targetGenes[chromosome] and 1 or 0)
if bot.inventory[princessSlot][chromosome][1] == targetGenes[chromosome] and bot.inventory[princessSlot][chromosome][2] == targetGenes[chromosome] then
if count == 2 then
weights[slot][5] = weights[slot][5] + 1
elseif count == 1 then
weights[slot][6] = weights[slot][6] + 1
end
elseif bot.inventory[princessSlot][chromosome][1] == targetGenes[chromosome] or bot.inventory[princessSlot][chromosome][2] == targetGenes[chromosome] then
if count == 2 then
weights[slot][3] = weights[slot][3] + 1
elseif count == 1 then
weights[slot][4] = weights[slot][4] + 1
end
else
if count == 2 and statistics[chromosome][1] > 2 or count == 1 and statistics[chromosome][1] <= 2 then
weights[slot][1] = weights[slot][1] + 1
elseif count == 1 and statistics[chromosome][1] > 2 or count == 2 and statistics[chromosome][1] <= 2 then
weights[slot][2] = weights[slot][2] + 1
end
end
end
end
--选择权重最高的雄蜂与公主蜂杂交
droneSlot = nil
for slot,weight in pairs(weights) do
if droneSlot then
for i=1,6 do
if weights[droneSlot][i] < weight[i] then
droneSlot = slot
break
elseif weights[droneSlot][i] > weight[i] then
break
end
end
else
droneSlot = slot
end
end
nextGeneration(droneSlot)
end
--4.丢弃期间产生的所有杂蜂,将雄蜂数量繁殖到16以上后返回
for _,slot in pairs(bot.getItemsWithLabel(bot.inventoryLabel)) do
if slot ~= droneSlot and bot.inventory[slot].type == "beeDrone" then
robot.select(slot)
robot.dropUp()
end
end
while robot.count(droneSlot--[[@as number]]) < 16 do
nextGeneration(droneSlot)
for _,slot in pairs(bot.getItemsWithLabel(bot.inventoryLabel)) do
if bot.inventory[slot].type == "beeDrone" then
droneSlot = slot
break
end
end
end
bot.inventoryLabel = previousLabel
bot.inventory[princessSlot].inventoryLabel = previousLabel
if droneSlot ~= assistantDroneSlot then
bot.inventory[droneSlot].inventoryLabel = previousLabel
end
return droneSlot, princessSlot
end
function M.breedDrones(princessSlot, droneSlot, targetAmount)--繁殖雄蜂
if bot.inventory[princessSlot].type ~= "beePrincess" or bot.inventory[droneSlot].type ~= "beeDrone" then
error(string.format("错误的调用strategy.breedDrones(%d, %d, %d)",princessSlot, droneSlot, targetAmount))
end
for _, chromosome in pairs(chromosomeList) do
local p1, p2 = bot.inventory[princessSlot][chromosome][1], bot.inventory[princessSlot][chromosome][2]
local d1, d2 = bot.inventory[droneSlot][chromosome][1], bot.inventory[droneSlot][chromosome][2]
if p1 ~= p2 or d1 ~= d2 or p1 ~= d1 then
error(string.format("错误的调用strategy.breedDrones(%d, %d, %d),参与繁殖的公主蜂和雄蜂在 %s 基因上不为同种纯合",princessSlot, droneSlot, targetAmount, chromosome))
end
end
local previousLabel = bot.inventoryLabel
bot.inventoryLabel = "breedDrones:"..bot.inventory[princessSlot].species[1]
bot.inventory[droneSlot].inventoryLabel = bot.inventoryLabel
while robot.count(droneSlot) < targetAmount do
device.nextGeneration(princessSlot, droneSlot)
princessSlot = nil
for _,slot in pairs(bot.getItemsWithLabel(bot.inventoryLabel)) do
if bot.inventory[slot].type == "beePrincess" then
if princessSlot then
error("错误的调用strategy.breedDrones(),繁殖过程中出现了两只公主蜂")
end
princessSlot = slot
end
end
if not princessSlot then
error("错误的调用strategy.breedDrones().nextGeneration,繁殖过程中未找到公主蜂")
end
droneSlot = nil
for _,slot in pairs(bot.getItemsWithLabel(bot.inventoryLabel)) do
if bot.inventory[slot].type == "beeDrone" then
if droneSlot and bot.inventory[slot].tag ~= bot.inventory[droneSlot].tag then
error("错误的调用strategy.breedDrones(),繁殖过程中出现了多种基因型的雄蜂")
end
droneSlot = slot
end
end
end
bot.inventoryLabel = previousLabel
bot.inventory[princessSlot].inventoryLabel = previousLabel
bot.inventory[droneSlot].inventoryLabel = previousLabel
return princessSlot, droneSlot
end
function M.getAssistantDrones()--获取样板雄蜂
local previousLabel = bot.inventoryLabel
bot.inventoryLabel = "breedAssistantDrones"
local droneTag, princess = beeData.getAssistantBeesTag()
local droneSlot = bot.checkItem({name="Forestry:beeDroneGE",tag=droneTag}, 16)
if not droneSlot then
error("获取样板雄蜂失败:未能提供足够的样板雄蜂")
end
local temp = upgrade_me.getItemsInNetwork({label=bot.inventory[droneSlot].label})
local droneCount = 0
for _, stack in pairs(temp) do
if stack.tag == droneTag then
droneCount = stack.size
break
end
end
if droneCount < 20 then
if princess then
princess = bot.checkItem({name="Forestry:beePrincessGE",tag=princess}, 1)
if not princess then
error("获取辅助公主蜂失败:未找到现存的辅助公主蜂")
end
else
princess = beeData.getPrincessTag(true)
princess = bot.checkItem({name="Forestry:beePrincessGE",tag=princess}, 1)
if not princess then
error("培育样板雄蜂失败:未找到可用的初始公主蜂")
end
local targetGenes = {}
for _, chromosome in pairs(chromosomeList) do
targetGenes[chromosome] = bot.inventory[droneSlot][chromosome][1]
end
droneSlot, princess = M.purify(princess, droneSlot, targetGenes, droneSlot, ":assistant")
if not droneSlot then
error("培育样板雄蜂过程中发生基因丢失")
end
end
princess, droneSlot = M.breedDrones(princess, droneSlot, 48-droneCount)
beeData.updateAssistantPrincess(princess)
robot.select(princess)
upgrade_me.sendItems()
end
bot.inventoryLabel = previousLabel
local excess = robot.count(droneSlot--[[@as number]]) - 16
if excess > 0 then
robot.select(droneSlot--[[@as number]])
upgrade_me.sendItems(excess)
end
bot.inventory[droneSlot].inventoryLabel = previousLabel
return droneSlot
end
function M.newSpecies(species, mutation)--突变新品种并优化基因
--校验输入
local allele1Tag, allele2Tag = beeData.getDroneTag(mutation.parents[1]), beeData.getDroneTag(mutation.parents[2])
if not allele1Tag or not allele2Tag then
error(string.format("错误的调用strategy.newSpecies(%s, %s),突变所需的亲本品种不存在",species, mutation.name))
end
if not mutation.dimension then
local function confirmMutation()
io.write("是否继续执行突变?[Y/n]:")
local answer = io.read()
if answer ~= "Y" and answer ~= "y" then
error("突变"..mutation.name..":已取消")
end
end
if mutation.date then
print(mutation.name.."蜂突变仅在"..mutation.date[1].."到"..mutation.date[2].."之间发生")
confirmMutation()
end
if mutation.lunar_phase then
if type(mutation.lunar_phase) == "table" then
print(mutation.name.."蜂突变仅在月相在"..mutation.lunar_phase[1].."到"..mutation.lunar_phase[2].."之间发生")
else
print(mutation.name.."蜂突变仅在月相为"..mutation.lunar_phase.."时发生")
end
confirmMutation()
end
if mutation.time then
print(mutation.name.."蜂突变仅在"..mutation.time.."时发生")
confirmMutation()
end
end
--获取亲本公主蜂与雄蜂
local previousLabel = bot.inventoryLabel
bot.inventoryLabel = "newSpecies:"..species
local allele1Slot, allele2Slot, assistantDroneSlot, princessSlot
local mutatedBeeList, droneSlot
local function isTemplatedGenes(tag)
local genes = require("analyzeGenes")({name="Forestry:beeDroneGE",individual={},tag=tag})
for chromosome, gene in pairs({flowering=1,flowerProvider="extrabees.flower.rock",fertility=4,territory=1,temperatureTolerance="BOTH_5",humidityTolerance="BOTH_5",nocturnal=true,tolerantFlyer=true,caveDwelling=true}) do
if genes[chromosome][1] ~= gene or genes[chromosome][2] ~= gene then
return false
end
end
return true
end
local function getOperations(isPrincessParent, isPrincessTemplated, isAllele1Templated, isAllele2Templated)
--以最小purify步数,快速将公主蜂转换为一亲本纯种,同时让亲本蜂具备模板基因
if isPrincessParent == 1 and isAllele2Templated then
return isPrincessTemplated and {} or {"purify(1)"}, false
end
if isPrincessParent == 2 and isAllele1Templated then
return isPrincessTemplated and {} or {"purify(2)"}, true
end
if isAllele2Templated then
return {"purify(1)"}, false
end
if isAllele1Templated then
return {"purify(2)"}, true
end
return mutation.parents[1] == mutation.parents[2] and {"purify(1)"} or {"purify(2)", "purify(1)"}, false
end
::GET_PARENT_BEES::
assistantDroneSlot = M.getAssistantDrones()--[[@as number]]
princessSlot = bot.checkItem({name="Forestry:beePrincessGE",tag=beeData.getPrincessTag(true)}, 1)
if not princessSlot then
error(string.format("突变%s:无法获取公主蜂", mutation.name))
end
local isPrincessParent
if bot.inventory[princessSlot].species[1] == mutation.parents[1] and bot.inventory[princessSlot].species[2] == mutation.parents[1] then
isPrincessParent = 1
elseif bot.inventory[princessSlot].species[1] == mutation.parents[2] and bot.inventory[princessSlot].species[2] == mutation.parents[2] then
isPrincessParent = 2
end
allele1Tag, allele2Tag = beeData.getDroneTag(mutation.parents[1]), beeData.getDroneTag(mutation.parents[2])
local operations, exchanged = getOperations(isPrincessParent, isTemplatedGenes(bot.inventory[princessSlot].tag), isTemplatedGenes(allele1Tag), isTemplatedGenes(allele2Tag))
allele1Slot = bot.checkItem({name="Forestry:beeDroneGE",tag=allele1Tag}, 16)
allele2Slot = bot.checkItem({name="Forestry:beeDroneGE",tag=allele2Tag}, 16)
if not allele1Slot and not allele2Slot then
error(string.format("突变%s:缺乏必需的亲本雄蜂", mutation.name))
end
for _, operation in ipairs(operations) do
if operation == "purify(1)" then
allele1Slot, princessSlot = M.purify(princessSlot, allele1Slot, beeData.getTargetGenes(mutation.parents[1]), assistantDroneSlot, ":allele1")
if not allele1Slot then
beeData.updateUsingPrincess(princessSlot)
error("突变"..mutation.name.."亲本雄蜂1基因丢失")
end
elseif operation == "purify(2)" then
allele2Slot, princessSlot = M.purify(princessSlot, allele2Slot, beeData.getTargetGenes(mutation.parents[2]), assistantDroneSlot, ":allele2")
if not allele2Slot then
beeData.updateUsingPrincess(princessSlot)
error("突变"..mutation.name.."亲本雄蜂2基因丢失")
end
end
end
if exchanged then
if mutation.parents[1] == mutation.parents[2] then
allele1Slot = allele2Slot
elseif allele2Slot ~= assistantDroneSlot then
robot.select(allele2Slot)
upgrade_me.sendItems()
end
allele2Slot = nil
else
if mutation.parents[1] == mutation.parents[2] then
allele2Slot = allele1Slot
elseif allele1Slot ~= assistantDroneSlot then
robot.select(allele1Slot)
upgrade_me.sendItems()
end
allele1Slot = nil
end
if bot.inventory[assistantDroneSlot] and assistantDroneSlot ~= (exchanged and allele1Slot or allele2Slot) then
robot.select(assistantDroneSlot)
upgrade_me.sendItems()
assistantDroneSlot = nil
end
--执行突变
if exchanged then
robot.select(allele1Slot--[[@as number]])
upgrade_me.sendItems(robot.count(allele1Slot--[[@as number]])-1)
mutatedBeeList, princessSlot = M.mutate(princessSlot, allele1Slot, species, mutation)
allele1Slot = nil
else
robot.select(allele2Slot--[[@as number]])
upgrade_me.sendItems(robot.count(allele2Slot--[[@as number]])-1)
mutatedBeeList, princessSlot = M.mutate(princessSlot, allele2Slot, species, mutation)
allele2Slot = nil
end
if not mutatedBeeList then
beeData.updateUsingPrincess(princessSlot)
goto GET_PARENT_BEES
end
--纯化突变产生的基因
while true do
if not mutatedBeeList[1] then
beeData.updateUsingPrincess(princessSlot)
goto GET_PARENT_BEES
end
local mDrone = mutatedBeeList[1]
table.remove(mutatedBeeList, 1)
assistantDroneSlot = M.getAssistantDrones()--[[@as number]]
if mDrone == princessSlot then
mDrone = assistantDroneSlot
end
droneSlot, princessSlot = M.purify(princessSlot, mDrone, beeData.getTargetGenes(species), assistantDroneSlot, ":mutated")
if bot.inventory[assistantDroneSlot] and assistantDroneSlot ~= droneSlot and assistantDroneSlot ~= princessSlot then
robot.select(assistantDroneSlot)
upgrade_me.sendItems()
assistantDroneSlot = nil
end
if droneSlot then
break
end
end
--处理结果
for _,slot in pairs(mutatedBeeList--[[@as table]]) do
robot.select(slot)
robot.dropUp()
end
for _,slot in pairs(bot.getItemsWithLabel(bot.inventoryLabel)) do
if slot ~= droneSlot and slot ~= princessSlot then
robot.select(slot)
upgrade_me.sendItems()
end
end
bot.inventoryLabel = previousLabel
bot.inventory[princessSlot].inventoryLabel = previousLabel
bot.inventory[droneSlot].inventoryLabel = previousLabel
beeData.updateUsingPrincess(princessSlot)
local updated, oldAssistantPrincessTag = beeData.updateAssistantDrone(droneSlot)
if updated then
beeData.updateAssistantPrincess(princessSlot, oldAssistantPrincessTag)
end
return droneSlot, princessSlot
end
function M.optimizeSpecies(species)--优化现有品种
local droneTag = beeData.getDroneTag(species)
if not droneTag then
error(string.format("错误的调用strategy.optimizeSpecies(%s),该品种不存在", species))
end
local previousLabel = bot.inventoryLabel
bot.inventoryLabel = "optimizeSpecies:"..species
local droneSlot = bot.checkItem({name="Forestry:beeDroneGE",tag=droneTag}, 16)
if not droneSlot then
error(string.format("错误的调用strategy.optimizeSpecies(%s),未找到目标品种的雄蜂", species))
end
local princessSlot = bot.checkItem({name="Forestry:beePrincessGE",tag=beeData.getPrincessTag(true)}, 1)
if not princessSlot then
error(string.format("错误的调用strategy.optimizeSpecies(%s),缺少公主蜂", species))
end
local assistantDroneSlot = M.getAssistantDrones()
droneSlot, princessSlot = M.purify(princessSlot, droneSlot, beeData.getTargetGenes(species), assistantDroneSlot, ":optimize")
for _,slot in pairs(bot.getItemsWithLabel(bot.inventoryLabel)) do
if slot ~= droneSlot and slot ~= princessSlot then
robot.select(slot)
upgrade_me.sendItems()
end
end
bot.inventoryLabel = previousLabel
bot.inventory[droneSlot].inventoryLabel = previousLabel
bot.inventory[princessSlot].inventoryLabel = previousLabel
return droneSlot, princessSlot
end
function M.task(species)--制定突变链