View difference between Paste ID: EAcC1hYc and GzL6rPbY
SHOW: | | - or go back to the newest paste.
1
-- this version for computer instead of turtle
2
-- online has problems tho, displayname can be bee.species.meadows, and name is garbled but does contain lower drone princess
3
-- bad hack because we dont have proper destination slots for pushpullitem, slot must be empty
4
5-
-- b7VLgwUP--computer
5+
-- GzL6rPbY--computer
6
-- Q0LaxCKb--turtle
7
-- gXa8k1tJ
8
-- fKC8kx5b
9
-- H0CppXue
10
-- QTRQHPhM
11
-- RwTbaqpP
12
13
local apiary = peripheral.wrap("apiculture_0_0")
14
local chest = peripheral.wrap("back")
15
local maxDrones = 6
16
local minDrones = 3
17
local species = ...
18
19
bees = {}
20
stacks = {}
21
purebreedBanList = {}		--some bees have too low of fertility
22
purebreedDroneCount = 0		--we detect that by counting before and after drone counts during purebreeding
23
purebreedBee = nil			
24
25
26
shutdownMsg = nil
27
     
28
function addParent(parent, offspring)
29
    if not bees[parent] then
30
    bees[parent] = {
31
        --name = parent,
32
        score = 0,
33
        mutateFrom = {}
34
    }
35
    end
36
end
37
     
38
function addOffspring(offspring, parentss)
39
    if bees[offspring] then
40
    for i, parents in ipairs(parentss) do
41
        table.insert(bees[offspring].mutateFrom, parents)
42
    end
43
    else
44
    bees[offspring] = {
45
        score = 0,
46
        mutateFrom = parentss
47
    }
48
    end
49
    for i, parents in ipairs(parentss) do
50
    for i, parent in ipairs(parents) do
51
        addParent(parent, offspring)
52
    end
53
    end
54
end
55
     
56
	 
57
58
     
59
-- produce combinations from 1 or 2 lists
60
function choose(list, list2)
61
    local newList = {}
62
    if list2 then
63
    for i = 1, #list2 do
64
        for j = 1, #list do
65
        if list[j] ~= list[i] then
66
            table.insert(newList, {list[j], list2[i]})
67
        end
68
        end
69
    end
70
    else
71
    for i = 1, #list do
72
        for j = i, #list do
73
        if list[i] ~= list[j] then
74
            table.insert(newList, {list[i], list[j]})
75
        end
76
        end
77
    end
78
    end
79
    return newList
80
end
81
     
