I am using this game and these examples to teach myself how to program. My bot is my first program ever, and java is the first language I am learning. Because of that I may be overanalyzing things. Since I am just learning though, I am trying to understand the effects of nearly everything.
When I encounted the " static " modifier reading the java tutorials, I thought of plenty of neat ways to use it. My first thought was making a map. Then i thought about arrays of where the units are. Then I realized if it worked, it would essentially render the sensor limits pointless. I was sure that the limit would be enforced somehow, so i doublechecked the documentation. Under the radio section I found:
Then I remembered that one of the example bots uses the static modifier. I double checked, and sure enough it has :Each bot exists in its own VM, and cannot access memory of another bot directly.
Code: Select all
public static final int CITY = 0;
public static final int GRUNT = 1;
public static final int HOVERCRAFT = 2;
public static final int ARTIL = 3;
To prove conclusively that the bots dont use class variables, I wrote some code and did some testing. Sure enough, despite me declaring the destination as a class variable, each bot went to its own seperate destination. When I checked the "run natively" option, it demonstrated that I did code it correctly, all of the bots converged on the same location.
Now I am really confused. I understand that static members could be useful to inner or packaged classes, but if there is a limit of one instance of the class per environment (I hope thats the right term) like there is in this game, I still don't understand what benefit declaring a member static could confer.
I also searched on the web, but that gave me more questions than answers. I got a few hits that static final primatives are constants and therefore cut out during compile time. I got a few saying that you only need final to declare constants, not static. Another said that constants weren't supported by the language. Another suggested that some compilers do things others don't. I probed technical documents to the best of my abilities, and I am sure I learned some things from it, but not the answer to my question.
P.S. This is the code I used to answer my first question on if static variables worked if class instances were loaded in seperate VMs. The difference between running natively and in the modified VM is quite noticable . If anyone as newb as I am now wants a demonstration, feel free.
Code: Select all
import java.util.Random;
public class Testing {
public int orderType = 1; // always a move order
public double destX, destY; // the move order will use these fields to communicate a destination
Random r = new Random(); // help us randomize our actions
public static double teamX,teamY;
{
Testing.teamX = 25 ;
Testing.teamY = 25 ;
}
public Object think(double dx, double dy, double x, double y, boolean moving, int terrain,
int ourID, int ourType, double hp, double maxHP, double range, double time,
double[] objX, double[] objY, int[] objID, int[] objFaction, int[] objType, int[][] incomingRadio)
{
if (moving ){ //if we are already moving
if (destX == Testing.teamX && destY == Testing.teamY) return null; // no new orders if we are headed to the team goal
destX = Testing.teamX;
destY = Testing.teamY;
return this;// get with the team
}
if (!moving && ourType != 0){// if we arent moving and we arent a city
if (destX == Testing.teamX && destY == Testing.teamY){// if we have arrived at the destination
Testing.teamX = r.nextDouble() * (dx);
Testing.teamY = r.nextDouble() * (dy);
return this;// choose random destination for the team
}
destX = Testing.teamX;
destY = Testing.teamY;
return this;// get with the team
}
return this;
}
public int build(double dx, double dy, double x, double y, int terrain, int id, int buildItem,
double hp, double maxHP, double time,
double[] objX, double[] objY, int[] objID, int[] objFaction, int[] objType, int[][] incomingRadio)
{
if (buildItem != 0) return 0; // if we're currently building, keep at it
return 1 + r.nextInt(3); // choose randomly
}
}