Author: sveinung
Date: Wed Dec 10 03:18:51 2014
New Revision: 27225

URL: http://svn.gna.org/viewcvs/freeciv?rev=27225&view=rev
Log:
Make finding out if a unit type can perform hostile actions fast.

In the future it may be a good idea to make a scale in stead of a binary
classification.

Thanks to Marko Lindqvist <cazfi> for pointing out that "hostile" was a much
better term to use here than "violent".

See patch #5494

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

Modified: trunk/common/actions.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/actions.c?rev=27225&r1=27224&r2=27225&view=diff
==============================================================================
--- trunk/common/actions.c      (original)
+++ trunk/common/actions.c      Wed Dec 10 03:18:51 2014
@@ -31,7 +31,8 @@
 
 static struct action *action_new(enum gen_action id,
                                  enum action_target_kind target_kind,
-                                 const char *ui_name);
+                                 const char *ui_name,
+                                 bool hostile);
 
 static bool is_enabler_active(const struct action_enabler *enabler,
                              const struct player *actor_player,
@@ -59,59 +60,73 @@
   /* Hard code the actions */
   actions[ACTION_SPY_POISON] = action_new(ACTION_SPY_POISON, ATK_CITY,
       /* TRANS: _Poison City (3% chance of success). */
-      N_("%sPoison City%s"));
+      N_("%sPoison City%s"),
+      TRUE);
   actions[ACTION_SPY_SABOTAGE_UNIT] =
       action_new(ACTION_SPY_SABOTAGE_UNIT, ATK_UNIT,
                  /* TRANS: _Sabotage Enemy Unit (3% chance of success). */
-                 N_("%sSabotage Enemy Unit%s"));
+                 N_("%sSabotage Enemy Unit%s"),
+                 TRUE);
   actions[ACTION_SPY_BRIBE_UNIT] =
       action_new(ACTION_SPY_BRIBE_UNIT, ATK_UNIT,
                  /* TRANS: _Bribe Enemy Unit (3% chance of success). */
-                 N_("%sBribe Enemy Unit%s"));
+                 N_("%sBribe Enemy Unit%s"),
+                 TRUE);
   actions[ACTION_SPY_SABOTAGE_CITY] =
       action_new(ACTION_SPY_SABOTAGE_CITY, ATK_CITY,
                  /* TRANS: _Sabotage City (3% chance of success). */
-                 N_("%sSabotage City%s"));
+                 N_("%sSabotage City%s"),
+                 TRUE);
   actions[ACTION_SPY_TARGETED_SABOTAGE_CITY] =
       action_new(ACTION_SPY_TARGETED_SABOTAGE_CITY, ATK_CITY,
                  /* TRANS: Industrial _Sabotage (3% chance of success). */
-                 N_("Industrial %sSabotage%s"));
+                 N_("Industrial %sSabotage%s"),
+                 TRUE);
   actions[ACTION_SPY_INCITE_CITY] =
       action_new(ACTION_SPY_INCITE_CITY, ATK_CITY,
                  /* TRANS: Incite a _Revolt (3% chance of success). */
-                 N_("Incite a %sRevolt%s"));
+                 N_("Incite a %sRevolt%s"),
+                 TRUE);
   actions[ACTION_ESTABLISH_EMBASSY] =
       action_new(ACTION_ESTABLISH_EMBASSY, ATK_CITY,
                  /* TRANS: Establish _Embassy (100% chance of success). */
-                 N_("Establish %sEmbassy%s"));
+                 N_("Establish %sEmbassy%s"),
+                 FALSE);
   actions[ACTION_SPY_STEAL_TECH] =
       action_new(ACTION_SPY_STEAL_TECH, ATK_CITY,
                  /* TRANS: Steal _Technology (3% chance of success). */
-                 N_("Steal %sTechnology%s"));
+                 N_("Steal %sTechnology%s"),
+                 TRUE);
   actions[ACTION_SPY_TARGETED_STEAL_TECH] =
       action_new(ACTION_SPY_TARGETED_STEAL_TECH, ATK_CITY,
                  /* TRANS: Indus_trial Espionage (3% chance of success). */
-                 N_("Indus%strial Espionage%s"));
+                 N_("Indus%strial Espionage%s"),
+                 TRUE);
   actions[ACTION_SPY_INVESTIGATE_CITY] =
       action_new(ACTION_SPY_INVESTIGATE_CITY, ATK_CITY,
                  /* TRANS: _Investigate City (100% chance of success). */
-                 N_("%sInvestigate City%s"));
+                 N_("%sInvestigate City%s"),
+                 TRUE);
   actions[ACTION_SPY_STEAL_GOLD] =
       action_new(ACTION_SPY_STEAL_GOLD, ATK_CITY,
                  /* TRANS: Steal _Gold (100% chance of success). */
-                 N_("Steal %sGold%s"));
+                 N_("Steal %sGold%s"),
+                 TRUE);
   actions[ACTION_TRADE_ROUTE] =
       action_new(ACTION_TRADE_ROUTE, ATK_CITY,
                  /* TRANS: Establish Trade _Route (100% chance of success). */
-                 N_("Establish Trade %sRoute%s"));
+                 N_("Establish Trade %sRoute%s"),
+                 FALSE);
   actions[ACTION_MARKETPLACE] =
       action_new(ACTION_MARKETPLACE, ATK_CITY,
                  /* TRANS: Enter _Marketplace (100% chance of success). */
-                 N_("Enter %sMarketplace%s"));
+                 N_("Enter %sMarketplace%s"),
+                 FALSE);
   actions[ACTION_HELP_WONDER] =
       action_new(ACTION_HELP_WONDER, ATK_CITY,
                  /* TRANS: Help build _Wonder (100% chance of success). */
-                 N_("Help build %sWonder%s"));
+                 N_("Help build %sWonder%s"),
+                 FALSE);
 
   /* Initialize the action enabler list */
   action_iterate(act) {
@@ -142,7 +157,8 @@
 **************************************************************************/
 static struct action *action_new(enum gen_action id,
                                  enum action_target_kind target_kind,
-                                 const char *ui_name)
+                                 const char *ui_name,
+                                 bool hostile)
 {
   struct action *action;
 
@@ -152,6 +168,7 @@
   action->actor_kind = AAK_UNIT;
   action->target_kind = target_kind;
   sz_strlcpy(action->ui_name, ui_name);
+  action->hostile = hostile;
 
   return action;
 }