82
83
--bee list from Master Apiarist Database v3.1
84
--Forestry v2.3.0.7 (683)
85
--Extra Bees v1.8-dev2
86
--Magic Bees v2.1.7 (build 78)
87
88
addOffspring("Common", choose({"Forest", "Meadows", "Modest", "Wintry", "Tropical", "Marshy", "Water", "Rocky", "Mystical", "Sorcerous", "Unusual", "Attuned"}))
89
addOffspring("Cultivated", choose({"Common"}, {"Forest", "Meadows", "Modest", "Wintry", "Tropical", "Marshy", "Water", "Rocky", "Mystical", "Sorcerous", "Unusual", "Attuned"}))
90
91
addOffspring("Noble", {{"Common", "Cultivated"}})
92
addOffspring("Majestic", {{"Noble", "Cultivated"}})
93
addOffspring("Imperial", {{"Noble", "Majestic"}})
94
addOffspring("Diligent", {{"Common", "Cultivated"}})
95
addOffspring("Unweary", {{"Diligent", "Cultivated"}})
96
addOffspring("Industrious", {{"Diligent", "Unweary"}})
97
addOffspring("Heroic", {{"Steadfast", "Valiant"}})
98
addOffspring("Sinister", choose({"Cultivated"}, {"Modest", "Tropical"}))
99
addOffspring("Fiendish", choose({"Sinister"}, {"Cultivated", "Modest", "Tropical"}))
100
addOffspring("Frugal", choose({"Modest"}, {"Sinister", "Fiendish"}))
101
addOffspring("Demonic", {{"Sinister", "Fiendish"}})
102
addOffspring("Austere", {{"Modest", "Frugal"}})
103
addOffspring("Exotic", {{"Austere", "Tropical"}})
104
addOffspring("Edenic", {{"Exotic", "Tropical"}})
105
addOffspring("Spectral", {{"Hermitic", "Ender"}})
106
addOffspring("Phantasmal", {{"Spectral", "Ender"}})
107
addOffspring("Icy", {{"Industrious", "Wintry"}})
108
addOffspring("Glacial", {{"Icy", "Wintry"}})
109
addOffspring("Vindictive", {{"Monastic", "Demonic"}})
110
addOffspring("Vengeful", choose({"Vindictive"}, {"Demonic", "Monastic"}))
111
addOffspring("Avenging", {{"Vengeful", "Vindictive"}})
112
addOffspring("Leporine", {{"Meadows", "Forest"}})
113
addOffspring("Merry", {{"Wintry", "Forest"}})
114
addOffspring("Tipsy", {{"Wintry", "Meadows"}})
115
addOffspring("Tricky", {{"Sinister", "Common"}})
116
addOffspring("Rural", {{"Meadows", "Diligent"}})
117
addOffspring("Secluded", {{"Monastic", "Austere"}})
118
addOffspring("Hermitic", {{"Monastic", "Secluded"}})
119
addOffspring("Arid", {{"Meadows", "Modest"}})
120
addOffspring("Barren", {{"Arid", "Common"}})
121
addOffspring("Desolate", {{"Arid", "Barren"}})
122
addOffspring("Decaying", {{"Desolate", "Modest"}})
123
addOffspring("Skeletal", {{"Desolate", "Frugal"}})
124
addOffspring("Creepy", {{"Desolate", "Austere"}})
125
addOffspring("Decomposing", {{"Gnawing", "Common"}})
126
addOffspring("Tolerant", {{"Rocky", "Diligent"}})
127
addOffspring("Robust", {{"Rocky", "Tolerant"}})
128
addOffspring("Resilient", {{"Imperial", "Robust"}})
129
addOffspring("Corroded", {{"Resilient", "Forest"}})
130
addOffspring("Tarnished", {{"Resilient", "Marshy"}})
131
addOffspring("Rusty", {{"Resilient", "Meadows"}})
132
addOffspring("Leaden", {{"Resilient", "Unweary"}})
133
addOffspring("Galvanized", {{"Tarnished", "Cultivated"}})
134
addOffspring("Impregnable", {{"Resilient", "Noble"}})
135
addOffspring("Invincible", {{"Resilient", "Ender"}})
136
addOffspring("Glittering", {{"Corroded", "Imperial"}})
137
addOffspring("Shining", {{"Rusty", "Imperial"}})
138
addOffspring("Valuable", {{"Glittering", "Shining"}})
139
addOffspring("Lapis", {{"Resilient", "Water"}})
140
addOffspring("Emerald", {{"Lapis", "Noble"}})
141
addOffspring("Ruby", {{"Emerald", "Austere"}})
142
addOffspring("Sapphire", {{"Emerald", "Ocean"}})
143
addOffspring("Diamond", {{"Lapis", "Imperial"}})
144
addOffspring("Unstable", {{"Austere", "Rocky"}})
145
addOffspring("Nuclear", {{"Unstable", "Rusty"}})
146
addOffspring("Radioactive", {{"Nuclear", "Glittering"}})
147
addOffspring("Ancient", {{"Noble", "Diligent"}})
148
addOffspring("Primeval", {{"Ancient", "Noble"}})
149
addOffspring("Prehistoric", {{"Primeval", "Majestic"}})
150
addOffspring("Relic", {{"Prehistoric", "Imperial"}})
151
addOffspring("Fossilised", {{"Primeval", "Growing"}})
152
addOffspring("Resinous", {{"Primeval", "Fungal"}})
153
addOffspring("Oily", {{"Primeval", "Ocean"}})
154
addOffspring("Distilled", {{"Oily", "Industrious"}})
155
addOffspring("Refined", {{"Oily", "Distilled"}})
156
addOffspring("Elastic", {{"Refined", "Resinous"}})
157
addOffspring("River", {{"Water", "Common"}})
158
addOffspring("Ocean", {{"Water", "Diligent"}})
159
addOffspring("Stained", {{"Ebony", "Ocean"}})
160
addOffspring("Growing", {{"Diligent", "Forest"}})
161
addOffspring("Thriving", {{"Growing", "Rural"}})
162
addOffspring("Blooming", {{"Thriving", "Growing"}})
163
addOffspring("Sweetened", {{"Valiant", "Diligent"}})
164
addOffspring("Sugary", {{"Sweetened", "Diligent"}})
165
addOffspring("Ripening", {{"Sugary", "Forest"}})
166
addOffspring("Fruity", {{"Ripening", "Rural"}})
167
addOffspring("Farmed", {{"Cultivated", "Rural"}})
168
addOffspring("Bovine", {{"Rural", "Water"}})
169
addOffspring("Caffeinated", {{"Tropical", "Rural"}})
170
addOffspring("Minty", {{"Tropical", "Farmed"}})
171
addOffspring("Damp", {{"Common", "Marshy"}})
172
addOffspring("Boggy", {{"Damp", "Marshy"}})
173
addOffspring("Fungal", {{"Boggy", "Damp"}})
174
addOffspring("Furious", {{"Embittered", "Sinister"}})
175
addOffspring("Volcanic", {{"Embittered", "Furious"}})
176
addOffspring("Malicious", {{"Sinister", "Tropical"}})
177
addOffspring("Infectious", {{"Malicious", "Tropical"}})
178
addOffspring("Virulent", {{"Malicious", "Infectious"}})
179
addOffspring("Viscous", {{"Water", "Exotic"}})
180
addOffspring("Glutinous", {{"Viscous", "Exotic"}})
181
addOffspring("Sticky", {{"Viscous", "Glutinous"}})
182
addOffspring("Corrosive", {{"Virulent", "Sticky"}})
183
addOffspring("Caustic", {{"Corrosive", "Fiendish"}})
184
addOffspring("Acidic", {{"Corrosive", "Caustic"}})
185
addOffspring("Excited", {{"Cultivated", "Valiant"}})
186
addOffspring("Energetic", {{"Excited", "Valiant"}})
187
addOffspring("Frigid", {{"Wintry", "Diligent"}})
188
addOffspring("Absolute", {{"Ocean", "Frigid"}})
189
addOffspring("Shadowed", {{"Tolerant", "Sinister"}})
190
addOffspring("Darkened", {{"Shadowed", "Embittered"}})
191
addOffspring("Abyssal", {{"Shadowed", "Darkened"}})
192
addOffspring("Maroon", {{"Forest", "Valiant"}})
193
addOffspring("Saffron", {{"Meadows", "Valiant"}})
194
addOffspring("Prussian", {{"Water", "Valiant"}})
195
addOffspring("Natural", {{"Tropical", "Valiant"}})
196
addOffspring("Ebony", {{"Rocky", "Valiant"}})
197
addOffspring("Bleached", {{"Wintry", "Valiant"}})
198
addOffspring("Sepia", {{"Marshy", "Valiant"}})
199
addOffspring("Amber", {{"Maroon", "Saffron"}})
200
addOffspring("Turquoise", {{"Natural", "Prussian"}})
201
addOffspring("Indigo", {{"Maroon", "Prussian"}})
202
addOffspring("Slate", {{"Ebony", "Bleached"}})
203
addOffspring("Azure", {{"Prussian", "Bleached"}})
204
addOffspring("Lavender", {{"Maroon", "Bleached"}})
205
addOffspring("Lime", {{"Natural", "Bleached"}})
206
addOffspring("Fuchsia", {{"Indigo", "Lavender"}})
207
addOffspring("Ashen", {{"Slate", "Bleached"}})
208
addOffspring("Celebratory", {{"Austere", "Excited"}})
209
addOffspring("Jaded", {{"Ender", "Relic"}})
210
addOffspring("Glowering", {{"Furious", "Excited"}})
211
addOffspring("Hazardous", {{"Austere", "Desolate"}})
212
addOffspring("Lustered", {{"Resilient", "Unweary"}})
213
addOffspring("Abnormal", {{"Secluded", "Ender"}})
214
addOffspring("Spatial", {{"Abnormal", "Hermitic"}})
215
addOffspring("Quantum", {{"Spatial", "Spectral"}})
216
addOffspring("Eldritch", choose({"Cultivated"}, {"Mystical", "Sorcerous", "Unusual", "Attuned"}))
217
addOffspring("Esoteric", {{"Cultivated", "Eldritch"}})
218
addOffspring("Mysterious", {{"Eldritch", "Esoteric"}})
219
addOffspring("Arcane", {{"Esoteric", "Mysterious"}})
220
addOffspring("Charmed", {{"Cultivated", "Eldritch"}})
221
addOffspring("Enchanted", {{"Eldritch", "Charmed"}})
222
addOffspring("Supernatural", {{"Charmed", "Enchanted"}})
223
addOffspring("Ethereal", {{"Arcane", "Supernatural"}})
224
addOffspring("Watery", {{"Supernatural", "Ethereal"}})
225
addOffspring("Earthen", {{"Supernatural", "Ethereal"}})
226
addOffspring("Firey", {{"Supernatural", "Ethereal"}})
227
addOffspring("Windy", {{"Supernatural", "Ethereal"}})
228
addOffspring("Pupil", {{"Monastic", "Arcane"}})
229
addOffspring("Scholarly", {{"Arcane", "Pupil"}})
230
addOffspring("Savant", {{"Pupil", "Scholarly"}})
231
addOffspring("Aware", {{"Ethereal", "Attuned"}})
232
addOffspring("Spirit", choose({"Aware"}, {"MystEtherealical", "Attuned"}))
233
addOffspring("Soul", {{"Aware", "Spirit"}})
234
addOffspring("Skulking", {{"Modest", "Eldritch"}})
235
addOffspring("Ghastly", {{"Skulking", "Ethereal"}})
236
addOffspring("Spidery", {{"Tropical", "Skulking"}})
237
addOffspring("Smouldering", {{"Skulking", "Hateful"}})
238
addOffspring("Timely", {{"Imperial", "Ethereal"}})
239
addOffspring("Lordly", {{"Imperial", "Timely"}})
240
addOffspring("Doctoral", {{"Timely", "Lordly"}})
241
addOffspring("Infernal", {{"Infernal", "ves"}})
242
addOffspring("Hateful", {{"Infernal", "Eldritch"}})
243
addOffspring("Spiteful", {{"Infernal", "Hateful"}})
244
addOffspring("Withering", {{"Demonic", "Spiteful"}})
245
addOffspring("Oblivion", {{"Oblivion", "ves"}})
246
addOffspring("Nameless", {{"Ethereal", "Oblivion"}})
247
addOffspring("Abandoned", {{"Oblivion", "Nameless"}})
248
addOffspring("Forlorn", {{"Nameless", "Abandoned"}})
249
addOffspring("Draconic", {{"Imperial", "Abandoned"}})
250
addOffspring("Ferrous", {{"Common", "Industrious"}})
251
addOffspring("Auric", {{"Minium", "Plumbum"}})
252
addOffspring("Cuprum", {{"Industrious", "Meadows"}})
253
addOffspring("Stannum", {{"Industrious", "Forest"}})
254
addOffspring("Argentum", {{"Imperial", "Modest"}})
255
addOffspring("Plumbum", {{"Stannum", "Common"}})
256
addOffspring("Aluminum", {{"Industrious", "Cultivated"}})
257
addOffspring("Ardite", {{"Industrious", "Infernal"}})
258
addOffspring("Cobalt", {{"Imperial", "Infernal"}})
259
addOffspring("Manyullyn", {{"Ardite", "Cobalt"}})
260
addOffspring("Diamandi", {{"Austere", "Auric"}})
261
addOffspring("Esmeraldi", {{"Austere", "Argentum"}})
262
addOffspring("Apatine", {{"Rural", "Cuprum"}})
263
addOffspring("Mutable", {{"Unusual", "Eldritch"}})
264
addOffspring("Transmuting", {{"Unusual", "Mutable"}})
265
addOffspring("Crumbling", {{"Unusual", "Mutable"}})
266
addOffspring("Invisible", {{"Mystical", "Mutable"}})
267
addOffspring("Aer", {{"Windy", "Windy"}})
268
addOffspring("Ignis", {{"Firey", "Firey"}})
269
addOffspring("Aqua", {{"Watery", "Watery"}})
270
addOffspring("Solum", {{"Earthen", "Earthen"}})
271
addOffspring("Ordered", {{"Ethereal", "Arcane"}})
272
addOffspring("Chaotic", {{"Ethereal", "Supernatural"}})
273
addOffspring("Brainy", {{"Skulking", "Pupil"}})
274
addOffspring("Batty", {{"Skulking", "Windy"}})
275
addOffspring("Poultry", {{"Common", "Skulking"}})
276
addOffspring("Beefy", {{"Common", "Skulking"}})
277
addOffspring("Porcine", {{"Common", "Skulking"}})
278
addOffspring("Minium", {{"Frugal", "Eldritch"}})
279
280
local function fixStacks()
281
	for i, stack in pairs(stacks) do
