1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-14 10:12:59 +02:00
vcmi/client/windows/QuickRecruitmentWindow.cpp

172 lines
5.4 KiB
C++
Raw Normal View History

/*
* QuickRecruitmentWindow.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#include "StdInc.h"
#include "QuickRecruitmentWindow.h"
#include "../../lib/mapObjects/CGTownInstance.h"
#include "../CPlayerInterface.h"
#include "../widgets/Buttons.h"
#include "../widgets/CreatureCostBox.h"
2023-05-30 16:31:45 +02:00
#include "../widgets/Slider.h"
#include "../gui/CGuiHandler.h"
#include "../gui/Shortcut.h"
#include "../../CCallback.h"
Spells configuration version 2 (effect-based) * Indirect spell effects loading * Json serializer improvements * spell->canBeCastAt do not allow useless cast for any spell * Added proxy caster class for spell-created obstacles * Handle damage from spell-created obstacles inside mechanics * Experimental GameState integration/regression tests * Ignore mod settings and load only "vcmi" mod when running tests * fixed https://bugs.vcmi.eu/view.php?id=2765 (with tests) * Huge improvements of BattleAI regarding spell casts * AI can cast almost any combat spell except TELEPORT, SACRIFICE and obstacle placement spells. * Possible fix for https://bugs.vcmi.eu/view.php?id=1811 * CStack factored out to several classes * [Battle] Allowed RETURN_AFTER_STRIKE effect on server side to be optional * [Battle] Allowed BattleAction have multiple destinations * [Spells] Converted limit|immunity to target condition * [Spells] Use partial configuration reload for backward compatibility handling * [Tests] Started tests for CUnitState * Partial fixes of fire shield effect * [Battle] Do HP calculations in 64 bits * [BattleAI] Use threading for spell cast evaluation * [BattleAI] Made AI be able to evaluate modified turn order (on hypothetical battle state) * Implemented https://bugs.vcmi.eu/view.php?id=2811 * plug rare freeze when hypnotized unit shots vertically * Correctly apply ONLY_MELEE_FIGHT / ONLY_DISTANCE_FIGHT for unit damage, attack & defense * [BattleAI] Try to not waste a cast if battle is actually won already * Extended JsonSerializeFormat API * fixed https://bugs.vcmi.eu/view.php?id=2847 * Any unit effect can be now chained (not only damage like Chain Lightning) ** only damage effect for now actually uses "chainFactor" * Possible quick fix for https://bugs.vcmi.eu/view.php?id=2860
2017-07-20 06:08:49 +02:00
#include "../../lib/ResourceSet.h"
#include "../../lib/CCreatureHandler.h"
#include "CreaturePurchaseCard.h"
void QuickRecruitmentWindow::setButtons()
{
setCancelButton();
setBuyButton();
setMaxButton();
}
void QuickRecruitmentWindow::setCancelButton()
{
cancelButton = std::make_shared<CButton>(Point((pos.w / 2) + 48, 418), AnimationPath::builtin("ICN6432.DEF"), CButton::tooltip(), [&](){ close(); }, EShortcut::GLOBAL_CANCEL);
cancelButton->setImageOrder(0, 1, 2, 3);
}
void QuickRecruitmentWindow::setBuyButton()
{
buyButton = std::make_shared<CButton>(Point((pos.w / 2) - 32, 418), AnimationPath::builtin("IBY6432.DEF"), CButton::tooltip(), [&](){ purchaseUnits(); }, EShortcut::GLOBAL_ACCEPT);
buyButton->setImageOrder(0, 1, 2, 3);
}
void QuickRecruitmentWindow::setMaxButton()
{
maxButton = std::make_shared<CButton>(Point((pos.w/2)-112, 418), AnimationPath::builtin("IRCBTNS.DEF"), CButton::tooltip(), [&](){ maxAllCards(cards); }, EShortcut::RECRUITMENT_MAX);
maxButton->setImageOrder(0, 1, 2, 3);
}
2022-09-17 14:44:13 +02:00
void QuickRecruitmentWindow::setCreaturePurchaseCards()
{
int availableAmount = getAvailableCreatures();
Point position = Point((pos.w - 100*availableAmount - 8*(availableAmount-1))/2,64);
for (int i = 0; i < town->getTown()->creatures.size(); i++)
{
if(!town->getTown()->creatures.at(i).empty() && !town->creatures.at(i).second.empty() && town->creatures[i].first)
{
cards.push_back(std::make_shared<CreaturePurchaseCard>(town->creatures[i].second, position, town->creatures[i].first, this));
position.x += 108;
}
}
totalCost = std::make_shared<CreatureCostBox>(Rect((this->pos.w/2)-45, position.y+260, 97, 74), "");
}
void QuickRecruitmentWindow::initWindow(Rect startupPosition)
{
pos.x = startupPosition.x + 238;
pos.y = startupPosition.y + 45;
pos.w = 332;
pos.h = 461;
int creaturesAmount = getAvailableCreatures();
if(creaturesAmount > 3)
{
pos.w += 108 * (creaturesAmount - 3);
pos.x -= 55 * (creaturesAmount - 3);
}
backgroundTexture = std::make_shared<CFilledTexture>(ImagePath::builtin("DIBOXBCK.pcx"), Rect(0, 0, pos.w, pos.h));
costBackground = std::make_shared<CPicture>(ImagePath::builtin("QuickRecruitmentWindow/costBackground.png"), pos.w/2-113, 335);
}
void QuickRecruitmentWindow::maxAllCards(std::vector<std::shared_ptr<CreaturePurchaseCard> > cards)
{
auto allAvailableResources = LOCPLINT->cb->getResourceAmount();
for(auto i : boost::adaptors::reverse(cards))
{
si32 maxAmount = i->creatureOnTheCard->maxAmount(allAvailableResources);
vstd::amin(maxAmount, i->maxAmount);
i->slider->setAmount(maxAmount);
if(i->slider->getValue() != maxAmount)
i->slider->scrollTo(maxAmount);
else
i->sliderMoved(maxAmount);
i->slider->scrollToMax();
2023-04-05 02:26:29 +02:00
allAvailableResources -= (i->creatureOnTheCard->getFullRecruitCost() * maxAmount);
}
maxButton->block(allAvailableResources == LOCPLINT->cb->getResourceAmount());
}
2022-09-17 14:44:13 +02:00
void QuickRecruitmentWindow::purchaseUnits()
{
for(auto selected : boost::adaptors::reverse(cards))
{
if(selected->slider->getValue())
{
2024-08-06 01:02:38 +02:00
int level = 0;
int i = 0;
for(auto c : town->getTown()->creatures)
2024-08-06 01:02:38 +02:00
{
for(auto c2 : c)
if(c2 == selected->creatureOnTheCard->getId())
level = i;
i++;
}
auto onRecruit = [=](CreatureID id, int count){ LOCPLINT->cb->recruitCreatures(town, town->getUpperArmy(), id, count, level); };
2023-04-05 02:26:29 +02:00
CreatureID crid = selected->creatureOnTheCard->getId();
SlotID dstslot = town -> getSlotFor(crid);
if(!dstslot.validSlot())
continue;
onRecruit(crid, selected->slider->getValue());
}
}
close();
}
int QuickRecruitmentWindow::getAvailableCreatures()
{
int creaturesAmount = 0;
for (int i=0; i< town->getTown()->creatures.size(); i++)
if(!town->getTown()->creatures.at(i).empty() && !town->creatures.at(i).second.empty() && town->creatures[i].first)
creaturesAmount++;
return creaturesAmount;
}
void QuickRecruitmentWindow::updateAllSliders()
{
auto allAvailableResources = LOCPLINT->cb->getResourceAmount();
for(auto i : boost::adaptors::reverse(cards))
2023-04-05 02:26:29 +02:00
allAvailableResources -= (i->creatureOnTheCard->getFullRecruitCost() * i->slider->getValue());
for(auto i : cards)
{
si32 maxAmount = i->creatureOnTheCard->maxAmount(allAvailableResources);
vstd::amin(maxAmount, i->maxAmount);
if(maxAmount < 0)
continue;
if(i->slider->getValue() + maxAmount < i->maxAmount)
i->slider->setAmount(i->slider->getValue() + maxAmount);
else
i->slider->setAmount(i->maxAmount);
i->slider->scrollTo(i->slider->getValue());
}
totalCost->createItems(LOCPLINT->cb->getResourceAmount() - allAvailableResources);
totalCost->set(LOCPLINT->cb->getResourceAmount() - allAvailableResources);
}
QuickRecruitmentWindow::QuickRecruitmentWindow(const CGTownInstance * townd, Rect startupPosition)
2017-12-21 20:27:39 +02:00
: CWindowObject(PLAYER_COLORED | BORDERED),
town(townd)
{
OBJECT_CONSTRUCTION;
initWindow(startupPosition);
setButtons();
2022-09-17 14:44:13 +02:00
setCreaturePurchaseCards();
maxAllCards(cards);
2024-10-07 18:01:36 +02:00
center();
}