Ticket #1706: 0002-Refactor-messy-monster-energy-calculation.txt

File 0002-Refactor-messy-monster-energy-calculation.txt, 4.3 KB (added by daniel.santos, 8 years ago)
Line 
1From 0a23b78a4b93f4ba35e17de786e830d74d1112cc Mon Sep 17 00:00:00 2001
2From: Daniel Santos <daniel.santos@pobox.com>
3Date: Thu, 1 Nov 2012 14:41:08 -0500
4Subject: Refactor messy monster energy calculation
5
6There is a lot of copy & paste code which is bad -- it bloats both the
7source code & generated object code.  This chance reduces source bloat,
8as well as generated code, as long as the compiler doesn't decide that
9that adj_energy is a candidate for inlining.  This can be prevented with
10a compiler-specific noinline modifier, but there's currently no support
11for sniffing the compiler & such.
12
13Signed-off-by: Daniel Santos <daniel.santos@pobox.com>
14---
15 src/monster/mon-power.c |   33 ++++++++++++++++++---------------
16 1 file changed, 18 insertions(+), 15 deletions(-)
17
18diff --git a/src/monster/mon-power.c b/src/monster/mon-power.c
19index 2a6b93c..48adc50 100644
20--- a/src/monster/mon-power.c
21+++ b/src/monster/mon-power.c
22@@ -109,6 +109,15 @@ static long eval_blow_effect(int effect, int atk_dam, int rlev)
23        return (atk_dam);
24 }
25 
26+static byte adj_energy(monster_race *r_ptr)
27+{
28+       /* just in case the array changes... */
29+       const unsigned max_ind = sizeof(extract_energy) / sizeof(extract_energy[0]) - 1;
30+       unsigned i = r_ptr->speed + (rsf_has(r_ptr->spell_flags, RSF_HASTE) ? 5 : 0);
31+       /* fastest monster in the game is currently +30, but let's bounds check anyway. */
32+       return extract_energy[i < max_ind ? i : max_ind];
33+}
34+
35 static long eval_max_dam(monster_race *r_ptr)
36 {
37        int rlev, i;
38@@ -195,7 +204,7 @@ static long eval_max_dam(monster_race *r_ptr)
39                                melee_dam *= 10;
40                else
41                {
42-                       melee_dam = melee_dam * 3 + melee_dam * extract_energy[r_ptr->speed + (rsf_has(r_ptr->spell_flags, RSF_HASTE) ? 5 : 0)] / 7;
43+                       melee_dam = melee_dam * 3 + melee_dam * adj_energy(r_ptr) / 7;
44                }
45 
46                /*
47@@ -254,13 +263,13 @@ static long eval_max_dam(monster_race *r_ptr)
48         * Adjust for speed.  Monster at speed 120 will do double damage,
49         * monster at speed 100 will do half, etc.  Bonus for monsters who can haste self.
50         */
51-       dam = (dam * extract_energy[r_ptr->speed + (rsf_has(r_ptr->spell_flags, RSF_HASTE) ? 5 : 0)]) / 10;
52+       dam = (dam * adj_energy(r_ptr)) / 10;
53 
54        /*
55         * Adjust threat for speed -- multipliers are more threatening.
56         */
57        if (rf_has(r_ptr->flags, RF_MULTIPLY))
58-               r_ptr->highest_threat = (r_ptr->highest_threat * extract_energy[r_ptr->speed + (rsf_has(r_ptr->spell_flags, RSF_HASTE) ? 5 : 0)]) / 5;
59+               r_ptr->highest_threat = (r_ptr->highest_threat * adj_energy(r_ptr)) / 5;
60 
61        /*
62         * Adjust threat for friends.
63@@ -514,14 +523,11 @@ for (iteration = 0; iteration < 3; iteration ++) {
64                 */
65                if (rf_has(r_ptr->flags, RF_MULTIPLY)) {
66                        if (flags_test(r_ptr->flags, RF_SIZE, RF_KILL_WALL, RF_PASS_WALL, FLAG_END))
67-                               power[i] = MAX(power[i], power[i] * extract_energy[r_ptr->speed
68-                                       + (rsf_has(r_ptr->spell_flags, RSF_HASTE) ? 5 : 0)]);
69+                               power[i] = MAX(power[i], power[i] * adj_energy(r_ptr));
70                        else if (flags_test(r_ptr->flags, RF_SIZE, RF_OPEN_DOOR, RF_BASH_DOOR, FLAG_END))
71-                               power[i] = MAX(power[i], power[i] *  extract_energy[r_ptr->speed
72-                                       + (rsf_has(r_ptr->spell_flags, RSF_HASTE) ? 5 : 0)] * 3 / 2);
73+                               power[i] = MAX(power[i], power[i] *  adj_energy(r_ptr) * 3 / 2);
74                        else
75-                               power[i] = MAX(power[i], power[i] * extract_energy[r_ptr->speed
76-                                       + (rsf_has(r_ptr->spell_flags, RSF_HASTE) ? 5 : 0)] / 2);
77+                               power[i] = MAX(power[i], power[i] * adj_energy(r_ptr) / 2);
78                }
79 
80                /*
81@@ -566,14 +572,11 @@ for (iteration = 0; iteration < 3; iteration ++) {
82 
83                        if (rf_has(r_ptr->flags, RF_MULTIPLY)) {
84                                if (flags_test(r_ptr->flags, RF_SIZE, RF_KILL_WALL, RF_PASS_WALL, FLAG_END))
85-                                       count = MAX(1, extract_energy[r_ptr->speed
86-                                               + (rsf_has(r_ptr->spell_flags, RSF_HASTE) ? 5 : 0)]) * count;
87+                                       count = MAX(1, adj_energy(r_ptr)) * count;
88                                else if (flags_test(r_ptr->flags, RF_SIZE, RF_OPEN_DOOR, RF_BASH_DOOR, FLAG_END))
89-                                       count = MAX(1, extract_energy[r_ptr->speed
90-                                               + (rsf_has(r_ptr->spell_flags, RSF_HASTE) ? 5 : 0)] * 3 / 2) * count;
91+                                       count = MAX(1, adj_energy(r_ptr) * 3 / 2) * count;
92                                else
93-                                       count = MAX(1, extract_energy[r_ptr->speed
94-                                               + (rsf_has(r_ptr->spell_flags, RSF_HASTE) ? 5 : 0)] / 2) * count;
95+                                       count = MAX(1, adj_energy(r_ptr) / 2) * count;
96                        }
97 
98                        /* Very rare monsters count less towards total monster power on the
99--
1001.7.10.4
101