Skip to content

Commit

Permalink
Move !sellhouse to talkactions
Browse files Browse the repository at this point in the history
  • Loading branch information
ranisalt committed Feb 10, 2017
1 parent 1b94394 commit 1c227d3
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 62 deletions.
1 change: 0 additions & 1 deletion data/XML/commands.xml
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<commands>
<command cmd="!sellhouse" group="1" acctype="1" log="no" />
</commands>
19 changes: 19 additions & 0 deletions 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
1 change: 1 addition & 0 deletions data/talkactions/talkactions.xml
Expand Up @@ -38,6 +38,7 @@
<!-- player talkactions -->
<talkaction words="!buypremium" script="buyprem.lua" />
<talkaction words="!buyhouse" script="buyhouse.lua" />
<talkaction words="!sellhouse" separator=" " script="sellhouse.lua" />
<talkaction words="!leavehouse" script="leavehouse.lua" />
<talkaction words="!changesex" script="changesex.lua" />
<talkaction words="!uptime" script="uptime.lua" />
Expand Down
62 changes: 1 addition & 61 deletions src/commands.cpp
Expand Up @@ -24,74 +24,14 @@
#include "commands.h"
#include "player.h"
#include "game.h"
#include "iologindata.h"
#include "scheduler.h"

#include "pugicast.h"

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<HouseTile*>(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<std::string, CommandFunction> defined_commands = {
// TODO: move all commands to talkactions

// player commands
{"!sellhouse", sellHouse}
};
std::map<std::string, CommandFunction> defined_commands = {};

}

Expand Down
5 changes: 5 additions & 0 deletions src/enums.h
Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions src/luascript.cpp
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<House>(L, 1);
Player* player = getUserdata<Player>(L, 2);
Player* tradePartner = getUserdata<Player>(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()
Expand Down
1 change: 1 addition & 0 deletions src/luascript.h
Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions src/tools.cpp
Expand Up @@ -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.";
}
Expand Down

0 comments on commit 1c227d3

Please sign in to comment.