File: game.cpp

package info (click to toggle)
konquest 4%3A18.04.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,164 kB
  • sloc: cpp: 4,045; xml: 99; makefile: 5; sh: 2
file content (203 lines) | stat: -rw-r--r-- 6,248 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
    Copyright 2003 Russell Steffen <rsteffen@bayarea.net>
    Copyright 2003 Stephan Zehetner <s.zehetner@nevox.org>
    Copyright 2006 Dmitry Suzdalev <dimsuz@gmail.com>
    Copyright 2006 Inge Wallin <inge@lysator.liu.se>
    Copyright 2006 Pierre Ducroquet <pinaraf@gmail.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "game.h"
#include "players/player.h"
#include "planet.h"
#include <KLocalizedString>
#include <QDebug>
#include <cmath>

KRandomSequence Game::random = KRandomSequence();

Game::Game(QObject *parent) :
    QObject(parent)
{
    m_finalState = new QFinalState();
    m_turnCounter = 0;
    m_map = new Map(10, 10);
    m_gameMachine.addState(m_finalState);
    m_neutral = new NeutralPlayer(this);
    connect(&m_gameMachine, &QStateMachine::finished, this, &Game::finished);
}

const QList<Planet*> Game::planets()
{
    return m_map->planets();
}

bool Game::isRunning()
{
    return m_gameMachine.isRunning();
}

Coordinate Game::generatePlanetCoordinates(int x, int y)
{
    return Coordinate(random.getLong(x), random.getLong(y));
}

double Game::generateKillPercentage()
{
    // 0.30 - 0.90
    return 0.30 + random.getDouble()*0.60;
}

int Game::generatePlanetProduction()
{
    // 5 - 15
    return 5 + random.getLong(10);
}

bool Game::attack(Planet *sourcePlanet, Planet *destPlanet, long long shipCount, bool standingOrder)
{
    int arrival = int(std::ceil(m_map->distance(sourcePlanet, destPlanet))) + m_turnCounter;
    if(standingOrder)
    {
        m_currentPlayer->addStandingOrder(new AttackFleet(sourcePlanet, destPlanet, shipCount, arrival));
        return true;
    }
    else
    {
        AttackFleet *fleet = sourcePlanet->fleet().spawnAttackFleet(destPlanet, shipCount, arrival);
        if (fleet) {
            m_currentPlayer->addAttackFleet(fleet);
            return true;
        }
        return false;
    }
}

void Game::setPlayers(const QList<Player *> &players)
{
    m_players = players;
}

void Game::setCurrentPlayer(Player *player)
{
    m_currentPlayer = player;
}

bool Game::doFleetArrival(AttackFleet *fleet)
{
    // First, sanity check
    if (fleet->arrivalTurn != m_turnCounter)
        return false;

    // Check to see of (fleet owner) == (planet owner)
    // if the planet and fleet owner are the same, then merge the fleets
    // otherwise attack.
    if( fleet->owner == fleet->destination->player()) {
        fleet->destination->fleet().absorb(fleet);
        if ( !fleet->owner->isAiPlayer() )
            emit gameMsg(ki18np("Reinforcements (1 ship) have arrived for planet %2.",
                            "Reinforcements (%1 ships) have arrived for planet %2.")
                .subs(fleet->shipCount()), 0, fleet->destination);
    } else {
        // let's get ready to rumble...
        AttackFleet   *attacker       = fleet;
        Planet        *attackerPlanet = attacker->source;
        Planet        *defenderPlanet = attacker->destination;
        DefenseFleet  &defender       = defenderPlanet->fleet();

        bool  haveVictor  = false;
        bool  planetHolds = true;

        while( !haveVictor ) {
            double  attackerRoll = random.getDouble();
            double  defenderRoll = random.getDouble();

            /* special case if both have 0 kill percentages */
            if( defenderPlanet->killPercentage() == 0 &&
                attackerPlanet->killPercentage() == 0) {
                if(attackerRoll <  defenderRoll )
                    makeKill(&defender, attackerPlanet->player());
                else
                    makeKill(attacker, defenderPlanet->player());
            }

            if( defenderRoll < defenderPlanet->killPercentage() )
                makeKill(attacker, defenderPlanet->player());

            if( attacker->shipCount() <= 0 ) {
                haveVictor  = true;
                planetHolds = true;
                continue;
            }
            if( attackerRoll < attackerPlanet->killPercentage() )
                makeKill(&defender, attackerPlanet->player());

            if( defender.shipCount() <= 0 ) {
                haveVictor  = true;
                planetHolds = false;
            }
        }

        if( planetHolds ) {
            defenderPlanet->player()->statEnemyFleetsDestroyed(1);
            emit gameMsg(ki18n("Planet %2 has held against an attack from %1."),
                         attacker->owner, defenderPlanet);
        } else {
            Player *defender = defenderPlanet->player();
            attacker->owner->statEnemyFleetsDestroyed( 1 );

            defenderPlanet->conquer( attacker );

            emit gameMsg(ki18n("Planet %2 has fallen to %1."),
                         attacker->owner, defenderPlanet, defender);
        }
    }
    return true;
}

void Game::makeKill(Fleet *fleet, Player *player)
{
    fleet->removeShips( 1 );
    player->statEnemyShipsDestroyed( 1 );
}

void Game::findWinner()
{
    //qDebug() << "Searching for survivors";
    // Check for survivors
    Player *winner = 0;
    for (Player *player : players()) {
        if (player->isNeutral() || player->isSpectator()) {
            continue;
        }
        if (!player->isDead()) {
            if (winner) {
                //qDebug() << "Ok, returning 0";
                return;
            } else {
                winner = player;
            }
        }
    }
    //qDebug() << "Ok, returning " << winner;
    if (winner)
    {
        // We got a winner
        //qDebug() << "Trying to stop";
        this->stop();
        emit(finished());
    }
}