282
		if string.find(stack.beeInfo.displayName, "bees.species.") then
283
			stack.beeInfo.displayName = string.upper(string.sub(stack.beeInfo.displayName, 14, 14)) .. string.sub(stack.beeInfo.displayName, 15, -1)
284
		end
285
		if string.find(stack.name, "drone") then
286
			stack.name = "Drone"
287
		end
288
		if string.find(stack.name, "princess") then
289
			stack.name = "Princess"
290
		end
291
	end
292
end
293
	
294
--find the highest level bee i own that is a parent to species (of type "Princess" or "Drone" if type ~= nil)
295
--returns parent, offspring, score
296
--offspring is the next step after parent
297
--score is the depth of the search
298
299
function findAvailableParent(species, score, type)
300
301
	for i, stack in pairs(stacks) do
302
		if stack.beeInfo.displayName == species then
303
			if type then
304
				if string.find(stack.name, type) then
305
					return species, nil, score
306
				end					
307
			else
308
				return species, nil, score
309
			end
310
		end
311
	end
312
313
	local aScore, aParent, aOffspring
314
	local beeScore = 99999
315
	local beeParent = nil
316
	local beeOffspring = nil
317
	
318
	for i, parents in ipairs(bees[species].mutateFrom) do
319
		aParent, aOffspring, aScore = findAvailableParent(parents[1], score + 1, type)
