/* Notes, you'll have to update you're DECLARE_DO_FUNs to include do_chandelle in mud.h
 * You will also have to update tables.c to include do_chandelle. If you do not know how to do
 * these things you probably shouldn't be coding, or are new, in which case you'll want to learn
 * them. I assume you have the wits to do it yourself. You will also have to use cedit to create
 * the command after compile and reboot. There might be some slight tweaking involved as well
 * because this comes straight from the code I wrote for my mud, there may be some variants or
 * deviants that do not apply, they have been marked. This command should probably be placed at
 * the end of space.c.
 * -Arcturus
 */

/* Chandelle- By Arcturus
 * An accurate chandelle would require an upraise in altitude, but since this is space flight
 * I ignored that. But a chandelle is a maneuver used to turn your craft around.
 * Hence, executing a chandelle, turns you around.
 */
void do_chandelle( CHAR_DATA *ch, char *argument )
{
    char  buf[MAX_STRING_LENGTH];
    int chance;
    SHIP_DATA *ship;

    /* Standard Piloting Checks */
    if (  (ship = ship_from_cockpit(ch->in_room->vnum))  == NULL )
    {
       send_to_char("&RYou must be in the cockpit of a ship to do that!\n\r",ch);
       return;
    }
    if ( ship->class > MOBILE_SUIT )
    {
        send_to_char("&RThis isn't a spacecraft!\n\r",ch);
        return;
    }
    if (  (ship = ship_from_pilotseat(ch->in_room->vnum))  == NULL )
    {
        send_to_char("&RYour not in the pilots seat.\n\r",ch);
        return;
    }

    if ( autofly(ship))
    {
         send_to_char("&RYou'll have to turn off the ships autopilot first.\n\r",ch);
         return;
    }
    if (ship->shipstate == SHIP_DISABLED)
    {
       send_to_char("&RThe ships drive is disabled. Unable to maneuver.\n\r",ch);
       return;
    }
    if  ( ship->class == SHIP_PLATFORM )
    {
       send_to_char( "&RPlatforms can't turn!\n\r" , ch );
       return;
    }

    if (ship->shipstate == SHIP_HYPERSPACE)
    {
       send_to_char("&RYou can only do that in realspace!\n\r",ch);
       return;
    }
    if (ship->shipstate == SHIP_DOCKED)
    {
        send_to_char("&RYou can't do that until after you've launched!\n\r",ch);
        return;
    }
    if (ship->shipstate != SHIP_READY)
    {
        send_to_char("&RPlease wait until the ship has finished its current maneuver.\n\r",ch);
        return;
    }
     /* My sabotage algorithm check, probably should remove. */
     if(IS_SET(ship->flags,  SHIPFLAG_SABOTAGEDENGINE))
     {
         echo_to_cockpit( AT_BLUE , ship , "There is a small explosion and then your ship stops moving. It must have been sabbotage." );
         echo_to_cockpit( AT_BLOOD + AT_BLINK , ship , "Ships Drive DAMAGED!");
         ship->shipstate = SHIP_DISABLED;
         ship->currspeed = 0;
         REMOVE_BIT(ship->flags, SHIPFLAG_SABOTAGEDENGINE);
         return;
     }
     /* End sabotage algorithm check. */
     if ( ship->energy < (ship->currspeed/10) )
     {
         send_to_char("&RTheres not enough fuel!\n\r",ch);
         return;
     }
     if( ship->class > MIDSIZE_SHIP )
     {
         send_to_char("&RThis ship is a little big to pull that kind of maneuver.\n\r", ch);
         return;
     }
     if ( ship->class == FIGHTER_SHIP )
         chance = IS_NPC(ch) ? ch->top_level
                : (int)  (ch->pcdata->learned[gsn_starfighters]) ;
     if ( ship->class == MIDSIZE_SHIP )
         chance = IS_NPC(ch) ? ch->top_level
                : (int)  (ch->pcdata->learned[gsn_midships]) ;
     if ( number_percent( ) > chance )
     {
         send_to_char("&RYou fail to work the controls properly.\n\r",ch);
         if ( ship->class == FIGHTER_SHIP )
             learn_from_failure( ch, gsn_starfighters );
         if ( ship->class == MIDSIZE_SHIP )
             learn_from_failure( ch, gsn_midships );
         return;
     }
     /* Use fuel and turn the ships headings around */
     ship->energy -= (ship->currspeed/10);
     ship->hx *= -1;
     ship->hy *= -1;
     ship->hz *= -1;
     ch_printf( ch, "You begin to execute the chandelle maneuver.\n\r");
     act( AT_PLAIN, "$n manipulates the ships controls.", ch, NULL, argument , TO_ROOM );
     echo_to_cockpit( AT_YELLOW ,ship, "The ship enters a chandelle maneuver.\n\r" );
     /* If its cloaked, might need to remove this block. Keep the statements inside of it. */
     if(!IS_SET(ship->flags, SHIPFLAG_CLOAKED)) 
     {
        sprintf( buf, "%s enters a chandelle maneuver." , ship->name );
        echo_to_system( AT_ORANGE , ship , buf , NULL );
     }

     /* Mobile Suits probably don't apply, this is junk that was semi-removed from my code but still faintly exists.
      * Best bet is just to remove || ship->class == MOBILE_SUIT 
      * -Arcturus
      */
     if ( ship->class == FIGHTER_SHIP || ship->class == MOBILE_SUIT || ( ship->class == MIDSIZE_SHIP && ship->manuever > 50 ) )
        ship->shipstate = SHIP_BUSY_3;
     else if ( ship->class == MIDSIZE_SHIP || ( ship->class == CAPITAL_SHIP && ship->manuever > 50 ) )
        ship->shipstate = SHIP_BUSY_2;
     else
        ship->shipstate = SHIP_BUSY;
     if ( ship->class == FIGHTER_SHIP )
        learn_from_success( ch, gsn_starfighters );
     if ( ship->class == MIDSIZE_SHIP )
        learn_from_success( ch, gsn_midships );
     return;
}