Flying

From the Oblivion ConstructionSet Wiki
Revision as of 22:06, 23 December 2011 by imported>8asrun6aer
Jump to navigation Jump to search

Introduction

Flying isn't normally something players can do. Here's a script to allow that.

This isn't so much of a snippet of code as it is the complete code for allowing a player to fly in a more or less natural way. The script below uses velocity functions that prevent wall, ceiling, floor, and obstacle clipping. Setting up when and how to allow the player to fly ( via casted spell or constant ability) is up to you - the modder. But for this article, we'll be setting up a new constant Player Ability which will allow the player to initiate flying at any time by pressing a key.

Not only can you grant the ability to fly to users of your mod with such a script, but you can use this script yourself as a modder to test every inch of your newly modded world space in a live environment.

The main principle here is avoiding using the SetPos method of flying, and thus avoid clipping. Instead, this script works with a player's x,y,z velocities.

If you see any part of the example script that can be made more efficient or made easier to read, feel free to edit. Ideally, this should be made a community-authored script helping modders create efficient and realistic player flying effects.

Requirements

  • This script uses OBSE 20 functions. In order for the script to run, the player will need OBSE version 20 installed.


In-Game Controls

This script assigns the "/" key to begin and end flying mode. You can change this at or around line 62 in this script where it assigns this key value to the 'cstmControlActivateFlyMode' variable (for a list of valid keyboard key ID's to use instead, see GetControl).

While flying, use the regular forward/backward/strafe keys for movement. Should the player ever reassign directional control keys, flight controls will be reset only after the player reloads the game. This directional control reassignment on game-load happens around line 57.

Use the mouse to control flight direction.


The Script

Before continuing, you'll want to create a new Global value. In the TES CS, go to Gameplay -> Globals menu, right-click in the list of globals, select "New", and for the Form ID, enter cstmGlobalPlayerIsFlying as a short with value 0. This cstmGlobalPlayerIsFlying global variable holds the flying state of the player. It's global so that this state can be easily checked or altered in other scripts.

Also, note the only code block used by this script is the ScriptEffectUpdate block. The ScriptEffectFinish block is irrelevant to constant abilities, and the ScriptEffectStart block would make this script too dependent on initial new game environments.

To easily distinguish local variables for the sake of readability in this example script, each new local variable name begins with 'cstm'. Also, the script is heavily commented to hopefully make each step more clear.


scn cstmFlightScript

; *************
; note about globals used
; *************
; cstmGlobalPlayerIsFlying - indicates current flying state, accessible globally


;*******************************
; Positional data variables
;*******************************
float cstmXVelocity ; velocity towards x 0 point
float cstmYVelocity ; velocity towards z 0 point
float cstmZVelocity ; velocity towards y 0 point
float cstmZGravityModifyer ; counteracts effects of world gravity on Z
float cstmPitch ; up/down angle player is facing
float cstmZPitchModifyer ; modifies applied up/down speed based on the angle player is facing up or down
float cstmHeading ; 0-360 degrees from North player is facing
float cstmSpeed ; updated rate of player movement in any given direction
float cstmSpeedMax ; max forward speed


;*******************************
; Directional keys
;*******************************
short cstmControlForward
short cstmControlBackward
short cstmControlStrafeLeft
short cstmControlStrafeRight
short cstmControlActivateFlyMode ; key which activates or deactivates flying


;*******************************
; Values for damaging fatigue while flying
;*******************************
float cstmCurrentEncumbranceValue ; player's current encumbrance
float cstmMaxEncumbranceValue ; player's maximum encumbrance
float cstmFlyingFatigueDamage ; comulative amount of damage to fatigue in one second


;*******************************
; Misc
;*******************************
float cstmSecondsPassed ; seconds passed since last scripteffectupdate



Begin ScriptEffectStart
End


Begin ScriptEffectUpdate

	;*******************************
	; Reset Key bindings on game load
	;*******************************
	if ( GetGameRestarted || GetGameLoaded )
		set cstmControlForward to GetControl 0
		set cstmControlBackward to GetControl 1
		set cstmControlStrafeLeft to GetControl 2
		set cstmControlStrafeRight to GetControl 3
		set cstmControlActivateFlyMode to 53 ; forwardslash key
	endif
	
	
	;*******************************
	; Check current flying state, or for flying activation/deactivation
	;*******************************
	if ( cstmGlobalPlayerIsFlying == 0 )
		if ( iskeypressed2 cstmControlActivateFlyMode )
			; player activated flying
			set cstmGlobalPlayerIsFlying to 1
		else
			; not flying right now, skip rest of script update
			return
		endif
	elseif ( iskeypressed2 cstmControlActivateFlyMode )
		; player pressed flight activate button again to end flying
		set cstmGlobalPlayerIsFlying to 0
		; flying stopped, skip rest of script update
		return
	endif

	
	;*******************************
	; Check player state to see if flying should stop
	; Invalid states are if player is not in air, is swimming, etc
	;*******************************
	if ( Player.IsInAir != 1 || Player.IsSwimming )
		set cstmGlobalPlayerIsFlying to 0
		; flying stopped, skip rest of script update
		return
	endif


	;*******************************
	; Presetup
	;*******************************	
	set cstmSecondsPassed to ScriptEffectElapsedSeconds
	resetFallDamageTimer ; reset damage to be taken by falls or stopped flight based on current height

	
	;*******************************
	; Damage fatigue while flying
	;*******************************

Pausing here for a second. This script damages the player's fatigue while flying to avoid encouraging the player to fly over every dangerous situation in the game. If you're only testing a new map you've just made, you'll want to comment out this part as it'll only get in the way. Otherwise, think twice about letting the player fly around without a care in the world.

	if ( Player.getAV fatigue <= 0 )
		set cstmGlobalPlayerIsFlying to 0
		message "You are too weak to fly."
		; flying stopped, skip rest of script update
		return
	endif
	
	set cstmCurrentEncumbranceValue to Player.getAV encumbrance
	set cstmMaxEncumbranceValue to Player.getBaseAV encumbrance
	; check if player is carrying too much to fly, or if max encumbrance is less than 1 for some reason
	if ( ( cstmCurrentEncumbranceValue >= cstmMaxEncumbranceValue ) || ( cstmMaxEncumbranceValue < 1 ) )
		set cstmGlobalPlayerIsFlying to 0
		message "You are too encumbered to fly."
		; flying stopped, skip rest of script update
		return
	endif
	
	; Player takes about 15-45 fatigue damage per second while flying depending on how weighted down they are by encumbrance
	; Since ModAV2 rounds down to an integer, damage is fractionally accumulated until at least 1 full point is tallied during any fraction of a second
	set cstmFlyingFatigueDamage to cstmFlyingFatigueDamage + ( ( ( ( cstmCurrentEncumbranceValue / cstmMaxEncumbranceValue ) * 30 ) + 15 ) * cstmSecondsPassed )
	if ( cstmFlyingFatigueDamage >= 1 )
		Player.ModAV2 fatigue -1
		set cstmFlyingFatigueDamage to 0
	endif

Now for the most important part of flying - controlling speed, heading, and pitch while negating gravity.


	;*******************************
	; Movement
	;*******************************
	set cstmHeading to Player.GetAngle z
	set cstmSpeedMax to 600 ; adjust to control how fast a player flies, but keep high to have any noticeable effect
	set cstmPitch to Player.GetAngle x
	set cstmZPitchModifyer to ( cstmPitch / 90 ) * -1
	
	if ( isKeyPressed2 cstmControlForward )
		; going forward
		set cstmSpeed to cstmSpeedMax
		if ( cstmZPitchModifyer < 0 )
			set cstmZPitchModifyer to cstmZPitchModifyer * 2.0 ; move faster going down
		else
			set cstmZPitchModifyer to cstmZPitchModifyer * 0.7 ; move slower going up
		endif
		; modify heading if strafing
		if ( isKeyPressed2 cstmControlStrafeLeft )
			set cstmHeading to cstmHeading - 45
		elseif ( isKeyPressed2 cstmControlStrafeRight )
			set cstmHeading to cstmHeading + 45
		endif
	elseif ( isKeyPressed2 cstmControlBackward )
		; reverse speed
		set cstmSpeed to ( cstmSpeedMax -200 ) * -1
		if ( cstmZPitchModifyer < 0 )
			set cstmZPitchModifyer to cstmZPitchModifyer * 0.7 ; move slower going up
		else
			set cstmZPitchModifyer to cstmZPitchModifyer * 2.0 ; move faster going down
		endif
		; modify reverse heading if strafing
		if ( isKeyPressed2 cstmControlStrafeLeft )
			set cstmHeading to cstmHeading + 45
		elseif ( isKeyPressed2 cstmControlStrafeRight )
			set cstmHeading to cstmHeading - 45
		endif
	else
		; modify in-place heading if strafing, otherwise hover in place
		if ( isKeyPressed2 cstmControlStrafeLeft )
			set cstmSpeed to cstmSpeedMax - 200
			set cstmHeading to cstmHeading - 90
		elseif ( isKeyPressed2 cstmControlStrafeRight )
			set cstmSpeed to cstmSpeedMax - 200
			set cstmHeading to cstmHeading + 90
		else
			set cstmSpeed to 0 ; just hovering in place
		endif
	endif

	; normalize heading to 0-360 degree allowable range
	if ( cstmHeading < 0 )
		set cstmHeading to cstmHeading + 360
	elseif ( cstmHeading > 360 )
		set cstmHeading to cstmHeading - 360
	endif
	
	; use maths to adjust x,y direction of travel based on heading
	set cstmXVelocity to sin cstmHeading * cstmSpeed
	set cstmYVelocity to cos cstmHeading * cstmSpeed
	
	; Set up/down velocity adjusting for gravity's influence and player's pitch
	set cstmZGravityModifyer to ( ( GetLocalGravity z ) / ( 21 + ( 1 / 3 ) ) ) * cstmSecondsPassed ; see GetLocalGravity elderscrolls.com article for this formula
	set cstmZVelocity to ( cstmZPitchModifyer * cstmSpeed ) + cstmZGravityModifyer

	; player essentially "falls" to their new x,y,z coords
	Player.SetVelocity cstmXVelocity, cstmYVelocity, cstmZVelocity
	
End


Begin ScriptEffectFinish
End

Once the script is saved, create a new Ability Spell. Set the Type of the spell to Ability. Then, add a new effect to this new ability spell. For this new effect, set the Range to Self, the Duration to 0, the Script to cstmFlightScript, the Effect Name to something readable in-game like Flying, and click OK. You can then make this a PC Start spell, or add it as a default ability for any race.

Notes

  1. This script requires that the player be in the air in order to activate flying. In other words, the player must be jumping or falling before flying can be activated. This catch is necessary to not produce the same wall/ceiling clipping that the customary SetPos method does, and for initially adjusting player velocities.
  2. Sometimes, activating flying only causes the player to rise 20 or so feet and then fall. Click the activate key a few more times to really activate flying. The exact location of this bug in this script is not yet known, but likely somewhere in the initial flight activation check area (line ~69).
  3. A player can fly over castle walls in exterior world spaces. Because they didn't enter the castle grounds through the front door, they'll be bewildered by how every building within the castle walls is nothing but a fake graphics placeholder. Although as yet untested with this script, you may want to mention to your mod users any "Open Cities" mods that may be out there to correct this issue.