Make struct sacrifice_def private, avoid copying.
authorNeil Moore <neil@s-z.org>
Tue, 28 Oct 2014 02:07:01 +0000 (22:07 -0400)
committerNeil Moore <neil@s-z.org>
Tue, 28 Oct 2014 02:24:25 +0000 (22:24 -0400)
crawl-ref/source/ability.cc
crawl-ref/source/godabil.cc
crawl-ref/source/godabil.h
crawl-ref/source/sacrifice-data.h

index 35cfbbb..2f50902 100644 (file)
@@ -3145,7 +3145,7 @@ static spret_type _do_ability(const ability_def& abil, bool fail)
     case ABIL_RU_SACRIFICE_DURABILITY:
     case ABIL_RU_SACRIFICE_HAND:
         fail_check();
-        if (!ru_do_sacrifice(get_sacrifice_def(abil.ability)))
+        if (!ru_do_sacrifice(abil.ability))
             return SPRET_ABORT;
         break;
 
index dd32fc6..4f47a50 100644 (file)
@@ -67,7 +67,6 @@
 #include "prompt.h"
 #include "random.h"
 #include "religion.h"
-#include "sacrifice-data.h"
 #include "skill_menu.h"
 #include "shopping.h"
 #include "shout.h"
 #include "tiledef-main.h"
 #endif
 
+struct sacrifice_def
+{
+    ability_type  sacrifice;        // The ability that executes the sacrifice.
+    mutation_type mutation;         // The mutation that will be inflicted.
+    const char*   sacrifice_text;   // Format: "sacrifice your hand"
+                                    // in case of variable sacrifices or sac
+                                    // hand, this will be extended later
+    const char*   milestone_text;   // Format: "sacrificed <foo>"
+                                    // in case of variable sacrifices this will
+                                    // be extended later
+    int           base_piety;       // The piety that will be gained, modified
+                                    // by the skill points in the skill below.
+    skill_type    sacrifice_skill;  // This skill will be eliminated.
+    const char*   sacrifice_vector; // This is used for sacrifices which give
+                                    // multiple mutations. It is a key into
+                                    // you.props, yielding a list of mutations
+                                    // granted by the sacrifice.
+};
+
+// Load the sac_data array.
+#include "sacrifice-data.h"
+
 static void _zin_saltify(monster* mon);
 
 string zin_recite_text(const int seed, const int prayertype, int step)
@@ -4922,22 +4943,21 @@ bool qazlal_disaster_area()
     return true;
 }
 
-map<ability_type, sacrifice_def> sacrifice_data_map;
+static map<ability_type, const sacrifice_def *> sacrifice_data_map;
 
 void init_sac_index()
 {
     for (unsigned int i = ABIL_FIRST_SACRIFICE; i <= ABIL_FINAL_SACRIFICE; ++i)
     {
         unsigned int sac_index = i - ABIL_FIRST_SACRIFICE;
-        const sacrifice_def sacrifice = sac_data[sac_index];
-        sacrifice_data_map[static_cast<ability_type>(i)] = sacrifice;
+        sacrifice_data_map[static_cast<ability_type>(i)] = &sac_data[sac_index];
     }
 }
 
-const sacrifice_def& get_sacrifice_def(ability_type sac)
+static const sacrifice_def &_get_sacrifice_def(ability_type sac)
 {
     ASSERT_RANGE(sac, ABIL_FIRST_SACRIFICE, ABIL_FINAL_SACRIFICE+1);
-    return *&sacrifice_data_map[sac];
+    return *sacrifice_data_map[sac];
 }
 
 vector<ability_type> get_possible_sacrifices()
@@ -5184,11 +5204,11 @@ static int _piety_for_skill(skill_type skill)
 
 #define AS_MUT(csv) (static_cast<mutation_type>((csv).get_int()))
 
-static int _get_sacrifice_piety(sacrifice_def sac_def)
+static int _get_sacrifice_piety(ability_type sac)
 {
+    const sacrifice_def &sac_def = _get_sacrifice_def(sac);
     int piety_gain = sac_def.base_piety;
     ability_type sacrifice = sac_def.sacrifice;
-    bool variable_sac = false;
     mutation_type mut;
     int num_sacrifices = 0;
 
@@ -5196,7 +5216,6 @@ static int _get_sacrifice_piety(sacrifice_def sac_def)
 
     if (sac_def.sacrifice_vector)
     {
-        variable_sac = true;
         ASSERT(you.props.exists(sac_def.sacrifice_vector));
         CrawlVector &sacrifice_muts =
             you.props[sac_def.sacrifice_vector].get_vector();
@@ -5321,7 +5340,6 @@ void ru_offer_new_sacrifices()
     int number_of_tries = 0;
     int max_tries = 20;
     int max_overpiety = 170;
-    sacrifice_def sac_def;
 
     do
     {
@@ -5332,10 +5350,9 @@ void ru_offer_new_sacrifices()
             number_of_tries = 0;
             max_overpiety += 3;
         }
-        sac_def = get_sacrifice_def(possible_sacrifices[lesser_sacrifice]);
     }
     while (lesser_sacrifice == -1
-        || you.piety + _get_sacrifice_piety(sac_def) > max_overpiety);
+        || you.piety + _get_sacrifice_piety(possible_sacrifices[lesser_sacrifice]) > max_overpiety);
 
     number_of_tries = 0;
     do
@@ -5347,11 +5364,10 @@ void ru_offer_new_sacrifices()
             number_of_tries = 0;
             max_overpiety += 3;
         }
