FANDOM


Edit

Sequence Edit

This page is for sample scripts in TorqueScript.

Converts Fahrenheit to Celsius Edit

// converts Farenheit to Celsius 
%fahdeg = 32;
%celdeg = (5.0/9.0) * (%fahdeg - 32);
echo(%fahdeg @"degrees fahrenheit is "@ %celdeg @"degrees celsius");

Fahrenheit

Calculates the circumference and area of a circle given the radius Edit

// Calculates the circumference and area of a circle given the radius 

%pi = 3.1415926535897932384626433832795;
%radius = 5;
%circumference = 2.0 * %pi * %radius;
%area = %pi * %radius * %radius;
echo("The circumference is "@ %circumference);
echo("\nThe area is "@ %area);

Circle

Calculates interest rate payments Edit

// Calculates the regular payment on a loan
%prin = 200000; // principal borrowed
%interest = 0.065; // interest in decimals
%numpay = 12; // number of payments in a year
%years = 30; // number of years to repay the loan

// calculations

%intpay = %interest/%numpay;
%numer = %prin*%intpay;
%ttlpay = %numpay*%years;
%denom = 1 - mPow((%intpay+1) @ (-%ttlpay));
%regpay = %numer / %denom;

echo("The regular payment is: $"@ %regpay);

Interest

Sends a list of numbers to the players in the game Edit

  • Use the command /Dude in the game window to execute the function.
// You will need to use the Dude command in the game window to make this work.

function ServerCmdDude(%c)
{
    %val=6;
 
    while(%val)
	messageAll('', %val--);
}

MessageAll

Calculate the distance from a point to a line Edit

// Computes the distance from a point to a line

// x and y coefficients of a point

%x = 1;
%y = 1;

// The coefficients of an equation ax + by = c

%a = 3;
%b = 2;
%c = 4;

// Calculations

%numer = mAbs((%a * %x) + (%b * %y) - %c);
%sumsqr = (%a * %a) + (%b * %b);
%denom = mSqrt(%sumsqr);
%dist = %numer/%denom;
echo("Distance: "@%dist);

Line



Making a simple server command Edit

function serverCmdHelloWorld(%this)
{
   echo("Hello, World!");
}

Selection Edit

Converting Fahrenheit to Celsius Edit

%choice = 2;

if ( %choice == 1 )
{

// converts Fahrenheit to Celsius 
%fahdeg = 32;
%celdeg = (5.0/9.0) * (%fahdeg - 32);

echo(%fahdeg, "degrees Fahrenheit is ", %celdeg, " degrees Celsius");
}

if ( %choice == 2 )
{
// converts Celsius to Farenheit 

%celdeg = 0;
%fahdeg = (%celdeg)*(9.0/5.0)+32;

echo(%celdeg, "degrees Celsius is ", %fahdeg, " degrees Farenheit");
}

Selection1

Calculates whether a year is a leap year Edit

// uses nested loops to determine if a four-digit number is a leap year

// the year to be tested 

%year = 1998 ;

// determine if leap year and output

if (( %year >= 1000) && (%year <=9999))
{
	if (( %year % 4 ) == 0 )
	{
		if (( %year % 100 ) == 0)
		{
			if ((  %year % 400 ) == 0)
			{
			echo(%year, " is a leap year!"); 
			}
			else 
			{
			echo(%year, " is not a leap year!"); 
			}
		}
		else
		{
		echo(%year, " is a leap year!"); 
		}
	}
	else
	{
	echo(%year, " is not a leap year!"); 
	}
}
else
{
echo(%year, " is not a four-digit number!"); 
}

Selection2

Calculate an undergraduate's class Edit

// Given hours completed, find undergraduate's class

// declarations

%hours = 72 ;


if (%hours < 32)
{
echo("The student is a freshman\n"); 
}
else if (%hours < 64)
{
echo("The student is a sophomore\n"); 
}
else if (%hours < 96)
{
echo("The student is a junior\n"); 
}
else
{
echo("The student is a senior\n"); 
}

Selection3

Using the switch function Edit

%choice = 2;

switch ( %choice )
{
case 1:
echo( "You chose the 1st action." );
case 2:
echo( "You chose the 2nd action." );
case 3:
echo( "You chose the 3rd action." );
case 4:
echo( "You chose the 4th action." );
default:
echo( "You made an invalid selection." );
}

Selection4

Finds the first of three letters Edit

%l1 = A;
%l2 = B;
%l3 = C;


if (stricmp(%l1,%l2) < 0)
{
%first = %l1;
}
else
{
%first = %l2;
}

if (stricmp(%l3,%first) < 0)
{
%first = %l3;
}
echo ("The first letter is ", %first);

Selection5

Iteration Edit

Displays integers 1 through 5 and their square roots Edit

// displays integers 1 through 5 and their square roots

for (	%number = 1;
	%number <= 5;
	%number = %number +1)
{
echo (%number," ",mSqrt(%number));
}

Iteration1

Calculates the factorial of a number Edit

	$num = 10; // the number of which to find a factorial
	$fact = 1;

	for ( $i = $num; $i >= 2; $i--)
	{
	$fact=$fact*$i;
	}

function ServerCmdFactorial()
{
	messageAll('$1',$num);
	messageAll('','! is equal to ');
	messageAll('$1',$fact);
}

Iteration2

Displays the value of x^2 + 4x + 3 over any interval Edit

// displays value of x^2 + 4x + 3

$a = 1; // lower limit of interval
$b = 10; // upper limit of interval 
$incr = 1; // loop increment

for ( $x = $a; $x <= $b; $x+=$incr)
{
$y=($x*$x)+(4*$x)+3;
echo ($x SPC $y);
}

Iteration3

Height of object in free fall Edit

// Create table of height of object in free fall

%GRAVITY = 9.8;

// height of tower

%tower = 100;

// interval between calculations 

%delta = 1;

// table heading 

echo("\nTime in seconds      Height of object in meters\n");
echo(  "---------------      --------------------------\n");

// Calculate table as long as object is above ground

%t=0;
%height=%tower;
while (%height > 0) 
{
	echo(mAbs(%t) @ "                     " @ %height);
	%t=(%t-%delta);
	%height=%tower-(0.5*%GRAVITY*%t*%t);
	
}

Height

While statement used to implement a counting loop Edit

// while statement used to implement a counting loop
// Loop parameters

// Starting value

%init = 1;

// Loop stop value

%final = 10;

// Loop Step value

%step = 1;

while (%init <= %final)
{
	echo("Body of loop executed.\n");
	%init = %init + 1;
}

Whileloop

Find maximum and minimum values in an array Edit

for(%i=0;%i<9;%i++){
$var[%i]=%i;
}

$londx = 0;
$hindx = 0;

for (%j = 1; %j <=9; %j++) {
if ($var[%j] < $var[$londx]) $londx = %j;
if ($var[%j] > $var[$hindx]) $hindx = %j;
}

echo("The smallest element is ", $var[$londx]);
echo("The greatest element is ", $var[$hindx]);

Arrays3


See Also Edit

Arrays

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.