GlovePIE:FlightPIE

From WiiLi

Jump to: navigation, search


/**********************************************************
 *                      FlightPIE                         *
 *                     -----------                        *
 *              Created by Mitchell McKain                *
 *               <Hawk.McKain@gmail.com>                  *
 *             Last Modified Sept. 20th, 2008             *
 **********************************************************
 *             Released under GNU License v3              *
 **********************************************************
 *               *       About       *
 *               *********************
 * A Space/Flight Sim Script.
 * Verified to work in Descent II and Freespace but should
 * work in any other game that uses a joystick.
 * (Works smoothly in Battlezone with minor key assignment
 *  modifications.)
 *
 * Features on-the-fly calibration so you don't need to
 * hold the Wiimote at an unnatural angle.
 *
 * To calibrate the Wiimote simply hold 1 + 2 until it
 * rumbles and then press A + B to calibrate the Wiimote
 * and C + Z to calibrate the Nunchuk.
 *
 * Credit to Vattu for the variable analog idea.
 */

debug = "Hawk's Flight Simulator Script"

if not var.Calibrate
   PPJoy1.Analog0 = var.roll
   PPJoy1.Analog1 = var.pitch
   PPJoy1.Analog2 = var.throttle
   PPJoy1.Analog2 = var.analog3
   PPJoy1.Analog4 = Wiimote1.Nunchuk.JoyX

   // Joystick Buttons
   ppjoy1.Digital9 = wiimote1.up
   ppjoy1.Digital10 = wiimote1.down
   ppjoy1.Digital11 = wiimote1.left
   ppjoy1.Digital12 = wiimote1.Right
   ppjoy1.digital0 = wiimote1.A
   ppjoy1.digital1 = wiimote1.B
   ppjoy1.digital2 = wiimote1.minus
   ppjoy1.digital3 = wiimote1.home
   ppjoy1.digital4 = wiimote1.plus
   ppjoy1.digital5 = wiimote1.one
   ppjoy1.digital6 = wiimote1.two
   ppjoy1.digital7 = wiimote1.Nunchuk.ZButton

   // Menu Control Toggle
   if Wiimote1.Nunchuk.CButton
      key.up = Wiimote1.up
      key.down = Wiimote1.down
      key.left = Wiimote1.left
      key.right = Wiimote1.Right
      key.enter = Wiimote1.A
      key.escape = Wiimote1.B
   endif
endif

if not var.init
   // Set True to control ship orientation with the Nunchuk
   // (Default: False)
   var.useNunchuk = False

   // Set True to map the Nunchuk's Analog stick to WASD
   // (Default: False)
   var.mapNunchukToKeys = False

   // Allows you to swap between Nunchuk and Wiimote orientation
   // whenever you calibrate one or the other.
   // (Default: False)
   var.hotSwap = False

   // These offsets are the boundries used in the range calculation from -1 to 1
   // Laymans Terms: The larger the number the less sensative it will be.
   var.yOffset = 35 degrees // (Default: 35 degrees)
   var.xOffset = 60 degrees // (Default: 60 degrees)

   // Turns the throttle into an offset instead of an absolute
   // For future support of more Space sims. (Default: False)
   var.ConstThrottle = False

   // Multiplys Nunchuk.JoyY by the number to adjust the throttle.
   // Laymans Terms: Bigger the number the faster your throttle goes up.
   // (Default: 0.02)
   var.ThrottleSpeed = 0.02

   // Data Storage variable, no user useage.
   var.ThrottleOffset = 0

   // Enable to display the battery life on the LEDs
   // (Default: True)
   var.BatteryLife = True

   // Blink rate for battery check.
   // (Default: 500 ms)
   var.Blink = 500 ms

   // How long you have to hold a button to activate it's held function.
   // (Default: 400 ms)
   var.HeldTime = 400 ms

   var.init = True
endif

// Press C + 1 + 2 to activate Calibration mode
// Rumbles on activation.
if HeldDown(Wiimote1.One, var.HeldTime) and HeldDown(Wiimote1.Two, var.HeldTime)
   if not var.Calibrate
      wiimote.Rumble = 1
      wait 50 ms
      wiimote.Rumble = 0
      var.Calibrate = True
   endif
endif

