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: Raimar Falke <rf13@xxxxxxxxxxxxxxxxx>
Cc: "Per I. Mathisen" <per@xxxxxxxxxxx>, freeciv-ai@xxxxxxxxxxx
Subject: [freeciv-ai] Re: definitely last version of active diplomats patch
From: Mike Kaufman <kaufman@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 6 Sep 2002 10:00:41 -0500

On Fri, Sep 06, 2002 at 09:48:04AM +0200, Raimar Falke wrote:

what kind of crack are you smoking Raimar? Why on earth would you want to
move all these variables into the loop? Please explain from a performance
computing aspect...

-mike

> > +{
> > +  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;
> > +}
> > +
> > +/**************************************************************************
> > +  Find units that we can reach, and bribe them. Returns TRUE if survived
> > +  the ordeal, FALSE if not or we expended all our movement.
> > +
> > +  Requires an initialized warmap!
> > +**************************************************************************/
> > +static bool ai_diplomat_bribe_nearby(struct player *pplayer, 
> > +                                     struct unit *pdiplomat)
> > +{
> > +  struct packet_diplomat_action packet;
> > +  int move_rate = unit_type(pdiplomat)->move_rate;
> > +  struct unit *pvictim;
> > +  struct tile *ptile;
> > +  int gold_avail = pplayer->economic.gold - pplayer->ai.est_upkeep;
> > +  int cost, destx, desty;
> > +  bool target = FALSE;
> > +  enum goto_result result;
> > +
> > +  /* Check ALL possible targets */
> > +  do {
> 
> Same here.
> 
> > +   whole_map_iterate(x, y) {
> > +    ptile = map_get_tile(x, y);
> > +    if (warmap.cost[x][y] > move_rate && ptile->terrain != T_OCEAN) {
> > +      /* Can't get there */
> > +      continue;
> > +    }
> > +    destx = x;
> > +    desty = y;
> > +    if (ptile->terrain == T_OCEAN) {
> > +      /* Try to bribe a ship on the coast */
> > +      int best = 9999;
> > +      adjc_iterate(x, y, x2, y2) {
> > +        if (best > warmap.cost[x2][y2]) {
> > +          best = warmap.cost[x2][y2];
> > +          destx = x2;
> > +          desty = y2;
> > +        }
> > +      } adjc_iterate_end;
> > +      if (best >= move_rate) {
> > +        /* Can't get there, either */
> > +        continue;
> > +      }
> > +    }
> > +    pvictim = unit_list_get(&ptile->units, 0);
> > +    if (!pvictim || !pplayers_at_war(pplayer, unit_owner(pvictim))) {
> > +      continue;
> > +    }
> > +    if (unit_list_size(&ptile->units) > 1) {
> > +      /* Can't do a stack */
> > +      continue;
> > +    }
> > +    if (map_get_city(x, y)) {
> > +      /* Can't do city */
> > +      continue;
> > +    }
> > +    if (government_has_flag(get_gov_pplayer(unit_owner(pvictim)), 
> > +                            G_UNBRIBABLE)) {
> > +      /* Can't bribe */
> > +      continue;
> > +    }
> > +    cost = pvictim->bribe_cost = unit_bribe_cost(pvictim);
> > +    if (cost > gold_avail) {
> > +      /* Can't afford */
> > +      continue;
> > +    }
> > +    /* Found someone! */
> > +    pdiplomat->goto_dest_x = destx;
> > +    pdiplomat->goto_dest_y = desty;
> > +    result = do_unit_goto(pdiplomat, GOTO_MOVE_ANY, FALSE);
> > +    if (result == GR_DIED || pdiplomat->moves_left == 0) {
> > +      return FALSE;
> > +    }
> > +    if (diplomat_can_do_action(pdiplomat, DIPLOMAT_BRIBE, x, y)) {
> > +      packet.diplomat_id = pdiplomat->id;
> > +      packet.target_id = pvictim->id;
> > +      packet.action_type = DIPLOMAT_BRIBE;
> > +      handle_diplomat_action(pplayer, &packet);
> > +    } else {
> > +      /* usually because we ended move early due to another unit */
> > +      UNIT_LOG(LOG_DIPLOMAT, pdiplomat, "could not bribe target "
> > +               " (%d, %d), %d moves left", x, y, pdiplomat->moves_left);
> > +    }
> > +   } whole_map_iterate_end;
> > +  } while (target);
> > +
> > +  return (pdiplomat->moves_left > 0);
> > +}
> > +


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