Author: sveinung
Date: Wed Apr  1 23:48:09 2015
New Revision: 28688

URL: http://svn.gna.org/viewcvs/freeciv?rev=28688&view=rev
Log:
Cache unit state legality for each action

If a unit of a certain type in a given unit state could perform any action at
all was already cached. Expand the cache to also track each individual action.

See patch #5975

Modified:
    branches/S2_6/common/unittype.c
    branches/S2_6/common/unittype.h

Modified: branches/S2_6/common/unittype.c
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/common/unittype.c?rev=28688&r1=28687&r2=28688&view=diff
==============================================================================
--- branches/S2_6/common/unittype.c     (original)
+++ branches/S2_6/common/unittype.c     Wed Apr  1 23:48:09 2015
@@ -269,11 +269,14 @@
 /* Fake action representing any hostile action. */
 #define ACTION_HOSTILE ACTION_COUNT + 1
 
+/* Number of real and fake actions. */
+#define ACTION_AND_FAKES ACTION_HOSTILE + 1
+
 /* Cache of what generalized (ruleset defined) action enabler controlled
  * actions units of each type can perform. Checking if any action can be
  * done at all is done via the fake action ACTION_ANY. If any hostile
  * action can be performed is done via ACTION_HOSTILE. */
-static bv_unit_types unit_can_act_cache[ACTION_HOSTILE + 1];
+static bv_unit_types unit_can_act_cache[ACTION_AND_FAKES];
 
 /**************************************************************************
   Cache what generalized (ruleset defined) action enabler controlled
@@ -349,10 +352,10 @@
  * bit USP_COUNT to ((USP_COUNT - 1) * 2): Possible when the corresponding
  * property is FALSE
  */
-BV_DEFINE(bv_unit_state_action_cache, ((USP_COUNT - 1) * 2));
+BV_DEFINE(bv_ustate_act_cache, ((USP_COUNT - 1) * 2));
 
 /* Caches for each unit type */
-static bv_unit_state_action_cache unit_state_action_cache[U_LAST];
+static bv_ustate_act_cache ustate_act_cache[U_LAST][ACTION_AND_FAKES];
 static bv_diplrel_all_reqs dipl_rel_action_cache[U_LAST][ACTION_COUNT];
 
 /**************************************************************************
@@ -366,7 +369,11 @@
 
   /* The unit is not yet known to be allowed to perform any actions no
    * matter what its unit state is. */
-  BV_CLR_ALL(unit_state_action_cache[utype_index(putype)]);
+  action_iterate(action_id) {
+    BV_CLR_ALL(ustate_act_cache[utype_index(putype)][action_id]);
+  } action_iterate_end;
+  BV_CLR_ALL(ustate_act_cache[utype_index(putype)][ACTION_ANY]);
+  BV_CLR_ALL(ustate_act_cache[utype_index(putype)][ACTION_HOSTILE]);
 
   if (!is_actor_unit_type(putype)) {
     /* Not an actor unit. */
@@ -394,7 +401,10 @@
         /* Not required to be absent, so OK if present */
         req.present = FALSE;
         if (!is_req_in_vec(&req, &(enabler->actor_reqs))) {
-          BV_SET(unit_state_action_cache[utype_index(putype)],
+          BV_SET(ustate_act_cache[utype_index(putype)][ACTION_ANY],
+              requirement_unit_state_ereq(req.source.value.unit_state,
+                                         TRUE));
+          BV_SET(ustate_act_cache[utype_index(putype)][enabler->action],
               requirement_unit_state_ereq(req.source.value.unit_state,
                                          TRUE));
         }
@@ -402,7 +412,10 @@
         /* Not required to be present, so OK if absent */
         req.present = TRUE;
         if (!is_req_in_vec(&req, &(enabler->actor_reqs))) {
-          BV_SET(unit_state_action_cache[utype_index(putype)],
+          BV_SET(ustate_act_cache[utype_index(putype)][ACTION_ANY],
+                 requirement_unit_state_ereq(req.source.value.unit_state,
+                                            FALSE));
+          BV_SET(ustate_act_cache[utype_index(putype)][enabler->action],
                  requirement_unit_state_ereq(req.source.value.unit_state,
                                             FALSE));
         }
@@ -504,7 +517,20 @@
                                  const enum ustate_prop prop,
                                  const bool is_there)
 {
-  return BV_ISSET(unit_state_action_cache[utype_index(punit_type)],
+  return utype_can_do_act_when_ustate(punit_type, ACTION_ANY, prop, is_there);
+}
+
+/**************************************************************************
+  Return TRUE iff the unit type can do the specified (action enabler
+  controlled) action while its unit state property prop has the value
+  is_there.
+**************************************************************************/
+bool utype_can_do_act_when_ustate(const struct unit_type *punit_type,
+                                  const int action_id,
+                                  const enum ustate_prop prop,
+                                  const bool is_there)
+{
+  return BV_ISSET(ustate_act_cache[utype_index(punit_type)][action_id],
       requirement_unit_state_ereq(prop, is_there));
 }
 

Modified: branches/S2_6/common/unittype.h
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S2_6/common/unittype.h?rev=28688&r1=28687&r2=28688&view=diff
==============================================================================
--- branches/S2_6/common/unittype.h     (original)
+++ branches/S2_6/common/unittype.h     Wed Apr  1 23:48:09 2015
@@ -535,6 +535,11 @@
 bool can_unit_act_when_ustate_is(const struct unit_type *punit_type,
                                  const enum ustate_prop prop,
                                  const bool is_there);
+bool utype_can_do_act_when_ustate(const struct unit_type *punit_type,
+                                  const int action_id,
+                                  const enum ustate_prop prop,
+                                  const bool is_there);
+
 bool can_utype_do_act_if_tgt_diplrel(const struct unit_type *punit_type,
                                      const int action_id,
                                      const int prop,


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

Reply via email to