| 1 | // Copyright 2005 by Anthony Liekens [email protected] |
| 2 | |
| 3 | #include <iostream> |
| 4 | #include "math.h" |
| 5 | |
| 6 | #include "coordinate.h" |
| 7 | #include "planets.h" |
| 8 | #include "timer.h" |
| 9 | #include "extensions.h" |
| 10 | #include "selection.h" |
| 11 | #include "canvas.h" |
| 12 | |
| 13 | #include "ships.h" |
| 14 | |
| 15 | /* ##### SHIP ##### */ |
| 16 | |
| 17 | Ship::Ship( Player* owner, Planet *planet, Uint32 time ) { |
| 18 | this->owner = owner; |
| 19 | this->planet = planet; |
| 20 | planet->addResident( this, time ); |
| 21 | selected = false; |
| 22 | shipState = RESIDENT; |
| 23 | speed = 0.003 + frand( 0.002 ); |
| 24 | } |
| 25 | |
| 26 | void |
| 27 | Ship::die() { |
| 28 | shipState = DEAD; |
| 29 | } |
| 30 | |
| 31 | void |
| 32 | Ship::moveTo( Uint32 time, Planet* destinationPlanet, ActionQueue* actionQueue ) { |
| 33 | if(shipState == RESIDENT) { |
| 34 | |
| 35 | if( planet != destinationPlanet ) { |
| 36 | |
| 37 | // we're going to start moving |
| 38 | |
| 39 | shipState = MOVING; |
| 40 | planet->removeResident( this ); |
| 41 | setLocation( planet->getLocation() ); |
| 42 | selected = false; |
| 43 | |
| 44 | } else { |
| 45 | |
| 46 | // we are already at the planet where we are supposed to go, so no need to move |
| 47 | |
| 48 | selected = false; |
| 49 | return; |
| 50 | |
| 51 | } |
| 52 | |
| 53 | } else { |
| 54 | |
| 55 | // we're already moving |
| 56 | |
| 57 | setLocation( toLocation ); |
| 58 | |
| 59 | } |
| 60 | |
| 61 | // schedule our movement for the next 100 milliseconds |
| 62 | |
| 63 | fromTime = time; |
| 64 | toTime = time + 100; |
| 65 | fromLocation = location; |
| 66 | Coordinate destinationLocation = destinationPlanet->getLocation(); |
| 67 | double distance = fromLocation.distance( destinationLocation ); |
| 68 | if( distance < speed ) { |
| 69 | |
| 70 | // we are close enough to arrive at our destination planet |
| 71 | |
| 72 | shipState = RESIDENT; |
| 73 | planet = destinationPlanet; |
| 74 | destinationPlanet->addResident( this, time ); |
| 75 | |
| 76 | } else { |
| 77 | |
| 78 | // we are on the move, schedule the next movement step within 100 milliseconds |
| 79 | |
| 80 | double dx = destinationLocation.getX() - fromLocation.getX(); |
| 81 | double dy = destinationLocation.getY() - fromLocation.getY(); |
| 82 | dx = speed * dx / distance; |
| 83 | dy = speed * dy / distance; |
| 84 | toLocation.setX( fromLocation.getX() + dx ); |
| 85 | toLocation.setY( fromLocation.getY() + dy ); |
| 86 | ShipMovementAction* shipMovementAction = new ShipMovementAction( this, destinationPlanet ); |
| 87 | actionQueue->scheduleAction( toTime, shipMovementAction ); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | Ship::render( Uint32 color ) const { |
| 93 | Coordinate loc = getLocation(); |
| 94 | if( shipState == RESIDENT ) { |
| 95 | Coordinate planetLocation = planet->getLocation(); |
| 96 | Canvas::drawResidentShip(loc, planetLocation, color); |
| 97 | } else { |
| 98 | Canvas::drawFlyingShip(loc, getDirection(), color); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | Ship::renderSelection() const { |
| 104 | Coordinate loc = getLocation(); |
| 105 | if( selected ) |
| 106 | Canvas::drawSelection(loc); |
| 107 | } |
| 108 | |
| 109 | double |
| 110 | Ship::distance( Ship* ship ) { |
| 111 | return ship->getLocation().distance( getLocation() ); |
| 112 | } |
| 113 | |
| 114 | void |
| 115 | Ship::setLocation( double x, double y ) { |
| 116 | location.setX( x ); |
| 117 | location.setY( y ); |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | Ship::setLocation( const Coordinate& c ) { |
| 122 | location = c; |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | Ship::update(Uint32 time) |
| 127 | { |
| 128 | if( shipState != RESIDENT ) |
| 129 | { |
| 130 | orbitLocation.setX( fromLocation.getX() + ( toLocation.getX() - fromLocation.getX() ) * ( time - fromTime ) / ( toTime - fromTime ) ); |
| 131 | orbitLocation.setY( fromLocation.getY() + ( toLocation.getY() - fromLocation.getY() ) * ( time - fromTime ) / ( toTime - fromTime ) ); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | Coordinate |
| 136 | Ship::getLocation() const |
| 137 | { |
| 138 | if( shipState == RESIDENT ) |
| 139 | return location; |
| 140 | else |
| 141 | return orbitLocation; |
| 142 | } |
| 143 | |
| 144 | Ship::ShipStates |
| 145 | Ship::getShipState() const { |
| 146 | return shipState; |
| 147 | } |
| 148 | |
| 149 | void |
| 150 | Ship::setSelected( bool selected ) { |
| 151 | this->selected = selected; |
| 152 | } |
| 153 | |
| 154 | bool |
| 155 | Ship::getSelected() const { |
| 156 | return selected; |
| 157 | } |
| 158 | |
| 159 | double |
| 160 | Ship::getSpeed() const { |
| 161 | return speed; |
| 162 | } |
| 163 | |
| 164 | Player* |
| 165 | Ship::getOwner() const { |
| 166 | return owner; |
| 167 | } |
| 168 | |
| 169 | Planet * |
| 170 | Ship::getPlanet() const { |
| 171 | return planet; |
| 172 | } |
| 173 | |
| 174 | double |
| 175 | Ship::getDirection() const { |
| 176 | if(shipState == RESIDENT){ |
| 177 | return 0; |
| 178 | } else if( shipState == MOVING ) { |
| 179 | double dx = toLocation.getX() - location.getX(); |
| 180 | double dy = toLocation.getY() - location.getY(); |
| 181 | double l = sqrt( dx*dx + dy*dy ); // length of vector |
| 182 | double angle = acos( dx / l ); |
| 183 | if( dy < 0 ) angle = -angle; |
| 184 | return angle; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /* ##### SHIPS ##### */ |
| 189 | |
| 190 | void |
| 191 | Ships::removeDeadShips() { |
| 192 | remove_if( DeadShipRemovalFunctor() ); |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | Ships::moveTo( Uint32 time, Planet* destinationPlanet, ActionQueue* actionQueue ) { |
| 197 | for( iterator i = begin(); i != end(); i++ ) { |
| 198 | if( (*i)->getSelected() ) |
| 199 | (*i)->moveTo( time + rand() % 500, destinationPlanet, actionQueue ); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | int |
| 204 | Ships::numberSelectedShips() { |
| 205 | int counter = 0; |
| 206 | for( iterator i = begin(); i != end(); i++ ) { |
| 207 | if( (*i)->getSelected() ) |
| 208 | counter++; |
| 209 | } |
| 210 | return counter; |
| 211 | } |
| 212 | |
| 213 | void |
| 214 | Ships::render( Uint32 color ) const { |
| 215 | for( const_iterator i = begin(); i != end(); i++ ) { |
| 216 | (*i)->renderSelection(); |
| 217 | } |
| 218 | for( const_iterator i = begin(); i != end(); i++ ) { |
| 219 | (*i)->render(color ); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | void |
| 224 | Ships::update(Uint32 time) |
| 225 | { |
| 226 | for( const_iterator i = begin(); i != end(); i++ ) { |
| 227 | (*i)->update(time); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | void |
| 232 | Ships::select( Selection* selection ) { |
| 233 | |
| 234 | double minX = selection->getMinX(); |
| 235 | double maxX = selection->getMaxX(); |
| 236 | double minY = selection->getMinY(); |
| 237 | double maxY = selection->getMaxY(); |
| 238 | |
| 239 | for( iterator i = begin(); i != end(); i++ ) { |
| 240 | Coordinate location = (*i)->getLocation(); |
| 241 | if( (*i)->getShipState() == Ship::RESIDENT ) // you can only select ships when they reside on a planet for now |
| 242 | if( ( location.getX() > minX ) && ( location.getX() < maxX ) && ( location.getY() > minY ) && ( location.getY() < maxY ) ) |
| 243 | (*i)->setSelected( true ); |
| 244 | else |
| 245 | (*i)->setSelected( false ); |
| 246 | } |
| 247 | |
| 248 | } |
| 249 | |
| 250 | void |
| 251 | Ships::selectAll() { |
| 252 | |
| 253 | for( iterator i = begin(); i != end(); i++ ) { |
| 254 | if( (*i)->getShipState() == Ship::RESIDENT ) // you can only select ships when they reside on a planet for now |
| 255 | (*i)->setSelected( true ); |
| 256 | } |
| 257 | |
| 258 | } |
| 259 | |
| 260 | int |
| 261 | Ships::countResidents() { |
| 262 | int counter = 0; |
| 263 | for( const_iterator i = begin(); i != end(); i++ ) { |
| 264 | if( (*i)->getShipState() == Ship::RESIDENT ) |
| 265 | counter++; |
| 266 | } |
| 267 | return counter; |
| 268 | } |
| 269 | |
| 270 | Ship* |
| 271 | Ships::getRandomShip() { |
| 272 | int number = rand() % size(); |
| 273 | iterator i = begin(); |
| 274 | for( int k = 0; k < number; k++ ) |
| 275 | i++; |
| 276 | return (*i); |
| 277 | } |
| 278 | |
| 279 | Ship* |
| 280 | Ships::getRandomResidentShip() { |
| 281 | if( countResidents() > 0 ) { |
| 282 | Ship* result; |
| 283 | bool done = false; |
| 284 | do { |
| 285 | result = getRandomShip(); |
| 286 | if( result->getShipState() == Ship::RESIDENT ) |
| 287 | done = true; |
| 288 | } while( !done ); |
| 289 | return result; |
| 290 | } else { |
| 291 | return NULL; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | Ship* |
| 296 | Ships::getRandomNearbyResidentShip( Ship* ship ) { |
| 297 | |
| 298 | if( size() == 1 ) |
| 299 | return getRandomResidentShip(); |
| 300 | |
| 301 | Ship* result; |
| 302 | do { |
| 303 | result = getRandomResidentShip(); |
| 304 | } while( result == ship ); |
| 305 | double distance = ship->distance( result ); |
| 306 | |
| 307 | for( int i = 0; i < 10; i++ ) { |
| 308 | |
| 309 | Ship* otherShip; |
| 310 | do { |
| 311 | otherShip = getRandomResidentShip(); |
| 312 | } while( otherShip == ship ); |
| 313 | double otherDistance = ship->distance( otherShip ); |
| 314 | |
| 315 | if( otherDistance < distance ) { |
| 316 | result = otherShip; |
| 317 | distance = otherDistance; |
| 318 | } |
| 319 | |
| 320 | } |
| 321 | |
| 322 | return result; |
| 323 | } |
| 324 | |
| 325 | Ship* |
| 326 | Ships::getNearestResidentShip( Planet* c ) { |
| 327 | |
| 328 | Ship* result = NULL; |
| 329 | double max = 1000; |
| 330 | |
| 331 | Coordinate location = c->getLocation(); |
| 332 | |
| 333 | for( iterator i = begin(); i != end(); i++ ) { |
| 334 | if( (*i)->getShipState() == Ship::RESIDENT ) |
| 335 | if( (*i)->getPlanet() != c ) |
| 336 | if( location.distance( (*i)->getLocation() ) < max ) { |
| 337 | result = (*i); |
| 338 | max = location.distance( (*i)->getLocation() ); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | return result; |
| 343 | } |
| 344 | |
| 345 | /* ##### SHIPMOVEMENTACTION ##### */ |
| 346 | |
| 347 | ShipMovementAction::ShipMovementAction() { |
| 348 | } |
| 349 | |
| 350 | ShipMovementAction::ShipMovementAction( Ship* ship, Planet* planet ) { |
| 351 | this->ship = ship; |
| 352 | this->destination = planet; |
| 353 | } |
| 354 | |
| 355 | void |
| 356 | ShipMovementAction::execute( const Uint32& time ) { |
| 357 | if( ship ) |
| 358 | ship->moveTo( time, destination, getActionQueue() ); |
| 359 | } |