Complete.Org: Mailing Lists: Archives: freeciv-dev: February 2002:
[Freeciv-Dev] Re: [PATCH] aiunit.c ai_military_findvictim() cleanup (PR#
Home

[Freeciv-Dev] Re: [PATCH] aiunit.c ai_military_findvictim() cleanup (PR#

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: Petr Baudis <pasky@xxxxxxxxxxx>, Gregory Berkolaiko <gberkolaiko@xxxxxxxxxxx>
Cc: freeciv-dev@xxxxxxxxxxx, bugs@xxxxxxxxxxxxxxxxxxx
Subject: [Freeciv-Dev] Re: [PATCH] aiunit.c ai_military_findvictim() cleanup (PR#1264)
From: Raahul Kumar <raahul_da_man@xxxxxxxxx>
Date: Wed, 20 Feb 2002 06:51:47 -0800 (PST)

--- Petr Baudis <pasky@xxxxxxxxxxx> wrote:


> > Yep, that is clear.  But why this values are multiplied, why in such
> > particular way, why firepower is not taken into account, why health is not
> > taken into account, why if the inequality is satisfied we would better be
> > defending?  A lot of question if you think about it instead of just staring
> > at it.
>

The exact same questions I would have asked.
 

> Well, I think you're right and it's probably buggy, as I can't find any
> explanation for that as well. Added as FIXME.
> 
> About the multiplying; it's just simple proportion - you want do defend the
> city more when both you and he has little chances to succeed in attack
> (because
> he will be able to defend against you well); and you want to defend it less
> when both you and he has big chances to succeed in attack anyway. If the
> chances are equal, you'll still try to attack if you aren't alone in the
> city.
> 
> The comparasion isn't ideal in any rate. However I still lack enough wide
> knowledge about military in AI so I can't tell if it will hurt that much - I
> think it won't, as we can't be so much accurate everytime. Nevertheless,
> about
> the health - why we should consider it? If we are already weak, it doesn't
> really matter if we will attack or defend, chances to survive are AFAIK
> scaling equally.
> 

> /me looks around in his mailbox..
> /me sees nothing :(
> 

Great explanation of the attack/defending formulas. You've really done some
work
on this.

 retrieving revision 1.20
> diff -u -r1.1.1.5 -r1.20
> Index: aiunit.c
> ===================================================================
> RCS file: /home/cvs/aiciv/freeciv-a2/ai/aiunit.c,v
> retrieving revision 1.1.1.8
> retrieving revision 1.23
> diff -u -r1.1.1.8 -r1.23
> --- aiunit.c  19 Feb 2002 19:33:54 -0000      1.1.1.8
> +++ aiunit.c  20 Feb 2002 14:25:03 -0000      1.23
> @@ -733,106 +733,155 @@
>  }
>  
>  /*************************************************************************
> -This looks at tiles neighbouring the unit to find something to kill or
> -explore. It prefers tiles in the following order:
> +This function looks at tiles neighbouring the unit in order to find
> something
> +to kill or explore.  It prefers tiles in the following order:
> +
>  1. Undefended cities
>  2. Huts
>  3. Enemy units weaker than the unit
>  4. Land barbarians also like unfrastructure tiles (for later pillage)
> -If none of the following is there, nothing is chosen.
>  
> -work of Syela - mostly to fix the ZOC/goto strangeness
> +If none of the following is there, nothing is chosen.
>  **************************************************************************/
>  static int ai_military_findvictim(struct player *pplayer, struct unit
> *punit,
> -                               int *dest_x, int *dest_y)
> +                                  int *dest_x, int *dest_y)
>  {
> -  int x, y;
> -  int best = 0, a, b, c, d, e, f;
> -  struct unit *pdef;
> -  struct unit *patt;
> -  struct city *pcity;
> -  x = punit->x;
> -  y = punit->y;
> +#define SET_BEST(new_best) \
> +  do { best = new_best; *dest_x = x1; *dest_y = y1; } while (0)
> +
> +  int bellig = unit_belligerence_primitive(punit);
> +  int x = punit->x, y = punit->y;
> +  int best = 0;
> +  
>    *dest_y = y;
>    *dest_x = x;
> -  if (punit->unhappiness) best = 0 - 2 * MORT * TRADE_WEIGHTING; /*
> desperation */
> -  f = unit_type(punit)->build_cost;
> -  c = 0; /* only dealing with adjacent victims here */
> -
> -  if (get_transporter_capacity(punit)) {
> -    unit_list_iterate(map_get_tile(x, y)->units, aunit)
> -      if (!is_sailing_unit(aunit)) return(0);
> -    unit_list_iterate_end;
> -  } /* ferryboats do not attack.  no. -- Syela */
> +  
> +  if (punit->unhappiness > 0) {
> +    best = 0 - 2 * MORT * TRADE_WEIGHTING; /* desperation */
> +  }
> +
> +  /* Ferryboats with passengers do not attack. -- Syela */
> +  if (punit->ai.passenger > 0) {
> +    return 0;
> +  }
>  
>    adjc_iterate(x, y, x1, y1) {
> -    pdef = get_defender(punit, x1, y1);
> +    struct unit *pdef = get_defender(punit, x1, y1);
> +    
>      if (pdef) {
> -      patt = get_attacker(punit, x1, y1);
> -/* horsemen in city refused to attack phalanx just outside that was
> -bodyguarding catapult - patt will resolve this bug nicely -- Syela */
> -      if (can_unit_attack_tile(punit, x1, y1)) { /* thanks, Roar */
> -        d = unit_vulnerability(punit, pdef);
> -        if (map_get_city(x, y) && /* pikemen defend Knights, attack
> Catapults */
> -              get_total_defense_power(pdef, punit) *
> -              get_total_defense_power(punit, pdef) >= /* didn't like > --
> Syela */
> -              get_total_attack_power(patt, punit) * /* patt, not pdef */
> -              get_total_attack_power(punit, pdef) &&
> -              unit_list_size(&(map_get_tile(punit->x, punit->y)->units)) < 2
> &&
> -           get_total_attack_power(patt, punit)) { 
> -       freelog(LOG_DEBUG, "%s defending %s from %s's %s",
> -               unit_type(punit)->name,
> -               map_get_city(x, y)->name,
> -               unit_owner(pdef)->name, unit_type(pdef)->name);
> -        } else {
> -          a = reinforcements_value(punit, pdef->x, pdef->y);
> -          a += unit_belligerence_primitive(punit);
> -          a *= a;
> -          b = unit_type(pdef)->build_cost;
> -          if (map_get_city(x1, y1)) /* bonus restored 980804 -- Syela */
> -            b = (b + 40) * punit->hp / unit_type(punit)->hp;
> -/* c is always equal to zero in this routine, and f is already known */
> -/* arguable that I should use reinforcement_cost here?? -- Syela */
> -          if (a && is_my_turn(punit, pdef)) {
> -            e = ((b * a - f * d) * SHIELD_WEIGHTING / (a + d) - c *
> SHIELD_WEIGHTING);
> -/* no need to amortize! */
> -            if (e > best && ai_fuzzy(pplayer, TRUE)) {
> -           freelog(LOG_DEBUG, "Better than %d is %d (%s)",
> -                         best, e, unit_type(pdef)->name);
> -              best = e; *dest_y = y1; *dest_x = x1;
> -            } else {
> -           freelog(LOG_DEBUG, "NOT better than %d is %d (%s)",
> -                         best, e, unit_type(pdef)->name);
> -         }
> -          } /* end if we have non-zero belligerence */
> +      struct unit *patt = get_attacker(punit, x1, y1);
> +
> +      if (!can_unit_attack_tile(punit, x1, y1))
> +        continue;
> +
> +      /* If we are in the city, let's deeply consider defending it -
> however,
> +       * horsemen in city refused to attack phalanx just outside that was
> +       * bodyguarding catapult; thus, we get the best attacker on the tile
> as
> +       * well, for the case when there are multiple different units on one
> +       * title. Thus we force punit to attack a stack of units if they are
> +       * endangering punit seriously, even if they aren't that weak. */
> +      /* FIXME: The get_total_defense_power(pdef, punit) should probably use
> +       * patt rather than pdef. */
> +      if (map_get_city(x, y)
> +          && get_total_defense_power(pdef, punit) *
> +             get_total_defense_power(punit, pdef) >= /* didn't like > --
> Syela */
> +             get_total_attack_power(patt, punit) *
> +             get_total_attack_power(punit, pdef)
> +          && unit_list_size(&(map_get_tile(punit->x, punit->y)->units)) < 2
> +          && get_total_attack_power(patt, punit) > 0) {
> +        freelog(LOG_DEBUG, "%s defending %s from %s's %s",
> +                unit_type(punit)->name,
> +                map_get_city(x, y)->name,
> +                unit_owner(pdef)->name, unit_type(pdef)->name);
> +        
> +      } else {
> +        int vuln = unit_vulnerability(punit, pdef);
> +        /* The total possible attack power we can throw on the victim. Note
> +         * that we will even square this. */
> +        int attack = reinforcements_value(punit, pdef->x, pdef->y)
> +                     + bellig;
> +        /* Something like 'attractiveness' of the victim, how nice it would
> be
> +         * to destroy it. Larger value, worse loss for enemy. */
> +        int benefit = unit_type(pdef)->build_cost;
> +        /* We're only dealing with adjacent victims here. */
> +        /* TODO: Remove this when moving the desire equation to separate
> +         * function. */
> +        int move_cost = 0;
> +        /* The possible loss when we would loose this unit. */
> +        /* TODO: Possibly remove this when moving the desire equiation to
> +         * separate function? */

Equation instead of equiation.

> +        int loss = unit_type(punit)->build_cost;
> +        
> +        attack *= attack;
> +        
> +        /* If the victim is in the city, we increase the benefit and correct
> +         * it with our health because there may be more units in the city
> +         * stacked, and we won't destroy them all at once, so in the next
> +         * turn they may attack us. So we shouldn't send already injured
> +         * units to useless suicide. */

Hmm, intriguing.

> +        if (map_get_city(x1, y1)) {
> +          benefit = (benefit + 40) * punit->hp / unit_type(punit)->hp;
>          }
> -      }
> -    } else { /* no pdef */
> -      pcity = map_get_city(x1, y1);
> -      if (pcity && is_ground_unit(punit) &&
> -          map_get_terrain(punit->x, punit->y) != T_OCEAN) {
> -        if (pcity->owner != pplayer->player_no) { /* free goodies */
> -          best = 99999; *dest_y = y1; *dest_x = x1;
> +        
> +        /* If we have non-zero belligerence... */
> +        if (attack > 0 && is_my_turn(punit, pdef)) {
> +          int desire;
> +          
> +          /* TODO: This equation is simplified version of much worse ones in
> +           * that long fat routines, but it's still a common pattern, so we
> +           * will can this equation to one separate readable function. */
> +         
> +          /*         attractiveness     danger */ 
> +          desire = ((benefit * attack - loss * vuln) * SHIELD_WEIGHTING
> +                    / (attack + vuln) - move_cost * SHIELD_WEIGHTING);
> + 

The usage of move_cost. I do not see how the penalty for attacking with less
than one movepoint left is represented in this formula.
         
> +          /* No need to amortize! */
> +          
> +          if (desire > best && ai_fuzzy(pplayer, 1)) {
> +            freelog(LOG_DEBUG, "Better than %d is %d (%s)",
> 



__________________________________________________
Do You Yahoo!?
Yahoo! Sports - Coverage of the 2002 Olympic Games
http://sports.yahoo.com


[Prev in Thread] Current Thread [Next in Thread]