Author: cazfi
Date: Sun Dec 27 02:16:15 2015
New Revision: 31224

URL: http://svn.gna.org/viewcvs/freeciv?rev=31224&view=rev
Log:
Refactored adv_choice stuff so it's usually used via pointer

See patch #6662

Modified:
    trunk/ai/default/advdomestic.c
    trunk/ai/default/advdomestic.h
    trunk/ai/default/advmilitary.c
    trunk/ai/default/advmilitary.h
    trunk/ai/default/aicity.c
    trunk/ai/default/aitools.c
    trunk/ai/default/aitools.h
    trunk/server/advisors/advchoice.c
    trunk/server/advisors/advchoice.h

Modified: trunk/ai/default/advdomestic.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/ai/default/advdomestic.c?rev=31224&r1=31223&r2=31224&view=diff
==============================================================================
--- trunk/ai/default/advdomestic.c      (original)
+++ trunk/ai/default/advdomestic.c      Sun Dec 27 02:16:15 2015
@@ -441,8 +441,8 @@
 
   If want is 0, this advisor doesn't want anything.
 ***************************************************************************/
-void domestic_advisor_choose_build(struct ai_type *ait, struct player *pplayer,
-                                   struct city *pcity, struct adv_choice 
*choice)
+struct adv_choice *domestic_advisor_choose_build(struct ai_type *ait, struct 
player *pplayer,
+                                                 struct city *pcity)
 {
   struct adv_data *adv = adv_data_get(pplayer, NULL);
   /* Unit type with certain role */
@@ -450,8 +450,7 @@
   struct unit_type *founder_type;
   int settler_want, founder_want;
   struct ai_city *city_data = def_ai_city_data(pcity, ait);
-
-  adv_init_choice(choice);
+  struct adv_choice *choice = adv_new_choice();
 
   /* Find out desire for workers (terrain improvers) */
   settler_type = dai_role_utype_for_terrain_class(pcity, UTYF_SETTLERS,
@@ -550,22 +549,22 @@
   }
 
   {
-    struct adv_choice cur;
-
-    adv_init_choice(&cur);
+    struct adv_choice *cur;
+
+    cur = adv_new_choice();
     /* Consider building caravan-type units to aid wonder construction */  
-    dai_choose_help_wonder(ait, pcity, &cur, adv);
-    copy_if_better_choice(&cur, choice);
-
-    adv_init_choice(&cur);
+    dai_choose_help_wonder(ait, pcity, cur, adv);
+    choice = adv_better_choice_free(choice, cur);
+
+    cur = adv_new_choice();
     /* Consider city improvements */
-    building_advisor_choose(pcity, &cur);
-    copy_if_better_choice(&cur, choice);
-
-    adv_init_choice(&cur);
+    building_advisor_choose(pcity, cur);
+    choice = adv_better_choice_free(choice, cur);
+
+    cur = adv_new_choice();
     /* Consider building caravan-type units for trade route */
-    dai_choose_trade_route(ait, pcity, &cur, adv);
-    copy_if_better_choice(&cur, choice);
+    dai_choose_trade_route(ait, pcity, cur, adv);
+    choice = adv_better_choice_free(choice, cur);
   }
 
   if (choice->want >= 200) {
@@ -574,7 +573,7 @@
     choice->want = 199;
   }
 
-  return;
+  return choice;
 }
 
 /**************************************************************************

Modified: trunk/ai/default/advdomestic.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/ai/default/advdomestic.h?rev=31224&r1=31223&r2=31224&view=diff
==============================================================================
--- trunk/ai/default/advdomestic.h      (original)
+++ trunk/ai/default/advdomestic.h      Sun Dec 27 02:16:15 2015
@@ -13,11 +13,11 @@
 #ifndef FC__ADVDOMESTIC_H
 #define FC__ADVDOMESTIC_H
 
+/* common */
 #include "fc_types.h"
 