@@ -184,6 +201,16 @@
   fc_assert_msg(actions[action_id], "Action %d don't exist.", action_id);
 
   return actions[action_id]->target_kind;
+}
+
+/**************************************************************************
+  Returns TRUE iff the specified action is hostile.
+**************************************************************************/
+bool action_is_hostile(int action_id)
+{
+  fc_assert_msg(actions[action_id], "Action %d don't exist.", action_id);
+
+  return actions[action_id]->hostile;
 }
 
 /**************************************************************************

Modified: trunk/common/actions.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/actions.h?rev=27225&r1=27224&r2=27225&view=diff
==============================================================================
--- trunk/common/actions.h      (original)
+++ trunk/common/actions.h      Wed Dec 10 03:18:51 2014
@@ -123,6 +123,7 @@
   enum gen_action id;
   enum action_actor_kind actor_kind;
   enum action_target_kind target_kind;
+  bool hostile; /* TODO: Should this be a scale in stead? */
 
   /* The name of the action shown in the UI */
   char ui_name[MAX_LEN_NAME];
@@ -170,6 +171,8 @@
 
 enum action_actor_kind action_get_actor_kind(int action_id);
 enum action_target_kind action_get_target_kind(int action_id);
+
+bool action_is_hostile(int action_id);
 
 const char *action_get_rule_name(int action_id);
 const char *action_get_ui_name(int action_id);

Modified: trunk/common/unittype.c
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/unittype.c?rev=27225&r1=27224&r2=27225&view=diff
==============================================================================
--- trunk/common/unittype.c     (original)
+++ trunk/common/unittype.c     Wed Dec 10 03:18:51 2014
@@ -262,10 +262,14 @@
 /* Fake action representing any action at all. */
 #define ACTION_ANY ACTION_COUNT
 
+/* Fake action representing any hostile action. */
+#define ACTION_HOSTILE ACTION_COUNT + 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. */
-static bv_unit_types unit_can_act_cache[ACTION_ANY + 1];
+ * 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];
 
 /**************************************************************************
   Cache what generalized (ruleset defined) action enabler controlled
@@ -286,6 +290,9 @@
                                            &(enabler->actor_reqs))) {
         BV_SET(unit_can_act_cache[enabler->action], utype_index(putype));
         BV_SET(unit_can_act_cache[ACTION_ANY], utype_index(putype));
+        if (action_is_hostile(enabler->action)) {
+          BV_SET(unit_can_act_cache[ACTION_HOSTILE], utype_index(putype));
+        }
       }
   } action_enablers_iterate_end;
 }
@@ -307,6 +314,15 @@
                          const int action_id)
 {
   return BV_ISSET(unit_can_act_cache[action_id], utype_index(putype));
+}
+
+/**************************************************************************
+  Return TRUE iff units of this type can do hostile actions controlled by
+  generalized (ruleset defined) action enablers.
+**************************************************************************/
+bool utype_acts_hostile(const struct unit_type *putype)
+{
+  return utype_can_do_action(putype, ACTION_HOSTILE);
 }
 
 /* Cache if any action at all may be possible when the actor unit's state

Modified: trunk/common/unittype.h
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/unittype.h?rev=27225&r1=27224&r2=27225&view=diff
==============================================================================
--- trunk/common/unittype.h     (original)
+++ trunk/common/unittype.h     Wed Dec 10 03:18:51 2014
@@ -514,6 +514,7 @@
 bool is_actor_unit_type(const struct unit_type *putype);
 bool utype_can_do_action(const struct unit_type *putype,
                          const int action_id);
+bool utype_acts_hostile(const struct unit_type *putype);
 
 bool can_unit_act_when_ustate_is(const struct unit_type *punit_type,
                                  const enum ustate_prop prop,


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

Reply via email to