File: ships.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 (359 lines) | stat: -rw-r--r-- 7,496 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
// Copyright 2005 by Anthony Liekens anthony@liekens.net

#include <iostream>
#include "math.h"

#include "coordinate.h"
#include "planets.h"
#include "timer.h"
#include "extensions.h"
#include "selection.h"
#include "canvas.h"

#include "ships.h"

/* ##### SHIP ##### */

Ship::Ship( Player* owner, Planet *planet, Uint32 time ) {
	this->owner = owner;
	this->planet = planet;
	planet->addResident( this, time );
	selected = false;
	shipState = RESIDENT;
	speed = 0.003 + frand( 0.002 );
}

void
Ship::die() {
	shipState = DEAD;
}

void
Ship::moveTo( Uint32 time, Planet* destinationPlanet, ActionQueue* actionQueue ) {
	if(shipState == RESIDENT) {
	
		if( planet != destinationPlanet ) {
		
			// we're going to start moving
		
			shipState = MOVING;
			planet->removeResident( this );
			setLocation( planet->getLocation() );
			selected = false;
			
		} else {
		
			// we are already at the planet where we are supposed to go, so no need to move
		
			selected = false;
			return; 
			
		}
		
	} else {
	
		// we're already moving
	
		setLocation( toLocation );
		
	}
	
	// schedule our movement for the next 100 milliseconds
	
	fromTime = time;
	toTime = time + 100;
	fromLocation = location;
	Coordinate destinationLocation = destinationPlanet->getLocation();
	double distance = fromLocation.distance( destinationLocation );
	if( distance < speed ) {
	
		// we are close enough to arrive at our destination planet
		
		shipState = RESIDENT;
		planet = destinationPlanet;
		destinationPlanet->addResident( this, time );
		
	} else {
	
		// we are on the move, schedule the next movement step within 100 milliseconds
	
		double dx = destinationLocation.getX() - fromLocation.getX();
		double dy = destinationLocation.getY() - fromLocation.getY();
		dx = speed * dx / distance;
		dy = speed * dy / distance;
		toLocation.setX( fromLocation.getX() + dx );
		toLocation.setY( fromLocation.getY() + dy );
		ShipMovementAction* shipMovementAction = new ShipMovementAction( this, destinationPlanet );
		actionQueue->scheduleAction( toTime, shipMovementAction );
	}
}

void
Ship::render( Uint32 color ) const {
	Coordinate loc = getLocation();
	if( shipState == RESIDENT ) {
		Coordinate planetLocation = planet->getLocation();
		Canvas::drawResidentShip(loc, planetLocation, color);
	} else {
		Canvas::drawFlyingShip(loc, getDirection(), color);
	}
}

void
Ship::renderSelection() const {
	Coordinate loc = getLocation();
	if( selected )
	  Canvas::drawSelection(loc);
}

double
Ship::distance( Ship* ship ) {
	return ship->getLocation().distance( getLocation() );
}

void
Ship::setLocation( double x, double y ) {
	location.setX( x );
	location.setY( y );
}

void
Ship::setLocation( const Coordinate& c ) {
	location = c;
}

void
Ship::update(Uint32 time) 
{
	if( shipState != RESIDENT )
  {
		orbitLocation.setX( fromLocation.getX() + ( toLocation.getX() - fromLocation.getX() ) * ( time - fromTime ) / ( toTime - fromTime ) );
		orbitLocation.setY( fromLocation.getY() + ( toLocation.getY() - fromLocation.getY() ) * ( time - fromTime ) / ( toTime - fromTime ) );
	}
}

Coordinate
Ship::getLocation() const
{
  if( shipState == RESIDENT )
    return location;
  else
    return orbitLocation;
}

Ship::ShipStates
Ship::getShipState() const {
	return shipState;
}

void
Ship::setSelected( bool selected ) {
	this->selected = selected;
}

bool
Ship::getSelected() const {
	return selected;
}

double
Ship::getSpeed() const {
	return speed;
}

Player* 
Ship::getOwner() const {
	return owner;
}

Planet *
Ship::getPlanet() const {
	return planet;
}

double
Ship::getDirection() const {
	if(shipState == RESIDENT){
		return 0;
	} else if( shipState == MOVING ) {
		double dx = toLocation.getX() - location.getX();
		double dy = toLocation.getY() - location.getY();
		double l = sqrt( dx*dx + dy*dy ); // length of vector
		double angle = acos( dx / l );
		if( dy < 0 ) angle = -angle;
		return angle;
	}
}

