Project

General

Profile

Feature #2354 ยป 0087-daisettler.c-Replace-NULL-with-nullptr.patch

Marko Lindqvist, 10/10/2026 07:19 PM

View differences:

ai/default/daisettler.c
{
struct cityresult *result;
fc_assert_ret_val(ptile != NULL, NULL);
fc_assert_ret_val(ptile != nullptr, nullptr);
result = fc_calloc(1, sizeof(*result));
result->tile = ptile;
......
result->virt_boat = FALSE;
/* City centre */
result->city_center.tdc = NULL;
result->city_center.tdc = nullptr;
/* First worked tile */
result->best_other.tile = NULL;
result->best_other.tdc = NULL;
result->best_other.tile = nullptr;
result->best_other.tdc = nullptr;
result->best_other.cindex = 0;
result->remaining = 0;
......
*****************************************************************************/
static void cityresult_destroy(struct cityresult *result)
{
if (result != NULL) {
if (result->tdc_hash != NULL) {
if (result != nullptr) {
if (result->tdc_hash != nullptr) {
tile_data_cache_hash_destroy(result->tdc_hash);
}
free(result);
......
{
struct city *pcity = tile_city(center);
struct government *curr_govt = government_of_player(pplayer);
struct player *saved_owner = NULL;
struct tile *saved_claimer = NULL;
struct player *saved_owner = nullptr;
struct tile *saved_claimer = nullptr;
bool virtual_city = FALSE;
bool handicap = has_handicap(pplayer, H_MAP);
struct adv_data *adv = adv_data_get(pplayer, NULL);
struct adv_data *adv = adv_data_get(pplayer, nullptr);
struct cityresult *result;
const struct civ_map *nmap = &(wld.map);
fc_assert_ret_val(adv != NULL, NULL);
fc_assert_ret_val(center != NULL, NULL);
fc_assert_ret_val(adv != nullptr, nullptr);
fc_assert_ret_val(center != nullptr, nullptr);
pplayer->government = adv->goal.govt.gov;
/* Create a city result and set default values. */
result = cityresult_new(center);
if (pcity == NULL) {
if (pcity == nullptr) {
pcity = create_city_virtual(pplayer, result->tile, "Virtuaville");
saved_owner = tile_owner(result->tile);
saved_claimer = tile_claimer(result->tile);
......
if (reserved < 0
|| (handicap && !map_is_known(ptile, pplayer))
|| tile_worked(ptile) != NULL) {
|| tile_worked(ptile) != nullptr) {
/* Tile is reserved or we can't see it */
ptdc = tile_data_cache_new();
ptdc->shield = 0;
......
} city_tile_iterate_index_end;
/* We need a city center. */
if (result->city_center.tdc == NULL) {
fc_assert(result->city_center.tdc != NULL);
if (result->city_center.tdc == nullptr) {
fc_assert(result->city_center.tdc != nullptr);
return NULL;
return nullptr;
}
if (virtual_city) {
/* Baseline is a size one city (city center + best extra tile). */
result->total = result->city_center.tdc->sum
+ (result->best_other.tdc != NULL
+ (result->best_other.tdc != nullptr
? result->best_other.tdc->sum : 0);
} else if (result->best_other.tdc != NULL) {
} else if (result->best_other.tdc != nullptr) {
/* Baseline is best extra tile only. This is why making new cities
* is so darn good. */
result->total = result->best_other.tdc->sum;
......
int shield = result->city_center.tdc->shield
+ result->best_other.tdc->shield;
result->waste = adv->shield_priority
* city_waste(pcity, O_SHIELD, shield, NULL);
* city_waste(pcity, O_SHIELD, shield, nullptr);
if (game.info.fulltradesize == 1) {
int trade = result->city_center.tdc->trade
+ result->best_other.tdc->trade;
result->corruption = adv->science_priority
* city_waste(pcity, O_TRADE, trade, NULL);
* city_waste(pcity, O_TRADE, trade, nullptr);
} else {
result->corruption = 0;
}
......
* is possible (with notradesize) that we _gain_ value here. */
city_size_add(pcity, 1);
result->corruption = adv->science_priority
* (city_waste(pcity, O_TRADE, result->best_other.tdc->trade, NULL)
* (city_waste(pcity, O_TRADE, result->best_other.tdc->trade, nullptr)
- pcity->waste[O_TRADE]);
result->waste = adv->shield_priority
* (city_waste(pcity, O_SHIELD, result->best_other.tdc->shield, NULL)
* (city_waste(pcity, O_SHIELD, result->best_other.tdc->shield, nullptr)
- pcity->waste[O_SHIELD]);
city_size_add(pcity, -1);
}
......
tile_set_owner(result->tile, saved_owner, saved_claimer);
}
fc_assert_ret_val(result->city_center.tdc->sum >= 0, NULL);
fc_assert_ret_val(result->remaining >= 0, NULL);
fc_assert_ret_val(result->city_center.tdc->sum >= 0, nullptr);
fc_assert_ret_val(result->remaining >= 0, nullptr);
return result;
}
......
{
struct tile_data_cache *ptdc_copy = tile_data_cache_new();
fc_assert_ret_val(ptdc, NULL);
fc_assert_ret_val(ptdc, nullptr);
ptdc_copy->shield = ptdc->shield;
ptdc_copy->trade = ptdc->trade;
......
*****************************************************************************/
static void tile_data_cache_destroy(struct tile_data_cache *ptdc)
{
if (ptdc != NULL) {
if (ptdc != nullptr) {
free(ptdc);
}
}
......
struct player *plr,
int tindex)
{
struct ai_plr *ai = dai_plr_data_get(ait, plr, NULL);
fc_assert_ret_val(ai != NULL, NULL);
fc_assert_ret_val(ai->settler != NULL, NULL);
fc_assert_ret_val(ai->settler->tdc_hash != NULL, NULL);
struct ai_plr *ai = dai_plr_data_get(ait, plr, nullptr);
struct tile_data_cache *ptdc;
fc_assert_ret_val(ai != nullptr, nullptr);
fc_assert_ret_val(ai->settler != nullptr, nullptr);
fc_assert_ret_val(ai->settler->tdc_hash != nullptr, nullptr);
tile_data_cache_hash_lookup(ai->settler->tdc_hash, tindex, &ptdc);
if (!ptdc) {
#ifdef FREECIV_DEBUG
ai->settler->cache.miss++;
#endif /* FREECIV_DEBUG */
return NULL;
return nullptr;
} else if (ptdc->turn != game.info.turn) {
#ifdef FREECIV_DEBUG
ai->settler->cache.old++;
#endif /* FREECIV_DEBUG */
return NULL;
return nullptr;
} else {
#ifdef FREECIV_DEBUG
ai->settler->cache.hit++;
......
static void tdc_plr_set(struct ai_type *ait, struct player *plr, int tindex,
const struct tile_data_cache *ptdc)
{
struct ai_plr *ai = dai_plr_data_get(ait, plr, NULL);
struct ai_plr *ai = dai_plr_data_get(ait, plr, nullptr);
fc_assert_ret(ai != NULL);
fc_assert_ret(ai->settler != NULL);
fc_assert_ret(ai->settler->tdc_hash != NULL);
fc_assert_ret(ptdc != NULL);
fc_assert_ret(ai != nullptr);
fc_assert_ret(ai->settler != nullptr);
fc_assert_ret(ai->settler->tdc_hash != nullptr);
fc_assert_ret(ptdc != nullptr);
#ifdef FREECIV_DEBUG
ai->settler->cache.save++;
......
struct city *vcity = create_city_virtual(pplayer, vtile, "");
tile_set_worked(vtile, vcity); /* Link tile_city(vtile) to vcity. */
upgrade_city_extras(vcity, NULL); /* Give city free extras. */
upgrade_city_extras(vcity, nullptr); /* Give city free extras. */
extra_type_iterate(pextra) {
if (tile_has_extra(vtile, pextra)) {
/* TODO: Do not use full bonus of those road types
......
int tiles = city_map_tiles(cr->city_radius_sq);
struct tile_data_cache *ptdc;
fc_assert_ret(cr->tdc_hash != NULL);
fc_assert_ret(cr->tdc_hash != nullptr);
fc_assert_ret(tiles > 0);
city_map_reserved = fc_calloc(tiles, sizeof(*city_map_reserved));
......
/*************************************************************************//**
Calculates the desire for founding a new city at 'ptile'. The citymap
ensures that we do not build cities too close to each other. Returns NULL
if no place was found.
ensures that we do not build cities too close to each other.
Returns nullptr if no place was found.
*****************************************************************************/
struct cityresult *city_desirability(struct ai_type *ait, struct player *pplayer,
struct unit *punit, struct tile *ptile)
{
struct city *pcity = tile_city(ptile);
struct cityresult *cr = NULL;
struct cityresult *cr = nullptr;
const struct civ_map *nmap = &(wld.map);
fc_assert_ret_val(punit, NULL);
fc_assert_ret_val(pplayer, NULL);
fc_assert_ret_val(punit, nullptr);
fc_assert_ret_val(pplayer, nullptr);
if (!city_can_be_built_here(nmap, ptile, punit, FALSE)
|| (has_handicap(pplayer, H_MAP)
&& !map_is_known(ptile, pplayer))) {
return NULL;
return nullptr;
}
/* Check if another settler has taken a spot within mindist */
square_iterate(&(wld.map), ptile, game.info.citymindist-1, tile1) {
if (citymap_is_reserved(tile1)) {
return NULL;
return nullptr;
}
} square_iterate_end;
if (adv_danger_at(punit, ptile)) {
return NULL;
return nullptr;
}
if (pcity && (city_size_get(pcity) + unit_pop_value(punit)
> game.info.add_to_size_limit)) {
/* Can't exceed population limit. */
return NULL;
return nullptr;
}
if (!pcity && citymap_is_reserved(ptile)) {
return NULL; /* reserved, go away */
return nullptr; /* Reserved, go away */
}
/* If (x, y) is an existing city, consider immigration */
if (pcity && city_owner(pcity) == pplayer) {
return NULL;
return nullptr;
}
cr = cityresult_fill(ait, pplayer, ptile); /* Burn CPU, burn! */
if (!cr) {
/* Failed to find a good spot */
return NULL;
return nullptr;
}
/*** Alright: Now consider building a new city ***/
if (food_starvation(cr) || shield_starvation(cr)) {
cityresult_destroy(cr);
return NULL;
return nullptr;
}
cr->total += result_defense_bonus(pplayer, cr);
......
/* Add remaining points, which is our potential */
cr->total += cr->remaining;
fc_assert_ret_val(cr->total >= 0, NULL); /* Does not free cr! */
fc_assert_ret_val(cr->total >= 0, nullptr); /* Does not free cr! */
return cr;
}
......
The result is returned in "best".
Return value is a 'struct cityresult' if found something better than what
was originally in "best" was found; else NULL.
was originally in "best" was found; else nullptr.
TODO: Transparently check if we should add ourselves to an existing city.
*****************************************************************************/
......
struct unit *punit,
int boat_cost)
{
struct cityresult *cr = NULL, *best = NULL;
struct cityresult *cr = nullptr, *best = nullptr;
int best_turn = 0; /* Which turn we found the best fit */
struct player *pplayer = unit_owner(punit);
struct pf_map *pfm;
......
if (BORDERS_DISABLED != game.info.borders) {
struct player *powner = tile_owner(ptile);
if (NULL != powner
if (powner != nullptr
&& powner != pplayer
&& pplayers_in_peace(powner, pplayer)) {
/* Land theft does not make for good neighbors. */
......
cr = city_desirability(ait, pplayer, punit, ptile);
/* Check if actually found something */
if (cr == NULL) {
if (cr == nullptr) {
continue;
}
......
cityresult_destroy(best);
/* Save the new 'best' value. */
best = cr;
cr = NULL;
cr = nullptr;
best_turn = turns;
log_debug("settler map search (search): (%d, %d) " ADV_WANT_PRINTF,
......
} else {
/* Destroy the unused result. */
cityresult_destroy(cr);
cr = NULL;
cr = nullptr;
}
/* Can we terminate early? We have a 'good enough' spot, and
......
If (!look_for_boat && !use_virt_boat), will not consider placements
overseas.
Returns the better cityresult or NULL if no result was found.
Returns the better cityresult or nullptr if no result was found.
*****************************************************************************/
static struct cityresult *find_best_city_placement(struct ai_type *ait,
struct unit *punit,
......
{
struct pf_parameter parameter;
struct player *pplayer = unit_owner(punit);
struct unit *ferry = NULL;
struct cityresult *cr1 = NULL, *cr2 = NULL;
struct unit *ferry = nullptr;
struct cityresult *cr1 = nullptr, *cr2 = nullptr;
const struct civ_map *nmap = &(wld.map);
fc_assert_ret_val(is_ai(pplayer), NULL);
fc_assert_ret_val(is_ai(pplayer), nullptr);
/* Only virtual units may use virtual boats: */
fc_assert_ret_val(0 == punit->id || !use_virt_boat, NULL);
fc_assert_ret_val(0 == punit->id || !use_virt_boat, nullptr);
/* Phase 1: Consider building cities on our continent */
......
/* Phase 2: Consider travelling to another continent */
if (look_for_boat) {
int ferry_id = aiferry_find_boat(ait, punit, 1, NULL);
int ferry_id = aiferry_find_boat(ait, punit, 1, nullptr);
ferry = game_unit_by_number(ferry_id);
}
......
struct unit_type *boattype
= best_role_unit_for_player(pplayer, L_FERRYBOAT);
if (boattype == NULL) {
if (boattype == nullptr) {
/* Sea travel not possible yet. Bump tech want for ferries. */
boattype = get_role_unit(L_FERRYBOAT, 0);
if (NULL != boattype) {
if (boattype != nullptr) {
struct ai_plr *plr_data = def_ai_player_data(pplayer, ait);
unit_tech_reqs_iterate(boattype, padv) {
......
/* Return the result from the search on our current continent */
return cr1;
}
ferry = unit_virtual_create(pplayer, NULL, boattype, 0);
ferry = unit_virtual_create(pplayer, nullptr, boattype, 0);
unit_tile_set(ferry, unit_tile(punit));
}
......
/* If we use a virtual boat, we must have permission and be emigrating: */
/* FIXME: These assert do not free cr2! */
fc_assert_ret_val(!cr2 || (!cr2->virt_boat || use_virt_boat), NULL);
fc_assert_ret_val(!cr2 || (!cr2->virt_boat || cr2->overseas), NULL);
fc_assert_ret_val(!cr2 || (!cr2->virt_boat || use_virt_boat), nullptr);
fc_assert_ret_val(!cr2 || (!cr2->virt_boat || cr2->overseas), nullptr);
}
if (!cr1) {
......
*****************************************************************************/
void dai_auto_settler_init(struct ai_plr *ai)
{
fc_assert_ret(ai != NULL);
fc_assert_ret(ai->settler == NULL);
fc_assert_ret(ai != nullptr);
fc_assert_ret(ai->settler == nullptr);
ai->settler = fc_calloc(1, sizeof(*ai->settler));
ai->settler->tdc_hash = tile_data_cache_hash_new();
......
adv_want best_impr = 0; /* Value of best terrain improvement we can do */
enum unit_activity best_act;
struct extra_type *best_target;
struct tile *best_tile = NULL;
struct pf_path *path = NULL;
struct city *pcity = NULL;
struct tile *best_tile = nullptr;
struct pf_path *path = nullptr;
struct city *pcity = nullptr;
/* Time it will take worker to complete its given task */
int completion_time = 0;
......
/* Check that the mission is still possible. If the tile has become
* unavailable, call it off. */
if (!city_can_be_built_here(nmap, ptile, punit, FALSE)) {
dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL);
dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
set_unit_activity(punit, ACTIVITY_IDLE, ACTION_NONE);
send_unit_info(NULL, punit);
send_unit_info(nullptr, punit);
return; /* Avoid recursion at all cost */
} else {
/* Go there */
if ((!dai_gothere(ait, pplayer, punit, ptile)
&& NULL == game_unit_by_number(sanity))
&& game_unit_by_number(sanity) == nullptr)
|| punit->moves_left <= 0) {
return;
}
......
if (elevel != CBE_OK) {
UNIT_LOG(LOG_DEBUG, punit, "could not make city on %s",
tile_get_info_text(unit_tile(punit), TRUE, 0));
dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL);
dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
if (elevel == CBE_RECOVERABLE) {
/* Only known way to end in here is that hut turned in to a city
......
}
} else {
UNIT_LOG(LOG_DEBUG, punit, "could not go to target");
/* dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL); */
/* dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr); */
return;
}
}
......
/* Have nearby cities requests? */
pcity = worker_evaluate_city_requests(punit, &best_task, &path, state);
if (pcity != NULL) {
if (path != NULL) {
if (pcity != nullptr) {
if (path != nullptr) {
completion_time = pf_path_last_position(path)->turn;
best_impr = 1;
best_act = best_task->act;
best_target = best_task->tgt;
best_tile = best_task->ptile;
} else {
pcity = NULL;
pcity = nullptr;
}
}
if (pcity == NULL) {
if (pcity == nullptr) {
best_impr = worker_evaluate_improvements(nmap, punit,
&best_act, &best_target,
&best_tile, &path, state);
if (path != NULL) {
if (path != nullptr) {
completion_time = pf_path_last_position(path)->turn;
}
}
......
cityresult_destroy(result);
/*** Go back to and found a city ***/
if (path != NULL) {
if (path != nullptr) {
pf_path_destroy(path);
}
path = NULL;
path = nullptr;
goto BUILD_CITY;
} else if (best_impr > 0) {
UNIT_LOG(LOG_DEBUG, punit, "improves terrain instead of founding");
......
adv_unit_new_task(punit, AUT_AUTO_WORKER, best_tile);
} else {
UNIT_LOG(LOG_DEBUG, punit, "cannot find work");
fc_assert(result == NULL);
dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL);
fc_assert(result == nullptr);
dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
goto CLEANUP;
}
} else {
......
adv_unit_new_task(punit, AUT_AUTO_WORKER, best_tile);
}
if (best_tile != NULL
if (best_tile != nullptr
&& auto_worker_setup_work(nmap, pplayer, punit, state, 0, &path,
best_tile, best_act, &best_target,
completion_time)) {
if (pcity != NULL) {
if (pcity != nullptr) {
clear_worker_tasks(pcity);
}
}
CLEANUP:
if (NULL != path) {
if (path != nullptr) {
pf_path_destroy(path);
}
}
......
bool caller_closes;
struct ai_plr *ai = dai_plr_data_get(ait, pplayer, &caller_closes);
fc_assert_ret(ai != NULL);
fc_assert_ret(ai->settler != NULL);
fc_assert_ret(ai->settler->tdc_hash != NULL);
fc_assert_ret(ai != nullptr);
fc_assert_ret(ai->settler != nullptr);
fc_assert_ret(ai->settler->tdc_hash != nullptr);
#ifdef FREECIV_DEBUG
log_debug("[aisettler cache for %s] save: %d, miss: %d, old: %d, hit: %d",
......
*****************************************************************************/
void dai_auto_settler_free(struct ai_plr *ai)
{
fc_assert_ret(ai != NULL);
fc_assert_ret(ai != nullptr);
if (ai->settler) {
if (ai->settler->tdc_hash) {
......
}
free(ai->settler);
}
ai->settler = NULL;
ai->settler = nullptr;
}
/*************************************************************************//**
......
unit_activity_handling(punit, ACTIVITY_IDLE, ACTION_NONE);
/* Free city reservations */
dai_unit_new_task(ait, punit, AIUNIT_NONE, NULL);
dai_unit_new_task(ait, punit, AIUNIT_NONE, nullptr);
pcity = tile_city(ptile);
if (pcity) {
......
if (!pcity && punit) {
enum ane_kind reason = action_not_enabled_reason(punit,
ACTION_FOUND_CITY,
ptile, NULL, NULL);
ptile,
nullptr, nullptr);
if (reason == ANEK_CITY_TOO_CLOSE_TGT) {
/* This is acceptable. A hut in the path to the tile may have created
......
unit_type = best_role_unit(pcity, action_id_get_role(ACTION_FOUND_CITY));
if (unit_type == NULL) {
if (unit_type == nullptr) {
log_debug("No ACTION_FOUND_CITY role unit available");
return;
}
    (1-1/1)