File indexing completed on 2024-02-25 04:09:20

0001 /*
0002     SPDX-FileCopyrightText: 2003 Russell Steffen <rsteffen@bayarea.net>
0003     SPDX-FileCopyrightText: 2003 Stephan Zehetner <s.zehetner@nevox.org>
0004     SPDX-FileCopyrightText: 2006 Dmitry Suzdalev <dimsuz@gmail.com>
0005     SPDX-FileCopyrightText: 2006 Inge Wallin <inge@lysator.liu.se>
0006     SPDX-FileCopyrightText: 2006 Pierre Ducroquet <pinaraf@gmail.com>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "planet.h"
0012 #include "sector.h"
0013 #include "fleet.h"
0014 #include "game.h"
0015 #include "players/player.h"
0016 #include <QRandomGenerator>
0017 #include <QDebug>
0018 
0019 //---------------------------------------------------------------------------
0020 // class Planet
0021 //---------------------------------------------------------------------------
0022 
0023 Planet::Planet( const QString &planetName, Sector *sector, Player *initialOwner,
0024                 int newProd, double newKillP )
0025   : m_name(planetName),
0026     m_owner(initialOwner),
0027     m_sector(sector),
0028     m_homeFleet( this, 0 ),
0029     m_killPercentage(newKillP),
0030     m_productionRate(newProd), m_originalProductionRate(newProd),
0031     m_oldShips(newProd),
0032     m_showCurShips(true),
0033     m_justconquered(false)
0034 {
0035     m_planetLook = QRandomGenerator::global()->bounded(10);
0036     connect(&m_homeFleet, &DefenseFleet::update, this, &Planet::update);
0037     m_sector->setPlanet( this );
0038 }
0039 
0040 Planet::~Planet() {  m_sector->removePlanet(); }
0041 
0042 
0043 Planet *
0044 Planet::createPlayerPlanet( Sector *sector, Player *initialOwner, 
0045                 const QString &planetName )
0046 {
0047     return new Planet( planetName, sector, initialOwner, 10, 0.400 );
0048 }
0049 
0050 
0051 Planet *
0052 Planet::createNeutralPlanet( Sector *sector, Player *initialOwner,
0053                  const QString &planetName )
0054 {
0055     double     killP          = Game::generateKillPercentage();
0056     int        productionRate = (int) Game::generatePlanetProduction();
0057 
0058     return new Planet( planetName, sector,
0059                        initialOwner, productionRate, killP );
0060 }
0061 
0062 void
0063 Planet::conquer( AttackFleet *conqueringFleet )
0064 {
0065     m_owner->deleteStandingOrders(this);
0066     m_owner = conqueringFleet->owner;
0067     m_owner->statPlanetsConquered(1);
0068     m_homeFleet.become( conqueringFleet );
0069     m_productionRate = m_originalProductionRate;
0070     m_justconquered = true;
0071 }
0072 
0073 void
0074 Planet::turn(const GameOptions &options)
0075 {
0076     //qDebug() << "Planet::turn...";
0077 
0078     if (options.ProductionAfterConquere || !m_justconquered) {
0079         if (m_owner->isNeutral()) {
0080             m_homeFleet.addShips(options.NeutralsProduction);
0081             m_owner->statShipsBuilt(options.NeutralsProduction);
0082         }
0083         else {
0084             m_homeFleet.addShips(m_productionRate);
0085             m_owner->statShipsBuilt(m_productionRate);
0086         }
0087 
0088         m_owner->statShipCount(m_homeFleet.shipCount());
0089 
0090         if (options.CumulativeProduction) {
0091             m_productionRate++;
0092         }
0093     }
0094 
0095     m_oldShips = m_homeFleet.shipCount();
0096     m_showCurShips = true;
0097     m_justconquered = false;
0098     Q_EMIT update();
0099 }
0100 
0101 #include "moc_planet.cpp"