forked from UberWaffe/ScienceCostTweaker
-
Notifications
You must be signed in to change notification settings - Fork 17
/
sctmlib.lua
884 lines (846 loc) · 26.6 KB
/
sctmlib.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
-- sctm helper functions
if not sctm then sctm = {} end
-- uncomment this to enable debug output
-- sctm.enabledebug = true
function sctm.debug(logtext)
if (sctm.enabledebug) then
log("SCTD: " .. logtext)
end
end
function sctm.log(logtext)
log("SCT: " .. logtext)
end
-- lab functions
function sctm.lab_input_remove(labname, packname)
local removed = false
sctm.debug("removing " .. packname .. " from " .. labname)
if data.raw.lab[labname] and data.raw.lab[labname].inputs then
local labinputs = data.raw.lab[labname].inputs
local newinputs = {}
for _i, inputpack in pairs(labinputs) do
if inputpack and inputpack == packname then
-- table.remove(labinputs, _i)
-- labinputs[_i] = nil
removed = true
-- break
else
table.insert(newinputs, inputpack)
end
end
if removed then
data.raw.lab[labname].inputs = newinputs
sctm.debug("removed " .. packname .. " from " .. labname)
end
end
if not data.raw.lab[labname] then
sctm.debug("attempting to modify nonexistent lab " .. labname)
end
return removed
end
function sctm.lab_input_add(labname, packname)
local added = false
if data.raw.lab[labname] and data.raw.tool[packname] then
if not data.raw.lab[labname].inputs then
data.raw.lab[labname].inputs = {}
end
local labinputs = data.raw.lab[labname].inputs
local hasinput = false
for _i, inputpack in pairs(labinputs) do
if inputpack and inputpack == packname then
hasinput = true
added = true
break
end
end
if not hasinput then
local inputsize = table_size(labinputs)
labinputs[inputsize + 1] = packname
added = true
end
end
if not data.raw.lab[labname] then
sctm.debug("attempting to modify nonexistent lab " .. labname)
end
if not data.raw.tool[packname] then
sctm.debug("attempting to insert nonexistent science pack " .. packname)
end
return added
end
-- technology functions
local function removeprereq(prereqtable, depname)
local removed = false
for _i, dep in pairs(prereqtable) do
if dep and dep == depname then
table.remove(prereqtable, _i)
-- prereqtable[_i] = nil
removed = true
break
end
end
return removed
end
function sctm.tech_dependency_remove(techname, depname)
local removed = false
if data.raw.technology[techname] then
local tech = data.raw.technology[techname]
local hasdiff = false
if tech.normal then
if tech.normal.prerequisites then
removed = removeprereq(tech.normal.prerequisites, depname)
hasdiff = true
end
end
if tech.expensive then
if tech.expensive.prerequisites then
removed = removeprereq(tech.expensive.prerequisites, depname) or removed
hasdiff = true
end
end
if not hasdiff and tech.prerequisites then
removed = removeprereq(tech.prerequisites, depname) or removed
end
end
if not data.raw.technology[techname] then
sctm.debug("attempting to update nonexistent technology " .. techname)
end
return removed
end
local function addprereq(prereqtable, depname)
local hasdep = false
local added = false
for _i, dep in pairs(prereqtable) do
if dep and dep == depname then
hasdep = true
added = true
break
end
end
if not hasdep then
local prereqsize = table_size(prereqtable)
prereqtable[prereqsize + 1] = depname
added = true
end
end
function sctm.tech_dependency_add(techname, depname, hidden)
local added = false
local addhidden = hidden or false
sctm.debug("insert dep " .. depname .. " into " .. techname)
if data.raw.technology[techname] and (data.raw.technology[techname].enabled or true) and not (data.raw.technology[techname].hidden or false) and
data.raw.technology[depname] and (data.raw.technology[depname].enabled or true) and (not (data.raw.technology[depname].hidden or false) or addhidden) then
local tech = data.raw.technology[techname]
local hasdiff = false
if tech.normal then
if not tech.normal.prerequisites then
tech.normal.prerequisites = {}
end
added = addprereq(tech.normal.prerequisites, depname)
hasdiff = true
end
if tech.expensive then
if not tech.expensive.prerequisites then
tech.expensive.prerequisites = {}
end
added = addprereq(tech.expensive.prerequisites, depname) or added
hasdiff = true
end
if not hasdiff then
if not tech.prerequisites then
tech.prerequisites = {}
end
added = addprereq(tech.prerequisites, depname)
end
end
--sctm.debug(techname .. ":" .. serpent.block(data.raw.technology[techname]))
if not data.raw.technology[techname] or not (data.raw.technology[techname].enabled or true) or (data.raw.technology[techname].hidden or false) then
sctm.debug("attempting to update nonexistent or disabled technology " .. techname)
end
if not data.raw.technology[depname] or not (data.raw.technology[depname].enabled or true) or not (not (data.raw.technology[depname].hidden or false) or addhidden) then
sctm.debug("attempting to insert nonexistent or disabled technology " .. depname)
end
return added
end
function sctm.tech_dependency_get(techname)
local deps = {}
sctm.debug("retrieving dps for " .. techname)
if data.raw.technology[techname] then
local tech = data.raw.technology[techname]
if tech.normal and tech.normal.prerequisites then
for _i, prereq in pairs(tech.normal.prerequisites) do
table.insert(deps, prereq)
end
end
if tech.expensive and tech.expensive.prerequisites then
for _i, prereq in pairs(tech.expensive.prerequisites) do
table.insert(deps, prereq)
end
end
if tech.prerequisites then
for _i, prereq in pairs(tech.prerequisites) do
table.insert(deps, prereq)
end
end
end
return deps
end
-- ingredients = { ["automation-science-pack"] = 1 }
local function rempack(ingredientstable, packname)
local removed = false
for _i, pack in pairs(ingredientstable) do
if pack and (pack[1] == packname or (pack.name and pack.name == packname))then
table.remove(ingredientstable,_i)
-- ingredientstable[_i] = nil
removed = true
break
end
end
return removed
end
function sctm.tech_pack_remove(techname, packname)
sctm.debug("remove pack " .. packname .. " from " .. techname)
local removed = false
if data.raw.technology[techname] then
local tech = data.raw.technology[techname]
local hasdiff = false
if tech.normal then
if tech.normal.unit and tech.normal.unit.ingredients then
removed = rempack(tech.normal.unit.ingredients, packname)
end
hasdiff = true
end
if tech.expensive then
if tech.expensive.unit and tech.expensive.unit.ingredients then
removed = rempack(tech.expensive.unit.ingredients, packname) or removed
end
hasdiff = true
end
if not hasdiff then
if tech.unit and tech.unit.ingredients then
removed = rempack(tech.unit.ingredients, packname)
end
end
end
if not data.raw.technology[techname] then
sctm.debug("attempting to update nonexistent technology " .. techname)
end
return removed
end
-- ingredients = { ["automation-science-pack"] = 1 }
local function addpack(ingredientstable, newpack)
local added = false
local found = false
for _i, pack in pairs(ingredientstable) do
if pack and (pack[1] == newpack[1])then
found = true
break
end
end
if not found then
local ingredientsize = table_size(ingredientstable)
ingredientstable[ingredientsize + 1] = newpack
added = true
end
return addedd
end
function sctm.tech_pack_add(techname, packnormal, packexpensive)
local added = false
if not packnormal and not packexpensive then
return false
end
local normal = packnormal and table.deepcopy(packnormal) or table.deepcopy(packexpensive)
local expensive = packexpensive and table.deepcopy(packexpensive) or table.deepcopy(packnormal)
sctm.debug("add pack " .. normal[1] .. " to " .. techname)
if data.raw.technology[techname] and data.raw.tool[normal[1]] then
local tech = data.raw.technology[techname]
local hasdiff = false
if tech.normal then
if tech.normal.unit then
if not tech.normal.unit.ingredients then
tech.normal.ingredients = {}
end
added = addpack(tech.normal.unit.ingredients, normal)
end
hasdiff = true
end
if tech.expensive then
if tech.expensive.unit then
if not tech.expensive.unit.ingredients then
tech.expensive.unit.ingredients = {}
end
added = addpack(tech.expensive.unit.ingredients, expensive) or added
end
hasdiff = true
end
if not hasdiff or not added then
if tech.unit then
if not tech.unit.ingredients then
tech.unit.ingredients = {}
end
added = addpack(tech.unit.ingredients, normal)
end
end
end
if not data.raw.technology[techname] then
sctm.debug("attempting to update nonexistent technology " .. techname)
end
if not data.raw.tool[normal[1]] then
sctm.debug("attempting to add nonexistent pack " .. normal[1])
end
return added
end
local function replacepack(ingredientstable, oldpackname, newpackname)
local replaced = false
for _i, pack in pairs(ingredientstable) do
if pack and pack[1] == oldpackname then
pack[1] = newpackname
replaced = true
break
end
if pack and pack.name and pack.name == oldpackname then
pack.name = newpackname
replaced = true
break
end
end
return replaced
end
function sctm.tech_pack_replace(techname, oldpackname, newpackname)
sctm.debug("replace pack " .. oldpackname .. " by " .. newpackname .. " in " .. techname)
local replaced = false
if data.raw.technology[techname] and data.raw.tool[newpackname] then
local tech = data.raw.technology[techname]
local hasdiff = false
if tech.normal then
if tech.normal.unit and tech.normal.unit.ingredients then
replaced = replacepack(tech.normal.unit.ingredients, oldpackname, newpackname)
end
hasdiff = true
end
if tech.expensive then
if tech.expensive.unit and tech.expensive.unit.ingredients then
replaced = replacepack(tech.expensive.unit.ingredients, oldpackname, newpackname) or replaced
end
hasdiff = true
end
if not hasdiff or not replaced then
if tech.unit and tech.unit.ingredients then
replaced = replacepack(tech.unit.ingredients, oldpackname, newpackname)
end
end
end
if not data.raw.technology[techname] then
sctm.debug("attempting to update nonexistent technology " .. techname)
end
if not data.raw.tool[oldpackname] then
sctm.debug("attempting to remove nonexistent pack " .. oldpackname)
end
if not data.raw.tool[newpackname] then
sctm.debug("attempting to insert nonexistent pack " .. newpackname)
end
return replaced
end
local function addunlock(effectstable, recipename)
local hasunlock = false
for _i, effect in pairs(effectstable) do
if effect and effect.type == "unlock-recipe" and effect.recipe == recipename then
hasunlock = true
break
end
end
if not hasunlock then
local effectsize = table_size(effectstable)
effectstable[effectsize + 1] = { type="unlock-recipe", recipe = recipename }
end
end
function sctm.tech_unlock_add(techname, recipename)
if data.raw.recipe[recipename] and data.raw.technology[techname] then
local tech = data.raw.technology[techname]
local hasdiff = false
if tech.normal then
if not tech.normal.effects then
tech.normal.effects = {}
end
addunlock(tech.normal.effects, recipename)
hasdiff = true
end
if tech.expensive then
if not tech.expensive.effects then
tech.expensive.effects = {}
end
addunlock(tech.expensive.effects, recipename)
hasdiff = true
end
if not hasdiff then
if not tech.effects then
tech.effects = {}
end
addunlock(tech.effects, recipename)
end
end
if not data.raw.technology[techname] then
sctm.debug("attempting to update nonexistent technology " .. techname)
end
if not data.raw.recipe[recipename] then
sctm.debug("attempting to insert nonexistent recipe " .. recipename)
end
end
local function removeunlock(effectstable, recipename)
local removed = false
for _i, effect in pairs(effectstable) do
if effect and effect.type == "unlock-recipe" and effect.recipe == recipename then
table.remove(effectstable, _i)
-- effectstable[_i] = nil
removed = true
break
end
end
return removed
end
function sctm.tech_unlock_remove(techname, recipename)
local removed = false
if data.raw.technology[techname] then
local tech = data.raw.technology[techname]
local hasdiff = false
if tech.normal then
if tech.normal.effects then
removed = removeunlock(tech.normal.effects, recipename)
end
hasdiff = true
end
if tech.expensive then
if tech.expensive.effects then
removed = removeunlock(tech.expensive.effects, recipename) or removed
end
hasdiff = true
end
if not hasdiff then
if tech.effects then
removed = removeunlock(tech.effects, recipename)
end
end
end
if not data.raw.technology[techname] then
sctm.debug("attempting to update nonexistent technology " .. techname)
end
return removed
end
local function removeknownpacks(effectstable, packtable, techname)
local ts = table_size(effectstable)
local removed = false
for _j = ts, 1, -1 do
local effect = effectstable[_j]
if effect and effect.type == "unlock-recipe" then
local name = effect.recipe
local removedone = false
for _p, pack in pairs(packtable) do
if (pack.partial and name.find(pack.name, 1, true) ~= nil) or (not pack.partial and name == pack.name) then
sctm.debug("Moved science pack '" .. name .. "', unlocked by '" .. techname .. "' to research tree.")
table.remove(effectstable, _j)
-- effectstable[_j] = nil
removedone = true
end
end
if not removedone and name:find("science-pack",1,true) ~= nil and name:find("alien",1,true) == nil then
sctm.log("Found unknown science pack '" .. name .. "', unlocked by '" .. techname .. "'")
end
removed = removed or removedone
end
end
return removed
end
function sctm.tech_remove_known_packs(techname, packlist)
local removed = false
if data.raw.technology[techname] then
local hasdif = false
local tech = data.raw.technology[techname]
if tech.expensive then
hasdif = true
if tech.expensive.effects and table_size(tech.expensive.effects) then
removed = removeknownpacks(tech.expensive.effects, packlist, techname)
end
end
if tech.normal then
hasdif = true
if tech.normal.effects and table_size(tech.normal.effects) then
removed = removeknownpacks(tech.normal.effects, packlist, techname) and removed
end
end
if not hasdif and tech.effects and table_size(tech.effects) then
removed = removeknownpacks(tech.effects, packlist, techname)
end
end
if not data.raw.technology[techname] then
sctm.debug("attempting to update nonexistent technology" .. techname)
end
return removed
end
function sctm.tech_replace(oldtechname, newtechname)
local replaced = false
if data.raw.technology[oldtechname] and data.raw.technology[newtechname] then
local oldtech = data.raw.technology[oldtechname]
local newtech = table.deepcopy(data.raw.technology[newtechname])
if oldtech.effects then
for _, eff in pairs(oldtech.effects) do
if (not sctm.find_in_table(newtech.effects, eff)) then
local effectsize = table_size(newtech.effects)
newtech.effects[effectsize + 1] = eff
end
end
end
newtech.name = oldtech.name
data.raw.technology[newtechname].enabled = false
data.raw.technology[newtechname].hidden = true
data.raw.technology[oldtechname] = newtech
replaced = true;
end
return replaced
end
function sctm.tech_disable(techname)
local disabled = false
if data.raw.technology[techname] then
local removed
for _i, tech in pairs(data.raw.technology) do
removed = sctm.tech_dependency_remove(_i, techname)
if removed then
sctm.debug("removed " .. techname .. " dependency from " .. _i)
end
end
data.raw.technology[techname].enabled = false
disabled = true
end
if not data.raw.technology[techname] then
sctm.debug("attempting to update nonexistent technology " .. techname)
end
return disabled
end
-- recipe functions
local function removeingredient(ingredientstable, ingredientname)
local removed = false
for _i, ingredient in pairs(ingredientstable) do
if ingredient and (ingredient[1] == ingredientname or (ingredient.name and ingredient.name == ingredientname)) then
table.remove(ingredientstable, _i)
-- ingredientstable[_i] = nil
removed = true
break
end
end
return removed
end
function sctm.recipe_ingredient_remove(recipename, ingredientname)
local removed = false
if data.raw.recipe[recipename] then
local recipe = data.raw.recipe[recipename]
local hasdiff = false
if recipe.normal then
if recipe.normal.ingredients then
removed = removeingredient(recipe.normal.ingredients, ingredientname)
end
hasdiff = true
end
if recipe.expensive then
if recipe.expensive.ingredients then
removed = removeingredient(recipe.expensive.ingredients, ingredientname) or removed
end
hasdiff = true
end
if not hasdiff then
if recipe.ingredients then
removed = removeingredient(recipe.ingredients, ingredientname)
end
end
end
if not data.raw.recipe[recipename] then
sctm.debug("attempting to update nonexistent recipe " .. recipename)
end
return removed
end
local function addingredient(ingredientstable, newingredient)
local added = false
for _i, ingredient in pairs(ingredientstable) do
if ingredient and ingredient[1] == newingredient.name then
ingredientstable[_i] = newingredient
added = true
break
elseif ingredient and ingredient.name and ingredient.name == newingredient.name then
ingredient.amount = newingredient.amount
added = true
break
end
end
if not added then
local ingredientsize = table_size(ingredientstable)
ingredientstable[ingredientsize + 1] = newingredient
added = true
end
return added
end
function sctm.recipe_ingredient_add(recipename, ingredientnormal, ingredientexpensive)
local added = false
local normal = ingredientnormal and table.deepcopy(ingredientnormal) or table.deepcopy(ingredientexpensive)
local expensive = ingredientexpensive and table.deepcopy(ingredientexpensive) or table.deepcopy(ingredientnormal)
sctm.debug(recipename .. " insert " .. normal.name .. ", " .. expensive.name)
if data.raw.recipe[recipename] and (data.raw.item[normal.name] or data.raw.fluid[normal.name]) and (data.raw.item[expensive.name] or data.raw.fluid[expensive.name]) then
local recipe = data.raw.recipe[recipename]
local hasdiff = false
if recipe.normal then
if not recipe.normal.ingredients then
recipe.normal.ingredients = {}
end
added = addingredient(recipe.normal.ingredients, normal)
hasdiff = true
end
if recipe.expensive then
if not recipe.expensive.ingredients then
recipe.expensive.ingredients = {}
end
added = addingredient(recipe.expensive.ingredients, expensive) or added
hasdiff = true
end
if not hasdiff then
if not recipe.ingredients then
recipe.ingredients = {}
end
added = addingredient(recipe.ingredients, normal)
end
end
if not data.raw.recipe[recipename] then
sctm.debug("attempting to update nonexistent recipe " .. recipename)
end
if not data.raw.item[normal.name] and not data.raw.fluid[normal.name] then
sctm.debug("attempting to insert nonexistent ingredient " .. normal.name)
end
if not data.raw.item[expensive.name] and not data.raw.fluid[expensive.name] then
sctm.debug("attempting to insert nonexistent ingredient " .. expensive.name)
end
return added
end
local function replaceingredient(ingredientstable, oldingredient, newingredient)
local added = false
for _i, ingredient in pairs(ingredientstable) do
if ingredient and ingredient[1] == oldingredient then
local insertingredient = newingredient
if (newingredient.amount == 0) then
newingredient.amount = ingredient[2]
end
ingredientstable[_i] = newingredient
added = true
break
elseif ingredient and ingredient.name and ingredient.name == oldingredient then
if (newingredient.amount == 0) then
newingredient.amount = ingredient.amount
end
ingredientstable[_i] = newingredient
added = true
break
end
end
return added
end
function sctm.recipe_ingredient_replace(recipename, oldingredientnormal, newingredientnormal, oldingredientexpensive, newingredientexpensive)
local replaced = false
local normal = newingredientnormal and table.deepcopy(newingredientnormal) or table.deepcopy(newingredientexpensive)
local expensive = newingredientexpensive and table.deepcopy(newingredientexpensive) or table.deepcopy(newingredientnormal)
local oldnormal = oldingredientnormal and table.deepcopy(oldingredientnormal) or table.deepcopy(oldingredientexpensive)
local oldexpensive = oldingredientexpensive and table.deepcopy(oldingredientexpensive) or table.deepcopy(oldingredientnormal)
if not normal.name then
local newnorm = {}
newnorm.name = normal
newnorm.type = data.raw.fluid[newnorm.name] and "fluid" or "item"
newnorm.amount = 0
normal = newnorm
end
if not expensive.name then
local newexp = {}
newexp.name = expensive
newexp.type = data.raw.fluid[newexp.name] and "fluid" or "item"
newexp.amount = 0
expensive = newexp
end
if data.raw.recipe[recipename] and (data.raw.item[normal.name] or data.raw.fluid[normal.name]) and (data.raw.item[expensive.name] or data.raw.fluid[expensive.name]) then
local recipe = data.raw.recipe[recipename]
local hasdiff = false
if recipe.normal then
if recipe.normal.ingredients then
replaced = replaceingredient(recipe.normal.ingredients, oldnormal, normal)
end
hasdiff = true
end
if recipe.expensive then
if recipe.expensive.ingredients then
replaced = replaceingredient(recipe.expensive.ingredients, oldexpensive, expensive) or replaced
end
hasdiff = true
end
if not hasdiff then
if recipe.ingredients then
replaced = replaceingredient(recipe.ingredients, oldnormal, normal)
end
end
end
if not data.raw.recipe[recipename] then
sctm.debug("attempting to update nonexistent recipe " .. recipename)
end
if not data.raw.item[normal.name] and not data.raw.fluid[normal.name] then
sctm.debug("attempting to insert nonexistent ingredient " .. normal.name)
end
if not data.raw.item[expensive.name] and not data.raw.fluid[expensive.name] then
sctm.debug("attempting to insert nonexistent ingredient " .. expensive.name)
end
return replaced
end
local function replaceresult(resultstable, oldresult, newresult)
local added = false
for _i, result in pairs(resultstable) do
if result and result[1] == oldresult then
local insertresult = newresult
if (newresult.amount == 0) then
newresult.amount = result[2]
end
resultstable[_i] = newresult
added = true
break
elseif result and result.name and result.name == oldresult then
if (newresult.amount == 0) then
newresult.amount = result.amount
end
resultstable[_i] = newresult
added = true
break
end
end
return added
end
function sctm.recipe_result_replace(recipename, oldresultnormal, newresultnormal, oldresultexpensive, newresultexpensive)
local replaced = false
local normal = newresultnormal and table.deepcopy(newresultnormal) or table.deepcopy(newresultexpensive)
local expensive = newresultexpensive and table.deepcopy(newresultexpensive) or table.deepcopy(newresultnormal)
local oldnormal = oldresultnormal and table.deepcopy(oldresultnormal) or table.deepcopy(oldresultexpensive)
local oldexpensive = oldresultexpensive and table.deepcopy(oldresultexpensive) or table.deepcopy(oldresultnormal)
if not normal.name then
local newnorm = {}
newnorm.name = normal
newnorm.type = data.raw.fluid[newnorm.name] and "fluid" or "item"
newnorm.amount = 0
normal = newnorm
end
if not expensive.name then
local newexp = {}
newexp.name = expensive
newexp.type = data.raw.fluid[newexp.name] and "fluid" or "item"
newexp.amount = 0
expensive = newexp
end
if data.raw.recipe[recipename] and (data.raw.item[normal.name] or data.raw.fluid[normal.name]) and (data.raw.item[expensive.name] or data.raw.fluid[expensive.name]) then
local recipe = data.raw.recipe[recipename]
local hasdiff = false
if recipe.normal then
if recipe.normal.results then
replaced = replaceresult(recipe.normal.results, oldnormal, normal)
end
hasdiff = true
end
if recipe.expensive then
if recipe.expensive.results then
replaced = replaceresult(recipe.expensive.results, oldexpensive, expensive) or replaced
end
hasdiff = true
end
if not hasdiff then
if recipe.results then
replaced = replaceresult(recipe.results, oldnormal, normal)
end
end
end
if not data.raw.recipe[recipename] then
sctm.debug("attempting to update nonexistent recipe " .. recipename)
end
if not data.raw.item[normal.name] and not data.raw.fluid[normal.name] then
sctm.debug("attempting to insert nonexistent result " .. normal.name)
end
if not data.raw.item[expensive.name] and not data.raw.fluid[expensive.name] then
sctm.debug("attempting to insert nonexistent result " .. expensive.name)
end
return replaced
end
--[[
local function find_recipe_unlock(ingredient, sciencelist)
end
function sctm.recipe_find_techs(recipe, science_packs)
local science_list = {}
local hasdif = false
science_list.expensive = {}
science_list.normal = {}
if (recipe.expensive) then
if recipe.expensive.ingredients then
for _, ingredient in pairs(recipe.expensive.ingredients) do
local tech = find_recipe_unlock(ingredient, sicence_packs)
if tech then
table.insert(science_list.expensive, tech)
end
end
end
hasdif = true
if not recipe.normal then
science_list.normal = table.deepcopy(science_list.expensive)
end
end
if (recipe.normal) then
if recipe.normal.ingredients then
for _, ingredient in pairs(recipe.normal.ingredients) do
local tech = find_recipe_unlock(ingredient, sicence_packs)
if tech then
table.insert(science_list.normal, tech)
end
end
end
hasdif = true
if not recipe.expensive then
science_list.expensive = table.deepcopy(science_list.normal)
end
end
if not hasdif then
if recipe.ingredients then
for _, ingredient in pairs(recipe.ingredients) do
local tech = find_recipe_unlock(ingredient, sicence_packs)
if tech then
table.insert(science_list.normal, tech)
end
end
end
science_list.expensive = table.deepcopy(science_list.normal)
end
return science_list;
end
]]--
function sctm.find_in_table(table, what)
for _, value in pairs(table) do
if _ == what or value == what or (value.name and value.name == what) then
return true
end
end
return false
end
function sctm.hide_recipe(recipe_name)
r = data.raw.recipe[recipe_name]
if r then
if not r.normal and not r.expensive then
r.hidden = true
else
if r.normal then
r.normal.hidden = true
end
if r.expensive then
r.expensive.hidden = true
end
end
end
end
function sctm.set_category(recipe_name, category)
if type(recipe_name) == "string" and type(category) == "string" then
local recipe = data.raw.recipe[recipe_name]
if recipe then
recipe.category = category
end
else
log(debug.traceback())
end
end