

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'                    Quake FPS/Player Physics Calculator - By injeXion                         '
'                            Copyright © 2004 by injeXion                                      '
'                          Comments to injeXion@hotmail.com                                    '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				
	
	Function roundit(thenumber)

		Dim fractional

		fractional = thenumber - Int(thenumber)

		If fractional = 0 then
			roundit = thenumber
		elseif fractional >= 0.5 then
			roundit = (Int(thenumber) + 1)
		elseif fractional < 0.5 then
			roundit = (Int(thenumber))
		end if

	End Function
		
	Function roundup(thenumber)

		Dim fractional

		fractional = thenumber - Int(thenumber)

		If fractional = 0 then
			roundup = Int(thenumber)
		else
			roundup = (Int(thenumber) + 1)
		end if

	End Function
	
	Function roundtoplaces(thenumber, p)
								
		'If right(left(thenumber,p+1),1) >= 5 and len(trim(left(thenumber,p+1))) = p+1 then
		If len(trim(left(thenumber,p+1))) = p+1 then
			roundtoplaces = left(thenumber,p-1) & right(left(thenumber,p),1) + 1

		Else
			roundtoplaces = left(thenumber,p)
		End If
	
	
	End Function
	
	Function gravityeffect(ingravity, fps)

		gravityeffect = roundit(ingravity / fps) * fps

	End Function

	Function jumpheightcalc(jump_velocity, ingravity, fps, fpsx, rbool)

		Dim newheight
		Dim oldheight
		Dim newheight2
		Dim oldheight2
		Dim gravityperframe
		Dim frametime
		Dim velocity
		Dim oldvelocity
				
		fps = trim(fps)
		ingravity = int(trim(ingravity))
		velocity = jump_velocity

		frametime = fpsx/1000
		
		gravityperframe = -ingravity * frametime

		If gravityperframe >= 0 then
			gravityperframe = -1
		End if
	
		newheight = 0

		oldheight = newheight
		velocity = velocity + gravityperframe
		newheight = roundit(oldheight + (velocity)*0.5 * frametime)
		if rbool then
			velocity = roundit(velocity)
		end if
		Do	
			oldvelocity = velocity
			oldheight = newheight
			velocity = oldvelocity + gravityperframe
			newheight = roundit(oldheight + (oldvelocity + velocity)*0.5 * frametime)
			if rbool then
				velocity = roundit(velocity)
			end if
		Loop While (newheight > oldheight)
		
		jumpheightcalc = roundit(oldheight)

	End Function
	
	Function packetscalc(fps)

		If fps > 125 and fps <= 250 then
			packetscalc = roundup(fps / 2) & " or " & roundup(fps / 3) & " or " & roundup(fps / 4) & " or " & roundup(fps / 5) & " or " & roundup(fps / 6)
		ElseIf fps > 250 then
			packetscalc = roundup(fps / 3) & " or " & roundup(fps / 4) & " or " & roundup(fps / 5) & " or " & roundup(fps / 6) & " or " & roundup(fps / 7)
		Else
			packetscalc = roundup(fps / 1) & " or " & roundup(fps / 2) & " or " & roundup(fps / 3) & " or " & roundup(fps / 4) & " or " & roundup(fps / 5)
		End If
		
		If fps = "" then
			packetscalc = ""
		End if

	End Function
	
	Function maxpacketcalc(msec, mp)
		For x = 1 to roundup(1000/(msec*15))
				
			If (1000/msec)/x <= mp then
				maxpacketcalc = (1000/msec)/x
				'document.calculator.inputmp.value = roundtoplaces((1000/msec)/x,5)
				Exit Function
			End If
		Next
	End Function
	
	Function upstreamcalc(msec)
		
		Dim bytesperframe
		Dim maxpackets
		
		maxpackets = maxpacketcalc(msec, roundup(trim(document.calculator.inputmp.value)))
		bytesperframe = 62
		
		upstreamcalc = bytesperframe * (1000/msec) * ((maxpackets*msec)/1000) * (8/1024)
		
	End Function
			
	Sub button1_OnClick()
 			
 		If document.calculator.inputfps.value = "" then
			msgbox "You must enter a value for com_maxfps",vbExclamation,"Input Missing"
			Exit Sub
		End if
		
		If document.calculator.inputgravity.value = "" then
			msgbox "You must enter a value for g_gravity",vbExclamation,"Input Missing"
			Exit Sub
		End if
		
		If document.calculator.inputgravity.value <= 0 then
			msgbox "g_gravity must be a positive value",vbExclamation,"Input Error"
			Exit Sub
		End if
		
		If document.calculator.inputgravity.value > 72900 then '72900 = jump_velocity^2
			msgbox "Max g_gravity is 72900",vbExclamation,"Input Error"
			Exit Sub
		End if
		
		If document.calculator.inputfps.value > 1000 or document.calculator.inputfps.value < 15 then
			msgbox "Invalid MaxFPS, must be 15 <= MaxFPS <= 1000",vbExclamation,"Input Error"
			Exit Sub
		End if
		
		If document.calculator.inputmp.value <> "" then
			If document.calculator.inputmp.value > 125 or document.calculator.inputmp.value < 15 then
				msgbox "Invalid MaxPackets, must be 15 <= MaxPackets <= 125",vbExclamation,"Input Error"
				Exit Sub
			End if
		End If
			
		Dim ingravity
		Dim jump_velocity
		Dim jump_height
		Dim fps
		Dim fps_difference
		Dim actual_x
		Dim x
		Dim y
		Dim islarger
		Dim islarger2
		Dim recommended
		Dim optimal_fps
		Dim optimal_x
 		Dim best_height
 		
 		jump_velocity = 270			
		ingravity = int(document.calculator.inputgravity.value)
		
		fps_difference = 1000/3 - document.calculator.inputfps.value
		actual_x = int(1000/document.calculator.inputfps.value)
		'actual_x = 3
		
		'For x = 4 to 25
		'	If (1000/x - document.calculator.inputfps.value) < fps_difference and (1000/x - document.calculator.inputfps.value) >= 0 then
		'		fps_difference = 1000/x - document.calculator.inputfps.value
		'		actual_x = x
		'	End if
		'Next
		
		document.calculator.inputfps.value = left(1000/actual_x,3)
		fps = 1000/actual_x
		
		
		If ingravity < fps then
			msgbox "Setting the gravity lower than the framerate may cause problems",vbOKOnly,"Warning"
		End if
		
		
		
		If document.calculator.inputmp.value <> "" then
			document.calculator.upstream.value = left(upstreamcalc(actual_x),5)
		Else
			document.calculator.upstream.value = ""
		End If
		
		document.calculator.jumpheight.value = left(jumpheightcalc(jump_velocity, ingravity, fps, actual_x,true),7)
    		document.calculator.extraheight.value = left((document.calculator.jumpheight.value/jumpheightcalc(jump_velocity, ingravity, fps, actual_x,false) - 1),4)
		
		best_height = 0
	
		For x = 1 to 66
			If jumpheightcalc(jump_velocity, ingravity, 1000/x, x,true) > best_height then
					best_height = jumpheightcalc(jump_velocity, ingravity, 1000/x, x,true)
					optimal_fps = 1000/x
					optimal_x = x
			End If
		Next
		
		document.calculator.optimalfps.value = int(optimal_fps)
		document.calculator.optimalpackets.value = packetscalc(optimal_fps)
		
		ReDim jump_height(66 - (actual_x - 1) + 1, 2)
		y = 0
		For x = (actual_x - 1) to 66
			If x >= 1 then
				jump_height(y,0) = jumpheightcalc(jump_velocity, ingravity, 1000/x, x,true)
				jump_height(y,1) = 1000/x
				y = y + 1
			Else
				jump_height(y,0) = 0
				jump_height(y,1) = 0
				y = y + 1
			End if
		Next
		
		
		
		ReDim recommended_fpsheight(4,2)
		recommended = 0
				
		For x = 0 to (66 - (actual_x - 1) - 1)
			islarger = 0
			For y = 0 to (66 - (actual_x - 1) - 1)
				If jump_height(x,0) > jump_height(y,0) or (jump_height(x,0) = jump_height(y,0) and jump_height(x,1) > jump_height(y,1)) then
					islarger = islarger + 1
				End If
			Next
			If islarger > (66-4 - actual_x) then
				recommended_fpsheight(recommended,0) = jump_height(x,0)
				recommended_fpsheight(recommended,1) = jump_height(x,1)
				recommended = recommended + 1
			End If
		Next
		
		
		
		ReDim recommended_fps(4)
		
		For x = 0 to 3
			islarger2 = 0
			For y = 0 to 3
				If recommended_fpsheight(x,0) > recommended_fpsheight(y,0) or (recommended_fpsheight(x,0) = recommended_fpsheight(y,0) and recommended_fpsheight(x,1) > recommended_fpsheight(y,1)) then
					islarger2 = islarger2 + 1
				End If
			Next
			If islarger2 = 3 then
				recommended_fps(0) = recommended_fpsheight(x,1)
			ElseIf islarger2 = 2 then
				recommended_fps(1) = recommended_fpsheight(x,1)
			ElseIf islarger2 = 1 then
				recommended_fps(2) = recommended_fpsheight(x,1)
			ElseIf islarger2 = 0 then
				recommended_fps(3) = recommended_fpsheight(x,1)
			End If
		Next
		
		
		If int(recommended_fps(0)) <> 0 then
			document.calculator.recommendedfps.value = int(recommended_fps(0))
		Else
			document.calculator.recommendedfps.value = ""
		End If
		
		If int(recommended_fps(1)) <> 0 then
			document.calculator.recommendedfps2.value = int(recommended_fps(1))
		Else
			document.calculator.recommendedfps2.value = ""
		End If
		
		If int(recommended_fps(2)) <> 0 then
			document.calculator.recommendedfps3.value = int(recommended_fps(2))
		Else
			document.calculator.recommendedfps3.value = ""
		End If
		
		If int(recommended_fps(3)) <> 0 then
			document.calculator.recommendedfps4.value = int(recommended_fps(3))
		Else
			document.calculator.recommendedfps4.value = ""
		End If

		
		
		document.calculator.recommendedpackets.value = packetscalc(recommended_fps(0))
		document.calculator.recommendedpackets2.value = packetscalc(recommended_fps(1))
		document.calculator.recommendedpackets3.value = packetscalc(recommended_fps(2))
		document.calculator.recommendedpackets4.value = packetscalc(recommended_fps(3))


	End Sub	
	