-void domestic_advisor_choose_build(struct ai_type *ait, struct player *pplayer,
-                                   struct city *pcity,
-                                   struct adv_choice *choice);
+struct adv_choice *domestic_advisor_choose_build(struct ai_type *ait, struct 
player *pplayer,
+                                                 struct city *pcity);
 
 void dai_wonder_city_distance(struct ai_type *ait, struct player *pplayer, 
                               struct adv_data *adv);

Modified: trunk/ai/default/advmilitary.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/ai/default/advmilitary.c?rev=31224&r1=31223&r2=31224&view=diff
==============================================================================
--- trunk/ai/default/advmilitary.c      (original)
+++ trunk/ai/default/advmilitary.c      Sun Dec 27 02:16:15 2015
@@ -1134,9 +1134,9 @@
 If the target is overseas, the function might suggest building a ferry
 to carry a land attack unit, instead of the land attack unit itself.
 **************************************************************************/
-static void kill_something_with(struct ai_type *ait, struct player *pplayer,
-                                struct city *pcity, struct unit *myunit,
-                                struct adv_choice *choice)
+static struct adv_choice *kill_something_with(struct ai_type *ait, struct 
player *pplayer,
+                                              struct city *pcity, struct unit 
*myunit,
+                                              struct adv_choice *choice)
 {
   /* Our attack rating (with reinforcements) */
   int attack;
@@ -1157,25 +1157,24 @@
   struct unit_type *boattype;
   struct pf_map *ferry_map = NULL;
   int move_time;
-  struct adv_choice best_choice;
+  struct adv_choice *best_choice;
   struct ai_city *city_data = def_ai_city_data(pcity, ait);
   struct ai_city *acity_data;
 
-  adv_init_choice(&best_choice);
-  best_choice.value.utype = unit_type_get(myunit);
-  best_choice.type = CT_ATTACKER;
-  best_choice.want = choice->want;
-
-  fc_assert_ret(is_military_unit(myunit) && 
!utype_fuel(unit_type_get(myunit)));
+  best_choice = adv_new_choice();
+  best_choice->value.utype = unit_type_get(myunit);
+  best_choice->type = CT_ATTACKER;
+
+  fc_assert_ret_val(is_military_unit(myunit) && 
!utype_fuel(unit_type_get(myunit)), choice);
 
   if (city_data->danger != 0 && assess_defense(ait, pcity) == 0) {
     /* Defence comes first! */
     goto cleanup;
   }
 
-  best_choice.want = find_something_to_kill(ait, pplayer, myunit, &ptile, NULL,
-                                            &ferry_map, &ferryboat,
-                                            &boattype, &move_time);
+  best_choice->want = find_something_to_kill(ait, pplayer, myunit, &ptile, 
NULL,
+                                             &ferry_map, &ferryboat,
+                                             &boattype, &move_time);
   if (NULL == ptile
       || ptile == unit_tile(myunit)
       || !can_unit_attack_tile(myunit, ptile)) {
@@ -1262,29 +1261,30 @@
   if (NULL == ferry_map) {
     process_attacker_want(ait, pcity, benefit, def_type, def_owner,
                           def_vet, ptile,
-                          &best_choice, NULL, NULL, NULL);
+                          best_choice, NULL, NULL, NULL);
   } else { 
     /* Attract a boat to our city or retain the one that's already here */
-    fc_assert_ret(unit_class_get(myunit)->adv.sea_move != MOVE_FULL);
-    best_choice.need_boat = TRUE;
+    fc_assert_ret_val(unit_class_get(myunit)->adv.sea_move != MOVE_FULL, 
choice);
+    best_choice->need_boat = TRUE;
     process_attacker_want(ait, pcity, benefit, def_type, def_owner,
                           def_vet, ptile,
-                          &best_choice, ferry_map, ferryboat, boattype);
-  }
-
-  if (best_choice.want > choice->want) {
+                          best_choice, ferry_map, ferryboat, boattype);
+  }
+
+  if (best_choice->want > choice->want) {
     /* We want attacker more than what we have selected before */
-    copy_if_better_choice(&best_choice, choice);
+    adv_free_choice(choice);
+    choice = best_choice;
     CITY_LOG(LOG_DEBUG, pcity, "kill_something_with()"
             " %s has chosen attacker, %s, want=" ADV_WANT_PRINTF,
             city_name(pcity),
-            utype_rule_name(best_choice.value.utype),
-            best_choice.want);
+            utype_rule_name(best_choice->value.utype),
+            best_choice->want);
 
     if (NULL != ferry_map && !ferryboat) { /* need a new ferry */
       /* We might need a new boat even if there are boats free,
        * if they are blockaded or in inland seas*/
-      fc_assert_ret(unit_class_get(myunit)->adv.sea_move != MOVE_FULL);
+      fc_assert_ret_val(unit_class_get(myunit)->adv.sea_move != MOVE_FULL, 
choice);
       dai_choose_role_unit(ait, pplayer, pcity, choice, CT_ATTACKER,
                            L_FERRYBOAT, choice->want, TRUE);
       if (dai_is_ferry_type(choice->value.utype, ait)) {
@@ -1306,6 +1306,8 @@
   if (NULL != ferry_map) {
     pf_map_destroy(ferry_map);
   }
+
+  return choice;
 }
 
 /**********************************************************************
@@ -1372,10 +1374,9 @@
   It records its choice into adv_choice struct.
   If 'choice->want' is 0 this advisor doesn't want anything.
 ****************************************************************************/
-void military_advisor_choose_build(struct ai_type *ait,
-                                   struct player *pplayer,
-                                   struct city *pcity,
-                                   struct adv_choice *choice)
+struct adv_choice *military_advisor_choose_build(struct ai_type *ait,
+                                                 struct player *pplayer,
+                                                 struct city *pcity)
 {
   struct adv_data *ai = adv_data_get(pplayer, NULL);
   struct unit_type *punittype;
@@ -1385,8 +1386,7 @@
   struct ai_city *city_data = def_ai_city_data(pcity, ait);
   adv_want martial_value = 0;
   bool martial_need = FALSE;
-
-  adv_init_choice(choice);
+  struct adv_choice *choice = adv_new_choice();
 
   urgency = assess_danger(ait, pcity);
   /* Changing to quadratic to stop AI from building piles 
@@ -1524,7 +1524,7 @@
       || pcity->id == ai->wonder_city) {
     /* Things we consider below are not life-saving so we don't want to 
      * build them if our populace doesn't feel like it */
-    return;
+    return choice;
   }
 
   /* Consider making a land bodyguard */
@@ -1540,7 +1540,7 @@
     CITY_LOG(LOGLEVEL_BUILD, pcity,
              "severe danger (want " ADV_WANT_PRINTF "), force defender",
              choice->want);
-    return;
+    return choice;
   }
 
   /* Consider making an offensive diplomat */
