File: game.cpp

package info (click to toggle)
qonk 0.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 2,076 kB
  • ctags: 2,026
  • sloc: cpp: 14,978; sh: 3,464; makefile: 200
file content (377 lines) | stat: -rw-r--r-- 7,529 bytes parent folder | download | duplicates (3)
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "game.h"
#include "gameoptions.h"

#include "timer.h"
#include "planets.h"
#include "players.h"
#include "universe.h"
#include "messages.h"
#include "timer.h"
#include "settings.h"
#include "canvas.h"
#include "selection.h"

#include "extensions.h"

using namespace std;

Game::Game(GameOptions &go)
  : gameOptions(go), nextNumberOfPlanets(0), nextNumberOfComputerPlayers(0), timer(new Timer()),
    players(0), universe(0), messages(0), selection(0)
{
  int numberOfPlanets = gameOptions.getPlanets();
  int numberOfComputerPlayers = gameOptions.getPlayers();

  state = PLAYING;
  quit = false;
  timer->start();

  // Initialize selection
  x = Settings::getScreenWidth()/2;
  y = Settings::getScreenHeight()/2;
  xp = xn = yp = yn = 0;

  universe = new Universe( numberOfPlanets );

  players = new Players( universe );
  universe->actionQueue->scheduleAction( 1000, new UpdatePlayersStatsAction( players) );

  humanPlayer = new HumanPlayer( universe, 0xffffff, 1);
  humanPlayer->setVisible(true);
  universe->claim(0, humanPlayer, 1, 3);
  players->push_back(humanPlayer);
  
  bool vis = gameOptions.getEnemyVisibility();
     
  int hueCounter = rand() % 360;
  for (int i=0;i<numberOfComputerPlayers;i++)
  {
    Player *newPlayer = new ComputerPlayer( universe, HSVtoRGB( hueCounter, 0.9, 0.9 ), 2);
    newPlayer->setVisible(vis);
    universe->claim(0, newPlayer, 1, 3);
    hueCounter = ( hueCounter + 360 / numberOfComputerPlayers ) % 360;
    players->push_back(newPlayer);
  }

  NeutralPlayer *neutral = new NeutralPlayer(universe, 0x808080);
  neutral->setVisible(vis);
  universe->claimRemaining(0, neutral, 3, 2);
  players->push_back(neutral);

  messages = new Messages();
  selection = new Selection();
}

Game::~Game()
{
  delete players;
  delete universe;
  delete messages;
  delete selection;
  delete timer;
}

bool
Game::run(bool menuVisible)
{
  if (quit)
    return false;

  // TODO: Find a way to live without the next line.
  setPaused(menuVisible);

	if (timer->isPaused())
    return true;

  moveCursor(xp - xn, yp - yn);

  Uint32 time = getTime();
  // update the universe
  universe->update(time);

  // let players update their states
  players->update(this);
		
	if( state == PLAYING )
  {
    if(checkLost()) {
      setEnemyVisibility(true);
      state = LOST;
      MSGlost(this);
    }

		if( checkWin()) {
      nextNumberOfPlanets = gameOptions.getPlanets() + 1;
      nextNumberOfComputerPlayers = gameOptions.getPlayers() + 1;
        
      if( nextNumberOfPlanets == 11 )
      {
        int d = nextNumberOfPlanets - nextNumberOfComputerPlayers;
        if( d != 1 )
          nextNumberOfPlanets = 10;
      }

			MSGwon(this, nextNumberOfPlanets, nextNumberOfComputerPlayers);
			state = WON;
		}
	}

  return true;
}

