~ubuntu-branches/ubuntu/quantal/kdegames/quantal

« back to all changes in this revision

Viewing changes to konquest/player.cc

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:50 UTC
  • mfrom: (1.3.14)
  • Revision ID: package-import@ubuntu.com-20111215141750-6tj6brf4azhrt915
Tags: 4:4.7.90-0ubuntu1
new upstream beta release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    Copyright Russell Steffen <rsteffen@bayarea.net>
3
 
    Copyright Stephan Zehetner <s.zehetner@nevox.org>
4
 
    Copyright Dmitry Suzdalev <dimsuz@gmail.com>
5
 
    Copyright Inge Wallin <inge@lysator.liu.se>
6
 
    Copyright 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 "player.h"
24
 
#include "gamelogic.h"
25
 
 
26
 
#include <cmath>
27
 
using std::floor;
28
 
 
29
 
#include <qdebug.h>
30
 
 
31
 
#include "map.h"
32
 
 
33
 
 
34
 
//---------------------------------------------------------------------------
35
 
// class Player
36
 
//---------------------------------------------------------------------------
37
 
 
38
 
Player::Player( Map *map, const QString &newName, const QColor &color, int newPlrNum, enum AiLevel level )
39
 
  : m_map( map ),
40
 
    m_name( newName ), m_color( color ),
41
 
    m_playerNum( newPlrNum ),
42
 
    m_inPlay( true ),
43
 
    m_shipsBuilt(0),
44
 
    m_planetsConquered(0),
45
 
    m_fleetsLaunched(0),
46
 
    m_enemyFleetsDestroyed(0),
47
 
    m_enemyShipsDestroyed(0),
48
 
    m_AiLevel(level)
49
 
{
50
 
}
51
 
 
52
 
Player::~Player()
53
 
{
54
 
}
55
 
 
56
 
 
57
 
QString
58
 
Player::coloredName() const
59
 
{
60
 
    return QString("<font color=\"%1\">%2</font>").arg(m_color.name(), m_name);
61
 
}
62
 
 
63
 
 
64
 
Player *Player::createNeutralPlayer( Map *map )
65
 
{
66
 
    return new Player( map, "neutral", Qt::gray, NEUTRAL_PLAYER_NUMBER, Human );
67
 
}
68
 
 
69
 
 
70
 
bool
71
 
Player::NewAttack( Planet *sourcePlanet, Planet *destPlanet,
72
 
                   int shipCount, int turn )
73
 
{
74
 
    int          arrival = int(std::ceil(
75
 
                           m_map->distance( sourcePlanet, destPlanet )
76
 
                           )) + turn;
77
 
    AttackFleet *fleet;
78
 
 
79
 
    fleet = sourcePlanet->fleet().spawnAttackFleet( destPlanet, shipCount, 
80
 
                                                    arrival );
81
 
 
82
 
    if( fleet ) {
83
 
        m_newAttacks.append(fleet);
84
 
 
85
 
        return true;
86
 
    }
87
 
 
88
 
    return false;
89
 
}
90
 
 
91
 
void Player::doAiMove(GameLogic *gameLogic)
92
 
{
93
 
    int      ships, minimumShips, shipCountFactor;
94
 
    Planet  *target = 0;
95
 
    switch (getAiLevel())
96
 
    {
97
 
    case Human:
98
 
       // Error, we should not be called for human
99
 
       break;
100
 
    case ComputerWeak:
101
 
       minimumShips = 20;
102
 
       shipCountFactor = 2;
103
 
       break;
104
 
    case ComputerNormal: // Offensive
105
 
       minimumShips = 10;
106
 
       shipCountFactor = 2;
107
 
       break;
108
 
    case ComputerHard: // Defensive
109
 
       minimumShips = 30;
110
 
       shipCountFactor = 3;
111
 
       break;
112
 
    }
113
 
 
114
 
    foreach (Planet *home, gameLogic->planets()) {
115
 
        if (home->player() == gameLogic->currentPlayer()) {
116
 
            bool  hasAttack = false;
117
 
            ships = (int)floor(home->ships() * 0.7 );
118
 
                
119
 
            if (ships >= minimumShips) {
120
 
                double  minDistance = 100;
121
 
                    
122
 
                foreach (Planet *attack, gameLogic->planets()) {
123
 
                    bool    skip = false;
124
 
                    double  dist = gameLogic->map()->distance( home, attack );
125
 
                        
126
 
                    if (dist < minDistance
127
 
                        &&  attack->player() != gameLogic->currentPlayer()
128
 
                        && attack->ships() < ships ) {
129
 
                        foreach (AttackFleet *curFleet, gameLogic->currentPlayer()->attackList()) {
130
 
                            if (curFleet->destination == attack) {
131
 
                                skip = true;
132
 
                            }
133
 
                        }
134
 
                        if (skip) 
135
 
                            continue;
136
 
                            
137
 
                        target      = attack;
138
 
                        hasAttack   = true;
139
 
                        minDistance = dist;
140
 
                    }
141
 
                }
142
 
 
143
 
                if (hasAttack) {
144
 
                    //sendAttackFleet( home, target, ships );
145
 
                    gameLogic->currentPlayer()
146
 
                        ->NewAttack( home, target, ships, 
147
 
                                     gameLogic->turnNumber() );
148
 
                } else {
149
 
                    minDistance = 100;
150
 
                    int shipsToSend = 0;
151
 
                    bool hasDestination = false;
152
 
                        
153
 
                    foreach (Planet *attack, gameLogic->planets()) {
154
 
                        bool    skip = false;
155
 
                        double  dist = gameLogic->map()->distance( home, attack );
156
 
                        int     homeships = (int)floor(home->ships() * 0.5 );
157
 
                            
158
 
                        if (dist < minDistance
159
 
                            && attack->player() == gameLogic->currentPlayer()
160
 
                            && attack->ships() < homeships ) {
161
 
                            foreach (AttackFleet *curFleet,
162
 
                                     gameLogic->currentPlayer()->attackList()) {
163
 
                                if (curFleet->destination == attack) {
164
 
                                    skip = true;
165
 
                                }
166
 
                            }
167
 
                            if (skip)
168
 
                                continue;
169
 
                                
170
 
                            shipsToSend = (int)floor( double(home->ships()
171
 
                                                             - attack->ships()) / shipCountFactor);
172
 
                                
173
 
                            target         = attack;
174
 
                            hasDestination = true;
175
 
                            minDistance    = dist;
176
 
                        }
177
 
                    }
178
 
                    if (hasDestination) {
179
 
                        //FIXME: Move sendAttackFleet from GameView 
180
 
                        //       into GameLogic.
181
 
                        //sendAttackFleet( home, target, shipsToSend );
182
 
                        gameLogic->currentPlayer()
183
 
                            ->NewAttack( home, target, shipsToSend, 
184
 
                                         gameLogic->turnNumber() );
185
 
                    }
186
 
                }
187
 
            }
188
 
        }
189
 
    }
190
 
}