Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement two broken governor options #2083

Merged
merged 2 commits into from
Dec 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions common/aicore/cm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

// utility
#include "shared.h"
#include "support.h"
#include "timing.h"

// common
#include "city.h"
Expand Down Expand Up @@ -1189,8 +1187,7 @@ static void sort_lattice_by_fitness(const struct cm_state *state,
/**
Create the lattice.
*/
static void init_tile_lattice(struct city *pcity,
struct tile_type_vector *lattice)
static void init_tile_lattice(struct city *pcity, struct cm_state *state)
{
struct cm_tile_type type;
struct tile *pcenter = city_tile(pcity);
Expand All @@ -1205,21 +1202,23 @@ static void init_tile_lattice(struct city *pcity,
continue;
} else if (city_can_work_tile(pcity, ptile)) {
compute_tile_production(pcity, ptile, &type); // clobbers type
tile_type_lattice_add(lattice, &type,
tile_type_lattice_add(&state->lattice, &type,
ctindex); // copy type if needed
}
}
city_tile_iterate_index_end;

// Add all the specialists into the lattice.
init_specialist_lattice_nodes(lattice, pcity);
if (state->parameter.allow_specialists) {
init_specialist_lattice_nodes(&state->lattice, pcity);
}

// Set the lattice_depth fields, and clean up unreachable nodes.
top_sort_lattice(lattice);
clean_lattice(lattice, pcity);
top_sort_lattice(&state->lattice);
clean_lattice(&state->lattice, pcity);

// All done now.
print_lattice(LOG_LATTICE, lattice);
print_lattice(LOG_LATTICE, &state->lattice);
}

/**
Expand Down Expand Up @@ -1832,12 +1831,16 @@ static bool bb_next(struct cm_state *state, bool negative_ok)
/**
Initialize the state for the branch-and-bound algorithm.
*/
static struct cm_state *cm_state_init(struct city *pcity, bool negative_ok)
static struct cm_state *cm_state_init(struct city *pcity,
const cm_parameter *param,
bool negative_ok)
{
const int SCIENCE = 0, TAX = 1, LUXURY = 2;
const struct player *pplayer = city_owner(pcity);
int numtypes;
auto *state = new cm_state;
state->parameter = *param;

int rates[3];

log_base(LOG_CM_STATE, "creating cm_state for %s (size %d)",
Expand All @@ -1848,7 +1851,7 @@ static struct cm_state *cm_state_init(struct city *pcity, bool negative_ok)

// create the lattice
tile_type_vector_init(&state->lattice);
init_tile_lattice(pcity, &state->lattice);
init_tile_lattice(pcity, state);
numtypes = tile_type_vector_size(&state->lattice);

get_tax_rates(pplayer, rates);
Expand Down Expand Up @@ -1918,15 +1921,13 @@ static int min_food_surplus_for_fastest_growth(struct cm_state *state)
int food_needed = city_granary_size(city_size) - pcity->food_stock;
int min_turns;

city_map_iterate(city_radius_sq, cindex, x, y)
city_map_iterate_without_index(city_radius_sq, x, y)
{
struct tile *ptile = city_map_to_tile(pcity->tile, city_radius_sq, x, y);
if (!ptile) {
if (!ptile || !city_can_work_tile(pcity, ptile)) {
continue;
}
if (is_free_worked_index(cindex)) {
max_surplus += city_tile_output(pcity, ptile, is_celebrating, O_FOOD);
}
max_surplus += city_tile_output(pcity, ptile, is_celebrating, O_FOOD);
}
city_map_iterate_end;

Expand Down Expand Up @@ -1973,13 +1974,14 @@ static void begin_search(struct cm_state *state,

// copy the parameter and sort the main lattice by it
cm_copy_parameter(&state->parameter, parameter);
sort_lattice_by_fitness(state, &state->lattice);

if (parameter->max_growth) {
state->parameter.minimal_surplus[O_FOOD] =
min_food_surplus_for_fastest_growth(state);
state->parameter.factor[O_FOOD] = 0;
}

sort_lattice_by_fitness(state, &state->lattice);
init_min_production(state);

// clear out the old solution
Expand Down Expand Up @@ -2088,7 +2090,7 @@ static void cm_find_best_solution(struct cm_state *state,
void cm_query_result(struct city *pcity, const struct cm_parameter *param,
std::unique_ptr<cm_result> &result, bool negative_ok)
{
struct cm_state *state = cm_state_init(pcity, negative_ok);
struct cm_state *state = cm_state_init(pcity, param, negative_ok);

/* Refresh the city. Otherwise the CM can give wrong results or just be
* slower than necessary. Note that cities are often passed in in an
Expand Down
Loading