Freeciv21
Develop your civilization from humble roots to a global empire
unittools.h
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 #pragma once
14 
15 #include "fc_types.h"
16 
17 #include "packets.h" // enum unit_info_use
18 #include "unitlist.h"
19 
20 #define SPECENUM_NAME unit_loss_reason
21 #define SPECENUM_VALUE0 ULR_KILLED
22 #define SPECENUM_VALUE0NAME "killed"
23 #define SPECENUM_VALUE1 ULR_RETIRED
24 #define SPECENUM_VALUE1NAME "retired"
25 #define SPECENUM_VALUE2 ULR_DISBANDED
26 #define SPECENUM_VALUE2NAME "disbanded"
27 #define SPECENUM_VALUE3 ULR_BARB_UNLEASH
28 #define SPECENUM_VALUE3NAME "barb_unleash"
29 #define SPECENUM_VALUE4 ULR_CITY_LOST
30 #define SPECENUM_VALUE4NAME "city_lost"
31 #define SPECENUM_VALUE5 ULR_STARVED
32 #define SPECENUM_VALUE5NAME "starved"
33 #define SPECENUM_VALUE6 ULR_SOLD
34 #define SPECENUM_VALUE6NAME "sold"
35 #define SPECENUM_VALUE7 ULR_USED
36 #define SPECENUM_VALUE7NAME "used"
37 #define SPECENUM_VALUE8 ULR_EXECUTED
38 #define SPECENUM_VALUE8NAME "executed"
39 #define SPECENUM_VALUE9 ULR_ELIMINATED
40 #define SPECENUM_VALUE9NAME "eliminated"
41 #define SPECENUM_VALUE10 ULR_EDITOR
42 #define SPECENUM_VALUE10NAME "editor"
43 #define SPECENUM_VALUE11 ULR_NONNATIVE_TERR
44 #define SPECENUM_VALUE11NAME "nonnative_terr"
45 #define SPECENUM_VALUE12 ULR_PLAYER_DIED
46 #define SPECENUM_VALUE12NAME "player_died"
47 #define SPECENUM_VALUE13 ULR_ARMISTICE
48 #define SPECENUM_VALUE13NAME "armistice"
49 #define SPECENUM_VALUE14 ULR_SDI
50 #define SPECENUM_VALUE14NAME "sdi"
51 #define SPECENUM_VALUE15 ULR_DETONATED
52 #define SPECENUM_VALUE15NAME "detonated"
53 #define SPECENUM_VALUE16 ULR_MISSILE
54 #define SPECENUM_VALUE16NAME "missile"
55 #define SPECENUM_VALUE17 ULR_NUKE
56 #define SPECENUM_VALUE17NAME "nuke"
57 #define SPECENUM_VALUE18 ULR_HP_LOSS
58 #define SPECENUM_VALUE18NAME "hp_loss"
59 #define SPECENUM_VALUE19 ULR_FUEL
60 #define SPECENUM_VALUE19NAME "fuel"
61 #define SPECENUM_VALUE20 ULR_STACK_CONFLICT
62 #define SPECENUM_VALUE20NAME "stack_conflict"
63 #define SPECENUM_VALUE21 ULR_BRIBED
64 #define SPECENUM_VALUE21NAME "bribed"
65 #define SPECENUM_VALUE22 ULR_CAPTURED
66 #define SPECENUM_VALUE22NAME "captured"
67 #define SPECENUM_VALUE23 ULR_CAUGHT
68 #define SPECENUM_VALUE23NAME "caught"
69 #define SPECENUM_VALUE24 ULR_TRANSPORT_LOST
70 #define SPECENUM_VALUE24NAME "transport_lost"
71 #include "specenum_gen.h"
72 
73 // battle related
74 struct unit_type *find_a_unit_type(enum unit_role_id role,
75  enum unit_role_id role_tech);
76 bool maybe_make_veteran(struct unit *punit);
77 void notify_unit_experience(struct unit *punit);
78 void unit_versus_unit(struct unit *attacker, struct unit *defender,
79  int *att_hp, int *def_hp);
80 void unit_bombs_unit(struct unit *attacker, struct unit *defender,
81  int *att_hp, int *def_hp);
82 void combat_veterans(struct unit *attacker, struct unit *defender);
83 
84 // turn update related
85 void player_restore_units(struct player *pplayer);
86 void update_unit_activities(struct player *pplayer);
87 void execute_unit_orders(struct player *pplayer);
88 void finalize_unit_phase_beginning(struct player *pplayer);
89 
90 // various
91 void finish_unit_wait(struct unit *punit, int activity_count);
92 void place_partisans(struct tile *pcenter, struct player *powner, int count,
93  int sq_radius);
94 bool teleport_unit_to_city(struct unit *punit, struct city *pcity,
95  int move_cost, bool verbose);
96 void resolve_unit_stacks(struct player *pplayer, struct player *aplayer,
97  bool verbose);
98 struct unit_list *get_units_seen_via_ally(const struct player *pplayer,
99  const struct player *aplayer);
100 void remove_allied_visibility(struct player *pplayer, struct player *aplayer,
101  const struct unit_list *seen_units);
102 void give_allied_visibility(struct player *pplayer, struct player *aplayer);
103 int get_unit_vision_at(struct unit *punit, const struct tile *ptile,
104  enum vision_layer vlayer);
105 void unit_refresh_vision(struct unit *punit);
106 void unit_list_refresh_vision(struct unit_list *punitlist);
107 
109 enum class bounce_reason {
110  generic,
112 };
113 void bounce_unit(struct unit *punit, bool verbose,
115  int max_distance = 2);
116 bool unit_activity_needs_target_from_client(enum unit_activity activity);
117 void unit_assign_specific_activity_target(struct unit *punit,
118  enum unit_activity *activity,
119  struct extra_type **target);
120 void unit_forget_last_activity(struct unit *punit);
121 
122 /* creation/deletion/upgrading */
123 void transform_unit(struct unit *punit, const struct unit_type *to_unit,
124  bool has_to_pay);
125 struct unit *create_unit(struct player *pplayer, struct tile *ptile,
126  const struct unit_type *punittype,
127  int veteran_level, int homecity_id, int moves_left);
128 struct unit *create_unit_full(struct player *pplayer, struct tile *ptile,
129  const struct unit_type *punittype,
130  int veteran_level, int homecity_id,
131  int moves_left, int hp_left,
132  struct unit *ptrans);
133 void wipe_unit(struct unit *punit, enum unit_loss_reason reason,
134  struct player *killer);
135 void kill_unit(struct unit *pkiller, struct unit *punit, bool vet);
136 
137 struct unit *
138 unit_change_owner(struct unit *punit, struct player *pplayer, int homecity,
139  enum unit_loss_reason reason) fc__warn_unused_result;
140 
141 void unit_set_removal_callback(struct unit *punit,
142  void (*callback)(struct unit *punit));
143 void unit_unset_removal_callback(struct unit *punit);
144 
145 // sending to client
146 void package_unit(struct unit *punit, struct packet_unit_info *packet);
147 void package_short_unit(struct unit *punit,
148  struct packet_unit_short_info *packet,
149  enum unit_info_use packet_use, int info_city_id);
150 void send_unit_info(struct conn_list *dest, struct unit *punit);
151 void send_all_known_units(struct conn_list *dest);
152 void unit_goes_out_of_sight(struct player *pplayer, const unit *punit);
153 
154 // doing a unit activity
155 void do_nuclear_explosion(struct player *pplayer, struct tile *ptile);
156 bool do_airline(struct unit *punit, struct city *city2,
157  const struct action *paction);
158 void do_explore(struct unit *punit);
159 bool do_paradrop(struct unit *punit, struct tile *ptile,
160  const struct action *paction);
161 void unit_transport_load_send(struct unit *punit, struct unit *ptrans);
162 void unit_transport_unload_send(struct unit *punit);
163 bool unit_move(struct unit *punit, struct tile *pdesttile, int move_cost,
164  struct unit *embark_to, bool find_embark_target,
165  bool conquer_city_allowed);
166 bool execute_orders(struct unit *punit, const bool fresh);
167 
168 bool unit_can_do_action_now(const struct unit *punit);
169 void unit_did_action(struct unit *punit);
170 
171 bool unit_can_be_retired(struct unit *punit);
172 
173 void unit_activities_cancel_all_illegal(const struct tile *ptile);
174 void unit_activities_cancel_all_illegal_area(const struct tile *ptile);
175 
176 void unit_get_goods(struct unit *punit);
177 
178 bool unit_order_list_is_sane(int length, const struct unit_order *orders);
179 struct unit_order *create_unit_orders(int length,
180  const struct unit_order *orders);
unit_info_use
Definition: packets.h:60
Definition: city.h:291
Definition: player.h:231
Definition: tile.h:42
Definition: unit.h:134
#define fc__warn_unused_result
Definition: support.h:41
bool unit_move(struct unit *punit, struct tile *pdesttile, int move_cost, struct unit *embark_to, bool find_embark_target, bool conquer_city_allowed)
Moves a unit.
Definition: unittools.cpp:3878
void unit_did_action(struct unit *punit)
Mark a unit as having done something at the current time.
Definition: unittools.cpp:4879
void transform_unit(struct unit *punit, const struct unit_type *to_unit, bool has_to_pay)
Really transforms a single unit to another type.
Definition: unittools.cpp:1676
void place_partisans(struct tile *pcenter, struct player *powner, int count, int sq_radius)
Place partisans for powner around pcenter (normally around a city).
Definition: unittools.cpp:1180
bool do_airline(struct unit *punit, struct city *city2, const struct action *paction)
Go by airline, if both cities have an airport and neither has been used this turn the unit will be tr...
Definition: unittools.cpp:3018
bool execute_orders(struct unit *punit, const bool fresh)
Executes a unit's orders stored in punit->orders.
Definition: unittools.cpp:4384
void update_unit_activities(struct player *pplayer)
Iterate through all units and update them.
Definition: unittools.cpp:646
void unit_activities_cancel_all_illegal(const struct tile *ptile)
Cancel all illegal activities done by units at the specified tile.
Definition: unittools.cpp:774
void remove_allied_visibility(struct player *pplayer, struct player *aplayer, const struct unit_list *seen_units)
When two players cancel an alliance, a lot of units that were visible may no longer be visible (this ...
Definition: unittools.cpp:1593
void bounce_unit(struct unit *punit, bool verbose, bounce_reason reason=bounce_reason::generic, int max_distance=2)
Move or remove a unit due to stack conflicts.
Definition: unittools.cpp:1327
struct unit * unit_change_owner(struct unit *punit, struct player *pplayer, int homecity, enum unit_loss_reason reason) fc__warn_unused_result
We don't really change owner of the unit, but create completely new unit as its copy.
Definition: unittools.cpp:2302
void resolve_unit_stacks(struct player *pplayer, struct player *aplayer, bool verbose)
When in civil war or an alliance breaks there will potentially be units from both sides coexisting on...
Definition: unittools.cpp:1537
void unit_set_removal_callback(struct unit *punit, void(*callback)(struct unit *punit))
Set the call back to run when the server removes the unit.
Definition: unittools.cpp:1872
void unit_bombs_unit(struct unit *attacker, struct unit *defender, int *att_hp, int *def_hp)
This is the basic unit versus unit classic bombardment routine.
Definition: unittools.cpp:321
void package_unit(struct unit *punit, struct packet_unit_info *packet)
Package a unit_info packet.
Definition: unittools.cpp:2662
void unit_versus_unit(struct unit *attacker, struct unit *defender, int *att_hp, int *def_hp)
This is the basic unit versus unit combat routine.
Definition: unittools.cpp:268
void finalize_unit_phase_beginning(struct player *pplayer)
Iterate through all units and remember their current activities.
Definition: unittools.cpp:673
void execute_unit_orders(struct player *pplayer)
Iterate through all units and execute their orders.
Definition: unittools.cpp:659
void send_unit_info(struct conn_list *dest, struct unit *punit)
Send the unit to the players who need the info.
Definition: unittools.cpp:2808
bounce_reason
Why do we need to bounce a unit?
Definition: unittools.h:109
@ generic
We just need to do it.
@ terrain_change
We need to do it because of changing terrain.
int get_unit_vision_at(struct unit *punit, const struct tile *ptile, enum vision_layer vlayer)
Return the vision the unit will have at the given tile.
Definition: unittools.cpp:4791
struct unit_order * create_unit_orders(int length, const struct unit_order *orders)
Sanity-check unit order arrays from a packet and create a unit_order array from their contents if val...
Definition: unittools.cpp:5119
void notify_unit_experience(struct unit *punit)
Common notification for all experience levels.
Definition: unittools.cpp:723
void package_short_unit(struct unit *punit, struct packet_unit_short_info *packet, enum unit_info_use packet_use, int info_city_id)
Package a short_unit_info packet.
Definition: unittools.cpp:2750
void give_allied_visibility(struct player *pplayer, struct player *aplayer)
Refresh units visibility of 'aplayer' for 'pplayer' after alliance have been contracted.
Definition: unittools.cpp:1621
void unit_forget_last_activity(struct unit *punit)
Forget the unit's last activity so that it can't be resumed.
Definition: unittools.cpp:1073
struct unit * create_unit(struct player *pplayer, struct tile *ptile, const struct unit_type *punittype, int veteran_level, int homecity_id, int moves_left)
Wrapper of the below.
Definition: unittools.cpp:1762
void finish_unit_wait(struct unit *punit, int activity_count)
Finish activity of a unit that was deferred by unitwaittime.
Definition: unittools.cpp:1063
struct unit_type * find_a_unit_type(enum unit_role_id role, enum unit_role_id role_tech)
Returns a unit type that matches the role_tech or role roles.
Definition: unittools.cpp:159
void combat_veterans(struct unit *attacker, struct unit *defender)
Maybe make either side of combat veteran.
Definition: unittools.cpp:371
void unit_unset_removal_callback(struct unit *punit)
Remove the call back so nothing runs when the server removes the unit.
Definition: unittools.cpp:1886
bool maybe_make_veteran(struct unit *punit)
Unit has a chance to become veteran.
Definition: unittools.cpp:204
void unit_refresh_vision(struct unit *punit)
Refresh the unit's vision.
Definition: unittools.cpp:4818
void do_explore(struct unit *punit)
Autoexplore with unit.
Definition: unittools.cpp:3047
void unit_transport_load_send(struct unit *punit, struct unit *ptrans)
Put the unit onto the transporter, and tell everyone.
Definition: unittools.cpp:3261
bool unit_can_do_action_now(const struct unit *punit)
Used to implement the game rule controlled by the unitwaittime setting.
Definition: unittools.cpp:4844
void wipe_unit(struct unit *punit, enum unit_loss_reason reason, struct player *killer)
Remove the unit, and passengers if it is a carrying any.
Definition: unittools.cpp:2248
void unit_goes_out_of_sight(struct player *pplayer, const unit *punit)
Handle situation where unit goes out of player sight.
Definition: unittools.cpp:2795
bool do_paradrop(struct unit *punit, struct tile *ptile, const struct action *paction)
Returns whether the drop was made or not.
Definition: unittools.cpp:3082
bool unit_order_list_is_sane(int length, const struct unit_order *orders)
Returns TRUE iff the unit order array is sane.
Definition: unittools.cpp:4924
void player_restore_units(struct player *pplayer)
Definition: unittools.cpp:481
void do_nuclear_explosion(struct player *pplayer, struct tile *ptile)
Nuke all the squares in a 3x3 square around the center of the explosion pplayer is the player that ca...
Definition: unittools.cpp:2999
void unit_transport_unload_send(struct unit *punit)
Pull the unit off of the transporter, and tell everyone.
Definition: unittools.cpp:3316
bool unit_can_be_retired(struct unit *punit)
Units (usually barbarian units) may disband spontaneously if they are far from any enemy units or cit...
Definition: unittools.cpp:4906
bool teleport_unit_to_city(struct unit *punit, struct city *pcity, int move_cost, bool verbose)
Teleport punit to city at cost specified.
Definition: unittools.cpp:1212
void unit_assign_specific_activity_target(struct unit *punit, enum unit_activity *activity, struct extra_type **target)
For some activities (currently only pillaging), the precise target can be assigned by the server rath...
Definition: unittools.cpp:1105
void unit_activities_cancel_all_illegal_area(const struct tile *ptile)
Cancel all illegal activities done by units at the specified tile, and surrounding tiles.
Definition: unittools.cpp:801
void unit_get_goods(struct unit *punit)
Set carried goods for unit.
Definition: unittools.cpp:1773
void send_all_known_units(struct conn_list *dest)
For each specified connections, send information about all the units known to that player/conn.
Definition: unittools.cpp:2859
struct unit * create_unit_full(struct player *pplayer, struct tile *ptile, const struct unit_type *punittype, int veteran_level, int homecity_id, int moves_left, int hp_left, struct unit *ptrans)
Creates a unit, and set it's initial values, and put it into the right lists.
Definition: unittools.cpp:1789
void unit_list_refresh_vision(struct unit_list *punitlist)
Refresh the vision of all units in the list - see unit_refresh_vision.
Definition: unittools.cpp:4834
void kill_unit(struct unit *pkiller, struct unit *punit, bool vet)
Called when one unit kills another in combat (this function is only called in one place).
Definition: unittools.cpp:2358
bool unit_activity_needs_target_from_client(enum unit_activity activity)
Return TRUE iff activity requires some sort of target to be specified by the client.
Definition: unittools.cpp:1085
struct unit_list * get_units_seen_via_ally(const struct player *pplayer, const struct player *aplayer)
Returns the list of the units seen by 'pplayer' potentially seen only thanks to an alliance with 'apl...
Definition: unittools.cpp:1552