| | 27 | 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. |
| | 28 | |
| | 29 | 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: |
| | 30 | |
| | 31 | {{{ |
| | 32 | protected void configure() throws IOException{ |
| | 33 | File configfile = new File(System.getenv("HOME") + "/.quagents3/quagents3/quagents-config.cfg"); |
| | 34 | if(!configfile.isFile()) |
| | 35 | configfile.createNewFile(); |
| | 36 | else{ |
| | 37 | configfile.delete(); |
| | 38 | configfile.createNewFile(); |
| | 39 | } |
| | 40 | Writer config = new FileWriter(configfile); |
| | 41 | config.write("LOC_FLAG 1"); |
| | 42 | config.flush(); |
| | 43 | if(logfilename!=""){ |
| | 44 | config.write("LOC_FILENAME " + logfilename); |
| | 45 | config.flush(); |
| | 46 | } |
| | 47 | if(lograte!=-1){ |
| | 48 | config.write("NUM_FRAMES_PER_LOG " + lograte); |
| | 49 | config.flush(); |
| | 50 | } |
| | 51 | } |
| | 52 | }}} |
| | 53 | |
| | 54 | This makes modifications to the server's configuration file, enabling the data logging changes in the server code. |
| | 55 | |
| | 56 | 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: |
| | 57 | |
| | 58 | {{{ |
| | 59 | protected void setGUI() { |
| | 60 | if(explorers.length==0){ |
| | 61 | Quagents.frame.setVisible(false); |
| | 62 | return; |
| | 63 | } |
| | 64 | Quagents.next = new CaveExplorationGUI(explorers,survivors,director); |
| | 65 | Quagents.frame.setName("Quagents3 " + scenarioname); |
| | 66 | Quagents.frame.add(Quagents.next); |
| | 67 | Quagents.frame.pack(); |
| | 68 | Quagents.frame.setVisible(true); |
| | 69 | } |
| | 70 | } |
| | 71 | }}} |
| | 72 | |
| | 73 | 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: |
| | 74 | |
| | 75 | {{{ |
| | 76 | protected void runQuagentCode(){ |
| | 77 | for(int i=0; i<explorers.length; i++) |
| | 78 | explorers[i].executeCode(); |
| | 79 | } |
| | 80 | }}} |
| | 81 | |
| | 82 | 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: |
| | 83 | |
| | 84 | {{{ |
| | 85 | protected void initialize() { |
| | 86 | for(int i=0; i<explorers.length; i++){ |
| | 87 | explorers[i] = new CaveExplorer(i); |
| | 88 | } |
| | 89 | for(int i=0; i<survivors.length; i++){ |
| | 90 | if(randwidth > 0) |
| | 91 | survivors[i] = new TrappedSurvivor(i,randwidth,randheight); |
| | 92 | else |
| | 93 | survivors[i] = new TrappedSurvivor(i); |
| | 94 | } |
| | 95 | } |
| | 96 | }}} |
| | 97 | |
| | 98 | These functions are executed automatically in the following order at startup: |
| | 99 | * ''configure'' |
| | 100 | * ''initialize'' |
| | 101 | * ''setGUI'' |
| | 102 | * ''runQuagentCode'' |
| | 103 | |
| | 104 | See the previously created scenarios in quagents3.main for more example code. |