wiki:Java Client Tutorial

Version 3 (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

Running it all

Back-end Development

This section describes back-end Quagents development

Modifying internal structures =

Implementing a new Entity

QuagentEntity?

GODEntity