summaryrefslogtreecommitdiffstats
path: root/konquest/gamecore.h
blob: 9d82029f42686311a16c10cceba96ed184535e70 (plain)
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#ifndef _GAMECORE_H_
#define _GAMECORE_H_

#include <krandomsequence.h>

#include <tqobject.h>
#include <tqstring.h>
#include <tqcolor.h>
#include <tqptrlist.h>

// Board Size Constants
#define BOARD_ROWS 16
#define BOARD_COLS 16

// Maximum Number of Players
#define MAX_PLAYERS 10


//**********************************************************
// Forward declarations for classes in this file
//**********************************************************
class Player;
class Planet;
class Sector;
class Map;
class Fleet;


//**********************************************************
// Core Logic routines
//**********************************************************

class CoreLogic
{
public:
    CoreLogic();

    void generatePlanetCoordinates( int &x, int &y );
    double generateKillPercentage();
    int generatePlanetProduction();
    double generateMorale();

    double distance( Planet *p1, Planet *p2 );

    double roll();

private:
    KRandomSequence random;
    static bool class_init;
};


//**********************************************************
// class Fleet
// \--- class AttackFleet
//  \--- class DefenseFleet
//**********************************************************

class Fleet : public TQObject
{
        
public:

    Fleet( int initialShipCount );
    virtual ~Fleet() {}

    int getShipCount();
    void removeShips( int lostShips );

protected:
    int shipCount;
};

class AttackFleet : public Fleet
{

public:
    AttackFleet( Planet *source, Planet *dest, int initialCount, double arrivalTurn  );

    Player *owner;
    Planet *destination;
    double arrivalTurn;
    double killPercentage;

};


class DefenseFleet : public Fleet
{
        
public:
    DefenseFleet( Planet *newHome, int initialCount  );

    void absorb( AttackFleet *fleet );
    void become( AttackFleet *fleet );

    void addShips( int newShips );
    
    AttackFleet *spawnAttackFleet( Planet *destination, int shipCount, double arrivalTurn );

    Planet *home;

};


//**************************************************************
// class Player
//**************************************************************

class Player : public TQObject
{

public:
    Player( TQString newName, TQColor color, int number, bool isAi );
    virtual ~Player();

    enum { NEUTRAL_PLAYER_NUMBER = -1 };
    
public:
    TQString &getName();
    TQString getColoredName();
    TQColor &getColor();
    bool isNeutral();
    TQPtrList<AttackFleet> &getAttackList();

    // factory functions
    static Player *createPlayer( TQString newName, TQColor newColor, int playerNum, bool isAi  );
    static Player *createNeutralPlayer();

    bool NewAttack( Planet *sourcePlanet, Planet *destPlanet, int shipCount, int departureTurn );

    bool operator==( const Player &otherPlayer ) const;

    bool isInPlay();
    void setInPlay( bool );
    
private:
    TQString name;
    TQColor  color;
    int playerNum;
    bool inPlay;
    bool aiPlayer;

    TQPtrList<AttackFleet> attackList;

    // statistics counters
    int shipsBuilt;
    int planetsConquered;
    int fleetsLaunched;
    int enemyFleetsDestroyed;
    int enemyShipsDestroyed;

public:
    void statShipsBuilt( int );
    void statPlanetsConquered( int );
    void statFleetsLaunched( int );
    void statEnemyFleetsDestroyed( int );
    void statEnemyShipsDestroyed( int );

    int getShipsBuilt() { return shipsBuilt; }
    int getPlanetsConquered() { return  planetsConquered; }
    int getFleetsLaunched() { return  fleetsLaunched; }
    int getEnemyFleetsDestroyed() { return  enemyFleetsDestroyed; }
    int getEnemyShipsDestroyed() { return  enemyShipsDestroyed; }
    bool isAiPlayer();

};


//**************************************************************
// class Planet
//**************************************************************

class Planet : public TQObject
{
    TQ_OBJECT
  

private:

    Planet( TQString planetName, Sector &newParentSector,
            Player *initialOwner, int newProd,
            double newKillP, double newMorale );

public:
    virtual ~Planet();

    static Planet *createPlayerPlanet( Sector &parentSector,
                                       Player *initialOwner, TQString planetName );
    static Planet *createNeutralPlanet( Sector &parentSector,
                                        Player *initialOwner, TQString planetName );

    Sector &getSector() const;
    Player *getPlayer() const;
    const TQString &getName() const;
    DefenseFleet &getFleet();

    double getKillPercentage();
    void setKillPercentage( double newValue );
    double getMorale();
    void setMorale( double );
    int getProduction();
    void setProduction( int );

    void select();
    void conquer(  AttackFleet *conqueringFleet );
    void coup( Player *luckyPlayer );
    void turn();

signals:
    void update();
    void selected();

private:
    TQString name;
    Player *owner;
    Sector &parentSector;
    DefenseFleet homeFleet;

    double killPercentage;
    double morale;
    int productionRate;
};

//***************************************************************
// class Sector
//***************************************************************

class Sector : public TQObject
{
    TQ_OBJECT
  

public:

    // constructors
    Sector();
    Sector( Map *parentMap, int xpos, int ypos );
    Sector( const Sector & );

    // assignment operator (makes initialization easy)
    Sector &operator=( const Sector & );

    bool hasPlanet() const;
    void setPlanet( Planet *newPlanet );
    Planet *getPlanet();
    void removePlanet();

    void select();
    
    int getRow();
    int getColumn();

signals:
    void update();
    void selected();

protected slots:
    void childPlanetUpdate( );

    
protected:
    Planet *planet;  // a sector has 0 or 1 planets
    Map *parentMap;
    int x, y;

};
 
//*****************************************************************
// class Map
//*****************************************************************

class Map : public TQObject
{
    TQ_OBJECT
  

public:
    Map();
    virtual ~Map();

    const int getRows() const;
    const int getColumns() const;

    void populateMap( TQPtrList<Player> &players, Player *neutral,
                      int numNeutralPlanets, TQPtrList<Planet> &thePlanets );
    void clearMap();
    
    bool selectedSector( int &x, int &y ) const;
    void setSelectedSector( int x, int y );
    void setSelectedSector( const Planet & );
    void setSelectedSector();

    Sector &getSector( int x, int y );

protected slots:
    void childSectorUpdate();

signals:
    void update();

protected:
    void Freeze();
    void Thaw();
    bool freezeUpdates;

private:
    Sector &findRandomFreeSector();
    
protected:
    Sector grid[BOARD_ROWS][BOARD_COLS];  // a map is a 2-D array of sectors;
    const int rows; // size of grid in sectors
    const int columns;

    // This is used to implement a selected sector,
    // one who's boarder flashes.
    bool hasSelectedSector;
    int  sel_x, sel_y;
};

//---------------------------------------------------------------------------------
// Typedefs
//---------------------------------------------------------------------------------
typedef TQPoint Coordinate;  // Gotta start using this instead of int x,y crap
typedef TQPtrList<AttackFleet> AttackFleetList;
typedef TQPtrListIterator<AttackFleet> AttackFleetListIterator;
typedef TQPtrList<Player> PlayerList;
typedef TQPtrList<Planet> PlanetList;
typedef TQPtrListIterator<Player> PlayerListIterator;
typedef TQPtrListIterator<Planet> PlanetListIterator;

#endif // _GAMECORE_H_