File indexing completed on 2024-10-13 03:44:43

0001 /*
0002     SPDX-FileCopyrightText: 2006 Pierre Ducroquet <pinaraf@pinaraf.info>
0003     SPDX-FileCopyrightText: 2011 Jeffrey Kelling <overlordapophis@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "player.h"
0009 #include <QDebug>
0010 #include "../game.h"
0011 #include "../planet.h"
0012 
0013 Player::Player(Game *game, const QString &newName, const QColor &color) :
0014     QState(&(game->m_gameMachine)),
0015     m_game(game),
0016     m_name(newName),
0017     m_color(color),
0018     m_shipsBuilt(0),
0019     m_planetsConquered(0),
0020     m_fleetsLaunched(0),
0021     m_enemyFleetsDestroyed(0),
0022     m_enemyShipsDestroyed(0),
0023     m_turnProduction(0),
0024     m_turnShips(0)
0025 {
0026 }
0027 
0028 void Player::onEntry(QEvent *event)
0029 {
0030     if (isNeutral())
0031         m_game->newTurn();
0032 
0033     //qDebug() << "Entering state for player " << m_name;
0034     //qDebug() << this->metaObject()->className();
0035     m_game->setCurrentPlayer(this);
0036     Q_UNUSED(event);
0037     if (isDead())
0038         Q_EMIT donePlaying();
0039     else
0040         play();
0041 }
0042 
0043 void Player::onExit(QEvent *event)
0044 {
0045     Q_UNUSED(event);
0046     //qDebug() << "Exiting state for player " << m_name;
0047     //qDebug() << "We are moving our new attacks to our attacks";
0048     for(AttackFleetList::iterator a = m_standingOrders.begin(); a != m_standingOrders.end(); ++a)
0049     {
0050         AttackFleet* fleet = (*a)->source->fleet().spawnAttackFleet((*a)->destination, (*a)->shipCount(), (*a)->arrivalTurn);
0051         ++(*a)->arrivalTurn;
0052         if(fleet)
0053             m_newAttacks << fleet;
0054     }
0055     m_attackList += m_newAttacks;
0056     statFleetsLaunched(m_newAttacks.size());
0057     m_newAttacks.clear();
0058 }
0059 
0060 bool Player::isDead()
0061 {
0062     if (!m_attackList.isEmpty())
0063         return false;
0064     for (Planet *planet : m_game->planets())
0065         if (planet->player() == this)
0066             return false;
0067     return true;
0068 }
0069 
0070 bool Player::isAiPlayer()
0071 {
0072     return false;
0073 }
0074 
0075 bool Player::isNeutral()
0076 {
0077     return false;
0078 }
0079 
0080 bool Player::isSpectator()
0081 {
0082     return false;
0083 }
0084 
0085 QString Player::coloredName() const
0086 {
0087     return QStringLiteral("<font color=\"%1\">%2</font>").arg(m_color.name(), m_name);
0088 }
0089 
0090 
0091 /**
0092  * Reset the turn statistics.
0093  */
0094 
0095 void
0096 Player::resetTurnStats()
0097 {
0098     m_turnProduction = 0;
0099     m_turnShips = 0;
0100 }
0101 
0102 
0103 void Player::attackDone(AttackFleet *fleet)
0104 {
0105     m_attackList.removeAll(fleet);
0106 }
0107 
0108 void Player::addAttackFleet(AttackFleet *fleet)
0109 {
0110     m_newAttacks << fleet;
0111 }
0112 
0113 void Player::addStandingOrder(AttackFleet *fleet)
0114 {
0115     m_standingOrders << fleet;
0116 }
0117 
0118 void Player::cancelNewAttack(AttackFleet *fleet)
0119 {
0120     if (!m_newAttacks.removeAll(fleet))
0121     {
0122         if(!m_standingOrders.removeAll(fleet))
0123             return;
0124     }
0125     else
0126         fleet->source->fleet().absorb(fleet);
0127     fleet->deleteLater();
0128 }
0129 
0130 void Player::deleteStandingOrders(Planet *planet)
0131 {
0132     for(AttackFleetList::iterator a = m_standingOrders.begin(); a != m_standingOrders.end(); )
0133     {
0134         if((*a)->source == planet)
0135         {
0136             (*a)->deleteLater();
0137             a = m_standingOrders.erase(a);
0138         }
0139         else
0140             ++a;
0141     }
0142 }
0143 
0144 #include "moc_player.cpp"