320
		if aScore < beeScore then
321
			beeScore = aScore
322
			beeParent = aParent
323
			beeOffspring = aOffspring
324
		end
325
		aParent, aOffspring, aScore = findAvailableParent(parents[2], score + 1, type)
326
		if aScore < beeScore then
327
			beeScore = aScore
328
			beeParent = aParent
329
			beeOffspring = aOffspring
330
		end
331
	end
332
	if beeOffspring == nil then
333
		beeOffspring = species
334
	end
335
	return beeParent, beeOffspring, beeScore
336
end
337
338
--finds a princess and drone of the given species if they exist
339
local function findSpecies(species)
340
	local princessIndex = nil
341
	local droneIndex = nil
342
343
	for i, stack in pairs(stacks) do
344
		if stack.beeInfo.displayName == species then
345
			if string.find(stack.name, "Princess") then
346
				princessIndex = i
347
			end
348
			if string.find(stack.name, "Drone") then
349
				droneIndex = i
350
			end
351
		end
352
	end
353
	return princessIndex, droneIndex
354
end
355
356
--breed these two species, they must exists and have a breeding pair
357
local function breedSpecies(parent1, parent2)
358
	local princess1 = nil
359
	local princess2 = nil
360
	local drone1 = nil
361
	local drone2 = nil
