/
SpellcastingManager.cpp
2200 lines (1821 loc) · 61.4 KB
/
SpellcastingManager.cpp
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
#include "StdAfx.h"
#include "SpellcastingManager.h"
#include "WeenieObject.h"
#include "World.h"
#include "SpellProjectile.h"
#include "WeenieFactory.h"
#include "InferredPortalData.h"
#include "WClassID.h"
#include "Container.h"
#include "Player.h"
const float MAX_HEADING_TO_TARGET_FOR_CAST = 45.0f;
const float MAX_TURN_TIME_FOR_CAST = 8.0f;
const float MAX_MOTION_TIME_FOR_CAST = 4.0f;
const float MAX_PROJECTILE_CAST_RANGE = 100.0;
CSpellcastingManager::CSpellcastingManager(CWeenieObject *pWeenie)
{
m_pWeenie = pWeenie;
}
CSpellcastingManager::~CSpellcastingManager()
{
}
void CSpellcastingManager::EndCast(int error)
{
m_pWeenie->DoForcedStopCompletely();
m_pWeenie->NotifyUseDone(error);
m_bCasting = false;
m_SpellCastData = SpellCastData();
m_PendingMotions.clear();
m_bTurningToObject = false;
}
bool CSpellcastingManager::ResolveSpellBeingCasted()
{
if (!m_SpellCastData.spell_id)
return false;
CSpellTable *pSpellTable = MagicSystem::GetSpellTable();
if (!pSpellTable)
return false;
m_SpellCastData.spell = pSpellTable->GetSpellBase(m_SpellCastData.spell_id);
if (!m_SpellCastData.spell)
return false;
CSpellTableEx *pSpellTableEx = g_pPortalDataEx->GetSpellTableEx();
if (pSpellTableEx)
m_SpellCastData.spellEx = pSpellTableEx->GetSpellBase(m_SpellCastData.spell_id);
m_SpellCastData.spell_formula = m_SpellCastData.spell->InqSpellFormula();
m_SpellCastData.power_level_of_power_component = m_SpellCastData.spell_formula.GetPowerLevelOfPowerComponent();
m_SpellCastData.current_skill = DetermineSkillLevelForSpell();
m_SpellCastData.max_range = DetermineSpellRange();
if (m_SpellCastData.spell->_bitfield & SelfTargeted_SpellIndex)
{
m_SpellCastData.target_id = m_SpellCastData.source_id;
}
if (m_SpellCastData.equipped && m_SpellCastData.spell->InqTargetType() == ITEM_TYPE::TYPE_ITEM_ENCHANTABLE_TARGET)
{
m_SpellCastData.target_id = m_SpellCastData.caster_id;
}
// item enchantment
if (m_SpellCastData.equipped && (m_SpellCastData.spell->_school == 3) && (m_SpellCastData.spell->_bitfield & SelfTargeted_SpellIndex))
{
m_SpellCastData.target_id = m_SpellCastData.caster_id;
}
return true;
}
std::string appendSpellText(std::string text, std::string newText, SpellComponentType componentType, SpellComponentType lastComponentType)
{
if (!newText.size())
return text;
switch (componentType)
{
default:
case SpellComponentType::Undef_SpellComponentType:
case SpellComponentType::Action_SpellComponentType:
case SpellComponentType::ConceptPrefix_SpellComponentType:
{
if (!text.empty())
text += " ";
text += newText;
break;
}
case SpellComponentType::ConceptSuffix_SpellComponentType:
{
switch (lastComponentType)
{
default:
case SpellComponentType::Undef_SpellComponentType:
case SpellComponentType::Action_SpellComponentType:
if (!text.empty())
text += " ";
text += newText;
break;
case SpellComponentType::ConceptPrefix_SpellComponentType:
case SpellComponentType::ConceptSuffix_SpellComponentType:
newText[0] = ::tolower(newText[0]);
text += newText;
break;
}
break;
}
}
return text;
}
bool CSpellcastingManager::AddMotionsForSpell()
{
SpellComponentTable *pSpellComponents = MagicSystem::GetSpellComponentTable();
if (!pSpellComponents)
return false;
std::string spellWords;
SpellComponentType lastComponentType = Undef_SpellComponentType;
bool firstMotion = true;
for (DWORD i = 0; i < SPELLFORMULA_MAX_COMPS; i++)
{
DWORD comp = m_SpellCastData.spell_formula._comps[i];
if (!comp)
break;
const SpellComponentBase *pSpellComponent = pSpellComponents->InqSpellComponentBase(comp);
if (!pSpellComponent)
return false;
if (pSpellComponent->_category == Scarab_SpellComponentCategory && m_SpellCastData.spell->_bitfield & FastCast_SpellIndex)
continue;
DWORD gesture = pSpellComponent->_gesture;
if (gesture && (gesture & 0xFFFF) != 0)
{
if (gesture == 0x1000012F)
gesture = 0x13000132; // level 7's are wrong for some reason
// m_pWeenie->SendText(csprintf("Component \"%s\": %s %f %f 0x%08X (%u)", pSpellComponent->_name.c_str(), pSpellComponent->_text.c_str(), pSpellComponent->_time, pSpellComponent->_CDM, pSpellComponent->_gesture, pSpellComponent->_gesture & 0xFFFF), 1);
m_PendingMotions.push_back(SpellCastingMotion(gesture, 2.0f, firstMotion, firstMotion, pSpellComponent->_time));
firstMotion = false;
}
if (!pSpellComponent->_text.empty())
{
spellWords = appendSpellText(spellWords, pSpellComponent->_text, pSpellComponent->_type, lastComponentType);
lastComponentType = pSpellComponent->_type;
}
}
if (!spellWords.empty())
{
m_pWeenie->SpeakLocal(spellWords.c_str(), LTT_MAGIC_CASTING_CHANNEL);
}
m_SpellCastData.power_level_of_power_component = m_SpellCastData.spell_formula.GetPowerLevelOfPowerComponent();
return true;
}
void CSpellcastingManager::BeginCast()
{
m_bCasting = true;
m_PendingMotions.clear();
m_bTurningToObject = false;
AddMotionsForSpell();
BeginNextMotion();
}
CWeenieObject *CSpellcastingManager::GetCastTarget()
{
if (!m_bCasting || !m_SpellCastData.target_id)
return NULL;
return g_pWorld->FindObject(m_SpellCastData.target_id);
}
CWeenieObject *CSpellcastingManager::GetCastCaster()
{
// could be a contained item casting a spell on the player
return g_pWorld->FindObject(m_SpellCastData.caster_id);
}
CWeenieObject *CSpellcastingManager::GetCastSource()
{
return g_pWorld->FindObject(m_SpellCastData.source_id);
}
float CSpellcastingManager::HeadingToTarget()
{
if (!m_SpellCastData.target_id)
{
// Untargeted spell
return 0.0;
}
CWeenieObject *pTarget = GetCastTarget();
if (!pTarget)
{
// Don't know where the target is
EndCast(WERROR_OBJECT_GONE);
return 0.0;
}
if (pTarget->parent)
{
// Target has a parent (in inventory?)
// This needs additional logic
return 0.0;
}
// Return difference in heading.
float fHeadingToTarget = m_pWeenie->m_Position.heading_diff(pTarget->m_Position);
fHeadingToTarget = fabs(fHeadingToTarget);
if (fHeadingToTarget >= 180.0)
fHeadingToTarget = 360.0 - fHeadingToTarget;
return fHeadingToTarget;
}
bool CSpellcastingManager::MotionRequiresHeading()
{
if (!m_SpellCastData.target_id || m_SpellCastData.target_id == m_SpellCastData.caster_id || m_SpellCastData.target_id == m_SpellCastData.source_id)
return false;
if (CWeenieObject *target = g_pWorld->FindObject(m_SpellCastData.target_id))
{
if (CWeenieObject *owner = target->GetWorldTopLevelOwner())
{
if (owner->GetID() == m_SpellCastData.source_id)
return false;
}
}
if (m_PendingMotions.empty())
return true; // last motion requires heading
return m_PendingMotions.begin()->requiresHeading;
}
void CSpellcastingManager::BeginNextMotion()
{
if (m_pWeenie->IsDead())
{
EndCast(0);
return;
}
bool bNeedsTurnToObject = false;
if (MotionRequiresHeading() && HeadingToTarget() > MAX_HEADING_TO_TARGET_FOR_CAST)
{
bNeedsTurnToObject = true;
}
if (!m_bCasting)
{
// Must have errored...
return;
}
if (bNeedsTurnToObject)
{
m_bTurningToObject = true;
MovementParameters params;
params.speed = 1.0f;
params.action_stamp = ++m_pWeenie->m_wAnimSequence;
params.modify_interpreted_state = 0;
m_pWeenie->last_move_was_autonomous = false;
m_pWeenie->cancel_moveto();
m_pWeenie->TurnToObject(m_SpellCastData.target_id, ¶ms);
m_SpellCastData.cast_timeout = Timer::cur_time + MAX_TURN_TIME_FOR_CAST;
}
else
{
m_bTurningToObject = false;
if (m_PendingMotions.empty())
{
int error = LaunchSpellEffect();
EndCast(error);
}
else
{
m_SpellCastData.cast_timeout = Timer::cur_time + MAX_MOTION_TIME_FOR_CAST;
m_pWeenie->StopCompletely(0);
SpellCastingMotion &cast_motion = *m_PendingMotions.begin();
MovementParameters params;
params.action_stamp = ++m_pWeenie->m_wAnimSequence;
params.speed = cast_motion.speed;
MovementStruct mvs;
mvs.type = MovementTypes::RawCommand;
mvs.motion = cast_motion.motion;
mvs.params = ¶ms;
m_pWeenie->last_move_was_autonomous = false;
int errorCode;
if (!(errorCode = m_pWeenie->PerformMovement(mvs)))
{
m_fNextCastTime = Timer::cur_time + (cast_motion.min_time * 0.5f);
m_pWeenie->_server_control_timestamp++;
m_pWeenie->Animation_Update();
}
else
{
// m_pWeenie->PerformMovement(mvs);
EndCast(WERROR_MAGIC_GENERAL_FAILURE);
}
}
}
}
Position CSpellcastingManager::GetSpellProjectileSpawnPosition(CSpellProjectile *pProjectile, CWeenieObject *pTarget, float *pDistToTarget)
{
bool bArc = pProjectile->InqBoolQuality(GRAVITY_STATUS_BOOL, FALSE) ? true : false;
CWeenieObject *pSource = GetCastSource();
Position spawnPosition = pSource->m_Position.add_offset(Vector(0, 0, pSource->GetHeight() * (bArc ? 1.0 : (2.0 / 3.0))));
Vector targetOffset = spawnPosition.get_offset(pTarget->m_Position.add_offset(Vector(0, 0, pTarget->GetHeight() * (bArc ? (5.0 / 6.0) : (0.5)))));
Vector targetDir = targetOffset;
if (targetDir.normalize_check_small())
{
targetDir = spawnPosition.frame.get_vector_heading();
// spawnPosition.frame.m_origin += targetDir * minSpawnDist;
spawnPosition.frame.set_vector_heading(targetDir);
*pDistToTarget = 0.0f;
}
else
{
float minSpawnDist = (pSource->GetRadius() + pProjectile->GetRadius()) + 0.1f;
spawnPosition.frame.m_origin += targetDir * minSpawnDist;
spawnPosition.frame.set_vector_heading(targetDir);
*pDistToTarget = targetOffset.magnitude();
}
return spawnPosition;
}
Vector CSpellcastingManager::GetSpellProjectileSpawnVelocity(Position *pSpawnPosition, CWeenieObject *pTarget, float speed, bool tracked, bool gravity, Vector *pTargetDir)
{
Vector targetOffset = pSpawnPosition->get_offset(pTarget->m_Position.add_offset(Vector(0, 0, pTarget->GetHeight() * 0.5f)));
double targetDist = targetOffset.magnitude();
if (!tracked)
{
double t = targetDist / speed;
Vector v = targetOffset / t;
if (gravity)
v.z += (9.8*t) / 2.0f;
Vector targetDir = v;
targetDir.normalize();
if (pTargetDir)
*pTargetDir = targetDir;
return v;
}
Vector P0 = targetOffset;
Vector P1(0, 0, 0);
float s0 = pTarget->get_velocity().magnitude();
Vector V0 = pTarget->get_velocity();
if (V0.normalize_check_small())
V0 = Vector(0, 0, 0);
float s1 = speed;
double a = (V0.x * V0.x) + (V0.y * V0.y) - (s1 * s1);
double b = 2 * ((P0.x * V0.x) + (P0.y * V0.y) - (P1.x * V0.x) - (P1.y * V0.y));
double c = (P0.x * P0.x) + (P0.y * P0.y) + (P1.x * P1.x) + (P1.y * P1.y) - (2 * P1.x * P0.x) - (2 * P1.y * P0.y);
double t1 = (-b + sqrt((b * b) - (4 * a * c))) / (2 * a);
double t2 = (-b - sqrt((b * b) - (4 * a * c))) / (2 * a);
if (t1 < 0)
t1 = FLT_MAX;
if (t2 < 0)
t2 = FLT_MAX;
double t = min(t1, t2);
if (t >= 100.0)
{
return GetSpellProjectileSpawnVelocity(pSpawnPosition, pTarget, speed, false, gravity, pTargetDir);
}
Vector v;
v.x = (P0.x + (t * s0 * V0.x)) / (t); // * s1);
v.y = (P0.y + (t * s0 * V0.y)) / (t); // * s1);
v.z = (P0.z + (t * s0 * V0.z)) / (t); // * s1);
if (gravity)
{
v.z += (9.8*t) / 2.0f;
}
if (pTargetDir)
{
Vector targetDir = v;
if (targetDir.normalize_check_small())
targetDir = Vector(0, 0, 0);
*pTargetDir = targetDir;
}
return v;
}
const double MIN_BOLT_VELOCITY = 5.0;
const double MAX_BOLT_VELOCITY = 20.0;
double CalculateBoltVelocityByDistance(double distance)
{
return MIN_BOLT_VELOCITY + ((MAX_BOLT_VELOCITY-MIN_BOLT_VELOCITY) * min(1.0, max(0.0, (distance / 15.0))));
}
int CSpellcastingManager::LaunchRingProjectiles(DWORD wcid)
{
DWORD damage = 0;
switch (m_SpellCastData.power_level_of_power_component)
{
case 1: damage = Random::GenUInt(16, 31); break;
case 2: damage = Random::GenUInt(18, 35); break;
case 3: damage = Random::GenUInt(21, 42); break;
case 4: damage = Random::GenUInt(25, 50); break;
case 5: damage = Random::GenUInt(29, 59); break;
case 6: damage = Random::GenUInt(36, 71); break;
case 7:
case 8: damage = Random::GenUInt(42, 84); break;
case 9:
case 10: damage = Random::GenUInt(47, 94); break;
}
float velocity = 1.0f;
bool tracking = true;
bool gravity = false;
const int NUM_RING_PROJECTILES = 4;
for (int i = 0; i < NUM_RING_PROJECTILES; i++)
{
CSpellProjectile *pProjectile = new CSpellProjectile(m_SpellCastData, m_SpellCastData.target_id, damage);
// spawn default object properties
g_pWeenieFactory->ApplyWeenieDefaults(pProjectile, wcid);
// set spell id (is this even needed?)
pProjectile->SetSpellID(m_SpellCastData.spell_id);
double rads = ((i * 2) / (double)NUM_RING_PROJECTILES) * PI;
Vector dir(sin(rads), cos(rads), 0.0f);
float velocity = 2.0f;
// create the initial object
Position projSpawnPos = GetCastSource()->m_Position;
projSpawnPos.frame.set_heading(rads);
pProjectile->SetInitialPosition(projSpawnPos);
pProjectile->InitPhysicsObj();
pProjectile->m_Position = pProjectile->m_Position.add_offset(Vector(
(GetCastSource()->GetRadius() + pProjectile->GetRadius() + F_EPSILON) * dir.x,
(GetCastSource()->GetRadius() + pProjectile->GetRadius() + F_EPSILON) * dir.y, /*1.223465f*/0.8f));
/*
m_pWeenie->SendText(
csprintf("DEBUG: WeenieHeight/Radius: %f %f ProjHeight/Radius: %f %f",
m_pWeenie->GetHeight(), m_pWeenie->GetRadius(), pProjectile->GetHeight(), pProjectile->GetRadius()), LTT_ALL_CHANNELS);
*/
// set velocity
pProjectile->set_velocity(dir * velocity, 0);
// insert the object into the world
g_pWorld->CreateEntity(pProjectile);
// launch particle effect
pProjectile->EmitEffect(PS_Launch, 0.4f);
}
return WERROR_NONE;
}
bool CSpellcastingManager::LaunchProjectileSpell(ProjectileSpellEx *meta)
{
if (!meta)
return false;
CWeenieObject *pTarget = GetCastTarget();
if (!pTarget || !pTarget->InValidCell())
{
// Don't know where the target is
return false;
}
int numX = (int)(meta->_dims.x + 0.5f);
int numY = (int)(meta->_dims.y + 0.5f);
int numZ = (int)(meta->_dims.z + 0.5f);
for (int x = 0; x < numX; x++)
{
for (int y = 0; y < numY; y++)
{
for (int z = 0; z < numZ; z++)
{
DWORD damage = Random::GenUInt(meta->_baseIntensity, meta->_baseIntensity + meta->_variance);
CSpellProjectile *pProjectile = new CSpellProjectile( // revise me
m_SpellCastData, meta->_numProjectiles > 1 ? 0 : m_SpellCastData.target_id, damage);
// create the initial object
float distToTarget;
// spawn default object properties
g_pWeenieFactory->ApplyWeenieDefaults(pProjectile, meta->_wcid);
pProjectile->m_Qualities.SetInt(DAMAGE_TYPE_INT, meta->_etype);
// set spell id (is this even needed?)
pProjectile->SetSpellID(m_SpellCastData.spell_id);
pProjectile->SetInitialPosition(GetCastSource()->m_Position);
pProjectile->InitPhysicsObj();
pProjectile->m_PhysicsState |= MISSILE_PS|REPORT_COLLISIONS_PS;
pProjectile->m_PhysicsState &= ~IGNORE_COLLISIONS_PS;
Position projSpawnPos = GetSpellProjectileSpawnPosition(pProjectile, pTarget, &distToTarget);
Vector createOffset = projSpawnPos.localtoglobalvec(meta->_createOffset);
projSpawnPos = projSpawnPos.add_offset(
Vector(
Random::GenFloat(-1.0, 1.0) * meta->_peturbation.x * meta->_padding.x,
Random::GenFloat(-1.0, 1.0) * meta->_peturbation.y * meta->_padding.y,
Random::GenFloat(-1.0, 1.0) * meta->_peturbation.z * meta->_padding.z));
projSpawnPos = projSpawnPos.add_offset(createOffset);
Position spawnSourcePosition = projSpawnPos;
float radius = pProjectile->GetRadius();
Vector sizePerProjectile = meta->_padding; // * radius;
Vector projectileGroupOffset(x, y, z);
projectileGroupOffset.x *= sizePerProjectile.x;
projectileGroupOffset.y *= sizePerProjectile.y;
projectileGroupOffset.z *= sizePerProjectile.z;
projectileGroupOffset.x -= sizePerProjectile.x * ((meta->_dims.x - 1.0) / 2.0);
projectileGroupOffset.y -= sizePerProjectile.y * ((meta->_dims.y - 1.0) / 2.0);
projectileGroupOffset.z -= sizePerProjectile.z * ((meta->_dims.z - 1.0) / 2.0);
projectileGroupOffset = projSpawnPos.localtoglobalvec(projectileGroupOffset);
projSpawnPos = projSpawnPos.add_offset(projectileGroupOffset);
// set spell id (is this even needed?)
pProjectile->m_Position = projSpawnPos;
double maxVelocity = 5.0;
pProjectile->m_Qualities.InqFloat(MAXIMUM_VELOCITY_FLOAT, maxVelocity);
bool bGravity = false;
bool bTracking = !meta->_bNonTracking;
if (pProjectile->InqBoolQuality(GRAVITY_STATUS_BOOL, FALSE))
{
pProjectile->m_PhysicsState |= GRAVITY_PS;
bGravity = true;
}
if (pProjectile->InqBoolQuality(INELASTIC_BOOL, FALSE))
pProjectile->m_PhysicsState |= INELASTIC_PS;
if (pProjectile->InqBoolQuality(SCRIPTED_COLLISION_BOOL, FALSE))
pProjectile->m_PhysicsState |= SCRIPTED_COLLISION_PS;
if (pProjectile->InqBoolQuality(LIGHTS_STATUS_BOOL, FALSE))
pProjectile->m_PhysicsState |= LIGHTING_ON_PS;
// pProjectile->m_PhysicsState |= ETHEREAL_PS;
// set velocity
Vector spawnVelocity = GetSpellProjectileSpawnVelocity(&spawnSourcePosition, pTarget, maxVelocity, bTracking, bGravity, NULL);
if (meta->_spreadAngle > 0)
{
if (numX > 1)
{
double xRatio = x / (double)(numX - 1);
double theta = DEG2RAD((xRatio - 0.5) * meta->_spreadAngle);
double cs = cos(theta);
double sn = sin(theta);
double px = spawnVelocity.x * cs - spawnVelocity.y * sn;
double py = spawnVelocity.x * sn + spawnVelocity.y * cs;
spawnVelocity.x = px;
spawnVelocity.y = py;
}
}
pProjectile->set_velocity(spawnVelocity, 0);
// insert the object into the world
if (g_pWorld->CreateEntity(pProjectile))
{
// launch particle effect
pProjectile->EmitEffect(4, 0.8f);
/*
LOG(Temp, Normal, "Projectile Offset @ %.3f %.3f %.3f %f\n",
createOffset.x + projectileGroupOffset.x,
createOffset.y + projectileGroupOffset.y,
createOffset.z + projectileGroupOffset.z, pProjectile->m_velocityVector.magnitude());
*/
}
}
}
}
// LOG(Temp, Normal, "-\n");
return true;
}
void CSpellcastingManager::PerformCastParticleEffects()
{
if (!m_bCasting)
return;
// self effect
if (m_SpellCastData.spell->_caster_effect)
{
GetCastSource()->EmitEffect(m_SpellCastData.spell->_caster_effect, max(0.0, min(1.0, (m_SpellCastData.power_level_of_power_component - 1.0) / 7.0)));
}
// target effect
if (m_SpellCastData.spell->_target_effect)
{
if (CWeenieObject *pTarget = GetCastTarget())
pTarget->EmitEffect(m_SpellCastData.spell->_target_effect, max(0.0, min(1.0, (m_SpellCastData.power_level_of_power_component - 1.0) / 7.0)));
}
}
void CSpellcastingManager::BeginPortalSend(const Position &targetPos)
{
if (CPlayerWeenie *player = m_pWeenie->AsPlayer())
{
if (!player->IsRecalling())
{
player->BeginRecall(targetPos);
}
}
else
{
m_pWeenie->Movement_Teleport(targetPos, false);
}
}
int CSpellcastingManager::LaunchSpellEffect()
{
if (int targetError = CheckTargetValidity() && m_SpellCastData.range_check)
{
switch (targetError)
{
case WERROR_MAGIC_TARGET_OUT_OF_RANGE:
{
CWeenieObject *pTarget = GetCastTarget();
if (pTarget)
pTarget->SendText(csprintf("%s tried to cast a spell on you, but was too far away!", m_pWeenie->GetName().c_str()), LTT_MAGIC);
m_pWeenie->SendText("That target is too far away!", LTT_MAGIC);
break;
}
}
return targetError;
}
// cast source is assumed to be valid at this point
if (m_pWeenie->AsPlayer() && m_SpellCastData.uses_mana)
{
double chance = GetMagicSkillChance(m_SpellCastData.current_skill, m_SpellCastData.spell->_power);
if (chance < Random::RollDice(0.0, 1.0))
{
// fizzle
m_pWeenie->EmitEffect(PS_Fizzle, 0.542734265f);
m_pWeenie->AdjustMana(-5);
return WERROR_MAGIC_FIZZLE;
}
if (m_pWeenie->m_Position.distance(m_SpellCastData.initial_cast_position) >= 6.0)
{
// fizzle
m_pWeenie->EmitEffect(PS_Fizzle, 0.542734265f);
m_pWeenie->AdjustMana(-5);
m_pWeenie->SendText("Your movement disrupted spell casting!", LTT_MAGIC);
return WERROR_MAGIC_FIZZLE;
}
m_pWeenie->AdjustMana(-GenerateManaCost());
}
bool bSpellPerformed = false;
if (m_SpellCastData.spellEx && m_SpellCastData.spellEx->_meta_spell._spell)
{
switch (m_SpellCastData.spellEx->_meta_spell._sp_type)
{
case SpellType::Transfer_SpellType:
{
TransferSpellEx *meta = (TransferSpellEx *)m_SpellCastData.spellEx->_meta_spell._spell;
bSpellPerformed = DoTransferSpell(GetCastTarget(), meta);
break;
}
case SpellType::Boost_SpellType:
{
BoostSpellEx *meta = (BoostSpellEx *)m_SpellCastData.spellEx->_meta_spell._spell;
int boostMin = meta->_boost;
int boostMax = meta->_boost + meta->_boostVariance;
if (boostMin > boostMax)
{
int swapper = boostMin;
boostMin = boostMax;
boostMax = swapper;
}
bSpellPerformed = AdjustVital(GetCastTarget(), Random::GenInt(boostMin, boostMax), meta->_dt);
break;
}
case SpellType::Dispel_SpellType:
{
DispelSpellEx *meta = (DispelSpellEx *)m_SpellCastData.spellEx->_meta_spell._spell;
int minNum = (int) ((meta->_number * meta->_number_variance) + F_EPSILON);
int maxNum = (int) ((meta->_number * (1.0 / meta->_number_variance)) + F_EPSILON);
int numToDispel = Random::GenInt(minNum, maxNum);
CWeenieObject *pTarget = GetCastTarget();
if (pTarget)
{
if (!(m_SpellCastData.spell->_bitfield & Beneficial_SpellIndex))
{
if (pTarget->GetWorldTopLevelOwner()->ImmuneToDamage(m_pWeenie))
{
break;
}
}
PackableListWithJson<DWORD> possibleToDispel;
if (pTarget->m_Qualities._enchantment_reg)
{
if (pTarget->m_Qualities._enchantment_reg->_add_list)
{
for (auto &entry : *pTarget->m_Qualities._enchantment_reg->_add_list)
{
if (entry._power_level > meta->_max_power)
continue;
if (entry._power_level < meta->_min_power)
continue;
if (entry._duration <= 0.0)
continue;
if ((entry._id & 0xFFFF) == 666) // vitae
continue;
if (CSpellTableEx *pSpellTableEx = g_pPortalDataEx->GetSpellTableEx())
{
if (const CSpellBaseEx *spellBaseEx = pSpellTableEx->GetSpellBase(entry._id & 0xFFFF))
{
if (meta->_school && meta->_school != spellBaseEx->_school)
continue;
switch (meta->_align)
{
case 0: // neutral
{
break;
}
case 1: // good only
{
if (!(spellBaseEx->_bitfield & Beneficial_SpellIndex))
continue;
break;
}
case 2: // bad only
{
if (spellBaseEx->_bitfield & Beneficial_SpellIndex)
continue;
break;
}
}
possibleToDispel.push_back(entry._id);
}
}
}
}
if (pTarget->m_Qualities._enchantment_reg->_mult_list)
{
for (auto &entry : *pTarget->m_Qualities._enchantment_reg->_mult_list)
{
if (entry._power_level > meta->_max_power)
continue;
if (entry._power_level < meta->_min_power)
continue;
if (entry._duration <= 0.0)
continue;
if ((entry._id & 0xFFFF) == 666) // vitae
continue;
if (CSpellTableEx *pSpellTableEx = g_pPortalDataEx->GetSpellTableEx())
{
if (const CSpellBaseEx *spellBaseEx = pSpellTableEx->GetSpellBase(entry._id & 0xFFFF))
{
if (meta->_school && meta->_school != spellBaseEx->_school)
continue;
switch (meta->_align)
{
case 0: // neutral
{
break;
}
case 1: // good only
{
if (!(spellBaseEx->_bitfield & Beneficial_SpellIndex))
continue;
break;
}
case 2: // bad only
{
if (spellBaseEx->_bitfield & Beneficial_SpellIndex)
continue;
break;
}
}
possibleToDispel.push_back(entry._id);
}
}
}
}
}
PackableListWithJson<DWORD> listToDispel;
if (meta->_number < 0)
{
// dispel all
listToDispel = possibleToDispel;
}
else
{
while (numToDispel > 0 && !possibleToDispel.empty())
{
std::list<DWORD>::iterator randomEntry = possibleToDispel.begin();
std::advance(randomEntry, Random::GenUInt(0, (DWORD)(possibleToDispel.size() - 1)));
listToDispel.push_back(*randomEntry);
possibleToDispel.erase(randomEntry);
numToDispel--;
}
}
std::string spellNames;
for (auto entry : listToDispel)
{
if (CSpellTableEx *pSpellTableEx = g_pPortalDataEx->GetSpellTableEx())
{
if (const CSpellBaseEx *spellBaseEx = pSpellTableEx->GetSpellBase(entry & 0xFFFF))
{
if (spellNames.empty())
{
spellNames = spellBaseEx->_name;
}
else
{
spellNames += ", ";
spellNames += spellBaseEx->_name;
}
}
}
}
// "You cast Incantation of Nullify All Magic Self on yourself and dispel: ..."
// "Tusker's Friend casts Nullify All Magic Other on you, but the dispel fails."
if (listToDispel.size() > 0)
{
if (pTarget == m_pWeenie)
{
m_pWeenie->SendText(csprintf("You cast %s on yourself and dispel: %s", m_SpellCastData.spell->_name.c_str(), spellNames.c_str()), LTT_MAGIC);
}
else
{
m_pWeenie->SendText(csprintf("You cast %s on %s and dispel: %s", m_SpellCastData.spell->_name.c_str(), pTarget->GetName().c_str(), spellNames.c_str()), LTT_MAGIC);
pTarget->SendText(csprintf("%s casts %s on you and dispels: %s", m_pWeenie->GetName().c_str(), m_SpellCastData.spell->_name.c_str(), spellNames.c_str()), LTT_MAGIC);
}
if (pTarget->m_Qualities._enchantment_reg)
{
pTarget->m_Qualities._enchantment_reg->RemoveEnchantments(&listToDispel);
BinaryWriter expireMessage;
expireMessage.Write<DWORD>(0x2C8);
listToDispel.Pack(&expireMessage);
pTarget->SendNetMessage(&expireMessage, PRIVATE_MSG, TRUE, FALSE);
}
}
else
{
if (pTarget == m_pWeenie)
{
m_pWeenie->SendText(csprintf("You cast %s on yourself, but the dispel fails.", m_SpellCastData.spell->_name.c_str()), LTT_MAGIC);
}
else
{
m_pWeenie->SendText(csprintf("You cast %s on %s, but the dispel fails.", m_SpellCastData.spell->_name.c_str(), pTarget->GetName().c_str()), LTT_MAGIC);
pTarget->SendText(csprintf("%s casts %s on you, but the dispel fails.", m_pWeenie->GetName().c_str(), m_SpellCastData.spell->_name.c_str()), LTT_MAGIC);
}
}
bSpellPerformed = true;
}
break;
}
case SpellType::PortalLink_SpellType:
{
if (m_pWeenie->HasOwner())
break;
PortalLinkSpellEx *meta = (PortalLinkSpellEx *)m_SpellCastData.spellEx->_meta_spell._spell;
CWeenieObject *pTarget = GetCastTarget();
if (pTarget)
{
// portal bitmask, 0x10 = cannot be summoned
// portal bitmask, 0x20 = cannot be linked/recalled
switch (meta->_index)
{
case 1:
if (pTarget->m_Qualities.m_WeenieType == LifeStone_WeenieType)
{
m_pWeenie->m_Qualities.SetPosition(LINKED_LIFESTONE_POSITION, m_pWeenie->m_Position);
bSpellPerformed = true;
}
else
{
m_pWeenie->SendText("You cannot link that.", LTT_MAGIC);
}
break;
case 2: // primary portal tie
if (pTarget->m_Qualities.m_WeenieType == Portal_WeenieType &&
!(pTarget->m_Qualities.GetInt(PORTAL_BITMASK_INT, 0) & 0x20))