-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathentityscript.lua
2022 lines (1706 loc) · 62.2 KB
/
entityscript.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require("class")
local StateGraphs = {}
local Components = {}
local EventServerFiles = {}
StopUpdatingComponents = {}
local ClientSideInventoryImageFlags = {}
local function EntityWatchWorldState(self, var, fn)
self.worldstatewatching = self.worldstatewatching or {}
local watcherfns = self.worldstatewatching[var]
if watcherfns == nil then
watcherfns = {}
self.worldstatewatching[var] = watcherfns
end
table.insert(watcherfns, fn)
end
local function EntityStopWatchingWorldState(self, var, fn)
if self.worldstatewatching == nil then
return
end
local watcherfns = self.worldstatewatching[var]
if watcherfns ~= nil then
RemoveByValue(watcherfns, fn)
if next(watcherfns) == nil then
self.worldstatewatching[var] = nil
end
end
if next(self.worldstatewatching) == nil then
self.worldstatewatching = nil
end
end
local function ComponentWatchWorldState(self, var, fn)
EntityWatchWorldState(self.inst, var, fn)
TheWorld.components.worldstate:AddWatcher(var, self.inst, fn, self)
end
local function ComponentStopWatchingWorldState(self, var, fn)
EntityStopWatchingWorldState(self.inst, var, fn)
TheWorld.components.worldstate:RemoveWatcher(var, self.inst, fn, self)
end
local function LoadComponent(name)
if Components[name] == nil then
Components[name] = require("components/"..name)
assert(Components[name], "could not load component "..name)
Components[name].WatchWorldState = ComponentWatchWorldState
Components[name].StopWatchingWorldState = ComponentStopWatchingWorldState
end
return Components[name]
end
local function LoadStateGraph(name)
if StateGraphs[name] == nil then
local fn = require("stategraphs/"..name)
assert(fn, "could not load stategraph "..name)
StateGraphs[name] = fn
end
local sg = StateGraphs[name]
assert(sg, "stategraph "..name.." is not valid")
return sg
end
-------------------------------------------
--Loading event server files
function event_server_data(eventname, path)
local fullpath = eventname.."_event_server/"..path
if EventServerFiles[fullpath] == nil then
EventServerFiles[fullpath] = requireeventfile(fullpath)
if path.sub(1, 11) == "components/" then
EventServerFiles[fullpath].WatchWorldState = ComponentWatchWorldState
EventServerFiles[fullpath].StopWatchingWorldState = ComponentStopWatchingWorldState
end
end
return EventServerFiles[fullpath]
end
-------------------------------------------
--Override adding network component
local function OnActionComponentsDirty(inst)
inst.actioncomponents = inst.actionreplica.actioncomponents:value()
end
local function OnModActionComponentsDirty(inst, modname)
inst.modactioncomponents = inst.modactioncomponents or {}
inst.modactioncomponents[modname] = inst.actionreplica.modactioncomponents[modname]:value()
end
local function SerializeInherentActions(inst)
if inst.actionreplica == nil then
return
end
local t = {}
if inst.inherentactions ~= nil then
local id = 1
for k, v in pairs(ACTIONS) do
if inst.inherentactions[v] then
table.insert(t, id)
end
id = id + 1
end
end
inst.actionreplica.inherentactions:set(t)
end
local function DeserializeInherentActions(inst)
inst.inherentactions = {}
for _, v in ipairs(inst.actionreplica.inherentactions:value()) do
inst.inherentactions[v] = true
end
if next(inst.inherentactions) == nil then
inst.inherentactions = nil
else
local id = 1
for _, v in pairs(ACTIONS) do
if inst.inherentactions[id] then
inst.inherentactions[id] = nil
inst.inherentactions[v] = true
end
id = id + 1
end
end
end
local function SerializeAction(action)
if action ~= nil then
local id = 1
for _, v in pairs(ACTIONS) do
if action == v then
return id
end
id = id + 1
end
end
return 0
end
local function DeserializeAction(actionid)
if actionid > 0 then
local id = 1
for k, v in pairs(ACTIONS) do
if actionid == id then
return v
end
id = id + 1
end
end
end
local function OnInherentSceneActionDirty(inst)
inst.inherentsceneaction = DeserializeAction(inst.actionreplica.inherentsceneaction:value())
end
local function OnInherentSceneAltActionDirty(inst)
inst.inherentscenealtaction = DeserializeAction(inst.actionreplica.inherentscenealtaction:value())
end
local AddNetworkProxy = Entity.AddNetwork
function Entity:AddNetwork()
AddNetworkProxy(self)
local guid = self:GetGUID()
local inst = Ents[guid]
inst.actionreplica =
{
actioncomponents = net_bytearray(guid, "actioncomponents", "actioncomponentsdirty"),
inherentactions = net_bytearray(guid, "inherentactions", "inherentactionsdirty"),
inherentsceneaction = net_byte(guid, "inherentsceneaction", "inherentsceneactiondirty"),
inherentscenealtaction = net_byte(guid, "inherentscenealtaction", "inherentscenealtactiondirty"),
modactioncomponents = {}
}
for _,modname in pairs(ModManager:GetServerModsNames()) do
inst.actionreplica.modactioncomponents[modname] = net_smallbytearray(guid, "modactioncomponents"..modname, "modactioncomponentsdirty"..modname)
end
if not TheWorld.ismastersim then
inst:ListenForEvent("actioncomponentsdirty", OnActionComponentsDirty)
inst:ListenForEvent("inherentactionsdirty", DeserializeInherentActions)
inst:ListenForEvent("inherentsceneactiondirty", OnInherentSceneActionDirty)
inst:ListenForEvent("inherentscenealtactiondirty", OnInherentSceneAltActionDirty)
for _, modname in pairs(ModManager:GetServerModsNames()) do
inst:ListenForEvent("modactioncomponentsdirty"..modname, function(inst)
OnModActionComponentsDirty(inst, modname)
end)
end
end
end
local replica_mt =
{
__index = function(t, k)
return rawget(t, "inst"):ValidateReplicaComponent(k, rawget(t, "_")[k])
end,
}
EntityScript = Class(function(self, entity)
self.entity = entity
self.components = {}
self.lower_components_shadow = {}
self.GUID = entity:GetGUID()
self.spawntime = GetTime()
self.persists = true
self.inlimbo = false
self.name = nil
self.data = nil
self.listeners = nil
self.updatecomponents = nil
self.updatestaticcomponents = nil
self.actioncomponents = {}
self.inherentactions = nil
self.inherentsceneaction = nil
self.inherentscenealtaction = nil
self.event_listeners = nil
self.event_listening = nil
self.worldstatewatching = nil
self.pendingtasks = nil
self.children = nil
self.platformfollowers = nil
self.actionreplica = nil
--Replica components container with overriden accessor
self.replica = { _ = {}, inst = self }
setmetatable(self.replica, replica_mt)
end)
function EntityScript:GetSaveRecord()
local record =
{
prefab = self.prefab,
}
local x, y, z = self.Transform:GetWorldPosition()
if self.temp_save_platform_pos or self.entity:HasTag("player") then
record.age = self.Network:GetPlayerAge()
local platform = self:GetCurrentPlatform()
if self._snapshot_platform ~= nil then
if platform == nil and self._snapshot_platform == TheWorld.Map:GetPlatformAtPoint(x, z) then
--V2C: During RestoreSnapshotUserSession, players aren't attached to platforms correctly.
-- This is quick hack to fix that for now.
platform = self._snapshot_platform
end
self._snapshot_platform = nil
end
if platform then
local rx, ry, rz = platform.entity:WorldToLocalSpace(x, y, z)
-- NaN and +/-inf hunting
if isbadnumber(rx) or isbadnumber(ry) or isbadnumber(rz) then
print("EntityScript:GetSaveRecord error saving position: ", self.prefab, rx, ry, rz, ":", x, y, z, ":", platform)
if CONFIGURATION ~= "PRODUCTION" then
error("EntityScript:GetSaveRecord qnan error")
end
rx, ry, rz = 0, 0, 0
end
record.puid = platform.components.walkableplatform:GetUID()
record.rx = rx and math.floor(rx*1000)/1000 or 0
record.rz = rz and math.floor(rz*1000)/1000 or 0
if ry and ry ~= 0 then
ry = math.floor(ry*1000)/1000
record.ry = ry ~= 0 and ry or nil
end
end
end
-- NaN and +/-inf hunting
if isbadnumber(x) or isbadnumber(y) or isbadnumber(z) then
print("EntityScript:GetSaveRecord error saving position: ", self.prefab, x, y, z)
if CONFIGURATION ~= "PRODUCTION" then
error("EntityScript:GetSaveRecord qnan error")
end
x, y, z = 0, 0, 0
end
record.x = x and math.floor(x*1000)/1000 or 0
record.z = z and math.floor(z*1000)/1000 or 0
--y is often 0 in our game, so be selective.
if y and y ~= 0 then
y = math.floor(y*1000)/1000
record.y = y ~= 0 and y or nil
end
record.skinname = self.skinname
record.skin_id = self.skin_id
record.alt_skin_ids = self.alt_skin_ids
local references
record.data, references = self:GetPersistData()
return record, references
end
function EntityScript:Hide()
self.entity:Hide(false)
end
function EntityScript:Show()
self.entity:Show(false)
end
function EntityScript:StackableSkinHack(target)
-- NOTES(JBK): This is a replacement function for old comments that were the following line.
-- (this does not work on clients, so we're going to use the AnimState hack instead)
if self.AnimState == nil or target.AnimState == nil then
return true
end
return self.AnimState:GetSkinBuild() == target.AnimState:GetSkinBuild()
end
function EntityScript:IsInLimbo()
--V2C: faster than checking tag, but only valid on mastersim
return self.inlimbo
end
function EntityScript:ForceOutOfLimbo(state)
self.forcedoutoflimbo = state or nil
self.entity:SetInLimbo(self:IsInLimbo() and not self.forcedoutoflimbo or false)
end
function EntityScript:RemoveFromScene()
self.entity:AddTag("INLIMBO")
self.entity:SetInLimbo(not self.forcedoutoflimbo)
self.inlimbo = true
self.entity:Hide()
self:StopBrain()
if self.sg then
self.sg:Stop()
end
if self.Physics then
self.Physics:SetActive(false)
end
if self.Light and self.Light:GetDisableOnSceneRemoval() then
self.Light:Enable(false)
end
if self.AnimState then
self.AnimState:Pause()
end
if self.DynamicShadow then
self.DynamicShadow:Enable(false)
end
if self.MiniMapEntity then
self.MiniMapEntity:SetEnabled(false)
end
self:PushEvent("enterlimbo")
end
function EntityScript:ReturnToScene()
if not self:IsValid() then
print("[ERROR] Calling ReturnToScene on an invalid entity!", self.prefab) -- Keep this for debug logs to come.
end
self.entity:RemoveTag("INLIMBO")
self.entity:SetInLimbo(false)
self.inlimbo = false
self.entity:Show()
if self.Physics then
self.Physics:SetActive(true)
end
if self.Light then
self.Light:Enable(true)
end
if self.AnimState then
self.AnimState:Resume()
end
if self.DynamicShadow then
self.DynamicShadow:Enable(true)
end
if self.MiniMapEntity then
self.MiniMapEntity:SetEnabled(true)
end
self:RestartBrain()
if self.sg then
self.sg:Start()
end
self:PushEvent("exitlimbo")
end
function EntityScript:__tostring()
return string.format("%d - %s%s", self.GUID, self.prefab or "", self.inlimbo and "(LIMBO)" or "")
end
function EntityScript:AddInherentAction(act)
self.inherentactions = self.inherentactions or {}
self.inherentactions[act] = true
SerializeInherentActions(self)
end
function EntityScript:RemoveInherentAction(act)
if self.inherentactions ~= nil then
self.inherentactions[act] = nil
if next(self.inherentactions) == nil then
self.inherentactions = nil
end
SerializeInherentActions(self)
end
end
function EntityScript:GetTimeAlive()
return GetTime() - self.spawntime
end
function EntityScript:StartUpdatingComponent(cmp, do_static_update)
if not self:IsValid() then
return
end
if not self.updatecomponents then
self.updatecomponents = {}
NewUpdatingEnts[self.GUID] = self
num_updating_ents = num_updating_ents + 1
end
if do_static_update then
if not self.updatestaticcomponents then
self.updatestaticcomponents = {}
NewStaticUpdatingEnts[self.GUID] = self
end
end
if StopUpdatingComponents[cmp] == self then
StopUpdatingComponents[cmp] = nil
end
local cmpname = nil
for k,v in pairs(self.components) do
if v == cmp then
cmpname = k
break
end
end
self.updatecomponents[cmp] = cmpname or "component"
if do_static_update then
self.updatestaticcomponents[cmp] = cmpname or "component"
end
end
function EntityScript:StopUpdatingComponent(cmp)
if self.updatecomponents or self.updatestaticcomponents then
StopUpdatingComponents[cmp] = self
end
end
function EntityScript:StopUpdatingComponent_Deferred(cmp)
if self.updatecomponents then
self.updatecomponents[cmp] = nil
if IsTableEmpty(self.updatecomponents) then
self.updatecomponents = nil
UpdatingEnts[self.GUID] = nil
NewUpdatingEnts[self.GUID] = nil
num_updating_ents = num_updating_ents - 1
end
end
if self.updatestaticcomponents then
self.updatestaticcomponents[cmp] = nil
if IsTableEmpty(self.updatestaticcomponents) then
self.updatestaticcomponents = nil
StaticUpdatingEnts[self.GUID] = nil
NewStaticUpdatingEnts[self.GUID] = nil
end
end
end
function EntityScript:StartWallUpdatingComponent(cmp)
if not self:IsValid() then
return
end
if not self.wallupdatecomponents then
self.wallupdatecomponents = {}
NewWallUpdatingEnts[self.GUID] = self
end
local cmpname = nil
for k,v in pairs(self.components) do
if v == cmp then
cmpname = k
break
end
end
self.wallupdatecomponents[cmp] = cmpname or "component"
end
function EntityScript:StopWallUpdatingComponent(cmp)
if self.wallupdatecomponents then
self.wallupdatecomponents[cmp] = nil
local num = 0
for k,v in pairs(self.wallupdatecomponents) do
num = num + 1
break
end
if num == 0 then
self.wallupdatecomponents = nil
WallUpdatingEnts[self.GUID] = nil
NewWallUpdatingEnts[self.GUID] = nil
end
end
end
function EntityScript:GetComponentName(cmp)
for k,v in pairs(self.components) do
if v == cmp then
return k
end
end
return "component"
end
function EntityScript:AddTag(tag)
self.entity:AddTag(tag)
end
function EntityScript:RemoveTag(tag)
self.entity:RemoveTag(tag)
end
function EntityScript:AddOrRemoveTag(tag, condition)
if condition then
self.entity:AddTag(tag)
else
self.entity:RemoveTag(tag)
end
end
function EntityScript:HasTag(tag)
return self.entity:HasTag(tag)
end
function EntityScript:HasTags(...)
local tags = select(1, ...)
if type(tags) == "table" then
return self.entity:HasAllTags(unpack(tags))
else
return self.entity:HasAllTags(...)
end
end
EntityScript.HasAllTags = EntityScript.HasTags
function EntityScript:HasOneOfTags(...)
local tags = select(1, ...)
if type(tags) == "table" then
return self.entity:HasAnyTag(unpack(tags))
else
return self.entity:HasAnyTag(...)
end
end
EntityScript.HasAnyTag = EntityScript.HasOneOfTags
require("entityreplica")
--Additional initialization for network entity replicas
--defines: EntityScript:ValidateReplicaComponent(name)
-- EntityScript:ReplicateComponent(name)
-- EntityScript:UnreplicateEntity(name)
-- EntityScript:PrereplicateEntity(name)
-- EntityScript:ReplicateEntity()
require("componentactions")
--defines: EntityScript:RegisterComponentActions(name)
-- EntityScript:UnregisterComponentActions(name)
-- EntityScript:CollectActions(type, ...)
-- EntityScript:IsActionValid(action, right)
function EntityScript:AddComponent(name)
local lower_name = string.lower(name)
if self.lower_components_shadow[lower_name] ~= nil then
print("component "..name.." already exists on entity "..tostring(self).."!"..debugstack_oneline(3))
local existingcmp = self.components[lower_name]
if existingcmp == nil then
for k, v in pairs(self.components) do
if string.lower(k) == lower_name then
existingcmp = v
break
end
end
end
return existingcmp
end
local cmp = LoadComponent(name)
if not cmp then
error("component ".. name .. " does not exist!")
end
self:ReplicateComponent(name)
local loadedcmp = cmp(self)
self.components[name] = loadedcmp
self.lower_components_shadow[lower_name] = true
local postinitfns = ModManager:GetPostInitFns("ComponentPostInit", name)
for _, fn in ipairs(postinitfns) do
fn(loadedcmp, self)
end
self:RegisterComponentActions(name)
return loadedcmp
end
function EntityScript:RemoveComponent(name)
local cmp = self.components[name]
if cmp then
self:StopUpdatingComponent(cmp)
self:StopWallUpdatingComponent(cmp)
self.components[name] = nil
self.lower_components_shadow[string.lower(name)] = nil
if cmp.OnRemoveFromEntity then
cmp:OnRemoveFromEntity()
end
self:UnreplicateComponent(name)
self:UnregisterComponentActions(name)
end
end
function EntityScript:GetBasicDisplayName()
return (self.displaynamefn ~= nil and self:displaynamefn())
or (self.nameoverride ~= nil and STRINGS.NAMES[string.upper(self.nameoverride)])
or (self.name_author_netid ~= nil and ApplyLocalWordFilter(self.name, TEXT_FILTER_CTX_CHAT, self.name_author_netid)) -- this is more lika a TEXT_FILTER_CTX_NAME but its all user input (eg, naming a beefalo) so lets go with TEXT_FILTER_CTX_CHAT
or self.name
end
function EntityScript:GetAdjectivedName()
local name = self:GetBasicDisplayName()
if self:HasTag("player") then
--No adjectives for players
return name
elseif self:HasTag("smolder") then
return ConstructAdjectivedName(self, name, STRINGS.SMOLDERINGITEM)
elseif self:HasTag("diseased") then
return ConstructAdjectivedName(self, name, STRINGS.DISEASEDITEM)
elseif self:HasTag("rotten") then
return ConstructAdjectivedName(self, name, STRINGS.UI.HUD.SPOILED)
elseif self:HasTag("withered") then
return ConstructAdjectivedName(self, name, STRINGS.WITHEREDITEM)
elseif self:HasTag("waxedplant") then
-- No wet prefix for waxed plants.
return ConstructAdjectivedName(self, name, STRINGS.WAXEDPLANT)
elseif not self.no_wet_prefix and (self.always_wet_prefix or self:GetIsWet()) then
--custom
if self.wet_prefix ~= nil then
return ConstructAdjectivedName(self, name, self.wet_prefix)
end
--equippable
local equippable = self.replica.equippable
if equippable ~= nil then
local eslot = equippable:EquipSlot()
if eslot == EQUIPSLOTS.HANDS then
return ConstructAdjectivedName(self, name, STRINGS.WET_PREFIX.TOOL)
elseif eslot == EQUIPSLOTS.HEAD or eslot == EQUIPSLOTS.BODY then
return ConstructAdjectivedName(self, name, STRINGS.WET_PREFIX.CLOTHING)
end
end
--edible
for _, v in pairs(FOODTYPE) do
if self:HasTag("edible_"..v) then
return ConstructAdjectivedName(self, name, STRINGS.WET_PREFIX.FOOD)
end
end
--fuel
for _, v in pairs(FUELTYPE) do
if self:HasTag(v.."_fuel") then
return ConstructAdjectivedName(self, name, STRINGS.WET_PREFIX.FUEL)
end
end
--broken
if self:HasTag("broken") then
return ConstructAdjectivedName(self, ConstructAdjectivedName(self, name, STRINGS.WET_PREFIX.GENERIC), STRINGS.BROKENITEM)
end
--dead creatures
if self:HasTag("deadcreature") then
return ConstructAdjectivedName(self, ConstructAdjectivedName(self, name, STRINGS.WET_PREFIX.GENERIC), STRINGS.DEADCREATURE)
end
--generic
return ConstructAdjectivedName(self, name, STRINGS.WET_PREFIX.GENERIC)
elseif self:HasTag("broken") then
return ConstructAdjectivedName(self, name, STRINGS.BROKENITEM)
elseif self:HasTag("deadcreature") then
return ConstructAdjectivedName(self, name, STRINGS.DEADCREATURE)
end
return name
end
function EntityScript:GetDisplayName()
local name = self:GetAdjectivedName()
if self.prefab ~= nil then
local name_extention = STRINGS.NAME_DETAIL_EXTENTION[string.upper(self.prefab)]
if name_extention ~= nil then
return name.."\n"..name_extention
end
end
return name
end
--Can be used on clients
function EntityScript:GetIsWet()
if self:HasTag("moistureimmunity") then
return false
end
local replica = self.replica.inventoryitem or self.replica.moisture
if replica then
return replica:IsWet()
else
return self:HasTag("wet")
or (TheWorld.state.iswet and not self:HasTag("rainimmunity"))
or (self:HasTag("swimming") and not self:HasTag("likewateroffducksback"))
end
end
--Can be used on clients
function EntityScript:IsAcidSizzling()
if self:HasTag("acidrainimmune") then
return false
end
local replica = self.replica.inventoryitem
if replica ~= nil then
return replica:IsAcidSizzling()
end
local player_classified = self.player_classified
if player_classified ~= nil then
return player_classified.isacidsizzling:value()
end
return false
end
function EntityScript:GetSkinBuild()
-- We cache the build name so we don't need to search for it in the item tables.
self.skin_build_name = self.skin_build_name or GetBuildForItem(self.skinname)
return self.skin_build_name
end
function EntityScript:GetSkinName()
return self.override_skinname or self.skinname
end
function EntityScript:SetPrefabName(name)
self.prefab = name
self.entity:SetPrefabName(name)
self.name = self.name or (STRINGS.NAMES[string.upper(self.prefab)] or "MISSING NAME")
end
function EntityScript:SetPrefabNameOverride(nameoverride)
--Changes what description and name will show for the prefab without actually overriding the prefab itself.
--nameoverride should be the prefab that you wish to use for name and description. (IE: "spiderhole_rock" uses "spiderhole")
self.nameoverride = nameoverride
end
function EntityScript:SetDeployExtraSpacing(spacing)
--Extra spacing required when deploying other entities near this one.
self.deploy_extra_spacing = spacing
if spacing ~= nil then
--see components/map.lua
TheWorld.Map:RegisterDeployExtraSpacing(spacing)
end
end
function EntityScript:SetDeploySmartRadius(radius)
--Smart radius will calculate spacing using our radius and the radii of entities near me as well.
--NOTE: legacy deplay spacing generally does not care about size of other entities,
-- unless those entities specify deploy_extra_spacing.
self.deploy_smart_radius = radius
if radius then
--see components/map.lua
TheWorld.Map:RegisterDeploySmartRadius(radius)
end
end
function EntityScript:SetTerraformExtraSpacing(spacing)
--Extra spacing around entity that connot be terraformed.
self.terraform_extra_spacing = spacing
if spacing ~= nil then
self:AddTag("terraformblocker")
--see components/map.lua
TheWorld.Map:RegisterTerraformExtraSpacing(spacing)
else
self:RemoveTag("terraformblocker")
end
end
function EntityScript:SetGroundTargetBlockerRadius(radius)
--Extra spacing around entity that connot be terraformed.
self.ground_target_blocker_radius = radius
if radius ~= nil then
self:AddTag("groundtargetblocker")
--see components/map.lua
TheWorld.Map:RegisterGroundTargetBlocker(radius)
else
self:RemoveTag("groundtargetblocker")
end
end
function EntityScript:SpawnChild(name)
if self.prefabs then
assert(self.prefabs, "no prefabs registered for this entity ".. name)
local prefab = self.prefabs[name]
assert(prefab, "Could not spawn unknown child type "..name)
local inst = SpawnPrefab(prefab)
assert(inst, "Could not spawn prefab "..name.." "..prefab)
self:AddChild(inst)
return inst
else
local inst = SpawnPrefab(name)
self:AddChild(inst)
return inst
end
end
function EntityScript:RemoveChild(child)
child.parent = nil
if self.children then
self.children[child] = nil
end
child.entity:SetParent(nil)
end
function EntityScript:AddChild(child)
child.platform = nil
if child.parent then
child.parent:RemoveChild(child)
end
child.parent = self
if not self.children then
self.children = {}
end
self.children[child] = true
child.entity:SetParent(self.entity)
end
function EntityScript:RemovePlatformFollower(child)
if child.platform ~= self then
return
end
child.platform = nil
if self.platformfollowers then
self.platformfollowers[child] = nil
end
child.entity:SetPlatform(nil)
end
function EntityScript:AddPlatformFollower(child)
child.parent = nil
if child.platform then
child.platform:RemovePlatformFollower(child)
end
child.platform = self
if not self.platformfollowers then
self.platformfollowers = {}
end
self.platformfollowers[child] = true
child.entity:SetPlatform(self.entity)
end
--only works on master sim
function EntityScript:GetPlatformFollowers()
return self.platformfollowers
end
function EntityScript:GetBrainString()
local str = {}
if self.brain then
table.insert(str, "BRAIN:\n")
table.insert(str, tostring(self.brain))
table.insert(str, "--------\n")
end
return table.concat(str, "")
end
function EntityScript:GetDebugString()
if not self:IsValid() then
return tostring(self).." <INVALID>"
end
local str = {}
table.insert(str, tostring(self))
if self:IsAsleep() then
table.insert(str, " <ASLEEP>")
end
table.insert(str, string.format(" age %2.2f", self:GetTimeAlive()))
table.insert(str, "\n")
table.insert(str, self.entity:GetDebugString())
table.insert(str, "Buffered Action: "..tostring(self.bufferedaction).."\n")
if self.sg then
table.insert(str, "SG:" .. tostring(self.sg).."\n")
end
if self.debugstringfn then
table.insert(str, "DebugString: "..self:debugstringfn().."\n")
end
table.insert(str, "-----------\n")
----[[
local cmpkeys = {}
for k,v in pairs(self.components) do
table.insert(cmpkeys, k)
end
table.sort(cmpkeys)
for i,key in ipairs(cmpkeys) do
if self.components[key].GetDebugString and self.components[key]:GetDebugString() then
table.insert(str, key..": "..self.components[key]:GetDebugString().."\n")
end
end
--]]
--[[if self.brain then
table.insert(str, "-------\nBRAIN:\n")
table.insert(str, tostring(self.brain))
table.insert(str, "--------\n")
end
--]]
--[[
if self.event_listening or self.event_listeners then
table.insert(str, "-------\n")
end
if self.event_listening then
table.insert(str, "Listening for Events:\n")
for event, sources in pairs(self.event_listening) do
table.insert(str, string.format("\t%s%s: ", event, GetTableSize(sources) > 1 and string.format("(%u)", GetTableSize(sources)) or "") )
local max_list = 5 -- this can be a very long list
local n = 0
for source, fns in pairs(sources) do
table.insert(str, string.format("%s%s%s", n > 0 and ", " or "", tostring(source), #fns > 1 and string.format("(%u)", #fns) or ""))
n = n + 1
if n >= max_list then
break
end
end
table.insert(str, "\n")
end
end
if self.event_listeners then
table.insert(str, "Broadcasting Events:\n")
for event, listeners in pairs(self.event_listeners) do
table.insert(str, string.format("\t%s%s: ", event, GetTableSize(listeners) > 1 and string.format("(%u)", GetTableSize(listeners)) or "") )