From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 13088 invoked by alias); 8 Sep 2004 03:54:47 -0000 Mailing-List: contact xconq7-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: xconq7-owner@sources.redhat.com Received: (qmail 13070 invoked from network); 8 Sep 2004 03:54:46 -0000 Received: from unknown (HELO smtp810.mail.sc5.yahoo.com) (66.163.170.80) by sourceware.org with SMTP; 8 Sep 2004 03:54:46 -0000 Received: from unknown (HELO ?192.168.1.101?) (sampln@sbcglobal.net@67.123.174.46 with plain) by smtp810.mail.sc5.yahoo.com with SMTP; 8 Sep 2004 03:54:45 -0000 Subject: Unstable pre-release version of enhanced ai_plan_research From: Lincoln Peters To: Xconq list Content-Type: text/plain Message-Id: <1094615804.4338.58970.camel@localhost> Mime-Version: 1.0 Date: Wed, 08 Sep 2004 04:09:00 -0000 Content-Transfer-Encoding: 7bit X-SW-Source: 2004/txt/msg01138.txt.bz2 This version of ai_plan_research is woefully inadequate, but less so than the old version. So far, it just weights the advances by the number of units that depend on the advance. It still chooses advances randomly, but it has a higher chance of picking an advance that enables more units. And, no, it will not completely ignore advances that don't enable any units (since there may be benefits to such advances that it doesn't understand). However, since I introduced the code to my local source tree, I tried running "advances.g" under AI control and discovered that the new code is causing Xconq to segfault at seemingly-random times. Strangely enough, it never seems to crash while executing the ai_plan_research function. I have verified that it is somehow related to the new code, as I reverted to the old code and got no such crashes. My best guess is that I did something wrong with the calls to malloc() and/or free(); most of my programming experience is in C++, not C, and so I had to study those two functions at the last minute in order to figure out how to create a dynamic array without the aid of the "new" or "delete" keywords (and the documentation I was able to find is cryptic at best). I've included the new code at the bottom of this message. Perhaps someone more knowledgeable about C can figure out what's wrong with it? --- Lincoln Peters Don't worry about avoiding temptation -- as you grow older, it starts avoiding you. -- The Old Farmer's Almanac 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 thouroghly * 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 ); Dprintf( "Finished allocating the advance worth array.\n" ); Dprintf( "Evaluating the worth of advances...\n" ); for_all_advance_types(count1) { uWeight[count1] = 0; if ( side_can_research( side, count1 ) ) { /* 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); }