summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBardur Arantsson <bardur@scientician.net>2012-05-18 06:20:20 +0200
committerBardur Arantsson <bardur@scientician.net>2012-05-18 19:48:30 +0200
commit38ab9bd955e7516cdc0279dcdcf461f975fde3ef (patch)
tree4156f49c3482f99b8b09c5a87f1e5a600aba73fb /src
parentc1f2b8f2d299d22be4ad3f2f176b6e106eeff6dc (diff)
Lua: Move cast_school_spell() to C
Diffstat (limited to 'src')
-rw-r--r--src/cmd5.c2
-rw-r--r--src/externs.h1
-rw-r--r--src/lua_bind.c4
-rw-r--r--src/object.pkg1
-rw-r--r--src/object1.c5
-rw-r--r--src/spells.pkg27
-rw-r--r--src/spells1.c2
-rw-r--r--src/spells3.c6
-rw-r--r--src/spells4.c120
9 files changed, 129 insertions, 39 deletions
diff --git a/src/cmd5.c b/src/cmd5.c
index 5f0201fc..d0c60042 100644
--- a/src/cmd5.c
+++ b/src/cmd5.c
@@ -2409,7 +2409,7 @@ void cast_school_spell()
/* Actualy cast the choice */
if (spell != -1)
{
- exec_lua(format("cast_school_spell(%d, spell(%d))", spell, spell));
+ lua_cast_school_spell(spell, FALSE);
}
}
diff --git a/src/externs.h b/src/externs.h
index d933b2be..e48b99cd 100644
--- a/src/externs.h
+++ b/src/externs.h
@@ -1916,6 +1916,7 @@ int print_book(s16b sval, s32b pval, object_type *obj);
int school_book_length(int sval);
int spell_x(int sval, int pval, int i);
bool_ school_book_contains_spell(int sval, s32b spell_idx);
+void lua_cast_school_spell(s32b spell_idx, bool_ no_cost);
/* randart.c */
extern int get_activation_power(void);
diff --git a/src/lua_bind.c b/src/lua_bind.c
index e7870189..456e653d 100644
--- a/src/lua_bind.c
+++ b/src/lua_bind.c
@@ -249,10 +249,6 @@ int get_mana(s32b s) {
return exec_lua(format("return get_mana(%d)", s));
}
-int get_power(s32b s) {
- return exec_lua(format("return get_power(%d)", s));
-}
-
static int get_spell_stat(s32b s) {
return exec_lua(format("return get_spell_stat(%d)", s));
}
diff --git a/src/object.pkg b/src/object.pkg
index 1ff281c7..4859e35b 100644
--- a/src/object.pkg
+++ b/src/object.pkg
@@ -1132,7 +1132,6 @@ extern object_type *new_object();
extern void end_object(object_type *o_ptr);
extern bool get_item @ get_item_aux(int *cp, cptr pmt, cptr str, int mode);
extern void lua_set_item_tester(int tval, char *fct);
-extern bool is_magestaff();
extern void identify_pack_fully(void);
extern s16b inven_carry(object_type *o_ptr, bool final);
extern s32b calc_total_weight(void);
diff --git a/src/object1.c b/src/object1.c
index 3b418651..2be0984c 100644
--- a/src/object1.c
+++ b/src/object1.c
@@ -2828,6 +2828,8 @@ void display_ammo_damage(object_type *o_ptr)
*/
void describe_device(object_type *o_ptr)
{
+ char buf[128];
+
/* Wands/... of shcool spell */
if (((o_ptr->tval == TV_WAND) || (o_ptr->tval == TV_STAFF)) && object_known_p(o_ptr))
{
@@ -2844,7 +2846,8 @@ void describe_device(object_type *o_ptr)
text_out_c(TERM_L_BLUE, format("%d", school_spells[o_ptr->pval2].skill_level));
text_out("\nSpell fail: ");
- text_out_c(TERM_GREEN, string_exec_lua(format("return tostring(spell_chance(%d))", o_ptr->pval2)));
+ sprintf(buf, FMTs32b, spell_chance(o_ptr->pval2));
+ text_out_c(TERM_GREEN, buf);
text_out("\nSpell info: ");
text_out_c(TERM_YELLOW, get_spell_info(o_ptr->pval2));
diff --git a/src/spells.pkg b/src/spells.pkg
index 18620ccd..c259f672 100644
--- a/src/spells.pkg
+++ b/src/spells.pkg
@@ -2297,36 +2297,10 @@ extern s32b lua_get_level(s32b s, s32b lvl, s32b max, s32b min, s32b bonus);
/** Get level of device */
extern s32b get_level_device(s32b s, s32b max, s32b min);
-/** Get change of failure for spell. */
-extern s32b spell_chance(s32b s);
-
/** Get level of spell */
extern s32b get_level(s32b s, s32b max, s32b min);
-/** @fn lua_spell_chance(s32b chance, int level, int skill_level, int mana, int cur_mana, int stat)
- * @dgonly
- * @brief Get the chance a spell will fail.\n
- * @param chance Number \n chance is the inital chance a spell will work.
- * @brief Initial chance
- * @param level Number \n level represents the level of player skill.
- * @brief Player skill level
- * @param skill_level Number \n *unused*.
- * @brief *Unused*
- * @param mana Number \n mana is the mana required by the spell.
- * @brief Spell mana
- * @param cur_mana Number \n cur_mana is the player's current mana.
- * @brief Player mana
- * @param stat Number \n stat is the required casting statistic (INT or WIS).
- * @brief Casting statistic
- * @return Number \n Chance of failure.
- * @note
- * Note: do not call this function directly.\n
- * By order of DG.
- * @note (see file lua_bind.c)
- */
-extern s32b lua_spell_chance(s32b chance, int level, int skill_level, int mana, int cur_mana, int stat);
-
/** @fn lua_spell_device_chance(s32b chance, int level, int base_level)
* @dgonly
* @brief Get the chance a device will fail.\n
@@ -2908,3 +2882,4 @@ void init_school_books();
extern s32b SCHOOL_UDUN;
extern s32b SCHOOL_MELKOR;
+void lua_cast_school_spell @ cast_school_spell(s32b spell_idx, bool no_cost = FALSE);
diff --git a/src/spells1.c b/src/spells1.c
index 477dfdb9..ccd9f489 100644
--- a/src/spells1.c
+++ b/src/spells1.c
@@ -1259,7 +1259,7 @@ void spellbinder_trigger()
for (i = 0; i < p_ptr->spellbinder_num; i++)
{
msg_format("Triggering spell %s.", school_spells[p_ptr->spellbinder[i]].name);
- exec_lua(format("cast_school_spell(%d, spell(%d), TRUE)", p_ptr->spellbinder[i], p_ptr->spellbinder[i]));
+ lua_cast_school_spell(p_ptr->spellbinder[i], TRUE);
}
p_ptr->spellbinder_num = 0;
p_ptr->spellbinder_trigger = 0;
diff --git a/src/spells3.c b/src/spells3.c
index b7019e0f..f83ab513 100644
--- a/src/spells3.c
+++ b/src/spells3.c
@@ -2527,11 +2527,7 @@ void meta_inertia_control_timer_callback()
else if ((p_ptr->inertia_controlled_spell != -1) &&
(!p_ptr->wild_mode))
{
- char buf[128];
- sprintf(buf,
- "__spell_spell[" FMTs32b "]()",
- p_ptr->inertia_controlled_spell);
- exec_lua(buf);
+ lua_cast_school_spell(p_ptr->inertia_controlled_spell, TRUE);
}
}
diff --git a/src/spells4.c b/src/spells4.c
index 0429e1c1..aa62d827 100644
--- a/src/spells4.c
+++ b/src/spells4.c
@@ -35,6 +35,31 @@ static bool_ castable_while_confused(int s)
return exec_lua(buf);
}
+/** Describe what type of energy the spell uses for casting */
+cptr get_power_name(s32b s)
+{
+ return uses_piety_to_cast(s) ? "piety" : "mana";
+}
+
+/* Changes the amount of power(mana, piety, whatever) for the spell */
+void adjust_power(s32b s, s32b amount)
+{
+ if (uses_piety_to_cast(s))
+ {
+ inc_piety(GOD_ALL, amount);
+ }
+ else
+ {
+ increase_mana(amount);
+ }
+}
+
+/* Return the amount of power available for casting spell */
+s32b get_power(s32b s)
+{
+ return uses_piety_to_cast(s) ? p_ptr->grace : p_ptr->csp;
+}
+
/* Output the describtion when it is used as a spell */
void print_spell_desc(int s, int y)
{
@@ -483,3 +508,98 @@ int print_book(s16b sval, s32b pval, object_type *obj)
prt(format(" %-20s%-16s Level Cost Fail Info", "Name", "School"), 1, 0);
return y;
}
+
+static bool_ call_spell_function(s32b s)
+{
+ char buf[128];
+ sprintf(buf, "return (__spell_spell[" FMTs32b "]() ~= nil)", s);
+ return exec_lua(buf);
+}
+
+void lua_cast_school_spell(s32b s, bool_ no_cost)
+{
+ bool_ use = FALSE;
+
+ /* No magic? */
+ if (p_ptr->antimagic > 0)
+ {
+ msg_print("Your anti-magic field disrupts any magic attempts.");
+ return;
+ }
+
+ /* No magic? */
+ if (p_ptr->anti_magic)
+ {
+ msg_print("Your anti-magic shell disrupts any magic attempts.");
+ return;
+ }
+
+ /* if it costs something then some condition must be met */
+ if (!no_cost)
+ {
+ /* Require lite */
+ if (!castable_while_blind(s) && ((p_ptr->blind > 0) || no_lite()))
+ {
+ msg_print("You cannot see!");
+ return;
+ }
+
+ /* Not when confused */
+ if (!castable_while_confused(s) && (p_ptr->confused > 0))
+ {
+ msg_print("You are too confused!");
+ return;
+ }
+
+ /* Enough mana */
+ if (get_mana(s) > get_power(s))
+ {
+ char buf[128];
+ sprintf(buf,
+ "You do not have enough %s, do you want to try anyway?",
+ get_power_name(s));
+
+ if (!get_check(buf))
+ {
+ return;
+ }
+ }
+
+ /* Invoke the spell effect */
+ if (!magik(spell_chance(s)))
+ {
+ use = call_spell_function(s);
+ }
+ else
+ {
+ use = TRUE;
+
+ /* failures are dangerous; we'll flush the input buffer
+ so it isn't missed. */
+ if (flush_failure)
+ {
+ flush();
+ }
+
+ msg_print("You failed to get the spell off!");
+ }
+ }
+ else
+ {
+ call_spell_function(s);
+ }
+
+ /* Use the mana/piety */
+ if (use == TRUE)
+ {
+ /* Reduce mana */
+ adjust_power(s, -get_mana(s));
+
+ /* Take a turn */
+ energy_use = is_magestaff() ? 80 : 100;
+ }
+
+ /* Refresh player */
+ p_ptr->redraw |= PR_MANA;
+ p_ptr->window |= PW_PLAYER;
+}