-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.lua
1248 lines (1132 loc) · 29.7 KB
/
control.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
local util = require('util')
local player;
local made_by = {}
local Status = {
DONE = {},
BUSY = {},
WAIT = {},
ERR = {},
}
local _Status = {}
function _Status:is_done()
return self.kind == Status.DONE
end
function _Status:is_busy()
return self.kind == Status.BUSY
end
function _Status:is_wait()
return self.kind == Status.WAIT
end
function _Status:is_err()
return self.kind == Status.ERR
end
function _Status:tostring()
return self.desc
end
local DEBUG_STATUS = false
local function new_status(kind)
local ks = 'UNKNOWN'
if kind == Status.DONE then
ks = 'DONE'
elseif kind == Status.BUSY then
ks = 'BUSY'
elseif kind == Status.WAIT then
ks = 'WAIT'
elseif kind == Status.ERR then
ks = 'ERR'
end
local s = {
kind = kind,
}
if DEBUG_STATUS then
local i = debug.getinfo(2, 'nSl')
s.desc = ks..'('..(i.name or '_')..':'..i.short_src..':'..i.currentline..')'
else
s.desc = ks
end
setmetatable(s, {__index = _Status})
return s
end
function Status.done()
return new_status(Status.DONE)
end
function Status.busy()
return new_status(Status.BUSY)
end
function Status.wait()
return new_status(Status.WAIT)
end
function Status.err()
return new_status(Status.ERR)
end
local function is_status(x)
if x == nil then
return false
end
local m = getmetatable(x)
if m == nil then
return false
end
return m.__index == _Status
end
local function status_tostring(x)
return x:tostring()
end
local function player_inventories()
local res = {}
for name, inv in pairs(defines.inventory) do
if name == 'player_main' or name == 'player_quickbar' then
local i = player.get_inventory(inv)
if i ~= nil then
table.insert(res, i)
end
end
end
return res
end
local function player_inv_get_item_count(name)
local count = 0
for _, inv in ipairs(player_inventories()) do
count = count + inv.get_item_count(name)
end
return count
end
local function player_inv_remove(stack)
stack.count = stack.count or 1
local count = 0
for _, inv in ipairs(player_inventories()) do
local c = inv.remove({name = stack.name, count = stack.count - count})
count = count + c
if count >= stack.count then
return count
end
end
player.print('Only removed '..count..'/'..stack.count..' of '..stack.name)
return count
end
local function player_inv_can_insert(stack)
return player.get_inventory(defines.inventory.player_main).insert(stack)
end
local function player_inv_insert(stack)
return player.get_inventory(defines.inventory.player_main).insert(stack)
end
local Reservation = {}
-- Inventory is reserved so that one (high priority) plan doesn't keep
-- gathering an item that gets consumed in another plan.
local inventory_reservations = {}
function Reservation:new()
local r = {
items = {},
}
setmetatable(r, {__index = self})
return r
end
function Reservation:take(res)
if res == nil or res.items == nil then
return
end
for item, count in pairs(res.items) do
self.items[item] = (self.items[item] or 0) + count
end
res.items = {}
end
-- Returns the amount actually allocated.
function Reservation:alloc_item(item, count)
local reserved = inventory_reservations[item] or 0
local have = player_inv_get_item_count(item)
local avail = have - reserved
if avail < count then
count = avail
end
self.items[item] = (self.items[item] or 0) + count
inventory_reservations[item] = reserved + count
return count
end
function Reservation:alloc_item_to(item, want_count)
local count = self:item_count(item)
if count < want_count then
local added = self:alloc_item(item, want_count - count)
count = count + added
end
return count
end
function Reservation:use_item(item, count)
count = self:free_item(item, count)
local have = player_inv_get_item_count(item)
if count > have then
count = have
end
count = player_inv_remove({name=item, count=count})
return count
end
function Reservation:create_item(item, count)
count = player_inv_insert({name=item, count=count})
return self:alloc_item(item, count)
end
function Reservation:free_item(item, count)
if (self.items[item] or 0) < count then
player.print('ERROR tried to free '..tostring(count)..' but have '..tostring(self.items[item]))
count = self.items[item] or 0
end
local reserved = inventory_reservations[item] or 0
if reserved < count then
player.print('ERROR tried to free '..tostring(count)..' but have '..tostring(reserved)..' globally')
count = reserved
end
self.items[item] = (self.items[item] or 0) - count
inventory_reservations[item] = reserved - count
return count
end
function Reservation:free()
local items = {}
-- Can't iterate while mutating, so make a copy.
for item, _ in pairs(self.items) do
table.insert(items, item)
end
for _, item in ipairs(items) do
self:free_item(item, self.items[item])
end
end
function Reservation:item_count(item)
local count = self.items[item] or 0
local actual_count = player_inv_get_item_count(item)
if count > actual_count then
player.print('ERROR bad count')
count = actual_count
end
return count
end
function Reservation:total_items()
local total = 0
for _, count in pairs(self.items) do
total = total + count
end
return total
end
function Reservation:tostring()
local s = 'Res('
local comma = false
for item, count in pairs(self.items) do
if comma then
s = s..','
end
s = s .. item..'='..tostring(count)
comma = true
end
s = s .. ')'
return s
end
local Plan = {}
Plan.__index = Plan
function Plan:new_plan_type()
local base = self
local plan = {super=base}
function plan.raw_new()
local inst = {}
setmetatable(inst, { __index = plan })
inst.started = false
inst.updated = false
inst.name = "unknown plan"
inst.deps = {}
inst.reservation = Reservation:new()
return inst
end
setmetatable(plan, { __index = base })
return plan
end
function Plan:is_plan(plan)
local m = getmetatable(self)
return m.__index == plan
end
function Plan:print(prefix)
local status = self:check_status()
print(prefix..self.name..' '..status_tostring(status)..' '..self.reservation:tostring())
for _, dep in ipairs(self.deps) do
dep:print(prefix..'-')
end
end
function Plan:take_deps(res)
for _, dep in ipairs(self.deps) do
dep:take_deps(res)
res:take(dep.reservation)
end
end
function Plan:clear_deps()
if #self.deps > 0 then
for _, dep in ipairs(self.deps) do
dep:do_free()
end
self.deps = {}
end
end
function Plan:free()
self.reservation:free()
end
function Plan:do_free()
self:clear_deps()
self:free()
end
function Plan:do_check_status()
local busy = false
local wait = false
local err = false
for _, dep in ipairs(self.deps) do
local dep_status = dep:do_check_status()
if dep_status:is_busy() then
busy = true
elseif dep_status:is_wait() then
wait = true
elseif dep_status:is_err() then
err = true
end
end
if busy then
return (Status.busy())
elseif wait then
return (Status.wait())
elseif err then
return (Status.err())
end
return (self:check_status())
end
function Plan:check_status() -- luacheck: ignore self
return (Status.done())
end
function Plan:do_update(prefix)
if not self.started then
player.print(prefix.."Starting "..self.name)
self.started = true
end
local status = self:do_check_status()
if status:is_done() then
if self.started then
player.print(prefix..'Finished '..self.name)
end
self.updated = false
self.started = false
return status
end
local wait = false
local err = false
for _, dep in ipairs(self.deps) do
local dep_status = dep:do_check_status()
if dep_status:is_busy() then
local new_dep_status = dep:do_update(prefix..'-')
self.updated = false
if not is_status(new_dep_status) or new_dep_status:is_busy()then
return (Status.busy())
end
dep_status = new_dep_status
end
if dep_status:is_done() then
if dep.started then
player.print(prefix..'Finished '..dep.name)
end
dep.started = false
dep.updated = false
elseif dep_status:is_wait() then
wait = true
elseif dep_status:is_err() then
err = true
end
end
if wait then
return (Status.wait())
elseif err then
return (Status.err())
end
-- All deps are done.
if not self.updated then
player.print(prefix.."Updating "..self.name)
end
status = self:update()
self.updated = true
if not is_status(status) then
player.print('ERROR: bad status from '..self.name..': '..status_tostring(status))
-- say busy so that it gets checked next tick
return (Status.busy())
end
return status
end
function Plan:update() -- luacheck: ignore self
end
local MoveNear = Plan:new_plan_type()
local ChopTree = Plan:new_plan_type()
function MoveNear:new(dest, dist)
local move = self.raw_new()
move.dest = dest
move.dist = dist - 0.1
move.name = "MoveNear(("..tostring(dest.x)..","..tostring(dest.y).."),"..tostring(dist)..")"
move.past_positions = {}
move.wander_count = 0
move.stuck_count = 0
return move
end
function MoveNear:check_status()
local dir = self:direction()
if dir.hor == 0 and dir.vert == 0 then
return (Status.done())
end
return (Status.busy())
end
function MoveNear:direction()
local pos = player.position
local dx = self.dest.x - pos.x
local dy = self.dest.y - pos.y
local vert = 0;
local hor = 0;
if dy > self.dist then
vert = 1
elseif dy < -self.dist then
vert = -1
end
if dx > self.dist then
hor = 1
elseif dx < -self.dist then
hor = -1
end
return {hor=hor, vert=vert}
end
local function filter(t, f)
if t == nil then
return t
end
local res = {}
for _, v in ipairs(t) do
if f(v) then
table.insert(res, v)
end
end
return res
end
local function minimum(list, key)
local min = nil
local min_val = nil
for _, l in ipairs(list) do
local l_val = key(l)
if min == nil or l_val < min_val then
min = l
min_val = l_val
end
end
return min
end
local function entity_distance_to_player(e)
return util.distance(e.position, player.position)
end
local function get_nearest_tree()
local size = 100
local trees = player.surface.find_entities_filtered({
area = {{player.position.x - size, player.position.y - size},
{player.position.x + size, player.position.y + size}},
type = "tree",
})
trees = filter(trees, function(t)
return t.valid and t.health > 0
end)
local tree = minimum(trees, entity_distance_to_player)
if tree == "nil" then
player.print("Could not find tree")
return nil
end
return tree
end
local function pick_random(t)
local keys = {}
local i = 1
for k, _ in pairs(t) do
keys[i] = k
i = i + 1
end
return t[keys[math.random(1, #keys)]]
end
function MoveNear:update()
self:clear_deps()
if self.wander_count > 0 then
player.walking_state = {
walking = true,
direction = self.wander,
}
self.wander_count = self.wander_count - 1
return (Status.busy())
end
if #self.past_positions >= 10 then
local dist = util.distance(self.past_positions[1], player.position)
self.past_positions = {}
if dist < 0.1 then
local tree = get_nearest_tree()
if tree ~= nil and entity_distance_to_player(tree) < 1 then
table.insert(self.deps, ChopTree:new(tree))
return (Status.busy())
end
player.print('stuck')
self.wander = pick_random(defines.direction)
self.wander_count = math.random(10, 10 + self.stuck_count)
self.stuck_count = self.stuck_count + 1
return (Status.busy())
end
end
table.insert(self.past_positions, player.position)
local godir = self:direction()
local hor = godir.hor
local vert = godir.vert
local dir = nil;
if hor == 0 and vert == 0 then
player.walking_state = {
walking = false,
direction = defines.direction.north,
}
return (Status.done())
end
if hor == 0 then
if vert > 0 then
dir = defines.direction.south
elseif vert < 0 then
dir = defines.direction.north
end
elseif hor > 0 then
if vert > 0 then
dir = defines.direction.southeast
elseif vert < 0 then
dir = defines.direction.northeast
else
dir = defines.direction.east
end
elseif hor < 0 then
if vert > 0 then
dir = defines.direction.southwest
elseif vert < 0 then
dir = defines.direction.northwest
else
dir = defines.direction.west
end
end
player.walking_state = {
direction = dir,
walking = true,
}
return (Status.busy())
end
local function get_nearest_ore(name)
local size = 4096
local res = player.surface.find_entities_filtered({
area = {{player.position.x - size, player.position.y - size},
{player.position.x + size, player.position.y + size}},
name = name,
})
local ore = minimum(res, entity_distance_to_player)
if ore == nil then
player.print('Could not find nearest '..name)
end
return ore
end
local MineOre = Plan:new_plan_type()
function MineOre:new(tile, amount)
local plan = self.raw_new()
plan.tile = tile
plan.amount = amount
plan.name = "MineOre("..tile.name..","..amount..")"
plan.last_amount = tile.amount
plan.deps = {
MoveNear:new(tile.position, 2),
}
return plan
end
function MineOre:check_status()
if not self.tile.valid or self.reservation:item_count(self.tile.name) >= self.amount then
return (Status.done())
end
return (Status.busy())
end
function MineOre:update()
if player.selected ~= self.tile then
player.update_selected_entity(self.tile.position)
if player.selected ~= self.tile then
player.walking_state = {
walking = true,
direction = pick_random(defines.direction),
}
end
end
if player.mining_state.mining ~= true or player.mining_state.position ~= self.tile.position then
player.mining_state = {
mining = true,
position = self.tile.position,
}
end
self.reservation:alloc_item(self.tile.name, self.last_amount - self.tile.amount)
return (Status.busy())
end
function ChopTree:new(tree)
local plan = self.raw_new()
plan.tree = tree
plan.name = "Chop tree"
plan.deps = {
MoveNear:new(tree.position, 1),
}
return plan
end
function ChopTree:check_status()
if self.tree.valid then
return (Status.busy())
end
return (Status.done())
end
function ChopTree:update()
if player.selected ~= self.tree then
player.update_selected_entity(self.tree.position)
if player.selected ~= self.tile then
player.walking_state = {
walking = true,
direction = pick_random(defines.direction),
}
end
end
if player.mining_state.mining ~= true or player.mining_state.position ~= self.tree.position then
player.mining_state = {
mining = true,
position = self.tree.position,
}
end
return (Status.busy())
end
local SmeltItem = Plan:new_plan_type()
function SmeltItem:new(item, amount, recipe)
local plan = self.raw_new()
plan.item = item
plan.amount = amount
plan.recipe = recipe
plan:set_name()
plan.recipe_amount = 0
for _, p in ipairs(recipe.products) do
if p.name == plan.item then
plan.recipe_amount = plan.recipe_amount + p.amount
end
end
if plan.recipe_amount <= 0 then
player.print('weirdly cannot make '..item..' with '..recipe.name)
end
return plan
end
function SmeltItem:smelter_input()
local out = {}
local in_inv = self.smelter.get_inventory(defines.inventory.furnace_source)
for _, ing in ipairs(self.recipe.ingredients) do
out[ing.name] = in_inv.get_item_count(ing.name)
end
return out
end
function SmeltItem:pending_amount()
if self.smelter == nil then
return 0
end
local fuel_inv = self.smelter.get_fuel_inventory()
if fuel_inv.is_empty() then
return 0
end
local pending_in = self:smelter_input()
local runs = 100
for _, ing in ipairs(self.recipe.ingredients) do
local ing_runs = math.ceil(pending_in[ing.name] / ing.amount)
if ing_runs < runs then
runs = ing_runs
end
end
if self.smelter.is_crafting() then
runs = runs + 1
end
return self.recipe_amount * runs
end
local entity_cache = {}
local GetItem = Plan:new_plan_type()
function SmeltItem:set_name()
self.name = 'SmeltItem('..self.item..','..tostring(self.amount)..','..tostring(self.smelter)..')'
end
function SmeltItem:set_smelter()
if self.smelter ~= nil then
if self.smelter.valid then
return true
end
self.smelter = nil
end
-- Check cache
for i, e in ipairs(entity_cache) do
if e.name == 'stone-furnace' and not e.is_crafting() then
self.smelter = e
table.remove(entity_cache, i)
self:set_name()
return true
end
end
-- Check inventory
local furnace = 'stone-furnace'
if self.reservation:alloc_item_to(furnace, 1) < 1 then
table.insert(self.deps, GetItem:new(furnace, 1))
return false
end
local surf = player.surface
local pos = surf.find_non_colliding_position(furnace, player.position, 100, 4)
if pos == nil then
player.print('cannot find position for stone-furnace')
return false
end
self.smelter = surf.create_entity({
name = furnace,
position = pos,
force = player.force,
})
if self.smelter == nil then
player.print('could not create furnace')
return false
end
self:set_name()
if self.reservation:use_item(furnace, 1) < 1 then
player.print('Did not remove item from inventory')
end
return true
end
function SmeltItem:free()
if self.smelter ~= nil then
table.insert(entity_cache, self.smelter)
self.smelter = nil
end
end
function SmeltItem:go_near_smelter()
table.insert(self.deps, MoveNear:new(self.smelter.position, 2))
end
function SmeltItem:is_smelter_full()
local out_inv = self.smelter.get_output_inventory()
for _, ing in ipairs(self.recipe.products) do
if not out_inv.can_insert({name = ing.name, count = ing.amount}) then
return true
end
end
return false
end
function SmeltItem:empty_smelter()
local out_inv = self.smelter.get_output_inventory()
for item, count in pairs(out_inv.get_contents()) do
local stack = {name=item, count=count}
if player_inv_can_insert(stack) then
out_inv.remove(stack)
player_inv_insert(stack)
if item == self.item then
self.reservation:alloc_item(item, count)
end
else
player.print('cannot empty furnace')
return false
end
end
return true
end
function SmeltItem:need_fuel()
local fuel_inv = self.smelter.get_fuel_inventory()
return fuel_inv.is_empty()
end
function SmeltItem:need_empty()
local out_inv = self.smelter.get_output_inventory()
local reserved = self.reservation:item_count(self.item)
local out = out_inv.get_item_count(self.item)
return self:is_smelter_full() or out + reserved >= self.amount
end
-- Return table of item to remaining count needed.
function SmeltItem:feeding_amounts()
local out_inv = self.smelter.get_output_inventory()
local pending = out_inv.get_item_count(self.item)
local remaining = self.amount - self.reservation:item_count(self.item) - pending
local remaining_runs = math.ceil(remaining / self.recipe_amount)
if self.smelter.is_crafting() then
remaining_runs = remaining_runs - 1
end
local amounts = {}
local in_inv = self.smelter.get_inventory(defines.inventory.furnace_source)
for _, ing in ipairs(self.recipe.ingredients) do
local needed = remaining_runs * ing.amount
local tofeed = needed - in_inv.get_item_count(ing.name)
if tofeed > 0 then
amounts[ing.name] = tofeed
else
amounts[ing.name] = 0
end
end
return amounts
end
function SmeltItem:need_feed(amounts)
local in_inv = self.smelter.get_inventory(defines.inventory.furnace_source)
local any_empty = false
for _, ing in ipairs(self.recipe.ingredients) do
local have = in_inv.get_item_count(ing.name)
if amounts[ing.name] > 0 and have < ing.amount then
any_empty = true
end
end
return any_empty
end
function SmeltItem:check_status()
if self.reservation:item_count(self.item) >= self.amount then
return (Status.done())
end
if self.smelter == nil or not self.smelter.valid then
return (Status.busy())
end
if self:need_empty() then
return (Status.busy())
end
if self:need_fuel() then
return (Status.busy())
end
local feed_amounts = self:feeding_amounts()
if self:need_feed(feed_amounts) then
return (Status.busy())
end
return (Status.wait())
end
function SmeltItem:update()
self:take_deps(self.reservation)
self:clear_deps()
if not self:set_smelter() then
return (Status.busy())
end
local near_smelter = entity_distance_to_player(self.smelter) < 3
if self:need_empty() then
if not near_smelter then
self:go_near_smelter()
return (Status.busy())
end
if not self:empty_smelter() then
return (Status.err())
end
if self.reservation:item_count(self.item) >= self.amount then
return (Status.done())
end
end
if self:need_fuel() then
local fuel_inv = self.smelter.get_fuel_inventory()
local fuel_amount = self.reservation:alloc_item_to('raw-wood', 20)
if fuel_amount < 1 then
table.insert(self.deps, GetItem:new('raw-wood', 20))
return (Status.busy())
end
if not near_smelter then
self:go_near_smelter()
return (Status.busy())
end
if fuel_amount > 20 then
fuel_amount = 20
end
local stack = {name='raw-wood', count=fuel_amount}
local insert_amount = fuel_inv.insert(stack)
if insert_amount > 0 then
self.reservation:use_item('raw-wood', insert_amount)
else
player.print('could not fuel furnace')
return (Status.err())
end
end
local pending_amount = self:pending_amount()
if self.reservation:item_count(self.item) + pending_amount >= self.amount then
return (Status.wait())
end
local feed_amounts = self:feeding_amounts()
if not self:need_feed(feed_amounts) then
return (Status.wait())
end
local real_feed_amounts = {}
local in_inv = self.smelter.get_inventory(defines.inventory.furnace_source)
for _, ing in ipairs(self.recipe.ingredients) do
local inv_amount = in_inv.get_item_count(ing.name)
local stack_size = game.item_prototypes[ing.name].stack_size
local have = self.reservation:item_count(ing.name)
local tofeed = feed_amounts[ing.name]
if tofeed + inv_amount > stack_size then
tofeed = stack_size - inv_amount
end
if have < tofeed then
table.insert(self.deps, GetItem:new(ing.name, tofeed))
return (Status.busy())
end
real_feed_amounts[ing.name] = tofeed
end
if not near_smelter then
self:go_near_smelter()
return (Status.busy())
end
for _, ing in ipairs(self.recipe.ingredients) do
local amount = real_feed_amounts[ing.name]
if in_inv.can_insert({name=ing.name, count=amount}) then
amount = in_inv.insert({name=ing.name, count=amount})
if ing.name == nil then
ing.name.what.what = 1
end
self.reservation:use_item(ing.name, amount)
else
player.print('cannot insert into furnace')
return (Status.err())
end
end
return (Status.wait())
end
function GetItem:new(item, amount)
local plan = self.raw_new()
plan.item = item
plan.amount = amount
plan.name = "GetItem("..item..","..amount..")"
local recipes = made_by[item]
if recipes == nil then
player.print('cannot make '..item)
return plan
end
local recipe = filter(recipes, function(r)
return r.valid and r.enabled and not r.hidden
end)[1]
if recipe == nil then
player.print('cannot handmake '..item)
return plan
end
plan.recipe_amount = 0
for _, p in ipairs(recipe.products) do
if p.name == plan.item then
plan.recipe_amount = plan.recipe_amount + p.amount
end
end
if plan.recipe_amount > 0 then
plan.recipe = recipe
else
player.print('weirdly cannot make '..item..' with '..recipe.name)
end
return plan
end