Complete.Org: Mailing Lists: Archives: freeciv-dev: April 2005:
[Freeciv-Dev] (PR#12863) cleanup find_a_unit_type
Home

[Freeciv-Dev] (PR#12863) cleanup find_a_unit_type

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#12863) cleanup find_a_unit_type
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 21 Apr 2005 19:40:13 -0700
Reply-to: bugs@xxxxxxxxxxx

<URL: http://bugs.freeciv.org/Ticket/Display.html?id=12863 >

This patch cleans up find_a_unit_type a bit.

1.  It adds types (not just 'int') to the prototype (which was
previously quite cryptic).

2.  It puts the function comment (which was previously useless) into
(what I hope is) english.

3.  It changes the internals so that it uses
can_player_build_unit_direct instead of looking at game.global_advances.
 This will catch things like units that use CanNuke; it also checks
It's a bit slower but this is not performance-intensive code.  This
check could be questionable (for instance with government reqs) but I
think it's better than what we have now.

Note that the check requires that a minimum of 2 players must be able to
build the unit for it to be applicable.  This will break in a game with
just one player.  It would be better to take (game.nplayers + 2) / 3 or
something, except that we shouldn't include barbarians in this check.

-jason

? data/mom
? data/mom.serv
Index: server/unittools.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/unittools.c,v
retrieving revision 1.336
diff -u -r1.336 unittools.c
--- server/unittools.c  13 Apr 2005 18:19:14 -0000      1.336
+++ server/unittools.c  22 Apr 2005 02:33:45 -0000
@@ -94,18 +94,40 @@
 }
 
 /**************************************************************************
-  returns a unit type with a given role, use -1 if you don't want a tech 
-  role. Always try tech role and only if not available, return role unit.
+  Returns a unit type that matches the role_tech or role roles.
+
+  If role_tech is given, then we look at all units with this role
+  whose requirements are met by any player, and return a random one.  This
+  can be used to give a unit to barbarians taken from the set of most
+  advanced units researched by the 'real' players.
+
+  If role_tech is not give (-1) or if there are no matching unit types,
+  then we look at 'role' value and return a random matching unit type.
+
+  It is an error if there are no available units.  This function will
+  always return a valid unit.
 **************************************************************************/
-int find_a_unit_type(int role, int role_tech)
+Unit_Type_id find_a_unit_type(enum unit_role_id role,
+                             enum unit_role_id role_tech)
 {
   int which[U_LAST];
   int i, num=0;
 
   if (role_tech != -1) {
     for(i=0; i<num_role_units(role_tech); i++) {
-      int iunit = get_role_unit(role_tech, i);
-      if (game.global_advances[get_unit_type(iunit)->tech_requirement] >= 2) {
+      Unit_Type_id iunit = get_role_unit(role_tech, i);
+      const int minplayers = 2;
+      int players = 0;
+
+      /* Note, if there's only one player in the game this check will always
+       * fail. */
+      players_iterate(pplayer) {
+       if (!is_barbarian(pplayer)
+           && can_player_build_unit_direct(pplayer, iunit)) {
+         players++;
+       }
+      } players_iterate_end;
+      if (players > minplayers) {
        which[num++] = iunit;
       }
     }
Index: server/unittools.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/unittools.h,v
retrieving revision 1.72
diff -u -r1.72 unittools.h
--- server/unittools.h  24 Mar 2005 17:48:49 -0000      1.72
+++ server/unittools.h  22 Apr 2005 02:33:45 -0000
@@ -20,7 +20,8 @@
 #include "gotohand.h"
 
 /* battle related */
-int find_a_unit_type(int role, int role_tech);
+Unit_Type_id find_a_unit_type(enum unit_role_id role,
+                             enum unit_role_id role_tech);
 bool maybe_make_veteran(struct unit *punit);
 void unit_versus_unit(struct unit *attacker, struct unit *defender,
                      bool bombard);

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#12863) cleanup find_a_unit_type, Jason Short <=