Complete.Org: Mailing Lists: Archives: freeciv-ai: September 2002:
[freeciv-ai] Re: definitely last version of active diplomats patch
Home

[freeciv-ai] Re: definitely last version of active diplomats patch

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: Mike Kaufman <kaufman@xxxxxxxxxxxxxxxxxxxxxx>
Cc: "Per I. Mathisen" <per@xxxxxxxxxxx>, freeciv-ai@xxxxxxxxxxx
Subject: [freeciv-ai] Re: definitely last version of active diplomats patch
From: Raimar Falke <rf13@xxxxxxxxxxxxxxxxx>
Date: Fri, 6 Sep 2002 19:10:20 +0200

On Fri, Sep 06, 2002 at 10:00:41AM -0500, Mike Kaufman wrote:
> On Fri, Sep 06, 2002 at 09:48:04AM +0200, Raimar Falke wrote:
> 
> what kind of crack are you smoking Raimar?

Nothing ;)

> Why on earth would you want to move all these variables into the
> loop?

To use the smallest possible scope. So it is obvious that you can't do
this for the best_dist variable. And it is also stupid to move
"continent" in the loop. I would like a "const" in front of the
"continent".

I'm also for adding something like "try to use the smallest possible
scope" to the style guides.

> Please explain from a performance computing aspect...

If you are talking about the extra memory allocations/disallocations
in nested loops: gcc already merge them and does the only one "sub" in
the function lead in.

> > > +{
> > > +  int dist, urgency;
> > > +  int best_dist = 30; /* any city closer than this is better than none */
> > > +  int best_urgency = 0;
> > > +  struct city *ctarget = NULL;
> > > +  int continent = map_get_continent(x, y);
> > > +
> > > +  city_list_iterate(pplayer->cities, acity) {
> > 
> > Move the stuff into the loop.
> > 
> > > +    if (continent != map_get_continent(acity->x, acity->y)) {
> > > +      continue;
> > > +    }
> > > +    urgency = acity->ai.urgency + 1;
> > > +    if (unit_type_flag(utype, F_DIPLOMAT)) {
> > > +      if (!acity->ai.has_diplomat && acity->ai.diplomat_threat) {
> > > +        urgency *= 5; /* we can help */
> > > +      } else if (acity->ai.has_diplomat) {
> > > +        urgency /= 3; /* we are not really needed there */
> > > +      }
> > > +    }
> > > +    dist = warmap.cost[acity->x][acity->y];
> > > +    /* I don't know if this formula is optimal, but it works. */
> > > +    if (dist > best_dist) {
> > > +      /* punish city for being so far away */
> > > +      urgency /= (float)(dist/best_dist);
> > > +    }
> > > +    if (urgency > best_urgency) {
> > > +      ctarget = acity;
> > > +      best_urgency = urgency;
> > > +      best_dist = MAX(dist,1); /* squelch divide-by-zero */
> > > +    }
> > > +  } city_list_iterate_end;
> > > +  return ctarget;
> > > +}

        Raimar

-- 
 email: rf13@xxxxxxxxxxxxxxxxx
 "Despite all the medical advances of the 20th century, the mortality 
  rate remains unchanged at 1 death per person."


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