362
	
363
	princess1, drone1 = findSpecies(parent1)
364
	princess2, drone2 = findSpecies(parent2)
365
	
366
	if not (princess1 and drone2) then
367
		princess1 = princess2
368
		drone2 = drone1
369
	end
370
	
371
	print("Breeding " .. parent1 .. " with " .. parent2)
372
373
	if princess1 and drone2 then
374
		chest.pushItem("north", princess1, 1, 1)
375
		chest.pushItem("north", drone2, 1, 2)
376
		return true
377
	end
378
	return false
379
end
380
381
--finds the most common species of type Drone or Princess
382
local function findExtraBee(type, msg)
383
	local counts = {}
384
	local bestcount = 0
385
	local bestspecies = nil
386
387
	for i, stack in pairs(stacks) do
388
		if string.find(stack.name, type) then
389
			if counts[stack.beeInfo.displayName] then
390
				counts[stack.beeInfo.displayName] = counts[stack.beeInfo.displayName] + stack.qty
391
			else
392
				counts[stack.beeInfo.displayName] = stack.qty
393
			end
394
			if counts[stack.beeInfo.displayName] > bestcount then
395
				bestcount = counts[stack.beeInfo.displayName]
396
				bestspecies = stack.beeInfo.displayName
397
			end
398
		end
