#include #include #include "script.h" // kernel includes #include #include #include // lua includes #include #include #include using namespace luabind; static int lua_useitem(struct unit * u, const struct item_type * itype, int amount, struct order *ord) { char fname[64]; int retval = -1; const char * iname = itype->rtype->_name[0]; assert(u!=NULL); strcat(strcpy(fname, iname), "_use"); lua_State * L = (lua_State *)global.vm_state; if (is_function(L, fname)) { try { retval = luabind::call_function(L, fname, u, amount); } catch (luabind::error& e) { lua_State* L = e.state(); const char* error = lua_tostring(L, -1); log_error(("An exception occured while %s tried to call '%s': %s.\n", unitname(u), fname, error)); lua_pop(L, 1); std::terminate(); } } return retval; } static void item_register(const char * name, const char * appearance) { resource_type * rtype = (resource_type *)calloc(1, sizeof(resource_type)); item_type * itype = (item_type *)calloc(1, sizeof(item_type)); rtype->_name[0] = strdup(name); rtype->_name[1] = strcat(strcpy((char*)malloc(strlen(name)+3), name), "_p"); rtype->_appearance[0] = strdup(appearance); rtype->_appearance[1] = strcat(strcpy((char*)malloc(strlen(appearance)+3), appearance), "_p"); itype->use = lua_useitem; itype->rtype = rtype; rt_register(rtype); it_register(itype); init_itemnames(); } void bind_item(lua_State * L) { module(L)[ def("register_item", &item_register) ]; }