Author: sveinung
Date: Thu Dec 25 22:14:40 2014
New Revision: 27408

URL: http://svn.gna.org/viewcvs/freeciv?rev=27408&view=rev
Log:
Add a unit type role for each action after the unit flags and roles.

This makes it possible to request a unit type capable of performing the wanted
action using the already existing unit role system.

See patch #5554

Modified:
    trunk/common/actions.c
    trunk/common/actions.h
    trunk/common/unittype.c
    trunk/common/unittype.h
    trunk/server/ruleset.c

Modified: trunk/common/actions.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/actions.c?rev=27408&r1=27407&r2=27408&view=diff
==============================================================================
--- trunk/common/actions.c      (original)
+++ trunk/common/actions.c      Thu Dec 25 22:14:40 2014
@@ -340,6 +340,20 @@
 }
 
 /**************************************************************************
+  Get the unit type role corresponding to the ability to do action.
+**************************************************************************/
+int action_get_role(int action_id)
+{
+  fc_assert_msg(actions[action_id], "Action %d don't exist.", action_id);
+
+  fc_assert_msg(AAK_UNIT == action_get_actor_kind(action_id),
+                "Action %s isn't performed by a unit",
+                action_get_rule_name(action_id));
+
+  return action_id + L_LAST;
+}
+
+/**************************************************************************
   Create a new action enabler.
 **************************************************************************/
 struct action_enabler *action_enabler_new(void)

Modified: trunk/common/actions.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/actions.h?rev=27408&r1=27407&r2=27408&view=diff
==============================================================================
--- trunk/common/actions.h      (original)
+++ trunk/common/actions.h      Thu Dec 25 22:14:40 2014
@@ -174,6 +174,8 @@
 
 bool action_is_hostile(int action_id);
 
+int action_get_role(int action_id);
+
 const char *action_get_rule_name(int action_id);
 const char *action_get_ui_name(int action_id);
 const char *action_prepare_ui_name(int action_id, const char* mnemonic,

Modified: trunk/common/unittype.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/unittype.c?rev=27408&r1=27407&r2=27408&view=diff
==============================================================================
--- trunk/common/unittype.c     (original)
+++ trunk/common/unittype.c     Thu Dec 25 22:14:40 2014
@@ -37,6 +37,8 @@
 #include "unitlist.h"
 
 #include "unittype.h"
+
+#define MAX_UNIT_ROLES L_LAST + ACTION_COUNT
 
 static struct unit_type unit_types[U_LAST];
 static struct unit_class unit_classes[UCL_LAST];
@@ -317,6 +319,16 @@
 }
 
 /**************************************************************************
+  Return TRUE iff the unit type can perform the action corresponding to
+  the unit type role.
+**************************************************************************/
+static bool utype_can_do_action_role(const struct unit_type *putype,
+                                     const int role)
+{
+  return utype_can_do_action(putype, role - L_LAST);
+}
+
+/**************************************************************************
   Return TRUE iff units of this type can do hostile actions controlled by
   generalized (ruleset defined) action enablers.
 **************************************************************************/
@@ -462,6 +474,17 @@
   unit_can_act_cache_set(ptype);
   unit_state_action_cache_set(ptype);
   local_dipl_rel_action_cache_set(ptype);
+}
+
+/**************************************************************************
+  Cache what unit types may be allowed do what actions, both at all and
+  when certain properties are true.
+**************************************************************************/
+void unit_type_action_cache_init(void)
+{
+  unit_type_iterate(u) {
+    unit_type_action_cache_set(u);
+  } unit_type_iterate_end;
 }
 
 /**************************************************************************
@@ -999,8 +1022,8 @@
 Unit order is in terms of the order in the units ruleset.
 **************************************************************************/
 static bool first_init = TRUE;