399
	end
400
	if bestcount < 2 then
401
		shutdownMsg = "Need more " .. type .. "s!";
402
	else
403
		if msg == "Used" then
404
			print("Used Extra " .. bestspecies .. " " .. type)
405
		end
406
	end
407
	return bestspecies, bestcount
408
end
409
410
--helper function used when testing for low fertility
411
local function getTotalDroneCount()	
412
	stacks = chest.getAllStacks()
413
	fixStacks()
414
	local count = 0
415
	for i, stack in pairs(stacks) do
416
		if string.find(stack.name, "Drone") then
417
			count = count + 1
418
		end
419
	end
420
	return count
421
end
422
423
--attempt to create a princess and minDrones of species, which i own one of
424
--returns true if already purebred, else begins a breeding cycle
425
426
local function purebreed(species)
427
	local princessCount = 0
428
	local droneCount = 0
429
430
	--count how many i have
431
432
	for i, stack in pairs(stacks) do
433
		if stack.beeInfo.displayName == species then
434
			if string.find(stack.name, "Princess") then
435
				princessCount = princessCount + stack.qty
436
			end
437
			if string.find(stack.name, "Drone") then
438
				droneCount = droneCount + stack.qty
439
			end
440
		end
441
	end
442
	
443
	if princessCount >= 1 and droneCount >= minDrones then
444
		return true
445
	end
446
447
	print("Attempting to purebreed " .. species)
448
449
	if princessCount >= 1 and droneCount >= 1 then
450
		if purebreedBanList[species] then
451
			return true
452
		end
453
		purebreedBee = species
454
		purebreedDroneCount = getTotalDroneCount()
455
		breedSpecies(species, species)
456
		return false
457
	end
458
459
	--find something to breed it with, an available parent, or an extra bee
460
461
	local parent1, offspring, score
462
	if(princessCount >= 1) then
463
		parent1, offspring, score = findAvailableParent(species, 0, "Drone") 
464
		if score == 99999 then
465
			parent1, score = findExtraBee("Drone", "Used")
466
		else	
467
			--the purebreeding code sometimes gets stuck on low fertility bees		
468
			local princess1, drone1 = findSpecies(parent1)
469
			if princess1 and drone2 and not purebreed(parent1) then
470
				return false
471
			end
472
		end
473
	else
474
		parent1, offspring, score = findAvailableParent(species, 0, "Princess")
475
		if score == 99999 then
476
			parent1, score = findExtraBee("Princess", "Used")
477
		else		
478
			--local princess1, drone1 = findSpecies(parent1)
479
			if princess1 and drone2 and not purebreed(parent1) then
480
				return false
481
			end
482
		end
483
	end
484
	if shutdownMsg then
485
		return false
486
	end
487
488
	breedSpecies(species, parent1)
489
	return false
490
end
491
492
--the main breeding function
493
--uses findavailableparent, then tries to create the designated offspring
494
495
local function targetSpecies(species)
496
	local parent1
497
	local parent2 = nil
498
499
	--find the highest level bee i have that is a parent to species
500
	--offspring is the next step after parent on the way to species
501
502
	local parent1, offspring, score = findAvailableParent(species, 0, nil)
503
	if score == 0 then
504
		--found species		
505
		return true
506
	end
507
	if score == 99999 then
508
		shutdownMsg = "No available parents for " .. species
509
		return false
510
	end
511
512
	print("Targeting Species " .. offspring)
513
	
514
	local princess1, drone1 = findSpecies(parent1)
515
	local princess2, drone2
516
517
	--find which bee i need to breed parent1 with to get offspring, showing preference for bees that i own
518
519
	for i, parents in ipairs(bees[offspring].mutateFrom) do
520
		if parents[1] == parent1 then
521
			princess2, drone2 = findSpecies(parents[2])
