2022-10-12 23:51:55 +02:00
|
|
|
/*
|
|
|
|
* playerparams.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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-09-18 01:23:17 +02:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "playerparams.h"
|
|
|
|
#include "ui_playerparams.h"
|
2023-09-11 18:01:53 +02:00
|
|
|
#include "mapsettings/abstractsettings.h"
|
2023-08-19 23:22:31 +02:00
|
|
|
#include "../lib/constants/StringConstants.h"
|
2024-07-21 12:49:40 +02:00
|
|
|
#include "../lib/entities/faction/CTownHandler.h"
|
2023-05-24 01:05:59 +02:00
|
|
|
#include "../lib/mapping/CMap.h"
|
|
|
|
|
2022-09-18 01:23:17 +02:00
|
|
|
PlayerParams::PlayerParams(MapController & ctrl, int playerId, QWidget *parent) :
|
|
|
|
QWidget(parent),
|
|
|
|
ui(new Ui::PlayerParams),
|
|
|
|
controller(ctrl)
|
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2023-04-21 01:17:13 +02:00
|
|
|
|
|
|
|
//set colors and teams
|
2024-12-09 22:51:35 +02:00
|
|
|
ui->teamId->addItem(tr("No team"), QVariant(TeamID::NO_TEAM));
|
2023-04-21 01:17:13 +02:00
|
|
|
for(int i = 0, index = 0; i < PlayerColor::PLAYER_LIMIT_I; ++i)
|
|
|
|
{
|
|
|
|
if(i == playerId || !controller.map()->players[i].canAnyonePlay())
|
|
|
|
{
|
|
|
|
ui->playerColorCombo->addItem(QString::fromStdString(GameConstants::PLAYER_COLOR_NAMES[i]), QVariant(i));
|
|
|
|
if(i == playerId)
|
|
|
|
ui->playerColorCombo->setCurrentIndex(index);
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
|
|
|
|
//add teams
|
|
|
|
ui->teamId->addItem(QString::number(i + 1), QVariant(i));
|
|
|
|
}
|
2022-09-18 01:23:17 +02:00
|
|
|
|
|
|
|
playerColor = playerId;
|
|
|
|
assert(controller.map()->players.size() > playerColor);
|
|
|
|
playerInfo = controller.map()->players[playerColor];
|
2023-04-21 01:17:13 +02:00
|
|
|
ui->teamId->setCurrentIndex(playerInfo.team == TeamID::NO_TEAM ? 0 : playerInfo.team.getNum() + 1);
|
2022-09-18 01:23:17 +02:00
|
|
|
|
|
|
|
//load factions
|
|
|
|
for(auto idx : VLC->townh->getAllowedFactions())
|
|
|
|
{
|
2024-05-17 00:05:51 +02:00
|
|
|
const auto & faction = VLC->townh->objects.at(idx);
|
2023-01-04 15:17:50 +02:00
|
|
|
auto * item = new QListWidgetItem(QString::fromStdString(faction->getNameTranslated()));
|
2023-04-09 17:26:32 +02:00
|
|
|
item->setData(Qt::UserRole, QVariant::fromValue(idx.getNum()));
|
2022-09-18 01:23:17 +02:00
|
|
|
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
|
|
|
|
ui->allowedFactions->addItem(item);
|
|
|
|
if(playerInfo.allowedFactions.count(idx))
|
|
|
|
item->setCheckState(Qt::Checked);
|
|
|
|
else
|
|
|
|
item->setCheckState(Qt::Unchecked);
|
|
|
|
}
|
|
|
|
QObject::connect(ui->allowedFactions, SIGNAL(itemChanged(QListWidgetItem*)),
|
|
|
|
this, SLOT(allowedFactionsCheck(QListWidgetItem*)));
|
|
|
|
|
|
|
|
//load checks
|
|
|
|
bool canHumanPlay = playerInfo.canHumanPlay; //need variable to restore after signal received
|
|
|
|
playerInfo.canComputerPlay = true; //computer always can play
|
|
|
|
ui->radioCpu->setChecked(true);
|
|
|
|
if(canHumanPlay)
|
|
|
|
ui->radioHuman->setChecked(true);
|
|
|
|
if(playerInfo.isFactionRandom)
|
|
|
|
ui->randomFaction->setChecked(true);
|
|
|
|
if(playerInfo.generateHeroAtMainTown)
|
|
|
|
ui->generateHero->setChecked(true);
|
|
|
|
|
|
|
|
//load towns
|
|
|
|
int foundMainTown = -1;
|
|
|
|
for(int i = 0, townIndex = 0; i < controller.map()->objects.size(); ++i)
|
|
|
|
{
|
|
|
|
if(auto town = dynamic_cast<CGTownInstance*>(controller.map()->objects[i].get()))
|
|
|
|
{
|
2024-10-05 21:37:52 +02:00
|
|
|
auto * ctown = town->getTown();
|
2022-09-18 01:23:17 +02:00
|
|
|
if(!ctown)
|
|
|
|
ctown = VLC->townh->randomTown;
|
2024-10-05 21:37:52 +02:00
|
|
|
|
2022-09-18 01:23:17 +02:00
|
|
|
if(ctown && town->getOwner().getNum() == playerColor)
|
|
|
|
{
|
|
|
|
if(playerInfo.hasMainTown && playerInfo.posOfMainTown == town->pos)
|
|
|
|
foundMainTown = townIndex;
|
2023-09-11 18:01:53 +02:00
|
|
|
|
|
|
|
const auto name = AbstractSettings::getTownName(*controller.map(), i);
|
2022-09-18 01:23:17 +02:00
|
|
|
ui->mainTown->addItem(tr(name.c_str()), QVariant::fromValue(i));
|
|
|
|
++townIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(foundMainTown > -1)
|
|
|
|
{
|
|
|
|
ui->mainTown->setCurrentIndex(foundMainTown + 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui->generateHero->setEnabled(false);
|
|
|
|
playerInfo.hasMainTown = false;
|
|
|
|
playerInfo.generateHeroAtMainTown = false;
|
|
|
|
playerInfo.posOfMainTown = int3(-1, -1, -1);
|
|
|
|
}
|
|
|
|
|
2022-10-08 20:55:15 +02:00
|
|
|
ui->playerColor->setTitle(tr("Player ID: %1").arg(playerId));
|
2022-09-18 01:23:17 +02:00
|
|
|
show();
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayerParams::~PlayerParams()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlayerParams::on_radioHuman_toggled(bool checked)
|
|
|
|
{
|
|
|
|
if(checked)
|
|
|
|
playerInfo.canHumanPlay = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayerParams::on_radioCpu_toggled(bool checked)
|
|
|
|
{
|
|
|
|
if(checked)
|
|
|
|
playerInfo.canHumanPlay = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayerParams::on_generateHero_stateChanged(int arg1)
|
|
|
|
{
|
|
|
|
playerInfo.generateHeroAtMainTown = ui->generateHero->isChecked();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayerParams::on_randomFaction_stateChanged(int arg1)
|
|
|
|
{
|
|
|
|
playerInfo.isFactionRandom = ui->randomFaction->isChecked();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayerParams::allowedFactionsCheck(QListWidgetItem * item)
|
|
|
|
{
|
|
|
|
if(item->checkState() == Qt::Checked)
|
2023-04-09 17:26:32 +02:00
|
|
|
playerInfo.allowedFactions.insert(FactionID(item->data(Qt::UserRole).toInt()));
|
2022-09-18 01:23:17 +02:00
|
|
|
else
|
2023-04-09 17:26:32 +02:00
|
|
|
playerInfo.allowedFactions.erase(FactionID(item->data(Qt::UserRole).toInt()));
|
2022-09-18 01:23:17 +02:00
|
|
|
}
|
|
|
|
|
2023-09-11 18:44:55 +02:00
|
|
|
void PlayerParams::on_mainTown_currentIndexChanged(int index)
|
2022-09-18 01:23:17 +02:00
|
|
|
{
|
|
|
|
if(index == 0) //default
|
|
|
|
{
|
|
|
|
ui->generateHero->setEnabled(false);
|
|
|
|
ui->generateHero->setChecked(false);
|
|
|
|
playerInfo.hasMainTown = false;
|
|
|
|
playerInfo.posOfMainTown = int3(-1, -1, -1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui->generateHero->setEnabled(true);
|
|
|
|
auto town = controller.map()->objects.at(ui->mainTown->currentData().toInt());
|
|
|
|
playerInfo.hasMainTown = true;
|
|
|
|
playerInfo.posOfMainTown = town->pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-21 01:17:13 +02:00
|
|
|
|
|
|
|
void PlayerParams::on_teamId_activated(int index)
|
|
|
|
{
|
2023-09-28 18:43:04 +02:00
|
|
|
playerInfo.team.setNum(ui->teamId->currentData().toInt());
|
2023-04-21 01:17:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PlayerParams::on_playerColorCombo_activated(int index)
|
|
|
|
{
|
|
|
|
int data = ui->playerColorCombo->currentData().toInt();
|
|
|
|
if(data != playerColor)
|
|
|
|
{
|
|
|
|
controller.map()->players[playerColor].canHumanPlay = false;
|
|
|
|
controller.map()->players[playerColor].canComputerPlay = false;
|
|
|
|
playerColor = data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 18:01:53 +02:00
|
|
|
|
|
|
|
void PlayerParams::on_townSelect_clicked()
|
|
|
|
{
|
|
|
|
auto pred = [this](const CGObjectInstance * obj) -> bool
|
|
|
|
{
|
|
|
|
if(auto town = dynamic_cast<const CGTownInstance*>(obj))
|
|
|
|
return town->getOwner().getNum() == playerColor;
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2023-09-11 18:44:55 +02:00
|
|
|
for(int lvl : {0, 1})
|
2023-09-11 18:01:53 +02:00
|
|
|
{
|
2023-09-11 18:44:55 +02:00
|
|
|
auto & l = controller.scene(lvl)->objectPickerView;
|
|
|
|
l.highlight(pred);
|
|
|
|
l.update();
|
|
|
|
QObject::connect(&l, &ObjectPickerLayer::selectionMade, this, &PlayerParams::onTownPicked);
|
2023-09-11 18:01:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dynamic_cast<QWidget*>(parent()->parent()->parent()->parent())->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlayerParams::onTownPicked(const CGObjectInstance * obj)
|
|
|
|
{
|
|
|
|
dynamic_cast<QWidget*>(parent()->parent()->parent()->parent())->show();
|
|
|
|
|
2023-09-11 18:44:55 +02:00
|
|
|
for(int lvl : {0, 1})
|
2023-09-11 18:01:53 +02:00
|
|
|
{
|
2023-09-11 18:44:55 +02:00
|
|
|
auto & l = controller.scene(lvl)->objectPickerView;
|
|
|
|
l.clear();
|
|
|
|
l.update();
|
|
|
|
QObject::disconnect(&l, &ObjectPickerLayer::selectionMade, this, &PlayerParams::onTownPicked);
|
2023-09-11 18:01:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!obj) //discarded
|
|
|
|
return;
|
|
|
|
|
2023-09-11 18:44:55 +02:00
|
|
|
for(int i = 0; i < ui->mainTown->count(); ++i)
|
|
|
|
{
|
|
|
|
auto town = controller.map()->objects.at(ui->mainTown->itemData(i).toInt());
|
|
|
|
if(town == obj)
|
|
|
|
{
|
|
|
|
ui->mainTown->setCurrentIndex(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-09-11 18:01:53 +02:00
|
|
|
}
|
2023-09-11 18:44:55 +02:00
|
|
|
|