@@ -1564,7 +1564,7 @@
   if (punittype) {
     virtualunit = unit_virtual_create(pplayer, pcity, punittype,
                                       do_make_unit_veteran(pcity, punittype));
-    kill_something_with(ait, pplayer, pcity, virtualunit, choice);
+    choice = kill_something_with(ait, pplayer, pcity, virtualunit, choice);
     unit_virtual_destroy(virtualunit);
   }
 
@@ -1574,7 +1574,7 @@
   punittype = dai_choose_attacker(ait, pcity, TC_LAND);
   if (punittype) {
     virtualunit = unit_virtual_create(pplayer, pcity, punittype, 1);
-    kill_something_with(ait, pplayer, pcity, virtualunit, choice);
+    choice = kill_something_with(ait, pplayer, pcity, virtualunit, choice);
     unit_virtual_destroy(virtualunit);
   }
 
@@ -1592,4 +1592,6 @@
              dai_choice_rule_name(choice),
              choice->want);
   }
-}
+
+  return choice;
+}

Modified: trunk/ai/default/advmilitary.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/ai/default/advmilitary.h?rev=31224&r1=31223&r2=31224&view=diff
==============================================================================
--- trunk/ai/default/advmilitary.h      (original)
+++ trunk/ai/default/advmilitary.h      Sun Dec 27 02:16:15 2015
@@ -24,9 +24,9 @@
                                              struct unit *attacker);
 void military_advisor_choose_tech(struct player *pplayer,
                                  struct adv_choice *choice);