522
			if (princess1 and drone2) or (princess2 and drone1) then
523
				parent2 = parents[2]
524
				break
525
			end
526
			if not parent2 then
527
				parent2 = parents[2]
528
			end
529
			if princess2 or drone2 then
530
				parent2 = parents[2]
531
			end
532
		end
533
		if parents[2] == parent1 then
534
			princess2, drone2 = findSpecies(parents[1])
535
			if (princess1 and drone2) or (princess2 and drone1) then
536
				parent2 = parents[1]
537
				break
538
			end
539
			if not parent2 then
540
				parent2 = parents[1]
541
			end
542
			if princess2 or drone2 then
543
				parent2 = parents[1]
544
			end
545
		end
546
547
	end
548
	
549
	princess2, drone2 = findSpecies(parent2)
550
551
	if (princess1 and drone2) or (princess2 and drone1) then	
552
		--i have both bees i need so breed it now
553
		--the purebreeding code sometimes gets stuck on low fertility bees	
554
		if princess1 and drone1 and not purebreed(parent1) then
555
			return false
556
		end
557
		if princess2 and drone2 and not purebreed(parent2) then
558
			return false
559
		end
560
		breedSpecies(parent1, parent2)
561
		return false
562
	end
563
564
	--i have the species but not princess or drone, so purebreed it
565
	if princess2 or drone2 then
566
		purebreed(parent2)
567
		return false
568
	end
569
570
	--i dont have the species i need to breed with parent, so target it
571
	targetSpecies(parent2)
572
	return false
573
end
574
575
local function getEmptySlot()
576
	local size = chest.getInventorySize()
577
	stacks = chest.getAllStacks()
578
	for slot = 1, size do
579
		if stacks[slot] == nil then
580
			return slot
581
		end
582
	end
583
	shutdownMsg = "Out of Inventory Space!"
584
	return 0
585
end
586
587
local function dropExtraDrones()
588
589
	local species, count = findExtraBee("Drone", "Dropping")
590
	shutdownMsg = nil
591
592
	if count <= maxDrones then
593
		return
594
	end
595
	
596
	for i, stack in pairs(stacks) do
597
		if stack.beeInfo.displayName == species and string.find(stack.name, "Drone") then
598
			chest.pushItem("south", i, 1)
599
			count = count - 1
600
			if count <= maxDrones then
601
				return
602
			end
603
		end
604
	end
605
606
end
607
608
local function dropExtraItems()
609
	stacks = chest.getAllStacks()
610
	fixStacks()
611
	for i, stack in pairs(stacks) do
612
		if (not string.find(stack.name, "Drone")) and (not string.find(stack.name, "Princess")) then
613
			chest.pushItem("south", i, stack.qty)
614
		end
615
	end
616
617
end
618
619
local function main(species)
620
	
621
	local apiarystacks
622
623
	while(1) do
624
	
625
		--test for breeding in progress
626
		apiarystacks = apiary.getAllStacks()
627
		while apiarystacks[1] do
628
			sleep(2)
629
			apiarystacks = apiary.getAllStacks()
630
		end
631
		
632
		--clear apiary
633
		for slot = 3, 9 do
634
			if apiarystacks[slot] then
635
				apiary.pushItem("south", slot, apiarystacks[slot].qty, getEmptySlot())
636
			end
637
		end
638
		
639
		if purebreedBee then
640
			if purebreedDroneCount == getTotalDroneCount() then
641
				purebreedBanList[purebreedBee] = 1
642
			end
643
			purebreedBee = nil
644
		end
645
				
646
		--dropExtraItems()
647
648
		--get the list of all items in the chest
649
		stacks = chest.getAllStacks()
650
		fixStacks()
651
		if targetSpecies(species) then
652
			if purebreed(species) then
653
				shutdownMsg = "Breeding complete: " .. species
654
			end
655
		end
656
657
		if(shutdownMsg) then
658
			print(shutdownMsg)
659
			break
660
		end
661
662
		--dropExtraDrones()
663
664
		sleep(1)
665
	end
666
end
667
668
if species == nil then
669
	print("Usage")
670
	print("   programname species")
671
	return
672
end
673
if bees[species] == nil then
674
	print("Species does not exist in database")
675
end
676
677
main(species)