[Freeciv-Dev] (PR#11338) [Patch] can_transport()
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
<URL: http://rt.freeciv.org/Ticket/Display.html?id=11338 >
This introduces can_transport() and can_transport_type() which hide
ugliness of checking if unit(type) can transport other. They are called
from a couple of places. All is_air_units_transport() callers are gone
so it's removed.
Function names are probably not final. What naming convention should
be used here?
This one applies on top of #11336.
- Cazfi
diff -Nurd -X.diff_ignore freeciv/ai/aiferry.c freeciv/ai/aiferry.c
--- freeciv/ai/aiferry.c 2004-12-05 00:48:41.765625000 +0200
+++ freeciv/ai/aiferry.c 2004-12-05 00:49:38.812500000 +0200
@@ -269,11 +269,11 @@
/****************************************************************************
Runs a few checks to determine if "boat" is a free boat that can carry
- "cap" units of the same type as "punit" (the last one isn't implemented).
+ "cap" units of the same type as "punit".
****************************************************************************/
static bool is_boat_free(struct unit *boat, struct unit *punit, int cap)
{
- /* - Only ground-unit transporters are consider.
+ /* - Only transporters capable of transporting this unit are eligible.
* - Units with orders are skipped (the AI doesn't control units with
* orders).
* - Only boats that we own are eligible.
@@ -281,7 +281,7 @@
* are eligible.
* - Only boats with enough remaining capacity are eligible.
*/
- return (is_ground_units_transport(boat)
+ return (can_transport(boat, punit)
&& !unit_has_orders(boat)
&& boat->owner == punit->owner
&& (boat->ai.passenger == FERRY_AVAILABLE
diff -Nurd -X.diff_ignore freeciv/common/movetype.c freeciv/common/movetype.c
--- freeciv/common/movetype.c 2004-12-04 23:40:41.359375000 +0200
+++ freeciv/common/movetype.c 2004-12-05 00:49:38.828125000 +0200
@@ -390,3 +390,62 @@
return MR_OK;
}
+
+/**************************************************************************
+ Whether 'transporter' has ability to transport 'transported'
+**************************************************************************/
+bool can_transport(struct unit *transporter, struct unit *transported)
+{
+ return can_transport_type(transporter->type, transported->type);
+}
+
+/**************************************************************************
+ Whether 'transporter' of given type has ability to transport
+ 'transported' type
+**************************************************************************/
+bool can_transport_type(Unit_Type_id transporter, Unit_Type_id transported)
+{
+ if (get_unit_type(transporter)->transport_capacity <= 0)
+ {
+ return FALSE;
+ }
+
+ if (get_unit_type(transported)->move_type == LAND_MOVING)
+ {
+ if ((unit_type_flag(transporter, F_CARRIER)
+ || unit_type_flag(transporter, F_MISSILE_CARRIER)))
+ {
+ return FALSE;
+ }
+ return TRUE;
+ }
+
+ if (!unit_type_flag(transported, F_MISSILE)
+ && unit_type_flag(transporter, F_MISSILE_CARRIER))
+ {
+ return FALSE;
+ }
+
+ if (unit_type_flag(transported, F_MISSILE))
+ {
+ if (!unit_type_flag(transporter, F_MISSILE_CARRIER)
+ && !unit_type_flag(transporter, F_CARRIER))
+ {
+ return FALSE;
+ }
+ }
+ else if ((get_unit_type(transported)->move_type == AIR_MOVING
+ || get_unit_type(transported)->move_type == HELI_MOVING)
+ && !unit_type_flag(transporter, F_CARRIER))
+ {
+ return FALSE;
+ }
+
+ /* No unit can transport sea units */
+ if (get_unit_type(transported)->move_type == SEA_MOVING)
+ {
+ return FALSE;
+ }
+
+ return TRUE;
+}
diff -Nurd -X.diff_ignore freeciv/common/movetype.h freeciv/common/movetype.h
--- freeciv/common/movetype.h 2004-12-04 23:40:41.359375000 +0200
+++ freeciv/common/movetype.h 2004-12-05 00:49:38.843750000 +0200
@@ -46,5 +46,7 @@
const struct tile *src_tile,
const struct tile *dst_tile,
bool igzoc);
+bool can_transport(struct unit *transporter, struct unit *transported);
+bool can_transport_type(Unit_Type_id transporter, Unit_Type_id transported);
#endif /* FC__MOVEMENT_H */
diff -Nurd -X.diff_ignore freeciv/common/unit.c freeciv/common/unit.c
--- freeciv/common/unit.c 2004-12-04 23:40:41.375000000 +0200
+++ freeciv/common/unit.c 2004-12-05 00:49:38.859375000 +0200
@@ -248,16 +248,6 @@
}
/**************************************************************************
-...
-**************************************************************************/
-bool is_air_units_transport(struct unit *punit)
-{
- return (get_transporter_capacity(punit) > 0
- && (unit_flag(punit, F_MISSILE_CARRIER)
- || unit_flag(punit, F_CARRIER)));
-}
-
-/**************************************************************************
Is the unit capable of attacking?
**************************************************************************/
bool is_attack_unit(struct unit *punit)
@@ -569,19 +559,7 @@
}
/* Make sure this transporter can carry this type of unit. */
- if (is_ground_unit(pcargo)) {
- if (!is_ground_units_transport(ptrans)) {
- return FALSE;
- }
- } else if (unit_flag(pcargo, F_MISSILE)) {
- if (!is_air_units_transport(ptrans)) {
- return FALSE;
- }
- } else if (is_air_unit(pcargo) || is_heli_unit(pcargo)) {
- if (!unit_flag(ptrans, F_CARRIER)) {
- return FALSE;
- }
- } else {
+ if (!can_transport(ptrans, pcargo)) {
return FALSE;
}
diff -Nurd -X.diff_ignore freeciv/common/unit.h freeciv/common/unit.h
--- freeciv/common/unit.h 2004-12-04 23:40:41.375000000 +0200
+++ freeciv/common/unit.h 2004-12-05 00:49:38.859375000 +0200
@@ -275,7 +275,6 @@
struct player *pplayer);
int get_transporter_capacity(struct unit *punit);
bool is_ground_units_transport(struct unit *punit);
-bool is_air_units_transport(struct unit *punit);
int missile_carrier_capacity(const struct tile *ptile,
struct player *pplayer,
bool count_units_with_extra_fuel);
diff -Nurd -X.diff_ignore freeciv/server/sanitycheck.c
freeciv/server/sanitycheck.c
--- freeciv/server/sanitycheck.c 2004-12-04 23:40:41.500000000 +0200
+++ freeciv/server/sanitycheck.c 2004-12-05 00:49:38.859375000 +0200
@@ -354,18 +354,9 @@
}
/* Check for ground units in the ocean. */
- if (!pcity
- && is_ocean(map_get_terrain(ptile))
- && is_ground_unit(punit)) {
- SANITY_CHECK(punit->transported_by != -1);
- SANITY_CHECK(!is_ground_unit(transporter));
- SANITY_CHECK(is_ground_units_transport(transporter));
- } else if (!pcity
- && !is_ocean(map_get_terrain(ptile))
- && is_sailing_unit(punit)) {
+ if (!can_unit_exist_at_tile(punit, ptile)) {
SANITY_CHECK(punit->transported_by != -1);
- SANITY_CHECK(!is_sailing_unit(transporter));
- SANITY_CHECK(FALSE); /*
SANITY_CHECK(is_sailing_units_transport(transporter)); */
+ SANITY_CHECK(can_transport(transporter, punit));
}
/* Check for over-full transports. */
diff -Nurd -X.diff_ignore freeciv/server/unittools.c freeciv/server/unittools.c
--- freeciv/server/unittools.c 2004-12-04 23:40:41.562500000 +0200
+++ freeciv/server/unittools.c 2004-12-05 00:49:38.875000000 +0200
@@ -1602,7 +1602,7 @@
/* Get rid of excess standard units. */
if (capacity < 0) {
unit_list_iterate_safe(ptile->units, pcargo) {
- if (is_ground_unit(pcargo)
+ if (!can_unit_exist_at_tile(pcargo, ptile)
&& pcargo->transported_by == -1
&& !unit_flag(pcargo, F_UNDISBANDABLE)
&& !unit_flag(pcargo, F_GAMELOSS)) {
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Freeciv-Dev] (PR#11338) [Patch] can_transport(),
Marko Lindqvist <=
|
|