Skip to content

Commit

Permalink
Lua: Tile:addItem and Tile:addItemEx (otland#1944)
Browse files Browse the repository at this point in the history
doTileAddItemEx is now in compat.lua
  • Loading branch information
Mkalo authored and DSpeichert committed Jan 2, 2018
1 parent f7292a7 commit 76ffcee
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 36 deletions.
8 changes: 8 additions & 0 deletions data/lib/compat/compat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,14 @@ function doSetCreatureOutfit(cid, outfit, time)
return true
end

function doTileAddItemEx(pos, uid, flags)
local item = Item(uid)
if item then
return tile:addItemEx(item, flags)
end
return false
end

function isInArray(array, value) return table.contains(array, value) end

function doCreateItem(itemid, count, pos)
Expand Down
108 changes: 73 additions & 35 deletions src/luascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -929,9 +929,6 @@ void LuaScriptInterface::registerFunctions()
//Returns uid of the created item
lua_register(luaState, "doPlayerAddItem", LuaScriptInterface::luaDoPlayerAddItem);

//doTileAddItemEx(pos, uid)
lua_register(luaState, "doTileAddItemEx", LuaScriptInterface::luaDoTileAddItemEx);

//doSetCreatureLight(cid, lightLevel, lightColor, time)
lua_register(luaState, "doSetCreatureLight", LuaScriptInterface::luaDoSetCreatureLight);

Expand Down Expand Up @@ -1970,6 +1967,8 @@ void LuaScriptInterface::registerFunctions()
registerMethod("Tile", "hasFlag", LuaScriptInterface::luaTileHasFlag);

registerMethod("Tile", "queryAdd", LuaScriptInterface::luaTileQueryAdd);
registerMethod("Tile", "addItem", LuaScriptInterface::luaTileAddItem);
registerMethod("Tile", "addItemEx", LuaScriptInterface::luaTileAddItemEx);

registerMethod("Tile", "getHouse", LuaScriptInterface::luaTileGetHouse);

Expand Down Expand Up @@ -2879,38 +2878,6 @@ int LuaScriptInterface::luaDoPlayerAddItem(lua_State* L)
return 1;
}

int LuaScriptInterface::luaDoTileAddItemEx(lua_State* L)
{
//doTileAddItemEx(pos, uid)
const Position& pos = getPosition(L, 1);

Tile* tile = g_game.map.getTile(pos);
if (!tile) {
std::ostringstream ss;
ss << pos << ' ' << getErrorDesc(LUA_ERROR_TILE_NOT_FOUND);
reportErrorFunc(ss.str());
pushBoolean(L, false);
return 1;
}

uint32_t uid = getNumber<uint32_t>(L, 2);
Item* item = getScriptEnv()->getItemByUID(uid);
if (!item) {
reportErrorFunc(getErrorDesc(LUA_ERROR_ITEM_NOT_FOUND));
pushBoolean(L, false);
return 1;
}

if (item->getParent() != VirtualCylinder::virtualCylinder) {
reportErrorFunc("Item already has a parent");
pushBoolean(L, false);
return 1;
}

lua_pushnumber(L, g_game.internalAddItem(tile, item));
return 1;
}

int LuaScriptInterface::luaDebugPrint(lua_State* L)
{
//debugPrint(text)
Expand Down Expand Up @@ -5106,6 +5073,77 @@ int LuaScriptInterface::luaTileQueryAdd(lua_State* L)
return 1;
}

int LuaScriptInterface::luaTileAddItem(lua_State* L)
{
// tile:addItem(itemId[, count/subType = 1[, flags = 0]])
Tile* tile = getUserdata<Tile>(L, 1);
if (!tile) {
lua_pushnil(L);
return 1;
}

uint16_t itemId;
if (isNumber(L, 2)) {
itemId = getNumber<uint16_t>(L, 2);
} else {
itemId = Item::items.getItemIdByName(getString(L, 2));
if (itemId == 0) {
lua_pushnil(L);
return 1;
}
}

uint32_t subType = getNumber<uint32_t>(L, 3, 1);

Item* item = Item::CreateItem(itemId, std::min<uint32_t>(subType, 100));
if (!item) {
lua_pushnil(L);
return 1;
}

uint32_t flags = getNumber<uint32_t>(L, 4, 0);

ReturnValue ret = g_game.internalAddItem(tile, item, INDEX_WHEREEVER, flags);
if (ret == RETURNVALUE_NOERROR) {
pushUserdata<Item>(L, item);
setItemMetatable(L, -1, item);
} else {
delete item;
lua_pushnil(L);
}
return 1;
}

int LuaScriptInterface::luaTileAddItemEx(lua_State* L)
{
// tile:addItemEx(item[, flags = 0])
Item* item = getUserdata<Item>(L, 2);
if (!item) {
lua_pushnil(L);
return 1;
}

Tile* tile = getUserdata<Tile>(L, 1);
if (!tile) {
lua_pushnil(L);
return 1;
}

if (item->getParent() != VirtualCylinder::virtualCylinder) {
reportErrorFunc("Item already has a parent");
lua_pushnil(L);
return 1;
}

uint32_t flags = getNumber<uint32_t>(L, 3, 0);
ReturnValue ret = g_game.internalAddItem(tile, item, INDEX_WHEREEVER, flags);
if (ret == RETURNVALUE_NOERROR) {
ScriptEnvironment::removeTempItem(item);
}
lua_pushnumber(L, ret);
return 1;
}

int LuaScriptInterface::luaTileGetHouse(lua_State* L)
{
// tile:getHouse()
Expand Down
3 changes: 2 additions & 1 deletion src/luascript.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,6 @@ class LuaScriptInterface

//lua functions
static int luaDoPlayerAddItem(lua_State* L);
static int luaDoTileAddItemEx(lua_State* L);
static int luaDoSetCreatureLight(lua_State* L);

//get item info
Expand Down Expand Up @@ -612,6 +611,8 @@ class LuaScriptInterface
static int luaTileGetThingIndex(lua_State* L);

static int luaTileQueryAdd(lua_State* L);
static int luaTileAddItem(lua_State* L);
static int luaTileAddItemEx(lua_State* L);

static int luaTileGetHouse(lua_State* L);

Expand Down

0 comments on commit 76ffcee

Please sign in to comment.