-
Notifications
You must be signed in to change notification settings - Fork 7
/
data-util.lua
1385 lines (1276 loc) · 46.4 KB
/
data-util.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
-- WARNING WARNING WARNING
-- This file will be overwritten in mod zipfiles, edit bzlib/data-util.lua
-- WARNING WARNING WARNING
local me = require("me")
local util = {}
util.me = me
util.get_setting = util.me.get_setting
util.titanium_plate = ""
util.titanium_processing = ""
if mods["FactorioExtended-Plus-Core"] then
util.titanium_plate = "titanium-alloy"
else
util.titanium_plate = "titanium-plate"
end
if mods["pyrawores"] then
util.titanium_processing = "titanium-mk01"
else
util.titanium_processing = "titanium-processing"
end
function util.se6()
return mods["space-exploration"] and mods["space-exploration"] >= "0.6"
end
util.cablesg = util.se6() and "electronic" or "cable"
function get_setting(name)
if settings.startup[name] == nil then
return nil
end
return settings.startup[name].value
end
allbypass = {}
if get_setting("bz-recipe-bypass") then
for recipe in string.gmatch(me.get_setting("bz-recipe-bypass"), '[^",%s]+') do
allbypass[recipe] = true
end
end
function util.is_foundry()
return mods.bzfoundry and not me.get_setting("bzfoundry-minimal")
end
function should_force(options)
return options and options.force
end
function bypass(recipe_name)
if me.bypass[recipe_name] then return true end
if allbypass[recipe_name] then return true end
if get_setting("bz-tabula-rasa") then return true end
end
function util.fe_plus(sub)
if mods["FactorioExtended-Plus-"..sub] then
return true
end
end
function util.get_stack_size(default)
if mods.Krastorio2 then
local size = get_setting("kr-stack-size")
if size and tonumber(size) then
return tonumber(size)
end
end
return default
end
function util.k2assets()
if mods["Krastorio2Assets"] then
return "__Krastorio2Assets__"
end
return "__Krastorio2__/graphics"
end
-- check if a table contains a sought value
function util.contains(table, sought)
for i, value in pairs(table) do
if value == sought then
return true
end
end
return false
end
-- se landfill
-- params: ore, icon_size
function util.se_landfill(params)
if mods["space-exploration"] then
if not params.icon_size then params.icon_size = 64 end
local lname="landfill-"..params.ore
data:extend({
{
type = "recipe",
icons = {
{ icon = "__base__/graphics/icons/landfill.png", icon_size = 64, icon_mipmaps = 3 },
{ icon = "__"..me.name.."__/graphics/icons/"..params.ore..".png", icon_size = params.icon_size, scale = 0.33*64/params.icon_size},
},
energy_required = 1,
enabled=false,
name = lname,
category = "hard-recycling",
order = "z-b-"..params.ore,
subgroup = "terrain",
result = "landfill",
ingredients = {{params.ore, 50}},
}
})
util.add_unlock("se-recycling-facility", lname)
end
end
-- k2 matter
-- params: {k2matter}, k2baseicon , {icon}
function util.k2matter(params)
local matter = require("__Krastorio2__/lib/public/data-stages/matter-util")
if mods["space-exploration"] then
params.k2matter.need_stabilizer = true
end
if not params.k2matter.minimum_conversion_quantity then
params.k2matter.minimum_conversion_quantity = 10
end
if not data.raw.technology[params.k2matter.unlocked_by_technology] then
local icon = ""
if params.k2baseicon then
icon = util.k2assets().."/technologies/matter-"..params.k2baseicon..".png"
else
icon = util.k2assets().."/technologies/backgrounds/matter.png"
end
data:extend(
{
{
type = "technology",
name = params.k2matter.unlocked_by_technology,
icons =
{
{
icon = icon,
icon_size = 256,
},
params.icon,
},
prerequisites = {"kr-matter-processing"},
unit =
{
count = 350,
ingredients = mods["space-exploration"] and
{
{"automation-science-pack", 1},
{"logistic-science-pack", 1},
{"chemical-science-pack", 1},
{"se-astronomic-science-pack-4", 1},
{"se-energy-science-pack-4", 1},
{"se-material-science-pack-4", 1},
{"se-deep-space-science-pack-2", 1},
{"se-kr-matter-science-pack-2", 1},
} or
{
{"production-science-pack", 1},
{"utility-science-pack", 1},
{"matter-tech-card", 1}
},
time = 45,
},
-- (ignore for now) localised_name = {"technology-name.k2-conversion", {"item-name."..params.k2matter.item_name}},
},
})
end
matter.createMatterRecipe(params.k2matter)
end
-- se matter
-- params: ore, energy_required, quant_out, quant_in, icon_size, stream_out
function util.se_matter(params)
if mods["space-exploration"] > "0.6" then
if not params.quant_in then params.quant_in = params.quant_out end
if not params.icon_size then params.icon_size = 64 end
local fname = "matter-fusion-"..params.ore
local sedata = mods.Krastorio2 and "se-kr-matter-synthesis-data" or "se-fusion-test-data"
local sejunk = mods.Krastorio2 and "se-broken-data" or "se-junk-data"
data:extend({
{
type = "recipe",
name = fname,
localised_name = {"recipe-name.se-matter-fusion-to", {"item-name."..params.ore}},
category = "space-materialisation",
subgroup = "materialisation",
order = "a-b-z",
icons = {
{icon = "__space-exploration-graphics__/graphics/blank.png",
icon_size = 64, scale = 0.5},
{icon = "__space-exploration-graphics__/graphics/icons/fluid/particle-stream.png",
icon_size = 64, scale = 0.33, shift = {8,-8}},
{icon = "__"..util.me.name.."__/graphics/icons/"..params.ore..".png",
icon_size = params.icon_size, scale = 0.33 * 64/params.icon_size, shift={-8, 8}},
{icon = "__space-exploration-graphics__/graphics/icons/transition-arrow.png",
icon_size = 64, scale = 0.5},
},
energy_required = params.energy_required,
enabled = false,
ingredients = {
{sedata, 1},
{type="fluid", name="se-particle-stream", amount=50},
{type="fluid", name="se-space-coolant-supercooled", amount=25},
},
results = {
{params.ore, params.quant_out},
{"se-contaminated-scrap", 1},
{type=item, name=sedata, amount=1, probability=.99},
{type=item, name=sejunk, amount=1, probability=.01},
{type="fluid", name="se-space-coolant-hot", amount=25, catalyst_amount=25},
}
}
})
util.add_unlock("se-space-matter-fusion", fname)
if mods.Krastorio2 then
local lname = params.ore.."-to-particle-stream"
data:extend({
enabled = false,
{
type = "recipe",
name = lname,
localised_name = {"recipe-name.se-kr-matter-liberation", {"item-name."..params.ore}},
category = "space-materialisation",
subgroup = "advanced-particle-stream",
order = "a-b-z",
icons = {
{icon = "__space-exploration-graphics__/graphics/blank.png",
icon_size = 64, scale = 0.5},
{icon = "__space-exploration-graphics__/graphics/icons/fluid/particle-stream.png",
icon_size = 64, scale = 0.33, shift = {-8,8}},
{icon = "__"..util.me.name.."__/graphics/icons/"..params.ore..".png",
icon_size = params.icon_size, scale = 0.33 * 64/params.icon_size, shift={8, -8}},
{icon = "__space-exploration-graphics__/graphics/icons/transition-arrow.png",
icon_size = 64, scale = 0.5},
},
energy_required = 30,
enabled = false,
ingredients = {
{"se-kr-matter-liberation-data", 1},
{params.ore, params.quant_in},
{type="fluid", name="se-particle-stream", amount=50},
},
results = {
{type=item, name="se-kr-matter-liberation-data", amount=1, probability=.99},
{type=item, name=sejunk, amount=1, probability=.01},
{type="fluid", name="se-particle-stream", amount=params.stream_out, catalyst_amount=50},
}
}
})
if not data.raw.technology["bz-advanced-stream-production"] then
data:extend({
{
type = "technology",
name ="bz-advanced-stream-production",
localised_name = {"", {"technology-name.se-kr-advanced-stream-production"}, " 2"},
icon = "__space-exploration-graphics__/graphics/technology/material-fabricator.png",
icon_size = 128,
effects = {},
unit = {
count = 100,
time = 15,
ingredients = {
{"automation-science-pack", 1},
{"logistic-science-pack", 1},
{"chemical-science-pack", 1},
{"se-rocket-science-pack", 1},
{"space-science-pack", 1},
{"production-science-pack", 1},
{"utility-science-pack", 1},
{"se-astronomic-science-pack-4", 1},
{"se-energy-science-pack-4", 1},
{"se-material-science-pack-4", 1},
{"matter-tech-card", 1},
{"se-deep-space-science-pack-1", 1},
}
},
prerequisites = {"se-kr-advanced-stream-production"},
},
})
end
util.add_unlock("bz-advanced-stream-production", lname)
end
end
end
-- Get the normal prototype for a recipe -- either .normal or the recipe itself
function util.get_normal(recipe_name)
if data.raw.recipe[recipe_name] then
recipe = data.raw.recipe[recipe_name]
if recipe.normal and recipe.normal.ingredients then
return recipe.normal
elseif recipe.ingredients then
return recipe
end
end
end
-- Set/override a technology's prerequisites
function util.set_prerequisite(technology_name, prerequisites)
local technology = data.raw.technology[technology_name]
if technology then
technology.prerequisites = {}
for i, prerequisite in pairs(prerequisites) do
if data.raw.technology[prerequisite] then
table.insert(technology.prerequisites, prerequisite)
end
end
end
end
-- Add a prerequisite to a given technology
function util.add_prerequisite(technology_name, prerequisite)
local technology = data.raw.technology[technology_name]
if technology and data.raw.technology[prerequisite] then
if technology.prerequisites then
for i, pre in pairs(technology.prerequisites) do
if pre == prerequisite then return end
end
table.insert(technology.prerequisites, prerequisite)
else
technology.prerequisites = {prerequisite}
end
end
end
-- Remove a prerequisite from a given technology
function util.remove_prerequisite(technology_name, prerequisite)
local technology = data.raw.technology[technology_name]
local index = -1
if technology then
for i, prereq in pairs(technology.prerequisites) do
if prereq == prerequisite then
index = i
break
end
end
if index > -1 then
table.remove(technology.prerequisites, index)
end
end
end
-- Add an effect to a given technology
function util.add_effect(technology_name, effect)
local technology = data.raw.technology[technology_name]
if technology then
if not technology.effects then technology.effects = {} end
if effect and effect.type == "unlock-recipe" then
if not data.raw.recipe[effect.recipe] then
return
end
table.insert(technology.effects, effect)
end
end
end
-- Add an effect to a given technology to unlock recipe
function util.add_unlock(technology_name, recipe)
util.add_effect(technology_name, {type="unlock-recipe", recipe=recipe})
end
-- Check if a tech unlocks a recipe
function util.check_unlock(technology_name, recipe_name)
local technology = data.raw.technology[technology_name]
if technology and technology.effects then
for i, effect in pairs(technology.effects) do
if effect.type == "unlock-recipe" and effect.recipe == recipe_name then
return true
end
end
end
return false
end
-- remove recipe unlock effect from a given technology, multiple times if necessary
function util.remove_recipe_effect(technology_name, recipe_name)
local technology = data.raw.technology[technology_name]
local index = -1
local cnt = 0
if technology and technology.effects then
for i, effect in pairs(technology.effects) do
if effect.type == "unlock-recipe" and effect.recipe == recipe_name then
index = i
cnt = cnt + 1
end
end
if index > -1 then
table.remove(technology.effects, index)
if cnt > 1 then -- not over yet, do it again
util.remove_recipe_effect(technology_name, recipe_name)
end
end
end
end
-- Set technology ingredients
function util.set_tech_recipe(technology_name, ingredients)
local technology = data.raw.technology[technology_name]
if technology then
technology.unit.ingredients = ingredients
end
end
function util.set_enabled(recipe_name, enabled)
if data.raw.recipe[recipe_name] then
if data.raw.recipe[recipe_name].normal then data.raw.recipe[recipe_name].normal.enabled = enabled end
if data.raw.recipe[recipe_name].expensive then data.raw.recipe[recipe_name].expensive.enabled = enabled end
if not data.raw.recipe[recipe_name].normal then data.raw.recipe[recipe_name].enabled = enabled end
end
end
function util.set_hidden(recipe_name)
if data.raw.recipe[recipe_name] then
if data.raw.recipe[recipe_name].normal then data.raw.recipe[recipe_name].normal.hidden = true end
if data.raw.recipe[recipe_name].expensive then data.raw.recipe[recipe_name].expensive.hidden = true end
if not data.raw.recipe[recipe_name].normal then data.raw.recipe[recipe_name].hidden = true end
end
end
-- Add a given quantity of ingredient to a given recipe
function util.add_or_add_to_ingredient(recipe_name, ingredient, quantity, options)
if not should_force(options) and bypass(recipe_name) then return end
if data.raw.recipe[recipe_name] and data.raw.item[ingredient] then
me.add_modified(recipe_name)
add_or_add_to_ingredient(data.raw.recipe[recipe_name], ingredient, quantity)
add_or_add_to_ingredient(data.raw.recipe[recipe_name].normal, ingredient, quantity)
add_or_add_to_ingredient(data.raw.recipe[recipe_name].expensive, ingredient, quantity)
end
end
function add_or_add_to_ingredient(recipe, ingredient, quantity)
if recipe ~= nil and recipe.ingredients ~= nil then
for i, existing in pairs(recipe.ingredients) do
if existing[1] == ingredient or existing.name == ingredient then
add_to_ingredient(recipe, ingredient, quantity)
return
end
end
table.insert(recipe.ingredients, {ingredient, quantity})
end
end
-- Add a given quantity of ingredient to a given recipe
function util.add_ingredient(recipe_name, ingredient, quantity, options)
if not should_force(options) and bypass(recipe_name) then return end
local is_fluid = not not data.raw.fluid[ingredient]
if data.raw.recipe[recipe_name] and (data.raw.item[ingredient] or is_fluid) then
me.add_modified(recipe_name)
add_ingredient(data.raw.recipe[recipe_name], ingredient, quantity, is_fluid)
add_ingredient(data.raw.recipe[recipe_name].normal, ingredient, quantity, is_fluid)
add_ingredient(data.raw.recipe[recipe_name].expensive, ingredient, quantity, is_fluid)
end
end
function add_ingredient(recipe, ingredient, quantity, is_fluid)
if recipe ~= nil and recipe.ingredients ~= nil then
for i, existing in pairs(recipe.ingredients) do
if existing[1] == ingredient or existing.name == ingredient then
return
end
end
if is_fluid then
table.insert(recipe.ingredients, {type="fluid", name=ingredient, amount=quantity})
else
table.insert(recipe.ingredients, {ingredient, quantity})
end
end
end
-- Add a given ingredient prototype to a given recipe
function util.add_ingredient_raw(recipe_name, ingredient, options)
if not should_force(options) and bypass(recipe_name) then return end
if data.raw.recipe[recipe_name] and (data.raw.item[ingredient.name] or data.raw.item[ingredient[1]]) then
me.add_modified(recipe_name)
add_ingredient_raw(data.raw.recipe[recipe_name], ingredient)
add_ingredient_raw(data.raw.recipe[recipe_name].normal, ingredient)
add_ingredient_raw(data.raw.recipe[recipe_name].expensive, ingredient)
end
end
function add_ingredient_raw(recipe, ingredient)
if recipe ~= nil and recipe.ingredients ~= nil then
for i, existing in pairs(recipe.ingredients) do
if (
(existing[1] and (existing[1] == ingredient[1] or existing[1] == ingredient.name)) or
(existing.name and (existing.name == ingredient[1] or existing.name == ingredient.name))
) then
return
end
end
table.insert(recipe.ingredients, ingredient)
end
end
-- Set an ingredient to a given quantity
function util.set_ingredient(recipe_name, ingredient, quantity, options)
if not should_force(options) and bypass(recipe_name) then return end
if data.raw.recipe[recipe_name] and data.raw.item[ingredient] then
me.add_modified(recipe_name)
set_ingredient(data.raw.recipe[recipe_name], ingredient, quantity)
set_ingredient(data.raw.recipe[recipe_name].normal, ingredient, quantity)
set_ingredient(data.raw.recipe[recipe_name].expensive, ingredient, quantity)
end
end
function set_ingredient(recipe, ingredient, quantity)
if recipe ~= nil and recipe.ingredients ~= nil then
for i, existing in pairs(recipe.ingredients) do
if existing[1] == ingredient then
existing[2] = quantity
return
elseif existing.name == ingredient then
existing.amount = quantity
existing.amount_min = nil
existing.amount_max = nil
return
end
end
table.insert(recipe.ingredients, {ingredient, quantity})
end
end
-- Add a given quantity of product to a given recipe.
-- Only works for recipes with multiple products
function util.add_product(recipe_name, product, options)
if not should_force(options) and bypass(recipe_name) then return end
if data.raw.recipe[recipe_name] and
(data.raw.item[product[1]] or data.raw.item[product.name] or
data.raw.fluid[product[1]] or data.raw.fluid[product.name]
) then
add_product(data.raw.recipe[recipe_name], product)
add_product(data.raw.recipe[recipe_name].normal, product)
add_product(data.raw.recipe[recipe_name].expensive, product)
end
end
function add_product(recipe, product)
if recipe ~= nil then
if (product[1] and data.raw.item[product[1]]) or
(product.name and data.raw[product.type][product.name]) then
if not recipe.normal then
if recipe.results == nil then
recipe.results = {{recipe.result, recipe.result_count and recipe.result_count or 1}}
end
recipe.result = nil
recipe.result_count = nil
table.insert(recipe.results, product)
end
end
end
end
-- Get the amount of the ingredient, will check base/normal not expensive
function util.get_ingredient_amount(recipe_name, ingredient_name)
local recipe = data.raw.recipe[recipe_name]
if recipe then
if recipe.normal and recipe.normal.ingredients then
for i, ingredient in pairs(recipe.normal.ingredients) do
if ingredient[1] == ingredient_name then return ingredient[2] end
if ingredient.name == ingredient_name then return ingredient.amount end
end
elseif recipe.ingredients then
for i, ingredient in pairs(recipe.ingredients) do
if ingredient[1] == ingredient_name then return ingredient[2] end
if ingredient.name == ingredient_name then return ingredient.amount end
end
end
return 0
end
return 0
end
-- Get the amount of the result, will check base/normal not expensive
function util.get_amount(recipe_name, product)
if not product then product = recipe_name end
local recipe = data.raw.recipe[recipe_name]
if recipe then
if recipe.normal and recipe.normal.results then
for i, result in pairs(recipe.normal.results) do
if result[1] == product then return result[2] end
if result.name == product then return result.amount end
end
elseif recipe.normal and recipe.normal.result_count then
return recipe.normal.result_count
elseif recipe.results then
for i, result in pairs(recipe.results) do
if result[1] == product then return result[2] end
if result.name == product then return result.amount end
end
elseif recipe.result_count then
return recipe.result_count
end
return 1
end
return 0
end
-- Get the count of results
function util.get_result_count(recipe_name, product)
if not product then product = recipe_name end
local recipe = data.raw.recipe[recipe_name]
if recipe then
if recipe.normal and recipe.normal.results then
return #(recipe.normal.results)
elseif recipe.results then
return #(recipe.results)
end
return 1
end
return 0
end
-- Replace one ingredient with another in a recipe
-- Use amount to set an amount. If that amount is a multiplier instead of an exact amount, set multiply true.
function util.replace_ingredient(recipe_name, old, new, amount, multiply, options)
if not should_force(options) and bypass(recipe_name) then return end
if data.raw.recipe[recipe_name] and (data.raw.item[new] or data.raw.fluid[new]) then
me.add_modified(recipe_name)
replace_ingredient(data.raw.recipe[recipe_name], old, new, amount, multiply)
replace_ingredient(data.raw.recipe[recipe_name].normal, old, new, amount, multiply)
replace_ingredient(data.raw.recipe[recipe_name].expensive, old, new, amount, multiply)
end
end
function replace_ingredient(recipe, old, new, amount, multiply)
if recipe ~= nil and recipe.ingredients ~= nil then
for i, existing in pairs(recipe.ingredients) do
if existing[1] == new or existing.name == new then
return
end
end
for i, ingredient in pairs(recipe.ingredients) do
if ingredient.name == old then
ingredient.name = new
if amount then
if multiply then
ingredient.amount = amount * ingredient.amount
else
ingredient.amount = amount
end
end
end
if ingredient[1] == old then
ingredient[1] = new
if amount then
if multiply then
ingredient[2] = amount * ingredient[2]
else
ingredient[2] = amount
end
end
end
end
end
end
-- Remove an ingredient from a recipe
function util.remove_ingredient(recipe_name, old, options)
if not should_force(options) and bypass(recipe_name) then return end
if data.raw.recipe[recipe_name] then
me.add_modified(recipe_name)
remove_ingredient(data.raw.recipe[recipe_name], old)
remove_ingredient(data.raw.recipe[recipe_name].normal, old)
remove_ingredient(data.raw.recipe[recipe_name].expensive, old)
end
end
function remove_ingredient(recipe, old)
index = -1
if recipe ~= nil and recipe.ingredients ~= nil then
for i, ingredient in pairs(recipe.ingredients) do
if ingredient.name == old or ingredient[1] == old then
index = i
break
end
end
if index > -1 then
table.remove(recipe.ingredients, index)
end
end
end
-- Replace an amount of a product, leaving at least 1 of old
function util.replace_some_product(recipe_name, old, old_amount, new, new_amount, options)
if not should_force(options) and bypass(recipe_name) then return end
local is_fluid = not not data.raw.fluid[new] -- NOTE CURRENTLY UNUSUED
if data.raw.recipe[recipe_name] and (data.raw.item[new] or is_fluid) then
me.add_modified(recipe_name)
replace_some_product(data.raw.recipe[recipe_name], old, old_amount, new, new_amount, is_fluid)
replace_some_product(data.raw.recipe[recipe_name].normal, old, old_amount, new, new_amount, is_fluid)
replace_some_product(data.raw.recipe[recipe_name].expensive, old, old_amount, new, new_amount, is_fluid)
end
end
function replace_some_product(recipe, old, old_amount, new, new_amount)
if recipe ~= nil then
if recipe.result == new then return end
if recipe.results then
for i, existing in pairs(recipe.results) do
if existing[1] == new or existing.name == new then
return
end
end
end
add_product(recipe, {new, new_amount})
for i, product in pairs(recipe.results) do
if product.name == old then
product.amount = math.max(1, product.amount - old_amount)
end
if product[1] == old then
product[2] = math.max(1, product[2] - old_amount)
end
end
end
end
-- Replace an amount of an ingredient in a recipe. Keep at least 1 of old.
function util.replace_some_ingredient(recipe_name, old, old_amount, new, new_amount, options)
if not should_force(options) and bypass(recipe_name) then return end
local is_fluid = not not data.raw.fluid[new]
if data.raw.recipe[recipe_name] and (data.raw.item[new] or is_fluid) then
me.add_modified(recipe_name)
replace_some_ingredient(data.raw.recipe[recipe_name], old, old_amount, new, new_amount, is_fluid)
replace_some_ingredient(data.raw.recipe[recipe_name].normal, old, old_amount, new, new_amount, is_fluid)
replace_some_ingredient(data.raw.recipe[recipe_name].expensive, old, old_amount, new, new_amount, is_fluid)
end
end
function replace_some_ingredient(recipe, old, old_amount, new, new_amount, is_fluid)
if recipe ~= nil and recipe.ingredients ~= nil then
for i, existing in pairs(recipe.ingredients) do
if existing[1] == new or existing.name == new then
return
end
end
for i, ingredient in pairs(recipe.ingredients) do
if ingredient.name == old then
ingredient.amount = math.max(1, ingredient.amount - old_amount)
end
if ingredient[1] == old then
ingredient[2] = math.max(1, ingredient[2] - old_amount)
end
end
add_ingredient(recipe, new, new_amount, is_fluid)
end
end
-- set the amount of a product.
function util.set_product_amount(recipe_name, product, amount, options)
if not should_force(options) and bypass(recipe_name) then return end
me.add_modified(recipe_name)
if data.raw.recipe[recipe_name] then
set_product_amount(data.raw.recipe[recipe_name], product, amount)
set_product_amount(data.raw.recipe[recipe_name].normal, product, amount)
set_product_amount(data.raw.recipe[recipe_name].expensive, product, amount)
end
end
function set_product_amount(recipe, product, amount)
if recipe then
if recipe.result_count then
recipe.result_count = amount
end
if recipe.results then
for i, result in pairs(recipe.results) do
if result.name == product then
if result.amount then
result.amount = amount
end
if result.amount_min ~= nil then
result.amount_min = nil
result.amount_max = nil
result.amount = amount
end
end
if result[1] == product then
result[2] = amount
end
end
end
if not recipe.results and not recipe.result_count then
-- implicit one item result
recipe.result_count = amount
end
end
end
-- multiply the cost, energy, and results of a recipe by a multiple
function util.multiply_recipe(recipe_name, multiple, options)
if not should_force(options) and bypass(recipe_name) then return end
me.add_modified(recipe_name)
if data.raw.recipe[recipe_name] then
multiply_recipe(data.raw.recipe[recipe_name], multiple)
multiply_recipe(data.raw.recipe[recipe_name].normal, multiple)
multiply_recipe(data.raw.recipe[recipe_name].expensive, multiple)
end
end
function multiply_recipe(recipe, multiple)
if recipe then
if recipe.energy_required then
recipe.energy_required = recipe.energy_required * multiple
else
recipe.energy_required = 0.5 * multiple -- 0.5 is factorio default
end
if recipe.result_count then
recipe.result_count = recipe.result_count * multiple
end
if recipe.results then
for i, result in pairs(recipe.results) do
if result.name then
if result.amount then
result.amount = result.amount * multiple
end
if result.amount_min ~= nil then
result.amount_min = result.amount_min * multiple
result.amount_max = result.amount_max * multiple
end
if result.catalyst_amount then
result.catalyst_amount = result.catalyst_amount * multiple
end
end
if result[1] then
result[2] = result[2] * multiple
end
end
end
if not recipe.results and not recipe.result_count then
-- implicit one item result
recipe.result_count = multiple
end
if recipe.ingredients then
for i, ingredient in pairs(recipe.ingredients) do
if ingredient.name then
ingredient.amount = ingredient.amount * multiple
end
if ingredient[1] then
ingredient[2] = ingredient[2] * multiple
end
end
end
end
end
-- Returns true if a recipe has an ingredient
function util.has_ingredient(recipe_name, ingredient)
return data.raw.recipe[recipe_name] and (
has_ingredient(data.raw.recipe[recipe_name], ingredient) or
has_ingredient(data.raw.recipe[recipe_name].normal, ingredient))
end
function has_ingredient(recipe, ingredient)
if recipe ~= nil and recipe.ingredients ~= nil then
for i, existing in pairs(recipe.ingredients) do
if existing[1] == ingredient or existing.name == ingredient then
return true
end
end
end
return false
end
-- Remove a product from a recipe, WILL NOT remove the only product
function util.remove_product(recipe_name, old, options)
if not should_force(options) and bypass(recipe_name) then return end
me.add_modified(recipe_name)
if data.raw.recipe[recipe_name] then
remove_product(data.raw.recipe[recipe_name], old)
remove_product(data.raw.recipe[recipe_name].normal, old)
remove_product(data.raw.recipe[recipe_name].expensive, old)
end
end
function remove_product(recipe, old)
index = -1
if recipe ~= nil and recipe.results ~= nil then
for i, result in pairs(recipe.results) do
if result.name == old or result[1] == old then
index = i
break
end
end
if index > -1 then
table.remove(recipe.results, index)
end
end
end
function util.set_main_product(recipe_name, product, options)
if not should_force(options) and bypass(recipe_name) then return end
if data.raw.recipe[recipe_name] then
set_main_product(data.raw.recipe[recipe_name], product)
set_main_product(data.raw.recipe[recipe_name].normal, product)
set_main_product(data.raw.recipe[recipe_name].expensive, product)
end
end
function set_main_product(recipe, product)
if recipe then
recipe.main_product = product
end
end
-- Replace one product with another in a recipe
function util.replace_product(recipe_name, old, new, options)
if not should_force(options) and bypass(recipe_name) then return end
if data.raw.recipe[recipe_name] then
replace_product(data.raw.recipe[recipe_name], old, new, options)
replace_product(data.raw.recipe[recipe_name].normal, old, new, options)
replace_product(data.raw.recipe[recipe_name].expensive, old, new, options)
end
end
function replace_product(recipe, old, new, options)
if recipe then
if recipe.main_product == old then
recipe.main_product = new
end
if recipe.result == old then
recipe.result = new
return
end
if recipe.results then
for i, result in pairs(recipe.results) do
if result.name == old then result.name = new end
if result[1] == old then result[1] = new end
end
end
end
end
-- Remove an element of type t and name from data.raw
function util.remove_raw(t, name)
if not data.raw[t] then
log(t.." not found in data.raw")
return
end
if data.raw[t][name] then
for i, elem in pairs(data.raw[t]) do
if elem.name == name then
data.raw[t][i] = nil
break
end
end
end
end
-- Set energy required
function util.set_recipe_time(recipe_name, time, options)
if not should_force(options) and bypass(recipe_name) then return end
me.add_modified(recipe_name)
if data.raw.recipe[recipe_name] then
set_recipe_time(data.raw.recipe[recipe_name], time)
set_recipe_time(data.raw.recipe[recipe_name].normal, time)
set_recipe_time(data.raw.recipe[recipe_name].expensive, time)
end
end
function set_recipe_time(recipe, time)
if recipe then
if recipe.energy_required then
recipe.energy_required = time
end
end
end
-- Multiply energy required
function util.multiply_time(recipe_name, factor, options)
if not should_force(options) and bypass(recipe_name) then return end
me.add_modified(recipe_name)
if data.raw.recipe[recipe_name] then
multiply_time(data.raw.recipe[recipe_name], factor)
multiply_time(data.raw.recipe[recipe_name].normal, factor)
multiply_time(data.raw.recipe[recipe_name].expensive, factor)
end
end
function multiply_time(recipe, factor)
if recipe then
if recipe.energy_required then
recipe.energy_required = recipe.energy_required * factor
end
end