-void  military_advisor_choose_build(struct ai_type *ait,
-                                    struct player *pplayer, struct city *pcity,
-                                   struct adv_choice *choice);
+struct adv_choice *military_advisor_choose_build(struct ai_type *ait,
+                                                 struct player *pplayer,
+                                                 struct city *pcity);
 void dai_assess_danger_player(struct ai_type *ait, struct player *pplayer);
 int assess_defense_quadratic(struct ai_type *ait, struct city *pcity);
 int assess_defense_unit(struct ai_type *ait, struct city *pcity,

Modified: trunk/ai/default/aicity.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/ai/default/aicity.c?rev=31224&r1=31223&r2=31224&view=diff
==============================================================================
--- trunk/ai/default/aicity.c   (original)
+++ trunk/ai/default/aicity.c   Sun Dec 27 02:16:15 2015
@@ -267,11 +267,9 @@
 static void dai_city_choose_build(struct ai_type *ait, struct player *pplayer,
                                   struct city *pcity)
 {
-  struct adv_choice newchoice;
+  struct adv_choice *newchoice;
   struct adv_data *adv = adv_data_get(pplayer, NULL);
   struct ai_city *city_data = def_ai_city_data(pcity, ait);
-
-  adv_init_choice(&newchoice);
 
   if (has_handicap(pplayer, H_AWAY)
       && city_built_last_turn(pcity)
@@ -288,8 +286,9 @@
          || city_data->urgency == 0)
         && !(dai_on_war_footing(ait, pplayer) && city_data->choice.want > 0
              && pcity->id != adv->wonder_city)) {
-      domestic_advisor_choose_build(ait, pplayer, pcity, &newchoice);
-      copy_if_better_choice(&newchoice, &(city_data->choice));
+      newchoice = domestic_advisor_choose_build(ait, pplayer, pcity);
+      city_data->choice = *(adv_better_choice(&(city_data->choice), 
newchoice));
+      adv_free_choice(newchoice);
     }
   }
 
