Blockland Wiki
Register
Advertisement

[]

Sequence[]

This page is for sample scripts in TorqueScript.

Converts Fahrenheit to Celsius[]

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

Temperature

Calculates the circumference and area of a circle given the radius[]

// 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[]

// 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[]

  • 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[]

// 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[]

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

Selection[]

Converting Fahrenheit to Celsius[]

%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");
}

Selection

Calculates whether a year is a leap year[]

// 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!"); 
}

Selection

Calculate an undergraduate's class[]

// 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"); 
}

Selection

Using the switch function[]

%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." );
}

Selection

Finds the first of three letters[]

%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);

Selection

Iteration[]

Displays integers 1 through 5 and their square roots[]

// displays integers 1 through 5 and their square roots

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

Square Roots

Calculates the factorial of a number[]

	$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);
}

Factorials

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

// 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);
}

Polynomials

Height of object in free fall[]

// 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 of object in free fall

While statement used to implement a counting loop[]

// 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;
}

While loop

Find maximum and minimum values in an array[]

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]);

Arrays


See Also[]

Arrays

Advertisement