Author: cazfi Date: Sun Jan 10 17:57:22 2016 New Revision: 31433 URL: http://svn.gna.org/viewcvs/freeciv?rev=31433&view=rev Log: Added support for tracking the reason a 'choice' has been made. This does not affect default builds, but must be enabled by preprocessor macro.
See patch #6808 Modified: trunk/ai/default/advdomestic.c trunk/ai/default/advmilitary.c trunk/ai/default/aiair.c trunk/ai/default/aicity.c trunk/ai/default/aidiplomat.c trunk/ai/default/aihunt.c trunk/ai/default/aiparatrooper.c 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=31433&r1=31432&r2=31433&view=diff ============================================================================== --- trunk/ai/default/advdomestic.c (original) +++ trunk/ai/default/advdomestic.c Sun Jan 10 17:57:22 2016 @@ -475,6 +475,7 @@ settler_want); dai_choose_role_unit(ait, pplayer, pcity, choice, CT_CIVILIAN, UTYF_SETTLERS, settler_want, FALSE); + adv_choice_set_use(choice, "worker"); } /* Terrain improvers don't use boats (yet) */ @@ -517,6 +518,7 @@ action_get_role(ACTION_FOUND_CITY), founder_want, city_data->founder_boat); + adv_choice_set_use(choice, "founder"); } else if (founder_want < -choice->want) { /* We need boats to colonize! */ @@ -538,6 +540,7 @@ * If no ferryboat is found, above founder choice stays. */ dai_choose_role_unit(ait, pplayer, pcity, choice, CT_CIVILIAN, L_FERRYBOAT, -founder_want, TRUE); + adv_choice_set_use(choice, "founder's boat"); } } else if (!founder_type && (founder_want > choice->want || founder_want < -choice->want)) { @@ -551,18 +554,21 @@ { struct adv_choice *cur; + /* Consider building caravan-type units to aid wonder construction */ cur = adv_new_choice(); - /* Consider building caravan-type units to aid wonder construction */ + adv_choice_set_use(cur, "wonder"); dai_choose_help_wonder(ait, pcity, cur, adv); choice = adv_better_choice_free(choice, cur); + /* Consider city improvements */ cur = adv_new_choice(); - /* Consider city improvements */ + adv_choice_set_use(cur, "improvement"); building_advisor_choose(pcity, cur); choice = adv_better_choice_free(choice, cur); + /* Consider building caravan-type units for trade route */ cur = adv_new_choice(); - /* Consider building caravan-type units for trade route */ + adv_choice_set_use(cur, "trade route"); dai_choose_trade_route(ait, pcity, cur, adv); choice = adv_better_choice_free(choice, cur); } Modified: trunk/ai/default/advmilitary.c URL: http://svn.gna.org/viewcvs/freeciv/trunk/ai/default/advmilitary.c?rev=31433&r1=31432&r2=31433&view=diff ============================================================================== --- trunk/ai/default/advmilitary.c (original) +++ trunk/ai/default/advmilitary.c Sun Jan 10 17:57:22 2016 @@ -1164,6 +1164,7 @@ best_choice = adv_new_choice(); best_choice->value.utype = unit_type_get(myunit); best_choice->type = CT_ATTACKER; + adv_choice_set_use(best_choice, "attacker"); fc_assert_ret_val(is_military_unit(myunit) && !utype_fuel(unit_type_get(myunit)), choice); @@ -1298,6 +1299,8 @@ choice->want, ai->stats.available_boats, ai->stats.boats); #endif /* DEBUG */ + + adv_choice_set_use(choice, "attacker ferry"); } /* else can not build ferries yet */ } } @@ -1341,6 +1344,7 @@ choice->want = want; choice->value.utype = punittype; choice->type = CT_DEFENDER; + adv_choice_set_use(choice, "bodyguard"); } } unit_virtual_destroy(virtualunit); @@ -1372,6 +1376,7 @@ && !city_has_building(pcity, improvement_by_number(id))) { choice->value.building = improvement_by_number(id); choice->type = CT_BUILDING; + adv_choice_set_use(choice, "veterancy building"); } } @@ -1459,6 +1464,7 @@ * nobody behind them. */ if (dai_process_defender_want(ait, pplayer, pcity, danger, choice)) { choice->want = 100 + danger; + adv_choice_set_use(choice, "first defender"); build_walls = FALSE; CITY_LOG(LOG_DEBUG, pcity, "m_a_c_d wants first defender with " ADV_WANT_PRINTF, @@ -1491,6 +1497,7 @@ choice->want = 100; } choice->type = CT_BUILDING; + adv_choice_set_use(choice, "defense building"); CITY_LOG(LOG_DEBUG, pcity, "m_a_c_d wants defense building with " ADV_WANT_PRINTF, choice->want); @@ -1512,6 +1519,7 @@ } choice->want += martial_value; + adv_choice_set_use(choice, "defender"); CITY_LOG(LOG_DEBUG, pcity, "m_a_c_d wants %s with desire " ADV_WANT_PRINTF, utype_rule_name(choice->value.utype), Modified: trunk/ai/default/aiair.c URL: http://svn.gna.org/viewcvs/freeciv/trunk/ai/default/aiair.c?rev=31433&r1=31432&r2=31433&view=diff ============================================================================== --- trunk/ai/default/aiair.c (original) +++ trunk/ai/default/aiair.c Sun Jan 10 17:57:22 2016 @@ -496,6 +496,7 @@ choice->value.utype = punittype; choice->type = CT_ATTACKER; choice->need_boat = FALSE; + adv_choice_set_use(choice, "offensive air"); want_something = TRUE; log_debug("%s wants to build %s (want=%d)", city_name(pcity), utype_rule_name(punittype), profit); Modified: trunk/ai/default/aicity.c URL: http://svn.gna.org/viewcvs/freeciv/trunk/ai/default/aicity.c?rev=31433&r1=31432&r2=31433&view=diff ============================================================================== --- trunk/ai/default/aicity.c (original) +++ trunk/ai/default/aicity.c Sun Jan 10 17:57:22 2016 @@ -251,6 +251,7 @@ /* FIXME: 101 is the "overriding military emergency" indicator */ choice->want = 101; choice->type = CT_ATTACKER; + adv_choice_set_use(choice, "barbarian"); } else { log_base(LOG_WANT, "Barbarians don't know what to build!"); } @@ -287,7 +288,7 @@ && !(dai_on_war_footing(ait, pplayer) && city_data->choice.want > 0 && pcity->id != adv->wonder_city)) { newchoice = domestic_advisor_choose_build(ait, pplayer, pcity); - city_data->choice = *(adv_better_choice(&(city_data->choice), newchoice)); + adv_choice_copy(&(city_data->choice), adv_better_choice(&(city_data->choice), newchoice)); adv_free_choice(newchoice); } } @@ -302,16 +303,19 @@ city_data->choice.value.utype = best_role_unit(pcity, action_get_role(ACTION_TRADE_ROUTE)); city_data->choice.type = CT_CIVILIAN; + adv_choice_set_use(&(city_data->choice), "fallback trade route"); } else { unsigned int our_def = assess_defense_quadratic(ait, pcity); if (our_def == 0 && dai_process_defender_want(ait, pplayer, pcity, 1, &(city_data->choice))) { + adv_choice_set_use(&(city_data->choice), "fallback defender"); CITY_LOG(LOG_DEBUG, pcity, "Building fallback defender"); } else if (best_role_unit(pcity, UTYF_SETTLERS)) { city_data->choice.value.utype = dai_role_utype_for_terrain_class(pcity, UTYF_SETTLERS, TC_LAND); city_data->choice.type = CT_CIVILIAN; + adv_choice_set_use(&(city_data->choice), "fallback worker"); } else { CITY_LOG(LOG_ERROR, pcity, "Cannot even build a fallback " "(caravan/coinage/settlers). Fix the ruleset!"); @@ -864,7 +868,7 @@ /* Note that this function mungs the seamap, but we don't care */ TIMING_LOG(AIT_CITY_MILITARY, TIMER_START); choice = military_advisor_choose_build(ait, pplayer, pcity); - city_data->choice = *choice; + adv_choice_copy(&(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) { @@ -1061,6 +1065,7 @@ struct ai_city *city_data = def_ai_city_data(pcity, ait); if (city_data != NULL) { + adv_deinit_choice(&(city_data->choice)); city_set_ai_data(pcity, ait, NULL); FC_FREE(city_data); } Modified: trunk/ai/default/aidiplomat.c URL: http://svn.gna.org/viewcvs/freeciv/trunk/ai/default/aidiplomat.c?rev=31433&r1=31432&r2=31433&view=diff ============================================================================== --- trunk/ai/default/aidiplomat.c (original) +++ trunk/ai/default/aidiplomat.c Sun Jan 10 17:57:22 2016 @@ -150,6 +150,7 @@ choice->type = CT_DEFENDER; choice->value.utype = ut; choice->need_boat = FALSE; + adv_choice_set_use(choice, "defensive diplomat"); } else if (num_role_units(UTYF_DIPLOMAT) > 0) { /* We don't know diplomats yet... */ log_base(LOG_DIPLOMAT_BUILD, @@ -305,6 +306,7 @@ choice->type = CT_CIVILIAN; /* so we don't build barracks for it */ choice->value.utype = ut; choice->need_boat = FALSE; + adv_choice_set_use(choice, "offensive diplomat"); BV_SET(ai->stats.diplomat_reservations, acity->id); } } Modified: trunk/ai/default/aihunt.c URL: http://svn.gna.org/viewcvs/freeciv/trunk/ai/default/aihunt.c?rev=31433&r1=31432&r2=31433&view=diff ============================================================================== --- trunk/ai/default/aihunt.c (original) +++ trunk/ai/default/aihunt.c Sun Jan 10 17:57:22 2016 @@ -213,6 +213,7 @@ choice->want = best; choice->type = CT_ATTACKER; choice->need_boat = FALSE; + adv_choice_set_use(choice, "missile"); } else if (best >= 0) { CITY_LOG(LOGLEVEL_HUNT, pcity, "not pri missile w/ want " ADV_WANT_PRINTF "(old want " ADV_WANT_PRINTF ")", best, choice->want); @@ -240,6 +241,7 @@ choice->want = want; choice->type = CT_ATTACKER; choice->need_boat = FALSE; + adv_choice_set_use(choice, "hunter"); } } Modified: trunk/ai/default/aiparatrooper.c URL: http://svn.gna.org/viewcvs/freeciv/trunk/ai/default/aiparatrooper.c?rev=31433&r1=31432&r2=31433&view=diff ============================================================================== --- trunk/ai/default/aiparatrooper.c (original) +++ trunk/ai/default/aiparatrooper.c Sun Jan 10 17:57:22 2016 @@ -389,13 +389,14 @@ profit = calculate_want_for_paratrooper(virtual_unit, pcity->tile); unit_virtual_destroy(virtual_unit); - /* update choise struct if it's worth */ + /* update choice struct if it's worth */ if (profit > choice->want) { /* Update choice */ choice->want = profit; choice->value.utype = u_type; choice->type = CT_ATTACKER; choice->need_boat = FALSE; + adv_choice_set_use(choice, "paratrooper"); log_base(LOGLEVEL_PARATROOPER, "%s wants to build %s (want=%d)", city_name(pcity), utype_rule_name(u_type), profit); } Modified: trunk/server/advisors/advchoice.c URL: http://svn.gna.org/viewcvs/freeciv/trunk/server/advisors/advchoice.c?rev=31433&r1=31432&r2=31433&view=diff ============================================================================== --- trunk/server/advisors/advchoice.c (original) +++ trunk/server/advisors/advchoice.c Sun Jan 10 17:57:22 2016 @@ -19,7 +19,9 @@ #include "support.h" /* common */ +#include "improvement.h" #include "requirements.h" +#include "unittype.h" #include "advchoice.h" @@ -32,6 +34,22 @@ choice->want = 0; choice->type = CT_NONE; choice->need_boat = FALSE; +#ifdef ADV_CHOICE_TRACK + choice->use = NULL; +#endif /* ADV_CHOICE_TRACK */ +} + +/************************************************************************** + Clear choice without freeing it itself +**************************************************************************/ +void adv_deinit_choice(struct adv_choice *choice) +{ +#ifdef ADV_CHOICE_TRACK + if (choice->use != NULL) { + free(choice->use); + choice->use = NULL; + } +#endif /* ADV_CHOICE_TRACK */ } /************************************************************************** @@ -51,6 +69,11 @@ **************************************************************************/ void adv_free_choice(struct adv_choice *choice) { +#ifdef ADV_CHOICE_TRACK + if (choice->use != NULL) { + free(choice->use); + } +#endif /* ADV_CHOICE_TRACK */ free(choice); } @@ -75,12 +98,82 @@ struct adv_choice *second) { if (second->want > first->want) { - free(first); + adv_free_choice(first); return second; } else { - free(second); + adv_free_choice(second); return first; } } + + +#ifdef ADV_CHOICE_TRACK + +/************************************************************************** + Copy contents of one choice structure to the other +**************************************************************************/ +void adv_choice_copy(struct adv_choice *dest, struct adv_choice *src) +{ + if (dest != src) { + dest->type = src->type; + dest->value = src->value; + dest->want = src->want; + dest->need_boat = src->need_boat; + if (dest->use != NULL) { + free(dest->use); + } + if (src->use != NULL) { + dest->use = fc_strdup(src->use); + } else { + dest->use = NULL; + } + } +} + +/************************************************************************** + Set the use the choice is meant for. +**************************************************************************/ +void adv_choice_set_use(struct adv_choice *choice, const char *use) +{ + if (choice->use != NULL) { + free(choice->use); + } + choice->use = fc_strdup(use); +} + +/************************************************************************** + Log the choice information. +**************************************************************************/ +void adv_choice_log_info(struct adv_choice *choice, const char *loc1, const char *loc2) +{ + const char *use; + const char *name; + + if (choice->use != NULL) { + use = choice->use; + } else { + use = "<unknown>"; + } + + if (choice->type == CT_BUILDING) { + name = improvement_rule_name(choice->value.building); + } else if (choice->type == CT_NONE) { + name = "None"; + } else { + name = utype_rule_name(choice->value.utype); + } + + if (loc2 != NULL) { + log_base(ADV_CHOICE_LOG_LEVEL, "Choice at \"%s:%s\": %s, " + "want " ADV_WANT_PRINTF " as %s (%d)", + loc1, loc2, name, choice->want, use, choice->type); + } else { + log_base(ADV_CHOICE_LOG_LEVEL, "Choice at \"%s\": %s, " + "want " ADV_WANT_PRINTF " as %s (%d)", + loc1, name, choice->want, use, choice->type); + } +} + +#endif /* ADV_CHOICE_TRACK */ Modified: trunk/server/advisors/advchoice.h URL: http://svn.gna.org/viewcvs/freeciv/trunk/server/advisors/advchoice.h?rev=31433&r1=31432&r2=31433&view=diff ============================================================================== --- trunk/server/advisors/advchoice.h (original) +++ trunk/server/advisors/advchoice.h Sun Jan 10 17:57:22 2016 @@ -13,6 +13,13 @@ #ifndef FC__ADVCHOICE_H #define FC__ADVCHOICE_H +/* Uncomment to have choice infomation tracked */ +/* #define ADV_CHOICE_TRACK */ + +#ifdef ADV_CHOICE_TRACK +#define ADV_CHOICE_LOG_LEVEL LOG_NORMAL +#endif + enum choice_type { CT_NONE = 0, CT_BUILDING = 1, @@ -27,9 +34,13 @@ universals_u value; /* what the advisor wants */ adv_want want; /* how much it wants it */ bool need_boat; /* unit being built wants a boat */ +#ifdef ADV_CHOICE_TRACK + char *use; +#endif /* ADV_CHOICE_TRACK */ }; void adv_init_choice(struct adv_choice *choice); +void adv_deinit_choice(struct adv_choice *choice); struct adv_choice *adv_new_choice(void); void adv_free_choice(struct adv_choice *choice); @@ -39,4 +50,19 @@ struct adv_choice *adv_better_choice_free(struct adv_choice *first, struct adv_choice *second); +#ifdef ADV_CHOICE_TRACK +void adv_choice_copy(struct adv_choice *dest, struct adv_choice *src); +void adv_choice_set_use(struct adv_choice *choice, const char *use); +void adv_choice_log_info(struct adv_choice *choice, const char *loc1, const char *loc2); +#else /* ADV_CHOICE_TRACK */ +static inline void adv_choice_copy(struct adv_choice *dest, struct adv_choice *src) +{ + if (dest != src) { + *dest = *src; + } +} +#define adv_choice_set_use(_choice, _use) +#define adv_choice_log_info(_choice, _loc1, _loc2) +#endif /* ADV_CHOICE_TRACK */ + #endif /* FC__ADVCHOICE_H */ _______________________________________________ Freeciv-commits mailing list Freeciv-commits@gna.org https://mail.gna.org/listinfo/freeciv-commits