ESM Math Library

From the Oblivion ConstructionSet Wiki
Revision as of 21:09, 9 May 2006 by imported>DragoonWraith (re-added Useful Code category)
Jump to navigation Jump to search

Welcome

to the Stage Function Repository! The goal of the Repository is, to create a huge database of available functions all using the method described in the article Simulating new functions. If you've written such a function and you think it might be helpful to others, don't hesitate to contribute to this repository.

Step by Step

To get this up and running follow there simple setup steps:

  • Open any Plugin you wish or create a new one with the Construction Set
  • Create a new Quest called "f" (yes, just the letter "f", nothing more)
  • Activate the Checkbox "Allow repeated stages". (This is VERY important!!)
  • Create a new Quest Script and copy the whole content of the "Quest Script" section from this article into this script. Don't forget to attach it to your f-Quest!
  • Create a new stage in this quest for each function you want to use from this repository and copy all code from it's section in this article to the related Result Script textbox. Make sure to select the right stage number for the function.
  • That's it! You should be able to use them now!

Quest Script

ScriptName FunctionQuestScript

; Internals
short doOnce
float fQuestDelayTime

; Constants
float pi
float rad

; Function In- and Output
float fin1
float fin2
float fin3
float fout
float fout2
float fout3

;S5 FUNCTION sqrt
float sqr

;S10 FUNCTION Hypotenuse
;float sqr
float n

;S15 FUNCTION SinCosTan 1
float ang
float sin
float cos
float tan
float exp

;S16 FUNCTION SinCodTan 2
;float ang
;float sin
;float cos
float t1
float t2
float t5
float t6

;S17 FUNCTION SinCosTan 3
;float ang
;float sin
;float cos
;float t2

;S20 FUNCTION Arcsine 1
;float sin
;float ang
;float n

;S21 FUNCTION Arcsine 2
float t3
;float t5
float t7

;S22 FUNCTION Arccosine 1

;S23 FUNCTION Arccosine 2

;S24 FUNCTION Arctan
;float t3
;float t5
;float t7

;S30 FUNCTION getAngle
float tan
;float ang

; Set constants first
Begin Gamemode
  if doOnce == 0
    set pi to 3.1415927
    set rad to 180.0/pi

    set fQuestDelayTime to 30
    set doOnce to 1
  endif
End

This Quest Script already contains everything needed for the following functions.


Stage 5: Square Root

Based on Square Root Article


Code to include in FunctionQuestScript:

;S5 FUNCTION sqrt
float sqr

Code for the Stage Result Script:

;FUNCTION float sqrt(float input)
if (f.fin1 <= 0)
  set f.fout to 0
else
  set f.sqr to f.fin1/2
  set f.sqr to (f.sqr+(f.fin1/f.sqr))/2
  set f.sqr to (f.sqr+(f.fin1/f.sqr))/2
  set f.sqr to (f.sqr+(f.fin1/f.sqr))/2
  set f.sqr to (f.sqr+(f.fin1/f.sqr))/2
  set f.sqr to (f.sqr+(f.fin1/f.sqr))/2
  set f.fout to f.sqr
endif

Usage in another script:

;CALL float sqrt(float input)
set f.fin1 to myVarForInput
setStage f 5
set myResult to f.fout

Stage 10: Hypotenuse

Code to include in FunctionQuestScript:

;S10 FUNCTION Hypotenuse
float n

Code for the Stage Result Script:

;FUNCTION float Hypotenuse(float CathetusA, float CathetusB)

set f.n to ((f.fin1 * f.fin1) + (f.fin2 * f.fin2))
;CALL float sqrt(float input)
set f.fin1 to f.n
setStage f 5
;fout is already the result

Usage in another script:

;CALL float Hypotenuse(float CathetusA, float CathetusB)
set f.fin1 to myVarForCathetusA
set f.fin2 to myVarForCathetusB
setStage f 10
set myResult to f.fout

Stage 15: Sin Cos Tan

Based on Trigonometry Functions Article


Code to include in FunctionQuestScript:

;S15 FUNCTION SinCosTan
float ang
float sin
float cos
float exp

Code for the Stage Result Script:

;FUNCTION float,float,float SinCosTan(float Angle)
;Taylor Series Variant 1 - script by Galerion

set f.ang to f.fin1

if f.ang < -180
  set f.ang to (f.ang + 360)
