Credits:
Nahruto
Remere for his function
How to use
Add this to the end of your global.lua file.
talkactions.xml
/scripts/trade.lua
Nahruto
Remere for his function
How to use
- To turn your trade system on simply say ' !trade system "on '
- To invite someone to a trade simply say ' !invite "PLAYERNAME '
- After that, the player you invited will receive a pop-up message saying "xxxx wants to trade with you"
- Now you can begin trading.
- But an item in your right hand then say ' !trade "TradePrice '
- Then you say ' !trade accept '
Add this to the end of your global.lua file.
Code:
_warpzone = 2147483648 -- start storing strings here (THIS IS THE ABSOLUTE MAXIMUM VALUE FOR THIS)
_maxlength = 1024 -- multiply by 3 to get the true length.
setPlayerStorageInteger = setPlayerStorageValue
getPlayerStorageInteger = getPlayerStorageValue
function setPlayerStorageString(cid, key, value)
if #value > (_maxlength-1) * 3 - 1 then -- Last word is reserved for 0 termination of the string.
error("Storage string is too long")
end
if key > _warpzone / _maxlength then
error("Storage string key is too large (" .. key .. ")")
end
key = _warpzone + key * _maxlength
local word = 0
local wordwrap = 0
local wordcount = 0
local i = 1
while i <= #value do
local byte = string.byte(string.sub(value, i, i))
word = bit.bor(word, bit.lshift(byte, wordwrap))
wordwrap = wordwrap + 8
if wordwrap == 24 then
--[[
In the ideal world we would be able to store 4 characters per word,
however, as the default return value for getPlayerStorageValue is
-1, we can't use the last bit.
]]--
setPlayerStorageInteger(cid, key + wordcount, word)
word = 0
wordwrap = 0
wordcount = wordcount + 1
end
i = i + 1
end
-- store the last word
setPlayerStorageInteger(cid, key + wordcount, word)
end
function getPlayerStorageString(cid, key)
if key > _warpzone / _maxlength then
error("Storage string key is too large (" .. key .. ")")
end
key = _warpzone + key * _maxlength
local wordcount = 0
local str = ""
while true do
if wordcount >= _maxlength then
break
end
local word = getPlayerStorageInteger(cid, key + wordcount)
if word == -1 then
-- end of string
break
else
-- Extract the 3 characters from the value
byte = bit.band(word, 255)
if byte == 0 then break else str = str .. string.char(byte) end
byte = bit.rshift(bit.band(word, 65280), 8)
if byte == 0 then break else str = str .. string.char(byte) end
byte = bit.rshift(bit.band(word, 16711680), 16)
if byte == 0 then break else str = str .. string.char(byte) end
end
wordcount = wordcount + 1
end
return str
end
talkactions.xml
Code:
<talkaction words="!trade" script="trade.lua"/>
<talkaction words="!invite" script="trade.lua"/>
<talkaction words="!trade system" script="trade.lua"/>
<talkaction words="!accept" script="trade.lua"/>
<talkaction words="!trade accept" script="trade.lua"/>
/scripts/trade.lua
Code:
local SLOT = 5 --5 left hand, or 6 right hand.
local TradeON = 35210
local invited = 35211
local Trading = 35212
local Accepted = 35213
local offered = 35214
local TrItem = {id=35215, count=35216, prize=35217}
local function ResetStorages(cid, t)
for i = 1, #t do
setPlayerStorageValue(cid, t[i], 0)
end
end
function onSay(cid, words, param)
if words == "!trade system" then
if param == "on" then
if getPlayerStorageValue(cid, TradeON) ~= 1 then
setPlayerStorageValue(cid, TradeON, 1)
doPlayerSendTextMessage(cid,22,"Trade system is now on.")
else
doPlayerSendCancel(cid, "Your trade system is already on.")
end
elseif param == "off" then
if getPlayerStorageValue(cid, TradeON) == 1 then
ResetStorages(cid, {TradeON, invited, Accepted, TrItem.id, TrItem.count, TrItem.prize, offered})
setPlayerStorageString(cid, Trading, "NOTHING LoL")
doPlayerSendTextMessage(cid,22,"Trade system is now off.")
else
doPlayerSendCancel(cid, "Your trade system is already off.")
end
end
elseif words == "!invite" then
if getPlayerStorageValue(cid, TradeON) == 1 then
if param ~= "" then
local TradingDude = getPlayerByName(param)
if getPlayerStorageValue(TradingDude, TradeON) == 1 then
doPlayerPopupFYI(TradingDude, ""..getPlayerName(cid).." wants to trade with you.")
setPlayerStorageValue(cid, invited, 1)
setPlayerStorageValue(TradingDude, invited, 1)
setPlayerStorageString(cid, Trading, getPlayerName(TradingDude))
setPlayerStorageString(TradingDude, Trading, getPlayerName(cid))
else
doPlayerSendCancel(cid, "This player is not trading.")
end
else
doPlayerSendCancel(cid, "You should write a player name.")
end
else
doPlayerSendCancel(cid, "Your trade system is off.")
end
elseif words == "!accept" then
if getPlayerStorageValue(cid, invited) == 1 then
local DudeName = getPlayerStorageString(cid, Trading)
local Dude = getPlayerByName(DudeName)
setPlayerStorageValue(cid, Accepted, 1)
setPlayerStorageValue(Dude, Accepted, 1)
doPlayerSendTextMessage(cid,22,"You have accepted the request to trade with "..DudeName..".")
doPlayerSendTextMessage(Dude,22,""..getPlayerName( cid).." accepted your request to trade.")
end
elseif words == "!trade" then
if param ~= "" then
local TradeItem = getPlayerSlotItem(cid, SLOT)
if TradeItem.itemid > 0 then
local prize = tonumber(param)
local DudeName = getPlayerStorageString(cid, Trading)
local Dude = getPlayerByName(DudeName)
if TradeItem.type == 0 then
TradeItem.type = 1
end
doPlayerSendTextMessage(Dude,22,""..getPlayerName( cid).." wants to trade "..TradeItem.type.." "..getItemName(TradeItem.itemid).." for "..prize..".")
setPlayerStorageValue(Dude, TrItem.id, TradeItem.itemid)
setPlayerStorageValue(Dude, TrItem.count, TradeItem.type)
setPlayerStorageValue(Dude, TrItem.prize, prize)
setPlayerStorageValue(Dude, offered, 1)
setPlayerStorageValue(cid, offered, 1)
else
doPlayerSendCancel(cid, "Put the item to trade in your hand.")
end
else
doPlayerSendCancel(cid, "You should write a prize.")
end
elseif words == "!trade accept" then
local DudeName = getPlayerStorageString(cid, Trading)
local Dude = getPlayerByName(DudeName)
if getPlayerStorageValue(cid, offered) == 1 and getPlayerStorageValue(Dude, offered) == 1 then
local Item_ = getPlayerStorageValue(cid, TrItem.id)
local Count_ = getPlayerStorageValue(cid, TrItem.count)
local Prize_ = getPlayerStorageValue(cid, TrItem.prize)
if getPlayerMoney(cid) >= Prize_ then
if doPlayerRemoveItem(Dude, Item_, Count_) == TRUE then
doPlayerRemoveMoney(cid, Prize_)
doPlayerAddMoney(Dude, Prize_)
doPlayerAddItem(cid, Item_, Count_)
doPlayerSendTextMessage(cid,22,"Trade completed.")
doPlayerSendTextMessage(Dude,22,"Trade completed.")
ResetStorages(cid, {TrItem.id, TrItem.count, TrItem.prize, offered})
ResetStorages(Dude, {TrItem.id, TrItem.count, TrItem.prize, offered})
end
else
doPlayerSendCancel(cid, "You dont have enough money.")
end
else
doPlayerSendCancel(cid, "The player is not trading.")
end
end
end
function getPlayerMoney(cid)
return getPlayerItemCount(cid,2148) + (getPlayerItemCount(cid,2152)*100) + (getPlayerItemCount(cid,2160)*10000)
end