/* ##### SHIPS ##### */

void
Ships::removeDeadShips() {
	remove_if( DeadShipRemovalFunctor() );
}

void
Ships::moveTo( Uint32 time, Planet* destinationPlanet, ActionQueue* actionQueue ) {
	for( iterator i = begin(); i != end(); i++ ) {
		if( (*i)->getSelected() )
			(*i)->moveTo( time + rand() % 500, destinationPlanet, actionQueue );
	}
}

int
Ships::numberSelectedShips() {
	int counter = 0;
	for( iterator i = begin(); i != end(); i++ ) {
		if( (*i)->getSelected() )
			counter++;
	}
	return counter;
}

void
Ships::render( Uint32 color ) const {
  for( const_iterator i = begin(); i != end(); i++ ) {
    (*i)->renderSelection();
  }
  for( const_iterator i = begin(); i != end(); i++ ) {
    (*i)->render(color );
  }
}

void
Ships::update(Uint32 time)
{
  for( const_iterator i = begin(); i != end(); i++ ) {
    (*i)->update(time);
  }
}

void
Ships::select( Selection* selection ) {

	double minX = selection->getMinX();
	double maxX = selection->getMaxX();
	double minY = selection->getMinY();
	double maxY = selection->getMaxY();
	
	for( iterator i = begin(); i != end(); i++ ) {
		Coordinate location = (*i)->getLocation();
		if( (*i)->getShipState() == Ship::RESIDENT ) // you can only select ships when they reside on a planet for now
			if( ( location.getX() > minX ) && ( location.getX() < maxX ) && ( location.getY() > minY ) && ( location.getY() < maxY ) )
				(*i)->setSelected( true );
			else
				(*i)->setSelected( false );
	}
	
}

void
Ships::selectAll() {

	for( iterator i = begin(); i != end(); i++ ) {
		if( (*i)->getShipState() == Ship::RESIDENT ) // you can only select ships when they reside on a planet for now
			(*i)->setSelected( true );
	}
	
}

int
Ships::countResidents() {
	int counter = 0;
	for( const_iterator i = begin(); i != end(); i++ ) {
		if( (*i)->getShipState() == Ship::RESIDENT )
			counter++;
	}
	return counter;
}

Ship*
Ships::getRandomShip() {
	int number = rand() % size();
	iterator i = begin();
	for( int k = 0; k < number; k++ )
		i++;
	return (*i);
}

Ship*
Ships::getRandomResidentShip() {
	if( countResidents() > 0 ) {
		Ship* result;
		bool done = false;
		do {
			result = getRandomShip();
			if( result->getShipState() == Ship::RESIDENT )
				done = true;
		} while( !done );
		return result;
	} else {
		return NULL;
	}
}

Ship*
Ships::getRandomNearbyResidentShip( Ship* ship ) {

	if( size() == 1 )
		return getRandomResidentShip();

	Ship* result;
	do {
		result = getRandomResidentShip();
	} while( result == ship );
	double distance = ship->distance( result );
	
	for( int i = 0; i < 10; i++ ) {

		Ship* otherShip;
		do {
			otherShip = getRandomResidentShip();
		} while( otherShip == ship );
		double otherDistance = ship->distance( otherShip );
		
		if( otherDistance < distance ) {
			result = otherShip;
			distance = otherDistance;
		}
		
	}
	
	return result;
}

Ship*
Ships::getNearestResidentShip( Planet* c ) {

	Ship* result = NULL;
	double max = 1000;
	
	Coordinate location = c->getLocation();
	
	for( iterator i = begin(); i != end(); i++ ) {
		if( (*i)->getShipState() == Ship::RESIDENT )
		if( (*i)->getPlanet() != c )
		if( location.distance( (*i)->getLocation() ) < max ) {
			result = (*i);
			max = location.distance( (*i)->getLocation() );
		}
	}

	return result;
}

/* ##### SHIPMOVEMENTACTION ##### */

ShipMovementAction::ShipMovementAction() {
}

ShipMovementAction::ShipMovementAction( Ship* ship, Planet* planet ) {
	this->ship = ship;
	this->destination = planet;
}

void
ShipMovementAction::execute( const Uint32& time ) {
	if( ship )
		ship->moveTo( time, destination, getActionQueue() );
}