wiki:Java Client Tutorial

Version 4 (modified by jherwitz, 13 years ago) (diff)

--

Java Client Tutorial

This tutorial deals with development and usage of the Quagents Java platform. For further information on classes, member functions, etc., see the Javadoc located in the Quagents3 download.

Front-end Development

This section details front-end Quagents development. This mostly takes place in the quagents3.main package.

Creating a Scenario

All scenarios must extend the abstract Scenario class. This implies that several functions and a constructor are required for every new scenario.

First let us discuss the ocnstructor. The Scenario constructor takes two arguments, a map name and a set of eligible maps to execute the scenario on. The set of maps is in the form of a string array, with each string being the name of the map as quake will recognize it. If the developer wishes to take advantage of the Quagents random map generator, simply put "randommap" as an element of this array.

public static final String[] maps = {"randommap", "delta","firstroom", "hurdles", "largeroom",
	"medroom", "smallroom", "maze1"};
private static final String scenarioname = "Sandbox";
//...
public Sandbox(){
		super(scenarioname,maps);
	}

The developer must also define a few specific functions for the Scenario class to be implemented. These are procedural, and the Scenario object will execute them automatically in a logical order to initialize the scenario correctly.

The configure function allows the developer to make server modifications, in order to enable any fundamental rules. For example, to enable data logging in the CaptureTheFlag? scenario, we have:

	protected void configure() throws IOException{
		File configfile = new File(System.getenv("HOME") + "/.quagents3/quagents3/quagents-config.cfg");
		if(!configfile.isFile())
			configfile.createNewFile();
		else{
			configfile.delete();
			configfile.createNewFile();
		}
		Writer config = new FileWriter(configfile);
		config.write("LOC_FLAG 1");
		config.flush();
		if(logfilename!=""){
			config.write("LOC_FILENAME " + logfilename);
			config.flush();
		}
		if(lograte!=-1){
			config.write("NUM_FRAMES_PER_LOG " + lograte);
			config.flush();
		}
	}

This makes modifications to the server's configuration file, enabling the data logging changes in the server code.

The setGUI function allows the user to insert a new GUI to run alongside the Quagents3 scenario execution. If no GUI is desired, simply define this as a blank function. The CaveExploration? handles this function as such:

protected void setGUI() {
		if(explorers.length==0){
			Quagents.frame.setVisible(false);
			return;
		}
		Quagents.next = new CaveExplorationGUI(explorers,survivors,director);
		Quagents.frame.setName("Quagents3 " + scenarioname);
		Quagents.frame.add(Quagents.next);
		Quagents.frame.pack();
		Quagents.frame.setVisible(true);
		}
	}

The runQuagentCode function allows the developer an opportunity to start his quagents executing. Many times, this consists of a call to the Quagent interface's executeCode function. The CaveExploration? scenario once again provides an example:

protected void runQuagentCode(){
		for(int i=0; i<explorers.length; i++)
			explorers[i].executeCode();
	}

Finally, the initialize function allows the developer an opportunity to instantiate and initialize Quagents or any other miscellaneous scenario components. For instance, in CaveExplorer? we have:

protected void initialize() {
		for(int i=0; i<explorers.length; i++){
			explorers[i] = new CaveExplorer(i);
		}
		for(int i=0; i<survivors.length; i++){
			if(randwidth > 0)
				survivors[i] = new TrappedSurvivor(i,randwidth,randheight);
			else
				survivors[i] = new TrappedSurvivor(i);
		}
	}

These functions are executed automatically in the following order at startup:

  • configure
  • initialize
  • setGUI
  • runQuagentCode

See the previously created scenarios in quagents3.main for more example code.

Writing Quagent Code

Note: The following may vary depending on QuagentEntity? type, but the steps necessary are invariant and will only differ syntactically.

The first step in creating a Quagent is construction. This is accomplished by calling the constructor with a parameter of the Quagents3 server IP address, in string form. That is:

Client quagent = new Client("127.0.0.1");

Next, we have the initialization phase. The quagent does not spawn on construction. Instead, we have a buffered connection to the server in which we may modify certain initial attributes. Among these are in-game name, spawn location, and team. When these attributes are set (for information on syntax, see javadoc) the developer may call the ready function. This finalizes all initial attributes and spawns the Quagent in-game. Trivially, it is implemented as:

quagent.ready();

The quagent has now entered the live phase of the game, during which most commands are unlocked for use. While many of these exist, the usage is constant throughout. Generally, the user must create an object for the Command they are about to execute. Then, the developer must execute it through a quagent. There are two options for this, the execute function which blocks until the command is returned from the server, or the start function which is non-blocking. For instance,

Rotate r = new Rotate(180);
quagent.execute(r);

or

Rotate r = new Rotate(180);
quagent.start(r);

are both valid. Additionally, the Client class provides many convenience functions, which allow execution directly through the quagent, of the form:

quagent.rotate(180);

Data and exit codes returned by these functions may be accessed through member getter functions. Note that these do not block, so usage of the Command or Quagent waitForTerminate (which blocks for return) function will ensure updated data.

It is also possible to enable client-based periodic functions. These execute every specified period. In order to enable this execution, simply append the desired period (in milliseconds) to the end of the constructor or function. For example,

Facing facing = new Facing(100);

will execute a Facing command every 100ms. This functionality is useful for sensor simulator or obtaining periodic data on the scenario execution.

Back-end Development

This section describes back-end Quagents development

Modifying internal structures =

Implementing a new Entity

QuagentEntity?

GODEntity