void
Game::handle(GameAction gameAction, int value)
{
  if (timer->isPaused())
    return;

  switch (gameAction)
  {
    case GA_NEXT_ROUND:
      if (!value) return;
      if(state==WON)
      {
        quit = true;
      }
      gameOptions.setPlanets(nextNumberOfPlanets);
      gameOptions.setPlayers(nextNumberOfComputerPlayers);

      break;
    case GA_RESTART_ROUND:
      if (!value) return;
      quit = true;
      break;
    case GA_SELECT_ALL:
      if (value)
        selectAllPlanets();
      break;
    case GA_TOGGLE_ENEMY_VISIBILITY:
      if (value)
      {
        gameOptions.setEnemyVisibility(!gameOptions.getEnemyVisibility());
        setEnemyVisibility(gameOptions.getEnemyVisibility());
      }
      break;
    case GA_SELECTION:
      if (value)
        startSelection(x, y);
      else
        endSelection(x, y);
      break;
    case GA_SELECT_NEAREST_PLANET:
      if (value)
        selectNearestPlanet(x, y);
      break;
    case GA_MOVE_TO_NEAREST_PLANET:
      if (value)
        moveToNearestPlanet(x, y);
      break;
    case GA_FLEET_STRENGTH_UP:
      if (value)
        fleetStrengthUp();
      break;
    case GA_FLEET_STRENGTH_DOWN:
      if (value)
        fleetStrengthDown();
      break;
    case GA_SET_FLEET_STRENGTH_SINGLE:
      if (value)
        setFleetStrength(1);
      break;
    case GA_CURSOR_UP:
        yn = value;
        break;
    case GA_CURSOR_DOWN:
        yp = value;
        break;
    case GA_CURSOR_LEFT:
        xn = value;
        break;
    case GA_CURSOR_RIGHT:
        xp = value;
        break;
    default:
      if (value 
          && gameAction >= GA_SET_FLEET_STRENGTH_10
          && gameAction <= GA_SET_FLEET_STRENGTH_100)
      {
        setFleetStrength(10 * (1 + gameAction - GA_SET_FLEET_STRENGTH_10));
      }
  }
}

void
Game::moveCursor(int xrel, int yrel)
{
  x += xrel;
  y += yrel;

  x = max(0, min(x, Settings::getScreenWidth()));
  y = max(0, min(y, Settings::getScreenHeight()));
  
  if (selection->isSelecting())
    selection->update(x, y);
  else
    universe->highlightNearestPlanet(x, y);
}

bool
Game::checkWin()
{
        bool allPlayersDead = true;
        for( Players::iterator i = players->begin(); i != players->end(); i++ ) {
                if( (*i)->getTeam() == 2 )
                
                if( ((ComputerPlayer*)(*i))->getPoints() > 0 )
                        allPlayersDead = false;
        }

        return allPlayersDead;
}

bool
Game::checkLost()
{
  return humanPlayer->getPoints() == 0;
}

void
Game::fleetStrengthUp()
{
  int str = humanPlayer->getFleetStrength();
  if (str == 1)
    str = 10;
  else if (str == 100)
    str = 1;
  else
    str += 10;

  setFleetStrength(str);
}

void
Game::fleetStrengthDown()
{
  int str = humanPlayer->getFleetStrength();
  if (str == 1)
    str = 100;
  else if (str == 10)
    str = 1;
  else
    str -= 10;

  setFleetStrength(str);
}

void
Game::setFleetStrength(int str)
{
  humanPlayer->setFleetStrength(str);
  
  stringstream s;
  if (str == 1)
	  s << "Single ship mode";
  else
  {
	  s << "Fleet strength " << str << "%";
  }

  messages->setFleetStrengthMessage( Message(getTime(), s.str(), 0x808080) );
}

void
Game::setPaused(bool p)
{
  if (p ^ timer->isPaused())
    timer->pause();

}

void
Game::setEnemyVisibility(bool visible)
{
  int humanTeam = humanPlayer->getTeam();

  for( Players::iterator i = players->begin(); i != players->end(); i++ )
  {
    Player *p = *i;
    if (p->getTeam() != humanTeam)
      p->setVisible(visible);
  }

  stringstream s;
  s << "Made enemies " << (visible ? "visible" : "invisible");

  messages->addMessage(timer->getTime(), Message(getTime(), s.str(), 20000, 0x808080));
}

void
Game::selectAllPlanets()
{
  humanPlayer->selectAllPlanets();
}

void
Game::selectNearestPlanet(int x, int y)
{
  humanPlayer->selectNearestPlanet(x, y);
}

void
Game::moveToNearestPlanet(int x, int y)
{
  humanPlayer->moveToNearestPlanet(getTime(), x, y);
}

void
Game::startSelection(int x, int y)
{
  selection->start(x, y);
}

void
Game::updateSelection(int x, int y)
{
  selection->update(x, y);
}

void
Game::endSelection(int x, int y)
{
  selection->end();

  if (selection->isEmpty())
    humanPlayer->selectPlanetAt(x, y);
  else
    universe->planets->sourceSelect(selection, humanPlayer);
}

void
Game::addMessage(int offset, Message msg)
{
  messages->addMessage( timer->getTime() + offset, msg);
}

Uint32
Game::getTime() const
{
  return timer->getTime();
}

void
Game::render()
{
  Uint32 time = getTime();
  
  universe->renderBackground(time);
  players->render();

  humanPlayer->render();
  selection->render();

  universe->renderForeground(time);

  messages->render(time);

  Canvas::drawCursor(x, y);
}