// Calibration - All variables default to 0
// To calibrate the control sit a natrual position and tap
// the following combos.
// Wiimote : A + B
// Nunchuk : C + Z
if var.Calibrate
   // Cancel Calibration
   if Pressed(Wiimote1.One) and Pressed(Wiimote1.Two)
      wiimote.Rumble = 1
      wait 50 ms
      wiimote.Rumble = 0
      var.Calibrate = False
   endif

   // Calibrate the Wiimote
   if Wiimote.A and Wiimote.B
      wiimote.Rumble = 1
      wait 50 ms
      wiimote.Rumble = 0
      var.w.x.offset = round(Wiimote1.SmoothRoll)
      var.w.x.r1 = var.w.x.offset - var.xOffset
      var.w.x.r2 = var.w.x.offset + var.xOffset
      var.w.y.offset = round(Wiimote1.SmoothPitch)
      var.w.y.r1 = var.w.y.offset - var.yOffset
      var.w.y.r2 = var.w.y.offset + var.yOffset
      if var.hotSwap
         var.useNunchuk = False
      endif
      var.Calibrate = False
   endif

   // Calibrate the Nunchuk
   if Wiimote.Nunchuk.ZButton and Wiimote.Nunchuk.CButton
      wiimote.Rumble = 1
      wait 50 ms
      wiimote.Rumble = 0
      var.n.x.offset = round(Wiimote1.Nunchuk.SmoothRoll)
      var.n.x.r1 = var.n.x.offset - var.xOffset
      var.n.x.r2 = var.n.x.offset + var.xOffset
      var.n.y.offset = round(Wiimote1.Nunchuk.SmoothPitch)
      var.n.y.r1 = var.n.y.offset - var.yOffset
      var.n.y.r2 = var.n.y.offset + var.yOffset
      if var.hotSwap
         var.useNunchuk = True
      endif
      var.Calibrate = False
   endif
else

// Orientation
if not var.useNunchuk
   var.roll = EnsureMapRange(Wiimote1.SmoothRoll, var.w.x.r1, var.w.x.r2, -1,1)
   var.pitch = EnsureMapRange(Wiimote1.SmoothPitch, var.w.y.r1, var.w.y.r2, -1,1)
else
   var.roll = EnsureMapRange(Wiimote1.Nunchuk.SmoothRoll, var.n.x.r1, var.n.x.r2, -1,1)
   var.pitch = EnsureMapRange(Wiimote1.Nunchuk.SmoothPitch, var.n.y.r1, var.n.y.r2, -1,1)
endif

if wiimote.A
   debug = var.w.y.offset +" "+ var.w.x.offset +" || "+ var.roll +" "+ var.pitch
elseif wiimote.nunchuk.CButton
   debug = var.n.y.offset +" "+ var.n.x.offset +" || "+ var.roll +" "+ var.pitch
endif

// Throttle
if var.mapNunchukToKeys
   key.s = Wiimote.Nunchuk.JoyY > 0.2
   key.w = Wiimote.Nunchuk.JoyY < -0.2
   key.d = Wiimote.Nunchuk.JoyX > 0.2
   key.a = Wiimote.Nunchuk.JoyX < -0.2
else
if not Wiimote.Nunchuk.CButton
   if var.ConstThrottle
      var.throttle = var.ThrottleOffset
   else
      var.throttle = 0
   endif
   var.analog3 = Wiimote1.Nunchuk.JoyY
else
   if var.ConstThrottle
      var.ThrottleOffset -= RoundTo(var.ThrottleSpeed * Wiimote.Nunchuk.JoyY, -2)
      var.ThrottleOffset = EnsureRange(var.ThrottleOffset, -1, 1)
      var.throttle = var.ThrottleOffset
   else
      var.throttle = Wiimote1.Nunchuk.JoyY
   endif
   var.analog3 = 0
endif
endif
endif

// -------------------------------------------------------------------------- \\

if var.BatteryLife
// Coded by Unknown
var.Batt = wiimote.Battery / 48
if true then
   wait 5 seconds
   //it sends an instruction that tells the Wiimote to actually
   //send the report.
   Wiimote.Report15 = 0x80 | Int(wiimote.Rumble)
end if

// Display the battery level of your wiimote using the four LEDs on the bottom.
// Battery level is displayed in four levels increasing to the right, like a cell
// phone battery gauge. As the battery gets close to the next level down, the LED
// for the current level will blink

if 0<=var.Batt<=0.25 then
   Wiimote.Leds = 1
   wait var.Blink
   Wiimote.Leds = 0
   wait var.Blink
elseif 0.25 < var.Batt<=1 then
   Wiimote.Leds = 1
elseif 1 < var.Batt<=1.25 then
   Wiimote.Leds = 3
   wait var.Blink
   Wiimote.Leds = 1
   wait var.Blink
elseif 1.25 < var.Batt<=2 then
   Wiimote.Leds = 3
elseif 2 < var.Batt<=2.25 then
   Wiimote.Leds = 7
   wait var.Blink
   Wiimote.Leds = 3
   wait var.Blink
elseif 2.25 < var.Batt<=3 then
   Wiimote.Leds = 7
elseif 3 < var.Batt<=3.25 then
   Wiimote.Leds = 15
   wait var.Blink
   Wiimote.Leds = 7
   wait var.Blink
elseif 3.25 < var.Batt<=4 then
  Wiimote.Leds = 15
else
  Wiimote.Leds = 0
endif
endif
Personal tools
Online Casino - best online casino reviews.
Facebook Developers - facebook applications, facebook developers, facebook development, social network application development and viral widget social media strategy