@@ -498,13 +498,17 @@ void Creature::onDeath() {
498
498
bool lastHitUnjustified = false ;
499
499
bool mostDamageUnjustified = false ;
500
500
const auto &lastHitCreature = g_game ().getCreatureByID (lastHitCreatureId);
501
+ const auto &thisPlayer = getPlayer ();
502
+ const auto &thisCreature = getCreature ();
503
+ const auto &thisMaster = getMaster ();
504
+ const auto &thisMonster = getMonster ();
501
505
std::shared_ptr<Creature> lastHitCreatureMaster;
502
- if (lastHitCreature && getPlayer () ) {
506
+ if (lastHitCreature && thisPlayer ) {
503
507
/* *
504
508
* @deprecated -- This is here to trigger the deprecated onKill events in lua
505
509
*/
506
- lastHitCreature->deprecatedOnKilledCreature (getCreature () , true );
507
- lastHitUnjustified = lastHitCreature->onKilledPlayer (getPlayer () , true );
510
+ lastHitCreature->deprecatedOnKilledCreature (thisCreature , true );
511
+ lastHitUnjustified = lastHitCreature->onKilledPlayer (thisPlayer , true );
508
512
lastHitCreatureMaster = lastHitCreature->getMaster ();
509
513
} else {
510
514
lastHitCreatureMaster = nullptr ;
@@ -517,6 +521,7 @@ void Creature::onDeath() {
517
521
int32_t mostDamage = 0 ;
518
522
std::map<std::shared_ptr<Creature>, uint64_t > experienceMap;
519
523
std::unordered_set<std::shared_ptr<Player>> killers;
524
+
520
525
for (const auto &[creatureId, damageInfo] : damageMap) {
521
526
if (creatureId == 0 ) {
522
527
continue ;
@@ -533,12 +538,10 @@ void Creature::onDeath() {
533
538
mostDamageCreature = attacker;
534
539
}
535
540
536
- if (attacker != getCreature () ) {
541
+ if (attacker != thisCreature ) {
537
542
const uint64_t gainExp = getGainedExperience (attacker);
538
543
const auto &attackerMaster = attacker->getMaster () ? attacker->getMaster () : attacker;
539
- if (auto attackerPlayer = attackerMaster->getPlayer ()) {
540
- attackerPlayer->removeAttacked (getPlayer ());
541
-
544
+ if (const auto &attackerPlayer = attackerMaster->getPlayer ()) {
542
545
const auto &party = attackerPlayer->getParty ();
543
546
killers.insert (attackerPlayer);
544
547
if (party && party->getLeader () && party->isSharedExperienceActive () && party->isSharedExperienceEnabled ()) {
@@ -563,36 +566,44 @@ void Creature::onDeath() {
563
566
}
564
567
565
568
for (const auto &[creature, experience] : experienceMap) {
566
- creature->onGainExperience (experience, getCreature () );
569
+ creature->onGainExperience (experience, thisCreature );
567
570
}
568
571
569
- mostDamageCreature = mostDamageCreature && mostDamageCreature->getMaster () ? mostDamageCreature->getMaster () : mostDamageCreature;
572
+ const auto &mostDamageCreatureMaster = mostDamageCreature ? mostDamageCreature->getMaster () : nullptr ;
573
+ mostDamageCreature = mostDamageCreatureMaster ? mostDamageCreatureMaster : mostDamageCreature;
574
+
570
575
for (const auto &killer : killers) {
571
- if (const auto &monster = getMonster ()) {
572
- killer->onKilledMonster (monster);
573
- } else if (const auto &player = getPlayer (); player && mostDamageCreature != killer) {
574
- killer->onKilledPlayer (player, false );
576
+ if (thisMonster) {
577
+ killer->onKilledMonster (thisMonster);
578
+ } else if (thisPlayer) {
579
+ bool isResponsible = mostDamageCreature == killer || (mostDamageCreatureMaster && mostDamageCreatureMaster == killer);
580
+ if (isResponsible) {
581
+ killer->onKilledPlayer (thisPlayer, false );
582
+ }
583
+
584
+ killer->removeAttacked (thisPlayer);
575
585
}
576
586
}
577
587
578
588
/* *
579
589
* @deprecated -- This is here to trigger the deprecated onKill events in lua
580
590
*/
581
- const auto &mostDamageCreatureMaster = mostDamageCreature ? mostDamageCreature->getMaster () : nullptr ;
582
- if (mostDamageCreature && (mostDamageCreature != lastHitCreature || getMonster ()) && mostDamageCreature != lastHitCreatureMaster) {
591
+ if (mostDamageCreature && (mostDamageCreature != lastHitCreature || thisMonster) && mostDamageCreature != lastHitCreatureMaster) {
583
592
if (lastHitCreature != mostDamageCreatureMaster && (lastHitCreatureMaster == nullptr || mostDamageCreatureMaster != lastHitCreatureMaster)) {
584
- mostDamageUnjustified = mostDamageCreature->deprecatedOnKilledCreature (getCreature () , false );
593
+ mostDamageUnjustified = mostDamageCreature->deprecatedOnKilledCreature (thisCreature , false );
585
594
}
586
595
}
587
596
588
- bool killedByPlayer = (mostDamageCreature && mostDamageCreature->getPlayer ()) || (mostDamageCreatureMaster && mostDamageCreatureMaster->getPlayer ());
589
- if (getPlayer ()) {
597
+ const auto &mostDamagePlayer = mostDamageCreature ? mostDamageCreature->getPlayer () : nullptr ;
598
+ const auto &mostMasterPlayer = mostDamageCreatureMaster ? mostDamageCreatureMaster->getPlayer () : nullptr ;
599
+ bool killedByPlayer = mostDamagePlayer || mostMasterPlayer;
600
+ if (thisPlayer) {
590
601
g_metrics ().addCounter (
591
602
" player_death" ,
592
603
1 ,
593
604
{
594
605
{ " name" , getNameDescription () },
595
- { " level" , std::to_string (getPlayer () ->getLevel ()) },
606
+ { " level" , std::to_string (thisPlayer ->getLevel ()) },
596
607
{ " most_damage_creature" , mostDamageCreature ? mostDamageCreature->getName () : " (none)" },
597
608
{ " last_hit_creature" , lastHitCreature ? lastHitCreature->getName () : " (none)" },
598
609
{ " most_damage_dealt" , std::to_string (mostDamage) },
@@ -613,7 +624,7 @@ void Creature::onDeath() {
613
624
{
614
625
{ " name" , getName () },
615
626
{ " killer" , killerName },
616
- { " is_summon" , std::to_string (getMaster () ? true : false ) },
627
+ { " is_summon" , std::to_string (thisMaster ? true : false ) },
617
628
{ " by_player" , std::to_string (killedByPlayer) },
618
629
}
619
630
);
@@ -622,11 +633,11 @@ void Creature::onDeath() {
622
633
bool droppedCorpse = dropCorpse (lastHitCreature, mostDamageCreature, lastHitUnjustified, mostDamageUnjustified);
623
634
death (lastHitCreature);
624
635
625
- if (droppedCorpse && !getPlayer () ) {
626
- g_game ().removeCreature (static_self_cast<Creature>() , false );
636
+ if (droppedCorpse && !thisPlayer ) {
637
+ g_game ().removeCreature (thisCreature , false );
627
638
}
628
639
629
- if (getMaster () ) {
640
+ if (thisMaster ) {
630
641
removeMaster ();
631
642
}
632
643
}
0 commit comments