Freeciv-3.2
Loading...
Searching...
No Matches
unittools.c
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22/* utility */
23#include "bitvector.h"
24#include "fcintl.h"
25#include "log.h"
26#include "mem.h"
27#include "rand.h"
28#include "shared.h"
29#include "support.h"
30
31/* common */
32#include "base.h"
33#include "city.h"
34#include "combat.h"
35#include "events.h"
36#include "game.h"
37#include "government.h"
38#include "idex.h"
39#include "map.h"
40#include "movement.h"
41#include "packets.h"
42#include "player.h"
43#include "research.h"
44#include "terrain.h"
45#include "unit.h"
46#include "unitlist.h"
47#include "unittype.h"
48
49/* common/scriptcore */
50#include "luascript_signal.h"
51#include "luascript_types.h"
52
53/* aicore */
54#include "path_finding.h"
55#include "pf_tools.h"
56
57/* server/scripting */
58#include "script_server.h"
59
60/* server */
61#include "actiontools.h"
62#include "aiiface.h"
63#include "barbarian.h"
64#include "citytools.h"
65#include "cityturn.h"
66#include "diplhand.h"
67#include "gamehand.h"
68#include "maphand.h"
69#include "notify.h"
70#include "plrhand.h"
71#include "sanitycheck.h"
72#include "sernet.h"
73#include "srv_main.h"
74#include "techtools.h"
75#include "unithand.h"
76
77/* server/advisors */
78#include "advgoto.h"
79#include "autoexplorer.h"
80#include "autosettlers.h"
81
82/* ai */
83#include "handicaps.h"
84
85#include "unittools.h"
86
87
88/* Tools for controlling the client vision of every unit when a unit
89 * moves + script effects. See unit_move(). You can access this data with
90 * punit->server.moving; it may be NULL if the unit is not moving). */
93 struct unit *punit; /* NULL for invalidating. */
94 struct player *powner;
98};
99
100#define SPECLIST_TAG unit_move_data
101#include "speclist.h"
102#define unit_move_data_list_iterate(_plist, _pdata) \
103 TYPED_LIST_ITERATE(struct unit_move_data, _plist, _pdata)
104#define unit_move_data_list_iterate_end LIST_ITERATE_END
105#define unit_move_data_list_iterate_rev(_plist, _pdata) \
106 TYPED_LIST_ITERATE_REV(struct unit_move_data, _plist, _pdata)
107#define unit_move_data_list_iterate_rev_end LIST_ITERATE_REV_END
108
109/* This data structure lets the auto attack code cache each potential
110 * attacker unit's probability of success against the target unit during
111 * the checks if the unit can do autoattack. It is then reused when the
112 * list of potential attackers is sorted by probability of success. */
116};
117
118#define SPECLIST_TAG autoattack_prob
119#define SPECLIST_TYPE struct autoattack_prob
120#include "speclist.h"
121
122#define autoattack_prob_list_iterate_safe(autoattack_prob_list, _aap_, \
123 _unit_) \
124 TYPED_LIST_ITERATE(struct autoattack_prob, autoattack_prob_list, _aap_) \
125 struct unit *_unit_ = game_unit_by_number(_aap_->unit_id); \
126 \
127 if (_unit_ == NULL) { \
128 continue; \
129 }
130
131#define autoattack_prob_list_iterate_safe_end LIST_ITERATE_END
132
133static void unit_restore_hitpoints(struct unit *punit);
134static void unit_restore_movepoints(struct player *pplayer, struct unit *punit);
135static void update_unit_activity(struct unit *punit);
136static bool try_to_save_unit(struct unit *punit, const struct unit_type *pttype,
137 bool helpless, bool teleporting,
138 const struct city *pexclcity);
139static void wakeup_neighbor_sentries(struct unit *punit);
140static void do_upgrade_effects(struct player *pplayer);
141
142static bool maybe_cancel_patrol_due_to_enemy(struct unit *punit);
143
144static bool maybe_become_veteran_real(struct unit *punit, int base_chance,
145 bool settler);
146
147static void unit_transport_load_tp_status(struct unit *punit,
148 struct unit *ptrans,
149 bool force);
150
151static void wipe_unit_full(struct unit *punit, bool transported,
153 struct player *killer);
154
155static int get_unit_vision_base(const struct unit *punit,
156 enum vision_layer vlayer,
157 const int base);
158static int unit_vision_range_modifiers(const struct unit *punit,
159 const struct tile *ptile);
160
161/**********************************************************************/
177{
178 struct unit_type *which[U_LAST];
179 int i, num = 0;
180
181 if (role_tech != -1) {
182 for (i = 0; i < num_role_units(role_tech); i++) {
184 const int minplayers = 2;
185 int players = 0;
186
187 /* Note, if there's only one player in the game this check will always
188 * fail. */
189 players_iterate(pplayer) {
190 if (!is_barbarian(pplayer)
192 players++;
193 }
195
196 if (players > minplayers) {
197 which[num++] = iunit;
198 }
199 }
200 }
201 if (num == 0) {
202 for (i = 0; i < num_role_units(role); i++) {
203 which[num++] = get_role_unit(role, i);
204 }
205 }
206
207 /* Ruleset code should ensure there is at least one unit for each
208 * possibly-required role, or check before calling this function. */
209 fc_assert_exit_msg(0 < num, "No unit types in find_a_unit_type(%d, %d)!",
210 role, role_tech);
211
212 return which[fc_rand(num)];
213}
214
215/**********************************************************************/
223
224/**********************************************************************/
238 bool settler)
239{
240 const struct veteran_system *vsystem;
241 const struct veteran_level *vlevel;
242 int chance;
243
245
249
252
253 if (punit->veteran + 1 >= vsystem->levels
255 return FALSE;
256 } else if (!settler) {
258
259 /* The modification is tacked on as a multiplier to the base chance.
260 * For example with a base chance of 50% for green units and a modifier
261 * of +50% the end chance is 75%. */
262 chance = vlevel->base_raise_chance * mod / 100;
263 } else if (settler && unit_has_type_flag(punit, UTYF_SETTLERS)) {
264 chance = base_chance * vlevel->work_raise_chance / 100;
265 } else {
266 /* No battle and no work done. */
267 return FALSE;
268 }
269
270 if (fc_rand(100) < chance) {
271 punit->veteran++;
272 return TRUE;
273 }
274
275 return FALSE;
276}
277
278/**********************************************************************/
288bool unit_versus_unit(struct unit *attacker, struct unit *defender,
289 int *att_hp, int *def_hp, int *att_vet, int *def_vet,
290 const struct action *paction)
291{
292 int attackpower = get_total_attack_power(attacker, defender, paction);
293 int defensepower = get_total_defense_power(attacker, defender);
295 struct player *plr1 = unit_owner(attacker);
296 struct player *plr2 = unit_owner(defender);
297 struct civ_map *nmap = &(wld.map);
298 int max_rounds;
299 int rounds;
300 int att_strength;
301 int def_strength;
302
303 *att_hp = attacker->hp;
304 *def_hp = defender->hp;
305 get_modified_firepower(nmap, attacker, defender,
307
308 log_verbose("attack:%d, defense:%d, attack firepower:%d, "
309 "defense firepower:%d", attackpower, defensepower,
311
314
317
318 /* In a combat between equal strength units the values are 50% / 50%.
319 * -> scaling that to 100% by doubling, to match scale of chances
320 * in existing rulesets, and in !combat_odds_scaled_veterancy case. */
323
324 if (attackpower == 0) {
325 *att_hp = 0;
326 } else if (defensepower == 0) {
327 *def_hp = 0;
328 }
330 for (rounds = 0;
331 *att_hp > 0 && *def_hp > 0
333 rounds++) {
335 *def_hp -= attack_firepower;
336 } else {
337 *att_hp -= defense_firepower;
338 }
339 }
340 if (*att_hp < 0) {
341 *att_hp = 0;
342 }
343 if (*def_hp < 0) {
344 *def_hp = 0;
345 }
346
347 return attackpower <= 0 || defensepower <= 0;
348}
349
350/**********************************************************************/
356void unit_bombs_unit(struct unit *attacker, struct unit *defender,
357 int *att_hp, int *def_hp,
358 const struct action *paction)
359{
360 int i;
361 int rate = unit_bombard_rate(attacker);
362 int attackpower = get_total_attack_power(attacker, defender, paction);
363 int defensepower = get_total_defense_power(attacker, defender);
365 struct player *plr1 = unit_owner(attacker);
366 struct player *plr2 = unit_owner(defender);
367 struct civ_map *nmap = &(wld.map);
368
369 *att_hp = attacker->hp;
370 *def_hp = defender->hp;
371 get_modified_firepower(nmap, attacker, defender,
373
374 log_verbose("attack:%d, defense:%d, attack firepower:%d, "
375 "defense firepower:%d", attackpower, defensepower,
377
380
381 for (i = 0; i < rate; i++) {
383 *def_hp -= attack_firepower;
384 }
385 }
386
387 if (*def_hp <= 0) {
388 if (BV_ISSET(paction->sub_results, ACT_SUB_RES_NON_LETHAL)) {
389 /* Don't kill the target. */
390 *def_hp = 1;
391 } else {
392 *def_hp = 0;
393 }
394 }
395}
396
397/**********************************************************************/
401void combat_veterans(struct unit *attacker, struct unit *defender,
402 bool powerless, int att_vet, int def_vet)
403{
405 if (attacker->hp <= 0 || defender->hp <= 0
408 att_vet = 100;
409 def_vet = 100;
410 }
411 if (attacker->hp > 0) {
412 maybe_make_veteran(attacker, att_vet);
413 }
414 if (defender->hp > 0) {
415 maybe_make_veteran(defender, def_vet);
416 }
417 }
418 }
419}
420
421/**********************************************************************/
425static void do_upgrade_effects(struct player *pplayer)
426{
428 struct unit_list *candidates;
429
430 if (upgrades <= 0) {
431 return;
432 }
434
435 unit_list_iterate(pplayer->units, punit) {
436 /* We have to be careful not to strand units at sea, for example by
437 * upgrading a frigate to an ironclad while it was carrying a unit. */
438 if (UU_OK == unit_upgrade_test(&(wld.map), punit, TRUE)) {
439 unit_list_prepend(candidates, punit); /* Potential candidate :) */
440 }
442
443 while (upgrades > 0 && unit_list_size(candidates) > 0) {
444 /* Upgrade one unit. The unit is chosen at random from the list of
445 * available candidates. */
448 const struct unit_type *type_from = unit_type_get(punit);
449 const struct unit_type *type_to = can_upgrade_unittype(pplayer, type_from);
450
453 _("%s was upgraded for free to %s."),
457 upgrades--;
458 }
459
461}
462
463/**********************************************************************/
478void player_restore_units(struct player *pplayer)
479{
480 const struct civ_map *nmap = &(wld.map);
481
482 /* 1) get Leonardo out of the way first: */
483 do_upgrade_effects(pplayer);
484
486
487 /* 2) Modify unit hitpoints. Helicopters can even lose them. */
489
490 /* 3) Check that unit has hitpoints */
491 if (punit->hp <= 0) {
492 /* This should usually only happen for heli units, but if any other
493 * units get 0 hp somehow, catch them too. --dwp */
494 /* if 'game.server.killunhomed' is activated unhomed units are slowly
495 * killed; notify player here */
496 if (!punit->homecity && 0 < game.server.killunhomed) {
498 ftc_server, _("Your %s has run out of hit points "
499 "because it was not supported by a city."),
501 } else {
503 _("Your %s has run out of hit points."),
505 }
506
508 continue; /* Continue iterating... */
509 }
510
511 /* 4) Rescue planes if needed */
513 /* Shall we emergency return home on the last vapors? */
514
515 /* I think this is strongly against the spirit of client goto.
516 * The problem is (again) that here we know too much. -- Zamar */
517
518 if (punit->fuel <= 1
520 struct unit *carrier;
521
523 if (carrier) {
525 } else {
526 struct pf_map *pfm;
527 struct pf_parameter parameter;
528 bool alive = TRUE;
529
530 pft_fill_unit_parameter(&parameter, nmap, punit);
531 parameter.omniscience = !has_handicap(pplayer, H_MAP);
532 pfm = pf_map_new(&parameter);
533
534 pf_map_move_costs_iterate(pfm, ptile, move_cost, TRUE) {
535 if (move_cost > punit->moves_left) {
536 /* Too far */
537 break;
538 }
539
540 if (is_refuel_point(ptile, pplayer, punit)) {
541 struct pf_path *path;
542 int id = punit->id;
543
544 /* Client orders may be running for this unit - if so
545 * we free them before engaging goto. */
547
548 path = pf_map_path(pfm, ptile);
549
550 alive = adv_follow_path(punit, path, ptile);
551
552 if (!alive) {
553 log_error("rescue plane: unit %d died enroute!", id);
554 } else if (!same_pos(unit_tile(punit), ptile)) {
555 /* Enemy units probably blocked our route
556 * FIXME: We should try find alternative route around
557 * the enemy unit instead of just giving up and crashing. */
558 log_debug("rescue plane: unit %d could not move to "
559 "refuel point!", punit->id);
560 }
561
562 if (alive) {
563 /* Clear activity. Unit info will be sent in the end of
564 * the function. */
568
571 if (carrier) {
573 }
574 }
575
576 notify_player(pplayer, unit_tile(punit),
578 _("Your %s has returned to refuel."),
580 }
581 pf_path_destroy(path);
582 break;
583 }
586
587 if (!alive) {
588 /* Unit died trying to move to refuel point. */
589 return;
590 }
591 }
592 }
593
594 /* 5) Update fuel */
595 punit->fuel--;
596
597 /* 6) Automatically refuel air units in cities, airbases, and
598 * transporters (carriers). */
601 }
602 }
604
605 /* 7) Check if there are air units without fuel */
607 const struct unit_type *utype = unit_type_get(punit);
608
609 if (punit->fuel <= 0 && utype_fuel(utype)) {
610 /* Notifications sent from the lua script when unit wiped. */
612 }
614
615 /* Send all updates. */
616 unit_list_iterate(pplayer->units, punit) {
619}
620
621/**********************************************************************/
632{
633 bool was_lower;
634 const struct unit_type *utype = unit_type_get(punit);
635
636 was_lower = (punit->hp < utype->hp);
637
639
640 fc_assert(punit->hp >= 0);
641 fc_assert(punit->hp <= utype->hp);
642
643 if (punit->hp == utype->hp) {
646 }
647 }
648
649 punit->moved = FALSE;
651}
652
653/**********************************************************************/
658static void unit_restore_movepoints(struct player *pplayer, struct unit *punit)
659{
662}
663
664/**********************************************************************/
673
674/**********************************************************************/
677void execute_unit_orders(struct player *pplayer)
678{
680 if (unit_has_orders(punit)) {
682 }
684}
685
686/**********************************************************************/
689void unit_tc_effect_refresh(struct player *pplayer)
690{
691 unit_list_iterate(pplayer->units, punit) {
694}
695
696/**********************************************************************/
700{
701 /* Remember activities only after all knock-on effects of unit activities
702 * on other units have been resolved */
703 unit_list_iterate(pplayer->units, punit) {
709}
710
711/**********************************************************************/
715static int total_activity(struct tile *ptile, enum unit_activity act,
716 struct extra_type *tgt)
717{
718 int total = 0;
720
721 unit_list_iterate(ptile->units, punit) {
722 if (punit->activity == act
723 && (!tgt_matters || punit->activity_target == tgt)) {
724 total += punit->activity_count;
725 }
727
728 return total;
729}
730
731/**********************************************************************/
735static bool total_activity_done(struct tile *ptile, enum unit_activity act,
736 struct extra_type *tgt)
737{
738 return total_activity(ptile, act, tgt) >= tile_activity_time(act, ptile, tgt);
739}
740
741/**********************************************************************/
745{
746 const struct veteran_level *vlevel;
747 const struct veteran_system *vsystem;
748
749 if (!punit) {
750 return;
751 }
752
756
759
762 /* TRANS: Your <unit> became ... rank of <veteran level>. */
763 _("Your %s became more experienced and achieved the rank "
764 "of %s."),
766}
767
768/**********************************************************************/
771static void unit_convert(struct unit *punit)
772{
773 const struct unit_type *to_type;
774 const struct unit_type *from_type;
775
777 to_type = from_type->converted_to;
778
779 if (unit_can_convert(&(wld.map), punit)) {
783 _("%s converted to %s."),
786 } else {
789 _("%s cannot be converted."),
791 }
792}
793
794/**********************************************************************/
798{
799 if (unit_has_orders(punit)) {
802 _("Orders for %s aborted because activity "
803 "is no longer available."),
806 }
807
810}
811
812/**********************************************************************/
823
824/**********************************************************************/
835
836/**********************************************************************/
850
851/**********************************************************************/
855static void update_unit_activity(struct unit *punit)
856{
857 struct player *pplayer = unit_owner(punit);
859 enum unit_activity activity = punit->activity;
860 struct tile *ptile = unit_tile(punit);
861 const struct unit_type *act_utype = unit_type_get(punit);
862
863 switch (activity) {
864 case ACTIVITY_IDLE:
865 case ACTIVITY_EXPLORE:
867 case ACTIVITY_SENTRY:
868 case ACTIVITY_GOTO:
869 case ACTIVITY_LAST:
870 /* We don't need the activity_count for the above */
871 break;
872
874 case ACTIVITY_CONVERT:
876 break;
877
878 case ACTIVITY_CLEAN:
879 case ACTIVITY_MINE:
881 case ACTIVITY_PILLAGE:
883 case ACTIVITY_PLANT:
885 case ACTIVITY_BASE:
888
889 /* Settler may become veteran when doing something useful */
892 }
893 break;
894 };
895
897
898 switch (activity) {
899 case ACTIVITY_EXPLORE:
900 /* Not accumulating activity - will be handled more like movement
901 * after the TC */
903 case ACTIVITY_IDLE:
905 case ACTIVITY_SENTRY:
906 case ACTIVITY_GOTO:
908 case ACTIVITY_CONVERT:
909 case ACTIVITY_LAST:
910 /* No default, ensure all handled */
911 break;
912
913 case ACTIVITY_PILLAGE:
918
920
921 /* Change vision if effects have changed. */
923 }
924 break;
925
926 case ACTIVITY_CLEAN:
927 /* TODO: Remove this fallback target setting when target always correctly
928 * set */
929 {
930 struct extra_type *pextra;
931
932 if (punit->activity_target == NULL) {
933 pextra = prev_extra_in_tile(ptile, ERM_CLEAN,
934 NULL, punit);
935 if (pextra != NULL) {
936 punit->activity_target = pextra;
937 }
938 } else {
940 pextra = punit->activity_target;
941 } else {
942 pextra = NULL;
943 }
944 }
945
946 if (pextra != NULL) {
947 if (total_activity_done(ptile, ACTIVITY_CLEAN, pextra)) {
948 destroy_extra(ptile, pextra);
950 }
951 }
952 }
953 break;
954
955 case ACTIVITY_BASE:
956 {
961 }
962 }
963 break;
964
966 {
971 }
972 }
973 break;
974
976 case ACTIVITY_MINE:
978 case ACTIVITY_PLANT:
980 if (total_activity_done(ptile, activity, punit->activity_target)) {
981 struct terrain *old = tile_terrain(ptile);
982
983 /* The function below could change the terrain. Therefore, we have to
984 * check the terrain (which will also do a sanity check for the tile). */
985 tile_apply_activity(ptile, activity, punit->activity_target);
986 check_terrain_change(ptile, old);
988 }
989 break;
990 }
991
992 if (unit_activity_done) {
994
996
998 if (punit2->activity == activity
999 && punit2->activity_target == act_tgt) {
1000 /* This unit was helping with the work just finished.
1001 * Mark it idle (already finished) so its "current"
1002 * activity is not considered illegal
1003 * in tile_change_side_effects() . */
1005 }
1007
1009 }
1010
1011 if (activity == ACTIVITY_FORTIFYING) {
1014 punit, ptile, punit->activity_target)) {
1017 }
1018 }
1019
1020 if (activity == ACTIVITY_CONVERT) {
1023 punit, ptile, punit->activity_target)) {
1027 }
1028 }
1029
1030 if (unit_activity_done) {
1031 if (activity == ACTIVITY_PILLAGE) {
1032 /* Casus Belli for when the action is completed. */
1033 /* TODO: is it more logical to put Casus_Belli_Success here, change
1034 * Casus_Belli_Complete to Casus_Belli_Successful_Beginning and
1035 * trigger it when an activity successfully has began? */
1038 act_utype,
1042 }
1043 }
1044}
1045
1046/**********************************************************************/
1054
1055/**********************************************************************/
1060{
1061 switch (activity) {
1062 case ACTIVITY_PILLAGE:
1063 /* Can be set server side. */
1064 return FALSE;
1065 default:
1066 return activity_requires_target(activity);
1067 }
1068}
1069
1070/**********************************************************************/
1080 enum unit_activity *activity,
1081 struct extra_type **target)
1082{
1083 const struct civ_map *nmap = &(wld.map);
1084
1085 if (*activity == ACTIVITY_PILLAGE
1086 && *target == NULL) {
1087 struct tile *ptile = unit_tile(punit);
1088 struct extra_type *tgt;
1089 bv_extras extras = *tile_extras(ptile);
1090
1091 while ((tgt = get_preferred_pillage(extras))) {
1092
1093 BV_CLR(extras, extra_index(tgt));
1094
1095 if (can_unit_do_activity_targeted(nmap, punit, *activity, tgt)) {
1096 *target = tgt;
1097 return;
1098 }
1099 }
1100
1101 /* Nothing we can pillage here. */
1102 *activity = ACTIVITY_IDLE;
1103 }
1104}
1105
1106/**********************************************************************/
1111 struct player *powner,
1112 struct unit_type *u_type,
1113 int sq_radius,
1114 struct tile **dst_tile)
1115{
1116 int bestvalue = 0;
1117 struct civ_map *nmap = &(wld.map);
1118
1119 /* coords of best tile in arg pointers */
1121 int value;
1122
1123 if (!is_native_tile(u_type, ptile)) {
1124 continue;
1125 }
1126
1127 if (NULL != tile_city(ptile)) {
1128 continue;
1129 }
1130
1131 if (0 < unit_list_size(ptile->units)) {
1132 continue;
1133 }
1134
1135 /* City may not have changed hands yet; see place_partisans(). */
1136 value = get_virtual_defense_power(nmap, NULL, u_type, powner,
1137 ptile, FALSE, 0);
1138 value *= 10;
1139
1140 if (tile_continent(ptile) != tile_continent(pcenter)) {
1141 value /= 2;
1142 }
1143
1144 value -= fc_rand(value/3);
1145
1146 if (value > bestvalue) {
1147 *dst_tile = ptile;
1148 bestvalue = value;
1149 }
1151
1152 return bestvalue > 0;
1153}
1154
1155/**********************************************************************/
1158void place_partisans(struct tile *pcenter, struct player *powner,
1159 int count, int sq_radius)
1160{
1161 struct tile *ptile = NULL;
1163 const struct civ_map *nmap = &(wld.map);
1164
1165 while (count-- > 0
1167 sq_radius, &ptile)) {
1168 struct unit *punit;
1169
1170 punit = unit_virtual_prepare(powner, ptile, u_type, 0, 0, -1, -1);
1172 punit->activity = ACTIVITY_FORTIFIED; /* Yes; directly fortified */
1173 }
1174
1175 (void) place_unit(punit, powner, NULL, NULL, FALSE);
1176 }
1177}
1178
1179/**********************************************************************/
1184bool teleport_unit_to_city(struct unit *punit, struct city *pcity,
1185 int move_cost, bool verbose)
1186{
1187 struct tile *src_tile = unit_tile(punit), *dst_tile = pcity->tile;
1188
1189 if (city_owner(pcity) == unit_owner(punit)) {
1190 log_verbose("Teleported %s %s from (%d,%d) to %s",
1192 unit_rule_name(punit), TILE_XY(src_tile), city_name_get(pcity));
1193 if (verbose) {
1196 _("Teleported your %s to %s."),
1198 city_link(pcity));
1199 }
1200
1201 /* Silently free orders since they won't be applicable anymore. */
1203
1204 if (move_cost == -1) {
1205 move_cost = punit->moves_left;
1206 }
1207 unit_move(punit, dst_tile, move_cost,
1209
1210 return TRUE;
1211 }
1212 return FALSE;
1213}
1214
1215/**********************************************************************/
1222void bounce_unit(struct unit *punit, bool verbose)
1223{
1224 struct player *pplayer;
1225 struct tile *punit_tile;
1226 int count = 0;
1227
1228 /* I assume that there are no topologies that have more than
1229 * (2d + 1)^2 tiles in the "square" of "radius" d. */
1230#define DIST 2
1231 struct tile *tiles[(2 * DIST + 1) * (2 * DIST + 1)];
1232
1233 if (!punit) {
1234 return;
1235 }
1236
1237 pplayer = unit_owner(punit);
1239
1240 square_iterate(&(wld.map), punit_tile, DIST, ptile) {
1241 if (count >= ARRAY_SIZE(tiles)) {
1242 break;
1243 }
1244
1245 if (ptile == punit_tile) {
1246 continue;
1247 }
1248
1249 if (can_unit_survive_at_tile(&(wld.map), punit, ptile)
1250 && !is_non_allied_city_tile(ptile, pplayer)
1251 && !is_non_allied_unit_tile(ptile, pplayer)) {
1252 tiles[count++] = ptile;
1253 }
1255
1256 if (count > 0) {
1257 struct tile *ptile = tiles[fc_rand(count)];
1258
1259 if (verbose) {
1260 notify_player(pplayer, ptile, E_UNIT_RELOCATED, ftc_server,
1261 /* TRANS: A unit is moved to resolve stack conflicts. */
1262 _("Moved your %s."),
1263 unit_link(punit));
1264 }
1265
1266 /* TODO: should a unit be able to bounce to a transport like is done
1267 * below? What if the unit can't legally enter the transport, say
1268 * because the transport is Unreachable and the unit doesn't have it in
1269 * its embarks field or because "Transport Embark" isn't enabled? Kept
1270 * like it was to preserve the old rules for now. -- Sveinung */
1271 unit_move(punit, ptile, 0, NULL, TRUE, FALSE, FALSE, FALSE, FALSE);
1272 return;
1273 }
1274
1275 /* Didn't find a place to bounce the unit, going to disband it.
1276 * Try to bounce transported units. */
1278 struct unit_list *pcargo_units;
1279
1282 bounce_unit(pcargo, verbose);
1284 }
1285
1286 if (verbose) {
1288 /* TRANS: A unit is disbanded to resolve stack conflicts. */
1289 _("Disbanded your %s."),
1291 }
1292
1294}
1295#undef DIST
1296
1297/**********************************************************************/
1302static void throw_units_from_illegal_cities(struct player *pplayer,
1303 bool verbose)
1304{
1305 struct tile *ptile;
1306 struct city *pcity;
1307 struct unit *ptrans;
1308 struct unit_list *pcargo_units;
1309
1310 /* Unload undesired units from transports, if possible. */
1311 unit_list_iterate(pplayer->units, punit) {
1312 ptile = unit_tile(punit);
1313 pcity = tile_city(ptile);
1314 if (NULL != pcity
1315 && !pplayers_allied(city_owner(pcity), pplayer)
1319 if (!pplayers_allied(unit_owner(pcargo), pplayer)) {
1320 if (can_unit_exist_at_tile(&(wld.map), pcargo, ptile)) {
1322 }
1323 }
1325 }
1327
1328 /* Bounce units except transported ones which will be bounced with their
1329 * transport. */
1331 ptile = unit_tile(punit);
1332 pcity = tile_city(ptile);
1333 if (NULL != pcity
1334 && !pplayers_allied(city_owner(pcity), pplayer)) {
1336 if (NULL == ptrans || pplayer != unit_owner(ptrans)) {
1337 bounce_unit(punit, verbose);
1338 }
1339 }
1341
1342#ifdef FREECIV_DEBUG
1343 /* Sanity check. */
1344 unit_list_iterate(pplayer->units, punit) {
1345 ptile = unit_tile(punit);
1346 pcity = tile_city(ptile);
1347 fc_assert_msg(NULL == pcity
1348 || pplayers_allied(city_owner(pcity), pplayer),
1349 "Failed to throw %s %d from %s %d (%d, %d)",
1351 punit->id,
1352 city_name_get(pcity),
1353 pcity->id,
1354 TILE_XY(ptile));
1356#endif /* FREECIV_DEBUG */
1357}
1358
1359/**********************************************************************/
1367static void resolve_stack_conflicts(struct player *pplayer,
1368 struct player *aplayer, bool verbose)
1369{
1371 struct tile *ptile = unit_tile(punit);
1372
1373 if (is_non_allied_unit_tile(ptile, pplayer)) {
1375 if (unit_owner(aunit) == pplayer
1376 || unit_owner(aunit) == aplayer
1377 || !can_unit_survive_at_tile(&(wld.map), aunit, ptile)) {
1378 bounce_unit(aunit, verbose);
1379 }
1381 }
1383}
1384
1385/**********************************************************************/
1396void resolve_unit_stacks(struct player *pplayer, struct player *aplayer,
1397 bool verbose)
1398{
1399 throw_units_from_illegal_cities(pplayer, verbose);
1401
1402 resolve_stack_conflicts(pplayer, aplayer, verbose);
1403 resolve_stack_conflicts(aplayer, pplayer, verbose);
1404}
1405
1406/**********************************************************************/
1411struct unit_list *get_units_seen_via_ally(const struct player *pplayer,
1412 const struct player *aplayer)
1413{
1414 struct unit_list *seen_units = unit_list_new();
1415
1416 /* Anybody's units inside ally's cities */
1417 city_list_iterate(aplayer->cities, pcity) {
1418 unit_list_iterate(city_tile(pcity)->units, punit) {
1419 if (can_player_see_unit(pplayer, punit)) {
1421 }
1424
1425 /* Ally's own units inside transports */
1427 if (unit_transported(punit) && can_player_see_unit(pplayer, punit)) {
1429 }
1431
1432 /* Make sure the same unit is not added in multiple phases
1433 * (unit within transport in a city) */
1435
1436 return seen_units;
1437}
1438
1439/**********************************************************************/
1446void remove_allied_visibility(struct player *pplayer, struct player *aplayer,
1447 const struct unit_list *seen_units)
1448{
1450 /* We need to hide units previously seen by the client. */
1451 if (!can_player_see_unit(pplayer, punit)) {
1452 unit_goes_out_of_sight(pplayer, punit);
1453 }
1455
1456 city_list_iterate(aplayer->cities, pcity) {
1457 /* The player used to know what units were in these cities. Now that
1458 * they don't, they need to get a new short city packet updating the
1459 * occupied status. */
1460 if (map_is_known_and_seen(pcity->tile, pplayer, V_MAIN)) {
1461 send_city_info(pplayer, pcity);
1462 }
1464}
1465
1466/**********************************************************************/
1470void give_allied_visibility(struct player *pplayer,
1471 struct player *aplayer)
1472{
1474 if (can_player_see_unit(pplayer, punit)) {
1475 send_unit_info(pplayer->connections, punit);
1476 }
1478}
1479
1480/**********************************************************************/
1483static bool is_refuel_tile(const struct tile *ptile,
1484 const struct player *pplayer,
1485 const struct unit *punit)
1486{
1487 const struct unit_type *utype;
1488 const struct unit_class *pclass;
1489
1490 if (is_allied_city_tile(ptile, pplayer)) {
1491 return TRUE;
1492 }
1493
1494 utype = unit_type_get(punit);
1495 if (utype_has_flag(utype, UTYF_COAST) && is_safe_ocean(&(wld.map), ptile)) {
1496 return TRUE;
1497 }
1498
1499 pclass = utype_class(utype);
1500 if (NULL != pclass->cache.refuel_extras) {
1501 const struct player_tile *plrtile = map_get_player_tile(ptile, pplayer);
1502
1503 extra_type_list_iterate(pclass->cache.refuel_extras, pextra) {
1504 if (BV_ISSET(plrtile->extras, extra_index(pextra))) {
1505 return TRUE;
1506 }
1508 }
1509
1510 return FALSE;
1511}
1512
1513/**********************************************************************/
1517{
1518 return unit_transported(punit) /* Carrier */
1520}
1521
1522/**********************************************************************/
1525bool is_refuel_point(const struct tile *ptile,
1526 const struct player *pplayer,
1527 const struct unit *punit)
1528{
1529 if (is_non_allied_unit_tile(ptile, pplayer)) {
1530 return FALSE;
1531 }
1532
1533 return is_refuel_tile(ptile, pplayer, punit) || unit_could_load_at(punit, ptile);
1534}
1535
1536/**********************************************************************/
1547void transform_unit(struct unit *punit, const struct unit_type *to_unit,
1548 int vet_loss)
1549{
1550 struct player *pplayer = unit_owner(punit);
1551 const struct unit_type *old_type = punit->utype;
1553 int old_hp = unit_type_get(punit)->hp;
1554 int lvls;
1555
1556 punit->utype = to_unit;
1557
1558 /* New type may not have the same veteran system, and we may want to
1559 * knock some levels off. */
1562 /* Keeping the old behavior, so first clip top, then reduce */
1564
1565 /* Scale HP and MP, rounding down. Be careful with integer arithmetic,
1566 * and don't kill the unit. unit_move_rate() is used to take into account
1567 * global effects like Magellan's Expedition. */
1568 punit->hp = MAX(punit->hp * unit_type_get(punit)->hp / old_hp, 1);
1569 if (old_mr == 0) {
1571 } else {
1573 }
1574
1576
1580 }
1581
1582 /* Update unit upkeep */
1584
1586
1588
1589 CALL_PLR_AI_FUNC(unit_transformed, pplayer, punit, old_type);
1591
1594}
1595
1596/**********************************************************************/
1599struct unit *create_unit(struct player *pplayer, struct tile *ptile,
1600 const struct unit_type *type, int veteran_level,
1601 int homecity_id, int moves_left)
1602{
1603 return create_unit_full(pplayer, ptile, type, veteran_level, homecity_id,
1604 moves_left, -1, NULL);
1605}
1606
1607/**********************************************************************/
1611{
1612 if (punit->homecity != 0) {
1614
1617 }
1618 }
1619}
1620
1621/**********************************************************************/
1626struct unit *create_unit_full(struct player *pplayer, struct tile *ptile,
1627 const struct unit_type *type, int veteran_level,
1628 int homecity_id, int moves_left, int hp_left,
1629 struct unit *ptrans)
1630{
1631 struct unit *punit
1632 = unit_virtual_prepare(pplayer, ptile, type, veteran_level,
1634 struct city *pcity = (!homecity_id || utype_has_flag(type, UTYF_NOHOME))
1636 bool could_place;
1637
1639 could_place = place_unit(punit, pplayer, pcity, ptrans, FALSE);
1641 if (!could_place) {
1643 punit = NULL;
1644 }
1645
1646 return punit;
1647}
1648
1649/**********************************************************************/
1659struct unit *unit_virtual_prepare(struct player *pplayer, struct tile *ptile,
1660 const struct unit_type *type,
1661 int veteran_level, int homecity_id,
1662 int moves_left, int hp_left)
1663{
1664 struct unit *punit;
1665
1666 fc_assert_ret_val(ptile != NULL, NULL);
1668 unit_tile_set(punit, ptile);
1669
1671 punit->homecity = 0; /* none */
1672 } else {
1674 }
1675
1676 if (hp_left >= 0) {
1677 /* Override default full HP */
1678 punit->hp = hp_left;
1679 }
1680
1681 if (moves_left >= 0) {
1682 int mr = unit_move_rate(punit);
1683
1684 /* Override default full MP */
1685 /* FIXME: there are valid situations when a unit have mp
1686 * over its move rate. Here, keeping the old behavior. */
1688 /* Assume that if moves_left < 0 then the unit is "fresh",
1689 * and not moved; else the unit has had something happen
1690 * to it (eg, bribed) which we treat as equivalent to moved.
1691 * (Otherwise could pass moved arg too...) --dwp */
1692 punit->moved = TRUE;
1693 }
1694
1695 return punit;
1696}
1697
1698/**********************************************************************/
1707bool place_unit(struct unit *punit, struct player *pplayer,
1708 struct city *pcity, struct unit *ptrans, bool force)
1709{
1710 struct tile *ptile;
1711
1712 fc_assert_ret_val(pplayer, FALSE);
1714 ptile = punit->tile;
1715 fc_assert_ret_val(ptile, FALSE);
1716
1717 /* Register unit */
1720
1721 if (ptrans) {
1722 /* Set transporter for unit. */
1724 }
1725
1727 || can_unit_exist_at_tile(&(wld.map), punit, ptile),
1728 FALSE);
1729
1730 unit_list_prepend(pplayer->units, punit);
1731 unit_list_prepend(ptile->units, punit);
1733 if (pcity && !unit_has_type_flag(punit, UTYF_NOHOME)) {
1734 fc_assert(punit->homecity == pcity->id);
1735 fc_assert(city_owner(pcity) == pplayer);
1737 /* update unit upkeep */
1738 city_units_upkeep(pcity);
1739 /* Refresh the unit's homecity. */
1740 city_refresh(pcity);
1741 send_city_info(pplayer, pcity);
1742 }
1743
1744 punit->server.vision = vision_new(pplayer, ptile);
1746
1748
1751
1752 /* The unit may have changed the available tiles in nearby cities. */
1754 sync_cities();
1755
1756 CALL_FUNC_EACH_AI(unit_created, punit);
1757 CALL_PLR_AI_FUNC(unit_got, pplayer, punit);
1758
1759 return TRUE;
1760}
1761
1762/**********************************************************************/
1766 void (*callback)(struct unit *punit))
1767{
1768 /* Tried to overwrite another call back. If this assertion is triggered
1769 * in a case where two call back are needed it may be time to support
1770 * more than one unit removal call back at a time. */
1772
1774}
1775
1776/**********************************************************************/
1783
1784/**********************************************************************/
1788static void server_remove_unit_full(struct unit *punit, bool transported,
1790{
1791 struct packet_unit_remove packet;
1792 struct tile *ptile = unit_tile(punit);
1793 struct city *pcity = tile_city(ptile);
1795 struct unit *ptrans;
1796 struct player *pplayer = unit_owner(punit);
1797
1798 /* The unit is doomed. */
1800
1801#if defined(FREECIV_DEBUG) && !defined(FREECIV_NDEBUG)
1802 unit_list_iterate(ptile->units, pcargo) {
1805#endif /* FREECIV_DEBUG */
1806
1807 CALL_PLR_AI_FUNC(unit_lost, pplayer, punit);
1808 CALL_FUNC_EACH_AI(unit_destroyed, punit);
1809
1810 /* Save transporter for updating below. */
1812 /* Unload unit. */
1814
1815 /* Since settlers plot in new cities in the minimap before they
1816 are built, so that no two settlers head towards the same city
1817 spot, we need to ensure this reservation is cleared should
1818 the settler disappear on the way. */
1820
1821 /* Clear the vision before sending unit remove. Else, we might duplicate
1822 * the PACKET_UNIT_REMOVE if we lose vision of the unit tile. */
1823 if (punit->server.vision != NULL) {
1827 }
1828
1829 packet.unit_id = punit->id;
1830 /* Send to onlookers. */
1833 transported)) {
1834 lsend_packet_unit_remove(aplayer->connections, &packet);
1835 }
1837 /* Send to global observers. */
1841 }
1843
1844 if (punit->server.moving != NULL) {
1845 /* Do not care of this unit for running moves. */
1847 }
1848
1850 /* Run the unit removal call back. */
1852 }
1853
1854 /* check if this unit had UTYF_GAMELOSS flag */
1856 && ULR_EDITOR != reason) {
1858 _("Unable to defend %s, %s has lost the game."),
1860 player_name(pplayer));
1861 notify_player(pplayer, ptile, E_GAME_END, ftc_server,
1862 _("Losing %s meant losing the game! "
1863 "Be more careful next time!"),
1864 unit_link(punit));
1866 }
1867
1870
1873 punit = NULL;
1874
1875 if (NULL != ptrans) {
1876 /* Update the occupy info. */
1878 }
1879
1880 /* This unit may have blocked tiles of adjacent cities. Update them. */
1882 sync_cities();
1883
1884 if (phomecity) {
1887 }
1888
1889 if (pcity && pcity != phomecity) {
1890 city_refresh(pcity);
1891 send_city_info(city_owner(pcity), pcity);
1892 }
1893
1894 if (pcity && unit_list_size(ptile->units) == 0) {
1895 /* The last unit in the city was killed: update the occupied flag. */
1896 send_city_info(NULL, pcity);
1897 }
1898}
1899
1900/**********************************************************************/
1909
1910/**********************************************************************/
1913static void unit_lost_with_transport(const struct player *pplayer,
1914 struct unit *pcargo,
1915 const struct unit_type *ptransport,
1916 struct player *killer)
1917{
1919 _("%s lost when %s was lost."),
1922 /* Unit is not transported any more at this point, but it has jumped
1923 * off the transport and drowns outside. So it must be removed from
1924 * all clients.
1925 * However, we don't know if given client has received ANY updates
1926 * about the swimming unit, and we can't remove it if it's not there
1927 * in the first place -> we send it once here just to be sure it's
1928 * there. */
1931}
1932
1933/**********************************************************************/
1937static void wipe_unit_full(struct unit *punit, bool transported,
1939 struct player *killer)
1940{
1941 struct tile *ptile = unit_tile(punit);
1942 struct player *pplayer = unit_owner(punit);
1943 const struct unit_type *putype_save = unit_type_get(punit); /* for notify messages */
1944 struct unit_list *helpless = unit_list_new();
1945 struct unit_list *imperiled = unit_list_new();
1946 struct unit_list *unsaved = unit_list_new();
1948 struct city *pexclcity;
1949 struct civ_map *nmap = &(wld.map);
1950
1951 if (killer != NULL
1954 player_loot_player(killer, pplayer);
1955 }
1956
1957 /* The unit is doomed. */
1959
1960 /* If a unit is being lost due to loss of its city, ensure that we don't
1961 * try to teleport any of its cargo to that city (which may not yet
1962 * have changed hands or disappeared). (It is assumed that the unit's
1963 * home city is always the one that is being lost/transferred/etc.) */
1964 if (reason == ULR_CITY_LOST) {
1966 } else {
1967 pexclcity = NULL;
1968 }
1969
1970 /* Remove unit itself from its transport */
1971 if (ptrans != NULL) {
1974 }
1975
1976 /* First pull all units off of the transporter. */
1978 /* Use iterate_safe as unloaded units will be removed from the list
1979 * while iterating. */
1981 bool healthy = FALSE;
1982
1983 if (!can_unit_unload(pcargo, punit)) {
1985 } else {
1986 if (!can_unit_exist_at_tile(nmap, pcargo, ptile)) {
1988 } else {
1989 /* These units do not need to be saved. */
1990 healthy = TRUE;
1991 }
1992 }
1993
1994 /* Could use unit_transport_unload_send() here, but that would
1995 * call send_unit_info() for the transporter unnecessarily.
1996 * Note that this means that unit might to get seen briefly
1997 * by clients other than owner's, for example as a result of
1998 * update of homecity common to this cargo and some other
1999 * destroyed unit. */
2001 if (pcargo->activity == ACTIVITY_SENTRY) {
2002 /* Activate sentried units - like planes on a disbanded carrier.
2003 * Note this will activate ground units even if they just change
2004 * transporter. */
2006 }
2007
2008 /* Unit info for unhealthy units will be sent when they are
2009 * assigned new transport or removed. */
2010 if (healthy) {
2012 }
2014 }
2015
2016 /* Now remove the unit. */
2017 server_remove_unit_full(punit, transported, reason);
2018
2019 switch (reason) {
2020 case ULR_KILLED:
2021 case ULR_EXECUTED:
2022 case ULR_SDI:
2023 case ULR_NUKE:
2024 case ULR_BRIBED:
2025 case ULR_CAPTURED:
2026 case ULR_CAUGHT:
2027 case ULR_ELIMINATED:
2028 case ULR_TRANSPORT_LOST:
2029 if (killer != NULL) {
2030 killer->score.units_killed++;
2031 }
2032 pplayer->score.units_lost++;
2033 break;
2034 case ULR_BARB_UNLEASH:
2035 case ULR_CITY_LOST:
2036 case ULR_STARVED:
2037 case ULR_UPKEEP:
2038 case ULR_NONNATIVE_TERR:
2039 case ULR_ARMISTICE:
2040 case ULR_HP_LOSS:
2041 case ULR_FUEL:
2042 case ULR_STACK_CONFLICT:
2043 case ULR_SOLD:
2044 pplayer->score.units_lost++;
2045 break;
2046 case ULR_RETIRED:
2047 case ULR_DISBANDED:
2048 case ULR_USED:
2049 case ULR_DETONATED:
2050 case ULR_MISSILE:
2051 pplayer->score.units_used++;
2052 break;
2053 case ULR_EDITOR:
2054 case ULR_PLAYER_DIED:
2055 break;
2056 }
2057
2058 /* First, sort out helpless cargo. */
2059 if (unit_list_size(helpless) > 0) {
2060 struct unit_list *remaining = unit_list_new();
2061
2062 /* Grant priority to gameloss units and units with the EvacuateFirst
2063 * unit type flag. */
2070 pexclcity)) {
2072 }
2073 } else {
2074 unit_list_prepend(remaining, pcargo);
2075 }
2077
2078 /* Handle non-priority units. */
2079 unit_list_iterate_safe(remaining, pcargo) {
2082 }
2084
2085 unit_list_destroy(remaining);
2086 }
2088
2089 /* Then, save any imperiled cargo. */
2090 if (unit_list_size(imperiled) > 0) {
2091 struct unit_list *remaining = unit_list_new();
2092
2093 /* Grant priority to gameloss units and units with the EvacuateFirst
2094 * unit type flag. */
2101 pexclcity)) {
2103 }
2104 } else {
2105 unit_list_prepend(remaining, pcargo);
2106 }
2108
2109 /* Handle non-priority units. */
2110 unit_list_iterate_safe(remaining, pcargo) {
2113 }
2115
2116 unit_list_destroy(remaining);
2117 }
2119
2120 /* Finally, kill off the unsaved units. */
2121 if (unit_list_size(unsaved) > 0) {
2125 }
2127}
2128
2129/**********************************************************************/
2138
2139/**********************************************************************/
2145static bool try_to_save_unit(struct unit *punit, const struct unit_type *pttype,
2146 bool helpless, bool teleporting,
2147 const struct city *pexclcity)
2148{
2149 struct tile *ptile = unit_tile(punit);
2150 struct player *pplayer = unit_owner(punit);
2152
2153 /* Helpless units cannot board a transport in their current state. */
2154 if (!helpless
2155 && ptransport != NULL) {
2158 return TRUE;
2159 } else {
2160 /* Only units that cannot find transport are considered for teleport. */
2161 if (teleporting) {
2162 struct city *pcity = find_closest_city(ptile, pexclcity,
2166 if (pcity != NULL) {
2167 char tplink[MAX_LEN_LINK]; /* In case unit dies when teleported */
2168
2170
2171 if (teleport_unit_to_city(punit, pcity, 0, FALSE)) {
2172 notify_player(pplayer, ptile, E_UNIT_RELOCATED, ftc_server,
2173 _("%s escaped the destruction of %s, and fled to %s."),
2174 tplink,
2176 city_link(pcity));
2177 return TRUE;
2178 }
2179 }
2180 }
2181 }
2182
2183 /* The unit could not use transport on the tile, and could not teleport. */
2184 return FALSE;
2185}
2186
2187/**********************************************************************/
2193struct unit *unit_change_owner(struct unit *punit, struct player *pplayer,
2195{
2196 struct unit *gained_unit;
2197 int id = 0;
2198
2199#ifndef FREECIV_NDEBUG
2200 bool placed;
2201#endif
2202
2205
2206 /* Convert the unit to your cause. It's supposed that the original unit
2207 * is on a valid tile and is not transported. */
2211 fc_assert_action(gained_unit, goto uco_wipe); /* Tile must be valid */
2212
2213 /* Owner changes, nationality not. */
2214 gained_unit->nationality = punit->nationality;
2215
2216 /* Copy some more unit fields */
2217 gained_unit->fuel = punit->fuel;
2218 gained_unit->paradropped = punit->paradropped;
2219 gained_unit->birth_turn = punit->birth_turn;
2220 gained_unit->current_form_turn = punit->current_form_turn;
2221
2222 /* Fog is lifted in the placing algorithm. */
2223#ifndef FREECIV_NDEBUG
2224 placed =
2225#endif
2226 place_unit(gained_unit, pplayer,
2228 NULL, FALSE);
2229
2231
2232 id = gained_unit->id;
2233
2234 /* Update unit upkeep in the new homecity */
2235 if (homecity > 0) {
2237 }
2238
2239 /* Be sure to wipe the converted unit! */
2240 /* Old homecity upkeep is updated in process */
2241#ifndef FREECIV_NDEBUG
2242 uco_wipe:
2243#endif
2244
2246
2247 if (!unit_is_alive(id)) {
2248 /* Destroyed by a script */
2249 return NULL;
2250 }
2251
2252 return gained_unit; /* Returns the replacement. */
2253}
2254
2255/**********************************************************************/
2260void kill_unit(struct unit *pkiller, struct unit *punit, bool vet)
2261{
2263 struct player *pvictim = unit_owner(punit);
2264 struct player *pvictor = unit_owner(pkiller);
2265 struct tile *deftile = unit_tile(punit);
2266 int unitcount = 0;
2267 bool escaped;
2268 const struct civ_map *nmap = &(wld.map);
2269
2272
2273 /* The unit is doomed. */
2275
2279 unitcount++;
2280 }
2282
2283 if (!is_stack_vulnerable(deftile) || unitcount == 1) {
2284 if (vet) {
2286 }
2288 } else { /* Unitcount > 1 */
2289 int i;
2290 int slots = player_slot_count();
2291 int num_killed[slots];
2292 int num_escaped[slots];
2293 struct unit *other_killed[slots];
2294
2295 fc_assert(unitcount > 1);
2296
2297 /* Initialize */
2298 for (i = 0; i < slots; i++) {
2299 num_killed[i] = 0;
2300 other_killed[i] = NULL;
2301 num_escaped[i] = 0;
2302 }
2303
2304 /* Count killed units */
2306 struct player *vplayer = unit_owner(vunit);
2307
2310 escaped = FALSE;
2311
2314 && vunit->hp > 0
2315 && vunit->moves_left > pkiller->moves_left
2316 && fc_rand(2)) {
2317 int curr_def_bonus;
2318 int def_bonus = 0;
2319 struct tile *dsttile = NULL;
2320 int move_cost;
2321
2322 fc_assert(vunit->hp > 0);
2323
2325 if (can_exist_at_tile(nmap, vunit->utype, ptile2)
2326 && NULL == tile_city(ptile2)) {
2327 move_cost = map_move_cost_unit(nmap, vunit, ptile2);
2328 if (pkiller->moves_left <= vunit->moves_left - move_cost
2330 || unit_list_size(ptile2->units)) == 0) {
2332 vunit->utype);
2333 if (def_bonus <= curr_def_bonus) {
2335 dsttile = ptile2;
2336 }
2337 }
2338 }
2340
2341 if (dsttile != NULL) {
2344 NULL, NULL, dsttile,
2346 NULL, NULL)
2347 != NULL);
2348
2349 if (escaped) {
2351 unitcount--;
2352 }
2353 }
2354 }
2355
2356 if (!escaped) {
2358
2359 if (vunit != punit) {
2362 }
2363 }
2364 }
2366
2367 /* Inform the destroyer again if more than one unit was killed */
2368 if (unitcount > 1) {
2370 /* TRANS: "... Cannon ... the Polish Destroyer ...." */
2371 PL_("Your attacking %s succeeded against the %s %s "
2372 "(and %d other unit)!",
2373 "Your attacking %s succeeded against the %s %s "
2374 "(and %d other units)!", unitcount - 1),
2377 punit_link,
2378 unitcount - 1);
2379 }
2380
2381 if (vet) {
2383 }
2384
2385 /* Inform the owners: this only tells about owned units that were killed.
2386 * there may have been 20 units who died but if only 2 belonged to the
2387 * particular player they'll only learn about those.
2388 *
2389 * Also if a large number of units die you don't find out what type
2390 * they all are. */
2391 for (i = 0; i < slots; i++) {
2392 if (num_killed[i] == 1) {
2393 if (i == player_index(pvictim)) {
2397 /* TRANS: "Cannon ... the Polish Destroyer." */
2398 _("%s lost to an attack by the %s %s."),
2399 punit_link,
2401 pkiller_link);
2402 } else {
2406 /* TRANS: "Cannon lost when the Polish Destroyer
2407 * attacked the German Musketeers." */
2408 _("%s lost when the %s %s attacked the %s %s."),
2413 punit_link);
2414 }
2415 } else if (num_killed[i] > 1) {
2416 if (i == player_index(pvictim)) {
2417 int others = num_killed[i] - 1;
2418
2419 if (others == 1) {
2422 /* TRANS: "Musketeers (and Cannon) lost to an
2423 * attack from the Polish Destroyer." */
2424 _("%s (and %s) lost to an attack from the %s %s."),
2425 punit_link,
2428 pkiller_link);
2429 } else {
2432 /* TRANS: "Musketeers and 3 other units lost to
2433 * an attack from the Polish Destroyer."
2434 * (only happens with at least 2 other units) */
2435 PL_("%s and %d other unit lost to an attack "
2436 "from the %s %s.",
2437 "%s and %d other units lost to an attack "
2438 "from the %s %s.", others),
2439 punit_link,
2440 others,
2442 pkiller_link);
2443 }
2444 } else {
2447 /* TRANS: "2 units lost when the Polish Destroyer
2448 * attacked the German Musketeers."
2449 * (only happens with at least 2 other units) */
2450 PL_("%d unit lost when the %s %s attacked the %s %s.",
2451 "%d units lost when the %s %s attacked the %s %s.",
2452 num_killed[i]),
2453 num_killed[i],
2457 punit_link);
2458 }
2459 }
2460 }
2461
2462 /* Inform the owner of the units that escaped.
2463 * 'deftile' is the original tile they defended at, not where
2464 * they escaped to, as there might be multiple different tiles
2465 * different units escaped to. */
2466 for (i = 0; i < slots; i++) {
2467 if (0 < num_escaped[i]) {
2470 PL_("%d unit escaped from attack by %s %s",
2471 "%d units escaped from attack by %s %s",
2472 num_escaped[i]),
2473 num_escaped[i],
2476 );
2477 }
2478 }
2479
2480 /* Remove the units - note the logic of which units actually die
2481 * must be mimicked exactly in at least one place up above. */
2482 punit = NULL; /* Wiped during following iteration so unsafe to use */
2483
2488 }
2490 }
2491}
2492
2493/**********************************************************************/
2496void collect_ransom(struct unit *pcollector, struct unit *punit, bool vet)
2497{
2498 struct player *pvictim = unit_owner(punit);
2501 struct tile *deftile = unit_tile(punit);
2502 int unitcount, ransom;
2503
2504 /* The unit is doomed. */
2506
2509
2510 if (pvictim->economic.gold < ransom) {
2511 ransom = pvictim->economic.gold;
2512 }
2513
2515 PL_("%d Barbarian leader captured.",
2516 "%d Barbarian leaders captured.",
2517 unitcount),
2518 unitcount);
2520 PL_("%d gold ransom paid.",
2521 "%d gold ransom paid.",
2522 ransom),
2523 ransom);
2524 pvictor->economic.gold += ransom;
2525 pvictim->economic.gold -= ransom;
2526 send_player_info_c(pvictor, NULL); /* Let me see my new gold :-) */
2527
2528 if (vet) {
2530 }
2531
2534
2535 if (!is_stack_vulnerable(deftile) || unitcount == 1) {
2537 } else {
2538 int i;
2539 int slots = player_slot_count();
2540 int num_collected[slots];
2541
2542 fc_assert(unitcount > 1);
2543
2544 /* Initialize */
2545 for (i = 0; i < slots; i++) {
2546 num_collected[i] = 0;
2547 }
2548
2549 /* Count captured units */
2551 struct player *vplayer = unit_owner(vunit);
2552
2555
2556 for (i = 0; i < slots; i++) {
2557 if (num_collected[i] == 1) {
2560 _("%s %s collected ransom of %s."),
2563 } else if (num_collected[i] > 1) {
2566 PL_("%s %s collected ransom of %s and %d other unit.",
2567 "%s %s collected ransom of %s and %d other units.",
2568 num_collected[i] - 1),
2571 punit_link,
2572 num_collected[i] - 1);
2573 }
2574 }
2575
2579 }
2580}
2581
2582/**********************************************************************/
2586void package_unit(struct unit *punit, struct packet_unit_info *packet)
2587{
2588 packet->id = punit->id;
2589 packet->owner = player_number(unit_owner(punit));
2591 packet->tile = tile_index(unit_tile(punit));
2592 packet->facing = punit->facing;
2593 packet->homecity = punit->homecity;
2595 packet->upkeep[o] = punit->upkeep[o];
2597 packet->veteran = punit->veteran;
2599 packet->movesleft = punit->moves_left;
2600 packet->hp = punit->hp;
2601 packet->activity = punit->activity;
2603
2604 if (punit->activity_target != NULL) {
2606 } else {
2607 packet->activity_tgt = EXTRA_NONE;
2608 }
2609
2610 packet->changed_from = punit->changed_from;
2612
2613 if (punit->changed_from_target != NULL) {
2615 } else {
2616 packet->changed_from_tgt = EXTRA_NONE;
2617 }
2618
2620 packet->fuel = punit->fuel;
2621 packet->goto_tile = (NULL != punit->goto_tile
2622 ? tile_index(punit->goto_tile) : -1);
2623 packet->paradropped = punit->paradropped;
2624 packet->done_moving = punit->done_moving;
2625 packet->stay = punit->stay;
2626 packet->birth_turn = punit->birth_turn;
2628 if (!unit_transported(punit)) {
2629 packet->transported = FALSE;
2630 packet->transported_by = 0;
2631 } else {
2632 packet->transported = TRUE;
2634 }
2635 if (punit->carrying != NULL) {
2636 packet->carrying = goods_index(punit->carrying);
2637 } else {
2638 packet->carrying = -1;
2639 }
2640 packet->occupied = (get_transporter_occupancy(punit) > 0);
2641 packet->battlegroup = punit->battlegroup;
2642 packet->has_orders = punit->has_orders;
2643 if (punit->has_orders) {
2644 packet->orders_length = punit->orders.length;
2645 packet->orders_index = punit->orders.index;
2646 packet->orders_repeat = punit->orders.repeat;
2648 memcpy(packet->orders, punit->orders.list,
2649 punit->orders.length * sizeof(struct unit_order));
2650 } else {
2651 packet->orders_length = packet->orders_index = 0;
2652 packet->orders_repeat = packet->orders_vigilant = FALSE;
2653 /* No need to initialize array. */
2654 }
2655
2660}
2661
2662/**********************************************************************/
2668 struct packet_unit_short_info *packet,
2669 enum unit_info_use packet_use, int info_city_id)
2670{
2671 packet->packet_use = packet_use;
2672 packet->info_city_id = info_city_id;
2673
2674 packet->id = punit->id;
2675 packet->owner = player_number(unit_owner(punit));
2676 packet->tile = tile_index(unit_tile(punit));
2677 packet->facing = punit->facing;
2678 packet->veteran = punit->veteran;
2680 packet->hp = punit->hp;
2681 packet->occupied = (get_transporter_occupancy(punit) > 0);
2683 || punit->activity == ACTIVITY_GOTO) {
2684 packet->activity = ACTIVITY_IDLE;
2685 } else {
2686 packet->activity = punit->activity;
2687 }
2688
2689 if (punit->activity_target == NULL) {
2690 packet->activity_tgt = EXTRA_NONE;
2691 } else {
2693 }
2694
2695 /* Transported_by information is sent to the client even for units that
2696 * aren't fully known. Note that for non-allied players, any transported
2697 * unit can't be seen at all. For allied players we have to know if
2698 * transporters have room in them so that we can load units properly. */
2699 if (!unit_transported(punit)) {
2700 packet->transported = FALSE;
2701 packet->transported_by = 0;
2702 } else {
2703 packet->transported = TRUE;
2705 }
2706}
2707
2708/**********************************************************************/
2711void unit_goes_out_of_sight(struct player *pplayer, struct unit *punit)
2712{
2714 if (punit->server.moving != NULL) {
2715 /* Update status of 'pplayer' vision for 'punit'. */
2717 }
2718}
2719
2720/**********************************************************************/
2724void send_unit_info(struct conn_list *dest, struct unit *punit)
2725{
2726 const struct player *powner;
2727 struct packet_unit_info info;
2729 struct unit_move_data *pdata;
2730
2731 if (dest == NULL) {
2732 dest = game.est_connections;
2733 }
2734
2736
2738 package_unit(punit, &info);
2741
2742 conn_list_iterate(dest, pconn) {
2743 struct player *pplayer = conn_get_player(pconn);
2744
2745 /* Be careful to consider all cases where pplayer is NULL... */
2746 if (pplayer == NULL) {
2747 if (pconn->observer) {
2749 }
2750 } else if (pplayer == powner) {
2752 if (pdata != NULL) {
2753 BV_SET(pdata->can_see_unit, player_index(pplayer));
2754 }
2755 } else if (can_player_see_unit(pplayer, punit)) {
2757 if (pdata != NULL) {
2758 BV_SET(pdata->can_see_unit, player_index(pplayer));
2759 }
2760 }
2762}
2763
2764/**********************************************************************/
2769{
2770 conn_list_do_buffer(dest);
2771 conn_list_iterate(dest, pconn) {
2772 struct player *pplayer = pconn->playing;
2773
2774 if (NULL == pplayer && !pconn->observer) {
2775 continue;
2776 }
2777
2780 send_unit_info(dest, punit);
2783 }
2786 flush_packets();
2787}
2788
2789/**********************************************************************/
2796static void do_nuke_tile(struct player *pplayer, struct tile *ptile,
2797 const struct action *paction)
2798{
2799 struct city *pcity = NULL;
2800 int pop_loss;
2801
2802 pcity = tile_city(ptile);
2803
2805
2806 /* unit in a city may survive */
2807 if (pcity && fc_rand(100) < game.info.nuke_defender_survival_chance_pct) {
2808 continue;
2809 }
2811 _("Your %s was nuked by %s."),
2813 pplayer == unit_owner(punit)
2814 ? _("yourself")
2815 : nation_plural_for_player(pplayer));
2816 if (unit_owner(punit) != pplayer) {
2817 notify_player(pplayer, ptile, E_UNIT_WIN_ATT, ftc_server,
2818 _("The %s %s was nuked."),
2821 }
2822 wipe_unit(punit, ULR_NUKE, pplayer);
2824
2825
2826 if (pcity) {
2827 struct player *owner = city_owner(pcity);
2828 char city_name[MAX_LEN_LINK];
2829
2831
2833 _("%s was nuked by %s."),
2834 city_name,
2835 pplayer == owner
2836 ? _("yourself")
2837 : nation_plural_for_player(pplayer));
2838
2839 if (owner != pplayer) {
2840 notify_player(pplayer, ptile, E_CITY_NUKED, ftc_server,
2841 _("You nuked %s."),
2842 city_name);
2843 }
2844
2845 pop_loss = round((game.info.nuke_pop_loss_pct * city_size_get(pcity)) / 100.0);
2846 if (city_reduce_size(pcity, pop_loss, pplayer, "nuke")) {
2847 /* Send city size reduction to everyone seeing it */
2848 send_city_info(NULL, pcity);
2849 } else {
2850 /* City was destroyed */
2852 _("%s was destroyed by the nuke."),
2853 city_name);
2854 if (owner != pplayer) {
2855 notify_player(pplayer, ptile, E_CITY_NUKED, ftc_server,
2856 _("Your nuke destroyed %s."),
2857 city_name);
2858 }
2859 }
2860 }
2861
2862 if (fc_rand(2) == 1) {
2863 struct extra_type *pextra;
2864
2865 pextra = rand_extra_for_tile(ptile, EC_FALLOUT, FALSE);
2866 if (pextra != NULL && !tile_has_extra(ptile, pextra)) {
2867 tile_add_extra(ptile, pextra);
2868 update_tile_knowledge(ptile);
2869 }
2870 }
2871}
2872
2873/**********************************************************************/
2882 const struct unit_type *act_utype,
2883 struct player *pplayer, struct tile *ptile)
2884{
2887 &(const struct req_context) {
2888 .player = pplayer,
2889 /* City: Wait for users before choosing
2890 * home city or target tile city */
2891 .tile = ptile,
2892 .unittype = act_utype,
2893 .action = paction,
2894 },
2895 NULL,
2897
2899 do_nuke_tile(pplayer, ptile1, paction);
2901
2902 script_server_signal_emit("nuke_exploded", 2, API_TYPE_TILE, ptile,
2903 API_TYPE_PLAYER, pplayer);
2905 _("The %s detonated a nuke!"),
2906 nation_plural_for_player(pplayer));
2907}
2908
2909/**********************************************************************/
2913bool do_airline(struct unit *punit, struct city *pdest_city,
2914 const struct action *paction)
2915{
2917
2920 _("%s transported successfully."),
2921 unit_link(punit));
2922
2925 /* Can only airlift to allied and domestic cities */
2926 FALSE, FALSE,
2927 BV_ISSET(paction->sub_results, ACT_SUB_RES_HUT_ENTER),
2929
2930 /* Update airlift fields. */
2932 psrc_city->airlift--;
2934 }
2936 pdest_city->airlift--;
2938 }
2939
2940 return TRUE;
2941}
2942
2943/**********************************************************************/
2946void do_explore(struct unit *punit)
2947{
2948 switch (manage_auto_explorer(punit)) {
2949 case MR_DEATH:
2950 /* don't use punit! */
2951 return;
2952 case MR_NOT_ALLOWED:
2953 /* Needed for something else */
2954 return;
2955 case MR_OK:
2956 /* FIXME: manage_auto_explorer() isn't supposed to change the activity,
2957 * but don't count on this. See PR#39792.
2958 */
2960 break;
2961 }
2962
2964 default:
2966
2967 /* FIXME: When the manage_auto_explorer() call changes the activity from
2968 * EXPLORE to IDLE, in unit_activity_handling() ai.control is left
2969 * alone. We reset it here. See PR#12931. */
2971 break;
2972 }
2973
2974 send_unit_info(NULL, punit); /* probably duplicate */
2975}
2976
2977/**********************************************************************/
2982bool do_paradrop(struct unit *punit, struct tile *ptile,
2983 const struct action *paction)
2984{
2985 struct player *pplayer = unit_owner(punit);
2986 struct player *tgt_player = tile_owner(ptile);
2987 const struct unit_type *act_utype = unit_type_get(punit);
2988 const struct city *pcity;
2989
2990 /* Hard requirements */
2991 /* FIXME: hard requirements belong in common/actions's
2992 * is_action_possible() and the explanation texts belong in
2993 * server/unithand's action not enabled system (expl_act_not_enabl(),
2994 * ane_kind, explain_why_no_action_enabled(), etc)
2995 */
2996 if (!map_is_known_and_seen(ptile, pplayer, V_MAIN)) {
2997 /* Only take in account values from player map. */
2998 const struct player_tile *plrtile = map_get_player_tile(ptile, pplayer);
2999
3000 if (NULL == plrtile->site) {
3001 bv_extras fbv;
3002
3003 dbv_to_bv(fbv.vec, &(plrtile->extras));
3004
3006 &fbv)) {
3007 notify_player(pplayer, ptile, E_BAD_COMMAND, ftc_server,
3008 _("This unit cannot paradrop into %s."),
3010 return FALSE;
3011 }
3012 }
3013
3014 if (NULL != plrtile->site
3015 && plrtile->owner != NULL
3016 && !pplayers_allied(pplayer, plrtile->owner)
3018 notify_player(pplayer, ptile, E_BAD_COMMAND, ftc_server,
3019 /* TRANS: Paratroopers ... Paradrop Unit */
3020 _("%s cannot conquer a city with \"%s\"."),
3023 return FALSE;
3024 }
3025
3026 if (NULL != plrtile->site
3027 && plrtile->owner != NULL
3028 && (pplayers_non_attack(pplayer, plrtile->owner)
3029 || (player_diplstate_get(pplayer, plrtile->owner)->type
3030 == DS_ALLIANCE)
3031 || (player_diplstate_get(pplayer, plrtile->owner)->type
3032 == DS_TEAM))
3034 notify_player(pplayer, ptile, E_BAD_COMMAND, ftc_server,
3035 _("Cannot attack unless you declare war first."));
3036 return FALSE;
3037 }
3038 }
3039
3040
3041 /* Kill the unit when the landing goes wrong. */
3042
3043 /* Safe terrain, really? Not transformed since player last saw it. */
3044 if (!can_unit_exist_at_tile(&(wld.map), punit, ptile)
3045 && (!BV_ISSET(paction->sub_results, ACT_SUB_RES_MAY_EMBARK)
3046 || !unit_could_load_at(punit, ptile))) {
3047 map_show_circle(pplayer, ptile, act_utype->vision_radius_sq);
3048 notify_player(pplayer, ptile, E_UNIT_LOST_MISC, ftc_server,
3049 _("Your %s paradropped into the %s and was lost."),
3052 pplayer->score.units_lost++;
3054 return TRUE;
3055 }
3056
3057 pcity = tile_city(ptile);
3058
3059 if ((pcity != NULL && !pplayers_allied(pplayer, city_owner(pcity))
3061 || is_non_allied_unit_tile(ptile, pplayer)) {
3062 struct player *main_victim = NULL;
3063 struct player *secondary_victim = NULL;
3065
3066 map_show_circle(pplayer, ptile, act_utype->vision_radius_sq);
3067 maybe_make_contact(ptile, pplayer);
3068 notify_player(pplayer, ptile, E_UNIT_LOST_MISC, ftc_server,
3069 _("Your %s was killed by enemy units at the "
3070 "paradrop destination."),
3072 /* TODO: Should defender score.units_killed get increased too?
3073 * What if there's units of several allied players? Should the
3074 * city owner or owner of the first/random unit get the kill? */
3075 pplayer->score.units_lost++;
3076
3077 if (pcity != NULL) {
3078 struct player *owner = city_owner(pcity);
3079
3080 if (!pplayers_at_war(pplayer, owner)) {
3082 } else {
3084 }
3085
3087 } else {
3089 }
3090
3091 if (main_victim == NULL) {
3092 unit_list_iterate(ptile->units, tgt) {
3093 struct player *owner = unit_owner(tgt);
3094
3095 if (!pplayers_at_war(pplayer, owner)) {
3097 break;
3098 } else if (secondary_victim == NULL) {
3100 }
3102
3103 if (main_victim == NULL) {
3104 /* There's no victim with whom the attacker isn't in war,
3105 * fallback to one with whom there's already a war. */
3107 }
3108 }
3109
3111 main_victim, ptile,
3112 victim_link);
3113
3115 return TRUE;
3116 }
3117
3118 /* All ok */
3120 if (unit_move(punit, ptile,
3121 /* Done by Action_Success_Actor_Move_Cost */
3122 0,
3123 NULL, BV_ISSET(paction->sub_results,
3127 BV_ISSET(paction->sub_results, ACT_SUB_RES_HUT_ENTER),
3128 BV_ISSET(paction->sub_results, ACT_SUB_RES_HUT_FRIGHTEN))) {
3129 /* Ensure we finished on valid state. */
3132 }
3133
3134 /* May cause an incident */
3136 ptile, tile_link(ptile));
3137
3138 return TRUE;
3139}
3140
3141/**********************************************************************/
3145static bool hut_get_limited(struct unit *punit)
3146{
3147 bool ok = TRUE;
3148 int hut_chance = fc_rand(12);
3149 struct player *pplayer = unit_owner(punit);
3150 const struct civ_map *nmap = &(wld.map);
3151
3152 /* 1 in 12 to get barbarians */
3153 if (hut_chance != 0) {
3154 int cred = 25;
3155
3157 PL_("You found %d gold.",
3158 "You found %d gold.", cred), cred);
3159 pplayer->economic.gold += cred;
3162 notify_player(pplayer, unit_tile(punit),
3164 _("An abandoned village is here."));
3165 } else {
3167 _("Your %s has been killed by barbarians!"),
3170 ok = FALSE;
3171 }
3172
3173 return ok;
3174}
3175
3176/**********************************************************************/
3180static void unit_enter_hut(struct unit *punit, bool frighten_hut)
3181{
3182 struct player *pplayer = unit_owner(punit);
3183 int id = punit->id;
3184 struct tile *ptile = unit_tile(punit);
3185 bool hut = FALSE;
3186 const struct req_context context = {
3187 .player = pplayer,
3188 .tile = ptile,
3189 };
3190
3192 if (tile_has_extra(ptile, pextra)
3193 && are_reqs_active(&context, tile_owner(ptile), &pextra->rmreqs,
3195 ) {
3196 hut = TRUE;
3197 /* FIXME: are all enter-removes extras worth counting? */
3198 pplayer->server.huts++;
3199
3200 destroy_extra(ptile, pextra);
3202
3203 /* FIXME: enable different classes
3204 * to behave differently with different huts */
3205 if (frighten_hut) {
3206 script_server_signal_emit("hut_frighten", punit,
3207 extra_rule_name(pextra));
3208 } else if (is_ai(pplayer) && has_handicap(pplayer, H_LIMITEDHUTS)) {
3209 /* AI with H_LIMITEDHUTS only gets 25 gold (or barbs if unlucky) */
3211 } else {
3212 script_server_signal_emit("hut_enter", punit, extra_rule_name(pextra));
3213 }
3214
3215 /* We need punit for the callbacks, can't continue if the unit died */
3216 if (!unit_is_alive(id)) {
3217 break;
3218 }
3219 }
3221
3222 if (hut) {
3223 send_player_info_c(pplayer, pplayer->connections); /* eg, gold */
3224 }
3225 return;
3226}
3227
3228/**********************************************************************/
3232{
3233 bv_player can_see_unit;
3234
3237
3238 BV_CLR_ALL(can_see_unit);
3239 players_iterate(pplayer) {
3240 if (can_player_see_unit(pplayer, punit)) {
3241 BV_SET(can_see_unit, player_index(pplayer));
3242 }
3244
3246
3247 players_iterate(pplayer) {
3248 if (BV_ISSET(can_see_unit, player_index(pplayer))
3249 && !can_player_see_unit(pplayer, punit)) {
3250 unit_goes_out_of_sight(pplayer, punit);
3251 }
3253
3256}
3257
3258/**********************************************************************/
3262 struct unit *ptrans,
3263 bool force)
3264{
3265 bool had_cargo;
3266
3269
3271
3273
3274 if (!had_cargo) {
3275 /* Transport's loaded status changed */
3277 }
3278}
3279
3280/**********************************************************************/
3284{
3285 struct unit *ptrans;
3286
3288
3290
3292
3294
3297}
3298
3299/**********************************************************************/
3303static void autoattack_prob_free(struct autoattack_prob *prob)
3304{
3305 free(prob);
3306}
3307
3308/**********************************************************************/
3317static int compare_units(const struct autoattack_prob *const *p1,
3318 const struct autoattack_prob *const *q1)
3319{
3320 const struct unit *p1unit = game_unit_by_number((*p1)->unit_id);
3321 const struct unit *q1unit = game_unit_by_number((*q1)->unit_id);
3322
3323 /* Sort by transport depth first. This makes sure that no transport
3324 * attacks before its cargo does -- cargo sorts earlier in the list. */
3325 {
3326 const struct unit *p1trans = p1unit, *q1trans = q1unit;
3327
3328 /* Walk the transport stacks in parallel, so as to bail out as soon as
3329 * one of them is empty (avoid walking deep stacks more often than
3330 * necessary). */
3331 while (p1trans && q1trans) {
3334 }
3335 if (!p1trans && q1trans) {
3336 /* q1 is at greater depth (perhaps it's p1's cargo). It should sort
3337 * earlier in the list (p1 > q1). */
3338 return 1;
3339 } else if (p1trans && !q1trans) {
3340 /* p1 is at greater depth, so should sort earlier (p1 < q1). */
3341 return -1;
3342 }
3343 /* else same depth, so move on to checking win chance: */
3344 }
3345
3346 /* Put the units with the highest probability of success first. The up
3347 * side of this is that units with bonuses against the victim attacks
3348 * before other units. The downside is that strong units can be led
3349 * away by sacrificial units. */
3350 return (-1
3351 /* Assume the worst. */
3352 * action_prob_cmp_pessimist((*p1)->prob, (*q1)->prob));
3353}
3354
3355/**********************************************************************/
3360{
3361 struct autoattack_prob_list *autoattack;
3362 int moves = punit->moves_left;
3363 int sanity1 = punit->id;
3364 struct civ_map *nmap = &(wld.map);
3365
3366 if (!game.server.autoattack) {
3367 return TRUE;
3368 }
3369
3371
3372 /* Kludge to prevent attack power from dropping to zero during calc */
3374
3375 adjc_iterate(nmap, unit_tile(punit), ptile) {
3376 /* First add all eligible units to a autoattack list */
3377 unit_list_iterate(ptile->units, penemy) {
3379 struct tile *tgt_tile = unit_tile(punit);
3380
3381 fc_assert_action(tgt_tile, continue);
3382
3383 probability->prob =
3387 punit, NULL);
3388
3389 if (action_prob_possible(probability->prob)) {
3390 probability->unit_id = penemy->id;
3392 } else {
3394 }
3397
3398 /* Sort the potential attackers from highest to lowest success
3399 * probability. */
3400 if (autoattack_prob_list_size(autoattack) >= 2) {
3402 }
3403
3405 int sanity2 = penemy->id;
3406 struct tile *ptile = unit_tile(penemy);
3407 struct unit *enemy_defender = get_defender(nmap, punit, ptile, NULL);
3408 double punitwin, penemywin;
3409 double threshold = 0.25;
3410 struct tile *tgt_tile = unit_tile(punit);
3411
3413
3414 if (tile_city(ptile) && unit_list_size(ptile->units) == 1) {
3415 /* Don't leave city defenseless */
3416 threshold = 0.90;
3417 }
3418
3419 if (NULL != enemy_defender) {
3421 } else {
3422 /* 'penemy' can attack 'punit' but it may be not reciproque. */
3423 punitwin = 1.0;
3424 }
3425
3426 /* Previous attacks may have changed the odds. Recalculate. */
3427 peprob->prob =
3431 punit, NULL);
3432
3433 if (!action_prob_possible(peprob->prob)) {
3434 /* No longer legal. */
3435 continue;
3436 }
3437
3438 /* Assume the worst. */
3440
3441 if ((penemywin > 1.0 - punitwin
3443 && penemywin > threshold) {
3444
3445#ifdef REALLY_DEBUG_THIS
3446 log_test("AA %s -> %s (%d,%d) %.2f > %.2f && > %.2f",
3449 1.0 - punitwin, threshold);
3450#endif
3451
3456 } else {
3457#ifdef REALLY_DEBUG_THIS
3458 log_test("!AA %s -> %s (%d,%d) %.2f > %.2f && > %.2f",
3461 1.0 - punitwin, threshold);
3462#endif
3463 continue;
3464 }
3465
3468 }
3471 } else {
3472 autoattack_prob_list_destroy(autoattack);
3473 return FALSE; /* moving unit dead */
3474 }
3476
3477 autoattack_prob_list_destroy(autoattack);
3479 /* We could have lost movement in combat */
3482 return TRUE;
3483 } else {
3484 return FALSE;
3485 }
3486}
3487
3488/**********************************************************************/
3491static void cancel_orders(struct unit *punit, char *dbg_msg)
3492{
3495 log_debug("%s", dbg_msg);
3496}
3497
3498/**********************************************************************/
3503{
3504 bool alone_in_city;
3505
3506 if (NULL != tile_city(unit_tile(punit))) {
3507 int count = 0;
3508
3510 /* Consider only units not transported. */
3511 if (!unit_transported(aunit)) {
3512 count++;
3513 }
3515
3516 alone_in_city = (1 == count);
3517 } else {
3519 }
3520
3521 /* There may be sentried units with a sightrange > 3, but we don't
3522 wake them up if the punit is farther away than 3. */
3523 square_iterate(&(wld.map), unit_tile(punit), 3, ptile) {
3524 unit_list_iterate(ptile->units, penemy) {
3526 int radius_sq = get_unit_vision_at(penemy, unit_tile(penemy), V_MAIN);
3527
3529 && penemy->activity == ACTIVITY_SENTRY
3530 && radius_sq >= distance_sq
3531 /* If the unit moved on a city, and the unit is alone, consider
3532 * it is visible. */
3533 && (alone_in_city
3535 /* on board transport; don't awaken */
3539 }
3542
3543 /* Wakeup patrolling units we bump into.
3544 We do not wakeup units further away than 3 squares... */
3545 square_iterate(&(wld.map), unit_tile(punit), 3, ptile) {
3546 unit_list_iterate(ptile->units, ppatrol) {
3547 if (punit != ppatrol
3549 && ppatrol->orders.vigilant) {
3551 cancel_orders(ppatrol, " stopping because of nearby enemy");
3554 _("Orders for %s aborted after enemy movement was "
3555 "spotted."),
3557 }
3558 }
3561}
3562
3563/**********************************************************************/
3573 struct tile *src_tile,
3574 struct tile *dst_tile,
3575 bool passenger,
3577{
3578 struct city *fromcity = tile_city(src_tile);
3579 struct city *tocity = tile_city(dst_tile);
3580 struct city *homecity_start_pos = NULL;
3581 struct city *homecity_end_pos = NULL;
3587 const struct unit_type *type_end_pos = type_start_pos;
3590 int saved_id = punit->id;
3591 bool alive = TRUE;
3592 const struct civ_map *nmap = &(wld.map);
3593
3595 if (!passenger) {
3596 /* The unit that does the move may conquer. */
3598 }
3599
3600 /* Run for passengers too. A passenger may have been killed when its
3601 * transport conquered a city. (unit_conquer_city() can cause Lua code
3602 * to run) */
3603
3605 if (alive) {
3606 /* In case script has changed something about unit */
3610 }
3611 }
3612
3613 if (homecity_id_start_pos != 0) {
3615 }
3618 } else {
3620 }
3621
3622 /* We only do refreshes for non-AI players to now make sure the AI turns
3623 doesn't take too long. Perhaps we should make a special refresh_city
3624 functions that only refreshed happines. */
3625
3626 /* might have changed owners or may be destroyed */
3628
3629 if (tocity) { /* entering a city */
3630 if (tocity->owner == pplayer_end_pos) {
3634 }
3635 }
3636 if (homecity_start_pos) {
3638 }
3639 }
3640
3641 if (fromcity) { /* leaving a city */
3642 if (homecity_start_pos) {
3644 }
3646 && fromcity->owner == pplayer_start_pos
3650 }
3651 }
3652
3653 /* Entering/leaving a fortress or friendly territory */
3655 bool friendly_end = FALSE;
3656
3659 } else {
3661 type_end_pos);
3662
3663 if (max_friendliness_range >= 0
3666 } else {
3669
3670 if (max_friendliness_range >= 0
3673 }
3674 }
3675 }
3676
3677 if (friendly_end) {
3680 }
3681 }
3682
3686 }
3693 }
3694
3696 sync_cities();
3697
3698 return alive;
3699}
3700
3701/**********************************************************************/
3705static void check_unit_activity(struct unit *punit)
3706{
3707 switch (punit->activity) {
3708 case ACTIVITY_IDLE:
3709 case ACTIVITY_SENTRY:
3710 case ACTIVITY_EXPLORE:
3711 case ACTIVITY_GOTO:
3712 break;
3713 case ACTIVITY_CLEAN:
3714 case ACTIVITY_MINE:
3715 case ACTIVITY_IRRIGATE:
3716 case ACTIVITY_CULTIVATE:
3717 case ACTIVITY_PLANT:
3718 case ACTIVITY_FORTIFIED:
3719 case ACTIVITY_PILLAGE:
3720 case ACTIVITY_TRANSFORM:
3722 case ACTIVITY_BASE:
3723 case ACTIVITY_GEN_ROAD:
3724 case ACTIVITY_CONVERT:
3725 case ACTIVITY_LAST:
3727 break;
3728 };
3729}
3730
3731/**********************************************************************/
3735 const struct tile *psrctile,
3736 const struct tile *pdesttile)
3737{
3738 struct unit_move_data *pdata;
3739 struct player *powner = unit_owner(punit);
3740
3741 if (punit->server.moving) {
3742 /* Recursive moving (probably due to a script). */
3744 pdata->ref_count++;
3745 fc_assert_msg(pdata->punit == punit,
3746 "Unit number %d (%p) was going to die, but "
3747 "server attempts to move it.",
3748 punit->id, punit);
3749 fc_assert_msg(pdata->old_vision == NULL,
3750 "Unit number %d (%p) has done an incomplete move.",
3751 punit->id, punit);
3752 } else {
3753 pdata = fc_malloc(sizeof(*pdata));
3754 pdata->ref_count = 1;
3755 pdata->punit = punit;
3757 BV_CLR_ALL(pdata->can_see_unit);
3758 }
3759 pdata->powner = powner;
3760 BV_CLR_ALL(pdata->can_see_move);
3761 pdata->old_vision = punit->server.vision;
3762
3763 return pdata;
3764}
3765
3766/**********************************************************************/
3770 const struct tile *psrctile,
3771 struct tile *pdesttile)
3772{
3773 struct vision *new_vision;
3774 struct unit *punit = pdata->punit;
3776 const v_radius_t radius_sq
3780
3781 /* Remove unit from the source tile. */
3783
3784#ifndef FREECIV_NDEBUG
3785 bool success =
3786#endif
3789
3790 /* Set new tile. */
3793
3794 if (unit_transported(punit)) {
3795 /* Silently free orders since they won't be applicable anymore. */
3797 }
3798
3799 /* Check unit activity. */
3803
3804 /* We first unfog the destination, then send the move,
3805 * and then fog the old territory. This means that the player
3806 * gets a chance to see the newly explored territory while the
3807 * client moves the unit, and both areas are visible during the
3808 * move */
3809
3810 /* Enhance vision if unit steps into a fortress */
3813 vision_change_sight(new_vision, radius_sq);
3815}
3816
3817/**********************************************************************/
3821{
3823 fc_assert_ret(pdata->ref_count > 0);
3824 fc_assert_msg(pdata->old_vision == NULL,
3825 "Unit number %d (%p) has done an incomplete move.",
3826 pdata->punit != NULL ? pdata->punit->id : -1, pdata->punit);
3827
3828 pdata->ref_count--;
3829 if (pdata->ref_count == 0) {
3830 if (pdata->punit != NULL) {
3831 fc_assert(pdata->punit->server.moving == pdata);
3832 pdata->punit->server.moving = NULL;
3833 }
3834 free(pdata);
3835 }
3836}
3837
3838/**********************************************************************/
3842 const struct tile *psrctile,
3843 struct tile *pdesttile,
3844 bool adj)
3845{
3848 struct unit_move_data *pdata;
3849
3850 /* Make new data for 'punit'. */
3853
3854 /* Add all contained units. */
3856 struct unit_move_data *cargo_data;
3857
3861
3862 /* Determine the players able to see the move(s), now that the player
3863 * vision has been increased. */
3864 if (adj) {
3865 /* Main unit for adjacent move: the move is visible for every player
3866 * able to see on the matching unit layer. */
3867 enum vision_layer vlayer = unit_type_get(punit)->vlayer;
3868
3872 BV_SET(pdata->can_see_unit, player_index(oplayer));
3873 BV_SET(pdata->can_see_move, player_index(oplayer));
3874 }
3876 }
3877
3879
3881
3882 if (adj && pmove_data->punit == punit) {
3883 /* If positions are adjacent, we have already handled 'punit'. See
3884 * above. */
3885 continue;
3886 }
3887
3889 if ((adj
3891 pmove_data != pdata))
3893 pmove_data != pdata)) {
3894 BV_SET(pmove_data->can_see_unit, player_index(oplayer));
3895 BV_SET(pmove_data->can_see_move, player_index(oplayer));
3896 }
3898 pmove_data != pdata)) {
3899 /* The unit was seen with its source tile even if it was
3900 * teleported. */
3901 BV_SET(pmove_data->can_see_unit, player_index(oplayer));
3902 }
3905
3906 return plist;
3907}
3908
3909/**********************************************************************/
3920bool unit_move(struct unit *punit, struct tile *pdesttile, int move_cost,
3921 struct unit *embark_to, bool find_embark_target,
3923 bool enter_hut, bool frighten_hut)
3924{
3925 struct player *pplayer;
3926 struct tile *psrctile;
3927 struct city *psrccity;
3928 struct city *pdestcity;
3929 struct unit *ptransporter;
3932 struct unit_move_data_list *plist;
3933 struct unit_move_data *pdata;
3934 int saved_id;
3935 bool unit_lives;
3936 bool adj;
3937 enum direction8 facing;
3938
3939 /* Some checks. */
3942
3943 pplayer = unit_owner(punit);
3944 saved_id = punit->id;
3947
3949
3950 /* Unload the unit if on a transport. */
3952 if (ptransporter != NULL) {
3953 /* Unload unit _before_ setting the new tile! */
3955 /* Send updated information to anyone watching that transporter
3956 * was unloading cargo. */
3958 }
3959
3960 /* Wakup units next to us before we move. */
3962
3963 /* Make info packets at 'psrctile'. */
3964 if (adj) {
3965 /* If tiles are adjacent, we will show the move to users able
3966 * to see it. */
3969 }
3970
3971 /* Set unit orientation */
3972 if (adj) {
3973 /* Only change orientation when moving to adjacent tile */
3974 punit->facing = facing;
3975 }
3976
3978
3979 /* Move magic. */
3980 punit->moved = TRUE;
3981 punit->moves_left = MAX(0, punit->moves_left - move_cost);
3982 if (punit->moves_left == 0 && !unit_has_orders(punit)) {
3983 /* The next order may not require any remaining move fragments. */
3985 }
3986
3987 /* No longer relevant. */
3990
3991 /* Claim ownership of fortress? */
3994 /* Yes. We claim *all* bases if there's *any* claimable base(s).
3995 * Even if original unit cannot claim other kind of bases, the
3996 * first claimed base will have influence over other bases,
3997 * or something like that. */
3998 tile_claim_bases(pdesttile, pplayer);
3999 }
4000
4002 fc_assert(pdata->punit == punit);
4003
4004 /* Check timeout settings. */
4007
4008 /* FIXME: Seen enemy cargo in a non-enemy transport should count too,
4009 * if they are ever seen. */
4011 /* Increase the timeout if an enemy unit moves and the
4012 * timeoutaddenemymove setting is in use. */
4013 if (penemy->is_connected
4014 && pplayer != penemy
4015 && pplayers_at_war(pplayer, penemy)
4016 && BV_ISSET(pdata->can_see_move, player_index(penemy))) {
4018 break;
4019 }
4021
4024 }
4025 }
4026
4028 /* The unit can perform an action to the city at the destination tile.
4029 * A long distance move (like an airlift) doesn't ask what action to
4030 * perform before moving. Ask now. */
4031
4034 }
4035
4036 /* Notifications of the move to the clients. */
4037 if (adj) {
4038 /* Special case: 'punit' is moving to adjacent position. Then we show
4039 * 'punit' move to all users able to see 'psrctile' or 'pdesttile'. */
4040
4041 /* Make info packets at 'pdesttile'. */
4044
4047
4048 if (aplayer == NULL) {
4049 if (pconn->observer) {
4050 /* Global observers see all... */
4053 }
4054 } else if (BV_ISSET(pdata->can_see_move, player_index(aplayer))) {
4055 if (aplayer == pplayer) {
4058 } else {
4061 }
4062 }
4064 }
4065
4066 /* Other moves. */
4068 if (adj && pmove_data == pdata) {
4069 /* If positions are adjacent, we have already shown 'punit' move.
4070 * See above. */
4071 continue;
4072 }
4073
4074 /* Make info packets at 'pdesttile'. */
4078
4081
4082 if (aplayer == NULL) {
4083 if (pconn->observer) {
4084 /* Global observers see all... */
4086 }
4087 } else if (BV_ISSET(pmove_data->can_see_move, player_index(aplayer))) {
4088 if (aplayer == pmove_data->powner) {
4090 } else {
4092 }
4093 }
4096
4097 /* Clear old vision. */
4099 if (pmove_data->old_vision != NULL) {
4100 vision_clear_sight(pmove_data->old_vision);
4101 vision_free(pmove_data->old_vision);
4102 pmove_data->old_vision = NULL;
4103 }
4105
4106 /* Move consequences. */
4108 struct unit *aunit = pmove_data->punit;
4109
4110 if (aunit != NULL
4111 && unit_owner(aunit) == pmove_data->powner
4112 && unit_tile(aunit) == pdesttile) {
4114 pdata != pmove_data,
4116 }
4118
4119 unit_lives = (pdata->punit == punit);
4120
4121 /* Wakeup units and make contact. */
4122 if (unit_lives) {
4124 }
4125 maybe_make_contact(pdesttile, pplayer);
4126
4127 if (unit_lives) {
4128 /* Special checks for ground units in the ocean. */
4130 if (embark_to != NULL) {
4132 } else if (find_embark_target) {
4133 /* TODO: Consider to stop supporting find_embark_target and make all
4134 * callers that wants auto loading set embark_to. */
4136 } else {
4138 }
4139 if (ptransporter) {
4141
4142 /* Set activity to sentry if boarding a ship. */
4143 if (is_human(pplayer)
4148 }
4149
4151 }
4152 }
4153 }
4154
4155 /* Remove units going out of sight. */
4157 struct unit *aunit = pmove_data->punit;
4158
4159 if (aunit == NULL) {
4160 continue; /* Died! */
4161 }
4162
4164 if (BV_ISSET(pmove_data->can_see_unit, player_index(aplayer))
4167 }
4170
4171 /* Inform the owner's client about actor unit arrival. Can, depending on
4172 * the client settings, cause the client to start the process that makes
4173 * the action selection dialog pop up. */
4175 if (pdestcity != NULL) {
4176 /* Arrival in a city counts. */
4177
4179 struct unit *ptrans;
4180 bool ok;
4181 struct unit *act_unit;
4182 struct player *act_player;
4183
4184 act_unit = pmove_data->punit;
4186
4187 if (act_unit == NULL
4188 || !unit_is_alive(act_unit->id)) {
4189 /* The unit died before reaching this point. */
4190 continue;
4191 }
4192
4193 if (unit_tile(act_unit) != pdesttile) {
4194 /* The unit didn't arrive at the destination tile. */
4195 continue;
4196 }
4197
4198 if (!is_human(act_player)) {
4199 /* Only humans need reminders. */
4200 continue;
4201 }
4202
4203 if (!unit_transported(act_unit)) {
4204 /* Don't show the action selection dialog again. Non transported
4205 * units are handled before they move to the tile. */
4206 continue;
4207 }
4208
4209 /* Open action dialog only if 'act_unit' and all its transporters
4210 * (recursively) don't have orders. */
4212 /* The unit it self has orders. */
4213 continue;
4214 }
4215
4218 if (NULL == ptrans) {
4219 /* No (recursive) transport has orders. */
4220 ok = TRUE;
4221 break;
4222 } else if (unit_has_orders(ptrans)) {
4223 /* A unit transporting the unit has orders */
4224 ok = FALSE;
4225 break;
4226 }
4227 }
4228
4229 if (!ok) {
4230 /* A unit transporting act_unit has orders. */
4231 continue;
4232 }
4233
4235 /* There is a valid target. */
4236
4237 act_unit->action_decision_want = ACT_DEC_PASSIVE;
4238 act_unit->action_decision_tile = pdesttile;
4239
4240 /* Let the client know that this unit wants the player to decide
4241 * what to do. */
4243 }
4245 }
4246
4248 /* Check cities at source and destination. */
4250 if (psrccity != NULL) {
4252 }
4253 if (pdestcity != NULL) {
4255 }
4256
4257 if (unit_lives) {
4258 /* Let the scripts run ... */
4261 }
4262
4263 if (unit_lives) {
4264 /* Autoattack. */
4266 }
4267
4268 if (unit_lives && (enter_hut || frighten_hut)) {
4269 /* Is there a hut? */
4273 }
4274
4276
4277 if (unit_lives) {
4278 CALL_FUNC_EACH_AI(unit_move_seen, punit);
4279 }
4280
4281 return unit_lives;
4282}
4283
4284/**********************************************************************/
4288 struct tile *ptile)
4289{
4290 return (is_non_allied_unit_tile(ptile, unit_owner(punit))
4292}
4293
4294/**********************************************************************/
4301{
4302 bool cancel = FALSE;
4303 int radius_sq = get_unit_vision_at(punit, unit_tile(punit), V_MAIN);
4304 struct player *pplayer = unit_owner(punit);
4305
4306 circle_iterate(&(wld.map), unit_tile(punit), radius_sq, ptile) {
4307 struct unit *penemy = tile_non_allied_unit(ptile, pplayer);
4308 struct vision_site *pdcity = map_get_player_site(ptile, pplayer);
4309
4310 if ((penemy && can_player_see_unit(pplayer, penemy))
4312 && pdcity->occupied)) {
4313 cancel = TRUE;
4314 break;
4315 }
4317
4318 return cancel;
4319}
4320
4321/**********************************************************************/
4329static inline bool player_is_watching(struct unit *punit, const bool fresh)
4330{
4331 /* The player just sent the orders to the unit. The unit has moves left.
4332 * It is therefore safe to assume that the player already is paying
4333 * attention to the unit. */
4334 return fresh && punit->moves_left > 0;
4335}
4336
4337/**********************************************************************/
4356bool execute_orders(struct unit *punit, const bool fresh)
4357{
4358 struct act_prob prob;
4359 bool performed;
4360 const char *name;
4361 bool res, last_order;
4362 int unitid = punit->id;
4363 struct player *pplayer = unit_owner(punit);
4364 int moves_made = 0;
4365 const struct civ_map *nmap = &(wld.map);
4366
4368
4369 if (punit->activity != ACTIVITY_IDLE) {
4370 /* Unit's in the middle of an activity; wait for it to finish. */
4372 return TRUE;
4373 }
4374
4375 log_debug("Executing orders for %s %d", unit_rule_name(punit), punit->id);
4376
4377 /* Any time the orders are canceled we should give the player a message. */
4378
4379 while (TRUE) {
4380 struct unit_order order;
4381 struct action *oaction;
4382 struct tile *dst_tile;
4383 struct city *tgt_city;
4384 struct unit *tgt_unit;
4385 int tgt_id;
4386 int sub_tgt_id;
4387 struct extra_type *pextra;
4389
4390 if (punit->done_moving) {
4391 log_debug(" stopping because we're done this turn");
4392 return TRUE;
4393 }
4394
4396 /* "Patrol" orders are stopped if an enemy is near. */
4397 cancel_orders(punit, " stopping because of nearby enemy");
4399 _("Orders for %s aborted as there are units nearby."),
4400 unit_link(punit));
4401 return TRUE;
4402 }
4403
4404 if (moves_made == punit->orders.length) {
4405 /* For repeating orders, don't repeat more than once per turn. */
4406 log_debug(" stopping because we ran a round");
4409 return TRUE;
4410 }
4411 moves_made++;
4412
4413 order = punit->orders.list[punit->orders.index];
4414
4415 /* An ORDER_PERFORM_ACTION that doesn't specify an action should not get
4416 * this far. */
4418 || action_id_exists(order.action)),
4419 continue);
4420
4421 switch (order.order) {
4422 case ORDER_MOVE:
4423 case ORDER_ACTION_MOVE:
4424 case ORDER_FULL_MP:
4425 if (0 == punit->moves_left) {
4426 log_debug(" stopping because of no more move points");
4427 return TRUE;
4428 }
4429 break;
4432 log_debug(" stopping. Not enough move points this turn");
4433 return TRUE;
4434 }
4435 break;
4436 case ORDER_ACTIVITY:
4437 case ORDER_LAST:
4438 /* Those actions don't require moves left. */
4439 break;
4440 }
4441
4443 && punit->orders.index + 1 == punit->orders.length);
4444
4445 if (last_order) {
4446 /* Clear the orders before we engage in the move. That way any
4447 * has_orders checks will yield FALSE and this will be treated as
4448 * a normal move. This is important: for instance a caravan goto
4449 * will popup the caravan dialog on the last move only. */
4451 }
4452
4453 /* Advance the orders one step forward. This is needed because any
4454 * updates sent to the client as a result of the action should include
4455 * the new index value. Note that we have to send_unit_info somewhere
4456 * after this point so that the client is properly updated. */
4457 punit->orders.index++;
4458
4459 switch (order.order) {
4460 case ORDER_FULL_MP:
4462 /* If the unit doesn't have full MP then it just waits until the
4463 * next turn. We assume that the next turn it will have full MP
4464 * (there's no check for that). */
4466 log_debug(" waiting this turn");
4468 }
4469 break;
4470 case ORDER_ACTIVITY:
4471 {
4472 enum unit_activity activity = order.activity;
4473
4474 fc_assert(activity == ACTIVITY_SENTRY);
4475
4476 if (can_unit_do_activity(nmap, punit, activity)) {
4478 set_unit_activity(punit, activity);
4480
4481 break;
4482 }
4483 }
4484
4485 cancel_orders(punit, " orders canceled because of failed activity");
4487 _("Orders for %s aborted since they "
4488 "give an invalid activity."),
4489 unit_link(punit));
4490 return TRUE;
4491 case ORDER_MOVE:
4492 case ORDER_ACTION_MOVE:
4493 /* Move unit */
4494 if (!(dst_tile = mapstep(&(wld.map), unit_tile(punit), order.dir))) {
4495 cancel_orders(punit, " move order sent us to invalid location");
4497 _("Orders for %s aborted since they "
4498 "give an invalid location."),
4499 unit_link(punit));
4500 return TRUE;
4501 }
4502
4503 if (order.order != ORDER_ACTION_MOVE
4505 /* Plain move required: no attack, trade route etc. */
4506 cancel_orders(punit, " orders canceled because of enemy");
4508 _("Orders for %s aborted as there "
4509 "are units in the way."),
4510 unit_link(punit));
4511 return TRUE;
4512 }
4513
4514 log_debug(" moving to %d,%d", TILE_XY(dst_tile));
4516 order.order != ORDER_ACTION_MOVE);
4517 if (!player_unit_by_number(pplayer, unitid)) {
4518 log_debug(" unit died while moving.");
4519 /* A player notification should already have been sent. */
4520 return FALSE;
4521 }
4522
4523 if (res && !same_pos(dst_tile, unit_tile(punit))) {
4524 /* Movement succeeded but unit didn't move. */
4525 log_debug(" orders resulted in combat.");
4527 return TRUE;
4528 }
4529
4530 if (!res) {
4531 fc_assert(0 <= punit->moves_left);
4532
4533 /* Movement failed (ZOC, etc.) */
4534 cancel_orders(punit, " attempt to move failed.");
4535
4537 /* The final move "failed" because the unit needs to ask the
4538 * player what action it should take.
4539 *
4540 * The action decision request notifies the player. Its
4541 * location at the unit's last order makes it clear to the
4542 * player who the decision is for. ("The Spy I sent to Berlin
4543 * has arrived.")
4544 *
4545 * A notification message is therefore redundant. */
4546 && !(last_order
4549 /* The player may have missed this. No one else will announce it
4550 * in a satisfying manner. Inform the player. */
4551 notify_player(pplayer, unit_tile(punit),
4553 _("Orders for %s aborted because of failed move."),
4554 unit_link(punit));
4555 }
4556
4557 return TRUE;
4558 }
4559 break;
4562
4563 /* Checked in unit_order_list_is_sane() */
4564 fc_assert_action(oaction != NULL, continue);
4565
4566 log_debug(" orders: doing action %s", action_rule_name(oaction));
4567
4569
4570 dst_tile = index_to_tile(&(wld.map), order.target);
4571
4572 if (dst_tile == NULL) {
4573 /* Could be at the edge of the map while trying to target a tile
4574 * outside of it. */
4575
4576 cancel_orders(punit, " target location doesn't exist");
4578 order.action, dst_tile, NULL, NULL);
4579
4580 return TRUE;
4581 }
4582
4583 /* Get the target city from the target tile. */
4585
4586 if (tgt_city == NULL && tk == ATK_CITY) {
4587 /* This action targets a city but no city target was found. */
4588
4589 cancel_orders(punit, " perform action vs city with no city");
4591 order.action, dst_tile, tgt_city, NULL);
4592
4593 return TRUE;
4594 }
4595
4596 /* Get a target unit at the target tile. */
4598
4599 if (tgt_unit == NULL && tk == ATK_UNIT) {
4600 /* This action targets a unit but no target unit was found. */
4601
4602 cancel_orders(punit, " perform action vs unit with no unit");
4604 order.action, dst_tile, tgt_city, tgt_unit);
4605
4606 return TRUE;
4607 }
4608
4609 /* Server side sub target assignment */
4610 if (oaction->target_complexity == ACT_TGT_COMPL_FLEXIBLE
4611 && order.sub_target == NO_TARGET) {
4612 /* Try to find a sub target. */
4614 } else {
4615 /* The client should have specified a sub target if needed */
4616 sub_tgt_id = order.sub_target;
4617 }
4618
4619 /* Get a target extra at the target tile */
4620 pextra = (sub_tgt_id == NO_TARGET ?
4621 NULL :
4622 extra_by_number(sub_tgt_id));
4623
4625 && pextra != NULL
4626 && actres_creates_extra(oaction->result, pextra)
4627 && tile_has_extra(dst_tile, pextra)) {
4628 /* Already there. Move on to the next order. */
4629 break;
4630 }
4631
4633 && pextra != NULL
4634 && actres_removes_extra(oaction->result, pextra)
4635 && !tile_has_extra(dst_tile, pextra)) {
4636 /* Already not there. Move on to the next order. */
4637 break;
4638 }
4639
4640 /* No target selected. */
4641 tgt_id = -1;
4642
4643 /* Assume impossible until told otherwise. */
4644 prob = ACTPROB_IMPOSSIBLE;
4645
4646 switch (tk) {
4647 case ATK_UNITS:
4648 prob = action_prob_vs_stack(nmap, punit, order.action,
4649 dst_tile);
4650 tgt_id = dst_tile->index;
4651 break;
4652 case ATK_TILE:
4653 prob = action_prob_vs_tile(nmap, punit, order.action,
4654 dst_tile, pextra);
4655 tgt_id = dst_tile->index;
4656 break;
4657 case ATK_EXTRAS:
4658 prob = action_prob_vs_extras(nmap, punit, order.action,
4659 dst_tile, pextra);
4660 tgt_id = dst_tile->index;
4661 break;
4662 case ATK_CITY:
4663 prob = action_prob_vs_city(nmap, punit, order.action,
4664 tgt_city);
4665 tgt_id = tgt_city->id;
4666 break;
4667 case ATK_UNIT:
4668 prob = action_prob_vs_unit(nmap, punit, order.action,
4669 tgt_unit);
4670
4671 tgt_id = tgt_unit->id;
4672 break;
4673 case ATK_SELF:
4674 prob = action_prob_self(nmap, punit, order.action);
4675
4676 tgt_id = unitid;
4677 break;
4678 case ATK_COUNT:
4679 log_error("Invalid action target kind");
4680
4681 /* The check below will abort and cancel the orders because prob
4682 * was initialized to impossible above this switch statement. */
4683
4684 break;
4685 }
4686
4687 if (!action_prob_possible(prob)) {
4688 /* The player has enough information to know that this action is
4689 * against the rules. Don't risk any punishment by trying to
4690 * perform it. */
4691
4692 cancel_orders(punit, " illegal action");
4694 _("%s could not do %s to %s."),
4698
4699 /* Try to explain what rule made it illegal. */
4701 order.action, dst_tile, tgt_city, tgt_unit);
4702
4703 return TRUE;
4704 }
4705
4707 /* This action needs a name. */
4709 } else {
4710 /* This action doesn't need a name. */
4711 name = "";
4712 }
4713
4715 unitid,
4716 tgt_id,
4717 sub_tgt_id,
4718 name,
4719 order.action,
4721
4722 if (!player_unit_by_number(pplayer, unitid)) {
4723 /* The unit "died" while performing the action. */
4724 return FALSE;
4725 }
4726
4727 if (!performed) {
4728 /* The action wasn't performed as ordered. */
4729
4730 cancel_orders(punit, " failed action");
4732 _("Orders for %s aborted because "
4733 "doing %s to %s failed."),
4737
4738 return TRUE;
4739 }
4740
4741 if (action_id_get_act_time(order.action, punit, dst_tile, pextra)
4743 /* Done at turn change. */
4746 break;
4747 }
4748
4749 break;
4750 case ORDER_LAST:
4751 /* Should be caught when loading the unit orders from the savegame or
4752 * when receiving the unit orders from the client. */
4753 fc_assert_msg(order.order != ORDER_LAST, "Invalid order: last.");
4754 cancel_orders(punit, " invalid order!");
4756 _("Your %s has invalid orders."),
4757 unit_link(punit));
4758 return TRUE;
4759 }
4760
4761 if (last_order) {
4763 log_debug(" stopping because orders are complete");
4764 return TRUE;
4765 }
4766
4767 if (punit->orders.index == punit->orders.length) {
4769 /* Start over. */
4770 log_debug(" repeating orders.");
4771 punit->orders.index = 0;
4772 }
4773 } /* end while */
4774}
4775
4776/**********************************************************************/
4783static int get_unit_vision_base(const struct unit *punit,
4784 enum vision_layer vlayer,
4785 const int base)
4786{
4787 switch (vlayer) {
4788 case V_MAIN:
4789 return MAX(0, base);
4790 case V_INVIS:
4791 case V_SUBSURFACE:
4792 return CLIP(0, base, 2);
4793 case V_COUNT:
4794 break;
4795 }
4796
4797 log_error("Unsupported vision layer variant: %d.", vlayer);
4798
4799 return 0;
4800}
4801
4802/**********************************************************************/
4805static int unit_vision_range_modifiers(const struct unit *punit,
4806 const struct tile *ptile)
4807{
4808 const struct unit_type *utype = unit_type_get(punit);
4809
4810 return (utype->vision_radius_sq
4812 &(const struct req_context) {
4813 .player = unit_owner(punit),
4814 .tile = ptile,
4815 .unittype = utype,
4816 .unit = punit,
4817 },
4819}
4820
4821/**********************************************************************/
4828int get_unit_vision_at(const struct unit *punit, const struct tile *ptile,
4829 enum vision_layer vlayer)
4830{
4833}
4834
4835/**********************************************************************/
4842{
4843 struct vision *uvision = punit->server.vision;
4844 const struct tile *utile = unit_tile(punit);
4845 int mod = unit_vision_range_modifiers(punit, utile);
4846 const v_radius_t radius_sq
4850
4851 vision_change_sight(uvision, radius_sq);
4853}
4854
4855/**********************************************************************/
4864
4865/**********************************************************************/
4870{
4871 time_t dt;
4872
4873 if (!punit) {
4874 return FALSE;
4875 }
4876
4877 if (game.server.unitwaittime <= 0) {
4878 return TRUE;
4879 }
4880
4881 if (punit->server.action_turn != game.info.turn - 1) {
4882 return TRUE;
4883 }
4884
4886 if (dt < game.server.unitwaittime) {
4887 char buf[64];
4890 ftc_server, _("Your unit may not act for another %s "
4891 "this turn. See /help unitwaittime."), buf);
4892 return FALSE;
4893 }
4894
4895 return TRUE;
4896}
4897
4898/**********************************************************************/
4903{
4904 if (!punit) {
4905 return;
4906 }
4907
4910}
4911
4912/**********************************************************************/
4918{
4919 /* check if there is enemy nearby */
4920 square_iterate(&(wld.map), unit_tile(punit), 3, ptile) {
4922 || is_enemy_unit_tile(ptile, unit_owner(punit))) {
4923 return FALSE;
4924 }
4925 }
4927
4928 return TRUE;
4929}
4930
4931/**********************************************************************/
4934void random_movements(struct player *pplayer)
4935{
4936 struct civ_map *nmap = &(wld.map);
4937
4938 game.server.random_move_time = pplayer;
4939
4942 bool moved = TRUE;
4943 struct tile *curtile = unit_tile(punit);
4944 int id = punit->id;
4945
4946 while (moved && unit_is_alive(id) && punit->moves_left > 0) {
4947 /*
4948 * List of all 8 directions
4949 */
4950 enum direction8 dirs[8] = {
4953 };
4954 int n;
4955
4956 moved = FALSE;
4957
4958 for (n = 8; n > 0 && !moved; n--) {
4959 enum direction8 choice = (enum direction8) fc_rand(n);
4960 struct tile *dest = mapstep(&(wld.map), curtile, dirs[choice]);
4961
4962 if (dest != NULL) {
4963 enum gen_action act
4965
4966 if (act != ACTION_NONE) {
4967 if (unit_perform_action(pplayer, id, tile_index(dest), NO_TARGET,
4968 "", act, ACT_REQ_RULES)) {
4969 moved = TRUE;
4970 break;
4971 }
4972 }
4973
4974 if (!moved) {
4976 NULL)) {
4977 if (unit_perform_action(pplayer, id, tile_index(dest),
4979 ACT_REQ_RULES)) {
4980 moved = TRUE;
4981 }
4983 punit, dest, NULL)) {
4984 if (unit_perform_action(pplayer, id, tile_index(dest),
4986 ACT_REQ_RULES)) {
4987 moved = TRUE;
4988 }
4990 punit, dest, NULL)) {
4991 if (unit_perform_action(pplayer, id, tile_index(dest),
4993 ACT_REQ_RULES)) {
4994 moved = TRUE;
4995 }
4996 }
4997 }
4998 }
4999
5000 if (!moved) {
5001 /* Choice was bad, so replace it with the last direction
5002 * in the list. On the next iteration, one fewer choice
5003 * will remain. */
5004 dirs[choice] = dirs[n - 1];
5005 }
5006 }
5007 }
5008 }
5010
5012}
const char * action_name_translation(const struct action *action)
Definition actions.c:1991
bool action_mp_full_makes_legal(const struct unit *actor, const action_id act_id)
Definition actions.c:6292
const char * action_id_name_translation(action_id act_id)
Definition actions.c:2011
bool action_prob_possible(const struct act_prob probability)
Definition actions.c:5821
struct act_prob action_prob_vs_tile(const struct civ_map *nmap, const struct unit *actor_unit, const action_id act_id, const struct tile *target_tile, const struct extra_type *target_extra)
Definition actions.c:5342
enum gen_action select_actres_action_unit_on_stack(struct civ_map *nmap, enum action_result actres, struct unit *punit, struct tile *ptile)
Definition actions.c:8156
const char * action_rule_name(const struct action *action)
Definition actions.c:1977
enum action_sub_target_kind action_get_sub_target_kind(const struct action *paction)
Definition actions.c:1880
bool is_action_enabled_unit_on_tile(const struct civ_map *nmap, const action_id wanted_action, const struct unit *actor_unit, const struct tile *target_tile, const struct extra_type *target_extra)
Definition actions.c:4082
struct act_prob action_prob_vs_extras(const struct civ_map *nmap, const struct unit *actor_unit, const action_id act_id, const struct tile *target_tile, const struct extra_type *target_extra)
Definition actions.c:5425
struct act_prob action_prob_self(const struct civ_map *nmap, const struct unit *actor_unit, const action_id act_id)
Definition actions.c:5495
struct act_prob action_prob_vs_city(const struct civ_map *nmap, const struct unit *actor_unit, const action_id act_id, const struct city *target_city)
Definition actions.c:4984
bool action_id_exists(const action_id act_id)
Definition actions.c:1829
int action_prob_cmp_pessimist(const struct act_prob ap1, const struct act_prob ap2)
Definition actions.c:5881
struct act_prob action_prob_vs_stack(const struct civ_map *nmap, const struct unit *actor_unit, const action_id act_id, const struct tile *target_tile)
Definition actions.c:5259
struct act_prob action_prob_vs_unit(const struct civ_map *nmap, const struct unit *actor_unit, const action_id act_id, const struct unit *target_unit)
Definition actions.c:5067
double action_prob_to_0_to_1_pessimist(const struct act_prob ap)
Definition actions.c:5932
static struct action * action_by_number(action_id act_id)
Definition actions.h:635
#define action_has_result(_act_, _res_)
Definition actions.h:431
#define action_id_get_act_time(act_id, actor_unit, tgt_tile, tgt_extra)
Definition actions.h:676
#define ACTPROB_IMPOSSIBLE
Definition actions.h:922
#define action_id_get_target_kind(act_id)
Definition actions.h:652
#define action_id_has_result_safe(act_id, result)
Definition actions.h:665
#define ACTION_NONE
Definition actions.h:311
void action_consequence_complete(const struct action *paction, struct player *offender, const struct unit_type *offender_utype, struct player *victim_player, const struct tile *victim_tile, const char *victim_link)
void action_consequence_caught(const struct action *paction, struct player *offender, const struct unit_type *offender_utype, struct player *victim_player, const struct tile *victim_tile, const char *victim_link)
struct act_prob action_auto_perf_unit_prob(const enum action_auto_perf_cause cause, struct unit *actor, const struct player *other_player, const struct output_type *eval_output, const struct action *eval_action, const struct tile *target_tile, const struct city *target_city, const struct unit *target_unit, const struct extra_type *target_extra)
const struct action * action_auto_perf_unit_do(const enum action_auto_perf_cause cause, struct unit *actor, const struct player *other_player, const struct output_type *eval_output, const struct action *eval_action, const struct tile *target_tile, const struct city *target_city, const struct unit *target_unit, const struct extra_type *target_extra)
struct unit * action_tgt_unit(struct unit *actor, struct tile *target_tile, bool accept_all_actions)
void action_consequence_success(const struct action *paction, struct player *offender, const struct unit_type *offender_utype, struct player *victim_player, const struct tile *victim_tile, const char *victim_link)
struct city * action_tgt_city(struct unit *actor, struct tile *target_tile, bool accept_all_actions)
int action_sub_target_id_for_action(const struct action *paction, struct unit *actor_unit)
bool actres_removes_extra(enum action_result result, const struct extra_type *pextra)
Definition actres.c:803
bool actres_creates_extra(enum action_result result, const struct extra_type *pextra)
Definition actres.c:782
bool adv_follow_path(struct unit *punit, struct pf_path *path, struct tile *ptile)
Definition advgoto.c:47
#define CALL_FUNC_EACH_AI(_func,...)
Definition ai.h:387
#define CALL_PLR_AI_FUNC(_func, _player,...)
Definition ai.h:377
#define n
Definition astring.c:77
enum unit_move_result manage_auto_explorer(struct unit *punit)
void adv_unit_new_task(struct unit *punit, enum adv_unit_task task, struct tile *ptile)
void dbv_to_bv(unsigned char *dest, const struct dbv *src)
Definition bitvector.c:235
#define BV_CLR_ALL(bv)
Definition bitvector.h:95
#define BV_SET(bv, bit)
Definition bitvector.h:81
#define BV_ISSET(bv, bit)
Definition bitvector.h:78
#define BV_CLR(bv, bit)
Definition bitvector.h:86
bool is_friendly_city_near(const struct civ_map *nmap, const struct player *owner, const struct tile *ptile, int distance)
Definition city.c:2106
const char * city_name_get(const struct city *pcity)
Definition city.c:1137
bool city_exists_within_max_city_map(const struct civ_map *nmap, const struct tile *ptile, bool may_be_on_center)
Definition city.c:2126
static bool is_non_allied_city_tile(const struct tile *ptile, const struct player *pplayer)
Definition city.h:770
#define city_list_iterate(citylist, pcity)
Definition city.h:508
#define city_tile(_pcity_)
Definition city.h:564
static bool is_enemy_city_tile(const struct tile *ptile, const struct player *pplayer)
Definition city.h:734
static citizens city_size_get(const struct city *pcity)
Definition city.h:569
static bool is_allied_city_tile(const struct tile *ptile, const struct player *pplayer)
Definition city.h:746
#define output_type_iterate(output)
Definition city.h:845
#define city_owner(_pcity_)
Definition city.h:563
#define city_list_iterate_end
Definition city.h:510
#define output_type_iterate_end
Definition city.h:851
bool city_map_update_tile_now(struct tile *ptile)
Definition citytools.c:3340
const char * city_name_suggestion(struct player *pplayer, struct tile *ptile)
Definition citytools.c:458
void send_city_info(struct player *dest, struct city *pcity)
Definition citytools.c:2363
struct city * find_closest_city(const struct tile *ptile, const struct city *pexclcity, const struct player *pplayer, bool only_ocean, bool only_continent, bool only_known, bool only_player, bool only_enemy, const struct unit_class *pclass)
Definition citytools.c:856
void refresh_dumb_city(struct city *pcity)
Definition citytools.c:2216
void sync_cities(void)
Definition citytools.c:3349
bool unit_conquer_city(struct unit *punit, struct city *pcity)
Definition citytools.c:1987
void city_units_upkeep(const struct city *pcity)
Definition citytools.c:3163
bool city_reduce_size(struct city *pcity, citizens pop_loss, struct player *destroyer, const char *reason)
Definition cityturn.c:763
bool city_refresh(struct city *pcity)
Definition cityturn.c:159
double unit_win_chance(const struct civ_map *nmap, const struct unit *attacker, const struct unit *defender, const struct action *paction)
Definition combat.c:478
bool is_unit_reachable_at(const struct unit *defender, const struct unit *attacker, const struct tile *location)
Definition combat.c:85
bool is_stack_vulnerable(const struct tile *ptile)
Definition combat.c:989
int get_virtual_defense_power(const struct civ_map *nmap, const struct unit_type *att_type, const struct unit_type *def_type, struct player *def_player, struct tile *ptile, bool fortified, int veteran)
Definition combat.c:712
int unit_bombard_rate(struct unit *punit)
Definition combat.c:1020
int get_total_defense_power(const struct unit *attacker, const struct unit *defender)
Definition combat.c:767
int get_total_attack_power(const struct unit *attacker, const struct unit *defender, const struct action *paction)
Definition combat.c:610
struct unit * get_defender(const struct civ_map *nmap, const struct unit *attacker, const struct tile *ptile, const struct action *paction)
Definition combat.c:836
void get_modified_firepower(const struct civ_map *nmap, const struct unit *attacker, const struct unit *defender, int *att_fp, int *def_fp)
Definition combat.c:409
char * incite_cost
Definition comments.c:75
struct player * conn_get_player(const struct connection *pconn)
Definition connection.c:763
void conn_list_do_unbuffer(struct conn_list *dest)
Definition connection.c:366
void conn_list_do_buffer(struct conn_list *dest)
Definition connection.c:356
bool conn_is_global_observer(const struct connection *pconn)
Definition connection.c:753
#define conn_list_iterate(connlist, pconn)
Definition connection.h:108
#define conn_list_iterate_end
Definition connection.h:110
static void base(QVariant data1, QVariant data2)
Definition dialogs.cpp:3173
static void enter_hut(QVariant data1, QVariant data2)
Definition dialogs.cpp:2792
static void frighten_hut(QVariant data1, QVariant data2)
Definition dialogs.cpp:2852
struct unit struct city struct unit struct tile struct extra_type const struct act_prob *act_probs int actor_unit_id struct unit struct unit * punit
Definition dialogs_g.h:73
int get_target_bonus_effects(struct effect_list *plist, const struct req_context *context, const struct player *other_player, enum effect_type effect_type)
Definition effects.c:748
int get_unit_bonus(const struct unit *punit, enum effect_type effect_type)
Definition effects.c:1070
int get_player_bonus(const struct player *pplayer, enum effect_type effect_type)
Definition effects.c:828
struct extra_type * rand_extra_for_tile(struct tile *ptile, enum extra_cause cause, bool generated)
Definition extras.c:283
struct extra_type * prev_extra_in_tile(const struct tile *ptile, enum extra_rmcause rmcause, const struct player *pplayer, const struct unit *punit)
Definition extras.c:790
struct extra_type * extra_by_number(int id)
Definition extras.c:183
static struct extra_type extras[MAX_EXTRA_TYPES]
Definition extras.c:31
const char * extra_rule_name(const struct extra_type *pextra)
Definition extras.c:203
bool is_extra_removed_by(const struct extra_type *pextra, enum extra_rmcause rmcause)
Definition extras.c:353
#define extra_type_list_iterate(extralist, pextra)
Definition extras.h:165
#define extra_type_by_rmcause_iterate_end
Definition extras.h:358
#define extra_index(_e_)
Definition extras.h:183
#define extra_type_by_rmcause_iterate(_rmcause, _extra)
Definition extras.h:353
#define EXTRA_NONE
Definition extras.h:85
#define extra_type_list_iterate_end
Definition extras.h:167
#define NO_TARGET
Definition fc_types.h:354
#define ACT_TIME_INSTANTANEOUS
Definition fc_types.h:349
@ RPT_CERTAIN
Definition fc_types.h:701
@ AUT_NONE
Definition fc_types.h:370
@ HB_DISABLED
Definition fc_types.h:1290
#define IDENTITY_NUMBER_ZERO
Definition fc_types.h:92
#define PL_(String1, String2, n)
Definition fcintl.h:71
#define _(String)
Definition fcintl.h:67
const char * tile_link(const struct tile *ptile)
const struct ft_color ftc_server
const char * city_link(const struct city *pcity)
const char * unit_link(const struct unit *punit)
const char * unit_tile_link(const struct unit *punit)
#define MAX_LEN_LINK
struct civ_game game
Definition game.c:62
int current_turn_timeout(void)
Definition game.c:848
struct world wld
Definition game.c:63
void game_remove_unit(struct world *gworld, struct unit *punit)
Definition game.c:124
struct unit * game_unit_by_number(int id)
Definition game.c:116
struct city * game_city_by_number(int id)
Definition game.c:107
void increase_timeout_because_unit_moved(void)
Definition gamehand.c:1036
struct city * owner
Definition citydlg.c:226
GType type
Definition repodlgs.c:1313
bool has_handicap(const struct player *pplayer, enum handicap_type htype)
Definition handicaps.c:66
@ H_MAP
Definition handicaps.h:28
@ H_LIMITEDHUTS
Definition handicaps.h:20
void idex_register_unit(struct world *iworld, struct unit *punit)
Definition idex.c:82
const char * name
Definition inputfile.c:127
#define fc_assert_msg(condition, message,...)
Definition log.h:181
#define fc_assert_ret(condition)
Definition log.h:191
#define fc_assert_exit_msg(condition, message,...)
Definition log.h:211
#define log_test
Definition log.h:136
#define log_verbose(message,...)
Definition log.h:109
#define fc_assert(condition)
Definition log.h:176
#define fc_assert_ret_val(condition, val)
Definition log.h:194
#define fc_assert_action(condition, action)
Definition log.h:187
#define log_debug(message,...)
Definition log.h:115
#define log_error(message,...)
Definition log.h:103
int sq_map_distance(const struct tile *tile0, const struct tile *tile1)
Definition map.c:641
bool is_safe_ocean(const struct civ_map *nmap, const struct tile *ptile)
Definition map.c:667
struct tile * index_to_tile(const struct civ_map *imap, int mindex)
Definition map.c:456
bool same_pos(const struct tile *tile1, const struct tile *tile2)
Definition map.c:949
struct tile * mapstep(const struct civ_map *nmap, const struct tile *ptile, enum direction8 dir)
Definition map.c:371
bool base_get_direction_for_step(const struct civ_map *nmap, const struct tile *start_tile, const struct tile *end_tile, enum direction8 *dir)
Definition map.c:1337
#define adjc_iterate_end
Definition map.h:433
#define square_iterate(nmap, center_tile, radius, tile_itr)
Definition map.h:391
#define adjc_iterate(nmap, center_tile, itr_tile)
Definition map.h:428
#define square_iterate_end
Definition map.h:394
static int map_move_cost_unit(const struct civ_map *nmap, struct unit *punit, const struct tile *ptile)
Definition map.h:293
#define circle_iterate(nmap, center_tile, sq_radius, tile_itr)
Definition map.h:401
#define circle_iterate_end
Definition map.h:404
void vision_clear_sight(struct vision *vision)
Definition maphand.c:2543
void destroy_extra(struct tile *ptile, struct extra_type *pextra)
Definition maphand.c:2631
void bounce_units_on_terrain_change(struct tile *ptile)
Definition maphand.c:1929
void tile_claim_bases(struct tile *ptile, struct player *powner)
Definition maphand.c:2230
void create_extra(struct tile *ptile, struct extra_type *pextra, struct player *pplayer)
Definition maphand.c:2566
void tile_change_side_effects(struct tile *ptile, bool refresh_city)
Definition maphand.c:2734
bool map_is_known_and_seen(const struct tile *ptile, const struct player *pplayer, enum vision_layer vlayer)
Definition maphand.c:930
void map_show_circle(struct player *pplayer, struct tile *ptile, int radius_sq)
Definition maphand.c:874
void update_tile_knowledge(struct tile *ptile)
Definition maphand.c:1449
void check_terrain_change(struct tile *ptile, struct terrain *oldter)
Definition maphand.c:2005
struct player_tile * map_get_player_tile(const struct tile *ptile, const struct player *pplayer)
Definition maphand.c:1392
void vision_change_sight(struct vision *vision, const v_radius_t radius_sq)
Definition maphand.c:2531
#define map_get_player_site(_ptile_, _pplayer_)
Definition maphand.h:93
#define FC_FREE(ptr)
Definition mem.h:41
#define fc_malloc(sz)
Definition mem.h:34
bool can_exist_at_tile(const struct civ_map *nmap, const struct unit_type *utype, const struct tile *ptile)
Definition movement.c:279
bool can_unit_exist_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Definition movement.c:351
bool is_native_tile(const struct unit_type *punittype, const struct tile *ptile)
Definition movement.c:363
int unit_move_rate(const struct unit *punit)
Definition movement.c:90
bool unit_could_load_at(const struct unit *punit, const struct tile *ptile)
Definition movement.c:943
bool is_native_to_class(const struct unit_class *punitclass, const struct terrain *pterrain, const bv_extras *extras)
Definition movement.c:374
bool can_unit_survive_at_tile(const struct civ_map *nmap, const struct unit *punit, const struct tile *ptile)
Definition movement.c:523
@ MR_OK
Definition movement.h:35
@ MR_NOT_ALLOWED
Definition movement.h:53
@ MR_DEATH
Definition movement.h:36
static const char * name_translation_get(const struct name_translation *ptrans)
const char * nation_rule_name(const struct nation_type *pnation)
Definition nation.c:138
struct nation_type * nation_of_unit(const struct unit *punit)
Definition nation.c:463
const char * nation_adjective_for_player(const struct player *pplayer)
Definition nation.c:169
const char * nation_plural_for_player(const struct player *pplayer)
Definition nation.c:178
void notify_player(const struct player *pplayer, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
Definition notify.c:291
void notify_conn(struct conn_list *dest, const struct tile *ptile, enum event_type event, const struct ft_color color, const char *format,...)
Definition notify.c:238
unit_info_use
Definition packets.h:66
@ UNIT_INFO_IDENTITY
Definition packets.h:67
int send_packet_unit_remove(struct connection *pc, const struct packet_unit_remove *packet)
void lsend_packet_unit_remove(struct conn_list *dest, const struct packet_unit_remove *packet)
void dlsend_packet_unit_remove(struct conn_list *dest, int unit_id)
int send_packet_unit_short_info(struct connection *pc, const struct packet_unit_short_info *packet, bool force_to_send)
int send_packet_unit_info(struct connection *pc, const struct packet_unit_info *packet)
void pf_path_destroy(struct pf_path *path)
struct pf_map * pf_map_new(const struct pf_parameter *parameter)
struct pf_path * pf_map_path(struct pf_map *pfm, struct tile *ptile)
void pf_map_destroy(struct pf_map *pfm)
#define pf_map_move_costs_iterate_end
#define pf_map_move_costs_iterate(ARG_pfm, NAME_tile, NAME_cost, COND_from_start)
void pft_fill_unit_parameter(struct pf_parameter *parameter, const struct civ_map *nmap, const struct unit *punit)
Definition pf_tools.c:839
struct unit * player_unit_by_number(const struct player *pplayer, int unit_id)
Definition player.c:1229
struct player * player_by_number(const int player_id)
Definition player.c:849
bool can_player_see_unit_at(const struct player *pplayer, const struct unit *punit, const struct tile *ptile, bool is_transported)
Definition player.c:1034
int player_slot_count(void)
Definition player.c:418
int player_number(const struct player *pplayer)
Definition player.c:837
const char * player_name(const struct player *pplayer)
Definition player.c:895
bool can_player_see_unit(const struct player *pplayer, const struct unit *punit)
Definition player.c:1104
bool pplayers_at_war(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1388
int player_index(const struct player *pplayer)
Definition player.c:829
struct player_diplstate * player_diplstate_get(const struct player *plr1, const struct player *plr2)
Definition player.c:324
bool pplayers_allied(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1405
struct player_slot * slots
Definition player.c:51
bool pplayers_non_attack(const struct player *pplayer, const struct player *pplayer2)
Definition player.c:1459
#define players_iterate_end
Definition player.h:537
#define players_iterate(_pplayer)
Definition player.h:532
static bool is_barbarian(const struct player *pplayer)
Definition player.h:489
#define is_ai(plr)
Definition player.h:230
#define is_human(plr)
Definition player.h:229
void player_update_last_war_action(struct player *pplayer)
Definition plrhand.c:866
void player_status_add(struct player *plr, enum player_status pstatus)
Definition plrhand.c:3217
void maybe_make_contact(struct tile *ptile, struct player *pplayer)
Definition plrhand.c:2365
void send_player_info_c(struct player *src, struct conn_list *dest)
Definition plrhand.c:1147
struct conn_list * player_reply_dest(struct player *pplayer)
Definition plrhand.c:1595
void player_loot_player(struct player *pvictor, struct player *pvictim)
Definition plrhand.c:411
#define phase_players_iterate(pplayer)
Definition plrhand.h:113
#define phase_players_iterate_end
Definition plrhand.h:118
#define fc_rand(_size)
Definition rand.h:56
bool are_reqs_active(const struct req_context *context, const struct player *other_player, const struct requirement_vector *reqs, const enum req_problem_type prob_type)
void script_server_signal_emit(const char *signal_name,...)
void script_server_remove_exported_object(void *object)
void flush_packets(void)
Definition sernet.c:381
void format_time_duration(time_t t, char *buf, int maxlen)
Definition shared.c:1976
#define CLIP(lower, current, upper)
Definition shared.h:57
#define ARRAY_SIZE(x)
Definition shared.h:85
#define MIN(x, y)
Definition shared.h:55
#define MAX(x, y)
Definition shared.h:54
int identity_number(void)
Definition srv_main.c:2020
struct act_prob prob
Definition unittools.c:115
Definition city.h:320
int id
Definition city.h:326
struct tile * tile
Definition city.h:322
struct unit_list * units_supported
Definition city.h:406
int unitwaittime
Definition game.h:202
struct player * random_move_time
Definition game.h:279
struct conn_list * est_connections
Definition game.h:97
struct packet_game_info info
Definition game.h:89
int autoupgrade_veteran_loss
Definition game.h:136
int killunhomed
Definition game.h:158
struct civ_game::@31::@35 server
int ransom_gold
Definition game.h:179
bool autoattack
Definition game.h:135
int timeoutaddenemymove
Definition game.h:212
enum gameloss_style gameloss_style
enum goods_selection_method goods_selection
enum happyborders_type happyborders
int nuke_defender_survival_chance_pct
bool only_real_fight_makes_veteran
enum airlifting_style airlifting_style
bool only_killing_makes_veteran
bool combat_odds_scaled_veterancy
int upkeep[O_LAST]
enum action_decision action_decision_want
struct unit_order orders[MAX_LEN_ROUTE]
enum unit_activity activity
Unit_type_id type
enum unit_activity changed_from
enum direction8 facing
enum server_side_agent ssa_controller
enum direction8 facing
enum diplstate_type type
Definition player.h:197
int units_used
Definition player.h:106
int units_lost
Definition player.h:104
struct unit_list * units
Definition player.h:280
struct conn_list * connections
Definition player.h:296
int huts
Definition player.h:347
bool is_alive
Definition player.h:266
struct player_economic economic
Definition player.h:282
struct player_score score
Definition player.h:281
struct player::@70::@72 server
const struct player * player
Definition tile.h:50
struct unit_list * units
Definition tile.h:58
bv_player can_see_unit
Definition unittools.c:95
struct player * powner
Definition unittools.c:94
bv_player can_see_move
Definition unittools.c:96
struct unit * punit
Definition unittools.c:93
struct vision * old_vision
Definition unittools.c:97
enum unit_activity activity
Definition unit.h:93
enum unit_orders order
Definition unit.h:92
int action
Definition unit.h:99
enum direction8 dir
Definition unit.h:101
int target
Definition unit.h:96
int sub_target
Definition unit.h:97
int vision_radius_sq
Definition unittype.h:522
enum vision_layer vlayer
Definition unittype.h:569
Definition unit.h:137
time_t action_timestamp
Definition unit.h:244
int length
Definition unit.h:194
int upkeep[O_LAST]
Definition unit.h:147
bool has_orders
Definition unit.h:192
struct unit::@80 orders
enum action_decision action_decision_want
Definition unit.h:201
int battlegroup
Definition unit.h:190
enum unit_activity activity
Definition unit.h:156
int moves_left
Definition unit.h:149
int id
Definition unit.h:144
bool moved
Definition unit.h:172
int index
Definition unit.h:194
struct vision * vision
Definition unit.h:243
bool vigilant
Definition unit.h:196
int hp
Definition unit.h:150
int fuel
Definition unit.h:152
struct extra_type * changed_from_target
Definition unit.h:169
int current_form_turn
Definition unit.h:207
bool stay
Definition unit.h:204
enum direction8 facing
Definition unit.h:141
struct unit::@81::@84 server
struct tile * tile
Definition unit.h:139
struct extra_type * activity_target
Definition unit.h:163
int activity_count
Definition unit.h:161
struct unit_order * list
Definition unit.h:197
enum unit_activity changed_from
Definition unit.h:167
struct player * nationality
Definition unit.h:143
bool repeat
Definition unit.h:195
void(* removal_callback)(struct unit *punit)
Definition unit.h:252
struct unit_move_data * moving
Definition unit.h:246
int action_turn
Definition unit.h:245
int homecity
Definition unit.h:145
bool paradropped
Definition unit.h:173
bool done_moving
Definition unit.h:180
int birth_turn
Definition unit.h:206
struct goods_type * carrying
Definition unit.h:185
struct tile * goto_tile
Definition unit.h:154
struct tile * action_decision_tile
Definition unit.h:202
const struct unit_type * utype
Definition unit.h:138
int veteran
Definition unit.h:151
int changed_from_count
Definition unit.h:168
bool dying
Definition unit.h:249
enum server_side_agent ssa_controller
Definition unit.h:171
struct civ_map map
#define sz_strlcpy(dest, src)
Definition support.h:195
#define TRUE
Definition support.h:46
#define FALSE
Definition support.h:47
#define fc__fallthrough
Definition support.h:119
const char * terrain_name_translation(const struct terrain *pterrain)
Definition terrain.c:238
struct extra_type * get_preferred_pillage(bv_extras extras)
Definition terrain.c:550
void tile_add_extra(struct tile *ptile, const struct extra_type *pextra)
Definition tile.c:955
bool tile_has_claimable_base(const struct tile *ptile, const struct unit_type *punittype)
Definition tile.c:215
bool tile_apply_activity(struct tile *ptile, Activity_type_id act, struct extra_type *tgt)
Definition tile.c:681
int tile_extras_defense_bonus(const struct tile *ptile, const struct unit_type *punittype)
Definition tile.c:233
int tile_activity_time(enum unit_activity activity, const struct tile *ptile, const struct extra_type *tgt)
Definition tile.c:418
int tile_has_not_aggressive_extra_for_unit(const struct tile *ptile, const struct unit_type *punittype)
Definition tile.c:196
struct city * tile_city(const struct tile *ptile)
Definition tile.c:83
#define tile_index(_pt_)
Definition tile.h:88
#define tile_terrain(_tile)
Definition tile.h:114
#define TILE_XY(ptile)
Definition tile.h:43
static const bv_extras * tile_extras(const struct tile *ptile)
Definition tile.h:124
#define tile_continent(_tile)
Definition tile.h:92
#define tile_has_extra(ptile, pextra)
Definition tile.h:151
#define tile_owner(_tile)
Definition tile.h:96
struct goods_type * goods_from_city_to_unit(struct city *src, struct unit *punit)
Goods_type_id goods_index(const struct goods_type *pgood)
struct unit * tile_non_allied_unit(const struct tile *ptile, const struct player *pplayer)
Definition unit.c:1314
int get_transporter_occupancy(const struct unit *ptrans)
Definition unit.c:1791
void free_unit_orders(struct unit *punit)
Definition unit.c:1777
bool unit_is_alive(int id)
Definition unit.c:2261
int get_activity_rate_this_turn(const struct unit *punit)
Definition unit.c:534
bool unit_transport_load(struct unit *pcargo, struct unit *ptrans, bool force)
Definition unit.c:2378
void set_unit_activity(struct unit *punit, enum unit_activity new_activity)
Definition unit.c:1096
struct player * unit_nationality(const struct unit *punit)
Definition unit.c:1253
struct unit * unit_transport_get(const struct unit *pcargo)
Definition unit.c:2449
bool unit_transport_unload(struct unit *pcargo)
Definition unit.c:2398
int unit_gain_hitpoints(const struct unit *punit)
Definition unit.c:2191
bool can_unit_continue_current_activity(const struct civ_map *nmap, struct unit *punit)
Definition unit.c:849
struct unit * unit_virtual_create(struct player *pplayer, struct city *pcity, const struct unit_type *punittype, int veteran_level)
Definition unit.c:1632
bool can_unit_do_activity(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity)
Definition unit.c:876
void unit_virtual_destroy(struct unit *punit)
Definition unit.c:1737
void unit_tile_set(struct unit *punit, struct tile *ptile)
Definition unit.c:1263
enum unit_upgrade_result unit_upgrade_test(const struct civ_map *nmap, const struct unit *punit, bool is_free)
Definition unit.c:1996
bool unit_transported(const struct unit *pcargo)
Definition unit.c:2433
bool can_unit_unload(const struct unit *pcargo, const struct unit *ptrans)
Definition unit.c:760
struct unit_list * unit_transport_cargo(const struct unit *ptrans)
Definition unit.c:2459
bool unit_has_orders(const struct unit *punit)
Definition unit.c:210
struct unit * transporter_for_unit(const struct unit *pcargo)
Definition unit.c:1920
bool unit_can_convert(const struct civ_map *nmap, const struct unit *punit)
Definition unit.c:2043
bool activity_requires_target(enum unit_activity activity)
Definition unit.c:574
bool can_unit_do_activity_targeted(const struct civ_map *nmap, const struct unit *punit, enum unit_activity activity, struct extra_type *target)
Definition unit.c:915
#define unit_tile(_pu)
Definition unit.h:396
static bool is_enemy_unit_tile(const struct tile *ptile, const struct player *pplayer)
Definition unit.h:419
#define unit_cargo_iterate_end
Definition unit.h:572
@ ORDER_ACTION_MOVE
Definition unit.h:44
@ ORDER_ACTIVITY
Definition unit.h:40
@ ORDER_FULL_MP
Definition unit.h:42
@ ORDER_MOVE
Definition unit.h:38
@ ORDER_LAST
Definition unit.h:48
@ ORDER_PERFORM_ACTION
Definition unit.h:46
static bool is_allied_unit_tile(const struct tile *ptile, const struct player *pplayer)
Definition unit.h:407
#define unit_cargo_iterate(_ptrans, _pcargo)
Definition unit.h:569
#define CHECK_UNIT(punit)
Definition unit.h:269
#define unit_owner(_pu)
Definition unit.h:395
@ UU_OK
Definition unit.h:60
#define unit_home(_pu_)
Definition unit.h:393
static bool is_non_allied_unit_tile(const struct tile *ptile, const struct player *pplayer)
Definition unit.h:431
void illegal_action_msg(struct player *pplayer, const enum event_type event, struct unit *actor, const action_id stopped_action, const struct tile *target_tile, const struct city *target_city, const struct unit *target_unit)
Definition unithand.c:2492
bool unit_activity_handling(struct unit *punit, enum unit_activity new_activity)
Definition unithand.c:6471
bool unit_perform_action(struct player *pplayer, const int actor_id, const int target_id, const int sub_tgt_id_incoming, const char *name, const action_id action_type, const enum action_requester requester)
Definition unithand.c:3313
bool unit_move_handling(struct unit *punit, struct tile *pdesttile, bool move_do_not_act)
Definition unithand.c:5533
#define unit_list_iterate(unitlist, punit)
Definition unitlist.h:31
#define unit_list_iterate_safe(unitlist, _unit)
Definition unitlist.h:39
#define unit_list_iterate_end
Definition unitlist.h:33
#define unit_list_iterate_safe_end
Definition unitlist.h:61
void unit_bombs_unit(struct unit *attacker, struct unit *defender, int *att_hp, int *def_hp, const struct action *paction)
Definition unittools.c:356
void unit_did_action(struct unit *punit)
Definition unittools.c:4902
static void autoattack_prob_free(struct autoattack_prob *prob)
Definition unittools.c:3303
void collect_ransom(struct unit *pcollector, struct unit *punit, bool vet)
Definition unittools.c:2496
bool do_airline(struct unit *punit, struct city *pdest_city, const struct action *paction)
Definition unittools.c:2913
void place_partisans(struct tile *pcenter, struct player *powner, int count, int sq_radius)
Definition unittools.c:1158
static bool try_to_save_unit(struct unit *punit, const struct unit_type *pttype, bool helpless, bool teleporting, const struct city *pexclcity)
Definition unittools.c:2145
static struct unit_move_data_list * construct_move_data_list(struct unit *punit, const struct tile *psrctile, struct tile *pdesttile, bool adj)
Definition unittools.c:3841
bool execute_orders(struct unit *punit, const bool fresh)
Definition unittools.c:4356
static void unit_convert(struct unit *punit)
Definition unittools.c:771
void update_unit_activities(struct player *pplayer)
Definition unittools.c:667
static void throw_units_from_illegal_cities(struct player *pplayer, bool verbose)
Definition unittools.c:1302
#define unit_move_data_list_iterate_rev_end
Definition unittools.c:107
void transform_unit(struct unit *punit, const struct unit_type *to_unit, int vet_loss)
Definition unittools.c:1547
void random_movements(struct player *pplayer)
Definition unittools.c:4934
bool unit_versus_unit(struct unit *attacker, struct unit *defender, int *att_hp, int *def_hp, int *att_vet, int *def_vet, const struct action *paction)
Definition unittools.c:288
static void server_remove_unit_full(struct unit *punit, bool transported, enum unit_loss_reason reason)
Definition unittools.c:1788
struct unit_list * get_units_seen_via_ally(const struct player *pplayer, const struct player *aplayer)
Definition unittools.c:1411
static bool is_refuel_tile(const struct tile *ptile, const struct player *pplayer, const struct unit *punit)
Definition unittools.c:1483
static void unit_lost_with_transport(const struct player *pplayer, struct unit *pcargo, const struct unit_type *ptransport, struct player *killer)
Definition unittools.c:1913
void remove_allied_visibility(struct player *pplayer, struct player *aplayer, const struct unit_list *seen_units)
Definition unittools.c:1446
static bool maybe_cancel_goto_due_to_enemy(struct unit *punit, struct tile *ptile)
Definition unittools.c:4287
void combat_veterans(struct unit *attacker, struct unit *defender, bool powerless, int att_vet, int def_vet)
Definition unittools.c:401
void resolve_unit_stacks(struct player *pplayer, struct player *aplayer, bool verbose)
Definition unittools.c:1396
void do_nuclear_explosion(const struct action *paction, const struct unit_type *act_utype, struct player *pplayer, struct tile *ptile)
Definition unittools.c:2881
static void unit_move_data_unref(struct unit_move_data *pdata)
Definition unittools.c:3820
void unit_set_removal_callback(struct unit *punit, void(*callback)(struct unit *punit))
Definition unittools.c:1765
static void unit_enter_hut(struct unit *punit, bool frighten_hut)
Definition unittools.c:3180
void package_unit(struct unit *punit, struct packet_unit_info *packet)
Definition unittools.c:2586
#define unit_move_data_list_iterate_end
Definition unittools.c:104
void unit_goes_out_of_sight(struct player *pplayer, struct unit *punit)
Definition unittools.c:2711
static void unit_restore_movepoints(struct player *pplayer, struct unit *punit)
Definition unittools.c:658
static void wakeup_neighbor_sentries(struct unit *punit)
Definition unittools.c:3502
static int total_activity(struct tile *ptile, enum unit_activity act, struct extra_type *tgt)
Definition unittools.c:715
void unit_activities_cancel_all_illegal_plr(const struct player *pplayer)
Definition unittools.c:815
static void unit_transport_load_tp_status(struct unit *punit, struct unit *ptrans, bool force)
Definition unittools.c:3261
#define autoattack_prob_list_iterate_safe_end
Definition unittools.c:131
static bool unit_survive_autoattack(struct unit *punit)
Definition unittools.c:3359
void finalize_unit_phase_beginning(struct player *pplayer)
Definition unittools.c:699
void execute_unit_orders(struct player *pplayer)
Definition unittools.c:677
static bool find_a_good_partisan_spot(struct tile *pcenter, struct player *powner, struct unit_type *u_type, int sq_radius, struct tile **dst_tile)
Definition unittools.c:1110
void send_unit_info(struct conn_list *dest, struct unit *punit)
Definition unittools.c:2724
static void wipe_unit_full(struct unit *punit, bool transported, enum unit_loss_reason reason, struct player *killer)
Definition unittools.c:1937
static void resolve_stack_conflicts(struct player *pplayer, struct player *aplayer, bool verbose)
Definition unittools.c:1367
static void check_unit_activity(struct unit *punit)
Definition unittools.c:3705
static void unit_restore_hitpoints(struct unit *punit)
Definition unittools.c:631
static void do_upgrade_effects(struct player *pplayer)
Definition unittools.c:425
static struct unit_move_data * unit_move_data_get(struct unit *punit, const struct tile *psrctile, const struct tile *pdesttile)
Definition unittools.c:3734
bool place_unit(struct unit *punit, struct player *pplayer, struct city *pcity, struct unit *ptrans, bool force)
Definition unittools.c:1707
void notify_unit_experience(struct unit *punit)
Definition unittools.c:744
void unit_activities_cancel(struct unit *punit)
Definition unittools.c:797
void package_short_unit(struct unit *punit, struct packet_unit_short_info *packet, enum unit_info_use packet_use, int info_city_id)
Definition unittools.c:2667
struct unit * unit_virtual_prepare(struct player *pplayer, struct tile *ptile, const struct unit_type *type, int veteran_level, int homecity_id, int moves_left, int hp_left)
Definition unittools.c:1659
void give_allied_visibility(struct player *pplayer, struct player *aplayer)
Definition unittools.c:1470
void unit_forget_last_activity(struct unit *punit)
Definition unittools.c:1050
#define unit_move_data_list_iterate(_plist, _pdata)
Definition unittools.c:102
struct unit * create_unit_full(struct player *pplayer, struct tile *ptile, const struct unit_type *type, int veteran_level, int homecity_id, int moves_left, int hp_left, struct unit *ptrans)
Definition unittools.c:1626
static void server_remove_unit(struct unit *punit, enum unit_loss_reason reason)
Definition unittools.c:1904
void unit_unset_removal_callback(struct unit *punit)
Definition unittools.c:1779
int get_unit_vision_at(const struct unit *punit, const struct tile *ptile, enum vision_layer vlayer)
Definition unittools.c:4828
bool maybe_make_veteran(struct unit *punit, int base_chance)
Definition unittools.c:219
void unit_refresh_vision(struct unit *punit)
Definition unittools.c:4841
struct unit * create_unit(struct player *pplayer, struct tile *ptile, const struct unit_type *type, int veteran_level, int homecity_id, int moves_left)
Definition unittools.c:1599
void do_explore(struct unit *punit)
Definition unittools.c:2946
void unit_transport_load_send(struct unit *punit, struct unit *ptrans)
Definition unittools.c:3231
bool unit_can_do_action_now(const struct unit *punit)
Definition unittools.c:4869
#define autoattack_prob_list_iterate_safe(autoattack_prob_list, _aap_, _unit_)
Definition unittools.c:122
void wipe_unit(struct unit *punit, enum unit_loss_reason reason, struct player *killer)
Definition unittools.c:2133
void unit_tc_effect_refresh(struct player *pplayer)
Definition unittools.c:689
bool do_paradrop(struct unit *punit, struct tile *ptile, const struct action *paction)
Definition unittools.c:2982
static bool player_is_watching(struct unit *punit, const bool fresh)
Definition unittools.c:4329
static int get_unit_vision_base(const struct unit *punit, enum vision_layer vlayer, const int base)
Definition unittools.c:4783
void player_restore_units(struct player *pplayer)
Definition unittools.c:478
#define DIST
bool is_refuel_point(const struct tile *ptile, const struct player *pplayer, const struct unit *punit)
Definition unittools.c:1525
static int unit_vision_range_modifiers(const struct unit *punit, const struct tile *ptile)
Definition unittools.c:4805
static void update_unit_activity(struct unit *punit)
Definition unittools.c:855
#define unit_move_data_list_iterate_rev(_plist, _pdata)
Definition unittools.c:105
void unit_transport_unload_send(struct unit *punit)
Definition unittools.c:3283
bool is_unit_being_refueled(const struct unit *punit)
Definition unittools.c:1516
struct unit_type * find_a_unit_type(enum unit_role_id role, enum unit_role_id role_tech)
Definition unittools.c:175
static void cancel_orders(struct unit *punit, char *dbg_msg)
Definition unittools.c:3491
bool unit_can_be_retired(struct unit *punit)
Definition unittools.c:4917
static bool total_activity_done(struct tile *ptile, enum unit_activity act, struct extra_type *tgt)
Definition unittools.c:735
void unit_activities_cancel_all_illegal_tile(const struct tile *ptile)
Definition unittools.c:827
struct unit * unit_change_owner(struct unit *punit, struct player *pplayer, int homecity, enum unit_loss_reason reason)
Definition unittools.c:2193
bool teleport_unit_to_city(struct unit *punit, struct city *pcity, int move_cost, bool verbose)
Definition unittools.c:1184
static void do_nuke_tile(struct player *pplayer, struct tile *ptile, const struct action *paction)
Definition unittools.c:2796
static bool hut_get_limited(struct unit *punit)
Definition unittools.c:3145
void unit_assign_specific_activity_target(struct unit *punit, enum unit_activity *activity, struct extra_type **target)
Definition unittools.c:1079
static int compare_units(const struct autoattack_prob *const *p1, const struct autoattack_prob *const *q1)
Definition unittools.c:3317
void unit_activities_cancel_all_illegal_area(const struct tile *ptile)
Definition unittools.c:842
static bool unit_move_consequences(struct unit *punit, struct tile *src_tile, struct tile *dst_tile, bool passenger, bool conquer_city_allowed)
Definition unittools.c:3572
void unit_get_goods(struct unit *punit)
Definition unittools.c:1610
void send_all_known_units(struct conn_list *dest)
Definition unittools.c:2768
static bool maybe_become_veteran_real(struct unit *punit, int base_chance, bool settler)
Definition unittools.c:237
static bool maybe_cancel_patrol_due_to_enemy(struct unit *punit)
Definition unittools.c:4300
bool unit_move(struct unit *punit, struct tile *pdesttile, int move_cost, struct unit *embark_to, bool find_embark_target, bool conquer_city_allowed, bool conquer_extras_allowed, bool enter_hut, bool frighten_hut)
Definition unittools.c:3920
static void unit_move_by_data(struct unit_move_data *pdata, const struct tile *psrctile, struct tile *pdesttile)
Definition unittools.c:3769
void unit_list_refresh_vision(struct unit_list *punitlist)
Definition unittools.c:4858
void kill_unit(struct unit *pkiller, struct unit *punit, bool vet)
Definition unittools.c:2260
void bounce_unit(struct unit *punit, bool verbose)
Definition unittools.c:1222
bool unit_activity_needs_target_from_client(enum unit_activity activity)
Definition unittools.c:1059
const struct unit_type * unit_type_get(const struct unit *punit)
Definition unittype.c:123
const char * unit_name_translation(const struct unit *punit)
Definition unittype.c:1569
bool can_player_build_unit_direct(const struct player *p, const struct unit_type *punittype, bool consider_reg_impr_req)
Definition unittype.c:1968
struct unit_type * get_role_unit(int role, int role_index)
Definition unittype.c:2253
const char * unit_rule_name(const struct unit *punit)
Definition unittype.c:1587
const struct veteran_system * utype_veteran_system(const struct unit_type *punittype)
Definition unittype.c:2568
int num_role_units(int role)
Definition unittype.c:2203
bool utype_player_already_has_this_unique(const struct player *pplayer, const struct unit_type *putype)
Definition unittype.c:1927
const struct unit_type * can_upgrade_unittype(const struct player *pplayer, const struct unit_type *punittype)
Definition unittype.c:1703
struct unit_class * unit_class_get(const struct unit *punit)
Definition unittype.c:2499
bool unit_has_type_flag(const struct unit *punit, enum unit_type_flag_id flag)
Definition unittype.c:190
const struct veteran_level * vsystem_veteran_level(const struct veteran_system *vsystem, int level)
Definition unittype.c:2585
Unit_type_id utype_number(const struct unit_type *punittype)
Definition unittype.c:100
const char * utype_name_translation(const struct unit_type *punittype)
Definition unittype.c:1560
#define utype_class(_t_)
Definition unittype.h:749
#define utype_fuel(ptype)
Definition unittype.h:839
static bool utype_has_flag(const struct unit_type *punittype, int flag)
Definition unittype.h:617
#define U_LAST
Definition unittype.h:40
struct vision * vision_new(struct player *pplayer, struct tile *ptile)
Definition vision.c:33
void vision_free(struct vision *vision)
Definition vision.c:50
#define ASSERT_VISION(v)
Definition vision.h:97
#define V_RADIUS(main_sq, invis_sq, subs_sq)
Definition vision.h:95
#define vision_site_owner(v)
Definition vision.h:128
short int v_radius_t[V_COUNT]
Definition vision.h:82