Author: sveinung
Date: Fri Nov  4 13:10:26 2016
New Revision: 34384

URL: http://svn.gna.org/viewcvs/freeciv?rev=34384&view=rev
Log:
autoattack: cargo attacks before transporters.

The function compare_units() tried to make a cargo unit auto attack before
its transporter by sorting the autoattacker list. Fix the function that
compares two units during the sorting so it does:
 * put cargo units *before* transporting units.
 * check the cargo status of *both* units being compared.
 * consider recursive transports.

See bug #25259

Modified:
    trunk/server/unittools.c

Modified: trunk/server/unittools.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/server/unittools.c?rev=34384&r1=34383&r2=34384&view=diff
==============================================================================
--- trunk/server/unittools.c    (original)
+++ trunk/server/unittools.c    Fri Nov  4 13:10:26 2016
@@ -3031,22 +3031,26 @@
   free(prob);
 }
 
-/*****************************************************************
-  This function is passed to autoattack_prob_list_sort() to sort
-  a list of units and action probabilities according to their win
-  chance against the autoattack target.
-
-  If the unit is being transported, then push it to the front of
-  the list, since we wish to leave its transport out of combat
-  if at all possible.
-*****************************************************************/
+/**************************************************************************
+  This function is passed to autoattack_prob_list_sort() to sort a list of
+  units and action probabilities according to their win chance against the
+  autoattack target modified by transportation relationships.
+
+  The reason for making sure that a cargo unit is ahead of it
+  transporter(s) is to leave transports out of combat if at all possible.
+  (The transport could be destroyed during combat.)
+**************************************************************************/
 static int compare_units(const struct autoattack_prob *const *p1,
                          const struct autoattack_prob *const *q1)
 {
+  const struct unit *p1unit = game_unit_by_number((*p1)->unit_id);
   const struct unit *q1unit = game_unit_by_number((*q1)->unit_id);
 
-  if (unit_transport_get(q1unit)) {
-    return -1; /* q is better */
+  /* Sort by transport depth first. This makes sure that no transport
+   * attacks before its cargo does. */
+  if (unit_transport_depth(p1unit) != unit_transport_depth(q1unit)) {
+    /* The units have a different number of recursive transporters. */
+    return unit_transport_depth(q1unit) - unit_transport_depth(p1unit);
   } else {
     /* Assume the worst. */
     return action_prob_cmp_pessimist((*p1)->prob, (*q1)->prob);


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to