12 #include <QElapsedTimer>
38 #define log_apply_result log_debug
39 #define log_handle_city log_debug
40 #define log_handle_city2 log_debug
41 #define log_results_are_equal log_debug
43 #define SHOW_APPLY_RESULT_ON_SERVER_ERRORS false
44 #define ALWAYS_APPLY_AT_SERVER false
45 #define MAX_LEN_PRESET_NAME 80
46 #define SAVED_PARAMETER_SIZE 32
48 #define CMA_NUM_PARAMS 5
50 #define SPECLIST_TAG preset
51 #define SPECLIST_TYPE struct cma_preset
88 std::unique_ptr<cm_result> &&result);
101 m_instance =
nullptr;
153 gimb->handle_city(
city);
223 if (last_request_id < 0) {
226 if (last_request_id != 0) {
227 int city_id = pcity->
id;
230 qDebug(
"apply_result_on_server(city %d) !check_city()!", city_id);
241 #if SHOW_APPLY_RESULT_ON_SERVER_ERRORS
242 qCritical(
"apply_result_on_server(city %d=\"%s\") no match!", pcity->
id,
245 log_test(
"apply_result_on_server(city %d=\"%s\") have:", pcity->
id,
250 log_test(
"apply_result_on_server(city %d=\"%s\") want:", pcity->
id,
268 struct city *pcity, std::unique_ptr<cm_result> &&result)
270 int first_request_id = 0, last_request_id = 0, i;
279 stats.apply_result_ignored++;
284 qCritical(
"apply_result_on_server(city %d=\"%s\") bad result!",
291 stats.apply_result_applied++;
301 if (
tile_worked(ptile) == pcity && !result->worker_positions[idx]) {
304 last_request_id = dsend_packet_city_make_specialist(
306 if (first_request_id == 0) {
307 first_request_id = last_request_id;
320 for (i = 0; i < pcity->
specialists[sp] - result->specialists[sp]; i++) {
325 if (first_request_id == 0) {
326 first_request_id = last_request_id;
339 if (
nullptr ==
tile_worked(ptile) && result->worker_positions[idx]) {
343 last_request_id = dsend_packet_city_make_worker(
345 if (first_request_id == 0) {
346 first_request_id = last_request_id;
360 for (i = 0; i < result->specialists[sp] - pcity->
specialists[sp]; i++) {
365 if (first_request_id == 0) {
366 first_request_id = last_request_id;
381 first_request_id = dsend_packet_city_refresh(&
client.
conn, pcity->
id);
382 last_request_id = first_request_id;
383 stats.refresh_forced++;
397 log_debug(
"cma_put_city_under_agent(city %d=\"%s\")", pcity->
id,
402 log_debug(
"cma_put_city_under_agent: return");
423 *parameter = my_parameter;
545 int i, city_id = pcity->
id;
555 for (i = 0; i < 5; i++) {
560 if (pcity !=
check_city(city_id, ¶meter)) {
566 if (!result->found_a_valid) {
572 _(
"The citizen governor can't fulfill the requirements "
573 "for %s. Passing back control."),
580 if (pcity ==
check_city(city_id,
nullptr) && i == 0) {
582 _(
"The citizen governor has gotten confused dealing "
583 "with %s. You may want to have a look."),
601 _(
"The citizen governor has gotten confused dealing "
602 "with %s. You may want to have a look."),
607 qCInfo(bugs_category,
608 "handle_city() CMA: %s has changed multiple times.",
621 gimb->put_city_under_agent(pcity, parameter);
635 return gimb->is_city_under_agent(pcity, parameter);
648 return gimb->get_parameter(attr, city_id, parameter);
657 gimb->set_parameter(attr, city_id, parameter);
718 delete[] ppreset->
descr;
732 return ppreset->
descr;
757 for (i = 0; i < preset_list_size(
preset_list); i++) {
810 .require_happy =
false,
811 .allow_disorder =
false,
812 .allow_specialists =
true,
813 .factor = {10, 5, 0, 4, 0, 4},
816 .minimal_surplus = {-20, 0, 0, -20, 0, 0},
817 .require_happy =
false,
818 .allow_disorder =
false,
819 .allow_specialists =
true,
820 .factor = {25, 5, 0, 4, 0, 4},
823 .minimal_surplus = {0, -20, 0, -20, 0, 0},
824 .require_happy =
false,
825 .allow_disorder =
false,
826 .allow_specialists =
true,
827 .factor = {10, 25, 0, 4, 0, 4},
830 .minimal_surplus = {0, 0, 0, -20, 0, 0},
831 .require_happy =
false,
832 .allow_disorder =
false,
833 .allow_specialists =
true,
834 .factor = {10, 5, 0, 25, 0, 4},
837 .minimal_surplus = {0, 0, 0, -20, 0, 0},
838 .require_happy =
false,
839 .allow_disorder =
false,
840 .allow_specialists =
true,
841 .factor = {10, 5, 0, 4, 0, 25},
844 N_(
"?cma:Very happy"),
N_(
"?cma:Prefer food"),
845 N_(
"?cma:Prefer production"),
N_(
"?cma:Prefer gold"),
846 N_(
"?cma:Prefer science")};
size_t attr_city_get(enum attr_city what, int city_id, size_t max_data_length, void *data)
Get city related attribute.
void attr_city_set(enum attr_city what, int city_id, size_t data_length, const void *const data)
Set city related attribute.
@ ATTR_CITY_CMAFE_PARAMETER
@ ATTR_CITY_CMA_PARAMETER
struct player * city_owner(const struct city *pcity)
Return the owner of the city.
struct tile * city_tile(const struct city *pcity)
Return the tile location of the city.
const char * city_name_get(const struct city *pcity)
Return the name of the city.
int city_map_radius_sq_get(const struct city *pcity)
Returns the current squared radius of the city.
bool city_can_work_tile(const struct city *pcity, const struct tile *ptile)
Returns TRUE when a tile is available to be worked, or the city itself is currently working the tile ...
citizens city_size_get(const struct city *pcity)
Get the city size.
#define output_type_iterate(output)
#define city_map_iterate_end
#define city_tile_iterate_skip_center_end
#define city_tile_iterate_skip_center(_radius_sq, _city_tile, _tile, _index, _x, _y)
#define is_city_center_index(city_tile_index)
#define city_map_iterate(_radius_sq, _index, _x, _y)
#define output_type_iterate_end
int city_change_specialist(struct city *pcity, Specialist_type_id from, Specialist_type_id to)
Change a specialist in the given city.
void refresh_city_dialog(struct city *pcity)
void city_report_dialog_update_city(struct city *pcity)
void result_came_from_server(int request)
bool apply_result_on_server(struct city *pcity, std::unique_ptr< cm_result > &&result)
Change the actual city setting to the given result.
bool is_city_under_agent(const struct city *pcity, struct cm_parameter *parameter)
void set_parameter(enum attr_city attr, int city_id, const struct cm_parameter *parameter)
void put_city_under_agent(struct city *pcity, const struct cm_parameter *const parameter)
std::unique_ptr< cm_result > cma_result_got
void handle_city(struct city *pcity)
The given city has changed.
bool get_parameter(enum attr_city attr, int city_id, struct cm_parameter *parameter)
void release_city(struct city *pcity)
struct city * check_city(int city_id, struct cm_parameter *parameter)
Returns TRUE if the city is valid for CMA.
static governor * m_instance
void add_city_remove(struct city *pcity)
std::set< int > scity_remove
void add_city_changed(struct city *pcity)
std::set< int > scity_changed
void add_city_new(struct city *pcity)
void create_event(struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
Creates a struct packet_generic_message packet and injects it via handle_chat_msg.
void cm_copy_parameter(struct cm_parameter *dest, const struct cm_parameter *const src)
Copy the parameter from the source to the destination field.
void cm_init_parameter(struct cm_parameter *dest)
Initialize the parameter to sane default values.
void cm_query_result(struct city *pcity, const struct cm_parameter *param, std::unique_ptr< cm_result > &result, bool negative_ok)
Wrapper that actually runs the branch & bound, and returns the best solution.
int cm_result_citizens(const std::unique_ptr< cm_result > &result)
Count the total number of citizens in the result.
void cm_result_from_main_map(std::unique_ptr< cm_result > &result, const struct city *pcity)
Copy the city's current setup into the cm result structure.
std::unique_ptr< cm_result > cm_result_new(struct city *pcity)
Create a new cm_result.
void cm_print_result(const std::unique_ptr< cm_result > &result)
Print debugging information about a full CM result.
void cm_print_city(const struct city *pcity)
Debugging routines.
void connection_do_buffer(struct connection *pc)
Turn on buffering, using a counter so that calls may be nested.
void connection_do_unbuffer(struct connection *pc)
Turn off buffering if internal counter of number of times buffering was turned on falls to zero,...
void dio_put_bool8_raw(struct raw_data_out *dout, bool value)
Insert value 0 or 1 using 8 bits.
void dio_output_init(struct raw_data_out *dout, void *destination, size_t dest_size)
Initializes the output to the given output buffer and the given buffer size.
void dio_put_uint8_raw(struct raw_data_out *dout, int value)
Insert value using 8 bits.
void dio_put_sint16_raw(struct raw_data_out *dout, int value)
Insert value using 16 bits.
bool dio_get_sint16_raw(struct data_in *din, int *dest)
Take value from 16 bits.
bool dio_get_uint8_raw(struct data_in *din, int *dest)
Receive uint8 value to dest.
bool dio_get_bool8_raw(struct data_in *din, bool *dest)
Take boolean value from 8 bits.
size_t dio_output_used(struct raw_data_out *dout)
Return the maximum number of bytes used.
void dio_input_init(struct data_in *din, const void *src, size_t src_size)
Initializes the input to the given input buffer and the given number of valid input bytes.
const struct ft_color ftc_client
const char * city_link(const struct city *pcity)
Get a text link to a city.
struct city * game_city_by_number(int id)
Often used function to get a city pointer from a city ID.
#define ALWAYS_APPLY_AT_SERVER
int cities_results_request()
const char * cmafec_get_short_descr_of_city(const struct city *pcity)
Return short description of city governor preset.
void cmafec_set_fe_parameter(struct city *pcity, const struct cm_parameter *const parameter)
Sets the front-end parameter.
bool operator==(const struct cm_result &result1, const struct cm_result &result2)
static struct preset_list * preset_list
int cmafec_preset_num()
Returns the total number of presets.
#define SAVED_PARAMETER_SIZE
const struct cm_parameter * cmafec_preset_get_parameter(int idx)
Returns the indexed preset's parameter.
#define log_results_are_equal
void cma_got_result(int citynr)
bool cma_is_city_under_agent(const struct city *pcity, struct cm_parameter *parameter)
Check whether city is under governor control, and fill parameter if it is.
void create_default_cma_presets()
Create default cma presets for a new user (or without configuration file)
char * cmafec_preset_get_descr(int idx)
Returns the indexed preset's description.
void cma_put_city_under_agent(struct city *pcity, const struct cm_parameter *const parameter)
Put city under governor control.
int cmafec_preset_get_index_of_parameter(const struct cm_parameter *const parameter)
Returns the index of the preset which matches the given parameter.
const char * cmafec_get_short_descr(const struct cm_parameter *const parameter)
Returns the description of the matching preset or "custom" if no preset could be found.
void cmafec_preset_remove(int idx)
Removes a preset.
bool cma_get_parameter(enum attr_city attr, int city_id, struct cm_parameter *parameter)
Get the parameter.
#define MAX_LEN_PRESET_NAME
void cma_release_city(struct city *pcity)
Release city from governor control.
void cma_set_parameter(enum attr_city attr, int city_id, const struct cm_parameter *parameter)
Set attribute block for city from parameter.
void cmafec_preset_add(const char *descr_name, const cm_parameter *pparam)
Adds a preset.
void cmafec_get_fe_parameter(struct city *pcity, struct cm_parameter *dest)
Return the front-end parameter for the given city.
#define fc_assert_ret(condition)
#define fc_assert(condition)
#define fc_assert_ret_val(condition, val)
#define fc_assert_action(condition, action)
#define log_debug(message,...)
void update_turn_done_button_state()
Update the turn done button state.
struct nation_type * nation_of_city(const struct city *pcity)
Return the nation of the player who owns the city.
const char * nation_rule_name(const struct nation_type *pnation)
Return the (untranslated) rule name of the nation (adjective form).
struct city * player_city_by_number(const struct player *pplayer, int city_id)
If the specified player owns the city with the specified id, return pointer to the city struct.
Q_GLOBAL_STATIC(QVector< QString >, future_name_translation)
#define specialist_type_iterate_end
#define specialist_type_iterate(sp)
#define DEFAULT_SPECIALIST
citizens specialists[SP_MAX]
int minimal_surplus[O_LAST]
std::vector< bool > worker_positions
citizens specialists[SP_MAX]
struct cm_parameter parameter
size_t fc_strlcpy(char *dest, const char *src, size_t n)
fc_strlcpy() provides utf-8 version of (non-standard) function strlcpy() It is intended as more user-...
#define tile_worked(_tile)