forked from opentibiabr/canary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombat.cpp
2198 lines (1833 loc) · 70.7 KB
/
combat.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
/**
* Canary - A free and open-source MMORPG server emulator
* Copyright (©) 2019-2024 OpenTibiaBR <opentibiabr@outlook.com>
* Repository: https://github.com/opentibiabr/canary
* License: https://github.com/opentibiabr/canary/blob/main/LICENSE
* Contributors: https://github.com/opentibiabr/canary/graphs/contributors
* Website: https://docs.opentibiabr.com/
*/
#include "pch.hpp"
#include "declarations.hpp"
#include "creatures/combat/combat.hpp"
#include "lua/creature/events.hpp"
#include "creatures/players/wheel/player_wheel.hpp"
#include "game/game.hpp"
#include "game/scheduling/dispatcher.hpp"
#include "io/iobestiary.hpp"
#include "creatures/monsters/monster.hpp"
#include "creatures/monsters/monsters.hpp"
#include "items/weapons/weapons.hpp"
#include "map/spectators.hpp"
#include "lib/metrics/metrics.hpp"
#include "lua/callbacks/event_callback.hpp"
#include "lua/callbacks/events_callbacks.hpp"
int32_t Combat::getLevelFormula(std::shared_ptr<Player> player, const std::shared_ptr<Spell> wheelSpell, const CombatDamage &damage) const {
if (!player) {
return 0;
}
uint32_t magicLevelSkill = player->getMagicLevel();
// Wheel of destiny - Runic Mastery
if (player->wheel()->getInstant("Runic Mastery") && wheelSpell && damage.instantSpellName.empty() && normal_random(0, 100) <= 25) {
const auto conjuringSpell = g_spells().getInstantSpellByName(damage.runeSpellName);
if (conjuringSpell && conjuringSpell != wheelSpell) {
uint32_t castResult = conjuringSpell->canCast(player) ? 20 : 10;
magicLevelSkill += magicLevelSkill * castResult / 100;
}
}
int32_t levelFormula = player->getLevel() * 2 + (player->getMagicLevel() + player->getSpecializedMagicLevel(damage.primary.type, true)) * 3;
return levelFormula;
}
CombatDamage Combat::getCombatDamage(std::shared_ptr<Creature> creature, std::shared_ptr<Creature> target) const {
CombatDamage damage;
damage.origin = params.origin;
damage.primary.type = params.combatType;
damage.instantSpellName = instantSpellName;
damage.runeSpellName = runeSpellName;
// Wheel of destiny
std::shared_ptr<Spell> wheelSpell = nullptr;
std::shared_ptr<Player> attackerPlayer = creature ? creature->getPlayer() : nullptr;
if (attackerPlayer) {
wheelSpell = attackerPlayer->wheel()->getCombatDataSpell(damage);
}
// End
if (formulaType == COMBAT_FORMULA_DAMAGE) {
damage.primary.value = normal_random(
static_cast<int32_t>(mina),
static_cast<int32_t>(maxa)
);
} else if (creature) {
int32_t min, max;
if (creature->getCombatValues(min, max)) {
damage.primary.value = normal_random(min, max);
} else if (std::shared_ptr<Player> player = creature->getPlayer()) {
if (params.valueCallback) {
params.valueCallback->getMinMaxValues(player, damage, params.useCharges);
} else if (formulaType == COMBAT_FORMULA_LEVELMAGIC) {
int32_t levelFormula = getLevelFormula(player, wheelSpell, damage);
damage.primary.value = normal_random(
static_cast<int32_t>(levelFormula * mina + minb),
static_cast<int32_t>(levelFormula * maxa + maxb)
);
} else if (formulaType == COMBAT_FORMULA_SKILL) {
std::shared_ptr<Item> tool = player->getWeapon();
const WeaponShared_ptr weapon = g_weapons().getWeapon(tool);
if (weapon) {
damage.primary.value = normal_random(
static_cast<int32_t>(minb),
static_cast<int32_t>(weapon->getWeaponDamage(player, target, tool, true) * maxa + maxb)
);
damage.secondary.type = weapon->getElementType();
damage.secondary.value = weapon->getElementDamage(player, target, tool);
if (params.useCharges) {
auto charges = tool->getAttribute<uint16_t>(ItemAttribute_t::CHARGES);
if (charges != 0) {
g_game().transformItem(tool, tool->getID(), charges - 1);
}
}
} else {
damage.primary.value = normal_random(
static_cast<int32_t>(minb),
static_cast<int32_t>(maxb)
);
}
}
}
if (attackerPlayer && wheelSpell && wheelSpell->isInstant()) {
wheelSpell->getCombatDataAugment(attackerPlayer, damage);
}
}
return damage;
}
void Combat::getCombatArea(const Position ¢erPos, const Position &targetPos, const std::unique_ptr<AreaCombat> &area, std::vector<std::shared_ptr<Tile>> &list) {
if (targetPos.z >= MAP_MAX_LAYERS) {
return;
}
if (area) {
area->getList(centerPos, targetPos, list);
} else {
list.emplace_back(g_game().map.getOrCreateTile(targetPos));
}
}
CombatType_t Combat::ConditionToDamageType(ConditionType_t type) {
switch (type) {
case CONDITION_FIRE:
return COMBAT_FIREDAMAGE;
case CONDITION_ENERGY:
return COMBAT_ENERGYDAMAGE;
case CONDITION_BLEEDING:
return COMBAT_PHYSICALDAMAGE;
case CONDITION_DROWN:
return COMBAT_DROWNDAMAGE;
case CONDITION_POISON:
return COMBAT_EARTHDAMAGE;
case CONDITION_FREEZING:
return COMBAT_ICEDAMAGE;
case CONDITION_DAZZLED:
return COMBAT_HOLYDAMAGE;
case CONDITION_CURSED:
return COMBAT_DEATHDAMAGE;
default:
break;
}
return COMBAT_NONE;
}
ConditionType_t Combat::DamageToConditionType(CombatType_t type) {
switch (type) {
case COMBAT_FIREDAMAGE:
return CONDITION_FIRE;
case COMBAT_ENERGYDAMAGE:
return CONDITION_ENERGY;
case COMBAT_DROWNDAMAGE:
return CONDITION_DROWN;
case COMBAT_EARTHDAMAGE:
return CONDITION_POISON;
case COMBAT_ICEDAMAGE:
return CONDITION_FREEZING;
case COMBAT_HOLYDAMAGE:
return CONDITION_DAZZLED;
case COMBAT_DEATHDAMAGE:
return CONDITION_CURSED;
case COMBAT_PHYSICALDAMAGE:
return CONDITION_BLEEDING;
default:
return CONDITION_NONE;
}
}
bool Combat::isPlayerCombat(std::shared_ptr<Creature> target) {
if (target->getPlayer()) {
return true;
}
if (target->isSummon() && target->getMaster()->getPlayer()) {
return true;
}
return false;
}
ReturnValue Combat::canTargetCreature(std::shared_ptr<Player> player, std::shared_ptr<Creature> target) {
if (player == target) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}
if (!player->hasFlag(PlayerFlags_t::IgnoreProtectionZone)) {
// pz-zone
if (player->getZoneType() == ZONE_PROTECTION) {
return RETURNVALUE_ACTIONNOTPERMITTEDINPROTECTIONZONE;
}
if (target->getZoneType() == ZONE_PROTECTION) {
return RETURNVALUE_ACTIONNOTPERMITTEDINPROTECTIONZONE;
}
// nopvp-zone
if (isPlayerCombat(target)) {
if (player->getZoneType() == ZONE_NOPVP) {
return RETURNVALUE_ACTIONNOTPERMITTEDINANOPVPZONE;
}
if (target->getZoneType() == ZONE_NOPVP) {
return RETURNVALUE_YOUMAYNOTATTACKAPERSONINPROTECTIONZONE;
}
}
}
if (player->hasFlag(PlayerFlags_t::CannotUseCombat) || !target->isAttackable()) {
if (target->getPlayer()) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
} else {
return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE;
}
}
if (target->getPlayer()) {
if (isProtected(player, target->getPlayer())) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}
if (player->hasSecureMode() && !Combat::isInPvpZone(player, target) && player->getSkullClient(target->getPlayer()) == SKULL_NONE) {
return RETURNVALUE_TURNSECUREMODETOATTACKUNMARKEDPLAYERS;
}
}
return Combat::canDoCombat(player, target, true);
}
ReturnValue Combat::canDoCombat(std::shared_ptr<Creature> caster, std::shared_ptr<Tile> tile, bool aggressive) {
if (tile->hasProperty(CONST_PROP_BLOCKPROJECTILE)) {
return RETURNVALUE_NOTENOUGHROOM;
}
if (aggressive && tile->hasFlag(TILESTATE_PROTECTIONZONE)) {
return RETURNVALUE_ACTIONNOTPERMITTEDINPROTECTIONZONE;
}
if (tile->hasFlag(TILESTATE_FLOORCHANGE)) {
return RETURNVALUE_NOTENOUGHROOM;
}
if (tile->getTeleportItem()) {
return RETURNVALUE_NOTENOUGHROOM;
}
if (caster) {
const Position &casterPosition = caster->getPosition();
const Position &tilePosition = tile->getPosition();
if (casterPosition.z < tilePosition.z) {
return RETURNVALUE_FIRSTGODOWNSTAIRS;
} else if (casterPosition.z > tilePosition.z) {
return RETURNVALUE_FIRSTGOUPSTAIRS;
}
if (std::shared_ptr<Player> player = caster->getPlayer()) {
if (player->hasFlag(PlayerFlags_t::IgnoreProtectionZone)) {
return RETURNVALUE_NOERROR;
}
}
}
ReturnValue ret = g_events().eventCreatureOnAreaCombat(caster, tile, aggressive);
if (ret == RETURNVALUE_NOERROR) {
ret = g_callbacks().checkCallbackWithReturnValue(EventCallback_t::creatureOnTargetCombat, &EventCallback::creatureOnAreaCombat, caster, tile, aggressive);
}
return ret;
}
bool Combat::isInPvpZone(std::shared_ptr<Creature> attacker, std::shared_ptr<Creature> target) {
return attacker->getZoneType() == ZONE_PVP && target->getZoneType() == ZONE_PVP;
}
bool Combat::isProtected(std::shared_ptr<Player> attacker, std::shared_ptr<Player> target) {
uint32_t protectionLevel = g_configManager().getNumber(PROTECTION_LEVEL, __FUNCTION__);
if (target->getLevel() < protectionLevel || attacker->getLevel() < protectionLevel) {
return true;
}
if ((!attacker->getVocation()->canCombat() || !target->getVocation()->canCombat()) && (attacker->getVocationId() == VOCATION_NONE || target->getVocationId() == VOCATION_NONE)) {
return true;
}
if (attacker->getSkull() == SKULL_BLACK && attacker->getSkullClient(target) == SKULL_NONE) {
return true;
}
return false;
}
ReturnValue Combat::canDoCombat(std::shared_ptr<Creature> attacker, std::shared_ptr<Creature> target, bool aggressive) {
if (!aggressive) {
return RETURNVALUE_NOERROR;
}
auto targetPlayer = target ? target->getPlayer() : nullptr;
if (target) {
std::shared_ptr<Tile> tile = target->getTile();
if (tile->hasProperty(CONST_PROP_BLOCKPROJECTILE)) {
return RETURNVALUE_NOTENOUGHROOM;
}
if (tile->hasFlag(TILESTATE_PROTECTIONZONE)) {
auto permittedOnPz = targetPlayer ? targetPlayer->hasPermittedConditionInPZ() : false;
return permittedOnPz ? RETURNVALUE_NOERROR : RETURNVALUE_ACTIONNOTPERMITTEDINPROTECTIONZONE;
}
}
if (attacker) {
const std::shared_ptr<Creature> attackerMaster = attacker->getMaster();
if (targetPlayer) {
if (targetPlayer->hasFlag(PlayerFlags_t::CannotBeAttacked)) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}
const std::shared_ptr<Tile> targetPlayerTile = targetPlayer->getTile();
if (const std::shared_ptr<Player> attackerPlayer = attacker->getPlayer()) {
if (attackerPlayer->hasFlag(PlayerFlags_t::CannotAttackPlayer)) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}
if (isProtected(attackerPlayer, targetPlayer)) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}
// nopvp-zone
auto attackerTile = attackerPlayer->getTile();
if (targetPlayerTile && targetPlayerTile->hasFlag(TILESTATE_NOPVPZONE)) {
return RETURNVALUE_ACTIONNOTPERMITTEDINANOPVPZONE;
} else if (attackerTile && attackerTile->hasFlag(TILESTATE_NOPVPZONE) && targetPlayerTile && !targetPlayerTile->hasFlag(TILESTATE_NOPVPZONE | TILESTATE_PROTECTIONZONE)) {
return RETURNVALUE_ACTIONNOTPERMITTEDINANOPVPZONE;
}
if (attackerPlayer->getFaction() != FACTION_DEFAULT && attackerPlayer->getFaction() != FACTION_PLAYER && attackerPlayer->getFaction() == targetPlayer->getFaction()) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}
}
if (attackerMaster) {
if (const std::shared_ptr<Player> masterAttackerPlayer = attackerMaster->getPlayer()) {
if (masterAttackerPlayer->hasFlag(PlayerFlags_t::CannotAttackPlayer)) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}
if (targetPlayerTile && targetPlayerTile->hasFlag(TILESTATE_NOPVPZONE)) {
return RETURNVALUE_ACTIONNOTPERMITTEDINANOPVPZONE;
}
if (isProtected(masterAttackerPlayer, targetPlayer)) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}
}
}
if (attacker->getMonster() && (!attackerMaster || attackerMaster->getMonster())) {
if (attacker->getFaction() != FACTION_DEFAULT && !attacker->getMonster()->isEnemyFaction(targetPlayer->getFaction())) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}
}
} else if (target && target->getMonster()) {
if (attacker->getFaction() != FACTION_DEFAULT && attacker->getFaction() != FACTION_PLAYER && attacker->getMonster() && !attacker->getMonster()->isEnemyFaction(target->getFaction())) {
return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE;
}
if (const std::shared_ptr<Player> attackerPlayer = attacker->getPlayer()) {
if (attackerPlayer->hasFlag(PlayerFlags_t::CannotAttackMonster)) {
return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE;
}
if (target->isSummon() && target->getMaster()->getPlayer() && target->getZoneType() == ZONE_NOPVP) {
return RETURNVALUE_ACTIONNOTPERMITTEDINANOPVPZONE;
}
} else if (attacker->getMonster()) {
const std::shared_ptr<Creature> targetMaster = target->getMaster();
if ((!targetMaster || !targetMaster->getPlayer()) && attacker->getFaction() == FACTION_DEFAULT) {
if (!attackerMaster || !attackerMaster->getPlayer()) {
return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE;
}
}
}
} else if (target && target->getNpc()) {
return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE;
}
if (g_game().getWorldType() == WORLD_TYPE_NO_PVP) {
if (attacker->getPlayer() || (attackerMaster && attackerMaster->getPlayer())) {
if (targetPlayer) {
if (!isInPvpZone(attacker, target)) {
return RETURNVALUE_YOUMAYNOTATTACKTHISPLAYER;
}
}
if (target && target->isSummon() && target->getMaster()->getPlayer()) {
if (!isInPvpZone(attacker, target)) {
return RETURNVALUE_YOUMAYNOTATTACKTHISCREATURE;
}
}
}
}
}
ReturnValue ret = g_events().eventCreatureOnTargetCombat(attacker, target);
if (ret == RETURNVALUE_NOERROR) {
ret = g_callbacks().checkCallbackWithReturnValue(EventCallback_t::creatureOnTargetCombat, &EventCallback::creatureOnTargetCombat, attacker, target);
}
return ret;
}
void Combat::setPlayerCombatValues(formulaType_t newFormulaType, double newMina, double newMinb, double newMaxa, double newMaxb) {
this->formulaType = newFormulaType;
this->mina = newMina;
this->minb = newMinb;
this->maxa = newMaxa;
this->maxb = newMaxb;
}
bool Combat::setParam(CombatParam_t param, uint32_t value) {
switch (param) {
case COMBAT_PARAM_TYPE: {
params.combatType = static_cast<CombatType_t>(value);
return true;
}
case COMBAT_PARAM_EFFECT: {
params.impactEffect = static_cast<uint16_t>(value);
return true;
}
case COMBAT_PARAM_DISTANCEEFFECT: {
params.distanceEffect = static_cast<uint16_t>(value);
return true;
}
case COMBAT_PARAM_BLOCKARMOR: {
params.blockedByArmor = (value != 0);
return true;
}
case COMBAT_PARAM_BLOCKSHIELD: {
params.blockedByShield = (value != 0);
return true;
}
case COMBAT_PARAM_TARGETCASTERORTOPMOST: {
params.targetCasterOrTopMost = (value != 0);
return true;
}
case COMBAT_PARAM_CREATEITEM: {
params.itemId = value;
return true;
}
case COMBAT_PARAM_AGGRESSIVE: {
params.aggressive = (value != 0);
return true;
}
case COMBAT_PARAM_DISPEL: {
params.dispelType = static_cast<ConditionType_t>(value);
return true;
}
case COMBAT_PARAM_USECHARGES: {
params.useCharges = (value != 0);
return true;
}
case COMBAT_PARAM_IMPACTSOUND: {
params.soundImpactEffect = static_cast<SoundEffect_t>(value);
return true;
}
case COMBAT_PARAM_CASTSOUND: {
params.soundCastEffect = static_cast<SoundEffect_t>(value);
return true;
}
case COMBAT_PARAM_CHAIN_EFFECT: {
params.chainEffect = static_cast<uint8_t>(value);
return true;
}
}
return false;
}
bool Combat::setCallback(CallBackParam_t key) {
switch (key) {
case CALLBACK_PARAM_LEVELMAGICVALUE: {
params.valueCallback = std::make_unique<ValueCallback>(COMBAT_FORMULA_LEVELMAGIC);
return true;
}
case CALLBACK_PARAM_SKILLVALUE: {
params.valueCallback = std::make_unique<ValueCallback>(COMBAT_FORMULA_SKILL);
return true;
}
case CALLBACK_PARAM_TARGETTILE: {
params.tileCallback = std::make_unique<TileCallback>();
return true;
}
case CALLBACK_PARAM_TARGETCREATURE: {
params.targetCallback = std::make_unique<TargetCallback>();
return true;
}
case CALLBACK_PARAM_CHAINVALUE: {
params.chainCallback = std::make_unique<ChainCallback>();
params.chainCallback->setFromLua(true);
return true;
}
case CALLBACK_PARAM_CHAINPICKER: {
params.chainPickerCallback = std::make_unique<ChainPickerCallback>();
return true;
}
}
return false;
}
void Combat::setChainCallback(uint8_t chainTargets, uint8_t chainDistance, bool backtracking) {
params.chainCallback = std::make_unique<ChainCallback>(chainTargets, chainDistance, backtracking);
g_logger().trace("ChainCallback created: {}, with targets: {}, distance: {}, backtracking: {}", params.chainCallback != nullptr, chainTargets, chainDistance, backtracking);
}
CallBack* Combat::getCallback(CallBackParam_t key) {
switch (key) {
case CALLBACK_PARAM_LEVELMAGICVALUE:
case CALLBACK_PARAM_SKILLVALUE: {
return params.valueCallback.get();
}
case CALLBACK_PARAM_TARGETTILE: {
return params.tileCallback.get();
}
case CALLBACK_PARAM_TARGETCREATURE: {
return params.targetCallback.get();
}
case CALLBACK_PARAM_CHAINVALUE: {
return params.chainCallback.get();
}
case CALLBACK_PARAM_CHAINPICKER: {
return params.chainPickerCallback.get();
}
}
return nullptr;
}
void Combat::CombatHealthFunc(std::shared_ptr<Creature> caster, std::shared_ptr<Creature> target, const CombatParams ¶ms, CombatDamage* data) {
if (!data) {
g_logger().error("[{}]: CombatDamage is nullptr", __FUNCTION__);
return;
}
assert(data);
CombatDamage damage = *data;
std::shared_ptr<Player> attackerPlayer = nullptr;
if (caster) {
attackerPlayer = caster->getPlayer();
}
std::shared_ptr<Monster> targetMonster = nullptr;
if (target) {
targetMonster = target->getMonster();
}
std::shared_ptr<Monster> attackerMonster = nullptr;
if (caster) {
attackerMonster = caster->getMonster();
}
std::shared_ptr<Player> targetPlayer = nullptr;
if (target) {
targetPlayer = target->getPlayer();
}
if (attackerPlayer) {
std::shared_ptr<Item> item = attackerPlayer->getWeapon();
damage = applyImbuementElementalDamage(attackerPlayer, item, damage);
g_events().eventPlayerOnCombat(attackerPlayer, target, item, damage);
if (targetPlayer && targetPlayer->getSkull() != SKULL_BLACK) {
if (damage.primary.type != COMBAT_HEALING) {
damage.primary.value /= 2;
}
if (damage.secondary.type != COMBAT_HEALING) {
damage.secondary.value /= 2;
}
}
damage.damageMultiplier += attackerPlayer->wheel()->getMajorStatConditional("Divine Empowerment", WheelMajor_t::DAMAGE);
g_logger().trace("Wheel Divine Empowerment damage multiplier {}", damage.damageMultiplier);
}
if (g_game().combatBlockHit(damage, caster, target, params.blockedByShield, params.blockedByArmor, params.itemId != 0)) {
return;
}
// Player attacking monster
if (attackerPlayer && targetMonster) {
const std::unique_ptr<PreySlot> &slot = attackerPlayer->getPreyWithMonster(targetMonster->getRaceId());
if (slot && slot->isOccupied() && slot->bonus == PreyBonus_Damage && slot->bonusTimeLeft > 0) {
damage.primary.value += static_cast<int32_t>(std::ceil((damage.primary.value * slot->bonusPercentage) / 100));
damage.secondary.value += static_cast<int32_t>(std::ceil((damage.secondary.value * slot->bonusPercentage) / 100));
}
}
// Monster attacking player
if (attackerMonster && targetPlayer) {
const std::unique_ptr<PreySlot> &slot = targetPlayer->getPreyWithMonster(attackerMonster->getRaceId());
if (slot && slot->isOccupied() && slot->bonus == PreyBonus_Defense && slot->bonusTimeLeft > 0) {
damage.primary.value -= static_cast<int32_t>(std::ceil((damage.primary.value * slot->bonusPercentage) / 100));
damage.secondary.value -= static_cast<int32_t>(std::ceil((damage.secondary.value * slot->bonusPercentage) / 100));
}
}
if (g_game().combatChangeHealth(caster, target, damage)) {
CombatConditionFunc(caster, target, params, &damage);
CombatDispelFunc(caster, target, params, nullptr);
}
}
CombatDamage Combat::applyImbuementElementalDamage(std::shared_ptr<Player> attackerPlayer, std::shared_ptr<Item> item, CombatDamage damage) {
if (!item) {
return damage;
}
if (item->getWeaponType() == WEAPON_AMMO && attackerPlayer && attackerPlayer->getInventoryItem(CONST_SLOT_LEFT) != nullptr) {
item = attackerPlayer->getInventoryItem(CONST_SLOT_LEFT);
}
for (uint8_t slotid = 0; slotid < item->getImbuementSlot(); slotid++) {
ImbuementInfo imbuementInfo;
if (!item->getImbuementInfo(slotid, &imbuementInfo)) {
continue;
}
if (imbuementInfo.imbuement->combatType == COMBAT_NONE
|| damage.primary.type == COMBAT_HEALING
|| damage.secondary.type == COMBAT_HEALING) {
continue;
}
if (damage.primary.type != COMBAT_PHYSICALDAMAGE) {
break;
}
float damagePercent = imbuementInfo.imbuement->elementDamage / 100.0;
damage.secondary.type = imbuementInfo.imbuement->combatType;
damage.secondary.value = damage.primary.value * (damagePercent);
damage.primary.value = damage.primary.value * (1 - damagePercent);
if (imbuementInfo.imbuement->soundEffect != SoundEffect_t::SILENCE) {
g_game().sendSingleSoundEffect(item->getPosition(), imbuementInfo.imbuement->soundEffect, item->getHoldingPlayer());
}
// If damage imbuement is set, we can return without checking other slots
break;
}
return damage;
}
void Combat::CombatManaFunc(std::shared_ptr<Creature> caster, std::shared_ptr<Creature> target, const CombatParams ¶ms, CombatDamage* data) {
if (!data) {
g_logger().error("[{}]: CombatDamage is nullptr", __FUNCTION__);
return;
}
assert(data);
CombatDamage damage = *data;
if (damage.primary.value < 0) {
if (caster && target && caster->getPlayer() && target->getSkull() != SKULL_BLACK && target->getPlayer()) {
damage.primary.value /= 2;
}
}
if (g_game().combatChangeMana(caster, target, damage)) {
CombatConditionFunc(caster, target, params, nullptr);
CombatDispelFunc(caster, target, params, nullptr);
}
}
bool Combat::checkFearConditionAffected(std::shared_ptr<Player> player) {
if (player->isImmuneFear()) {
return false;
}
if (player->hasCondition(CONDITION_FEARED)) {
return false;
}
auto party = player->getParty();
if (party) {
auto affectedCount = (party->getMemberCount() + 5) / 5;
g_logger().debug("[{}] Player is member of a party, {} members can be feared", __FUNCTION__, affectedCount);
for (const auto &member : party->getMembers()) {
if (member->hasCondition(CONDITION_FEARED)) {
affectedCount -= 1;
}
}
if (affectedCount <= 0) {
return false;
}
}
return true;
}
void Combat::CombatConditionFunc(std::shared_ptr<Creature> caster, std::shared_ptr<Creature> target, const CombatParams ¶ms, CombatDamage* data) {
if (params.origin == ORIGIN_MELEE && data && data->primary.value == 0 && data->secondary.value == 0) {
return;
}
for (const auto &condition : params.conditionList) {
std::shared_ptr<Player> player = nullptr;
if (target) {
player = target->getPlayer();
}
if (player) {
// Cleanse charm rune (target as player)
if (player->isImmuneCleanse(condition->getType())) {
player->sendCancelMessage("You are still immune against this spell.");
return;
} else if (caster && caster->getMonster()) {
uint16_t playerCharmRaceid = player->parseRacebyCharm(CHARM_CLEANSE, false, 0);
if (playerCharmRaceid != 0) {
const auto mType = g_monsters().getMonsterType(caster->getName());
if (mType && playerCharmRaceid == mType->info.raceid) {
const auto charm = g_iobestiary().getBestiaryCharm(CHARM_CLEANSE);
if (charm && (charm->chance > normal_random(0, 100))) {
if (player->hasCondition(condition->getType())) {
player->removeCondition(condition->getType());
}
player->setImmuneCleanse(condition->getType());
player->sendCancelMessage(charm->cancelMsg);
return;
}
}
}
}
if (condition->getType() == CONDITION_FEARED && !checkFearConditionAffected(player)) {
return;
}
}
if (caster == target || (target && !target->isImmune(condition->getType()))) {
auto conditionCopy = condition->clone();
if (caster) {
conditionCopy->setParam(CONDITION_PARAM_OWNER, caster->getID());
conditionCopy->setPositionParam(CONDITION_PARAM_CASTER_POSITION, caster->getPosition());
}
// TODO: infight condition until all aggressive conditions has ended
if (target) {
target->addCombatCondition(conditionCopy, caster && caster->getPlayer() != nullptr);
}
}
}
}
void Combat::CombatDispelFunc(std::shared_ptr<Creature>, std::shared_ptr<Creature> target, const CombatParams ¶ms, CombatDamage*) {
if (target) {
target->removeCombatCondition(params.dispelType);
}
}
void Combat::CombatNullFunc(std::shared_ptr<Creature> caster, std::shared_ptr<Creature> target, const CombatParams ¶ms, CombatDamage*) {
CombatConditionFunc(caster, target, params, nullptr);
CombatDispelFunc(caster, target, params, nullptr);
}
void Combat::combatTileEffects(const CreatureVector &spectators, std::shared_ptr<Creature> caster, std::shared_ptr<Tile> tile, const CombatParams ¶ms) {
if (params.itemId != 0) {
uint16_t itemId = params.itemId;
switch (itemId) {
case ITEM_FIREFIELD_PERSISTENT_FULL:
itemId = ITEM_FIREFIELD_PVP_FULL;
break;
case ITEM_FIREFIELD_PERSISTENT_MEDIUM:
itemId = ITEM_FIREFIELD_PVP_MEDIUM;
break;
case ITEM_FIREFIELD_PERSISTENT_SMALL:
itemId = ITEM_FIREFIELD_PVP_SMALL;
break;
case ITEM_ENERGYFIELD_PERSISTENT:
itemId = ITEM_ENERGYFIELD_PVP;
break;
case ITEM_POISONFIELD_PERSISTENT:
itemId = ITEM_POISONFIELD_PVP;
break;
case ITEM_MAGICWALL_PERSISTENT:
itemId = ITEM_MAGICWALL;
break;
case ITEM_WILDGROWTH_PERSISTENT:
itemId = ITEM_WILDGROWTH;
break;
default:
break;
}
if (caster) {
std::shared_ptr<Player> casterPlayer;
if (caster->isSummon()) {
casterPlayer = caster->getMaster()->getPlayer();
} else {
casterPlayer = caster->getPlayer();
}
if (casterPlayer) {
if (g_game().getWorldType() == WORLD_TYPE_NO_PVP || tile->hasFlag(TILESTATE_NOPVPZONE)) {
if (itemId == ITEM_FIREFIELD_PVP_FULL) {
itemId = ITEM_FIREFIELD_NOPVP;
} else if (itemId == ITEM_POISONFIELD_PVP) {
itemId = ITEM_POISONFIELD_NOPVP;
} else if (itemId == ITEM_ENERGYFIELD_PVP) {
itemId = ITEM_ENERGYFIELD_NOPVP;
} else if (itemId == ITEM_MAGICWALL) {
itemId = ITEM_MAGICWALL_SAFE;
} else if (itemId == ITEM_WILDGROWTH) {
itemId = ITEM_WILDGROWTH_SAFE;
}
} else if (itemId == ITEM_FIREFIELD_PVP_FULL || itemId == ITEM_POISONFIELD_PVP || itemId == ITEM_ENERGYFIELD_PVP || itemId == ITEM_MAGICWALL || itemId == ITEM_WILDGROWTH) {
casterPlayer->addInFightTicks();
}
}
}
std::shared_ptr<Item> item = Item::CreateItem(itemId);
if (caster) {
item->setOwner(caster);
}
ReturnValue ret = g_game().internalAddItem(tile, item);
if (ret == RETURNVALUE_NOERROR) {
item->startDecaying();
}
}
if (params.tileCallback) {
params.tileCallback->onTileCombat(caster, tile);
}
if (params.impactEffect != CONST_ME_NONE) {
Game::addMagicEffect(spectators, tile->getPosition(), params.impactEffect);
}
if (params.soundImpactEffect != SoundEffect_t::SILENCE) {
g_game().sendDoubleSoundEffect(tile->getPosition(), params.soundCastEffect, params.soundImpactEffect, caster);
} else if (params.soundCastEffect != SoundEffect_t::SILENCE) {
g_game().sendSingleSoundEffect(tile->getPosition(), params.soundCastEffect, caster);
}
}
void Combat::postCombatEffects(std::shared_ptr<Creature> caster, const Position &origin, const Position &pos, const CombatParams ¶ms) {
if (caster && params.distanceEffect != CONST_ANI_NONE) {
addDistanceEffect(caster, origin, pos, params.distanceEffect);
}
if (params.soundImpactEffect != SoundEffect_t::SILENCE) {
g_game().sendDoubleSoundEffect(pos, params.soundCastEffect, params.soundImpactEffect, caster);
} else if (params.soundCastEffect != SoundEffect_t::SILENCE) {
g_game().sendSingleSoundEffect(pos, params.soundCastEffect, caster);
}
}
void Combat::addDistanceEffect(std::shared_ptr<Creature> caster, const Position &fromPos, const Position &toPos, uint16_t effect) {
if (effect == CONST_ANI_WEAPONTYPE) {
if (!caster) {
return;
}
std::shared_ptr<Player> player = caster->getPlayer();
if (!player) {
return;
}
switch (player->getWeaponType()) {
case WEAPON_AXE:
effect = CONST_ANI_WHIRLWINDAXE;
break;
case WEAPON_SWORD:
effect = CONST_ANI_WHIRLWINDSWORD;
break;
case WEAPON_CLUB:
effect = CONST_ANI_WHIRLWINDCLUB;
break;
case WEAPON_MISSILE: {
auto weapon = player->getWeapon();
if (weapon) {
const auto &iType = Item::items[weapon->getID()];
effect = iType.shootType;
}
break;
}
default:
effect = CONST_ANI_NONE;
break;
}
}
if (effect != CONST_ANI_NONE) {
g_game().addDistanceEffect(fromPos, toPos, effect);
}
}
void Combat::doChainEffect(const Position &origin, const Position &dest, uint8_t effect) {
if (effect > 0) {
std::vector<Direction> dirList;
FindPathParams fpp;
fpp.minTargetDist = 0;
fpp.maxTargetDist = 1;
fpp.maxSearchDist = 9;
Position pos = origin;
if (g_game().map.getPathMatching(origin, dirList, FrozenPathingConditionCall(dest), fpp)) {
for (auto dir : dirList) {
pos = getNextPosition(dir, pos);
g_game().addMagicEffect(pos, effect);
}
}
g_game().addMagicEffect(dest, effect);
}
}
void Combat::setupChain(const std::shared_ptr<Weapon> &weapon) {
if (!weapon) {
return;
}
if (weapon->isChainDisabled()) {
return;
}
const auto &weaponType = weapon->getWeaponType();
if (weaponType == WEAPON_NONE || weaponType == WEAPON_SHIELD || weaponType == WEAPON_AMMO || weaponType == WEAPON_DISTANCE || weaponType == WEAPON_MISSILE) {
return;
}
// clang-format off
static std::list<uint32_t> areaList = {
0, 0, 0, 1, 0, 0, 0,
0, 1, 1, 1, 1, 1, 0,
0, 1, 1, 1, 1, 1, 0,
1, 1, 1, 3, 1, 1, 1,
0, 1, 1, 1, 1, 1, 0,
0, 1, 1, 1, 1, 1, 0,
0, 0, 0, 1, 0, 0, 0,
};
// clang-format on
auto area = std::make_unique<AreaCombat>();
area->setupArea(areaList, 7);
setArea(area);
g_logger().trace("Weapon: {}, element type: {}", Item::items[weapon->getID()].name, weapon->params.combatType);
setParam(COMBAT_PARAM_TYPE, weapon->params.combatType);
if (weaponType != WEAPON_WAND) {
setParam(COMBAT_PARAM_BLOCKARMOR, true);
}
weapon->params.chainCallback = std::make_unique<ChainCallback>();
auto setCommonValues = [this, weapon](double formula, SoundEffect_t impactSound, uint32_t effect) {
double weaponSkillFormula = weapon->getChainSkillValue();
setPlayerCombatValues(COMBAT_FORMULA_SKILL, 0, 0, weaponSkillFormula ? weaponSkillFormula : formula, 0);
setParam(COMBAT_PARAM_IMPACTSOUND, impactSound);
setParam(COMBAT_PARAM_EFFECT, effect);
setParam(COMBAT_PARAM_BLOCKARMOR, true);
};