-
Notifications
You must be signed in to change notification settings - Fork 2
/
modmain.lua
916 lines (805 loc) · 32.7 KB
/
modmain.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
--Thanks rezecib!
local function CheckDlcEnabled(dlc)
-- if the constant doesn't even exist, then they can't have the DLC
if not GLOBAL.rawget(GLOBAL, dlc) then
return false
end
GLOBAL.assert(GLOBAL.rawget(GLOBAL, "IsDLCEnabled"), "Old version of game, please update (IsDLCEnabled function missing)")
return GLOBAL.IsDLCEnabled(GLOBAL[dlc])
end
local DLCROG = CheckDlcEnabled("REIGN_OF_GIANTS")
local CSW = CheckDlcEnabled("CAPY_DLC")
local HML = CheckDlcEnabled("PORKLAND_DLC")
local _G = GLOBAL
local DeBuG = GetModConfigData("debug")
local range = GetModConfigData("range")
local inv_first = GetModConfigData("is_inv_first")
local c = { r = 0, g = 0.3, b = 0 }
local Builder = _G.require 'components/builder'
local Highlight = _G.require 'components/highlight'
local IngredientUI = _G.require 'widgets/ingredientui'
local RecipePopup = _G.require 'widgets/recipepopup'
local TabGroup = _G.require 'widgets/tabgroup'
local CraftSlot = _G.require 'widgets/craftslot'
local highlit = {}-- tracking what is highlighted
local consumedChests = {}
local nearbyChests = {}
local validChests = {}
local TEASER_SCALE_TEXT = 1
local TEASER_SCALE_BTN = 1.5
local CONTROL_ACCEPT = _G.CONTROL_ACCEPT
local function isTable(t)
return type(t) == 'table'
end
local function debugPrint(...)
local arg = { ... }
if DeBuG then
for k, v in pairs(arg) do
print(v)
end
end
end
local function unhighlight(highlit)
while #highlit > 0 do
local v = table.remove(highlit)
if v and v.components.highlight then
v.components.highlight:UnHighlight()
end
end
end
local function highlight(insts, highlit)
for k, v in pairs(insts) do
if not v.components.highlight then
v:AddComponent('highlight')
end
if v.components.highlight then
v.components.highlight:Highlight(c.r, c.g, c.b)
table.insert(highlit, v)
end
end
end
-- given the list of instances, return the list of instances of chest
local function filterChest(inst)
local chest = {}
for k, v in pairs(inst) do
if (v and v.components.container and v.components.container.type) then
-- as cooker and backpack are considered as containers as well
-- regex to match ['chest', 'chester']
if string.find(v.components.container.type, 'chest') ~= nil then
table.insert(chest, v)
end
end
end
return chest
end
-- given the player, return the chests close to the player
local function getNearbyChest(player, dist)
if dist == nil then
dist = range
end
if not player then
return {}
end
local x, y, z = player.Transform:GetWorldPosition()
local inst = _G.TheSim:FindEntities(x, y, z, dist, {}, { 'NOBLOCK', 'player', 'FX' }) or {}
return filterChest(inst)
end
-- return: contains (T/F) total (num) qualifyChests (list of chest contains the item)
local function findFromChests(chests, item)
if not (chests and item) then
return false, 0, {}
end
local qualifyChests = {}
local total = 0
local contains = false
for k, v in pairs(chests) do
local found, n = v.components.container:Has(item, 1)
if found then
contains = true
total = total + n
table.insert(qualifyChests, v);
end
end
return contains, total, qualifyChests
end
local function findFromNearbyChests(player, item)
if not (player and item) then
return false, 0, {}
end
local chests = getNearbyChest(player)
return findFromChests(chests, item)
end
-- return: whether it is enough to fullfill the amt requirement, and the amt not fulfilled.
local function removeFromNearbyChests(player, item, amt)
if not (player and item and amt ~= nil) then
debugPrint('removeFromNearbyChests: player | item | amt missing!')
return false, amt
end
debugPrint('removeFromNearbyChests', player, item, amt)
consumedChests = {}-- clear consumed chests
local chests = getNearbyChest(player, range + 3)-- extend the range a little bit, avoid error caused by slight player movement
local numItemsFound = 0
for k, v in pairs(chests) do
local container = v.components.container
found, num_found = container:Has(item, 1)
if found then
numItemsFound = numItemsFound + num_found
table.insert(consumedChests, v)
if (amt > num_found) then
-- not enough
container:ConsumeByName(item, num_found)
amt = amt - num_found
else
container:ConsumeByName(item, amt)
amt = 0
break
end
end
end
debugPrint('Found ' .. numItemsFound .. ' ' .. item .. ' from ' .. #consumedChests .. ' chests')
if amt == 0 then
return true, 0
else
return false, amt
end
end
local function playerConsumeByName(player, item, amt)
if not (player and item and amt) then
return false
end
local inventory = player.components.inventory
if inv_first then
found_inv, num_in_inv = inventory:Has(item, 1)
if amt <= num_in_inv then
-- there are more resources available in inv then needed
inventory:ConsumeByName(item, amt)
return true
end
inventory:ConsumeByName(item, num_in_inv)
amt = amt - num_in_inv
debugPrint('Found ' .. num_in_inv .. ' in inventory, take ' .. amt .. 'from chests')
removeFromNearbyChests(player, item, amt)
return true
else
done, remain = removeFromNearbyChests(player, item, amt)
if not done then
inventory:ConsumeByName(item, remain)
end
return true
end
end
local function playerGetByName(player, item, amt)
debugPrint('playerGetByName ' .. item)
if not (player and item and amt and amt ~= 0) then
debugPrint('playerGetByName: player | item | amt missing!')
return {}
end
local items = {}
local function addToItems(another_item)
for k, v in pairs(another_item) do
if items[k] == nil then
items[k] = v
else
items[k] = items[k] + v
end
end
end
local function tryGetFromContainer(volume)
found, num = volume:Has(item, 1)
if found then
if num >= amt then
-- there is more than necessary
addToItems(volume:GetItemByName(item, amt))
amt = 0
return true
else
-- it's not enough
addToItems(volume:GetItemByName(item, num))
amt = amt - num
return false
end
end
return false
end
local inventory = player.components.inventory
local chests = getNearbyChest(player)
if inv_first then
-- get ingredients from inventory first
if tryGetFromContainer(inventory) then
return items
end
for k, v in pairs(chests) do
local container = v.components.container
if tryGetFromContainer(container) then
return items
end
end
else
-- get ingredients from chests first
for k, v in pairs(chests) do
local container = v.components.container
if tryGetFromContainer(container) then
return items
end
end
tryGetFromContainer(inventory)
return items
end
return items
end
-- detect if the number of chests around the player changes.
-- If true, push event stacksizechange
-- TODO: find a better event to push than "stacksizechange"
local _oldCmpChestsNum = 0
local _newCmpChestsNum = 0
local function compareValidChests(player)
_newCmpChestsNum = table.getn(getNearbyChest(player))
if (_oldCmpChestsNum ~= _newCmpChestsNum) then
_oldCmpChestsNum = _newCmpChestsNum
debugPrint('Chest number changed!')
player:PushEvent("stacksizechange")
end
end
-- override original function
-- Support DS, RoG. SW not tested
-- to unhighlight chest when tabgroup are deselected
function TabGroup:DeselectAll(...)
for k, v in ipairs(self.tabs) do
v:Deselect()
end
unhighlight(highlit)
end
----------------------------------------------------------
---------------Override Builder functions (DS, RoG)-------
-- to test if canbuild with the material from chest
function Builder:CanBuild(recname)
if self.freebuildmode then
return true
end
local player = self.inst
local chests = getNearbyChest(player)
local recipe = _G.GetRecipe(recname)
if recipe then
for ik, iv in pairs(recipe.ingredients) do
local amt = math.max(1, _G.RoundUp(iv.amount * self.ingredientmod))
found, num_found = findFromChests(chests, iv.type)
has, num_hold = player.components.inventory:Has(iv.type, amt)
if (amt > num_found + num_hold) then
return false
end
end
return true
end
return false
end
-- to keep updating the number of chests as the player move around
function Builder:OnUpdate(dt)
compareValidChests(self.inst)
self:EvaluateTechTrees()
if self.EvaluateAutoFixers ~= nil then
self:EvaluateAutoFixers()
end
end
-- This function is for RoG, base game doesn't have this function'
function Builder:GetIngredients(recname)
debugPrint('Custom Builder:GetIngredients: ' .. recname)
local recipe = _G.GetRecipe(recname)
if recipe then
local ingredients = {}
for k, v in pairs(recipe.ingredients) do
local amt = math.max(1, _G.RoundUp(v.amount * self.ingredientmod))
-- local items = self.inst.components.inventory:GetItemByName(v.type, amt)
local items = playerGetByName(self.inst, v.type, amt)
ingredients[v.type] = items
end
return ingredients
end
end
-- to take ingredients from both inv and chests
function Builder:RemoveIngredients(recname_or_ingre)
if not isTable(recname_or_ingre) then
-- param is a recname, which is base game
local recipe = _G.GetRecipe(recname_or_ingre)
self.inst:PushEvent("consumeingredients", { recipe = recipe })
if recipe then
for k, v in pairs(recipe.ingredients) do
local amt = math.max(1, _G.RoundUp(v.amount * self.ingredientmod))
playerConsumeByName(self.inst, v.type, amt)
end
end
else
-- this is RoG version of removeIngredients
-- RoG uses another function: getingredients to load all ingredients, so this part
-- does not require a lot modification
debugPrint('RoG Ver Builder:RemoveIngredients')
for item, ents in pairs(recname_or_ingre) do
for k, v in pairs(ents) do
for i = 1, v do
-- TODO: change this line
-- Now it can successfully deduct the number of items, but if the item is in
-- the chest, it will not pushevent: "loseitem". Although I didn't see a major
-- effect on that, but better to add it back
self.inst.components.inventory:RemoveItem(k, false):Remove()
end
end
end
self.inst:PushEvent("consumeingredients")
end
end
----------------------------------------------------------
---------------End Override Builder functions-------------
if HML then
function RecipePopup:Refresh()
local recipe = self.recipe
local owner = self.owner
if not owner then
return false
end
local builder = owner.components.builder
local knows = owner.components.builder:KnowsRecipe(recipe.name) or recipe.nounlock
local buffered = owner.components.builder:IsBuildBuffered(recipe.name)
local can_build = owner.components.builder:CanBuild(recipe.name) or buffered
local tech_level = owner.components.builder.accessible_tech_trees
local should_hint = not knows and _G.ShouldHintRecipe(recipe.level, tech_level) and not _G.CanPrototypeRecipe(recipe.level, tech_level)
local equippedBody = owner.components.inventory:GetEquippedItem(_G.EQUIPSLOTS.BODY)
local showamulet = equippedBody and equippedBody.prefab == "greenamulet"
local controller_id = _G.TheInput:GetControllerID()
if should_hint then
self.recipecost:Hide()
self.button:Hide()
local hint_text = {
["SCIENCEMACHINE"] = _G.STRINGS.UI.CRAFTING.NEEDSCIENCEMACHINE,
["ALCHEMYMACHINE"] = _G.STRINGS.UI.CRAFTING.NEEDALCHEMYENGINE,
["SHADOWMANIPULATOR"] = _G.STRINGS.UI.CRAFTING.NEEDSHADOWMANIPULATOR,
["PRESTIHATITATOR"] = _G.STRINGS.UI.CRAFTING.NEEDPRESTIHATITATOR,
["HOGUSPORKUSATOR"] = _G.STRINGS.UI.CRAFTING.NEEDHOUGSPORKUSATOR,
["CANTRESEARCH"] = _G.STRINGS.UI.CRAFTING.CANTRESEARCH,
["PIRATIHATITATOR"] = _G.STRINGS.UI.CRAFTING.NEEDPIRATIHATITATOR,
["SEALAB"] = _G.STRINGS.UI.CRAFTING.NEEDSEALAB, }
--[[
if SaveGameIndex:IsModeShipwrecked() or SaveGameIndex:IsModePorkland() then
hint_text["PRESTIHATITATOR"] = STRINGS.UI.CRAFTING.NEEDPIRATIHATITATOR
hint_text["SEALAB"] = STRINGS.UI.CRAFTING.NEEDSEALAB
end
]]
local str = hint_text[_G.GetHintTextForRecipe(recipe)] or "Text not found."
self.teaser:SetScale(TEASER_SCALE_TEXT)
self.teaser:SetString(str)
self.teaser:Show()
showamulet = false
elseif knows then
self.teaser:Hide()
self.recipecost:Hide()
if _G.TheInput:ControllerAttached() then
self.button:Hide()
self.teaser:Show()
if can_build then
self.teaser:SetScale(TEASER_SCALE_BTN)
self.teaser:SetString(_G.TheInput:GetLocalizedControl(controller_id, CONTROL_ACCEPT) .. " " .. (buffered and _G.STRINGS.UI.CRAFTING.PLACE or _G.STRINGS.UI.CRAFTING.BUILD))
else
self.teaser:SetScale(TEASER_SCALE_TEXT)
self.teaser:SetString(_G.STRINGS.UI.CRAFTING.NEEDSTUFF)
end
else
self.button:Show()
self.button:SetPosition(320, -105, 0)
self.button:SetScale(1, 1, 1)
self.button:SetText(buffered and _G.STRINGS.UI.CRAFTING.PLACE or _G.STRINGS.UI.CRAFTING.BUILD)
if can_build then
self.button:Enable()
else
self.button:Disable()
end
end
else
self.teaser:Hide()
self.recipecost:Hide()
if _G.TheInput:ControllerAttached() then
self.button:Hide()
self.teaser:Show()
self.teaser:SetColour(1, 1, 1, 1)
if can_build then
self.teaser:SetScale(TEASER_SCALE_BTN)
self.teaser:SetString(_G.TheInput:GetLocalizedControl(controller_id, CONTROL_ACCEPT) .. " " .. _G.STRINGS.UI.CRAFTING.PROTOTYPE)
else
self.teaser:SetScale(TEASER_SCALE_TEXT)
self.teaser:SetString(_G.STRINGS.UI.CRAFTING.NEEDSTUFF)
end
else
self.button.image_normal = "button.tex"
self.button.image:SetTexture(_G.UI_ATLAS, self.button.image_normal)
self.button:Show()
self.button:SetPosition(320, -105, 0)
self.button:SetScale(1, 1, 1)
self.button:SetText(_G.STRINGS.UI.CRAFTING.PROTOTYPE)
if can_build then
self.button:Enable()
else
self.button:Disable()
end
end
end
if not showamulet then
self.amulet:Hide()
else
self.amulet:Show()
end
self.name:SetString(_G.STRINGS.NAMES[string.upper(self.recipe.name)])
self.desc:SetString(_G.STRINGS.RECIPE_DESC[string.upper(self.recipe.name)])
for k, v in pairs(self.ing) do
v:Kill()
end
self.ing = {}
local center = 330
local num = 0
for k, v in pairs(recipe.ingredients) do
num = num + 1
end
local w = 64
local div = 10
local offset = center
if num > 1 then
offset = offset - (w / 2 + div) * (num - 1)
end
local total, need, has_chest, num_found_chest, has_inv, num_found_inv
validChests = {}
for k, v in pairs(recipe.ingredients) do
-- calculations
local validChestsOfIngredient = {}
need = _G.RoundUp(v.amount * owner.components.builder.ingredientmod)
has_inv, num_found_inv = owner.components.inventory:Has(v.type, need)
has_chest, num_found_chest, validChestsOfIngredient = findFromNearbyChests(owner, v.type)
total = num_found_chest + num_found_inv
if v.type == "oinc" then
num_found = owner.components.shopper:GetMoney(owner.components.inventory)
has = num_found >= v.amount
end
local item_img = v.type
if _G.SaveGameIndex:IsModeShipwrecked() and _G.SW_ICONS[item_img] ~= nil then
item_img = _G.SW_ICONS[item_img]
end
if _G.SaveGameIndex:IsModePorkland() and _G.PORK_ICONS[item_img] ~= nil then
item_img = _G.PORK_ICONS[item_img]
end
local imageName = item_img .. ".tex"
local ingredientUI = IngredientUI(
v:GetAtlas(imageName),
imageName,
need,
total,
total >= need,
_G.STRINGS.NAMES[string.upper(v.type)],
owner
)
-- ingredientUI.quant:SetString(string.format("Inv:%d/%d\nAll:%d/%d", num_found_inv, need, total, need))
ingredientUI.quant:SetString(string.format("All:%d/%d\n(Inv:%d)", total, need, num_found_inv))
local ing = self.contents:AddChild(ingredientUI)
ing:SetPosition(_G.Vector3(offset, 80, 0))
offset = offset + (w + div)
self.ing[k] = ing
end
for k, v in pairs(recipe.character_ingredients) do
local has, amount = builder:HasCharacterIngredient(v)
print("amount", amount)
local imageName = v.type .. ".tex"
local ing = self.contents:AddChild(IngredientUI(v:GetAtlas(imageName), imageName, need, total, total >= need, _G.STRINGS.NAMES[string.upper(v.type)], owner, v.type))
ing:SetPosition(_G.Vector3(offset, self.skins_spinner ~= nil and 110 or 80, 0))
offset = offset + (w + div)
self.ing[k] = ing
end
highlight(validChests, highlit)
end
end
if DLCROG then
function RecipePopup:Refresh()
-- unhighlight(highlit)
local recipe = self.recipe
local owner = self.owner
if not owner then
return false
end
local knows = owner.components.builder:KnowsRecipe(recipe.name)
local buffered = owner.components.builder:IsBuildBuffered(recipe.name)
local can_build = owner.components.builder:CanBuild(recipe.name) or buffered
local tech_level = owner.components.builder.accessible_tech_trees
local should_hint = not knows and _G.ShouldHintRecipe(recipe.level, tech_level) and
not _G.CanPrototypeRecipe(recipe.level, tech_level)
local equippedBody = owner.components.inventory:GetEquippedItem(_G.EQUIPSLOTS.BODY)
local showamulet = equippedBody and equippedBody.prefab == "greenamulet"
local controller_id = _G.TheInput:GetControllerID()
if should_hint then
self.recipecost:Hide()
self.button:Hide()
local hint_text = {
["SCIENCEMACHINE"] = _G.STRINGS.UI.CRAFTING.NEEDSCIENCEMACHINE,
["ALCHEMYMACHINE"] = _G.STRINGS.UI.CRAFTING.NEEDALCHEMYENGINE,
["SHADOWMANIPULATOR"] = _G.STRINGS.UI.CRAFTING.NEEDSHADOWMANIPULATOR,
["PRESTIHATITATOR"] = _G.STRINGS.UI.CRAFTING.NEEDPRESTIHATITATOR,
["CANTRESEARCH"] = _G.STRINGS.UI.CRAFTING.CANTRESEARCH,
["ANCIENTALTAR_HIGH"] = _G.STRINGS.UI.CRAFTING.NEEDSANCIENT_FOUR,
["SEALAB"] = _G.STRINGS.UI.CRAFTING.NEEDALCHEMYENGINE
}
if _G.SaveGameIndex:IsModeShipwrecked() then
hint_text["PRESTIHATITATOR"] = _G.STRINGS.UI.CRAFTING.NEEDPIRATIHATITATOR
end
local str = hint_text[_G.GetHintTextForRecipe(recipe)] or "Text not found."
self.teaser:SetScale(TEASER_SCALE_TEXT)
self.teaser:SetString(str)
self.teaser:Show()
showamulet = false
elseif knows then
self.teaser:Hide()
self.recipecost:Hide()
if _G.TheInput:ControllerAttached() then
self.button:Hide()
self.teaser:Show()
if can_build then
self.teaser:SetScale(TEASER_SCALE_BTN)
self.teaser:SetString(
_G.TheInput:GetLocalizedControl(controller_id, CONTROL_ACCEPT) ..
" " .. (buffered and _G.STRINGS.UI.CRAFTING.PLACE or _G.STRINGS.UI.CRAFTING.BUILD)
)
else
self.teaser:SetScale(TEASER_SCALE_TEXT)
self.teaser:SetString(_G.STRINGS.UI.CRAFTING.NEEDSTUFF)
end
else
self.button:Show()
self.button:SetPosition(320, -105, 0)
self.button:SetScale(1, 1, 1)
self.button:SetText(buffered and _G.STRINGS.UI.CRAFTING.PLACE or _G.STRINGS.UI.CRAFTING.BUILD)
if can_build then
self.button:Enable()
else
self.button:Disable()
end
end
else
self.teaser:Hide()
self.recipecost:Hide()
if _G.TheInput:ControllerAttached() then
self.button:Hide()
self.teaser:Show()
self.teaser:SetColour(1, 1, 1, 1)
if can_build then
self.teaser:SetScale(TEASER_SCALE_BTN)
self.teaser:SetString(
_G.TheInput:GetLocalizedControl(controller_id, CONTROL_ACCEPT) ..
" " .. _G.STRINGS.UI.CRAFTING.PROTOTYPE
)
else
self.teaser:SetScale(TEASER_SCALE_TEXT)
self.teaser:SetString(_G.STRINGS.UI.CRAFTING.NEEDSTUFF)
end
else
self.button.image_normal = "button.tex"
self.button.image:SetTexture(_G.UI_ATLAS, self.button.image_normal)
self.button:Show()
self.button:SetPosition(320, -105, 0)
self.button:SetScale(1, 1, 1)
self.button:SetText(_G.STRINGS.UI.CRAFTING.PROTOTYPE)
if can_build then
self.button:Enable()
else
self.button:Disable()
end
end
end
if not showamulet then
self.amulet:Hide()
else
self.amulet:Show()
end
self.name:SetString(_G.STRINGS.NAMES[string.upper(self.recipe.name)])
self.desc:SetString(_G.STRINGS.RECIPE_DESC[string.upper(self.recipe.name)])
for k, v in pairs(self.ing) do
v:Kill()
end
self.ing = {}
local center = 330
local num = 0
for k, v in pairs(recipe.ingredients) do
num = num + 1
end
local w = 64
local div = 10
local offset = center
if num > 1 then
offset = offset - (w / 2 + div) * (num - 1)
end
local total, need, has_chest, num_found_chest, has_inv, num_found_inv
validChests = {}
for k, v in pairs(recipe.ingredients) do
-- calculations
local validChestsOfIngredient = {}
need = _G.RoundUp(v.amount * owner.components.builder.ingredientmod)
has_inv, num_found_inv = owner.components.inventory:Has(v.type, need)
has_chest, num_found_chest, validChestsOfIngredient = findFromNearbyChests(owner, v.type)
total = num_found_chest + num_found_inv
-- merge tables
for k1, v1 in pairs(validChestsOfIngredient) do
table.insert(validChests, v1)
end
local item_img = v.type
if _G.SaveGameIndex:IsModeShipwrecked() and _G.SW_ICONS[item_img] ~= nil then
item_img = _G.SW_ICONS[item_img]
end
local ingredientUI = IngredientUI(
v.atlas,
item_img .. ".tex",
need,
total,
total >= need,
_G.STRINGS.NAMES[string.upper(v.type)],
owner
)
-- ingredientUI.quant:SetString(string.format("Inv:%d/%d\nAll:%d/%d", num_found_inv, need, total, need))
ingredientUI.quant:SetString(string.format("All:%d/%d\n(Inv:%d)", total, need, num_found_inv))
local ing = self.contents:AddChild(ingredientUI)
ing:SetPosition(_G.Vector3(offset, 80, 0))
offset = offset + (w + div)
self.ing[k] = ing
end
highlight(validChests, highlit)
end
end
if CSW then
function RecipePopup:Refresh()
-- unhighlight(highlit)
local recipe = self.recipe
local owner = self.owner
if not owner then
return false
end
local knows = owner.components.builder:KnowsRecipe(recipe.name)
local buffered = owner.components.builder:IsBuildBuffered(recipe.name)
local can_build = owner.components.builder:CanBuild(recipe.name) or buffered
local tech_level = owner.components.builder.accessible_tech_trees
local should_hint = not knows and _G.ShouldHintRecipe(recipe.level, tech_level) and
not _G.CanPrototypeRecipe(recipe.level, tech_level)
local equippedBody = owner.components.inventory:GetEquippedItem(_G.EQUIPSLOTS.BODY)
local showamulet = equippedBody and equippedBody.prefab == "greenamulet"
local controller_id = _G.TheInput:GetControllerID()
if should_hint then
self.recipecost:Hide()
self.button:Hide()
local hint_text = {
["SCIENCEMACHINE"] = _G.STRINGS.UI.CRAFTING.NEEDSCIENCEMACHINE,
["ALCHEMYMACHINE"] = _G.STRINGS.UI.CRAFTING.NEEDALCHEMYENGINE,
["SHADOWMANIPULATOR"] = _G.STRINGS.UI.CRAFTING.NEEDSHADOWMANIPULATOR,
["PRESTIHATITATOR"] = _G.STRINGS.UI.CRAFTING.NEEDPRESTIHATITATOR,
["CANTRESEARCH"] = _G.STRINGS.UI.CRAFTING.CANTRESEARCH,
["ANCIENTALTAR_HIGH"] = _G.STRINGS.UI.CRAFTING.NEEDSANCIENT_FOUR,
["SEALAB"] = _G.STRINGS.UI.CRAFTING.NEEDALCHEMYENGINE
}
if _G.SaveGameIndex:IsModeShipwrecked() then
hint_text["PRESTIHATITATOR"] = _G.STRINGS.UI.CRAFTING.NEEDPIRATIHATITATOR
end
local str = hint_text[_G.GetHintTextForRecipe(recipe)] or "Text not found."
self.teaser:SetScale(TEASER_SCALE_TEXT)
self.teaser:SetString(str)
self.teaser:Show()
showamulet = false
elseif knows then
self.teaser:Hide()
self.recipecost:Hide()
if _G.TheInput:ControllerAttached() then
self.button:Hide()
self.teaser:Show()
if can_build then
self.teaser:SetScale(TEASER_SCALE_BTN)
self.teaser:SetString(
_G.TheInput:GetLocalizedControl(controller_id, CONTROL_ACCEPT) ..
" " .. (buffered and _G.STRINGS.UI.CRAFTING.PLACE or _G.STRINGS.UI.CRAFTING.BUILD)
)
else
self.teaser:SetScale(TEASER_SCALE_TEXT)
self.teaser:SetString(_G.STRINGS.UI.CRAFTING.NEEDSTUFF)
end
else
self.button:Show()
self.button:SetPosition(320, -105, 0)
self.button:SetScale(1, 1, 1)
self.button:SetText(buffered and _G.STRINGS.UI.CRAFTING.PLACE or _G.STRINGS.UI.CRAFTING.BUILD)
if can_build then
self.button:Enable()
else
self.button:Disable()
end
end
else
self.teaser:Hide()
self.recipecost:Hide()
if _G.TheInput:ControllerAttached() then
self.button:Hide()
self.teaser:Show()
self.teaser:SetColour(1, 1, 1, 1)
if can_build then
self.teaser:SetScale(TEASER_SCALE_BTN)
self.teaser:SetString(
_G.TheInput:GetLocalizedControl(controller_id, CONTROL_ACCEPT) ..
" " .. _G.STRINGS.UI.CRAFTING.PROTOTYPE
)
else
self.teaser:SetScale(TEASER_SCALE_TEXT)
self.teaser:SetString(_G.STRINGS.UI.CRAFTING.NEEDSTUFF)
end
else
self.button.image_normal = "button.tex"
self.button.image:SetTexture(_G.UI_ATLAS, self.button.image_normal)
self.button:Show()
self.button:SetPosition(320, -105, 0)
self.button:SetScale(1, 1, 1)
self.button:SetText(_G.STRINGS.UI.CRAFTING.PROTOTYPE)
if can_build then
self.button:Enable()
else
self.button:Disable()
end
end
end
if not showamulet then
self.amulet:Hide()
else
self.amulet:Show()
end
self.name:SetString(_G.STRINGS.NAMES[string.upper(self.recipe.name)])
self.desc:SetString(_G.STRINGS.RECIPE_DESC[string.upper(self.recipe.name)])
for k, v in pairs(self.ing) do
v:Kill()
end
self.ing = {}
local center = 330
local num = 0
for k, v in pairs(recipe.ingredients) do
num = num + 1
end
local w = 64
local div = 10
local offset = center
if num > 1 then
offset = offset - (w / 2 + div) * (num - 1)
end
local total, need, has_chest, num_found_chest, has_inv, num_found_inv
validChests = {}
for k, v in pairs(recipe.ingredients) do
-- calculations
local validChestsOfIngredient = {}
need = _G.RoundUp(v.amount * owner.components.builder.ingredientmod)
has_inv, num_found_inv = owner.components.inventory:Has(v.type, need)
has_chest, num_found_chest, validChestsOfIngredient = findFromNearbyChests(owner, v.type)
total = num_found_chest + num_found_inv
-- merge tables
for k1, v1 in pairs(validChestsOfIngredient) do
table.insert(validChests, v1)
end
local item_img = v.type
if _G.SaveGameIndex:IsModeShipwrecked() and _G.SW_ICONS[item_img] ~= nil then
item_img = _G.SW_ICONS[item_img]
end
local ingredientUI = IngredientUI(
v.atlas,
item_img .. ".tex",
need,
total,
total >= need,
_G.STRINGS.NAMES[string.upper(v.type)],
owner
)
-- ingredientUI.quant:SetString(string.format("Inv:%d/%d\nAll:%d/%d", num_found_inv, need, total, need))
ingredientUI.quant:SetString(string.format("All:%d/%d\n(Inv:%d)", total, need, num_found_inv))
local ing = self.contents:AddChild(ingredientUI)
ing:SetPosition(_G.Vector3(offset, 80, 0))
offset = offset + (w + div)
self.ing[k] = ing
end
highlight(validChests, highlit)
end
end
function CraftSlot:OnLoseFocus()
CraftSlot._base.OnLoseFocus(self)
unhighlight(highlit)
self:Close()
end
-- Fix for Hamlet by Vlad Undying <3
-- Thanks for the fix! -- Huan Wang