This is the mail archive of the xconq7@sources.redhat.com mailing list for the Xconq project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

First stable version of improved ai_plan_research


The bug-fixes provided by Eric seem to have eliminated the crashes that
I was experiencing.  It is still far from perfect, but it should allow
the AI to be at least a litter smarter when it picks advances.

Over the next few days, I hope to make it also consider the relative
worths of units that require the advance, what other advances require
the advance in question, and how long it will take to achieve the
advance.


A perfect AI would also have the following abilities for selecting
advances, all of which are (as far as I can tell) impossible to
implement in its current form:

1. Decide what kinds of units it wants, then choose the proper sequence
of advances to gain access to them.
2. If an advance is required for an advanced unit to grow beyond a
certain size, try to obtain that advance before the unit reaches that
size.
3. Determine that an existing unit requires another unit to function
properly, and if that second unit is unavailable, research the necessary
advances.

And so on, and so on, and so on...

---
Lincoln Peters
<sampln@sbcglobal.net>

What use is magic if it can't save a unicorn?
		-- Peter S. Beagle, "The Last Unicorn"


Here's the new function:

static void
ai_plan_research(Side *side)
{
	/*  There are many things to consider when choosing an advance:
	 * 1. What new units do I gain?
	 * 2. How does it affect my existing units?
	 * 3. How long will it take to gain this advance?
	 * 4. Based on all of these merits, how important is this advance?
	 *
	 * Unfortunately, many of these issues are impossible to thoroughly
	 * handle without radically re-engineering the AI, so I'll do what I
	 * can with what we have.
	 *           -- Lincoln Peters
	 */
	
	int numAdvances = 0;	/* Number of advances that could be researched */
	int tWeight = 0;	/* Total weight (potential value) of all advances */
	int *uWeight;		/* Peceived value of available advances */
	int count1 = 0;		/* Used in any place where a counter is needed */
	int count2 = 0;		/* Likewise */
	int count3 = 0;		/* Likewise */
	int result;		/* Random number to determine advance with weighting */
	
	int n, a, i;		/* Legacy code; will be removed after debugging */
	
	/* First, get a count of the available advances. */
	
	for_all_advance_types(count1) {
		if ( side_can_research( side, count1 ) ) {
			numAdvances++;
		}
	}
	
	if ( numAdvances == 0 ) {
		/* No advances are available! */
		Dprintf( "Warning: Trying to pick an advance when no advances are available.\n" );
		net_set_side_research( side, NONATYPE );
		return;
	}
	
	/* Now try to calculate the worth of those advances. */
	
	/* Allocating memory for this array is sooooo much easier in C++! */
	Dprintf( "About to allocate the array for tracking advance worth...\n" );
	/* uWeight = malloc( 2 * numAdvances ); */
	uWeight = (int *)xmalloc(numAdvances * sizeof(int));
	Dprintf( "Finished allocating the advance worth array.\n" );
	
	Dprintf( "Evaluating the worth of advances...\n" );
	for_all_advance_types(count1) {
		if ( side_can_research( side, count1 ) ) {
			uWeight[count2] = 0;
			
			/* Does it enable new units? */
			for_all_unit_types(count3) {
				if ( ua_needed_to_build( count3, count1 ) ) {
					/* This unit needs this advance. */
					uWeight[count2]++;
				}
				
				/* The following block of code refers to tables
				 * that exist in the documentation, but not in
				 * the code.  It should be possible to safely
				 * un-comment it once those tables are properly
				 * implemented.
				 
				if ( ua_multiply_production( count3, count1 ) > 1 ) {
					uWeight[count2] += ua_multiply_production( count3, count1 );
				} else if ( ua_multiply_production( count3, count1 ) < 1 ) {
					uWeight[count2] -= 1.0 / ua_multiply_production( count3, count1 );
				}
				
				uWeight[count2] = uWeight[count2] + ua_add_production( count3, count1 );
				*/
			}
			
			/* Just to be safe... */
			if ( uWeight[count2] < 1 ) {
				uWeight[count2] = 1;
			}
			
			tWeight += uWeight[count2];
			count2++;
		}
		
	}
	Dprintf( "Finished determines worth of advances.\n" );
	
	/* Need to adjust worth based on time required to achieve it. */
	
	/* Finally, choose a course of action! */
	
	result = xrandom(tWeight);
	count2 = 0;
	
	Dprintf( "Selecting an advance...\n" );
	for_all_advance_types(count1) {
		if ( side_can_research( side, count1 ) ) {
			if ( result <= uWeight[count2] ) {
				/* We have a course of action! */
				Dprintf( "Selected an advance!  Cleaning up...\n" );
				free( uWeight );
				Dprintf( "Successfully deallocated the advance worth array.\n" );
				net_set_side_research(side, count2);
				Dprintf( "Advance selection complete.\n" );
				return;
			} else {
				/* Not this one.  Keep looking... */
				result -= uWeight[count2];
			}
			
			count2++;
		}
	}
	
	/* If we've gotten here without a course of action being chosed,
	 * something really weird has happened. */
	 
	Dprintf( "AI failed to pick an advance; falling back to the bad decision-making process!\n");
	
	free( uWeight );
	
	

	i = 0;
	for_all_advance_types(a) {
	    if (side_can_research(side, a)) {
		++i;
	    }
	}
	n = xrandom(i);
	i = 0;
	for_all_advance_types(a) {
	    if (side_can_research(side, a)) {
		if (i == n) {
		    net_set_side_research(side, a);
		    return;
		}
		++i;
	    }
	}
	net_set_side_research(side, NONATYPE);
	
}



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]