From 1c227d3b6697a96a1fa61484c0349f212912c460 Mon Sep 17 00:00:00 2001 From: Ranieri Althoff Date: Fri, 10 Feb 2017 01:37:13 -0200 Subject: [PATCH] Move !sellhouse to talkactions --- data/XML/commands.xml | 1 - data/talkactions/scripts/sellhouse.lua | 19 ++++++++ data/talkactions/talkactions.xml | 1 + src/commands.cpp | 62 +------------------------- src/enums.h | 5 +++ src/luascript.cpp | 53 ++++++++++++++++++++++ src/luascript.h | 1 + src/tools.cpp | 15 +++++++ 8 files changed, 95 insertions(+), 62 deletions(-) create mode 100644 data/talkactions/scripts/sellhouse.lua diff --git a/data/XML/commands.xml b/data/XML/commands.xml index 12257fcf1e..bebac04e0c 100644 --- a/data/XML/commands.xml +++ b/data/XML/commands.xml @@ -1,4 +1,3 @@ - diff --git a/data/talkactions/scripts/sellhouse.lua b/data/talkactions/scripts/sellhouse.lua new file mode 100644 index 0000000000..525d71c529 --- /dev/null +++ b/data/talkactions/scripts/sellhouse.lua @@ -0,0 +1,19 @@ +function onSay(player, words, param) + local tradePartner = Player(param) + if not tradePartner or tradePartner == player then + player:sendCancelMessage("Trade player not found.") + return false + end + + local house = player:getTile():getHouse() + if not house then + player:sendCancelMessage("You must stand in your house to initiate the trade.") + return false + end + + local returnValue = house:startTrade(player, tradePartner) + if returnValue ~= RETURNVALUE_NOERROR then + player:sendCancelMessage(returnValue) + end + return false +end diff --git a/data/talkactions/talkactions.xml b/data/talkactions/talkactions.xml index 802a536d6a..ede68bb406 100644 --- a/data/talkactions/talkactions.xml +++ b/data/talkactions/talkactions.xml @@ -38,6 +38,7 @@ + diff --git a/src/commands.cpp b/src/commands.cpp index f6861d3402..6b6bec5b79 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -24,8 +24,6 @@ #include "commands.h" #include "player.h" #include "game.h" -#include "iologindata.h" -#include "scheduler.h" #include "pugicast.h" @@ -33,65 +31,7 @@ extern Game g_game; namespace { -void sellHouse(Player& player, const std::string& param) -{ - Player* tradePartner = g_game.getPlayerByName(param); - if (!tradePartner || tradePartner == &player) { - player.sendCancelMessage("Trade player not found."); - return; - } - - if (!Position::areInRange<2, 2, 0>(tradePartner->getPosition(), player.getPosition())) { - player.sendCancelMessage("Trade player is too far away."); - return; - } - - if (!tradePartner->isPremium()) { - player.sendCancelMessage("Trade player does not have a premium account."); - return; - } - - HouseTile* houseTile = dynamic_cast(player.getTile()); - if (!houseTile) { - player.sendCancelMessage("You must stand in your house to initiate the trade."); - return; - } - - House* house = houseTile->getHouse(); - if (!house || house->getOwner() != player.getGUID()) { - player.sendCancelMessage("You don't own this house."); - return; - } - - if (g_game.map.houses.getHouseByPlayerId(tradePartner->getGUID())) { - player.sendCancelMessage("Trade player already owns a house."); - return; - } - - if (IOLoginData::hasBiddedOnHouse(tradePartner->getGUID())) { - player.sendCancelMessage("Trade player is currently the highest bidder of an auctioned house."); - return; - } - - Item* transferItem = house->getTransferItem(); - if (!transferItem) { - player.sendCancelMessage("You can not trade this house."); - return; - } - - transferItem->getParent()->setParent(&player); - - if (!g_game.internalStartTrade(&player, tradePartner, transferItem)) { - house->resetTransferItem(); - } -} - -std::map defined_commands = { - // TODO: move all commands to talkactions - - // player commands - {"!sellhouse", sellHouse} -}; +std::map defined_commands = {}; } diff --git a/src/enums.h b/src/enums.h index c32db1d414..bbb06312b5 100644 --- a/src/enums.h +++ b/src/enums.h @@ -391,6 +391,11 @@ enum ReturnValue { RETURNVALUE_YOUARENOTTHEOWNER, RETURNVALUE_NOSUCHRAIDEXISTS, RETURNVALUE_ANOTHERRAIDISALREADYEXECUTING, + RETURNVALUE_TRADEPLAYERFARAWAY, + RETURNVALUE_YOUDONTOWNTHISHOUSE, + RETURNVALUE_TRADEPLAYERALREADYOWNSAHOUSE, + RETURNVALUE_TRADEPLAYERHIGHESTBIDDER, + RETURNVALUE_YOUCANNOTTRADETHISHOUSE, }; enum SpeechBubble_t diff --git a/src/luascript.cpp b/src/luascript.cpp index ba9fd26fb6..7160120d3c 100644 --- a/src/luascript.cpp +++ b/src/luascript.cpp @@ -1712,6 +1712,11 @@ void LuaScriptInterface::registerFunctions() registerEnum(RETURNVALUE_CANONLYUSEONESHIELD) registerEnum(RETURNVALUE_NOPARTYMEMBERSINRANGE) registerEnum(RETURNVALUE_YOUARENOTTHEOWNER) + registerEnum(RETURNVALUE_TRADEPLAYERFARAWAY) + registerEnum(RETURNVALUE_YOUDONTOWNTHISHOUSE) + registerEnum(RETURNVALUE_TRADEPLAYERALREADYOWNSAHOUSE) + registerEnum(RETURNVALUE_TRADEPLAYERHIGHESTBIDDER) + registerEnum(RETURNVALUE_YOUCANNOTTRADETHISHOUSE) registerEnum(RELOAD_TYPE_ALL) registerEnum(RELOAD_TYPE_ACTIONS) @@ -2398,6 +2403,7 @@ void LuaScriptInterface::registerFunctions() registerMethod("House", "getOwnerGuid", LuaScriptInterface::luaHouseGetOwnerGuid); registerMethod("House", "setOwnerGuid", LuaScriptInterface::luaHouseSetOwnerGuid); + registerMethod("House", "startTrade", LuaScriptInterface::luaHouseStartTrade); registerMethod("House", "getBeds", LuaScriptInterface::luaHouseGetBeds); registerMethod("House", "getBedCount", LuaScriptInterface::luaHouseGetBedCount); @@ -10355,6 +10361,53 @@ int LuaScriptInterface::luaHouseSetOwnerGuid(lua_State* L) return 1; } +int LuaScriptInterface::luaHouseStartTrade(lua_State* L) +{ + // house:startTrade(player, tradePartner) + House* house = getUserdata(L, 1); + Player* player = getUserdata(L, 2); + Player* tradePartner = getUserdata(L, 3); + + if (!player || !tradePartner || !house) { + lua_pushnil(L); + return 1; + } + + if (!Position::areInRange<2, 2, 0>(tradePartner->getPosition(), player->getPosition())) { + lua_pushnumber(L, RETURNVALUE_TRADEPLAYERFARAWAY); + return 1; + } + + if (house->getOwner() != player->getGUID()) { + lua_pushnumber(L, RETURNVALUE_YOUDONTOWNTHISHOUSE); + return 1; + } + + if (g_game.map.houses.getHouseByPlayerId(tradePartner->getGUID())) { + lua_pushnumber(L, RETURNVALUE_TRADEPLAYERALREADYOWNSAHOUSE); + return 1; + } + + if (IOLoginData::hasBiddedOnHouse(tradePartner->getGUID())) { + lua_pushnumber(L, RETURNVALUE_TRADEPLAYERHIGHESTBIDDER); + return 1; + } + + Item* transferItem = house->getTransferItem(); + if (!transferItem) { + lua_pushnumber(L, RETURNVALUE_YOUCANNOTTRADETHISHOUSE); + return 1; + } + + transferItem->getParent()->setParent(player); + if (!g_game.internalStartTrade(player, tradePartner, transferItem)) { + house->resetTransferItem(); + } + + lua_pushnumber(L, RETURNVALUE_NOERROR); + return 1; +} + int LuaScriptInterface::luaHouseGetBeds(lua_State* L) { // house:getBeds() diff --git a/src/luascript.h b/src/luascript.h index 26b6e6d294..c511934d7b 100644 --- a/src/luascript.h +++ b/src/luascript.h @@ -1076,6 +1076,7 @@ class LuaScriptInterface static int luaHouseGetOwnerGuid(lua_State* L); static int luaHouseSetOwnerGuid(lua_State* L); + static int luaHouseStartTrade(lua_State* L); static int luaHouseGetBeds(lua_State* L); static int luaHouseGetBedCount(lua_State* L); diff --git a/src/tools.cpp b/src/tools.cpp index 7b76161e90..e142f43e24 100644 --- a/src/tools.cpp +++ b/src/tools.cpp @@ -1173,6 +1173,21 @@ const char* getReturnMessage(ReturnValue value) case RETURNVALUE_ANOTHERRAIDISALREADYEXECUTING: return "Another raid is already executing."; + case RETURNVALUE_TRADEPLAYERFARAWAY: + return "Trade player is too far away."; + + case RETURNVALUE_YOUDONTOWNTHISHOUSE: + return "You don't own this house."; + + case RETURNVALUE_TRADEPLAYERALREADYOWNSAHOUSE: + return "Trade player already owns a house."; + + case RETURNVALUE_TRADEPLAYERHIGHESTBIDDER: + return "Trade player is currently the highest bidder of an auctioned house."; + + case RETURNVALUE_YOUCANNOTTRADETHISHOUSE: + return "You can not trade this house."; + default: // RETURNVALUE_NOTPOSSIBLE, etc return "Sorry, not possible."; }