61 #define WORKER_FACTOR 1024
94 ACTRES_TRANSFORM_TERRAIN);
106 ACTRES_CLEAN_POLLUTION);
108 ACTRES_CLEAN_FALLOUT);
121 #define MAX_DEP_ROADS 5
124 bool potential_road[12], real_road[12], is_slow[12];
125 int dx[12] = {-1, 0, 1, -1, 1, -1, 0, 1, 0, -2, 2, 0};
126 int dy[12] = {-1, -1, -1, 0, 0, 1, 1, 1, -2, 0, 0, 2};
134 if (proad ==
nullptr) {
144 pdep_roads[dep_count] = pdep;
151 for (i = 0; i < 12; i++) {
155 real_road[i] =
false;
156 potential_road[i] =
false;
160 ACTIVITY_GEN_ROAD, pextra);
164 potential_road[i] = real_road[i];
165 for (j = 0; !potential_road[i] && j < dep_count; j++) {
172 is_slow[i] = (build_time == 0 || build_time > 5);
177 if (punit->activity == ACTIVITY_GEN_ROAD) {
182 fc_assert(punit->activity_target !=
nullptr);
186 if (build_rnbr == rnbr) {
188 potential_road[i] =
true;
190 for (j = 0; !potential_road[i] && j < dep_count; j++) {
191 if (build_rnbr == dep_rnbr[j]) {
192 potential_road[i] =
true;
207 for (i = 0; i < 12; i++) {
234 if (potential_road[0] && !real_road[1] && !real_road[3]
235 && (!real_road[2] || !real_road[8])
236 && (!is_slow[2] || !is_slow[4] || !is_slow[7] || !is_slow[6]
240 if (potential_road[2] && !real_road[1] && !real_road[4]
241 && (!real_road[7] || !real_road[10])
242 && (!is_slow[0] || !is_slow[3] || !is_slow[7] || !is_slow[6]
246 if (potential_road[5] && !real_road[6] && !real_road[3]
247 && (!real_road[5] || !real_road[11])
248 && (!is_slow[2] || !is_slow[4] || !is_slow[7] || !is_slow[1]
252 if (potential_road[7] && !real_road[6] && !real_road[4]
253 && (!real_road[0] || !real_road[9])
254 && (!is_slow[2] || !is_slow[3] || !is_slow[0] || !is_slow[1]
268 if (potential_road[1] && !real_road[4] && !real_road[3]
269 && (!is_slow[5] || !is_slow[6] || !is_slow[7])) {
272 if (potential_road[3] && !real_road[1] && !real_road[6]
273 && (!is_slow[2] || !is_slow[4] || !is_slow[7])) {
276 if (potential_road[4] && !real_road[1] && !real_road[6]
277 && (!is_slow[0] || !is_slow[3] || !is_slow[5])) {
280 if (potential_road[6] && !real_road[4] && !real_road[3]
281 && (!is_slow[0] || !is_slow[1] || !is_slow[2])) {
296 const struct player *pplayer,
enum unit_activity act,
298 int old_tile_value,
bool in_use,
int delay,
adv_want *best_value,
299 int *best_old_tile_value,
int *best_extra,
bool *improve_worked,
300 int *best_delay,
enum unit_activity *best_act,
305 int total_value = 0, base_value = 0;
306 int old_improvement_value;
314 if (new_tile_value > old_tile_value) {
316 }
else if (new_tile_value == old_tile_value && extra > 0) {
323 if (improves || extra > 0) {
324 if (!(*improve_worked) && !in_use) {
329 && (new_tile_value > *best_value
330 || (new_tile_value == *best_value
331 && old_tile_value < *best_old_tile_value))) {
332 *best_value = new_tile_value;
333 *best_old_tile_value = old_tile_value;
336 *best_target = target;
348 base_value = new_tile_value - old_tile_value;
356 total_value =
amortize(total_value, delay);
358 if (*improve_worked) {
359 old_improvement_value = *best_value;
363 old_improvement_value =
368 if (total_value > old_improvement_value
369 || (total_value == old_improvement_value
370 && old_tile_value > *best_old_tile_value)) {
372 *best_value = total_value;
373 *improve_worked =
true;
375 *best_value = new_tile_value;
376 *improve_worked =
false;
378 *best_old_tile_value = old_tile_value;
381 *best_target = target;
421 enum unit_activity *best_act,
431 int best_oldv = 9999;
435 bool improve_worked =
false;
440 struct unit *enroute =
nullptr;
455 bool consider =
true;
471 if (
unit_owner(aunit) == pplayer && aunit->id != punit->
id
497 if ((enroute && enroute->
id == punit->
id) || pos.
turn < eta
500 < inbound_distance))) {
503 "Considering (%d, %d) because we're closer "
504 "(%d, %d) than %d (%d, %d)",
507 eta, inbound_distance);
516 enum extra_cause cause =
518 enum extra_rmcause rmcause =
546 base_value, oldv, in_use, turns, &best_newv, &best_oldv,
547 &best_extra, &improve_worked, &best_delay, best_act,
548 best_target, best_tile, ptile);
555 enum unit_activity act = ACTIVITY_LAST;
556 enum unit_activity eval_act = ACTIVITY_LAST;
594 if (eval_act == ACTIVITY_LAST) {
606 if (base_value >= 0) {
621 int mc_multiplier = 1;
636 if (po_road->
move_cost < old_move_cost) {
653 mc_multiplier += old_move_cost;
675 if (act != ACTIVITY_LAST) {
677 pplayer, act, pextra, extra, base_value, oldv, in_use,
678 turns, &best_newv, &best_oldv, &best_extra,
679 &improve_worked, &best_delay, best_act, best_target,
691 ACTION_ROAD, punit,
unit_home(punit), ptile,
710 pplayer, ACTIVITY_GEN_ROAD, dep_tgt, extra,
711 dep_value, oldv, in_use, dep_turns, &best_newv,
712 &best_oldv, &best_extra, &improve_worked,
713 &best_delay, best_act, best_target, best_tile,
725 ACTION_BASE, punit,
unit_home(punit), ptile,
739 pplayer, ACTIVITY_BASE, dep_tgt, 0.0, dep_value,
740 oldv, in_use, dep_turns, &best_newv, &best_oldv,
741 &best_extra, &improve_worked, &best_delay, best_act,
742 best_target, best_tile, ptile);
757 if (!improve_worked) {
761 (best_newv - best_oldv + best_extra) *
WORKER_FACTOR, best_delay);
765 best_newv =
MAX(best_newv, 0);
768 log_debug(
"Settler %d@(%d,%d) wants to %s at (%d,%d) with "
775 *best_act = ACTIVITY_IDLE;
776 *best_tile =
nullptr;
802 struct city *taskcity =
nullptr;
815 bool consider =
true;
820 if (
unit_owner(aunit) == pplayer && aunit->id != punit->
id
830 ptask->tgt, ptask->ptile)) {
832 struct unit *enroute =
nullptr;
840 int value = (ptask->want + 1) * 10 / (pos.
turn + 1);
842 if (value > best_value) {
854 && ((enroute && enroute->
id == punit->
id) || pos.
turn < eta
857 < inbound_distance)))) {
873 if (!path->
empty()) {
889 enum unit_activity best_act;
890 struct tile *best_tile =
nullptr;
893 struct city *taskcity;
896 int completion_time = 0;
916 if (taskcity !=
nullptr) {
918 completion_time = path[-1].turn;
923 best_target = best_task->
tgt;
927 &best_target, completion_time)) {
941 completion_time = path[-1].turn;
948 best_tile, best_act, &best_target,
960 enum unit_activity best_act,
966 struct pf_map *pfm =
nullptr;
968 bool working =
false;
969 struct unit *displaced;
983 state[
tile_index(best_tile)].eta > completion_time
984 || (state[
tile_index(best_tile)].eta == completion_time
988 "%d (%d,%d) has displaced %d (%d,%d) for worksite %d,%d",
989 punit->
id, completion_time,
1001 int saved_id = punit->
id;
1013 || punit->
activity != ACTIVITY_IDLE) {
1019 "%d itself acted due to displacement recursion. "
1020 "Was going from (%d, %d) to (%d, %d). "
1021 "Now heading from (%d, %d) to (%d, %d).",
1029 unit_activity_name(best_act),
1034 if (!path->
empty()) {
1042 if (!path->
empty()) {
1061 "didn't start work yet; got to (%d, %d) with "
1062 "%d move frags left",
1112 if (
is_ai(pplayer)) {
1132 / ((
game.
info.warminglevel + 1) / 2)
1135 / ((
game.
info.coolinglevel + 1) / 2)
1149 if ((punit->ssa_controller == SSA_AUTOSETTLER ||
is_ai(pplayer))
1152 log_debug(
"%s %s at (%d, %d) is controlled by server side agent %s.",
1155 server_side_agent_name(SSA_AUTOSETTLER));
1156 if (punit->activity == ACTIVITY_SENTRY) {
1159 if (punit->activity == ACTIVITY_GOTO && punit->moves_left > 0) {
1162 if (punit->activity != ACTIVITY_IDLE) {
1163 if (!
is_ai(pplayer)) {
1171 if (punit->activity == ACTIVITY_IDLE) {
1172 if (!
is_ai(pplayer)) {
1182 if (
is_ai(pplayer)) {
1187 log_time(QStringLiteral(
"%1 autosettlers consumed %2 milliseconds.")
1201 if (punit->
server.adv->task == task) {
1206 punit->
server.adv->task = task;
1216 enum unit_activity activity,
1217 bool omniscient_cheat,
1219 const struct tile *ptile)
1221 struct action *paction =
nullptr;
1239 if (paction ==
nullptr) {
1247 paction->
id, punit,
unit_home(punit), ptile, omniscient_cheat,
1255 ptile, omniscient_cheat, ptile));
1258 paction->
id, punit,
unit_home(punit), ptile, omniscient_cheat, ptile,
1262 paction->
id, punit,
unit_home(punit), ptile, omniscient_cheat));
enum action_actor_kind action_get_actor_kind(const struct action *paction)
Get the actor kind of an action.
bool action_prob_possible(const struct act_prob probability)
Returns TRUE iff the given action probability belongs to an action that may be possible.
void action_list_end(action_id *act_list, int size)
Terminate an action list of the specified size.
struct act_prob action_speculate_unit_on_self(action_id act_id, const struct unit *actor, const struct city *actor_home, const struct tile *actor_tile, bool omniscient_cheat)
Returns a speculation about the actor unit's probability of successfully performing the chosen action...
void action_list_add_all_by_result(action_id *act_list, int *position, enum action_result result)
Add all actions with the specified result to the specified action list starting at the specified posi...
struct act_prob action_speculate_unit_on_units(action_id act_id, const struct unit *actor, const struct city *actor_home, const struct tile *actor_tile, bool omniscient_cheat, const struct tile *target)
Returns a speculation about the actor unit's probability of successfully performing the chosen action...
struct act_prob action_speculate_unit_on_city(const action_id act_id, const struct unit *actor, const struct city *actor_home, const struct tile *actor_tile, const bool omniscient_cheat, const struct city *target)
Returns a speculation about the actor unit's probability of successfully performing the chosen action...
struct action * action_by_number(action_id act_id)
Return the action with the given id.
enum unit_activity action_get_activity(const struct action *paction)
Returns the unit activity this action may cause or ACTIVITY_LAST if the action doesn't result in a un...
enum action_target_kind action_get_target_kind(const struct action *paction)
Get the target kind of an action.
struct act_prob action_speculate_unit_on_tile(action_id act_id, const struct unit *actor, const struct city *actor_home, const struct tile *actor_tile, bool omniscient_cheat, const struct tile *target_tile, const struct extra_type *target_extra)
Returns a speculation about the actor unit's probability of successfully performing the chosen action...
#define action_iterate_end
#define action_id_get_activity(act_id)
#define action_iterate(_act_)
bool adv_follow_path(struct unit *punit, const PFPath &path, struct tile *ptile)
Move a unit along a path without disturbing its activity, role or assigned destination Return FALSE i...
#define CALL_PLR_AI_FUNC(_func, _player,...)
bool adv_settler_safe_tile(const struct player *pplayer, struct unit *punit, struct tile *ptile)
Do we consider tile safe for autosettler to work?
action_id as_actions_rmextra[MAX_NUM_ACTIONS]
void adv_unit_new_task(struct unit *punit, enum adv_unit_task task, struct tile *ptile)
Change unit's advisor task.
action_id as_actions_transform[MAX_NUM_ACTIONS]
static enum tile_behavior autosettler_tile_behavior(const struct tile *ptile, enum known_type known, const struct pf_parameter *param)
Don't enter in enemy territories.
bool auto_settler_setup_work(struct player *pplayer, struct unit *punit, struct settlermap *state, int recursion, PFPath *path, struct tile *best_tile, enum unit_activity best_act, struct extra_type **best_target, int completion_time)
Setup our settler to do the work it has found.
adv_want adv_settlers_road_bonus(struct tile *ptile, struct road_type *proad)
Calculate the attractiveness of building a road/rail at the given tile.
void auto_settler_findwork(struct player *pplayer, struct unit *punit, struct settlermap *state, int recursion)
Find some work for our settlers and/or workers.
void auto_settlers_player(struct player *pplayer)
Run through all the players settlers and let those on ai.control work automagically.
static void consider_settler_action(const struct player *pplayer, enum unit_activity act, struct extra_type *target, adv_want extra, int new_tile_value, int old_tile_value, bool in_use, int delay, adv_want *best_value, int *best_old_tile_value, int *best_extra, bool *improve_worked, int *best_delay, enum unit_activity *best_act, struct extra_type **best_target, struct tile **best_tile, struct tile *ptile)
Compares the best known tile improvement action with improving ptile with activity act.
void auto_settlers_ruleset_init()
Initialize auto settlers based on the ruleset.
static civtimer * as_timer
action_id as_actions_extra[MAX_NUM_ACTIONS]
adv_want settler_evaluate_improvements(struct unit *punit, enum unit_activity *best_act, struct extra_type **best_target, struct tile **best_tile, PFPath *path, struct settlermap *state)
Finds tiles to improve, using punit.
struct city * settler_evaluate_city_requests(struct unit *punit, struct worker_task **best_task, PFPath *path, struct settlermap *state)
Return best city request to fulfill.
void adv_settlers_free()
Free resources allocated for autosettlers system.
bool auto_settlers_speculate_can_act_at(const struct unit *punit, enum unit_activity activity, bool omniscient_cheat, struct extra_type *target, const struct tile *ptile)
Returns TRUE iff the unit can do the targeted activity at the given location.
#define as_transform_action_iterate_end
#define as_rmextra_action_iterate(_act_)
#define as_rmextra_action_iterate_end
#define as_extra_action_iterate_end
#define as_extra_action_iterate(_act_)
#define as_transform_action_iterate(_act_)
struct extra_type * base_extra_get(const struct base_type *pbase)
Return extra that base is.
#define base_deps_iterate(_reqs, _dep)
#define base_deps_iterate_end
struct tile * city_tile(const struct city *pcity)
Return the tile location 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 ...
#define city_list_iterate(citylist, pcity)
#define city_tile_iterate_index_end
#define city_list_iterate_end
#define city_tile_iterate_index(_radius_sq, _city_tile, _tile, _index)
void citymap_turn_init(struct player *pplayer)
Initialize citymap by reserving worked tiles and establishing the crowdedness of (virtual) cities.
bool has_handicap(const struct player *pplayer, enum handicap_type htype)
AI players may have handicaps - allowing them to cheat or preventing them from using certain algorith...
int adv_city_worker_act_get(const struct city *pcity, int city_tile_index, enum unit_activity act_id)
Return the value for activity 'doing' on tile 'city_tile_index' of city 'pcity'.
int adv_city_worker_extra_get(const struct city *pcity, int city_tile_index, const struct extra_type *pextra)
Return the value for extra on tile 'city_tile_index' of city 'pcity'.
void initialize_infrastructure_cache(struct player *pplayer)
Do all tile improvement calculations and cache them for later.
int adv_city_worker_rmextra_get(const struct city *pcity, int city_tile_index, const struct extra_type *pextra)
Return the value for extra removal on tile 'city_tile_index' of city 'pcity'.
int city_tile_value(const struct city *pcity, const struct tile *ptile, int foodneed, int prodneed)
Returns a measure of goodness of a tile to pcity.
void log_time(const QString &msg, bool log)
#define fc_assert_ret(condition)
#define fc_assert(condition)
#define fc_assert_ret_val(condition, val)
#define log_debug(message,...)
struct tile * map_pos_to_tile(const struct civ_map *nmap, int map_x, int map_y)
Return the tile for the given cartesian (map) position.
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Are (x1,y1) and (x2,y2) really the same when adjusted? This function might be necessary ALOT of place...
int real_map_distance(const struct tile *tile0, const struct tile *tile1)
Return real distance between two tiles.
struct terrain_misc terrain_control
#define current_topo_has_flag(flag)
#define whole_map_iterate(_map, _tile)
#define whole_map_iterate_end
#define index_to_map_pos(pmap_x, pmap_y, mindex)
const char * nation_rule_name(const struct nation_type *pnation)
Return the (untranslated) rule name of the nation (adjective form).
struct nation_type * nation_of_player(const struct player *pplayer)
Return the nation of a player.
bool pf_map_position(struct pf_map *pfm, struct tile *ptile, struct pf_position *pos)
Get info about position at ptile and put it in pos.
struct pf_map * pf_map_new(const struct pf_parameter *parameter)
Factory function to create a new map according to the parameter.
PFPath pf_map_path(struct pf_map *pfm, struct tile *ptile)
CHECK DOCS AFTER FULL CONVERSTION OF pf_path to class PFPath Tries to find the best path in the given...
void pf_map_destroy(struct pf_map *pfm)
After usage the map must be destroyed.
struct unit * player_unit_by_number(const struct player *pplayer, int unit_id)
If the specified player owns the unit with the specified id, return pointer to the unit struct.
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Returns true iff players are allied.
Road_type_id road_number(const struct road_type *proad)
Return the road id.
bool road_provides_move_bonus(const struct road_type *proad)
Does road type provide move bonus.
struct extra_type * road_extra_get(const struct road_type *proad)
Return extra that road is.
#define road_deps_iterate(_reqs, _dep)
#define road_deps_iterate_end
static int recursion[AIT_LAST]
#define UNIT_LOG(_, punit, msg,...)
#define TIMING_LOG(timer, activity)
struct packet_game_info info
enum tile_behavior(* get_TB)(const struct tile *ptile, enum known_type known, const struct pf_parameter *param)
const struct player * owner
struct city_list * cities
struct player_ai ai_common
enum unit_activity activity
struct unit::@76::@79 server
int terrain_extra_build_time(const struct terrain *pterrain, enum unit_activity activity, const struct extra_type *tgt)
Time to complete the extra building activity on the given terrain.
bool tile_has_road(const struct tile *ptile, const struct road_type *proad)
Returns TRUE if the given tile has a road of given type on it.
struct city * tile_city(const struct tile *ptile)
Return the city on this tile (or nullptr), checking for city center.
#define tile_worked(_tile)
#define tile_terrain(_tile)
#define tile_has_extra(ptile, pextra)
#define tile_owner(_tile)
void timer_destroy(civtimer *t)
Deletes timer.
double timer_read_seconds(civtimer *t)
Read value from timer.
void timer_start(civtimer *t)
Start timing, adding to previous accumulated time if timer has not been cleared.
civtimer * timer_renew(civtimer *t, enum timer_timetype type, enum timer_use use)
Allocate a new timer, or reuse t, with specified "type" and "use".
bool timer_in_use(civtimer *t)
Return whether timer is in use.
int get_turns_for_activity_at(const struct unit *punit, enum unit_activity activity, const struct tile *ptile, struct extra_type *tgt)
Return the estimated number of turns for the worker unit to start and complete the activity at the gi...
void set_unit_activity(struct unit *punit, enum unit_activity new_activity)
Assign a new untargeted task to a unit.
bool is_military_unit(const struct unit *punit)
Military units are capable of enforcing martial law.
bool is_square_threatened(const struct player *pplayer, const struct tile *ptile, bool omniscient)
Return TRUE iff this tile is threatened from any unit within 2 tiles.
bool unit_is_cityfounder(const struct unit *punit)
Is a cityfounder unit?
const char * get_activity_text(enum unit_activity activity)
Return the name of the activity in a static buffer.
bool unit_has_orders(const struct unit *punit)
Return TRUE iff the unit is following client-side orders.
bool activity_requires_target(enum unit_activity activity)
Return TRUE if activity requires some sort of target to be specified.
#define CHECK_UNIT(punit)
bool unit_activity_handling(struct unit *punit, enum unit_activity new_activity)
Handle request for changing activity.
bool unit_activity_handling_targeted(struct unit *punit, enum unit_activity new_activity, struct extra_type **new_target)
Handle request for targeted activity.
#define unit_list_iterate(unitlist, punit)
#define unit_list_iterate_safe(unitlist, _unit)
#define unit_list_iterate_end
#define unit_list_iterate_safe_end
const char * unit_rule_name(const struct unit *punit)
Return the (untranslated) rule name of the unit.
const struct unit_type * unit_type_get(const struct unit *punit)
Return the unit type for this unit.
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Return whether the unit has the given flag.
#define worker_task_list_iterate(tasklist, ptask)
#define worker_task_list_iterate_end