1/*
2 Copyright 2003 Russell Steffen <rsteffen@bayarea.net>
3 Copyright 2003 Stephan Zehetner <s.zehetner@nevox.org>
4 Copyright 2006 Dmitry Suzdalev <dimsuz@gmail.com>
5 Copyright 2006 Inge Wallin <inge@lysator.liu.se>
6 Copyright 2006 Pierre Ducroquet <pinaraf@gmail.com>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22
23#include "planet.h"
24#include "sector.h"
25#include "fleet.h"
26#include "game.h"
27#include "players/player.h"
28#include <krandomsequence.h>
29#include <KDebug>
30
31//---------------------------------------------------------------------------
32// class Planet
33//---------------------------------------------------------------------------
34
35Planet::Planet( const QString &planetName, Sector *sector, Player *initialOwner,
36 int newProd, double newKillP )
37 : m_name(planetName),
38 m_owner(initialOwner),
39 m_sector(sector),
40 m_homeFleet( this, 0 ),
41 m_killPercentage(newKillP),
42 m_productionRate(newProd), m_originalProductionRate(newProd),
43 m_oldShips(newProd),
44 m_showCurShips(true),
45 m_justconquered(false)
46{
47 KRandomSequence r;
48 m_planetLook = r.getLong(10);
49 connect(&m_homeFleet, SIGNAL(update()), this, SIGNAL(update()));
50 m_sector->setPlanet( this );
51}
52
53Planet::~Planet() { m_sector->removePlanet(); }
54
55
56Planet *
57Planet::createPlayerPlanet( Sector *sector, Player *initialOwner,
58 const QString &planetName )
59{
60 return new Planet( planetName, sector, initialOwner, 10, 0.400 );
61}
62
63
64Planet *
65Planet::createNeutralPlanet( Sector *sector, Player *initialOwner,
66 const QString &planetName )
67{
68 double killP = Game::generateKillPercentage();
69 int productionRate = (int) Game::generatePlanetProduction();
70
71 return new Planet( planetName, sector,
72 initialOwner, productionRate, killP );
73}
74
75void
76Planet::conquer( AttackFleet *conqueringFleet )
77{
78 m_owner->deleteStandingOrders(this);
79 m_owner = conqueringFleet->owner;
80 m_owner->statPlanetsConquered(1);
81 m_homeFleet.become( conqueringFleet );
82 m_productionRate = m_originalProductionRate;
83 m_justconquered = true;
84}
85
86void
87Planet::turn(const GameOptions &options)
88{
89 kDebug() << "Planet::turn...";
90
91 if (options.ProductionAfterConquere || !m_justconquered) {
92 if (m_owner->isNeutral()) {
93 m_homeFleet.addShips(options.NeutralsProduction);
94 m_owner->statShipsBuilt(options.NeutralsProduction);
95 }
96 else {
97 m_homeFleet.addShips(m_productionRate);
98 m_owner->statShipsBuilt(m_productionRate);
99 }
100
101 m_owner->statShipCount(m_homeFleet.shipCount());
102
103 if (options.CumulativeProduction) {
104 m_productionRate++;
105 }
106 }
107
108 m_oldShips = m_homeFleet.shipCount();
109 m_showCurShips = true;
110 m_justconquered = false;
111 emit update();
112}
113