Java AI Help

Java AI Help

Here is where ideas can be collected for the skirmish AI in development

Moderators: hoijui, Moderators

Post Reply
msm8bball
Posts: 52
Joined: 09 Oct 2009, 08:08

Java AI Help

Post by msm8bball »

I'm really new to developing on this scale. I'm a Computer Information Technology Engineering major and I've only had a few Java classes so far.

I'm attempting to dive into writing a Java AI, but I'm kind of lost. I followed all the instructions at http://springrts.com/wiki/AI:Development:Lang:Java but when I attempt to test it in game, at the end of the countdown the game crashes. I don't see any output or know of a way to make it do output, so I don't know where to start looking for mistakes.

Code: Select all

package myjavaai;


import com.springrts.ai.oo.*;

public class MyJavaAIFactory extends OOAIFactory {
	OOAICallback callback;

public OOAI createAI(int teamId, OOAICallback callback)
	{
	
	return new myjavaai.MyJavaAI(teamId, callback);
	}

}
and

Code: Select all

package myjavaai;
import java.util.ArrayList;
import java.util.List;

import com.springrts.ai.AICommand;
import com.springrts.ai.AICommandWrapper;
import com.springrts.ai.command.BuildUnitAICommand;
import com.springrts.ai.oo.*;

public class MyJavaAI extends AbstractOOAI {
	
	OOAICallback clb;
	public int myteamID=0;
	private Unit commander;
	List<UnitDef> unitDefs = this.clb.getUnitDefs();
	UnitDef solarPlant = null;
	
	public MyJavaAI(int teamId, OOAICallback callback)
	{init(teamId, callback);
	
		
	}
	@Override public int init(int teamId, OOAICallback callback) {
			
		   this.clb = callback;
		   myteamID=teamId;
		   
		   for (UnitDef def : unitDefs)
		     if (def.getName().equals("armsolar"))
		     {
		         solarPlant = def;
		         break;
		     }
		   return 0;

		} 
	@Override public int unitFinished(Unit unit) {

		   if (unit.getDef().getName().equals("armcom"))
		       this.commander = unit;

		   return 0;
		}
	
	@Override public int update(int frame) {

		   if (frame == 0)
		   {
			AICommand command = new BuildUnitAICommand(this.commander, -1,
			           new ArrayList<AICommand.Option>(), 10000, solarPlant,
			           this.commander.getPos(), 0);
			this.clb.getEngine().handleCommand(AICommandWrapper.COMMAND_TO_ID_ENGINE,
			           -1, command);


		   }

		   return 0;

		} 

}
and here's the Lua file....

Code: Select all

--
--  Info Definition Table format
--
--
--  These keywords must be lowercase for LuaParser to read them.
--
--  key:      user defined or one of the SKIRMISH_AI_PROPERTY_* defines in
--            SSkirmishAILibrary.h
--  value:    the value of the property
--  desc:     the description (could be used as a tooltip)
--
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

local infos = {
	{
		key    = 'shortName',
		value  = 'MyJavaAI',
		desc   = 'machine conform name.',
	},
	{
		key    = 'version',
		value  = '0.1', -- AI version - !This comment is used for parsing!
	},
	{
		key    = 'className',
		value  = 'myjavaai.MyJavaAIFactory',
		desc   = 'fully qualified name of a class that implements interface com.springrts.ai.AI',
	},
	{
		key    = 'name',
		value  = 'high-level Java stub Skirmish AI',
		desc   = 'human readable name.',
	},
	{
		key    = 'loadSupported',
		value  = 'no',
		desc   = 'whether this AI supports loading or not',
	},
	{
		key    = 'interfaceShortName',
		value  = 'Java', -- AI Interface name - !This comment is used for parsing!
		desc   = 'the shortName of the AI interface this AI needs',
	},
	{
		key    = 'interfaceVersion',
		value  = '0.1', -- AI Interface version - !This comment is used for parsing!
		desc   = 'the minimum version of the AI interface required by this AI',
	},
}

return infos
If this isn't the right place for help, I apologize. Where should I go if I need help? Thanks!
User avatar
hoijui
Former Engine Dev
Posts: 4344
Joined: 22 Sep 2007, 09:51

Re: Java AI Help

Post by hoijui »

i think the best thing would be, if you you first try to compile one of the Java AIs about which you know they work, and then start altering that. When you feel comfortable enough, you can stil start a new project from scratch.
If you want to get around compiling spring yourself, then have a look at NullJavaAI, NullOOJavaAI and HughAI(can be found here in the forum). to get the source code, go http://springrts.com/wiki/Building_spring and get sources from git.
when you got that, you can do git submodule update --init (this will get you HughAI sources). the AIs are under AI/Skirmish.

So in general, you should probably try to get familiar with all of it a bit (GIT, spring, the Java Interface), compile some stuff, look what you can alter, ...

the main log files youll be interested in are <springroot>/infolog.txt and <springroot>/AI/Interfaces/Java/0.1/log/*. To get more debugging info, look at and change some vars in <springroot>/AI/Interfaces/Java/0.1/interface.properties.
Post Reply

Return to “AI”