@@ -383,7 +382,7 @@
   improvement_iterate(pimprove) {
     if (can_city_sell_building(pcity, pimprove)
        && !building_has_effect(pimprove, EFT_DEFEND_BONUS)) {
-/* selling walls to buy defenders is counterproductive -- Syela */
+      /* selling walls to buy defenders is counterproductive -- Syela */
       really_handle_city_sell(pplayer, pcity, pimprove);
       break;
     }
@@ -860,9 +859,13 @@
   initialize_infrastructure_cache(pplayer);
   city_list_iterate(pplayer->cities, pcity) {
     struct ai_city *city_data = def_ai_city_data(pcity, ait);
+    struct adv_choice *choice;
+
     /* Note that this function mungs the seamap, but we don't care */
     TIMING_LOG(AIT_CITY_MILITARY, TIMER_START);
-    military_advisor_choose_build(ait, pplayer, pcity, &city_data->choice);
+    choice = military_advisor_choose_build(ait, pplayer, pcity);
+    city_data->choice = *choice;
+    adv_free_choice(choice);
     TIMING_LOG(AIT_CITY_MILITARY, TIMER_STOP);
     if (dai_on_war_footing(ait, pplayer) && city_data->choice.want > 0) {
       continue; /* Go, soldiers! */

Modified: trunk/ai/default/aitools.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/ai/default/aitools.c?rev=31224&r1=31223&r2=31224&view=diff
==============================================================================
--- trunk/ai/default/aitools.c  (original)
+++ trunk/ai/default/aitools.c  Sun Dec 27 02:16:15 2015
@@ -1016,22 +1016,6 @@
 }
 
 /**************************************************************************
-  After call to this function best is better of cur and best 
-**************************************************************************/
-void copy_if_better_choice(struct adv_choice *cur, struct adv_choice *best)
-{
-  if (best->want < cur->want) {
-    /* This simple minded copy works for now.
-     *
-     * TODO: We should get rid of this function. Choice structures should
-     *       be accessed via pointers, and those pointers should be updated
-     *       instead of copying whole structures.
-     */
-    *best = *cur;
-  }
-}
-
-/**************************************************************************
   Does choice type refer to unit
 **************************************************************************/
 bool is_unit_choice_type(enum choice_type type)

Modified: trunk/ai/default/aitools.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/ai/default/aitools.h?rev=31224&r1=31223&r2=31224&view=diff
==============================================================================
--- trunk/ai/default/aitools.h  (original)
+++ trunk/ai/default/aitools.h  Sun Dec 27 02:16:15 2015
@@ -70,7 +70,6 @@
 int dai_gold_reserve(struct player *pplayer);
 
 void adjust_choice(int value, struct adv_choice *choice);
-void copy_if_better_choice(struct adv_choice *cur, struct adv_choice *best);
 
 bool is_unit_choice_type(enum choice_type type);
 

Modified: trunk/server/advisors/advchoice.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/server/advisors/advchoice.c?rev=31224&r1=31223&r2=31224&view=diff
==============================================================================
--- trunk/server/advisors/advchoice.c   (original)
+++ trunk/server/advisors/advchoice.c   Sun Dec 27 02:16:15 2015
@@ -33,3 +33,54 @@
   choice->type = CT_NONE;
   choice->need_boat = FALSE;
 }
+
+/**************************************************************************
+  Dynamically allocate a new choice.
+**************************************************************************/
+struct adv_choice *adv_new_choice(void)
+{
+  struct adv_choice *choice = fc_malloc(sizeof(*choice));
+
+  adv_init_choice(choice);
+
+  return choice;
+}
+
+/**************************************************************************
+  Free dynamically allocated choice.
+**************************************************************************/
+void adv_free_choice(struct adv_choice *choice)
+{
+  free(choice);
+}
+
+/**************************************************************************
+  Return better one of the choices given. In case of a draw, first one
+  is preferred.
+**************************************************************************/
+struct adv_choice *adv_better_choice(struct adv_choice *first,
+                                     struct adv_choice *second)
+{
+  if (second->want > first->want) {
+    return second;
+  } else {
+    return first;
+  }
+}
+
+/**************************************************************************
+  Return better one of the choices given, and free the other.
+**************************************************************************/
+struct adv_choice *adv_better_choice_free(struct adv_choice *first,
+                                          struct adv_choice *second)
+{
+  if (second->want > first->want) {
+    free(first);
+
+    return second;
+  } else {
+    free(second);
+
+    return first;
+  }
+}

Modified: trunk/server/advisors/advchoice.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/server/advisors/advchoice.h?rev=31224&r1=31223&r2=31224&view=diff
==============================================================================
--- trunk/server/advisors/advchoice.h   (original)
+++ trunk/server/advisors/advchoice.h   Sun Dec 27 02:16:15 2015
@@ -31,4 +31,12 @@
 
 void adv_init_choice(struct adv_choice *choice);
 
+struct adv_choice *adv_new_choice(void);
+void adv_free_choice(struct adv_choice *choice);
+
+struct adv_choice *adv_better_choice(struct adv_choice *first,
+                                     struct adv_choice *second);
+struct adv_choice *adv_better_choice_free(struct adv_choice *first,
+                                          struct adv_choice *second);
+
 #endif   /* FC__ADVCHOICE_H */


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to