elseif f.ang > 180
  set f.ang to (f.ang - 360)
endif

;approximate
set f.ang to (f.ang/f.rad)
set f.cos to 1
set f.exp to f.ang
set f.sin to f.exp
set f.exp to (f.exp * f.ang)
set f.cos to (f.cos - f.exp / 2)
set f.exp to (f.exp * f.ang)
set f.sin to (f.sin - f.exp / 6)
set f.exp to (f.exp * f.ang)
set f.cos to (f.cos + f.exp / 24)
set f.exp to (f.exp * f.ang)
set f.sin to (f.sin + f.exp / 120)
set f.exp to (f.exp * f.ang)
set f.cos to (f.cos - f.exp / 720)
set f.exp to (f.exp * f.ang)
set f.sin to (f.sin - f.exp / 5040)
set f.exp to (f.exp * f.ang)
set f.cos to (f.cos + f.exp / 40320)
set f.exp to (f.exp * f.ang)
set f.sin to (f.sin + f.exp / 362880)

set f.fout to f.sin
set f.fout2 to f.cos
set f.fout3 to (f.sin/f.cos) ;tan

Usage in another script:

;CALL float,float,float SinCosTan(float Angle)
set f.fin1 to myVarForAngle
setStage f 15
set myResultSin to f.fout
set myResultCos to f.fout2
set myResultTan to f.fout3

Stage 16: Sin Cos Tan 2

Based on Trigonometry Functions Article


Code to include in FunctionQuestScript:

;S16 FUNCTION SinCodTan 2
float ang
float sin
float cos
float t1
float t2
float t5
float t6

Code for the Stage Result Script:

;FUNCTION float,float,float SinCosTan(float Angle)
;Taylor Series Variant 2 - script by JOG

set f.ang to f.fin1

if f.ang < -180
  set f.ang to f.ang + 360
elseif f.ang > 180
  set f.ang to f.ang - 360
endif

set f.t1 to (f.ang/f.rad)
set f.t2 to (f.t1*f.t1)
set f.t5 to (f.t2*f.t2*f.t1)
set f.t6 to (f.t5*f.t1)
set f.sin to (f.t1 - (f.t1*f.t2/6) + (f.t5/120) - (f.t5*f.t2/5040) + (f.t6*f.t2*f.t1/362880))
set f.cos to (1 - (f.t2/2) + (f.t2*f.t2/24) - (f.t6/720) + (f.t6*f.t2/40320))

set f.fout to f.sin
set f.fout2 to f.cos
set f.fout3 to f.sin/f.cos ;tan

Usage in another script:

;CALL float,float,float SinCosTan(float Angle)
set f.fin1 to myVarForAngle
setStage f 16
set myResultSin to f.fout
set myResultCos to f.fout2
set myResultTan to f.fout3

Stage 17: Sin Cos Tan 3

Based on a posting by Galsiah in THIS THREAD


Code to include in FunctionQuestScript:

;S17 FUNCTION SinCosTan 3
float ang
float sin
float cos
float t2

Code for the Stage Result Script:

;FUNCTION float,float,float SinCosTan(float Angle)
;script by Galsiah

set f.ang to f.fin1
if f.ang < 0
  set f.ang to (f.ang + 360)
elseif f.ang >= 360
  set f.ang to (f.ang - 360)
endif
set f.ang to (f.ang/f.rad)

set f.n to 1
if (f.ang > 4.7123)
    Set f.ang to (f.ang - 6.2832)
elseif ( f.ang > 1.5708 )
    Set f.ang to (f.ang - 3.1416)
    Set f.n to -1
endif

set f.t2 to (f.ang * f.ang)
set f.sin to (f.ang*(1 - (f.t2*(0.16605 - (0.00761*f.t2)))))
set f.sin to (f.sin*f.n)

set f.cos to (1 - (f.t2*(0.4967 - (0.3705*f.t2))))
set f.cos to (f.cos*f.n)

set f.fout to f.sin
set f.fout2 to f.cos
set f.fout3 to f.sin/f.cos ;tan

Usage in another script:

;CALL float,float,float SinCosTan(float Angle)
set f.fin1 to myVarForAngle
setStage f 17
set myResultSin to f.fout
set myResultCos to f.fout2
set myResultTan to f.fout3


Stage 20: Arcsine

Based on Trigonometry Functions Article


Code to include in FunctionQuestScript:

