-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcomponentutil.lua
More file actions
1907 lines (1681 loc) · 72.3 KB
/
componentutil.lua
File metadata and controls
1907 lines (1681 loc) · 72.3 KB
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
require("components/raindome") --load some global functions defined for this component
require("components/temperatureoverrider") --load some global functions defined for this component
local GroundTiles = require("worldtiledefs")
--require_health being true means an entity is considered "dead" if it lacks the health replica.
function IsEntityDead(inst, require_health)
local health = inst.replica.health
if health == nil then
return require_health == true
end
return health:IsDead()
end
function IsEntityDeadOrGhost(inst, require_health)
if inst:HasTag("playerghost") then
return true
end
return IsEntityDead(inst, require_health)
end
function IsEntityElectricImmune(inst)
return inst:HasTag("electricdamageimmune") or (inst.components.inventory and inst.components.inventory:IsInsulated())
end
function GetStackSize(inst)
local stackable = inst.replica.stackable
return stackable and stackable:StackSize() or 1
end
function HandleDugGround(dug_ground, x, y, z)
local spawnturf = GroundTiles.turf[dug_ground] or nil
if spawnturf ~= nil then
local loot = SpawnPrefab("turf_"..spawnturf.name)
if loot.components.inventoryitem ~= nil then
loot.components.inventoryitem:InheritWorldWetnessAtXZ(x, z)
end
loot.Transform:SetPosition(x, y, z)
if loot.Physics ~= nil then
local angle = math.random() * TWOPI
loot.Physics:SetVel(2 * math.cos(angle), 10, 2 * math.sin(angle))
end
else
SpawnPrefab("sinkhole_spawn_fx_"..tostring(math.random(3))).Transform:SetPosition(x, y, z)
end
end
local VIRTUALOCEAN_HASTAGS = {"virtualocean"}
local VIRTUALOCEAN_CANTTAGS = {"INLIMBO"}
function FindVirtualOceanEntity(x, y, z, r)
local ents = TheSim:FindEntities(x, y, z, r or MAX_PHYSICS_RADIUS, VIRTUALOCEAN_HASTAGS, VIRTUALOCEAN_CANTTAGS)
for _, ent in ipairs(ents) do
if ent.Physics ~= nil then
local radius = ent.Physics:GetRadius()
local ex, ey, ez = ent.Transform:GetWorldPosition()
local dx, dz = ex - x, ez - z
if dx * dx + dz * dz <= radius * radius then
return ent
end
end
end
return nil
end
-- Use in your boats ondeploy
local ITEM_LAUNCHSPEED = 2
local ITEM_LAUNCHMULT = 1
local ITEM_STARTHEIGHT = 0.1
local ITEM_VERTICALSPEED = 0.1
local TIME_FOR_BOAT = .6
local IGNORE_WALKABLE_PLATFORM_TAGS = { "ignorewalkableplatforms", "activeprojectile", "flying", "FX", "DECOR", "INLIMBO", "herd", "walkableplatform" }
function PushAwayItemsOnBoatPlace(inst)
local function launch_with_delay(item)
Launch2(item, inst, ITEM_LAUNCHSPEED, ITEM_LAUNCHMULT, ITEM_STARTHEIGHT, inst.components.walkableplatform.platform_radius + item:GetPhysicsRadius(0.25), ITEM_VERTICALSPEED)
if item.components.inventoryitem then
item.components.inventoryitem:SetLanded(false, true)
end
end
local pos = inst:GetPosition()
local platform_radius_sq = inst.components.walkableplatform.platform_radius * inst.components.walkableplatform.platform_radius
for i, v in ipairs(TheSim:FindEntities(pos.x, pos.y, pos.z, inst.components.walkableplatform.platform_radius, nil, IGNORE_WALKABLE_PLATFORM_TAGS)) do
if v ~= inst and v.entity:GetParent() == nil and v.components.amphibiouscreature == nil and v.components.drownable == nil then
local time = Remap(v:GetDistanceSqToPoint(pos),
0, platform_radius_sq,
0, TIME_FOR_BOAT)
if v.special_item_boat_push_case then --Mods.
v.special_item_boat_push_case(v, inst, time)
elseif v:HasTag("bird") then
v:PushEvent("flyaway")
else
v:DoTaskInTime(time, launch_with_delay)
end
end
end
end
--------------------------------------------------------------------------
--Tags useful for testing against combat targets that you can hit,
--but aren't really considered "alive".
-- Lifedrain (Batbat, mauler) uses this list
NON_LIFEFORM_TARGET_TAGS =
{
"structure",
"wall",
"balloon",
"groundspike",
"smashable",
"veggie", --stuff like lureplants... not considered life?
"deck_of_cards",
}
--Shadows and Gestalts don't have souls.
--NOTE: -Adding "soulless" tag to entities is preferred over expanding this list.
-- -Gestalts should already be using "soulless" tag.
SOULLESS_TARGET_TAGS = ConcatArrays(
{
"soulless",
"chess",
"shadow",
"shadowcreature",
"shadowminion",
"shadowchesspiece",
},
NON_LIFEFORM_TARGET_TAGS
)
--------------------------------------------------------------------------
local IGNORE_DROWNING_ONREMOVE_TAGS = {"ignorewalkableplatforms", "ignorewalkableplatformdrowning", "activeprojectile", "flying", "FX", "DECOR", "INLIMBO"}
function TempTile_HandleTileChange_Ocean(x, y, z)
local _world = TheWorld
local _map = _world.Map
-- If we're swapping to an ocean tile, do like a broken boat would do and deal with everything in our tile bounds
-- Behaviour pulled from walkableplatform's onremove/DestroyObjectsOnPlatform response.
local tile_radius_plus_overhang = ((TILE_SCALE / 2) + 1.0) * 1.4142
local entities_near_tile = TheSim:FindEntities(x, 0, z, tile_radius_plus_overhang, nil, IGNORE_DROWNING_ONREMOVE_TAGS)
local shore_point = nil
for _, ent in ipairs(entities_near_tile) do
local has_drownable = (ent.components.drownable ~= nil)
if has_drownable and shore_point == nil then
shore_point = Vector3(FindRandomPointOnShoreFromOcean(x, y, z))
end
ent:PushEvent("onsink", {boat = nil, shore_pt = shore_point})
-- We're testing the overhang, so we need to verify that anything we find isn't
-- still on some adjacent dock or land tile after we remove ourself.
if ent:IsValid() and not has_drownable and ent.entity:GetParent() == nil
and ent.components.amphibiouscreature == nil
and not _map:IsVisualGroundAtPoint(ent.Transform:GetWorldPosition()) then
if ent.components.inventoryitem ~= nil then
ent.components.inventoryitem:SetLanded(false, true)
else
DestroyEntity(ent, _world, true, true)
end
end
end
end
function TempTile_HandleTileChange_Ocean_Warn(x, y, z)
local _map = TheWorld.Map
-- Behaviour pulled from walkableplatform's onremove/DestroyObjectsOnPlatform response.
local tile_radius_plus_overhang = ((TILE_SCALE / 2) + 1.0) * 1.4142
local entities_near_tile = TheSim:FindEntities(x, 0, z, tile_radius_plus_overhang, nil, IGNORE_DROWNING_ONREMOVE_TAGS)
for _, ent in ipairs(entities_near_tile) do
-- Only push these events on prefabs that are actually standing on a temp tile.
-- We use the VisualGround test because we're accounting for tile overhang.
if _map:IsVisualGroundAtPoint(ent.Transform:GetWorldPosition()) then
ent:PushEvent("abandon_ship")
if ent:HasTag("player") then
ent:PushEvent("onpresink")
end
end
end
end
local IGNORE_FALLING_ONREMOVE_TAGS = {"ignorewalkableplatforms", "ignorewalkableplatformdrowning", "activeprojectile", "flying", "FX", "DECOR", "INLIMBO"}
function TempTile_HandleTileChange_Void(x, y, z)
local _world = TheWorld
local _map = _world.Map
local tile_radius_plus_overhang = ((TILE_SCALE / 2) + 1.0) * 1.4142
local entities_near_tile = TheSim:FindEntities(x, 0, z, tile_radius_plus_overhang, nil, IGNORE_FALLING_ONREMOVE_TAGS)
local teleport_point = nil
for _, ent in ipairs(entities_near_tile) do
local drownable = ent.components.drownable
if drownable and teleport_point == nil then
teleport_point = Vector3(FindRandomPointOnShoreFromOcean(x, y, z))
end
ent:PushEvent("onfallinvoid", {teleport_pt = teleport_point})
-- We're testing the overhang, so we need to verify that anything we find isn't
-- still on some adjacent dock or land tile after we remove ourself.
local canfallinvoid = drownable and ent.sg and ent.sg.sg.states.abyss_fall ~= nil -- NOTES(JBK): If things do not support the abyss_fall state we should kill it instead.
if ent:IsValid() and not canfallinvoid and ent.entity:GetParent() == nil
and not _map:IsVisualGroundAtPoint(ent.Transform:GetWorldPosition()) then
if ent.components.inventoryitem ~= nil then
ent.components.inventoryitem:SetLanded(false, true)
else
DestroyEntity(ent, _world, true, true)
end
end
end
end
function TempTile_HandleTileChange_Void_Warn(x, y, z)
local _map = TheWorld.Map
local tile_radius_plus_overhang = ((TILE_SCALE / 2) + 1.0) * 1.4142
local entities_near_tile = TheSim:FindEntities(x, 0, z, tile_radius_plus_overhang, nil, IGNORE_FALLING_ONREMOVE_TAGS)
for _, ent in ipairs(entities_near_tile) do
-- Only push these events on prefabs that are actually standing on a temp tile.
-- We use the VisualGround test because we're accounting for tile overhang.
if _map:IsVisualGroundAtPoint(ent.Transform:GetWorldPosition()) then
ent:PushEvent("onprefallinvoid")
end
end
end
function TempTile_HandleTileChange(x, y, z, tile)
if TileGroupManager:IsOceanTile(tile) then
TempTile_HandleTileChange_Ocean(x, y, z)
elseif TileGroupManager:IsInvalidTile(tile) then
TempTile_HandleTileChange_Void(x, y, z)
end
end
function TempTile_HandleTileChange_Warn(x, y, z, tile)
if TileGroupManager:IsOceanTile(tile) then
TempTile_HandleTileChange_Ocean_Warn(x, y, z)
elseif TileGroupManager:IsInvalidTile(tile) then
TempTile_HandleTileChange_Void_Warn(x, y, z)
end
end
--------------------------------------------------------------------------
local function Bridge_DeployCheck_ShouldStopAtTile(tile) -- Internal.
return TileGroupManager:IsTemporaryTile(tile) and tile ~= WORLD_TILES.FARMING_SOIL
end
local function Bridge_DeployCheck_CanStartAtTile(tile) -- Internal.
return TileGroupManager:IsLandTile(tile) and not Bridge_DeployCheck_ShouldStopAtTile(tile)
end
local function Bridge_DeployCheck_HandleOverhangs(sx, sz, TILE_SCALE, _map) -- Internal.
-- If a point lays on an overhang we need to adjust it so that it is not on an overhang by reflecting it over the tile border first.
local cx, cy, cz = _map:GetTileCenterPoint(sx, 0, sz)
local dx, dz = cx - sx, cz - sz
local signdx, signdz = dx < 0 and -1 or 1, dz < 0 and -1 or 1
local absdx, absdz = math.abs(dx), math.abs(dz)
local ishorizontal = absdx > absdz
local rsx, rsz, dirx, dirz
if ishorizontal then
rsx = sx + 2 * (absdx - TILE_SCALE * 0.5) * signdx
rsz = sz
dirx = signdx * TILE_SCALE
dirz = 0
else
rsx = sx
rsz = sz + 2 * (absdz - TILE_SCALE * 0.5) * signdz
dirx = 0
dirz = signdz * TILE_SCALE
end
if _map:IsLandTileAtPoint(rsx, 0, rsz) then
return rsx, rsz, dirx, dirz
end
-- We have reflected from an overhang onto another overhang along a coastline fallback to rectangle direction.
if not ishorizontal then -- Flip the logic so the reflection happens in the opposite direction.
rsx = sx + 2 * (absdx - TILE_SCALE * 0.5) * signdx
rsz = sz
dirx = signdx * TILE_SCALE
dirz = 0
else
rsx = sx
rsz = sz + 2 * (absdz - TILE_SCALE * 0.5) * signdz
dirx = 0
dirz = signdz * TILE_SCALE
end
if _map:IsLandTileAtPoint(rsx, 0, rsz) then
return rsx, rsz, dirx, dirz
end
-- We are on a corner of a tile reflect both points so we are on the solid tile first and then use non-overhang protocols.
rsx = sx + 2 * (absdx - TILE_SCALE * 0.5) * signdx
rsz = sz + 2 * (absdz - TILE_SCALE * 0.5) * signdz
return rsx, rsz, nil, nil
end
local function Bridge_DeployCheck_HandleGround(sx, sz, TILE_SCALE, _map, isvalidtileforbridgeatpointfn) -- Internal.
-- We are on a ground tile so we will first do a diamond direction check first and then a rectangle fallback.
local cx, cy, cz = _map:GetTileCenterPoint(sx, 0, sz)
local dx, dz = cx - sx, cz - sz
local signdx, signdz = dx < 0 and -1 or 1, dz < 0 and -1 or 1
local absdx, absdz = math.abs(dx), math.abs(dz)
local ishorizontal = absdx > absdz
local rsx, rsz, dirx, dirz
if ishorizontal then
rsx = sx + 2 * (absdx - TILE_SCALE * 0.5) * signdx
rsz = sz
dirx = -signdx * TILE_SCALE
dirz = 0
else
rsx = sx
rsz = sz + 2 * (absdz - TILE_SCALE * 0.5) * signdz
dirx = 0
dirz = -signdz * TILE_SCALE
end
if isvalidtileforbridgeatpointfn(_map, rsx, 0, rsz) then
return dirx, dirz
end
-- Check the other adjacent diagonal path.
if not ishorizontal then
rsx = sx + 2 * (absdx - TILE_SCALE * 0.5) * signdx
rsz = sz
dirx = -signdx * TILE_SCALE
dirz = 0
else
rsx = sx
rsz = sz + 2 * (absdz - TILE_SCALE * 0.5) * signdz
dirx = 0
dirz = -signdz * TILE_SCALE
end
if isvalidtileforbridgeatpointfn(_map, rsx, 0, rsz) then
return dirx, dirz
end
-- We are too far in land for tiles to be able to be chosen.
return nil, nil
end
local function IsValidTileForBridgeAtPoint_Fallback(_map, x, y, z)
return _map:IsValidTileForVineBridgeAtPoint(x, y, z)
end
local function CanDeployBridgeAtPoint_Fallback(_map, x, y, z)
return _map:CanDeployVineBridgeAtPoint(x, y, z)
end
local function Bridge_Deploy_Raytrace(sx, sz, dirx, dirz, maxlength, _map, candeploybridgeatpointfn, inst) -- Internal.
-- Scan for land.
local hitland = false
local spots
for i = 0, maxlength do -- Intentionally 0 to max to have a + 1 for the end tile cap inclusion.
sx, sz = sx + dirx, sz + dirz
local pt_offseted = Point(sx, 0, sz)
local tile_current = _map:GetTileAtPoint(sx, 0, sz)
if TileGroupManager:IsLandTile(tile_current) then
hitland = not Bridge_DeployCheck_ShouldStopAtTile(tile_current)
break
end
-- Check for adjacent tiles to make sure those are not invisible tiles too.
if GROUND_INVISIBLETILES[_map:GetTileAtPoint(sx + dirz, 0, sz + dirx)] or GROUND_INVISIBLETILES[_map:GetTileAtPoint(sx - dirz, 0, sz - dirx)] then
return false
end
if not candeploybridgeatpointfn(_map, pt_offseted, inst) then
return false
end
if not spots then
spots = {}
end
table.insert(spots, pt_offseted)
end
if not hitland or not spots then
return false
end
spots.direction = {x = dirx, z = dirz,}
return true, spots
end
local function Bridge_Deploy_GetBestRayTrace(sx, sz, maxlength, _map, candeploybridgeatpointfn, inst) -- Internal.
-- We have a point inside of a tile that we want to calculate out the best ray trace direction for.
-- There are several cases and we will know what to do after we trace all directions.
local cx, cy, cz = _map:GetTileCenterPoint(sx, 0, sz)
local success_N, spots_N
local success_E, spots_E
local success_S, spots_S
local success_W, spots_W
if Bridge_DeployCheck_CanStartAtTile(_map:GetTileAtPoint(cx, 0, cz - TILE_SCALE)) then
success_N, spots_N = Bridge_Deploy_Raytrace(cx, cz - TILE_SCALE, 0, TILE_SCALE, maxlength, _map, candeploybridgeatpointfn, inst)
else
success_N = false
end
if Bridge_DeployCheck_CanStartAtTile(_map:GetTileAtPoint(cx - TILE_SCALE, 0, cz)) then
success_E, spots_E = Bridge_Deploy_Raytrace(cx - TILE_SCALE, cz, TILE_SCALE, 0, maxlength, _map, candeploybridgeatpointfn, inst)
else
success_E = false
end
if Bridge_DeployCheck_CanStartAtTile(_map:GetTileAtPoint(cx, 0, cz + TILE_SCALE)) then
success_S, spots_S = Bridge_Deploy_Raytrace(cx, cz + TILE_SCALE, 0, -TILE_SCALE, maxlength, _map, candeploybridgeatpointfn, inst)
else
success_S = false
end
if Bridge_DeployCheck_CanStartAtTile(_map:GetTileAtPoint(cx + TILE_SCALE, 0, cz)) then
success_W, spots_W = Bridge_Deploy_Raytrace(cx + TILE_SCALE, cz, -TILE_SCALE, 0, maxlength, _map, candeploybridgeatpointfn, inst)
else
success_W = false
end
local success_count = (success_N and 1 or 0) + (success_E and 1 or 0) + (success_S and 1 or 0) + (success_W and 1 or 0)
if success_count == 0 then -- Nothing valid.
return false
elseif success_count == 1 then -- One edge is valid.
return true, spots_N or spots_E or spots_S or spots_W
elseif success_count == 2 then -- Two edges are valid check which direction.
if success_N and success_S or success_E and success_W then -- Opposite sides.
if success_N then -- N-S
if cz < sz then
return true, spots_N
else
return true, spots_S
end
else -- E-W
if cx < sx then
return true, spots_E
else
return true, spots_W
end
end
else -- Corner.
local ishorizontal = math.abs(cx - sx) > math.abs(cz - sz)
if success_N and success_E then
if cx - sx < cz - sz then
return true, spots_E
else
return true, spots_N
end
elseif success_E and success_S then
if sx - cx > cz - sz then
return true, spots_E
else
return true, spots_S
end
elseif success_S and success_W then
if cx - sx > cz - sz then
return true, spots_W
else
return true, spots_S
end
else -- W-N
if sx - cx < cz - sz then
return true, spots_W
else
return true, spots_N
end
end
end
elseif success_count == 3 then -- U shape. We will aim to go out of the U because it will always be longer.
if not success_N then
return true, spots_S
elseif not success_E then
return true, spots_W
elseif not success_S then
return true, spots_N
else -- not success_W
return true, spots_E
end
else -- Square shape. Do not let it be placed here.
return false
end
end
function Bridge_DeployCheck_Helper(inst, pt, options)
local _world = TheWorld
if _world.ismastersim then
local requiredworldcomponent = options and options.requiredworldcomponent or nil
if requiredworldcomponent and not _world.components[requiredworldcomponent] then
return false
end
end
local _map = _world.Map
local TILE_SCALE = TILE_SCALE
local maxlength = options and options.maxlength or TUNING.ROPEBRIDGE_LENGTH_TILES
local isvalidtileforbridgeatpointfn = options and options.isvalidtileforbridgeatpointfn or IsValidTileForBridgeAtPoint_Fallback
local candeploybridgeatpointfn = options and options.candeploybridgeatpointfn or CanDeployBridgeAtPoint_Fallback
local deployskipfirstlandtile = options and options.deployskipfirstlandtile
-- NOTES(JBK): We want the player position to not be involved for the bridge construction at all.
-- So we will need to transform the point into a position that makes the most sense given the geometric nature of tiles.
local sx, sy, sz = pt:Get()
local osx, osy, osz = sx, sy, sz
local dirx, dirz
if isvalidtileforbridgeatpointfn(_map, sx, 0, sz) then
if deployskipfirstlandtile then
return Bridge_Deploy_GetBestRayTrace(sx, sz, maxlength, _map, candeploybridgeatpointfn, inst)
elseif _map:IsVisualGroundAtPoint(sx, 0, sz) then
sx, sz, dirx, dirz = Bridge_DeployCheck_HandleOverhangs(sx, sz, TILE_SCALE, _map)
end
end
if dirx == nil then
if deployskipfirstlandtile then
return false
end
if not _map:IsLandTileAtPoint(sx, 0, sz) then
return false
end
dirx, dirz = Bridge_DeployCheck_HandleGround(sx, sz, TILE_SCALE, _map, isvalidtileforbridgeatpointfn)
end
if dirx == nil then
return false
end
local tile = _map:GetTileAtPoint(sx, 0, sz)
if Bridge_DeployCheck_ShouldStopAtTile(tile) then
return false
end
-- We now have a valid direction and starting point align our tile ray trace to tile coordinates finally.
sx, sy, sz = _map:GetTileCenterPoint(sx, 0, sz)
local success, spots = Bridge_Deploy_Raytrace(sx, sz, dirx, dirz, maxlength, _map, candeploybridgeatpointfn, inst)
if success then
return success, spots
end
return false
end
--------------------------------------------------------------------------
function DecayCharlieResidueAndGoOnCooldownIfItExists(inst)
local roseinspectableuser = inst.components.roseinspectableuser
if roseinspectableuser == nil then
return
end
roseinspectableuser:ForceDecayResidue()
roseinspectableuser:GoOnCooldown()
end
function DecayCharlieResidueIfItExists(inst)
local roseinspectableuser = inst.components.roseinspectableuser
if roseinspectableuser == nil then
return
end
roseinspectableuser:ForceDecayResidue()
end
local function OnFuelPresentation3(inst)
inst:ReturnToScene()
if inst.components.inventoryitem ~= nil then
inst.components.inventoryitem:OnDropped(true, .5)
end
end
local function OnFuelPresentation2(inst, x, z, upgraded)
local fx = SpawnPrefab(upgraded and "shadow_puff_solid" or "shadow_puff")
fx.Transform:SetPosition(x, 0, z)
inst:DoTaskInTime(3 * FRAMES, OnFuelPresentation3)
end
local function OnFuelPresentation1(inst, x, z, upgraded)
local fx = SpawnPrefab((upgraded or TheWorld:HasTag("cave")) and "charlie_snap_solid" or "charlie_snap")
fx.Transform:SetPosition(x, 2, z)
inst:DoTaskInTime(25 * FRAMES, OnFuelPresentation2, x, z, upgraded)
end
local function OnResidueActivated_Fuel_Internal(inst, doer, odds)
local skilltreeupdater = doer.components.skilltreeupdater
local upgraded = skilltreeupdater and skilltreeupdater:IsActivated("winona_charlie_2") and TryLuckRoll(doer, odds, LuckFormulas.ResidueUpgradeFuel) or nil
local fuel = SpawnPrefab(upgraded and "horrorfuel" or "nightmarefuel")
fuel:RemoveFromScene()
local x, y, z = inst.Transform:GetWorldPosition()
local radius = inst:GetPhysicsRadius(0)
if radius > 0 then
radius = radius + 1.5
end
local theta = math.random() * PI2
x, z = x + math.cos(theta) * radius, z + math.sin(theta) * radius
fuel.Transform:SetPosition(x, 0, z)
fuel:DoTaskInTime(0.5, OnFuelPresentation1, x, z, upgraded)
end
local function OnResidueActivated_Fuel(inst, doer)
OnResidueActivated_Fuel_Internal(inst, doer, TUNING.SKILLS.WINONA.ROSEGLASSES_UPGRADE_CHANCE)
end
local function OnResidueActivated_Fuel_IncreasedHorror(inst, doer)
OnResidueActivated_Fuel_Internal(inst, doer, TUNING.SKILLS.WINONA.ROSEGLASSES_UPGRADE_CHANCE_INCREASED)
end
function MakeRoseTarget_CreateFuel(inst)
local roseinspectable = inst:AddComponent("roseinspectable")
roseinspectable:SetOnResidueActivated(OnResidueActivated_Fuel)
roseinspectable:SetForcedInduceCooldownOnActivate(true)
end
function MakeRoseTarget_CreateFuel_IncreasedHorror(inst)
local roseinspectable = inst:AddComponent("roseinspectable")
roseinspectable:SetOnResidueActivated(OnResidueActivated_Fuel_IncreasedHorror)
roseinspectable:SetForcedInduceCooldownOnActivate(true)
end
--------------------------------------------------------------------------
local function IsValidTileForVineBridgeAtPoint_Wrapper(_map, x, y, z)
return _map:IsValidTileForVineBridgeAtPoint(x, y, z)
end
local function CanDeployVineBridgeAtPoint_Wrapper(_map, x, y, z)
return _map:CanDeployVineBridgeAtPoint(x, y, z)
end
local RosePoint_VineBridge_Options = {
maxlength = TUNING.SKILLS.WINONA.CHARLIE_VINEBRIDGE_LENGTH_TILES,
isvalidtileforbridgeatpointfn = IsValidTileForVineBridgeAtPoint_Wrapper,
candeploybridgeatpointfn = CanDeployVineBridgeAtPoint_Wrapper,
requiredworldcomponent = "vinebridgemanager",
}
local function RosePoint_VineBridge_Check(inst, pt)
return Bridge_DeployCheck_Helper(inst, pt, RosePoint_VineBridge_Options)
end
local function RosePoint_VineBridge_Do(inst, pt, spots)
local vinebridgemanager = TheWorld.components.vinebridgemanager
local duration = TUNING.VINEBRIDGE_DURATION
local breakdata = {}
local spawndata = {
base_time = 0.5,
random_time = 0.0,
direction = spots.direction,
}
for i, spot in ipairs(spots) do
spawndata.base_time = 0.25 * i
vinebridgemanager:QueueCreateVineBridgeAtPoint(spot.x, spot.y, spot.z, spawndata)
breakdata.fxtime = duration + 0.25 * i
breakdata.shaketime = breakdata.fxtime - 1
breakdata.destroytime = breakdata.fxtime + 70 * FRAMES
vinebridgemanager:QueueDestroyForVineBridgeAtPoint(spot.x, spot.y, spot.z, breakdata)
end
return true
end
-- NOTES(JBK): Functions and names for CLOSEINSPECTORUTIL checks.
-- The order of priority is defined by what is present in this table use the contextname to table.insert new ones.
ROSEPOINT_CONFIGURATIONS = {
{
contextname = "Vine Bridge",
checkfn = RosePoint_VineBridge_Check,
callbackfn = RosePoint_VineBridge_Do,
--forcedcooldown = nil,
--cooldownfn = nil,
},
}
--------------------------------------------------------------------------
--closeinspector
CLOSEINSPECTORUTIL = {}
CLOSEINSPECTORUTIL.IsValidTarget = function(doer, target)
if TheWorld.ismastersim then
return not (
(target.Physics and target.Physics:GetMass() ~= 0) or
target.components.locomotor or
target.components.inventoryitem or
target:HasTag("character")
)
else
return not (
(target.Physics and target.Physics:GetMass() ~= 0) or
target:HasTag("locomotor") or
target.replica.inventoryitem or
target:HasTag("character")
)
end
end
CLOSEINSPECTORUTIL.IsValidPos = function(doer, pos)
local is_cooldown_rose = true
local player_classified = doer.player_classified
if player_classified then
is_cooldown_rose = player_classified.roseglasses_cooldown:value()
end
for _, config in ipairs(ROSEPOINT_CONFIGURATIONS) do
local will_cooldown = false
if config.forcedcooldown ~= nil then
will_cooldown = config.forcedcooldown
elseif config.cooldownfn ~= nil then
will_cooldown = config.cooldownfn(self.inst, self.point, data)
end
if not will_cooldown or (will_cooldown and not is_cooldown_rose) then
if config.checkfn(doer, pos) then
return true
end
end
end
return false
end
CLOSEINSPECTORUTIL.CanCloseInspect = function(doer, targetorpos)
if doer == nil then
return false
elseif TheWorld.ismastersim then
if not (doer.components.inventory and doer.components.inventory:EquipHasTag("closeinspector")) or
(doer.components.rider and doer.components.rider:IsRiding())
then
return false
end
else
local inventory = doer.replica.inventory
if not (inventory and inventory:EquipHasTag("closeinspector")) then
return false
end
local rider = doer.replica.rider
if rider and rider:IsRiding() then
return false
end
end
if targetorpos:is_a(EntityScript) then
return targetorpos:IsValid() and CLOSEINSPECTORUTIL.IsValidTarget(doer, targetorpos)
end
return CLOSEINSPECTORUTIL.IsValidPos(doer, targetorpos)
end
--------------------------------------------------------------------------
-- rabbitkingmanager and rabbit prefabs
function HasMeatInInventoryFor_Checker(item)
return item.components.edible ~= nil and item.components.edible.foodtype == FOODTYPE.MEAT and not item:HasTag("smallcreature")
end
function HasMeatInInventoryFor(inst)
local inventory = inst.components.inventory
if inventory == nil then
return false
end
if inventory:EquipHasTag("hidesmeats") then
return false
end
return inventory:FindItem(HasMeatInInventoryFor_Checker) ~= nil
end
--------------------------------------------------------------------------
-- These may be used on both client and server so have the callbacks acceptable for both uses.
-- Return nil for no count logic and go back to default logic.
-- For items going into a player's inventory.
-- TODO(JBK): Logic for swapping items is not handled.
DesiredMaxTakeCountFunctions = {}
function SetDesiredMaxTakeCountFunction(prefab, callback)
DesiredMaxTakeCountFunctions[prefab] = callback
end
function GetDesiredMaxTakeCountFunction(prefab)
return DesiredMaxTakeCountFunctions[prefab]
end
-- For items going out of a player's inventory they need their own support added.
--DesiredMaxPutCountFunctions = {}
--function SetDesiredMaxPutCountFunction(prefab, callback)
-- DesiredMaxPutCountFunctions[prefab] = callback
--end
--function GetDesiredMaxPutCountFunction(prefab)
-- return DesiredMaxPutCountFunctions[prefab]
--end
--------------------------------------------------------------------------
PICKABLE_FOOD_PRODUCTS =
{
ancientfruit_nightvision = true,
berries = true,
berries_juicy = true,
blue_cap = true,
cactus_meat = true,
carrot = true,
cave_banana = true,
cutlichen = true,
green_cap = true,
red_cap = true,
wormlight_lesser = true,
}
function IsFoodSourcePickable(inst)
return inst.components.pickable ~= nil and PICKABLE_FOOD_PRODUCTS[inst.components.pickable.product]
end
--------------------------------------------------------------------------
-- wobycourier
function GetWobyCourierChestPosition(inst)
if inst.woby_commands_classified then
local x = inst.woby_commands_classified.chest_posx:value()
local z = inst.woby_commands_classified.chest_posz:value()
if x ~= WOBYCOURIER_NO_CHEST_COORD and z ~= WOBYCOURIER_NO_CHEST_COORD then
return x, z
end
end
return nil, nil
end
--------------------------------------------------------------------------
-- Placer
HAS_AXISALIGNED_MOD_ENABLED = nil
KNOWN_AXISALIGNED_MODS = {
"workshop-351325790",
}
function UpdateAxisAlignmentValues(intervals)
TUNING.AXISALIGNEDPLACEMENT_INTERVALS = intervals
TUNING.AXISALIGNEDPLACEMENT_CIRCLESIZE = math.min(8 / intervals, 4)
if ThePlayer then
ThePlayer:PushEvent("refreshaxisalignedplacementintervals")
end
end
local DEFAULT_AXISALIGNMENT_VALUE = 1
AXISALIGNMENT_VALUES = {
{text = STRINGS.UI.OPTIONS.AXISALIGNEDPLACEMENT_SIZE_HALFWALL, data = 2},
{text = STRINGS.UI.OPTIONS.AXISALIGNEDPLACEMENT_SIZE_WALL, data = DEFAULT_AXISALIGNMENT_VALUE},
{text = STRINGS.UI.OPTIONS.AXISALIGNEDPLACEMENT_SIZE_HALFTILE, data = 0.5},
{text = STRINGS.UI.OPTIONS.AXISALIGNEDPLACEMENT_SIZE_TILE, data = 0.25},
}
function CycleAxisAlignmentValues() -- Do not save with Profile.
local closestdiff
local closestindex
local intervals = TUNING.AXISALIGNEDPLACEMENT_INTERVALS
local defaultindex
for i, v in ipairs(AXISALIGNMENT_VALUES) do
local diff = math.abs(v.data - intervals)
if closestdiff == nil or diff < closestdiff then
closestdiff = diff
closestindex = i
end
if v.data == DEFAULT_AXISALIGNMENT_VALUE then
defaultindex = i
end
end
if not closestindex then
closestindex = defaultindex or 1 -- Default got eliminated somewhere.
end
closestindex = closestindex + 1
if closestindex > #AXISALIGNMENT_VALUES then
closestindex = 1
end
UpdateAxisAlignmentValues(AXISALIGNMENT_VALUES[closestindex].data)
end
--------------------------------------------------------------------------
-- wagpunk_arena_manager
WAGPUNK_ARENA_COLLISION_DATA = { -- x, z, rotation, sfxlooper
{-28, -20, 315, false},
{-28, -10, 0, false},
{-28, 0, 0, true},
{-28, 10, 0, false},
{-28, 20, 45, false},
{-24, 20, 45, false},
{-24, 24, 45, true},
{-20, 24, 45, false},
{-20, 28, 45, false},
{-10, 28, 90, false},
{0, 28, 90, true},
{10, 28, 90, false},
{20, 28, 135, false},
{20, 24, 135, false},
{24, 24, 135, true},
{24, 20, 135, false},
{28, 20, 135, false},
{28, 10, 180, false},
{28, 0, 180, true},
{28, -10, 180, false},
{28, -20, 225, false},
{24, -20, 225, false},
{24, -24, 225, true},
{20, -24, 225, false},
{20, -28, 225, false},
{10, -28, 270, false},
{0, -28, 270, true},
{-10, -28, 270, false},
{-20, -28, 315, false},
{-20, -24, 315, false},
{-24, -24, 315, true},
{-24, -20, 315, false},
}
--------------------------------------------------------------------------
local CLEARSPOT_CANT_TAGS = {"INLIMBO", "NOCLICK", "FX", "irreplaceable"}
function ClearSpotForRequiredPrefabAtXZ(x, z, r)
local _world = TheWorld
local ents = TheSim:FindEntities(x, 0, z, MAX_PHYSICS_RADIUS, nil, CLEARSPOT_CANT_TAGS)
for _, ent in ipairs(ents) do
if ent:IsValid() then
local radius = ent:GetPhysicsRadius(0) + r
if ent:GetDistanceSqToPoint(x, 0, z) < radius * radius then
DestroyEntity(ent, _world)
end
end
end
end
--------------------------------------------------------------------------
--For visual fx
--e.g. used by electrocute_fx
local function IsSmallCreature(inst)
return inst:HasAnyTag("smallcreature", "smallcreaturecorpse", "small")
end
local function IsEpicCreature(inst)
return inst:HasAnyTag("epic", "epiccorpse")
end
local function IsLargeCreature(inst)
return inst:HasAnyTag("largecreature", "largecreaturecorpse", "large")
end
function GetCombatFxSize(ent)
local r = ent.override_combat_fx_radius
local sz = ent.override_combat_fx_size
local ht = ent.override_combat_fx_height
local r1 = r or ent:GetPhysicsRadius(0)
if IsSmallCreature(ent) then
r = r or math.min(0.5, r1)
sz = sz or "tiny"
elseif r1 >= 1.5 or IsEpicCreature(ent) then
r = r or math.max(1.5, r1)
sz = sz or "large"
elseif r1 >= 0.9 or IsLargeCreature(ent) then
r = r or math.max(1, r1)
sz = sz or "med"
else
r = r or math.max(0.5, r1)
sz = sz or "small"
end
if ht == nil then
ht = (ent.components.amphibiouscreature and ent.components.amphibiouscreature.in_water and "low") or
(ent:HasTag("flying") and "high") or
(not (ent.sg and ent.sg:HasState("electrocute")) and "low") or --ground plants with no electrocute state
(ent:HasTag("creaturecorpse") and "low") or
nil
elseif string.len(ht) == 0 then
ht = nil
end
return r, sz, ht
end
function GetElectrocuteFxAnim(sz, ht)
return string.format(ht and "shock_%s_%s" or "shock_%s", sz or "small", ht)
end
--Returns true if entity supports electrocution at all, even if it's in a state that currently doesn't allow it
function CanEntityBeElectrocuted(inst)
return inst.sg
and (inst.sg:HasState("electrocute") or inst.sg.mem.burn_on_electrocute)
and not inst.sg.mem.noelectrocute
end
function CalcEntityElectrocuteDuration(inst, override)
local default = TUNING.ELECTROCUTE_DEFAULT_DURATION
local duration =
inst.electrocute_duration or
(inst.sg and inst.sg.mem.burn_on_electrocute and TUNING.ELECTROCUTE_SHORT_DURATION) or
default
if override then
if override > default then
return math.max(duration, override)
elseif override < default then
return math.min(duration, override)
end
end
return duration
end
--------------------------------------------------------------------------
function SpawnElectricHitSparks(inst, target, flash)
--target or inst might be removed
if not inst or not target or not inst:IsValid() or not target:IsValid() then
return
end
local fx_prefab = IsEntityElectricImmune(target) and "electrichitsparks_electricimmune" or "electrichitsparks"
SpawnPrefab(fx_prefab):AlignToTarget(target, inst, flash)
end
function LightningStrikeAttack(inst)
if IsEntityElectricImmune(inst) or (inst.sg and inst.sg:HasStateTag("noelectrocute")) then
return false
end
if inst.components.health then
local wetness_mult = TUNING.ELECTRIC_WET_DAMAGE_MULT * inst:GetWetMultiplier()
local damage = TUNING.LIGHTNING_DAMAGE + wetness_mult * TUNING.LIGHTNING_DAMAGE
inst.components.health:DoDelta(-damage, false, "lightning")
end
--V2C: -switched to stategraph event instead of GoToState
-- -use Immediate to preserve legacy timing
inst:PushEventImmediate("electrocute")
-- NOTE(Omar): I really wanted to improve lightning damage technicals to use GetAttacked, but weather.lua is set up a bit awkwardly with the spawning of the entity,
-- and it's prefab is to be determined during logic, so whatever, health:DoDelta and PushEvent, you're here to stay -__-
--inst.components.combat:GetAttacked(lightning, damage, nil, "electric")
return true
end
local LIGHTNING_EXCLUDE_TAGS = { "player", "INLIMBO", "lightningblocker", "FX" }
local LIGHTNING_BURNING_ONEOF_TAGS = {"wall", "fence", "plant", "structure", "_inventoryitem", "bush", "pickable"}
for k, v in pairs(FUELTYPE) do
if v ~= FUELTYPE.USAGE then --Not a real fuel
table.insert(LIGHTNING_EXCLUDE_TAGS, v.."_fueled")
end