message() method

AI discussion, ideas, and SDK help.
Post Reply
User avatar
SunTzu
Lux Cartographer
Posts: 1586
Joined: Sat Jan 14, 2006 1:48 am
Location: Maryland

message() method

Post by SunTzu » Tue Jul 10, 2007 11:05 pm

Are there any constraints on what can go in the message() method?

Lux seems to have a problem with list.toArray()...
Log.txt wrote:There was an error in com.sillysoft.lux.agent.Defender@132021a's message("attackNotice", x) phase:
java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at java.util.Vector.toArray(Unknown Source)
at com.sillysoft.lux.agent.Defender.message(Defender.java:421)
at com.sillysoft.lux.B.C(Unknown Source)
at com.sillysoft.lux.B.A(Unknown Source)
at com.sillysoft.lux.gui.H.A(Unknown Source)
at com.sillysoft.lux.gui.K.A(Unknown Source)
at com.sillysoft.lux.gui.K.mousePressed(Unknown Source)
at java.awt.AWTEventMulticaster.mousePressed(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

User avatar
dustin
Lux Creator
Lux Creator
Posts: 10915
Joined: Thu May 15, 2003 2:01 am
Location: Cascadia
Contact:

Post by dustin » Wed Jul 11, 2007 3:39 am

Can you post your code? The method I posted before works in message() for me when I tested it.

http://java.sun.com/j2se/1.4.2/docs/api ... ption.html

User avatar
SunTzu
Lux Cartographer
Posts: 1586
Joined: Sat Jan 14, 2006 1:48 am
Location: Maryland

Post by SunTzu » Wed Jul 11, 2007 9:08 am

Here's the code. This statement

homeCountries = (Country[]) list.toArray(homeCountries);

is the one referenced in the error message I posted before. (Defender.java:421)

Code: Select all

public String message( String message, Object data )
	{
	 if ("attackNotice".equals(message) && board.getTurnCount() == 1 )
		{
		boolean onList = false;

		// Get attack notice info
		List dataList = (List) data;
		int attackingCountryCode = ((Integer)dataList.get(0)).intValue();
		int defendingCountryCode = ((Integer)dataList.get(1)).intValue();

		 // Put all of our countries into a Vector
		Vector list = new Vector();
		CountryIterator mine = new PlayerIterator(ID, countries);
		while (mine.hasNext())
			{
			Country current = mine.next();

			// Check if attacked country is on list
			if (defendingCountryCode == current.getCode() )
				{
				onList = true;
				}
  			list.add(this);
			}

		// Add attacked country if it's not on the list
		if (!onList)
			{
			list.add(countries[defendingCountryCode]);
			}

		// Transfer the Vector into an array
		Country[] homeCountries = new Country[ list.size() ];
		homeCountries = (Country[]) list.toArray(homeCountries);
    	knowsHome = true;
		}
         return null;
	}

User avatar
Bertrand
Reaper Creator
Posts: 568
Joined: Mon Nov 28, 2005 4:35 pm
Location: Montreal

Post by Bertrand » Wed Jul 11, 2007 12:26 pm

The list.add(this) statement looks suspect. I would try list.add(current) instead.

User avatar
dustin
Lux Creator
Lux Creator
Posts: 10915
Joined: Thu May 15, 2003 2:01 am
Location: Cascadia
Contact:

Post by dustin » Wed Jul 11, 2007 3:54 pm

Yeah, any time you use this, it's a reference to your Agent.

User avatar
SunTzu
Lux Cartographer
Posts: 1586
Joined: Sat Jan 14, 2006 1:48 am
Location: Maryland

Post by SunTzu » Wed Jul 11, 2007 4:24 pm

Thanks guys, that fixed the original error!

For some reason, the boolean knowsHome variable was received by the global variable, but not the Country[] homeCountries object.

I found a way to bypass having to use message() for what I want to accomplish, but I'm curious why the Country[] object wasn't set...

User avatar
dustin
Lux Creator
Lux Creator
Posts: 10915
Joined: Thu May 15, 2003 2:01 am
Location: Cascadia
Contact:

Post by dustin » Wed Jul 11, 2007 4:38 pm

Code: Select all

      // Transfer the Vector into an array
      Country[] homeCountries = new Country[ list.size() ]; 
You're declaring a local array with that line, so in the local context it will use the local var, not the global one. Take away the beginning "Country[]" declaration on that line and it will use the global one.

Post Reply