;S20 FUNCTION Arcsine
float sin
float ang
float n

Code for the Stage Result Script:

;FUNCTION float Arcsine(float sin)
;Abramowitz/Stegun Approximation - script by JOG

set f.sin to f.fin1
set f.n to 1 - f.sin
set f.ang to f.n/2
set f.ang to (f.ang+(f.n/f.ang))/2
set f.ang to (f.ang+(f.n/f.ang))/2
set f.ang to (f.ang+(f.n/f.ang))/2
set f.n to (f.ang+(f.n/f.ang))/2

set f.fout to f.rad*(1.5707963 - f.n*(1.5707288 - 0.2121144*f.sin+0.0742610*f.sin*f.sin - 0.0187293*f.sin*f.sin*f.sin))

Usage in another script:

;CALL float Arcsine(float sin)
set f.fin1 to myVarForSin
setStage f 20
set myResult to f.fout

Stage 21: Arcsine 2

Based on Trigonometry Functions Article


Code to include in FunctionQuestScript:

;S21 FUNCTION Arcsine 2
float t3
float t5
float t7

Code for the Stage Result Script:

;FUNCTION float Arcsine(float sin)
;Approximation by Taylor Series - script by DragoonWraith

set f.t3 to (f.fin1 * f.fin1 * f.fin1)
set f.t5 to (f.t3 * f.fin1 * f.fin1)
set f.t7 to (f.t5 * f.fin1 * f.fin1)

set f.fout to (f.fin1 + (1/2)*(f.t3/3) + (3/8)*(f.t5/5) + (15/48)*(f.t7/7) )

Usage in another script:

;CALL float Arcsine(float sin)
set f.fin1 to myVarForSin
setStage f 21
set myResult to f.fout

Stage 22: Arccosine

Based on Trigonometry Functions Article


Code for the Stage Result Script:

;FUNCTION float Arccosine(float cos)
setStage f 20 ;Call Arcsine with same input
set f.fout to ((f.pi / 2) - f.fout)

Usage in another script:

;CALL float Arccosine(float cos)
set f.fin1 to myVarForCos
setStage f 22
set myResult to f.fout

Stage 23: Arccosine 2

Based on Trigonometry Functions Article


Code for the Stage Result Script:

;FUNCTION float Arccosine(float cos)
setStage f 21 ;Call Arcsine with same input
set f.fout to ((f.pi / 2) - f.fout)

Usage in another script:

;CALL float Arccosine(float cos)
set f.fin1 to myVarForCos
setStage f 23
set myResult to f.fout

Stage 24: Arctan

Based on Trigonometry Functions Article

Code to include in FunctionQuestScript:

;S24 FUNCTION Arctan
float t3
float t5
float t7

Code for the Stage Result Script:

;FUNCTION float Arctan(float tan)
;Approximation by Taylor Series - script by DragoonWraith

set f.t3 to (f.fin1 * f.fin1 * f.fin1)
set f.t5 to (f.t3 * f.fin1 * f.fin1)
set f.t7 to (f.t5 * f.fin1 * f.fin1)

set f.fout to (f.fin1 - (f.t3/3) + (f.t5/5) - (f.t7/7))

Usage in another script:

;CALL float Arctan(float tan)
set f.fin1 to myVarForTan
setStage f 24
set myResult to f.fout

Stage 30: getAngle

Code to include in FunctionQuestScript:

;S30 FUNCTION getAngle
float tan
float ang

Code for the Stage Result Script:

;FUNCTION float getAngle(float x, float y)

set f.tan to (f.fin1/f.fin2)
if f.tan >1 || f.tan < -1
  set f.tan to (f.fin2/ -f.fin1)
  if f.fin1 >= 0
    set f.ang to 90
  else
    set f.ang to -90
  endif
else
  if f.fin2 >= 0
    set f.ang to 0
  else
    set f.ang to 180
  endif
endif

;CALL float Arctan(float tan)
set f.fin1 to f.tan
setStage f 24

if f.fout > (f.pi/2)
  set f.fout to (f.pi/2)
elseif f.fout < (f.pi/ -2)
  set f.fout to (f.pi/ -2)
endif
set f.fout to ((f.fout*f.rad) + f.ang)

Usage in another script:

;CALL float getAngle(float x, float y)
set f.fin1 to myVarForX
set f.fin2 to myVarForY
setStage f 30
set myResultAngle to f.fout