Split "Establish Embassy".
[freeciv.git] / server / unithand.c
blobd156b01d682d0b31a022e1255e8ee387d6c223a6
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.
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 ***********************************************************************/
14 #ifdef HAVE_CONFIG_H
15 #include <fc_config.h>
16 #endif
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
22 /* utility */
23 #include "astring.h"
24 #include "fcintl.h"
25 #include "mem.h"
26 #include "rand.h"
27 #include "shared.h"
29 /* common */
30 #include "actions.h"
31 #include "ai.h"
32 #include "city.h"
33 #include "combat.h"
34 #include "events.h"
35 #include "game.h"
36 #include "log.h"
37 #include "map.h"
38 #include "movement.h"
39 #include "packets.h"
40 #include "player.h"
41 #include "research.h"
42 #include "specialist.h"
43 #include "traderoutes.h"
44 #include "unit.h"
45 #include "unitlist.h"
47 /* common/scriptcore */
48 #include "luascript_types.h"
50 /* server */
51 #include "actiontools.h"
52 #include "barbarian.h"
53 #include "citizenshand.h"
54 #include "citytools.h"
55 #include "cityturn.h"
56 #include "diplomats.h"
57 #include "maphand.h"
58 #include "notify.h"
59 #include "plrhand.h"
60 #include "sanitycheck.h"
61 #include "spacerace.h"
62 #include "srv_main.h"
63 #include "techtools.h"
64 #include "unittools.h"
66 /* server/advisors */
67 #include "autoexplorer.h"
68 #include "autosettlers.h"
70 /* server/scripting */
71 #include "script_server.h"
73 #include "unithand.h"
75 /* An explanation why an action isn't enabled. */
76 struct ane_expl {
77 /* The kind of reason why an action isn't enabled. */
78 enum ane_kind kind;
80 union {
81 /* The city without the needed capacity- */
82 struct city *capacity_city;
84 /* The bad terrain in question. */
85 struct terrain *no_act_terrain;
87 /* The player to advice declaring war on. */
88 struct player *no_war_with;
90 /* The nation that can't be involved. */
91 struct nation_type *no_act_nation;
93 /* The action that blocks the action. */
94 struct action *blocker;
96 /* The required distance. */
97 int distance;
101 static void illegal_action(struct player *pplayer,
102 struct unit *actor,
103 enum gen_action stopped_action,
104 struct player *tgt_player,
105 const struct tile *target_tile,
106 const struct city *target_city,
107 const struct unit *target_unit,
108 const enum action_requester requester);
109 static bool city_add_unit(struct player *pplayer, struct unit *punit,
110 struct city *pcity);
111 static bool city_build(struct player *pplayer, struct unit *punit,
112 struct tile *ptile, const char *name);
113 static bool do_unit_establish_trade(struct player *pplayer,
114 struct unit *punit,
115 struct city *pcity_dest,
116 bool est_if_able);
118 static bool unit_do_recycle(struct player *pplayer,
119 struct unit *punit,
120 struct city *pcity);
121 static bool do_unit_help_build_wonder(struct player *pplayer,
122 struct unit *punit,
123 struct city *pcity_dest);
124 static bool unit_bombard(struct unit *punit, struct tile *ptile);
125 static bool unit_nuke(struct player *pplayer, struct unit *punit,
126 struct tile *def_tile);
127 static bool unit_do_destroy_city(struct player *act_player,
128 struct unit *act_unit,
129 struct city *tgt_city);
130 static bool do_unit_disband(struct player *pplayer, struct unit *punit);
131 static bool do_unit_change_homecity(struct unit *punit,
132 struct city *pcity);
133 static bool do_unit_upgrade(struct player *pplayer,
134 struct unit *punit, struct city *pcity,
135 enum action_requester ordered_by);
136 static bool do_attack(struct unit *actor_unit, struct tile *target_tile);
137 static bool do_unit_conquer_city(struct player *act_player,
138 struct unit *act_unit,
139 struct city *tgt_city,
140 struct action *paction);
142 /**************************************************************************
143 Upgrade all units of a given type.
144 **************************************************************************/
145 void handle_unit_type_upgrade(struct player *pplayer, Unit_type_id uti)
147 struct unit_type *to_unittype;
148 struct unit_type *from_unittype = utype_by_number(uti);
149 int number_of_upgraded_units = 0;
151 if (NULL == from_unittype) {
152 /* Probably died or bribed. */
153 log_verbose("handle_unit_type_upgrade() invalid unit type %d", uti);
154 return;
157 to_unittype = can_upgrade_unittype(pplayer, from_unittype);
158 if (!to_unittype) {
159 notify_player(pplayer, NULL, E_BAD_COMMAND, ftc_server,
160 _("Illegal packet, can't upgrade %s (yet)."),
161 utype_name_translation(from_unittype));
162 return;
166 * Try to upgrade units. The order we upgrade in is arbitrary (if
167 * the player really cared they should have done it manually).
169 conn_list_do_buffer(pplayer->connections);
170 unit_list_iterate(pplayer->units, punit) {
171 if (unit_type_get(punit) == from_unittype) {
172 struct city *pcity = tile_city(unit_tile(punit));
174 if (is_action_enabled_unit_on_city(ACTION_UPGRADE_UNIT, punit, pcity)
175 && unit_perform_action(pplayer, punit->id, pcity->id, 0, "",
176 ACTION_UPGRADE_UNIT, ACT_REQ_SS_AGENT)) {
177 number_of_upgraded_units++;
178 } else if (UU_NO_MONEY == unit_upgrade_test(punit, FALSE)) {
179 break;
182 } unit_list_iterate_end;
183 conn_list_do_unbuffer(pplayer->connections);
185 /* Alert the player about what happened. */
186 if (number_of_upgraded_units > 0) {
187 const int cost = unit_upgrade_price(pplayer, from_unittype, to_unittype);
188 notify_player(pplayer, NULL, E_UNIT_UPGRADED, ftc_server,
189 /* FIXME: plurality of number_of_upgraded_units ignored!
190 * (Plurality of unit names is messed up anyway.) */
191 /* TRANS: "2 Musketeers upgraded to Riflemen for 100 gold."
192 * Plurality is in gold (second %d), not units. */
193 PL_("%d %s upgraded to %s for %d gold.",
194 "%d %s upgraded to %s for %d gold.",
195 cost * number_of_upgraded_units),
196 number_of_upgraded_units,
197 utype_name_translation(from_unittype),
198 utype_name_translation(to_unittype),
199 cost * number_of_upgraded_units);
200 send_player_info_c(pplayer, pplayer->connections);
201 } else {
202 notify_player(pplayer, NULL, E_UNIT_UPGRADED, ftc_server,
203 _("No units could be upgraded."));
207 /**************************************************************************
208 Upgrade the unit to a newer unit type.
210 Returns TRUE iff action could be done, FALSE if it couldn't. Even if
211 this returns TRUE, unit may have died during the action.
212 **************************************************************************/
213 static bool do_unit_upgrade(struct player *pplayer,
214 struct unit *punit, struct city *pcity,
215 enum action_requester ordered_by)
217 char buf[512];
219 if (UU_OK == unit_upgrade_info(punit, buf, sizeof(buf))) {
220 struct unit_type *from_unit = unit_type_get(punit);
221 struct unit_type *to_unit = can_upgrade_unittype(pplayer, from_unit);
223 transform_unit(punit, to_unit, FALSE);
224 send_player_info_c(pplayer, pplayer->connections);
226 if (ordered_by == ACT_REQ_PLAYER) {
227 int cost = unit_upgrade_price(pplayer, from_unit, to_unit);
229 notify_player(pplayer, unit_tile(punit), E_UNIT_UPGRADED, ftc_server,
230 PL_("%s upgraded to %s for %d gold.",
231 "%s upgraded to %s for %d gold.", cost),
232 utype_name_translation(from_unit),
233 unit_link(punit),
234 cost);
237 return TRUE;
238 } else {
239 if (ordered_by == ACT_REQ_PLAYER) {
240 notify_player(pplayer, unit_tile(punit), E_UNIT_UPGRADED, ftc_server,
241 "%s", buf);
244 return FALSE;
248 /**************************************************************************
249 Capture all the units at pdesttile using punit.
251 Returns TRUE iff action could be done, FALSE if it couldn't. Even if
252 this returns TRUE, unit may have died during the action.
253 **************************************************************************/
254 static bool do_capture_units(struct player *pplayer,
255 struct unit *punit,
256 struct tile *pdesttile)
258 struct city *pcity;
259 char capturer_link[MAX_LEN_LINK];
260 const char *capturer_nation = nation_plural_for_player(pplayer);
261 bv_unit_types unique_on_tile;
263 /* Sanity check: The actor still exists. */
264 fc_assert_ret_val(pplayer, FALSE);
265 fc_assert_ret_val(punit, FALSE);
267 /* Sanity check: make sure that the capture won't result in the actor
268 * ending up with more than one unit of each unique unit type. */
269 BV_CLR_ALL(unique_on_tile);
270 unit_list_iterate(pdesttile->units, to_capture) {
271 bool unique_conflict = FALSE;
273 /* Check what the player already has. */
274 if (utype_player_already_has_this_unique(pplayer,
275 unit_type_get(to_capture))) {
276 /* The player already has a unit of this kind. */
277 unique_conflict = TRUE;
280 if (utype_has_flag(unit_type_get(to_capture), UTYF_UNIQUE)) {
281 /* The type of the units at the tile must also be checked. Two allied
282 * players can both have their unique unit at the same tile.
283 * Capturing them both would give the actor two units of a kind that
284 * is supposed to be unique. */
286 if (BV_ISSET(unique_on_tile, utype_index(unit_type_get(to_capture)))) {
287 /* There is another unit of the same kind at this tile. */
288 unique_conflict = TRUE;
289 } else {
290 /* Remember the unit type in case another unit of the same kind is
291 * encountered later. */
292 BV_SET(unique_on_tile, utype_index(unit_type_get(to_capture)));
296 if (unique_conflict) {
297 log_debug("capture units: already got unique unit");
298 notify_player(pplayer, pdesttile, E_UNIT_ILLEGAL_ACTION, ftc_server,
299 /* TRANS: You can only have one Leader. */
300 _("You can only have one %s."),
301 unit_link(to_capture));
303 return FALSE;
305 } unit_list_iterate_end;
307 /* N.B: unit_link() always returns the same pointer. */
308 sz_strlcpy(capturer_link, unit_link(punit));
310 pcity = tile_city(pdesttile);
311 unit_list_iterate(pdesttile->units, to_capture) {
312 struct player *uplayer = unit_owner(to_capture);
313 const char *victim_link;
315 unit_owner(to_capture)->score.units_lost++;
316 to_capture = unit_change_owner(to_capture, pplayer,
317 (game.server.homecaughtunits
318 ? punit->homecity
319 : IDENTITY_NUMBER_ZERO),
320 ULR_CAPTURED);
321 /* As unit_change_owner() currently remove the old unit and
322 * replace by a new one (with a new id), we want to make link to
323 * the new unit. */
324 victim_link = unit_link(to_capture);
326 /* Notify players */
327 notify_player(pplayer, pdesttile, E_MY_DIPLOMAT_BRIBE, ftc_server,
328 /* TRANS: <unit> ... <unit> */
329 _("Your %s succeeded in capturing the %s %s."),
330 capturer_link, nation_adjective_for_player(uplayer),
331 victim_link);
332 notify_player(uplayer, pdesttile,
333 E_ENEMY_DIPLOMAT_BRIBE, ftc_server,
334 /* TRANS: <unit> ... <Poles> */
335 _("Your %s was captured by the %s."),
336 victim_link, capturer_nation);
338 /* May cause an incident */
339 action_id_consequence_success(ACTION_CAPTURE_UNITS, pplayer,
340 unit_owner(to_capture),
341 pdesttile, victim_link);
343 if (NULL != pcity) {
344 /* The captured unit is in a city. Bounce it. */
345 bounce_unit(to_capture, TRUE);
347 } unit_list_iterate_end;
349 /* Subtract movement point from capturer */
350 punit->moves_left -= SINGLE_MOVE;
351 if (punit->moves_left < 0) {
352 punit->moves_left = 0;
355 send_unit_info(NULL, punit);
357 return TRUE;
360 /**************************************************************************
361 Expel the target unit to his owner's capital.
363 Returns TRUE iff action could be done, FALSE if it couldn't. Even if
364 this returns TRUE, unit may have died during the action.
365 **************************************************************************/
366 static bool do_expel_unit(struct player *pplayer,
367 struct unit *actor,
368 struct unit *target)
370 char target_link[MAX_LEN_LINK];
371 struct player *uplayer;
372 struct tile *target_tile;
373 struct city *pcity;
375 /* Maybe it didn't survive the Lua call back. Why wasn't this caught by
376 * the caller? Check in the code that emits the signal. */
377 fc_assert_ret_val(target, FALSE);
379 uplayer = unit_owner(target);
381 /* A unit is supposed to have an owner. */
382 fc_assert_ret_val(uplayer, FALSE);
384 /* Maybe it didn't survive the Lua call back. Why wasn't this caught by
385 * the caller? Check in the code that emits the signal. */
386 fc_assert_ret_val(actor, FALSE);
388 /* Where is the actor player? */
389 fc_assert_ret_val(pplayer, FALSE);
391 /* The price of attempting an expulsion is a single move. Applies before
392 * the player is told if the target has a capital. */
393 actor->moves_left = MAX(0, actor->moves_left - SINGLE_MOVE);
394 send_unit_info(NULL, actor);
396 target_tile = unit_tile(target);
398 /* Expel the target unit to his owner's capital. */
399 pcity = player_capital(uplayer);
401 /* N.B: unit_link() always returns the same pointer. */
402 sz_strlcpy(target_link, unit_link(target));
404 if (pcity == NULL) {
405 /* No where to send the expelled unit. */
407 /* Notify the actor player. */
408 notify_player(pplayer, target_tile, E_UNIT_ACTION_FAILED, ftc_server,
409 /* TRANS: <Poles> <Spy> */
410 _("The %s don't have a capital to expel their %s to."),
411 nation_plural_for_player(uplayer), target_link);
413 /* Nothing more could be done. */
414 return FALSE;
417 /* Please review the code below and above (including the strings sent to
418 * the players) before allowing expulsion to non capital cities. */
419 fc_assert(is_capital(pcity));
421 /* Notify everybody involved. */
422 notify_player(pplayer, target_tile, E_UNIT_DID_EXPEL, ftc_server,
423 /* TRANS: <Border Patrol> ... <Spy> */
424 _("Your %s succeeded in expelling the %s %s."),
425 unit_link(actor), nation_adjective_for_player(uplayer),
426 target_link);
427 notify_player(uplayer, target_tile, E_UNIT_WAS_EXPELLED, ftc_server,
428 /* TRANS: <unit> ... <Poles> */
429 _("Your %s was expelled by the %s."),
430 target_link, nation_plural_for_player(pplayer));
432 /* Being expelled destroys all remaining movement. */
433 if (!teleport_unit_to_city(target, pcity, -1, FALSE)) {
434 log_error("Bug in unit expulsion: unit can't teleport.");
436 return FALSE;
439 /* This may cause a diplomatic incident */
440 action_id_consequence_success(ACTION_EXPEL_UNIT, pplayer, uplayer,
441 target_tile, target_link);
443 /* Mission accomplished. */
444 return TRUE;
447 /**************************************************************************
448 Restore some of the target unit's hit points.
450 Returns TRUE iff action could be done, FALSE if it couldn't. Even if
451 this returns TRUE, unit may have died during the action.
452 **************************************************************************/
453 static bool do_heal_unit(struct player *act_player,
454 struct unit *act_unit,
455 struct unit *tgt_unit)
457 int healing_limit;
458 int tgt_hp_max;
459 struct player *tgt_player;
460 struct tile *tgt_tile;
462 /* Sanity checks: got all the needed input. */
463 fc_assert_ret_val(act_player, FALSE);
464 fc_assert_ret_val(act_unit, FALSE);
465 fc_assert_ret_val(tgt_unit, FALSE);
467 /* The target unit can't have more HP than this. */
468 tgt_hp_max = unit_type_get(tgt_unit)->hp;
470 /* Sanity check: target isn't at full health and can therefore can be
471 * healed. */
472 fc_assert_ret_val(tgt_unit->hp < tgt_hp_max, FALSE);
474 /* Fetch the target unit's owner. */
475 tgt_player = unit_owner(tgt_unit);
476 fc_assert_ret_val(tgt_player, FALSE);
478 /* Fetch the target unit's tile. */
479 tgt_tile = unit_tile(tgt_unit);
480 fc_assert_ret_val(tgt_tile, FALSE);
482 /* The max amount of HP that can be added. */
483 healing_limit = tgt_hp_max / 4;
485 /* Heal the target unit. */
486 tgt_unit->hp = MIN(tgt_unit->hp + healing_limit, tgt_hp_max);
487 send_unit_info(NULL, tgt_unit);
489 /* Healing a unit spends the actor's movement. */
490 act_unit->moves_left = 0;
491 send_unit_info(NULL, act_unit);
493 /* This may have diplomatic consequences. */
494 action_id_consequence_success(ACTION_HEAL_UNIT, act_player, tgt_player,
495 tgt_tile, unit_link(tgt_unit));
497 return TRUE;
500 /**************************************************************************
501 Returns TRUE iff the player is able to change his diplomatic
502 relationship to the other player to war.
504 Note that the player can't declare war on someone he already is at war
505 with.
506 **************************************************************************/
507 static bool rel_may_become_war(const struct player *pplayer,
508 const struct player *oplayer)
510 enum diplstate_type ds;
512 fc_assert_ret_val(pplayer, FALSE);
513 fc_assert_ret_val(oplayer, FALSE);
515 ds = player_diplstate_get(pplayer, oplayer)->type;
517 /* The player can't declare war on someone he already is at war with. */
518 return ds != DS_WAR
519 /* The player can't declare war on a teammate or on himself. */
520 && ds != DS_TEAM && pplayer != oplayer;
523 /**************************************************************************
524 Returns the first player that may enable the specified action if war is
525 declared.
527 Helper for need_war_player(). Use it in stead.
528 **************************************************************************/
529 static struct player *need_war_player_hlp(const struct unit *actor,
530 const int act,
531 const struct tile *target_tile,
532 const struct city *target_city,
533 const struct unit *target_unit)
535 if (action_id_get_actor_kind(act) != AAK_UNIT) {
536 /* No unit can ever do this action so it isn't relevant. */
537 return NULL;
540 if (!unit_can_do_action(actor, act)) {
541 /* The unit can't do the action no matter if there is war or not. */
542 return NULL;
545 /* Look for hard coded war requirements that can't be an action enabler
546 * requirement. */
547 switch ((enum gen_action)act) {
548 case ACTION_BOMBARD:
549 case ACTION_NUKE:
550 case ACTION_ATTACK:
551 /* Target is tile or unit stack but a city (or unit) can block it. */
552 if ((act != ACTION_NUKE || unit_tile(actor) != target_tile)
553 && target_tile) {
554 /* This isn't nuking the actor's own tile so hard coded restrictions
555 * do apply. */
557 struct city *tcity;
558 struct unit *tunit;
560 if ((tcity = tile_city(target_tile))
561 && rel_may_become_war(unit_owner(actor), city_owner(tcity))) {
562 return city_owner(tcity);
565 if ((tunit = is_non_attack_unit_tile(target_tile, unit_owner(actor)))
566 && rel_may_become_war(unit_owner(actor), unit_owner(tunit))) {
567 return unit_owner(tunit);
570 break;
572 case ACTION_ESTABLISH_EMBASSY:
573 case ACTION_ESTABLISH_EMBASSY_STAY:
574 case ACTION_SPY_INVESTIGATE_CITY:
575 case ACTION_INV_CITY_SPEND:
576 case ACTION_SPY_POISON:
577 case ACTION_SPY_STEAL_GOLD:
578 case ACTION_SPY_SABOTAGE_CITY:
579 case ACTION_SPY_TARGETED_SABOTAGE_CITY:
580 case ACTION_SPY_STEAL_TECH:
581 case ACTION_SPY_TARGETED_STEAL_TECH:
582 case ACTION_SPY_INCITE_CITY:
583 case ACTION_TRADE_ROUTE:
584 case ACTION_MARKETPLACE:
585 case ACTION_HELP_WONDER:
586 case ACTION_SPY_BRIBE_UNIT:
587 case ACTION_SPY_SABOTAGE_UNIT:
588 case ACTION_CAPTURE_UNITS: /* Only foreign is a hard req. */
589 case ACTION_FOUND_CITY:
590 case ACTION_JOIN_CITY:
591 case ACTION_STEAL_MAPS:
592 case ACTION_SPY_NUKE:
593 case ACTION_DESTROY_CITY:
594 case ACTION_EXPEL_UNIT:
595 case ACTION_RECYCLE_UNIT:
596 case ACTION_DISBAND_UNIT:
597 case ACTION_HOME_CITY:
598 case ACTION_UPGRADE_UNIT:
599 case ACTION_PARADROP:
600 case ACTION_AIRLIFT:
601 case ACTION_HEAL_UNIT:
602 case ACTION_CONQUER_CITY:
603 /* No special help. */
604 break;
605 case ACTION_COUNT:
606 /* Invalid. */
607 fc_assert(act != ACTION_COUNT);
608 break;
611 /* Look for war requirements from the action enablers. */
612 if (can_utype_do_act_if_tgt_diplrel(unit_type_get(actor),
613 act, DS_WAR, FALSE)) {
614 /* The unit can do the action even if there isn't war. */
615 return NULL;
618 switch (action_id_get_target_kind(act)) {
619 case ATK_CITY:
620 if (target_city == NULL) {
621 /* No target city. */
622 return NULL;
625 if (rel_may_become_war(unit_owner(actor), city_owner(target_city))) {
626 return city_owner(target_city);
628 break;
629 case ATK_UNIT:
630 if (target_unit == NULL) {
631 /* No target unit. */
632 return NULL;
635 if (rel_may_become_war(unit_owner(actor), unit_owner(target_unit))) {
636 return unit_owner(target_unit);
638 break;
639 case ATK_UNITS:
640 if (target_tile == NULL) {
641 /* No target units since no target tile. */
642 return NULL;
645 unit_list_iterate(target_tile->units, tunit) {
646 if (rel_may_become_war(unit_owner(actor), unit_owner(tunit))) {
647 return unit_owner(tunit);
649 } unit_list_iterate_end;
650 break;
651 case ATK_TILE:
652 if (target_tile == NULL) {
653 /* No target tile. */
654 return NULL;
657 if (rel_may_become_war(unit_owner(actor), tile_owner(target_tile))) {
658 return tile_owner(target_tile);
660 break;
661 case ATK_SELF:
662 /* Can't declare war on itself. */
663 return NULL;
664 break;
665 case ATK_COUNT:
666 /* Nothing to check. */
667 fc_assert(action_id_get_target_kind(act) != ATK_COUNT);
668 return NULL;
671 /* Declaring war won't enable the specified action. */
672 return NULL;
675 /**************************************************************************
676 Returns the first player that may enable the specified action if war is
677 declared. If the specified action is ACTION_ANY the first player that
678 may enable any action at all if war is declared will be returned.
679 **************************************************************************/
680 static struct player *need_war_player(const struct unit *actor,
681 const int action_id,
682 const struct tile *target_tile,
683 const struct city *target_city,
684 const struct unit *target_unit)
686 if (action_id == ACTION_ANY) {
687 /* Any action at all will do. */
688 action_iterate(act) {
689 struct player *war_player;
691 war_player = need_war_player_hlp(actor, act,
692 target_tile, target_city,
693 target_unit);
695 if (war_player != NULL) {
696 /* Declaring war on this player may enable this action. */
697 return war_player;
699 } action_iterate_end;
701 /* No action at all may be enabled by declaring war. */
702 return NULL;
703 } else {
704 /* Look for the specified action. */
705 return need_war_player_hlp(actor, action_id,
706 target_tile, target_city,
707 target_unit);
711 /**************************************************************************
712 Returns TRUE iff the specified terrain type blocks the specified action.
714 If the "action" is ACTION_ANY all actions are checked.
715 **************************************************************************/
716 static bool does_terrain_block_action(const int action_id,
717 bool is_target,
718 struct unit *actor_unit,
719 struct terrain *pterrain)
721 if (action_id == ACTION_ANY) {
722 /* Any action is OK. */
723 action_iterate(alt_act) {
724 if (utype_can_do_action(unit_type_get(actor_unit), alt_act)
725 && !does_terrain_block_action(alt_act, is_target,
726 actor_unit, pterrain)) {
727 /* Only one action has to be possible. */
728 return FALSE;
730 } action_iterate_end;
732 /* No action enabled. */
733 return TRUE;
736 /* ACTION_ANY is handled above. */
737 fc_assert_ret_val(action_id_exists(action_id), FALSE);
739 action_enabler_list_iterate(action_enablers_for_action(action_id),
740 enabler) {
741 if (requirement_fulfilled_by_terrain(pterrain,
742 (is_target ? &enabler->target_reqs : &enabler->actor_reqs))
743 && requirement_fulfilled_by_unit_type(unit_type_get(actor_unit),
744 &enabler->actor_reqs)) {
745 /* This terrain kind doesn't block this action enabler. */
746 return FALSE;
748 } action_enabler_list_iterate_end;
750 return TRUE;
753 /**************************************************************************
754 Returns TRUE iff the specified nation blocks the specified action.
756 If the "action" is ACTION_ANY all actions are checked.
757 **************************************************************************/
758 static bool does_nation_block_action(const int action_id,
759 bool is_target,
760 struct unit *actor_unit,
761 struct nation_type *pnation)
763 if (action_id == ACTION_ANY) {
764 /* Any action is OK. */
765 action_iterate(alt_act) {
766 if (utype_can_do_action(unit_type_get(actor_unit), alt_act)
767 && !does_nation_block_action(alt_act, is_target,
768 actor_unit, pnation)) {
769 /* Only one action has to be possible. */
770 return FALSE;
772 } action_iterate_end;
774 /* No action enabled. */
775 return TRUE;
778 /* ACTION_ANY is handled above. */
779 fc_assert_ret_val(action_id_exists(action_id), FALSE);
781 action_enabler_list_iterate(action_enablers_for_action(action_id),
782 enabler) {
783 if (requirement_fulfilled_by_nation(pnation,
784 (is_target ? &enabler->target_reqs
785 : &enabler->actor_reqs))
786 && requirement_fulfilled_by_unit_type(unit_type_get(actor_unit),
787 &enabler->actor_reqs)) {
788 /* This nation doesn't block this action enabler. */
789 return FALSE;
791 } action_enabler_list_iterate_end;
793 return TRUE;
796 /**************************************************************************
797 Returns an explaination why punit can't perform the specified action
798 based on the current game state.
799 **************************************************************************/
800 static struct ane_expl *expl_act_not_enabl(struct unit *punit,
801 const int action_id,
802 const struct tile *target_tile,
803 const struct city *target_city,
804 const struct unit *target_unit)
806 struct player *must_war_player;
807 struct action *blocker;
808 struct player *tgt_player = NULL;
809 struct ane_expl *explnat = fc_malloc(sizeof(struct ane_expl));
810 bool can_exist = can_unit_exist_at_tile(punit, unit_tile(punit));
811 bool on_native = is_native_tile(unit_type_get(punit), unit_tile(punit));
812 int action_custom;
814 if (action_id != ACTION_ANY) {
815 /* A specific action should have a suitable target. */
816 switch (action_id_get_target_kind(action_id)) {
817 case ATK_CITY:
818 if (target_city == NULL) {
819 explnat->kind = ANEK_MISSING_TARGET;
821 break;
822 case ATK_UNIT:
823 if (target_unit == NULL) {
824 explnat->kind = ANEK_MISSING_TARGET;
826 break;
827 case ATK_UNITS:
828 case ATK_TILE:
829 if (target_tile == NULL) {
830 explnat->kind = ANEK_MISSING_TARGET;
832 break;
833 case ATK_SELF:
834 /* No other target. */
835 break;
836 case ATK_COUNT:
837 fc_assert(action_id_get_target_kind(action_id) != ATK_COUNT);
838 break;
842 if (explnat->kind == ANEK_MISSING_TARGET) {
843 /* No point continuing. */
844 return explnat;
847 if (action_id == ACTION_ANY) {
848 /* Find the target player of some actions. */
849 if (target_city) {
850 /* Individual city targets have the highest priority. */
851 tgt_player = city_owner(target_city);
852 } else if (target_unit) {
853 /* Individual unit targets have the next priority. */
854 tgt_player = unit_owner(target_unit);
855 } else if (target_tile) {
856 /* Tile targets have the lowest priority. */
857 tgt_player = tile_owner(target_tile);
859 } else {
860 /* Find the target player of this action. */
861 switch (action_id_get_target_kind(action_id)) {
862 case ATK_CITY:
863 tgt_player = city_owner(target_city);
864 break;
865 case ATK_UNIT:
866 tgt_player = unit_owner(target_unit);
867 break;
868 case ATK_TILE:
869 tgt_player = tile_owner(target_tile);
870 break;
871 case ATK_UNITS:
872 /* A unit stack may contain units with multiple owners. Pick the
873 * first one. */
874 if (target_tile
875 && unit_list_size(target_tile->units) > 0) {
876 tgt_player = unit_owner(unit_list_get(target_tile->units, 0));
878 break;
879 case ATK_SELF:
880 /* A unit acting against itself. */
881 tgt_player = unit_owner(punit);
882 break;
883 case ATK_COUNT:
884 fc_assert(action_id_get_target_kind(action_id) != ATK_COUNT);
885 break;
889 switch (action_id) {
890 case ACTION_FOUND_CITY:
891 /* Detects that the target is closer to a city than citymindist allows.
892 * Detects that the target tile is claimed by a foreigner even when it
893 * is legal to found a city on an unclaimed or domestic tile. */
894 action_custom = city_build_here_test(target_tile, punit);
895 break;
896 case ACTION_AIRLIFT:
897 action_custom = test_unit_can_airlift_to(NULL, punit, target_city);
898 break;
899 case ACTION_NUKE:
900 if (target_tile != unit_tile(punit)) {
901 /* unit_attack_units_at_tile_result() matters for neighbor tiles. */
902 action_custom = unit_attack_units_at_tile_result(punit, target_tile);
903 } else {
904 action_custom = ATT_OK;
906 break;
907 case ACTION_ATTACK:
908 action_custom = unit_attack_units_at_tile_result(punit, target_tile);
909 break;
910 case ACTION_CONQUER_CITY:
911 if (target_city) {
912 action_custom = unit_move_to_tile_test(punit, punit->activity,
913 unit_tile(punit),
914 city_tile(target_city),
915 FALSE, NULL, TRUE);
916 } else {
917 action_custom = MR_OK;
919 break;
920 default:
921 action_custom = 0;
922 break;
925 if (!unit_can_do_action(punit, action_id)) {
926 explnat->kind = ANEK_ACTOR_UNIT;
927 } else if (action_id == ACTION_FOUND_CITY
928 && tile_city(target_tile)) {
929 explnat->kind = ANEK_BAD_TARGET;
930 } else if ((!can_exist
931 && !utype_can_do_act_when_ustate(unit_type_get(punit), action_id,
932 USP_LIVABLE_TILE, FALSE))
933 || (can_exist
934 && !utype_can_do_act_when_ustate(unit_type_get(punit), action_id,
935 USP_LIVABLE_TILE, TRUE))) {
936 explnat->kind = ANEK_BAD_TERRAIN_ACT;
937 explnat->no_act_terrain = tile_terrain(unit_tile(punit));
938 } else if ((!on_native
939 && !utype_can_do_act_when_ustate(unit_type_get(punit), action_id,
940 USP_NATIVE_TILE, FALSE))
941 || (on_native
942 && !utype_can_do_act_when_ustate(unit_type_get(punit), action_id,
943 USP_NATIVE_TILE, TRUE))) {
944 explnat->kind = ANEK_BAD_TERRAIN_ACT;
945 explnat->no_act_terrain = tile_terrain(unit_tile(punit));
946 } else if (punit
947 && does_terrain_block_action(action_id, FALSE,
948 punit, tile_terrain(unit_tile(punit)))) {
949 /* No action enabler allows acting against this terrain kind. */
950 explnat->kind = ANEK_BAD_TERRAIN_ACT;
951 explnat->no_act_terrain = tile_terrain(unit_tile(punit));
952 } else if (action_id == ACTION_FOUND_CITY
953 && target_tile
954 && terrain_has_flag(tile_terrain(target_tile),
955 TER_NO_CITIES)) {
956 explnat->kind = ANEK_BAD_TERRAIN_TGT;
957 explnat->no_act_terrain = tile_terrain(target_tile);
958 } else if (target_tile
959 && does_terrain_block_action(action_id, TRUE,
960 punit, tile_terrain(target_tile))) {
961 /* No action enabler allows acting against this terrain kind. */
962 explnat->kind = ANEK_BAD_TERRAIN_TGT;
963 explnat->no_act_terrain = tile_terrain(target_tile);
964 } else if (unit_transported(punit)
965 && !utype_can_do_act_when_ustate(unit_type_get(punit), action_id,
966 USP_TRANSPORTED, TRUE)) {
967 explnat->kind = ANEK_IS_TRANSPORTED;
968 } else if (!unit_transported(punit)
969 && !utype_can_do_act_when_ustate(unit_type_get(punit), action_id,
970 USP_TRANSPORTED, FALSE)) {
971 explnat->kind = ANEK_IS_NOT_TRANSPORTED;
972 } else if (0 < get_transporter_occupancy(punit)
973 && !utype_can_do_act_when_ustate(unit_type_get(punit), action_id,
974 USP_TRANSPORTING, TRUE)) {
975 explnat->kind = ANEK_IS_TRANSPORTING;
976 } else if (!(0 < get_transporter_occupancy(punit))
977 && !utype_can_do_act_when_ustate(unit_type_get(punit), action_id,
978 USP_TRANSPORTING, FALSE)) {
979 explnat->kind = ANEK_IS_NOT_TRANSPORTING;
980 } else if ((punit->homecity > 0)
981 && !utype_can_do_act_when_ustate(unit_type_get(punit), action_id,
982 USP_HAS_HOME_CITY, TRUE)) {
983 explnat->kind = ANEK_ACTOR_HAS_HOME_CITY;
984 } else if ((punit->homecity <= 0)
985 && !utype_can_do_act_when_ustate(unit_type_get(punit), action_id,
986 USP_HAS_HOME_CITY, FALSE)) {
987 explnat->kind = ANEK_ACTOR_HAS_NO_HOME_CITY;
988 } else if ((punit->homecity <= 0)
989 && (action_id == ACTION_TRADE_ROUTE
990 || action_id == ACTION_MARKETPLACE)) {
991 explnat->kind = ANEK_ACTOR_HAS_NO_HOME_CITY;
992 } else if ((must_war_player = need_war_player(punit,
993 action_id,
994 target_tile,
995 target_city,
996 target_unit))) {
997 explnat->kind = ANEK_NO_WAR;
998 explnat->no_war_with = must_war_player;
999 } else if (action_mp_full_makes_legal(punit, action_id)) {
1000 explnat->kind = ANEK_LOW_MP;
1001 } else if (tgt_player
1002 && unit_owner(punit) != tgt_player
1003 && !can_utype_do_act_if_tgt_diplrel(unit_type_get(punit),
1004 action_id,
1005 DRO_FOREIGN,
1006 TRUE)) {
1007 explnat->kind = ANEK_FOREIGN;
1008 } else if (action_id == ACTION_FOUND_CITY
1009 && action_custom == CB_BAD_BORDERS) {
1010 explnat->kind = ANEK_FOREIGN;
1011 } else if (tgt_player
1012 && unit_owner(punit) == tgt_player
1013 && !can_utype_do_act_if_tgt_diplrel(unit_type_get(punit),
1014 action_id,
1015 DRO_FOREIGN,
1016 FALSE)) {
1017 explnat->kind = ANEK_DOMESTIC;
1018 } else if (punit
1019 && does_nation_block_action(action_id, FALSE,
1020 punit, unit_owner(punit)->nation)) {
1021 explnat->kind = ANEK_NATION_ACT;
1022 explnat->no_act_nation = unit_owner(punit)->nation;
1023 } else if (tgt_player
1024 && does_nation_block_action(action_id, TRUE,
1025 punit, tgt_player->nation)) {
1026 explnat->kind = ANEK_NATION_TGT;
1027 explnat->no_act_nation = tgt_player->nation;
1028 } else if ((target_tile && tile_city(target_tile))
1029 && !utype_may_act_tgt_city_tile(unit_type_get(punit),
1030 action_id,
1031 CITYT_CENTER,
1032 TRUE)) {
1033 explnat->kind = ANEK_IS_CITY_CENTER;
1034 } else if ((target_tile && !tile_city(target_tile))
1035 && !utype_may_act_tgt_city_tile(unit_type_get(punit),
1036 action_id,
1037 CITYT_CENTER,
1038 FALSE)) {
1039 explnat->kind = ANEK_IS_NOT_CITY_CENTER;
1040 } else if ((target_tile && tile_owner(target_tile) != NULL)
1041 && !utype_may_act_tgt_city_tile(unit_type_get(punit),
1042 action_id,
1043 CITYT_CLAIMED,
1044 TRUE)) {
1045 explnat->kind = ANEK_TGT_IS_CLAIMED;
1046 } else if ((target_tile && tile_owner(target_tile) == NULL)
1047 && !utype_may_act_tgt_city_tile(unit_type_get(punit),
1048 action_id,
1049 CITYT_CLAIMED,
1050 FALSE)) {
1051 explnat->kind = ANEK_TGT_IS_UNCLAIMED;
1052 } else if (action_id_exists(action_id) && punit
1053 && ((target_tile
1054 && !action_id_distance_inside_max(action_id,
1055 real_map_distance(unit_tile(punit), target_tile)))
1056 || (target_city
1057 && !action_id_distance_inside_max(action_id,
1058 real_map_distance(unit_tile(punit),
1059 city_tile(target_city))))
1060 || (target_unit
1061 && !action_id_distance_inside_max(action_id,
1062 real_map_distance(unit_tile(punit),
1063 unit_tile(target_unit)))))) {
1064 explnat->kind = ANEK_DISTANCE_FAR;
1065 explnat->distance = action_by_number(action_id)->max_distance;
1066 } else if (action_id == ACTION_PARADROP && punit && target_tile
1067 && real_map_distance(unit_tile(punit), target_tile)
1068 > unit_type_get(punit)->paratroopers_range) {
1069 explnat->kind = ANEK_DISTANCE_FAR;
1070 explnat->distance = unit_type_get(punit)->paratroopers_range;
1071 } else if (action_id_exists(action_id) && punit
1072 && ((target_tile
1073 && real_map_distance(unit_tile(punit), target_tile)
1074 < action_by_number(action_id)->min_distance)
1075 || (target_city
1076 && real_map_distance(unit_tile(punit),
1077 city_tile(target_city))
1078 < action_by_number(action_id)->min_distance)
1079 || (target_unit
1080 && real_map_distance(unit_tile(punit),
1081 unit_tile(target_unit))
1082 < action_by_number(action_id)->min_distance))) {
1083 explnat->kind = ANEK_DISTANCE_NEAR;
1084 explnat->distance = action_by_number(action_id)->min_distance;
1085 } else if (target_city
1086 && (action_id == ACTION_JOIN_CITY
1087 && action_actor_utype_hard_reqs_ok(ACTION_JOIN_CITY,
1088 unit_type_get(punit))
1089 && (city_size_get(target_city) + unit_pop_value(punit)
1090 > game.info.add_to_size_limit))) {
1091 /* TODO: Check max city size requirements from action enabler target
1092 * vectors. */
1093 explnat->kind = ANEK_CITY_TOO_BIG;
1094 } else if (target_city
1095 && (action_id == ACTION_JOIN_CITY
1096 && action_actor_utype_hard_reqs_ok(ACTION_JOIN_CITY,
1097 unit_type_get(punit))
1098 && (!city_can_grow_to(target_city,
1099 city_size_get(target_city)
1100 + unit_pop_value(punit))))) {
1101 explnat->kind = ANEK_CITY_POP_LIMIT;
1102 } else if ((action_id == ACTION_NUKE
1103 || action_id == ACTION_ATTACK)
1104 && action_custom != ATT_OK) {
1105 switch (action_custom) {
1106 case ATT_NON_ATTACK:
1107 explnat->kind = ANEK_ACTOR_UNIT;
1108 break;
1109 case ATT_UNREACHABLE:
1110 explnat->kind = ANEK_TGT_UNREACHABLE;
1111 break;
1112 case ATT_NONNATIVE_SRC:
1113 explnat->kind = ANEK_BAD_TERRAIN_ACT;
1114 explnat->no_act_terrain = tile_terrain(unit_tile(punit));
1115 break;
1116 case ATT_NONNATIVE_DST:
1117 explnat->kind = ANEK_BAD_TERRAIN_TGT;
1118 explnat->no_act_terrain = tile_terrain(target_tile);
1119 break;
1120 default:
1121 fc_assert(action_custom != ATT_OK);
1122 explnat->kind = ANEK_UNKNOWN;
1123 break;
1125 } else if (action_id == ACTION_AIRLIFT
1126 && action_custom == AR_SRC_NO_FLIGHTS) {
1127 explnat->kind = ANEK_CITY_NO_CAPACITY;
1128 explnat->capacity_city = tile_city(unit_tile(punit));
1129 } else if (action_id == ACTION_AIRLIFT
1130 && action_custom == AR_DST_NO_FLIGHTS) {
1131 explnat->kind = ANEK_CITY_NO_CAPACITY;
1132 explnat->capacity_city = game_city_by_number(target_city->id);
1133 } else if (action_id == ACTION_FOUND_CITY
1134 && action_custom == CB_NO_MIN_DIST) {
1135 explnat->kind = ANEK_CITY_TOO_CLOSE_TGT;
1136 } else if (action_id == ACTION_PARADROP
1137 && target_tile
1138 && !map_is_known(target_tile, unit_owner(punit))) {
1139 explnat->kind = ANEK_TGT_TILE_UNKNOWN;
1140 } else if (action_id == ACTION_CONQUER_CITY
1141 && action_custom != MR_OK) {
1142 switch (action_custom) {
1143 case MR_CANNOT_DISEMBARK:
1144 explnat->kind = ANEK_DISEMBARK_ACT;
1145 break;
1146 case MR_TRIREME:
1147 explnat->kind = ANEK_TRIREME_MOVE;
1148 break;
1149 default:
1150 fc_assert(action_custom != MR_OK);
1151 explnat->kind = ANEK_UNKNOWN;
1152 break;
1154 } else if ((game.scenario.prevent_new_cities
1155 && utype_can_do_action(unit_type_get(punit), ACTION_FOUND_CITY))
1156 && (action_id == ACTION_FOUND_CITY
1157 || action_id == ACTION_ANY)) {
1158 /* Please add a check for any new action forbidding scenario setting
1159 * above this comment. */
1160 explnat->kind = ANEK_SCENARIO_DISABLED;
1161 } else if (action_id_exists(action_id)
1162 && (blocker = action_is_blocked_by(action_id, punit,
1163 target_tile, target_city,
1164 target_unit))) {
1165 explnat->kind = ANEK_ACTION_BLOCKS;
1166 explnat->blocker = blocker;
1167 } else {
1168 explnat->kind = ANEK_UNKNOWN;
1171 return explnat;
1174 /**************************************************************************
1175 Give the reason kind why an action isn't enabled.
1176 **************************************************************************/
1177 enum ane_kind action_not_enabled_reason(struct unit *punit,
1178 enum gen_action action_id,
1179 const struct tile *target_tile,
1180 const struct city *target_city,
1181 const struct unit *target_unit)
1183 struct ane_expl *explnat = expl_act_not_enabl(punit, action_id,
1184 target_tile,
1185 target_city, target_unit);
1186 enum ane_kind out = explnat->kind;
1188 free(explnat);
1190 return out;
1193 /**************************************************************************
1194 Explain why punit can't perform any action at all based on its current
1195 game state.
1196 **************************************************************************/
1197 static void explain_why_no_action_enabled(struct unit *punit,
1198 const struct tile *target_tile,
1199 const struct city *target_city,
1200 const struct unit *target_unit)
1202 struct player *pplayer = unit_owner(punit);
1203 struct ane_expl *explnat = expl_act_not_enabl(punit, ACTION_ANY,
1204 target_tile,
1205 target_city, target_unit);
1207 switch (explnat->kind) {
1208 case ANEK_ACTOR_UNIT:
1209 /* This shouldn't happen unless the client is buggy given the current
1210 * users. */
1211 fc_assert_msg(explnat->kind != ANEK_ACTOR_UNIT,
1212 "Asked to explain why a non actor can't act.");
1214 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1215 _("Unit cannot do anything."));
1216 break;
1217 case ANEK_MISSING_TARGET:
1218 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1219 _("Your %s found no suitable target."),
1220 unit_name_translation(punit));
1221 break;
1222 case ANEK_BAD_TARGET:
1223 /* This shouldn't happen at the moment. Only specific action checks
1224 * will trigger bad target checks. This is a reply to a question about
1225 * any action. */
1226 fc_assert(explnat->kind != ANEK_BAD_TARGET);
1228 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1229 _("Your %s found no suitable target."),
1230 unit_name_translation(punit));
1231 break;
1232 case ANEK_BAD_TERRAIN_ACT:
1234 const char *types[utype_count()];
1235 int i = 0;
1237 if (!utype_can_do_act_when_ustate(unit_type_get(punit),
1238 ACTION_ANY, USP_LIVABLE_TILE,
1239 FALSE)
1240 && !can_unit_exist_at_tile(punit, unit_tile(punit))) {
1241 unit_type_iterate(utype) {
1242 if (utype_can_do_act_when_ustate(utype, ACTION_ANY,
1243 USP_LIVABLE_TILE, FALSE)) {
1244 types[i++] = utype_name_translation(utype);
1246 } unit_type_iterate_end;
1249 if (0 < i) {
1250 struct astring astr = ASTRING_INIT;
1252 notify_player(pplayer, unit_tile(punit),
1253 E_BAD_COMMAND, ftc_server,
1254 _("Your %s cannot act from %s. "
1255 "Only %s can act from a non livable tile."),
1256 unit_name_translation(punit),
1257 terrain_name_translation(explnat->no_act_terrain),
1258 astr_build_or_list(&astr, types, i));
1260 astr_free(&astr);
1261 } else {
1262 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1263 _("Unit cannot act from %s."),
1264 terrain_name_translation(explnat->no_act_terrain));
1267 break;
1268 case ANEK_BAD_TERRAIN_TGT:
1269 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1270 _("Unit cannot act against %s."),
1271 terrain_name_translation(explnat->no_act_terrain));
1272 break;
1273 case ANEK_IS_TRANSPORTED:
1274 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1275 _("This unit is being transported, and"
1276 " so cannot act."));
1277 break;
1278 case ANEK_IS_NOT_TRANSPORTED:
1279 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1280 _("This unit cannot act when it isn't being "
1281 "transported."));
1282 break;
1283 case ANEK_IS_TRANSPORTING:
1284 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1285 _("This unit is transporting, and"
1286 " so cannot act."));
1287 break;
1288 case ANEK_IS_NOT_TRANSPORTING:
1289 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1290 _("This unit cannot act when it isn't transporting."));
1291 break;
1292 case ANEK_ACTOR_HAS_HOME_CITY:
1293 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1294 _("This unit has a home city, and so cannot act."));
1295 break;
1296 case ANEK_ACTOR_HAS_NO_HOME_CITY:
1297 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1298 _("This unit cannot act unless it has a home city."));
1299 break;
1300 case ANEK_NO_WAR:
1301 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1302 _("You must declare war on %s first. Try using "
1303 "the Nations report (F3)."),
1304 player_name(explnat->no_war_with));
1305 break;
1306 case ANEK_DOMESTIC:
1307 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1308 _("This unit cannot act against domestic targets."));
1309 break;
1310 case ANEK_FOREIGN:
1311 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1312 _("This unit cannot act against foreign targets."));
1313 break;
1314 case ANEK_NATION_ACT:
1315 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1316 /* TRANS: Swedish ... Riflemen */
1317 _("%s %s cannot act."),
1318 nation_adjective_translation(explnat->no_act_nation),
1319 unit_name_translation(punit));
1320 break;
1321 case ANEK_NATION_TGT:
1322 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1323 /* TRANS: ... Pirate ... */
1324 _("This unit cannot act against %s targets."),
1325 nation_adjective_translation(explnat->no_act_nation));
1326 break;
1327 case ANEK_LOW_MP:
1328 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1329 _("This unit has too few moves left to act."));
1330 break;
1331 case ANEK_IS_CITY_CENTER:
1332 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1333 _("This unit cannot act against city centers."));
1334 break;
1335 case ANEK_IS_NOT_CITY_CENTER:
1336 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1337 _("This unit cannot act against non city centers."));
1338 break;
1339 case ANEK_TGT_IS_CLAIMED:
1340 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1341 _("This unit cannot act against claimed tiles."));
1342 break;
1343 case ANEK_TGT_IS_UNCLAIMED:
1344 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1345 _("This unit cannot act against unclaimed tiles."));
1346 break;
1347 case ANEK_DISTANCE_NEAR:
1348 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1349 _("This unit is to near its target to act."));
1350 break;
1351 case ANEK_DISTANCE_FAR:
1352 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1353 _("This unit is to far away from its target to act."));
1354 break;
1355 case ANEK_SCENARIO_DISABLED:
1356 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1357 _("Can't perform any action this scenario permits."));
1358 break;
1359 case ANEK_CITY_TOO_CLOSE_TGT:
1360 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1361 _("Can't perform any action this close to a city."));
1362 break;
1363 case ANEK_CITY_TOO_BIG:
1364 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1365 /* TRANS: Settler ... Berlin */
1366 _("%s can't do anything to %s. It is too big."),
1367 unit_name_translation(punit),
1368 city_name_get(target_city));
1369 break;
1370 case ANEK_CITY_POP_LIMIT:
1371 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1372 /* TRANS: London ... Settlers */
1373 _("%s needs an improvement to grow, so "
1374 "%s cannot do anything to it."),
1375 city_name_get(target_city),
1376 unit_name_translation(punit));
1377 break;
1378 case ANEK_CITY_NO_CAPACITY:
1379 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1380 /* TRANS: Paris ... Warriors (think: airlift) */
1381 _("%s don't have enough capacity, so "
1382 "%s cannot do anything."),
1383 city_name_get(explnat->capacity_city),
1384 unit_name_translation(punit));
1385 break;
1386 case ANEK_TGT_TILE_UNKNOWN:
1387 notify_player(pplayer, target_tile, E_BAD_COMMAND, ftc_server,
1388 /* TRANS: Paratroopers ... */
1389 _("%s can't do anything to an unknown target tile."),
1390 unit_name_translation(punit));
1391 break;
1392 case ANEK_TRIREME_MOVE:
1393 notify_player(pplayer, target_tile, E_BAD_COMMAND, ftc_server,
1394 _("%s cannot move that far from the coast line."),
1395 unit_link(punit));
1396 break;
1397 case ANEK_DISEMBARK_ACT:
1398 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1399 _("%s cannot disembark outside of a city or a native base "
1400 "for %s."),
1401 unit_link(punit),
1402 utype_name_translation(
1403 unit_type_get(unit_transport_get(punit))));
1404 break;
1405 case ANEK_TGT_UNREACHABLE:
1406 notify_player(pplayer, target_tile, E_BAD_COMMAND, ftc_server,
1407 _("%s can't do anything since there is an unreachable "
1408 "unit."),
1409 unit_name_translation(punit));
1410 break;
1411 case ANEK_ACTION_BLOCKS:
1412 /* If an action blocked another action the blocking action must be
1413 * possible. */
1414 fc_assert(explnat->kind != ANEK_ACTION_BLOCKS);
1415 /* Fall through to unknown cause. */
1416 case ANEK_UNKNOWN:
1417 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
1418 _("No action possible."));
1419 break;
1422 free(explnat);
1425 /**************************************************************************
1426 Handle a query for what actions a unit may do.
1428 MUST always send a reply so the client can move on in the queue. This
1429 includes when the client give invalid input. That the acting unit died
1430 before the server received a request for what actions it could do should
1431 not stop the client from processing the next unit in the queue.
1432 **************************************************************************/
1433 void handle_unit_get_actions(struct connection *pc,
1434 const int actor_unit_id,
1435 const int target_unit_id_client,
1436 const int target_tile_id,
1437 const bool disturb_player)
1439 struct player *actor_player;
1440 struct unit *actor_unit;
1441 struct tile *target_tile;
1442 struct act_prob probabilities[MAX_NUM_ACTIONS];
1444 struct unit *target_unit;
1445 struct city *target_city;
1447 int actor_target_distance;
1448 const struct player_tile *plrtile;
1450 /* No potentially legal action is known yet. If none is found the player
1451 * should get an explanation. */
1452 bool at_least_one_action = FALSE;
1454 /* A target should only be sent if it is possible to act against it */
1455 int target_city_id = IDENTITY_NUMBER_ZERO;
1456 int target_unit_id = IDENTITY_NUMBER_ZERO;
1458 actor_player = pc->playing;
1459 actor_unit = game_unit_by_number(actor_unit_id);
1460 target_tile = index_to_tile(&(wld.map), target_tile_id);
1462 /* Initialize the action probabilities. */
1463 action_iterate(act) {
1464 probabilities[act] = ACTPROB_NA;
1465 } action_iterate_end;
1467 /* Check if the request is valid. */
1468 if (!target_tile || !actor_unit || !actor_player
1469 || actor_unit->owner != actor_player) {
1470 dsend_packet_unit_actions(pc, actor_unit_id,
1471 IDENTITY_NUMBER_ZERO, IDENTITY_NUMBER_ZERO,
1472 target_tile_id,
1473 disturb_player,
1474 probabilities);
1475 return;
1478 /* Select the targets. */
1480 if (target_unit_id_client == IDENTITY_NUMBER_ZERO) {
1481 /* Find a new target unit. */
1482 target_unit = action_tgt_unit(actor_unit, target_tile, TRUE);
1483 } else {
1484 /* Prepare the client selected target unit. */
1485 target_unit = game_unit_by_number(target_unit_id_client);
1488 /* Find the target city. */
1489 target_city = action_tgt_city(actor_unit, target_tile, TRUE);
1491 /* The specified target unit must be located at the target tile. */
1492 if (target_unit && unit_tile(target_unit) != target_tile) {
1493 notify_player(actor_player, unit_tile(actor_unit),
1494 E_BAD_COMMAND, ftc_server,
1495 _("Target not at target tile."));
1496 dsend_packet_unit_actions(pc, actor_unit_id,
1497 IDENTITY_NUMBER_ZERO, IDENTITY_NUMBER_ZERO,
1498 target_tile_id,
1499 disturb_player,
1500 probabilities);
1501 return;
1504 /* The player may have outdated information about the target tile.
1505 * Limiting the player knowledge look up to the target tile is OK since
1506 * all targets must be located at it. */
1507 plrtile = map_get_player_tile(target_tile, actor_player);
1509 /* Distance between actor and target tile. */
1510 actor_target_distance = real_map_distance(unit_tile(actor_unit),
1511 target_tile);
1513 /* Find out what can be done to the targets. */
1515 /* Set the probability for the actions. */
1516 action_iterate(act) {
1517 if (action_id_get_actor_kind(act) != AAK_UNIT) {
1518 /* Not relevant. */
1519 continue;
1522 switch (action_id_get_target_kind(act)) {
1523 case ATK_CITY:
1524 if (plrtile && plrtile->site) {
1525 /* Only a known city may be targeted. */
1526 if (target_city) {
1527 /* Calculate the probabilities. */
1528 probabilities[act] = action_prob_vs_city(actor_unit, act,
1529 target_city);
1530 } else if (!tile_is_seen(target_tile, actor_player)
1531 && action_maybe_possible_actor_unit(act, actor_unit)
1532 && action_id_distance_accepted(act,
1533 actor_target_distance)) {
1534 /* The target city is non existing. The player isn't aware of this
1535 * fact because he can't see the tile it was located on. The
1536 * actor unit it self doesn't contradict the requirements to
1537 * perform the action. The (no longer existing) target city was
1538 * known to be close enough. */
1539 probabilities[act] = ACTPROB_NOT_KNOWN;
1540 } else {
1541 /* The actor unit is known to be unable to act or the target city
1542 * is known to be too far away. */
1543 probabilities[act] = ACTPROB_IMPOSSIBLE;
1545 } else {
1546 /* No target to act against. */
1547 probabilities[act] = ACTPROB_IMPOSSIBLE;
1549 break;
1550 case ATK_UNIT:
1551 if (target_unit) {
1552 /* Calculate the probabilities. */
1553 probabilities[act] = action_prob_vs_unit(actor_unit, act,
1554 target_unit);
1555 } else {
1556 /* No target to act against. */
1557 probabilities[act] = ACTPROB_IMPOSSIBLE;
1559 break;
1560 case ATK_UNITS:
1561 if (target_tile) {
1562 /* Calculate the probabilities. */
1563 probabilities[act] = action_prob_vs_units(actor_unit, act,
1564 target_tile);
1565 } else {
1566 /* No target to act against. */
1567 probabilities[act] = ACTPROB_IMPOSSIBLE;
1569 break;
1570 case ATK_TILE:
1571 if (target_tile) {
1572 /* Calculate the probabilities. */
1573 probabilities[act] = action_prob_vs_tile(actor_unit, act,
1574 target_tile);
1575 } else {
1576 /* No target to act against. */
1577 probabilities[act] = ACTPROB_IMPOSSIBLE;
1579 break;
1580 case ATK_SELF:
1581 if (actor_target_distance == 0) {
1582 /* Calculate the probabilities. */
1583 probabilities[act] = action_prob_self(actor_unit, act);
1584 } else {
1585 /* Don't bother with self targeted actions unless the actor is
1586 * asking about what can be done to its own tile. */
1587 probabilities[act] = ACTPROB_IMPOSSIBLE;
1589 break;
1590 case ATK_COUNT:
1591 fc_assert_action(action_id_get_target_kind(act) != ATK_COUNT,
1592 probabilities[act] = ACTPROB_IMPOSSIBLE);
1593 break;
1595 } action_iterate_end;
1597 /* Analyze the probabilities. Decide what targets to send and if an
1598 * explanation is needed. */
1599 action_iterate(act) {
1600 if (action_prob_possible(probabilities[act])) {
1601 /* An action can be done. No need to explain why no action can be
1602 * done. */
1603 at_least_one_action = TRUE;
1605 switch (action_id_get_target_kind(act)) {
1606 case ATK_CITY:
1607 /* The city should be sent as a target since it is possible to act
1608 * against it. */
1610 /* All city targeted actions requires that the player is aware of
1611 * the target city. It is therefore in the player's map. */
1612 fc_assert_action(plrtile, continue);
1613 fc_assert_action(plrtile->site, continue);
1615 target_city_id = plrtile->site->identity;
1616 break;
1617 case ATK_UNIT:
1618 /* The unit should be sent as a target since it is possible to act
1619 * against it. */
1620 fc_assert(target_unit != NULL);
1621 target_unit_id = target_unit->id;
1622 break;
1623 case ATK_TILE:
1624 case ATK_UNITS:
1625 /* The target tile aren't selected here so it haven't changed. */
1626 fc_assert(target_tile != NULL);
1627 break;
1628 case ATK_SELF:
1629 /* The target unit is the actor unit. It is already sent. */
1630 fc_assert(actor_unit != NULL);
1631 break;
1632 case ATK_COUNT:
1633 fc_assert_msg(action_id_get_target_kind(act) != ATK_COUNT,
1634 "Invalid action target kind.");
1635 break;
1638 if (target_city_id != IDENTITY_NUMBER_ZERO
1639 && target_unit != IDENTITY_NUMBER_ZERO) {
1640 /* No need to find out more. */
1641 break;
1644 } action_iterate_end;
1646 /* Send possible actions and targets. */
1647 dsend_packet_unit_actions(pc,
1648 actor_unit_id, target_unit_id, target_city_id,
1649 target_tile_id,
1650 disturb_player,
1651 probabilities);
1653 if (disturb_player && !at_least_one_action) {
1654 /* The user should get an explanation why no action is possible. */
1655 explain_why_no_action_enabled(actor_unit,
1656 target_tile, target_city, target_unit);
1660 /**************************************************************************
1661 Try to explain to the player why an action is illegal.
1663 Event type should be E_BAD_COMMAND if the player should know that the
1664 action is illegal or E_UNIT_ILLEGAL_ACTION if the player potentially new
1665 information is being revealed.
1666 **************************************************************************/
1667 void illegal_action_msg(struct player *pplayer,
1668 const enum event_type event,
1669 struct unit *actor,
1670 const int stopped_action,
1671 const struct tile *target_tile,
1672 const struct city *target_city,
1673 const struct unit *target_unit)
1675 struct ane_expl *explnat;
1677 /* Explain why the action was illegal. */
1678 explnat = expl_act_not_enabl(actor, stopped_action,
1679 target_tile, target_city, target_unit);
1680 switch (explnat->kind) {
1681 case ANEK_ACTOR_UNIT:
1683 struct astring astr = ASTRING_INIT;
1685 if (role_units_translations(&astr,
1686 action_id_get_role(stopped_action),
1687 TRUE)) {
1688 notify_player(pplayer, unit_tile(actor),
1689 event, ftc_server,
1690 /* TRANS: Only Diplomat or Spy can do Steal Gold. */
1691 _("Only %s can do %s."),
1692 astr_str(&astr),
1693 action_id_name_translation(stopped_action));
1694 astr_free(&astr);
1695 } else {
1696 notify_player(pplayer, unit_tile(actor),
1697 event, ftc_server,
1698 /* TRANS: Spy can't do Capture Units. */
1699 _("%s can't do %s."),
1700 unit_name_translation(actor),
1701 action_id_name_translation(stopped_action));
1704 break;
1705 case ANEK_MISSING_TARGET:
1706 notify_player(pplayer, unit_tile(actor), event, ftc_server,
1707 _("Your %s found no target suitable for %s."),
1708 unit_name_translation(actor),
1709 action_id_name_translation(stopped_action));
1710 break;
1711 case ANEK_BAD_TARGET:
1712 notify_player(pplayer, unit_tile(actor), event, ftc_server,
1713 _("Having your %s do %s to this target is redundant."),
1714 unit_name_translation(actor),
1715 action_id_name_translation(stopped_action));
1716 break;
1717 case ANEK_BAD_TERRAIN_ACT:
1719 const char *types[utype_count()];
1720 int i = 0;
1722 if (!utype_can_do_act_when_ustate(unit_type_get(actor),
1723 stopped_action, USP_LIVABLE_TILE,
1724 FALSE)
1725 && !can_unit_exist_at_tile(actor, unit_tile(actor))) {
1726 unit_type_iterate(utype) {
1727 if (utype_can_do_act_when_ustate(utype, stopped_action,
1728 USP_LIVABLE_TILE, FALSE)) {
1729 types[i++] = utype_name_translation(utype);
1731 } unit_type_iterate_end;
1734 if (0 < i) {
1735 struct astring astr = ASTRING_INIT;
1737 notify_player(pplayer, unit_tile(actor),
1738 event, ftc_server,
1739 _("Your %s can't do %s from %s. "
1740 "Only %s can do %s from a non livable tile."),
1741 unit_name_translation(actor),
1742 action_id_name_translation(stopped_action),
1743 terrain_name_translation(explnat->no_act_terrain),
1744 action_id_name_translation(stopped_action),
1745 astr_build_or_list(&astr, types, i));
1747 astr_free(&astr);
1748 } else {
1749 notify_player(pplayer, unit_tile(actor),
1750 event, ftc_server,
1751 _("Your %s can't do %s from %s."),
1752 unit_name_translation(actor),
1753 action_id_name_translation(stopped_action),
1754 terrain_name_translation(explnat->no_act_terrain));
1757 break;
1758 case ANEK_BAD_TERRAIN_TGT:
1759 notify_player(pplayer, unit_tile(actor),
1760 event, ftc_server,
1761 _("Your %s can't do %s to %s."),
1762 unit_name_translation(actor),
1763 action_id_name_translation(stopped_action),
1764 terrain_name_translation(explnat->no_act_terrain));
1765 break;
1766 case ANEK_IS_TRANSPORTED:
1767 notify_player(pplayer, unit_tile(actor),
1768 event, ftc_server,
1769 _("Your %s can't do %s while being transported."),
1770 unit_name_translation(actor),
1771 action_id_name_translation(stopped_action));
1772 break;
1773 case ANEK_IS_NOT_TRANSPORTED:
1774 notify_player(pplayer, unit_tile(actor),
1775 event, ftc_server,
1776 _("Your %s can't do %s while not being transported."),
1777 unit_name_translation(actor),
1778 action_id_name_translation(stopped_action));
1779 break;
1780 case ANEK_IS_TRANSPORTING:
1781 notify_player(pplayer, unit_tile(actor),
1782 event, ftc_server,
1783 _("Your %s can't do %s while transporting."),
1784 unit_name_translation(actor),
1785 action_id_name_translation(stopped_action));
1786 break;
1787 case ANEK_IS_NOT_TRANSPORTING:
1788 notify_player(pplayer, unit_tile(actor),
1789 event, ftc_server,
1790 _("Your %s can't do %s while not transporting."),
1791 unit_name_translation(actor),
1792 action_id_name_translation(stopped_action));
1793 break;
1794 case ANEK_ACTOR_HAS_HOME_CITY:
1795 notify_player(pplayer, unit_tile(actor),
1796 event, ftc_server,
1797 _("Your %s can't do %s because it has a home city."),
1798 unit_name_translation(actor),
1799 action_id_name_translation(stopped_action));
1800 break;
1801 case ANEK_ACTOR_HAS_NO_HOME_CITY:
1802 notify_player(pplayer, unit_tile(actor),
1803 event, ftc_server,
1804 _("Your %s can't do %s because it is homeless."),
1805 unit_name_translation(actor),
1806 action_id_name_translation(stopped_action));
1807 break;
1808 case ANEK_NO_WAR:
1809 notify_player(pplayer, unit_tile(actor),
1810 event, ftc_server,
1811 _("Your %s can't do %s while you"
1812 " aren't at war with %s."),
1813 unit_name_translation(actor),
1814 action_id_name_translation(stopped_action),
1815 player_name(explnat->no_war_with));
1816 break;
1817 case ANEK_DOMESTIC:
1818 notify_player(pplayer, unit_tile(actor),
1819 event, ftc_server,
1820 _("Your %s can't do %s to domestic %s."),
1821 unit_name_translation(actor),
1822 action_id_name_translation(stopped_action),
1823 action_target_kind_translated_name(
1824 action_id_get_target_kind(stopped_action)));
1825 break;
1826 case ANEK_FOREIGN:
1827 notify_player(pplayer, unit_tile(actor),
1828 event, ftc_server,
1829 _("Your %s can't do %s to foreign %s."),
1830 unit_name_translation(actor),
1831 action_id_name_translation(stopped_action),
1832 action_target_kind_translated_name(
1833 action_id_get_target_kind(stopped_action)));
1834 break;
1835 case ANEK_NATION_ACT:
1836 notify_player(pplayer, unit_tile(actor),
1837 event, ftc_server,
1838 /* TRANS: Swedish ... Riflemen ... Expel Unit */
1839 _("%s %s can't do %s."),
1840 nation_adjective_translation(explnat->no_act_nation),
1841 unit_name_translation(actor),
1842 action_id_name_translation(stopped_action));
1843 break;
1844 case ANEK_NATION_TGT:
1845 notify_player(pplayer, unit_tile(actor),
1846 event, ftc_server,
1847 /* TRANS: Riflemen... Expel Unit... Pirate... Migrants */
1848 _("Your %s can't do %s to %s %s."),
1849 unit_name_translation(actor),
1850 action_id_name_translation(stopped_action),
1851 nation_adjective_translation(explnat->no_act_nation),
1852 action_target_kind_translated_name(
1853 action_id_get_target_kind(stopped_action)));
1854 break;
1855 case ANEK_LOW_MP:
1856 notify_player(pplayer, unit_tile(actor),
1857 event, ftc_server,
1858 _("Your %s has too few moves left to %s."),
1859 unit_name_translation(actor),
1860 action_id_name_translation(stopped_action));
1861 break;
1862 case ANEK_IS_CITY_CENTER:
1863 notify_player(pplayer, unit_tile(actor),
1864 event, ftc_server,
1865 _("Your %s can't do %s to city centers."),
1866 unit_name_translation(actor),
1867 action_id_name_translation(stopped_action));
1868 break;
1869 case ANEK_IS_NOT_CITY_CENTER:
1870 notify_player(pplayer, unit_tile(actor),
1871 event, ftc_server,
1872 _("Your %s can only do %s to city centers."),
1873 unit_name_translation(actor),
1874 action_id_name_translation(stopped_action));
1875 break;
1876 case ANEK_TGT_IS_CLAIMED:
1877 notify_player(pplayer, unit_tile(actor),
1878 event, ftc_server,
1879 _("Your %s can't do %s to claimed tiles."),
1880 unit_name_translation(actor),
1881 action_id_name_translation(stopped_action));
1882 break;
1883 case ANEK_TGT_IS_UNCLAIMED:
1884 notify_player(pplayer, unit_tile(actor),
1885 event, ftc_server,
1886 _("Your %s can't do %s to unclaimed tiles."),
1887 unit_name_translation(actor),
1888 action_id_name_translation(stopped_action));
1889 break;
1890 case ANEK_DISTANCE_NEAR:
1891 notify_player(pplayer, unit_tile(actor),
1892 event, ftc_server,
1893 PL_("Your %s must be at least %d tile away to do %s.",
1894 "Your %s must be at least %d tiles away to do %s.",
1895 explnat->distance),
1896 unit_name_translation(actor),
1897 explnat->distance,
1898 action_id_name_translation(stopped_action));
1899 break;
1900 case ANEK_DISTANCE_FAR:
1901 notify_player(pplayer, unit_tile(actor),
1902 event, ftc_server,
1903 PL_("Your %s can't be more than %d tile away to do %s.",
1904 "Your %s can't be more than %d tiles away to do %s.",
1905 explnat->distance),
1906 unit_name_translation(actor),
1907 explnat->distance,
1908 action_id_name_translation(stopped_action));
1909 break;
1910 case ANEK_SCENARIO_DISABLED:
1911 notify_player(pplayer, unit_tile(actor),
1912 event, ftc_server,
1913 /* TRANS: Can't do Build City in this scenario. */
1914 _("Can't do %s in this scenario."),
1915 action_id_name_translation(stopped_action));
1916 break;
1917 case ANEK_CITY_TOO_CLOSE_TGT:
1918 notify_player(pplayer, unit_tile(actor),
1919 event, ftc_server,
1920 /* TRANS: Can't do Build City this close to a city. */
1921 _("Can't do %s this close to a city."),
1922 action_id_name_translation(stopped_action));
1923 break;
1924 case ANEK_CITY_TOO_BIG:
1925 notify_player(pplayer, unit_tile(actor),
1926 event, ftc_server,
1927 /* TRANS: Settlers ... Join City ... London */
1928 _("%s can't do %s to %s. It is too big."),
1929 unit_name_translation(actor),
1930 action_id_name_translation(stopped_action),
1931 city_name_get(target_city));
1932 break;
1933 case ANEK_CITY_POP_LIMIT:
1934 notify_player(pplayer, unit_tile(actor),
1935 event, ftc_server,
1936 /* TRANS: London ... Settlers ... Join City */
1937 _("%s needs an improvement to grow, so "
1938 "%s cannot do %s."),
1939 city_name_get(target_city),
1940 unit_name_translation(actor),
1941 action_id_name_translation(stopped_action));
1942 break;
1943 case ANEK_CITY_NO_CAPACITY:
1944 notify_player(pplayer, unit_tile(actor),
1945 event, ftc_server,
1946 /* TRANS: Paris ... Airlift to City ... Warriors */
1947 _("%s has no capacity to %s %s."),
1948 city_name_get(explnat->capacity_city),
1949 action_id_name_translation(stopped_action),
1950 unit_name_translation(actor));
1951 break;
1952 case ANEK_TGT_TILE_UNKNOWN:
1953 notify_player(pplayer, unit_tile(actor),
1954 event, ftc_server,
1955 /* TRANS: Paratroopers ... Drop Paratrooper */
1956 _("%s can't do %s to an unknown tile."),
1957 unit_name_translation(actor),
1958 action_id_name_translation(stopped_action));
1959 break;
1960 case ANEK_TRIREME_MOVE:
1961 notify_player(pplayer, target_tile, event, ftc_server,
1962 _("%s cannot move that far from the coast line."),
1963 unit_link(actor));
1964 break;
1965 case ANEK_DISEMBARK_ACT:
1966 notify_player(pplayer, unit_tile(actor), event, ftc_server,
1967 _("%s cannot disembark outside of a city or a native base "
1968 "for %s."),
1969 unit_link(actor),
1970 utype_name_translation(
1971 unit_type_get(unit_transport_get(actor))));
1972 break;
1973 case ANEK_TGT_UNREACHABLE:
1974 notify_player(pplayer, target_tile,
1975 event, ftc_server,
1976 _("Your %s can't do %s there since there's an "
1977 "unreachable unit."),
1978 unit_name_translation(actor),
1979 action_id_name_translation(stopped_action));
1980 break;
1981 case ANEK_ACTION_BLOCKS:
1982 notify_player(pplayer, unit_tile(actor),
1983 event, ftc_server,
1984 /* TRANS: Freight ... Recycle Unit ... Help Wonder ... */
1985 _("Your %s can't do %s when %s is legal."),
1986 unit_name_translation(actor),
1987 action_id_name_translation(stopped_action),
1988 action_id_name_translation(explnat->blocker->id));
1989 break;
1990 case ANEK_UNKNOWN:
1991 notify_player(pplayer, unit_tile(actor),
1992 event, ftc_server,
1993 _("Your %s was unable to %s."),
1994 unit_name_translation(actor),
1995 action_id_name_translation(stopped_action));
1996 break;
1999 free(explnat);
2002 /**************************************************************************
2003 Tell the client that the action it requested is illegal. This can be
2004 caused by the player (and therefore the client) not knowing that some
2005 condition of an action no longer is true.
2006 **************************************************************************/
2007 static void illegal_action(struct player *pplayer,
2008 struct unit *actor,
2009 enum gen_action stopped_action,
2010 struct player *tgt_player,
2011 const struct tile *target_tile,
2012 const struct city *target_city,
2013 const struct unit *target_unit,
2014 const enum action_requester requester)
2016 /* Why didn't the game check before trying something illegal? Did a good
2017 * reason to not call is_action_enabled_unit_on...() appear? The game is
2018 * omniscient... */
2019 fc_assert(requester != ACT_REQ_RULES);
2021 /* Don't punish the player for something the game did. Don't tell the
2022 * player that the rules required the game to try to do something
2023 * illegal. */
2024 fc_assert_ret_msg((requester == ACT_REQ_PLAYER
2025 || requester == ACT_REQ_SS_AGENT),
2026 "The player wasn't responsible for this.");
2028 /* The mistake may have a cost. */
2029 actor->moves_left = MAX(0, actor->moves_left
2030 - get_target_bonus_effects(NULL,
2031 unit_owner(actor),
2032 tgt_player,
2033 NULL,
2034 NULL,
2035 NULL,
2036 actor,
2037 unit_type_get(actor),
2038 NULL,
2039 NULL,
2040 action_by_number(stopped_action),
2041 EFT_ILLEGAL_ACTION_MOVE_COST));
2043 send_unit_info(NULL, actor);
2045 illegal_action_msg(pplayer, E_UNIT_ILLEGAL_ACTION,
2046 actor, stopped_action,
2047 target_tile, target_city, target_unit);
2050 /**************************************************************************
2051 Inform the client that something went wrong during a unit diplomat query
2052 **************************************************************************/
2053 static void unit_query_impossible(struct connection *pc,
2054 const int diplomat_id,
2055 const int target_id)
2057 dsend_packet_unit_action_answer(pc,
2058 diplomat_id, target_id,
2060 ACTION_NONE);
2063 /**************************************************************************
2064 Tell the client the cost of bribing a unit, inciting a revolt, or
2065 any other parameters needed for action.
2067 Only send result back to the requesting connection, not all
2068 connections for that player.
2069 **************************************************************************/
2070 void handle_unit_action_query(struct connection *pc,
2071 const int actor_id,
2072 const int target_id,
2073 const enum gen_action action_type)
2075 struct player *pplayer = pc->playing;
2076 struct unit *pactor = player_unit_by_number(pplayer, actor_id);
2077 struct unit *punit = game_unit_by_number(target_id);
2078 struct city *pcity = game_city_by_number(target_id);
2080 if (!action_id_exists(action_type)) {
2081 /* Non existing action */
2082 log_error("handle_unit_action_query() the action %d doesn't exist.",
2083 action_type);
2085 unit_query_impossible(pc, actor_id, target_id);
2086 return;
2089 if (NULL == pactor) {
2090 /* Probably died or bribed. */
2091 log_verbose("handle_unit_action_query() invalid actor %d",
2092 actor_id);
2093 unit_query_impossible(pc, actor_id, target_id);
2094 return;
2097 if (!utype_may_act_at_all(unit_type_get(pactor))) {
2098 /* Shouldn't happen */
2099 log_error("handle_unit_action_query() %s (%d) is not an actor",
2100 unit_rule_name(pactor), actor_id);
2101 unit_query_impossible(pc, actor_id, target_id);
2102 return;
2105 switch (action_type) {
2106 case ACTION_SPY_BRIBE_UNIT:
2107 if (punit
2108 && is_action_enabled_unit_on_unit(action_type,
2109 pactor, punit)) {
2110 dsend_packet_unit_action_answer(pc,
2111 actor_id, target_id,
2112 unit_bribe_cost(punit, pplayer),
2113 action_type);
2114 } else {
2115 illegal_action(pplayer, pactor, action_type,
2116 punit ? unit_owner(punit) : NULL,
2117 NULL, NULL, punit, ACT_REQ_PLAYER);
2118 unit_query_impossible(pc, actor_id, target_id);
2119 return;
2121 break;
2122 case ACTION_SPY_INCITE_CITY:
2123 if (pcity
2124 && is_action_enabled_unit_on_city(action_type,
2125 pactor, pcity)) {
2126 dsend_packet_unit_action_answer(pc,
2127 actor_id, target_id,
2128 city_incite_cost(pplayer, pcity),
2129 action_type);
2130 } else {
2131 illegal_action(pplayer, pactor, action_type,
2132 pcity ? city_owner(pcity) : NULL,
2133 NULL, pcity, NULL, ACT_REQ_PLAYER);
2134 unit_query_impossible(pc, actor_id, target_id);
2135 return;
2137 break;
2138 case ACTION_SPY_TARGETED_SABOTAGE_CITY:
2139 if (pcity
2140 && is_action_enabled_unit_on_city(action_type,
2141 pactor, pcity)) {
2142 spy_send_sabotage_list(pc, pactor, pcity, action_type);
2143 } else {
2144 illegal_action(pplayer, pactor, action_type,
2145 pcity ? city_owner(pcity) : NULL,
2146 NULL, pcity, NULL, ACT_REQ_PLAYER);
2147 unit_query_impossible(pc, actor_id, target_id);
2148 return;
2150 break;
2151 default:
2152 unit_query_impossible(pc, actor_id, target_id);
2153 return;
2157 /**************************************************************************
2158 Handle a request to do an action.
2160 action_type must be a valid action.
2161 **************************************************************************/
2162 void handle_unit_do_action(struct player *pplayer,
2163 const int actor_id,
2164 const int target_id,
2165 const int value,
2166 const char *name,
2167 const enum gen_action action_type)
2169 (void) unit_perform_action(pplayer, actor_id, target_id, value, name,
2170 action_type, ACT_REQ_PLAYER);
2173 /**************************************************************************
2174 Execute a request to perform an action and let the caller know if it was
2175 performed or not.
2177 The action must be a valid action.
2179 Returns TRUE iff action could be done, FALSE if it couldn't. Even if
2180 this returns TRUE, unit may have died during the action.
2181 **************************************************************************/
2182 bool unit_perform_action(struct player *pplayer,
2183 const int actor_id,
2184 const int target_id,
2185 const int value,
2186 const char *name,
2187 const enum gen_action action_type,
2188 const enum action_requester requester)
2190 struct action *paction;
2191 struct unit *actor_unit = player_unit_by_number(pplayer, actor_id);
2192 struct tile *target_tile = index_to_tile(&(wld.map), target_id);
2193 struct unit *punit = game_unit_by_number(target_id);
2194 struct city *pcity = game_city_by_number(target_id);
2196 if (!action_id_exists(action_type)) {
2197 /* Non existing action */
2198 log_error("unit_perform_action() the action %d doesn't exist.",
2199 action_type);
2201 return FALSE;
2204 paction = action_by_number(action_type);
2206 if (NULL == actor_unit) {
2207 /* Probably died or bribed. */
2208 log_verbose("handle_unit_do_action() invalid actor %d",
2209 actor_id);
2210 return FALSE;
2213 if (!utype_may_act_at_all(unit_type_get(actor_unit))) {
2214 /* Shouldn't happen */
2215 log_error("handle_unit_do_action() %s (%d) is not an actor unit",
2216 unit_rule_name(actor_unit), actor_id);
2217 return FALSE;
2220 if (paction->unitwaittime_controlled
2221 && !unit_can_do_action_now(actor_unit)) {
2222 /* Action not possible due to unitwaittime setting. */
2223 return FALSE;
2226 #define ACTION_STARTED_UNIT_CITY(action, actor, target, action_performer) \
2227 if (pcity \
2228 && is_action_enabled_unit_on_city(action_type, \
2229 actor_unit, pcity)) { \
2230 script_server_signal_emit("action_started_unit_city", 3, \
2231 API_TYPE_ACTION, action_by_number(action), \
2232 API_TYPE_UNIT, actor, \
2233 API_TYPE_CITY, target); \
2234 if (!actor || !unit_is_alive(actor_id)) { \
2235 /* Actor unit was destroyed during pre action Lua. */ \
2236 return FALSE; \
2238 if (!target || !city_exist(target_id)) { \
2239 /* Target city was destroyed during pre action Lua. */ \
2240 return FALSE; \
2242 return action_performer; \
2243 } else { \
2244 illegal_action(pplayer, actor_unit, action_type, \
2245 pcity ? city_owner(pcity) : NULL, NULL, pcity, NULL, \
2246 requester); \
2249 #define ACTION_STARTED_UNIT_SELF(action, actor, action_performer) \
2250 if (actor_unit \
2251 && is_action_enabled_unit_on_self(action_type, actor_unit)) { \
2252 script_server_signal_emit("action_started_unit_self", 2, \
2253 API_TYPE_ACTION, action_by_number(action), \
2254 API_TYPE_UNIT, actor); \
2255 if (!actor || !unit_is_alive(actor_id)) { \
2256 /* Actor unit was destroyed during pre action Lua. */ \
2257 return FALSE; \
2259 return action_performer; \
2260 } else { \
2261 illegal_action(pplayer, actor_unit, action_type, \
2262 unit_owner(actor_unit), NULL, NULL, actor_unit, \
2263 requester); \
2266 #define ACTION_STARTED_UNIT_UNIT(action, actor, target, action_performer) \
2267 if (punit \
2268 && is_action_enabled_unit_on_unit(action_type, actor_unit, punit)) {\
2269 script_server_signal_emit("action_started_unit_unit", 3, \
2270 API_TYPE_ACTION, action_by_number(action), \
2271 API_TYPE_UNIT, actor, \
2272 API_TYPE_UNIT, target); \
2273 if (!actor || !unit_is_alive(actor_id)) { \
2274 /* Actor unit was destroyed during pre action Lua. */ \
2275 return FALSE; \
2277 if (!target || !unit_is_alive(target_id)) { \
2278 /* Target unit was destroyed during pre action Lua. */ \
2279 return FALSE; \
2281 return action_performer; \
2282 } else { \
2283 illegal_action(pplayer, actor_unit, action_type, \
2284 punit ? unit_owner(punit) : NULL, NULL, NULL, punit, \
2285 requester); \
2288 #define ACTION_STARTED_UNIT_UNITS(action, actor, target, action_performer)\
2289 if (target_tile \
2290 && is_action_enabled_unit_on_units(action_type, \
2291 actor_unit, target_tile)) { \
2292 script_server_signal_emit("action_started_unit_units", 3, \
2293 API_TYPE_ACTION, action_by_number(action), \
2294 API_TYPE_UNIT, actor, \
2295 API_TYPE_TILE, target); \
2296 if (!actor || !unit_is_alive(actor_id)) { \
2297 /* Actor unit was destroyed during pre action Lua. */ \
2298 return FALSE; \
2300 return action_performer; \
2301 } else { \
2302 illegal_action(pplayer, actor_unit, action_type, \
2303 target_tile ? tile_owner(target_tile) : NULL, \
2304 target_tile, NULL, NULL, \
2305 requester); \
2308 #define ACTION_STARTED_UNIT_TILE(action, actor, target, action_performer) \
2309 if (target_tile \
2310 && is_action_enabled_unit_on_tile(action_type, \
2311 actor_unit, target_tile)) { \
2312 script_server_signal_emit("action_started_unit_tile", 3, \
2313 API_TYPE_ACTION, action_by_number(action), \
2314 API_TYPE_UNIT, actor, \
2315 API_TYPE_TILE, target); \
2316 if (!actor || !unit_is_alive(actor_id)) { \
2317 /* Actor unit was destroyed during pre action Lua. */ \
2318 return FALSE; \
2320 return action_performer; \
2321 } else { \
2322 illegal_action(pplayer, actor_unit, action_type, \
2323 NULL, target_tile, NULL, NULL, \
2324 requester); \
2327 switch(action_type) {
2328 case ACTION_SPY_BRIBE_UNIT:
2329 ACTION_STARTED_UNIT_UNIT(action_type, actor_unit, punit,
2330 diplomat_bribe(pplayer, actor_unit, punit,
2331 action_type));
2332 break;
2333 case ACTION_SPY_SABOTAGE_UNIT:
2334 ACTION_STARTED_UNIT_UNIT(action_type, actor_unit, punit,
2335 spy_sabotage_unit(pplayer, actor_unit,
2336 punit, action_type));
2337 break;
2338 case ACTION_EXPEL_UNIT:
2339 ACTION_STARTED_UNIT_UNIT(action_type, actor_unit, punit,
2340 do_expel_unit(pplayer, actor_unit, punit));
2341 break;
2342 case ACTION_HEAL_UNIT:
2343 ACTION_STARTED_UNIT_UNIT(action_type, actor_unit, punit,
2344 do_heal_unit(pplayer, actor_unit, punit));
2345 break;
2346 case ACTION_DISBAND_UNIT:
2347 ACTION_STARTED_UNIT_SELF(action_type, actor_unit,
2348 do_unit_disband(pplayer, actor_unit));
2349 break;
2350 case ACTION_SPY_SABOTAGE_CITY:
2351 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2352 diplomat_sabotage(pplayer, actor_unit, pcity,
2353 B_LAST, action_type));
2354 break;
2355 case ACTION_SPY_TARGETED_SABOTAGE_CITY:
2356 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2357 diplomat_sabotage(pplayer, actor_unit, pcity,
2358 value - 1, action_type));
2359 break;
2360 case ACTION_SPY_POISON:
2361 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2362 spy_poison(pplayer, actor_unit, pcity,
2363 action_type));
2364 break;
2365 case ACTION_SPY_INVESTIGATE_CITY:
2366 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2367 diplomat_investigate(pplayer,
2368 actor_unit, pcity,
2369 action_type,
2370 FALSE));
2371 break;
2372 case ACTION_INV_CITY_SPEND:
2373 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2374 diplomat_investigate(pplayer,
2375 actor_unit, pcity,
2376 action_type,
2377 TRUE));
2378 break;
2379 case ACTION_ESTABLISH_EMBASSY:
2380 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2381 diplomat_embassy(pplayer, actor_unit, pcity,
2382 action_type, FALSE));
2383 break;
2384 case ACTION_ESTABLISH_EMBASSY_STAY:
2385 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2386 diplomat_embassy(pplayer, actor_unit, pcity,
2387 action_type, TRUE));
2388 break;
2389 case ACTION_SPY_INCITE_CITY:
2390 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2391 diplomat_incite(pplayer, actor_unit, pcity,
2392 action_type));
2393 break;
2394 case ACTION_SPY_STEAL_TECH:
2395 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2396 diplomat_get_tech(pplayer, actor_unit, pcity,
2397 A_UNSET, action_type));
2398 break;
2399 case ACTION_SPY_TARGETED_STEAL_TECH:
2400 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2401 diplomat_get_tech(pplayer, actor_unit, pcity,
2402 value, action_type));
2403 break;
2404 case ACTION_SPY_STEAL_GOLD:
2405 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2406 spy_steal_gold(pplayer, actor_unit, pcity,
2407 action_type));
2408 break;
2409 case ACTION_STEAL_MAPS:
2410 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2411 spy_steal_some_maps(pplayer, actor_unit,
2412 pcity, action_type));
2413 break;
2414 case ACTION_TRADE_ROUTE:
2415 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2416 do_unit_establish_trade(pplayer, actor_unit,
2417 pcity, TRUE));
2418 break;
2419 case ACTION_MARKETPLACE:
2420 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2421 do_unit_establish_trade(pplayer, actor_unit,
2422 pcity, FALSE));
2423 break;
2424 case ACTION_HELP_WONDER:
2425 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2426 do_unit_help_build_wonder(pplayer,
2427 actor_unit, pcity));
2428 break;
2429 case ACTION_SPY_NUKE:
2430 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2431 spy_nuke_city(pplayer, actor_unit, pcity,
2432 action_type));
2433 break;
2434 case ACTION_JOIN_CITY:
2435 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2436 city_add_unit(pplayer, actor_unit, pcity));
2437 break;
2438 case ACTION_DESTROY_CITY:
2439 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2440 unit_do_destroy_city(pplayer,
2441 actor_unit, pcity));
2442 break;
2443 case ACTION_RECYCLE_UNIT:
2444 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2445 unit_do_recycle(pplayer, actor_unit, pcity));
2446 break;
2447 case ACTION_HOME_CITY:
2448 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2449 do_unit_change_homecity(actor_unit, pcity));
2450 break;
2451 case ACTION_UPGRADE_UNIT:
2452 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2453 do_unit_upgrade(pplayer, actor_unit,
2454 pcity, requester));
2455 break;
2456 case ACTION_CONQUER_CITY:
2457 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2458 do_unit_conquer_city(pplayer, actor_unit,
2459 pcity, paction));
2460 break;
2461 case ACTION_AIRLIFT:
2462 ACTION_STARTED_UNIT_CITY(action_type, actor_unit, pcity,
2463 do_airline(actor_unit, pcity));
2464 break;
2465 case ACTION_CAPTURE_UNITS:
2466 ACTION_STARTED_UNIT_UNITS(action_type, actor_unit, target_tile,
2467 do_capture_units(pplayer, actor_unit,
2468 target_tile));
2469 break;
2470 case ACTION_BOMBARD:
2471 ACTION_STARTED_UNIT_UNITS(action_type, actor_unit, target_tile,
2472 unit_bombard(actor_unit, target_tile));
2473 break;
2474 case ACTION_FOUND_CITY:
2475 ACTION_STARTED_UNIT_TILE(action_type, actor_unit, target_tile,
2476 city_build(pplayer, actor_unit,
2477 target_tile, name));
2478 break;
2479 case ACTION_NUKE:
2480 ACTION_STARTED_UNIT_TILE(action_type, actor_unit, target_tile,
2481 unit_nuke(pplayer, actor_unit, target_tile));
2482 break;
2483 case ACTION_PARADROP:
2484 ACTION_STARTED_UNIT_TILE(action_type, actor_unit, target_tile,
2485 do_paradrop(actor_unit, target_tile));
2486 break;
2487 case ACTION_ATTACK:
2488 ACTION_STARTED_UNIT_TILE(action_type, actor_unit, target_tile,
2489 do_attack(actor_unit, target_tile));
2490 break;
2491 case ACTION_COUNT:
2492 log_error("handle_unit_do_action() %s (%d) ordered to perform an "
2493 "invalid action.",
2494 unit_rule_name(actor_unit), actor_id);
2495 break;
2498 /* Something must have gone wrong. */
2499 return FALSE;
2502 /**************************************************************************
2503 Transfer a unit from one city (and possibly player) to another.
2504 If 'rehome' is not set, only change the player which owns the unit
2505 (the new owner is new_pcity's owner). Otherwise the new unit will be
2506 given a homecity, even if it was homeless before.
2507 This new homecity must be valid for this unit.
2508 **************************************************************************/
2509 void unit_change_homecity_handling(struct unit *punit, struct city *new_pcity,
2510 bool rehome)
2512 struct city *old_pcity = game_city_by_number(punit->homecity);
2513 struct player *old_owner = unit_owner(punit);
2514 struct player *new_owner = city_owner(new_pcity);
2516 /* Calling this function when new_pcity is same as old_pcity should
2517 * be safe with current implementation, but it is not meant to
2518 * be used that way. */
2519 fc_assert_ret(new_pcity != old_pcity);
2521 /* If 'rehome' is not set, this function should only be used to change
2522 * which player owns the unit */
2523 fc_assert_ret(rehome || new_owner != old_owner);
2525 if (old_owner != new_owner) {
2526 struct city *pcity = tile_city(punit->tile);
2528 fc_assert(!utype_player_already_has_this_unique(new_owner,
2529 unit_type_get(punit)));
2531 vision_clear_sight(punit->server.vision);
2532 vision_free(punit->server.vision);
2534 if (pcity != NULL
2535 && !can_player_see_units_in_city(old_owner, pcity)) {
2536 /* Special case when city is being transferred. At this point city
2537 * itself has changed owner, so it's enemy city now that old owner
2538 * cannot see inside. All the normal methods of removing transferred
2539 * unit from previous owner's client think that there's no need to
2540 * remove unit as client shouldn't have it in first place. */
2541 unit_goes_out_of_sight(old_owner, punit);
2544 /* Remove AI control of the old owner. */
2545 CALL_PLR_AI_FUNC(unit_lost, old_owner, punit);
2547 unit_list_remove(old_owner->units, punit);
2548 unit_list_prepend(new_owner->units, punit);
2549 punit->owner = new_owner;
2551 /* Activate AI control of the new owner. */
2552 CALL_PLR_AI_FUNC(unit_got, new_owner, punit);
2554 punit->server.vision = vision_new(new_owner, unit_tile(punit));
2555 unit_refresh_vision(punit);
2558 if (rehome) {
2559 fc_assert(!unit_has_type_flag(punit, UTYF_NOHOME));
2561 /* Remove from old city first and add to new city only after that.
2562 * This is more robust in case old_city == new_city (currently
2563 * prohibited by fc_assert in the beginning of the function).
2565 if (old_pcity) {
2566 /* Even if unit is dead, we have to unlink unit pointer (punit). */
2567 unit_list_remove(old_pcity->units_supported, punit);
2568 /* update unit upkeep */
2569 city_units_upkeep(old_pcity);
2572 unit_list_prepend(new_pcity->units_supported, punit);
2574 /* update unit upkeep */
2575 city_units_upkeep(new_pcity);
2577 punit->homecity = new_pcity->id;
2580 if (!can_unit_continue_current_activity(punit)) {
2581 /* This is mainly for cases where unit owner changes to one not knowing
2582 * Railroad tech when unit is already building railroad. */
2583 set_unit_activity(punit, ACTIVITY_IDLE);
2586 /* Send info to players and observers. */
2587 send_unit_info(NULL, punit);
2589 city_refresh(new_pcity);
2590 send_city_info(new_owner, new_pcity);
2592 if (old_pcity) {
2593 fc_assert(city_owner(old_pcity) == old_owner);
2594 city_refresh(old_pcity);
2595 send_city_info(old_owner, old_pcity);
2598 unit_get_goods(punit);
2600 fc_assert(unit_owner(punit) == city_owner(new_pcity));
2603 /**************************************************************************
2604 Change a unit's home city.
2606 Returns TRUE iff the action could be done, FALSE if it couldn't.
2607 **************************************************************************/
2608 static bool do_unit_change_homecity(struct unit *punit,
2609 struct city *pcity)
2611 unit_change_homecity_handling(punit, pcity, TRUE);
2613 return punit->homecity == pcity->id;
2616 /**************************************************************************
2617 Disband a unit.
2619 No shields spent to build the unit is added to the shield stock of any
2620 city even if the unit is located inside it.
2622 Returns TRUE iff the action could be done, FALSE if it couldn't. Even if
2623 this returns TRUE, the unit may have died during the action.
2624 **************************************************************************/
2625 static bool do_unit_disband(struct player *pplayer, struct unit *punit)
2627 /* Sanity check: The actor still exists. */
2628 fc_assert_ret_val(pplayer, FALSE);
2629 fc_assert_ret_val(punit, FALSE);
2631 wipe_unit(punit, ULR_DISBANDED, NULL);
2633 /* The unit is now disbanded. */
2634 return TRUE;
2637 /**************************************************************************
2638 Recycle a unit in a city.
2640 1/2 of the shields used to build the unit is added to the city's shield
2641 stock for the current production.
2643 Returns TRUE iff action could be done, FALSE if it couldn't. Even if
2644 this returns TRUE, unit may have died during the action.
2645 **************************************************************************/
2646 static bool unit_do_recycle(struct player *pplayer,
2647 struct unit *punit,
2648 struct city *pcity)
2650 int shields;
2652 /* Sanity check: The actor still exists. */
2653 fc_assert_ret_val(pplayer, FALSE);
2654 fc_assert_ret_val(punit, FALSE);
2656 /* Sanity check: The target city still exists. */
2657 fc_assert_ret_val(pcity, FALSE);
2659 shields = unit_disband_shields(punit);
2661 /* Add the shields from recycling the unit to the city's current
2662 * production. */
2663 pcity->shield_stock += shields;
2665 /* If we change production later at this turn. No penalty is added. */
2666 pcity->disbanded_shields += shields;
2668 notify_player(pplayer, city_tile(pcity), E_CARAVAN_ACTION, ftc_server,
2669 /* TRANS: ... Ironclad ... New York */
2670 _("Recyled your %s to help the current production in %s."),
2671 unit_link(punit),
2672 city_link(pcity));
2674 send_city_info(city_owner(pcity), pcity);
2676 wipe_unit(punit, ULR_DISBANDED, NULL);
2678 /* The unit is now recycled. */
2679 return TRUE;
2682 /**************************************************************************
2683 This function assumes that the target city is valid. It should only be
2684 called after checking that the unit legally can join the target city.
2686 Returns TRUE iff action could be done, FALSE if it couldn't. Even if
2687 this returns TRUE, unit may have died during the action.
2688 **************************************************************************/
2689 static bool city_add_unit(struct player *pplayer, struct unit *punit,
2690 struct city *pcity)
2692 int amount = unit_pop_value(punit);
2694 /* Sanity check: The actor is still alive. */
2695 fc_assert_ret_val(punit, FALSE);
2697 /* Sanity check: The target city still exists. */
2698 fc_assert_ret_val(pcity, FALSE);
2700 fc_assert_ret_val(amount > 0, FALSE);
2702 script_server_signal_emit("city_size_change", 3,
2703 API_TYPE_CITY, pcity,
2704 API_TYPE_INT, amount,
2705 API_TYPE_STRING, "unit_added");
2706 city_size_add(pcity, amount);
2707 /* Make the new people something, otherwise city fails the checks */
2708 pcity->specialists[DEFAULT_SPECIALIST] += amount;
2709 citizens_update(pcity, unit_nationality(punit));
2710 /* Refresh the city data. */
2711 city_refresh(pcity);
2713 /* Notify the unit owner that the unit successfully joined the city. */
2714 notify_player(pplayer, city_tile(pcity), E_CITY_BUILD, ftc_server,
2715 _("%s added to aid %s in growing."),
2716 unit_tile_link(punit),
2717 city_link(pcity));
2718 if (pplayer != city_owner(pcity)) {
2719 /* Notify the city owner when a foreign unit joins a city. */
2720 notify_player(city_owner(pcity), city_tile(pcity), E_CITY_BUILD,
2721 ftc_server,
2722 /* TRANS: another player had his unit joint your city. */
2723 _("%s adds %s to your city %s."),
2724 player_name(unit_owner(punit)),
2725 unit_tile_link(punit),
2726 city_link(pcity));;
2729 action_id_consequence_success(ACTION_JOIN_CITY, pplayer,
2730 city_owner(pcity), city_tile(pcity),
2731 city_link(pcity));
2732 wipe_unit(punit, ULR_USED, NULL);
2734 sanity_check_city(pcity);
2736 send_city_info(NULL, pcity);
2738 return TRUE;
2741 /**************************************************************************
2742 This function assumes a certain level of consistency checking: There
2743 is no city under punit->(x,y), and that location is a valid one on
2744 which to build a city. It should only be called after a call to a
2745 function like test_unit_add_or_build_city, which does the checking.
2747 Returns TRUE iff action could be done, FALSE if it couldn't. Even if
2748 this returns TRUE, unit may have died during the action.
2749 **************************************************************************/
2750 static bool city_build(struct player *pplayer, struct unit *punit,
2751 struct tile *ptile, const char *name)
2753 char message[1024];
2754 int size;
2755 struct player *nationality;
2756 struct player *towner;
2758 /* Sanity check: The actor still exists. */
2759 fc_assert_ret_val(pplayer, FALSE);
2760 fc_assert_ret_val(punit, FALSE);
2762 towner = tile_owner(ptile);
2764 if (!is_allowed_city_name(pplayer, name, message, sizeof(message))) {
2765 notify_player(pplayer, ptile, E_BAD_COMMAND, ftc_server,
2766 "%s", message);
2767 return FALSE;
2770 nationality = unit_nationality(punit);
2772 create_city(pplayer, ptile, name, nationality);
2773 size = unit_type_get(punit)->city_size;
2774 if (size > 1) {
2775 struct city *pcity = tile_city(ptile);
2777 fc_assert_ret_val(pcity != NULL, FALSE);
2779 city_change_size(pcity, size, nationality, NULL);
2781 wipe_unit(punit, ULR_USED, NULL);
2783 /* May cause an incident even if the target tile is unclaimed. A ruleset
2784 * could give everyone a casus belli against the city founder. A rule
2785 * like that would make sense in a story where deep ecology is on the
2786 * table. (See also Voluntary Human Extinction Movement) */
2787 action_id_consequence_success(ACTION_FOUND_CITY, pplayer, towner,
2788 ptile, tile_link(ptile));
2790 return TRUE;
2793 /**************************************************************************
2794 Handle change in unit activity.
2795 **************************************************************************/
2796 static void handle_unit_change_activity_real(struct player *pplayer,
2797 int unit_id,
2798 enum unit_activity activity,
2799 struct extra_type *activity_target)
2801 struct unit *punit = player_unit_by_number(pplayer, unit_id);
2803 if (NULL == punit) {
2804 /* Probably died or bribed. */
2805 log_verbose("handle_unit_change_activity() invalid unit %d", unit_id);
2806 return;
2809 if (punit->activity == activity
2810 && punit->activity_target == activity_target
2811 && !punit->ai_controlled) {
2812 /* Treat change in ai.control as change in activity, so
2813 * idle autosettlers behave correctly when selected --dwp
2815 return;
2818 /* Remove city spot reservations for AI settlers on city founding
2819 * mission, before goto_tile reset. */
2820 if (punit->server.adv->task != AUT_NONE) {
2821 adv_unit_new_task(punit, AUT_NONE, NULL);
2824 punit->ai_controlled = FALSE;
2825 punit->goto_tile = NULL;
2827 if (activity == ACTIVITY_GOTO) {
2828 /* Don't permit a client to set a unit's activity to ACTIVITY_GOTO.
2829 * Setting ACTIVITY_GOTO from the client results in a unit indicating
2830 * it is going somewhere while it is standing still. The appearance of
2831 * the unit doing something can trick the user to not make use of it.
2833 * Handled here because adv_follow_path() uses unit_activity_handling()
2834 * to set a unit's activity to ACTIVITY_GOTO. */
2835 return;
2838 /* The activity can now be set. */
2839 unit_activity_handling_targeted(punit, activity, &activity_target);
2841 if (activity == ACTIVITY_EXPLORE) {
2842 /* Exploring is handled here explicitly, since the player expects to
2843 * see an immediate response from setting a unit to auto-explore.
2844 * Handling it deeper in the code leads to some tricky recursive loops -
2845 * see PR#2631. */
2846 if (punit->moves_left > 0) {
2847 do_explore(punit);
2852 /**************************************************************************
2853 Handle change in unit activity.
2854 **************************************************************************/
2855 void handle_unit_change_activity(struct player *pplayer, int unit_id,
2856 enum unit_activity activity,
2857 int target_id)
2859 struct extra_type *activity_target;
2861 if (target_id < 0 || target_id >= game.control.num_extra_types) {
2862 activity_target = NULL;
2863 } else {
2864 activity_target = extra_by_number(target_id);
2867 #ifdef FREECIV_WEB
2868 /* Web-client is not capable of selecting target, so we do it server side */
2869 if (activity_target == NULL) {
2870 struct unit *punit = player_unit_by_number(pplayer, unit_id);
2871 bool required = TRUE;
2873 if (punit == NULL) {
2874 return;
2877 if (activity == ACTIVITY_IRRIGATE) {
2878 struct tile *ptile = unit_tile(punit);
2879 struct terrain *pterrain = tile_terrain(ptile);
2881 if (pterrain->irrigation_result != pterrain) {
2882 required = FALSE;
2883 } else {
2884 activity_target = next_extra_for_tile(ptile, EC_IRRIGATION,
2885 pplayer, punit);
2887 } else if (activity == ACTIVITY_MINE) {
2888 struct tile *ptile = unit_tile(punit);
2889 struct terrain *pterrain = tile_terrain(ptile);
2891 if (pterrain->mining_result != pterrain) {
2892 required = FALSE;
2893 } else {
2894 activity_target = next_extra_for_tile(ptile, EC_MINE,
2895 pplayer, punit);
2897 } else if (activity == ACTIVITY_BASE) {
2898 struct tile *ptile = unit_tile(punit);
2899 struct base_type *pbase =
2900 get_base_by_gui_type(BASE_GUI_FORTRESS, punit, ptile);
2902 if (pbase != NULL) {
2903 activity_target = base_extra_get(pbase);
2906 } else if (activity == ACTIVITY_POLLUTION) {
2907 activity_target = prev_extra_in_tile(unit_tile(punit), ERM_CLEANPOLLUTION,
2908 pplayer, punit);
2909 } else if (activity == ACTIVITY_FALLOUT) {
2910 activity_target = prev_extra_in_tile(unit_tile(punit), ERM_CLEANFALLOUT,
2911 pplayer, punit);
2912 } else {
2913 required = FALSE;
2916 if (activity_target == NULL && required) {
2917 /* Nothing more we can do */
2918 return;
2921 #endif /* FREECIV_WEB */
2923 handle_unit_change_activity_real(pplayer, unit_id, activity, activity_target);
2926 /**************************************************************************
2927 Make sure everyone who can see combat does.
2928 **************************************************************************/
2929 static void see_combat(struct unit *pattacker, struct unit *pdefender)
2931 struct packet_unit_short_info unit_att_short_packet, unit_def_short_packet;
2932 struct packet_unit_info unit_att_packet, unit_def_packet;
2935 * Special case for attacking/defending:
2937 * Normally the player doesn't get the information about the units inside a
2938 * city. However for attacking/defending the player has to know the unit of
2939 * the other side. After the combat a remove_unit packet will be sent
2940 * to the client to tidy up.
2942 * Note these packets must be sent out before unit_versus_unit is called,
2943 * so that the original unit stats (HP) will be sent.
2945 package_short_unit(pattacker, &unit_att_short_packet,
2946 UNIT_INFO_IDENTITY, 0);
2947 package_short_unit(pdefender, &unit_def_short_packet,
2948 UNIT_INFO_IDENTITY, 0);
2949 package_unit(pattacker, &unit_att_packet);
2950 package_unit(pdefender, &unit_def_packet);
2952 conn_list_iterate(game.est_connections, pconn) {
2953 struct player *pplayer = pconn->playing;
2955 if (pplayer != NULL) {
2957 /* NOTE: this means the player can see combat between submarines even
2958 * if neither sub is visible. See similar comment in send_combat. */
2959 if (map_is_known_and_seen(unit_tile(pattacker), pplayer, V_MAIN)
2960 || map_is_known_and_seen(unit_tile(pdefender), pplayer,
2961 V_MAIN)) {
2963 /* Units are sent even if they were visible already. They may
2964 * have changed orientation for combat. */
2965 if (pplayer == unit_owner(pattacker)) {
2966 send_packet_unit_info(pconn, &unit_att_packet);
2967 } else {
2968 send_packet_unit_short_info(pconn, &unit_att_short_packet, FALSE);
2971 if (pplayer == unit_owner(pdefender)) {
2972 send_packet_unit_info(pconn, &unit_def_packet);
2973 } else {
2974 send_packet_unit_short_info(pconn, &unit_def_short_packet, FALSE);
2977 } else if (pconn->observer) {
2978 /* Global observer sees everything... */
2979 send_packet_unit_info(pconn, &unit_att_packet);
2980 send_packet_unit_info(pconn, &unit_def_packet);
2982 } conn_list_iterate_end;
2985 /**************************************************************************
2986 Send combat info to players.
2987 **************************************************************************/
2988 static void send_combat(struct unit *pattacker, struct unit *pdefender,
2989 int veteran, int bombard)
2991 struct packet_unit_combat_info combat;
2993 combat.attacker_unit_id=pattacker->id;
2994 combat.defender_unit_id=pdefender->id;
2995 combat.attacker_hp=pattacker->hp;
2996 combat.defender_hp=pdefender->hp;
2997 combat.make_winner_veteran=veteran;
2999 players_iterate(other_player) {
3000 /* NOTE: this means the player can see combat between submarines even
3001 * if neither sub is visible. See similar comment in see_combat. */
3002 if (map_is_known_and_seen(unit_tile(pattacker), other_player, V_MAIN)
3003 || map_is_known_and_seen(unit_tile(pdefender), other_player,
3004 V_MAIN)) {
3005 lsend_packet_unit_combat_info(other_player->connections, &combat);
3008 * Remove the client knowledge of the units. This corresponds to the
3009 * send_packet_unit_short_info calls up above.
3011 if (!can_player_see_unit(other_player, pattacker)) {
3012 unit_goes_out_of_sight(other_player, pattacker);
3014 if (!can_player_see_unit(other_player, pdefender)) {
3015 unit_goes_out_of_sight(other_player, pdefender);
3018 } players_iterate_end;
3020 /* Send combat info to non-player observers as well. They already know
3021 * about the unit so no unit_info is needed. */
3022 conn_list_iterate(game.est_connections, pconn) {
3023 if (NULL == pconn->playing && pconn->observer) {
3024 send_packet_unit_combat_info(pconn, &combat);
3026 } conn_list_iterate_end;
3029 /**************************************************************************
3030 This function assumes the bombard is legal. The calling function should
3031 have already made all necessary checks.
3033 Returns TRUE iff action could be done, FALSE if it couldn't. Even if
3034 this returns TRUE, unit may have died during the action.
3035 **************************************************************************/
3036 static bool unit_bombard(struct unit *punit, struct tile *ptile)
3038 struct player *pplayer = unit_owner(punit);
3039 struct city *pcity = tile_city(ptile);
3041 /* Sanity check: The actor still exists. */
3042 fc_assert_ret_val(pplayer, FALSE);
3043 fc_assert_ret_val(punit, FALSE);
3045 log_debug("Start bombard: %s %s to %d, %d.",
3046 nation_rule_name(nation_of_player(pplayer)),
3047 unit_rule_name(punit), TILE_XY(ptile));
3049 unit_list_iterate_safe(ptile->units, pdefender) {
3051 /* Sanity checks */
3052 fc_assert_ret_val_msg(!pplayers_non_attack(unit_owner(punit),
3053 unit_owner(pdefender)),
3054 FALSE,
3055 "Trying to attack a unit with which you have "
3056 "peace or cease-fire at (%d, %d).",
3057 TILE_XY(unit_tile(pdefender)));
3058 fc_assert_ret_val_msg(!pplayers_allied(unit_owner(punit),
3059 unit_owner(pdefender)),
3060 FALSE,
3061 "Trying to attack a unit with which you have "
3062 "alliance at (%d, %d).",
3063 TILE_XY(unit_tile(pdefender)));
3065 if (is_unit_reachable_at(pdefender, punit, ptile)) {
3066 bool adj;
3067 enum direction8 facing;
3068 int att_hp, def_hp;
3070 adj = base_get_direction_for_step(punit->tile, pdefender->tile, &facing);
3072 if (adj) {
3073 punit->facing = facing;
3075 /* Unlike with normal attack, we don't change orientation of
3076 * defenders when bombarding */
3079 unit_bombs_unit(punit, pdefender, &att_hp, &def_hp);
3081 see_combat(punit, pdefender);
3083 punit->hp = att_hp;
3084 pdefender->hp = def_hp;
3086 send_combat(punit, pdefender, 0, 1);
3088 send_unit_info(NULL, pdefender);
3090 /* May cause an incident */
3091 action_id_consequence_success(ACTION_BOMBARD, unit_owner(punit),
3092 unit_owner(pdefender),
3093 unit_tile(pdefender),
3094 unit_link(pdefender));
3097 } unit_list_iterate_safe_end;
3099 punit->moves_left = 0;
3101 unit_did_action(punit);
3102 unit_forget_last_activity(punit);
3104 if (pcity
3105 && city_size_get(pcity) > 1
3106 && get_city_bonus(pcity, EFT_UNIT_NO_LOSE_POP) <= 0
3107 && kills_citizen_after_attack(punit)) {
3108 city_reduce_size(pcity, 1, pplayer, "bombard");
3109 city_refresh(pcity);
3110 send_city_info(NULL, pcity);
3113 send_unit_info(NULL, punit);
3115 return TRUE;
3118 /**************************************************************************
3119 Do a "regular" nuclear attack.
3121 Can be stopped by an EFT_NUKE_PROOF (SDI defended) city.
3123 This function assumes the attack is legal. The calling function should
3124 have already made all necessary checks.
3126 Returns TRUE iff action could be done, FALSE if it couldn't. Even if
3127 this returns TRUE, unit may have died during the action.
3128 **************************************************************************/
3129 static bool unit_nuke(struct player *pplayer, struct unit *punit,
3130 struct tile *def_tile)
3132 struct city *pcity;
3134 /* Sanity check: The actor still exists. */
3135 fc_assert_ret_val(pplayer, FALSE);
3136 fc_assert_ret_val(punit, FALSE);
3138 log_debug("Start nuclear attack: %s %s against (%d, %d).",
3139 nation_rule_name(nation_of_player(pplayer)),
3140 unit_rule_name(punit),
3141 TILE_XY(def_tile));
3143 if ((pcity = sdi_try_defend(pplayer, def_tile))) {
3144 /* FIXME: Remove the hard coded reference to SDI defense. */
3145 notify_player(pplayer, unit_tile(punit), E_UNIT_LOST_ATT, ftc_server,
3146 _("Your %s was shot down by "
3147 "SDI defenses, what a waste."), unit_tile_link(punit));
3148 notify_player(city_owner(pcity), def_tile, E_UNIT_WIN, ftc_server,
3149 _("The nuclear attack on %s was avoided by"
3150 " your SDI defense."), city_link(pcity));
3152 /* Trying to nuke something this close can be... unpopular. */
3153 action_id_consequence_caught(ACTION_NUKE, pplayer,
3154 city_owner(pcity),
3155 def_tile, unit_tile_link(punit));
3157 /* Remove the destroyed nuke. */
3158 wipe_unit(punit, ULR_SDI, city_owner(pcity));
3160 return FALSE;
3163 dlsend_packet_nuke_tile_info(game.est_connections, tile_index(def_tile));
3165 wipe_unit(punit, ULR_DETONATED, NULL);
3166 do_nuclear_explosion(pplayer, def_tile);
3168 /* May cause an incident even if the target tile is unclaimed. A ruleset
3169 * could give everyone a casus belli against the tile nuker. A rule
3170 * like that would make sense in a story where detonating any nuke at all
3171 * could be forbidden. */
3172 action_id_consequence_success(ACTION_NUKE, pplayer,
3173 tile_owner(def_tile),
3174 def_tile,
3175 tile_link(def_tile));
3177 return TRUE;
3180 /**************************************************************************
3181 Destroy the target city.
3183 This function assumes the destruction is legal. The calling function
3184 should have already made all necessary checks.
3186 Returns TRUE iff action could be done, FALSE if it couldn't. Even if
3187 this returns TRUE, unit may have died during the action.
3188 **************************************************************************/
3189 static bool unit_do_destroy_city(struct player *act_player,
3190 struct unit *act_unit,
3191 struct city *tgt_city)
3193 int tgt_city_id;
3194 struct player *tgt_player;
3195 bool try_civil_war = FALSE;
3197 /* Sanity check: The actor still exists. */
3198 fc_assert_ret_val(act_player, FALSE);
3199 fc_assert_ret_val(act_unit, FALSE);
3201 /* Sanity check: The target city still exists. */
3202 fc_assert_ret_val(tgt_city, FALSE);
3204 tgt_player = city_owner(tgt_city);
3206 /* How can a city be ownerless? */
3207 fc_assert_ret_val(tgt_player, FALSE);
3209 /* Save city ID. */
3210 tgt_city_id = tgt_city->id;
3212 if (is_capital(tgt_city)
3213 && (tgt_player->spaceship.state == SSHIP_STARTED
3214 || tgt_player->spaceship.state == SSHIP_LAUNCHED)) {
3215 /* Destroying this city destroys the victim's space ship. */
3216 spaceship_lost(tgt_player);
3219 if (is_capital(tgt_city)
3220 && civil_war_possible(tgt_player, TRUE, TRUE)
3221 && normal_player_count() < MAX_NUM_PLAYERS
3222 && civil_war_triggered(tgt_player)) {
3223 /* Destroying this city can trigger a civil war. */
3224 try_civil_war = TRUE;
3227 /* Let the actor know. */
3228 notify_player(act_player, city_tile(tgt_city),
3229 E_UNIT_WIN, ftc_server,
3230 _("You destroy %s completely."),
3231 city_tile_link(tgt_city));
3233 if (tgt_player != act_player) {
3234 /* This was done to a foreign city. Inform the victim player. */
3235 notify_player(tgt_player, city_tile(tgt_city),
3236 E_CITY_LOST, ftc_server,
3237 _("%s has been destroyed by %s."),
3238 city_tile_link(tgt_city),
3239 player_name(act_player));
3242 /* May cause an incident */
3243 action_id_consequence_success(ACTION_DESTROY_CITY, act_player,
3244 tgt_player, city_tile(tgt_city),
3245 city_link(tgt_city));
3247 /* Run post city destruction Lua script. */
3248 script_server_signal_emit("city_destroyed", 3,
3249 API_TYPE_CITY, tgt_city,
3250 API_TYPE_PLAYER, tgt_player,
3251 API_TYPE_PLAYER, act_player);
3253 /* Can't be sure of city existence after running script. */
3254 if (city_exist(tgt_city_id)) {
3255 remove_city(tgt_city);
3258 if (try_civil_war) {
3259 /* Try to start the civil war. */
3260 (void) civil_war(tgt_player);
3263 /* The city is no more. */
3264 return TRUE;
3267 /**************************************************************************
3268 Do a "regular" attack.
3270 This function assumes the attack is legal. The calling function should
3271 have already made all necessary checks.
3273 Returns TRUE iff action could be done, FALSE if it couldn't. Even if
3274 this returns TRUE, unit may have died during the action.
3275 **************************************************************************/
3276 static bool do_attack(struct unit *punit, struct tile *def_tile)
3278 char loser_link[MAX_LEN_LINK], winner_link[MAX_LEN_LINK];
3279 struct unit *ploser, *pwinner;
3280 struct city *pcity;
3281 int moves_used, def_moves_used;
3282 int old_unit_vet, old_defender_vet, vet;
3283 int winner_id;
3284 struct player *pplayer = unit_owner(punit);
3285 bool adj;
3286 enum direction8 facing;
3287 int att_hp, def_hp;
3288 struct unit *pdefender;
3290 if (!(pdefender = get_defender(punit, def_tile))) {
3291 /* Can't fight air... */
3292 return FALSE;
3295 log_debug("Start attack: %s %s against %s %s.",
3296 nation_rule_name(nation_of_player(pplayer)),
3297 unit_rule_name(punit),
3298 nation_rule_name(nation_of_unit(pdefender)),
3299 unit_rule_name(pdefender));
3301 /* Sanity checks */
3302 fc_assert_ret_val_msg(!pplayers_non_attack(pplayer,
3303 unit_owner(pdefender)),
3304 FALSE,
3305 "Trying to attack a unit with which you have peace "
3306 "or cease-fire at (%d, %d).", TILE_XY(def_tile));
3307 fc_assert_ret_val_msg(!pplayers_allied(pplayer, unit_owner(pdefender)),
3308 FALSE,
3309 "Trying to attack a unit with which you have "
3310 "alliance at (%d, %d).", TILE_XY(def_tile));
3312 moves_used = unit_move_rate(punit) - punit->moves_left;
3313 def_moves_used = unit_move_rate(pdefender) - pdefender->moves_left;
3315 adj = base_get_direction_for_step(punit->tile, pdefender->tile, &facing);
3317 fc_assert(adj);
3318 if (adj) {
3319 punit->facing = facing;
3320 pdefender->facing = opposite_direction(facing);
3323 old_unit_vet = punit->veteran;
3324 old_defender_vet = pdefender->veteran;
3325 unit_versus_unit(punit, pdefender, &att_hp, &def_hp);
3327 if ((att_hp <= 0 || uclass_has_flag(unit_class_get(punit), UCF_MISSILE))
3328 && unit_transported(punit)) {
3329 /* Dying attacker must be first unloaded so it doesn't die insider transport */
3330 unit_transport_unload_send(punit);
3333 see_combat(punit, pdefender);
3335 punit->hp = att_hp;
3336 pdefender->hp = def_hp;
3338 combat_veterans(punit, pdefender);
3340 /* Adjust attackers moves_left _after_ unit_versus_unit() so that
3341 * the movement attack modifier is correct! --dwp
3343 * For greater Civ2 compatibility (and game balance issues), we recompute
3344 * the new total MP based on the HP the unit has left after being damaged,
3345 * and subtract the MPs that had been used before the combat (plus the
3346 * points used in the attack itself, for the attacker). -GJW, Glip
3348 punit->moves_left = unit_move_rate(punit) - moves_used - SINGLE_MOVE;
3349 pdefender->moves_left = unit_move_rate(pdefender) - def_moves_used;
3351 if (punit->moves_left < 0) {
3352 punit->moves_left = 0;
3354 if (pdefender->moves_left < 0) {
3355 pdefender->moves_left = 0;
3357 unit_did_action(punit);
3358 unit_forget_last_activity(punit);
3360 if (punit->hp > 0
3361 && (pcity = tile_city(def_tile))
3362 && city_size_get(pcity) > 1
3363 && get_city_bonus(pcity, EFT_UNIT_NO_LOSE_POP) <= 0
3364 && kills_citizen_after_attack(punit)) {
3365 city_reduce_size(pcity, 1, pplayer, "attack");
3366 city_refresh(pcity);
3367 send_city_info(NULL, pcity);
3369 if (unit_has_type_flag(punit, UTYF_ONEATTACK)) {
3370 punit->moves_left = 0;
3372 pwinner = (punit->hp > 0) ? punit : pdefender;
3373 winner_id = pwinner->id;
3374 ploser = (pdefender->hp > 0) ? punit : pdefender;
3376 vet = (pwinner->veteran == ((punit->hp > 0) ? old_unit_vet :
3377 old_defender_vet)) ? 0 : 1;
3379 send_combat(punit, pdefender, vet, 0);
3381 /* N.B.: unit_link always returns the same pointer. */
3382 sz_strlcpy(loser_link, unit_tile_link(ploser));
3383 sz_strlcpy(winner_link, uclass_has_flag(unit_class_get(pwinner), UCF_MISSILE)
3384 ? unit_tile_link(pwinner) : unit_link(pwinner));
3386 if (punit == ploser) {
3387 /* The attacker lost */
3388 log_debug("Attacker lost: %s %s against %s %s.",
3389 nation_rule_name(nation_of_player(pplayer)),
3390 unit_rule_name(punit),
3391 nation_rule_name(nation_of_unit(pdefender)),
3392 unit_rule_name(pdefender));
3394 notify_player(unit_owner(pwinner), unit_tile(pwinner),
3395 E_UNIT_WIN, ftc_server,
3396 /* TRANS: "Your Cannon ... the Polish Destroyer." */
3397 _("Your %s survived the pathetic attack from the %s %s."),
3398 winner_link,
3399 nation_adjective_for_player(unit_owner(ploser)),
3400 loser_link);
3401 if (vet) {
3402 notify_unit_experience(pwinner);
3404 notify_player(unit_owner(ploser), def_tile,
3405 E_UNIT_LOST_ATT, ftc_server,
3406 /* TRANS: "... Cannon ... the Polish Destroyer." */
3407 _("Your attacking %s failed against the %s %s!"),
3408 loser_link,
3409 nation_adjective_for_player(unit_owner(pwinner)),
3410 winner_link);
3411 wipe_unit(ploser, ULR_KILLED, unit_owner(pwinner));
3412 } else {
3413 /* The defender lost, the attacker punit lives! */
3415 log_debug("Defender lost: %s %s against %s %s.",
3416 nation_rule_name(nation_of_player(pplayer)),
3417 unit_rule_name(punit),
3418 nation_rule_name(nation_of_unit(pdefender)),
3419 unit_rule_name(pdefender));
3421 punit->moved = TRUE; /* We moved */
3422 kill_unit(pwinner, ploser,
3423 vet && !uclass_has_flag(unit_class_get(punit), UCF_MISSILE));
3424 if (unit_is_alive(winner_id)) {
3425 if (uclass_has_flag(unit_class_get(pwinner), UCF_MISSILE)) {
3426 wipe_unit(pwinner, ULR_MISSILE, NULL);
3427 return TRUE;
3429 } else {
3430 return TRUE;
3434 /* If attacker wins, and occupychance > 0, it might move in. Don't move in
3435 * if there are enemy units in the tile (a fortress, city or air base with
3436 * multiple defenders and unstacked combat). Note that this could mean
3437 * capturing (or destroying) a city. */
3439 if (pwinner == punit && fc_rand(100) < game.server.occupychance
3440 && !is_non_allied_unit_tile(def_tile, pplayer)) {
3442 /* Hack: make sure the unit has enough moves_left for the move to succeed,
3443 and adjust moves_left to afterward (if successful). */
3445 int old_moves = punit->moves_left;
3446 int full_moves = unit_move_rate(punit);
3448 punit->moves_left = full_moves;
3449 /* Post attack occupy move. */
3450 if (((pcity = tile_city(def_tile))
3451 && is_action_enabled_unit_on_city(ACTION_CONQUER_CITY,
3452 punit, pcity)
3453 && unit_perform_action(unit_owner(punit), punit->id, pcity->id,
3454 0, "", ACTION_CONQUER_CITY, ACT_REQ_RULES))
3455 || (unit_move_handling(punit, def_tile, FALSE, TRUE, NULL))) {
3456 int mcost = MAX(0, full_moves - punit->moves_left - SINGLE_MOVE);
3458 /* Move cost is bigger of attack (SINGLE_MOVE) and occupying move costs.
3459 * Attack SINGLE_COST is already calculated in to old_moves. */
3460 punit->moves_left = old_moves - mcost;
3461 if (punit->moves_left < 0) {
3462 punit->moves_left = 0;
3464 } else {
3465 punit->moves_left = old_moves;
3469 /* The attacker may have died for many reasons */
3470 if (game_unit_by_number(winner_id) != NULL) {
3471 send_unit_info(NULL, pwinner);
3474 return TRUE;
3477 /**************************************************************************
3478 Have the unit conquer a city.
3480 This function assumes the attack is legal. The calling function should
3481 have already made all necessary checks.
3483 Returns TRUE iff action could be done, FALSE if it couldn't. Even if
3484 this returns TRUE, unit may have died during the action.
3485 **************************************************************************/
3486 static bool do_unit_conquer_city(struct player *act_player,
3487 struct unit *act_unit,
3488 struct city *tgt_city,
3489 struct action *paction)
3491 bool success;
3492 struct tile *tgt_tile = city_tile(tgt_city);
3493 int move_cost = map_move_cost_unit(act_unit, tgt_tile);
3494 int tgt_city_id = tgt_city->id;
3495 struct player *tgt_player = city_owner(tgt_city);
3496 const char *victim_link = city_link(tgt_city);
3498 /* Sanity check */
3499 fc_assert_ret_val(tgt_tile, FALSE);
3501 unit_move(act_unit, tgt_tile, move_cost, NULL, TRUE);
3503 /* The city may have been destroyed during the conquest. */
3504 success = (!city_exist(tgt_city_id)
3505 || city_owner(tgt_city) == act_player);
3507 if (success) {
3508 /* May cause an incident */
3509 action_consequence_success(paction, act_player, tgt_player, tgt_tile,
3510 victim_link);
3513 return success;
3516 /**************************************************************************
3517 see also aiunit could_unit_move_to_tile()
3518 **************************************************************************/
3519 static bool can_unit_move_to_tile_with_notify(struct unit *punit,
3520 struct tile *dest_tile,
3521 bool igzoc,
3522 struct unit *embark_to,
3523 bool enter_enemy_city)
3525 struct tile *src_tile = unit_tile(punit);
3526 enum unit_move_result reason =
3527 unit_move_to_tile_test(punit, punit->activity,
3528 src_tile, dest_tile, igzoc, embark_to,
3529 enter_enemy_city);
3531 switch (reason) {
3532 case MR_OK:
3533 return TRUE;
3535 case MR_NO_WAR:
3536 notify_player(unit_owner(punit), src_tile, E_BAD_COMMAND, ftc_server,
3537 _("Cannot attack unless you declare war first."));
3538 break;
3540 case MR_ZOC:
3541 notify_player(unit_owner(punit), src_tile, E_BAD_COMMAND, ftc_server,
3542 _("%s can only move into your own zone of control."),
3543 unit_link(punit));
3544 break;
3546 case MR_TRIREME:
3547 notify_player(unit_owner(punit), src_tile, E_BAD_COMMAND, ftc_server,
3548 _("%s cannot move that far from the coast line."),
3549 unit_link(punit));
3550 break;
3552 case MR_PEACE:
3553 if (tile_owner(dest_tile)) {
3554 notify_player(unit_owner(punit), src_tile, E_BAD_COMMAND, ftc_server,
3555 _("Cannot invade unless you break peace with "
3556 "%s first."),
3557 player_name(tile_owner(dest_tile)));
3559 break;
3561 case MR_CANNOT_DISEMBARK:
3562 notify_player(unit_owner(punit), src_tile, E_BAD_COMMAND, ftc_server,
3563 _("%s cannot disembark outside of a city or a native base "
3564 "for %s."),
3565 unit_link(punit),
3566 utype_name_translation(
3567 unit_type_get(unit_transport_get(punit))));
3568 break;
3570 case MR_NON_NATIVE_MOVE:
3571 notify_player(unit_owner(punit), src_tile, E_BAD_COMMAND, ftc_server,
3572 _("Terrain is unsuitable for %s units."),
3573 uclass_name_translation(unit_class_get(punit)));
3574 break;
3576 default:
3577 /* FIXME: need more explanations someday! */
3578 break;
3581 return FALSE;
3584 /**************************************************************************
3585 Will try to move to/attack the tile dest_x,dest_y. Returns TRUE if this
3586 was done, FALSE if it wasn't for some reason. Even if this returns TRUE,
3587 the unit may have died upon arrival to new tile.
3589 'igzoc' means ignore ZOC rules - not necessary for igzoc units etc, but
3590 done in some special cases (moving barbarians out of initial hut).
3591 Should normally be FALSE.
3593 'move_do_not_act' is another special case which should normally be
3594 FALSE. If TRUE any enabler controlled actions punit can perform to
3595 pdesttile it self or something located at it will be ignored. If FALSE
3596 the system will check if punit can perform any enabler controlled action
3597 to pdesttile. If it can the player will be asked to choose what to do. If
3598 it can't and punit is unable to move (or perform another non enabler
3599 controlled action) to pdesttile the game will try to explain why.
3601 FIXME: This function needs a good cleaning.
3602 **************************************************************************/
3603 bool unit_move_handling(struct unit *punit, struct tile *pdesttile,
3604 bool igzoc, bool move_do_not_act,
3605 struct unit *embark_to)
3607 struct player *pplayer = unit_owner(punit);
3608 struct city *pcity = tile_city(pdesttile);
3610 /*** Phase 1: Basic checks ***/
3612 /* this occurs often during lag, and to the AI due to some quirks -- Syela */
3613 if (!is_tiles_adjacent(unit_tile(punit), pdesttile)) {
3614 log_debug("tiles not adjacent in move request");
3615 return FALSE;
3619 if (punit->moves_left <= 0) {
3620 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
3621 _("This unit has no moves left."));
3622 return FALSE;
3625 if (!unit_can_do_action_now(punit)) {
3626 return FALSE;
3629 /*** Phase 2: Special abilities checks ***/
3631 /* Actors. Pop up an action selection dialog in the client.
3632 * If the AI has used a goto to send an actor to a target do not
3633 * pop up a dialog in the client.
3634 * For tiles occupied by allied cities or units, keep moving if
3635 * move_do_not_act tells us to, or if the unit is on goto and the tile
3636 * is not the final destination. */
3637 if (!move_do_not_act) {
3638 const bool can_not_move = !unit_can_move_to_tile(punit, pdesttile,
3639 igzoc, FALSE);
3640 struct tile *ttile = action_tgt_tile(punit, pdesttile, can_not_move);
3642 /* Consider to pop up the action selection dialog if a potential city,
3643 * unit or units target exists at the destination tile. A tile target
3644 * will only trigger the pop up if it may be legal. */
3645 if ((0 < unit_list_size(pdesttile->units) || pcity || ttile)) {
3646 /* A target (unit or city) exists at the tile. If a target is an ally
3647 * it still looks like a target since move_do_not_act isn't set.
3648 * Assume that the intention is to do an action. */
3650 if ((action_tgt_unit(punit, pdesttile, can_not_move)
3651 || action_tgt_city(punit, pdesttile, can_not_move)
3652 || action_tgt_tile_units(punit, pdesttile, can_not_move)
3653 || ttile)
3654 || can_not_move) {
3655 /* There is a target punit, from the player's point of view, may be
3656 * able to act against OR punit can't do any non action move. The
3657 * client should therefore ask what action(s) the unit can perform
3658 * to any targets at pdesttile.
3660 * In the first case the unit needs a decision about what action, if
3661 * any at all, to take. Asking what actions the unit can perform
3662 * will return a list of actions that may, from the players point of
3663 * view, be possible. The client can then show this list to the
3664 * player or, if configured to do so, make the choice it self.
3666 * In the last case the player may need an explanation about why no
3667 * action could be taken. Asking what actions the unit can perform
3668 * will provide this explanation. */
3669 punit->action_decision_want = ACT_DEC_ACTIVE;
3670 punit->action_decision_tile = pdesttile;
3671 send_unit_info(player_reply_dest(pplayer), punit);
3673 /* The move wasn't done because the unit wanted the player to
3674 * decide what to do or because the unit couldn't move to the
3675 * target tile. */
3676 return FALSE;
3681 /*** Phase 3: OK now move the unit ***/
3683 /* We cannot move a transport into a tile that holds
3684 * units or cities not allied with all of our cargo. */
3685 if (get_transporter_capacity(punit) > 0) {
3686 unit_list_iterate(unit_tile(punit)->units, pcargo) {
3687 if (unit_contained_in(pcargo, punit)
3688 && (is_non_allied_unit_tile(pdesttile, unit_owner(pcargo))
3689 || is_non_allied_city_tile(pdesttile,
3690 unit_owner(pcargo)))) {
3691 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
3692 _("A transported unit is not allied to all "
3693 "units or city on target tile."));
3694 return FALSE;
3696 } unit_list_iterate_end;
3699 if (can_unit_move_to_tile_with_notify(punit, pdesttile, igzoc,
3700 embark_to, FALSE)) {
3701 int move_cost = map_move_cost_unit(punit, pdesttile);
3703 unit_move(punit, pdesttile, move_cost, embark_to,
3704 FALSE);
3706 return TRUE;
3707 } else {
3708 return FALSE;
3712 /**************************************************************************
3713 Handle request to help in wonder building.
3715 Returns TRUE iff action could be done, FALSE if it couldn't. Even if
3716 this returns TRUE, unit may have died during the action.
3717 **************************************************************************/
3718 static bool do_unit_help_build_wonder(struct player *pplayer,
3719 struct unit *punit,
3720 struct city *pcity_dest)
3722 const char *work;
3724 /* Sanity check: The actor still exists. */
3725 fc_assert_ret_val(pplayer, FALSE);
3726 fc_assert_ret_val(punit, FALSE);
3728 /* Sanity check: The target city still exists. */
3729 fc_assert_ret_val(pcity_dest, FALSE);
3731 pcity_dest->shield_stock += unit_build_shield_cost(punit);
3732 pcity_dest->caravan_shields += unit_build_shield_cost(punit);
3734 conn_list_do_buffer(pplayer->connections);
3736 if (build_points_left(pcity_dest) >= 0) {
3737 /* TRANS: Your Caravan helps build the Pyramids in Bergen (4
3738 * remaining). You can reorder '4' and 'remaining' in the actual
3739 * format string. */
3740 work = _("remaining");
3741 } else {
3742 /* TRANS: Your Caravan helps build the Pyramids in Bergen (4
3743 * surplus). You can reorder '4' and 'surplus' in the actual
3744 * format string. */
3745 work = _("surplus");
3748 /* Let the player that just donated shields to the wonder building know
3749 * the result of his donation. */
3750 notify_player(pplayer, city_tile(pcity_dest), E_CARAVAN_ACTION,
3751 ftc_server,
3752 /* TRANS: Your Caravan helps build the Pyramids in Bergen
3753 * (4 surplus). */
3754 _("Your %s helps build the %s in %s (%d %s)."),
3755 unit_link(punit),
3756 improvement_name_translation(
3757 pcity_dest->production.value.building),
3758 city_link(pcity_dest),
3759 abs(build_points_left(pcity_dest)),
3760 work);
3762 /* May cause an incident */
3763 action_id_consequence_success(ACTION_HELP_WONDER, pplayer,
3764 city_owner(pcity_dest),
3765 city_tile(pcity_dest),
3766 city_link(pcity_dest));
3768 if (city_owner(pcity_dest) != unit_owner(punit)) {
3769 /* Tell the city owner about the gift he just received. */
3771 notify_player(city_owner(pcity_dest), city_tile(pcity_dest),
3772 E_CARAVAN_ACTION, ftc_server,
3773 /* TRANS: Help building the Pyramids in Bergen received
3774 * from Persian Caravan (4 surplus). */
3775 _("Help building the %s in %s received from %s %s "
3776 "(%d %s)."),
3777 improvement_name_translation(
3778 pcity_dest->production.value.building),
3779 city_link(pcity_dest),
3780 nation_adjective_for_player(pplayer),
3781 unit_link(punit),
3782 abs(build_points_left(pcity_dest)),
3783 work);
3786 wipe_unit(punit, ULR_USED, NULL);
3787 send_player_info_c(pplayer, pplayer->connections);
3788 send_city_info(pplayer, pcity_dest);
3789 conn_list_do_unbuffer(pplayer->connections);
3791 return TRUE;
3794 /**************************************************************************
3795 Handle request to establish traderoute. If pcity_dest is NULL, assumes
3796 that unit is inside target city.
3798 Returns TRUE iff action could be done, FALSE if it couldn't. Even if
3799 this returns TRUE, unit may have died during the action.
3800 **************************************************************************/
3801 static bool do_unit_establish_trade(struct player *pplayer,
3802 struct unit *punit,
3803 struct city *pcity_dest,
3804 bool est_if_able)
3806 char homecity_link[MAX_LEN_LINK], destcity_link[MAX_LEN_LINK];
3807 char punit_link[MAX_LEN_LINK];
3808 int revenue;
3809 bool can_establish;
3810 int home_overbooked = 0;
3811 int dest_overbooked = 0;
3812 int home_max;
3813 int dest_max;
3814 struct city *pcity_homecity;
3815 struct trade_route_list *routes_out_of_dest;
3816 struct trade_route_list *routes_out_of_home;
3817 enum traderoute_bonus_type bonus_type;
3818 const char *bonus_str;
3819 struct goods_type *goods;
3820 const char *goods_str;
3822 /* Sanity check: The actor still exists. */
3823 fc_assert_ret_val(pplayer, FALSE);
3824 fc_assert_ret_val(punit, FALSE);
3826 /* Sanity check: The target city still exists. */
3827 fc_assert_ret_val(pcity_dest, FALSE);
3829 pcity_homecity = player_city_by_number(pplayer, punit->homecity);
3831 if (!pcity_homecity) {
3832 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
3833 _("Sorry, your %s cannot establish"
3834 " a trade route because it has no home city."),
3835 unit_link(punit));
3836 return FALSE;
3839 if (game.info.goods_selection == GSM_ARRIVAL) {
3840 goods = goods_from_city_to_unit(pcity_homecity, punit);
3841 } else {
3842 goods = punit->carrying;
3844 if (goods == NULL) {
3845 notify_player(pplayer, unit_tile(punit), E_BAD_COMMAND, ftc_server,
3846 _("Sorry, your %s cannot establish"
3847 " a trade route because it's not carrying any goods."),
3848 unit_link(punit));
3849 return FALSE;
3852 sz_strlcpy(homecity_link, city_link(pcity_homecity));
3853 sz_strlcpy(destcity_link, city_link(pcity_dest));
3855 if (!can_cities_trade(pcity_homecity, pcity_dest)) {
3856 notify_player(pplayer, city_tile(pcity_dest), E_BAD_COMMAND, ftc_server,
3857 _("Sorry, your %s cannot establish"
3858 " a trade route between %s and %s."),
3859 unit_link(punit),
3860 homecity_link,
3861 destcity_link);
3862 return FALSE;
3865 sz_strlcpy(punit_link, unit_tile_link(punit));
3866 routes_out_of_home = trade_route_list_new();
3867 routes_out_of_dest = trade_route_list_new();
3869 /* This part of code works like can_establish_trade_route, except
3870 * that we actually do the action of making the trade route. */
3872 /* If we can't make a new trade route we can still get the trade bonus. */
3873 can_establish = est_if_able
3874 && !have_cities_trade_route(pcity_homecity, pcity_dest);
3876 if (can_establish) {
3877 home_max = max_trade_routes(pcity_homecity);
3878 dest_max = max_trade_routes(pcity_dest);
3879 home_overbooked = city_num_trade_routes(pcity_homecity) - home_max;
3880 dest_overbooked = city_num_trade_routes(pcity_dest) - dest_max;
3883 if (can_establish && (home_overbooked >= 0 || dest_overbooked >= 0)) {
3884 int trade = trade_base_between_cities(pcity_homecity, pcity_dest);
3886 /* See if there's a trade route we can cancel at the home city. */
3887 if (home_overbooked >= 0) {
3888 if (home_max <= 0
3889 || (city_trade_removable(pcity_homecity, routes_out_of_home)
3890 >= trade)) {
3891 notify_player(pplayer, city_tile(pcity_dest),
3892 E_BAD_COMMAND, ftc_server,
3893 _("Sorry, your %s cannot establish"
3894 " a trade route here!"),
3895 punit_link);
3896 if (home_max > 0) {
3897 notify_player(pplayer, city_tile(pcity_dest),
3898 E_BAD_COMMAND, ftc_server,
3899 PL_(" The city of %s already has %d "
3900 "better trade route!",
3901 " The city of %s already has %d "
3902 "better trade routes!", home_max),
3903 homecity_link,
3904 home_max);
3906 can_establish = FALSE;
3910 /* See if there's a trade route we can cancel at the dest city. */
3911 if (can_establish && dest_overbooked >= 0) {
3912 if (dest_max <= 0
3913 || (city_trade_removable(pcity_dest, routes_out_of_dest)
3914 >= trade)) {
3915 notify_player(pplayer, city_tile(pcity_dest),
3916 E_BAD_COMMAND, ftc_server,
3917 _("Sorry, your %s cannot establish"
3918 " a trade route here!"),
3919 punit_link);
3920 if (dest_max > 0) {
3921 notify_player(pplayer, city_tile(pcity_dest),
3922 E_BAD_COMMAND, ftc_server,
3923 PL_(" The city of %s already has %d "
3924 "better trade route!",
3925 " The city of %s already has %d "
3926 "better trade routes!", dest_max),
3927 destcity_link,
3928 dest_max);
3930 can_establish = FALSE;
3935 /* We now know for sure whether we can establish a trade route. */
3937 /* Calculate and announce initial revenue. */
3938 revenue = get_caravan_enter_city_trade_bonus(pcity_homecity, pcity_dest,
3939 can_establish);
3941 bonus_type = trade_route_settings_by_type(cities_trade_route_type(pcity_homecity, pcity_dest))->bonus_type;
3942 bonus_str = NULL;
3944 switch (bonus_type) {
3945 case TBONUS_NONE:
3946 break;
3947 case TBONUS_GOLD:
3948 /* TRANS: used as part of caravan revenue sentence. */
3949 bonus_str = Q_("?tradebonustype:gold");
3950 break;
3951 case TBONUS_SCIENCE:
3952 /* TRANS: used as part of caravan revenue sentence. */
3953 bonus_str = Q_("?tradebonustype:research");
3954 break;
3955 case TBONUS_BOTH:
3956 /* TRANS: used as part of caravan revenue sentence. */
3957 bonus_str = Q_("?tradebonustype:gold and research");
3958 break;
3961 conn_list_do_buffer(pplayer->connections);
3963 goods_str = goods_name_translation(goods);
3965 if (bonus_str != NULL) {
3966 notify_player(pplayer, city_tile(pcity_dest),
3967 E_CARAVAN_ACTION, ftc_server,
3968 /* TRANS: ... Caravan ... Paris ... Stockholm, ... Goods... gold and research. */
3969 PL_("Your %s from %s has arrived in %s carrying %s,"
3970 " and revenues amount to %d in %s.",
3971 "Your %s from %s has arrived in %s carrying %s,"
3972 " and revenues amount to %d in %s.",
3973 revenue),
3974 punit_link,
3975 homecity_link,
3976 destcity_link,
3977 goods_str,
3978 revenue,
3979 bonus_str);
3980 } else {
3981 notify_player(pplayer, city_tile(pcity_dest),
3982 E_CARAVAN_ACTION, ftc_server,
3983 /* TRANS: ... Caravan ... Paris ... Stockholm ... Goods */
3984 _("Your %s from %s has arrived in %s carrying %s."),
3985 punit_link,
3986 homecity_link,
3987 destcity_link,
3988 goods_str);
3990 wipe_unit(punit, ULR_USED, NULL);
3992 if (bonus_type == TBONUS_GOLD || bonus_type == TBONUS_BOTH) {
3993 pplayer->economic.gold += revenue;
3995 send_player_info_c(pplayer, pplayer->connections);
3998 if (bonus_type == TBONUS_SCIENCE || bonus_type == TBONUS_BOTH) {
3999 /* add bulbs and check for finished research */
4000 update_bulbs(pplayer, revenue, TRUE);
4002 /* Inform everyone about tech changes */
4003 send_research_info(research_get(pplayer), NULL);
4006 if (can_establish) {
4007 struct trade_route *proute_from, *proute_to;
4008 struct city_list *cities_out_of_home;
4009 struct city_list *cities_out_of_dest;
4011 /* Announce creation of trade route (it's not actually created until
4012 * later in this function, as we have to cancel existing routes, but
4013 * it makes more sense to announce in this order) */
4015 /* Always tell the unit owner */
4016 notify_player(pplayer, NULL,
4017 E_CARAVAN_ACTION, ftc_server,
4018 _("New trade route established from %s to %s."),
4019 homecity_link,
4020 destcity_link);
4021 if (pplayer != city_owner(pcity_dest)) {
4022 notify_player(city_owner(pcity_dest), city_tile(pcity_dest),
4023 E_CARAVAN_ACTION, ftc_server,
4024 _("The %s established a trade route between their "
4025 "city %s and %s."),
4026 nation_plural_for_player(pplayer),
4027 homecity_link,
4028 destcity_link);
4031 cities_out_of_home = city_list_new();
4032 cities_out_of_dest = city_list_new();
4034 /* Now cancel any less profitable trade route from the home city. */
4035 trade_route_list_iterate(routes_out_of_home, premove) {
4036 struct trade_route *pback;
4038 city_list_append(cities_out_of_home, game_city_by_number(premove->partner));
4040 pback = remove_trade_route(pcity_homecity, premove, TRUE, FALSE);
4041 free(premove);
4042 free(pback);
4043 } trade_route_list_iterate_end;
4045 /* And the same for the dest city. */
4046 trade_route_list_iterate(routes_out_of_dest, premove) {
4047 struct trade_route *pback;
4049 city_list_append(cities_out_of_dest, game_city_by_number(premove->partner));
4051 pback = remove_trade_route(pcity_dest, premove, TRUE, FALSE);
4052 free(premove);
4053 free(pback);
4054 } trade_route_list_iterate_end;
4056 /* Actually create the new trade route */
4057 proute_from = fc_malloc(sizeof(struct trade_route));
4058 proute_from->partner = pcity_dest->id;
4059 proute_from->goods = goods;
4061 proute_to = fc_malloc(sizeof(struct trade_route));
4062 proute_to->partner = pcity_homecity->id;
4063 proute_to->goods = goods;
4065 if (goods_has_flag(goods, GF_BIDIRECTIONAL)) {
4066 proute_from->dir = RDIR_BIDIRECTIONAL;
4067 proute_to->dir = RDIR_BIDIRECTIONAL;
4068 } else {
4069 proute_from->dir = RDIR_FROM;
4070 proute_to->dir = RDIR_TO;
4072 trade_route_list_append(pcity_homecity->routes, proute_from);
4073 trade_route_list_append(pcity_dest->routes, proute_to);
4075 /* Refresh the cities. */
4076 city_refresh(pcity_homecity);
4077 city_refresh(pcity_dest);
4078 city_list_iterate(cities_out_of_home, pcity) {
4079 city_refresh(pcity);
4080 } city_list_iterate_end;
4081 city_list_iterate(cities_out_of_dest, pcity) {
4082 city_refresh(pcity);
4083 } city_list_iterate_end;
4085 /* Notify the owners of the cities. */
4086 send_city_info(pplayer, pcity_homecity);
4087 send_city_info(city_owner(pcity_dest), pcity_dest);
4088 city_list_iterate(cities_out_of_home, pcity) {
4089 send_city_info(city_owner(pcity), pcity);
4090 } city_list_iterate_end;
4091 city_list_iterate(cities_out_of_dest, pcity) {
4092 send_city_info(city_owner(pcity), pcity);
4093 } city_list_iterate_end;
4095 /* Notify each player about the other cities so that they know about
4096 * its size for the trade calculation . */
4097 if (pplayer != city_owner(pcity_dest)) {
4098 send_city_info(city_owner(pcity_dest), pcity_homecity);
4099 send_city_info(pplayer, pcity_dest);
4102 city_list_iterate(cities_out_of_home, pcity) {
4103 if (city_owner(pcity_dest) != city_owner(pcity)) {
4104 send_city_info(city_owner(pcity_dest), pcity);
4105 send_city_info(city_owner(pcity), pcity_dest);
4107 if (pplayer != city_owner(pcity)) {
4108 send_city_info(pplayer, pcity);
4109 send_city_info(city_owner(pcity), pcity_homecity);
4111 } city_list_iterate_end;
4113 city_list_iterate(cities_out_of_dest, pcity) {
4114 if (city_owner(pcity_dest) != city_owner(pcity)) {
4115 send_city_info(city_owner(pcity_dest), pcity);
4116 send_city_info(city_owner(pcity), pcity_dest);
4118 if (pplayer != city_owner(pcity)) {
4119 send_city_info(pplayer, pcity);
4120 send_city_info(city_owner(pcity), pcity_homecity);
4122 } city_list_iterate_end;
4124 city_list_destroy(cities_out_of_home);
4125 city_list_destroy(cities_out_of_dest);
4128 /* May cause an incident */
4129 action_id_consequence_success(est_if_able ?
4130 ACTION_TRADE_ROUTE :
4131 ACTION_MARKETPLACE,
4132 pplayer, city_owner(pcity_dest),
4133 city_tile(pcity_dest),
4134 city_link(pcity_dest));
4136 conn_list_do_unbuffer(pplayer->connections);
4138 /* Free data. */
4139 trade_route_list_destroy(routes_out_of_home);
4140 trade_route_list_destroy(routes_out_of_dest);
4142 return TRUE;
4145 /**************************************************************************
4146 Change various unit server side client state.
4148 The server keeps various unit state that is owned by the client. The only
4149 consequence this state has for the game is how the client reacts to it.
4150 The state may be server side because the server writes to it or simply to
4151 have it end up in the save game.
4152 **************************************************************************/
4153 void handle_unit_sscs_set(struct player *pplayer,
4154 int unit_id,
4155 enum unit_ss_data_type type,
4156 int value)
4158 struct unit *punit = player_unit_by_number(pplayer, unit_id);
4160 if (NULL == punit) {
4161 /* Being asked to unqueue a "spent" unit because the client haven't
4162 * been told that it's gone is expected. */
4163 if (type != USSDT_UNQUEUE) {
4164 /* Probably died or bribed. */
4165 log_verbose("handle_unit_sscs_set() invalid unit %d", unit_id);
4168 return;
4171 switch (type) {
4172 case USSDT_QUEUE:
4173 /* Reminds the client to ask the server about what actions the unit can
4174 * perform against the target tile. Action decision state can be set by
4175 * the server it self too. */
4177 if (index_to_tile(&(wld.map), value) == NULL) {
4178 /* Asked to be reminded to ask what actions the unit can do to a non
4179 * existing target tile. */
4180 log_verbose("unit_sscs_set() invalid target tile %d for unit %d",
4181 value, unit_id);
4182 break;
4185 punit->action_decision_want = ACT_DEC_ACTIVE;
4186 punit->action_decision_tile = index_to_tile(&(wld.map), value);
4188 /* Let the client know that this unit needs the player to decide
4189 * what to do. */
4190 send_unit_info(player_reply_dest(pplayer), punit);
4192 break;
4193 case USSDT_UNQUEUE:
4194 /* Delete the reminder for the client to ask the server about what
4195 * actions the unit can perform against a certain target tile.
4196 * Action decision state can be set by the server it self too. */
4198 punit->action_decision_want = ACT_DEC_NOTHING;
4199 punit->action_decision_tile = NULL;
4201 /* Let the client know that this unit no longer needs the player to
4202 * decide what to do. */
4203 send_unit_info(player_reply_dest(pplayer), punit);
4205 break;
4206 case USSDT_BATTLE_GROUP:
4207 /* Battlegroups are handled entirely by the client, so all we have to
4208 do here is save the battlegroup ID so that it'll be persistent. */
4210 punit->battlegroup = CLIP(-1, value, MAX_NUM_BATTLEGROUPS);
4212 break;
4216 /**************************************************************************
4217 Handle request to set unit to autosettler mode.
4218 **************************************************************************/
4219 void handle_unit_autosettlers(struct player *pplayer, int unit_id)
4221 struct unit *punit = player_unit_by_number(pplayer, unit_id);
4223 if (NULL == punit) {
4224 /* Probably died or bribed. */
4225 log_verbose("handle_unit_autosettlers() invalid unit %d", unit_id);
4226 return;
4229 if (!can_unit_do_autosettlers(punit)) {
4230 return;
4233 punit->ai_controlled = TRUE;
4234 send_unit_info(NULL, punit);
4237 /**************************************************************************
4238 Update everything that needs changing when unit activity changes from
4239 old activity to new one.
4240 **************************************************************************/
4241 static void unit_activity_dependencies(struct unit *punit,
4242 enum unit_activity old_activity,
4243 struct extra_type *old_target)
4245 switch (punit->activity) {
4246 case ACTIVITY_IDLE:
4247 switch (old_activity) {
4248 case ACTIVITY_PILLAGE:
4250 if (old_target != NULL) {
4251 unit_list_iterate_safe(unit_tile(punit)->units, punit2) {
4252 if (punit2->activity == ACTIVITY_PILLAGE) {
4253 extra_deps_iterate(&(punit2->activity_target->reqs), pdep) {
4254 if (pdep == old_target) {
4255 set_unit_activity(punit2, ACTIVITY_IDLE);
4256 send_unit_info(NULL, punit2);
4257 break;
4259 } extra_deps_iterate_end;
4261 } unit_list_iterate_safe_end;
4263 break;
4265 case ACTIVITY_EXPLORE:
4266 /* Restore unit's control status */
4267 punit->ai_controlled = FALSE;
4268 break;
4269 default:
4270 ; /* do nothing */
4272 break;
4273 case ACTIVITY_EXPLORE:
4274 punit->ai_controlled = TRUE;
4275 set_unit_activity(punit, ACTIVITY_EXPLORE);
4276 send_unit_info(NULL, punit);
4277 break;
4278 default:
4279 /* do nothing */
4280 break;
4284 /**************************************************************************
4285 Handle request for changing activity.
4286 **************************************************************************/
4287 void unit_activity_handling(struct unit *punit,
4288 enum unit_activity new_activity)
4290 /* Must specify target for ACTIVITY_BASE */
4291 fc_assert_ret(new_activity != ACTIVITY_BASE
4292 && new_activity != ACTIVITY_GEN_ROAD);
4294 if (new_activity == ACTIVITY_PILLAGE) {
4295 struct extra_type *target = NULL;
4297 /* Assume untargeted pillaging if no target specified */
4298 unit_activity_handling_targeted(punit, new_activity, &target);
4299 } else if (can_unit_do_activity(punit, new_activity)) {
4300 enum unit_activity old_activity = punit->activity;
4301 struct extra_type *old_target = punit->activity_target;
4303 free_unit_orders(punit);
4304 set_unit_activity(punit, new_activity);
4305 send_unit_info(NULL, punit);
4306 unit_activity_dependencies(punit, old_activity, old_target);
4310 /**************************************************************************
4311 Handle request for targeted activity.
4312 **************************************************************************/
4313 void unit_activity_handling_targeted(struct unit *punit,
4314 enum unit_activity new_activity,
4315 struct extra_type **new_target)
4317 if (!activity_requires_target(new_activity)) {
4318 unit_activity_handling(punit, new_activity);
4319 } else if (can_unit_do_activity_targeted(punit, new_activity, *new_target)) {
4320 enum unit_activity old_activity = punit->activity;
4321 struct extra_type *old_target = punit->activity_target;
4322 enum unit_activity stored_activity = new_activity;
4324 free_unit_orders(punit);
4325 unit_assign_specific_activity_target(punit,
4326 &new_activity, new_target);
4327 if (new_activity != stored_activity
4328 && !activity_requires_target(new_activity)) {
4329 /* unit_assign_specific_activity_target() changed our target activity
4330 * (to ACTIVITY_IDLE in practice) */
4331 unit_activity_handling(punit, new_activity);
4332 } else {
4333 set_unit_activity_targeted(punit, new_activity, *new_target);
4334 send_unit_info(NULL, punit);
4335 unit_activity_dependencies(punit, old_activity, old_target);
4340 /****************************************************************************
4341 Handle a client request to load the given unit into the given transporter.
4342 ****************************************************************************/
4343 void handle_unit_load(struct player *pplayer, int cargo_id, int trans_id,
4344 int ttile_idx)
4346 struct unit *pcargo = player_unit_by_number(pplayer, cargo_id);
4347 struct unit *ptrans = game_unit_by_number(trans_id);
4348 struct tile *ptile = index_to_tile(&(wld.map), ttile_idx);
4349 struct tile *ctile;
4350 struct tile *ttile;
4351 bool moves = FALSE;
4352 bool leave = FALSE;
4354 if (NULL == pcargo) {
4355 /* Probably died or bribed. */
4356 log_verbose("handle_unit_load() invalid cargo %d", cargo_id);
4357 return;
4360 if (NULL == ptrans) {
4361 /* Probably died or bribed. */
4362 log_verbose("handle_unit_load() invalid transport %d", trans_id);
4363 return;
4366 ttile = unit_tile(ptrans);
4367 if (!same_pos(ttile, ptile)) {
4368 /* Transport no longer in where client assumed it to be. */
4369 return;
4372 ctile = unit_tile(pcargo);
4374 if (!same_pos(ctile, ttile)) {
4375 if (pcargo->moves_left <= 0
4376 || !unit_can_move_to_tile(pcargo, ttile, FALSE, FALSE)) {
4377 return;
4380 moves = TRUE;
4383 if (unit_transported(pcargo)) {
4384 if (!can_unit_unload(pcargo, ptrans)) {
4385 /* Can't leave current transport */
4386 return;
4389 leave = TRUE;
4392 /* A player may only load their units, but they may be loaded into
4393 * other player's transporters, depending on the rules in
4394 * could_unit_load(). */
4395 if (!could_unit_load(pcargo, ptrans)) {
4396 return;
4399 /* It's possible. Let's make all the necessary steps. */
4400 if (leave) {
4401 unit_transport_unload(pcargo);
4404 if (moves) {
4405 /* Pre load move. */
4406 unit_move_handling(pcargo, ttile, FALSE, TRUE, ptrans);
4407 return;
4410 /* Load the unit and send out info to clients. */
4411 unit_transport_load_send(pcargo, ptrans);
4414 /****************************************************************************
4415 Handle a client request to unload the given unit from the given
4416 transporter.
4417 ****************************************************************************/
4418 void handle_unit_unload(struct player *pplayer, int cargo_id, int trans_id)
4420 struct unit *pcargo = game_unit_by_number(cargo_id);
4421 struct unit *ptrans = game_unit_by_number(trans_id);
4423 if (NULL == pcargo) {
4424 /* Probably died or bribed. */
4425 log_verbose("handle_unit_unload() invalid cargo %d", cargo_id);
4426 return;
4429 if (NULL == ptrans) {
4430 /* Probably died or bribed. */
4431 log_verbose("handle_unit_unload() invalid transport %d", trans_id);
4432 return;
4435 /* You are allowed to unload a unit if it is yours or if the transporter
4436 * is yours. */
4437 if (unit_owner(pcargo) != pplayer && unit_owner(ptrans) != pplayer) {
4438 return;
4441 if (!can_unit_unload(pcargo, ptrans)) {
4442 return;
4445 if (!can_unit_survive_at_tile(pcargo, unit_tile(pcargo))) {
4446 return;
4449 /* Unload the unit and send out info to clients. */
4450 unit_transport_unload_send(pcargo);
4453 /****************************************************************************
4454 Receives route packages.
4455 ****************************************************************************/
4456 void handle_unit_orders(struct player *pplayer,
4457 const struct packet_unit_orders *packet)
4459 int length = packet->length, i;
4460 struct unit *punit = player_unit_by_number(pplayer, packet->unit_id);
4461 struct tile *src_tile = index_to_tile(&(wld.map), packet->src_tile);
4463 if (NULL == punit) {
4464 /* Probably died or bribed. */
4465 log_verbose("handle_unit_orders() invalid unit %d", packet->unit_id);
4466 return;
4469 if (0 > length || MAX_LEN_ROUTE < length) {
4470 /* Shouldn't happen */
4471 log_error("handle_unit_orders() invalid %s (%d) "
4472 "packet length %d (max %d)", unit_rule_name(punit),
4473 packet->unit_id, length, MAX_LEN_ROUTE);
4474 return;
4477 if (src_tile != unit_tile(punit)) {
4478 /* Failed sanity check. Usually this happens if the orders were sent
4479 * in the previous turn, and the client thought the unit was in a
4480 * different position than it's actually in. The easy solution is to
4481 * discard the packet. We don't send an error message to the client
4482 * here (though maybe we should?). */
4483 log_verbose("handle_unit_orders() invalid %s (%d) tile (%d, %d) "
4484 "!= (%d, %d)", unit_rule_name(punit), punit->id,
4485 TILE_XY(src_tile), TILE_XY(unit_tile(punit)));
4486 return;
4489 if (ACTIVITY_IDLE != punit->activity) {
4490 /* New orders implicitly abandon current activity */
4491 unit_activity_handling(punit, ACTIVITY_IDLE);
4494 for (i = 0; i < length; i++) {
4495 if (packet->orders[i] < 0 || packet->orders[i] > ORDER_LAST) {
4496 log_error("%s() %s (player nb %d) has sent an invalid order %d "
4497 "at index %d, truncating", __FUNCTION__,
4498 player_name(pplayer), player_number(pplayer),
4499 packet->orders[i], i);
4500 length = i;
4501 break;
4503 switch (packet->orders[i]) {
4504 case ORDER_MOVE:
4505 case ORDER_ACTION_MOVE:
4506 if (!map_untrusted_dir_is_valid(packet->dir[i])) {
4507 log_error("handle_unit_orders() %d isn't a valid move direction. "
4508 "Sent in order number %d from %s to unit number %d.",
4509 packet->dir[i], i,
4510 player_name(pplayer), packet->unit_id);
4512 return;
4514 break;
4515 case ORDER_ACTIVITY:
4516 switch (packet->activity[i]) {
4517 case ACTIVITY_FALLOUT:
4518 case ACTIVITY_POLLUTION:
4519 case ACTIVITY_PILLAGE:
4520 case ACTIVITY_MINE:
4521 case ACTIVITY_IRRIGATE:
4522 case ACTIVITY_TRANSFORM:
4523 case ACTIVITY_CONVERT:
4524 /* Simple activities. */
4525 break;
4526 case ACTIVITY_FORTIFYING:
4527 case ACTIVITY_SENTRY:
4528 if (i != length - 1) {
4529 /* Only allowed as the last order. */
4530 log_error("handle_unit_orders() activity %d is only allowed in "
4531 "the last order. "
4532 "Sent in order number %d from %s to unit number %d.",
4533 packet->activity[i], i,
4534 player_name(pplayer), packet->unit_id);
4536 return;
4538 break;
4539 case ACTIVITY_BASE:
4540 if (!is_extra_caused_by(extra_by_number(packet->target[i]), EC_BASE)) {
4541 log_error("handle_unit_orders() %s isn't a base. "
4542 "Sent in order number %d from %s to unit number %d.",
4543 extra_rule_name(extra_by_number(packet->target[i])), i,
4544 player_name(pplayer), packet->unit_id);
4546 return;
4548 break;
4549 case ACTIVITY_GEN_ROAD:
4550 if (!is_extra_caused_by(extra_by_number(packet->target[i]), EC_ROAD)) {
4551 log_error("handle_unit_orders() %s isn't a road. "
4552 "Sent in order number %d from %s to unit number %d.",
4553 extra_rule_name(extra_by_number(packet->target[i])), i,
4554 player_name(pplayer), packet->unit_id);
4556 return;
4558 break;
4559 /* Not supported yet. */
4560 case ACTIVITY_EXPLORE:
4561 case ACTIVITY_IDLE:
4562 /* Not set from the client. */
4563 case ACTIVITY_GOTO:
4564 case ACTIVITY_FORTIFIED:
4565 /* Compatiblity, used in savegames. */
4566 case ACTIVITY_OLD_ROAD:
4567 case ACTIVITY_OLD_RAILROAD:
4568 case ACTIVITY_FORTRESS:
4569 case ACTIVITY_AIRBASE:
4570 /* Unused. */
4571 case ACTIVITY_PATROL_UNUSED:
4572 case ACTIVITY_LAST:
4573 case ACTIVITY_UNKNOWN:
4574 log_error("handle_unit_orders() unsupported activity %d. "
4575 "Sent in order number %d from %s to unit number %d.",
4576 packet->activity[i], i,
4577 player_name(pplayer), packet->unit_id);
4579 return;
4582 if (packet->target[i] == EXTRA_NONE
4583 && unit_activity_needs_target_from_client(packet->activity[i])) {
4584 /* The orders system can't do server side target assignment for
4585 * this activity. */
4586 log_error("handle_unit_orders() can't assign target for %d. "
4587 "Sent in order number %d from %s to unit number %d.",
4588 packet->activity[i], i,
4589 player_name(pplayer), packet->unit_id);
4591 return;
4594 break;
4595 case ORDER_PERFORM_ACTION:
4596 if (!action_id_exists(packet->action[i])) {
4597 /* Non existing action */
4598 log_error("handle_unit_orders() the action %d doesn't exist. "
4599 "Sent in order number %d from %s to unit number %d.",
4600 packet->action[i], i,
4601 player_name(pplayer), packet->unit_id);
4603 return;
4606 if (action_id_distance_inside_max(packet->action[i], 2)) {
4607 /* Long range actions aren't supported in unit orders. Clients
4608 * should order them performed via the unit_do_action packet.
4610 * Reason: A unit order stores an action's target as the tile it is
4611 * located on. The tile is stored as a direction (when the target
4612 * is at a tile adjacent to the actor unit tile) or as no
4613 * direction (when the target is at the same tile as the actor
4614 * unit). The order system will pick a suitable target at the
4615 * specified tile during order execution. This makes it impossible
4616 * to target something that isn't at or next to the actors tile.
4617 * Being unable to exploit the full range of an action handicaps
4618 * it.
4620 * A patch that allows a distant target in an order should remove
4621 * this check and update the comment in the Qt client's
4622 * go_act_menu::create(). */
4624 log_error("handle_unit_orders() the action %s isn't supported in "
4625 "unit orders. "
4626 "Sent in order number %d from %s to unit number %d.",
4627 action_id_name_translation(packet->action[i]), i,
4628 player_name(pplayer), packet->unit_id);
4630 return;
4633 if (!action_id_distance_inside_max(packet->action[i], 1)
4634 && map_untrusted_dir_is_valid(packet->dir[i])) {
4635 /* Actor must be on the target tile. */
4636 log_error("handle_unit_orders() can't do %s to a neighbor tile. "
4637 "Sent in order number %d from %s to unit number %d.",
4638 action_id_rule_name(packet->action[i]), i,
4639 player_name(pplayer), packet->unit_id);
4641 return;
4644 /* Validate individual actions. */
4645 switch ((enum gen_action) packet->action[i]) {
4646 case ACTION_SPY_TARGETED_SABOTAGE_CITY:
4647 /* Sabotage target is production (-1) or a building. */
4648 if (!(packet->target[i] - 1 == -1
4649 || improvement_by_number(packet->target[i] - 1))) {
4650 /* Sabotage target is invalid. */
4652 log_error("handle_unit_orders() can't do %s without a target. "
4653 "Sent in order number %d from %s to unit number %d.",
4654 action_id_rule_name(packet->action[i]), i,
4655 player_name(pplayer), packet->unit_id);
4657 return;
4659 break;
4660 case ACTION_SPY_TARGETED_STEAL_TECH:
4661 if (packet->target[i] == A_NONE
4662 || (!valid_advance_by_number(packet->target[i])
4663 && packet->target[i] != A_FUTURE)) {
4664 /* Target tech is invalid. */
4666 log_error("handle_unit_orders() can't do %s without a target. "
4667 "Sent in order number %d from %s to unit number %d.",
4668 action_id_rule_name(packet->action[i]), i,
4669 player_name(pplayer), packet->unit_id);
4671 return;
4673 break;
4674 case ACTION_ESTABLISH_EMBASSY:
4675 case ACTION_ESTABLISH_EMBASSY_STAY:
4676 case ACTION_SPY_INVESTIGATE_CITY:
4677 case ACTION_INV_CITY_SPEND:
4678 case ACTION_SPY_POISON:
4679 case ACTION_SPY_STEAL_GOLD:
4680 case ACTION_SPY_SABOTAGE_CITY:
4681 case ACTION_SPY_STEAL_TECH:
4682 case ACTION_SPY_INCITE_CITY:
4683 case ACTION_TRADE_ROUTE:
4684 case ACTION_MARKETPLACE:
4685 case ACTION_HELP_WONDER:
4686 case ACTION_SPY_BRIBE_UNIT:
4687 case ACTION_SPY_SABOTAGE_UNIT:
4688 case ACTION_CAPTURE_UNITS:
4689 case ACTION_FOUND_CITY:
4690 case ACTION_JOIN_CITY:
4691 case ACTION_STEAL_MAPS:
4692 case ACTION_BOMBARD:
4693 case ACTION_SPY_NUKE:
4694 case ACTION_NUKE:
4695 case ACTION_DESTROY_CITY:
4696 case ACTION_EXPEL_UNIT:
4697 case ACTION_RECYCLE_UNIT:
4698 case ACTION_DISBAND_UNIT:
4699 case ACTION_HOME_CITY:
4700 case ACTION_UPGRADE_UNIT:
4701 case ACTION_ATTACK:
4702 case ACTION_CONQUER_CITY:
4703 case ACTION_PARADROP:
4704 case ACTION_AIRLIFT:
4705 case ACTION_HEAL_UNIT:
4706 /* No validation required. */
4707 break;
4708 /* Invalid action. Should have been caught above. */
4709 case ACTION_COUNT:
4710 fc_assert_ret_msg(packet->action[i] != ACTION_NONE,
4711 "ACTION_NONE in ORDER_PERFORM_ACTION order. "
4712 "Order number %d from %s to unit number %d.",
4713 i, player_name(pplayer), packet->unit_id);
4716 /* Don't validate that the target tile really contains a target or
4717 * that the actor player's map think the target tile has one.
4718 * The player may target a something from his player map that isn't
4719 * there any more, a target he thinks is there even if his player map
4720 * doesn't have it or even a target he assumes will be there when the
4721 * unit reaches the target tile.
4723 * With that said: The client should probably at least have an
4724 * option to only aim city targeted actions at cities. */
4726 break;
4727 case ORDER_FULL_MP:
4728 break;
4729 case ORDER_LAST:
4730 /* An invalid order. This is handled in execute_orders. */
4731 break;
4735 /* This must be before old orders are freed. If this is is
4736 * settlers on city founding mission, city spot reservation
4737 * from goto_tile must be freed, and free_unit_orders() loses
4738 * goto_tile information */
4739 adv_unit_new_task(punit, AUT_NONE, NULL);
4741 free_unit_orders(punit);
4742 /* If we waited on a tile, reset punit->done_moving */
4743 punit->done_moving = (punit->moves_left <= 0);
4745 /* Make sure that the unit won't keep its old ai_controlled state after
4746 * it has recieved new orders from the client. */
4747 punit->ai_controlled = FALSE;
4749 if (length == 0) {
4750 fc_assert(!unit_has_orders(punit));
4751 send_unit_info(NULL, punit);
4752 return;
4755 punit->has_orders = TRUE;
4756 punit->orders.length = length;
4757 punit->orders.index = 0;
4758 punit->orders.repeat = packet->repeat;
4759 punit->orders.vigilant = packet->vigilant;
4760 punit->orders.list
4761 = fc_malloc(length * sizeof(*(punit->orders.list)));
4762 for (i = 0; i < length; i++) {
4763 punit->orders.list[i].order = packet->orders[i];
4764 punit->orders.list[i].dir = packet->dir[i];
4765 punit->orders.list[i].activity = packet->activity[i];
4766 punit->orders.list[i].target = packet->target[i];
4767 punit->orders.list[i].action = packet->action[i];
4770 if (!packet->repeat) {
4771 punit->goto_tile = index_to_tile(&(wld.map), packet->dest_tile);
4772 } else {
4773 /* Make sure that no old goto_tile remains. */
4774 punit->goto_tile = NULL;
4777 #ifdef FREECIV_DEBUG
4778 log_debug("Orders for unit %d: length:%d", packet->unit_id, length);
4779 for (i = 0; i < length; i++) {
4780 log_debug(" %d,%s", packet->orders[i], dir_get_name(packet->dir[i]));
4782 #endif /* FREECIV_DEBUG */
4784 if (!is_player_phase(unit_owner(punit), game.info.phase)
4785 || execute_orders(punit, TRUE)) {
4786 /* Looks like the unit survived. */
4787 send_unit_info(NULL, punit);
4791 /**************************************************************************
4792 Handle worker task assigned to the city
4793 **************************************************************************/
4794 void handle_worker_task(struct player *pplayer,
4795 const struct packet_worker_task *packet)
4797 struct city *pcity = game_city_by_number(packet->city_id);
4798 struct worker_task *ptask = NULL;
4799 struct tile *ptile = index_to_tile(&(wld.map), packet->tile_id);
4801 if (pcity == NULL || pcity->owner != pplayer || ptile == NULL) {
4802 return;
4805 worker_task_list_iterate(pcity->task_reqs, ptask_old) {
4806 if (tile_index(ptask_old->ptile) == packet->tile_id) {
4807 ptask = ptask_old;
4809 } worker_task_list_iterate_end;
4811 if (ptask == NULL) {
4812 if (packet->activity == ACTIVITY_LAST) {
4813 return;
4816 ptask = fc_malloc(sizeof(struct worker_task));
4817 worker_task_init(ptask);
4818 worker_task_list_append(pcity->task_reqs, ptask);
4819 } else {
4820 if (packet->activity == ACTIVITY_LAST) {
4821 worker_task_list_remove(pcity->task_reqs, ptask);
4822 free(ptask);
4823 ptask = NULL;
4827 if (ptask != NULL) {
4828 ptask->ptile = ptile;
4829 ptask->act = packet->activity;
4830 if (packet->tgt >= 0) {
4831 if (packet->tgt < MAX_EXTRA_TYPES) {
4832 ptask->tgt = extra_by_number(packet->tgt);
4833 } else {
4834 log_debug("Illegal worker task target %d", packet->tgt);
4835 ptask->tgt = NULL;
4837 } else {
4838 ptask->tgt = NULL;
4840 ptask->want = packet->want;
4843 lsend_packet_worker_task(pplayer->connections, packet);