From 97a8874ed73cb13529b6222dbd9ecc14f8a42130 Mon Sep 17 00:00:00 2001 From: Arseniy Shestakov Date: Tue, 26 Jan 2016 08:41:09 +0300 Subject: [PATCH] CGCreature: fix crash on draw Also according to H3 behaviour if there artifact monster guarded it's will be lost on draw. --- lib/mapObjects/CGPandoraBox.cpp | 8 ++++---- lib/mapObjects/MiscObjects.cpp | 20 ++++++++++++-------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/lib/mapObjects/CGPandoraBox.cpp b/lib/mapObjects/CGPandoraBox.cpp index f512716d6..b21fc3ced 100644 --- a/lib/mapObjects/CGPandoraBox.cpp +++ b/lib/mapObjects/CGPandoraBox.cpp @@ -295,10 +295,10 @@ void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int val, int nega void CGPandoraBox::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const { - if(result.winner) - return; - - giveContentsUpToExp(hero); + if(result.winner == 0) + { + giveContentsUpToExp(hero); + } } void CGPandoraBox::blockingDialogAnswered(const CGHeroInstance *hero, ui32 answer) const diff --git a/lib/mapObjects/MiscObjects.cpp b/lib/mapObjects/MiscObjects.cpp index 51a293ed3..5f1014670 100644 --- a/lib/mapObjects/MiscObjects.cpp +++ b/lib/mapObjects/MiscObjects.cpp @@ -453,22 +453,26 @@ void CGCreature::flee( const CGHeroInstance * h ) const void CGCreature::battleFinished(const CGHeroInstance *hero, const BattleResult &result) const { - - if(result.winner==0) + if(result.winner == 0) { giveReward(hero); cb->removeObject(this); } + else if(result.winner > 1) // draw + { + // guarded reward is lost forever on draw + cb->removeObject(this); + } else { //merge stacks into one TSlots::const_iterator i; CCreature * cre = VLC->creh->creatures[formation.basicType]; - for (i = stacks.begin(); i != stacks.end(); i++) + for(i = stacks.begin(); i != stacks.end(); i++) { - if (cre->isMyUpgrade(i->second->type)) + if(cre->isMyUpgrade(i->second->type)) { - cb->changeStackType (StackLocation(this, i->first), cre); //un-upgrade creatures + cb->changeStackType(StackLocation(this, i->first), cre); //un-upgrade creatures } } @@ -476,16 +480,16 @@ void CGCreature::battleFinished(const CGHeroInstance *hero, const BattleResult & if(!hasStackAtSlot(SlotID(0))) cb->moveStack(StackLocation(this, stacks.begin()->first), StackLocation(this, SlotID(0)), stacks.begin()->second->count); - while (stacks.size() > 1) //hopefully that's enough + while(stacks.size() > 1) //hopefully that's enough { // TODO it's either overcomplicated (if we assume there'll be only one stack) or buggy (if we allow multiple stacks... but that'll also cause troubles elsewhere) i = stacks.end(); i--; SlotID slot = getSlotFor(i->second->type); - if (slot == i->first) //no reason to move stack to its own slot + if(slot == i->first) //no reason to move stack to its own slot break; else - cb->moveStack (StackLocation(this, i->first), StackLocation(this, slot), i->second->count); + cb->moveStack(StackLocation(this, i->first), StackLocation(this, slot), i->second->count); } cb->setObjProperty(id, ObjProperty::MONSTER_POWER, stacks.begin()->second->count * 1000); //remember casualties