Complete.Org: Mailing Lists: Archives: freeciv-dev: September 2003:
[Freeciv-Dev] Re: (PR#6179) more rigorous handling of transported_by
Home

[Freeciv-Dev] Re: (PR#6179) more rigorous handling of transported_by

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] Re: (PR#6179) more rigorous handling of transported_by
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 16 Sep 2003 21:33:20 -0700
Reply-to: rt@xxxxxxxxxxxxxx

incoming wrote:

> This patch primarily adds a couple of sanity checks on the 
> transported_by field.

The patch had a typo/bug; this one fixes it.

jason

Index: server/barbarian.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/barbarian.c,v
retrieving revision 1.67
diff -u -r1.67 barbarian.c
--- server/barbarian.c  2003/08/04 15:42:47     1.67
+++ server/barbarian.c  2003/09/17 04:31:48
@@ -383,17 +383,21 @@
     (void) create_unit(barbarians, xu, yu,
                       get_role_unit(L_BARBARIAN_LEADER, 0), FALSE, 0, -1);
   } else {                   /* sea raiders - their units will be veteran */
+    struct unit *punit, *ptrans;
+
     barbarians = create_barbarian_player(FALSE);
     boat = find_a_unit_type(L_BARBARIAN_BOAT,-1);
-    (void) create_unit(barbarians, xu, yu, boat, TRUE, 0, -1);
+    ptrans = create_unit(barbarians, xu, yu, boat, TRUE, 0, -1);
     cap = get_transporter_capacity(unit_list_get(&map_get_tile(xu, yu)->units, 
0));
     for (i = 0; i < cap-1; i++) {
       unit = find_a_unit_type(L_BARBARIAN_SEA,L_BARBARIAN_SEA_TECH);
-      (void) create_unit(barbarians, xu, yu, unit, TRUE, 0, -1);
+      punit = create_unit(barbarians, xu, yu, unit, TRUE, 0, -1);
+      punit->transported_by = ptrans->id;
       freelog(LOG_DEBUG, "Created barbarian unit %s", unit_types[unit].name);
     }
-    (void) create_unit(barbarians, xu, yu,
-                      get_role_unit(L_BARBARIAN_LEADER, 0), FALSE, 0, -1);
+    punit = create_unit(barbarians, xu, yu,
+                       get_role_unit(L_BARBARIAN_LEADER, 0), FALSE, 0, -1);
+    punit->transported_by = ptrans->id;
   }
 
   unit_list_iterate(map_get_tile(x, y)->units, punit2) {
Index: server/sanitycheck.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/sanitycheck.c,v
retrieving revision 1.31
diff -u -r1.31 sanitycheck.c
--- server/sanitycheck.c        2003/08/01 15:58:08     1.31
+++ server/sanitycheck.c        2003/09/17 04:31:48
@@ -22,11 +22,12 @@
 #include "log.h"
 #include "map.h"
 #include "player.h"
+#include "terrain.h"
 #include "unit.h"
 
 #include "maphand.h"
-
 #include "sanitycheck.h"
+#include "unittools.h"
 
 #ifndef NDEBUG
 
@@ -261,6 +262,17 @@
 
       assert(punit->moves_left >= 0);
       assert(punit->hp > 0);
+
+      /* Check for ground units in the ocean. */
+      if (is_ocean(map_get_terrain(punit->x, punit->y))
+         && is_ground_unit(punit)) {
+       assert(punit->transported_by != -1);
+       assert(!is_ground_unit(find_unit_by_id(punit->transported_by)));
+      }
+
+      /* Check for over-full transports. */
+      assert(get_transporter_occupancy(punit)
+            <= get_transporter_capacity(punit));
     } unit_list_iterate_end;
   } players_iterate_end;
 }
Index: server/unittools.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/unittools.c,v
retrieving revision 1.250
diff -u -r1.250 unittools.c
--- server/unittools.c  2003/09/17 02:57:21     1.250
+++ server/unittools.c  2003/09/17 04:31:49
@@ -1404,6 +1404,22 @@
   }
 }
 
+/****************************************************************************
+  Expensive function to check how many units are in the transport.
+****************************************************************************/
+int get_transporter_occupancy(struct unit *ptrans)
+{
+  int occupied = 0;
+
+  unit_list_iterate(map_get_tile(ptrans->x, ptrans->y)->units, pcargo) {
+    if (pcargo->transported_by == ptrans->id) {
+      occupied++;
+    }
+  } unit_list_iterate_end;
+
+  return occupied;
+}
+
 /**************************************************************************
 ...
 **************************************************************************/
@@ -2894,6 +2910,20 @@
   punit->moves_left = MAX(0, punit->moves_left - move_cost);
   unit_list_insert(&pdesttile->units, punit);
   check_unit_activity(punit);
+
+  /* If the unit needs a transporter, put it on one. */
+  if (is_ground_unit(punit)
+      && is_ocean(pdesttile->terrain)) {
+    unit_list_iterate(map_get_tile(punit->x, punit->y)->units, ptrans) {
+      if (is_ground_units_transport(ptrans)
+         && (get_transporter_occupancy(ptrans)
+             < get_transporter_capacity(ptrans))) {
+       punit->transported_by = ptrans->id;
+       break;
+      }
+    } unit_list_iterate_end;
+    assert(punit->transported_by != -1);
+  }
 
   /* set activity to sentry if boarding a ship unless the unit is just 
    * passing through the ship on its way somewhere else.  If the unit is
Index: server/unittools.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/unittools.h,v
retrieving revision 1.54
diff -u -r1.54 unittools.h
--- server/unittools.h  2003/09/09 20:10:28     1.54
+++ server/unittools.h  2003/09/17 04:31:49
@@ -29,6 +29,7 @@
 /* move check related */
 bool is_airunit_refuel_point(int x, int y, struct player *pplayer,
                             Unit_Type_id type, bool unit_is_on_tile);
+int get_transporter_occupancy(struct unit *ptrans);
 
 /* turn update related */
 void player_restore_units(struct player *pplayer);

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] Re: (PR#6179) more rigorous handling of transported_by, Jason Short <=