Page 1 of 2

AI MapHack!

Posted: 20 Nov 2005, 19:19
by krogothe
:lol: :lol: :lol: :lol:

Posted: 20 Nov 2005, 19:34
by jcnossen
AI's are already doing something similar, only split up in multiple arrays to save data and speed up. For example there is the metal map that everyone is using, JCAI used to have a map with "scores" for a while, and a 2d array to store occupation of buildings on, a 2d array to store various gamestate such as enemy firepower....

My point is, there is no reason to put all data into one big array. You can better seperate things and make various arrays that are scaled up for a particular job. It's a good programming habit to split tasks up, and so it's logical to also split up the data that is used by these tasks/processes.

Posted: 21 Nov 2005, 00:07
by krogothe
Actually the Array would be 3 dimensional:

float Score [x][y][typeofscore] = some function

or yes i could make a load of 2d arrays. Some functions however will use combined scores from different things (eg placing a far away resource building like a geo or mex will check against the threat array). The best way would probably be how you suggested since conservative calculations are giving me an array size of over a megabyte already!
I read the source for JCAI and absolutely loved that function to create a TGA picture with the maps. that could be expanded into a realtime PIP map (like the radar), where you can switch the score type displayed using the debugger.
Since the TGA maker would be a simple function that could be reused that is definitely something i want to try!
I need to make a proper, concrete design plan for the whole AI now. I will be an able enough programmer within a couple of days and it is getting quite complex. I shall expand on this idea later!
Thanks for the tip Zaph it does make sense!

Posted: 21 Nov 2005, 01:26
by Triaxx2
Why not have a single, expungable 3D array, that is generated only when it's needed, and pulls data from the various 2D array's?

Posted: 21 Nov 2005, 01:57
by jcnossen
If you're talking about visualization, use the debug window code in JCAI. Type .debugwindow in game to show what it does...

If you need to have a generic scoring function for these 2D arrays, then it is better to use templates than to put everything in one array. I already started to use something similar in JCAI:

Code: Select all

	template<typename HF> int2 SelectGameInfoSector (HF hf)
	{
		float bestscore;
		int2 best (-1,0);

		for (int y=0;y<gsh;y++)
			for (int x=0;x<gsw;x++)
			{
				GameInfo& i=gameinfo[y*gsw+x];
				float score = hf(x,y,i);
				if (best.x < 0 || bestscore < score)
				{
					best=int2(x,y);
					bestscore=score;
				}
			}
		return best;
	}
Now you can write a function:

float GetSectorScore (int  x,int y,GameInfo& i) {
  return i.EnemyValue / i.EnemyPower;
}

or something...

You could argue that this is also possible with a function pointer, but now I can use boost::bind on it...
There are probably better ways to do this, I never spend much time on it, but my point is that there are a lot of ways (and using templates is a very nice one of them) to implement a flexible or generic algorithm into C++.

You could make a template like this:

Code: Select all

template<typename ElemType, int bw, int bh>
class StateMap 
{
public:
  enum { BlockW=bw, BlockH=bh };
  ElemType& Get (int x,int y) { 
   return blocks[(y/BlockH)*w+x/BlockW]; 
  }

  template<typename OtherStateMap>
  ElemType& GetFromOtherStateMapCoords(OtherStateMap &other, int x, int y) {
    int rx = x * OtherStateMap::BlockW;
    int ry = y * OtherStateMap::BlockH;
    return Get(rx,ry);
  }

  int w,h;
  ElemType *blocks;
};

Which you can use like:

StateMap<EnemyInfo, 32,32> enemyInfoMap;
StateMap<MetalInfo,64,64> metalInfoMap;

MetalInfo& mi = metalInfoMap.GetFromOtherStateMapCoords (enemyInfoMap, 3, 5);

// now mi points to the metal info element on (3,5) on the enemyinfo map.
Hmm I should have made this earlier ;)

Posted: 21 Nov 2005, 07:49
by b1ind
OT: What is the need for the enum in there? Is that the only way you can assign default values to members?

Posted: 21 Nov 2005, 12:46
by jcnossen
I think you can do it with a constant as well, but then the linker also starts messing with it. I prefer to do it with an enumerator...

Posted: 21 Nov 2005, 18:05
by krogothe
:lol: :lol: :lol:

Posted: 21 Nov 2005, 18:42
by jcnossen
The statemap template allows you to use maps of different resolutions and compare their values quickly without having to code all the position scaling... sorry if it was a little offtopic, it was a nice idea that I suddenly had to implement :wink:

For pathfinding you might want to look into Dijkstra's algorithm, it allows pathfinding for a single goal and many units so to say... probably very useful when the AI needs to brake through the weakest defenses

If you want to test your energy calculations, you could use JCAI as a base. I never had the time or motivation to make good calculations for the resource "curve", but the framework is there... Look into ResourceUnitHandler.cpp, it's generates the tasks for resource buildings, and if you replace that with your own you can have results very soon... this would be a win-win situation :wink:
Send me a PM if you're interested.

Posted: 21 Nov 2005, 20:16
by AF
After that i dunno if this would eb of any use.

See the gridmanager clas sin NTAI. It isnt being sued and I had a few extras to add, btu ti let you save values in matrixes of different resolutions and had a few methods to do thgins sucha s icnrease all values in the grid by a percentage and draw TGA etc....

Posted: 29 Nov 2005, 21:16
by Veylon
I've done alright with managing energy and metal.

Just make sure the AI doens't build anything unaffordable and no Building units when metal < 10% or factories when metal < 50%. That's about all the rules I've needed.

Posted: 29 Nov 2005, 21:18
by AF
Aha, I thought G->Pl->feasable was a little odd in how it filtered out odd orders.

Posted: 30 Nov 2005, 00:55
by jcnossen
Just make sure the AI doens't build anything unaffordable and no Building units when metal < 10% or factories when metal < 50%. That's about all the rules I've needed.
And when it is trying to create new build tasks, you look at all the other tasks to determine whether something is affordable I suppose? Might just work for jcai as well I think... :-)

Posted: 30 Nov 2005, 10:37
by Targ Collective
Too literal. The AI needs to think in terms of income vs. expenditure, and make decisions based on that; not based on stockpiled resources.

Posted: 30 Nov 2005, 11:33
by jcnossen
Might be, but you're not making an AI... ;)
JCAI "thinks" in terms of income vs expenditure currently, and see what that leads to. Simpler systems can often be more effective, but they have less "side factors" that have to be fulfilled.
You can't calculate everything, you run into high processor usage or not being able to know things precisely... so then the more precise decision making algorithms can screw things up very much. I'm not saying that's the only thing wrong with jcai right now, but it's one of the biggest problems.

Posted: 30 Nov 2005, 12:24
by BvDorp
Zaphod, I see u talking about JCAI again... why not make some brilliant version to show the others how it must be done? :twisted:

Just kidding you here.. But I see you suggesting some features etc, y not implent them?

Posted: 30 Nov 2005, 12:59
by jcnossen
Because other things are have a higher priority.

Posted: 30 Nov 2005, 20:11
by AF
Such a tradeoff is a sign of a bad approach to the problem.

Posted: 30 Nov 2005, 20:42
by jcnossen
Whatever

Posted: 30 Nov 2005, 20:46
by AF
-_-