-        sac_def = get_sacrifice_def(possible_sacrifices[sacrifice]);
     }
     while (sacrifice == -1
             || sacrifice == lesser_sacrifice
-            || you.piety + _get_sacrifice_piety(sac_def) > max_overpiety);
+            || you.piety + _get_sacrifice_piety(possible_sacrifices[sacrifice]) > max_overpiety);
 
     number_of_tries = 0;
     do
@@ -5363,12 +5379,11 @@ void ru_offer_new_sacrifices()
             number_of_tries = 0;
             max_overpiety += 3;
         }
-        sac_def = get_sacrifice_def(possible_sacrifices[greater_sacrifice]);
     }
     while (greater_sacrifice == -1
             || greater_sacrifice == lesser_sacrifice
             || greater_sacrifice == sacrifice
-            || you.piety + _get_sacrifice_piety(sac_def) > max_overpiety);
+            || you.piety + _get_sacrifice_piety(possible_sacrifices[greater_sacrifice]) > max_overpiety);
 
     ASSERT(you.props.exists("available_sacrifices"));
     CrawlVector &available_sacrifices
@@ -5422,9 +5437,11 @@ static void _ru_kill_skill(skill_type skill)
     you.stop_train.insert(skill);
 }
 
-static void _extra_sacrifice_code(sacrifice_def sac_def)
+static void _extra_sacrifice_code(ability_type sac)
 {
-    if (sac_def.sacrifice == ABIL_RU_SACRIFICE_HAND) {
+    const sacrifice_def &sac_def = _get_sacrifice_def(sac);
+    if (sac_def.sacrifice == ABIL_RU_SACRIFICE_HAND)
+    {
         equipment_type ring_slot;
 
         if (you.species == SP_OCTOPODE)
@@ -5496,16 +5513,16 @@ static void _extra_sacrifice_code(sacrifice_def sac_def)
     }
 }
 
-bool ru_do_sacrifice(sacrifice_def sac_def)
+bool ru_do_sacrifice(ability_type sac)
 {
+    const sacrifice_def &sac_def = _get_sacrifice_def(sac);
     bool variable_sac;
     mutation_type mut;
     int num_sacrifices;
     string offer_text;
     string mile_text;
     string sac_text;
-    bool is_sac_arcana = (sac_def.sacrifice == ABIL_RU_SACRIFICE_ARCANA)
-        ? true : false;
+    const bool is_sac_arcana = sac == ABIL_RU_SACRIFICE_ARCANA;
     int piety_gain = 0;
 
     // For variable sacrifices, we need to compose the text that will be
@@ -5554,7 +5571,7 @@ bool ru_do_sacrifice(sacrifice_def sac_def)
         mile_text = make_stringf("%s", sac_def.milestone_text);
     }
 
-    piety_gain = _get_sacrifice_piety(sac_def);
+    piety_gain = _get_sacrifice_piety(sac);
 
     // get confirmation that the sacrifice is desired.
     if (!_execute_sacrifice(piety_gain, offer_text.c_str()))
@@ -5594,7 +5611,7 @@ bool ru_do_sacrifice(sacrifice_def sac_def)
     mark_milestone("sacrifice", mile_text.c_str());
 
     // Any special handling that's needed.
-    _extra_sacrifice_code(sac_def);
+    _extra_sacrifice_code(sac);
 
     // Update how many Ru sacrifices you have. This is used to avoid giving the
     // player extra silver damage.
index fc16721..98d512d 100644 (file)
 #define BEOGH_ARM_GIFT_KEY "given beogh armour"
 #define BEOGH_SH_GIFT_KEY "given beogh shield"
 
-struct sacrifice_def
-{
-    ability_type  sacrifice;        // The ability that executes the sacrifice.
-    mutation_type mutation;         // The mutation that will be inflicted.
-    const char*   sacrifice_text;   // Format: "sacrifice your hand"
-                                    // in case of variable sacrifices or sac
-                                    // hand, this will be extended later
-    const char*   milestone_text;   // Format: "sacrificed <foo>"
-                                    // in case of variable sacrifices this will
-                                    // be extended later
-    int           base_piety;       // The piety that will be gained, modified
-                                    // by the skill points in the skill below.
-    skill_type    sacrifice_skill;  // This skill will be eliminated.
-    const char*   sacrifice_vector; // This is used for sacrifices which give
-                                    // multiple mutations. It is a key into
-                                    // you.props, yielding a list of mutations
-                                    // granted by the sacrifice.
-};
-
 struct bolt;
 class stack_iterator;
 
@@ -130,12 +111,11 @@ void qazlal_elemental_force();
 bool qazlal_disaster_area();
 
 void init_sac_index();
-const sacrifice_def& get_sacrifice_def(ability_type sac);
 const skill_type arcane_mutation_to_skill(mutation_type mutation);
 const char* arcane_mutation_to_school_name(mutation_type mutation);
 vector<ability_type> get_possible_sacrifices();
 void ru_offer_new_sacrifices();
-bool ru_do_sacrifice(sacrifice_def sac_data);
+bool ru_do_sacrifice(ability_type sac);
 bool ru_reject_sacrifices();
 void ru_reset_sacrifice_timer(bool clear_timer = false);
 void ru_expire_sacrifices();
index 3a58c0f..6d266fd 100644 (file)
@@ -1,3 +1,4 @@
+// See struct sacrifice_def in godabil.cc
 static const sacrifice_def sac_data[] =
 {