From c6df756b86ae4e28bc342dffa6ab41a7c865fa9a Mon Sep 17 00:00:00 2001 From: Robert Vollmert Date: Tue, 20 Oct 2009 22:05:19 +0200 Subject: Move remaining libraries from clua.cc. That's l_file.cc, l_food.cc, l_global.cc. --- crawl-ref/source/clua.cc | 234 +----------------------------------------- crawl-ref/source/dlua.cc | 3 +- crawl-ref/source/l_file.cc | 24 ++++- crawl-ref/source/l_food.cc | 174 +++++++++++++++++++++++++++++++ crawl-ref/source/l_global.cc | 50 +++++++++ crawl-ref/source/l_libs.h | 6 +- crawl-ref/source/makefile.obj | 2 + 7 files changed, 258 insertions(+), 235 deletions(-) create mode 100644 crawl-ref/source/l_food.cc create mode 100644 crawl-ref/source/l_global.cc (limited to 'crawl-ref') diff --git a/crawl-ref/source/clua.cc b/crawl-ref/source/clua.cc index e05a378af4..02f4a6c69e 100644 --- a/crawl-ref/source/clua.cc +++ b/crawl-ref/source/clua.cc @@ -591,10 +591,6 @@ bool CLua::callfn(const char *fn, int nargs, int nret) // structs extern void luaopen_kills(lua_State *ls); -void luaopen_food(lua_State *ls); -void luaopen_file(lua_State *ls); -void luaopen_globals(lua_State *ls); - void CLua::init_lua() { if (_state) @@ -617,13 +613,13 @@ void CLua::init_lua() luaopen_kills(_state); cluaopen_you(_state); cluaopen_item(_state); - luaopen_food(_state); + cluaopen_food(_state); cluaopen_crawl(_state); - luaopen_file(_state); + cluaopen_file(_state); cluaopen_options(_state); cluaopen_monsters(_state); - luaopen_globals(_state); + cluaopen_globals(_state); load_cmacro(); load_chooks(); @@ -721,185 +717,6 @@ void clua_register_metatable(lua_State *ls, const char *tn, } } -///////////////////////////////////////////////////////////////////// -// Food information. - -static int food_do_eat(lua_State *ls) -{ - bool eaten = false; - if (!you.turn_is_over) - eaten = eat_food(-1); - lua_pushboolean(ls, eaten); - return (1); -} - -static int food_prompt_eat_chunks(lua_State *ls) -{ - int eaten = 0; - if (!you.turn_is_over) - eaten = prompt_eat_chunks(); - - lua_pushboolean(ls, (eaten != 0)); - return (1); -} - -static int food_prompt_floor(lua_State *ls) -{ - int eaten = 0; - if (!you.turn_is_over) - { - eaten = eat_from_floor(); - if (eaten == 1) - burden_change(); - } - lua_pushboolean(ls, (eaten != 0)); - return (1); -} - -static int food_prompt_inventory(lua_State *ls) -{ - bool eaten = false; - if (!you.turn_is_over) - eaten = eat_from_inventory(); - lua_pushboolean(ls, eaten); - return (1); -} - -static int food_prompt_inventory_menu(lua_State *ls) -{ - bool eaten = false; - if (!you.turn_is_over) - eaten = prompt_eat_inventory_item(); - lua_pushboolean(ls, eaten); - return (1); -} - -static int food_can_eat(lua_State *ls) -{ - LUA_ITEM(item, 1); - bool hungercheck = true; - - if (lua_isboolean(ls, 2)) - hungercheck = lua_toboolean(ls, 2); - - bool edible = item && can_ingest(item->base_type, - item->sub_type, - true, - true, - hungercheck); - lua_pushboolean(ls, edible); - return (1); -} - -static bool eat_item(const item_def &item) -{ - if (in_inventory(item)) - { - eat_inventory_item(item.link); - return (true); - } - else - { - int ilink = item_on_floor(item, you.pos()); - - if (ilink != NON_ITEM) - { - eat_floor_item(ilink); - return (true); - } - return (false); - } -} - -static int food_eat(lua_State *ls) -{ - LUA_ITEM(item, 1); - - bool eaten = false; - if (!you.turn_is_over) - { - // When we get down to eating, we don't care if the eating is courtesy - // an un-ided amulet of the gourmand. - bool edible = item && can_ingest(item->base_type, - item->sub_type, - false, - false); - if (edible) - eaten = eat_item(*item); - } - lua_pushboolean(ls, eaten); - return (1); -} - -static int food_rotting(lua_State *ls) -{ - LUA_ITEM(item, 1); - - bool rotting = false; - if (item && item->base_type == OBJ_FOOD && item->sub_type == FOOD_CHUNK) - rotting = food_is_rotten(*item); - - lua_pushboolean(ls, rotting); - return (1); -} - -static int food_dangerous(lua_State *ls) -{ - LUA_ITEM(item, 1); - - bool dangerous = false; - if (item) - { - dangerous = (is_poisonous(*item) || is_mutagenic(*item) - || causes_rot(*item) || is_forbidden_food(*item)); - } - lua_pushboolean(ls, dangerous); - return (1); -} - -static int food_ischunk(lua_State *ls) -{ - LUA_ITEM(item, 1); - lua_pushboolean(ls, - item && item->base_type == OBJ_FOOD - && item->sub_type == FOOD_CHUNK); - return (1); -} - -static const struct luaL_reg food_lib[] = -{ - { "do_eat", food_do_eat }, - { "prompt_eat_chunks", food_prompt_eat_chunks }, - { "prompt_floor", food_prompt_floor }, - { "prompt_inventory", food_prompt_inventory }, - { "prompt_inv_menu", food_prompt_inventory_menu }, - { "can_eat", food_can_eat }, - { "eat", food_eat }, - { "rotting", food_rotting }, - { "dangerous", food_dangerous }, - { "ischunk", food_ischunk }, - { NULL, NULL }, -}; - -void luaopen_food(lua_State *ls) -{ - luaL_openlib(ls, "food", food_lib, 0); -} - - -/////////////////////////////////////////////////////////// -// File operations - -static const struct luaL_reg file_lib[] = -{ - { "write", CLua::file_write }, - { NULL, NULL }, -}; - -void luaopen_file(lua_State *ls) -{ - luaL_openlib(ls, "file", file_lib, 0); -} // Pushing various objects. @@ -958,51 +775,6 @@ void clua_push_dgn_event(lua_State *ls, const dgn_event *devent) } -////////////////////////////////////////////////////////////////////// -// Miscellaneous globals - -#define PATTERN_FLUSH_CEILING 100 - -typedef std::map pattern_map; -static pattern_map pattern_cache; - -static text_pattern &get_text_pattern(const std::string &s, bool checkcase) -{ - pattern_map::iterator i = pattern_cache.find(s); - if (i != pattern_cache.end()) - return i->second; - - if (pattern_cache.size() > PATTERN_FLUSH_CEILING) - pattern_cache.clear(); - - pattern_cache[s] = text_pattern(s, !checkcase); - return (pattern_cache[s]); -} - -static int lua_pmatch(lua_State *ls) -{ - const char *pattern = luaL_checkstring(ls, 1); - if (!pattern) - return (0); - - const char *text = luaL_checkstring(ls, 2); - if (!text) - return (0); - - bool checkcase = true; - if (lua_isboolean(ls, 3)) - checkcase = lua_toboolean(ls, 3); - - text_pattern &tp = get_text_pattern(pattern, checkcase); - lua_pushboolean( ls, tp.matches(text) ); - return (1); -} - -void luaopen_globals(lua_State *ls) -{ - lua_pushcfunction(ls, lua_pmatch); - lua_setglobal(ls, "pmatch"); -} //////////////////////////////////////////////////////////////////////// // lua_text_pattern diff --git a/crawl-ref/source/dlua.cc b/crawl-ref/source/dlua.cc index dc800406ca..a49f2e9fd6 100644 --- a/crawl-ref/source/dlua.cc +++ b/crawl-ref/source/dlua.cc @@ -307,6 +307,7 @@ void init_dungeon_lua() lua_stack_cleaner clean(dlua); dluaopen_crawl(dlua); + dluaopen_file(dlua); dluaopen_mapgrd(dlua); dluaopen_you(dlua); @@ -318,9 +319,7 @@ void init_dungeon_lua() luaL_openlib(dlua, "dgn", dgn_level_dlib, 0); luaL_openlib(dlua, "dgn", dgn_mons_dlib, 0); luaL_openlib(dlua, "dgn", dgn_tile_dlib, 0); - luaL_openlib(dlua, "debug", debug_dlib, 0); - luaL_openlib(dlua, "file", file_dlib, 0); luaL_openlib(dlua, "los", los_dlib, 0); dlua.execfile("clua/dungeon.lua", true, true); diff --git a/crawl-ref/source/l_file.cc b/crawl-ref/source/l_file.cc index 78eb10f289..c44e90e44a 100644 --- a/crawl-ref/source/l_file.cc +++ b/crawl-ref/source/l_file.cc @@ -1,10 +1,28 @@ #include "AppHdr.h" +#include "clua.h" #include "dlua.h" #include "l_libs.h" #include "tags.h" +/////////////////////////////////////////////////////////// +// User-accessible file operations + +static const struct luaL_reg file_clib[] = +{ + { "write", CLua::file_write }, + { NULL, NULL }, +}; + +void cluaopen_file(lua_State *ls) +{ + luaL_openlib(ls, "file", file_clib, 0); +} + +/////////////////////////////////////////////////////////// +// Non-user-accessible file operations + static int file_marshall(lua_State *ls) { if (lua_gettop(ls) != 2) @@ -126,7 +144,7 @@ static int file_unmarshall_meta(lua_State *ls) return (0); } -const struct luaL_reg file_dlib[] = +static const struct luaL_reg file_dlib[] = { { "marshall", file_marshall }, { "marshall_meta", file_marshall_meta }, @@ -137,3 +155,7 @@ const struct luaL_reg file_dlib[] = { NULL, NULL } }; +void dluaopen_file(lua_State *ls) +{ + luaL_openlib(ls, "file", file_dlib, 0); +} diff --git a/crawl-ref/source/l_food.cc b/crawl-ref/source/l_food.cc new file mode 100644 index 0000000000..0855b4bad0 --- /dev/null +++ b/crawl-ref/source/l_food.cc @@ -0,0 +1,174 @@ +#include "AppHdr.h" + +#include "clua.h" +#include "l_libs.h" + +#include "food.h" +#include "invent.h" +#include "items.h" +#include "player.h" + +///////////////////////////////////////////////////////////////////// +// Food information. + +static int food_do_eat(lua_State *ls) +{ + bool eaten = false; + if (!you.turn_is_over) + eaten = eat_food(-1); + lua_pushboolean(ls, eaten); + return (1); +} + +static int food_prompt_eat_chunks(lua_State *ls) +{ + int eaten = 0; + if (!you.turn_is_over) + eaten = prompt_eat_chunks(); + + lua_pushboolean(ls, (eaten != 0)); + return (1); +} + +static int food_prompt_floor(lua_State *ls) +{ + int eaten = 0; + if (!you.turn_is_over) + { + eaten = eat_from_floor(); + if (eaten == 1) + burden_change(); + } + lua_pushboolean(ls, (eaten != 0)); + return (1); +} + +static int food_prompt_inventory(lua_State *ls) +{ + bool eaten = false; + if (!you.turn_is_over) + eaten = eat_from_inventory(); + lua_pushboolean(ls, eaten); + return (1); +} + +static int food_prompt_inventory_menu(lua_State *ls) +{ + bool eaten = false; + if (!you.turn_is_over) + eaten = prompt_eat_inventory_item(); + lua_pushboolean(ls, eaten); + return (1); +} + +static int food_can_eat(lua_State *ls) +{ + LUA_ITEM(item, 1); + bool hungercheck = true; + + if (lua_isboolean(ls, 2)) + hungercheck = lua_toboolean(ls, 2); + + bool edible = item && can_ingest(item->base_type, + item->sub_type, + true, + true, + hungercheck); + lua_pushboolean(ls, edible); + return (1); +} + +static bool eat_item(const item_def &item) +{ + if (in_inventory(item)) + { + eat_inventory_item(item.link); + return (true); + } + else + { + int ilink = item_on_floor(item, you.pos()); + + if (ilink != NON_ITEM) + { + eat_floor_item(ilink); + return (true); + } + return (false); + } +} + +static int food_eat(lua_State *ls) +{ + LUA_ITEM(item, 1); + + bool eaten = false; + if (!you.turn_is_over) + { + // When we get down to eating, we don't care if the eating is courtesy + // an un-ided amulet of the gourmand. + bool edible = item && can_ingest(item->base_type, + item->sub_type, + false, + false); + if (edible) + eaten = eat_item(*item); + } + lua_pushboolean(ls, eaten); + return (1); +} + +static int food_rotting(lua_State *ls) +{ + LUA_ITEM(item, 1); + + bool rotting = false; + if (item && item->base_type == OBJ_FOOD && item->sub_type == FOOD_CHUNK) + rotting = food_is_rotten(*item); + + lua_pushboolean(ls, rotting); + return (1); +} + +static int food_dangerous(lua_State *ls) +{ + LUA_ITEM(item, 1); + + bool dangerous = false; + if (item) + { + dangerous = (is_poisonous(*item) || is_mutagenic(*item) + || causes_rot(*item) || is_forbidden_food(*item)); + } + lua_pushboolean(ls, dangerous); + return (1); +} + +static int food_ischunk(lua_State *ls) +{ + LUA_ITEM(item, 1); + lua_pushboolean(ls, + item && item->base_type == OBJ_FOOD + && item->sub_type == FOOD_CHUNK); + return (1); +} + +static const struct luaL_reg food_lib[] = +{ + { "do_eat", food_do_eat }, + { "prompt_eat_chunks", food_prompt_eat_chunks }, + { "prompt_floor", food_prompt_floor }, + { "prompt_inventory", food_prompt_inventory }, + { "prompt_inv_menu", food_prompt_inventory_menu }, + { "can_eat", food_can_eat }, + { "eat", food_eat }, + { "rotting", food_rotting }, + { "dangerous", food_dangerous }, + { "ischunk", food_ischunk }, + { NULL, NULL }, +}; + +void cluaopen_food(lua_State *ls) +{ + luaL_openlib(ls, "food", food_lib, 0); +} diff --git a/crawl-ref/source/l_global.cc b/crawl-ref/source/l_global.cc new file mode 100644 index 0000000000..5aa50f7df3 --- /dev/null +++ b/crawl-ref/source/l_global.cc @@ -0,0 +1,50 @@ +#include "AppHdr.h" + +#include "clua.h" +#include "l_libs.h" + +////////////////////////////////////////////////////////////////////// +// Miscellaneous globals + +#define PATTERN_FLUSH_CEILING 100 + +typedef std::map pattern_map; +static pattern_map pattern_cache; + +static text_pattern &get_text_pattern(const std::string &s, bool checkcase) +{ + pattern_map::iterator i = pattern_cache.find(s); + if (i != pattern_cache.end()) + return i->second; + + if (pattern_cache.size() > PATTERN_FLUSH_CEILING) + pattern_cache.clear(); + + pattern_cache[s] = text_pattern(s, !checkcase); + return (pattern_cache[s]); +} + +static int lua_pmatch(lua_State *ls) +{ + const char *pattern = luaL_checkstring(ls, 1); + if (!pattern) + return (0); + + const char *text = luaL_checkstring(ls, 2); + if (!text) + return (0); + + bool checkcase = true; + if (lua_isboolean(ls, 3)) + checkcase = lua_toboolean(ls, 3); + + text_pattern &tp = get_text_pattern(pattern, checkcase); + lua_pushboolean( ls, tp.matches(text) ); + return (1); +} + +void cluaopen_globals(lua_State *ls) +{ + lua_pushcfunction(ls, lua_pmatch); + lua_setglobal(ls, "pmatch"); +} diff --git a/crawl-ref/source/l_libs.h b/crawl-ref/source/l_libs.h index e54480317d..05c71dca4d 100644 --- a/crawl-ref/source/l_libs.h +++ b/crawl-ref/source/l_libs.h @@ -13,11 +13,15 @@ */ void cluaopen_crawl(lua_State *ls); +void cluaopen_file(lua_State *ls); +void cluaopen_food(lua_State *ls); void cluaopen_item(lua_State *ls); void cluaopen_monsters(lua_State *ls); void cluaopen_options(lua_State *ls); void cluaopen_you(lua_State *ls); +void cluaopen_globals(lua_State *ls); + /* * Libraries and loaders, accessed from init_dungeon_lua(). */ @@ -31,7 +35,6 @@ extern const struct luaL_reg dgn_item_dlib[]; extern const struct luaL_reg dgn_level_dlib[]; extern const struct luaL_reg dgn_mons_dlib[]; extern const struct luaL_reg dgn_tile_dlib[]; -extern const struct luaL_reg file_dlib[]; extern const struct luaL_reg los_dlib[]; extern const struct luaL_reg mapmarker_dlib[]; @@ -44,6 +47,7 @@ void register_itemlist(lua_State *ls); void register_builder_funcs(lua_State *ls); void dluaopen_crawl(lua_State *ls); +void dluaopen_file(lua_State *ls); void dluaopen_mapgrd(lua_State *ls); void dluaopen_you(lua_State *ls); diff --git a/crawl-ref/source/makefile.obj b/crawl-ref/source/makefile.obj index 48e47ef222..30c76c95af 100644 --- a/crawl-ref/source/makefile.obj +++ b/crawl-ref/source/makefile.obj @@ -50,6 +50,8 @@ l_dgnmon.o \ l_dgntil.o \ l_dgn_bf.o \ l_file.o \ +l_food.o \ +l_global.o \ l_item.o \ l_los.o \ l_mapgrd.o \ -- cgit v1.2.3-54-g00ecf