Project

General

Profile

Feature #1526 ยป 0098-unittype.-ch-Replace-NULL-with-nullptr.patch

Marko Lindqvist, 06/15/2025 05:47 AM

View differences:

common/unittype.c
if (game.control.num_unit_types > 0) {
return unit_types;
}
return NULL;
return nullptr;
}
/**********************************************************************//**
......
if (game.control.num_unit_types > 0) {
return &unit_types[game.control.num_unit_types - 1];
}
return NULL;
return nullptr;
}
/**********************************************************************//**
......
/**********************************************************************//**
Return a pointer for the unit type struct for the given unit type id.
This function returns NULL for invalid unit pointers (some callers
This function returns nullptr for invalid unit pointers (some callers
rely on this).
**************************************************************************/
struct unit_type *utype_by_number(const Unit_type_id id)
{
if (id < 0 || id >= game.control.num_unit_types) {
return NULL;
return nullptr;
}
return &unit_types[id];
}
......
**************************************************************************/
const struct unit_type *unit_type_get(const struct unit *punit)
{
fc_assert_ret_val(NULL != punit, NULL);
fc_assert_ret_val(punit != nullptr, nullptr);
return punit->utype;
}
......
conversion factor in percent) and
FIXME: Should the ai know about this? */
if (otype == O_GOLD || otype == O_SHIELD) {
gold_upkeep_factor = get_target_bonus_effects(NULL,
gold_upkeep_factor = get_target_bonus_effects(nullptr,
&(const struct req_context) {
.player = pplayer,
.unittype = ut,
......
.tile = ptile,
.city = pcity
},
NULL, EFT_SHIELD2GOLD_PCT);
nullptr, EFT_SHIELD2GOLD_PCT);
if (gold_upkeep_factor > 0) {
switch (otype) {
......
}
}
val *= get_target_bonus_effects(NULL,
val *= get_target_bonus_effects(nullptr,
&(const struct req_context) {
.player = pplayer,
.output = get_output_type(otype),
......
.tile = ptile,
.city = pcity
},
NULL, EFT_UPKEEP_PCT) / 100;
nullptr, EFT_UPKEEP_PCT) / 100;
return val;
}
......
/* FIXME: Probably wrong result if the effect
* EFT_ACTION_SUCCESS_MOVE_COST depends on unit state. Add unit state
* parameters? */
mpco += get_target_bonus_effects(NULL,
mpco += get_target_bonus_effects(nullptr,
&(const struct req_context) {
.player = act_player,
.city = tile_city(post_action_tile),
......
.unittype = putype,
.action = paction,
},
NULL,
nullptr,
EFT_ACTION_SUCCESS_MOVE_COST);
return mpco;
......
/**********************************************************************//**
Returns the number of shields it takes to build this unit type.
If pplayer is NULL, owner of the pcity is used instead.
If pplayer is nullptr, owner of the pcity is used instead.
**************************************************************************/
int utype_build_shield_cost(const struct city *pcity,
const struct player *pplayer,
......
const struct player *owner;
const struct tile *ptile;
if (pcity != NULL) {
if (pcity != nullptr) {
owner = city_owner(pcity);
ptile = city_tile(pcity);
} else {
owner = NULL;
ptile = NULL;
owner = nullptr;
ptile = nullptr;
}
if (pplayer != NULL) {
if (pplayer != nullptr) {
/* Override city owner. */
owner = pplayer;
}
base = punittype->build_cost
* (100 + get_unittype_bonus(owner, ptile, punittype, NULL,
* (100 + get_unittype_bonus(owner, ptile, punittype, nullptr,
EFT_UNIT_BUILD_COST_PCT)) / 100;
return MAX(base * game.info.shieldbox / 100, 1);
......
**************************************************************************/
int unit_build_shield_cost(const struct city *pcity, const struct unit *punit)
{
return utype_build_shield_cost(pcity, NULL, unit_type_get(punit));
return utype_build_shield_cost(pcity, nullptr, unit_type_get(punit));
}
/**********************************************************************//**
......
int shields_in_stock)
{
int cost = 0;
const int missing = utype_build_shield_cost(pcity, NULL, punittype) - shields_in_stock;
const int missing
= utype_build_shield_cost(pcity, nullptr, punittype) - shields_in_stock;
struct player *owner;
struct tile *ptile;
......
cost *= 2;
}
if (pcity != NULL) {
if (pcity != nullptr) {
owner = city_owner(pcity);
ptile = city_tile(pcity);
} else {
owner = NULL;
ptile = NULL;
owner = nullptr;
ptile = nullptr;
}
cost = cost
* (100 + get_unittype_bonus(owner, ptile, punittype, NULL,
* (100 + get_unittype_bonus(owner, ptile, punittype, nullptr,
EFT_UNIT_BUY_COST_PCT))
/ 100;
......
int pop_cost = punittype->pop_cost;
pop_cost -= get_unittype_bonus(city_owner(pcity), city_tile(pcity),
punittype, NULL, EFT_POPCOST_FREE);
punittype, nullptr, EFT_POPCOST_FREE);
return MAX(0, pop_cost);
}
......
/**********************************************************************//**
Return whether this player can upgrade this unit type (to any other
unit type). Returns NULL if no upgrade is possible.
unit type). Returns nullptr if no upgrade is possible.
**************************************************************************/
const struct unit_type *can_upgrade_unittype(const struct player *pplayer,
const struct unit_type *punittype)
{
const struct unit_type *upgrade = punittype;
const struct unit_type *best_upgrade = NULL;
const struct unit_type *best_upgrade = nullptr;
/* For some reason this used to check
* can_player_build_unit_direct() for the unittype
......
/**********************************************************************//**
Return the cost (gold) of upgrading a single unit of the specified type
to the new type. This price could (but currently does not) depend on
to the new type. This price could (but currently does not) depend on
other attributes (like nation or government type) of the player the unit
belongs to.
**************************************************************************/
......
* before generalized actions appears. */
const struct action *paction = action_by_number(ACTION_UPGRADE_UNIT);
int missing = (utype_build_shield_cost_base(to)
- unit_shield_value(NULL, from, paction));
- unit_shield_value(nullptr, from, paction));
int base_cost = 2 * missing + (missing * missing) / 20;
return base_cost
......
/**********************************************************************//**
Returns the unit type that has the given (translated) name.
Returns NULL if none match.
Returns nullptr if none match.
**************************************************************************/
struct unit_type *unit_type_by_translated_name(const char *name)
{
......
}
} unit_type_iterate_end;
return NULL;
return nullptr;
}
/**********************************************************************//**
Returns the unit type that has the given (untranslated) rule name.
Returns NULL if none match.
Returns nullptr if none match.
**************************************************************************/
struct unit_type *unit_type_by_rule_name(const char *name)
{
......
}
} unit_type_iterate_end;
return NULL;
return nullptr;
}
/**********************************************************************//**
Returns the unit class that has the given (untranslated) rule name.
Returns NULL if none match.
Returns nullptr if none match.
**************************************************************************/
struct unit_class *unit_class_by_rule_name(const char *s)
{
......
}
} unit_class_iterate_end;
return NULL;
return nullptr;
}
/**********************************************************************//**
......
fc_assert_ret(id >= UCF_USER_FLAG_1 && id <= UCF_LAST_USER_FLAG);
if (user_class_flags[ufid].name != NULL) {
if (user_class_flags[ufid].name != nullptr) {
FC_FREE(user_class_flags[ufid].name);
user_class_flags[ufid].name = NULL;
user_class_flags[ufid].name = nullptr;
}
if (name && name[0] != '\0') {
user_class_flags[ufid].name = fc_strdup(name);
}
if (user_class_flags[ufid].helptxt != NULL) {
if (user_class_flags[ufid].helptxt != nullptr) {
free(user_class_flags[ufid].helptxt);
user_class_flags[ufid].helptxt = NULL;
user_class_flags[ufid].helptxt = nullptr;
}
if (helptxt && helptxt[0] != '\0') {
......
const char *unit_class_flag_id_name_cb(enum unit_class_flag_id flag)
{
if (flag < UCF_USER_FLAG_1 || flag > UCF_LAST_USER_FLAG) {
return NULL;
return nullptr;
}
return user_class_flags[flag - UCF_USER_FLAG_1].name;
......
fc_assert_ret(id >= UTYF_USER_FLAG_1 && id <= UTYF_LAST_USER_FLAG);
if (user_type_flags[ufid].name != NULL) {
if (user_type_flags[ufid].name != nullptr) {
FC_FREE(user_type_flags[ufid].name);
user_type_flags[ufid].name = NULL;
user_type_flags[ufid].name = nullptr;
}
if (name && name[0] != '\0') {
user_type_flags[ufid].name = fc_strdup(name);
}
if (user_type_flags[ufid].helptxt != NULL) {
if (user_type_flags[ufid].helptxt != nullptr) {
free(user_type_flags[ufid].helptxt);
user_type_flags[ufid].helptxt = NULL;
user_type_flags[ufid].helptxt = nullptr;
}
if (helptxt && helptxt[0] != '\0') {
......
const char *unit_type_flag_id_name_cb(enum unit_type_flag_id flag)
{
if (flag < UTYF_USER_FLAG_1 || flag > UTYF_LAST_USER_FLAG) {
return NULL;
return nullptr;
}
return user_type_flags[flag - UTYF_USER_FLAG_1].name;
......
const struct req_context context = { .player = p, .unittype = punittype };
bool barbarian = is_barbarian(p);
fc_assert_ret_val(NULL != punittype, FALSE);
fc_assert_ret_val(punittype != nullptr, FALSE);
if (barbarian
&& !utype_has_role(punittype, L_BARBARIAN_BUILD)
......
req_copy(&copy, preq);
copy.range = REQ_RANGE_WORLD;
if (!is_req_active(&context, NULL, &copy, RPT_CERTAIN)) {
if (!is_req_active(&context, nullptr, &copy, RPT_CERTAIN)) {
return FALSE;
}
} else if (!is_req_active(&context, NULL, preq, RPT_CERTAIN)) {
} else if (!is_req_active(&context, nullptr, preq, RPT_CERTAIN)) {
return FALSE;
}
}
} else if (!is_req_active(&context, NULL, preq, RPT_CERTAIN)) {
} else if (!is_req_active(&context, nullptr, preq, RPT_CERTAIN)) {
return FALSE;
}
} requirement_vector_iterate_end;
......
returns FALSE if unit is obsolete.
**************************************************************************/
bool can_player_build_unit_now(const struct player *p,
const struct unit_type *punittype)
const struct unit_type *punittype)
{
if (!can_player_build_unit_direct(p, punittype, FALSE)) {
return FALSE;
......
bool can_player_build_unit_later(const struct player *p,
const struct unit_type *punittype)
{
fc_assert_ret_val(NULL != punittype, FALSE);
fc_assert_ret_val(punittype != nullptr, FALSE);
if (utype_has_flag(punittype, UTYF_NOBUILD)) {
return FALSE;
......
j = 0;
unit_type_iterate(u) {
if (func_has(u, i)) {
with_role[i][j++] = u;
with_role[i][j++] = u;
}
} unit_type_iterate_end;
fc_assert(j == n_with_role[i]);
......
for (i = 0; i < MAX_UNIT_ROLES; i++) {
free(with_role[i]);
with_role[i] = NULL;
with_role[i] = nullptr;
n_with_role[i] = 0;
}
}
......
if (first_init) {
for (i = 0; i < MAX_UNIT_ROLES; i++) {
with_role[i] = NULL;
with_role[i] = nullptr;
n_with_role[i] = 0;
}
} else {
......
}
}
return NULL;
return nullptr;
}
/**********************************************************************//**
......
}
}
return NULL;
return nullptr;
}
/**********************************************************************//**
......
{
fc_assert_ret_val((role >= 0 && role <= UTYF_LAST_USER_FLAG)
|| (role >= L_FIRST && role < L_LAST)
|| (role >= L_LAST && role < MAX_UNIT_ROLES), NULL);
fc_assert_ret_val(!first_init, NULL);
|| (role >= L_LAST && role < MAX_UNIT_ROLES), nullptr);
fc_assert_ret_val(!first_init, nullptr);
if (role_index == -1) {
role_index = n_with_role[role] - 1;
}
fc_assert_ret_val(role_index >= 0 && role_index < n_with_role[role], NULL);
fc_assert_ret_val(role_index >= 0
&& role_index < n_with_role[role], nullptr);
return with_role[role][role_index];
}
/**********************************************************************//**
Return "best" unit this city can build, with given role/flag.
Returns NULL if none match. "Best" means highest unit type id.
Returns nullptr if none match. "Best" means highest unit type id.
**************************************************************************/
struct unit_type *best_role_unit(const struct city *pcity, int role)
{
......
fc_assert_ret_val((role >= 0 && role <= UTYF_LAST_USER_FLAG)
|| (role >= L_FIRST && role < L_LAST)
|| (role >= L_LAST && role < MAX_UNIT_ROLES), NULL);
fc_assert_ret_val(!first_init, NULL);
|| (role >= L_LAST && role < MAX_UNIT_ROLES), nullptr);
fc_assert_ret_val(!first_init, nullptr);
for (j = n_with_role[role] - 1; j >= 0; j--) {
u = with_role[role][j];
......
}
}
return NULL;
return nullptr;
}
/**********************************************************************//**
Return "best" unit the player can build, with given role/flag.
Returns NULL if none match. "Best" means highest unit type id.
Returns nullptr if none match. "Best" means highest unit type id.
TODO: Cache the result per player?
**************************************************************************/
......
fc_assert_ret_val((role >= 0 && role <= UTYF_LAST_USER_FLAG)
|| (role >= L_FIRST && role < L_LAST)
|| (role >= L_LAST && role < MAX_UNIT_ROLES), NULL);
fc_assert_ret_val(!first_init, NULL);
|| (role >= L_LAST && role < MAX_UNIT_ROLES), nullptr);
fc_assert_ret_val(!first_init, nullptr);
for (j = n_with_role[role] - 1; j >= 0; j--) {
struct unit_type *utype = with_role[role][j];
......
}
}
return NULL;
return nullptr;
}
/**********************************************************************//**
Return first unit the player can build, with given role/flag.
Returns NULL if none match. Used eg when placing starting units.
Returns nullptr if none match. Used eg when placing starting units.
**************************************************************************/
struct unit_type *first_role_unit_for_player(const struct player *pplayer,
int role)
......
fc_assert_ret_val((role >= 0 && role <= UTYF_LAST_USER_FLAG)
|| (role >= L_FIRST && role < L_LAST)
|| (role >= L_LAST && role < MAX_UNIT_ROLES), NULL);
fc_assert_ret_val(!first_init, NULL);
|| (role >= L_LAST && role < MAX_UNIT_ROLES), nullptr);
fc_assert_ret_val(!first_init, nullptr);
for (j = 0; j < n_with_role[role]; j++) {
struct unit_type *utype = with_role[role][j];
......
}
}
return NULL;
return nullptr;
}
/**********************************************************************//**
......
{
int i;
/* Can't use unit_type_iterate or utype_by_number here because
/* Can't use unit_type_iterate() or utype_by_number() here because
* num_unit_types isn't known yet. */
for (i = 0; i < ARRAY_SIZE(unit_types); i++) {
unit_types[i].item_number = i;
requirement_vector_init(&(unit_types[i].build_reqs));
unit_types[i].helptext = NULL;
unit_types[i].veteran = NULL;
unit_types[i].helptext = nullptr;
unit_types[i].veteran = nullptr;
unit_types[i].bonuses = combat_bonus_list_new();
unit_types[i].ruledit_disabled = FALSE;
unit_types[i].ruledit_dlg = NULL;
unit_types[i].ruledit_dlg = nullptr;
}
}
......
**************************************************************************/
static void unit_type_free(struct unit_type *punittype)
{
if (NULL != punittype->helptext) {
if (punittype->helptext != nullptr) {
strvec_destroy(punittype->helptext);
punittype->helptext = NULL;
punittype->helptext = nullptr;
}
requirement_vector_free(&(punittype->build_reqs));
......
if (game.control.num_unit_classes > 0) {
return unit_classes;
}
return NULL;
return nullptr;
}
/**********************************************************************//**
......
if (game.control.num_unit_classes > 0) {
return &unit_classes[game.control.num_unit_classes - 1];
}
return NULL;
return nullptr;
}
/**********************************************************************//**
......
struct unit_class *uclass_by_number(const Unit_Class_id id)
{
if (id < 0 || id >= game.control.num_unit_classes) {
return NULL;
return nullptr;
}
return &unit_classes[id];
}
......
**************************************************************************/
struct unit_class *utype_class(const struct unit_type *punittype)
{
fc_assert(NULL != punittype->uclass);
fc_assert(punittype->uclass != nullptr);
return punittype->uclass;
}
#endif /* utype_class */
......
* num_unit_classes isn't known yet. */
for (i = 0; i < ARRAY_SIZE(unit_classes); i++) {
unit_classes[i].item_number = i;
unit_classes[i].cache.refuel_extras = NULL;
unit_classes[i].cache.native_tile_extras = NULL;
unit_classes[i].cache.native_bases = NULL;
unit_classes[i].cache.bonus_roads = NULL;
unit_classes[i].cache.hiding_extras = NULL;
unit_classes[i].cache.subset_movers = NULL;
unit_classes[i].helptext = NULL;
unit_classes[i].cache.refuel_extras = nullptr;
unit_classes[i].cache.native_tile_extras = nullptr;
unit_classes[i].cache.native_bases = nullptr;
unit_classes[i].cache.bonus_roads = nullptr;
unit_classes[i].cache.hiding_extras = nullptr;
unit_classes[i].cache.subset_movers = nullptr;
unit_classes[i].helptext = nullptr;
unit_classes[i].ruledit_disabled = FALSE;
}
}
......
int i;
for (i = 0; i < ARRAY_SIZE(unit_classes); i++) {
if (unit_classes[i].cache.refuel_extras != NULL) {
if (unit_classes[i].cache.refuel_extras != nullptr) {
extra_type_list_destroy(unit_classes[i].cache.refuel_extras);
unit_classes[i].cache.refuel_extras = NULL;
unit_classes[i].cache.refuel_extras = nullptr;
}
if (unit_classes[i].cache.native_tile_extras != NULL) {
if (unit_classes[i].cache.native_tile_extras != nullptr) {
extra_type_list_destroy(unit_classes[i].cache.native_tile_extras);
unit_classes[i].cache.native_tile_extras = NULL;
unit_classes[i].cache.native_tile_extras = nullptr;
}
if (unit_classes[i].cache.native_bases != NULL) {
if (unit_classes[i].cache.native_bases != nullptr) {
extra_type_list_destroy(unit_classes[i].cache.native_bases);
unit_classes[i].cache.native_bases = NULL;
unit_classes[i].cache.native_bases = nullptr;
}
if (unit_classes[i].cache.bonus_roads != NULL) {
if (unit_classes[i].cache.bonus_roads != nullptr) {
extra_type_list_destroy(unit_classes[i].cache.bonus_roads);
unit_classes[i].cache.bonus_roads = NULL;
unit_classes[i].cache.bonus_roads = nullptr;
}
if (unit_classes[i].cache.hiding_extras != NULL) {
if (unit_classes[i].cache.hiding_extras != nullptr) {
extra_type_list_destroy(unit_classes[i].cache.hiding_extras);
unit_classes[i].cache.hiding_extras = NULL;
unit_classes[i].cache.hiding_extras = nullptr;
}
if (unit_classes[i].cache.subset_movers != NULL) {
if (unit_classes[i].cache.subset_movers != nullptr) {
unit_class_list_destroy(unit_classes[i].cache.subset_movers);
}
if (unit_classes[i].helptext != NULL) {
if (unit_classes[i].helptext != nullptr) {
strvec_destroy(unit_classes[i].helptext);
unit_classes[i].helptext = NULL;
unit_classes[i].helptext = nullptr;
}
}
}
......
const struct veteran_system *
utype_veteran_system(const struct unit_type *punittype)
{
fc_assert_ret_val(punittype != NULL, NULL);
fc_assert_ret_val(punittype != nullptr, nullptr);
if (punittype->veteran) {
return punittype->veteran;
}
fc_assert_ret_val(game.veteran != NULL, NULL);
fc_assert_ret_val(game.veteran != nullptr, nullptr);
return game.veteran;
}
......
const struct veteran_level *
vsystem_veteran_level(const struct veteran_system *vsystem, int level)
{
fc_assert_ret_val(vsystem->definitions != NULL, NULL);
fc_assert_ret_val(vsystem->levels > level, NULL);
fc_assert_ret_val(vsystem->definitions != nullptr, nullptr);
fc_assert_ret_val(vsystem->levels > level, nullptr);
return (vsystem->definitions + level);
}
......
{
const struct veteran_system *vsystem = utype_veteran_system(punittype);
fc_assert_ret_val(vsystem != NULL, NULL);
fc_assert_ret_val(vsystem != nullptr, nullptr);
return vsystem_veteran_level(vsystem, level);
}
/**********************************************************************//**
Return translated name of the given veteran level.
NULL if this unit type doesn't have different veteran levels.
nullptr if this unit type doesn't have different veteran levels.
**************************************************************************/
const char *utype_veteran_name_translation(const struct unit_type *punittype,
int level)
{
if (utype_veteran_levels(punittype) <= 1) {
return NULL;
return nullptr;
} else {
const struct veteran_level *vlvl = utype_veteran_level(punittype, level);
......
{
const struct veteran_system *vsystem = utype_veteran_system(punittype);
fc_assert_ret_val(vsystem != NULL, 1);
fc_assert_ret_val(vsystem != nullptr, 1);
return vsystem->levels;
}
......
struct veteran_system *vsystem;
/* There must be at least one level. */
fc_assert_ret_val(count > 0, NULL);
fc_assert_ret_val(count > 0, nullptr);
vsystem = fc_calloc(1, sizeof(*vsystem));
vsystem->levels = count;
......
{
struct veteran_level *vlevel;
fc_assert_ret(vsystem != NULL);
fc_assert_ret(vsystem != nullptr);
fc_assert_ret(vsystem->levels > level);
vlevel = vsystem->definitions + level;
names_set(&vlevel->name, NULL, vlist_name, NULL);
names_set(&vlevel->name, nullptr, vlist_name, nullptr);
vlevel->power_fact = vlist_power;
vlevel->move_bonus = vlist_move;
vlevel->base_raise_chance = vlist_raise;
......
if (is_extra_caused_by(pextra, EC_BASE)) {
extra_type_list_append(pclass->cache.native_bases, pextra);
}
if (proad != NULL && road_provides_move_bonus(proad)) {
if (proad != nullptr && road_provides_move_bonus(proad)) {
extra_type_list_append(pclass->cache.bonus_roads, pextra);
}
if (pextra->eus == EUS_HIDDEN) {
......
} extra_type_iterate_end;
terrain_type_iterate(pterrain) {
if (is_native_to_class(puclass, pterrain, NULL)) {
if (is_native_to_class(puclass, pterrain, nullptr)) {
if (is_ocean(pterrain)) {
sea_moving = TRUE;
} else {
common/unittype.h
int firepower;
struct combat_bonus_list *bonuses;
#define U_NOT_OBSOLETED (NULL)
#define U_NOT_OBSOLETED (nullptr)
const struct unit_type *obsoleted_by;
const struct unit_type *converted_to;
int convert_time;
......
#define unit_type_iterate(_p) \
{ \
struct unit_type *_p = unit_type_array_first(); \
if (NULL != _p) { \
if (_p != nullptr) { \
const struct unit_type *_end_##_p = unit_type_array_last(); \
for (; _p <= _end_##_p; _p++) {
......
#define unit_class_iterate(_p) \
{ \
struct unit_class *_p = unit_class_array_first(); \
if (NULL != _p) { \
if (_p != nullptr) { \
const struct unit_class *_end_##_p = unit_class_array_last(); \
for (; _p <= _end_##_p; _p++) {
    (1-1/1)