From 62e385cb769000cfefa052b1ab3e155314e7ac37 Mon Sep 17 00:00:00 2001 From: Bardur Arantsson Date: Thu, 5 Apr 2012 23:42:13 +0200 Subject: Lua: Move HOOK_CALC_BONUS corruption code to C --- lib/core/crpt_aux.lua | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'lib/core/crpt_aux.lua') diff --git a/lib/core/crpt_aux.lua b/lib/core/crpt_aux.lua index 97f8d4b6..c1772c04 100644 --- a/lib/core/crpt_aux.lua +++ b/lib/core/crpt_aux.lua @@ -140,7 +140,6 @@ function add_corruption(c) assert(c.get_text, "No corruption get_text") assert(c.lose_text, "No corruption lose_text") assert(c.desc, "No corruption desc") - assert(c.hooks, "Nothing to do for corruption") if not c.random then c.random = TRUE end if not c.removable then c.removable = TRUE end if not c.allow then c.allow = function() return not nil end end @@ -155,16 +154,18 @@ function add_corruption(c) end local index, h - for index, h in c.hooks do - add_hook_script(index, "__lua__corrupt_callback"..__corruptions_callbacks_max, "__lua__corrupt_callback"..__corruptions_callbacks_max) - setglobal("__lua__corrupt_callback"..__corruptions_callbacks_max, - function (...) - if test_depend_corrupt(%__corruptions_max) == TRUE then - return call(%h, arg) + if type(c.hooks) == "table" then + for index, h in c.hooks do + add_hook_script(index, "__lua__corrupt_callback"..__corruptions_callbacks_max, "__lua__corrupt_callback"..__corruptions_callbacks_max) + setglobal("__lua__corrupt_callback"..__corruptions_callbacks_max, + function (...) + if test_depend_corrupt(%__corruptions_max) == TRUE then + return call(%h, arg) + end end - end - ) - __corruptions_callbacks_max = __corruptions_callbacks_max + 1 + ) + __corruptions_callbacks_max = __corruptions_callbacks_max + 1 + end end if type(c.desc) == "table" then -- cgit v1.2.3 From 3f819c170739b098f9f7951fc9c2ff407f3bbf03 Mon Sep 17 00:00:00 2001 From: Bardur Arantsson Date: Fri, 6 Apr 2012 11:40:03 +0200 Subject: Lua: Moved corruption "can_gain" checks to C --- lib/core/crpt_aux.lua | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'lib/core/crpt_aux.lua') diff --git a/lib/core/crpt_aux.lua b/lib/core/crpt_aux.lua index c1772c04..85cddbf3 100644 --- a/lib/core/crpt_aux.lua +++ b/lib/core/crpt_aux.lua @@ -55,11 +55,7 @@ function test_depend_corrupt(corrupt, can_gain) end -- are we even allowed to get it? - if __corruptions[corrupt].can_gain and (not __corruptions[corrupt].can_gain()) then - return FALSE - end - - return TRUE + return player_can_gain_corruption(corrupt) end -- Gain a new corruption -- cgit v1.2.3 From 818696e7d00a0f504f9e147ee691164ae391e0bb Mon Sep 17 00:00:00 2001 From: Bardur Arantsson Date: Fri, 6 Apr 2012 18:31:10 +0200 Subject: Lua: Remove dead Lua code dealing with corruption hooks --- lib/core/crpt_aux.lua | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) (limited to 'lib/core/crpt_aux.lua') diff --git a/lib/core/crpt_aux.lua b/lib/core/crpt_aux.lua index 85cddbf3..7703a815 100644 --- a/lib/core/crpt_aux.lua +++ b/lib/core/crpt_aux.lua @@ -149,23 +149,9 @@ function add_corruption(c) __corruptions[o].oppose[__corruptions_max] = TRUE end - local index, h - if type(c.hooks) == "table" then - for index, h in c.hooks do - add_hook_script(index, "__lua__corrupt_callback"..__corruptions_callbacks_max, "__lua__corrupt_callback"..__corruptions_callbacks_max) - setglobal("__lua__corrupt_callback"..__corruptions_callbacks_max, - function (...) - if test_depend_corrupt(%__corruptions_max) == TRUE then - return call(%h, arg) - end - end - ) - __corruptions_callbacks_max = __corruptions_callbacks_max + 1 - end - end - if type(c.desc) == "table" then local new_desc = "" + local index, h for index, h in c.desc do new_desc = new_desc..h.."\n" end -- cgit v1.2.3 From 1de4221b9e906ce8324237943165c161f78b5e07 Mon Sep 17 00:00:00 2001 From: Bardur Arantsson Date: Fri, 6 Apr 2012 18:34:59 +0200 Subject: Lua: Remove unused lose_all_corruptions() --- lib/core/crpt_aux.lua | 9 --------- 1 file changed, 9 deletions(-) (limited to 'lib/core/crpt_aux.lua') diff --git a/lib/core/crpt_aux.lua b/lib/core/crpt_aux.lua index 7703a815..369cf5d1 100644 --- a/lib/core/crpt_aux.lua +++ b/lib/core/crpt_aux.lua @@ -120,15 +120,6 @@ function lose_corruption() end end --- Lose all corruptions (for e.g. Potion of New Life) -function lose_all_corruptions() - local i; - for i = 0, __corruptions_max - 1 do - lose_corruption() - end - return -1 -end - -- Creates a new corruption function add_corruption(c) assert(c.color, "No corruption color") -- cgit v1.2.3 From 41b6617e992d3fedda4c0a93ddd0fa4ad834a2ae Mon Sep 17 00:00:00 2001 From: Bardur Arantsson Date: Fri, 6 Apr 2012 19:24:21 +0200 Subject: Lua: Refactor the "set" part of player.corruption() to C --- lib/core/crpt_aux.lua | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) (limited to 'lib/core/crpt_aux.lua') diff --git a/lib/core/crpt_aux.lua b/lib/core/crpt_aux.lua index 369cf5d1..24b71860 100644 --- a/lib/core/crpt_aux.lua +++ b/lib/core/crpt_aux.lua @@ -5,20 +5,8 @@ __corruptions_max = 0 __corruptions_callbacks_max = 0 -- Get the corruption -function player.corruption(c, set) - if set then - player.corruptions_aux[c + 1] = set - player.redraw = bor(player.redraw, PR_BASIC) - player.update = bor(player.update, PU_BONUS, PU_TORCH, PU_BODY, PU_POWERS) - if (set == TRUE) and (__corruptions[c].gain) then - __corruptions[c].gain() - end - if (set == FALSE) and (__corruptions[c].lose) then - __corruptions[c].lose() - end - else - return player.corruptions_aux[c + 1] - end +function player.corruption(c) + return player.corruptions_aux[c + 1] end -- Test if we have that corruption @@ -76,7 +64,7 @@ function gain_corruption(group) if (max > 0) then local ret = rand_int(max) - player.corruption(pos[ret], TRUE) + player_gain_corruption(pos[ret]) cmsg_print(TERM_L_RED, __corruptions[pos[ret]].get_text) return pos[ret] @@ -103,13 +91,13 @@ function lose_corruption() if (max > 0) then local ret = rand_int(max) - player.corruption(pos[ret], FALSE) + player_lose_corruption(pos[ret]) cmsg_print(TERM_L_RED, __corruptions[pos[ret]].lose_text) -- Ok now lets see if it broke some dependancies for i = 0, max - 1 do if player.corruption(pos[i]) ~= test_depend_corrupt(pos[i]) then - player.corruption(pos[i], FALSE) + player_lose_corruption(pos[i]) cmsg_print(TERM_L_RED, __corruptions[pos[i]].lose_text) end end -- cgit v1.2.3 From a01f15e08da3b2e0e326b2a94100e17962136741 Mon Sep 17 00:00:00 2001 From: Bardur Arantsson Date: Sat, 7 Apr 2012 14:50:36 +0200 Subject: Lua: Refactor all the corruption code into C --- lib/core/crpt_aux.lua | 144 -------------------------------------------------- 1 file changed, 144 deletions(-) delete mode 100644 lib/core/crpt_aux.lua (limited to 'lib/core/crpt_aux.lua') diff --git a/lib/core/crpt_aux.lua b/lib/core/crpt_aux.lua deleted file mode 100644 index 24b71860..00000000 --- a/lib/core/crpt_aux.lua +++ /dev/null @@ -1,144 +0,0 @@ --- Core functions for corruptions - -__corruptions = {} -__corruptions_max = 0 -__corruptions_callbacks_max = 0 - --- Get the corruption -function player.corruption(c) - return player.corruptions_aux[c + 1] -end - --- Test if we have that corruption --- We must: --- 1) have it or be willing to get it --- 2) have all its dependancies --- 3) have none of its opposing corruptions --- 4) pass the possible tests -function test_depend_corrupt(corrupt, can_gain) - local i, c - - if not can_gain then can_gain = FALSE end - - if can_gain == TRUE then - if (player.corruption(corrupt) ~= FALSE) then - return FALSE - end - else - if (player.corruption(corrupt) ~= TRUE) then - return FALSE - end - end - - for c, i in __corruptions[corrupt].depends do - if test_depend_corrupt(c) ~= TRUE then - return FALSE - end - end - - for c, i in __corruptions[corrupt].oppose do - if test_depend_corrupt(c) ~= FALSE then - return FALSE - end - end - - -- are we even allowed to get it? - return player_can_gain_corruption(corrupt) -end - --- Gain a new corruption -function gain_corruption(group) - local i, max - local pos = {} - - -- Get the list of all possible ones - max = 0 - for i = 0, __corruptions_max - 1 do - if __corruptions[i].group == group and test_depend_corrupt(i, TRUE) == TRUE and __corruptions[i].random == TRUE and __corruptions[i].allow() then - pos[max] = i - max = max + 1 - end - end - - -- Ok now get one of them - if (max > 0) then - local ret = rand_int(max) - - player_gain_corruption(pos[ret]) - cmsg_print(TERM_L_RED, __corruptions[pos[ret]].get_text) - - return pos[ret] - else - return -1 - end -end - --- Lose an existing corruption -function lose_corruption() - local i, max - local pos = {} - - -- Get the list of all possible ones - max = 0 - for i = 0, __corruptions_max - 1 do - if test_depend_corrupt(i) == TRUE and __corruptions[i].removable == TRUE then - pos[max] = i - max = max + 1 - end - end - - -- Ok now get one of them - if (max > 0) then - local ret = rand_int(max) - - player_lose_corruption(pos[ret]) - cmsg_print(TERM_L_RED, __corruptions[pos[ret]].lose_text) - - -- Ok now lets see if it broke some dependancies - for i = 0, max - 1 do - if player.corruption(pos[i]) ~= test_depend_corrupt(pos[i]) then - player_lose_corruption(pos[i]) - cmsg_print(TERM_L_RED, __corruptions[pos[i]].lose_text) - end - end - - return pos[ret] - else - return -1 - end -end - --- Creates a new corruption -function add_corruption(c) - assert(c.color, "No corruption color") - assert(c.name, "No corruption name") - assert(c.get_text, "No corruption get_text") - assert(c.lose_text, "No corruption lose_text") - assert(c.desc, "No corruption desc") - if not c.random then c.random = TRUE end - if not c.removable then c.removable = TRUE end - if not c.allow then c.allow = function() return not nil end end - - if c.depends == nil then c.depends = {} end - if c.oppose == nil then c.oppose = {} end - - -- We must make sure the other ones opposes too - local o, i - for o, i in c.oppose do - __corruptions[o].oppose[__corruptions_max] = TRUE - end - - if type(c.desc) == "table" then - local new_desc = "" - local index, h - for index, h in c.desc do - new_desc = new_desc..h.."\n" - end - c.desc = new_desc - end - - __corruptions[__corruptions_max] = c - __corruptions_max = __corruptions_max + 1 - return (__corruptions_max - 1) -end - -- cgit v1.2.3