-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathActors3D.bb
More file actions
845 lines (760 loc) · 28.8 KB
/
Copy pathActors3D.bb
File metadata and controls
845 lines (760 loc) · 28.8 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
; Gubbin joint names
Dim GubbinJoints$(5)
; Default values
GubbinJoints$(0) = "Head"
GubbinJoints$(1) = "Chest"
GubbinJoints$(2) = "L_Shoulder"
GubbinJoints$(3) = "R_Shoulder"
GubbinJoints$(4) = "L_Forearm"
GubbinJoints$(5) = "R_Forearm"
; Other options
Global HideNametags = 0, DisableCollisions = False
Const nearFadeModifier# = 0.25 ; 500 units
Const farFadeModifier# = 0.75 ; 1500 units
; --- Bounded actor-sound channel pool (issue #489) ------------------------
;
; Per-step actor sounds -- the footstep EmitSound in the client loop and the
; attack/hit/death EmitSound in PlayActorSound below -- were fire-and-forget:
; the channel handle EmitSound returns was discarded and never reclaimed.
; With several actors moving and fighting, the audio backend's sources/
; channels accumulate until allocation fails and the client hard-crashes
; (no RuntimeError, empty Client Log) -- issue #489. The managed SoundZone
; path already avoids this by keeping its handle and ChannelPlaying()-
; checking it; the per-step actor paths did not.
;
; EmitActorSound (defined just above PlayActorSound) routes every actor
; sound through this fixed-size ring of channel handles, StopChannel()-ing
; the slot it is about to reuse, so the live actor-sound channel count is
; capped at ActorSoundPoolSize regardless of session length or actor count.
Const ActorSoundPoolSize = 24
Dim ActorSoundChannel(ActorSoundPoolSize - 1)
Global ActorSoundCursor = 0
; Loads gubbin joint names from file
Function LoadGubbinNames()
F = ReadFile("Data\Game Data\Gubbins.dat")
If F = 0 Then RuntimeError("File not found: " + "Data\Game Data\Gubbins.dat!")
For i = 0 To 5
; Bound joint names against a corrupted Gubbins.dat. Joints are
; short identifiers like "Head", "Hand"; 64 covers any realistic
; mesh joint label.
GubbinJoints$(i) = ReadBoundedString$(F, 64)
Next
CloseFile(F)
End Function
; Sets the weapon mesh
Function SetActorWeapon(AI.ActorInstance, MeshID)
; Free existing mesh if there is one
If AI\WeaponEN <> 0
FreeEntityEmitters(AI\WeaponEN)
FreeEntity(AI\WeaponEN)
AI\WeaponEN = 0
EndIf
If MeshID > -1 And MeshID < 65535
AI\WeaponEN = GetMesh(MeshID)
; Soft-fail: a server (hostile or just out-of-sync) can pick
; any MeshID for the actor's weapon. Previously a missing
; mesh crashed the entire client via RuntimeError. Log and
; leave the slot empty -- the actor appears unarmed until
; the next equipment update.
If AI\WeaponEN = 0
WriteLog(MainLog, "SetActorWeapon: missing weapon mesh ID " + MeshID + " on race '" + AI\Actor\Race$ + "', leaving unequipped")
Return
EndIf
EntityAutoFade(AI\WeaponEN, nearFadeModifier * CameraViewRange, farFadeModifier * CameraViewRange)
RHand = FindChild(AI\EN, "R_Hand")
If RHand = 0
WriteLog(MainLog, "SetActorWeapon: race '" + AI\Actor\Race$ + "' mesh missing 'R_Hand' joint, leaving unequipped")
FreeEntity(AI\WeaponEN) : AI\WeaponEN = 0
Return
EndIf
EntityParent AI\WeaponEN, RHand, False
PositionEntity AI\WeaponEN, LoadedMeshX#(MeshID), LoadedMeshY#(MeshID), LoadedMeshZ#(MeshID)
ScaleEntity AI\WeaponEN, LoadedMeshScales#(MeshID), LoadedMeshScales#(MeshID), LoadedMeshScales#(MeshID)
; Correct rotation for Max models
If AI\TeamID = True Then TurnEntity AI\WeaponEN, 0, 180, 90
CreateEntityEmitters(AI\WeaponEN)
EndIf
End Function
; Sets the shield mesh
Function SetActorShield(AI.ActorInstance, MeshID)
; Free existing mesh if there is one
If AI\ShieldEN <> 0
FreeEntityEmitters(AI\ShieldEN)
FreeEntity(AI\ShieldEN)
AI\ShieldEN = 0
EndIf
If MeshID > -1 And MeshID < 65535
AI\ShieldEN = GetMesh(MeshID)
If AI\ShieldEN = 0
WriteLog(MainLog, "SetActorShield: missing shield mesh ID " + MeshID + " on race '" + AI\Actor\Race$ + "', leaving unequipped")
Return
EndIf
EntityAutoFade(AI\ShieldEN, nearFadeModifier * CameraViewRange, farFadeModifier * CameraViewRange)
LHand = FindChild(AI\EN, "L_Hand")
If LHand = 0
WriteLog(MainLog, "SetActorShield: race '" + AI\Actor\Race$ + "' mesh missing 'L_Hand' joint, leaving unequipped")
FreeEntity(AI\ShieldEN) : AI\ShieldEN = 0
Return
EndIf
EntityParent AI\ShieldEN, LHand, False
PositionEntity AI\ShieldEN, LoadedMeshX#(MeshID), LoadedMeshY#(MeshID), LoadedMeshZ#(MeshID)
ScaleEntity AI\ShieldEN, LoadedMeshScales#(MeshID), LoadedMeshScales#(MeshID), LoadedMeshScales#(MeshID)
; Correct rotation for Max models
If AI\TeamID = True Then TurnEntity AI\ShieldEN, 0, 180, 90
CreateEntityEmitters(AI\ShieldEN)
EndIf
End Function
; Sets the chest mesh
Function SetActorChestArmour(AI.ActorInstance, MeshID)
; Free existing mesh if there is one
If AI\ChestEN <> 0
FreeEntityEmitters(AI\ChestEN)
FreeEntity(AI\ChestEN)
AI\ChestEN = 0
EndIf
If MeshID > -1 And MeshID < 65535
AI\ChestEN = GetMesh(MeshID)
If AI\ChestEN = 0
WriteLog(MainLog, "SetActorChestArmour: missing chest mesh ID " + MeshID + " on race '" + AI\Actor\Race$ + "', leaving unequipped")
Return
EndIf
EntityAutoFade(AI\ChestEN, nearFadeModifier * CameraViewRange, farFadeModifier * CameraViewRange)
Chest = FindChild(AI\EN, "Chest")
If Chest = 0
WriteLog(MainLog, "SetActorChestArmour: race '" + AI\Actor\Race$ + "' mesh missing 'Chest' joint, leaving unequipped")
FreeEntity(AI\ChestEN) : AI\ChestEN = 0
Return
EndIf
EntityParent AI\ChestEN, Chest, False
PositionEntity AI\ChestEN, LoadedMeshX#(MeshID), LoadedMeshY#(MeshID), LoadedMeshZ#(MeshID)
ScaleEntity AI\ChestEN, LoadedMeshScales#(MeshID), LoadedMeshScales#(MeshID), LoadedMeshScales#(MeshID)
; Correct rotation for Max models
If AI\TeamID = True Then TurnEntity AI\ChestEN, 0, 180, 90
CreateEntityEmitters(AI\ChestEN)
EndIf
End Function
; Sets the hair/helmet mesh
Function SetActorHat(AI.ActorInstance, MeshID)
; Free existing mesh if there is one
If AI\HatEN <> 0
FreeEntityEmitters(AI\HatEN)
FreeEntity(AI\HatEN)
AI\HatEN = 0
EndIf
If MeshID > -1 And MeshID < 65535
AI\HatEN = GetMesh(MeshID)
If AI\HatEN = 0
WriteLog(MainLog, "SetActorHat: missing hat mesh ID " + MeshID + " on race '" + AI\Actor\Race$ + "', leaving unequipped")
Return
EndIf
EntityAutoFade(AI\HatEN, nearFadeModifier* CameraViewRange, farFadeModifier * CameraViewRange)
Bonce = FindChild(AI\EN, "Head")
If Bonce = 0
WriteLog(MainLog, "SetActorHat: race '" + AI\Actor\Race$ + "' mesh missing 'Head' joint, leaving unequipped")
FreeEntity(AI\HatEN) : AI\HatEN = 0
Return
EndIf
EntityParent AI\HatEN, Bonce, False
PositionEntity AI\HatEN, LoadedMeshX#(MeshID), LoadedMeshY#(MeshID), LoadedMeshZ#(MeshID)
ScaleEntity AI\HatEN, LoadedMeshScales#(MeshID), LoadedMeshScales#(MeshID), LoadedMeshScales#(MeshID)
; Correct rotation for Max models
If AI\TeamID = True Then TurnEntity AI\HatEN, 0, 180, 90
CreateEntityEmitters(AI\HatEN)
Else
; Bound AI\Hair to the [4]-slot Hair-ID arrays (5 entries each).
; Hair can arrive from the wire (Asc of one byte = 0..255) or a
; saved character (ReadShort = 0..65535); without this guard a
; hostile / out-of-range value reads past MaleHairIDs/FemaleHairIDs
; into adjacent Actor type fields, corrupting the actor template
; in memory on every hair fallback for that race.
If AI\Hair < 0 Or AI\Hair > 4 Then Return
If AI\Gender = 0
ID = AI\Actor\MaleHairIDs[AI\Hair]
Else
ID = AI\Actor\FemaleHairIDs[AI\Hair]
EndIf
If ID > -1 And ID < 65535
AI\HatEN = GetMesh(ID)
If AI\HatEN = 0
WriteLog(MainLog, "SetActorHat: missing hair mesh ID " + ID + " on race '" + AI\Actor\Race$ + "', skipping")
Return
EndIf
EntityAutoFade(AI\HatEN, nearFadeModifier * CameraViewRange, farFadeModifier * CameraViewRange)
Bonce = FindChild(AI\EN, "Head")
If Bonce = 0
WriteLog(MainLog, "SetActorHat (hair fallback): race '" + AI\Actor\Race$ + "' mesh missing 'Head' joint, skipping")
FreeEntity(AI\HatEN) : AI\HatEN = 0
Return
EndIf
EntityParent AI\HatEN, Bonce, False
PositionEntity AI\HatEN, LoadedMeshX#(ID), LoadedMeshY#(ID), LoadedMeshZ#(ID)
ScaleEntity AI\HatEN, LoadedMeshScales#(ID), LoadedMeshScales#(ID), LoadedMeshScales#(ID)
; Correct rotation for Max models
If AI\TeamID = True Then TurnEntity AI\HatEN, 0, 180, 90
CreateEntityEmitters(AI\HatEN)
EndIf
EndIf
End Function
; Loads the 3D stuff for an actor instance
Function LoadActorInstance3D(A.ActorInstance, Scale# = 1.0, SkipAttachments = False, autoFade = True)
A\Actor\Radius# = 0.0
A\CollisionEN = CreatePivot()
; Main mesh and textures
If A\Gender = 0
ActorAnimSet.AnimSet = AnimList(A\Actor\MAnimationSet)
Name$ = GetMeshName$(A\Actor\MeshIDs[0])
; CE model
If Upper$(Left$(Name$, 10)) = "CE\CESAVES"
; ActorAnimSet (the AnimList slot for MAnimationSet) is sparse and can
; be Null when the referenced set was deleted from Animations.dat. The
; later sequence loop (~line 432) and PlayAnimation (Animations.bb:53)
; both guard this, but the CE branch derefs AnimStart[] first -- an
; every-frame client crash at actor spawn for any CE-mesh actor whose
; animset slot is empty. Guard the idle-frame read; frame 0 is the
; consistent degraded state (a Null set extracts no sequences anyway).
If ActorAnimSet <> Null
A\EN = LoadCEMesh(A\Actor\MeshIDs[0], ActorAnimSet\AnimStart[Anim_Idle], ActorAnimSet\AnimStart[Anim_Idle])
Else
A\EN = LoadCEMesh(A\Actor\MeshIDs[0], 0, 0)
EndIf
; Normal model
Else
A\EN = GetMesh(A\Actor\MeshIDs[0], ActorHasMultipleTextures(A\Actor, 0))
If A\EN = 0
FreeEntity(A\CollisionEN)
Return False
EndIf
; Meshes exported from Max have a pointless root pivot - get rid of it
If CountSurfaces(A\EN) < 1
; This is a server field, being used as a flag to say that the mesh is a 3DS Max model.
; I have avoided adding a new field for this to conserve server memory, as both client
; and server use the same Actor type definition.
; It is used when attaching gubbins, as Max meshes have different joint rotations to
; all other meshes.
A\TeamID = True
For i = 1 To CountChildren(A\EN)
EN = GetChild(A\EN, i)
If CountSurfaces(EN) > 0 Then A\EN = EN : Exit
Next
EndIf
EndIf
Scale# = Scale# * LoadedMeshScales#(A\Actor\MeshIDs[0]) * A\Actor\Scale#
EntityParent(A\EN, A\CollisionEN)
MMV.MeshMinMaxVertices = MeshMinMaxVertices(A\EN)
PositionEntity A\EN, 0.0, (MMV\MaxY# - MMV\MinY#) / -2.0, 0.0
FaceTex = A\FaceTex
If A\Actor\MaleFaceIDs[FaceTex] = 65535 And FaceTex > 0 Then FaceTex = 0
BodyTex = A\BodyTex
If A\Actor\MaleBodyIDs[BodyTex] = 65535 And BodyTex > 0 Then BodyTex = 0
If CountSurfaces(A\EN) > 1 And A\Actor\MaleFaceIDs[FaceTex] < 65535
; Find which surface is body
B = GetSurfaceBrush(GetSurface(A\EN, 1)) : T = GetBrushTexture(B)
Name$ = TextureName$(T)
FreeTexture T : FreeBrush(B)
; Default way round
FaceSurface = GetSurface(A\EN, 2)
BodySurface = GetSurface(A\EN, 1)
; Should be the other way round
If Instr(Upper$(Name$), "HEAD") > 0
FaceSurface = GetSurface(A\EN, 1)
BodySurface = GetSurface(A\EN, 2)
Else
B = GetSurfaceBrush(GetSurface(A\EN, 2)) : T = GetBrushTexture(B)
Name$ = Upper$(TextureName$(T))
FreeTexture T : FreeBrush(B)
; A texture is assigned which is not one of the dummy textures, check if it's a body texture
If Instr(Name$, "HEAD") = 0
For i = 0 To 4
Name2$ = Upper$(GetTextureName$(A\Actor\MaleBodyIDs[i]))
If Name2$ <> ""
Name2$ = Left$(Name2$, Len(Name2$) - 1)
If Instr(Name$, Name2$)
FaceSurface = GetSurface(A\EN, 1)
BodySurface = GetSurface(A\EN, 2)
Exit
EndIf
EndIf
Next
EndIf
EndIf
; Paint
B = CreateBrush()
Tex = GetTexture(A\Actor\MaleBodyIDs[BodyTex])
If Tex <> 0
BrushTexture(B, Tex)
PaintSurface(BodySurface, B)
EndIf
Tex = GetTexture(A\Actor\MaleFaceIDs[FaceTex])
If Tex <> 0
BrushTexture(B, Tex)
PaintSurface(FaceSurface, B)
EndIf
FreeBrush(B)
UnloadTexture(A\Actor\MaleBodyIDs[BodyTex])
UnloadTexture(A\Actor\MaleFaceIDs[FaceTex])
Else
Tex = GetTexture(A\Actor\MaleBodyIDs[BodyTex])
If Tex <> 0 Then EntityTexture(A\EN, Tex)
UnloadTexture(A\Actor\MaleBodyIDs[BodyTex])
EndIf
; Beard. Bound A\Beard against the [4]-slot BeardIDs array
; (5 entries). A\Beard arrives from the wire (Asc of one byte =
; 0..255), from a saved character (ReadShort = 0..65535), or
; from a script-driven SetActorBeard call; without this guard a
; hostile / out-of-range value reads past BeardIDs into adjacent
; Actor type fields. Blitz3D's And is not short-circuiting, so
; the bounds check has to be a separate If above the array read.
Local BeardOK = False
If A\Beard >= 0 And A\Beard <= 4
If A\Actor\BeardIDs[A\Beard] > -1 And A\Actor\BeardIDs[A\Beard] < 65535 And SkipAttachments = False
BeardOK = True
EndIf
EndIf
If BeardOK
ID = A\Actor\BeardIDs[A\Beard]
BeardEN = GetMesh(ID, True)
If BeardEN <> 0
Bonce = FindChild(A\EN, "Head")
If Bonce = 0
; Beard is decorative. If the actor mesh has no Head
; joint, just skip it -- no reason to crash the client.
; Mirrors the P_AppearanceUpdate "D" soft-fail in
; ClientNet.bb (PR #130); LoadActorInstance3D is the
; sibling path that runs when an actor first appears
; with a beard configured.
WriteLog(MainLog, "LoadActorInstance3D: race '" + A\Actor\Race$ + "' mesh missing 'Head' joint, skipping beard")
FreeEntity BeardEN
Else
EntityParent BeardEN, Bonce, False
PositionEntity BeardEN, LoadedMeshX#(ID), LoadedMeshY#(ID), LoadedMeshZ#(ID)
ScaleEntity BeardEN, LoadedMeshScales#(ID), LoadedMeshScales#(ID), LoadedMeshScales#(ID)
; Correct rotation for Max models
If A\TeamID = True Then TurnEntity BeardEN, 0, 180, 90
NameEntity(BeardEN, "Beard")
EndIf
EndIf
EndIf
; Get radius
MaxLength# = MeshWidth#(A\EN)
If MeshDepth#(A\EN) > MaxLength# Then MaxLength# = MeshDepth#(A\EN)
A\Actor\Radius# = (MaxLength# * LoadedMeshScales#(A\Actor\MeshIDs[0]) * A\Actor\Scale#) / 2.0
Else
ActorAnimSet.AnimSet = AnimList(A\Actor\FAnimationSet)
Name$ = GetMeshName$(A\Actor\MeshIDs[1])
; CE model
If Upper$(Left$(Name$, 10)) = "CE\CESAVES"
; Null-animset guard -- see the male branch above (AnimList is sparse).
If ActorAnimSet <> Null
A\EN = LoadCEMesh(A\Actor\MeshIDs[1], ActorAnimSet\AnimStart[Anim_Idle], ActorAnimSet\AnimStart[Anim_Idle])
Else
A\EN = LoadCEMesh(A\Actor\MeshIDs[1], 0, 0)
EndIf
; Normal model
Else
A\EN = GetMesh(A\Actor\MeshIDs[1], ActorHasMultipleTextures(A\Actor, 1))
If A\EN = 0
FreeEntity(A\CollisionEN)
Return False
EndIf
If CountSurfaces(A\EN) < 1
; This is a server field, being used as a flag to say that the model is a Max model.
; I have avoided adding a new field for this to conserve server memory, as both client
; and server use the same Actor type definition.
; It is used when attaching gubbins, as Max meshes have different joint rotations to
; all other meshes.
A\TeamID = True
For i = 1 To CountChildren(A\EN)
EN = GetChild(A\EN, i)
If CountSurfaces(EN) > 0 Then A\EN = EN : Exit
Next
EndIf
EndIf
Scale# = Scale# * LoadedMeshScales#(A\Actor\MeshIDs[1]) * A\Actor\Scale#
EntityParent(A\EN, A\CollisionEN)
MMV.MeshMinMaxVertices = MeshMinMaxVertices(A\EN)
PositionEntity A\EN, 0.0, (MMV\MaxY# - MMV\MinY#) / -2.0, 0.0
BodyTex = A\BodyTex
If A\Actor\FemaleBodyIDs[BodyTex] = 65535 And BodyTex > 0 Then BodyTex = 0
FaceTex = A\FaceTex
If A\Actor\FemaleFaceIDs[FaceTex] = 65535 And FaceTex > 0 Then FaceTex = 0
If CountSurfaces(A\EN) > 1 And A\Actor\FemaleFaceIDs[FaceTex] < 65535
; Find which surface is body
B = GetSurfaceBrush(GetSurface(A\EN, 1)) : T = GetBrushTexture(B)
Name$ = TextureName$(T)
FreeTexture T : FreeBrush(B)
FaceSurface = GetSurface(A\EN, 2)
BodySurface = GetSurface(A\EN, 1)
If Instr(Upper$(Name$), "HEAD") > 0
FaceSurface = GetSurface(A\EN, 1)
BodySurface = GetSurface(A\EN, 2)
Else
B = GetSurfaceBrush(GetSurface(A\EN, 2)) : T = GetBrushTexture(B)
Name$ = Upper$(TextureName$(T))
FreeTexture T : FreeBrush(B)
; A texture is assigned which is not one of the dummy textures, check if it's a face texture
If Instr(Name$, "HEAD") = 0
For i = 0 To 5
Name2$ = Upper$(GetTextureName$(A\Actor\FemaleBodyIDs[i]))
If Name2$ <> ""
Name2$ = Left$(Name2$, Len(Name2$) - 1)
If Instr(Name$, Name2$)
FaceSurface = GetSurface(A\EN, 1)
BodySurface = GetSurface(A\EN, 2)
Exit
EndIf
EndIf
Next
EndIf
EndIf
; Paint
B = CreateBrush()
Tex = GetTexture(A\Actor\FemaleBodyIDs[BodyTex])
If Tex <> 0
BrushTexture(B, Tex)
PaintSurface(BodySurface, B)
EndIf
Tex = GetTexture(A\Actor\FemaleFaceIDs[FaceTex])
If Tex <> 0
BrushTexture(B, Tex)
PaintSurface(FaceSurface, B)
EndIf
FreeBrush(B)
UnloadTexture(A\Actor\FemaleBodyIDs[BodyTex])
UnloadTexture(A\Actor\FemaleFaceIDs[FaceTex])
Else
Tex = GetTexture(A\Actor\FemaleBodyIDs[BodyTex])
If Tex <> 0 Then EntityTexture A\EN, Tex
UnloadTexture(A\Actor\FemaleBodyIDs[BodyTex])
EndIf
; Get radius
MaxLength# = MeshWidth#(A\EN)
If MeshDepth#(A\EN) > MaxLength# Then MaxLength# = MeshDepth#(A\EN)
A\Actor\Radius# = (MaxLength# * LoadedMeshScales#(A\Actor\MeshIDs[1]) * A\Actor\Scale#) / 2.0
EndIf
; Animations
If ActorAnimSet <> Null
For i = 0 To 149
If ActorAnimSet\AnimEnd[i] > 0
A\AnimSeqs[i] = ExtractAnimSeq(A\EN, ActorAnimSet\AnimStart[i], ActorAnimSet\AnimEnd[i])
EndIf
Next
EndIf
; Scale
ScaleEntity A\CollisionEN, Scale#, Scale#, Scale#
If SkipAttachments = False
; Attached emitters
CreateEntityEmitters(A\EN)
; Hair
SetActorHat(A, -1)
; Collision
MaxLength# = MMV\MaxX# - MMV\MinX#
If MMV\MaxZ# - MMV\MinZ# > MaxLength# Then MaxLength# = ((MMV\MaxZ# - MMV\MinZ#) + MaxLength#) / 2.0
EntityRadius(A\CollisionEN, (MaxLength# * Scale#) / 2.0, ((MMV\MaxY# - MMV\MinY#) * Scale#) / 2.0)
Width# = (MMV\MaxX# - MMV\MinX#) * Scale#
Height# = (MMV\MaxY# - MMV\MinY#) * Scale#
Depth# = (MMV\MaxZ# - MMV\MinZ#) * Scale#
EntityBox(A\CollisionEN, MMV\MinX# * Scale#, MMV\MinY# * Scale#, MMV\MinZ# * Scale#, Width#, Height#, Depth#)
If A\Actor\PolyCollision = False
EntityType(A\CollisionEN, C_Actor)
Else
EntityType(A\CollisionEN, C_ActorTri1)
EntityType(A\EN, C_ActorTri2)
EndIf
; Shadow [###]
; A\ShadowEN = CreateMesh()
; s = CreateSurface(A\ShadowEN)
; v1 = AddVertex(s, -1.0, 0.0, -1.0, 0.0, 0.0)
; v2 = AddVertex(s, -1.0, 0.0, 1.0, 0.0, 1.0)
; v3 = AddVertex(s, 1.0, 0.0, 1.0, 1.0, 1.0)
; v4 = AddVertex(s, 1.0, 0.0, -1.0, 1.0, 0.0)
; AddTriangle s, v1, v2, v3
; AddTriangle s, v1, v3, v4
; EntityFX A\ShadowEN, 1
; EntityBlend A\ShadowEN, 2
; ScaleEntity A\ShadowEN, (MaxLength# * Scale#) * 0.3, 1, (MaxLength# * Scale#) * 0.3 ; 0.5 for half side * 0.75 to compensate for falloff ; MeshWidth#(A\EN) * Scale# * 0.5, 1.0, MeshDepth#(A\EN) * Scale# * 0.5 ;
; Tex = LoadTexture("Data\Textures\Shadow.bmp")
; If Tex = 0 Then RuntimeError("Could not load Data\Textures\Shadow.bmp!")
; EntityTexture(A\ShadowEN, Tex)
; Debug code to display the collision radius
; CollisionSphere = CreateSphere()
; EntityColor(CollisionSphere, 255, 0, 0)
; EntityAlpha(CollisionSphere, 0.35)
; EntityParent(CollisionSphere, A\CollisionEN, False)
; ScaleEntity(CollisionSphere, MaxLength# / 2.0, (MMV\MaxY# - MMV\MinY#) / 2.0, MaxLength# / 2.0)
; Items
UpdateActorItems(A)
; Nametag
If HideNametags <> 1 Then CreateActorNametag(A)
EndIf
;- Enable AutoFade when not in CharSelection Screen
If autoFade
EntityAutoFade(A\EN, nearFadeModifier * CameraViewRange, farFadeModifier * CameraViewRange)
If BeardEN <> 0 Then EntityAutoFade(BeardEN, nearFadeModifier * CameraViewRange, farFadeModifier * CameraViewRange)
;If A\ShadowEN <> 0 Then EntityAutoFade(A\ShadowEN, nearFadeModifier * CameraViewRange, farFadeModifier * CameraViewRange)
EndIf
; Free MinMaxVertices data
Delete(MMV)
; Type handle
NameEntity A\CollisionEN, Handle(A)
NameEntity A\EN, Handle(A)
Return True
End Function
; Textures an actor instance's nametag entity
Function CreateActorNametag(A.ActorInstance)
If A\NametagEN <> 0 Then FreeEntity(A\NametagEN)
MMV.MeshMinMaxVertices = MeshMinMaxVertices(A\EN)
; Name
A\NametagEN = GY_Create3DText(0.0, 0.0, 1.0, 1.0, Len(A\Name$), GY_TitleFont, 0, 0)
GY_Set3DText(A\NametagEN, A\Name$)
PositionMesh(A\NametagEN, MeshWidth#(A\NametagEN) / -2.0, 1.0, 0.0)
EntityParent(A\NametagEN, A\EN)
ScaleEntity(A\NametagEN, -0.35 * Len(A\Name$), 0.65, 1, True) ;-0.45, 0.75
PositionEntity(A\NametagEN, 0, MMV\MaxY#, 0)
TranslateEntity(A\NametagEN, 0, 0.5, 0, True) ;0, 0.5, 0
EntityAutoFade(A\NametagEN, 70, 75)
If A\RNID = 0
;Adding 3rd color options for name tags Cysis145
If A\Actor\Aggressiveness = 3
EntityColor(A\NametagEN, 0, 174, 14) ;Non Combatant
ElseIf A\Actor\Aggressiveness = 0
EntityColor(A\NametagEN, 255, 219, 114) ; Passive
ElseIf A\Actor\Aggressiveness = 1
EntityColor(A\NametagEN, 255, 219, 114) ;Defensive
Else
EntityColor(A\NametagEN, 168, 0, 0) ; Always Attacks
EndIf
EndIf
; Extra tag (second line)
If A\Tag$ <> ""
Tag$ = "<" + A\Tag$ + ">"
EN = GY_Create3DText(0.0, 0.0, 1.0, 1.0, Len(Tag$), GY_TitleFont, 0, 0)
GY_Set3DText(EN, Tag$)
PositionMesh(EN, MeshWidth#(EN) / -2.0, 1.0, 0.0)
EntityParent(EN, A\NametagEN)
ScaleEntity(EN, -0.3 * Len(Tag$), 0.6, 1, True) ;-0.45, 0.75
PositionEntity(EN, 0, 0, 0)
TranslateEntity(EN, 0.0, -0.5, 0.0, True) ; 0,-0.7
TranslateEntity(A\NametagEN, 0.0, 0.7, 0.0, True) ;0, 0.7, 0
EntityAutoFade(EN, 70, 75)
If A\RNID = 0
;Adding 3rd color options for name tags Cysis145
If A\Actor\Aggressiveness = 3 ;Non combatant
EntityColor(EN, 0, 174, 14)
ElseIf A\Actor\Aggressiveness = 0 ; Passive
EntityColor(EN, 255, 219, 114)
ElseIf A\Actor\Aggressiveness = 1 ; Defensive
EntityColor(EN, 255, 219, 114)
Else
EntityColor(EN, 168, 0, 0) ;Always Attacks
EndIf
EndIf
TranslateEntity(A\NametagEN, 0, 0.5, 0, True)
EndIf
Delete(MMV)
End Function
; Frees the 3D stuff for an actor instance, but leaves the instance
Function FreeActorInstance3D(A.ActorInstance)
For i = 0 To 5
If A\GubbinEN[i] <> 0 Then FreeEntityEmitters(A\GubbinEN[i]) : FreeEntity A\GubbinEN[i] : A\GubbinEN[i] = 0
Next
If A\HatEN <> 0 Then FreeEntityEmitters(A\HatEN) : FreeEntity A\HatEN : A\HatEN = 0
If A\ShieldEN <> 0 Then FreeEntityEmitters(A\ShieldEN) : FreeEntity A\ShieldEN : A\ShieldEN = 0
If A\WeaponEN <> 0 Then FreeEntityEmitters(A\WeaponEN) : FreeEntity A\WeaponEN : A\WeaponEN = 0
If A\ChestEN <> 0 Then FreeEntityEmitters(A\ChestEN) : FreeEntity A\ChestEN : A\ChestEN = 0
;If A\ShadowEN <> 0 Then FreeEntity A\ShadowEN : A\ShadowEN = 0
If A\NametagEN <> 0 Then FreeEntity A\NametagEN : A\NametagEN = 0
If A\EN <> 0
; If no other actor instances are using this mesh, unload it completely
Found = False
For A2.ActorInstance = Each ActorInstance
If A2\Actor\MeshIDs[A2\Gender] = A\Actor\MeshIDs[A\Gender]
If A2 <> A
Found = True
Exit
EndIf
EndIf
Next
If Found = False Then UnloadMesh(A\Actor\MeshIDs[A\Gender])
; Free main mesh
FreeEntityEmitters(A\EN)
FreeEntity(A\EN)
A\EN = 0
EndIf
FreeEntity A\CollisionEN : A\CollisionEN = 0
End Function
; Frees an actor instance and the 3D as well
Function SafeFreeActorInstance(A.ActorInstance)
; Free actor instance
If Handle(A) = PlayerTarget Then PlayerTarget = 0
FreeActorInstance3D(A)
FreeActorInstance(A)
End Function
; Shows a gubbin
Function ShowGubbin(A.ActorInstance, Num, SuppressError = False)
If A\GubbinEN[Num] = 0
ID = A\Actor\MeshIDs[2 + Num]
If ID < 65535
A\GubbinEN[Num] = GetMesh(ID)
If A\GubbinEN[Num] = 0
; A missing gubbin mesh shouldn't crash the client --
; the server controls Race / MeshIDs and could (hostile
; or just out-of-sync) point at a deleted ID. Log and
; leave the gubbin hidden.
WriteLog(MainLog, "ShowGubbin: missing gubbin mesh ID " + ID + " (slot " + Num + ") on race '" + A\Actor\Race$ + "', skipping")
Return
EndIf
EntityAutoFade(A\GubbinEN[Num], nearFadeModifier * CameraViewRange, farFadeModifier * CameraViewRange)
Bone = FindChild(A\EN, GubbinJoints$(Num))
If Bone = 0
WriteLog(MainLog, "ShowGubbin: race '" + A\Actor\Race$ + "' mesh missing joint '" + GubbinJoints$(Num) + "', skipping")
FreeEntity(A\GubbinEN[Num]) : A\GubbinEN[Num] = 0
Return
EndIf
EntityParent(A\GubbinEN[Num], Bone, False)
PositionEntity A\GubbinEN[Num], LoadedMeshX#(ID), LoadedMeshY#(ID), LoadedMeshZ#(ID)
ScaleEntity A\GubbinEN[Num], LoadedMeshScales#(ID), LoadedMeshScales#(ID), LoadedMeshScales#(ID)
; Correct rotation for Max models
If A\TeamID = True Then TurnEntity A\GubbinEN[Num], 0, 180, 90
CreateEntityEmitters(A\GubbinEN[Num])
EndIf
Else
ShowEntity(A\GubbinEN[Num])
CreateEntityEmitters(A\GubbinEN[Num])
EndIf
End Function
; Hides a gubbin
Function HideGubbin(A.ActorInstance, Num)
If A\GubbinEN[Num] <> 0
HideEntity(A\GubbinEN[Num])
FreeEntityEmitters(A\GubbinEN[Num])
EndIf
End Function
; Creates emitters on an entity's children based on names (RECURSIVE)
Function CreateEntityEmitters(E)
For i = 1 To CountChildren(E)
CE = GetChild(E, i)
CreateEntityEmitters(CE)
Name$ = EntityName$(CE)
If Len(Name$) > 4
If Upper$(Left$(Name$, 2)) = "E_"
Name$ = Mid$(Name$, 3)
Pos = Instr(Name$, "_")
If Pos > 0
EmitterName$ = Left$(Name$, Pos - 1)
TextureID = Mid$(Name$, Pos + 1)
Texture = GetTexture(TextureID)
If Texture <> 0 And Len(EmitterName$) > 1
Config = RP_LoadEmitterConfig("Data\Emitter Configs\" + EmitterName$ + ".rpc", Texture, Cam)
If Config <> 0
EmitterEN = RP_CreateEmitter(Config)
If EmitterEN <> 0
EntityParent(EmitterEN, CE, False)
Else
RP_FreeEmitterConfig(Config, False)
UnloadTexture(TextureID)
EndIf
EndIf
EndIf
EndIf
EndIf
EndIf
Next
End Function
; Frees emitters attached to an entity (RECURSIVE)
Function FreeEntityEmitters(E)
For i = 1 To CountChildren(E)
CE = GetChild(E, i)
FreeEntityEmitters(CE)
If Object.RP_Emitter(EntityName$(CE)) <> Null
RP_FreeEmitter(CE, True, False)
EndIf
Next
End Function
; Updates an actor instance's 3D based on what is on their inventory
Function UpdateActorItems(A.ActorInstance)
; Items
If A\Inventory\Items[SlotI_Weapon] <> Null
If A\Gender = 0
SetActorWeapon(A, A\Inventory\Items[SlotI_Weapon]\Item\MMeshID)
Else
SetActorWeapon(A, A\Inventory\Items[SlotI_Weapon]\Item\FMeshID)
EndIf
ElseIf A\WeaponEN <> 0
SetActorWeapon(A, -1)
EndIf
If A\Inventory\Items[SlotI_Shield] <> Null
If A\Gender = 0
SetActorShield(A, A\Inventory\Items[SlotI_Shield]\Item\MMeshID)
Else
SetActorShield(A, A\Inventory\Items[SlotI_Shield]\Item\FMeshID)
EndIf
ElseIf A\ShieldEN <> 0
SetActorShield(A, -1)
EndIf
If A\Inventory\Items[SlotI_Chest] <> Null
If A\Gender = 0
SetActorChestArmour(A, A\Inventory\Items[SlotI_Chest]\Item\MMeshID)
Else
SetActorChestArmour(A, A\Inventory\Items[SlotI_Chest]\Item\FMeshID)
EndIf
ElseIf A\ChestEN <> 0
SetActorChestArmour(A, -1)
EndIf
If A\Inventory\Items[SlotI_Hat] <> Null
If A\Gender = 0
SetActorHat(A, A\Inventory\Items[SlotI_Hat]\Item\MMeshID)
Else
SetActorHat(A, A\Inventory\Items[SlotI_Hat]\Item\FMeshID)
EndIf
ElseIf A\HatEN <> 0
SetActorHat(A, -1)
EndIf
; Gubbins
For i = 0 To 5
HideGubbin(A, i)
Next
For i = 0 To SlotI_Backpack - 1
If A\Inventory\Items[i] <> Null
For j = 0 To 5
If A\Inventory\Items[i]\Item\Gubbins[j] = True
ShowGubbin(A, j)
EndIf
Next
EndIf
Next
End Function
; Emits an actor sound through the bounded recycling channel pool (see the
; pool declaration near the top of this module). Use this for every per-step
; / per-action actor EmitSound so the live channel count stays capped and the
; client can't exhaust the audio backend (issue #489). Returns the channel
; handle (0 if Snd is 0 / unregistered).
Function EmitActorSound%(Snd, EN)
; GetSound() returns 0 for an unregistered/missing sound id -- nothing to
; play, and EmitSound(0, ...) would burn a pool slot on silence.
If Snd = 0 Then Return 0
; Reclaim the slot we are about to overwrite if its previous sound is
; still playing. This StopChannel is what bounds the live channel count.
If ActorSoundChannel(ActorSoundCursor) <> 0
If ChannelPlaying(ActorSoundChannel(ActorSoundCursor))
StopChannel(ActorSoundChannel(ActorSoundCursor))
EndIf
EndIf
Local Ch = EmitSound(Snd, EN)
ActorSoundChannel(ActorSoundCursor) = Ch
ActorSoundCursor = (ActorSoundCursor + 1) Mod ActorSoundPoolSize
Return Ch
End Function
; Plays an actor speech sound
Function PlayActorSound(A.ActorInstance, Speech)
Result = 65535
If A\Gender = 0
If A\Actor\MSpeechIDs[Speech] < 65535 Then Result = A\Actor\MSpeechIDs[Speech]
Else
If A\Actor\FSpeechIDs[Speech] < 65535 Then Result = A\Actor\FSpeechIDs[Speech]
EndIf
If Result < 65535
EN = FindChild(A\EN, "Head")
If EN = 0 Then EN = A\EN
EmitActorSound(GetSound(Result), EN)
EndIf
End Function