Author: sveinung
Date: Fri Nov  4 13:41:59 2016
New Revision: 34387

URL: http://svn.gna.org/viewcvs/freeciv?rev=34387&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:
    branches/S2_6/server/unittools.c

Modified: branches/S2_6/server/unittools.c
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/server/unittools.c?rev=34387&r1=34386&r2=34387&view=diff
==============================================================================
--- branches/S2_6/server/unittools.c    (original)
+++ branches/S2_6/server/unittools.c    Fri Nov  4 13:41:59 2016
@@ -2937,13 +2937,15 @@
   send_unit_info(NULL, ptrans);
 }
 
-/*****************************************************************
-  This function is passed to unit_list_sort() to sort a list of
-  units according to their win chance against autoattack_x|y.
-  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 unit_list_sort() to sort a list of units
+  according to their win chance against 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 unit *const *p1,
                          const struct unit *const *q1)
 {
@@ -2952,7 +2954,14 @@
   int p1uwc = unit_win_chance(*p1, p1def);
   int q1uwc = unit_win_chance(*q1, q1def);
 
-  if (p1uwc < q1uwc || unit_transport_get(*q1)) {
+  /* Sort by transport depth first. This makes sure that no transport
+   * attacks before its cargo does. */
+  if (unit_transport_depth(*p1) != unit_transport_depth(*q1)) {
+    /* The units have a different number of recursive transporters. */
+    return unit_transport_depth(*q1) - unit_transport_depth(*p1);
+  }
+
+  if (p1uwc < q1uwc) {
     return -1; /* q is better */
   } else if (p1uwc == q1uwc) {
     return 0;


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

Reply via email to