Part 1 -- Tips for Tedious Coding: An Introduction into Javascript
Have you ever had to produce tedious lines of code, or wanted to produce a cool effect, but didn't feel like writing everything out? Now, I shall unveil and easy way for the production of tedious lines of code. Yay!
Have you ever heard of javascript? Its a type of scripting language -- but heres the secret -- that is uncompiled. All you need is a text editor! So, lets open up our favorite text editor -- which I shall assume is Notepad++.
Introduction:
Now, let's type in the basic part of the script that we need:
Code
var fso = new ActiveXObject("Scripting.FileSystemObject");
var outputFileName = fso.GetParentFolderName(WScript.ScriptFullName) + "\\" + fso.GetBaseName(WScript.ScriptFullName) + ".txt";
var outputStream = fso.CreateTextFile(outputFileName, true);"
var outputFileName = fso.GetParentFolderName(WScript.ScriptFullName) + "\\" + fso.GetBaseName(WScript.ScriptFullName) + ".txt";
var outputStream = fso.CreateTextFile(outputFileName, true);"
-Line 1 states we are creating a new object.
-Line 2 states what its name shall be.
-I don't really know what Line 3 is (I'm guessing it is merely outputs untill told to stop.)
Now, lets make something we can all enjoy: SPINNING PROBES!!!!!
Code
var currentSpinSpeed = 1;
while(currentSpinSpeed < 13) {
currentSpinSpeed *= 1.02;
outputStream.WriteLine(" turncwise " + Math.floor(currentSpinSpeed));
outputStream.WriteLine(" wait 1");
}
while(currentSpinSpeed < 13) {
currentSpinSpeed *= 1.02;
outputStream.WriteLine(" turncwise " + Math.floor(currentSpinSpeed));
outputStream.WriteLine(" wait 1");
}
So, we set our variable "currentSpinSpeed" equal to one. While this variable is less than 13, we multiply it by 1.02. In the text file it will output, it will write the line "turncwise + Math.floor(1.02)" -> "turncwise 1" Can you see with Math.floor does? It takes any non-interger and round it DOWN.
Now, our code is almost done, all we need is one final line:
Code
outputStream.Close();
We tell our program to finish and close the file.
Our final code looks like:
Quote
var fso = new ActiveXObject("Scripting.FileSystemObject");
var outputFileName = fso.GetParentFolderName(WScript.ScriptFullName) + "\\" + fso.GetBaseName(WScript.ScriptFullName) + ".genicc";
var outputStream = fso.CreateTextFile(outputFileName, true);
var currentSpinSpeed = 1;
while(currentSpinSpeed < 13) {
currentSpinSpeed *= 1.02;
outputStream.WriteLine(" turncwise " + Math.floor(currentSpinSpeed));
outputStream.WriteLine(" wait 1");
}
outputStream.Close();
var outputFileName = fso.GetParentFolderName(WScript.ScriptFullName) + "\\" + fso.GetBaseName(WScript.ScriptFullName) + ".genicc";
var outputStream = fso.CreateTextFile(outputFileName, true);
var currentSpinSpeed = 1;
while(currentSpinSpeed < 13) {
currentSpinSpeed *= 1.02;
outputStream.WriteLine(" turncwise " + Math.floor(currentSpinSpeed));
outputStream.WriteLine(" wait 1");
}
outputStream.Close();
Save the file as: probeBuildUp.js. Now click on the file and it will create probeBuildUp.txt.
*This javascript originally comes from BSTRhino. I have used it because if its simplicity -- yet stunning visual effect I luve spinning probes
-----------------------------------
Bouncing Grenades:
I have always loved this effect, and all it requires is iscript magic.
Code
var currentFrameNumber = 0;
Set the current frame number.
Code
function generatePlayFrameCommand() {
outputStream.WriteLine(" playfram " + currentFrameNumber);
++currentFrameNumber;
if(currentFrameNumber >= 4) {
currentFrameNumber = 0;
}
}
outputStream.WriteLine(" playfram " + currentFrameNumber);
++currentFrameNumber;
if(currentFrameNumber >= 4) {
currentFrameNumber = 0;
}
}
We define a function that we will use later in the code. You probably can understand this except for a few things (if you are unfimiliar with coding santax).
'++' == add one to the variable.
'>=' == less than or equal to.
Code
var arcStepArray = new Array();
for(var currentArcInitialStep = 1; currentArcInitialStep * 2 - 1 <= 64; currentArcInitialStep *= 2) {
arcStepArray.push(currentArcInitialStep);
outputStream.WriteLine("Arc" + arcStepArray.length + ":");
var currentHeightValue = 0;
for(var currentStepValue = currentArcInitialStep; currentStepValue >= 1; currentStepValue /= 2) {
currentHeightValue -= currentStepValue;
outputStream.WriteLine(" shvertpos " + currentHeightValue);
generatePlayFrameCommand();
outputStream.WriteLine(" wait 1");
}
var currentDecrementValue = 1;
while(true) {
currentHeightValue += currentDecrementValue;
if(currentHeightValue > 0) {
break;
}
outputStream.WriteLine(" shvertpos " + currentHeightValue);
generatePlayFrameCommand();
outputStream.WriteLine(" wait 1");
currentDecrementValue *= 2
}
outputStream.WriteLine("goto Arc" + (arcStepArray.length - 1));
outputStream.WriteLine();
}
outputStream.Close();
for(var currentArcInitialStep = 1; currentArcInitialStep * 2 - 1 <= 64; currentArcInitialStep *= 2) {
arcStepArray.push(currentArcInitialStep);
outputStream.WriteLine("Arc" + arcStepArray.length + ":");
var currentHeightValue = 0;
for(var currentStepValue = currentArcInitialStep; currentStepValue >= 1; currentStepValue /= 2) {
currentHeightValue -= currentStepValue;
outputStream.WriteLine(" shvertpos " + currentHeightValue);
generatePlayFrameCommand();
outputStream.WriteLine(" wait 1");
}
var currentDecrementValue = 1;
while(true) {
currentHeightValue += currentDecrementValue;
if(currentHeightValue > 0) {
break;
}
outputStream.WriteLine(" shvertpos " + currentHeightValue);
generatePlayFrameCommand();
outputStream.WriteLine(" wait 1");
currentDecrementValue *= 2
}
outputStream.WriteLine("goto Arc" + (arcStepArray.length - 1));
outputStream.WriteLine();
}
outputStream.Close();
Now, for good practice, analyze that code and see if you can figure out what it means. Here are some tips:
'var1 += var2' == Variable #1 is equal to Variable #2 plus one.
'while(true)' == run this code while the output from is is a non-zero value.
Here is the overall code needed for bouncing grenades:
Quote
var fso = new ActiveXObject("Scripting.FileSystemObject");
var outputFileName = fso.GetParentFolderName(WScript.ScriptFullName) + "\\" + fso.GetBaseName(WScript.ScriptFullName) + ".genicc";
var outputStream = fso.CreateTextFile(outputFileName, true);
var currentFrameNumber = 0;
function generatePlayFrameCommand() {
outputStream.WriteLine(" playfram " + currentFrameNumber);
++currentFrameNumber;
if(currentFrameNumber >= 4) {
currentFrameNumber = 0;
}
}
var arcStepArray = new Array();
for(var currentArcInitialStep = 1; currentArcInitialStep * 2 - 1 <= 64; currentArcInitialStep *= 2) {
arcStepArray.push(currentArcInitialStep);
outputStream.WriteLine("Arc" + arcStepArray.length + ":");
var currentHeightValue = 0;
for(var currentStepValue = currentArcInitialStep; currentStepValue >= 1; currentStepValue /= 2) {
currentHeightValue -= currentStepValue;
outputStream.WriteLine(" shvertpos " + currentHeightValue);
generatePlayFrameCommand();
outputStream.WriteLine(" wait 1");
}
var currentDecrementValue = 1;
while(true) {
currentHeightValue += currentDecrementValue;
if(currentHeightValue > 0) {
break;
}
outputStream.WriteLine(" shvertpos " + currentHeightValue);
generatePlayFrameCommand();
outputStream.WriteLine(" wait 1");
currentDecrementValue *= 2
}
outputStream.WriteLine("goto Arc" + (arcStepArray.length - 1));
outputStream.WriteLine();
}
outputStream.Close();
var outputFileName = fso.GetParentFolderName(WScript.ScriptFullName) + "\\" + fso.GetBaseName(WScript.ScriptFullName) + ".genicc";
var outputStream = fso.CreateTextFile(outputFileName, true);
var currentFrameNumber = 0;
function generatePlayFrameCommand() {
outputStream.WriteLine(" playfram " + currentFrameNumber);
++currentFrameNumber;
if(currentFrameNumber >= 4) {
currentFrameNumber = 0;
}
}
var arcStepArray = new Array();
for(var currentArcInitialStep = 1; currentArcInitialStep * 2 - 1 <= 64; currentArcInitialStep *= 2) {
arcStepArray.push(currentArcInitialStep);
outputStream.WriteLine("Arc" + arcStepArray.length + ":");
var currentHeightValue = 0;
for(var currentStepValue = currentArcInitialStep; currentStepValue >= 1; currentStepValue /= 2) {
currentHeightValue -= currentStepValue;
outputStream.WriteLine(" shvertpos " + currentHeightValue);
generatePlayFrameCommand();
outputStream.WriteLine(" wait 1");
}
var currentDecrementValue = 1;
while(true) {
currentHeightValue += currentDecrementValue;
if(currentHeightValue > 0) {
break;
}
outputStream.WriteLine(" shvertpos " + currentHeightValue);
generatePlayFrameCommand();
outputStream.WriteLine(" wait 1");
currentDecrementValue *= 2
}
outputStream.WriteLine("goto Arc" + (arcStepArray.length - 1));
outputStream.WriteLine();
}
outputStream.Close();
-----------------------------------
Application of Javascript (and in fuller understanding -- coding in general) to Effects Using Mathematics:
What I am going to create in this section is a circle of explosions that you can use as an attack or death animation for a unit. I usually use it for larger units' deaths and such.
I know Starcraft incorporates a large array of players -- from youngling to expert elder -- so, skip this section if you understand polar equations.
The equation for a circle using x and y's is Math.sqrt(x*x + y*y) = radius*radius. Writing that equation made me want to puke. Couldn't there be some easier way?
Of course there is -- its called a polar equation. the equation for a circle is simply: r = 1. For a spiral, it is r = Theta.
A Crash Course on Polor Equations:
Polar equations relate angles with radii. At the given angle, the radius will be a certian length. At the end of the radius, it will plot the point.
But how are those points found be people like you or me? By these set of equations:
x = radius * cos(theta)
y = radius * sin(theta)
(This is basic Algebra and Trig. And some Geometry I guess... O well.)
So, if we are going to use a polar equation in our javascript, we're going to need these two equations as functions:
Code
//functions
function calcx(radians) {
xasixnum = Math.round(Math.cos(radians) * 10);
return xasixnum;
}
function calcy(radians) {
yasixsum = Math.round(Math.sin(radians) * 10);
return yasixsum;
}
function calcx(radians) {
xasixnum = Math.round(Math.cos(radians) * 10);
return xasixnum;
}
function calcy(radians) {
yasixsum = Math.round(Math.sin(radians) * 10);
return yasixsum;
}
Now, javascript uses radius, which differs from degrees.
Degrees * pi / 180 = Radians
We want our explosions to go full cicle so:
Code
// Program entry point
for(var currentRadians = 0; currentRadians < 2 * Math.PI; currentRadians += Math.PI/16) {
outputStream.WriteLine(" imgol 334" + " " + calcx(currentRadians) + " " + calcy(currentRadians));
outputStream.WriteLine(" playsndbtwn 7 12");
outputStream.WriteLine(" wait 1");
}
outputStream.Close();
for(var currentRadians = 0; currentRadians < 2 * Math.PI; currentRadians += Math.PI/16) {
outputStream.WriteLine(" imgol 334" + " " + calcx(currentRadians) + " " + calcy(currentRadians));
outputStream.WriteLine(" playsndbtwn 7 12");
outputStream.WriteLine(" wait 1");
}
outputStream.Close();
Here is our full code:
Quote
var fso = new ActiveXObject("Scripting.FileSystemObject");
var outputFileName = fso.GetParentFolderName(WScript.ScriptFullName) + "\\" + fso.GetBaseName(WScript.ScriptFullName) + ".icc";
var outputStream = fso.CreateTextFile(outputFileName, true);
//functions
function calcx(radians) {
xasixnum = Math.round(Math.cos(radians) * 10);
return xasixnum;
}
function calcy(radians) {
yasixsum = Math.round(Math.sin(radians) * 10);
return yasixsum;
}
// Program entry point
for(var currentRadians = 0; currentRadians < 2 * Math.PI; currentRadians += Math.PI/16) {
outputStream.WriteLine(" imgol 334" + " " + calcx(currentRadians) + " " + calcy(currentRadians));
outputStream.WriteLine(" playsndbtwn 7 12");
outputStream.WriteLine(" wait 1");
}
outputStream.Close();
var outputFileName = fso.GetParentFolderName(WScript.ScriptFullName) + "\\" + fso.GetBaseName(WScript.ScriptFullName) + ".icc";
var outputStream = fso.CreateTextFile(outputFileName, true);
//functions
function calcx(radians) {
xasixnum = Math.round(Math.cos(radians) * 10);
return xasixnum;
}
function calcy(radians) {
yasixsum = Math.round(Math.sin(radians) * 10);
return yasixsum;
}
// Program entry point
for(var currentRadians = 0; currentRadians < 2 * Math.PI; currentRadians += Math.PI/16) {
outputStream.WriteLine(" imgol 334" + " " + calcx(currentRadians) + " " + calcy(currentRadians));
outputStream.WriteLine(" playsndbtwn 7 12");
outputStream.WriteLine(" wait 1");
}
outputStream.Close();
Next up: Completely Random Atttack Sequences
Next up: Application to Triggers
Next up: Application to AI Design
Nect up: Advanced Special Attacks and Spell with Application of
Post has been edited 9 time(s), last time on Mar 25 2008, 4:56 am by A_of-s_t.