-static int n_with_role[L_LAST];
-static struct unit_type **with_role[L_LAST];
+static int n_with_role[MAX_UNIT_ROLES];
+static struct unit_type **with_role[MAX_UNIT_ROLES];
 
 /**************************************************************************
 Do the real work for role_unit_precalcs, for one role (or flag), given by i.
@@ -1037,7 +1060,7 @@
 {
   int i;
 
-  for (i = 0; i < L_LAST; i++) {
+  for (i = 0; i < MAX_UNIT_ROLES; i++) {
     free(with_role[i]);
     with_role[i] = NULL;
     n_with_role[i] = 0;
@@ -1053,7 +1076,7 @@
   int i;
 
   if (first_init) {
-    for (i = 0; i < L_LAST; i++) {
+    for (i = 0; i < MAX_UNIT_ROLES; i++) {
       with_role[i] = NULL;
       n_with_role[i] = 0;
     }
@@ -1067,6 +1090,9 @@
   for (i = L_FIRST; i < L_LAST; i++) {
     precalc_one(i, utype_has_role);
   }
+  for (i = L_LAST; i < MAX_UNIT_ROLES; i++) {
+    precalc_one(i, utype_can_do_action_role);
+  }
   first_init = FALSE;
 }
 
@@ -1076,7 +1102,8 @@
 int num_role_units(int role)
 {
   fc_assert_ret_val((role >= 0 && role <= UTYF_LAST_USER_FLAG)
-                    || (role >= L_FIRST && role < L_LAST), -1);
+                    || (role >= L_FIRST && role < L_LAST)
+                    || (role >= L_LAST && role < MAX_UNIT_ROLES), -1);
   fc_assert_ret_val(!first_init, -1);
   return n_with_role[role];
 }
@@ -1125,7 +1152,8 @@
 struct unit_type *get_role_unit(int role, int index)
 {
   fc_assert_ret_val((role >= 0 && role <= UTYF_LAST_USER_FLAG)
-                    || (role >= L_FIRST && role < L_LAST), NULL);
+                    || (role >= L_FIRST && role < L_LAST)
+                    || (role >= L_LAST && role < MAX_UNIT_ROLES), NULL);
   fc_assert_ret_val(!first_init, NULL);
   if (index==-1) {
     index = n_with_role[role]-1;
@@ -1144,7 +1172,8 @@
   int j;
 
   fc_assert_ret_val((role >= 0 && role <= UTYF_LAST_USER_FLAG)
-                    || (role >= L_FIRST && role < L_LAST), NULL);
+                    || (role >= L_FIRST && role < L_LAST)
+                    || (role >= L_LAST && role < MAX_UNIT_ROLES), NULL);
   fc_assert_ret_val(!first_init, NULL);
 
   for(j=n_with_role[role]-1; j>=0; j--) {
@@ -1170,7 +1199,8 @@
   int j;
 
   fc_assert_ret_val((role >= 0 && role <= UTYF_LAST_USER_FLAG)
-                    || (role >= L_FIRST && role < L_LAST), NULL);
+                    || (role >= L_FIRST && role < L_LAST)
+                    || (role >= L_LAST && role < MAX_UNIT_ROLES), NULL);
   fc_assert_ret_val(!first_init, NULL);
 
   for(j = n_with_role[role]-1; j >= 0; j--) {
@@ -1194,7 +1224,8 @@
   int j;
 
   fc_assert_ret_val((role >= 0 && role <= UTYF_LAST_USER_FLAG)
-                    || (role >= L_FIRST && role < L_LAST), NULL);
+                    || (role >= L_FIRST && role < L_LAST)
+                    || (role >= L_LAST && role < MAX_UNIT_ROLES), NULL);
   fc_assert_ret_val(!first_init, NULL);
 
   for(j = 0; j < n_with_role[role]; j++) {

Modified: trunk/common/unittype.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/unittype.h?rev=27408&r1=27407&r2=27408&view=diff
==============================================================================
--- trunk/common/unittype.h     (original)
+++ trunk/common/unittype.h     Thu Dec 25 22:14:40 2014
@@ -657,6 +657,7 @@
                        void *data);
 
 void unit_type_action_cache_set(struct unit_type *ptype);
+void unit_type_action_cache_init(void);
 
 /* Initialization and iteration */
 void unit_classes_init(void);

Modified: trunk/server/ruleset.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/server/ruleset.c?rev=27408&r1=27407&r2=27408&view=diff
==============================================================================
--- trunk/server/ruleset.c      (original)
+++ trunk/server/ruleset.c      Thu Dec 25 22:14:40 2014
@@ -6556,6 +6556,9 @@
     /* Init nations we just loaded. */
     update_nations_with_startpos();
 
+    /* Needed by role_unit_precalcs(). */
+    unit_type_action_cache_init();
+
     /* Prepare caches we want to sanity check. */
     role_unit_precalcs();
     road_integrators_cache_init();
@@ -6623,7 +6626,6 @@
     } unit_class_iterate_end;
     unit_type_iterate(u) {
       u->unknown_move_cost = utype_unknown_move_cost(u);
-      unit_type_action_cache_set(u);
     } unit_type_iterate_end;
 
     /* Build advisors unit class cache corresponding to loaded rulesets */


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

Reply via email to